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.

1820 lines
56 KiB

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