PageRenderTime 71ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/testStruct/src/xmlParser.cpp

https://github.com/vrx/ethica
C++ | 2974 lines | 2374 code | 228 blank | 372 comment | 485 complexity | 46d842e8d824aecc81c33e0e7de60c8b MD5 | raw file

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.44
  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 - All rights reserved.
  46. * Commercialized by <a href="http://www.Business-Insight.com">Business-Insight</a>
  47. * See the file "AFPL-license.txt about the licensing terms
  48. *
  49. ****************************************************************************
  50. */
  51. #ifndef _CRT_SECURE_NO_DEPRECATE
  52. #define _CRT_SECURE_NO_DEPRECATE
  53. #endif
  54. #include "xmlParser.h"
  55. #ifdef _XMLWINDOWS
  56. //#ifdef _DEBUG
  57. //#define _CRTDBG_MAP_ALLOC
  58. //#include <crtdbg.h>
  59. //#endif
  60. #define WIN32_LEAN_AND_MEAN
  61. #include <Windows.h> // to have IsTextUnicode, MultiByteToWideChar, WideCharToMultiByte to handle unicode files
  62. // to have "MessageBoxA" to display error messages for openFilHelper
  63. #endif
  64. #include <memory.h>
  65. #include <assert.h>
  66. #include <stdio.h>
  67. #include <string.h>
  68. #include <stdlib.h>
  69. XMLCSTR XMLNode::getVersion() { return _CXML("v2.44"); }
  70. void freeXMLString(XMLSTR t){if(t)free(t);}
  71. static XMLNode::XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8;
  72. static char guessWideCharChars=1, dropWhiteSpace=1, removeCommentsInMiddleOfText=1;
  73. inline int mmin( const int t1, const int t2 ) { return t1 < t2 ? t1 : t2; }
  74. // You can modify the initialization of the variable "XMLClearTags" below
  75. // to change the clearTags that are currently recognized by the library.
  76. // The number on the second columns is the length of the string inside the
  77. // first column.
  78. // The "<!DOCTYPE" declaration must be the second in the list.
  79. // The "<!--" declaration must be the third in the list.
  80. // All ClearTag Strings must start with the '<' character.
  81. typedef struct { XMLCSTR lpszOpen; int openTagLen; XMLCSTR lpszClose;} ALLXMLClearTag;
  82. static ALLXMLClearTag XMLClearTags[] =
  83. {
  84. { _CXML("<![CDATA["),9, _CXML("]]>") },
  85. { _CXML("<!DOCTYPE"),9, _CXML(">") },
  86. { _CXML("<!--") ,4, _CXML("-->") },
  87. { _CXML("<PRE>") ,5, _CXML("</PRE>") },
  88. // { _CXML("<Script>") ,8, _CXML("</Script>")},
  89. { NULL ,0, NULL }
  90. };
  91. // You can modify the initialization of the variable "XMLEntities" below
  92. // to change the character entities that are currently recognized by the library.
  93. // The number on the second columns is the length of the string inside the
  94. // first column. Additionally, the syntaxes "&#xA0;" and "&#160;" are recognized.
  95. typedef struct { XMLCSTR s; int l; XMLCHAR c;} XMLCharacterEntity;
  96. static XMLCharacterEntity XMLEntities[] =
  97. {
  98. { _CXML("&amp;" ), 5, _CXML('&' )},
  99. { _CXML("&lt;" ), 4, _CXML('<' )},
  100. { _CXML("&gt;" ), 4, _CXML('>' )},
  101. { _CXML("&quot;"), 6, _CXML('\"')},
  102. { _CXML("&apos;"), 6, _CXML('\'')},
  103. { NULL , 0, '\0' }
  104. };
  105. // When rendering the XMLNode to a string (using the "createXMLString" function),
  106. // you can ask for a beautiful formatting. This formatting is using the
  107. // following indentation character:
  108. #define INDENTCHAR _CXML('\t')
  109. // The following function parses the XML errors into a user friendly string.
  110. // You can edit this to change the output language of the library to something else.
  111. XMLCSTR XMLNode::getError(XMLError xerror)
  112. {
  113. switch (xerror)
  114. {
  115. case eXMLErrorNone: return _CXML("No error");
  116. case eXMLErrorMissingEndTag: return _CXML("Warning: Unmatched end tag");
  117. case eXMLErrorNoXMLTagFound: return _CXML("Warning: No XML tag found");
  118. case eXMLErrorEmpty: return _CXML("Error: No XML data");
  119. case eXMLErrorMissingTagName: return _CXML("Error: Missing start tag name");
  120. case eXMLErrorMissingEndTagName: return _CXML("Error: Missing end tag name");
  121. case eXMLErrorUnmatchedEndTag: return _CXML("Error: Unmatched end tag");
  122. case eXMLErrorUnmatchedEndClearTag: return _CXML("Error: Unmatched clear tag end");
  123. case eXMLErrorUnexpectedToken: return _CXML("Error: Unexpected token found");
  124. case eXMLErrorNoElements: return _CXML("Error: No elements found");
  125. case eXMLErrorFileNotFound: return _CXML("Error: File not found");
  126. case eXMLErrorFirstTagNotFound: return _CXML("Error: First Tag not found");
  127. case eXMLErrorUnknownCharacterEntity:return _CXML("Error: Unknown character entity");
  128. case eXMLErrorCharacterCodeAbove255: return _CXML("Error: Character code above 255 is forbidden in MultiByte char mode.");
  129. case eXMLErrorCharConversionError: return _CXML("Error: unable to convert between WideChar and MultiByte chars");
  130. case eXMLErrorCannotOpenWriteFile: return _CXML("Error: unable to open file for writing");
  131. case eXMLErrorCannotWriteFile: return _CXML("Error: cannot write into file");
  132. case eXMLErrorBase64DataSizeIsNotMultipleOf4: return _CXML("Warning: Base64-string length is not a multiple of 4");
  133. case eXMLErrorBase64DecodeTruncatedData: return _CXML("Warning: Base64-string is truncated");
  134. case eXMLErrorBase64DecodeIllegalCharacter: return _CXML("Error: Base64-string contains an illegal character");
  135. case eXMLErrorBase64DecodeBufferTooSmall: return _CXML("Error: Base64 decode output buffer is too small");
  136. };
  137. return _CXML("Unknown");
  138. }
  139. /////////////////////////////////////////////////////////////////////////
  140. // Here start the abstraction layer to be OS-independent //
  141. /////////////////////////////////////////////////////////////////////////
  142. // Here is an abstraction layer to access some common string manipulation functions.
  143. // The abstraction layer is currently working for gcc, Microsoft Visual Studio 6.0,
  144. // Microsoft Visual Studio .NET, CC (sun compiler) and Borland C++.
  145. // If you plan to "port" the library to a new system/compiler, all you have to do is
  146. // to edit the following lines.
  147. #ifdef XML_NO_WIDE_CHAR
  148. char myIsTextWideChar(const void *b, int len) { return FALSE; }
  149. #else
  150. #if defined (UNDER_CE) || !defined(_XMLWINDOWS)
  151. char myIsTextWideChar(const void *b, int len) // inspired by the Wine API: RtlIsTextUnicode
  152. {
  153. #ifdef sun
  154. // for SPARC processors: wchar_t* buffers must always be alligned, otherwise it's a char* buffer.
  155. if ((((unsigned long)b)%sizeof(wchar_t))!=0) return FALSE;
  156. #endif
  157. const wchar_t *s=(const wchar_t*)b;
  158. // buffer too small:
  159. if (len<(int)sizeof(wchar_t)) return FALSE;
  160. // odd length test
  161. if (len&1) return FALSE;
  162. /* only checks the first 256 characters */
  163. len=mmin(256,len/sizeof(wchar_t));
  164. // Check for the special byte order:
  165. if (*((unsigned short*)s) == 0xFFFE) return TRUE; // IS_TEXT_UNICODE_REVERSE_SIGNATURE;
  166. if (*((unsigned short*)s) == 0xFEFF) return TRUE; // IS_TEXT_UNICODE_SIGNATURE
  167. // checks for ASCII characters in the UNICODE stream
  168. int i,stats=0;
  169. for (i=0; i<len; i++) if (s[i]<=(unsigned short)255) stats++;
  170. if (stats>len/2) return TRUE;
  171. // Check for UNICODE NULL chars
  172. for (i=0; i<len; i++) if (!s[i]) return TRUE;
  173. return FALSE;
  174. }
  175. #else
  176. char myIsTextWideChar(const void *b,int l) { return (char)IsTextUnicode((CONST LPVOID)b,l,NULL); }
  177. #endif
  178. #endif
  179. #ifdef _XMLWINDOWS
  180. // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
  181. #ifdef _XMLWIDECHAR
  182. wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
  183. {
  184. int i;
  185. if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,NULL,0);
  186. else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,NULL,0);
  187. if (i<0) return NULL;
  188. wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(XMLCHAR));
  189. if (ce==XMLNode::char_encoding_UTF8) i=(int)MultiByteToWideChar(CP_UTF8,0 ,s,-1,d,i);
  190. else i=(int)MultiByteToWideChar(CP_ACP ,MB_PRECOMPOSED,s,-1,d,i);
  191. d[i]=0;
  192. return d;
  193. }
  194. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return _wfopen(filename,mode); }
  195. static inline int xstrlen(XMLCSTR c) { return (int)wcslen(c); }
  196. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _wcsnicmp(c1,c2,l);}
  197. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
  198. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _wcsicmp(c1,c2); }
  199. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
  200. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
  201. #else
  202. char *myWideCharToMultiByte(const wchar_t *s)
  203. {
  204. UINT codePage=CP_ACP; if (characterEncoding==XMLNode::char_encoding_UTF8) codePage=CP_UTF8;
  205. int i=(int)WideCharToMultiByte(codePage, // code page
  206. 0, // performance and mapping flags
  207. s, // wide-character string
  208. -1, // number of chars in string
  209. NULL, // buffer for new string
  210. 0, // size of buffer
  211. NULL, // default for unmappable chars
  212. NULL // set when default char used
  213. );
  214. if (i<0) return NULL;
  215. char *d=(char*)malloc(i+1);
  216. WideCharToMultiByte(codePage, // code page
  217. 0, // performance and mapping flags
  218. s, // wide-character string
  219. -1, // number of chars in string
  220. d, // buffer for new string
  221. i, // size of buffer
  222. NULL, // default for unmappable chars
  223. NULL // set when default char used
  224. );
  225. d[i]=0;
  226. return d;
  227. }
  228. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
  229. static inline int xstrlen(XMLCSTR c) { return (int)strlen(c); }
  230. #ifdef __BORLANDC__
  231. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strnicmp(c1,c2,l);}
  232. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return stricmp(c1,c2); }
  233. #else
  234. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _strnicmp(c1,c2,l);}
  235. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _stricmp(c1,c2); }
  236. #endif
  237. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
  238. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
  239. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
  240. #endif
  241. #else
  242. // for gcc and CC
  243. #ifdef XML_NO_WIDE_CHAR
  244. char *myWideCharToMultiByte(const wchar_t *s) { return NULL; }
  245. #else
  246. char *myWideCharToMultiByte(const wchar_t *s)
  247. {
  248. const wchar_t *ss=s;
  249. int i=(int)wcsrtombs(NULL,&ss,0,NULL);
  250. if (i<0) return NULL;
  251. char *d=(char *)malloc(i+1);
  252. wcsrtombs(d,&s,i,NULL);
  253. d[i]=0;
  254. return d;
  255. }
  256. #endif
  257. #ifdef _XMLWIDECHAR
  258. wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
  259. {
  260. const char *ss=s;
  261. int i=(int)mbsrtowcs(NULL,&ss,0,NULL);
  262. if (i<0) return NULL;
  263. wchar_t *d=(wchar_t *)malloc((i+1)*sizeof(wchar_t));
  264. mbsrtowcs(d,&s,i,NULL);
  265. d[i]=0;
  266. return d;
  267. }
  268. int xstrlen(XMLCSTR c) { return wcslen(c); }
  269. #ifdef sun
  270. // for CC
  271. #include <widec.h>
  272. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncasecmp(c1,c2,l);}
  273. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncmp(c1,c2,l);}
  274. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wscasecmp(c1,c2); }
  275. #else
  276. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);}
  277. #ifdef __linux__
  278. // for gcc/linux
  279. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncasecmp(c1,c2,l);}
  280. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wcscasecmp(c1,c2); }
  281. #else
  282. #include <wctype.h>
  283. // for gcc/non-linux (MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw)
  284. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2)
  285. {
  286. wchar_t left,right;
  287. do
  288. {
  289. left=towlower(*c1++); right=towlower(*c2++);
  290. } while (left&&(left==right));
  291. return (int)left-(int)right;
  292. }
  293. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l)
  294. {
  295. wchar_t left,right;
  296. while(l--)
  297. {
  298. left=towlower(*c1++); right=towlower(*c2++);
  299. if ((!left)||(left!=right)) return (int)left-(int)right;
  300. }
  301. return 0;
  302. }
  303. #endif
  304. #endif
  305. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); }
  306. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); }
  307. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode)
  308. {
  309. char *filenameAscii=myWideCharToMultiByte(filename);
  310. FILE *f;
  311. if (mode[0]==_CXML('r')) f=fopen(filenameAscii,"rb");
  312. else f=fopen(filenameAscii,"wb");
  313. free(filenameAscii);
  314. return f;
  315. }
  316. #else
  317. static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); }
  318. static inline int xstrlen(XMLCSTR c) { return strlen(c); }
  319. static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncasecmp(c1,c2,l);}
  320. static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);}
  321. static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return strcasecmp(c1,c2); }
  322. static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); }
  323. static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); }
  324. #endif
  325. static inline int _strnicmp(const char *c1,const char *c2, int l) { return strncasecmp(c1,c2,l);}
  326. #endif
  327. ///////////////////////////////////////////////////////////////////////////////
  328. // the "xmltoc,xmltob,xmltoi,xmltol,xmltof,xmltoa" functions //
  329. ///////////////////////////////////////////////////////////////////////////////
  330. // These 6 functions are not used inside the XMLparser.
  331. // There are only here as "convenience" functions for the user.
  332. // If you don't need them, you can delete them without any trouble.
  333. #ifdef _XMLWIDECHAR
  334. #ifdef _XMLWINDOWS
  335. // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
  336. char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)_wtoi(t); return v; }
  337. int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return _wtoi(t); return v; }
  338. long long xmltol(XMLCSTR t,long long v){ if (t&&(*t)) return _wtoi64(t); return v; }
  339. double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; }
  340. #else
  341. #ifdef sun
  342. // for CC
  343. #include <widec.h>
  344. char xmltob(XMLCSTR t,char v){ if (t) return (char)wstol(t,NULL,10); return v; }
  345. int xmltoi(XMLCSTR t,int v){ if (t) return (int)wstol(t,NULL,10); return v; }
  346. long long xmltol(XMLCSTR t,long long v){ if (t) return wstol(t,NULL,10); return v; }
  347. #else
  348. // for gcc
  349. char xmltob(XMLCSTR t,char v){ if (t) return (char)wcstol(t,NULL,10); return v; }
  350. int xmltoi(XMLCSTR t,int v){ if (t) return (int)wcstol(t,NULL,10); return v; }
  351. long long xmltol(XMLCSTR t,long long v){ if (t) return wcstol(t,NULL,10); return v; }
  352. #endif
  353. double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; }
  354. #endif
  355. #else
  356. #ifdef _XMLWINDOWS
  357. long long xmltol(XMLCSTR t,long long v){ if (t&&(*t)) return _atoi64(t); return v; }
  358. #else
  359. long long xmltol(XMLCSTR t,long long v){ if (t&&(*t)) return atoll(t); return v; }
  360. #endif
  361. char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)atoi(t); return v; }
  362. int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return atoi(t); return v; }
  363. double xmltof(XMLCSTR t,double v){ if (t&&(*t)) return atof(t); return v; }
  364. #endif
  365. XMLCSTR xmltoa(XMLCSTR t, XMLCSTR v){ if (t) return t; return v; }
  366. XMLCHAR xmltoc(XMLCSTR t,const XMLCHAR v){ if (t&&(*t)) return *t; return v; }
  367. /////////////////////////////////////////////////////////////////////////
  368. // the "openFileHelper" function //
  369. /////////////////////////////////////////////////////////////////////////
  370. // Since each application has its own way to report and deal with errors, you should modify & rewrite
  371. // the following "openFileHelper" function to get an "error reporting mechanism" tailored to your needs.
  372. XMLNode XMLNode::openFileHelper(XMLCSTR filename, XMLCSTR tag)
  373. {
  374. // guess the value of the global parameter "characterEncoding"
  375. // (the guess is based on the first 200 bytes of the file).
  376. FILE *f=xfopen(filename,_CXML("rb"));
  377. if (f)
  378. {
  379. char bb[205];
  380. int l=(int)fread(bb,1,200,f);
  381. setGlobalOptions(guessCharEncoding(bb,l),guessWideCharChars,dropWhiteSpace,removeCommentsInMiddleOfText);
  382. fclose(f);
  383. }
  384. // parse the file
  385. XMLResults pResults;
  386. XMLNode xnode=XMLNode::parseFile(filename,tag,&pResults);
  387. // display error message (if any)
  388. if (pResults.error != eXMLErrorNone)
  389. {
  390. // create message
  391. char message[2000],*s1=(char*)"",*s3=(char*)""; XMLCSTR s2=_CXML("");
  392. if (pResults.error==eXMLErrorFirstTagNotFound) { s1=(char*)"First Tag should be '"; s2=tag; s3=(char*)"'.\n"; }
  393. #ifdef _XMLWINDOWS
  394. _snprintf(message,2000,
  395. #else
  396. snprintf(message,2000,
  397. #endif
  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 XMLStatus
  561. {
  562. eInsideTag = 0,
  563. eOutsideTag
  564. } XMLStatus;
  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. free(t);
  623. fclose(f);
  624. return eXMLErrorCannotWriteFile;
  625. }
  626. if (fclose(f)!=0)
  627. {
  628. free(t);
  629. return eXMLErrorCannotWriteFile;
  630. }
  631. free(t);
  632. return eXMLErrorNone;
  633. }
  634. // Duplicate a given string.
  635. XMLSTR stringDup(XMLCSTR lpszData, int cbData)
  636. {
  637. if (lpszData==NULL) return NULL;
  638. XMLSTR lpszNew;
  639. if (cbData==-1) cbData=(int)xstrlen(lpszData);
  640. lpszNew = (XMLSTR)malloc((cbData+1) * sizeof(XMLCHAR));
  641. if (lpszNew)
  642. {
  643. memcpy(lpszNew, lpszData, (cbData) * sizeof(XMLCHAR));
  644. lpszNew[cbData] = (XMLCHAR)NULL;
  645. }
  646. return lpszNew;
  647. }
  648. XMLSTR ToXMLStringTool::toXMLUnSafe(XMLSTR dest,XMLCSTR source)
  649. {
  650. XMLSTR dd=dest;
  651. XMLCHAR ch;
  652. XMLCharacterEntity *entity;
  653. while ((ch=*source))
  654. {
  655. entity=XMLEntities;
  656. do
  657. {
  658. if (ch==entity->c) {xstrcpy(dest,entity->s); dest+=entity->l; source++; goto out_of_loop1; }
  659. entity++;
  660. } while(entity->s);
  661. #ifdef _XMLWIDECHAR
  662. *(dest++)=*(source++);
  663. #else
  664. switch(XML_ByteTable[(unsigned char)ch])
  665. {
  666. case 4:
  667. if ((!(source[1]))||(!(source[2]))||(!(source[3]))) { *(dest++)='_'; source++; }
  668. else
  669. {
  670. *dest=*source;
  671. dest[1]=source[1];
  672. dest[2]=source[2];
  673. dest[3]=source[3];
  674. dest+=4; source+=4;
  675. }
  676. break;
  677. case 3:
  678. if ((!(source[1]))||(!(source[2]))) { *(dest++)='_'; source++; }
  679. else
  680. {
  681. *dest=*source;
  682. dest[1]=source[1];
  683. dest[2]=source[2];
  684. dest+=3; source+=3;
  685. }
  686. break;
  687. case 2:
  688. if (!(source[1])) { *(dest++)='_'; source++; }
  689. else
  690. {
  691. *dest=*source;
  692. dest[1]=source[1];
  693. dest+=2; source+=2;
  694. }
  695. break;
  696. case 1: *(dest++)=*(source++);
  697. }
  698. #endif
  699. out_of_loop1:
  700. ;
  701. }
  702. *dest=0;
  703. return dd;
  704. }
  705. // private (used while rendering):
  706. int ToXMLStringTool::lengthXMLString(XMLCSTR source)
  707. {
  708. int r=0;
  709. XMLCharacterEntity *entity;
  710. XMLCHAR ch;
  711. while ((ch=*source))
  712. {
  713. entity=XMLEntities;
  714. do
  715. {
  716. if (ch==entity->c) { r+=entity->l; source++; goto out_of_loop1; }
  717. entity++;
  718. } while(entity->s);
  719. #ifdef _XMLWIDECHAR
  720. r++; source++;
  721. #else
  722. ch=XML_ByteTable[(unsigned char)ch]; r+=ch; source+=ch;
  723. #endif
  724. out_of_loop1:
  725. ;
  726. }
  727. return r;
  728. }
  729. ToXMLStringTool::~ToXMLStringTool(){ freeBuffer(); }
  730. void ToXMLStringTool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; }
  731. XMLSTR ToXMLStringTool::toXML(XMLCSTR source)
  732. {
  733. if (!source)
  734. {
  735. if (buflen<1) { buflen=1; buf=(XMLSTR)malloc(sizeof(XMLCHAR)); }
  736. *buf=0;
  737. return buf;
  738. }
  739. int l=lengthXMLString(source)+1;
  740. if (l>buflen) { freeBuffer(); buflen=l; buf=(XMLSTR)malloc(l*sizeof(XMLCHAR)); }
  741. return toXMLUnSafe(buf,source);
  742. }
  743. // private:
  744. XMLSTR fromXMLString(XMLCSTR s, int lo, XML *pXML)
  745. {
  746. // This function is the opposite of the function "toXMLString". It decodes the escape
  747. // sequences &amp;, &quot;, &apos;, &lt;, &gt; and replace them by the characters
  748. // &,",',<,>. This function is used internally by the XML Parser. All the calls to
  749. // the XML library will always gives you back "decoded" strings.
  750. //
  751. // in: string (s) and length (lo) of string
  752. // out: new allocated string converted from xml
  753. if (!s) return NULL;
  754. int ll=0,j;
  755. XMLSTR d;
  756. XMLCSTR ss=s;
  757. XMLCharacterEntity *entity;
  758. while ((lo>0)&&(*s))
  759. {
  760. if (*s==_CXML('&'))
  761. {
  762. if ((lo>2)&&(s[1]==_CXML('#')))
  763. {
  764. s+=2; lo-=2;
  765. if ((*s==_CXML('X'))||(*s==_CXML('x'))) { s++; lo--; }
  766. while ((*s)&&(*s!=_CXML(';'))&&((lo--)>0)) s++;
  767. if (*s!=_CXML(';'))
  768. {
  769. pXML->error=eXMLErrorUnknownCharacterEntity;
  770. return NULL;
  771. }
  772. s++; lo--;
  773. } else
  774. {
  775. entity=XMLEntities;
  776. do
  777. {
  778. if ((lo>=entity->l)&&(xstrnicmp(s,entity->s,entity->l)==0)) { s+=entity->l; lo-=entity->l; break; }
  779. entity++;
  780. } while(entity->s);
  781. if (!entity->s)
  782. {
  783. pXML->error=eXMLErrorUnknownCharacterEntity;
  784. return NULL;
  785. }
  786. }
  787. } else
  788. {
  789. #ifdef _XMLWIDECHAR
  790. s++; lo--;
  791. #else
  792. j=XML_ByteTable[(unsigned char)*s]; s+=j; lo-=j; ll+=j-1;
  793. #endif
  794. }
  795. ll++;
  796. }
  797. d=(XMLSTR)malloc((ll+1)*sizeof(XMLCHAR));
  798. s=d;
  799. while (ll-->0)
  800. {
  801. if (*ss==_CXML('&'))
  802. {
  803. if (ss[1]==_CXML('#'))
  804. {
  805. ss+=2; j=0;
  806. if ((*ss==_CXML('X'))||(*ss==_CXML('x')))
  807. {
  808. ss++;
  809. while (*ss!=_CXML(';'))
  810. {
  811. if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j<<4)+*ss-_CXML('0');
  812. else if ((*ss>=_CXML('A'))&&(*ss<=_CXML('F'))) j=(j<<4)+*ss-_CXML('A')+10;
  813. else if ((*ss>=_CXML('a'))&&(*ss<=_CXML('f'))) j=(j<<4)+*ss-_CXML('a')+10;
  814. else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
  815. ss++;
  816. }
  817. } else
  818. {
  819. while (*ss!=_CXML(';'))
  820. {
  821. if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j*10)+*ss-_CXML('0');
  822. else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;}
  823. ss++;
  824. }
  825. }
  826. #ifndef _XMLWIDECHAR
  827. if (j>255) { free((void*)s); pXML->error=eXMLErrorCharacterCodeAbove255;return NULL;}
  828. #endif
  829. (*d++)=(XMLCHAR)j; ss++;
  830. } else
  831. {
  832. entity=XMLEntities;
  833. do
  834. {
  835. if (xstrnicmp(ss,entity->s,entity->l)==0) { *(d++)=entity->c; ss+=entity->l; break; }
  836. entity++;
  837. } while(entity->s);
  838. }
  839. } else
  840. {
  841. #ifdef _XMLWIDECHAR
  842. *(d++)=*(ss++);
  843. #else
  844. switch(XML_ByteTable[(unsigned char)*ss])
  845. {
  846. case 4: *(d++)=*(ss++); ll--;
  847. case 3: *(d++)=*(ss++); ll--;
  848. case 2: *(d++)=*(ss++); ll--;
  849. case 1: *(d++)=*(ss++);
  850. }
  851. #endif
  852. }
  853. }
  854. *d=0;
  855. return (XMLSTR)s;
  856. }
  857. #define XML_isSPACECHAR(ch) ((ch==_CXML('\n'))||(ch==_CXML(' '))||(ch== _CXML('\t'))||(ch==_CXML('\r')))
  858. // private:
  859. char myTagCompare(XMLCSTR cclose, XMLCSTR copen)
  860. // !!!! WARNING strange convention&:
  861. // return 0 if equals
  862. // return 1 if different
  863. {
  864. if (!cclose) return 1;
  865. int l=(int)xstrlen(cclose);
  866. if (xstrnicmp(cclose, copen, l)!=0) return 1;
  867. const XMLCHAR c=copen[l];
  868. if (XML_isSPACECHAR(c)||
  869. (c==_CXML('/' ))||
  870. (c==_CXML('<' ))||
  871. (c==_CXML('>' ))||
  872. (c==_CXML('=' ))) return 0;
  873. return 1;
  874. }
  875. // Obtain the next character from the string.
  876. static inline XMLCHAR getNextChar(XML *pXML)
  877. {
  878. XMLCHAR ch = pXML->lpXML[pXML->nIndex];
  879. #ifdef _XMLWIDECHAR
  880. if (ch!=0) pXML->nIndex++;
  881. #else
  882. pXML->nIndex+=XML_ByteTable[(unsigned char)ch];
  883. #endif
  884. return ch;
  885. }
  886. // Find the next token in a string.
  887. // pcbToken contains the number of characters that have been read.
  888. static NextToken GetNextToken(XML *pXML, int *pcbToken, enum XMLTokenTypeTag *pType)
  889. {
  890. NextToken result;
  891. XMLCHAR ch;
  892. XMLCHAR chTemp;
  893. int indexStart,nFoundMatch,nIsText=FALSE;
  894. result.pClr=NULL; // prevent warning
  895. // Find next non-white space character
  896. do { indexStart=pXML->nIndex; ch=getNextChar(pXML); } while XML_isSPACECHAR(ch);
  897. if (ch)
  898. {
  899. // Cache the current string pointer
  900. result.pStr = &pXML->lpXML[indexStart];
  901. // check for standard tokens
  902. switch(ch)
  903. {
  904. // Check for quotes
  905. case _CXML('\''):
  906. case _CXML('\"'):
  907. // Type of token
  908. *pType = eTokenQuotedText;
  909. chTemp = ch;
  910. // Set the size
  911. nFoundMatch = FALSE;
  912. // Search through the string to find a matching quote
  913. while((ch = getNextChar(pXML)))
  914. {
  915. if (ch==chTemp) { nFoundMatch = TRUE; break; }
  916. if (ch==_CXML('<')) break;
  917. }
  918. // If we failed to find a matching quote
  919. if (nFoundMatch == FALSE)
  920. {
  921. pXML->nIndex=indexStart+1;
  922. nIsText=TRUE;
  923. break;
  924. }
  925. // 4.02.2002
  926. // if (FindNonWhiteSpace(pXML)) pXML->nIndex--;
  927. break;
  928. // Equals (used with attribute values)
  929. case _CXML('='):
  930. *pType = eTokenEquals;
  931. break;
  932. // Close tag
  933. case _CXML('>'):
  934. *pType = eTokenCloseTag;
  935. break;
  936. // Check for tag start and tag end
  937. case _CXML('<'):
  938. {
  939. // First check whether the token is in the clear tag list (meaning it
  940. // does not need formatting).
  941. ALLXMLClearTag *ctag=XMLClearTags;
  942. do
  943. {
  944. if (!xstrncmp(ctag->lpszOpen, result.pStr, ctag->openTagLen))
  945. {
  946. result.pClr=ctag;
  947. pXML->nIndex+=ctag->openTagLen-1;
  948. *pType=eTokenClear;
  949. return result;
  950. }
  951. ctag++;
  952. } while(ctag->lpszOpen);
  953. // Peek at the next character to see if we have an end tag '</',
  954. // or an xml declaration '<?'
  955. chTemp = pXML->lpXML[pXML->nIndex];
  956. // If we have a tag end...
  957. if (chTemp == _CXML('/'))
  958. {
  959. // Set the type and ensure we point at the next character
  960. getNextChar(pXML);
  961. *pType = eTokenTagEnd;
  962. }
  963. // If we have an XML declaration tag
  964. else if (chTemp == _CXML('?'))
  965. {
  966. // Set the type and ensure we point at the next character
  967. getNextChar(pXML);
  968. *pType = eTokenDeclaration;
  969. }
  970. // Otherwise we must have a start tag
  971. else
  972. {
  973. *pType = eTokenTagStart;
  974. }
  975. break;
  976. }
  977. // Check to see if we have a short hand type end tag ('/>').
  978. case _CXML('/'):
  979. // Peek at the next character to see if we have a short end tag '/>'
  980. chTemp = pXML->lpXML[pXML->nIndex];
  981. // If we have a short hand end tag...
  982. if (chTemp == _CXML('>'))
  983. {
  984. // Set the type and ensure we point at the next character
  985. getNextChar(pXML);
  986. *pType = eTokenShortHandClose;
  987. break;
  988. }
  989. // If we haven't found a short hand closing tag then drop into the
  990. // text process
  991. // Other characters
  992. default:
  993. nIsText = TRUE;
  994. }
  995. // If this is a TEXT node
  996. if (nIsText)
  997. {
  998. // Indicate we are dealing with text
  999. *pType = eTokenText;
  1000. while((ch = getNextChar(pXML)))
  1001. {
  1002. if XML_isSPACECHAR(ch)
  1003. {
  1004. indexStart++; break;
  1005. } else if (ch==_CXML('/'))
  1006. {
  1007. // If we find a slash then this maybe text or a short hand end tag
  1008. // Peek at the next character to see it we have short hand end tag
  1009. ch=pXML->lpXML[pXML->nIndex];
  1010. // If we found a short hand end tag then we need to exit the loop
  1011. if (ch==_CXML('>')) { pXML->nIndex--; break; }
  1012. } else if ((ch==_CXML('<'))||(ch==_CXML('>'))||(ch==_CXML('=')))
  1013. {
  1014. pXML->nIndex--; break;
  1015. }
  1016. }
  1017. }
  1018. *pcbToken = pXML->nIndex-indexStart;
  1019. } else
  1020. {
  1021. // If we failed to obtain a valid character
  1022. *pcbToken = 0;
  1023. *pType = eTokenError;
  1024. result.pStr=NULL;
  1025. }
  1026. return result;
  1027. }
  1028. XMLCSTR XMLNode::updateName_WOSD(XMLSTR lpszName)
  1029. {
  1030. if (!d) { free(lpszName); return NULL; }
  1031. if (d->lpszName&&(lpszName!=d->lpszName)) free((void*)d->lpszName);
  1032. d->lpszName=lpszName;
  1033. return lpszName;
  1034. }
  1035. // private:
  1036. XMLNode::XMLNode(struct XMLNodeDataTag *p){ d=p; (p->ref_count)++; }
  1037. XMLNode::XMLNode(XMLNodeData *pParent, XMLSTR lpszName, char isDeclaration)
  1038. {
  1039. d=(XMLNodeData*)malloc(sizeof(XMLNodeData));
  1040. d->ref_count=1;
  1041. d->lpszName=NULL;
  1042. d->nChild= 0;
  1043. d->nText = 0;
  1044. d->nClear = 0;
  1045. d->nAttribute = 0;
  1046. d->isDeclaration = isDeclaration;
  1047. d->pParent = pParent;
  1048. d->pChild= NULL;
  1049. d->pText= NULL;
  1050. d->pClear= NULL;
  1051. d->pAttribute= NULL;
  1052. d->pOrder= NULL;
  1053. updateName_WOSD(lpszName);
  1054. }
  1055. XMLNode XMLNode::createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration) { return XMLNode(NULL,lpszName,isDeclaration); }
  1056. XMLNode XMLNode::createXMLTopNode(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL,stringDup(lpszName),isDeclaration); }
  1057. #define MEMORYINCREASE 50
  1058. static inline void myFree(void *p) { if (p) free(p); }
  1059. static inline void *myRealloc(void *p, int newsize, int memInc, int sizeofElem)
  1060. {
  1061. if (p==NULL) { if (memInc) return malloc(memInc*sizeofElem); return malloc(sizeofElem); }
  1062. if ((memInc==0)||((newsize%memInc)==0)) p=realloc(p,(newsize+memInc)*sizeofElem);
  1063. // if (!p)
  1064. // {
  1065. // printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220);
  1066. // }
  1067. return p;
  1068. }
  1069. // private:
  1070. XMLElementPosition XMLNode::findPosition(XMLNodeData *d, int index, XMLElementType xxtype)
  1071. {
  1072. if (index<0) return -1;
  1073. int i=0,j=(int)((index<<2)+xxtype),*o=d->pOrder; while (o[i]!=j) i++; return i;
  1074. }
  1075. // private:
  1076. // update "order" information when deleting a content of a XMLNode
  1077. int XMLNode::removeOrderElement(XMLNodeData *d, XMLElementType t, int index)
  1078. {
  1079. int n=d->nChild+d->nText+d->nClear, *o=d->pOrder,i=findPosition(d,index,t);
  1080. memmove(o+i, o+i+1, (n-i)*sizeof(int));
  1081. for (;i<n;i++)
  1082. if ((o[i]&3)==(int)t) o[i]-=4;
  1083. // We should normally do:
  1084. // d->pOrder=(int)realloc(d->pOrder,n*sizeof(int));
  1085. // but we skip reallocation because it's too time consuming.
  1086. // Anyway, at the end, it will be free'd completely at once.
  1087. return i;
  1088. }
  1089. void *XMLNode::addToOrder(int memoryIncrease,int *_pos, int nc, void *p, int size, XMLElementType xtype)
  1090. {
  1091. // in: *_pos is the position inside d->pOrder ("-1" means "EndOf")
  1092. // out: *_pos is the index inside p
  1093. p=myRealloc(p,(nc+1),memoryIncrease,size);
  1094. int n=d->nChild+d->nText+d->nClear;
  1095. d->pOrder=(int*)myRealloc(d->pOrder,n+1,memoryIncrease*3,sizeof(int));
  1096. int pos=*_pos,*o=d->pOrder;
  1097. if ((pos<0)||(pos>=n)) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
  1098. int i=pos;
  1099. memmove(o+i+1, o+i, (n-i)*sizeof(int));
  1100. while ((pos<n)&&((o[pos]&3)!=(int)xtype)) pos++;
  1101. if (pos==n) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; }
  1102. o[i]=o[pos];
  1103. for (i=pos+1;i<=n;i++) if ((o[i]&3)==(int)xtype) o[i]+=4;
  1104. *_pos=pos=o[pos]>>2;
  1105. memmove(((char*)p)+(pos+1)*size,((char*)p)+pos*size,(nc-pos)*size);
  1106. return p;
  1107. }
  1108. // Add a child node to the given element.
  1109. XMLNode XMLNode::addChild_priv(int memoryIncrease, XMLSTR lpszName, char isDeclaration, int pos)
  1110. {
  1111. if (!lpszName) return emptyXMLNode;
  1112. d->pChild=(XMLNode*)addToOrder(memoryIncrease,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild);
  1113. d->pChild[pos].d=NULL;
  1114. d->pChild[pos]=XMLNode(d,lpszName,isDeclaration);
  1115. d->nChild++;
  1116. return d->pChild[pos];
  1117. }
  1118. // Add an attribute to an element.
  1119. XMLAttribute *XMLNode::addAttribute_priv(int memoryIncrease,XMLSTR lpszName, XMLSTR lpszValuev)
  1120. {
  1121. if (!lpszName) return &emptyXMLAttribute;
  1122. if (!d) { myFree(lpszName); myFree(lpszValuev); return &emptyXMLAttribute; }
  1123. int nc=d->nAttribute;
  1124. d->pAttribute=(XMLAttribute*)myRealloc(d->pAttribute,(nc+1),memoryIncrease,sizeof(XMLAttribute));
  1125. XMLAttribute *pAttr=d->pAttribute+nc;
  1126. pAttr->lpszName = lpszName;
  1127. pAttr->lpszValue = lpszValuev;
  1128. d->nAttribute++;
  1129. return pAttr;
  1130. }
  1131. // Add text to the element.
  1132. XMLCSTR XMLNode::addText_priv(int memoryIncrease, XMLSTR lpszValue, int pos)
  1133. {
  1134. if (!lpszValue) return NULL;
  1135. if (!d) { myFree(lpszValue); return NULL; }
  1136. d->pText=(XMLCSTR*)addToOrder(memoryIncrease,&pos,d->nText,d->pText,sizeof(XMLSTR),eNodeText);
  1137. d->pText[pos]=lpszValue;
  1138. d->nText++;
  1139. return lpszValue;
  1140. }
  1141. // Add clear (unformatted) text to the element.
  1142. XMLClear *XMLNode::addClear_priv(int memoryIncrease, XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos)
  1143. {
  1144. if (!lpszValue) return &emptyXMLClear;
  1145. if (!d) { myFree(lpszValue); return &emptyXMLClear; }
  1146. d->pClear=(XMLClear *)addToOrder(memoryIncrease,&pos,d->nClear,d->pClear,sizeof(XMLClear),eNodeClear);
  1147. XMLClear *pNewClear=d->pClear+pos;
  1148. pNewClear->lpszValue = lpszValue;
  1149. if (!lpszOpen) lpszOpen=XMLClearTags->lpszOpen;
  1150. if (!lpszClose) lpszClose=XMLClearTags->lpszClose;
  1151. pNewClear->lpszOpenTag = lpszOpen;
  1152. pNewClear->lpszCloseTag = lpszClose;
  1153. d->nClear++;
  1154. return pNewClear;
  1155. }
  1156. // private:
  1157. // Parse a clear (unformatted) type node.
  1158. char XMLNode::parseClearTag(void *px, void *_pClear)
  1159. {
  1160. XML *pXML=(XML *)px;
  1161. ALLXMLClearTag pClear=*((ALLXMLClearTag*)_pClear);
  1162. int cbTemp=0;
  1163. XMLCSTR lpszTemp=NULL;
  1164. XMLCSTR lpXML=&pXML->lpXML[pXML->nIndex];
  1165. static XMLCSTR docTypeEnd=_CXML("]>");
  1166. // Find the closing tag
  1167. // Seems the <!DOCTYPE need a better treatment so lets handle it
  1168. if (pClear.lpszOpen==XMLClearTags[1].lpszOpen)
  1169. {
  1170. XMLCSTR pCh=lpXML;
  1171. while (*pCh)
  1172. {
  1173. if (*pCh==_CXML('<')) { pClear.lpszClose=docTypeEnd; lpszTemp=xstrstr(lpXML,docTypeEnd); break; }
  1174. else if (*pCh==_CXML('>')) { lpszTemp=pCh; break; }
  1175. #ifdef _XMLWIDECHAR
  1176. pCh++;
  1177. #else
  1178. pCh+=XML_ByteTable[(unsigned char)(*pCh)];
  1179. #endif
  1180. }
  1181. } else lpszTemp=xstrstr(lpXML, pClear.lpszClose);
  1182. if (lpszTemp)
  1183. {
  1184. // Cache the size and increment the index
  1185. cbTemp = (int)(lpszTemp - lpXML);
  1186. pXML->nIndex += cbTemp+(int)xstrlen(pClear.lpszClose);
  1187. // Add the clear node to the current element
  1188. addClear_priv(MEMORYINCREASE,cbTemp?stringDup(lpXML,cbTemp):NULL, pClear.lpszOpen, pClear.lpszClose,-1);
  1189. return 0;
  1190. }
  1191. // If we failed to find the end tag
  1192. pXML->error = eXMLErrorUnmatchedEndClearTag;
  1193. return 1;
  1194. }
  1195. void XMLNode::exactMemory(XMLNodeData *d)
  1196. {
  1197. if (d->pOrder) d->pOrder=(int*)realloc(d->pOrder,(d->nChild+d->nText+d->nClear)*sizeof(int));
  1198. if (d->pChild) d->pChild=(XMLNode*)realloc(d->pChild,d->nChild*sizeof(XMLNode));
  1199. if (d->pAttribute) d->pAttribute=(XMLAttribute*)realloc(d->pAttribute,d->nAttribute*sizeof(XMLAttribute));
  1200. if (d->pText) d->pText=(XMLCSTR*)realloc(d->pText,d->nText*sizeof(XMLSTR));
  1201. if (d->pClear) d->pClear=(XMLClear *)realloc(d->pClear,d->nClear*sizeof(XMLClear));
  1202. }
  1203. char XMLNode::maybeAddTxT(void *pa, XMLCSTR tokenPStr)
  1204. {
  1205. XML *pXML=(XML *)pa;
  1206. XMLCSTR lpszText=pXML->lpszText;
  1207. if (!lpszText) return 0;
  1208. if (dropWhiteSpace) while (XML_isSPACECHAR(*lpszText)&&(lpszText!=tokenPStr)) lpszText++;
  1209. int cbText = (int)(tokenPStr - lpszText);
  1210. if (!cbText) { pXML->lpszText=NULL; return 0; }
  1211. if (dropWhiteSpace) { cbText--; while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; }
  1212. if (!cbText) { pXML->lpszText=NULL; return 0; }
  1213. XMLSTR lpt=fromXMLString(lpszText,cbText,pXML);
  1214. if (!lpt) return 1;
  1215. pXML->lpszText=NULL;
  1216. if (removeCommentsInMiddleOfText && d->nText && d->nClear)
  1217. {
  1218. // if the previous insertion was a comment (<!-- -->) AND
  1219. // if the previous previous insertion was a text then, delete the comment and append the text
  1220. int n=d->nChild+d->nText+d->nClear-1,*o=d->pOrder;
  1221. if (((o[n]&3)==eNodeClear)&&((o[n-1]&3)==eNodeText))
  1222. {
  1223. int i=o[n]>>2;
  1224. if (d->pClear[i].lpszOpenTag==XMLClearTags[2].lpszOpen)
  1225. {
  1226. deleteClear(i);
  1227. i=o[n-1]>>2;
  1228. n=xstrlen(d->pText[i]);
  1229. int n2=xstrlen(lpt)+1;
  1230. d->pText[i]=(XMLSTR)realloc((void*)d->pText[i],(n+n2)*sizeof(XMLCHAR));
  1231. if (!d->pText[i]) return 1;
  1232. memcpy((void*)(d->pText[i]+n),lpt,n2*sizeof(XMLCHAR));
  1233. free(lpt);
  1234. return 0;
  1235. }
  1236. }
  1237. }
  1238. addText_priv(MEMORYINCREASE,lpt,-1);
  1239. return 0;
  1240. }
  1241. // private:
  1242. // Recursively parse an XML element.
  1243. int XMLNode::ParseXMLElement(v

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