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.

239 lines
8.4 KiB

7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
7 years ago
​
  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. {
  73. LPCTSTR lpszOpenTag;
  74. LPCTSTR lpszValue;
  75. LPCTSTR lpszCloseTag;
  76. } XMLClear;
  77. // Structure for XML attribute.
  78. typedef struct
  79. {
  80. LPCTSTR lpszName;
  81. LPCTSTR lpszValue;
  82. } XMLAttribute;
  83. struct XMLNodeContents;
  84. typedef struct XMLNode
  85. {
  86. // friend class XMLNode;
  87. protected:
  88. typedef struct // to allow shallow copy and "intelligent/smart" pointers (automatic delete):
  89. {
  90. LPCTSTR lpszName; // Element name (=NULL if root)
  91. int nChild, // Num of child nodes
  92. nText, // Num of text fields
  93. nClear, // Num of Clear fields (comments)
  94. nAttribute, // Num of attributes
  95. isDeclaration; // Whether node is an XML declaration - '<?xml ?>'
  96. XMLNode * pParent; // Pointer to parent element (=NULL if root)
  97. XMLNode * pChild; // Array of child nodes
  98. LPCTSTR * pText; // Array of text fields
  99. XMLClear * pClear; // Array of clear fields
  100. XMLAttribute * pAttribute; // Array of attributes
  101. int * pOrder; // order in which the child_nodes,text_fields,clear_fields and
  102. int ref_count;
  103. } XMLNodeData;
  104. XMLNodeData * d;
  105. // protected constructor: use one of these four methods to get your first instance of XMLNode:
  106. // - parseString
  107. // - parseFile
  108. // - openFileHelper
  109. // - createXMLTopNode
  110. XMLNode(XMLNode * pParent, LPCTSTR lpszName, int isDeclaration);
  111. public:
  112. // You must create your first instance of XMLNode with these 3 parse functions:
  113. // (see complete explanation of parameters below)
  114. static XMLNode parseString(LPCTSTR lpszXML, LPCTSTR tag = NULL, XMLResults * pResults = NULL);
  115. static XMLNode parseFile(const char * lpszXML, LPCTSTR tag = NULL, XMLResults * pResults = NULL);
  116. static XMLNode openFileHelper(const char * lpszXML, LPCTSTR tag);
  117. // The tag parameter should be the name of the first tag inside the XML file.
  118. // If the tag parameter is omitted, the 3 functions return a node that represents
  119. // the head of the xml document including the declaration term (<? ... ?>).
  120. // If the XML document is corrupted:
  121. // - The "openFileHelper" method will stop execution and display an error message.
  122. // - The 2 other methods will initialize the "pResults" variable with some information that
  123. // can be used to trace the error.
  124. // you can have a detailed explanation of the parsing error with this function:
  125. static LPCTSTR getError(XMLError error);
  126. LPCTSTR getName(); // name of the node
  127. LPCTSTR getText(int i = 0); // return ith text field
  128. int nText(); // nbr of text field
  129. XMLNode getChildNode(int i); // return ith child node
  130. XMLNode getChildNode(LPCTSTR name, int i); // return ith child node with specific name
  131. // (return an empty node if failing)
  132. XMLNode getChildNode(LPCTSTR name, int * i = NULL); // return next child node with specific name
  133. // (return an empty node if failing)
  134. int nChildNode(LPCTSTR name); // return the number of child node with specific name
  135. int nChildNode(); // nbr of child node
  136. XMLAttribute getAttribute(int i); // return ith attribute
  137. char isAttributeSet(LPCTSTR name); // test if an attribute with a specific name is given
  138. LPCTSTR getAttribute(LPCTSTR name, int i); // return ith attribute content with specific name
  139. // (return a NULL if failing)
  140. LPCTSTR getAttribute(LPCTSTR name, int * i = NULL); // return next attribute content with specific name
  141. // (return a NULL if failing)
  142. int nAttribute(); // nbr of attribute
  143. XMLClear getClear(int i); // return ith clear field (comment)
  144. int nClear(); // nbr of clear field
  145. LPTSTR createXMLString(int nFormat, int * pnSize = NULL); // create XML string starting from current XMLNode
  146. XMLNodeContents enumContents(int i); // enumerate all the different contents (child,text,
  147. // clear,attribute) of the current XMLNode. The order
  148. // is reflecting the order of the original file/string
  149. int nElement(); // nbr of different contents for current node
  150. char isEmpty(); // is this node Empty?
  151. char isDeclaration();
  152. // to allow shallow copy:
  153. ~XMLNode();
  154. XMLNode(const XMLNode & A);
  155. XMLNode & operator=(const XMLNode & A);
  156. static void destroyCurrentBuffer(XMLNodeData * d);
  157. XMLNode() :
  158. d(NULL)
  159. {
  160. }
  161. static XMLNode emptyXMLNode;
  162. static XMLClear emptyXMLClear;
  163. static XMLAttribute emptyXMLAttribute;
  164. // The following functions allows you to create from scratch a XMLNode structure
  165. // The strings given as parameters for these methods will be free'd by the XMLNode class:
  166. static XMLNode createXMLTopNode();
  167. XMLNode addChild(LPCTSTR lpszName, int isDeclaration = FALSE);
  168. XMLAttribute * addAttribute(LPCTSTR lpszName, LPCTSTR lpszValuev);
  169. LPCTSTR addText(LPCTSTR lpszValue);
  170. XMLClear * addClear(LPCTSTR lpszValue, LPCTSTR lpszOpen, LPCTSTR lpszClose);
  171. private:
  172. // these are functions used internally (don't bother about them):
  173. int ParseClearTag(void * pXML, void * pClear);
  174. int ParseXMLElement(void * pXML);
  175. void addToOrder(int index, int type);
  176. static int CreateXMLStringR(XMLNodeData * pEntry, LPTSTR lpszMarker, int nFormat);
  177. static void * enumContent(XMLNodeData * pEntry, int i, XMLElementType * nodeType);
  178. static int nElement(XMLNodeData * pEntry);
  179. static void removeOrderElement(XMLNodeData * d, XMLElementType t, int index);
  180. static void exactMemory(XMLNodeData * d);
  181. } XMLNode;
  182. // This structure is given by the function "enumContents".
  183. typedef struct XMLNodeContents
  184. {
  185. // This dictates what's the content of the XMLNodeContent
  186. enum XMLElementType type;
  187. // should be an union to access the appropriate data.
  188. // compiler does not allow union of object with constructor... too bad.
  189. XMLNode child;
  190. XMLAttribute attrib;
  191. LPCTSTR text;
  192. XMLClear clear;
  193. } XMLNodeContents;
  194. // The 2 following functions are processing strings so that all the characters
  195. // &,",',<,> are replaced by their XML equivalent: &amp;, &quot;, &apos;, &lt;, &gt;.
  196. // The second function ("toXMLStringFast") allows you to re-use the same output
  197. // buffer for all the conversions so that only a few memory allocations are performed.
  198. // If the output buffer is too small to contain the resulting string, it will
  199. // be enlarged. These 2 functions are useful when creating from scratch an
  200. // XML file using printf.
  201. LPTSTR toXMLString(LPCTSTR source);
  202. LPTSTR toXMLStringFast(LPTSTR * destBuffer, int * destSz, LPCTSTR source);
  203. // you should not use this one:
  204. LPTSTR toXMLString(LPTSTR dest, LPCTSTR source);
  205. // duplicate (copy in a new allocated buffer) the source string
  206. LPTSTR stringDup(LPCTSTR source, int cbData = 0);
  207. #endif