2 |
andreas |
1 |
/*
|
|
|
2 |
* Header file for Mini-XML, a small XML file parsing library.
|
|
|
3 |
*
|
|
|
4 |
* https://www.msweet.org/mxml
|
|
|
5 |
*
|
|
|
6 |
* Copyright © 2003-2019 by Michael R Sweet.
|
|
|
7 |
*
|
|
|
8 |
* Licensed under Apache License v2.0. See the file "LICENSE" for more
|
|
|
9 |
* information.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/*
|
|
|
13 |
* Prevent multiple inclusion...
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
#ifndef _mxml_h_
|
|
|
17 |
# define _mxml_h_
|
|
|
18 |
|
|
|
19 |
/*
|
|
|
20 |
* Include necessary headers...
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
# include <stdio.h>
|
|
|
24 |
# include <stdlib.h>
|
|
|
25 |
# include <string.h>
|
|
|
26 |
# include <ctype.h>
|
|
|
27 |
# include <errno.h>
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
/*
|
|
|
31 |
* Constants...
|
|
|
32 |
*/
|
|
|
33 |
|
|
|
34 |
# define MXML_MAJOR_VERSION 3 /* Major version number */
|
|
|
35 |
# define MXML_MINOR_VERSION 1 /* Minor version number */
|
|
|
36 |
|
|
|
37 |
# define MXML_TAB 8 /* Tabs every N columns */
|
|
|
38 |
|
|
|
39 |
# define MXML_NO_CALLBACK 0 /* Don't use a type callback */
|
|
|
40 |
# define MXML_INTEGER_CALLBACK mxml_integer_cb
|
|
|
41 |
/* Treat all data as integers */
|
|
|
42 |
# define MXML_OPAQUE_CALLBACK mxml_opaque_cb
|
|
|
43 |
/* Treat all data as opaque */
|
|
|
44 |
# define MXML_REAL_CALLBACK mxml_real_cb
|
|
|
45 |
/* Treat all data as real numbers */
|
|
|
46 |
# define MXML_TEXT_CALLBACK 0 /* Treat all data as text */
|
|
|
47 |
# define MXML_IGNORE_CALLBACK mxml_ignore_cb
|
|
|
48 |
/* Ignore all non-element content */
|
|
|
49 |
|
|
|
50 |
# define MXML_NO_PARENT 0 /* No parent for the node */
|
|
|
51 |
|
|
|
52 |
# define MXML_DESCEND 1 /* Descend when finding/walking */
|
|
|
53 |
# define MXML_NO_DESCEND 0 /* Don't descend when finding/walking */
|
|
|
54 |
# define MXML_DESCEND_FIRST -1 /* Descend for first find */
|
|
|
55 |
|
|
|
56 |
# define MXML_WS_BEFORE_OPEN 0 /* Callback for before open tag */
|
|
|
57 |
# define MXML_WS_AFTER_OPEN 1 /* Callback for after open tag */
|
|
|
58 |
# define MXML_WS_BEFORE_CLOSE 2 /* Callback for before close tag */
|
|
|
59 |
# define MXML_WS_AFTER_CLOSE 3 /* Callback for after close tag */
|
|
|
60 |
|
|
|
61 |
# define MXML_ADD_BEFORE 0 /* Add node before specified node */
|
|
|
62 |
# define MXML_ADD_AFTER 1 /* Add node after specified node */
|
|
|
63 |
# define MXML_ADD_TO_PARENT NULL /* Add node relative to parent */
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
/*
|
|
|
67 |
* Data types...
|
|
|
68 |
*/
|
|
|
69 |
|
|
|
70 |
typedef enum mxml_sax_event_e /**** SAX event type. ****/
|
|
|
71 |
{
|
|
|
72 |
MXML_SAX_CDATA, /* CDATA node */
|
|
|
73 |
MXML_SAX_COMMENT, /* Comment node */
|
|
|
74 |
MXML_SAX_DATA, /* Data node */
|
|
|
75 |
MXML_SAX_DIRECTIVE, /* Processing directive node */
|
|
|
76 |
MXML_SAX_ELEMENT_CLOSE, /* Element closed */
|
|
|
77 |
MXML_SAX_ELEMENT_OPEN /* Element opened */
|
|
|
78 |
} mxml_sax_event_t;
|
|
|
79 |
|
|
|
80 |
typedef enum mxml_type_e /**** The XML node type. ****/
|
|
|
81 |
{
|
|
|
82 |
MXML_IGNORE = -1, /* Ignore/throw away node @since Mini-XML 2.3@ */
|
|
|
83 |
MXML_ELEMENT, /* XML element with attributes */
|
|
|
84 |
MXML_INTEGER, /* Integer value */
|
|
|
85 |
MXML_OPAQUE, /* Opaque string */
|
|
|
86 |
MXML_REAL, /* Real value */
|
|
|
87 |
MXML_TEXT, /* Text fragment */
|
|
|
88 |
MXML_CUSTOM /* Custom data @since Mini-XML 2.1@ */
|
|
|
89 |
} mxml_type_t;
|
|
|
90 |
|
|
|
91 |
typedef void (*mxml_custom_destroy_cb_t)(void *);
|
|
|
92 |
/**** Custom data destructor ****/
|
|
|
93 |
|
|
|
94 |
typedef void (*mxml_error_cb_t)(const char *);
|
|
|
95 |
/**** Error callback function ****/
|
|
|
96 |
|
|
|
97 |
typedef struct _mxml_node_s mxml_node_t; /**** An XML node. ****/
|
|
|
98 |
|
|
|
99 |
typedef struct _mxml_index_s mxml_index_t;
|
|
|
100 |
/**** An XML node index. ****/
|
|
|
101 |
|
|
|
102 |
typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *);
|
|
|
103 |
/**** Custom data load callback function ****/
|
|
|
104 |
|
|
|
105 |
typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *);
|
|
|
106 |
/**** Custom data save callback function ****/
|
|
|
107 |
|
|
|
108 |
typedef int (*mxml_entity_cb_t)(const char *);
|
|
|
109 |
/**** Entity callback function */
|
|
|
110 |
|
|
|
111 |
typedef mxml_type_t (*mxml_load_cb_t)(mxml_node_t *);
|
|
|
112 |
/**** Load callback function ****/
|
|
|
113 |
|
|
|
114 |
typedef const char *(*mxml_save_cb_t)(mxml_node_t *, int);
|
|
|
115 |
/**** Save callback function ****/
|
|
|
116 |
|
|
|
117 |
typedef void (*mxml_sax_cb_t)(mxml_node_t *, mxml_sax_event_t, void *);
|
|
|
118 |
/**** SAX callback function ****/
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
/*
|
|
|
122 |
* C++ support...
|
|
|
123 |
*/
|
|
|
124 |
|
|
|
125 |
# ifdef __cplusplus
|
|
|
126 |
extern "C" {
|
|
|
127 |
# endif /* __cplusplus */
|
|
|
128 |
|
|
|
129 |
/*
|
|
|
130 |
* Prototypes...
|
|
|
131 |
*/
|
|
|
132 |
|
|
|
133 |
extern void mxmlAdd(mxml_node_t *parent, int where,
|
|
|
134 |
mxml_node_t *child, mxml_node_t *node);
|
|
|
135 |
extern void mxmlDelete(mxml_node_t *node);
|
|
|
136 |
extern void mxmlElementDeleteAttr(mxml_node_t *node,
|
|
|
137 |
const char *name);
|
|
|
138 |
extern const char *mxmlElementGetAttr(mxml_node_t *node, const char *name);
|
|
|
139 |
extern const char *mxmlElementGetAttrByIndex(mxml_node_t *node, int idx, const char **name);
|
|
|
140 |
extern int mxmlElementGetAttrCount(mxml_node_t *node);
|
|
|
141 |
extern void mxmlElementSetAttr(mxml_node_t *node, const char *name,
|
|
|
142 |
const char *value);
|
|
|
143 |
extern void mxmlElementSetAttrf(mxml_node_t *node, const char *name,
|
|
|
144 |
const char *format, ...)
|
|
|
145 |
# ifdef __GNUC__
|
|
|
146 |
__attribute__ ((__format__ (__printf__, 3, 4)))
|
|
|
147 |
# endif /* __GNUC__ */
|
|
|
148 |
;
|
|
|
149 |
extern int mxmlEntityAddCallback(mxml_entity_cb_t cb);
|
|
|
150 |
extern const char *mxmlEntityGetName(int val);
|
|
|
151 |
extern int mxmlEntityGetValue(const char *name);
|
|
|
152 |
extern void mxmlEntityRemoveCallback(mxml_entity_cb_t cb);
|
|
|
153 |
extern mxml_node_t *mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
|
|
|
154 |
const char *element, const char *attr,
|
|
|
155 |
const char *value, int descend);
|
|
|
156 |
extern mxml_node_t *mxmlFindPath(mxml_node_t *node, const char *path);
|
|
|
157 |
extern const char *mxmlGetCDATA(mxml_node_t *node);
|
|
|
158 |
extern const void *mxmlGetCustom(mxml_node_t *node);
|
|
|
159 |
extern const char *mxmlGetElement(mxml_node_t *node);
|
|
|
160 |
extern mxml_node_t *mxmlGetFirstChild(mxml_node_t *node);
|
|
|
161 |
extern int mxmlGetInteger(mxml_node_t *node);
|
|
|
162 |
extern mxml_node_t *mxmlGetLastChild(mxml_node_t *node);
|
|
|
163 |
extern mxml_node_t *mxmlGetNextSibling(mxml_node_t *node);
|
|
|
164 |
extern const char *mxmlGetOpaque(mxml_node_t *node);
|
|
|
165 |
extern mxml_node_t *mxmlGetParent(mxml_node_t *node);
|
|
|
166 |
extern mxml_node_t *mxmlGetPrevSibling(mxml_node_t *node);
|
|
|
167 |
extern double mxmlGetReal(mxml_node_t *node);
|
|
|
168 |
extern int mxmlGetRefCount(mxml_node_t *node);
|
|
|
169 |
extern const char *mxmlGetText(mxml_node_t *node, int *whitespace);
|
|
|
170 |
extern mxml_type_t mxmlGetType(mxml_node_t *node);
|
|
|
171 |
extern void *mxmlGetUserData(mxml_node_t *node);
|
|
|
172 |
extern void mxmlIndexDelete(mxml_index_t *ind);
|
|
|
173 |
extern mxml_node_t *mxmlIndexEnum(mxml_index_t *ind);
|
|
|
174 |
extern mxml_node_t *mxmlIndexFind(mxml_index_t *ind,
|
|
|
175 |
const char *element,
|
|
|
176 |
const char *value);
|
|
|
177 |
extern int mxmlIndexGetCount(mxml_index_t *ind);
|
|
|
178 |
extern mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element,
|
|
|
179 |
const char *attr);
|
|
|
180 |
extern mxml_node_t *mxmlIndexReset(mxml_index_t *ind);
|
|
|
181 |
extern mxml_node_t *mxmlLoadFd(mxml_node_t *top, int fd,
|
|
|
182 |
mxml_type_t (*cb)(mxml_node_t *));
|
|
|
183 |
extern mxml_node_t *mxmlLoadFile(mxml_node_t *top, FILE *fp,
|
|
|
184 |
mxml_type_t (*cb)(mxml_node_t *));
|
|
|
185 |
extern mxml_node_t *mxmlLoadString(mxml_node_t *top, const char *s,
|
|
|
186 |
mxml_type_t (*cb)(mxml_node_t *));
|
|
|
187 |
extern mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string);
|
|
|
188 |
extern mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data,
|
|
|
189 |
mxml_custom_destroy_cb_t destroy);
|
|
|
190 |
extern mxml_node_t *mxmlNewElement(mxml_node_t *parent, const char *name);
|
|
|
191 |
extern mxml_node_t *mxmlNewInteger(mxml_node_t *parent, int integer);
|
|
|
192 |
extern mxml_node_t *mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
|
|
|
193 |
extern mxml_node_t *mxmlNewOpaquef(mxml_node_t *parent, const char *format, ...)
|
|
|
194 |
# ifdef __GNUC__
|
|
|
195 |
__attribute__ ((__format__ (__printf__, 2, 3)))
|
|
|
196 |
# endif /* __GNUC__ */
|
|
|
197 |
;
|
|
|
198 |
extern mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real);
|
|
|
199 |
extern mxml_node_t *mxmlNewText(mxml_node_t *parent, int whitespace, const char *string);
|
|
|
200 |
extern mxml_node_t *mxmlNewTextf(mxml_node_t *parent, int whitespace, const char *format, ...)
|
|
|
201 |
# ifdef __GNUC__
|
|
|
202 |
__attribute__ ((__format__ (__printf__, 3, 4)))
|
|
|
203 |
# endif /* __GNUC__ */
|
|
|
204 |
;
|
|
|
205 |
extern mxml_node_t *mxmlNewXML(const char *version);
|
|
|
206 |
extern int mxmlRelease(mxml_node_t *node);
|
|
|
207 |
extern void mxmlRemove(mxml_node_t *node);
|
|
|
208 |
extern int mxmlRetain(mxml_node_t *node);
|
|
|
209 |
extern char *mxmlSaveAllocString(mxml_node_t *node,
|
|
|
210 |
mxml_save_cb_t cb);
|
|
|
211 |
extern int mxmlSaveFd(mxml_node_t *node, int fd,
|
|
|
212 |
mxml_save_cb_t cb);
|
|
|
213 |
extern int mxmlSaveFile(mxml_node_t *node, FILE *fp,
|
|
|
214 |
mxml_save_cb_t cb);
|
|
|
215 |
extern int mxmlSaveString(mxml_node_t *node, char *buffer,
|
|
|
216 |
int bufsize, mxml_save_cb_t cb);
|
|
|
217 |
extern mxml_node_t *mxmlSAXLoadFd(mxml_node_t *top, int fd,
|
|
|
218 |
mxml_type_t (*cb)(mxml_node_t *),
|
|
|
219 |
mxml_sax_cb_t sax, void *sax_data);
|
|
|
220 |
extern mxml_node_t *mxmlSAXLoadFile(mxml_node_t *top, FILE *fp,
|
|
|
221 |
mxml_type_t (*cb)(mxml_node_t *),
|
|
|
222 |
mxml_sax_cb_t sax, void *sax_data);
|
|
|
223 |
extern mxml_node_t *mxmlSAXLoadString(mxml_node_t *top, const char *s,
|
|
|
224 |
mxml_type_t (*cb)(mxml_node_t *),
|
|
|
225 |
mxml_sax_cb_t sax, void *sax_data);
|
|
|
226 |
extern int mxmlSetCDATA(mxml_node_t *node, const char *data);
|
|
|
227 |
extern int mxmlSetCustom(mxml_node_t *node, void *data,
|
|
|
228 |
mxml_custom_destroy_cb_t destroy);
|
|
|
229 |
extern void mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
|
|
|
230 |
mxml_custom_save_cb_t save);
|
|
|
231 |
extern int mxmlSetElement(mxml_node_t *node, const char *name);
|
|
|
232 |
extern void mxmlSetErrorCallback(mxml_error_cb_t cb);
|
|
|
233 |
extern int mxmlSetInteger(mxml_node_t *node, int integer);
|
|
|
234 |
extern int mxmlSetOpaque(mxml_node_t *node, const char *opaque);
|
|
|
235 |
extern int mxmlSetOpaquef(mxml_node_t *node, const char *format, ...)
|
|
|
236 |
# ifdef __GNUC__
|
|
|
237 |
__attribute__ ((__format__ (__printf__, 2, 3)))
|
|
|
238 |
# endif /* __GNUC__ */
|
|
|
239 |
;
|
|
|
240 |
extern int mxmlSetReal(mxml_node_t *node, double real);
|
|
|
241 |
extern int mxmlSetText(mxml_node_t *node, int whitespace,
|
|
|
242 |
const char *string);
|
|
|
243 |
extern int mxmlSetTextf(mxml_node_t *node, int whitespace,
|
|
|
244 |
const char *format, ...)
|
|
|
245 |
# ifdef __GNUC__
|
|
|
246 |
__attribute__ ((__format__ (__printf__, 3, 4)))
|
|
|
247 |
# endif /* __GNUC__ */
|
|
|
248 |
;
|
|
|
249 |
extern int mxmlSetUserData(mxml_node_t *node, void *data);
|
|
|
250 |
extern void mxmlSetWrapMargin(int column);
|
|
|
251 |
extern mxml_node_t *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
|
|
|
252 |
int descend);
|
|
|
253 |
extern mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
|
|
|
254 |
int descend);
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
/*
|
|
|
258 |
* Semi-private functions...
|
|
|
259 |
*/
|
|
|
260 |
|
|
|
261 |
extern void mxml_error(const char *format, ...)
|
|
|
262 |
# ifdef __GNUC__
|
|
|
263 |
__attribute__ ((__format__ (__printf__, 1, 2)))
|
|
|
264 |
# endif /* __GNUC__ */
|
|
|
265 |
;
|
|
|
266 |
extern mxml_type_t mxml_ignore_cb(mxml_node_t *node);
|
|
|
267 |
extern mxml_type_t mxml_integer_cb(mxml_node_t *node);
|
|
|
268 |
extern mxml_type_t mxml_opaque_cb(mxml_node_t *node);
|
|
|
269 |
extern mxml_type_t mxml_real_cb(mxml_node_t *node);
|
|
|
270 |
|
|
|
271 |
|
|
|
272 |
/*
|
|
|
273 |
* C++ support...
|
|
|
274 |
*/
|
|
|
275 |
|
|
|
276 |
# ifdef __cplusplus
|
|
|
277 |
}
|
|
|
278 |
# endif /* __cplusplus */
|
|
|
279 |
#endif /* !_mxml_h_ */
|