Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/xmlparse.l
Parent Directory
|
Revision Log
Revision 396 -
(hide annotations)
(download)
Tue May 18 18:39:22 2010 UTC (14 years, 10 months ago) by amb
File size: 23764 byte(s)
Tue May 18 18:39:22 2010 UTC (14 years, 10 months ago) by amb
File size: 23764 byte(s)
Handle floating point numbers in scientific notation.
1 | amb | 334 | %{ |
2 | /*************************************** | ||
3 | amb | 396 | $Header: /home/amb/CVS/routino/src/xmlparse.l,v 1.15 2010-05-18 18:39:22 amb Exp $ |
4 | amb | 334 | |
5 | A simple generic XML parser where the structure comes from the function parameters. | ||
6 | amb | 348 | Not intended to be fully conforming to XML staandard or a validating parser but |
7 | sufficient to parse OSM XML and simple program configuration files. | ||
8 | amb | 334 | |
9 | Part of the Routino routing software. | ||
10 | ******************/ /****************** | ||
11 | This file Copyright 2010 Andrew M. Bishop | ||
12 | |||
13 | This program is free software: you can redistribute it and/or modify | ||
14 | it under the terms of the GNU Affero General Public License as published by | ||
15 | the Free Software Foundation, either version 3 of the License, or | ||
16 | (at your option) any later version. | ||
17 | |||
18 | This program is distributed in the hope that it will be useful, | ||
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
21 | GNU Affero General Public License for more details. | ||
22 | |||
23 | You should have received a copy of the GNU Affero General Public License | ||
24 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
25 | ***************************************/ | ||
26 | |||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | amb | 363 | #include <ctype.h> |
31 | amb | 334 | #include <string.h> |
32 | |||
33 | amb | 356 | #include "xmlparse.h" |
34 | |||
35 | |||
36 | amb | 334 | /* Parser outputs */ |
37 | |||
38 | amb | 348 | #define LEX_EOF 0 |
39 | amb | 334 | |
40 | amb | 348 | #define LEX_TAG_BEGIN 1 |
41 | #define LEX_XML_DECL_BEGIN 2 | ||
42 | #define LEX_TAG_POP 3 | ||
43 | #define LEX_TAG_PUSH 4 | ||
44 | #define LEX_XML_DECL_FINISH 6 | ||
45 | #define LEX_TAG_FINISH 7 | ||
46 | #define LEX_ATTR_KEY 8 | ||
47 | #define LEX_ATTR_VAL 9 | ||
48 | amb | 334 | |
49 | amb | 348 | #define LEX_ERROR 100 |
50 | |||
51 | #define LEX_ERROR_TAG_START 101 | ||
52 | #define LEX_ERROR_XML_DECL_START 102 | ||
53 | #define LEX_ERROR_TAG 103 | ||
54 | #define LEX_ERROR_XML_DECL 104 | ||
55 | #define LEX_ERROR_ATTR 105 | ||
56 | #define LEX_ERROR_END_TAG 106 | ||
57 | #define LEX_ERROR_COMMENT 107 | ||
58 | #define LEX_ERROR_CLOSE 108 | ||
59 | amb | 351 | #define LEX_ERROR_ATTR_VAL 109 |
60 | amb | 356 | #define LEX_ERROR_ENTITY_REF 110 |
61 | #define LEX_ERROR_CHAR_REF 111 | ||
62 | amb | 348 | |
63 | #define LEX_ERROR_UNEXP_TAG 201 | ||
64 | #define LEX_ERROR_UNBALANCED 202 | ||
65 | #define LEX_ERROR_NO_START 203 | ||
66 | #define LEX_ERROR_UNEXP_ATT 204 | ||
67 | #define LEX_ERROR_UNEXP_EOF 205 | ||
68 | #define LEX_ERROR_XML_NOT_FIRST 206 | ||
69 | |||
70 | amb | 363 | #define LEX_ERROR_CALLBACK 255 |
71 | amb | 348 | |
72 | amb | 363 | |
73 | amb | 334 | /* Lexer definitions */ |
74 | |||
75 | #define YY_SKIP_YYWRAP 1 /* Remove error with prototype of ..._yywrap */ | ||
76 | #ifndef yywrap | ||
77 | /*+ Needed in lex but does nothing. +*/ | ||
78 | #define yywrap() 1 | ||
79 | #endif | ||
80 | |||
81 | /*+ Reset the current string. +*/ | ||
82 | #define reset_string \ | ||
83 | amb | 389 | if(!string) string=(char*)malloc(16); \ |
84 | *string=0; \ | ||
85 | amb | 334 | stringused=0; |
86 | |||
87 | /*+ append information to the current string. +*/ | ||
88 | #define append_string(xx) \ | ||
89 | newlen=strlen(xx); \ | ||
90 | if((stringused+newlen)>=stringlen) \ | ||
91 | string=(char*)realloc((void*)string,stringlen=(stringused+newlen+16)); \ | ||
92 | strcpy(string+stringused,xx); \ | ||
93 | stringused+=newlen; | ||
94 | |||
95 | #define YY_NO_INPUT | ||
96 | |||
97 | |||
98 | /* Lexer functions and variables */ | ||
99 | |||
100 | extern int yylex(void); | ||
101 | |||
102 | static char *yylval=NULL; | ||
103 | |||
104 | amb | 374 | static int xmlparse_options; |
105 | |||
106 | amb | 334 | %} |
107 | |||
108 | amb | 344 | %option 8bit |
109 | amb | 348 | %option pointer |
110 | amb | 344 | %option batch |
111 | %option yylineno | ||
112 | amb | 348 | |
113 | amb | 344 | %option nodefault |
114 | %option perf-report | ||
115 | %option fast | ||
116 | amb | 348 | %option nounput |
117 | amb | 344 | |
118 | amb | 334 | |
119 | amb | 348 | /* Grammar based on http://www.w3.org/TR/2004/REC-xml-20040204/ but for ASCII not Unicode. */ |
120 | amb | 334 | |
121 | amb | 348 | S [ \t\r\n] |
122 | amb | 334 | |
123 | amb | 348 | letter [a-zA-Z] |
124 | digit [0-9] | ||
125 | xdigit [a-fA-F0-9] | ||
126 | |||
127 | namechar ({letter}|{digit}|[-._:]) | ||
128 | name ({letter}|[_:]){namechar}* | ||
129 | |||
130 | entityref &{name}; | ||
131 | charref &#({digit}+|x{xdigit}+); | ||
132 | |||
133 | |||
134 | amb | 334 | %x COMMENT |
135 | amb | 348 | %x CDATA |
136 | %x DOCTYPE | ||
137 | %x XML_DECL_START XML_DECL | ||
138 | %x TAG_START TAG | ||
139 | %x ATTR_KEY ATTR_VAL | ||
140 | %x END_TAG1 END_TAG2 | ||
141 | amb | 334 | %x DQUOTED SQUOTED |
142 | |||
143 | %% | ||
144 | /* Must use static variables since the parser returns often. */ | ||
145 | static char *string=NULL; | ||
146 | static int stringlen=0,stringused=0; | ||
147 | amb | 348 | static int after_attr=0; |
148 | amb | 334 | int newlen; |
149 | amb | 348 | int doctype_depth=0; |
150 | amb | 334 | |
151 | amb | 348 | /* Handle top level entities */ |
152 | amb | 334 | |
153 | "<!--" { BEGIN(COMMENT); } | ||
154 | amb | 348 | "<![CDATA[" { BEGIN(CDATA); } |
155 | "<!DOCTYPE" { BEGIN(DOCTYPE); doctype_depth=0; } | ||
156 | "</" { BEGIN(END_TAG1); } | ||
157 | "<?" { BEGIN(XML_DECL_START); } | ||
158 | amb | 334 | "<" { BEGIN(TAG_START); } |
159 | amb | 348 | ">" { return(LEX_ERROR_CLOSE); } |
160 | [^<>]+ { } | ||
161 | amb | 334 | |
162 | amb | 348 | /* Comments */ |
163 | amb | 334 | |
164 | amb | 348 | <COMMENT>"--->" { return(LEX_ERROR_COMMENT); } |
165 | <COMMENT>"-->" { BEGIN(INITIAL); } | ||
166 | <COMMENT>"--"[^->]+ { } | ||
167 | <COMMENT>[^-]+ { } | ||
168 | amb | 334 | <COMMENT>"-" { } |
169 | |||
170 | amb | 348 | /* CDATA */ |
171 | amb | 334 | |
172 | amb | 348 | <CDATA>"]]>" { BEGIN(INITIAL); } |
173 | <CDATA>"]" { } | ||
174 | <CDATA>[^]]+ { } | ||
175 | amb | 334 | |
176 | amb | 348 | /* CDATA */ |
177 | amb | 334 | |
178 | amb | 348 | <DOCTYPE>"<" { doctype_depth++; } |
179 | <DOCTYPE>">" { if(doctype_depth==0) BEGIN(INITIAL); else doctype_depth--; } | ||
180 | <DOCTYPE>[^<>]+ { } | ||
181 | amb | 334 | |
182 | amb | 348 | /* XML Declaration start */ |
183 | |||
184 | <XML_DECL_START>{name} { BEGIN(XML_DECL); yylval=yytext; return(LEX_XML_DECL_BEGIN); } | ||
185 | <XML_DECL_START>.|\n { return(LEX_ERROR_XML_DECL_START); } | ||
186 | |||
187 | /* Tag middle */ | ||
188 | |||
189 | <XML_DECL>"?>" { BEGIN(INITIAL); return(LEX_XML_DECL_FINISH); } | ||
190 | <XML_DECL>{S}+ { } | ||
191 | <XML_DECL>{name} { after_attr=XML_DECL; BEGIN(ATTR_KEY); yylval=yytext; return(LEX_ATTR_KEY); } | ||
192 | <XML_DECL>.|\n { return(LEX_ERROR_XML_DECL); } | ||
193 | |||
194 | /* Any tag start */ | ||
195 | |||
196 | <TAG_START>{name} { BEGIN(TAG); yylval=yytext; return(LEX_TAG_BEGIN); } | ||
197 | <TAG_START>.|\n { return(LEX_ERROR_TAG_START); } | ||
198 | |||
199 | /* End-tag start */ | ||
200 | |||
201 | <END_TAG1>{name} { BEGIN(END_TAG2); yylval=yytext; return(LEX_TAG_POP); } | ||
202 | <END_TAG1>.|\n { return(LEX_ERROR_END_TAG); } | ||
203 | |||
204 | <END_TAG2>">" { BEGIN(INITIAL); } | ||
205 | <END_TAG2>.|\n { return(LEX_ERROR_END_TAG); } | ||
206 | |||
207 | /* Any tag middle */ | ||
208 | |||
209 | amb | 334 | <TAG>"/>" { BEGIN(INITIAL); return(LEX_TAG_FINISH); } |
210 | <TAG>">" { BEGIN(INITIAL); return(LEX_TAG_PUSH); } | ||
211 | amb | 348 | <TAG>{S}+ { } |
212 | <TAG>{name} { after_attr=TAG; BEGIN(ATTR_KEY); yylval=yytext; return(LEX_ATTR_KEY); } | ||
213 | <TAG>.|\n { return(LEX_ERROR_TAG); } | ||
214 | amb | 334 | |
215 | amb | 348 | /* Attributes */ |
216 | amb | 334 | |
217 | amb | 348 | <ATTR_KEY>= { BEGIN(ATTR_VAL); } |
218 | <ATTR_KEY>.|\n { return(LEX_ERROR_ATTR); } | ||
219 | amb | 334 | |
220 | amb | 348 | <ATTR_VAL>\" { BEGIN(DQUOTED); reset_string; } |
221 | <ATTR_VAL>\' { BEGIN(SQUOTED); reset_string; } | ||
222 | <ATTR_VAL>.|\n { return(LEX_ERROR_ATTR); } | ||
223 | |||
224 | amb | 334 | /* Quoted strings */ |
225 | |||
226 | amb | 348 | <DQUOTED>\" { BEGIN(after_attr); yylval=string; return(LEX_ATTR_VAL); } |
227 | amb | 374 | <DQUOTED>{entityref} { if(xmlparse_options&XMLPARSE_RETURN_ATTR_ENCODED) {append_string(yytext);} |
228 | else { const char *str=ParseXML_Decode_Entity_Ref(yytext); if(str) {append_string(str);} else {yylval=yytext; return(LEX_ERROR_ENTITY_REF);} } } | ||
229 | <DQUOTED>{charref} { if(xmlparse_options&XMLPARSE_RETURN_ATTR_ENCODED) {append_string(yytext);} | ||
230 | else { const char *str=ParseXML_Decode_Char_Ref(yytext); if(str) {append_string(str);} else {yylval=yytext; return(LEX_ERROR_CHAR_REF);} } } | ||
231 | amb | 356 | <DQUOTED>[<>&] { yylval=yytext; return(LEX_ERROR_ATTR_VAL); } |
232 | amb | 348 | <DQUOTED>[^<>&\"]+ { append_string(yytext); } |
233 | amb | 334 | |
234 | amb | 348 | <SQUOTED>\' { BEGIN(after_attr); yylval=string; return(LEX_ATTR_VAL); } |
235 | amb | 374 | <SQUOTED>{entityref} { if(xmlparse_options&XMLPARSE_RETURN_ATTR_ENCODED) {append_string(yytext);} |
236 | else { const char *str=ParseXML_Decode_Entity_Ref(yytext); if(str) {append_string(str);} else {yylval=yytext; return(LEX_ERROR_ENTITY_REF);} } } | ||
237 | <SQUOTED>{charref} { if(xmlparse_options&XMLPARSE_RETURN_ATTR_ENCODED) {append_string(yytext);} | ||
238 | else { const char *str=ParseXML_Decode_Char_Ref(yytext); if(str) {append_string(str);} else {yylval=yytext; return(LEX_ERROR_CHAR_REF);} } } | ||
239 | amb | 356 | <SQUOTED>[<>&] { yylval=yytext; return(LEX_ERROR_ATTR_VAL); } |
240 | amb | 348 | <SQUOTED>[^<>&\']+ { append_string(yytext); } |
241 | amb | 334 | |
242 | /* End of file */ | ||
243 | |||
244 | amb | 368 | <<EOF>> { free(string); string=NULL; stringlen=stringused=0; BEGIN(INITIAL); return(LEX_EOF); } |
245 | amb | 334 | |
246 | %% | ||
247 | |||
248 | |||
249 | amb | 348 | /*++++++++++++++++++++++++++++++++++++++ |
250 | A function to call the callback function with the parameters needed. | ||
251 | |||
252 | amb | 363 | int call_callback Returns 1 if the callback returned with an error. |
253 | |||
254 | amb | 356 | const char *name The name of the tag. |
255 | amb | 348 | |
256 | amb | 363 | int (*callback)() The callback function. |
257 | amb | 348 | |
258 | int type The type of tag (start and/or end). | ||
259 | |||
260 | int nattributes The number of attributes collected. | ||
261 | |||
262 | char *attributes[XMLPARSE_MAX_ATTRS] The list of attributes. | ||
263 | ++++++++++++++++++++++++++++++++++++++*/ | ||
264 | |||
265 | amb | 363 | static inline int call_callback(const char *name,int (*callback)(),int type,int nattributes,char *attributes[XMLPARSE_MAX_ATTRS]) |
266 | amb | 344 | { |
267 | switch(nattributes) | ||
268 | { | ||
269 | amb | 373 | case 0: return (*callback)(name,type); |
270 | case 1: return (*callback)(name,type,attributes[0]); | ||
271 | case 2: return (*callback)(name,type,attributes[0],attributes[1]); | ||
272 | case 3: return (*callback)(name,type,attributes[0],attributes[1],attributes[2]); | ||
273 | case 4: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3]); | ||
274 | case 5: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4]); | ||
275 | case 6: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5]); | ||
276 | case 7: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6]); | ||
277 | case 8: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7]); | ||
278 | case 9: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8]); | ||
279 | case 10: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8],attributes[9]); | ||
280 | case 11: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8],attributes[9],attributes[10]); | ||
281 | case 12: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8],attributes[9],attributes[10],attributes[11]); | ||
282 | case 13: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8],attributes[9],attributes[10],attributes[11],attributes[12]); | ||
283 | case 14: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8],attributes[9],attributes[10],attributes[11],attributes[12],attributes[13]); | ||
284 | case 15: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8],attributes[9],attributes[10],attributes[11],attributes[12],attributes[13],attributes[14]); | ||
285 | case 16: return (*callback)(name,type,attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],attributes[5],attributes[6],attributes[7],attributes[8],attributes[9],attributes[10],attributes[11],attributes[12],attributes[13],attributes[14],attributes[15]); | ||
286 | amb | 334 | |
287 | amb | 344 | default: |
288 | amb | 363 | fprintf(stderr,"XML Parser: Error on line %d: too many attributes for tag '%s' source code needs changing.\n",yylineno,name); |
289 | amb | 344 | exit(1); |
290 | } | ||
291 | } | ||
292 | |||
293 | |||
294 | amb | 334 | /*++++++++++++++++++++++++++++++++++++++ |
295 | Parse the XML and call the functions for each tag as seen. | ||
296 | |||
297 | amb | 348 | int ParseXML Returns 0 if OK or something else in case of an error. |
298 | |||
299 | amb | 334 | FILE *file The file to parse. |
300 | |||
301 | xmltag **tags The array of pointers to tags for the top level. | ||
302 | amb | 337 | |
303 | amb | 366 | int options A list of XML Parser options OR-ed together. |
304 | amb | 334 | ++++++++++++++++++++++++++++++++++++++*/ |
305 | |||
306 | amb | 366 | int ParseXML(FILE *file,xmltag **tags,int options) |
307 | amb | 334 | { |
308 | int yychar,i; | ||
309 | |||
310 | amb | 351 | char *attributes[XMLPARSE_MAX_ATTRS]={NULL}; |
311 | amb | 334 | int attribute=0; |
312 | |||
313 | int stackdepth=0,stackused=0; | ||
314 | amb | 344 | xmltag ***tags_stack=NULL; |
315 | xmltag **tag_stack=NULL; | ||
316 | amb | 334 | xmltag *tag=NULL; |
317 | |||
318 | amb | 368 | /* The actual parser. */ |
319 | amb | 334 | |
320 | amb | 374 | xmlparse_options=options; |
321 | |||
322 | amb | 334 | yyin=file; |
323 | |||
324 | amb | 368 | yyrestart(yyin); |
325 | amb | 334 | |
326 | amb | 396 | yylineno=1; |
327 | |||
328 | amb | 368 | BEGIN(INITIAL); |
329 | amb | 334 | |
330 | amb | 348 | do |
331 | { | ||
332 | yychar=yylex(); | ||
333 | |||
334 | amb | 334 | switch(yychar) |
335 | { | ||
336 | amb | 348 | /* The start of a tag for an XML declaration */ |
337 | |||
338 | case LEX_XML_DECL_BEGIN: | ||
339 | |||
340 | if(tag_stack) | ||
341 | { | ||
342 | fprintf(stderr,"XML Parser: Error on line %d: XML declaration not before all other tags.\n",yylineno); | ||
343 | yychar=LEX_ERROR_XML_NOT_FIRST; | ||
344 | break; | ||
345 | } | ||
346 | |||
347 | amb | 334 | /* The start of a tag for an element */ |
348 | |||
349 | case LEX_TAG_BEGIN: | ||
350 | |||
351 | tag=NULL; | ||
352 | |||
353 | for(i=0;tags[i];i++) | ||
354 | if(!strcasecmp(yylval,tags[i]->name)) | ||
355 | { | ||
356 | tag=tags[i]; | ||
357 | |||
358 | amb | 356 | for(i=0;i<tag->nattributes;i++) |
359 | amb | 351 | if(attributes[i]) |
360 | { | ||
361 | free(attributes[i]); | ||
362 | attributes[i]=NULL; | ||
363 | } | ||
364 | amb | 334 | |
365 | break; | ||
366 | } | ||
367 | |||
368 | if(tag==NULL) | ||
369 | { | ||
370 | amb | 348 | fprintf(stderr,"XML Parser: Error on line %d: unexpected tag '%s'.\n",yylineno,yylval); |
371 | yychar=LEX_ERROR_UNEXP_TAG; | ||
372 | amb | 334 | } |
373 | amb | 348 | |
374 | amb | 334 | break; |
375 | |||
376 | /* The end of the start-tag for an element */ | ||
377 | |||
378 | case LEX_TAG_PUSH: | ||
379 | |||
380 | if(stackused==stackdepth) | ||
381 | amb | 344 | { |
382 | tag_stack =(xmltag**) realloc((void*)tag_stack ,(stackdepth+=8)*sizeof(xmltag*)); | ||
383 | tags_stack=(xmltag***)realloc((void*)tags_stack,(stackdepth+=8)*sizeof(xmltag**)); | ||
384 | } | ||
385 | amb | 334 | |
386 | amb | 344 | tag_stack [stackused]=tag; |
387 | tags_stack[stackused]=tags; | ||
388 | stackused++; | ||
389 | |||
390 | if(tag->callback) | ||
391 | amb | 363 | if(call_callback(tag->name,tag->callback,XMLPARSE_TAG_START,tag->nattributes,attributes)) |
392 | yychar=LEX_ERROR_CALLBACK; | ||
393 | amb | 344 | |
394 | amb | 334 | tags=tag->subtags; |
395 | |||
396 | amb | 344 | break; |
397 | |||
398 | amb | 348 | /* The end of the empty-element-tag for an XML declaration */ |
399 | |||
400 | case LEX_XML_DECL_FINISH: | ||
401 | |||
402 | amb | 334 | /* The end of the empty-element-tag for an element */ |
403 | |||
404 | case LEX_TAG_FINISH: | ||
405 | |||
406 | if(tag->callback) | ||
407 | amb | 363 | if(call_callback(tag->name,tag->callback,XMLPARSE_TAG_START|XMLPARSE_TAG_END,tag->nattributes,attributes)) |
408 | yychar=LEX_ERROR_CALLBACK; | ||
409 | amb | 334 | |
410 | amb | 344 | if(stackused>0) |
411 | tag=tag_stack[stackused-1]; | ||
412 | else | ||
413 | tag=NULL; | ||
414 | amb | 334 | |
415 | break; | ||
416 | |||
417 | /* The end of the end-tag for an element */ | ||
418 | |||
419 | case LEX_TAG_POP: | ||
420 | |||
421 | amb | 344 | stackused--; |
422 | tags=tags_stack[stackused]; | ||
423 | tag =tag_stack [stackused]; | ||
424 | amb | 334 | |
425 | amb | 348 | if(strcmp(tag->name,yylval)) |
426 | { | ||
427 | fprintf(stderr,"XML Parser: Error on line %d: end tag '</%s>' doesn't match start tag '<%s ...>'.\n",yylineno,yylval,tag->name); | ||
428 | yychar=LEX_ERROR_UNBALANCED; | ||
429 | } | ||
430 | |||
431 | if(stackused<0) | ||
432 | { | ||
433 | fprintf(stderr,"XML Parser: Error on line %d: end tag '</%s>' seen but there was no start tag '<%s ...>'.\n",yylineno,yylval,yylval); | ||
434 | yychar=LEX_ERROR_NO_START; | ||
435 | } | ||
436 | |||
437 | amb | 356 | for(i=0;i<tag->nattributes;i++) |
438 | amb | 351 | if(attributes[i]) |
439 | { | ||
440 | free(attributes[i]); | ||
441 | attributes[i]=NULL; | ||
442 | } | ||
443 | |||
444 | amb | 344 | if(tag->callback) |
445 | amb | 363 | if(call_callback(tag->name,tag->callback,XMLPARSE_TAG_END,tag->nattributes,attributes)) |
446 | yychar=LEX_ERROR_CALLBACK; | ||
447 | amb | 344 | |
448 | amb | 348 | if(stackused>0) |
449 | tag=tag_stack[stackused-1]; | ||
450 | else | ||
451 | tag=NULL; | ||
452 | amb | 344 | |
453 | amb | 334 | break; |
454 | |||
455 | /* An attribute key */ | ||
456 | |||
457 | case LEX_ATTR_KEY: | ||
458 | |||
459 | attribute=-1; | ||
460 | |||
461 | amb | 356 | for(i=0;i<tag->nattributes;i++) |
462 | amb | 334 | if(!strcasecmp(yylval,tag->attributes[i])) |
463 | { | ||
464 | attribute=i; | ||
465 | |||
466 | break; | ||
467 | } | ||
468 | |||
469 | if(attribute==-1) | ||
470 | { | ||
471 | amb | 366 | if((options&XMLPARSE_UNKNOWN_ATTRIBUTES)==XMLPARSE_UNKNOWN_ATTR_ERROR || |
472 | ((options&XMLPARSE_UNKNOWN_ATTRIBUTES)==XMLPARSE_UNKNOWN_ATTR_ERRNONAME && !strchr(yylval,':'))) | ||
473 | amb | 337 | { |
474 | amb | 348 | fprintf(stderr,"XML Parser: Error on line %d: unexpected attribute '%s' for tag '%s'.\n",yylineno,yylval,tag->name); |
475 | yychar=LEX_ERROR_UNEXP_ATT; | ||
476 | amb | 337 | } |
477 | amb | 366 | else if((options&XMLPARSE_UNKNOWN_ATTRIBUTES)==XMLPARSE_UNKNOWN_ATTR_WARN) |
478 | amb | 348 | fprintf(stderr,"XML Parser: Warning on line %d: unexpected attribute '%s' for tag '%s'.\n",yylineno,yylval,tag->name); |
479 | amb | 334 | } |
480 | amb | 348 | |
481 | amb | 334 | break; |
482 | |||
483 | /* An attribute value */ | ||
484 | |||
485 | case LEX_ATTR_VAL: | ||
486 | |||
487 | amb | 363 | if(tag->callback && attribute!=-1 && yylval) |
488 | amb | 334 | attributes[attribute]=strcpy(malloc(strlen(yylval)+1),yylval); |
489 | amb | 348 | |
490 | break; | ||
491 | |||
492 | /* End of file */ | ||
493 | |||
494 | case LEX_EOF: | ||
495 | |||
496 | if(tag) | ||
497 | { | ||
498 | fprintf(stderr,"XML Parser: Error on line %d: end of file seen without end tag '</%s>'.\n",yylineno,tag->name); | ||
499 | yychar=LEX_ERROR_UNEXP_EOF; | ||
500 | } | ||
501 | |||
502 | break; | ||
503 | |||
504 | case LEX_ERROR_TAG_START: | ||
505 | fprintf(stderr,"XML Parser: Error on line %d: character '<' seen not at start of tag.\n",yylineno); | ||
506 | break; | ||
507 | |||
508 | case LEX_ERROR_XML_DECL_START: | ||
509 | fprintf(stderr,"XML Parser: Error on line %d: characters '<?' seen not at start of XML declaration.\n",yylineno); | ||
510 | break; | ||
511 | |||
512 | case LEX_ERROR_TAG: | ||
513 | fprintf(stderr,"XML Parser: Error on line %d: invalid character seen inside tag '<%s...>'.\n",yylineno,tag->name); | ||
514 | break; | ||
515 | |||
516 | case LEX_ERROR_XML_DECL: | ||
517 | fprintf(stderr,"XML Parser: Error on line %d: invalid character seen inside XML declaration '<?%s...>'.\n",yylineno,tag->name); | ||
518 | break; | ||
519 | |||
520 | case LEX_ERROR_ATTR: | ||
521 | fprintf(stderr,"XML Parser: Error on line %d: invalid attribute definition seen in tag.\n",yylineno); | ||
522 | break; | ||
523 | |||
524 | case LEX_ERROR_END_TAG: | ||
525 | fprintf(stderr,"XML Parser: Error on line %d: invalid character seen in end-tag.\n",yylineno); | ||
526 | break; | ||
527 | |||
528 | case LEX_ERROR_COMMENT: | ||
529 | fprintf(stderr,"XML Parser: Error on line %d: invalid comment seen.\n",yylineno); | ||
530 | break; | ||
531 | |||
532 | case LEX_ERROR_CLOSE: | ||
533 | fprintf(stderr,"XML Parser: Error on line %d: character '>' seen not at end of tag.\n",yylineno); | ||
534 | break; | ||
535 | amb | 351 | |
536 | case LEX_ERROR_ATTR_VAL: | ||
537 | amb | 356 | fprintf(stderr,"XML Parser: Error on line %d: invalid character '%s' seen in attribute value.\n",yylineno,yylval); |
538 | amb | 351 | break; |
539 | amb | 356 | |
540 | case LEX_ERROR_ENTITY_REF: | ||
541 | fprintf(stderr,"XML Parser: Error on line %d: invalid entity reference '%s' seen in attribute value.\n",yylineno,yylval); | ||
542 | break; | ||
543 | |||
544 | case LEX_ERROR_CHAR_REF: | ||
545 | fprintf(stderr,"XML Parser: Error on line %d: invalid character reference '%s' seen in attribute value.\n",yylineno,yylval); | ||
546 | break; | ||
547 | amb | 334 | } |
548 | amb | 348 | } |
549 | while(yychar>LEX_EOF && yychar<LEX_ERROR); | ||
550 | amb | 334 | |
551 | /* Delete the tagdata */ | ||
552 | |||
553 | amb | 356 | for(i=0;i<XMLPARSE_MAX_ATTRS;i++) |
554 | amb | 334 | if(attributes[i]) |
555 | free(attributes[i]); | ||
556 | |||
557 | if(stackdepth) | ||
558 | amb | 344 | free(tags_stack); |
559 | amb | 348 | |
560 | return(yychar); | ||
561 | amb | 334 | } |
562 | amb | 348 | |
563 | |||
564 | /*++++++++++++++++++++++++++++++++++++++ | ||
565 | Return the current parser line number. | ||
566 | |||
567 | amb | 388 | unsigned long ParseXML_LineNumber Returns the line number. |
568 | amb | 348 | ++++++++++++++++++++++++++++++++++++++*/ |
569 | |||
570 | amb | 388 | unsigned long ParseXML_LineNumber(void) |
571 | amb | 348 | { |
572 | return(yylineno); | ||
573 | } | ||
574 | amb | 356 | |
575 | |||
576 | /*++++++++++++++++++++++++++++++++++++++ | ||
577 | Convert an XML entity reference into an ASCII string. | ||
578 | |||
579 | amb | 377 | char *ParseXML_Decode_Entity_Ref Returns a pointer to the replacement decoded string. |
580 | amb | 356 | |
581 | const char *string The entity reference string. | ||
582 | ++++++++++++++++++++++++++++++++++++++*/ | ||
583 | |||
584 | amb | 377 | char *ParseXML_Decode_Entity_Ref(const char *string) |
585 | amb | 356 | { |
586 | if(!strcmp(string,"&")) return("&"); | ||
587 | if(!strcmp(string,"<")) return("<"); | ||
588 | if(!strcmp(string,">")) return(">"); | ||
589 | if(!strcmp(string,"'")) return("'"); | ||
590 | if(!strcmp(string,""")) return("\""); | ||
591 | return(NULL); | ||
592 | } | ||
593 | |||
594 | |||
595 | /*++++++++++++++++++++++++++++++++++++++ | ||
596 | Convert an XML character reference into an ASCII string. | ||
597 | |||
598 | amb | 377 | char *ParseXML_Decode_Char_Ref Returns a pointer to the replacement decoded string. |
599 | amb | 356 | |
600 | const char *string The character reference string. | ||
601 | ++++++++++++++++++++++++++++++++++++++*/ | ||
602 | |||
603 | amb | 377 | char *ParseXML_Decode_Char_Ref(const char *string) |
604 | amb | 356 | { |
605 | static char result[2]=" "; | ||
606 | long int val; | ||
607 | |||
608 | if(string[2]=='x') val=strtol(string+3,NULL,16); | ||
609 | else val=strtol(string+2,NULL,10); | ||
610 | |||
611 | if(val<0 || val>255) | ||
612 | return(NULL); | ||
613 | |||
614 | result[0]=val&0xff; | ||
615 | |||
616 | return(result); | ||
617 | } | ||
618 | |||
619 | |||
620 | /*++++++++++++++++++++++++++++++++++++++ | ||
621 | Convert a string into something that is safe to output in an XML file. | ||
622 | |||
623 | amb | 377 | char *ParseXML_Encode_Safe_XML Returns a pointer to the replacement encoded string (or the original if no change needed). |
624 | amb | 356 | |
625 | const char *string The string to convert. | ||
626 | ++++++++++++++++++++++++++++++++++++++*/ | ||
627 | |||
628 | amb | 377 | char *ParseXML_Encode_Safe_XML(const char *string) |
629 | amb | 356 | { |
630 | static const char hexstring[17]="0123456789ABCDEF"; | ||
631 | int i=0,j=0,len; | ||
632 | char *result; | ||
633 | |||
634 | for(i=0;string[i];i++) | ||
635 | if(string[i]=='<' || string[i]=='>' || string[i]=='&' || string[i]=='\'' || string[i]=='"' || string[i]<32 || string[i]>126) | ||
636 | break; | ||
637 | |||
638 | if(!string[i]) | ||
639 | amb | 377 | return((char*)string); |
640 | amb | 356 | |
641 | len=i+256-6; | ||
642 | |||
643 | result=(char*)malloc(len+7); | ||
644 | strncpy(result,string,i); | ||
645 | |||
646 | do | ||
647 | { | ||
648 | for(;j<len && string[i];i++) | ||
649 | if(string[i]=='<') | ||
650 | { | ||
651 | result[j++]='&'; | ||
652 | result[j++]='l'; | ||
653 | result[j++]='t'; | ||
654 | result[j++]=';'; | ||
655 | } | ||
656 | else if(string[i]=='>') | ||
657 | { | ||
658 | result[j++]='&'; | ||
659 | result[j++]='g'; | ||
660 | result[j++]='t'; | ||
661 | result[j++]=';'; | ||
662 | } | ||
663 | else if(string[i]=='&') | ||
664 | { | ||
665 | result[j++]='&'; | ||
666 | result[j++]='a'; | ||
667 | result[j++]='m'; | ||
668 | result[j++]='p'; | ||
669 | result[j++]=';'; | ||
670 | } | ||
671 | else if(string[i]=='\'') | ||
672 | { | ||
673 | result[j++]='&'; | ||
674 | result[j++]='a'; | ||
675 | result[j++]='p'; | ||
676 | result[j++]='o'; | ||
677 | result[j++]='s'; | ||
678 | result[j++]=';'; | ||
679 | } | ||
680 | else if(string[i]=='"') | ||
681 | { | ||
682 | result[j++]='&'; | ||
683 | result[j++]='q'; | ||
684 | result[j++]='u'; | ||
685 | result[j++]='o'; | ||
686 | result[j++]='t'; | ||
687 | result[j++]=';'; | ||
688 | } | ||
689 | else if(string[i]<32 || string[i]>126) | ||
690 | { | ||
691 | result[j++]='&'; | ||
692 | result[j++]='#'; | ||
693 | result[j++]='x'; | ||
694 | result[j++]=hexstring[(string[i]&0xf0)>>4]; | ||
695 | result[j++]=hexstring[ string[i]&0x0f ]; | ||
696 | result[j++]=';'; | ||
697 | } | ||
698 | else | ||
699 | result[j++]=string[i]; | ||
700 | |||
701 | if(string[i]) /* Not finished */ | ||
702 | { | ||
703 | len+=256; | ||
704 | result=(char*)realloc((void*)result,len+7); | ||
705 | } | ||
706 | } | ||
707 | while(string[i]); | ||
708 | |||
709 | result[j]=0; | ||
710 | |||
711 | return(result); | ||
712 | } | ||
713 | amb | 363 | |
714 | |||
715 | /*++++++++++++++++++++++++++++++++++++++ | ||
716 | Convert a string to a integer (checking that it really is a integer). | ||
717 | |||
718 | int ParseXML_GetInteger Returns 1 if a integer could be found or 0 otherwise. | ||
719 | |||
720 | const char *string The string to be parsed. | ||
721 | |||
722 | int *number Returns the number. | ||
723 | ++++++++++++++++++++++++++++++++++++++*/ | ||
724 | |||
725 | int ParseXML_GetInteger(const char *string,int *number) | ||
726 | { | ||
727 | const char *p=string; | ||
728 | |||
729 | if(*p=='-' || *p=='+') | ||
730 | p++; | ||
731 | |||
732 | while(isdigit(*p)) | ||
733 | p++; | ||
734 | |||
735 | if(*p) | ||
736 | return(0); | ||
737 | |||
738 | *number=atoi(string); | ||
739 | |||
740 | return(1); | ||
741 | } | ||
742 | |||
743 | |||
744 | /*++++++++++++++++++++++++++++++++++++++ | ||
745 | Convert a string to a floating point number (checking that it really is a number). | ||
746 | |||
747 | int ParseXML_GetFloating Returns 1 if a number could be found or 0 otherwise. | ||
748 | |||
749 | const char *string The string to be parsed. | ||
750 | |||
751 | int *number Returns the number. | ||
752 | ++++++++++++++++++++++++++++++++++++++*/ | ||
753 | |||
754 | int ParseXML_GetFloating(const char *string,double *number) | ||
755 | { | ||
756 | const char *p=string; | ||
757 | |||
758 | if(*p=='-' || *p=='+') | ||
759 | p++; | ||
760 | |||
761 | while(isdigit(*p) || *p=='.') | ||
762 | p++; | ||
763 | |||
764 | amb | 396 | if(*p=='e' || *p=='E') |
765 | { | ||
766 | p++; | ||
767 | |||
768 | if(*p=='-' || *p=='+') | ||
769 | p++; | ||
770 | |||
771 | while(isdigit(*p)) | ||
772 | p++; | ||
773 | } | ||
774 | |||
775 | amb | 363 | if(*p) |
776 | return(0); | ||
777 | |||
778 | *number=atof(string); | ||
779 | |||
780 | return(1); | ||
781 | } |
Properties
Name | Value |
---|---|
cvs:description | A simple generic XML parser. |