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