File: cpl_minixml.h

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (223 lines) | stat: -rw-r--r-- 8,170 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**********************************************************************
 * $Id$
 *
 * Project:  CPL - Common Portability Library
 * Purpose:  Declarations for MiniXML Handler.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 **********************************************************************
 * Copyright (c) 2001, Frank Warmerdam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#ifndef CPL_MINIXML_H_INCLUDED
#define CPL_MINIXML_H_INCLUDED

#include "cpl_port.h"

/**
 * \file cpl_minixml.h
 *
 * Definitions for CPL mini XML Parser/Serializer.
 */

CPL_C_START

/** XML node type */
typedef enum
{
    /*! Node is an element */ CXT_Element = 0,
    /*! Node is a raw text value */ CXT_Text = 1,
    /*! Node is attribute */ CXT_Attribute = 2,
    /*! Node is an XML comment. */ CXT_Comment = 3,
    /*! Node is a special literal */ CXT_Literal = 4
} CPLXMLNodeType;

/*! @cond Doxygen_Suppress */
typedef struct CPLXMLNode CPLXMLNode;
/*! @endcond */

/**
 * Document node structure.
 *
 * This C structure is used to hold a single text fragment representing a
 * component of the document when parsed.   It should be allocated with the
 * appropriate CPL function, and freed with CPLDestroyXMLNode().  The structure
 * contents should not normally be altered by application code, but may be
 * freely examined by application code.
 *
 * Using the psChild and psNext pointers, a hierarchical tree structure
 * for a document can be represented as a tree of CPLXMLNode structures.
 */
struct CPLXMLNode
{
    /**
     * \brief Node type
     *
     * One of CXT_Element, CXT_Text, CXT_Attribute, CXT_Comment,
     * or CXT_Literal.
     */
    CPLXMLNodeType eType;

    /**
     * \brief Node value
     *
     * For CXT_Element this is the name of the element, without the angle
     * brackets.  Note there is a single CXT_Element even when the document
     * contains a start and end element tag.  The node represents the pair.
     * All text or other elements between the start and end tag will appear
     * as children nodes of this CXT_Element node.
     *
     * For CXT_Attribute the pszValue is the attribute name.  The value of
     * the attribute will be a CXT_Text child.
     *
     * For CXT_Text this is the text itself (value of an attribute, or a
     * text fragment between an element start and end tags.
     *
     * For CXT_Literal it is all the literal text.  Currently this is just
     * used for !DOCTYPE lines, and the value would be the entire line.
     *
     * For CXT_Comment the value is all the literal text within the comment,
     * but not including the comment start/end indicators ("<--" and "-->").
     */
    char *pszValue;

    /**
     * \brief Next sibling.
     *
     * Pointer to next sibling, that is the next node appearing after this
     * one that has the same parent as this node.  NULL if this node is the
     * last child of the parent element.
     */
    struct CPLXMLNode *psNext;

    /**
     * \brief Child node.
     *
     * Pointer to first child node, if any.  Only CXT_Element and CXT_Attribute
     * nodes should have children.  For CXT_Attribute it should be a single
     * CXT_Text value node, while CXT_Element can have any kind of child.
     * The full list of children for a node are identified by walking the
     * psNext's starting with the psChild node.
     */

    struct CPLXMLNode *psChild;
};

CPLXMLNode CPL_DLL *CPLParseXMLString(const char *);
void CPL_DLL CPLDestroyXMLNode(CPLXMLNode *);
CPLXMLNode CPL_DLL *CPLGetXMLNode(CPLXMLNode *poRoot, const char *pszPath);
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
/*! @cond Doxygen_Suppress */
extern "C++"
{
    inline const CPLXMLNode *CPLGetXMLNode(const CPLXMLNode *poRoot,
                                           const char *pszPath)
    {
        return const_cast<const CPLXMLNode *>(
            CPLGetXMLNode(const_cast<CPLXMLNode *>(poRoot), pszPath));
    }
}
/*! @endcond */
#endif
CPLXMLNode CPL_DLL *CPLSearchXMLNode(CPLXMLNode *poRoot, const char *pszTarget);
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
/*! @cond Doxygen_Suppress */
extern "C++"
{
    inline const CPLXMLNode *CPLSearchXMLNode(const CPLXMLNode *poRoot,
                                              const char *pszTarget)
    {
        return const_cast<const CPLXMLNode *>(
            CPLSearchXMLNode(const_cast<CPLXMLNode *>(poRoot), pszTarget));
    }
}
/*! @endcond */
#endif
const char CPL_DLL *CPLGetXMLValue(const CPLXMLNode *poRoot,
                                   const char *pszPath, const char *pszDefault);
CPLXMLNode CPL_DLL *CPLCreateXMLNode(CPLXMLNode *poParent, CPLXMLNodeType eType,
                                     const char *pszText);
char CPL_DLL *CPLSerializeXMLTree(const CPLXMLNode *psNode);
void CPL_DLL CPLAddXMLChild(CPLXMLNode *psParent, CPLXMLNode *psChild);
int CPL_DLL CPLRemoveXMLChild(CPLXMLNode *psParent, CPLXMLNode *psChild);
void CPL_DLL CPLAddXMLSibling(CPLXMLNode *psOlderSibling,
                              CPLXMLNode *psNewSibling);
CPLXMLNode CPL_DLL *CPLCreateXMLElementAndValue(CPLXMLNode *psParent,
                                                const char *pszName,
                                                const char *pszValue);
void CPL_DLL CPLAddXMLAttributeAndValue(CPLXMLNode *psParent,
                                        const char *pszName,
                                        const char *pszValue);
CPLXMLNode CPL_DLL *CPLCloneXMLTree(const CPLXMLNode *psTree);
int CPL_DLL CPLSetXMLValue(CPLXMLNode *psRoot, const char *pszPath,
                           const char *pszValue);
void CPL_DLL CPLStripXMLNamespace(CPLXMLNode *psRoot, const char *pszNameSpace,
                                  int bRecurse);
void CPL_DLL CPLCleanXMLElementName(char *);

CPLXMLNode CPL_DLL *CPLParseXMLFile(const char *pszFilename);
int CPL_DLL CPLSerializeXMLTreeToFile(const CPLXMLNode *psTree,
                                      const char *pszFilename);

CPL_C_END

#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)

extern "C++"
{
#ifndef DOXYGEN_SKIP
#include <memory>
#endif

    /*! @cond Doxygen_Suppress */
    struct CPL_DLL CPLXMLTreeCloserDeleter
    {
        void operator()(CPLXMLNode *psNode) const
        {
            CPLDestroyXMLNode(psNode);
        }
    };
    /*! @endcond */

    /** Manage a tree of XML nodes so that all nodes are freed when the instance
     * goes out of scope.  Only the top level node should be in a
     * CPLXMLTreeCloser.
     */
    class CPL_DLL CPLXMLTreeCloser
        : public std::unique_ptr<CPLXMLNode, CPLXMLTreeCloserDeleter>
    {
      public:
        /** Constructor */
        explicit CPLXMLTreeCloser(CPLXMLNode *data)
            : std::unique_ptr<CPLXMLNode, CPLXMLTreeCloserDeleter>(data)
        {
        }

        /** Returns a pointer to the document (root) element
         * @return the node pointer */
        CPLXMLNode *getDocumentElement();
    };

}  // extern "C++"

#endif /* __cplusplus */

#endif /* CPL_MINIXML_H_INCLUDED */