00001 /*
00002 This product contains certain software code or other information
00003 ("AT&T Software") proprietary to AT&T Corp. ("AT&T"). The AT&T
00004 Software is provided to you "AS IS". YOU ASSUME TOTAL RESPONSIBILITY
00005 AND RISK FOR USE OF THE AT&T SOFTWARE. AT&T DOES NOT MAKE, AND
00006 EXPRESSLY DISCLAIMS, ANY EXPRESS OR IMPLIED WARRANTIES OF ANY KIND
00007 WHATSOEVER, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00008 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, WARRANTIES OF
00009 TITLE OR NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS, ANY
00010 WARRANTIES ARISING BY USAGE OF TRADE, COURSE OF DEALING OR COURSE OF
00011 PERFORMANCE, OR ANY WARRANTY THAT THE AT&T SOFTWARE IS "ERROR FREE" OR
00012 WILL MEET YOUR REQUIREMENTS.
00013
00014 Unless you accept a license to use the AT&T Software, you shall not
00015 reverse compile, disassemble or otherwise reverse engineer this
00016 product to ascertain the source code for any AT&T Software.
00017
00018 (c) AT&T Corp. All rights reserved. AT&T is a registered trademark of AT&T Corp.
00019
00020 ***********************************************************************
00021
00022 History:
00023
00024 24/11/99 - initial release by Hartmut Liefke, liefke@seas.upenn.edu
00025 Dan Suciu, suciu@research.att.com
00026 */
00027
00028 //**************************************************************************
00029 //**************************************************************************
00030
00031 // This module contains a few Input file parsing routines used for
00032 // parsing XML files. Most importantly, it keeps track of the line number
00033
00034 #include "Input.hpp"
00035
00036 class FileParser : public Input
00037 {
00038 unsigned curlineno; // The current line number
00039
00040 public:
00041
00042 char OpenFile(char *filename)
00043 // Opens the file and fills the buffer
00044 {
00045 curlineno=1;
00046
00047 return Input::OpenFile(filename);
00048 }
00049
00050 unsigned GetCurLineNo() { return curlineno; }
00051
00052 char ReadStringUntil(char **destptr,int *destlen,char stopatwspace,char c1,char c2)
00053 // This function scans ahead in the buffer until some character c1 or c2 occurs
00054 // If 'stopwspace=1', then we also stop at the first white space detected.
00055 // In this case, *destptr and *destlen will contain the pointer and length
00056 // of the buffer *with* the character c1 or c2 and the functions returns 1.
00057 // If a white space has been detected, then the function also returns 1,
00058 // but the white-space is *not* included in the length.
00059 // If the function couldn't find such a character in the current buffer, we try to refill
00060 // If it is still not found, then the function returns the current buffer in *destptr and *destlen
00061 // and returns 0.
00062 {
00063 char *curptr,*ptr;
00064 int len,i;
00065
00066 // Let's get as much as possible from the input buffer
00067 len=GetCurBlockPtr(&ptr);
00068 curptr=ptr;
00069
00070 i=len;
00071 // We search for characters 'c1', 'c2', ' ', '\t' ...
00072 // If we find such a character, then we store the pointer in 'destptr'
00073 // and the length in 'destlen' and exit.
00074 while(i!=0)
00075 {
00076 if(*curptr=='\n')
00077 curlineno++;
00078
00079 if((*curptr==c1)||(*curptr==c2))
00080 {
00081 curptr++;
00082 *destptr=ptr;
00083 *destlen=curptr-ptr;
00084 FastSkipData(*destlen);
00085 return 1;
00086 }
00087 if((stopatwspace)&&((*curptr==' ')||(*curptr=='\t')||(*curptr=='\r')||(*curptr=='\n')))
00088 {
00089 *destptr=ptr;
00090 *destlen=curptr-ptr;
00091 FastSkipData(*destlen);
00092 return 1;
00093 }
00094 curptr++;
00095 i--;
00096 }
00097
00098 // We couldn't find characters --> Try to refill
00099
00100 RefillAndGetCurBlockPtr(&ptr,&len);
00101
00102 curptr=ptr;
00103
00104 i=len;
00105
00106 // Now we try the same thing again:
00107
00108 // We search for characters 'c1', 'c2', ' ', '\t' ...
00109 // If we find such a character, then we store the pointer in 'destptr'
00110 // and the length in 'destlen' and exit.
00111 while(i!=0)
00112 {
00113 if(*curptr=='\n')
00114 curlineno++;
00115
00116 if((*curptr==c1)||(*curptr==c2))
00117 {
00118 curptr++;
00119 *destptr=ptr;
00120 *destlen=curptr-ptr;
00121 FastSkipData(*destlen);
00122 return 1;
00123 }
00124 if((stopatwspace)&&((*curptr==' ')||(*curptr=='\t')||(*curptr=='\r')||(*curptr=='\n')))
00125 {
00126 *destptr=ptr;
00127 *destlen=curptr-ptr;
00128 FastSkipData(*destlen);
00129 return 1;
00130 }
00131 curptr++;
00132 i--;
00133 }
00134
00135 *destptr=ptr;
00136 *destlen=len;
00137 FastSkipData(len);
00138 return 0;
00139 }
00140
00141 char ReadStringUntil(char **destptr,int *destlen,char c1)
00142 // This function scans ahead in the buffer until some character c1 occurs
00143 // In this case, *destptr and *destlen will contain the pointer and length
00144 // of the buffer *with* the character and the functions returns 0.
00145
00146 // If the function couldn't find such a character in the current buffer, we try to refill
00147 // If it is still not found, then the function returns the current buffer in *destptr and *destlen
00148 // and returns 0.
00149 {
00150 char *curptr,*ptr;
00151 int len,i;
00152
00153 len=GetCurBlockPtr(&ptr);
00154 curptr=ptr;
00155
00156 i=len;
00157
00158 // We search for character 'c1'.
00159 // If we find such a character, then we store the pointer in 'destptr'
00160 // and the length in 'destlen' and exit.
00161 while(i!=0)
00162 {
00163 if(*curptr==c1)
00164 {
00165 curptr++;
00166 *destptr=ptr;
00167 *destlen=curptr-ptr;
00168 FastSkipData(*destlen);
00169 return 1;
00170 }
00171 if(*curptr=='\n')
00172 curlineno++;
00173 curptr++;
00174 i--;
00175 }
00176
00177 // We couldn't find characters --> Try to refill
00178
00179 RefillAndGetCurBlockPtr(&ptr,&len);
00180 curptr=ptr;
00181
00182 i=len;
00183
00184 // Now we try the same thing again:
00185
00186 // We search for character 'c1'.
00187 // If we find such a character, then we store the pointer in 'destptr'
00188 // and the length in 'destlen' and exit.
00189 while(i!=0)
00190 {
00191 if(*curptr==c1)
00192 {
00193 curptr++;
00194 *destptr=ptr;
00195 *destlen=curptr-ptr;
00196 FastSkipData(*destlen);
00197 return 1;
00198 }
00199 if(*curptr=='\n')
00200 curlineno++;
00201 curptr++;
00202 i--;
00203 }
00204
00205 *destptr=ptr;
00206 *destlen=len;
00207 FastSkipData(len);
00208 return 0;
00209 }
00210
00211 char ReadWhiteSpaces(char **destptr,int *destlen)
00212 // This function scans ahead in the buffer over all white-spaces
00213 // *destptr and *destlen will contain the pointer and length
00214 // of the buffer with the white-spaces
00215
00216 // If the buffer got fill without reaching a non-whitespace character,
00217 // then the function returns the current buffer in *destptr and *destlen
00218 // and returns 1. Otherwise,
00219 {
00220 char *curptr,*ptr;
00221 int len,i;
00222
00223 len=GetCurBlockPtr(&ptr);
00224 curptr=ptr;
00225
00226 i=len;
00227
00228 // We search for non-white-space characters
00229 // If we find such a character, then we store the pointer in 'destptr'
00230 // and the length in 'destlen' and exit.
00231 while(i!=0)
00232 {
00233 if((*curptr!=' ')&&(*curptr!='\t')&&(*curptr!='\r')&&(*curptr!='\n'))
00234 // No white space?
00235 {
00236 *destptr=ptr;
00237 *destlen=curptr-ptr;
00238 FastSkipData(*destlen);
00239 return 1;
00240 }
00241 if(*curptr=='\n')
00242 curlineno++;
00243 curptr++;
00244 i--;
00245 }
00246
00247 // We couldn't find characters --> Try to refill
00248
00249 RefillAndGetCurBlockPtr(&ptr,&len);
00250 curptr=ptr;
00251
00252 i=len;
00253
00254 // Now we try the same thing again:
00255
00256 // We search for non-white-space characters
00257 // If we find such a character, then we store the pointer in 'destptr'
00258 // and the length in 'destlen' and exit.
00259 while(i!=0)
00260 {
00261 if((*curptr!=' ')&&(*curptr!='\t')&&(*curptr!='\r')&&(*curptr!='\n'))
00262 // No white space?
00263 {
00264 *destptr=ptr;
00265 *destlen=curptr-ptr;
00266 FastSkipData(*destlen);
00267 return 1;
00268 }
00269 if(*curptr=='\n')
00270 curlineno++;
00271 curptr++;
00272 i--;
00273 }
00274
00275 // We look through the entire buffer and couldn't find a non-white-space
00276 // character?
00277
00278 *destptr=ptr;
00279 *destlen=len;
00280 FastSkipData(len);
00281 return 0;
00282 }
00283
00284 char ReadStringUntil(char **destptr,int *destlen,char *searchstr)
00285 // This function scans ahead in the buffer until string 'searchstr'
00286 // is reached. In this case, *destptr and *destlen will contain the pointer and length
00287 // of the buffer *with* the search string at the end and the functions returns 0.
00288 // If the function couldn't find such a character in the current buffer, we try to refill
00289 // If it is still not found, then the function returns the current buffer in *destptr and *destlen
00290 // and returns 1.
00291 {
00292 char *ptr;
00293 int len,stringlen;
00294 char refilled=0;
00295 int curoffset=0,i;
00296 char *curptr;
00297
00298 len=GetCurBlockPtr(&ptr);
00299 stringlen=strlen(searchstr);
00300
00301 do
00302 {
00303 // We try to find the first character
00304 curptr=ptr+curoffset;
00305 i=len-curoffset;
00306 while(i!=0)
00307 {
00308 if(*curptr==searchstr[0])
00309 break;
00310 if(*curptr=='\n')
00311 curlineno++;
00312 curptr++;
00313 i--;
00314 }
00315 if(i==0)
00316 // We couldn't find characters --> Try to refill
00317 {
00318 if(!refilled)
00319 {
00320 curoffset=len; // We can skip the part that we already scanned
00321 refilled=1;
00322 RefillAndGetCurBlockPtr(&ptr,&len);
00323 continue;
00324 }
00325 else
00326 // We couldn't find the first character in the current block --> We return current block
00327 {
00328 *destptr=ptr;
00329 *destlen=len;
00330 FastSkipData(len);
00331 return 0;
00332 }
00333 }
00334
00335 // We found the first character at *curptr
00336
00337 curoffset=curptr-ptr;
00338
00339 if(curoffset+stringlen>len)
00340 // There is not enough room for the search string?
00341 {
00342 if(!refilled) // If we didn't try refill yet, we try it now
00343 {
00344 refilled=1;
00345 RefillAndGetCurBlockPtr(&ptr,&len);
00346 }
00347 if(curoffset+stringlen>len) // Still not enough space?
00348 {
00349 *destptr=ptr;
00350 *destlen=curoffset; // We take everything up to (but excluding) the first character at *curptr
00351 FastSkipData(curoffset);
00352 return 0;
00353 }
00354 }
00355 // Now we check whether ptr+offset is equal to the searchstring
00356
00357 if(memcmp(ptr+curoffset,searchstr,stringlen)==0)
00358 // We found it !
00359 {
00360 *destptr=ptr;
00361 *destlen=curoffset+stringlen;
00362 FastSkipData(*destlen);
00363 return 1;
00364 }
00365 // We didn't find it ==> We go to next character after ptr+curoffset
00366 curoffset++;
00367 }
00368 while(1);
00369 }
00370
00371 void SkipChar()
00372 // Skips the next character
00373 {
00374 char c;
00375 Input::GetChar(&c);
00376 if(c=='\n')
00377 curlineno++;
00378 }
00379
00380 void GetChar(char *c)
00381 // Reads the next character
00382 {
00383 Input::GetChar(c);
00384 if(*c=='\n')
00385 curlineno++;
00386 }
00387
00388 void SkipData(int len)
00389 // Skips 'len' characters
00390 {
00391 char *ptr;
00392 int mylen;
00393 mylen=GetCurBlockPtr(&ptr);
00394
00395 for(int i=0;i<len;i++)
00396 {
00397 if(*ptr=='\n')
00398 curlineno++;
00399 ptr++;
00400 }
00401 Input::SkipData(len);
00402 }
00403 };
1.2.11.1 written by Dimitri van Heesch,
© 1997-2001