xref: /aoo42x/main/svtools/inc/svtools/svparser.hxx (revision facfa769)
101aa44aaSAndrew Rist /**************************************************************
2*facfa769Smseidel  *
301aa44aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
401aa44aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
501aa44aaSAndrew Rist  * distributed with this work for additional information
601aa44aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
701aa44aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
801aa44aaSAndrew Rist  * "License"); you may not use this file except in compliance
901aa44aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*facfa769Smseidel  *
1101aa44aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*facfa769Smseidel  *
1301aa44aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1401aa44aaSAndrew Rist  * software distributed under the License is distributed on an
1501aa44aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1601aa44aaSAndrew Rist  * KIND, either express or implied.  See the License for the
1701aa44aaSAndrew Rist  * specific language governing permissions and limitations
1801aa44aaSAndrew Rist  * under the License.
19*facfa769Smseidel  *
2001aa44aaSAndrew Rist  *************************************************************/
2101aa44aaSAndrew Rist 
2201aa44aaSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _SVPARSER_HXX
25cdf0e10cSrcweir #define _SVPARSER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "svtools/svtdllapi.h"
28cdf0e10cSrcweir #include <tools/string.hxx>
29cdf0e10cSrcweir #include <tools/ref.hxx>
30cdf0e10cSrcweir #include <rtl/textenc.h>
31cdf0e10cSrcweir #include <boost/utility.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir struct SvParser_Impl;
35cdf0e10cSrcweir class SvStream;
36cdf0e10cSrcweir class SvUShorts;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir enum SvParserState
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 	SVPAR_ACCEPTED = 0,
41cdf0e10cSrcweir 	SVPAR_NOTSTARTED,
42cdf0e10cSrcweir 	SVPAR_WORKING,
43cdf0e10cSrcweir 	SVPAR_PENDING,
44cdf0e10cSrcweir 	SVPAR_WAITFORDATA,
45cdf0e10cSrcweir 	SVPAR_ERROR
46cdf0e10cSrcweir };
47cdf0e10cSrcweir 
48cdf0e10cSrcweir class SVT_DLLPUBLIC SvParser : public SvRefBase
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	DECL_STATIC_LINK( SvParser, NewDataRead, void* );
51cdf0e10cSrcweir 
52cdf0e10cSrcweir protected:
53cdf0e10cSrcweir 	SvStream&		rInput;
54*facfa769Smseidel 	String			aToken;				// gescanntes Token
55*facfa769Smseidel 	sal_uLong			nlLineNr;			// akt. Zeilen Nummer
56*facfa769Smseidel 	sal_uLong			nlLinePos;			// akt. Spalten Nummer
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	SvParser_Impl	*pImplData;			// interne Daten
59*facfa769Smseidel 	long			nTokenValue;		// zusaetzlicher Wert (RTF)
60*facfa769Smseidel 	sal_Bool			bTokenHasValue;	// indicates whether nTokenValue is valid
61cdf0e10cSrcweir 	SvParserState	eState;				// Status auch in abgl. Klassen
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	rtl_TextEncoding	eSrcEnc;		// Source encoding
64cdf0e10cSrcweir 
65*facfa769Smseidel 	sal_uLong nNextChPos;
66*facfa769Smseidel 	sal_Unicode	nNextCh;				// Akt. Zeichen fuer die "lex"
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	sal_Bool			bDownloadingFile : 1;// sal_True: Es wird gerade ein externes
70cdf0e10cSrcweir 										//		 File geladen. d.h. alle
71cdf0e10cSrcweir 										// 		 DataAvailable Links muessen
72cdf0e10cSrcweir 										//		 ignoriert werden.
73*facfa769Smseidel 										// Wenn keines der folgenden
74cdf0e10cSrcweir 										// Flags gesetzt ist, wird der
75cdf0e10cSrcweir 										// Stream als ANSI gelesen,
76cdf0e10cSrcweir 										// aber als CharSet DONTKNOW
77cdf0e10cSrcweir 										// zurueckgegeben.
78*facfa769Smseidel 	sal_Bool			bUCS2BSrcEnc : 1; // oder als big-endian UCS2
79*facfa769Smseidel 	sal_Bool			bSwitchToUCS2 : 1; // Umschalten ist erlaubt
80cdf0e10cSrcweir 
81*facfa769Smseidel 	sal_Bool			bRTF_InTextRead : 1; // only for RTF-Parser!!!
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	struct TokenStackType
84cdf0e10cSrcweir 	{
85cdf0e10cSrcweir 		String	sToken;
86cdf0e10cSrcweir 		long 	nTokenValue;
87cdf0e10cSrcweir 		sal_Bool	bTokenHasValue;
88cdf0e10cSrcweir 		int 	nTokenId;
89cdf0e10cSrcweir 
TokenStackTypeSvParser::TokenStackType90cdf0e10cSrcweir 		inline TokenStackType() { nTokenId = 0; }
~TokenStackTypeSvParser::TokenStackType91cdf0e10cSrcweir 		inline ~TokenStackType() { }
92cdf0e10cSrcweir 	};
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	// Methoden fuer Token-Stack
95cdf0e10cSrcweir 	int SkipToken( short nCnt = -1 );		// n Tokens zurueck "skippen"
96cdf0e10cSrcweir 	TokenStackType* GetStackPtr( short nCnt );
97cdf0e10cSrcweir 	inline sal_uInt8 GetStackPos() const;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	// scanne das naechste Token:
100cdf0e10cSrcweir 	//	Tokenstack abarbeiten und ggfs. _GetNextToken() rufen. Diese
101cdf0e10cSrcweir 	//	ist fuers erkennen von neuen Token verantwortlich
102cdf0e10cSrcweir 	int GetNextToken();
103cdf0e10cSrcweir 	virtual int _GetNextToken() = 0;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	// wird fuer jedes Token gerufen, das in CallParser erkannt wird
106cdf0e10cSrcweir 	virtual void NextToken( int nToken );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	// zu Zeiten der SvRefBase-Ableitung darf nicht jeder loeschen
109cdf0e10cSrcweir 	virtual	~SvParser();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	void ClearTxtConvContext();
112cdf0e10cSrcweir 
113cdf0e10cSrcweir private:
114cdf0e10cSrcweir 	TokenStackType* pTokenStack;
115cdf0e10cSrcweir 	TokenStackType *pTokenStackPos;
116cdf0e10cSrcweir 	sal_uInt8 nTokenStackSize, nTokenStackPos;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir public:
119cdf0e10cSrcweir 	// Konstruktor
120cdf0e10cSrcweir 	SvParser( SvStream& rIn, sal_uInt8 nStackSize = 3 );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	virtual	 SvParserState CallParser() = 0;	// Aufruf des Parsers
123cdf0e10cSrcweir 
GetStatus() const124cdf0e10cSrcweir 	inline SvParserState GetStatus() const	{ return eState; }	// StatusInfo
125cdf0e10cSrcweir 
GetLineNr() const126cdf0e10cSrcweir 	inline sal_uLong	GetLineNr() const		{ return nlLineNr; }
GetLinePos() const127cdf0e10cSrcweir 	inline sal_uLong	GetLinePos() const		{ return nlLinePos; }
IncLineNr()128cdf0e10cSrcweir 	inline sal_uLong	IncLineNr() 			{ return ++nlLineNr; }
IncLinePos()129cdf0e10cSrcweir 	inline sal_uLong	IncLinePos()			{ return ++nlLinePos; }
130cdf0e10cSrcweir 	inline sal_uLong	SetLineNr( sal_uLong nlNum );			// inline unten
131cdf0e10cSrcweir 	inline sal_uLong	SetLinePos( sal_uLong nlPos );			// inline unten
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	sal_Unicode	GetNextChar();
134*facfa769Smseidel 	void RereadLookahead();
135cdf0e10cSrcweir 
IsParserWorking() const136cdf0e10cSrcweir 	inline int 	IsParserWorking() const	{ return SVPAR_WORKING == eState; }
137cdf0e10cSrcweir 
GetAsynchCallLink() const138cdf0e10cSrcweir 	Link GetAsynchCallLink() const
139cdf0e10cSrcweir 		{ return STATIC_LINK( this, SvParser, NewDataRead ); }
140cdf0e10cSrcweir 
CallAsyncCallLink()141cdf0e10cSrcweir 	long CallAsyncCallLink() { return NewDataRead( this, 0 ); }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	// fuers asynchrone lesen aus dem SvStream
144cdf0e10cSrcweir 	/*virtual*/ void SaveState( int nToken );
145cdf0e10cSrcweir 	/*virtual*/ void RestoreState();
146cdf0e10cSrcweir 	virtual void Continue( int nToken );
147cdf0e10cSrcweir 
SetDownloadingFile(sal_Bool bSet)148cdf0e10cSrcweir 	inline void SetDownloadingFile( sal_Bool bSet ) { bDownloadingFile = bSet; }
IsDownloadingFile() const149cdf0e10cSrcweir 	inline sal_Bool IsDownloadingFile() const { return bDownloadingFile; }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	// Set/get source encoding. The UCS2BEncoding flag is valid if source
152cdf0e10cSrcweir 	// encoding is UCS2. It specifies a big endian encoding.
153cdf0e10cSrcweir 	void SetSrcEncoding( rtl_TextEncoding eSrcEnc );
GetSrcEncoding() const154cdf0e10cSrcweir 	rtl_TextEncoding GetSrcEncoding() const { return eSrcEnc; }
155cdf0e10cSrcweir 
SetSrcUCS2BEncoding(sal_Bool bSet)156cdf0e10cSrcweir 	void SetSrcUCS2BEncoding( sal_Bool bSet ) { bUCS2BSrcEnc = bSet; }
IsSrcUCS2BEncoding() const157cdf0e10cSrcweir 	sal_Bool IsSrcUCS2BEncoding() const { return bUCS2BSrcEnc; }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	// Darf der Zeichensatz auf UCS/2 umgeschaltet werden, wenn
160cdf0e10cSrcweir 	// in den ersten beiden Zeichen im Stream eine BOM steht?
SetSwitchToUCS2(sal_Bool bSet)161cdf0e10cSrcweir 	void SetSwitchToUCS2( sal_Bool bSet ) { bSwitchToUCS2 = bSet; }
IsSwitchToUCS2() const162cdf0e10cSrcweir 	sal_Bool IsSwitchToUCS2() const { return bSwitchToUCS2; }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	// Aus wie vielen Bytes betseht ein Zeichen
165cdf0e10cSrcweir 	inline sal_uInt16 GetCharSize() const;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	int GetSaveToken() const;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	// Aufbau einer Which-Map 'rWhichMap' aus einem Array von
170cdf0e10cSrcweir 	// 'pWhichIds' von Which-Ids. Es hat die Lange 'nWhichIds'.
171cdf0e10cSrcweir 	// Die Which-Map wird nicht geloescht.
172cdf0e10cSrcweir 	static void BuildWhichTbl( SvUShorts &rWhichMap,
173cdf0e10cSrcweir 							   sal_uInt16 *pWhichIds,
174cdf0e10cSrcweir 							   sal_uInt16 nWhichIds );
175cdf0e10cSrcweir };
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 
178cdf0e10cSrcweir #ifndef GOODIES_DECL_SVPARSER_DEFINED
179cdf0e10cSrcweir #define GOODIES_DECL_SVPARSER_DEFINED
180cdf0e10cSrcweir SV_DECL_REF(SvParser)
181cdf0e10cSrcweir #endif
SV_IMPL_REF(SvParser)182cdf0e10cSrcweir SV_IMPL_REF(SvParser)
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 
186cdf0e10cSrcweir inline sal_uLong SvParser::SetLineNr( sal_uLong nlNum )
187cdf0e10cSrcweir {	sal_uLong nlOld = nlLineNr; nlLineNr = nlNum; return nlOld; }
188cdf0e10cSrcweir 
SetLinePos(sal_uLong nlPos)189cdf0e10cSrcweir inline sal_uLong SvParser::SetLinePos( sal_uLong nlPos )
190cdf0e10cSrcweir {	sal_uLong nlOld = nlLinePos; nlLinePos = nlPos; return nlOld; }
191cdf0e10cSrcweir 
GetStackPos() const192cdf0e10cSrcweir inline sal_uInt8 SvParser::GetStackPos() const
193cdf0e10cSrcweir {	return nTokenStackPos; }
194cdf0e10cSrcweir 
GetCharSize() const195cdf0e10cSrcweir inline sal_uInt16 SvParser::GetCharSize() const
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	return (RTL_TEXTENCODING_UCS2 == eSrcEnc) ? 2 : 1;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 
201cdf0e10cSrcweir /*========================================================================
202cdf0e10cSrcweir  * SvKeyValue.
203cdf0e10cSrcweir  *======================================================================*/
204cdf0e10cSrcweir 
205cdf0e10cSrcweir SV_DECL_REF(SvKeyValueIterator)
206cdf0e10cSrcweir 
207cdf0e10cSrcweir class SvKeyValue
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	/** Representation.
210cdf0e10cSrcweir 	*/
211cdf0e10cSrcweir 	String m_aKey;
212cdf0e10cSrcweir 	String m_aValue;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir public:
215cdf0e10cSrcweir 	/** Construction.
216cdf0e10cSrcweir 	*/
SvKeyValue(void)217cdf0e10cSrcweir 	SvKeyValue (void)
218cdf0e10cSrcweir 	{}
219cdf0e10cSrcweir 
SvKeyValue(const String & rKey,const String & rValue)220cdf0e10cSrcweir 	SvKeyValue (const String &rKey, const String &rValue)
221cdf0e10cSrcweir 		: m_aKey (rKey), m_aValue (rValue)
222cdf0e10cSrcweir 	{}
223cdf0e10cSrcweir 
SvKeyValue(const SvKeyValue & rOther)224cdf0e10cSrcweir 	SvKeyValue (const SvKeyValue &rOther)
225cdf0e10cSrcweir 		: m_aKey (rOther.m_aKey), m_aValue (rOther.m_aValue)
226cdf0e10cSrcweir 	{}
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	/** Assignment.
229cdf0e10cSrcweir 	*/
operator =(SvKeyValue & rOther)230cdf0e10cSrcweir 	SvKeyValue& operator= (SvKeyValue &rOther)
231cdf0e10cSrcweir 	{
232cdf0e10cSrcweir 		m_aKey   = rOther.m_aKey;
233cdf0e10cSrcweir 		m_aValue = rOther.m_aValue;
234cdf0e10cSrcweir 		return *this;
235cdf0e10cSrcweir 	}
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	/** Operation.
238cdf0e10cSrcweir 	*/
GetKey(void) const239cdf0e10cSrcweir 	const String& GetKey   (void) const { return m_aKey; }
GetValue(void) const240cdf0e10cSrcweir 	const String& GetValue (void) const { return m_aValue; }
241cdf0e10cSrcweir 
SetKey(const String & rKey)242cdf0e10cSrcweir 	void SetKey   (const String &rKey  ) { m_aKey = rKey; }
SetValue(const String & rValue)243cdf0e10cSrcweir 	void SetValue (const String &rValue) { m_aValue = rValue; }
244cdf0e10cSrcweir };
245cdf0e10cSrcweir 
246cdf0e10cSrcweir /*========================================================================
247cdf0e10cSrcweir  * SvKeyValueIterator.
248cdf0e10cSrcweir  *======================================================================*/
249cdf0e10cSrcweir class SvKeyValueList_Impl;
250cdf0e10cSrcweir class SVT_DLLPUBLIC SvKeyValueIterator : public SvRefBase,
251*facfa769Smseidel 	private boost::noncopyable
252cdf0e10cSrcweir {
253*facfa769Smseidel 	/** Representation.
254*facfa769Smseidel 	*/
255*facfa769Smseidel 	SvKeyValueList_Impl* m_pList;
256*facfa769Smseidel 	sal_uInt16               m_nPos;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir public:
259*facfa769Smseidel 	/** Construction/Destruction.
260*facfa769Smseidel 	*/
261*facfa769Smseidel 	SvKeyValueIterator (void);
262*facfa769Smseidel 	virtual ~SvKeyValueIterator (void);
263*facfa769Smseidel 
264*facfa769Smseidel 	/** Operation.
265*facfa769Smseidel 	*/
266*facfa769Smseidel 	virtual sal_Bool GetFirst (SvKeyValue &rKeyVal);
267*facfa769Smseidel 	virtual sal_Bool GetNext  (SvKeyValue &rKeyVal);
268*facfa769Smseidel 	virtual void Append   (const SvKeyValue &rKeyVal);
269cdf0e10cSrcweir };
270cdf0e10cSrcweir 
271cdf0e10cSrcweir SV_IMPL_REF(SvKeyValueIterator);
272cdf0e10cSrcweir 
273cdf0e10cSrcweir #endif //_SVPARSER_HXX
274cdf0e10cSrcweir 
275*facfa769Smseidel /* vim: set noet sw=4 ts=4: */
276