You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2243 lines
47 KiB

7 years ago
​
​
​
​
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
7 years ago
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifdef WIN32
  2. #ifndef WIN32_LEAN_AND_MEAN
  3. #define WIN32_LEAN_AND_MEAN
  4. #endif
  5. #include <Windows.h> // to have IsTextUnicode, MultiByteToWideChar, WideCharToMultiByte
  6. // to handle unicode files
  7. #endif
  8. #include "xmlParser.h"
  9. #include <assert.h>
  10. #include <memory.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. XMLNode XMLNode::emptyXMLNode;
  15. XMLClear XMLNode::emptyXMLClear = {NULL, NULL, NULL};
  16. XMLAttribute XMLNode::emptyXMLAttribute = {NULL, NULL};
  17. #ifndef WIN32
  18. int _tcslen(const char * c)
  19. {
  20. return strlen(c);
  21. }
  22. int _tcsnicmp(const char * c1, const char * c2, int l)
  23. {
  24. return strncasecmp(c1, c2, l);
  25. }
  26. int _tcsicmp(const char * c1, const char * c2)
  27. {
  28. return strcasecmp(c1, c2);
  29. }
  30. char * _tcsstr(const char * c1, const char * c2)
  31. {
  32. return (char *) strstr(c1, c2);
  33. }
  34. char * _tcschr(const char * c1, int c2)
  35. {
  36. return (char *) strchr(c1, c2);
  37. }
  38. char * _tcscpy(char * c1, const char * c2)
  39. {
  40. return (char *) strcpy(c1, c2);
  41. }
  42. #endif
  43. inline int mmin(const int t1, const int t2)
  44. {
  45. return t1 < t2 ? t1 : t2;
  46. }
  47. // Enumeration used to decipher what type a token is
  48. enum TokenTypeTag
  49. {
  50. eTokenText = 0,
  51. eTokenQuotedText,
  52. eTokenTagStart, /* "<" */
  53. eTokenTagEnd, /* "</" */
  54. eTokenCloseTag, /* ">" */
  55. eTokenEquals, /* "=" */
  56. eTokenDeclaration, /* "<?" */
  57. eTokenShortHandClose, /* "/>" */
  58. eTokenClear,
  59. eTokenError
  60. };
  61. #define INDENTCHAR _T('\t')
  62. typedef struct ClearTag
  63. {
  64. LPCTSTR lpszOpen;
  65. LPCTSTR lpszClose;
  66. } ClearTag;
  67. // Main structure used for parsing XML
  68. typedef struct XML
  69. {
  70. LPCTSTR lpXML;
  71. int nIndex;
  72. enum XMLError error;
  73. LPCTSTR lpEndTag;
  74. int cbEndTag;
  75. LPCTSTR lpNewElement;
  76. int cbNewElement;
  77. int nFirst;
  78. ClearTag * pClrTags;
  79. } XML;
  80. typedef struct
  81. {
  82. ClearTag * pClr;
  83. LPCTSTR pStr;
  84. } NextToken;
  85. // Enumeration used when parsing attributes
  86. typedef enum Attrib
  87. {
  88. eAttribName = 0,
  89. eAttribEquals,
  90. eAttribValue
  91. } Attrib;
  92. // Enumeration used when parsing elements to dictate whether we are currently
  93. // inside a tag
  94. typedef enum Status
  95. {
  96. eInsideTag = 0,
  97. eOutsideTag
  98. } Status;
  99. // private:
  100. LPTSTR toXMLString(LPTSTR dest, LPCTSTR source)
  101. {
  102. LPTSTR dd = dest;
  103. while (*source)
  104. {
  105. switch (*source)
  106. {
  107. case '<':
  108. _tcscpy(dest, _T("&lt;" ));
  109. dest += 4;
  110. break;
  111. case '>':
  112. _tcscpy(dest, _T("&gt;" ));
  113. dest += 4;
  114. break;
  115. case '&':
  116. _tcscpy(dest, _T("&amp;" ));
  117. dest += 5;
  118. break;
  119. case '\'':
  120. _tcscpy(dest, _T("&apos;"));
  121. dest += 6;
  122. break;
  123. case '"':
  124. _tcscpy(dest, _T("&quot;"));
  125. dest += 6;
  126. break;
  127. default:
  128. *dest = *source;
  129. dest++;
  130. break;
  131. }
  132. source++;
  133. }
  134. *dest = 0;
  135. return dd;
  136. }
  137. // private:
  138. int lengthXMLString(LPCTSTR source)
  139. {
  140. int r = 0;
  141. while (*source)
  142. {
  143. switch (*source)
  144. {
  145. case '<' : r += 3; break;
  146. case '>' : r += 3; break;
  147. case '&' : r += 4; break;
  148. case '\'': r += 5; break;
  149. case '"' : r += 5; break;
  150. }
  151. source++;
  152. r++;
  153. }
  154. return r;
  155. }
  156. LPTSTR toXMLString(LPCTSTR source)
  157. {
  158. LPTSTR dest = (LPTSTR) malloc((lengthXMLString(source) + 1) * sizeof(TCHAR));
  159. return toXMLString(dest, source);
  160. }
  161. LPTSTR toXMLStringFast(LPTSTR * dest, int * destSz, LPCTSTR source)
  162. {
  163. int l = lengthXMLString(source) + 1;
  164. if (l > *destSz)
  165. {
  166. *destSz = l;
  167. *dest = (LPTSTR) realloc(*dest, l * sizeof(TCHAR));
  168. }
  169. return toXMLString(*dest, source);
  170. }
  171. // private:
  172. LPTSTR fromXMLString(LPCTSTR s, int lo)
  173. {
  174. // This function is the opposite of the function "toXMLString". It decodes the escape
  175. // sequences &amp;, &quot;, &apos;, &lt;, &gt; and replace them by the characters
  176. // &,",',<,>. This function is used internally by the XML Parser. All the calls to
  177. // the XML library will always gives you back "decoded" strings.
  178. //
  179. // in: string (s) and length (lo) of string
  180. // out: new allocated string converted from xml
  181. if (! s)
  182. {
  183. return NULL;
  184. }
  185. int ll = 0;
  186. LPTSTR d;
  187. LPCTSTR ss = s;
  188. while (((lo--) > 0) && (*s))
  189. {
  190. if (*s == _T('&'))
  191. {
  192. s++;
  193. if (_tcsnicmp(s, _T("lt;" ), 3) == 0)
  194. {
  195. s += 2;
  196. lo -= 3;
  197. }
  198. else if (_tcsnicmp(s, _T("gt;" ), 3) == 0)
  199. {
  200. s += 2;
  201. lo -= 3;
  202. }
  203. else if (_tcsnicmp(s, _T("amp;" ), 4) == 0)
  204. {
  205. s += 3;
  206. lo -= 4;
  207. }
  208. else if (_tcsnicmp(s, _T("apos;"), 5) == 0)
  209. {
  210. s += 4;
  211. lo -= 5;
  212. }
  213. else if (_tcsnicmp(s, _T("quot;"), 5) == 0)
  214. {
  215. s += 4;
  216. lo -= 5;
  217. }
  218. else
  219. {
  220. ll = 0;
  221. while (s[ll] && (s[ll] != _T(';')) && (ll < 10))
  222. {
  223. ll++;
  224. }
  225. ll++;
  226. d = (LPTSTR) malloc((ll + 1) * sizeof(TCHAR));
  227. d[ll] = 0;
  228. while (ll--)
  229. {
  230. d[ll] = s[ll];
  231. }
  232. #ifdef _UNICODE
  233. printf("unknown escape character: '&%S'", d);
  234. #else
  235. printf("unknown escape character: '&%s'", d);
  236. #endif
  237. free(d);
  238. exit(255);
  239. }
  240. };
  241. ll++;
  242. s++;
  243. }
  244. d = (LPTSTR) malloc((ll + 1) * sizeof(TCHAR));
  245. s = d;
  246. while (ll--)
  247. {
  248. if (*ss == _T('&'))
  249. {
  250. ss++;
  251. if (_tcsnicmp(ss, _T("lt;" ), 3) == 0)
  252. {
  253. *(d++) = _T('<');
  254. ss += 3;
  255. }
  256. else if (_tcsnicmp(ss, _T("gt;" ), 3) == 0)
  257. {
  258. *(d++) = _T('>');
  259. ss += 3;
  260. }
  261. else if (_tcsnicmp(ss, _T("amp;" ), 4) == 0)
  262. {
  263. *(d++) = _T('&');
  264. ss += 4;
  265. }
  266. else if (_tcsnicmp(ss, _T("apos;"), 5) == 0)
  267. {
  268. *(d++) = _T('\'');
  269. ss += 5;
  270. }
  271. else
  272. {
  273. *(d++) = _T('"');
  274. ss += 5;
  275. }
  276. }
  277. else
  278. {
  279. *(d++) = *ss;
  280. ss++;
  281. }
  282. }
  283. *d = 0;
  284. return (LPTSTR) s;
  285. }
  286. // private:
  287. char myTagCompare(LPCTSTR cclose, LPCTSTR copen)
  288. // !!!! WARNING strange convention&:
  289. // return 0 if equals
  290. // return 1 if different
  291. {
  292. if (! cclose)
  293. {
  294. return 1;
  295. }
  296. int l = (int) _tcslen(cclose);
  297. if (_tcsnicmp(cclose, copen, l) != 0)
  298. {
  299. return 1;
  300. }
  301. const TCHAR c = copen[l];
  302. if ((c == _T('\n')) || (c == _T(' ')) || (c == _T('\t')) || (c == _T('\r')) || (c == _T('/')) || (c == _T('<')) || (c == _T('>')) || (c == _T('=')))
  303. {
  304. return 0;
  305. }
  306. return 1;
  307. }
  308. // private:
  309. // update "order" information when deleting a content of a XMLNode
  310. void XMLNode::removeOrderElement(XMLNodeData * d, XMLElementType t, int index)
  311. {
  312. int j = (int) ((index << 2) + t), i = 0, n = nElement(d) + 1, *o = d->pOrder;
  313. while ((o[i] != j) && (i < n))
  314. {
  315. i++;
  316. }
  317. n--;
  318. memmove(o + i, o + i + 1, (n - i) * sizeof(int));
  319. for (; i < n; i++)
  320. {
  321. if ((o[i] & 3) == (int) t)
  322. {
  323. o[i] -= 4;
  324. }
  325. }
  326. // We should normally do:
  327. // d->pOrder=(int)realloc(d->pOrder,n*sizeof(int));
  328. // but we skip reallocation because it's too time consuming.
  329. // Anyway, at the end, it will be free'd completely at once.
  330. }
  331. // Obtain the next character from the string.
  332. static inline TCHAR getNextChar(XML * pXML)
  333. {
  334. TCHAR ch = pXML->lpXML[pXML->nIndex];
  335. if (ch != 0)
  336. {
  337. pXML->nIndex++;
  338. }
  339. return ch;
  340. }
  341. // Find next non-white space character.
  342. static TCHAR FindNonWhiteSpace(XML * pXML)
  343. {
  344. TCHAR ch;
  345. int nExit = FALSE;
  346. assert(pXML);
  347. // Iterate through characters in the string until we find a NULL or a
  348. // non-white space character
  349. while ((nExit == FALSE) && (ch = getNextChar(pXML)))
  350. {
  351. switch (ch)
  352. {
  353. // Ignore white space
  354. case _T('\n'):
  355. case _T(' '):
  356. case _T('\t'):
  357. case _T('\r'): continue;
  358. default : nExit = TRUE;
  359. }
  360. }
  361. return ch;
  362. }
  363. // Find the next token in a string.
  364. // pcbToken contains the number of characters that have been read.
  365. static NextToken GetNextToken(XML * pXML, int * pcbToken, enum TokenTypeTag * pType)
  366. {
  367. NextToken result;
  368. LPCTSTR lpXML;
  369. TCHAR ch;
  370. TCHAR chTemp;
  371. int nSize;
  372. int nFoundMatch;
  373. int nExit;
  374. int n;
  375. LPCTSTR lpszOpen;
  376. int cbOpen;
  377. int nIsText = FALSE;
  378. // Find next non-white space character
  379. ch = FindNonWhiteSpace(pXML);
  380. if (ch)
  381. {
  382. // Cache the current string pointer
  383. lpXML = pXML->lpXML;
  384. result.pStr = &lpXML[pXML->nIndex - 1];
  385. // First check whether the token is in the clear tag list (meaning it
  386. // does not need formatting).
  387. n = 0;
  388. while (TRUE)
  389. {
  390. // Obtain the name of the open part of the clear tag
  391. lpszOpen = pXML->pClrTags[n].lpszOpen;
  392. if (lpszOpen)
  393. {
  394. // Compare the open tag with the current token
  395. cbOpen = (int) _tcslen(lpszOpen);
  396. // if (myTagCompare(lpszOpen, result.pStr) == 0)
  397. if (_tcsnicmp(lpszOpen, result.pStr, cbOpen) == 0)
  398. {
  399. result.pClr = &pXML->pClrTags[n];
  400. pXML->nIndex += (int) (_tcslen(lpszOpen) - 1);
  401. *pType = eTokenClear;
  402. return result;
  403. }
  404. n++;
  405. }
  406. else
  407. {
  408. break;
  409. }
  410. }
  411. // If we didn't find a clear tag then check for standard tokens
  412. chTemp = 0;
  413. lpXML = pXML->lpXML;
  414. switch (ch)
  415. {
  416. // Check for quotes
  417. case _T('\''):
  418. case _T('\"'):
  419. // Type of token
  420. *pType = eTokenQuotedText;
  421. chTemp = ch;
  422. n = pXML->nIndex;
  423. // Set the size
  424. nSize = 1;
  425. nFoundMatch = FALSE;
  426. // Search through the string to find a matching quote
  427. while ((ch = getNextChar(pXML)))
  428. {
  429. nSize++;
  430. if (ch == chTemp)
  431. {
  432. nFoundMatch = TRUE;
  433. break;
  434. }
  435. if (ch == _T('<'))
  436. {
  437. break;
  438. }
  439. }
  440. // If we failed to find a matching quote
  441. if (nFoundMatch == FALSE)
  442. {
  443. pXML->nIndex = n - 1;
  444. ch = getNextChar(pXML);
  445. nIsText = TRUE;
  446. break;
  447. }
  448. // 4.02.2002
  449. if (FindNonWhiteSpace(pXML))
  450. {
  451. pXML->nIndex--;
  452. }
  453. break;
  454. // Equals (used with attribute values)
  455. case _T('='):
  456. nSize = 1;
  457. *pType = eTokenEquals;
  458. break;
  459. // Close tag
  460. case _T('>'):
  461. nSize = 1;
  462. *pType = eTokenCloseTag;
  463. break;
  464. // Check for tag start and tag end
  465. case _T('<'):
  466. // Peek at the next character to see if we have an end tag '</',
  467. // or an xml declaration '<?'
  468. chTemp = pXML->lpXML[pXML->nIndex];
  469. // If we have a tag end...
  470. if (chTemp == _T('/'))
  471. {
  472. // Set the type and ensure we point at the next character
  473. getNextChar(pXML);
  474. *pType = eTokenTagEnd;
  475. nSize = 2;
  476. }
  477. // If we have an XML declaration tag
  478. else if (chTemp == _T('?'))
  479. {
  480. // Set the type and ensure we point at the next character
  481. getNextChar(pXML);
  482. *pType = eTokenDeclaration;
  483. nSize = 2;
  484. }
  485. // Otherwise we must have a start tag
  486. else
  487. {
  488. *pType = eTokenTagStart;
  489. nSize = 1;
  490. }
  491. break;
  492. // Check to see if we have a short hand type end tag ('/>').
  493. case _T('/'):
  494. // Peek at the next character to see if we have a short end tag '/>'
  495. chTemp = pXML->lpXML[pXML->nIndex];
  496. // If we have a short hand end tag...
  497. if (chTemp == _T('>'))
  498. {
  499. // Set the type and ensure we point at the next character
  500. getNextChar(pXML);
  501. *pType = eTokenShortHandClose;
  502. nSize = 2;
  503. break;
  504. }
  505. // If we haven't found a short hand closing tag then drop into the
  506. // text process
  507. // Other characters
  508. default: nIsText = TRUE;
  509. }
  510. // If this is a TEXT node
  511. if (nIsText)
  512. {
  513. // Indicate we are dealing with text
  514. *pType = eTokenText;
  515. nSize = 1;
  516. nExit = FALSE;
  517. while ((nExit == FALSE) && (ch = getNextChar(pXML)))
  518. {
  519. switch (ch)
  520. {
  521. // Break when we find white space
  522. case _T('\n'):
  523. case _T(' '):
  524. case _T('\t'):
  525. case _T('\r'): nExit = TRUE; break;
  526. // If we find a slash then this maybe text or a short hand end tag.
  527. case _T('/'):
  528. // Peek at the next character to see it we have short hand end tag
  529. chTemp = pXML->lpXML[pXML->nIndex];
  530. // If we found a short hand end tag then we need to exit the loop
  531. if (chTemp == _T('>'))
  532. {
  533. pXML->nIndex--; // 03.02.2002
  534. nExit = TRUE;
  535. }
  536. else
  537. {
  538. nSize++;
  539. }
  540. break;
  541. // Break when we find a terminator and decrement the index and
  542. // column count so that we are pointing at the right character
  543. // the next time we are called.
  544. case _T('<'):
  545. case _T('>'):
  546. case _T('='):
  547. pXML->nIndex--;
  548. nExit = TRUE;
  549. break;
  550. case 0: nExit = TRUE; break;
  551. default: nSize++;
  552. }
  553. }
  554. }
  555. *pcbToken = nSize;
  556. }
  557. else
  558. {
  559. // If we failed to obtain a valid character
  560. *pcbToken = 0;
  561. *pType = eTokenError;
  562. result.pStr = NULL;
  563. }
  564. return result;
  565. }
  566. // Parse XML errors into a user friendly string.
  567. LPCTSTR XMLNode::getError(XMLError error)
  568. {
  569. switch (error)
  570. {
  571. case eXMLErrorNone : return _T("No error");
  572. case eXMLErrorMissingEndTag : return _T("Warning: Unmatched end tag");
  573. case eXMLErrorEmpty : return _T("Error: No XML data");
  574. case eXMLErrorFirstNotStartTag : return _T("Error: First token not start tag");
  575. case eXMLErrorMissingTagName : return _T("Error: Missing start tag name");
  576. case eXMLErrorMissingEndTagName: return _T("Error: Missing end tag name");
  577. case eXMLErrorNoMatchingQuote : return _T("Error: Unmatched quote");
  578. case eXMLErrorUnmatchedEndTag : return _T("Error: Unmatched end tag");
  579. case eXMLErrorUnexpectedToken : return _T("Error: Unexpected token found");
  580. case eXMLErrorInvalidTag : return _T("Error: Invalid tag found");
  581. case eXMLErrorNoElements : return _T("Error: No elements found");
  582. case eXMLErrorFileNotFound : return _T("Error: File not found");
  583. case eXMLErrorTagNotFound : return _T("Error: First Tag not found");
  584. };
  585. return _T("Unknown");
  586. }
  587. // private:
  588. XMLNode::XMLNode(XMLNode * pParent, LPCTSTR lpszName, int isDeclaration)
  589. {
  590. d = (XMLNodeData *) malloc(sizeof(XMLNodeData));
  591. d->ref_count = 1;
  592. d->lpszName = lpszName;
  593. d->nChild = 0;
  594. d->nText = 0;
  595. d->nClear = 0;
  596. d->nAttribute = 0;
  597. d->isDeclaration = isDeclaration;
  598. d->pParent = pParent;
  599. d->pChild = NULL;
  600. d->pText = NULL;
  601. d->pClear = NULL;
  602. d->pAttribute = NULL;
  603. d->pOrder = NULL;
  604. }
  605. const int memoryIncrease = 50;
  606. static void * myRealloc(void * p, int newsize, int memInc, int sizeofElem)
  607. {
  608. if (p == NULL)
  609. {
  610. return malloc(memInc * sizeofElem);
  611. }
  612. if ((newsize % memInc) == 0)
  613. {
  614. p = realloc(p, (newsize + memInc) * sizeofElem);
  615. }
  616. // if (!p)
  617. // {
  618. // printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220);
  619. // }
  620. return p;
  621. }
  622. void XMLNode::addToOrder(int index, int type)
  623. {
  624. int n = nElement();
  625. d->pOrder = (int *) myRealloc(d->pOrder, n + 1, memoryIncrease * 3, sizeof(int));
  626. d->pOrder[n] = (index << 2) + type;
  627. }
  628. // Add a child node to the given element.
  629. XMLNode XMLNode::addChild(LPCTSTR lpszName, int isDeclaration)
  630. {
  631. if (! lpszName)
  632. {
  633. return emptyXMLNode;
  634. }
  635. int nc = d->nChild;
  636. d->pChild = (XMLNode *) myRealloc(d->pChild, nc + 1, memoryIncrease, sizeof(XMLNode));
  637. d->pChild[nc].d = NULL;
  638. d->pChild[nc] = XMLNode(this, lpszName, isDeclaration);
  639. addToOrder(nc, eNodeChild);
  640. d->nChild++;
  641. return d->pChild[nc];
  642. }
  643. XMLNode XMLNode::createXMLTopNode()
  644. {
  645. return XMLNode(NULL, NULL, FALSE);
  646. }
  647. // Add an attribute to an element.
  648. XMLAttribute * XMLNode::addAttribute(LPCTSTR lpszName, LPCTSTR lpszValuev)
  649. {
  650. if (! lpszName)
  651. {
  652. return &emptyXMLAttribute;
  653. }
  654. int na = d->nAttribute;
  655. d->pAttribute = (XMLAttribute *) myRealloc(d->pAttribute, na + 1, memoryIncrease, sizeof(XMLAttribute));
  656. XMLAttribute * pAttr = d->pAttribute + na;
  657. pAttr->lpszName = lpszName;
  658. pAttr->lpszValue = lpszValuev;
  659. addToOrder(na, eNodeAttribute);
  660. d->nAttribute++;
  661. return pAttr;
  662. }
  663. // Add text to the element.
  664. LPCTSTR XMLNode::addText(LPCTSTR lpszValue)
  665. {
  666. if (! lpszValue)
  667. {
  668. return NULL;
  669. }
  670. int nt = d->nText;
  671. d->pText = (LPCTSTR *) myRealloc(d->pText, nt + 1, memoryIncrease, sizeof(LPTSTR));
  672. d->pText[nt] = lpszValue;
  673. addToOrder(nt, eNodeText);
  674. d->nText++;
  675. return d->pText[nt];
  676. }
  677. // Add clear (unformatted) text to the element.
  678. XMLClear * XMLNode::addClear(LPCTSTR lpszValue, LPCTSTR lpszOpen, LPCTSTR lpszClose)
  679. {
  680. if (! lpszValue)
  681. {
  682. return &emptyXMLClear;
  683. }
  684. int nc = d->nClear;
  685. d->pClear = (XMLClear *) myRealloc(d->pClear, nc + 1, memoryIncrease, sizeof(XMLClear));
  686. XMLClear * pNewClear = d->pClear + nc;
  687. pNewClear->lpszValue = lpszValue;
  688. pNewClear->lpszOpenTag = lpszOpen;
  689. pNewClear->lpszCloseTag = lpszClose;
  690. addToOrder(nc, eNodeClear);
  691. d->nClear++;
  692. return pNewClear;
  693. }
  694. // Trim the end of the text to remove white space characters.
  695. static void FindEndOfText(LPCTSTR lpszToken, int * pcbText)
  696. {
  697. TCHAR ch;
  698. int cbText;
  699. assert(lpszToken);
  700. assert(pcbText);
  701. cbText = (*pcbText) - 1;
  702. while (TRUE)
  703. {
  704. assert(cbText >= 0);
  705. ch = lpszToken[cbText];
  706. switch (ch)
  707. {
  708. case _T('\r'):
  709. case _T('\n'):
  710. case _T('\t'):
  711. case _T(' ') : cbText--; break;
  712. default : *pcbText = cbText + 1; return;
  713. }
  714. }
  715. }
  716. // Duplicate a given string.
  717. LPTSTR stringDup(LPCTSTR lpszData, int cbData)
  718. {
  719. if (lpszData == NULL)
  720. {
  721. return NULL;
  722. }
  723. LPTSTR lpszNew;
  724. if (cbData == 0)
  725. {
  726. cbData = (int) _tcslen(lpszData);
  727. }
  728. lpszNew = (LPTSTR) malloc((cbData + 1) * sizeof(TCHAR));
  729. if (lpszNew)
  730. {
  731. memcpy(lpszNew, lpszData, (cbData) * sizeof(TCHAR));
  732. lpszNew[cbData] = (TCHAR) NULL;
  733. }
  734. return lpszNew;
  735. }
  736. // private:
  737. // Parse a clear (unformatted) type node.
  738. int XMLNode::ParseClearTag(void * px, void * pa)
  739. {
  740. XML * pXML = (XML *) px;
  741. ClearTag * pClear = (ClearTag *) pa;
  742. int cbTemp = 0;
  743. LPTSTR lpszTemp;
  744. LPCTSTR lpszXML = &pXML->lpXML[pXML->nIndex];
  745. // Find the closing tag
  746. lpszTemp = (char *) _tcsstr(lpszXML, pClear->lpszClose);
  747. // Iterate through the tokens until we find the closing tag.
  748. if (lpszTemp)
  749. {
  750. // Cache the size and increment the index
  751. cbTemp = (int) (lpszTemp - lpszXML);
  752. pXML->nIndex += cbTemp;
  753. pXML->nIndex += (int) _tcslen(pClear->lpszClose);
  754. // Add the clear node to the current element
  755. addClear(stringDup(lpszXML, cbTemp), pClear->lpszOpen, pClear->lpszClose);
  756. return TRUE;
  757. }
  758. // If we failed to find the end tag
  759. pXML->error = eXMLErrorUnmatchedEndTag;
  760. return FALSE;
  761. }
  762. void XMLNode::exactMemory(XMLNodeData * d)
  763. {
  764. if (memoryIncrease == 1)
  765. {
  766. return;
  767. }
  768. d->pOrder = (int *) realloc(d->pOrder, (d->nChild + d->nAttribute + d->nText + d->nClear) * sizeof(int));
  769. d->pChild = (XMLNode *) realloc(d->pChild, d->nChild * sizeof(XMLNode));
  770. d->pAttribute = (XMLAttribute *) realloc(d->pAttribute, d->nAttribute * sizeof(XMLAttribute));
  771. d->pText = (LPCTSTR *) realloc(d->pText, d->nText * sizeof(LPTSTR));
  772. d->pClear = (XMLClear *) realloc(d->pClear, d->nClear * sizeof(XMLClear));
  773. }
  774. // private:
  775. // Recursively parse an XML element.
  776. int XMLNode::ParseXMLElement(void * pa)
  777. {
  778. XML * pXML = (XML *) pa;
  779. int cbToken;
  780. enum TokenTypeTag type;
  781. NextToken token;
  782. LPCTSTR lpszTemp = NULL;
  783. int cbTemp;
  784. int nDeclaration;
  785. LPCTSTR lpszText = NULL;
  786. XMLNode pNew;
  787. enum Status status; // inside or outside a tag
  788. enum Attrib attrib = eAttribName;
  789. assert(pXML);
  790. // If this is the first call to the function
  791. if (pXML->nFirst)
  792. {
  793. // Assume we are outside of a tag definition
  794. pXML->nFirst = FALSE;
  795. status = eOutsideTag;
  796. }
  797. else
  798. {
  799. // If this is not the first call then we should only be called when inside a tag.
  800. status = eInsideTag;
  801. }
  802. // Iterate through the tokens in the document
  803. while (TRUE)
  804. {
  805. // Obtain the next token
  806. token = GetNextToken(pXML, &cbToken, &type);
  807. if (type != eTokenError)
  808. {
  809. // Check the current status
  810. switch (status)
  811. {
  812. // If we are outside of a tag definition
  813. case eOutsideTag:
  814. // Check what type of token we obtained
  815. switch (type)
  816. {
  817. // If we have found text or quoted text
  818. case eTokenText:
  819. case eTokenQuotedText:
  820. case eTokenEquals:
  821. if (! lpszText)
  822. {
  823. lpszText = token.pStr;
  824. }
  825. break;
  826. // If we found a start tag '<' and declarations '<?'
  827. case eTokenTagStart:
  828. case eTokenDeclaration:
  829. // Cache whether this new element is a declaration or not
  830. nDeclaration = type == eTokenDeclaration;
  831. // If we have node text then add this to the element
  832. if (lpszText)
  833. {
  834. cbTemp = (int) (token.pStr - lpszText);
  835. FindEndOfText(lpszText, &cbTemp);
  836. addText(stringDup(lpszText, cbTemp));
  837. lpszText = NULL;
  838. }
  839. // Find the name of the tag
  840. token = GetNextToken(pXML, &cbToken, &type);
  841. // Return an error if we couldn't obtain the next token or
  842. // it wasnt text
  843. if (type != eTokenText)
  844. {
  845. pXML->error = eXMLErrorMissingTagName;
  846. return FALSE;
  847. }
  848. // If we found a new element which is the same as this
  849. // element then we need to pass this back to the caller..
  850. #ifdef APPROXIMATE_PARSING
  851. if (d->lpszName && myTagCompare(d->lpszName, token.pStr) == 0)
  852. {
  853. // Indicate to the caller that it needs to create a
  854. // new element.
  855. pXML->lpNewElement = token.pStr;
  856. pXML->cbNewElement = cbToken;
  857. return TRUE;
  858. }
  859. else
  860. #endif
  861. {
  862. // If the name of the new element differs from the name of
  863. // the current element we need to add the new element to
  864. // the current one and recurse
  865. pNew = addChild(stringDup(token.pStr, cbToken), nDeclaration);
  866. while (! pNew.isEmpty())
  867. {
  868. // Callself to process the new node. If we return
  869. // FALSE this means we dont have any more
  870. // processing to do...
  871. if (! pNew.ParseXMLElement(pXML))
  872. {
  873. return FALSE;
  874. }
  875. else
  876. {
  877. // If the call to recurse this function
  878. // evented in a end tag specified in XML then
  879. // we need to unwind the calls to this
  880. // function until we find the appropriate node
  881. // (the element name and end tag name must
  882. // match)
  883. if (pXML->cbEndTag)
  884. {
  885. // If we are back at the root node then we
  886. // have an unmatched end tag
  887. if (! d->lpszName)
  888. {
  889. pXML->error = eXMLErrorUnmatchedEndTag;
  890. return FALSE;
  891. }
  892. // If the end tag matches the name of this
  893. // element then we only need to unwind
  894. // once more...
  895. if (myTagCompare(d->lpszName, pXML->lpEndTag) == 0)
  896. {
  897. pXML->cbEndTag = 0;
  898. }
  899. return TRUE;
  900. }
  901. else if (pXML->cbNewElement)
  902. {
  903. // If the call indicated a new element is to
  904. // be created on THIS element.
  905. // If the name of this element matches the
  906. // name of the element we need to create
  907. // then we need to return to the caller
  908. // and let it process the element.
  909. if (myTagCompare(d->lpszName, pXML->lpNewElement) == 0)
  910. {
  911. return TRUE;
  912. }
  913. // Add the new element and recurse
  914. pNew = addChild(stringDup(pXML->lpNewElement, pXML->cbNewElement));
  915. pXML->cbNewElement = 0;
  916. }
  917. else
  918. {
  919. // If we didn't have a new element to create
  920. pNew = emptyXMLNode;
  921. }
  922. }
  923. }
  924. }
  925. break;
  926. // If we found an end tag
  927. case eTokenTagEnd:
  928. // If we have node text then add this to the element
  929. if (lpszText)
  930. {
  931. cbTemp = (int) (token.pStr - lpszText);
  932. FindEndOfText(lpszText, &cbTemp);
  933. addText(fromXMLString(lpszText, cbTemp));
  934. lpszText = NULL;
  935. }
  936. // Find the name of the end tag
  937. token = GetNextToken(pXML, &cbTemp, &type);
  938. // The end tag should be text
  939. if (type != eTokenText)
  940. {
  941. pXML->error = eXMLErrorMissingEndTagName;
  942. return FALSE;
  943. }
  944. lpszTemp = token.pStr;
  945. // After the end tag we should find a closing tag
  946. token = GetNextToken(pXML, &cbToken, &type);
  947. if (type != eTokenCloseTag)
  948. {
  949. pXML->error = eXMLErrorMissingEndTagName;
  950. return FALSE;
  951. }
  952. // We need to return to the previous caller. If the name
  953. // of the tag cannot be found we need to keep returning to
  954. // caller until we find a match
  955. if (myTagCompare(d->lpszName, lpszTemp) != 0)
  956. #ifdef STRICT_PARSING
  957. {
  958. pXML->error = eXMLErrorUnmatchedEndTag;
  959. return FALSE;
  960. }
  961. #else
  962. {
  963. pXML->error = eXMLErrorMissingEndTag;
  964. pXML->lpEndTag = lpszTemp;
  965. pXML->cbEndTag = cbTemp;
  966. }
  967. #endif
  968. // Return to the caller
  969. exactMemory(d);
  970. return TRUE;
  971. // If we found a clear (unformatted) token
  972. case eTokenClear:
  973. // If we have node text then add this to the element
  974. if (lpszText)
  975. {
  976. cbTemp = (int) (token.pStr - lpszText);
  977. FindEndOfText(lpszText, &cbTemp);
  978. addText(stringDup(lpszText, cbTemp));
  979. lpszText = NULL;
  980. }
  981. if (! ParseClearTag(pXML, token.pClr))
  982. {
  983. return FALSE;
  984. }
  985. break;
  986. // Errors...
  987. case eTokenCloseTag: /* '>' */
  988. case eTokenShortHandClose: /* '/>' */ pXML->error = eXMLErrorUnexpectedToken; return FALSE;
  989. default : break;
  990. }
  991. break;
  992. // If we are inside a tag definition we need to search for attributes
  993. case eInsideTag:
  994. // Check what part of the attribute (name, equals, value) we
  995. // are looking for.
  996. switch (attrib)
  997. {
  998. // If we are looking for a new attribute
  999. case eAttribName:
  1000. // Check what the current token type is
  1001. switch (type)
  1002. {
  1003. // If the current type is text...
  1004. // Eg. 'attribute'
  1005. case eTokenText:
  1006. // Cache the token then indicate that we are next to
  1007. // look for the equals
  1008. lpszTemp = token.pStr;
  1009. cbTemp = cbToken;
  1010. attrib = eAttribEquals;
  1011. break;
  1012. // If we found a closing tag...
  1013. // Eg. '>'
  1014. case eTokenCloseTag:
  1015. // We are now outside the tag
  1016. status = eOutsideTag;
  1017. break;
  1018. // If we found a short hand '/>' closing tag then we can
  1019. // return to the caller
  1020. case eTokenShortHandClose: exactMemory(d); return TRUE;
  1021. // Errors...
  1022. case eTokenQuotedText: /* '"SomeText"' */
  1023. case eTokenTagStart: /* '<' */
  1024. case eTokenTagEnd: /* '</' */
  1025. case eTokenEquals: /* '=' */
  1026. case eTokenDeclaration: /* '<?' */
  1027. case eTokenClear : pXML->error = eXMLErrorUnexpectedToken; return FALSE;
  1028. default : break;
  1029. }
  1030. break;
  1031. // If we are looking for an equals
  1032. case eAttribEquals:
  1033. // Check what the current token type is
  1034. switch (type)
  1035. {
  1036. // If the current type is text...
  1037. // Eg. 'Attribute AnotherAttribute'
  1038. case eTokenText:
  1039. // Add the unvalued attribute to the list
  1040. addAttribute(stringDup(lpszTemp, cbTemp), NULL);
  1041. // Cache the token then indicate. We are next to
  1042. // look for the equals attribute
  1043. lpszTemp = token.pStr;
  1044. cbTemp = cbToken;
  1045. break;
  1046. // If we found a closing tag 'Attribute >' or a short hand
  1047. // closing tag 'Attribute />'
  1048. case eTokenShortHandClose:
  1049. case eTokenCloseTag:
  1050. // If we are a declaration element '<?' then we need
  1051. // to remove extra closing '?' if it exists
  1052. if (d->isDeclaration && (lpszTemp[cbTemp - 1]) == _T('?'))
  1053. {
  1054. cbTemp--;
  1055. }
  1056. if (cbTemp)
  1057. {
  1058. // Add the unvalued attribute to the list
  1059. addAttribute(stringDup(lpszTemp, cbTemp), NULL);
  1060. }
  1061. // If this is the end of the tag then return to the caller
  1062. if (type == eTokenShortHandClose)
  1063. {
  1064. exactMemory(d);
  1065. return TRUE;
  1066. }
  1067. // We are now outside the tag
  1068. status = eOutsideTag;
  1069. break;
  1070. // If we found the equals token...
  1071. // Eg. 'Attribute ='
  1072. case eTokenEquals:
  1073. // Indicate that we next need to search for the value
  1074. // for the attribute
  1075. attrib = eAttribValue;
  1076. break;
  1077. // Errors...
  1078. case eTokenQuotedText: /* 'Attribute "InvalidAttr"'*/
  1079. case eTokenTagStart: /* 'Attribute <' */
  1080. case eTokenTagEnd: /* 'Attribute </' */
  1081. case eTokenDeclaration: /* 'Attribute <?' */
  1082. case eTokenClear : pXML->error = eXMLErrorUnexpectedToken; return FALSE;
  1083. default : break;
  1084. }
  1085. break;
  1086. // If we are looking for an attribute value
  1087. case eAttribValue:
  1088. // Check what the current token type is
  1089. switch (type)
  1090. {
  1091. // If the current type is text or quoted text...
  1092. // Eg. 'Attribute = "Value"' or 'Attribute = Value' or
  1093. // 'Attribute = 'Value''.
  1094. case eTokenText:
  1095. case eTokenQuotedText:
  1096. // If we are a declaration element '<?' then we need
  1097. // to remove extra closing '?' if it exists
  1098. if (d->isDeclaration && (token.pStr[cbToken - 1]) == _T('?'))
  1099. {
  1100. cbToken--;
  1101. }
  1102. if (cbTemp)
  1103. {
  1104. // Add the valued attribute to the list
  1105. if (type == eTokenQuotedText)
  1106. {
  1107. token.pStr++;
  1108. cbToken -= 2;
  1109. }
  1110. addAttribute(stringDup(lpszTemp, cbTemp), fromXMLString(token.pStr, cbToken));
  1111. }
  1112. // Indicate we are searching for a new attribute
  1113. attrib = eAttribName;
  1114. break;
  1115. // Errors...
  1116. case eTokenTagStart: /* 'Attr = <' */
  1117. case eTokenTagEnd: /* 'Attr = </' */
  1118. case eTokenCloseTag: /* 'Attr = >' */
  1119. case eTokenShortHandClose: /* "Attr = />" */
  1120. case eTokenEquals: /* 'Attr = =' */
  1121. case eTokenDeclaration: /* 'Attr = <?' */
  1122. case eTokenClear:
  1123. pXML->error = eXMLErrorUnexpectedToken;
  1124. return FALSE;
  1125. break;
  1126. default: break;
  1127. }
  1128. }
  1129. }
  1130. }
  1131. // If we failed to obtain the next token
  1132. else
  1133. {
  1134. return FALSE;
  1135. }
  1136. }
  1137. }
  1138. // Count the number of lines and columns in an XML string.
  1139. static void CountLinesAndColumns(LPCTSTR lpXML, int nUpto, XMLResults * pResults)
  1140. {
  1141. TCHAR ch;
  1142. int n;
  1143. assert(lpXML);
  1144. assert(pResults);
  1145. pResults->nLine = 1;
  1146. pResults->nColumn = 1;
  1147. for (n = 0; n < nUpto; n++)
  1148. {
  1149. ch = lpXML[n];
  1150. assert(ch);
  1151. if (ch == _T('\n'))
  1152. {
  1153. pResults->nLine++;
  1154. pResults->nColumn = 1;
  1155. }
  1156. else
  1157. {
  1158. pResults->nColumn++;
  1159. }
  1160. }
  1161. }
  1162. // Parse XML and return the root element.
  1163. XMLNode XMLNode::parseString(LPCTSTR lpszXML, LPCTSTR tag, XMLResults * pResults)
  1164. {
  1165. if (! lpszXML)
  1166. {
  1167. if (pResults)
  1168. {
  1169. pResults->error = eXMLErrorNoElements;
  1170. pResults->nLine = 0;
  1171. pResults->nColumn = 0;
  1172. }
  1173. return emptyXMLNode;
  1174. }
  1175. static struct ClearTag tags[] = {
  1176. {_T("<![CDATA["), _T("]]>") },
  1177. {_T("<PRE>"), _T("</PRE>") },
  1178. {_T("<Script>"), _T("</Script>")},
  1179. {_T("<!--"), _T("-->") },
  1180. {_T("<!DOCTYPE"), _T(">") },
  1181. {NULL, NULL }
  1182. };
  1183. enum XMLError error;
  1184. XMLNode xnode(NULL, NULL, FALSE);
  1185. struct XML xml = {NULL, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE, NULL};
  1186. xml.lpXML = lpszXML;
  1187. xml.pClrTags = tags;
  1188. // Create header element
  1189. xnode.ParseXMLElement(&xml);
  1190. error = xml.error;
  1191. // If an error occurred
  1192. if ((error == eXMLErrorNone) || (error == eXMLErrorMissingEndTag))
  1193. {
  1194. if (tag && _tcslen(tag))
  1195. {
  1196. XMLNode nodeTmp;
  1197. int i = 0;
  1198. while (i < xnode.nChildNode())
  1199. {
  1200. nodeTmp = xnode.getChildNode(i);
  1201. if (_tcsicmp(nodeTmp.getName(), tag) == 0)
  1202. {
  1203. break;
  1204. }
  1205. if (nodeTmp.isDeclaration())
  1206. {
  1207. xnode = nodeTmp;
  1208. i = 0;
  1209. }
  1210. else
  1211. {
  1212. i++;
  1213. }
  1214. }
  1215. if (i < 0)
  1216. {
  1217. if (pResults)
  1218. {
  1219. pResults->error = eXMLErrorTagNotFound;
  1220. pResults->nLine = 0;
  1221. pResults->nColumn = 0;
  1222. }
  1223. return emptyXMLNode;
  1224. }
  1225. xnode = nodeTmp;
  1226. }
  1227. }
  1228. else
  1229. {
  1230. // Cleanup: this will destroy all the nodes
  1231. xnode = emptyXMLNode;
  1232. }
  1233. // If we have been given somewhere to place results
  1234. if (pResults)
  1235. {
  1236. pResults->error = error;
  1237. // If we have an error
  1238. if ((error != eXMLErrorNone) && (error != eXMLErrorMissingEndTag))
  1239. {
  1240. // Find which line and column it starts on.
  1241. CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
  1242. }
  1243. }
  1244. return xnode;
  1245. }
  1246. XMLNode XMLNode::parseFile(const char * filename, LPCTSTR tag, XMLResults * pResults)
  1247. {
  1248. FILE * f = fopen(filename, "rb");
  1249. if (f == NULL)
  1250. {
  1251. if (pResults)
  1252. {
  1253. pResults->error = eXMLErrorFileNotFound;
  1254. pResults->nLine = 0;
  1255. pResults->nColumn = 0;
  1256. }
  1257. return emptyXMLNode;
  1258. }
  1259. fseek(f, 0, SEEK_END);
  1260. int l = ftell(f);
  1261. fseek(f, 0, SEEK_SET);
  1262. char * buf = (char *) malloc(l + 1);
  1263. fread(buf, l, 1, f);
  1264. fclose(f);
  1265. buf[l] = 0;
  1266. #ifdef WIN32
  1267. #ifdef _UNICODE
  1268. if (! IsTextUnicode(buf, mmin(l, 10000), NULL))
  1269. {
  1270. LPTSTR b2 = (LPTSTR) malloc(l * 2 + 2);
  1271. MultiByteToWideChar(CP_ACP, // code page
  1272. MB_PRECOMPOSED, // character-type options
  1273. buf, // string to map
  1274. l, // number of bytes in string
  1275. b2, // wide-character buffer
  1276. l * 2 + 2); // size of buffer
  1277. free(buf);
  1278. buf = (char *) b2;
  1279. }
  1280. #else
  1281. if (IsTextUnicode(buf, mmin(l, 10000), NULL))
  1282. {
  1283. l >>= 1;
  1284. LPTSTR b2 = (LPTSTR) malloc(l + 2);
  1285. WideCharToMultiByte(CP_ACP, // code page
  1286. 0, // performance and mapping flags
  1287. (const WCHAR *) buf, // wide-character string
  1288. l, // number of chars in string
  1289. b2, // buffer for new string
  1290. l + 2, // size of buffer
  1291. NULL, // default for unmappable chars
  1292. NULL // set when default char used
  1293. );
  1294. free(buf);
  1295. buf = (char *) b2;
  1296. }
  1297. #endif
  1298. #endif
  1299. XMLNode x = parseString((LPTSTR) buf, tag, pResults);
  1300. free(buf);
  1301. return x;
  1302. }
  1303. XMLNode XMLNode::openFileHelper(const char * lpszXML, LPCTSTR tag)
  1304. {
  1305. XMLResults pResults;
  1306. XMLNode xnode = XMLNode::parseFile(lpszXML, tag, &pResults);
  1307. if (pResults.error != eXMLErrorNone)
  1308. {
  1309. printf(
  1310. "XML Parsing error inside file '%s'.\n"
  1311. #ifdef _UNICODE
  1312. "Error: %S\n"
  1313. #else
  1314. "Error: %s\n"
  1315. #endif
  1316. "At line %i, column %i.\n",
  1317. lpszXML, XMLNode::getError(pResults.error), pResults.nLine, pResults.nColumn);
  1318. if (pResults.error == eXMLErrorTagNotFound)
  1319. {
  1320. #ifdef _UNICODE
  1321. printf("Tag is '%S'.\n", tag);
  1322. #else
  1323. printf("Tag is '%s'.\n", tag);
  1324. #endif
  1325. }
  1326. exit(255);
  1327. }
  1328. return xnode;
  1329. }
  1330. XMLNodeContents XMLNode::enumContents(int i)
  1331. {
  1332. XMLNodeContents c;
  1333. if (! d)
  1334. {
  1335. c.type = eNodeNULL;
  1336. return c;
  1337. }
  1338. c.type = (XMLElementType) (d->pOrder[i] & 3);
  1339. i = (d->pOrder[i]) >> 2;
  1340. switch (c.type)
  1341. {
  1342. case eNodeChild : c.child = d->pChild[i]; break;
  1343. case eNodeAttribute: c.attrib = d->pAttribute[i]; break;
  1344. case eNodeText : c.text = d->pText[i]; break;
  1345. case eNodeClear : c.clear = d->pClear[i]; break;
  1346. default : break;
  1347. }
  1348. return c;
  1349. }
  1350. // private:
  1351. void * XMLNode::enumContent(XMLNodeData * pEntry, int i, XMLElementType * nodeType)
  1352. {
  1353. XMLElementType j = (XMLElementType) (pEntry->pOrder[i] & 3);
  1354. *nodeType = j;
  1355. i = (pEntry->pOrder[i]) >> 2;
  1356. switch (j)
  1357. {
  1358. case eNodeChild : return pEntry->pChild[i].d;
  1359. case eNodeAttribute: return pEntry->pAttribute + i;
  1360. case eNodeText : return (void *) (pEntry->pText[i]);
  1361. case eNodeClear : return pEntry->pClear + i;
  1362. default : break;
  1363. }
  1364. return NULL;
  1365. }
  1366. // private:
  1367. int XMLNode::nElement(XMLNodeData * pEntry)
  1368. {
  1369. return pEntry->nChild + pEntry->nText + pEntry->nClear + pEntry->nAttribute;
  1370. }
  1371. static inline void charmemset(LPTSTR dest, TCHAR c, int l)
  1372. {
  1373. while (l--)
  1374. {
  1375. *(dest++) = c;
  1376. }
  1377. }
  1378. // private:
  1379. // Creates an user friendly XML string from a given element with
  1380. // appropriate white space and carriage returns.
  1381. //
  1382. // This recurses through all subnodes then adds contents of the nodes to the
  1383. // string.
  1384. int XMLNode::CreateXMLStringR(XMLNodeData * pEntry, LPTSTR lpszMarker, int nFormat)
  1385. {
  1386. int nResult = 0;
  1387. int cb;
  1388. int cbElement;
  1389. int nIndex;
  1390. int nChildFormat = -1;
  1391. int bHasChildren = FALSE;
  1392. int i;
  1393. XMLAttribute * pAttr;
  1394. assert(pEntry);
  1395. #define LENSTR(lpsz) (lpsz ? _tcslen(lpsz) : 0)
  1396. // If the element has no name then assume this is the head node.
  1397. cbElement = (int) LENSTR(pEntry->lpszName);
  1398. if (cbElement)
  1399. {
  1400. // "<elementname "
  1401. cb = nFormat == -1 ? 0 : nFormat;
  1402. if (lpszMarker)
  1403. {
  1404. if (cb)
  1405. {
  1406. charmemset(lpszMarker, INDENTCHAR, sizeof(TCHAR) * cb);
  1407. }
  1408. nResult = cb;
  1409. lpszMarker[nResult++] = _T('<');
  1410. if (pEntry->isDeclaration)
  1411. {
  1412. lpszMarker[nResult++] = _T('?');
  1413. }
  1414. _tcscpy(&lpszMarker[nResult], pEntry->lpszName);
  1415. nResult += cbElement;
  1416. lpszMarker[nResult++] = _T(' ');
  1417. }
  1418. else
  1419. {
  1420. nResult += cbElement + 2 + cb;
  1421. if (pEntry->isDeclaration)
  1422. {
  1423. nResult++;
  1424. }
  1425. }
  1426. // Enumerate attributes and add them to the string
  1427. nIndex = pEntry->nAttribute;
  1428. pAttr = pEntry->pAttribute;
  1429. for (i = 0; i < nIndex; i++)
  1430. {
  1431. // "Attrib
  1432. cb = (int) LENSTR(pAttr->lpszName);
  1433. if (cb)
  1434. {
  1435. if (lpszMarker)
  1436. {
  1437. _tcscpy(&lpszMarker[nResult], pAttr->lpszName);
  1438. }
  1439. nResult += cb;
  1440. // "Attrib=Value "
  1441. cb = (int) lengthXMLString(pAttr->lpszValue);
  1442. if (lpszMarker)
  1443. {
  1444. lpszMarker[nResult] = _T('=');
  1445. lpszMarker[nResult + 1] = _T('"');
  1446. if (cb)
  1447. {
  1448. toXMLString(&lpszMarker[nResult + 2], pAttr->lpszValue);
  1449. }
  1450. lpszMarker[nResult + cb + 2] = _T('"');
  1451. }
  1452. nResult += cb + 3;
  1453. if (lpszMarker)
  1454. {
  1455. lpszMarker[nResult] = _T(' ');
  1456. }
  1457. nResult++;
  1458. }
  1459. pAttr++;
  1460. }
  1461. bHasChildren = (pEntry->nAttribute != nElement(pEntry));
  1462. if (pEntry->isDeclaration)
  1463. {
  1464. if (lpszMarker)
  1465. {
  1466. lpszMarker[nResult - 1] = _T('?');
  1467. lpszMarker[nResult] = _T('>');
  1468. }
  1469. nResult++;
  1470. if (nFormat != -1)
  1471. {
  1472. if (lpszMarker)
  1473. {
  1474. lpszMarker[nResult] = _T('\n');
  1475. }
  1476. nResult++;
  1477. }
  1478. }
  1479. else
  1480. // If there are child nodes we need to terminate the start tag
  1481. if (bHasChildren)
  1482. {
  1483. if (lpszMarker)
  1484. {
  1485. lpszMarker[nResult - 1] = _T('>');
  1486. }
  1487. if (nFormat != -1)
  1488. {
  1489. if (lpszMarker)
  1490. {
  1491. lpszMarker[nResult] = _T('\n');
  1492. }
  1493. nResult++;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. nResult--;
  1499. }
  1500. }
  1501. // Calculate the child format for when we recurse. This is used to
  1502. // determine the number of spaces used for prefixes.
  1503. if (nFormat != -1)
  1504. {
  1505. if (cbElement && (! pEntry->isDeclaration))
  1506. {
  1507. nChildFormat = nFormat + 1;
  1508. }
  1509. else
  1510. {
  1511. nChildFormat = nFormat;
  1512. }
  1513. }
  1514. // Enumerate through remaining children
  1515. nIndex = nElement(pEntry);
  1516. XMLElementType nodeType;
  1517. void * pChild;
  1518. for (i = 0; i < nIndex; i++)
  1519. {
  1520. pChild = enumContent(pEntry, i, &nodeType);
  1521. switch (nodeType)
  1522. {
  1523. // Text nodes
  1524. case eNodeText:
  1525. // "Text"
  1526. cb = (int) lengthXMLString((LPTSTR) pChild);
  1527. if (cb)
  1528. {
  1529. if (nFormat != -1)
  1530. {
  1531. if (lpszMarker)
  1532. {
  1533. charmemset(&lpszMarker[nResult], INDENTCHAR, sizeof(TCHAR) * (nFormat + 1));
  1534. toXMLString(&lpszMarker[nResult + nFormat + 1], (LPTSTR) pChild);
  1535. lpszMarker[nResult + nFormat + 1 + cb] = _T('\n');
  1536. }
  1537. nResult += cb + nFormat + 2;
  1538. }
  1539. else
  1540. {
  1541. if (lpszMarker)
  1542. {
  1543. toXMLString(&lpszMarker[nResult], (LPTSTR) pChild);
  1544. }
  1545. nResult += cb;
  1546. }
  1547. }
  1548. break;
  1549. // Clear type nodes
  1550. case eNodeClear:
  1551. // "OpenTag"
  1552. cb = (int) LENSTR(((XMLClear *) pChild)->lpszOpenTag);
  1553. if (cb)
  1554. {
  1555. if (nFormat != -1)
  1556. {
  1557. if (lpszMarker)
  1558. {
  1559. charmemset(&lpszMarker[nResult], INDENTCHAR, sizeof(TCHAR) * (nFormat + 1));
  1560. _tcscpy(&lpszMarker[nResult + nFormat + 1], ((XMLClear *) pChild)->lpszOpenTag);
  1561. }
  1562. nResult += cb + nFormat + 1;
  1563. }
  1564. else
  1565. {
  1566. if (lpszMarker)
  1567. {
  1568. _tcscpy(&lpszMarker[nResult], ((XMLClear *) pChild)->lpszOpenTag);
  1569. }
  1570. nResult += cb;
  1571. }
  1572. }
  1573. // "OpenTag Value"
  1574. cb = (int) LENSTR(((XMLClear *) pChild)->lpszValue);
  1575. if (cb)
  1576. {
  1577. if (lpszMarker)
  1578. {
  1579. _tcscpy(&lpszMarker[nResult], ((XMLClear *) pChild)->lpszValue);
  1580. }
  1581. nResult += cb;
  1582. }
  1583. // "OpenTag Value CloseTag"
  1584. cb = (int) LENSTR(((XMLClear *) pChild)->lpszCloseTag);
  1585. if (cb)
  1586. {
  1587. if (lpszMarker)
  1588. {
  1589. _tcscpy(&lpszMarker[nResult], ((XMLClear *) pChild)->lpszCloseTag);
  1590. }
  1591. nResult += cb;
  1592. }
  1593. if (nFormat != -1)
  1594. {
  1595. if (lpszMarker)
  1596. {
  1597. lpszMarker[nResult] = _T('\n');
  1598. }
  1599. nResult++;
  1600. }
  1601. break;
  1602. // Element nodes
  1603. case eNodeChild:
  1604. // Recursively add child nodes
  1605. nResult += CreateXMLStringR((XMLNodeData *) pChild, lpszMarker ? lpszMarker + nResult : 0, nChildFormat);
  1606. break;
  1607. default: break;
  1608. }
  1609. }
  1610. if ((cbElement) && (! pEntry->isDeclaration))
  1611. {
  1612. // If we have child entries we need to use long XML notation for
  1613. // closing the element - "<elementname>blah blah blah</elementname>"
  1614. if (bHasChildren)
  1615. {
  1616. // "</elementname>\0"
  1617. if (lpszMarker)
  1618. {
  1619. if (nFormat != -1)
  1620. {
  1621. if (nFormat)
  1622. {
  1623. charmemset(&lpszMarker[nResult], INDENTCHAR, sizeof(TCHAR) * nFormat);
  1624. nResult += nFormat;
  1625. }
  1626. }
  1627. _tcscpy(&lpszMarker[nResult], _T("</"));
  1628. nResult += 2;
  1629. _tcscpy(&lpszMarker[nResult], pEntry->lpszName);
  1630. nResult += cbElement;
  1631. if (nFormat == -1)
  1632. {
  1633. _tcscpy(&lpszMarker[nResult], _T(">"));
  1634. nResult++;
  1635. }
  1636. else
  1637. {
  1638. _tcscpy(&lpszMarker[nResult], _T(">\n"));
  1639. nResult += 2;
  1640. }
  1641. }
  1642. else
  1643. {
  1644. if (nFormat != -1)
  1645. {
  1646. nResult += cbElement + 4 + nFormat;
  1647. }
  1648. else
  1649. {
  1650. nResult += cbElement + 3;
  1651. }
  1652. }
  1653. }
  1654. else
  1655. {
  1656. // If there are no children we can use shorthand XML notation -
  1657. // "<elementname/>"
  1658. // "/>\0"
  1659. if (lpszMarker)
  1660. {
  1661. if (nFormat == -1)
  1662. {
  1663. _tcscpy(&lpszMarker[nResult], _T("/>"));
  1664. nResult += 2;
  1665. }
  1666. else
  1667. {
  1668. _tcscpy(&lpszMarker[nResult], _T("/>\n"));
  1669. nResult += 3;
  1670. }
  1671. }
  1672. else
  1673. {
  1674. nResult += nFormat == -1 ? 2 : 3;
  1675. }
  1676. }
  1677. }
  1678. return nResult;
  1679. }
  1680. #undef LENSTR
  1681. // Create an XML string from the head element.
  1682. // @param XMLElement * pHead - head element
  1683. // @param int nFormat - 0 if no formatting is required
  1684. // otherwise nonzero for formatted text
  1685. // with carriage returns and indentation.
  1686. // @param int *pnSize - [out] pointer to the size of the
  1687. // returned string not including the
  1688. // NULL terminator.
  1689. //
  1690. // @return LPTSTR - Allocated XML string, you must free
  1691. // this with free().
  1692. LPTSTR XMLNode::createXMLString(int nFormat, int * pnSize)
  1693. {
  1694. if (! d)
  1695. {
  1696. if (pnSize)
  1697. {
  1698. *pnSize = 0;
  1699. }
  1700. return NULL;
  1701. }
  1702. LPTSTR lpszResult = NULL;
  1703. int cbStr;
  1704. // Recursively Calculate the size of the XML string
  1705. nFormat = nFormat ? 0 : -1;
  1706. cbStr = CreateXMLStringR(d, 0, nFormat);
  1707. assert(cbStr);
  1708. // Alllocate memory for the XML string + the NULL terminator and
  1709. // create the recursively XML string.
  1710. lpszResult = (LPTSTR) malloc((cbStr + 1) * sizeof(TCHAR));
  1711. CreateXMLStringR(d, lpszResult, nFormat);
  1712. if (pnSize)
  1713. {
  1714. *pnSize = cbStr;
  1715. }
  1716. return lpszResult;
  1717. }
  1718. XMLNode::~XMLNode()
  1719. {
  1720. destroyCurrentBuffer(d);
  1721. }
  1722. void XMLNode::destroyCurrentBuffer(XMLNodeData * d)
  1723. {
  1724. if (! d)
  1725. {
  1726. return;
  1727. }
  1728. (d->ref_count)--;
  1729. if (d->ref_count == 0)
  1730. {
  1731. int i = 0;
  1732. if (d->pParent)
  1733. {
  1734. XMLNode * pa = d->pParent->d->pChild;
  1735. while (((void *) (pa[i].d)) != ((void *) d))
  1736. {
  1737. i++;
  1738. }
  1739. d->pParent->d->nChild--;
  1740. memmove(pa + i, pa + i + 1, (d->pParent->d->nChild - i) * sizeof(XMLNode));
  1741. removeOrderElement(d->pParent->d, eNodeChild, i);
  1742. }
  1743. for (i = 0; i < d->nChild; i++)
  1744. {
  1745. d->pChild[i].d->pParent = NULL;
  1746. destroyCurrentBuffer(d->pChild[i].d);
  1747. }
  1748. free(d->pChild);
  1749. for (i = 0; i < d->nText; i++)
  1750. {
  1751. free((void *) d->pText[i]);
  1752. }
  1753. free(d->pText);
  1754. for (i = 0; i < d->nClear; i++)
  1755. {
  1756. free((void *) d->pClear[i].lpszValue);
  1757. }
  1758. free(d->pClear);
  1759. for (i = 0; i < d->nAttribute; i++)
  1760. {
  1761. free((void *) d->pAttribute[i].lpszName);
  1762. if (d->pAttribute[i].lpszValue)
  1763. {
  1764. free((void *) d->pAttribute[i].lpszValue);
  1765. }
  1766. }
  1767. free(d->pAttribute);
  1768. free(d->pOrder);
  1769. free((void *) d->lpszName);
  1770. free(d);
  1771. }
  1772. }
  1773. XMLNode & XMLNode::operator=(const XMLNode & A)
  1774. {
  1775. // shallow copy
  1776. if (this != &A)
  1777. {
  1778. destroyCurrentBuffer(d);
  1779. d = A.d;
  1780. if (d)
  1781. {
  1782. (d->ref_count)++;
  1783. }
  1784. }
  1785. return *this;
  1786. }
  1787. XMLNode::XMLNode(const XMLNode & A)
  1788. {
  1789. // shallow copy
  1790. d = A.d;
  1791. if (d)
  1792. {
  1793. (d->ref_count)++;
  1794. }
  1795. }
  1796. int XMLNode::nChildNode(LPCTSTR name)
  1797. {
  1798. if (! d)
  1799. {
  1800. return 0;
  1801. }
  1802. int i, j = 0, n = d->nChild;
  1803. XMLNode * pc = d->pChild;
  1804. for (i = 0; i < n; i++)
  1805. {
  1806. if (_tcsicmp(pc->d->lpszName, name) == 0)
  1807. {
  1808. j++;
  1809. }
  1810. pc++;
  1811. }
  1812. return j;
  1813. }
  1814. XMLNode XMLNode::getChildNode(LPCTSTR name, int * j)
  1815. {
  1816. char temp[1024];
  1817. char * p = NULL;
  1818. if (! d)
  1819. {
  1820. return emptyXMLNode;
  1821. }
  1822. int i = 0, n = d->nChild;
  1823. if (j)
  1824. {
  1825. i = *j;
  1826. }
  1827. XMLNode * pc = d->pChild + i;
  1828. for (; i < n; i++)
  1829. {
  1830. p = (char *) strstr(pc->d->lpszName, ":");
  1831. if (p)
  1832. {
  1833. memset(temp, 0, sizeof(temp));
  1834. strcpy(temp, p + 1);
  1835. }
  1836. if (p)
  1837. {
  1838. if (_tcsicmp(temp, name) == 0)
  1839. {
  1840. if (j)
  1841. {
  1842. *j = i + 1;
  1843. }
  1844. return *pc;
  1845. }
  1846. pc++;
  1847. }
  1848. else
  1849. {
  1850. if (_tcsicmp(pc->d->lpszName, name) == 0)
  1851. {
  1852. if (j)
  1853. {
  1854. *j = i + 1;
  1855. }
  1856. return *pc;
  1857. }
  1858. pc++;
  1859. }
  1860. }
  1861. return emptyXMLNode;
  1862. }
  1863. XMLNode XMLNode::getChildNode(LPCTSTR name, int j)
  1864. {
  1865. if (! d)
  1866. {
  1867. return emptyXMLNode;
  1868. }
  1869. int i = 0;
  1870. while (j-- > 0)
  1871. {
  1872. getChildNode(name, &i);
  1873. }
  1874. return getChildNode(name, &i);
  1875. }
  1876. // Find an attribute on an node.
  1877. LPCTSTR XMLNode::getAttribute(LPCTSTR lpszAttrib, int * j)
  1878. {
  1879. if (! d)
  1880. {
  1881. return NULL;
  1882. }
  1883. int i = 0, n = d->nAttribute;
  1884. if (j)
  1885. {
  1886. i = *j;
  1887. }
  1888. XMLAttribute * pAttr = d->pAttribute + i;
  1889. for (; i < n; i++)
  1890. {
  1891. if (_tcsicmp(pAttr->lpszName, lpszAttrib) == 0)
  1892. {
  1893. if (j)
  1894. {
  1895. *j = i + 1;
  1896. }
  1897. return pAttr->lpszValue;
  1898. }
  1899. pAttr++;
  1900. }
  1901. return NULL;
  1902. }
  1903. char XMLNode::isAttributeSet(LPCTSTR lpszAttrib)
  1904. {
  1905. if (! d)
  1906. {
  1907. return FALSE;
  1908. }
  1909. int i, n = d->nAttribute;
  1910. XMLAttribute * pAttr = d->pAttribute;
  1911. for (i = 0; i < n; i++)
  1912. {
  1913. if (_tcsicmp(pAttr->lpszName, lpszAttrib) == 0)
  1914. {
  1915. return TRUE;
  1916. }
  1917. pAttr++;
  1918. }
  1919. return FALSE;
  1920. }
  1921. LPCTSTR XMLNode::getAttribute(LPCTSTR name, int j)
  1922. {
  1923. if (! d)
  1924. {
  1925. return NULL;
  1926. }
  1927. int i = 0;
  1928. while (j-- > 0)
  1929. {
  1930. getAttribute(name, &i);
  1931. }
  1932. return getAttribute(name, &i);
  1933. }
  1934. LPCTSTR XMLNode::getName()
  1935. {
  1936. if (! d)
  1937. {
  1938. return NULL;
  1939. }
  1940. return d->lpszName;
  1941. }
  1942. int XMLNode::nText()
  1943. {
  1944. if (! d)
  1945. {
  1946. return 0;
  1947. }
  1948. return d->nText;
  1949. }
  1950. int XMLNode::nChildNode()
  1951. {
  1952. if (! d)
  1953. {
  1954. return 0;
  1955. }
  1956. return d->nChild;
  1957. }
  1958. int XMLNode::nAttribute()
  1959. {
  1960. if (! d)
  1961. {
  1962. return 0;
  1963. }
  1964. return d->nAttribute;
  1965. }
  1966. int XMLNode::nClear()
  1967. {
  1968. if (! d)
  1969. {
  1970. return 0;
  1971. }
  1972. return d->nClear;
  1973. }
  1974. XMLClear XMLNode::getClear(int i)
  1975. {
  1976. if (! d)
  1977. {
  1978. return emptyXMLClear;
  1979. }
  1980. if (i >= d->nClear)
  1981. {
  1982. return emptyXMLClear;
  1983. }
  1984. return d->pClear[i];
  1985. }
  1986. XMLAttribute XMLNode::getAttribute(int i)
  1987. {
  1988. if (! d)
  1989. {
  1990. return emptyXMLAttribute;
  1991. }
  1992. if (i >= d->nAttribute)
  1993. {
  1994. return emptyXMLAttribute;
  1995. }
  1996. return d->pAttribute[i];
  1997. }
  1998. LPCTSTR XMLNode::getText(int i)
  1999. {
  2000. if (! d)
  2001. {
  2002. return NULL;
  2003. }
  2004. if (i >= d->nText)
  2005. {
  2006. return NULL;
  2007. }
  2008. return d->pText[i];
  2009. }
  2010. XMLNode XMLNode::getChildNode(int i)
  2011. {
  2012. if (! d)
  2013. {
  2014. return emptyXMLNode;
  2015. }
  2016. if (i >= d->nChild)
  2017. {
  2018. return emptyXMLNode;
  2019. }
  2020. return d->pChild[i];
  2021. }
  2022. char XMLNode::isDeclaration()
  2023. {
  2024. if (! d)
  2025. {
  2026. return 0;
  2027. }
  2028. return d->isDeclaration;
  2029. }
  2030. char XMLNode::isEmpty()
  2031. {
  2032. return (d == NULL);
  2033. }
  2034. int XMLNode::nElement()
  2035. {
  2036. if (! d)
  2037. {
  2038. return 0;
  2039. }
  2040. return d->nChild + d->nText + d->nClear + d->nAttribute;
  2041. }