xref: /trunk/main/autodoc/source/inc/tools/tkpchars.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef ADC_TKPCHARS_HXX
29*cdf0e10cSrcweir #define ADC_TKPCHARS_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // USED SERVICES
32*cdf0e10cSrcweir 	// BASE CLASSES
33*cdf0e10cSrcweir 	// COMPONENTS
34*cdf0e10cSrcweir 	// PARAMETRS
35*cdf0e10cSrcweir #include <adc_cl.hxx>
36*cdf0e10cSrcweir #include <stack>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir /**	@descr
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 	dpSource:
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 	1||||||||||||||||||||||a||||||||||||b|||c||||||||||||||||||||...
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 	1 := first character of Sourcecode.
48*cdf0e10cSrcweir 	a := nLastTokenStart, there starts the last cut token.
49*cdf0e10cSrcweir 	b := nLastCut, there is a '\0'-char which marks the end of
50*cdf0e10cSrcweir 		 the last cut token. The original character at b is stored
51*cdf0e10cSrcweir 		 in cCharAtLastCut and will replace the '\0'-char, when the
52*cdf0e10cSrcweir 		 next token is cut.
53*cdf0e10cSrcweir 	c := The current cursor position.
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	@needs 	cosv.lib
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	@use    This class can be used by any parser to get the chars of a
59*cdf0e10cSrcweir 			text one by one and separate them to tokens.
60*cdf0e10cSrcweir **/
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir class CharacterSource
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	public:
65*cdf0e10cSrcweir 		// LIFECYCLE
66*cdf0e10cSrcweir 						CharacterSource();
67*cdf0e10cSrcweir 						~CharacterSource();
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 		// OPERATIONS
70*cdf0e10cSrcweir 		/** Loads the complete contents of in_rSource into the classes private memory.
71*cdf0e10cSrcweir 			If in_rSource is a file, it has to be open of course.
72*cdf0e10cSrcweir             After loading the text, the CurChar() is set on the begin of the text.
73*cdf0e10cSrcweir 		**/
74*cdf0e10cSrcweir 		void			LoadText(
75*cdf0e10cSrcweir 							csv::bstream &		io_rSource);
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         void            InsertTextAtCurPos(
78*cdf0e10cSrcweir                             const char *        i_sText2Insert );
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 		///	@return CurChar() after moving forward one char.
81*cdf0e10cSrcweir             char            MoveOn();
82*cdf0e10cSrcweir 		/** @return
83*cdf0e10cSrcweir 			The token which starts at the char which was CurChar(), when
84*cdf0e10cSrcweir 			CutToken() was called the last time - or at the beginning of the text.
85*cdf0e10cSrcweir 			The token ends by the CurChar() being replaced by a '\0'.
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 			Value is valid until the next call of CutToken() or ~CharacterSource().
88*cdf0e10cSrcweir 		**/
89*cdf0e10cSrcweir 		const char *	CutToken();
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		// INQUIRY
92*cdf0e10cSrcweir         char            CurChar() const;
93*cdf0e10cSrcweir 		/// @return The result of the last CutToken(). Or NULL, if there was none yet.
94*cdf0e10cSrcweir 		const char *	CurToken() const;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	// INQUIRY
97*cdf0e10cSrcweir 		/// @return true, if
98*cdf0e10cSrcweir 		bool			IsFinished() const;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	private:
101*cdf0e10cSrcweir         struct S_SourceState
102*cdf0e10cSrcweir         {
103*cdf0e10cSrcweir     		DYN char *		dpSource;
104*cdf0e10cSrcweir 		    intt			nSourceSize;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 		    intt			nCurPos;
107*cdf0e10cSrcweir 		    intt			nLastCut;
108*cdf0e10cSrcweir 		    intt			nLastTokenStart;
109*cdf0e10cSrcweir 		    char 			cCharAtLastCut;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir                             S_SourceState(
112*cdf0e10cSrcweir     		                    DYN char *		dpSource,
113*cdf0e10cSrcweir 		                        intt			nSourceSize,
114*cdf0e10cSrcweir 		                        intt			nCurPos,
115*cdf0e10cSrcweir 		                        intt			nLastCut,
116*cdf0e10cSrcweir 		                        intt			nLastTokenStart,
117*cdf0e10cSrcweir 		                        char 			cCharAtLastCut );
118*cdf0e10cSrcweir         };
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         void            BeginSource();
121*cdf0e10cSrcweir 		intt			CurPos() const;
122*cdf0e10cSrcweir         char            MoveOn_OverStack();
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir         // DATA
125*cdf0e10cSrcweir         std::stack< S_SourceState >
126*cdf0e10cSrcweir                         aSourcesStack;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 		DYN char *		dpSource;
129*cdf0e10cSrcweir 		intt			nSourceSize;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		intt			nCurPos;
132*cdf0e10cSrcweir 		intt			nLastCut;
133*cdf0e10cSrcweir 		intt			nLastTokenStart;
134*cdf0e10cSrcweir 		char 			cCharAtLastCut;
135*cdf0e10cSrcweir };
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir inline char
139*cdf0e10cSrcweir CharacterSource::MoveOn()
140*cdf0e10cSrcweir 	{
141*cdf0e10cSrcweir if (DEBUG_ShowText())
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir 		Cerr() << char(dpSource[nCurPos+1]) << Flush();
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir         if ( nCurPos < nSourceSize-1 )
146*cdf0e10cSrcweir     		return dpSource[++nCurPos];
147*cdf0e10cSrcweir         else if ( aSourcesStack.size() > 0 )
148*cdf0e10cSrcweir             return MoveOn_OverStack();
149*cdf0e10cSrcweir         else
150*cdf0e10cSrcweir             return dpSource[nCurPos = nSourceSize];
151*cdf0e10cSrcweir 	}
152*cdf0e10cSrcweir inline char
153*cdf0e10cSrcweir CharacterSource::CurChar() const
154*cdf0e10cSrcweir 	{ return nCurPos != nLastCut ? dpSource[nCurPos] : cCharAtLastCut; }
155*cdf0e10cSrcweir inline const char *
156*cdf0e10cSrcweir CharacterSource::CurToken() const
157*cdf0e10cSrcweir 	{ return &dpSource[nLastTokenStart]; }
158*cdf0e10cSrcweir inline bool
159*cdf0e10cSrcweir CharacterSource::IsFinished() const
160*cdf0e10cSrcweir 	{ return nCurPos >= nSourceSize; }
161*cdf0e10cSrcweir inline intt
162*cdf0e10cSrcweir CharacterSource::CurPos() const
163*cdf0e10cSrcweir 	{ return nCurPos; }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir #endif
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 
171