Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / svtools / svparser.hxx
blobe1a74840add089cb90e699726187ec8042362453
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
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>
32 template<typename T> struct SvParser_Impl;
33 class SvStream;
35 enum class SvParserState
37 Accepted = 0,
38 NotStarted,
39 Working,
40 Pending,
41 Error
44 template<typename T>
45 class SVT_DLLPUBLIC SvParser : public SvRefBase
47 DECL_DLLPRIVATE_LINK( NewDataRead, LinkParamNone*, void );
49 protected:
50 SvStream& rInput;
51 OUStringBuffer aToken; // scanned token
52 sal_uInt32 nlLineNr; // current line number
53 sal_uInt32 nlLinePos; // current column number
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
62 rtl_TextEncoding eSrcEnc; // Source encoding
64 sal_uInt64 nNextChPos;
65 sal_uInt32 nNextCh; // current character codepoint in UTF32 for the "lex"
67 bool bSwitchToUCS2 : 1; // switching is allowed
68 bool bRTF_InTextRead : 1; // only for RTF-Parser!!!
70 struct TokenStackType
72 OUString sToken;
73 tools::Long nTokenValue;
74 bool bTokenHasValue;
75 T nTokenId;
77 TokenStackType();
80 // methods for Token stack
81 T SkipToken( short nCnt = -1 ); // "skip" n Tokens back
82 TokenStackType* GetStackPtr( short nCnt );
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;
90 // is called for each Token that is recognized in CallParser
91 virtual void NextToken( T nToken ) = 0;
93 // at times of SvRefBase derivation, not everybody may delete
94 virtual ~SvParser() override;
96 void ClearTxtConvContext();
98 private:
99 std::unique_ptr<TokenStackType[]> pTokenStack;
100 TokenStackType *pTokenStackPos;
101 sal_uInt8 nTokenStackSize, nTokenStackPos;
103 public:
104 SvParser( SvStream& rIn, sal_uInt8 nStackSize = 3 );
106 virtual SvParserState CallParser() = 0; // calling of the parser
108 SvParserState GetStatus() const; // StatusInfo
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 );
117 sal_uInt32 GetNextChar(); // Return next Unicode codepoint in UTF32.
118 void RereadLookahead();
120 bool IsParserWorking() const;
122 Link<LinkParamNone*,void> GetAsynchCallLink() const;
124 // for asynchronous reading from the SvStream
125 void SaveState( T nToken );
126 void RestoreState();
127 virtual void Continue( T nToken );
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;
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;
139 // how many bytes a character consists of
140 sal_uInt16 GetCharSize() const;
142 T GetSaveToken() const;
146 /*========================================================================
148 * SvKeyValue.
150 *======================================================================*/
152 class SvKeyValue
154 /** Representation.
156 OUString m_aKey;
157 OUString m_aValue;
159 public:
160 /** Construction.
162 SvKeyValue()
165 SvKeyValue (OUString aKey, OUString aValue)
166 : m_aKey (std::move(aKey)), m_aValue (std::move(aValue))
169 SvKeyValue (const SvKeyValue &rOther)
170 : m_aKey (rOther.m_aKey), m_aValue (rOther.m_aValue)
173 /** Assignment.
175 SvKeyValue& operator= (SvKeyValue const &rOther)
177 m_aKey = rOther.m_aKey;
178 m_aValue = rOther.m_aValue;
179 return *this;
182 /** Operation.
184 const OUString& GetKey() const { return m_aKey; }
185 const OUString& GetValue() const { return m_aValue; }
188 /*========================================================================
190 * SvKeyValueIterator.
192 *======================================================================*/
194 class SVT_DLLPUBLIC SvKeyValueIterator : public SvRefBase
196 struct Impl;
197 std::unique_ptr<Impl> mpImpl;
199 public:
200 /** Construction/Destruction.
202 SvKeyValueIterator();
203 virtual ~SvKeyValueIterator() override;
204 SvKeyValueIterator(const SvKeyValueIterator&) = delete;
205 SvKeyValueIterator& operator=( const SvKeyValueIterator& ) = delete;
207 /** Operation.
209 virtual bool GetFirst (SvKeyValue &rKeyVal);
210 virtual bool GetNext (SvKeyValue &rKeyVal);
211 virtual void Append (const SvKeyValue &rKeyVal);
214 typedef tools::SvRef<SvKeyValueIterator> SvKeyValueIteratorRef;
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */