LibreOffice Module svtools (master) 1
svparser.hxx
Go to the documentation of this file.
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
32template<typename T> struct SvParser_Impl;
33class SvStream;
34
35enum class SvParserState
36{
37 Accepted = 0,
39 Working,
40 Pending,
41 Error
42};
43
44template<typename T>
45class SVT_DLLPUBLIC SvParser : public SvRefBase
46{
47 DECL_DLLPRIVATE_LINK( NewDataRead, LinkParamNone*, void );
48
49protected:
50 SvStream& rInput;
51 OUStringBuffer aToken; // scanned token
52 sal_uInt32 nlLineNr; // current line number
53 sal_uInt32 nlLinePos; // current column number
54
55 std::unique_ptr<SvParser_Impl<T>> pImplData; // internal data
56 tools::Long m_nTokenIndex; // current token index to detect loops for seeking backwards
57 tools::Long nTokenValue; // additional value (RTF)
58 bool bTokenHasValue; // indicates whether nTokenValue is valid
59 bool bFuzzing; // indicates we are in Fuzzing mode
60 SvParserState eState; // status also in derived classes
61
62 rtl_TextEncoding eSrcEnc; // Source encoding
63
64 sal_uInt64 nNextChPos;
65 sal_uInt32 nNextCh; // current character codepoint in UTF32 for the "lex"
66
67 bool bSwitchToUCS2 : 1; // switching is allowed
68 bool bRTF_InTextRead : 1; // only for RTF-Parser!!!
69
71 {
72 OUString sToken;
76
78 };
79
80 // methods for Token stack
81 T SkipToken( short nCnt = -1 ); // "skip" n Tokens back
82 TokenStackType* GetStackPtr( short nCnt );
83
84 // scan the next token:
85 // work off Token stack and call GetNextToken_() if necessary.
86 // That one is responsible for the recognition of new Tokens.
87 T GetNextToken();
88 virtual T GetNextToken_() = 0;
89
90 // is called for each Token that is recognized in CallParser
91 virtual void NextToken( T nToken ) = 0;
92
93 // at times of SvRefBase derivation, not everybody may delete
94 virtual ~SvParser() override;
95
96 void ClearTxtConvContext();
97
98private:
99 std::unique_ptr<TokenStackType[]> pTokenStack;
100 TokenStackType *pTokenStackPos;
101 sal_uInt8 nTokenStackSize, nTokenStackPos;
102
103public:
104 SvParser( SvStream& rIn, sal_uInt8 nStackSize = 3 );
105
106 virtual SvParserState CallParser() = 0; // calling of the parser
107
108 SvParserState GetStatus() const; // StatusInfo
109
110 sal_uInt32 GetLineNr() const;
111 sal_uInt32 GetLinePos() const;
112 void IncLineNr();
113 sal_uInt32 IncLinePos();
114 void SetLineNr( sal_uInt32 nlNum );
115 void SetLinePos( sal_uInt32 nlPos );
116
117 sal_uInt32 GetNextChar(); // Return next Unicode codepoint in UTF32.
118 void RereadLookahead();
119
120 bool IsParserWorking() const;
121
122 Link<LinkParamNone*,void> GetAsynchCallLink() const;
123
124 // for asynchronous reading from the SvStream
125 void SaveState( T nToken );
126 void RestoreState();
127 virtual void Continue( T nToken );
128
129 // Set/get source encoding. The UCS2BEncoding flag is valid if source
130 // encoding is UCS2. It specifies a big endian encoding.
131 void SetSrcEncoding( rtl_TextEncoding eSrcEnc );
132 rtl_TextEncoding GetSrcEncoding() const;
133
134 // May the character set be switched to UCS/2, if a BOM
135 // is in the first two characters of the stream?
136 void SetSwitchToUCS2( bool bSet );
137 bool IsSwitchToUCS2() const;
138
139 // how many bytes a character consists of
140 sal_uInt16 GetCharSize() const;
141
142 T GetSaveToken() const;
143};
144
145
146/*========================================================================
147 *
148 * SvKeyValue.
149 *
150 *======================================================================*/
151
153{
156 OUString m_aKey;
157 OUString m_aValue;
158
159public:
163 {}
164
165 SvKeyValue (OUString aKey, OUString aValue)
166 : m_aKey (std::move(aKey)), m_aValue (std::move(aValue))
167 {}
168
169 SvKeyValue (const SvKeyValue &rOther)
170 : m_aKey (rOther.m_aKey), m_aValue (rOther.m_aValue)
171 {}
172
176 {
177 m_aKey = rOther.m_aKey;
178 m_aValue = rOther.m_aValue;
179 return *this;
180 }
181
184 const OUString& GetKey() const { return m_aKey; }
185 const OUString& GetValue() const { return m_aValue; }
186};
187
188/*========================================================================
189 *
190 * SvKeyValueIterator.
191 *
192 *======================================================================*/
193
195{
196 struct Impl;
197 std::unique_ptr<Impl> mpImpl;
198
199public:
203 virtual ~SvKeyValueIterator() override;
206
209 virtual bool GetFirst (SvKeyValue &rKeyVal);
210 virtual bool GetNext (SvKeyValue &rKeyVal);
211 virtual void Append (const SvKeyValue &rKeyVal);
212};
213
215
216/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvKeyValueIterator & operator=(const SvKeyValueIterator &)=delete
std::unique_ptr< Impl > mpImpl
Definition: svparser.hxx:196
virtual ~SvKeyValueIterator() override
SvKeyValueIterator(const SvKeyValueIterator &)=delete
OUString m_aValue
Definition: svparser.hxx:157
SvKeyValue()
Construction.
Definition: svparser.hxx:162
SvKeyValue(OUString aKey, OUString aValue)
Definition: svparser.hxx:165
const OUString & GetKey() const
Operation.
Definition: svparser.hxx:184
OUString m_aKey
Representation.
Definition: svparser.hxx:156
SvKeyValue(const SvKeyValue &rOther)
Definition: svparser.hxx:169
SvKeyValue & operator=(SvKeyValue const &rOther)
Assignment.
Definition: svparser.hxx:175
const OUString & GetValue() const
Definition: svparser.hxx:185
Error
long Long
tools::Long nTokenValue
Definition: svparser.hxx:73
tools::SvRef< SvKeyValueIterator > SvKeyValueIteratorRef
Definition: svparser.hxx:214
SvParserState
Definition: svparser.hxx:36
#define SVT_DLLPUBLIC
Definition: svtdllapi.h:27
unsigned char sal_uInt8