gooderp18绿色标准版
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.

219 line
8.3KB

  1. /**********************************************************************
  2. * $Id: cpl_minixml.h df507edcc67aa14ada1432baf516f49e6557fb06 2018-04-10 15:26:13 +0200 Even Rouault $
  3. *
  4. * Project: CPL - Common Portability Library
  5. * Purpose: Declarations for MiniXML Handler.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. **********************************************************************
  9. * Copyright (c) 2001, Frank Warmerdam
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. * DEALINGS IN THE SOFTWARE.
  28. ****************************************************************************/
  29. #ifndef CPL_MINIXML_H_INCLUDED
  30. #define CPL_MINIXML_H_INCLUDED
  31. #include "cpl_port.h"
  32. /**
  33. * \file cpl_minixml.h
  34. *
  35. * Definitions for CPL mini XML Parser/Serializer.
  36. */
  37. CPL_C_START
  38. /** XML node type */
  39. typedef enum
  40. {
  41. /*! Node is an element */ CXT_Element = 0,
  42. /*! Node is a raw text value */ CXT_Text = 1,
  43. /*! Node is attribute */ CXT_Attribute = 2,
  44. /*! Node is an XML comment. */ CXT_Comment = 3,
  45. /*! Node is a special literal */ CXT_Literal = 4
  46. } CPLXMLNodeType;
  47. /**
  48. * Document node structure.
  49. *
  50. * This C structure is used to hold a single text fragment representing a
  51. * component of the document when parsed. It should be allocated with the
  52. * appropriate CPL function, and freed with CPLDestroyXMLNode(). The structure
  53. * contents should not normally be altered by application code, but may be
  54. * freely examined by application code.
  55. *
  56. * Using the psChild and psNext pointers, a hierarchical tree structure
  57. * for a document can be represented as a tree of CPLXMLNode structures.
  58. */
  59. typedef struct CPLXMLNode
  60. {
  61. /**
  62. * \brief Node type
  63. *
  64. * One of CXT_Element, CXT_Text, CXT_Attribute, CXT_Comment,
  65. * or CXT_Literal.
  66. */
  67. CPLXMLNodeType eType;
  68. /**
  69. * \brief Node value
  70. *
  71. * For CXT_Element this is the name of the element, without the angle
  72. * brackets. Note there is a single CXT_Element even when the document
  73. * contains a start and end element tag. The node represents the pair.
  74. * All text or other elements between the start and end tag will appear
  75. * as children nodes of this CXT_Element node.
  76. *
  77. * For CXT_Attribute the pszValue is the attribute name. The value of
  78. * the attribute will be a CXT_Text child.
  79. *
  80. * For CXT_Text this is the text itself (value of an attribute, or a
  81. * text fragment between an element start and end tags.
  82. *
  83. * For CXT_Literal it is all the literal text. Currently this is just
  84. * used for !DOCTYPE lines, and the value would be the entire line.
  85. *
  86. * For CXT_Comment the value is all the literal text within the comment,
  87. * but not including the comment start/end indicators ("<--" and "-->").
  88. */
  89. char *pszValue;
  90. /**
  91. * \brief Next sibling.
  92. *
  93. * Pointer to next sibling, that is the next node appearing after this
  94. * one that has the same parent as this node. NULL if this node is the
  95. * last child of the parent element.
  96. */
  97. struct CPLXMLNode *psNext;
  98. /**
  99. * \brief Child node.
  100. *
  101. * Pointer to first child node, if any. Only CXT_Element and CXT_Attribute
  102. * nodes should have children. For CXT_Attribute it should be a single
  103. * CXT_Text value node, while CXT_Element can have any kind of child.
  104. * The full list of children for a node are identified by walking the
  105. * psNext's starting with the psChild node.
  106. */
  107. struct CPLXMLNode *psChild;
  108. } CPLXMLNode;
  109. CPLXMLNode CPL_DLL *CPLParseXMLString( const char * );
  110. void CPL_DLL CPLDestroyXMLNode( CPLXMLNode * );
  111. CPLXMLNode CPL_DLL *CPLGetXMLNode( CPLXMLNode *poRoot,
  112. const char *pszPath );
  113. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  114. /*! @cond Doxygen_Suppress */
  115. extern "C++"
  116. {
  117. inline const CPLXMLNode *CPLGetXMLNode( const CPLXMLNode *poRoot,
  118. const char *pszPath ) {
  119. return const_cast<const CPLXMLNode*>(CPLGetXMLNode(
  120. const_cast<CPLXMLNode*>(poRoot), pszPath));
  121. }
  122. }
  123. /*! @endcond */
  124. #endif
  125. CPLXMLNode CPL_DLL *CPLSearchXMLNode( CPLXMLNode *poRoot,
  126. const char *pszTarget );
  127. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  128. /*! @cond Doxygen_Suppress */
  129. extern "C++"
  130. {
  131. inline const CPLXMLNode *CPLSearchXMLNode( const CPLXMLNode *poRoot,
  132. const char *pszTarget ) {
  133. return const_cast<const CPLXMLNode*>(CPLSearchXMLNode(
  134. const_cast<CPLXMLNode*>(poRoot), pszTarget));
  135. }
  136. }
  137. /*! @endcond */
  138. #endif
  139. const char CPL_DLL *CPLGetXMLValue( const CPLXMLNode *poRoot,
  140. const char *pszPath,
  141. const char *pszDefault );
  142. CPLXMLNode CPL_DLL *CPLCreateXMLNode( CPLXMLNode *poParent,
  143. CPLXMLNodeType eType,
  144. const char *pszText );
  145. char CPL_DLL *CPLSerializeXMLTree( const CPLXMLNode *psNode );
  146. void CPL_DLL CPLAddXMLChild( CPLXMLNode *psParent,
  147. CPLXMLNode *psChild );
  148. int CPL_DLL CPLRemoveXMLChild( CPLXMLNode *psParent,
  149. CPLXMLNode *psChild );
  150. void CPL_DLL CPLAddXMLSibling( CPLXMLNode *psOlderSibling,
  151. CPLXMLNode *psNewSibling );
  152. CPLXMLNode CPL_DLL *CPLCreateXMLElementAndValue( CPLXMLNode *psParent,
  153. const char *pszName,
  154. const char *pszValue );
  155. void CPL_DLL CPLAddXMLAttributeAndValue( CPLXMLNode *psParent,
  156. const char *pszName,
  157. const char *pszValue );
  158. CPLXMLNode CPL_DLL *CPLCloneXMLTree( const CPLXMLNode *psTree );
  159. int CPL_DLL CPLSetXMLValue( CPLXMLNode *psRoot, const char *pszPath,
  160. const char *pszValue );
  161. void CPL_DLL CPLStripXMLNamespace( CPLXMLNode *psRoot,
  162. const char *pszNameSpace,
  163. int bRecurse );
  164. void CPL_DLL CPLCleanXMLElementName( char * );
  165. CPLXMLNode CPL_DLL *CPLParseXMLFile( const char *pszFilename );
  166. int CPL_DLL CPLSerializeXMLTreeToFile( const CPLXMLNode *psTree,
  167. const char *pszFilename );
  168. CPL_C_END
  169. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  170. extern "C++"
  171. {
  172. #ifndef DOXYGEN_SKIP
  173. #include <memory>
  174. #endif
  175. /*! @cond Doxygen_Suppress */
  176. struct CPLXMLTreeCloserDeleter
  177. {
  178. void operator()(CPLXMLNode* psNode) const { CPLDestroyXMLNode(psNode); }
  179. };
  180. /*! @endcond */
  181. /** Manage a tree of XML nodes so that all nodes are freed when the instance goes
  182. * out of scope. Only the top level node should be in a CPLXMLTreeCloser.
  183. */
  184. class CPLXMLTreeCloser: public std::unique_ptr<CPLXMLNode, CPLXMLTreeCloserDeleter>
  185. {
  186. public:
  187. /** Constructor */
  188. explicit CPLXMLTreeCloser(CPLXMLNode* data):
  189. std::unique_ptr<CPLXMLNode, CPLXMLTreeCloserDeleter>(data) {}
  190. /** Returns a pointer to the document (root) element
  191. * @return the node pointer */
  192. CPLXMLNode* getDocumentElement();
  193. };
  194. } // extern "C++"
  195. #endif /* __cplusplus */
  196. #endif /* CPL_MINIXML_H_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1