1 /*
2 This product contains certain software code or other information
3 ("AT&T Software") proprietary to AT&T Corp. ("AT&T").  The AT&T
4 Software is provided to you "AS IS".  YOU ASSUME TOTAL RESPONSIBILITY
5 AND RISK FOR USE OF THE AT&T SOFTWARE.  AT&T DOES NOT MAKE, AND
6 EXPRESSLY DISCLAIMS, ANY EXPRESS OR IMPLIED WARRANTIES OF ANY KIND
7 WHATSOEVER, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
8 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, WARRANTIES OF
9 TITLE OR NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS, ANY
10 WARRANTIES ARISING BY USAGE OF TRADE, COURSE OF DEALING OR COURSE OF
11 PERFORMANCE, OR ANY WARRANTY THAT THE AT&T SOFTWARE IS "ERROR FREE" OR
12 WILL MEET YOUR REQUIREMENTS.
13 
14 Unless you accept a license to use the AT&T Software, you shall not
15 reverse compile, disassemble or otherwise reverse engineer this
16 product to ascertain the source code for any AT&T Software.
17 
18 (c) AT&T Corp. All rights reserved.  AT&T is a registered trademark of AT&T Corp.
19 
20 ***********************************************************************
21 
22 History:
23 
24       24/11/99  - initial release by Hartmut Liefke, liefke@seas.upenn.edu
25                                      Dan Suciu,      suciu@research.att.com
26 */
27 
28 //**************************************************************************
29 //**************************************************************************
30 
31 // This module contains a few Input file parsing routines used for
32 // parsing XML files. Most importantly, it keeps track of the line number
33 
34 #include "stdafx.h"
35 
FileParser(Session * s)36 FileParser::FileParser(Session *s): Input(s)
37 {
38 	curlineno = 0;
39 }
40 
~FileParser()41 FileParser::~FileParser()
42 {
43 }
44 
OpenFile(char * filename)45 char FileParser::OpenFile(char *filename)
46    // Opens the file and fills the buffer
47 {
48    curlineno=1;
49 
50    return Input::OpenFile(filename);
51 }
52 
GetCurLineNo()53 unsigned FileParser::GetCurLineNo()
54 {
55 	return curlineno;
56 }
57 
ReadStringUntil(char ** destptr,int * destlen,char stopatwspace,char c1,char c2)58 char FileParser::ReadStringUntil(char **destptr,int *destlen,char stopatwspace,char c1,char c2)
59 // This function scans ahead in the buffer until some character c1 or c2 occurs
60 // If 'stopwspace=1', then we also stop at the first white space detected.
61 // In this case, *destptr and *destlen will contain the pointer and length
62 // of the buffer *with* the character c1 or c2 and the functions returns 1.
63 // If a white space has been detected, then the function also returns 1,
64 // but the white-space is *not* included in the length.
65 // If the function couldn't find such a character in the current buffer, we try to refill
66 // If it is still not found, then the function returns the current buffer in *destptr and *destlen
67 // and returns 0.
68 {
69    char *curptr,*ptr;
70    int  len,i;
71 
72    // Let's get as much as possible from the input buffer
73    len=GetCurBlockPtr(&ptr);
74    curptr=ptr;
75 
76    i=len;
77    // We search for characters 'c1', 'c2', ' ', '\t' ...
78    // If we find such a character, then we store the pointer in 'destptr'
79    // and the length in 'destlen' and exit.
80    while(i!=0)
81    {
82       if(*curptr=='\n')
83          curlineno++;
84 
85       if((*curptr==c1)||(*curptr==c2))
86       {
87          curptr++;
88          *destptr=ptr;
89          *destlen=curptr-ptr;
90          FastSkipData(*destlen);
91          return TRUE;
92       }
93       if((stopatwspace)&&((*curptr==' ')||(*curptr=='\t')||(*curptr=='\r')||(*curptr=='\n')))
94       {
95          *destptr=ptr;
96          *destlen=curptr-ptr;
97          FastSkipData(*destlen);
98          return TRUE;
99       }
100       curptr++;
101       i--;
102    }
103 
104    // We couldn't find characters --> Try to refill
105 
106    RefillAndGetCurBlockPtr(&ptr,&len);
107 
108    curptr=ptr;
109 
110    i=len;
111 
112    // Now we try the same thing again:
113 
114    // We search for characters 'c1', 'c2', ' ', '\t' ...
115    // If we find such a character, then we store the pointer in 'destptr'
116    // and the length in 'destlen' and exit.
117    while(i!=0)
118    {
119       if(*curptr=='\n')
120          curlineno++;
121 
122       if((*curptr==c1)||(*curptr==c2))
123       {
124          curptr++;
125          *destptr=ptr;
126          *destlen=curptr-ptr;
127          FastSkipData(*destlen);
128          return TRUE;
129       }
130       if((stopatwspace)&&((*curptr==' ')||(*curptr=='\t')||(*curptr=='\r')||(*curptr=='\n')))
131       {
132          *destptr=ptr;
133          *destlen=curptr-ptr;
134          FastSkipData(*destlen);
135          return TRUE;
136       }
137       curptr++;
138       i--;
139    }
140 
141    *destptr=ptr;
142    *destlen=len;
143    FastSkipData(len);
144    return FALSE;
145 }
146 
ReadStringUntil(char ** destptr,int * destlen,char c1)147 char FileParser::ReadStringUntil(char **destptr,int *destlen,char c1)
148    // This function scans ahead in the buffer until some character c1 occurs
149    // In this case, *destptr and *destlen will contain the pointer and length
150    // of the buffer *with* the character and the functions returns 1.
151    // If the function couldn't find such a character in the current buffer, we try to refill
152    // If it is still not found, then the function returns the current buffer in *destptr and *destlen
153    // and returns 0.
154 {
155    char *curptr,*ptr;
156    int  len,i;
157 
158    len=GetCurBlockPtr(&ptr);
159    curptr=ptr;
160 
161    i=len;
162 
163    // We search for character 'c1'.
164    // If we find such a character, then we store the pointer in 'destptr'
165    // and the length in 'destlen' and exit.
166    while(i!=0)
167    {
168       if(*curptr==c1)
169       {
170          curptr++;
171          *destptr=ptr;
172          *destlen=curptr-ptr;
173          FastSkipData(*destlen);
174          return TRUE;
175       }
176       if(*curptr=='\n')
177          curlineno++;
178       curptr++;
179       i--;
180    }
181 
182    // We couldn't find characters --> Try to refill
183 
184    RefillAndGetCurBlockPtr(&ptr,&len);
185    curptr=ptr;
186 
187    i=len;
188 
189    // Now we try the same thing again:
190 
191    // We search for character 'c1'.
192    // If we find such a character, then we store the pointer in 'destptr'
193    // and the length in 'destlen' and exit.
194    while(i!=0)
195    {
196       if(*curptr==c1)
197       {
198          curptr++;
199          *destptr=ptr;
200          *destlen=curptr-ptr;
201          FastSkipData(*destlen);
202          return TRUE;
203       }
204       if(*curptr=='\n')
205          curlineno++;
206       curptr++;
207       i--;
208    }
209 
210    *destptr=ptr;
211    *destlen=len;
212    FastSkipData(len);
213    return FALSE;
214 }
215 
ReadWhiteSpaces(char ** destptr,int * destlen)216 char FileParser::ReadWhiteSpaces(char **destptr,int *destlen)
217    // This function scans ahead in the buffer over all white-spaces
218    // *destptr and *destlen will contain the pointer and length
219    // of the buffer with the white-spaces
220 
221    // If the buffer got fill without reaching a non-whitespace character,
222    // then the function returns the current buffer in *destptr and *destlen
223    // and returns 1. Otherwise,
224 {
225    char *curptr,*ptr;
226    int  len,i;
227 
228    len=GetCurBlockPtr(&ptr);
229    curptr=ptr;
230 
231    i=len;
232 
233    // We search for non-white-space characters
234    // If we find such a character, then we store the pointer in 'destptr'
235    // and the length in 'destlen' and exit.
236    while(i!=0)
237    {
238       if((*curptr!=' ')&&(*curptr!='\t')&&(*curptr!='\r')&&(*curptr!='\n'))
239          // No white space?
240       {
241          *destptr=ptr;
242          *destlen=curptr-ptr;
243          FastSkipData(*destlen);
244          return 1;
245       }
246       if(*curptr=='\n')
247          curlineno++;
248       curptr++;
249       i--;
250    }
251 
252    // We couldn't find characters --> Try to refill
253 
254    RefillAndGetCurBlockPtr(&ptr,&len);
255    curptr=ptr;
256 
257    i=len;
258 
259    // Now we try the same thing again:
260 
261    // We search for non-white-space characters
262    // If we find such a character, then we store the pointer in 'destptr'
263    // and the length in 'destlen' and exit.
264    while(i!=0)
265    {
266       if((*curptr!=' ')&&(*curptr!='\t')&&(*curptr!='\r')&&(*curptr!='\n'))
267          // No white space?
268       {
269          *destptr=ptr;
270          *destlen=curptr-ptr;
271          FastSkipData(*destlen);
272          return 1;
273       }
274       if(*curptr=='\n')
275          curlineno++;
276       curptr++;
277       i--;
278    }
279 
280    // We look through the entire buffer and couldn't find a non-white-space
281    // character?
282 
283    *destptr=ptr;
284    *destlen=len;
285    FastSkipData(len);
286    return 0;
287 }
288 
ReadStringUntil(char ** destptr,int * destlen,char * searchstr)289 char FileParser::ReadStringUntil(char **destptr,int *destlen,char *searchstr)
290 // This function scans ahead in the buffer until string 'searchstr'
291 // is reached. In this case, *destptr and *destlen will contain the pointer and length
292 // of the buffer *with* the search string at the end and the functions returns 0.
293 // If the function couldn't find such a character in the current buffer, we try to refill
294 // If it is still not found, then the function returns the current buffer in *destptr and *destlen
295 // and returns 1.
296 {
297    char  *ptr;
298    int   len,stringlen;
299    char  refilled=0;
300    int   curoffset=0,i;
301    char  *curptr;
302 
303    len=GetCurBlockPtr(&ptr);
304    stringlen=strlen(searchstr);
305 
306    do
307    {
308       // We try to find the first character
309       curptr=ptr+curoffset;
310       i=len-curoffset;
311       while(i!=0)
312       {
313          if(*curptr==searchstr[0])
314             break;
315          if(*curptr=='\n')
316             curlineno++;
317          curptr++;
318          i--;
319       }
320       if(i==0)
321          // We couldn't find characters --> Try to refill
322       {
323          if(!refilled)
324          {
325             curoffset=len; // We can skip the part that we already scanned
326             refilled=1;
327             RefillAndGetCurBlockPtr(&ptr,&len);
328             continue;
329          }
330          else
331             // We couldn't find the first character in the current block --> We return current block
332          {
333             *destptr=ptr;
334             *destlen=len;
335             FastSkipData(len);
336             return 0;
337          }
338       }
339 
340       // We found the first character at *curptr
341 
342       curoffset=curptr-ptr;
343 
344       if(curoffset+stringlen>len)
345          // There is not enough room for the search string?
346       {
347          if(!refilled) // If we didn't try refill yet, we try it now
348          {
349             refilled=1;
350             RefillAndGetCurBlockPtr(&ptr,&len);
351          }
352          if(curoffset+stringlen>len) // Still not enough space?
353          {
354             *destptr=ptr;
355             *destlen=curoffset; // We take everything up to (but excluding) the first character at *curptr
356             FastSkipData(curoffset);
357             return 0;
358          }
359       }
360       // Now we check whether ptr+offset is equal to the searchstring
361 
362       if(memcmp(ptr+curoffset,searchstr,stringlen)==0)
363       // We found it !
364       {
365          *destptr=ptr;
366          *destlen=curoffset+stringlen;
367          FastSkipData(*destlen);
368          return 1;
369       }
370       // We didn't find it ==> We go to next character after ptr+curoffset
371       curoffset++;
372    }
373    while(1);
374 }
375 
SkipChar()376 char FileParser::SkipChar()
377    // Skips the next character
378 {
379    char c;
380 	if (!Input::GetChar(&c)) {
381 		return FALSE;
382 	}
383    if(c=='\n')
384       curlineno++;
385 	return TRUE;
386 }
387 
GetChar(char * c)388 char FileParser::GetChar(char *c)
389    // Reads the next character
390 {
391 	if(!Input::GetChar(c)) {
392 		return FALSE;
393 	}
394    if(*c=='\n')
395       curlineno++;
396 	return TRUE;
397 }
398 
SkipData(int len)399 void FileParser::SkipData(int len)
400    // Skips 'len' characters
401 {
402    char *ptr;
403    int  mylen;
404    mylen=GetCurBlockPtr(&ptr);
405 
406    for(int i=0;i<len;i++)
407    {
408       if(*ptr=='\n')
409          curlineno++;
410       ptr++;
411    }
412    Input::SkipData(len);
413 }
414