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.

1822 lines
56 KiB

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