Branch libreoffice-5-0-4
[LibreOffice.git] / include / svtools / svparser.hxx
blobeabdf18c6807f1b514a6bdfca36c424cda9fe549
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 #ifndef INCLUDED_SVTOOLS_SVPARSER_HXX
21 #define INCLUDED_SVTOOLS_SVPARSER_HXX
23 #include <svtools/svtdllapi.h>
24 #include <tools/link.hxx>
25 #include <tools/ref.hxx>
26 #include <tools/solar.h>
27 #include <rtl/textenc.h>
28 #include <rtl/ustring.hxx>
29 #include <boost/noncopyable.hpp>
30 #include <vector>
32 struct SvParser_Impl;
33 class SvStream;
35 enum SvParserState
37 SVPAR_ACCEPTED = 0,
38 SVPAR_NOTSTARTED,
39 SVPAR_WORKING,
40 SVPAR_PENDING,
41 SVPAR_WAITFORDATA,
42 SVPAR_ERROR
45 class SVT_DLLPUBLIC SvParser : public SvRefBase
47 DECL_LINK( NewDataRead, void* );
49 protected:
50 SvStream& rInput;
51 OUString aToken; // scanned token
52 sal_uLong nlLineNr; // current line number
53 sal_uLong nlLinePos; // current column number
55 SvParser_Impl *pImplData; // internal data
56 long nTokenValue; // additional vavlue (RTF)
57 bool bTokenHasValue; // indicates whether nTokenValue is valid
58 SvParserState eState; // status also in derived classes
60 rtl_TextEncoding eSrcEnc; // Source encoding
62 sal_uLong nNextChPos;
63 sal_Unicode nNextCh; // current character for the "lex"
66 bool bDownloadingFile : 1; // true: An external file is
67 // currently being loaded, i.e.
68 // all DataAvailable Links must
69 // be ignored.
70 // If none of the following
71 // flags is set, the stream
72 // is read as ANSI, but returned
73 // as CharSet DONTKNOW.
74 bool bUCS2BSrcEnc : 1; // or as big-endian UCS2
75 bool bSwitchToUCS2 : 1; // switching is allowed
77 bool bRTF_InTextRead : 1; // only for RTF-Parser!!!
79 struct TokenStackType
81 OUString sToken;
82 long nTokenValue;
83 bool bTokenHasValue;
84 int nTokenId;
86 TokenStackType()
87 : nTokenValue(0)
88 , bTokenHasValue(false)
89 , nTokenId(0)
92 ~TokenStackType() { }
95 // methods for Token stack
96 int SkipToken( short nCnt = -1 ); // "skip" n Tokens back
97 TokenStackType* GetStackPtr( short nCnt );
98 inline sal_uInt8 GetStackPos() const;
100 // scan the next token:
101 // work off Token stack and call _GetNextToken() if necessary.
102 // That one is responsible for the recognition of new Tokens.
103 int GetNextToken();
104 virtual int _GetNextToken() = 0;
106 // is called for each Token that is recognized in CallParser
107 virtual void NextToken( int nToken );
109 // at times of SvRefBase derivation, not everybody may delete
110 virtual ~SvParser();
112 void ClearTxtConvContext();
114 private:
115 TokenStackType* pTokenStack;
116 TokenStackType *pTokenStackPos;
117 sal_uInt8 nTokenStackSize, nTokenStackPos;
119 public:
120 SvParser( SvStream& rIn, sal_uInt8 nStackSize = 3 );
122 virtual SvParserState CallParser() = 0; // calling of the parser
124 inline SvParserState GetStatus() const { return eState; } // StatusInfo
126 inline sal_uLong GetLineNr() const { return nlLineNr; }
127 inline sal_uLong GetLinePos() const { return nlLinePos; }
128 inline sal_uLong IncLineNr() { return ++nlLineNr; }
129 inline sal_uLong IncLinePos() { return ++nlLinePos; }
130 inline sal_uLong SetLineNr( sal_uLong nlNum ); // inline bottom
131 inline sal_uLong SetLinePos( sal_uLong nlPos ); // inline bottom
133 sal_Unicode GetNextChar();
134 void RereadLookahead();
136 inline bool IsParserWorking() const { return SVPAR_WORKING == eState; }
138 Link<> GetAsynchCallLink() const
139 { return LINK( const_cast<SvParser*>(this), SvParser, NewDataRead ); }
141 long CallAsyncCallLink() { return NewDataRead( 0 ); }
143 // for asynchronous reading from the SvStream
144 void SaveState( int nToken );
145 void RestoreState();
146 virtual void Continue( int nToken );
148 inline void SetDownloadingFile( bool bSet ) { bDownloadingFile = bSet; }
149 inline bool IsDownloadingFile() const { return bDownloadingFile; }
151 // Set/get source encoding. The UCS2BEncoding flag is valid if source
152 // encoding is UCS2. It specifies a big endian encoding.
153 void SetSrcEncoding( rtl_TextEncoding eSrcEnc );
154 rtl_TextEncoding GetSrcEncoding() const { return eSrcEnc; }
156 void SetSrcUCS2BEncoding( bool bSet ) { bUCS2BSrcEnc = bSet; }
157 bool IsSrcUCS2BEncoding() const { return bUCS2BSrcEnc; }
159 // May the character set be switched to UCS/2, if a BOM
160 // is in the first two characters of the stream?
161 void SetSwitchToUCS2( bool bSet ) { bSwitchToUCS2 = bSet; }
162 bool IsSwitchToUCS2() const { return bSwitchToUCS2; }
164 // how many bytes a character consists of
165 inline sal_uInt16 GetCharSize() const;
167 int GetSaveToken() const;
169 // build a Which-Map 'rWhichMap' from an array of WhichIds
170 // 'pWhichIds'. It has the length 'nWhichIds'.
171 // The WhichMap is not deleted.
172 static void BuildWhichTable( std::vector<sal_uInt16> &rWhichMap,
173 sal_uInt16 *pWhichIds,
174 sal_uInt16 nWhichIds );
178 #ifndef GOODIES_DECL_SVPARSER_DEFINED
179 #define GOODIES_DECL_SVPARSER_DEFINED
180 typedef tools::SvRef<SvParser> SvParserRef;
181 #endif
183 inline sal_uLong SvParser::SetLineNr( sal_uLong nlNum )
184 { sal_uLong nlOld = nlLineNr; nlLineNr = nlNum; return nlOld; }
186 inline sal_uLong SvParser::SetLinePos( sal_uLong nlPos )
187 { sal_uLong nlOld = nlLinePos; nlLinePos = nlPos; return nlOld; }
189 inline sal_uInt8 SvParser::GetStackPos() const
190 { return nTokenStackPos; }
192 inline sal_uInt16 SvParser::GetCharSize() const
194 return (RTL_TEXTENCODING_UCS2 == eSrcEnc) ? 2 : 1;
198 /*========================================================================
200 * SvKeyValue.
202 *======================================================================*/
204 class SvKeyValue
206 /** Representation.
208 OUString m_aKey;
209 OUString m_aValue;
211 public:
212 /** Construction.
214 SvKeyValue()
217 SvKeyValue (const OUString &rKey, const OUString &rValue)
218 : m_aKey (rKey), m_aValue (rValue)
221 SvKeyValue (const SvKeyValue &rOther)
222 : m_aKey (rOther.m_aKey), m_aValue (rOther.m_aValue)
225 /** Assignment.
227 SvKeyValue& operator= (SvKeyValue &rOther)
229 m_aKey = rOther.m_aKey;
230 m_aValue = rOther.m_aValue;
231 return *this;
234 /** Operation.
236 const OUString& GetKey() const { return m_aKey; }
237 const OUString& GetValue() const { return m_aValue; }
239 void SetKey (const OUString &rKey ) { m_aKey = rKey; }
240 void SetValue (const OUString &rValue) { m_aValue = rValue; }
243 /*========================================================================
245 * SvKeyValueIterator.
247 *======================================================================*/
249 class SVT_DLLPUBLIC SvKeyValueIterator : public SvRefBase,
250 private boost::noncopyable
252 struct Impl;
253 Impl* mpImpl;
255 public:
256 /** Construction/Destruction.
258 SvKeyValueIterator();
259 virtual ~SvKeyValueIterator();
261 /** Operation.
263 virtual bool GetFirst (SvKeyValue &rKeyVal);
264 virtual bool GetNext (SvKeyValue &rKeyVal);
265 virtual void Append (const SvKeyValue &rKeyVal);
268 typedef tools::SvRef<SvKeyValueIterator> SvKeyValueIteratorRef;
270 #endif // INCLUDED_SVTOOLS_SVPARSER_HXX
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */