Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/svtools/svparser.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#pragma once
21
22
#include <svtools/svtdllapi.h>
23
#include <tools/link.hxx>
24
#include <tools/ref.hxx>
25
#include <tools/long.hxx>
26
#include <rtl/textenc.h>
27
#include <rtl/ustrbuf.hxx>
28
#include <rtl/ustring.hxx>
29
#include <memory>
30
#include <utility>
31
32
template<typename T> struct SvParser_Impl;
33
class SvStream;
34
35
enum class SvParserState
36
{
37
    Accepted = 0,
38
    NotStarted,
39
    Working,
40
    Pending,
41
    Error
42
};
43
44
template<typename T>
45
class SVT_DLLPUBLIC SvParser : public SvRefBase
46
{
47
    DECL_DLLPRIVATE_LINK( NewDataRead, LinkParamNone*, void );
48
49
protected:
50
    SvStream&           rInput;
51
    OUStringBuffer      aToken;             // scanned token
52
    sal_uInt32          nlLineNr;           // current line number
53
    sal_uInt32          nlLinePos;          // current column number
54
    sal_uInt32          nConversionErrors;  // count of conversion errors
55
56
    std::unique_ptr<SvParser_Impl<T>> pImplData; // internal data
57
    tools::Long                m_nTokenIndex;      // current token index to detect loops for seeking backwards
58
    tools::Long                nTokenValue;        // additional value (RTF)
59
    bool                bTokenHasValue;     // indicates whether nTokenValue is valid
60
    bool                bFuzzing;           // indicates we are in Fuzzing mode
61
    SvParserState       eState;             // status also in derived classes
62
63
    rtl_TextEncoding    eSrcEnc;            // Source encoding
64
65
    sal_uInt64          nNextChPos;
66
    sal_uInt32          nNextCh;            // current character codepoint in UTF32 for the "lex"
67
68
    bool                bSwitchToUCS2 : 1;  // switching is allowed
69
    bool                bRTF_InTextRead : 1;  // only for RTF-Parser!!!
70
71
    struct TokenStackType
72
    {
73
        OUString    sToken;
74
        tools::Long        nTokenValue;
75
        bool        bTokenHasValue;
76
        T           nTokenId;
77
78
        TokenStackType();
79
    };
80
81
    // methods for Token stack
82
    T SkipToken( short nCnt = -1 );       // "skip" n Tokens back
83
    TokenStackType* GetStackPtr( short nCnt );
84
85
    // scan the next token:
86
    //  work off Token stack and call GetNextToken_() if necessary.
87
    //  That one is responsible for the recognition of new Tokens.
88
    T GetNextToken();
89
    virtual T GetNextToken_() = 0;
90
91
    // is called for each Token that is recognized in CallParser
92
    virtual void NextToken( T nToken ) = 0;
93
94
    // at times of SvRefBase derivation, not everybody may delete
95
    virtual ~SvParser() override;
96
97
    void ClearTxtConvContext();
98
99
private:
100
    std::unique_ptr<TokenStackType[]> pTokenStack;
101
    TokenStackType *pTokenStackPos;
102
    sal_uInt8 nTokenStackSize, nTokenStackPos;
103
104
public:
105
    SvParser( SvStream& rIn, sal_uInt8 nStackSize = 3 );
106
107
    virtual  SvParserState CallParser() = 0; // calling of the parser
108
109
    SvParserState GetStatus() const;  // StatusInfo
110
111
    sal_uInt32   GetLineNr() const;
112
    sal_uInt32   GetLinePos() const;
113
    void         IncLineNr();
114
    sal_uInt32   IncLinePos();
115
    void         SetLineNr( sal_uInt32 nlNum );
116
    void         SetLinePos( sal_uInt32 nlPos );
117
118
    sal_uInt32 GetNextChar();   // Return next Unicode codepoint in UTF32.
119
    void RereadLookahead();
120
121
    bool IsParserWorking() const;
122
123
    Link<LinkParamNone*,void> GetAsynchCallLink() const;
124
125
    // for asynchronous reading from the SvStream
126
    void SaveState( T nToken );
127
    void RestoreState();
128
    virtual void Continue( T nToken );
129
130
    // Set/get source encoding. The UCS2BEncoding flag is valid if source
131
    // encoding is UCS2. It specifies a big endian encoding.
132
    void SetSrcEncoding( rtl_TextEncoding eSrcEnc );
133
    rtl_TextEncoding GetSrcEncoding() const;
134
135
    // May the character set be switched to UCS/2, if a BOM
136
    // is in the first two characters of the stream?
137
    void SetSwitchToUCS2( bool bSet );
138
    bool IsSwitchToUCS2() const;
139
140
    // how many bytes a character consists of
141
    sal_uInt16 GetCharSize() const;
142
143
    T GetSaveToken() const;
144
};
145
146
147
/*========================================================================
148
 *
149
 * SvKeyValue.
150
 *
151
 *======================================================================*/
152
153
class SvKeyValue
154
{
155
    /** Representation.
156
    */
157
    OUString m_aKey;
158
    OUString m_aValue;
159
160
public:
161
    /** Construction.
162
    */
163
    SvKeyValue()
164
83.7k
    {}
165
166
    SvKeyValue (OUString aKey, OUString aValue)
167
36.5k
        : m_aKey (std::move(aKey)), m_aValue (std::move(aValue))
168
36.5k
    {}
169
170
    SvKeyValue (const SvKeyValue &rOther)
171
42.0k
        : m_aKey (rOther.m_aKey), m_aValue (rOther.m_aValue)
172
42.0k
    {}
173
174
    /** Assignment.
175
    */
176
    SvKeyValue& operator= (SvKeyValue const &rOther)
177
61.6k
    {
178
61.6k
        m_aKey   = rOther.m_aKey;
179
61.6k
        m_aValue = rOther.m_aValue;
180
61.6k
        return *this;
181
61.6k
    }
182
183
    /** Operation.
184
    */
185
61.6k
    const OUString& GetKey() const { return m_aKey; }
186
59.0k
    const OUString& GetValue() const { return m_aValue; }
187
};
188
189
/*========================================================================
190
 *
191
 * SvKeyValueIterator.
192
 *
193
 *======================================================================*/
194
195
class SVT_DLLPUBLIC SvKeyValueIterator : public SvRefBase
196
{
197
    struct Impl;
198
    std::unique_ptr<Impl> mpImpl;
199
200
public:
201
    /** Construction/Destruction.
202
    */
203
    SvKeyValueIterator();
204
    virtual ~SvKeyValueIterator() override;
205
    SvKeyValueIterator(const SvKeyValueIterator&) = delete;
206
    SvKeyValueIterator& operator=( const SvKeyValueIterator& ) = delete;
207
208
    /** Operation.
209
    */
210
    virtual bool GetFirst (SvKeyValue &rKeyVal);
211
    virtual bool GetNext  (SvKeyValue &rKeyVal);
212
    virtual void Append   (const SvKeyValue &rKeyVal);
213
};
214
215
typedef tools::SvRef<SvKeyValueIterator> SvKeyValueIteratorRef;
216
217
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */