xref: /trunk/main/autodoc/source/inc/tools/tkpchars.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
1*1c78a5d6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*1c78a5d6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1c78a5d6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1c78a5d6SAndrew Rist  * distributed with this work for additional information
6*1c78a5d6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1c78a5d6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1c78a5d6SAndrew Rist  * "License"); you may not use this file except in compliance
9*1c78a5d6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*1c78a5d6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*1c78a5d6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1c78a5d6SAndrew Rist  * software distributed under the License is distributed on an
15*1c78a5d6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1c78a5d6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1c78a5d6SAndrew Rist  * specific language governing permissions and limitations
18*1c78a5d6SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*1c78a5d6SAndrew Rist  *************************************************************/
21*1c78a5d6SAndrew Rist 
22*1c78a5d6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef ADC_TKPCHARS_HXX
25cdf0e10cSrcweir #define ADC_TKPCHARS_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // USED SERVICES
28cdf0e10cSrcweir     // BASE CLASSES
29cdf0e10cSrcweir     // COMPONENTS
30cdf0e10cSrcweir     // PARAMETRS
31cdf0e10cSrcweir #include <adc_cl.hxx>
32cdf0e10cSrcweir #include <stack>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /** @descr
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     dpSource:
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     1||||||||||||||||||||||a||||||||||||b|||c||||||||||||||||||||...
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     1 := first character of Sourcecode.
44cdf0e10cSrcweir     a := nLastTokenStart, there starts the last cut token.
45cdf0e10cSrcweir     b := nLastCut, there is a '\0'-char which marks the end of
46cdf0e10cSrcweir          the last cut token. The original character at b is stored
47cdf0e10cSrcweir          in cCharAtLastCut and will replace the '\0'-char, when the
48cdf0e10cSrcweir          next token is cut.
49cdf0e10cSrcweir     c := The current cursor position.
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     @needs  cosv.lib
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     @use    This class can be used by any parser to get the chars of a
55cdf0e10cSrcweir             text one by one and separate them to tokens.
56cdf0e10cSrcweir **/
57cdf0e10cSrcweir 
58cdf0e10cSrcweir class CharacterSource
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     public:
61cdf0e10cSrcweir         // LIFECYCLE
62cdf0e10cSrcweir                         CharacterSource();
63cdf0e10cSrcweir                         ~CharacterSource();
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         // OPERATIONS
66cdf0e10cSrcweir         /** Loads the complete contents of in_rSource into the classes private memory.
67cdf0e10cSrcweir             If in_rSource is a file, it has to be open of course.
68cdf0e10cSrcweir             After loading the text, the CurChar() is set on the begin of the text.
69cdf0e10cSrcweir         **/
70cdf0e10cSrcweir         void            LoadText(
71cdf0e10cSrcweir                             csv::bstream &      io_rSource);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         void            InsertTextAtCurPos(
74cdf0e10cSrcweir                             const char *        i_sText2Insert );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         /// @return CurChar() after moving forward one char.
77cdf0e10cSrcweir             char            MoveOn();
78cdf0e10cSrcweir         /** @return
79cdf0e10cSrcweir             The token which starts at the char which was CurChar(), when
80cdf0e10cSrcweir             CutToken() was called the last time - or at the beginning of the text.
81cdf0e10cSrcweir             The token ends by the CurChar() being replaced by a '\0'.
82cdf0e10cSrcweir 
83cdf0e10cSrcweir             Value is valid until the next call of CutToken() or ~CharacterSource().
84cdf0e10cSrcweir         **/
85cdf0e10cSrcweir         const char *    CutToken();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         // INQUIRY
88cdf0e10cSrcweir         char            CurChar() const;
89cdf0e10cSrcweir         /// @return The result of the last CutToken(). Or NULL, if there was none yet.
90cdf0e10cSrcweir         const char *    CurToken() const;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     // INQUIRY
93cdf0e10cSrcweir         /// @return true, if
94cdf0e10cSrcweir         bool            IsFinished() const;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     private:
97cdf0e10cSrcweir         struct S_SourceState
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             DYN char *      dpSource;
100cdf0e10cSrcweir             intt            nSourceSize;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir             intt            nCurPos;
103cdf0e10cSrcweir             intt            nLastCut;
104cdf0e10cSrcweir             intt            nLastTokenStart;
105cdf0e10cSrcweir             char            cCharAtLastCut;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir                             S_SourceState(
108cdf0e10cSrcweir                                 DYN char *      dpSource,
109cdf0e10cSrcweir                                 intt            nSourceSize,
110cdf0e10cSrcweir                                 intt            nCurPos,
111cdf0e10cSrcweir                                 intt            nLastCut,
112cdf0e10cSrcweir                                 intt            nLastTokenStart,
113cdf0e10cSrcweir                                 char            cCharAtLastCut );
114cdf0e10cSrcweir         };
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         void            BeginSource();
117cdf0e10cSrcweir         intt            CurPos() const;
118cdf0e10cSrcweir         char            MoveOn_OverStack();
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         // DATA
121cdf0e10cSrcweir         std::stack< S_SourceState >
122cdf0e10cSrcweir                         aSourcesStack;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         DYN char *      dpSource;
125cdf0e10cSrcweir         intt            nSourceSize;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         intt            nCurPos;
128cdf0e10cSrcweir         intt            nLastCut;
129cdf0e10cSrcweir         intt            nLastTokenStart;
130cdf0e10cSrcweir         char            cCharAtLastCut;
131cdf0e10cSrcweir };
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 
134cdf0e10cSrcweir inline char
MoveOn()135cdf0e10cSrcweir CharacterSource::MoveOn()
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir if (DEBUG_ShowText())
138cdf0e10cSrcweir {
139cdf0e10cSrcweir         Cerr() << char(dpSource[nCurPos+1]) << Flush();
140cdf0e10cSrcweir }
141cdf0e10cSrcweir         if ( nCurPos < nSourceSize-1 )
142cdf0e10cSrcweir             return dpSource[++nCurPos];
143cdf0e10cSrcweir         else if ( aSourcesStack.size() > 0 )
144cdf0e10cSrcweir             return MoveOn_OverStack();
145cdf0e10cSrcweir         else
146cdf0e10cSrcweir             return dpSource[nCurPos = nSourceSize];
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir inline char
CurChar() const149cdf0e10cSrcweir CharacterSource::CurChar() const
150cdf0e10cSrcweir     { return nCurPos != nLastCut ? dpSource[nCurPos] : cCharAtLastCut; }
151cdf0e10cSrcweir inline const char *
CurToken() const152cdf0e10cSrcweir CharacterSource::CurToken() const
153cdf0e10cSrcweir     { return &dpSource[nLastTokenStart]; }
154cdf0e10cSrcweir inline bool
IsFinished() const155cdf0e10cSrcweir CharacterSource::IsFinished() const
156cdf0e10cSrcweir     { return nCurPos >= nSourceSize; }
157cdf0e10cSrcweir inline intt
CurPos() const158cdf0e10cSrcweir CharacterSource::CurPos() const
159cdf0e10cSrcweir     { return nCurPos; }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
164cdf0e10cSrcweir #endif
165