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



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019