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

/src/xmlParser.cpp

https://github.com/tristanstcyr/MacFungus-2.0
C++ | 2558 lines | 1981 code | 217 blank | 360 comment | 365 complexity | b1a30150b57a0fcbf5d0495672c92d73 MD5 | raw file
Possible License(s): BSD-3-Clause

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

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