1 /**
2 ****************************************************************************
3 * <P> XML.c - implementation file for basic XML parser written in ANSI C++
4 * for portability. It works by using recursion and a node tree for breaking
5 * down the elements of an XML document. </P>
6 *
7 * @version V2.43
8 * @author Frank Vanden Berghen
9 *
10 * NOTE:
11 *
12 * If you add "#define STRICT_PARSING", on the first line of this file
13 * the parser will see the following XML-stream:
14 * <a><b>some text</b><b>other text </a>
15 * as an error. Otherwise, this tring will be equivalent to:
16 * <a><b>some text</b><b>other text</b></a>
17 *
18 * NOTE:
19 *
20 * If you add "#define APPROXIMATE_PARSING" on the first line of this file
21 * the parser will see the following XML-stream:
22 * <data name="n1">
23 * <data name="n2">
24 * <data name="n3" />
25 * as equivalent to the following XML-stream:
26 * <data name="n1" />
27 * <data name="n2" />
28 * <data name="n3" />
29 * This can be useful for badly-formed XML-streams but prevent the use
30 * of the following XML-stream (problem is: tags at contiguous levels
31 * have the same names):
32 * <data name="n1">
33 * <data name="n2">
34 * <data name="n3" />
35 * </data>
36 * </data>
37 *
38 * NOTE:
39 *
40 * If you add "#define _XMLPARSER_NO_MESSAGEBOX_" on the first line of this file
41 * the "openFileHelper" function will always display error messages inside the
42 * console instead of inside a message-box-window. Message-box-windows are
43 * available on windows 9x/NT/2000/XP/Vista only.
44 *
45 * Copyright (c) 2002, Business-Insight
46 * <a href="http://www.Business-Insight.com">Business-Insight</a>
47 * All rights reserved.
48 * See the file "AFPL-license.txt" about the licensing terms
49 *
50 ****************************************************************************
51 */
52 #ifndef _CRT_SECURE_NO_DEPRECATE
53 #define _CRT_SECURE_NO_DEPRECATE
54 #endif
55 #include "xmlParser.h"
56 #ifdef _XMLWINDOWS
57 //#ifdef _DEBUG
58 //#define _CRTDBG_MAP_ALLOC
59 //#include <crtdbg.h>
60 //#endif
61 #define WIN32_LEAN_AND_MEAN
62 #include <Windows.h> // to have IsTextUnicode, MultiByteToWideChar, WideCharToMultiByte to handle unicode files
63 // to have "MessageBoxA" to display error messages for openFilHelper
64 #endif
65
66 #include <memory.h>
67 #include <assert.h>
68 #include <stdio.h>
69 #include <string.h>
70 #include <stdlib.h>
71
getVersion()72 XMLCSTR XMLNode::getVersion() { return _CXML("v2.43"); }
freeXMLString(XMLSTR t)73 void freeXMLString(XMLSTR t){if(t)free(t);}
74
75 static XMLNode::XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8;
76 static char guessWideCharChars=1, dropWhiteSpace=1, removeCommentsInMiddleOfText=1;
77
mmin(const int t1,const int t2)78 inline int mmin( const int t1, const int t2 ) { return t1 < t2 ? t1 : t2; }
79
80 // You can modify the initialization of the variable "XMLClearTags" below
81 // to change the clearTags that are currently recognized by the library.
82 // The number on the second columns is the length of the string inside the
83 // first column.
84 // The "<!DOCTYPE" declaration must be the second in the list.
85 // The "<!--" declaration must be the third in the list.
86 // All ClearTag Strings must start with the '<' character.
87 typedef struct { XMLCSTR lpszOpen; int openTagLen; XMLCSTR lpszClose;} ALLXMLClearTag;
88 static ALLXMLClearTag XMLClearTags[] =
89 {
90 { _CXML("<![CDATA["),9, _CXML("]]>") },
91 { _CXML("<!DOCTYPE"),9, _CXML(">") },
92 { _CXML("<!--") ,4, _CXML("-->") },
93 { _CXML("<PRE>") ,5, _CXML("</PRE>") },
94 // { _CXML("<Script>") ,8, _CXML("</Script>")},
95 { NULL ,0, NULL }
96 };
97
98 // You can modify the initialization of the variable "XMLEntities" below
99 // to change the character entities that are currently recognized by the library.
100 // The number on the second columns is the length of the string inside the
101 // first column. Additionally, the syntaxes " " and " " are recognized.
102 typedef struct { XMLCSTR s; int l; XMLCHAR c;} XMLCharacterEntity;
103 static XMLCharacterEntity XMLEntities[] =
104 {
105 { _CXML("&" ), 5, _CXML('&' )},
106 { _CXML("<" ), 4, _CXML('<' )},
107 { _CXML(">" ), 4, _CXML('>' )},
108 { _CXML("""), 6, _CXML('\"')},
109 { _CXML("'"), 6, _CXML('\'')},
110 { NULL , 0, '\0' }
111 };
112
113 // When rendering the XMLNode to a string (using the "createXMLString" function),
114 // you can ask for a beautiful formatting. This formatting is using the
115 // following indentation character:
116 #define INDENTCHAR _CXML('\t')
117
118 // The following function parses the XML errors into a user friendly string.
119 // You can edit this to change the output language of the library to something else.
getError(XMLError xerror)120 XMLCSTR XMLNode::getError(XMLError xerror)
121 {
122 switch (xerror)
123 {
124 case eXMLErrorNone: return _CXML("No error");
125 case eXMLErrorMissingEndTag: return _CXML("Warning: Unmatched end tag");
126 case eXMLErrorNoXMLTagFound: return _CXML("Warning: No XML tag found");
127 case eXMLErrorEmpty: return _CXML("Error: No XML data");
128 case eXMLErrorMissingTagName: return _CXML("Error: Missing start tag name");
129 case eXMLErrorMissingEndTagName: return _CXML("Error: Missing end tag name");
130 case eXMLErrorUnmatchedEndTag: return _CXML("Error: Unmatched end tag");
131 case eXMLErrorUnmatchedEndClearTag: return _CXML("Error: Unmatched clear tag end");
132 case eXMLErrorUnexpectedToken: return _CXML("Error: Unexpected token found");
133 case eXMLErrorNoElements: return _CXML("Error: No elements found");
134 case eXMLErrorFileNotFound: return _CXML("Error: File not found");
135 case eXMLErrorFirstTagNotFound: return _CXML("Error: First Tag not found");
136 case eXMLErrorUnknownCharacterEntity:return _CXML("Error: Unknown character entity");
137 case eXMLErrorCharacterCodeAbove255: return _CXML("Error: Character code above 255 is forbidden in MultiByte char mode.");
138 case eXMLErrorCharConversionError: return _CXML("Error: unable to convert between WideChar and MultiByte chars");
139 case eXMLErrorCannotOpenWriteFile: return _CXML("Error: unable to open file for writing");
140 case eXMLErrorCannotWriteFile: return _CXML("Error: cannot write into file");
141
142 case eXMLErrorBase64DataSizeIsNotMultipleOf4: return _CXML("Warning: Base64-string length is not a multiple of 4");
143 case eXMLErrorBase64DecodeTruncatedData: return _CXML("Warning: Base64-string is truncated");
144 case eXMLErrorBase64DecodeIllegalCharacter: return _CXML("Error: Base64-string contains an illegal character");
145 case eXMLErrorBase64DecodeBufferTooSmall: return _CXML("Error: Base64 decode output buffer is too small");
146 };
147 return _CXML("Unknown");
148 }
149
150 /////////////////////////////////////////////////////////////////////////
151 // Here start the abstraction layer to be OS-independent //
152 /////////////////////////////////////////////////////////////////////////
153
154 // Here is an abstraction layer to access some common string manipulation functions.
155 // The abstraction layer is currently working for gcc, Microsoft Visual Studio 6.0,
156 // Microsoft Visual Studio .NET, CC (sun compiler) and Borland C++.
157 // If you plan to "port" the library to a new system/compiler, all you have to do is
158 // to edit the following lines.
159 #ifdef XML_NO_WIDE_CHAR
myIsTextWideChar(const void * b,int len)160 char myIsTextWideChar(const void *b, int len) { return FALSE; }
161 #else
162 #if defined (UNDER_CE) || !defined(_XMLWINDOWS)
myIsTextWideChar(const void * b,int len)163 char myIsTextWideChar(const void *b, int len) // inspired by the Wine API: RtlIsTextUnicode
164 {
165 #ifdef sun
166 // for SPARC processors: wchar_t* buffers must always be alligned, otherwise it's a char* buffer.
167 if ((((unsigned long)b)%sizeof(wchar_t))!=0) return FALSE;
168 #endif
169 const wchar_t *s=(const wchar_t*)b;
170
171 // buffer too small:
172 if (len<(int)sizeof(wchar_t)) return FALSE;
173
174 // odd length test
175 if (len&1) return FALSE;
176
177 /* only checks the first 256 characters */
178 len=mmin(256,len/sizeof(wchar_t));
179
180 // Check for the special byte order:
181 if (*((unsigned short*)s) == 0xFFFE) return TRUE; // IS_TEXT_UNICODE_REVERSE_SIGNATURE;
182 if (*((unsigned short*)s) == 0xFEFF) return TRUE; // IS_TEXT_UNICODE_SIGNATURE
183
184 // checks for ASCII characters in the UNICODE stream
185 int i,stats=0;
186 for (i=0; i<len; i++) if (s[i]<=(unsigned short)255) stats++;
187 if (stats>len/2) return TRUE;
188
189 // Check for UNICODE NULL chars
190 for (i=0; i<len; i++) if (!s[i]) return TRUE;
191
192 return FALSE;
193 }
194 #else
myIsTextWideChar(const void * b,int l)195 char myIsTextWideChar(const void *b,int l) { return (char)IsTextUnicode((CONST LPVOID)b,l,NULL); }
196 #endif
197 #endif
198
199 #ifdef _XMLWINDOWS
200 // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
201 #ifdef _XMLWIDECHAR
myMultiByteToWideChar(const char * s,XMLNode::XMLCharEncoding ce)202 wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
203 {
204 int i;
205 if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,NULL,0);
206 else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,NULL,0);
207 if (i<0) return NULL;
208 wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(XMLCHAR));
209 if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,d,i);
210 else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,d,i);
211 d[i]=0;
212 return d;
213 }
xfopen(XMLCSTR filename,XMLCSTR mode)214 static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return _wfopen(filename,mode); }
xstrlen(XMLCSTR c)215 static inline int xstrlen(XMLCSTR c) { return (int)wcslen(c); }
xstrnicmp(XMLCSTR c1,XMLCSTR c2,int l)216 static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _wcsnicmp(c1,c2,l);}
xstrncmp(XMLCSTR c1,XMLCSTR c2,int l)217 static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
xstricmp(XMLCSTR c1,XMLCSTR c2)218 static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _wcsicmp(c1,c2); }
xstrstr(XMLCSTR c1,XMLCSTR c2)219 static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
xstrcpy(XMLSTR c1,XMLCSTR c2)220 static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
221 #else
myWideCharToMultiByte(const wchar_t * s)222 char *myWideCharToMultiByte(const wchar_t *s)
223 {
224 UINT codePage=CP_ACP; if (characterEncoding==XMLNode::char_encoding_UTF8) codePage=CP_UTF8;
225 int i=(int)WideCharToMultiByte(codePage, // code page
226 0, // performance and mapping flags
227 s, // wide-character string
228 -1, // number of chars in string
229 NULL, // buffer for new string
230 0, // size of buffer
231 NULL, // default for unmappable chars
232 NULL // set when default char used
233 );
234 if (i<0) return NULL;
235 char *d=(char*)malloc(i+1);
236 WideCharToMultiByte(codePage, // code page
237 0, // performance and mapping flags
238 s, // wide-character string
239 -1, // number of chars in string
240 d, // buffer for new string
241 i, // size of buffer
242 NULL, // default for unmappable chars
243 NULL // set when default char used
244 );
245 d[i]=0;
246 return d;
247 }
xfopen(XMLCSTR filename,XMLCSTR mode)248 static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
xstrlen(XMLCSTR c)249 static inline int xstrlen(XMLCSTR c) { return (int)strlen(c); }
250 #ifdef __BORLANDC__
xstrnicmp(XMLCSTR c1,XMLCSTR c2,int l)251 static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strnicmp(c1,c2,l);}
xstricmp(XMLCSTR c1,XMLCSTR c2)252 static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return stricmp(c1,c2); }
253 #else
xstrnicmp(XMLCSTR c1,XMLCSTR c2,int l)254 static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _strnicmp(c1,c2,l);}
xstricmp(XMLCSTR c1,XMLCSTR c2)255 static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _stricmp(c1,c2); }
256 #endif
xstrncmp(XMLCSTR c1,XMLCSTR c2,int l)257 static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
xstrstr(XMLCSTR c1,XMLCSTR c2)258 static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
xstrcpy(XMLSTR c1,XMLCSTR c2)259 static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
260 #endif
261 #else
262 // for gcc and CC
263 #ifdef XML_NO_WIDE_CHAR
myWideCharToMultiByte(const wchar_t * s)264 char *myWideCharToMultiByte(const wchar_t *s) { return NULL; }
265 #else
myWideCharToMultiByte(const wchar_t * s)266 char *myWideCharToMultiByte(const wchar_t *s)
267 {
268 const wchar_t *ss=s;
269 int i=(int)wcsrtombs(NULL,&ss,0,NULL);
270 if (i<0) return NULL;
271 char *d=(char *)malloc(i+1);
272 wcsrtombs(d,&s,i,NULL);
273 d[i]=0;
274 return d;
275 }
276 #endif
277 #ifdef _XMLWIDECHAR
myMultiByteToWideChar(const char * s,XMLNode::XMLCharEncoding ce)278 wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
279 {
280 const char *ss=s;
281 int i=(int)mbsrtowcs(NULL,&ss,0,NULL);
282 if (i<0) return NULL;
283 wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(wchar_t));
284 mbsrtowcs(d,&s,i,NULL);
285 d[i]=0;
286 return d;
287 }
xstrlen(XMLCSTR c)288 int xstrlen(XMLCSTR c) { return wcslen(c); }
289 #ifdef sun
290 // for CC
291 #include <widec.h>
xstrnicmp(XMLCSTR c1,XMLCSTR c2,int l)292 static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncasecmp(c1,c2,l);}
xstrncmp(XMLCSTR c1,XMLCSTR c2,int l)293 static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncmp(c1,c2,l);}
xstricmp(XMLCSTR c1,XMLCSTR c2)294 static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wscasecmp(c1,c2); }
295 #else
xstrncmp(XMLCSTR c1,XMLCSTR c2,int l)296 static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
297 #ifdef __linux__
298 // for gcc/linux
xstrnicmp(XMLCSTR c1,XMLCSTR c2,int l)299 static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncasecmp(c1,c2,l);}
xstricmp(XMLCSTR c1,XMLCSTR c2)300 static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wcscasecmp(c1,c2); }
301 #else
302 #include <wctype.h>
303 // for gcc/non-linux (MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw)
xstricmp(XMLCSTR c1,XMLCSTR c2)304 static inline int xstricmp(XMLCSTR c1, XMLCSTR c2)
305 {
306 wchar_t left,right;
307 do
308 {
309 left=towlower(*c1++); right=towlower(*c2++);
310 } while (left&&(left==right));
311 return (int)left-(int)right;
312 }
xstrnicmp(XMLCSTR c1,XMLCSTR c2,int l)313 static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l)
314 {
315 wchar_t left,right;
316 while(l--)
317 {
318 left=towlower(*c1++); right=towlower(*c2++);
319 if ((!left)||(left!=right)) return (int)left-(int)right;
320 }
321 return 0;
322 }
323 #endif
324 #endif
xstrstr(XMLCSTR c1,XMLCSTR c2)325 static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
xstrcpy(XMLSTR c1,XMLCSTR c2)326 static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
xfopen(XMLCSTR filename,XMLCSTR mode)327 static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode)
328 {
329 char *filenameAscii=myWideCharToMultiByte(filename);
330 FILE *f;
331 if (mode[0]==_CXML('r')) f=fopen(filenameAscii,"rb");
332 else f=fopen(filenameAscii,"wb");
333 free(filenameAscii);
334 return f;
335 }
336 #else
xfopen(XMLCSTR filename,XMLCSTR mode)337 static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
xstrlen(XMLCSTR c)338 static inline int xstrlen(XMLCSTR c) { return strlen(c); }
xstrnicmp(XMLCSTR c1,XMLCSTR c2,int l)339 static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncasecmp(c1,c2,l);}
xstrncmp(XMLCSTR c1,XMLCSTR c2,int l)340 static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
xstricmp(XMLCSTR c1,XMLCSTR c2)341 static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return strcasecmp(c1,c2); }
xstrstr(XMLCSTR c1,XMLCSTR c2)342 static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
xstrcpy(XMLSTR c1,XMLCSTR c2)343 static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
344 #endif
_strnicmp(const char * c1,const char * c2,int l)345 static inline int _strnicmp(const char *c1,const char *c2, int l) { return strncasecmp(c1,c2,l);}
346 #endif
347
348
349 ///////////////////////////////////////////////////////////////////////////////
350 // the "xmltoc,xmltob,xmltoi,xmltol,xmltof,xmltoa" functions //
351 ///////////////////////////////////////////////////////////////////////////////
352 // These 6 functions are not used inside the XMLparser.
353 // There are only here as "convenience" functions for the user.
354 // If you don't need them, you can delete them without any trouble.
355 #ifdef _XMLWIDECHAR
356 #ifdef _XMLWINDOWS
357 // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
xmltob(XMLCSTR t,char v)358 char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)_wtoi(t); return v; }
xmltoi(XMLCSTR t,int v)359 int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return _wtoi(t); return v; }
xmltol(XMLCSTR t,long v)360 long xmltol(XMLCSTR t,long v){ if (t&&(*t)) return _wtol(t); return v; }
xmltof(XMLCSTR t,double v)361 double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; }
362 #else
363 #ifdef sun
364 // for CC
365 #include <widec.h>
xmltob(XMLCSTR t,char v)366 char xmltob(XMLCSTR t,char v){ if (t) return (char)wstol(t,NULL,10); return v; }
xmltoi(XMLCSTR t,int v)367 int xmltoi(XMLCSTR t,int v){ if (t) return (int)wstol(t,NULL,10); return v; }
xmltol(XMLCSTR t,long v)368 long xmltol(XMLCSTR t,long v){ if (t) return wstol(t,NULL,10); return v; }
369 #else
370 // for gcc
xmltob(XMLCSTR t,char v)371 char xmltob(XMLCSTR t,char v){ if (t) return (char)wcstol(t,NULL,10); return v; }
xmltoi(XMLCSTR t,int v)372 int xmltoi(XMLCSTR t,int v){ if (t) return (int)wcstol(t,NULL,10); return v; }
xmltol(XMLCSTR t,long v)373 long xmltol(XMLCSTR t,long v){ if (t) return wcstol(t,NULL,10); return v; }
374 #endif
xmltof(XMLCSTR t,double v)375 double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; }
376 #endif
377 #else
xmltob(XMLCSTR t,char v)378 char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)atoi(t); return v; }
xmltoi(XMLCSTR t,int v)379 int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return atoi(t); return v; }
xmltol(XMLCSTR t,long v)380 long xmltol(XMLCSTR t,long v){ if (t&&(*t)) return atol(t); return v; }
xmltof(XMLCSTR t,double v)381 double xmltof(XMLCSTR t,double v){ if (t&&(*t)) return atof(t); return v; }
382 #endif
xmltoa(XMLCSTR t,XMLCSTR v)383 XMLCSTR xmltoa(XMLCSTR t, XMLCSTR v){ if (t) return t; return v; }
xmltoc(XMLCSTR t,const XMLCHAR v)384 XMLCHAR xmltoc(XMLCSTR t,const XMLCHAR v){ if (t&&(*t)) return *t; return v; }
385
386 /////////////////////////////////////////////////////////////////////////
387 // the "openFileHelper" function //
388 /////////////////////////////////////////////////////////////////////////
389
390 // Since each application has its own way to report and deal with errors, you should modify & rewrite
391 // the following "openFileHelper" function to get an "error reporting mechanism" tailored to your needs.
openFileHelper(XMLCSTR filename,XMLCSTR tag)392 XMLNode XMLNode::openFileHelper(XMLCSTR filename, XMLCSTR tag)
393 {
394 // guess the value of the global parameter "characterEncoding"
395 // (the guess is based on the first 200 bytes of the file).
396 FILE *f=xfopen(filename,_CXML("rb"));
397 if (f)
398 {
399 char bb[205];
400 int l=(int)fread(bb,1,200,f);
401 setGlobalOptions(guessCharEncoding(bb,l),guessWideCharChars,dropWhiteSpace,removeCommentsInMiddleOfText);
402 fclose(f);
403 }
404
405 // parse the file
406 XMLResults pResults;
407 XMLNode xnode=XMLNode::parseFile(filename,tag,&pResults);
408
409 // display error message (if any)
410 if (pResults.error != eXMLErrorNone)
411 {
412 // create message
413 char message[2000],*s1=(char*)"",*s3=(char*)""; XMLCSTR s2=_CXML("");
414 if (pResults.error==eXMLErrorFirstTagNotFound) { s1=(char*)"First Tag should be '"; s2=tag; s3=(char*)"'.\n"; }
415 sprintf(message,
416 #ifdef _XMLWIDECHAR
417 "XML Parsing error inside file '%S'.\n%S\nAt line %i, column %i.\n%s%S%s"
418 #else
419 "XML Parsing error inside file '%s'.\n%s\nAt line %i, column %i.\n%s%s%s"
420 #endif
421 ,filename,XMLNode::getError(pResults.error),pResults.nLine,pResults.nColumn,s1,s2,s3);
422
423 // display message
424 #if defined(_XMLWINDOWS) && !defined(UNDER_CE) && !defined(_XMLPARSER_NO_MESSAGEBOX_)
425 MessageBoxA(NULL,message,"XML Parsing error",MB_OK|MB_ICONERROR|MB_TOPMOST);
426 #else
427 printf("%s",message);
428 #endif
429 }
430 return xnode;
431 }
432
433 /////////////////////////////////////////////////////////////////////////
434 // Here start the core implementation of the XMLParser library //
435 /////////////////////////////////////////////////////////////////////////
436
437 // You should normally not change anything below this point.
438
439 #ifndef _XMLWIDECHAR
440 // If "characterEncoding=ascii" then we assume that all characters have the same length of 1 byte.
441 // If "characterEncoding=UTF8" then the characters have different lengths (from 1 byte to 4 bytes).
442 // If "characterEncoding=ShiftJIS" then the characters have different lengths (from 1 byte to 2 bytes).
443 // This table is used as lookup-table to know the length of a character (in byte) based on the
444 // content of the first byte of the character.
445 // (note: if you modify this, you must always have XML_utf8ByteTable[0]=0 ).
446 static const char XML_utf8ByteTable[256] =
447 {
448 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
449 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
450 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
451 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
452 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
453 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
454 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
455 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
456 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70 End of ASCII range
457 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80 0x80 to 0xc1 invalid
458 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90
459 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0
460 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0
461 1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0 0xc2 to 0xdf 2 byte
462 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
463 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,// 0xe0 0xe0 to 0xef 3 byte
464 4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1 // 0xf0 0xf0 to 0xf4 4 byte, 0xf5 and higher invalid
465 };
466 static const char XML_legacyByteTable[256] =
467 {
468 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
469 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
470 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
471 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
472 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
473 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
474 };
475 static const char XML_sjisByteTable[256] =
476 {
477 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
478 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
479 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
480 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
481 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
482 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
483 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
484 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
485 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
486 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0x9F 2 bytes
487 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90
488 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0
489 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0
490 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xc0
491 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xd0
492 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0 0xe0 to 0xef 2 bytes
493 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 // 0xf0
494 };
495 static const char XML_gb2312ByteTable[256] =
496 {
497 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
498 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
499 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
500 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
501 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
502 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
503 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
504 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
505 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
506 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80
507 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90
508 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0 0xa1 to 0xf7 2 bytes
509 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0
510 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0
511 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
512 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0
513 2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1 // 0xf0
514 };
515 static const char XML_gbk_big5_ByteTable[256] =
516 {
517 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
518 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
519 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
520 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
521 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
522 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
523 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
524 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
525 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
526 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0xfe 2 bytes
527 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90
528 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0
529 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0
530 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0
531 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
532 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0
533 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1 // 0xf0
534 };
535 static const char *XML_ByteTable=(const char *)XML_utf8ByteTable; // the default is "characterEncoding=XMLNode::encoding_UTF8"
536 #endif
537
538
539 XMLNode XMLNode::emptyXMLNode;
540 XMLClear XMLNode::emptyXMLClear={ NULL, NULL, NULL};
541 XMLAttribute XMLNode::emptyXMLAttribute={ NULL, NULL};
542
543 // Enumeration used to decipher what type a token is
544 typedef enum XMLTokenTypeTag
545 {
546 eTokenText = 0,
547 eTokenQuotedText,
548 eTokenTagStart, /* "<" */
549 eTokenTagEnd, /* "</" */
550 eTokenCloseTag, /* ">" */
551 eTokenEquals, /* "=" */
552 eTokenDeclaration, /* "<?" */
553 eTokenShortHandClose, /* "/>" */
554 eTokenClear,
555 eTokenError
556 } XMLTokenType;
557
558 // Main structure used for parsing XML
559 typedef struct XML
560 {
561 XMLCSTR lpXML;
562 XMLCSTR lpszText;
563 int nIndex,nIndexMissigEndTag;
564 enum XMLError error;
565 XMLCSTR lpEndTag;
566 int cbEndTag;
567 XMLCSTR lpNewElement;
568 int cbNewElement;
569 int nFirst;
570 } XML;
571
572 typedef struct
573 {
574 ALLXMLClearTag *pClr;
575 XMLCSTR pStr;
576 } NextToken;
577
578 // Enumeration used when parsing attributes
579 typedef enum Attrib
580 {
581 eAttribName = 0,
582 eAttribEquals,
583 eAttribValue
584 } Attrib;
585
586 // Enumeration used when parsing elements to dictate whether we are currently
587 // inside a tag
588 typedef enum XMLStatus
589 {
590 eInsideTag = 0,
591 eOutsideTag
592 } XMLStatus;
593
writeToFile(XMLCSTR filename,const char * encoding,char nFormat) const594 XMLError XMLNode::writeToFile(XMLCSTR filename, const char *encoding, char nFormat) const
595 {
596 if (!d) return eXMLErrorNone;
597 FILE *f=xfopen(filename,_CXML("wb"));
598 if (!f) return eXMLErrorCannotOpenWriteFile;
599 #ifdef _XMLWIDECHAR
600 unsigned char h[2]={ 0xFF, 0xFE };
601 if (!fwrite(h,2,1,f))
602 {
603 fclose(f);
604 return eXMLErrorCannotWriteFile;
605 }
606 if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration())))
607 {
608 if (!fwrite(L"<?xml version=\"1.0\" encoding=\"utf-16\"?>\n",sizeof(wchar_t)*40,1,f))
609 {
610 fclose(f);
611 return eXMLErrorCannotWriteFile;
612 }
613 }
614 #else
615 if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration())))
616 {
617 if (characterEncoding==char_encoding_UTF8)
618 {
619 // header so that windows recognize the file as UTF-8:
620 unsigned char h[3]={0xEF,0xBB,0xBF};
621 if (!fwrite(h,3,1,f))
622 {
623 fclose(f);
624 return eXMLErrorCannotWriteFile;
625 }
626 encoding="utf-8";
627 } else if (characterEncoding==char_encoding_ShiftJIS) encoding="SHIFT-JIS";
628
629 if (!encoding) encoding="ISO-8859-1";
630 if (fprintf(f,"<?xml version=\"1.0\" encoding=\"%s\"?>\n",encoding)<0)
631 {
632 fclose(f);
633 return eXMLErrorCannotWriteFile;
634 }
635 } else
636 {
637 if (characterEncoding==char_encoding_UTF8)
638 {
639 unsigned char h[3]={0xEF,0xBB,0xBF};
640 if (!fwrite(h,3,1,f))
641 {
642 fclose(f);
643 return eXMLErrorCannotWriteFile;
644 }
645 }
646 }
647 #endif
648 int i;
649 XMLSTR t=createXMLString(nFormat,&i);
650 if (!fwrite(t,sizeof(XMLCHAR)*i,1,f))
651 {
652 free(t);
653 fclose(f);
654 return eXMLErrorCannotWriteFile;
655 }
656 if (fclose(f)!=0)
657 {
658 free(t);
659 return eXMLErrorCannotWriteFile;
660 }
661 free(t);
662 return eXMLErrorNone;
663 }
664
665 // Duplicate a given string.
stringDup(XMLCSTR lpszData,int cbData)666 XMLSTR stringDup(XMLCSTR lpszData, int cbData)
667 {
668 if (lpszData==NULL) return NULL;
669
670 XMLSTR lpszNew;
671 if (cbData==-1) cbData=(int)xstrlen(lpszData);
672 lpszNew = (XMLSTR)malloc((cbData+1) * sizeof(XMLCHAR));
673 if (lpszNew)
674 {
675 memcpy(lpszNew, lpszData, (cbData) * sizeof(XMLCHAR));
676 lpszNew[cbData] = 0;
677 }
678 return lpszNew;
679 }
680
toXMLUnSafe(XMLSTR dest,XMLCSTR source)681 XMLSTR ToXMLStringTool::toXMLUnSafe(XMLSTR dest,XMLCSTR source)
682 {
683 XMLSTR dd=dest;
684 XMLCHAR ch;
685 XMLCharacterEntity *entity;
686 while ((ch=*source))
687 {
688 entity=XMLEntities;
689 do
690 {
691 if (ch==entity->c) {xstrcpy(dest,entity->s); dest+=entity->l; source++; goto out_of_loop1; }
692 entity++;
693 } while(entity->s);
694 #ifdef _XMLWIDECHAR
695 *(dest++)=*(source++);
696 #else
697 switch(XML_ByteTable[(unsigned char)ch])
698 {
699 case 4: *(dest++)=*(source++);
700 case 3: *(dest++)=*(source++);
701 case 2: *(dest++)=*(source++);
702 case 1: *(dest++)=*(source++);
703 }
704 #endif
705 out_of_loop1:
706 ;
707 }
708 *dest=0;
709 return dd;
710 }
711
712 // private (used while rendering):
lengthXMLString(XMLCSTR source)713 int ToXMLStringTool::lengthXMLString(XMLCSTR source)
714 {
715 int r=0;
716 XMLCharacterEntity *entity;
717 XMLCHAR ch;
718 while ((ch=*source))
719 {
720 entity=XMLEntities;
721 do
722 {
723 if (ch==entity->c) { r+=entity->l; source++; goto out_of_loop1; }
724 entity++;
725 } while(entity->s);
726 #ifdef _XMLWIDECHAR
727 r++; source++;
728 #else
729 ch=XML_ByteTable[(unsigned char)ch]; r+=ch; source+=ch;
730 #endif
731 out_of_loop1:
732 ;
733 }
734 return r;
735 }
736
~ToXMLStringTool()737 ToXMLStringTool::~ToXMLStringTool(){ freeBuffer(); }
freeBuffer()738 void ToXMLStringTool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; }
toXML(XMLCSTR source)739 XMLSTR ToXMLStringTool::toXML(XMLCSTR source)
740 {
741 if (!source)
742 {
743 if (buflen<1) { buflen=1; buf=(XMLSTR)malloc(sizeof(XMLCHAR)); }
744 *buf=0;
745 return buf;
746 }
747 int l=lengthXMLString(source)+1;
748 if (l>buflen) { freeBuffer(); buflen=l; buf=(XMLSTR)malloc(l*sizeof(XMLCHAR)); }
749 return toXMLUnSafe(buf,source);
750 }
751
752 // private:
fromXMLString(XMLCSTR s,int lo,XML * pXML)753 XMLSTR fromXMLString(XMLCSTR s, int lo, XML *pXML)
754 {
755 // This function is the opposite of the function "toXMLString". It decodes the escape
756 // sequences &, ", ', <, > and replace them by the characters
757 // &,",',<,>. This function is used internally by the XML Parser. All the calls to
758 // the XML library will always gives you back "decoded" strings.
759 //
760 // in: string (s) and length (lo) of string
761 // out: new allocated string converted from xml
762 if (!s) return NULL;
763
764 int ll=0,j;
765 XMLSTR d;
766 XMLCSTR ss=s;
767 XMLCharacterEntity *entity;
768 while ((lo>0)&&(*s))
769 {
770 if (*s==_CXML('&'))
771 {
772 if ((lo>2)&&(s[1]==_CXML('#')))
773 {
774 s+=2; lo-=2;
775 if ((*s==_CXML('X'))||(*s==_CXML('x'))) { s++; lo--; }
776 while ((*s)&&(*s!=_CXML(';'))&&((lo--)>0)) s++;
777 if (*s!=_CXML(';'))
778 {
779 pXML->error=eXMLErrorUnknownCharacterEntity;
780 return NULL;
781 }
782 s++; lo--;
783 } else
784 {
785 entity=XMLEntities;
786 do
787 {
788 if ((lo>=entity->l)&&(xstrnicmp(s,entity->s,entity->l)==0)) { s+=entity->l; lo-=entity->l; break; }
789 entity++;
790 } while(entity->s);
791 if (!entity->s)
792 {
793 pXML->error=eXMLErrorUnknownCharacterEntity;
794 return NULL;
795 }
796 }
797 } else
798 {
799 #ifdef _XMLWIDECHAR
800 s++; lo--;
801 #else
802 j=XML_ByteTable[(unsigned char)*s]; s+=j; lo-=j; ll+=j-1;
803 #endif
804 }
805 ll++;
806 }
807
808 d=(XMLSTR)malloc((ll+1)*sizeof(XMLCHAR));
809 s=d;
810 while (ll-->0)
811 {
812 if (*ss==_CXML('&'))
813 {
814 if (ss[1]==_CXML('#'))
815 {
816 ss+=2; j=0;
817 if ((*ss==_CXML('X'))||(*ss==_CXML('x')))
818 {
819 ss++;
820 while (*ss!=_CXML(';'))
821 {
822 if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j<<4)+*ss-_CXML('0');
823 else if ((*ss>=_CXML('A'))&&(*ss<=_CXML('F'))) j=(j<<4)+*ss-_CXML('A')+10;
824 else if ((*ss>=_CXML('a'))&&(*ss<=_CXML('f'))) j=(j<<4)+*ss-_CXML('a')+10;
825 else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
826 ss++;
827 }
828 } else
829 {
830 while (*ss!=_CXML(';'))
831 {
832 if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j*10)+*ss-_CXML('0');
833 else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
834 ss++;
835 }
836 }
837 #ifndef _XMLWIDECHAR
838 if (j>255) { free((void*)s); pXML->error=eXMLErrorCharacterCodeAbove255;return NULL;}
839 #endif
840 (*d++)=(XMLCHAR)j; ss++;
841 } else
842 {
843 entity=XMLEntities;
844 do
845 {
846 if (xstrnicmp(ss,entity->s,entity->l)==0) { *(d++)=entity->c; ss+=entity->l; break; }
847 entity++;
848 } while(entity->s);
849 }
850 } else
851 {
852 #ifdef _XMLWIDECHAR
853 *(d++)=*(ss++);
854 #else
855 switch(XML_ByteTable[(unsigned char)*ss])
856 {
857 case 4: *(d++)=*(ss++); ll--;
858 case 3: *(d++)=*(ss++); ll--;
859 case 2: *(d++)=*(ss++); ll--;
860 case 1: *(d++)=*(ss++);
861 }
862 #endif
863 }
864 }
865 *d=0;
866 return (XMLSTR)s;
867 }
868
869 #define XML_isSPACECHAR(ch) ((ch==_CXML('\n'))||(ch==_CXML(' '))||(ch== _CXML('\t'))||(ch==_CXML('\r')))
870
871 // private:
myTagCompare(XMLCSTR cclose,XMLCSTR copen)872 char myTagCompare(XMLCSTR cclose, XMLCSTR copen)
873 // !!!! WARNING strange convention&:
874 // return 0 if equals
875 // return 1 if different
876 {
877 if (!cclose) return 1;
878 int l=(int)xstrlen(cclose);
879 if (xstrnicmp(cclose, copen, l)!=0) return 1;
880 const XMLCHAR c=copen[l];
881 if (XML_isSPACECHAR(c)||
882 (c==_CXML('/' ))||
883 (c==_CXML('<' ))||
884 (c==_CXML('>' ))||
885 (c==_CXML('=' ))) return 0;
886 return 1;
887 }
888
889 // Obtain the next character from the string.
getNextChar(XML * pXML)890 static inline XMLCHAR getNextChar(XML *pXML)
891 {
892 XMLCHAR ch = pXML->lpXML[pXML->nIndex];
893 #ifdef _XMLWIDECHAR
894 if (ch!=0) pXML->nIndex++;
895 #else
896 pXML->nIndex+=XML_ByteTable[(unsigned char)ch];
897 #endif
898 return ch;
899 }
900
901 // Find the next token in a string.
902 // pcbToken contains the number of characters that have been read.
GetNextToken(XML * pXML,int * pcbToken,enum XMLTokenTypeTag * pType)903 static NextToken GetNextToken(XML *pXML, int *pcbToken, enum XMLTokenTypeTag *pType)
904 {
905 NextToken result;
906 XMLCHAR ch;
907 XMLCHAR chTemp;
908 int indexStart,nFoundMatch,nIsText=FALSE;
909 result.pClr=NULL; // prevent warning
910
911 // Find next non-white space character
912 do { indexStart=pXML->nIndex; ch=getNextChar(pXML); } while XML_isSPACECHAR(ch);
913
914 if (ch)
915 {
916 // Cache the current string pointer
917 result.pStr = &pXML->lpXML[indexStart];
918
919 // check for standard tokens
920 switch(ch)
921 {
922 // Check for quotes
923 case _CXML('\''):
924 case _CXML('\"'):
925 // Type of token
926 *pType = eTokenQuotedText;
927 chTemp = ch;
928
929 // Set the size
930 nFoundMatch = FALSE;
931
932 // Search through the string to find a matching quote
933 while((ch = getNextChar(pXML)))
934 {
935 if (ch==chTemp) { nFoundMatch = TRUE; break; }
936 if (ch==_CXML('<')) break;
937 }
938
939 // If we failed to find a matching quote
940 if (nFoundMatch == FALSE)
941 {
942 pXML->nIndex=indexStart+1;
943 nIsText=TRUE;
944 break;
945 }
946
947 // 4.02.2002
948 // if (FindNonWhiteSpace(pXML)) pXML->nIndex--;
949
950 break;
951
952 // Equals (used with attribute values)
953 case _CXML('='):
954 *pType = eTokenEquals;
955 break;
956
957 // Close tag
958 case _CXML('>'):
959 *pType = eTokenCloseTag;
960 break;
961
962 // Check for tag start and tag end
963 case _CXML('<'):
964
965 {
966 // First check whether the token is in the clear tag list (meaning it
967 // does not need formatting).
968 ALLXMLClearTag *ctag=XMLClearTags;
969 do
970 {
971 if (!xstrncmp(ctag->lpszOpen, result.pStr, ctag->openTagLen))
972 {
973 result.pClr=ctag;
974 pXML->nIndex+=ctag->openTagLen-1;
975 *pType=eTokenClear;
976 return result;
977 }
978 ctag++;
979 } while(ctag->lpszOpen);
980
981 // Peek at the next character to see if we have an end tag '</',
982 // or an xml declaration '<?'
983 chTemp = pXML->lpXML[pXML->nIndex];
984
985 // If we have a tag end...
986 if (chTemp == _CXML('/'))
987 {
988 // Set the type and ensure we point at the next character
989 getNextChar(pXML);
990 *pType = eTokenTagEnd;
991 }
992
993 // If we have an XML declaration tag
994 else if (chTemp == _CXML('?'))
995 {
996
997 // Set the type and ensure we point at the next character
998 getNextChar(pXML);
999 *pType = eTokenDeclaration;
1000 }
1001
1002 // Otherwise we must have a start tag
1003 else
1004 {
1005 *pType = eTokenTagStart;
1006 }
1007 break;
1008 }
1009
1010 // Check to see if we have a short hand type end tag ('/>').
1011 case _CXML('/'):
1012
1013 // Peek at the next character to see if we have a short end tag '/>'
1014 chTemp = pXML->lpXML[pXML->nIndex];
1015
1016 // If we have a short hand end tag...
1017 if (chTemp == _CXML('>'))
1018 {
1019 // Set the type and ensure we point at the next character
1020 getNextChar(pXML);
1021 *pType = eTokenShortHandClose;
1022 break;
1023 }
1024
1025 // If we haven't found a short hand closing tag then drop into the
1026 // text process
1027
1028 // Other characters
1029 default:
1030 nIsText = TRUE;
1031 }
1032
1033 // If this is a TEXT node
1034 if (nIsText)
1035 {
1036 // Indicate we are dealing with text
1037 *pType = eTokenText;
1038 while((ch = getNextChar(pXML)))
1039 {
1040 if XML_isSPACECHAR(ch)
1041 {
1042 indexStart++; break;
1043
1044 } else if (ch==_CXML('/'))
1045 {
1046 // If we find a slash then this maybe text or a short hand end tag
1047 // Peek at the next character to see it we have short hand end tag
1048 ch=pXML->lpXML[pXML->nIndex];
1049 // If we found a short hand end tag then we need to exit the loop
1050 if (ch==_CXML('>')) { pXML->nIndex--; break; }
1051
1052 } else if ((ch==_CXML('<'))||(ch==_CXML('>'))||(ch==_CXML('=')))
1053 {
1054 pXML->nIndex--; break;
1055 }
1056 }
1057 }
1058 *pcbToken = pXML->nIndex-indexStart;
1059 } else
1060 {
1061 // If we failed to obtain a valid character
1062 *pcbToken = 0;
1063 *pType = eTokenError;
1064 result.pStr=NULL;
1065 }
1066
1067 return result;
1068 }
1069
updateName_WOSD(XMLSTR lpszName)1070 XMLCSTR XMLNode::updateName_WOSD(XMLSTR lpszName)
1071 {
1072 if (!d) { free(lpszName); return NULL; }
1073 if (d->lpszName&&(lpszName!=d->lpszName)) free((void*)d->lpszName);
1074 d->lpszName=lpszName;
1075 return lpszName;
1076 }
1077
1078 // private:
XMLNode(struct XMLNodeDataTag * p)1079 XMLNode::XMLNode(struct XMLNodeDataTag *p){ d=p; (p->ref_count)++; }
XMLNode(XMLNodeData * pParent,XMLSTR lpszName,char isDeclaration)1080 XMLNode::XMLNode(XMLNodeData *pParent, XMLSTR lpszName, char isDeclaration)
1081 {
1082 d=(XMLNodeData*)malloc(sizeof(XMLNodeData));
1083 d->ref_count=1;
1084
1085 d->lpszName=NULL;
1086 d->nChild= 0;
1087 d->nText = 0;
1088 d->nClear = 0;
1089 d->nAttribute = 0;
1090
1091 d->isDeclaration = isDeclaration;
1092
1093 d->pParent = pParent;
1094 d->pChild= NULL;
1095 d->pText= NULL;
1096 d->pClear= NULL;
1097 d->pAttribute= NULL;
1098 d->pOrder= NULL;
1099
1100 updateName_WOSD(lpszName);
1101 }
1102
createXMLTopNode_WOSD(XMLSTR lpszName,char isDeclaration)1103 XMLNode XMLNode::createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration) { return XMLNode(NULL,lpszName,isDeclaration); }
createXMLTopNode(XMLCSTR lpszName,char isDeclaration)1104 XMLNode XMLNode::createXMLTopNode(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL,stringDup(lpszName),isDeclaration); }
1105
1106 #define MEMORYINCREASE 50
1107
myFree(void * p)1108 static inline void myFree(void *p) { if (p) free(p); }
myRealloc(void * p,int newsize,int memInc,int sizeofElem)1109 static inline void *myRealloc(void *p, int newsize, int memInc, int sizeofElem)
1110 {
1111 if (p==NULL) { if (memInc) return malloc(memInc*sizeofElem); return malloc(sizeofElem); }
1112 if ((memInc==0)||((newsize%memInc)==0)) p=realloc(p,(newsize+memInc)*sizeofElem);
1113 // if (!p)
1114 // {
1115 // printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220);
1116 // }
1117 return p;
1118 }
1119
1120 // private:
findPosition(XMLNodeData * d,int index,XMLElementType xxtype)1121 XMLElementPosition XMLNode::findPosition(XMLNodeData *d, int index, XMLElementType xxtype)
1122 {
1123 if (index<0) return -1;
1124 int i=0,j=(int)((index<<2)+xxtype),*o=d->pOrder; while (o[i]!=j) i++; return i;
1125 }
1126
1127 // private:
1128 // update "order" information when deleting a content of a XMLNode
removeOrderElement(XMLNodeData * d,XMLElementType t,int index)1129 int XMLNode::removeOrderElement(XMLNodeData *d, XMLElementType t, int index)
1130 {
1131 int n=d->nChild+d->nText+d->nClear, *o=d->pOrder,i=findPosition(d,index,t);
1132 memmove(o+i, o+i+1, (n-i)*sizeof(int));
1133 for (;i<n;i++)
1134 if ((o[i]&3)==(int)t) o[i]-=4;
1135 // We should normally do:
1136 // d->pOrder=(int)realloc(d->pOrder,n*sizeof(int));
1137 // but we skip reallocation because it's too time consuming.
1138 // Anyway, at the end, it will be free'd completely at once.
1139 return i;
1140 }
1141
addToOrder(int memoryIncrease,int * _pos,int nc,void * p,int size,XMLElementType xtype)1142 void *XMLNode::addToOrder(int memoryIncrease,int *_pos, int nc, void *p, int size, XMLElementType xtype)
1143 {
1144 // in: *_pos is the position inside d->pOrder ("-1" means "EndOf")
1145 // out: *_pos is the index inside p
1146 p=myRealloc(p,(nc+1),memoryIncrease,size);
1147 int n=d->nChild+d->nText+d->nClear;
1148 d->pOrder=(int*)myRealloc(d->pOrder,n+1,memoryIncrease*3,sizeof(int));
1149 int pos=*_pos,*o=d->pOrder;
1150
1151 if ((pos<0)||(pos>=n)) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
1152
1153 int i=pos;
1154 memmove(o+i+1, o+i, (n-i)*sizeof(int));
1155
1156 while ((pos<n)&&((o[pos]&3)!=(int)xtype)) pos++;
1157 if (pos==n) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
1158
1159 o[i]=o[pos];
1160 for (i=pos+1;i<=n;i++) if ((o[i]&3)==(int)xtype) o[i]+=4;
1161
1162 *_pos=pos=o[pos]>>2;
1163 memmove(((char*)p)+(pos+1)*size,((char*)p)+pos*size,(nc-pos)*size);
1164
1165 return p;
1166 }
1167
1168 // Add a child node to the given element.
addChild_priv(int memoryIncrease,XMLSTR lpszName,char isDeclaration,int pos)1169 XMLNode XMLNode::addChild_priv(int memoryIncrease, XMLSTR lpszName, char isDeclaration, int pos)
1170 {
1171 if (!lpszName) return emptyXMLNode;
1172 d->pChild=(XMLNode*)addToOrder(memoryIncrease,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
1173 d->pChild[pos].d=NULL;
1174 d->pChild[pos]=XMLNode(d,lpszName,isDeclaration);
1175 d->nChild++;
1176 return d->pChild[pos];
1177 }
1178
1179 // Add an attribute to an element.
addAttribute_priv(int memoryIncrease,XMLSTR lpszName,XMLSTR lpszValuev)1180 XMLAttribute *XMLNode::addAttribute_priv(int memoryIncrease,XMLSTR lpszName, XMLSTR lpszValuev)
1181 {
1182 if (!lpszName) return &emptyXMLAttribute;
1183 if (!d) { myFree(lpszName); myFree(lpszValuev); return &emptyXMLAttribute; }
1184 int nc=d->nAttribute;
1185 d->pAttribute=(XMLAttribute*)myRealloc(d->pAttribute,(nc+1),memoryIncrease,sizeof(XMLAttribute));
1186 XMLAttribute *pAttr=d->pAttribute+nc;
1187 pAttr->lpszName = lpszName;
1188 pAttr->lpszValue = lpszValuev;
1189 d->nAttribute++;
1190 return pAttr;
1191 }
1192
1193 // Add text to the element.
addText_priv(int memoryIncrease,XMLSTR lpszValue,int pos)1194 XMLCSTR XMLNode::addText_priv(int memoryIncrease, XMLSTR lpszValue, int pos)
1195 {
1196 if (!lpszValue) return NULL;
1197 if (!d) { myFree(lpszValue); return NULL; }
1198 d->pText=(XMLCSTR*)addToOrder(memoryIncrease,&pos,d->nText,d->pText,sizeof(XMLSTR),eNodeText);
1199 d->pText[pos]=lpszValue;
1200 d->nText++;
1201 return lpszValue;
1202 }
1203
1204 // Add clear (unformatted) text to the element.
addClear_priv(int memoryIncrease,XMLSTR lpszValue,XMLCSTR lpszOpen,XMLCSTR lpszClose,int pos)1205 XMLClear *XMLNode::addClear_priv(int memoryIncrease, XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos)
1206 {
1207 if (!lpszValue) return &emptyXMLClear;
1208 if (!d) { myFree(lpszValue); return &emptyXMLClear; }
1209 d->pClear=(XMLClear *)addToOrder(memoryIncrease,&pos,d->nClear,d->pClear,sizeof(XMLClear),eNodeClear);
1210 XMLClear *pNewClear=d->pClear+pos;
1211 pNewClear->lpszValue = lpszValue;
1212 if (!lpszOpen) lpszOpen=XMLClearTags->lpszOpen;
1213 if (!lpszClose) lpszClose=XMLClearTags->lpszClose;
1214 pNewClear->lpszOpenTag = lpszOpen;
1215 pNewClear->lpszCloseTag = lpszClose;
1216 d->nClear++;
1217 return pNewClear;
1218 }
1219
1220 // private:
1221 // Parse a clear (unformatted) type node.
parseClearTag(void * px,void * _pClear)1222 char XMLNode::parseClearTag(void *px, void *_pClear)
1223 {
1224 XML *pXML=(XML *)px;
1225 ALLXMLClearTag pClear=*((ALLXMLClearTag*)_pClear);
1226 int cbTemp=0;
1227 XMLCSTR lpszTemp=NULL;
1228 XMLCSTR lpXML=&pXML->lpXML[pXML->nIndex];
1229 static XMLCSTR docTypeEnd=_CXML("]>");
1230
1231 // Find the closing tag
1232 // Seems the <!DOCTYPE need a better treatment so lets handle it
1233 if (pClear.lpszOpen==XMLClearTags[1].lpszOpen)
1234 {
1235 XMLCSTR pCh=lpXML;
1236 while (*pCh)
1237 {
1238 if (*pCh==_CXML('<')) { pClear.lpszClose=docTypeEnd; lpszTemp=xstrstr(lpXML,docTypeEnd); break; }
1239 else if (*pCh==_CXML('>')) { lpszTemp=pCh; break; }
1240 #ifdef _XMLWIDECHAR
1241 pCh++;
1242 #else
1243 pCh+=XML_ByteTable[(unsigned char)(*pCh)];
1244 #endif
1245 }
1246 } else lpszTemp=xstrstr(lpXML, pClear.lpszClose);
1247
1248 if (lpszTemp)
1249 {
1250 // Cache the size and increment the index
1251 cbTemp = (int)(lpszTemp - lpXML);
1252
1253 pXML->nIndex += cbTemp+(int)xstrlen(pClear.lpszClose);
1254
1255 // Add the clear node to the current element
1256 addClear_priv(MEMORYINCREASE,cbTemp?stringDup(lpXML,cbTemp):NULL, pClear.lpszOpen, pClear.lpszClose,-1);
1257 return 0;
1258 }
1259
1260 // If we failed to find the end tag
1261 pXML->error = eXMLErrorUnmatchedEndClearTag;
1262 return 1;
1263 }
1264
exactMemory(XMLNodeData * d)1265 void XMLNode::exactMemory(XMLNodeData *d)
1266 {
1267 if (d->pOrder) d->pOrder=(int*)realloc(d->pOrder,(d->nChild+d->nText+d->nClear)*sizeof(int));
1268 if (d->pChild) d->pChild=(XMLNode*)realloc(d->pChild,d->nChild*sizeof(XMLNode));
1269 if (d->pAttribute) d->pAttribute=(XMLAttribute*)realloc(d->pAttribute,d->nAttribute*sizeof(XMLAttribute));
1270 if (d->pText) d->pText=(XMLCSTR*)realloc(d->pText,d->nText*sizeof(XMLSTR));
1271 if (d->pClear) d->pClear=(XMLClear *)realloc(d->pClear,d->nClear*sizeof(XMLClear));
1272 }
1273
maybeAddTxT(void * pa,XMLCSTR tokenPStr)1274 char XMLNode::maybeAddTxT(void *pa, XMLCSTR tokenPStr)
1275 {
1276 XML *pXML=(XML *)pa;
1277 XMLCSTR lpszText=pXML->lpszText;
1278 if (!lpszText) return 0;
1279 if (dropWhiteSpace) while (XML_isSPACECHAR(*lpszText)&&(lpszText!=tokenPStr)) lpszText++;
1280 int cbText = (int)(tokenPStr - lpszText);
1281 if (!cbText) { pXML->lpszText=NULL; return 0; }
1282 if (dropWhiteSpace) { cbText--; while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; }
1283 if (!cbText) { pXML->lpszText=NULL; return 0; }
1284 XMLSTR lpt=fromXMLString(lpszText,cbText,pXML);
1285 if (!lpt) return 1;
1286 pXML->lpszText=NULL;
1287 if (removeCommentsInMiddleOfText && d->nText && d->nClear)
1288 {
1289 // if the previous insertion was a comment (<!-- -->) AND
1290 // if the previous previous insertion was a text then, delete the comment and append the text
1291 int n=d->nChild+d->nText+d->nClear-1,*o=d->pOrder;
1292 if (((o[n]&3)==eNodeClear)&&((o[n-1]&3)==eNodeText))
1293 {
1294 int i=o[n]>>2;
1295 if (d->pClear[i].lpszOpenTag==XMLClearTags[2].lpszOpen)
1296 {
1297 deleteClear(i);
1298 i=o[n-1]>>2;
1299 n=xstrlen(d->pText[i]);
1300 int n2=xstrlen(lpt)+1;
1301 d->pText[i]=(XMLSTR)realloc((void*)d->pText[i],(n+n2)*sizeof(XMLCHAR));
1302 if (!d->pText[i]) return 1;
1303 memcpy((void*)(d->pText[i]+n),lpt,n2*sizeof(XMLCHAR));
1304 free(lpt);
1305 return 0;
1306 }
1307 }
1308 }
1309 addText_priv(MEMORYINCREASE,lpt,-1);
1310 return 0;
1311 }
1312 // private:
1313 // Recursively parse an XML element.
ParseXMLElement(void * pa)1314 int XMLNode::ParseXMLElement(void *pa)
1315 {
1316 XML *pXML=(XML *)pa;
1317 int cbToken;
1318 enum XMLTokenTypeTag xtype;
1319 NextToken token;
1320 XMLCSTR lpszTemp=NULL;
1321 int cbTemp=0;
1322 char nDeclaration;
1323 XMLNode pNew;
1324 enum XMLStatus status; // inside or outside a tag
1325 enum Attrib attrib = eAttribName;
1326
1327 assert(pXML);
1328
1329 // If this is the first call to the function
1330 if (pXML->nFirst)
1331 {
1332 // Assume we are outside of a tag definition
1333 pXML->nFirst = FALSE;
1334 status = eOutsideTag;
1335 } else
1336 {
1337 // If this is not the first call then we should only be called when inside a tag.
1338 status = eInsideTag;
1339 }
1340
1341 // Iterate through the tokens in the document
1342 for(;;)
1343 {
1344 // Obtain the next token
1345 token = GetNextToken(pXML, &cbToken, &xtype);
1346
1347 if (xtype != eTokenError)
1348 {
1349 // Check the current status
1350 switch(status)
1351 {
1352
1353 // If we are outside of a tag definition
1354 case eOutsideTag:
1355
1356 // Check what type of token we obtained
1357 switch(xtype)
1358 {
1359 // If we have found text or quoted text
1360 case eTokenText:
1361 case eTokenCloseTag: /* '>' */
1362 case eTokenShortHandClose: /* '/>' */
1363 case eTokenQuotedText:
1364 case eTokenEquals:
1365 break;
1366
1367 // If we found a start tag '<' and declarations '<?'
1368 case eTokenTagStart:
1369 case eTokenDeclaration:
1370
1371 // Cache whether this new element is a declaration or not
1372 nDeclaration = (xtype == eTokenDeclaration);
1373
1374 // If we have node text then add this to the element
1375 if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1376
1377 // Find the name of the tag
1378 token = GetNextToken(pXML, &cbToken, &xtype);
1379
1380 // Return an error if we couldn't obtain the next token or
1381 // it wasnt text
1382 if (xtype != eTokenText)
1383 {
1384 pXML->error = eXMLErrorMissingTagName;
1385 return FALSE;
1386 }
1387
1388 // If we found a new element which is the same as this
1389 // element then we need to pass this back to the caller..
1390
1391 #ifdef APPROXIMATE_PARSING
1392 if (d->lpszName &&
1393 myTagCompare(d->lpszName, token.pStr) == 0)
1394 {
1395 // Indicate to the caller that it needs to create a
1396 // new element.
1397 pXML->lpNewElement = token.pStr;
1398 pXML->cbNewElement = cbToken;
1399 return TRUE;
1400 } else
1401 #endif
1402 {
1403 // If the name of the new element differs from the name of
1404 // the current element we need to add the new element to
1405 // the current one and recurse
1406 pNew = addChild_priv(MEMORYINCREASE,stringDup(token.pStr,cbToken), nDeclaration,-1);
1407
1408 while (!pNew.isEmpty())
1409 {
1410 // Callself to process the new node. If we return
1411 // FALSE this means we dont have any more
1412 // processing to do...
1413
1414 if (!pNew.ParseXMLElement(pXML)) return FALSE;
1415 else
1416 {
1417 // If the call to recurse this function
1418 // evented in a end tag specified in XML then
1419 // we need to unwind the calls to this
1420 // function until we find the appropriate node
1421 // (the element name and end tag name must
1422 // match)
1423 if (pXML->cbEndTag)
1424 {
1425 // If we are back at the root node then we
1426 // have an unmatched end tag
1427 if (!d->lpszName)
1428 {
1429 pXML->error=eXMLErrorUnmatchedEndTag;
1430 return FALSE;
1431 }
1432
1433 // If the end tag matches the name of this
1434 // element then we only need to unwind
1435 // once more...
1436
1437 if (myTagCompare(d->lpszName, pXML->lpEndTag)==0)
1438 {
1439 pXML->cbEndTag = 0;
1440 }
1441
1442 return TRUE;
1443 } else
1444 if (pXML->cbNewElement)
1445 {
1446 // If the call indicated a new element is to
1447 // be created on THIS element.
1448
1449 // If the name of this element matches the
1450 // name of the element we need to create
1451 // then we need to return to the caller
1452 // and let it process the element.
1453
1454 if (myTagCompare(d->lpszName, pXML->lpNewElement)==0)
1455 {
1456 return TRUE;
1457 }
1458
1459 // Add the new element and recurse
1460 pNew = addChild_priv(MEMORYINCREASE,stringDup(pXML->lpNewElement,pXML->cbNewElement),0,-1);
1461 pXML->cbNewElement = 0;
1462 }
1463 else
1464 {
1465 // If we didn't have a new element to create
1466 pNew = emptyXMLNode;
1467
1468 }
1469 }
1470 }
1471 }
1472 break;
1473
1474 // If we found an end tag
1475 case eTokenTagEnd:
1476
1477 // If we have node text then add this to the element
1478 if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1479
1480 // Find the name of the end tag
1481 token = GetNextToken(pXML, &cbTemp, &xtype);
1482
1483 // The end tag should be text
1484 if (xtype != eTokenText)
1485 {
1486 pXML->error = eXMLErrorMissingEndTagName;
1487 return FALSE;
1488 }
1489 lpszTemp = token.pStr;
1490
1491 // After the end tag we should find a closing tag
1492 token = GetNextToken(pXML, &cbToken, &xtype);
1493 if (xtype != eTokenCloseTag)
1494 {
1495 pXML->error = eXMLErrorMissingEndTagName;
1496 return FALSE;
1497 }
1498 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1499
1500 // We need to return to the previous caller. If the name
1501 // of the tag cannot be found we need to keep returning to
1502 // caller until we find a match
1503 if (myTagCompare(d->lpszName, lpszTemp) != 0)
1504 #ifdef STRICT_PARSING
1505 {
1506 pXML->error=eXMLErrorUnmatchedEndTag;
1507 pXML->nIndexMissigEndTag=pXML->nIndex;
1508 return FALSE;
1509 }
1510 #else
1511 {
1512 pXML->error=eXMLErrorMissingEndTag;
1513 pXML->nIndexMissigEndTag=pXML->nIndex;
1514 pXML->lpEndTag = lpszTemp;
1515 pXML->cbEndTag = cbTemp;
1516 }
1517 #endif
1518
1519 // Return to the caller
1520 exactMemory(d);
1521 return TRUE;
1522
1523 // If we found a clear (unformatted) token
1524 case eTokenClear:
1525 // If we have node text then add this to the element
1526 if (maybeAddTxT(pXML,token.pStr)) return FALSE;
1527 if (parseClearTag(pXML, token.pClr)) return FALSE;
1528 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1529 break;
1530
1531 default:
1532 break;
1533 }
1534 break;
1535
1536 // If we are inside a tag definition we need to search for attributes
1537 case eInsideTag:
1538
1539 // Check what part of the attribute (name, equals, value) we
1540 // are looking for.
1541 switch(attrib)
1542 {
1543 // If we are looking for a new attribute
1544 case eAttribName:
1545
1546 // Check what the current token type is
1547 switch(xtype)
1548 {
1549 // If the current type is text...
1550 // Eg. 'attribute'
1551 case eTokenText:
1552 // Cache the token then indicate that we are next to
1553 // look for the equals
1554 lpszTemp = token.pStr;
1555 cbTemp = cbToken;
1556 attrib = eAttribEquals;
1557 break;
1558
1559 // If we found a closing tag...
1560 // Eg. '>'
1561 case eTokenCloseTag:
1562 // We are now outside the tag
1563 status = eOutsideTag;
1564 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1565 break;
1566
1567 // If we found a short hand '/>' closing tag then we can
1568 // return to the caller
1569 case eTokenShortHandClose:
1570 exactMemory(d);
1571 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1572 return TRUE;
1573
1574 // Errors...
1575 case eTokenQuotedText: /* '"SomeText"' */
1576 case eTokenTagStart: /* '<' */
1577 case eTokenTagEnd: /* '</' */
1578 case eTokenEquals: /* '=' */
1579 case eTokenDeclaration: /* '<?' */
1580 case eTokenClear:
1581 pXML->error = eXMLErrorUnexpectedToken;
1582 return FALSE;
1583 default: break;
1584 }
1585 break;
1586
1587 // If we are looking for an equals
1588 case eAttribEquals:
1589 // Check what the current token type is
1590 switch(xtype)
1591 {
1592 // If the current type is text...
1593 // Eg. 'Attribute AnotherAttribute'
1594 case eTokenText:
1595 // Add the unvalued attribute to the list
1596 addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL);
1597 // Cache the token then indicate. We are next to
1598 // look for the equals attribute
1599 lpszTemp = token.pStr;
1600 cbTemp = cbToken;
1601 break;
1602
1603 // If we found a closing tag 'Attribute >' or a short hand
1604 // closing tag 'Attribute />'
1605 case eTokenShortHandClose:
1606 case eTokenCloseTag:
1607 // If we are a declaration element '<?' then we need
1608 // to remove extra closing '?' if it exists
1609 pXML->lpszText=pXML->lpXML+pXML->nIndex;
1610
1611 if (d->isDeclaration &&
1612 (lpszTemp[cbTemp-1]) == _CXML('?'))
1613 {
1614 cbTemp--;
1615 if (d->pParent && d->pParent->pParent) xtype = eTokenShortHandClose;
1616 }
1617
1618 if (cbTemp)
1619 {
1620 // Add the unvalued attribute to the list
1621 addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL);
1622 }
1623
1624 // If this is the end of the tag then return to the caller
1625 if (xtype == eTokenShortHandClose)
1626 {
1627 exactMemory(d);
1628 return TRUE;
1629 }
1630
1631 // We are now outside the tag
1632 status = eOutsideTag;
1633 break;
1634
1635 // If we found the equals token...
1636 // Eg. 'Attribute ='
1637 case eTokenEquals:
1638 // Indicate that we next need to search for the value
1639 // for the attribute
1640 attrib = eAttribValue;
1641 break;
1642
1643 // Errors...
1644 case eTokenQuotedText: /* 'Attribute "InvalidAttr"'*/
1645 case eTokenTagStart: /* 'Attribute <' */
1646 case eTokenTagEnd: /* 'Attribute </' */
1647 case eTokenDeclaration: /* 'Attribute <?' */
1648 case eTokenClear:
1649 pXML->error = eXMLErrorUnexpectedToken;
1650 return FALSE;
1651 default: break;
1652 }
1653 break;
1654
1655 // If we are looking for an attribute value
1656 case eAttribValue:
1657 // Check what the current token type is
1658 switch(xtype)
1659 {
1660 // If the current type is text or quoted text...
1661 // Eg. 'Attribute = "Value"' or 'Attribute = Value' or
1662 // 'Attribute = 'Value''.
1663 case eTokenText:
1664 case eTokenQuotedText:
1665 // If we are a declaration element '<?' then we need
1666 // to remove extra closing '?' if it exists
1667 if (d->isDeclaration &&
1668 (token.pStr[cbToken-1]) == _CXML('?'))
1669 {
1670 cbToken--;
1671 }
1672
1673 if (cbTemp)
1674 {
1675 // Add the valued attribute to the list
1676 if (xtype==eTokenQuotedText) { token.pStr++; cbToken-=2; }
1677 XMLSTR attrVal=(XMLSTR)token.pStr;
1678 if (attrVal)
1679 {
1680 attrVal=fromXMLString(attrVal,cbToken,pXML);
1681 if (!attrVal) return FALSE;
1682 }
1683 addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp),attrVal);
1684 }
1685
1686 // Indicate we are searching for a new attribute
1687 attrib = eAttribName;
1688 break;
1689
1690 // Errors...
1691 case eTokenTagStart: /* 'Attr = <' */
1692 case eTokenTagEnd: /* 'Attr = </' */
1693 case eTokenCloseTag: /* 'Attr = >' */
1694 case eTokenShortHandClose: /* "Attr = />" */
1695 case eTokenEquals: /* 'Attr = =' */
1696 case eTokenDeclaration: /* 'Attr = <?' */
1697 case eTokenClear:
1698 pXML->error = eXMLErrorUnexpectedToken;
1699 return FALSE;
1700 break;
1701 default: break;
1702 }
1703 }
1704 }
1705 }
1706 // If we failed to obtain the next token
1707 else
1708 {
1709 if ((!d->isDeclaration)&&(d->pParent))
1710 {
1711 #ifdef STRICT_PARSING
1712 pXML->error=eXMLErrorUnmatchedEndTag;
1713 #else
1714 pXML->error=eXMLErrorMissingEndTag;
1715 #endif
1716 pXML->nIndexMissigEndTag=pXML->nIndex;
1717 }
1718 maybeAddTxT(pXML,pXML->lpXML+pXML->nIndex);
1719 return FALSE;
1720 }
1721 }
1722 }
1723
1724 // Count the number of lines and columns in an XML string.
CountLinesAndColumns(XMLCSTR lpXML,int nUpto,XMLResults * pResults)1725 static void CountLinesAndColumns(XMLCSTR lpXML, int nUpto, XMLResults *pResults)
1726 {
1727 XMLCHAR ch;
1728 assert(lpXML);
1729 assert(pResults);
1730
1731 struct XML xml={ lpXML,lpXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
1732
1733 pResults->nLine = 1;
1734 pResults->nColumn = 1;
1735 while (xml.nIndex<nUpto)
1736 {
1737 ch = getNextChar(&xml);
1738 if (ch != _CXML('\n')) pResults->nColumn++;
1739 else
1740 {
1741 pResults->nLine++;
1742 pResults->nColumn=1;
1743 }
1744 }
1745 }
1746
1747 // Parse XML and return the root element.
parseString(XMLCSTR lpszXML,XMLCSTR tag,XMLResults * pResults)1748 XMLNode XMLNode::parseString(XMLCSTR lpszXML, XMLCSTR tag, XMLResults *pResults)
1749 {
1750 if (!lpszXML)
1751 {
1752 if (pResults)
1753 {
1754 pResults->error=eXMLErrorNoElements;
1755 pResults->nLine=0;
1756 pResults->nColumn=0;
1757 }
1758 return emptyXMLNode;
1759 }
1760
1761 XMLNode xnode(NULL,NULL,FALSE);
1762 struct XML xml={ lpszXML, lpszXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
1763
1764 // Create header element
1765 xnode.ParseXMLElement(&xml);
1766 enum XMLError error = xml.error;
1767 if (!xnode.nChildNode()) error=eXMLErrorNoXMLTagFound;
1768 if ((xnode.nChildNode()==1)&&(xnode.nElement()==1)) xnode=xnode.getChildNode(); // skip the empty node
1769
1770 // If no error occurred
1771 if ((error==eXMLErrorNone)||(error==eXMLErrorMissingEndTag)||(error==eXMLErrorNoXMLTagFound))
1772 {
1773 XMLCSTR name=xnode.getName();
1774 if (tag&&(*tag)&&((!name)||(xstricmp(name,tag))))
1775 {
1776 xnode=xnode.getChildNode(tag);
1777 if (xnode.isEmpty())
1778 {
1779 if (pResults)
1780 {
1781 pResults->error=eXMLErrorFirstTagNotFound;
1782 pResults->nLine=0;
1783 pResults->nColumn=0;
1784 }
1785 return emptyXMLNode;
1786 }
1787 }
1788 } else
1789 {
1790 // Cleanup: this will destroy all the nodes
1791 xnode = emptyXMLNode;
1792 }
1793
1794
1795 // If we have been given somewhere to place results
1796 if (pResults)
1797 {
1798 pResults->error = error;
1799
1800 // If we have an error
1801 if (error!=eXMLErrorNone)
1802 {
1803 if (error==eXMLErrorMissingEndTag) xml.nIndex=xml.nIndexMissigEndTag;
1804 // Find which line and column it starts on.
1805 CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
1806 }
1807 }
1808 return xnode;
1809 }
1810
parseFile(XMLCSTR filename,XMLCSTR tag,XMLResults * pResults)1811 XMLNode XMLNode::parseFile(XMLCSTR filename, XMLCSTR tag, XMLResults *pResults)
1812 {
1813 if (pResults) { pResults->nLine=0; pResults->nColumn=0; }
1814 FILE *f=xfopen(filename,_CXML("rb"));
1815 if (f==NULL) { if (pResults) pResults->error=eXMLErrorFileNotFound; return emptyXMLNode; }
1816 fseek(f,0,SEEK_END);
1817 int l=(int)ftell(f),headerSz=0;
1818 if (!l) { if (pResults) pResults->error=eXMLErrorEmpty; fclose(f); return emptyXMLNode; }
1819 fseek(f,0,SEEK_SET);
1820 unsigned char *buf=(unsigned char*)malloc(l+4);
1821 l=(int)fread(buf,1,l,f);
1822 fclose(f);
1823 buf[l]=0;buf[l+1]=0;buf[l+2]=0;buf[l+3]=0;
1824 #ifdef _XMLWIDECHAR
1825 if (guessWideCharChars)
1826 {
1827 if (!myIsTextWideChar(buf,l))
1828 {
1829 XMLNode::XMLCharEncoding ce=XMLNode::char_encoding_legacy;
1830 if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) { headerSz=3; ce=XMLNode::char_encoding_UTF8; }
1831 XMLSTR b2=myMultiByteToWideChar((const char*)(buf+headerSz),ce);
1832 if (!b2)
1833 {
1834 // todo: unable to convert
1835 }
1836 free(buf); buf=(unsigned char*)b2; headerSz=0;
1837 } else
1838 {
1839 if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1840 if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1841 }
1842 } else
1843 {
1844 if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1845 if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1846 if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1847 }
1848 #else
1849 if (guessWideCharChars)
1850 {
1851 if (myIsTextWideChar(buf,l))
1852 {
1853 if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1854 if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1855 char *b2=myWideCharToMultiByte((const wchar_t*)(buf+headerSz));
1856 free(buf); buf=(unsigned char*)b2; headerSz=0;
1857 } else
1858 {
1859 if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1860 }
1861 } else
1862 {
1863 if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2;
1864 if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2;
1865 if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3;
1866 }
1867 #endif
1868
1869 if (!buf) { if (pResults) pResults->error=eXMLErrorCharConversionError; return emptyXMLNode; }
1870 XMLNode x=parseString((XMLSTR)(buf+headerSz),tag,pResults);
1871 free(buf);
1872 return x;
1873 }
1874
charmemset(XMLSTR dest,XMLCHAR c,int l)1875 static inline void charmemset(XMLSTR dest,XMLCHAR c,int l) { while (l--) *(dest++)=c; }
1876 // private:
1877 // Creates an user friendly XML string from a given element with
1878 // appropriate white space and carriage returns.
1879 //
1880 // This recurses through all subnodes then adds contents of the nodes to the
1881 // string.
CreateXMLStringR(XMLNodeData * pEntry,XMLSTR lpszMarker,int nFormat)1882 int XMLNode::CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat)
1883 {
1884 int nResult = 0;
1885 int cb=nFormat<0?0:nFormat;
1886 int cbElement;
1887 int nChildFormat=-1;
1888 int nElementI=pEntry->nChild+pEntry->nText+pEntry->nClear;
1889 int i,j;
1890 if ((nFormat>=0)&&(nElementI==1)&&(pEntry->nText==1)&&(!pEntry->isDeclaration)) nFormat=-2;
1891
1892 assert(pEntry);
1893
1894 #define LENSTR(lpsz) (lpsz ? xstrlen(lpsz) : 0)
1895
1896 // If the element has no name then assume this is the head node.
1897 cbElement = (int)LENSTR(pEntry->lpszName);
1898
1899 if (cbElement)
1900 {
1901 // "<elementname "
1902 if (lpszMarker)
1903 {
1904 if (cb) charmemset(lpszMarker, INDENTCHAR, cb);
1905 nResult = cb;
1906 lpszMarker[nResult++]=_CXML('<');
1907 if (pEntry->isDeclaration) lpszMarker[nResult++]=_CXML('?');
1908 xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
1909 nResult+=cbElement;
1910 lpszMarker[nResult++]=_CXML(' ');
1911
1912 } else
1913 {
1914 nResult+=cbElement+2+cb;
1915 if (pEntry->isDeclaration) nResult++;
1916 }
1917
1918 // Enumerate attributes and add them to the string
1919 XMLAttribute *pAttr=pEntry->pAttribute;
1920 for (i=0; i<pEntry->nAttribute; i++)
1921 {
1922 // "Attrib
1923 cb = (int)LENSTR(pAttr->lpszName);
1924 if (cb)
1925 {
1926 if (lpszMarker) xstrcpy(&lpszMarker[nResult], pAttr->lpszName);
1927 nResult += cb;
1928 // "Attrib=Value "
1929 if (pAttr->lpszValue)
1930 {
1931 cb=(int)ToXMLStringTool::lengthXMLString(pAttr->lpszValue);
1932 if (lpszMarker)
1933 {
1934 lpszMarker[nResult]=_CXML('=');
1935 lpszMarker[nResult+1]=_CXML('"');
1936 if (cb) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+2],pAttr->lpszValue);
1937 lpszMarker[nResult+cb+2]=_CXML('"');
1938 }
1939 nResult+=cb+3;
1940 }
1941 if (lpszMarker) lpszMarker[nResult] = _CXML(' ');
1942 nResult++;
1943 }
1944 pAttr++;
1945 }
1946
1947 if (pEntry->isDeclaration)
1948 {
1949 if (lpszMarker)
1950 {
1951 lpszMarker[nResult-1]=_CXML('?');
1952 lpszMarker[nResult]=_CXML('>');
1953 }
1954 nResult++;
1955 if (nFormat!=-1)
1956 {
1957 if (lpszMarker) lpszMarker[nResult]=_CXML('\n');
1958 nResult++;
1959 }
1960 } else
1961 // If there are child nodes we need to terminate the start tag
1962 if (nElementI)
1963 {
1964 if (lpszMarker) lpszMarker[nResult-1]=_CXML('>');
1965 if (nFormat>=0)
1966 {
1967 if (lpszMarker) lpszMarker[nResult]=_CXML('\n');
1968 nResult++;
1969 }
1970 } else nResult--;
1971 }
1972
1973 // Calculate the child format for when we recurse. This is used to
1974 // determine the number of spaces used for prefixes.
1975 if (nFormat!=-1)
1976 {
1977 if (cbElement&&(!pEntry->isDeclaration)) nChildFormat=nFormat+1;
1978 else nChildFormat=nFormat;
1979 }
1980
1981 // Enumerate through remaining children
1982 for (i=0; i<nElementI; i++)
1983 {
1984 j=pEntry->pOrder[i];
1985 switch((XMLElementType)(j&3))
1986 {
1987 // Text nodes
1988 case eNodeText:
1989 {
1990 // "Text"
1991 XMLCSTR pChild=pEntry->pText[j>>2];
1992 cb = (int)ToXMLStringTool::lengthXMLString(pChild);
1993 if (cb)
1994 {
1995 if (nFormat>=0)
1996 {
1997 if (lpszMarker)
1998 {
1999 charmemset(&lpszMarker[nResult],INDENTCHAR,nFormat+1);
2000 ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+nFormat+1],pChild);
2001 lpszMarker[nResult+nFormat+1+cb]=_CXML('\n');
2002 }
2003 nResult+=cb+nFormat+2;
2004 } else
2005 {
2006 if (lpszMarker) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult], pChild);
2007 nResult += cb;
2008 }
2009 }
2010 break;
2011 }
2012
2013 // Clear type nodes
2014 case eNodeClear:
2015 {
2016 XMLClear *pChild=pEntry->pClear+(j>>2);
2017 // "OpenTag"
2018 cb = (int)LENSTR(pChild->lpszOpenTag);
2019 if (cb)
2020 {
2021 if (nFormat!=-1)
2022 {
2023 if (lpszMarker)
2024 {
2025 charmemset(&lpszMarker[nResult], INDENTCHAR, nFormat+1);
2026 xstrcpy(&lpszMarker[nResult+nFormat+1], pChild->lpszOpenTag);
2027 }
2028 nResult+=cb+nFormat+1;
2029 }
2030 else
2031 {
2032 if (lpszMarker)xstrcpy(&lpszMarker[nResult], pChild->lpszOpenTag);
2033 nResult += cb;
2034 }
2035 }
2036
2037 // "OpenTag Value"
2038 cb = (int)LENSTR(pChild->lpszValue);
2039 if (cb)
2040 {
2041 if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszValue);
2042 nResult += cb;
2043 }
2044
2045 // "OpenTag Value CloseTag"
2046 cb = (int)LENSTR(pChild->lpszCloseTag);
2047 if (cb)
2048 {
2049 if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszCloseTag);
2050 nResult += cb;
2051 }
2052
2053 if (nFormat!=-1)
2054 {
2055 if (lpszMarker) lpszMarker[nResult] = _CXML('\n');
2056 nResult++;
2057 }
2058 break;
2059 }
2060
2061 // Element nodes
2062 case eNodeChild:
2063 {
2064 // Recursively add child nodes
2065 nResult += CreateXMLStringR(pEntry->pChild[j>>2].d, lpszMarker ? lpszMarker + nResult : 0, nChildFormat);
2066 break;
2067 }
2068 default: break;
2069 }
2070 }
2071
2072 if ((cbElement)&&(!pEntry->isDeclaration))
2073 {
2074 // If we have child entries we need to use long XML notation for
2075 // closing the element - "<elementname>blah blah blah</elementname>"
2076 if (nElementI)
2077 {
2078 // "</elementname>\0"
2079 if (lpszMarker)
2080 {
2081 if (nFormat >=0)
2082 {
2083 charmemset(&lpszMarker[nResult], INDENTCHAR,nFormat);
2084 nResult+=nFormat;
2085 }
2086
2087 lpszMarker[nResult]=_CXML('<'); lpszMarker[nResult+1]=_CXML('/');
2088 nResult += 2;
2089 xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
2090 nResult += cbElement;
2091
2092 lpszMarker[nResult]=_CXML('>');
2093 if (nFormat == -1) nResult++;
2094 else
2095 {
2096 lpszMarker[nResult+1]=_CXML('\n');
2097 nResult+=2;
2098 }
2099 } else
2100 {
2101 if (nFormat>=0) nResult+=cbElement+4+nFormat;
2102 else if (nFormat==-1) nResult+=cbElement+3;
2103 else nResult+=cbElement+4;
2104 }
2105 } else
2106 {
2107 // If there are no children we can use shorthand XML notation -
2108 // "<elementname/>"
2109 // "/>\0"
2110 if (lpszMarker)
2111 {
2112 lpszMarker[nResult]=_CXML('/'); lpszMarker[nResult+1]=_CXML('>');
2113 if (nFormat != -1) lpszMarker[nResult+2]=_CXML('\n');
2114 }
2115 nResult += nFormat == -1 ? 2 : 3;
2116 }
2117 }
2118
2119 return nResult;
2120 }
2121
2122 #undef LENSTR
2123
2124 // Create an XML string
2125 // @param int nFormat - 0 if no formatting is required
2126 // otherwise nonzero for formatted text
2127 // with carriage returns and indentation.
2128 // @param int *pnSize - [out] pointer to the size of the
2129 // returned string not including the
2130 // NULL terminator.
2131 // @return XMLSTR - Allocated XML string, you must free
2132 // this with free().
createXMLString(int nFormat,int * pnSize) const2133 XMLSTR XMLNode::createXMLString(int nFormat, int *pnSize) const
2134 {
2135 if (!d) { if (pnSize) *pnSize=0; return NULL; }
2136
2137 XMLSTR lpszResult = NULL;
2138 int cbStr;
2139
2140 // Recursively Calculate the size of the XML string
2141 if (!dropWhiteSpace) nFormat=0;
2142 nFormat = nFormat ? 0 : -1;
2143 cbStr = CreateXMLStringR(d, 0, nFormat);
2144 // Alllocate memory for the XML string + the NULL terminator and
2145 // create the recursively XML string.
2146 lpszResult=(XMLSTR)malloc((cbStr+1)*sizeof(XMLCHAR));
2147 CreateXMLStringR(d, lpszResult, nFormat);
2148 lpszResult[cbStr]=_CXML('\0');
2149 if (pnSize) *pnSize = cbStr;
2150 return lpszResult;
2151 }
2152
detachFromParent(XMLNodeData * d)2153 int XMLNode::detachFromParent(XMLNodeData *d)
2154 {
2155 XMLNode *pa=d->pParent->pChild;
2156 int i=0;
2157 while (((void*)(pa[i].d))!=((void*)d)) i++;
2158 d->pParent->nChild--;
2159 if (d->pParent->nChild) memmove(pa+i,pa+i+1,(d->pParent->nChild-i)*sizeof(XMLNode));
2160 else { free(pa); d->pParent->pChild=NULL; }
2161 return removeOrderElement(d->pParent,eNodeChild,i);
2162 }
2163
~XMLNode()2164 XMLNode::~XMLNode()
2165 {
2166 if (!d) return;
2167 d->ref_count--;
2168 emptyTheNode(0);
2169 }
deleteNodeContent()2170 void XMLNode::deleteNodeContent()
2171 {
2172 if (!d) return;
2173 if (d->pParent) { detachFromParent(d); d->pParent=NULL; d->ref_count--; }
2174 emptyTheNode(1);
2175 }
emptyTheNode(char force)2176 void XMLNode::emptyTheNode(char force)
2177 {
2178 XMLNodeData *dd=d; // warning: must stay this way!
2179 if ((dd->ref_count==0)||force)
2180 {
2181 if (d->pParent) detachFromParent(d);
2182 int i;
2183 XMLNode *pc;
2184 for(i=0; i<dd->nChild; i++)
2185 {
2186 pc=dd->pChild+i;
2187 pc->d->pParent=NULL;
2188 pc->d->ref_count--;
2189 pc->emptyTheNode(force);
2190 }
2191 myFree(dd->pChild);
2192 for(i=0; i<dd->nText; i++) free((void*)dd->pText[i]);
2193 myFree(dd->pText);
2194 for(i=0; i<dd->nClear; i++) free((void*)dd->pClear[i].lpszValue);
2195 myFree(dd->pClear);
2196 for(i=0; i<dd->nAttribute; i++)
2197 {
2198 free((void*)dd->pAttribute[i].lpszName);
2199 if (dd->pAttribute[i].lpszValue) free((void*)dd->pAttribute[i].lpszValue);
2200 }
2201 myFree(dd->pAttribute);
2202 myFree(dd->pOrder);
2203 myFree((void*)dd->lpszName);
2204 dd->nChild=0; dd->nText=0; dd->nClear=0; dd->nAttribute=0;
2205 dd->pChild=NULL; dd->pText=NULL; dd->pClear=NULL; dd->pAttribute=NULL;
2206 dd->pOrder=NULL; dd->lpszName=NULL; dd->pParent=NULL;
2207 }
2208 if (dd->ref_count==0)
2209 {
2210 free(dd);
2211 d=NULL;
2212 }
2213 }
2214
operator =(const XMLNode & A)2215 XMLNode& XMLNode::operator=( const XMLNode& A )
2216 {
2217 // shallow copy
2218 if (this != &A)
2219 {
2220 if (d) { d->ref_count--; emptyTheNode(0); }
2221 d=A.d;
2222 if (d) (d->ref_count) ++ ;
2223 }
2224 return *this;
2225 }
2226
XMLNode(const XMLNode & A)2227 XMLNode::XMLNode(const XMLNode &A)
2228 {
2229 // shallow copy
2230 d=A.d;
2231 if (d) (d->ref_count)++ ;
2232 }
2233
deepCopy() const2234 XMLNode XMLNode::deepCopy() const
2235 {
2236 if (!d) return XMLNode::emptyXMLNode;
2237 XMLNode x(NULL,stringDup(d->lpszName),d->isDeclaration);
2238 XMLNodeData *p=x.d;
2239 int n=d->nAttribute;
2240 if (n)
2241 {
2242 p->nAttribute=n; p->pAttribute=(XMLAttribute*)malloc(n*sizeof(XMLAttribute));
2243 while (n--)
2244 {
2245 p->pAttribute[n].lpszName=stringDup(d->pAttribute[n].lpszName);
2246 p->pAttribute[n].lpszValue=stringDup(d->pAttribute[n].lpszValue);
2247 }
2248 }
2249 if (d->pOrder)
2250 {
2251 n=(d->nChild+d->nText+d->nClear)*sizeof(int); p->pOrder=(int*)malloc(n); memcpy(p->pOrder,d->pOrder,n);
2252 }
2253 n=d->nText;
2254 if (n)
2255 {
2256 p->nText=n; p->pText=(XMLCSTR*)malloc(n*sizeof(XMLCSTR));
2257 while(n--) p->pText[n]=stringDup(d->pText[n]);
2258 }
2259 n=d->nClear;
2260 if (n)
2261 {
2262 p->nClear=n; p->pClear=(XMLClear*)malloc(n*sizeof(XMLClear));
2263 while (n--)
2264 {
2265 p->pClear[n].lpszCloseTag=d->pClear[n].lpszCloseTag;
2266 p->pClear[n].lpszOpenTag=d->pClear[n].lpszOpenTag;
2267 p->pClear[n].lpszValue=stringDup(d->pClear[n].lpszValue);
2268 }
2269 }
2270 n=d->nChild;
2271 if (n)
2272 {
2273 p->nChild=n; p->pChild=(XMLNode*)malloc(n*sizeof(XMLNode));
2274 while (n--)
2275 {
2276 p->pChild[n].d=NULL;
2277 p->pChild[n]=d->pChild[n].deepCopy();
2278 p->pChild[n].d->pParent=p;
2279 }
2280 }
2281 return x;
2282 }
2283
addChild(XMLNode childNode,int pos)2284 XMLNode XMLNode::addChild(XMLNode childNode, int pos)
2285 {
2286 XMLNodeData *dc=childNode.d;
2287 if ((!dc)||(!d)) return childNode;
2288 if (!dc->lpszName)
2289 {
2290 // this is a root node: todo: correct fix
2291 int j=pos;
2292 while (dc->nChild)
2293 {
2294 addChild(dc->pChild[0],j);
2295 if (pos>=0) j++;
2296 }
2297 return childNode;
2298 }
2299 if (dc->pParent) { if ((detachFromParent(dc)<=pos)&&(dc->pParent==d)) pos--; } else dc->ref_count++;
2300 dc->pParent=d;
2301 // int nc=d->nChild;
2302 // d->pChild=(XMLNode*)myRealloc(d->pChild,(nc+1),memoryIncrease,sizeof(XMLNode));
2303 d->pChild=(XMLNode*)addToOrder(0,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
2304 d->pChild[pos].d=dc;
2305 d->nChild++;
2306 return childNode;
2307 }
2308
deleteAttribute(int i)2309 void XMLNode::deleteAttribute(int i)
2310 {
2311 if ((!d)||(i<0)||(i>=d->nAttribute)) return;
2312 d->nAttribute--;
2313 XMLAttribute *p=d->pAttribute+i;
2314 free((void*)p->lpszName);
2315 if (p->lpszValue) free((void*)p->lpszValue);
2316 if (d->nAttribute) memmove(p,p+1,(d->nAttribute-i)*sizeof(XMLAttribute)); else { free(p); d->pAttribute=NULL; }
2317 }
2318
deleteAttribute(XMLAttribute * a)2319 void XMLNode::deleteAttribute(XMLAttribute *a){ if (a) deleteAttribute(a->lpszName); }
deleteAttribute(XMLCSTR lpszName)2320 void XMLNode::deleteAttribute(XMLCSTR lpszName)
2321 {
2322 int j=0;
2323 getAttribute(lpszName,&j);
2324 if (j) deleteAttribute(j-1);
2325 }
2326
updateAttribute_WOSD(XMLSTR lpszNewValue,XMLSTR lpszNewName,int i)2327 XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,int i)
2328 {
2329 if (!d) { if (lpszNewValue) free(lpszNewValue); if (lpszNewName) free(lpszNewName); return NULL; }
2330 if (i>=d->nAttribute)
2331 {
2332 if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue);
2333 return NULL;
2334 }
2335 XMLAttribute *p=d->pAttribute+i;
2336 if (p->lpszValue&&p->lpszValue!=lpszNewValue) free((void*)p->lpszValue);
2337 p->lpszValue=lpszNewValue;
2338 if (lpszNewName&&p->lpszName!=lpszNewName) { free((void*)p->lpszName); p->lpszName=lpszNewName; };
2339 return p;
2340 }
2341
updateAttribute_WOSD(XMLAttribute * newAttribute,XMLAttribute * oldAttribute)2342 XMLAttribute *XMLNode::updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
2343 {
2344 if (oldAttribute) return updateAttribute_WOSD((XMLSTR)newAttribute->lpszValue,(XMLSTR)newAttribute->lpszName,oldAttribute->lpszName);
2345 return addAttribute_WOSD((XMLSTR)newAttribute->lpszName,(XMLSTR)newAttribute->lpszValue);
2346 }
2347
updateAttribute_WOSD(XMLSTR lpszNewValue,XMLSTR lpszNewName,XMLCSTR lpszOldName)2348 XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,XMLCSTR lpszOldName)
2349 {
2350 int j=0;
2351 getAttribute(lpszOldName,&j);
2352 if (j) return updateAttribute_WOSD(lpszNewValue,lpszNewName,j-1);
2353 else
2354 {
2355 if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue);
2356 else return addAttribute_WOSD(stringDup(lpszOldName),lpszNewValue);
2357 }
2358 }
2359
indexText(XMLCSTR lpszValue) const2360 int XMLNode::indexText(XMLCSTR lpszValue) const
2361 {
2362 if (!d) return -1;
2363 int i,l=d->nText;
2364 if (!lpszValue) { if (l) return 0; return -1; }
2365 XMLCSTR *p=d->pText;
2366 for (i=0; i<l; i++) if (lpszValue==p[i]) return i;
2367 return -1;
2368 }
2369
deleteText(int i)2370 void XMLNode::deleteText(int i)
2371 {
2372 if ((!d)||(i<0)||(i>=d->nText)) return;
2373 d->nText--;
2374 XMLCSTR *p=d->pText+i;
2375 free((void*)*p);
2376 if (d->nText) memmove(p,p+1,(d->nText-i)*sizeof(XMLCSTR)); else { free(p); d->pText=NULL; }
2377 removeOrderElement(d,eNodeText,i);
2378 }
2379
deleteText(XMLCSTR lpszValue)2380 void XMLNode::deleteText(XMLCSTR lpszValue) { deleteText(indexText(lpszValue)); }
2381
updateText_WOSD(XMLSTR lpszNewValue,int i)2382 XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, int i)
2383 {
2384 if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; }
2385 if (i>=d->nText) return addText_WOSD(lpszNewValue);
2386 XMLCSTR *p=d->pText+i;
2387 if (*p!=lpszNewValue) { free((void*)*p); *p=lpszNewValue; }
2388 return lpszNewValue;
2389 }
2390
updateText_WOSD(XMLSTR lpszNewValue,XMLCSTR lpszOldValue)2391 XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue)
2392 {
2393 if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; }
2394 int i=indexText(lpszOldValue);
2395 if (i>=0) return updateText_WOSD(lpszNewValue,i);
2396 return addText_WOSD(lpszNewValue);
2397 }
2398
deleteClear(int i)2399 void XMLNode::deleteClear(int i)
2400 {
2401 if ((!d)||(i<0)||(i>=d->nClear)) return;
2402 d->nClear--;
2403 XMLClear *p=d->pClear+i;
2404 free((void*)p->lpszValue);
2405 if (d->nClear) memmove(p,p+1,(d->nClear-i)*sizeof(XMLClear)); else { free(p); d->pClear=NULL; }
2406 removeOrderElement(d,eNodeClear,i);
2407 }
2408
indexClear(XMLCSTR lpszValue) const2409 int XMLNode::indexClear(XMLCSTR lpszValue) const
2410 {
2411 if (!d) return -1;
2412 int i,l=d->nClear;
2413 if (!lpszValue) { if (l) return 0; return -1; }
2414 XMLClear *p=d->pClear;
2415 for (i=0; i<l; i++) if (lpszValue==p[i].lpszValue) return i;
2416 return -1;
2417 }
2418
deleteClear(XMLCSTR lpszValue)2419 void XMLNode::deleteClear(XMLCSTR lpszValue) { deleteClear(indexClear(lpszValue)); }
deleteClear(XMLClear * a)2420 void XMLNode::deleteClear(XMLClear *a) { if (a) deleteClear(a->lpszValue); }
2421
updateClear_WOSD(XMLSTR lpszNewContent,int i)2422 XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, int i)
2423 {
2424 if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; }
2425 if (i>=d->nClear) return addClear_WOSD(lpszNewContent);
2426 XMLClear *p=d->pClear+i;
2427 if (lpszNewContent!=p->lpszValue) { free((void*)p->lpszValue); p->lpszValue=lpszNewContent; }
2428 return p;
2429 }
2430
updateClear_WOSD(XMLSTR lpszNewContent,XMLCSTR lpszOldValue)2431 XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, XMLCSTR lpszOldValue)
2432 {
2433 if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; }
2434 int i=indexClear(lpszOldValue);
2435 if (i>=0) return updateClear_WOSD(lpszNewContent,i);
2436 return addClear_WOSD(lpszNewContent);
2437 }
2438
updateClear_WOSD(XMLClear * newP,XMLClear * oldP)2439 XMLClear *XMLNode::updateClear_WOSD(XMLClear *newP,XMLClear *oldP)
2440 {
2441 if (oldP) return updateClear_WOSD((XMLSTR)newP->lpszValue,(XMLSTR)oldP->lpszValue);
2442 return NULL;
2443 }
2444
nChildNode(XMLCSTR name) const2445 int XMLNode::nChildNode(XMLCSTR name) const
2446 {
2447 if (!d) return 0;
2448 int i,j=0,n=d->nChild;
2449 XMLNode *pc=d->pChild;
2450 for (i=0; i<n; i++)
2451 {
2452 if (xstricmp(pc->d->lpszName, name)==0) j++;
2453 pc++;
2454 }
2455 return j;
2456 }
2457
getChildNode(XMLCSTR name,int * j) const2458 XMLNode XMLNode::getChildNode(XMLCSTR name, int *j) const
2459 {
2460 if (!d) return emptyXMLNode;
2461 int i=0,n=d->nChild;
2462 if (j) i=*j;
2463 XMLNode *pc=d->pChild+i;
2464 for (; i<n; i++)
2465 {
2466 if (!xstricmp(pc->d->lpszName, name))
2467 {
2468 if (j) *j=i+1;
2469 return *pc;
2470 }
2471 pc++;
2472 }
2473 return emptyXMLNode;
2474 }
2475
getChildNode(XMLCSTR name,int j) const2476 XMLNode XMLNode::getChildNode(XMLCSTR name, int j) const
2477 {
2478 if (!d) return emptyXMLNode;
2479 if (j>=0)
2480 {
2481 int i=0;
2482 while (j-->0) getChildNode(name,&i);
2483 return getChildNode(name,&i);
2484 }
2485 int i=d->nChild;
2486 while (i--) if (!xstricmp(name,d->pChild[i].d->lpszName)) break;
2487 if (i<0) return emptyXMLNode;
2488 return getChildNode(i);
2489 }
2490
getChildNodeByPath(XMLCSTR _path,char createMissing,XMLCHAR sep)2491 XMLNode XMLNode::getChildNodeByPath(XMLCSTR _path, char createMissing, XMLCHAR sep)
2492 {
2493 XMLSTR path=stringDup(_path);
2494 XMLNode x=getChildNodeByPathNonConst(path,createMissing,sep);
2495 if (path) free(path);
2496 return x;
2497 }
2498
getChildNodeByPathNonConst(XMLSTR path,char createIfMissing,XMLCHAR sep)2499 XMLNode XMLNode::getChildNodeByPathNonConst(XMLSTR path, char createIfMissing, XMLCHAR sep)
2500 {
2501 if ((!path)||(!(*path))) return *this;
2502 XMLNode xn,xbase=*this;
2503 XMLCHAR *tend1,sepString[2]; sepString[0]=sep; sepString[1]=0;
2504 tend1=xstrstr(path,sepString);
2505 while(tend1)
2506 {
2507 *tend1=0;
2508 xn=xbase.getChildNode(path);
2509 if (xn.isEmpty())
2510 {
2511 if (createIfMissing) xn=xbase.addChild(path);
2512 else { *tend1=sep; return XMLNode::emptyXMLNode; }
2513 }
2514 *tend1=sep;
2515 xbase=xn;
2516 path=tend1+1;
2517 tend1=xstrstr(path,sepString);
2518 }
2519 xn=xbase.getChildNode(path);
2520 if (xn.isEmpty()&&createIfMissing) xn=xbase.addChild(path);
2521 return xn;
2522 }
2523
positionOfText(int i) const2524 XMLElementPosition XMLNode::positionOfText (int i) const { if (i>=d->nText ) i=d->nText-1; return findPosition(d,i,eNodeText ); }
positionOfClear(int i) const2525 XMLElementPosition XMLNode::positionOfClear (int i) const { if (i>=d->nClear) i=d->nClear-1; return findPosition(d,i,eNodeClear); }
positionOfChildNode(int i) const2526 XMLElementPosition XMLNode::positionOfChildNode(int i) const { if (i>=d->nChild) i=d->nChild-1; return findPosition(d,i,eNodeChild); }
positionOfText(XMLCSTR lpszValue) const2527 XMLElementPosition XMLNode::positionOfText (XMLCSTR lpszValue) const { return positionOfText (indexText (lpszValue)); }
positionOfClear(XMLCSTR lpszValue) const2528 XMLElementPosition XMLNode::positionOfClear(XMLCSTR lpszValue) const { return positionOfClear(indexClear(lpszValue)); }
positionOfClear(XMLClear * a) const2529 XMLElementPosition XMLNode::positionOfClear(XMLClear *a) const { if (a) return positionOfClear(a->lpszValue); return positionOfClear(); }
positionOfChildNode(XMLNode x) const2530 XMLElementPosition XMLNode::positionOfChildNode(XMLNode x) const
2531 {
2532 if ((!d)||(!x.d)) return -1;
2533 XMLNodeData *dd=x.d;
2534 XMLNode *pc=d->pChild;
2535 int i=d->nChild;
2536 while (i--) if (pc[i].d==dd) return findPosition(d,i,eNodeChild);
2537 return -1;
2538 }
positionOfChildNode(XMLCSTR name,int count) const2539 XMLElementPosition XMLNode::positionOfChildNode(XMLCSTR name, int count) const
2540 {
2541 if (!name) return positionOfChildNode(count);
2542 int j=0;
2543 do { getChildNode(name,&j); if (j<0) return -1; } while (count--);
2544 return findPosition(d,j-1,eNodeChild);
2545 }
2546
getChildNodeWithAttribute(XMLCSTR name,XMLCSTR attributeName,XMLCSTR attributeValue,int * k) const2547 XMLNode XMLNode::getChildNodeWithAttribute(XMLCSTR name,XMLCSTR attributeName,XMLCSTR attributeValue, int *k) const
2548 {
2549 int i=0,j;
2550 if (k) i=*k;
2551 XMLNode x;
2552 XMLCSTR t;
2553 do
2554 {
2555 x=getChildNode(name,&i);
2556 if (!x.isEmpty())
2557 {
2558 if (attributeValue)
2559 {
2560 j=0;
2561 do
2562 {
2563 t=x.getAttribute(attributeName,&j);
2564 if (t&&(xstricmp(attributeValue,t)==0)) { if (k) *k=i; return x; }
2565 } while (t);
2566 } else
2567 {
2568 if (x.isAttributeSet(attributeName)) { if (k) *k=i; return x; }
2569 }
2570 }
2571 } while (!x.isEmpty());
2572 return emptyXMLNode;
2573 }
2574
2575 // Find an attribute on an node.
getAttribute(XMLCSTR lpszAttrib,int * j) const2576 XMLCSTR XMLNode::getAttribute(XMLCSTR lpszAttrib, int *j) const
2577 {
2578 if (!d) return NULL;
2579 int i=0,n=d->nAttribute;
2580 if (j) i=*j;
2581 XMLAttribute *pAttr=d->pAttribute+i;
2582 for (; i<n; i++)
2583 {
2584 if (xstricmp(pAttr->lpszName, lpszAttrib)==0)
2585 {
2586 if (j) *j=i+1;
2587 return pAttr->lpszValue;
2588 }
2589 pAttr++;
2590 }
2591 return NULL;
2592 }
2593
isAttributeSet(XMLCSTR lpszAttrib) const2594 char XMLNode::isAttributeSet(XMLCSTR lpszAttrib) const
2595 {
2596 if (!d) return FALSE;
2597 int i,n=d->nAttribute;
2598 XMLAttribute *pAttr=d->pAttribute;
2599 for (i=0; i<n; i++)
2600 {
2601 if (xstricmp(pAttr->lpszName, lpszAttrib)==0)
2602 {
2603 return TRUE;
2604 }
2605 pAttr++;
2606 }
2607 return FALSE;
2608 }
2609
getAttribute(XMLCSTR name,int j) const2610 XMLCSTR XMLNode::getAttribute(XMLCSTR name, int j) const
2611 {
2612 if (!d) return NULL;
2613 int i=0;
2614 while (j-->0) getAttribute(name,&i);
2615 return getAttribute(name,&i);
2616 }
2617
enumContents(int i) const2618 XMLNodeContents XMLNode::enumContents(int i) const
2619 {
2620 XMLNodeContents c;
2621 if (!d) { c.etype=eNodeNULL; return c; }
2622 if (i<d->nAttribute)
2623 {
2624 c.etype=eNodeAttribute;
2625 c.attrib=d->pAttribute[i];
2626 return c;
2627 }
2628 i-=d->nAttribute;
2629 c.etype=(XMLElementType)(d->pOrder[i]&3);
2630 i=(d->pOrder[i])>>2;
2631 switch (c.etype)
2632 {
2633 case eNodeChild: c.child = d->pChild[i]; break;
2634 case eNodeText: c.text = d->pText[i]; break;
2635 case eNodeClear: c.clear = d->pClear[i]; break;
2636 default: break;
2637 }
2638 return c;
2639 }
2640
getName() const2641 XMLCSTR XMLNode::getName() const { if (!d) return NULL; return d->lpszName; }
nText() const2642 int XMLNode::nText() const { if (!d) return 0; return d->nText; }
nChildNode() const2643 int XMLNode::nChildNode() const { if (!d) return 0; return d->nChild; }
nAttribute() const2644 int XMLNode::nAttribute() const { if (!d) return 0; return d->nAttribute; }
nClear() const2645 int XMLNode::nClear() const { if (!d) return 0; return d->nClear; }
nElement() const2646 int XMLNode::nElement() const { if (!d) return 0; return d->nAttribute+d->nChild+d->nText+d->nClear; }
getClear(int i) const2647 XMLClear XMLNode::getClear (int i) const { if ((!d)||(i>=d->nClear )) return emptyXMLClear; return d->pClear[i]; }
getAttribute(int i) const2648 XMLAttribute XMLNode::getAttribute (int i) const { if ((!d)||(i>=d->nAttribute)) return emptyXMLAttribute; return d->pAttribute[i]; }
getAttributeName(int i) const2649 XMLCSTR XMLNode::getAttributeName (int i) const { if ((!d)||(i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszName; }
getAttributeValue(int i) const2650 XMLCSTR XMLNode::getAttributeValue(int i) const { if ((!d)||(i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszValue; }
getText(int i) const2651 XMLCSTR XMLNode::getText (int i) const { if ((!d)||(i>=d->nText )) return NULL; return d->pText[i]; }
getChildNode(int i) const2652 XMLNode XMLNode::getChildNode (int i) const { if ((!d)||(i>=d->nChild )) return emptyXMLNode; return d->pChild[i]; }
getParentNode() const2653 XMLNode XMLNode::getParentNode ( ) const { if ((!d)||(!d->pParent )) return emptyXMLNode; return XMLNode(d->pParent); }
isDeclaration() const2654 char XMLNode::isDeclaration ( ) const { if (!d) return 0; return d->isDeclaration; }
isEmpty() const2655 char XMLNode::isEmpty ( ) const { return (d==NULL); }
emptyNode()2656 XMLNode XMLNode::emptyNode ( ) { return XMLNode::emptyXMLNode; }
2657
addChild(XMLCSTR lpszName,char isDeclaration,XMLElementPosition pos)2658 XMLNode XMLNode::addChild(XMLCSTR lpszName, char isDeclaration, XMLElementPosition pos)
2659 { return addChild_priv(0,stringDup(lpszName),isDeclaration,pos); }
addChild_WOSD(XMLSTR lpszName,char isDeclaration,XMLElementPosition pos)2660 XMLNode XMLNode::addChild_WOSD(XMLSTR lpszName, char isDeclaration, XMLElementPosition pos)
2661 { return addChild_priv(0,lpszName,isDeclaration,pos); }
addAttribute(XMLCSTR lpszName,XMLCSTR lpszValue)2662 XMLAttribute *XMLNode::addAttribute(XMLCSTR lpszName, XMLCSTR lpszValue)
2663 { return addAttribute_priv(0,stringDup(lpszName),stringDup(lpszValue)); }
addAttribute_WOSD(XMLSTR lpszName,XMLSTR lpszValuev)2664 XMLAttribute *XMLNode::addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValuev)
2665 { return addAttribute_priv(0,lpszName,lpszValuev); }
addText(XMLCSTR lpszValue,XMLElementPosition pos)2666 XMLCSTR XMLNode::addText(XMLCSTR lpszValue, XMLElementPosition pos)
2667 { return addText_priv(0,stringDup(lpszValue),pos); }
addText_WOSD(XMLSTR lpszValue,XMLElementPosition pos)2668 XMLCSTR XMLNode::addText_WOSD(XMLSTR lpszValue, XMLElementPosition pos)
2669 { return addText_priv(0,lpszValue,pos); }
addClear(XMLCSTR lpszValue,XMLCSTR lpszOpen,XMLCSTR lpszClose,XMLElementPosition pos)2670 XMLClear *XMLNode::addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos)
2671 { return addClear_priv(0,stringDup(lpszValue),lpszOpen,lpszClose,pos); }
addClear_WOSD(XMLSTR lpszValue,XMLCSTR lpszOpen,XMLCSTR lpszClose,XMLElementPosition pos)2672 XMLClear *XMLNode::addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos)
2673 { return addClear_priv(0,lpszValue,lpszOpen,lpszClose,pos); }
updateName(XMLCSTR lpszName)2674 XMLCSTR XMLNode::updateName(XMLCSTR lpszName)
2675 { return updateName_WOSD(stringDup(lpszName)); }
updateAttribute(XMLAttribute * newAttribute,XMLAttribute * oldAttribute)2676 XMLAttribute *XMLNode::updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
2677 { return updateAttribute_WOSD(stringDup(newAttribute->lpszValue),stringDup(newAttribute->lpszName),oldAttribute->lpszName); }
updateAttribute(XMLCSTR lpszNewValue,XMLCSTR lpszNewName,int i)2678 XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,int i)
2679 { return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),i); }
updateAttribute(XMLCSTR lpszNewValue,XMLCSTR lpszNewName,XMLCSTR lpszOldName)2680 XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName)
2681 { return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),lpszOldName); }
updateText(XMLCSTR lpszNewValue,int i)2682 XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, int i)
2683 { return updateText_WOSD(stringDup(lpszNewValue),i); }
updateText(XMLCSTR lpszNewValue,XMLCSTR lpszOldValue)2684 XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2685 { return updateText_WOSD(stringDup(lpszNewValue),lpszOldValue); }
updateClear(XMLCSTR lpszNewContent,int i)2686 XMLClear *XMLNode::updateClear(XMLCSTR lpszNewContent, int i)
2687 { return updateClear_WOSD(stringDup(lpszNewContent),i); }
updateClear(XMLCSTR lpszNewValue,XMLCSTR lpszOldValue)2688 XMLClear *XMLNode::updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
2689 { return updateClear_WOSD(stringDup(lpszNewValue),lpszOldValue); }
updateClear(XMLClear * newP,XMLClear * oldP)2690 XMLClear *XMLNode::updateClear(XMLClear *newP,XMLClear *oldP)
2691 { return updateClear_WOSD(stringDup(newP->lpszValue),oldP->lpszValue); }
2692
setGlobalOptions(XMLCharEncoding _characterEncoding,char _guessWideCharChars,char _dropWhiteSpace,char _removeCommentsInMiddleOfText)2693 char XMLNode::setGlobalOptions(XMLCharEncoding _characterEncoding, char _guessWideCharChars,
2694 char _dropWhiteSpace, char _removeCommentsInMiddleOfText)
2695 {
2696 guessWideCharChars=_guessWideCharChars; dropWhiteSpace=_dropWhiteSpace; removeCommentsInMiddleOfText=_removeCommentsInMiddleOfText;
2697 #ifdef _XMLWIDECHAR
2698 if (_characterEncoding) characterEncoding=_characterEncoding;
2699 #else
2700 switch(_characterEncoding)
2701 {
2702 case char_encoding_UTF8: characterEncoding=_characterEncoding; XML_ByteTable=XML_utf8ByteTable; break;
2703 case char_encoding_legacy: characterEncoding=_characterEncoding; XML_ByteTable=XML_legacyByteTable; break;
2704 case char_encoding_ShiftJIS: characterEncoding=_characterEncoding; XML_ByteTable=XML_sjisByteTable; break;
2705 case char_encoding_GB2312: characterEncoding=_characterEncoding; XML_ByteTable=XML_gb2312ByteTable; break;
2706 case char_encoding_Big5:
2707 case char_encoding_GBK: characterEncoding=_characterEncoding; XML_ByteTable=XML_gbk_big5_ByteTable; break;
2708 default: return 1;
2709 }
2710 #endif
2711 return 0;
2712 }
2713
guessCharEncoding(void * buf,int l,char useXMLEncodingAttribute)2714 XMLNode::XMLCharEncoding XMLNode::guessCharEncoding(void *buf,int l, char useXMLEncodingAttribute)
2715 {
2716 #ifdef _XMLWIDECHAR
2717 return (XMLCharEncoding)0;
2718 #else
2719 if (l<25) return (XMLCharEncoding)0;
2720 if (guessWideCharChars&&(myIsTextWideChar(buf,l))) return (XMLCharEncoding)0;
2721 unsigned char *b=(unsigned char*)buf;
2722 if ((b[0]==0xef)&&(b[1]==0xbb)&&(b[2]==0xbf)) return char_encoding_UTF8;
2723
2724 // Match utf-8 model ?
2725 XMLCharEncoding bestGuess=char_encoding_UTF8;
2726 int i=0;
2727 while (i<l)
2728 switch (XML_utf8ByteTable[b[i]])
2729 {
2730 case 4: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=char_encoding_legacy; i=l; } // 10bbbbbb ?
2731 case 3: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=char_encoding_legacy; i=l; } // 10bbbbbb ?
2732 case 2: i++; if ((i<l)&&(b[i]& 0xC0)!=0x80) { bestGuess=char_encoding_legacy; i=l; } // 10bbbbbb ?
2733 case 1: i++; break;
2734 case 0: i=l;
2735 }
2736 if (!useXMLEncodingAttribute) return bestGuess;
2737 // if encoding is specified and different from utf-8 than it's non-utf8
2738 // otherwise it's utf-8
2739 char bb[201];
2740 l=mmin(l,200);
2741 memcpy(bb,buf,l); // copy buf into bb to be able to do "bb[l]=0"
2742 bb[l]=0;
2743 b=(unsigned char*)strstr(bb,"encoding");
2744 if (!b) return bestGuess;
2745 b+=8; while XML_isSPACECHAR(*b) b++; if (*b!='=') return bestGuess;
2746 b++; while XML_isSPACECHAR(*b) b++; if ((*b!='\'')&&(*b!='"')) return bestGuess;
2747 b++; while XML_isSPACECHAR(*b) b++;
2748
2749 if ((xstrnicmp((char*)b,"utf-8",5)==0)||
2750 (xstrnicmp((char*)b,"utf8",4)==0))
2751 {
2752 if (bestGuess==char_encoding_legacy) return char_encoding_error;
2753 return char_encoding_UTF8;
2754 }
2755
2756 if ((xstrnicmp((char*)b,"shiftjis",8)==0)||
2757 (xstrnicmp((char*)b,"shift-jis",9)==0)||
2758 (xstrnicmp((char*)b,"sjis",4)==0)) return char_encoding_ShiftJIS;
2759
2760 if (xstrnicmp((char*)b,"GB2312",6)==0) return char_encoding_GB2312;
2761 if (xstrnicmp((char*)b,"Big5",4)==0) return char_encoding_Big5;
2762 if (xstrnicmp((char*)b,"GBK",3)==0) return char_encoding_GBK;
2763
2764 return char_encoding_legacy;
2765 #endif
2766 }
2767 #undef XML_isSPACECHAR
2768
2769 //////////////////////////////////////////////////////////
2770 // Here starts the base64 conversion functions. //
2771 //////////////////////////////////////////////////////////
2772
2773 static const char base64Fillchar = _CXML('='); // used to mark partial words at the end
2774
2775 // this lookup table defines the base64 encoding
2776 XMLCSTR base64EncodeTable=_CXML("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
2777
2778 // Decode Table gives the index of any valid base64 character in the Base64 table]
2779 // 96: '=' - 97: space char - 98: illegal char - 99: end of string
2780 const unsigned char base64DecodeTable[] = {
2781 99,98,98,98,98,98,98,98,98,97, 97,98,98,97,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, //00 -29
2782 98,98,97,98,98,98,98,98,98,98, 98,98,98,62,98,98,98,63,52,53, 54,55,56,57,58,59,60,61,98,98, //30 -59
2783 98,96,98,98,98, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, 15,16,17,18,19,20,21,22,23,24, //60 -89
2784 25,98,98,98,98,98,98,26,27,28, 29,30,31,32,33,34,35,36,37,38, 39,40,41,42,43,44,45,46,47,48, //90 -119
2785 49,50,51,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, //120 -149
2786 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, //150 -179
2787 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, //180 -209
2788 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98,98,98,98,98, //210 -239
2789 98,98,98,98,98,98,98,98,98,98, 98,98,98,98,98,98 //240 -255
2790 };
2791
~XMLParserBase64Tool()2792 XMLParserBase64Tool::~XMLParserBase64Tool(){ freeBuffer(); }
2793
freeBuffer()2794 void XMLParserBase64Tool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; }
2795
encodeLength(int inlen,char formatted)2796 int XMLParserBase64Tool::encodeLength(int inlen, char formatted)
2797 {
2798 unsigned int i=((inlen-1)/3*4+4+1);
2799 if (formatted) i+=inlen/54;
2800 return i;
2801 }
2802
encode(unsigned char * inbuf,unsigned int inlen,char formatted)2803 XMLSTR XMLParserBase64Tool::encode(unsigned char *inbuf, unsigned int inlen, char formatted)
2804 {
2805 int i=encodeLength(inlen,formatted),k=17,eLen=inlen/3,j;
2806 alloc(i*sizeof(XMLCHAR));
2807 XMLSTR curr=(XMLSTR)buf;
2808 for(i=0;i<eLen;i++)
2809 {
2810 // Copy next three bytes into lower 24 bits of int, paying attention to sign.
2811 j=(inbuf[0]<<16)|(inbuf[1]<<8)|inbuf[2]; inbuf+=3;
2812 // Encode the int into four chars
2813 *(curr++)=base64EncodeTable[ j>>18 ];
2814 *(curr++)=base64EncodeTable[(j>>12)&0x3f];
2815 *(curr++)=base64EncodeTable[(j>> 6)&0x3f];
2816 *(curr++)=base64EncodeTable[(j )&0x3f];
2817 if (formatted) { if (!k) { *(curr++)=_CXML('\n'); k=18; } k--; }
2818 }
2819 eLen=inlen-eLen*3; // 0 - 2.
2820 if (eLen==1)
2821 {
2822 *(curr++)=base64EncodeTable[ inbuf[0]>>2 ];
2823 *(curr++)=base64EncodeTable[(inbuf[0]<<4)&0x3F];
2824 *(curr++)=base64Fillchar;
2825 *(curr++)=base64Fillchar;
2826 } else if (eLen==2)
2827 {
2828 j=(inbuf[0]<<8)|inbuf[1];
2829 *(curr++)=base64EncodeTable[ j>>10 ];
2830 *(curr++)=base64EncodeTable[(j>> 4)&0x3f];
2831 *(curr++)=base64EncodeTable[(j<< 2)&0x3f];
2832 *(curr++)=base64Fillchar;
2833 }
2834 *(curr++)=0;
2835 return (XMLSTR)buf;
2836 }
2837
decodeSize(XMLCSTR data,XMLError * xe)2838 unsigned int XMLParserBase64Tool::decodeSize(XMLCSTR data,XMLError *xe)
2839 {
2840 if (!data) return 0;
2841 if (xe) *xe=eXMLErrorNone;
2842 int size=0;
2843 unsigned char c;
2844 //skip any extra characters (e.g. newlines or spaces)
2845 while (*data)
2846 {
2847 #ifdef _XMLWIDECHAR
2848 if (*data>255) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2849 #endif
2850 c=base64DecodeTable[(unsigned char)(*data)];
2851 if (c<97) size++;
2852 else if (c==98) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2853 data++;
2854 }
2855 if (xe&&(size%4!=0)) *xe=eXMLErrorBase64DataSizeIsNotMultipleOf4;
2856 if (size==0) return 0;
2857 do { data--; size--; } while(*data==base64Fillchar); size++;
2858 return (unsigned int)((size*3)/4);
2859 }
2860
decode(XMLCSTR data,unsigned char * buf,int len,XMLError * xe)2861 unsigned char XMLParserBase64Tool::decode(XMLCSTR data, unsigned char *buf, int len, XMLError *xe)
2862 {
2863 if (!data) return 0;
2864 if (xe) *xe=eXMLErrorNone;
2865 int i=0,p=0;
2866 unsigned char d,c;
2867 for(;;)
2868 {
2869
2870 #ifdef _XMLWIDECHAR
2871 #define BASE64DECODE_READ_NEXT_CHAR(c) \
2872 do { \
2873 if (data[i]>255){ c=98; break; } \
2874 c=base64DecodeTable[(unsigned char)data[i++]]; \
2875 }while (c==97); \
2876 if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2877 #else
2878 #define BASE64DECODE_READ_NEXT_CHAR(c) \
2879 do { c=base64DecodeTable[(unsigned char)data[i++]]; }while (c==97); \
2880 if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; }
2881 #endif
2882
2883 BASE64DECODE_READ_NEXT_CHAR(c)
2884 if (c==99) { return 2; }
2885 if (c==96)
2886 {
2887 if (p==(int)len) return 2;
2888 if (xe) *xe=eXMLErrorBase64DecodeTruncatedData;
2889 return 1;
2890 }
2891
2892 BASE64DECODE_READ_NEXT_CHAR(d)
2893 if ((d==99)||(d==96)) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2894 if (p==(int)len) { if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall; return 0; }
2895 buf[p++]=(unsigned char)((c<<2)|((d>>4)&0x3));
2896
2897 BASE64DECODE_READ_NEXT_CHAR(c)
2898 if (c==99) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2899 if (p==(int)len)
2900 {
2901 if (c==96) return 2;
2902 if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall;
2903 return 0;
2904 }
2905 if (c==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2906 buf[p++]=(unsigned char)(((d<<4)&0xf0)|((c>>2)&0xf));
2907
2908 BASE64DECODE_READ_NEXT_CHAR(d)
2909 if (d==99 ) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2910 if (p==(int)len)
2911 {
2912 if (d==96) return 2;
2913 if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall;
2914 return 0;
2915 }
2916 if (d==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; }
2917 buf[p++]=(unsigned char)(((c<<6)&0xc0)|d);
2918 }
2919 }
2920 #undef BASE64DECODE_READ_NEXT_CHAR
2921
alloc(int newsize)2922 void XMLParserBase64Tool::alloc(int newsize)
2923 {
2924 if ((!buf)&&(newsize)) { buf=malloc(newsize); buflen=newsize; return; }
2925 if (newsize>buflen) { buf=realloc(buf,newsize); buflen=newsize; }
2926 }
2927
decode(XMLCSTR data,int * outlen,XMLError * xe)2928 unsigned char *XMLParserBase64Tool::decode(XMLCSTR data, int *outlen, XMLError *xe)
2929 {
2930 if (xe) *xe=eXMLErrorNone;
2931 if (!data) { *outlen=0; return (unsigned char*)""; }
2932 unsigned int len=decodeSize(data,xe);
2933 if (outlen) *outlen=len;
2934 if (!len) return NULL;
2935 alloc(len+1);
2936 if(!decode(data,(unsigned char*)buf,len,xe)){ return NULL; }
2937 return (unsigned char*)buf;
2938 }
2939
2940