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.

229 lines
9.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __INCLUDE_XML_NODE__
  2. #define __INCLUDE_XML_NODE__
  3. #include <stdlib.h>
  4. #ifdef WIN32
  5. #include <tchar.h>
  6. #endif
  7. // Some common types for char set portable code
  8. #ifdef _UNICODE
  9. #ifndef LPCTSTR
  10. #define LPCTSTR const unsigned short *
  11. #endif /* LPCTSTR */
  12. #ifndef LPTSTR
  13. #define LPTSTR unsigned short*
  14. #endif /* LPTSTR */
  15. #ifndef TCHAR
  16. #define TCHAR unsigned short
  17. #endif /* TCHAR */
  18. #else
  19. #ifndef LPCTSTR
  20. #define LPCTSTR const char *
  21. #endif /* LPCTSTR */
  22. #ifndef LPTSTR
  23. #define LPTSTR char *
  24. #endif /* LPTSTR */
  25. #ifndef TCHAR
  26. #define TCHAR char
  27. #endif /* TCHAR */
  28. #endif
  29. #ifndef FALSE
  30. #define FALSE 0
  31. #endif /* FALSE */
  32. #ifndef TRUE
  33. #define TRUE 1
  34. #endif /* TRUE */
  35. #ifndef WIN32
  36. #define _T(c) c
  37. #endif
  38. // Enumeration for XML parse errors.
  39. typedef enum XMLError
  40. {
  41. eXMLErrorNone = 0,
  42. eXMLErrorMissingEndTag,
  43. eXMLErrorEmpty,
  44. eXMLErrorFirstNotStartTag,
  45. eXMLErrorMissingTagName,
  46. eXMLErrorMissingEndTagName,
  47. eXMLErrorNoMatchingQuote,
  48. eXMLErrorUnmatchedEndTag,
  49. eXMLErrorUnexpectedToken,
  50. eXMLErrorInvalidTag,
  51. eXMLErrorNoElements,
  52. eXMLErrorFileNotFound,
  53. eXMLErrorTagNotFound
  54. } XMLError;
  55. // Enumeration used to manage type of data. Use in conjonction with structure XMLNodeContents
  56. typedef enum XMLElementType
  57. {
  58. eNodeChild=0,
  59. eNodeAttribute=1,
  60. eNodeText=2,
  61. eNodeClear=3,
  62. eNodeNULL=4
  63. } XMLElementType;
  64. // Structure used to obtain error details if the parse fails.
  65. typedef struct XMLResults
  66. {
  67. enum XMLError error;
  68. int nLine,nColumn;
  69. } XMLResults;
  70. // Structure for XML clear (unformatted) node (usually comments)
  71. typedef struct {
  72. LPCTSTR lpszOpenTag; LPCTSTR lpszValue; LPCTSTR lpszCloseTag;
  73. } XMLClear;
  74. // Structure for XML attribute.
  75. typedef struct {
  76. LPCTSTR lpszName; LPCTSTR lpszValue;
  77. } XMLAttribute;
  78. struct XMLNodeContents;
  79. typedef struct XMLNode
  80. {
  81. // friend class XMLNode;
  82. protected:
  83. typedef struct // to allow shallow copy and "intelligent/smart" pointers (automatic delete):
  84. {
  85. LPCTSTR lpszName; // Element name (=NULL if root)
  86. int nChild, // Num of child nodes
  87. nText, // Num of text fields
  88. nClear, // Num of Clear fields (comments)
  89. nAttribute, // Num of attributes
  90. isDeclaration; // Whether node is an XML declaration - '<?xml ?>'
  91. XMLNode *pParent; // Pointer to parent element (=NULL if root)
  92. XMLNode *pChild; // Array of child nodes
  93. LPCTSTR *pText; // Array of text fields
  94. XMLClear *pClear; // Array of clear fields
  95. XMLAttribute *pAttribute; // Array of attributes
  96. int *pOrder; // order in which the child_nodes,text_fields,clear_fields and
  97. int ref_count;
  98. } XMLNodeData;
  99. XMLNodeData *d;
  100. // protected constructor: use one of these four methods to get your first instance of XMLNode:
  101. // - parseString
  102. // - parseFile
  103. // - openFileHelper
  104. // - createXMLTopNode
  105. XMLNode(XMLNode *pParent, LPCTSTR lpszName, int isDeclaration);
  106. public:
  107. // You must create your first instance of XMLNode with these 3 parse functions:
  108. // (see complete explanation of parameters below)
  109. static XMLNode parseString (LPCTSTR lpszXML, LPCTSTR tag=NULL, XMLResults *pResults=NULL);
  110. static XMLNode parseFile (const char *lpszXML, LPCTSTR tag=NULL, XMLResults *pResults=NULL);
  111. static XMLNode openFileHelper(const char *lpszXML, LPCTSTR tag);
  112. // The tag parameter should be the name of the first tag inside the XML file.
  113. // If the tag parameter is omitted, the 3 functions return a node that represents
  114. // the head of the xml document including the declaration term (<? ... ?>).
  115. // If the XML document is corrupted:
  116. // - The "openFileHelper" method will stop execution and display an error message.
  117. // - The 2 other methods will initialize the "pResults" variable with some information that
  118. // can be used to trace the error.
  119. // you can have a detailed explanation of the parsing error with this function:
  120. static LPCTSTR getError(XMLError error);
  121. LPCTSTR getName(); // name of the node
  122. LPCTSTR getText(int i=0); // return ith text field
  123. int nText(); // nbr of text field
  124. XMLNode getChildNode(int i); // return ith child node
  125. XMLNode getChildNode(LPCTSTR name, int i); // return ith child node with specific name
  126. // (return an empty node if failing)
  127. XMLNode getChildNode(LPCTSTR name, int *i=NULL); // return next child node with specific name
  128. // (return an empty node if failing)
  129. int nChildNode(LPCTSTR name); // return the number of child node with specific name
  130. int nChildNode(); // nbr of child node
  131. XMLAttribute getAttribute(int i); // return ith attribute
  132. char isAttributeSet(LPCTSTR name); // test if an attribute with a specific name is given
  133. LPCTSTR getAttribute(LPCTSTR name, int i); // return ith attribute content with specific name
  134. // (return a NULL if failing)
  135. LPCTSTR getAttribute(LPCTSTR name, int *i=NULL); // return next attribute content with specific name
  136. // (return a NULL if failing)
  137. int nAttribute(); // nbr of attribute
  138. XMLClear getClear(int i); // return ith clear field (comment)
  139. int nClear(); // nbr of clear field
  140. LPTSTR createXMLString(int nFormat, int *pnSize=NULL); // create XML string starting from current XMLNode
  141. XMLNodeContents enumContents(int i); // enumerate all the different contents (child,text,
  142. // clear,attribute) of the current XMLNode. The order
  143. // is reflecting the order of the original file/string
  144. int nElement(); // nbr of different contents for current node
  145. char isEmpty(); // is this node Empty?
  146. char isDeclaration();
  147. // to allow shallow copy:
  148. ~XMLNode();
  149. XMLNode(const XMLNode &A);
  150. XMLNode& operator=( const XMLNode& A );
  151. static void destroyCurrentBuffer(XMLNodeData *d);
  152. XMLNode(): d(NULL){};
  153. static XMLNode emptyXMLNode;
  154. static XMLClear emptyXMLClear;
  155. static XMLAttribute emptyXMLAttribute;
  156. // The following functions allows you to create from scratch a XMLNode structure
  157. // The strings given as parameters for these methods will be free'd by the XMLNode class:
  158. static XMLNode createXMLTopNode();
  159. XMLNode addChild(LPCTSTR lpszName, int isDeclaration=FALSE);
  160. XMLAttribute *addAttribute(LPCTSTR lpszName, LPCTSTR lpszValuev);
  161. LPCTSTR addText(LPCTSTR lpszValue);
  162. XMLClear *addClear(LPCTSTR lpszValue, LPCTSTR lpszOpen, LPCTSTR lpszClose);
  163. private:
  164. // these are functions used internally (don't bother about them):
  165. int ParseClearTag(void *pXML, void *pClear);
  166. int ParseXMLElement(void *pXML);
  167. void addToOrder(int index, int type);
  168. static int CreateXMLStringR(XMLNodeData *pEntry, LPTSTR lpszMarker, int nFormat);
  169. static void *enumContent(XMLNodeData *pEntry,int i, XMLElementType *nodeType);
  170. static int nElement(XMLNodeData *pEntry);
  171. static void removeOrderElement(XMLNodeData *d, XMLElementType t, int index);
  172. static void exactMemory(XMLNodeData *d);
  173. } XMLNode;
  174. // This structure is given by the function "enumContents".
  175. typedef struct XMLNodeContents
  176. {
  177. // This dictates what's the content of the XMLNodeContent
  178. enum XMLElementType type;
  179. // should be an union to access the appropriate data.
  180. // compiler does not allow union of object with constructor... too bad.
  181. XMLNode child;
  182. XMLAttribute attrib;
  183. LPCTSTR text;
  184. XMLClear clear;
  185. } XMLNodeContents;
  186. // The 2 following functions are processing strings so that all the characters
  187. // &,",',<,> are replaced by their XML equivalent: &amp;, &quot;, &apos;, &lt;, &gt;.
  188. // The second function ("toXMLStringFast") allows you to re-use the same output
  189. // buffer for all the conversions so that only a few memory allocations are performed.
  190. // If the output buffer is too small to contain the resulting string, it will
  191. // be enlarged. These 2 functions are useful when creating from scratch an
  192. // XML file using printf.
  193. LPTSTR toXMLString(LPCTSTR source);
  194. LPTSTR toXMLStringFast(LPTSTR *destBuffer,int *destSz, LPCTSTR source);
  195. // you should not use this one:
  196. LPTSTR toXMLString(LPTSTR dest,LPCTSTR source);
  197. // duplicate (copy in a new allocated buffer) the source string
  198. LPTSTR stringDup(LPCTSTR source, int cbData=0);
  199. #endif