PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/CleanProject/XMLParser/xmlParser.cpp

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

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