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