PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/zeromq/foreign/xmlParser/xmlParser.cpp

https://bitbucket.org/michaelfeitosa/engine_sdk
C++ | 2923 lines | 2295 code | 230 blank | 398 comment | 467 complexity | 200e41ffaf7eace819acc919391dc381 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, BSD-3-Clause, Unlicense, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /**
  2. ****************************************************************************
  3. * <P> XML.c - implementation file for basic XML parser written in ANSI C++
  4. * for portability. It works by using recursion and a node tree for breaking
  5. * down the elements of an XML document. </P>
  6. *
  7. * @version V2.39
  8. * @author Frank Vanden Berghen
  9. *
  10. * NOTE:
  11. *
  12. * If you add "#define STRICT_PARSING", on the first line of this file
  13. * the parser will see the following XML-stream:
  14. * <a><b>some text</b><b>other text </a>
  15. * as an error. Otherwise, this tring will be equivalent to:
  16. * <a><b>some text</b><b>other text</b></a>
  17. *
  18. * NOTE:
  19. *
  20. * If you add "#define APPROXIMATE_PARSING" on the first line of this file
  21. * the parser will see the following XML-stream:
  22. * <data name="n1">
  23. * <data name="n2">
  24. * <data name="n3" />
  25. * as equivalent to the following XML-stream:
  26. * <data name="n1" />
  27. * <data name="n2" />
  28. * <data name="n3" />
  29. * This can be useful for badly-formed XML-streams but prevent the use
  30. * of the following XML-stream (problem is: tags at contiguous levels
  31. * have the same names):
  32. * <data name="n1">
  33. * <data name="n2">
  34. * <data name="n3" />
  35. * </data>
  36. * </data>
  37. *
  38. * NOTE:
  39. *
  40. * If you add "#define _XMLPARSER_NO_MESSAGEBOX_" on the first line of this file
  41. * the "openFileHelper" function will always display error messages inside the
  42. * console instead of inside a message-box-window. Message-box-windows are
  43. * available on windows 9x/NT/2000/XP/Vista only.
  44. *
  45. * Copyright (c) 2002, Frank Vanden Berghen
  46. * All rights reserved.
  47. *
  48. * The following license terms apply to projects that are in some way related to
  49. * the "ZeroMQ project", including applications
  50. * using "ZeroMQ project" and tools developed
  51. * for enhancing "ZeroMQ project". All other projects
  52. * (not related to "ZeroMQ project") have to use this
  53. * code under the Aladdin Free Public License (AFPL)
  54. * See the file "AFPL-license.txt" for more informations about the AFPL license.
  55. * (see http://www.artifex.com/downloads/doc/Public.htm for detailed AFPL terms)
  56. *
  57. * Redistribution and use in source and binary forms, with or without
  58. * modification, are permitted provided that the following conditions are met:
  59. * * Redistributions of source code must retain the above copyright
  60. * notice, this list of conditions and the following disclaimer.
  61. * * Redistributions in binary form must reproduce the above copyright
  62. * notice, this list of conditions and the following disclaimer in the
  63. * documentation and/or other materials provided with the distribution.
  64. * * Neither the name of Frank Vanden Berghen nor the
  65. * names of its contributors may be used to endorse or promote products
  66. * derived from this software without specific prior written permission.
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY Frank Vanden Berghen ``AS IS'' AND ANY
  69. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  70. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  71. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  72. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  74. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  75. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  76. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  77. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78. *
  79. ****************************************************************************
  80. */
  81. #if defined _MSC_VER
  82. #pragma warning (push)
  83. #pragma warning (disable:4996)
  84. #endif
  85. #ifndef _CRT_SECURE_NO_DEPRECATE
  86. #define _CRT_SECURE_NO_DEPRECATE
  87. #endif
  88. #include "xmlParser.hpp"
  89. #ifdef _XMLWINDOWS
  90. //#ifdef _DEBUG
  91. //#define _CRTDBG_MAP_ALLOC
  92. //#include <crtdbg.h>
  93. //#endif
  94. #define WIN32_LEAN_AND_MEAN
  95. #include <windows.h> // to have IsTextUnicode, MultiByteToWideChar, WideCharToMultiByte to handle unicode files
  96. // to have "MessageBoxA" to display error messages for openFilHelper
  97. #endif
  98. #include <memory.h>
  99. #include <assert.h>
  100. #include <stdio.h>
  101. #include <string.h>
  102. #include <stdlib.h>
  103. XMLCSTR XMLNode::getVersion() { return _CXML("v2.39"); }
  104. void freeXMLString(XMLSTR t){if(t)free(t);}
  105. static XMLNode::XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8;
  106. static char guessWideCharChars=1, dropWhiteSpace=1, removeCommentsInMiddleOfText=1;
  107. inline int mmin( const int t1, const int t2 ) { return t1 < t2 ? t1 : t2; }
  108. // You can modify the initialization of the variable "XMLClearTags" below
  109. // to change the clearTags that are currently recognized by the library.
  110. // The number on the second columns is the length of the string inside the
  111. // first column. The "<!DOCTYPE" declaration must be the second in the list.
  112. // The "<!--" declaration must be the third in the list.
  113. typedef struct { XMLCSTR lpszOpen; int openTagLen; XMLCSTR lpszClose;} ALLXMLClearTag;
  114. static ALLXMLClearTag XMLClearTags[] =
  115. {
  116. { _CXML("<![CDATA["),9, _CXML("]]>") },
  117. { _CXML("<!DOCTYPE"),9, _CXML(">") },
  118. { _CXML("<!--") ,4, _CXML("-->") },
  119. { _CXML("<PRE>") ,5, _CXML("</PRE>") },
  120. // { _CXML("<Script>") ,8, _CXML("</Script>")},
  121. { NULL ,0, NULL }
  122. };
  123. // You can modify the initialization of the variable "XMLEntities" below
  124. // to change the character entities that are currently recognized by the library.
  125. // The number on the second columns is the length of the string inside the
  126. // first column. Additionally, the syntaxes "&#xA0;" and "&#160;" are recognized.
  127. typedef struct { XMLCSTR s; int l; XMLCHAR c;} XMLCharacterEntity;
  128. static XMLCharacterEntity XMLEntities[] =
  129. {
  130. { _CXML("&amp;" ), 5, _CXML('&' )},
  131. { _CXML("&lt;" ), 4, _CXML('<' )},
  132. { _CXML("&gt;" ), 4, _CXML('>' )},
  133. { _CXML("&quot;"), 6, _CXML('\"')},
  134. { _CXML("&apos;"), 6, _CXML('\'')},
  135. { NULL , 0, '\0' }
  136. };
  137. // When rendering the XMLNode to a string (using the "createXMLString" function),
  138. // you can ask for a beautiful formatting. This formatting is using the
  139. // following indentation character:
  140. #define INDENTCHAR _CXML('\t')
  141. // The following function parses the XML errors into a user friendly string.
  142. // You can edit this to change the output language of the library to something else.
  143. XMLCSTR XMLNode::getError(XMLError xerror)
  144. {
  145. switch (xerror)
  146. {
  147. case eXMLErrorNone: return _CXML("No error");
  148. case eXMLErrorMissingEndTag: return _CXML("Warning: Unmatched end tag");
  149. case eXMLErrorNoXMLTagFound: return _CXML("Warning: No XML tag found");
  150. case eXMLErrorEmpty: return _CXML("Error: No XML data");
  151. case eXMLErrorMissingTagName: return _CXML("Error: Missing start tag name");
  152. case eXMLErrorMissingEndTagName: return _CXML("Error: Missing end tag name");
  153. case eXMLErrorUnmatchedEndTag: return _CXML("Error: Unmatched end tag");
  154. case eXMLErrorUnmatchedEndClearTag: return _CXML("Error: Unmatched clear tag end");
  155. case eXMLErrorUnexpectedToken: return _CXML("Error: Unexpected token found");
  156. case eXMLErrorNoElements: return _CXML("Error: No elements found");
  157. case eXMLErrorFileNotFound: return _CXML("Error: File not found");
  158. case eXMLErrorFirstTagNotFound: return _CXML("Error: First Tag not found");
  159. case eXMLErrorUnknownCharacterEntity:return _CXML("Error: Unknown character entity");
  160. case eXMLErrorCharacterCodeAbove255: return _CXML("Error: Character code above 255 is forbidden in MultiByte char mode.");
  161. case eXMLErrorCharConversionError: return _CXML("Error: unable to convert between WideChar and MultiByte chars");
  162. case eXMLErrorCannotOpenWriteFile: return _CXML("Error: unable to open file for writing");
  163. case eXMLErrorCannotWriteFile: return _CXML("Error: cannot write into file");
  164. case eXMLErrorBase64DataSizeIsNotMultipleOf4: return _CXML("Warning: Base64-string length is not a multiple of 4");
  165. case eXMLErrorBase64DecodeTruncatedData: return _CXML("Warning: Base64-string is truncated");
  166. case eXMLErrorBase64DecodeIllegalCharacter: return _CXML("Error: Base64-string contains an illegal character");
  167. case eXMLErrorBase64DecodeBufferTooSmall: return _CXML("Error: Base64 decode output buffer is too small");
  168. };
  169. return _CXML("Unknown");
  170. }
  171. /////////////////////////////////////////////////////////////////////////
  172. // Here start the abstraction layer to be OS-independent //
  173. /////////////////////////////////////////////////////////////////////////
  174. // Here is an abstraction layer to access some common string manipulation functions.
  175. // The abstraction layer is currently working for gcc, Microsoft Visual Studio 6.0,
  176. // Microsoft Visual Studio .NET, CC (sun compiler) and Borland C++.
  177. // If you plan to "port" the library to a new system/compiler, all you have to do is
  178. // to edit the following lines.
  179. #ifdef XML_NO_WIDE_CHAR
  180. char myIsTextWideChar(const void *b, int len) { return FALSE; }
  181. #else
  182. #if defined (UNDER_CE) || !defined(_XMLWINDOWS)
  183. char myIsTextWideChar(const void *b, int len) // inspired by the Wine API: RtlIsTextUnicode
  184. {
  185. #ifdef sun
  186. // for SPARC processors: wchar_t* buffers must always be alligned, otherwise it's a char* buffer.
  187. if ((((unsigned long)b)%sizeof(wchar_t))!=0) return FALSE;
  188. #endif
  189. const wchar_t *s=(const wchar_t*)b;
  190. // buffer too small:
  191. if (len<(int)sizeof(wchar_t)) return FALSE;
  192. // odd length test
  193. if (len&1) return FALSE;
  194. /* only checks the first 256 characters */
  195. len=mmin(256,len/sizeof(wchar_t));
  196. // Check for the special byte order:
  197. if (*((unsigned short*)s) == 0xFFFE) return TRUE; // IS_TEXT_UNICODE_REVERSE_SIGNATURE;
  198. if (*((unsigned short*)s) == 0xFEFF) return TRUE; // IS_TEXT_UNICODE_SIGNATURE
  199. // checks for ASCII characters in the UNICODE stream
  200. int i,stats=0;
  201. for (i=0; i<len; i++) if (s[i]<=(unsigned short)255) stats++;
  202. if (stats>len/2) return TRUE;
  203. // Check for UNICODE NULL chars
  204. for (i=0; i<len; i++) if (!s[i]) return TRUE;
  205. return FALSE;
  206. }
  207. #else
  208. char myIsTextWideChar(const void *b,int l) { return (char)IsTextUnicode((CONST LPVOID)b,l,NULL); }
  209. #endif
  210. #endif
  211. #ifdef _XMLWINDOWS
  212. // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
  213. #ifdef _XMLWIDECHAR
  214. wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
  215. {
  216. int i;
  217. if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,NULL,0);
  218. else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,NULL,0);
  219. if (i<0) return NULL;
  220. wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(XMLCHAR));
  221. if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,d,i);
  222. else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,d,i);
  223. d[i]=0;
  224. return d;
  225. }
  226. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return _wfopen(filename,mode); }
  227. static inline int xstrlen(XMLCSTR c) { return (int)wcslen(c); }
  228. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _wcsnicmp(c1,c2,l);}
  229. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
  230. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _wcsicmp(c1,c2); }
  231. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
  232. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
  233. #else
  234. char *myWideCharToMultiByte(const wchar_t *s)
  235. {
  236. UINT codePage=CP_ACP; if (characterEncoding==XMLNode::char_encoding_UTF8) codePage=CP_UTF8;
  237. int i=(int)WideCharToMultiByte(codePage, // code page
  238. 0, // performance and mapping flags
  239. s, // wide-character string
  240. -1, // number of chars in string
  241. NULL, // buffer for new string
  242. 0, // size of buffer
  243. NULL, // default for unmappable chars
  244. NULL // set when default char used
  245. );
  246. if (i<0) return NULL;
  247. char *d=(char*)malloc(i+1);
  248. WideCharToMultiByte(codePage, // code page
  249. 0, // performance and mapping flags
  250. s, // wide-character string
  251. -1, // number of chars in string
  252. d, // buffer for new string
  253. i, // size of buffer
  254. NULL, // default for unmappable chars
  255. NULL // set when default char used
  256. );
  257. d[i]=0;
  258. return d;
  259. }
  260. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
  261. static inline int xstrlen(XMLCSTR c) { return (int)strlen(c); }
  262. #ifdef __BORLANDC__
  263. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strnicmp(c1,c2,l);}
  264. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return stricmp(c1,c2); }
  265. #else
  266. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _strnicmp(c1,c2,l);}
  267. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _stricmp(c1,c2); }
  268. #endif
  269. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
  270. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
  271. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
  272. #endif
  273. #else
  274. // for gcc and CC
  275. #ifdef XML_NO_WIDE_CHAR
  276. char *myWideCharToMultiByte(const wchar_t *s) { return NULL; }
  277. #else
  278. char *myWideCharToMultiByte(const wchar_t *s)
  279. {
  280. const wchar_t *ss=s;
  281. int i=(int)wcsrtombs(NULL,&ss,0,NULL);
  282. if (i<0) return NULL;
  283. char *d=(char *)malloc(i+1);
  284. wcsrtombs(d,&s,i,NULL);
  285. d[i]=0;
  286. return d;
  287. }
  288. #endif
  289. #ifdef _XMLWIDECHAR
  290. wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
  291. {
  292. const char *ss=s;
  293. int i=(int)mbsrtowcs(NULL,&ss,0,NULL);
  294. if (i<0) return NULL;
  295. wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(wchar_t));
  296. mbsrtowcs(d,&s,i,NULL);
  297. d[i]=0;
  298. return d;
  299. }
  300. int xstrlen(XMLCSTR c) { return wcslen(c); }
  301. #ifdef sun
  302. // for CC
  303. #include <widec.h>
  304. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncasecmp(c1,c2,l);}
  305. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncmp(c1,c2,l);}
  306. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wscasecmp(c1,c2); }
  307. #else
  308. // for gcc
  309. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncasecmp(c1,c2,l);}
  310. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
  311. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wcscasecmp(c1,c2); }
  312. #endif
  313. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
  314. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
  315. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode)
  316. {
  317. char *filenameAscii=myWideCharToMultiByte(filename);
  318. FILE *f;
  319. if (mode[0]==_CXML('r')) f=fopen(filenameAscii,"rb");
  320. else f=fopen(filenameAscii,"wb");
  321. free(filenameAscii);
  322. return f;
  323. }
  324. #else
  325. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
  326. static inline int xstrlen(XMLCSTR c) { return strlen(c); }
  327. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncasecmp(c1,c2,l);}
  328. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
  329. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return strcasecmp(c1,c2); }
  330. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
  331. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
  332. #endif
  333. static inline int _strnicmp(const char *c1,const char *c2, int l) { return strncasecmp(c1,c2,l);}
  334. #endif
  335. ///////////////////////////////////////////////////////////////////////////////
  336. // the "xmltoc,xmltob,xmltoi,xmltol,xmltof,xmltoa" functions //
  337. ///////////////////////////////////////////////////////////////////////////////
  338. // These 6 functions are not used inside the XMLparser.
  339. // There are only here as "convenience" functions for the user.
  340. // If you don't need them, you can delete them without any trouble.
  341. #ifdef _XMLWIDECHAR
  342. #ifdef _XMLWINDOWS
  343. // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
  344. char xmltob(XMLCSTR t,int v){ if (t&&(*t)) return (char)_wtoi(t); return v; }
  345. int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return _wtoi(t); return v; }
  346. long xmltol(XMLCSTR t,long v){ if (t&&(*t)) return _wtol(t); return v; }
  347. double xmltof(XMLCSTR t,double v){ if (t&&(*t)) wscanf(t, "%f", &v); /*v=_wtof(t);*/ return v; }
  348. #else
  349. #ifdef sun
  350. // for CC
  351. #include <widec.h>
  352. char xmltob(XMLCSTR t,int v){ if (t) return (char)wstol(t,NULL,10); return v; }
  353. int xmltoi(XMLCSTR t,int v){ if (t) return (int)wstol(t,NULL,10); return v; }
  354. long xmltol(XMLCSTR t,long v){ if (t) return wstol(t,NULL,10); return v; }
  355. #else
  356. // for gcc
  357. char xmltob(XMLCSTR t,int v){ if (t) return (char)wcstol(t,NULL,10); return v; }
  358. int xmltoi(XMLCSTR t,int v){ if (t) return (int)wcstol(t,NULL,10); return v; }
  359. long xmltol(XMLCSTR t,long v){ if (t) return wcstol(t,NULL,10); return v; }
  360. #endif
  361. double xmltof(XMLCSTR t,double v){ if (t&&(*t)) wscanf(t, "%f", &v); /*v=_wtof(t);*/ return v; }
  362. #endif
  363. #else
  364. char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)atoi(t); return v; }
  365. int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return atoi(t); return v; }
  366. long xmltol(XMLCSTR t,long v){ if (t&&(*t)) return atol(t); return v; }
  367. double xmltof(XMLCSTR t,double v){ if (t&&(*t)) return atof(t); return v; }
  368. #endif
  369. XMLCSTR xmltoa(XMLCSTR t,XMLCSTR v){ if (t) return t; return v; }
  370. XMLCHAR xmltoc(XMLCSTR t,XMLCHAR v){ if (t&&(*t)) return *t; return v; }
  371. /////////////////////////////////////////////////////////////////////////
  372. // the "openFileHelper" function //
  373. /////////////////////////////////////////////////////////////////////////
  374. // Since each application has its own way to report and deal with errors, you should modify & rewrite
  375. // the following "openFileHelper" function to get an "error reporting mechanism" tailored to your needs.
  376. XMLNode XMLNode::openFileHelper(XMLCSTR filename, XMLCSTR tag)
  377. {
  378. // guess the value of the global parameter "characterEncoding"
  379. // (the guess is based on the first 200 bytes of the file).
  380. FILE *f=xfopen(filename,_CXML("rb"));
  381. if (f)
  382. {
  383. char bb[205];
  384. int l=(int)fread(bb,1,200,f);
  385. setGlobalOptions(guessCharEncoding(bb,l),guessWideCharChars,dropWhiteSpace,removeCommentsInMiddleOfText);
  386. fclose(f);
  387. }
  388. // parse the file
  389. XMLResults pResults;
  390. XMLNode xnode=XMLNode::parseFile(filename,tag,&pResults);
  391. // display error message (if any)
  392. if (pResults.error != eXMLErrorNone)
  393. {
  394. // create message
  395. char message[2000],*s1=(char*)"",*s3=(char*)""; XMLCSTR s2=_CXML("");
  396. if (pResults.error==eXMLErrorFirstTagNotFound) { s1=(char*)"First Tag should be '"; s2=tag; s3=(char*)"'.\n"; }
  397. sprintf(message,
  398. #ifdef _XMLWIDECHAR
  399. "XML Parsing error inside file '%S'.\n%S\nAt line %i, column %i.\n%s%S%s"
  400. #else
  401. "XML Parsing error inside file '%s'.\n%s\nAt line %i, column %i.\n%s%s%s"
  402. #endif
  403. ,filename,XMLNode::getError(pResults.error),pResults.nLine,pResults.nColumn,s1,s2,s3);
  404. // display message
  405. #if defined(_XMLWINDOWS) && !defined(UNDER_CE) && !defined(_XMLPARSER_NO_MESSAGEBOX_)
  406. MessageBoxA(NULL,message,"XML Parsing error",MB_OK|MB_ICONERROR|MB_TOPMOST);
  407. #else
  408. printf("%s",message);
  409. #endif
  410. exit(255);
  411. }
  412. return xnode;
  413. }
  414. /////////////////////////////////////////////////////////////////////////
  415. // Here start the core implementation of the XMLParser library //
  416. /////////////////////////////////////////////////////////////////////////
  417. // You should normally not change anything below this point.
  418. #ifndef _XMLWIDECHAR
  419. // If "characterEncoding=ascii" then we assume that all characters have the same length of 1 byte.
  420. // If "characterEncoding=UTF8" then the characters have different lengths (from 1 byte to 4 bytes).
  421. // If "characterEncoding=ShiftJIS" then the characters have different lengths (from 1 byte to 2 bytes).
  422. // This table is used as lookup-table to know the length of a character (in byte) based on the
  423. // content of the first byte of the character.
  424. // (note: if you modify this, you must always have XML_utf8ByteTable[0]=0 ).
  425. static const char XML_utf8ByteTable[256] =
  426. {
  427. // 0 1 2 3 4 5 6 7 8 9 a b c d e f
  428. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
  429. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
  430. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
  431. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
  432. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
  433. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
  434. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
  435. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70 End of ASCII range
  436. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80 0x80 to 0xc1 invalid
  437. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90
  438. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0
  439. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0
  440. 1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0 0xc2 to 0xdf 2 byte
  441. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
  442. 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,// 0xe0 0xe0 to 0xef 3 byte
  443. 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
  444. };
  445. static const char XML_legacyByteTable[256] =
  446. {
  447. 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,
  448. 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,
  449. 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,
  450. 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,
  451. 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,
  452. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  453. };
  454. static const char XML_sjisByteTable[256] =
  455. {
  456. // 0 1 2 3 4 5 6 7 8 9 a b c d e f
  457. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
  458. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
  459. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
  460. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
  461. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
  462. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
  463. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
  464. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
  465. 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0x9F 2 bytes
  466. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90
  467. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0
  468. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0
  469. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xc0
  470. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xd0
  471. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0 0xe0 to 0xef 2 bytes
  472. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 // 0xf0
  473. };
  474. static const char XML_gb2312ByteTable[256] =
  475. {
  476. // 0 1 2 3 4 5 6 7 8 9 a b c d e f
  477. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
  478. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
  479. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
  480. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
  481. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
  482. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
  483. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
  484. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
  485. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80
  486. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90
  487. 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0 0xa1 to 0xf7 2 bytes
  488. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0
  489. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0
  490. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
  491. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0
  492. 2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1 // 0xf0
  493. };
  494. static const char XML_gbk_big5_ByteTable[256] =
  495. {
  496. // 0 1 2 3 4 5 6 7 8 9 a b c d e f
  497. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00
  498. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10
  499. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20
  500. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30
  501. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40
  502. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50
  503. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60
  504. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70
  505. 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0xfe 2 bytes
  506. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90
  507. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0
  508. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0
  509. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0
  510. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0
  511. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0
  512. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1 // 0xf0
  513. };
  514. static const char *XML_ByteTable=(const char *)XML_utf8ByteTable; // the default is "characterEncoding=XMLNode::encoding_UTF8"
  515. #endif
  516. XMLNode XMLNode::emptyXMLNode;
  517. XMLClear XMLNode::emptyXMLClear={ NULL, NULL, NULL};
  518. XMLAttribute XMLNode::emptyXMLAttribute={ NULL, NULL};
  519. // Enumeration used to decipher what type a token is
  520. typedef enum XMLTokenTypeTag
  521. {
  522. eTokenText = 0,
  523. eTokenQuotedText,
  524. eTokenTagStart, /* "<" */
  525. eTokenTagEnd, /* "</" */
  526. eTokenCloseTag, /* ">" */
  527. eTokenEquals, /* "=" */
  528. eTokenDeclaration, /* "<?" */
  529. eTokenShortHandClose, /* "/>" */
  530. eTokenClear,
  531. eTokenError
  532. } XMLTokenType;
  533. // Main structure used for parsing XML
  534. typedef struct XML
  535. {
  536. XMLCSTR lpXML;
  537. XMLCSTR lpszText;
  538. int nIndex,nIndexMissigEndTag;
  539. enum XMLError error;
  540. XMLCSTR lpEndTag;
  541. int cbEndTag;
  542. XMLCSTR lpNewElement;
  543. int cbNewElement;
  544. int nFirst;
  545. } XML;
  546. typedef struct
  547. {
  548. ALLXMLClearTag *pClr;
  549. XMLCSTR pStr;
  550. } NextToken;
  551. // Enumeration used when parsing attributes
  552. typedef enum Attrib
  553. {
  554. eAttribName = 0,
  555. eAttribEquals,
  556. eAttribValue
  557. } Attrib;
  558. // Enumeration used when parsing elements to dictate whether we are currently
  559. // inside a tag
  560. typedef enum Status
  561. {
  562. eInsideTag = 0,
  563. eOutsideTag
  564. } Status;
  565. XMLError XMLNode::writeToFile(XMLCSTR filename, const char *encoding, char nFormat) const
  566. {
  567. if (!d) return eXMLErrorNone;
  568. FILE *f=xfopen(filename,_CXML("wb"));
  569. if (!f) return eXMLErrorCannotOpenWriteFile;
  570. #ifdef _XMLWIDECHAR
  571. unsigned char h[2]={ 0xFF, 0xFE };
  572. if (!fwrite(h,2,1,f))
  573. {
  574. fclose(f);
  575. return eXMLErrorCannotWriteFile;
  576. }
  577. if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration())))
  578. {
  579. if (!fwrite(L"<?xml version=\"1.0\" encoding=\"utf-16\"?>\n",sizeof(wchar_t)*40,1,f))
  580. {
  581. fclose(f);
  582. return eXMLErrorCannotWriteFile;
  583. }
  584. }
  585. #else
  586. if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration())))
  587. {
  588. if (characterEncoding==char_encoding_UTF8)
  589. {
  590. // header so that windows recognize the file as UTF-8:
  591. unsigned char h[3]={0xEF,0xBB,0xBF};
  592. if (!fwrite(h,3,1,f))
  593. {
  594. fclose(f);
  595. return eXMLErrorCannotWriteFile;
  596. }
  597. encoding="utf-8";
  598. } else if (characterEncoding==char_encoding_ShiftJIS) encoding="SHIFT-JIS";
  599. if (!encoding) encoding="ISO-8859-1";
  600. if (fprintf(f,"<?xml version=\"1.0\" encoding=\"%s\"?>\n",encoding)<0)
  601. {
  602. fclose(f);
  603. return eXMLErrorCannotWriteFile;
  604. }
  605. } else
  606. {
  607. if (characterEncoding==char_encoding_UTF8)
  608. {
  609. unsigned char h[3]={0xEF,0xBB,0xBF};
  610. if (!fwrite(h,3,1,f))
  611. {
  612. fclose(f);
  613. return eXMLErrorCannotWriteFile;
  614. }
  615. }
  616. }
  617. #endif
  618. int i;
  619. XMLSTR t=createXMLString(nFormat,&i);
  620. if (!fwrite(t,sizeof(XMLCHAR)*i,1,f))
  621. {
  622. fclose(f);
  623. return eXMLErrorCannotWriteFile;
  624. }
  625. if (fclose(f)!=0) return eXMLErrorCannotWriteFile;
  626. free(t);
  627. return eXMLErrorNone;
  628. }
  629. // Duplicate a given string.
  630. XMLSTR stringDup(XMLCSTR lpszData, int cbData)
  631. {
  632. if (lpszData==NULL) return NULL;
  633. XMLSTR lpszNew;
  634. if (cbData==-1) cbData=(int)xstrlen(lpszData);
  635. lpszNew = (XMLSTR)malloc((cbData+1) * sizeof(XMLCHAR));
  636. if (lpszNew)
  637. {
  638. memcpy(lpszNew, lpszData, (cbData) * sizeof(XMLCHAR));
  639. lpszNew[cbData] = (XMLCHAR)NULL;
  640. }
  641. return lpszNew;
  642. }
  643. XMLSTR ToXMLStringTool::toXMLUnSafe(XMLSTR dest,XMLCSTR source)
  644. {
  645. XMLSTR dd=dest;
  646. XMLCHAR ch;
  647. XMLCharacterEntity *entity;
  648. while ((ch=*source))
  649. {
  650. entity=XMLEntities;
  651. do
  652. {
  653. if (ch==entity->c) {xstrcpy(dest,entity->s); dest+=entity->l; source++; goto out_of_loop1; }
  654. entity++;
  655. } while(entity->s);
  656. #ifdef _XMLWIDECHAR
  657. *(dest++)=*(source++);
  658. #else
  659. switch(XML_ByteTable[(unsigned char)ch])
  660. {
  661. case 4: *(dest++)=*(source++);
  662. case 3: *(dest++)=*(source++);
  663. case 2: *(dest++)=*(source++);
  664. case 1: *(dest++)=*(source++);
  665. }
  666. #endif
  667. out_of_loop1:
  668. ;
  669. }
  670. *dest=0;
  671. return dd;
  672. }
  673. // private (used while rendering):
  674. int ToXMLStringTool::lengthXMLString(XMLCSTR source)
  675. {
  676. int r=0;
  677. XMLCharacterEntity *entity;
  678. XMLCHAR ch;
  679. while ((ch=*source))
  680. {
  681. entity=XMLEntities;
  682. do
  683. {
  684. if (ch==entity->c) { r+=entity->l; source++; goto out_of_loop1; }
  685. entity++;
  686. } while(entity->s);
  687. #ifdef _XMLWIDECHAR
  688. r++; source++;
  689. #else
  690. ch=XML_ByteTable[(unsigned char)ch]; r+=ch; source+=ch;
  691. #endif
  692. out_of_loop1:
  693. ;
  694. }
  695. return r;
  696. }
  697. ToXMLStringTool::~ToXMLStringTool(){ freeBuffer(); }
  698. void ToXMLStringTool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; }
  699. XMLSTR ToXMLStringTool::toXML(XMLCSTR source)
  700. {
  701. int l=lengthXMLString(source)+1;
  702. if (l>buflen) { buflen=l; buf=(XMLSTR)realloc(buf,l*sizeof(XMLCHAR)); }
  703. return toXMLUnSafe(buf,source);
  704. }
  705. // private:
  706. XMLSTR fromXMLString(XMLCSTR s, int lo, XML *pXML)
  707. {
  708. // This function is the opposite of the function "toXMLString". It decodes the escape
  709. // sequences &amp;, &quot;, &apos;, &lt;, &gt; and replace them by the characters
  710. // &,",',<,>. This function is used internally by the XML Parser. All the calls to
  711. // the XML library will always gives you back "decoded" strings.
  712. //
  713. // in: string (s) and length (lo) of string
  714. // out: new allocated string converted from xml
  715. if (!s) return NULL;
  716. int ll=0,j;
  717. XMLSTR d;
  718. XMLCSTR ss=s;
  719. XMLCharacterEntity *entity;
  720. while ((lo>0)&&(*s))
  721. {
  722. if (*s==_CXML('&'))
  723. {
  724. if ((lo>2)&&(s[1]==_CXML('#')))
  725. {
  726. s+=2; lo-=2;
  727. if ((*s==_CXML('X'))||(*s==_CXML('x'))) { s++; lo--; }
  728. while ((*s)&&(*s!=_CXML(';'))&&((lo--)>0)) s++;
  729. if (*s!=_CXML(';'))
  730. {
  731. pXML->error=eXMLErrorUnknownCharacterEntity;
  732. return NULL;
  733. }
  734. s++; lo--;
  735. } else
  736. {
  737. entity=XMLEntities;
  738. do
  739. {
  740. if ((lo>=entity->l)&&(xstrnicmp(s,entity->s,entity->l)==0)) { s+=entity->l; lo-=entity->l; break; }
  741. entity++;
  742. } while(entity->s);
  743. if (!entity->s)
  744. {
  745. pXML->error=eXMLErrorUnknownCharacterEntity;
  746. return NULL;
  747. }
  748. }
  749. } else
  750. {
  751. #ifdef _XMLWIDECHAR
  752. s++; lo--;
  753. #else
  754. j=XML_ByteTable[(unsigned char)*s]; s+=j; lo-=j; ll+=j-1;
  755. #endif
  756. }
  757. ll++;
  758. }
  759. d=(XMLSTR)malloc((ll+1)*sizeof(XMLCHAR));
  760. s=d;
  761. while (ll-->0)
  762. {
  763. if (*ss==_CXML('&'))
  764. {
  765. if (ss[1]==_CXML('#'))
  766. {
  767. ss+=2; j=0;
  768. if ((*ss==_CXML('X'))||(*ss==_CXML('x')))
  769. {
  770. ss++;
  771. while (*ss!=_CXML(';'))
  772. {
  773. if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j<<4)+*ss-_CXML('0');
  774. else if ((*ss>=_CXML('A'))&&(*ss<=_CXML('F'))) j=(j<<4)+*ss-_CXML('A')+10;
  775. else if ((*ss>=_CXML('a'))&&(*ss<=_CXML('f'))) j=(j<<4)+*ss-_CXML('a')+10;
  776. else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
  777. ss++;
  778. }
  779. } else
  780. {
  781. while (*ss!=_CXML(';'))
  782. {
  783. if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j*10)+*ss-_CXML('0');
  784. else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
  785. ss++;
  786. }
  787. }
  788. #ifndef _XMLWIDECHAR
  789. if (j>255) { free((void*)s); pXML->error=eXMLErrorCharacterCodeAbove255;return NULL;}
  790. #endif
  791. (*d++)=(XMLCHAR)j; ss++;
  792. } else
  793. {
  794. entity=XMLEntities;
  795. do
  796. {
  797. if (xstrnicmp(ss,entity->s,entity->l)==0) { *(d++)=entity->c; ss+=entity->l; break; }
  798. entity++;
  799. } while(entity->s);
  800. }
  801. } else
  802. {
  803. #ifdef _XMLWIDECHAR
  804. *(d++)=*(ss++);
  805. #else
  806. switch(XML_ByteTable[(unsigned char)*ss])
  807. {
  808. case 4: *(d++)=*(ss++); ll--;
  809. case 3: *(d++)=*(ss++); ll--;
  810. case 2: *(d++)=*(ss++); ll--;
  811. case 1: *(d++)=*(ss++);
  812. }
  813. #endif
  814. }
  815. }
  816. *d=0;
  817. return (XMLSTR)s;
  818. }
  819. #define XML_isSPACECHAR(ch) ((ch==_CXML('\n'))||(ch==_CXML(' '))||(ch== _CXML('\t'))||(ch==_CXML('\r')))
  820. // private:
  821. char myTagCompare(XMLCSTR cclose, XMLCSTR copen)
  822. // !!!! WARNING strange convention&:
  823. // return 0 if equals
  824. // return 1 if different
  825. {
  826. if (!cclose) return 1;
  827. int l=(int)xstrlen(cclose);
  828. if (xstrnicmp(cclose, copen, l)!=0) return 1;
  829. const XMLCHAR c=copen[l];
  830. if (XML_isSPACECHAR(c)||
  831. (c==_CXML('/' ))||
  832. (c==_CXML('<' ))||
  833. (c==_CXML('>' ))||
  834. (c==_CXML('=' ))) return 0;
  835. return 1;
  836. }
  837. // Obtain the next character from the string.
  838. static inline XMLCHAR getNextChar(XML *pXML)
  839. {
  840. XMLCHAR ch = pXML->lpXML[pXML->nIndex];
  841. #ifdef _XMLWIDECHAR
  842. if (ch!=0) pXML->nIndex++;
  843. #else
  844. pXML->nIndex+=XML_ByteTable[(unsigned char)ch];
  845. #endif
  846. return ch;
  847. }
  848. // Find the next token in a string.
  849. // pcbToken contains the number of characters that have been read.
  850. static NextToken GetNextToken(XML *pXML, int *pcbToken, enum XMLTokenTypeTag *pType)
  851. {
  852. NextToken result;
  853. XMLCHAR ch;
  854. XMLCHAR chTemp;
  855. int indexStart,nFoundMatch,nIsText=FALSE;
  856. result.pClr=NULL; // prevent warning
  857. // Find next non-white space character
  858. do { indexStart=pXML->nIndex; ch=getNextChar(pXML); } while XML_isSPACECHAR(ch);
  859. if (ch)
  860. {
  861. // Cache the current string pointer
  862. result.pStr = &pXML->lpXML[indexStart];
  863. // First check whether the token is in the clear tag list (meaning it
  864. // does not need formatting).
  865. ALLXMLClearTag *ctag=XMLClearTags;
  866. do
  867. {
  868. if (xstrncmp(ctag->lpszOpen, result.pStr, ctag->openTagLen)==0)
  869. {
  870. result.pClr=ctag;
  871. pXML->nIndex+=ctag->openTagLen-1;
  872. *pType=eTokenClear;
  873. return result;
  874. }
  875. ctag++;
  876. } while(ctag->lpszOpen);
  877. // If we didn't find a clear tag then check for standard tokens
  878. switch(ch)
  879. {
  880. // Check for quotes
  881. case _CXML('\''):
  882. case _CXML('\"'):
  883. // Type of token
  884. *pType = eTokenQuotedText;
  885. chTemp = ch;
  886. // Set the size
  887. nFoundMatch = FALSE;
  888. // Search through the string to find a matching quote
  889. while((ch = getNextChar(pXML)))
  890. {
  891. if (ch==chTemp) { nFoundMatch = TRUE; break; }
  892. if (ch==_CXML('<')) break;
  893. }
  894. // If we failed to find a matching quote
  895. if (nFoundMatch == FALSE)
  896. {
  897. pXML->nIndex=indexStart+1;
  898. nIsText=TRUE;
  899. break;
  900. }
  901. // 4.02.2002
  902. // if (FindNonWhiteSpace(pXML)) pXML->nIndex--;
  903. break;
  904. // Equals (used with attribute values)
  905. case _CXML('='):
  906. *pType = eTokenEquals;
  907. break;
  908. // Close tag
  909. case _CXML('>'):
  910. *pType = eTokenCloseTag;
  911. break;
  912. // Check for tag start and tag end
  913. case _CXML('<'):
  914. // Peek at the next character to see if we have an end tag '</',
  915. // or an xml declaration '<?'
  916. chTemp = pXML->lpXML[pXML->nIndex];
  917. // If we have a tag end...
  918. if (chTemp == _CXML('/'))
  919. {
  920. // Set the type and ensure we point at the next character
  921. getNextChar(pXML);
  922. *pType = eTokenTagEnd;
  923. }
  924. // If we have an XML declaration tag
  925. else if (chTemp == _CXML('?'))
  926. {
  927. // Set the type and ensure we point at the next character
  928. getNextChar(pXML);
  929. *pType = eTokenDeclaration;
  930. }
  931. // Otherwise we must have a start tag
  932. else
  933. {
  934. *pType = eTokenTagStart;
  935. }
  936. break;
  937. // Check to see if we have a short hand type end tag ('/>').
  938. case _CXML('/'):
  939. // Peek at the next character to see if we have a short end tag '/>'
  940. chTemp = pXML->lpXML[pXML->nIndex];
  941. // If we have a short hand end tag...
  942. if (chTemp == _CXML('>'))
  943. {
  944. // Set the type and ensure we point at the next character
  945. getNextChar(pXML);
  946. *pType = eTokenShortHandClose;
  947. break;
  948. }
  949. // If we haven't found a short hand closing tag then drop into the
  950. // text process
  951. // Other characters
  952. default:
  953. nIsText = TRUE;
  954. }
  955. // If this is a TEXT node
  956. if (nIsText)
  957. {
  958. // Indicate we are dealing with text
  959. *pType = eTokenText;
  960. while((ch = getNextChar(pXML)))
  961. {
  962. if XML_isSPACECHAR(ch)
  963. {
  964. indexStart++; break;
  965. } else if (ch==_CXML('/'))
  966. {
  967. // If we find a slash then this maybe text or a short hand end tag
  968. // Peek at the next character to see it we have short hand end tag
  969. ch=pXML->lpXML[pXML->nIndex];
  970. // If we found a short hand end tag then we need to exit the loop
  971. if (ch==_CXML('>')) { pXML->nIndex--; break; }
  972. } else if ((ch==_CXML('<'))||(ch==_CXML('>'))||(ch==_CXML('=')))
  973. {
  974. pXML->nIndex--; break;
  975. }
  976. }
  977. }
  978. *pcbToken = pXML->nIndex-indexStart;
  979. } else
  980. {
  981. // If we failed to obtain a valid character
  982. *pcbToken = 0;
  983. *pType = eTokenError;
  984. result.pStr=NULL;
  985. }
  986. return result;
  987. }
  988. XMLCSTR XMLNode::updateName_WOSD(XMLSTR lpszName)
  989. {
  990. if (!d) { free(lpszName); return NULL; }
  991. if (d->lpszName&&(lpszName!=d->lpszName)) free((void*)d->lpszName);
  992. d->lpszName=lpszName;
  993. return lpszName;
  994. }
  995. // private:
  996. XMLNode::XMLNode(struct XMLNodeDataTag *p){ d=p; (p->ref_count)++; }
  997. XMLNode::XMLNode(XMLNodeData *pParent, XMLSTR lpszName, char isDeclaration)
  998. {
  999. d=(XMLNodeData*)malloc(sizeof(XMLNodeData));
  1000. d->ref_count=1;
  1001. d->lpszName=NULL;
  1002. d->nChild= 0;
  1003. d->nText = 0;
  1004. d->nClear = 0;
  1005. d->nAttribute = 0;
  1006. d->isDeclaration = isDeclaration;
  1007. d->pParent = pParent;
  1008. d->pChild= NULL;
  1009. d->pText= NULL;
  1010. d->pClear= NULL;
  1011. d->pAttribute= NULL;
  1012. d->pOrder= NULL;
  1013. updateName_WOSD(lpszName);
  1014. }
  1015. XMLNode XMLNode::createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration) { return XMLNode(NULL,lpszName,isDeclaration); }
  1016. XMLNode XMLNode::createXMLTopNode(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL,stringDup(lpszName),isDeclaration); }
  1017. #define MEMORYINCREASE 50
  1018. static inline void myFree(void *p) { if (p) free(p); }
  1019. static inline void *myRealloc(void *p, int newsize, int memInc, int sizeofElem)
  1020. {
  1021. if (p==NULL) { if (memInc) return malloc(memInc*sizeofElem); return malloc(sizeofElem); }
  1022. if ((memInc==0)||((newsize%memInc)==0)) p=realloc(p,(newsize+memInc)*sizeofElem);
  1023. // if (!p)
  1024. // {
  1025. // printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220);
  1026. // }
  1027. return p;
  1028. }
  1029. // private:
  1030. XMLElementPosition XMLNode::findPosition(XMLNodeData *d, int index, XMLElementType xxtype)
  1031. {
  1032. if (index<0) return -1;
  1033. int i=0,j=(int)((index<<2)+xxtype),*o=d->pOrder; while (o[i]!=j) i++; return i;
  1034. }
  1035. // private:
  1036. // update "order" information when deleting a content of a XMLNode
  1037. int XMLNode::removeOrderElement(XMLNodeData *d, XMLElementType t, int index)
  1038. {
  1039. int n=d->nChild+d->nText+d->nClear, *o=d->pOrder,i=findPosition(d,index,t);
  1040. memmove(o+i, o+i+1, (n-i)*sizeof(int));
  1041. for (;i<n;i++)
  1042. if ((o[i]&3)==(int)t) o[i]-=4;
  1043. // We should normally do:
  1044. // d->pOrder=(int)realloc(d->pOrder,n*sizeof(int));
  1045. // but we skip reallocation because it's too time consuming.
  1046. // Anyway, at the end, it will be free'd completely at once.
  1047. return i;
  1048. }
  1049. void *XMLNode::addToOrder(int memoryIncrease,int *_pos, int nc, void *p, int size, XMLElementType xtype)
  1050. {
  1051. // in: *_pos is the position inside d->pOrder ("-1" means "EndOf")
  1052. // out: *_pos is the index inside p
  1053. p=myRealloc(p,(nc+1),memoryIncrease,size);
  1054. int n=d->nChild+d->nText+d->nClear;
  1055. d->pOrder=(int*)myRealloc(d->pOrder,n+1,memoryIncrease*3,sizeof(int));
  1056. int pos=*_pos,*o=d->pOrder;
  1057. if ((pos<0)||(pos>=n)) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
  1058. int i=pos;
  1059. memmove(o+i+1, o+i, (n-i)*sizeof(int));
  1060. while ((pos<n)&&((o[pos]&3)!=(int)xtype)) pos++;
  1061. if (pos==n) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
  1062. o[i]=o[pos];
  1063. for (i=pos+1;i<=n;i++) if ((o[i]&3)==(int)xtype) o[i]+=4;
  1064. *_pos=pos=o[pos]>>2;
  1065. memmove(((char*)p)+(pos+1)*size,((char*)p)+pos*size,(nc-pos)*size);
  1066. return p;
  1067. }
  1068. // Add a child node to the given element.
  1069. XMLNode XMLNode::addChild_priv(int memoryIncrease, XMLSTR lpszName, char isDeclaration, int pos)
  1070. {
  1071. if (!lpszName) return emptyXMLNode;
  1072. d->pChild=(XMLNode*)addToOrder(memoryIncrease,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
  1073. d->pChild[pos].d=NULL;
  1074. d->pChild[pos]=XMLNode(d,lpszName,isDeclaration);
  1075. d->nChild++;
  1076. return d->pChild[pos];
  1077. }
  1078. // Add an attribute to an element.
  1079. XMLAttribute *XMLNode::addAttribute_priv(int memoryIncrease,XMLSTR lpszName, XMLSTR lpszValuev)
  1080. {
  1081. if (!lpszName) return &emptyXMLAttribute;
  1082. if (!d) { myFree(lpszName); myFree(lpszValuev); return &emptyXMLAttribute; }
  1083. int nc=d->nAttribute;
  1084. d->pAttribute=(XMLAttribute*)myRealloc(d->pAttribute,(nc+1),memoryIncrease,sizeof(XMLAttribute));
  1085. XMLAttribute *pAttr=d->pAttribute+nc;
  1086. pAttr->lpszName = lpszName;
  1087. pAttr->lpszValue = lpszValuev;
  1088. d->nAttribute++;
  1089. return pAttr;
  1090. }
  1091. // Add text to the element.
  1092. XMLCSTR XMLNode::addText_priv(int memoryIncrease, XMLSTR lpszValue, int pos)
  1093. {
  1094. if (!lpszValue) return NULL;
  1095. if (!d) { myFree(lpszValue); return NULL; }
  1096. d->pText=(XMLCSTR*)addToOrder(memoryIncrease,&pos,d->nText,d->pText,sizeof(XMLSTR),eNodeText);
  1097. d->pText[pos]=lpszValue;
  1098. d->nText++;
  1099. return lpszValue;
  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. // private:
  1117. // Parse a clear (unformatted) type node.
  1118. char XMLNode::parseClearTag(void *px, void *_pClear)
  1119. {
  1120. XML *pXML=(XML *)px;
  1121. ALLXMLClearTag pClear=*((ALLXMLClearTag*)_pClear);
  1122. int cbTemp=0;
  1123. XMLCSTR lpszTemp=NULL;
  1124. XMLCSTR lpXML=&pXML->lpXML[pXML->nIndex];
  1125. static XMLCSTR docTypeEnd=_CXML("]>");
  1126. // Find the closing tag
  1127. // Seems the <!DOCTYPE need a better treatment so lets handle it
  1128. if (pClear.lpszOpen==XMLClearTags[1].lpszOpen)
  1129. {
  1130. XMLCSTR pCh=lpXML;
  1131. while (*pCh)
  1132. {
  1133. if (*pCh==_CXML('<')) { pClear.lpszClose=docTypeEnd; lpszTemp=xstrstr(lpXML,docTypeEnd); break; }
  1134. else if (*pCh==_CXML('>')) { lpszTemp=pCh; break; }
  1135. #ifdef _XMLWIDECHAR
  1136. pCh++;
  1137. #else
  1138. pCh+=XML_ByteTable[(unsigned char)(*pCh)];
  1139. #endif
  1140. }
  1141. } else lpszTemp=xstrstr(lpXML, pClear.lpszClose);
  1142. if (lpszTemp)
  1143. {
  1144. // Cache the size and increment the index
  1145. cbTemp = (int)(lpszTemp - lpXML);
  1146. pXML->nIndex += cbTemp+(int)xstrlen(pClear.lpszClose);
  1147. // Add the clear node to the current element
  1148. addClear_priv(MEMORYINCREASE,stringDup(lpXML,cbTemp), pClear.lpszOpen, pClear.lpszClose,-1);
  1149. return 0;
  1150. }
  1151. // If we failed to find the end tag
  1152. pXML->error = eXMLErrorUnmatchedEndClearTag;
  1153. return 1;
  1154. }
  1155. void XMLNode::exactMemory(XMLNodeData *d)
  1156. {
  1157. if (d->pOrder) d->pOrder=(int*)realloc(d->pOrder,(d->nChild+d->nText+d->nClear)*sizeof(int));
  1158. if (d->pChild) d->pChild=(XMLNode*)realloc(d->pChild,d->nChild*sizeof(XMLNode));
  1159. if (d->pAttribute) d->pAttribute=(XMLAttribute*)realloc(d->pAttribute,d->nAttribute*sizeof(XMLAttribute));
  1160. if (d->pText) d->pText=(XMLCSTR*)realloc(d->pText,d->nText*sizeof(XMLSTR));
  1161. if (d->pClear) d->pClear=(XMLClear *)realloc(d->pClear,d->nClear*sizeof(XMLClear));
  1162. }
  1163. char XMLNode::maybeAddTxT(void *pa, XMLCSTR tokenPStr)
  1164. {
  1165. XML *pXML=(XML *)pa;
  1166. XMLCSTR lpszText=pXML->lpszText;
  1167. if (!lpszText) return 0;
  1168. if (dropWhiteSpace) while (XML_isSPACECHAR(*lpszText)&&(lpszText!=tokenPStr)) lpszText++;
  1169. int cbText = (int)(tokenPStr - lpszText);
  1170. if (!cbText) { pXML->lpszText=NULL; return 0; }
  1171. if (dropWhiteSpace) { cbText--; while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; }
  1172. if (!cbText) { pXML->lpszText=NULL; return 0; }
  1173. XMLSTR lpt=fromXMLString(lpszText,cbText,pXML);
  1174. if (!lpt) return 1;
  1175. pXML->lpszText=NULL;
  1176. if (removeCommentsInMiddleOfText && d->nText && d->nClear)
  1177. {
  1178. // if the previous insertion was a comment (<!-- -->) AND
  1179. // if the previous previous insertion was a text then, delete the comment and append the text
  1180. int n=d->nChild+d->nText+d->nClear-1,*o=d->pOrder;
  1181. if (((o[n]&3)==eNodeClear)&&((o[n-1]&3)==eNodeText))
  1182. {
  1183. int i=o[n]>>2;
  1184. if (d->pClear[i].lpszOpenTag==XMLClearTags[2].lpszOpen)
  1185. {
  1186. deleteClear(i);
  1187. i=o[n-1]>>2;
  1188. n=xstrlen(d->pText[i]);
  1189. int n2=xstrlen(lpt)+1;
  1190. d->pText[i]=(XMLSTR)realloc((void*)d->pText[i],(n+n2)*sizeof(XMLCHAR));
  1191. if (!d->pText[i]) return 1;
  1192. memcpy((void*)(d->pText[i]+n),lpt,n2*sizeof(XMLCHAR));
  1193. free(lpt);
  1194. return 0;
  1195. }
  1196. }
  1197. }
  1198. addText_priv(MEMORYINCREASE,lpt,-1);
  1199. return 0;
  1200. }
  1201. // private:
  1202. // Recursively parse an XM…

Large files files are truncated, but you can click here to view the full file