forked from michaelrsweet/mxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mxml.h
279 lines (241 loc) · 10.2 KB
/
mxml.h
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Header file for Mini-XML, a small XML file parsing library.
*
* https://www.msweet.org/mxml
*
* Copyright © 2003-2021 by Michael R Sweet.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more
* information.
*/
/*
* Prevent multiple inclusion...
*/
#ifndef _mxml_h_
# define _mxml_h_
/*
* Include necessary headers...
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <errno.h>
/*
* Constants...
*/
# define MXML_MAJOR_VERSION 3 /* Major version number */
# define MXML_MINOR_VERSION 2 /* Minor version number */
# define MXML_TAB 8 /* Tabs every N columns */
# define MXML_NO_CALLBACK 0 /* Don't use a type callback */
# define MXML_INTEGER_CALLBACK mxml_integer_cb
/* Treat all data as integers */
# define MXML_OPAQUE_CALLBACK mxml_opaque_cb
/* Treat all data as opaque */
# define MXML_REAL_CALLBACK mxml_real_cb
/* Treat all data as real numbers */
# define MXML_TEXT_CALLBACK 0 /* Treat all data as text */
# define MXML_IGNORE_CALLBACK mxml_ignore_cb
/* Ignore all non-element content */
# define MXML_NO_PARENT 0 /* No parent for the node */
# define MXML_DESCEND 1 /* Descend when finding/walking */
# define MXML_NO_DESCEND 0 /* Don't descend when finding/walking */
# define MXML_DESCEND_FIRST -1 /* Descend for first find */
# define MXML_WS_BEFORE_OPEN 0 /* Callback for before open tag */
# define MXML_WS_AFTER_OPEN 1 /* Callback for after open tag */
# define MXML_WS_BEFORE_CLOSE 2 /* Callback for before close tag */
# define MXML_WS_AFTER_CLOSE 3 /* Callback for after close tag */
# define MXML_ADD_BEFORE 0 /* Add node before specified node */
# define MXML_ADD_AFTER 1 /* Add node after specified node */
# define MXML_ADD_TO_PARENT NULL /* Add node relative to parent */
/*
* Data types...
*/
typedef enum mxml_sax_event_e /**** SAX event type. ****/
{
MXML_SAX_CDATA, /* CDATA node */
MXML_SAX_COMMENT, /* Comment node */
MXML_SAX_DATA, /* Data node */
MXML_SAX_DIRECTIVE, /* Processing directive node */
MXML_SAX_ELEMENT_CLOSE, /* Element closed */
MXML_SAX_ELEMENT_OPEN /* Element opened */
} mxml_sax_event_t;
typedef enum mxml_type_e /**** The XML node type. ****/
{
MXML_IGNORE = -1, /* Ignore/throw away node @since Mini-XML 2.3@ */
MXML_ELEMENT, /* XML element with attributes */
MXML_INTEGER, /* Integer value */
MXML_OPAQUE, /* Opaque string */
MXML_REAL, /* Real value */
MXML_TEXT, /* Text fragment */
MXML_CUSTOM /* Custom data @since Mini-XML 2.1@ */
} mxml_type_t;
typedef void (*mxml_custom_destroy_cb_t)(void *);
/**** Custom data destructor ****/
typedef void (*mxml_error_cb_t)(const char *);
/**** Error callback function ****/
typedef struct _mxml_node_s mxml_node_t; /**** An XML node. ****/
typedef struct _mxml_index_s mxml_index_t;
/**** An XML node index. ****/
typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *);
/**** Custom data load callback function ****/
typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *);
/**** Custom data save callback function ****/
typedef int (*mxml_entity_cb_t)(const char *);
/**** Entity callback function */
typedef mxml_type_t (*mxml_load_cb_t)(mxml_node_t *);
/**** Load callback function ****/
typedef const char *(*mxml_save_cb_t)(mxml_node_t *, int);
/**** Save callback function ****/
typedef void (*mxml_sax_cb_t)(mxml_node_t *, mxml_sax_event_t, void *);
/**** SAX callback function ****/
/*
* C++ support...
*/
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
/*
* Prototypes...
*/
extern void mxmlAdd(mxml_node_t *parent, int where,
mxml_node_t *child, mxml_node_t *node);
extern void mxmlDelete(mxml_node_t *node);
extern void mxmlElementDeleteAttr(mxml_node_t *node,
const char *name);
extern const char *mxmlElementGetAttr(mxml_node_t *node, const char *name);
extern const char *mxmlElementGetAttrByIndex(mxml_node_t *node, int idx, const char **name);
extern int mxmlElementGetAttrCount(mxml_node_t *node);
extern void mxmlElementSetAttr(mxml_node_t *node, const char *name,
const char *value);
extern void mxmlElementSetAttrf(mxml_node_t *node, const char *name,
const char *format, ...)
# ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 3, 4)))
# endif /* __GNUC__ */
;
extern int mxmlEntityAddCallback(mxml_entity_cb_t cb);
extern const char *mxmlEntityGetName(int val);
extern int mxmlEntityGetValue(const char *name);
extern void mxmlEntityRemoveCallback(mxml_entity_cb_t cb);
extern mxml_node_t *mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
const char *element, const char *attr,
const char *value, int descend);
extern mxml_node_t *mxmlFindPath(mxml_node_t *node, const char *path);
extern const char *mxmlGetCDATA(mxml_node_t *node);
extern const void *mxmlGetCustom(mxml_node_t *node);
extern const char *mxmlGetElement(mxml_node_t *node);
extern mxml_node_t *mxmlGetFirstChild(mxml_node_t *node);
extern int mxmlGetInteger(mxml_node_t *node);
extern mxml_node_t *mxmlGetLastChild(mxml_node_t *node);
extern mxml_node_t *mxmlGetNextSibling(mxml_node_t *node);
extern const char *mxmlGetOpaque(mxml_node_t *node);
extern mxml_node_t *mxmlGetParent(mxml_node_t *node);
extern mxml_node_t *mxmlGetPrevSibling(mxml_node_t *node);
extern double mxmlGetReal(mxml_node_t *node);
extern int mxmlGetRefCount(mxml_node_t *node);
extern const char *mxmlGetText(mxml_node_t *node, int *whitespace);
extern mxml_type_t mxmlGetType(mxml_node_t *node);
extern void *mxmlGetUserData(mxml_node_t *node);
extern void mxmlIndexDelete(mxml_index_t *ind);
extern mxml_node_t *mxmlIndexEnum(mxml_index_t *ind);
extern mxml_node_t *mxmlIndexFind(mxml_index_t *ind,
const char *element,
const char *value);
extern int mxmlIndexGetCount(mxml_index_t *ind);
extern mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element,
const char *attr);
extern mxml_node_t *mxmlIndexReset(mxml_index_t *ind);
extern mxml_node_t *mxmlLoadFd(mxml_node_t *top, int fd,
mxml_type_t (*cb)(mxml_node_t *));
extern mxml_node_t *mxmlLoadFile(mxml_node_t *top, FILE *fp,
mxml_type_t (*cb)(mxml_node_t *));
extern mxml_node_t *mxmlLoadString(mxml_node_t *top, const char *s,
mxml_type_t (*cb)(mxml_node_t *));
extern mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string);
extern mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data,
mxml_custom_destroy_cb_t destroy);
extern mxml_node_t *mxmlNewElement(mxml_node_t *parent, const char *name);
extern mxml_node_t *mxmlNewInteger(mxml_node_t *parent, int integer);
extern mxml_node_t *mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
extern mxml_node_t *mxmlNewOpaquef(mxml_node_t *parent, const char *format, ...)
# ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 2, 3)))
# endif /* __GNUC__ */
;
extern mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real);
extern mxml_node_t *mxmlNewText(mxml_node_t *parent, int whitespace, const char *string);
extern mxml_node_t *mxmlNewTextf(mxml_node_t *parent, int whitespace, const char *format, ...)
# ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 3, 4)))
# endif /* __GNUC__ */
;
extern mxml_node_t *mxmlNewXML(const char *version);
extern int mxmlRelease(mxml_node_t *node);
extern void mxmlRemove(mxml_node_t *node);
extern int mxmlRetain(mxml_node_t *node);
extern char *mxmlSaveAllocString(mxml_node_t *node,
mxml_save_cb_t cb);
extern int mxmlSaveFd(mxml_node_t *node, int fd,
mxml_save_cb_t cb);
extern int mxmlSaveFile(mxml_node_t *node, FILE *fp,
mxml_save_cb_t cb);
extern int mxmlSaveString(mxml_node_t *node, char *buffer,
int bufsize, mxml_save_cb_t cb);
extern mxml_node_t *mxmlSAXLoadFd(mxml_node_t *top, int fd,
mxml_type_t (*cb)(mxml_node_t *),
mxml_sax_cb_t sax, void *sax_data);
extern mxml_node_t *mxmlSAXLoadFile(mxml_node_t *top, FILE *fp,
mxml_type_t (*cb)(mxml_node_t *),
mxml_sax_cb_t sax, void *sax_data);
extern mxml_node_t *mxmlSAXLoadString(mxml_node_t *top, const char *s,
mxml_type_t (*cb)(mxml_node_t *),
mxml_sax_cb_t sax, void *sax_data);
extern int mxmlSetCDATA(mxml_node_t *node, const char *data);
extern int mxmlSetCustom(mxml_node_t *node, void *data,
mxml_custom_destroy_cb_t destroy);
extern void mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
mxml_custom_save_cb_t save);
extern int mxmlSetElement(mxml_node_t *node, const char *name);
extern void mxmlSetErrorCallback(mxml_error_cb_t cb);
extern int mxmlSetInteger(mxml_node_t *node, int integer);
extern int mxmlSetOpaque(mxml_node_t *node, const char *opaque);
extern int mxmlSetOpaquef(mxml_node_t *node, const char *format, ...)
# ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 2, 3)))
# endif /* __GNUC__ */
;
extern int mxmlSetReal(mxml_node_t *node, double real);
extern int mxmlSetText(mxml_node_t *node, int whitespace,
const char *string);
extern int mxmlSetTextf(mxml_node_t *node, int whitespace,
const char *format, ...)
# ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 3, 4)))
# endif /* __GNUC__ */
;
extern int mxmlSetUserData(mxml_node_t *node, void *data);
extern void mxmlSetWrapMargin(int column);
extern mxml_node_t *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
int descend);
extern mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
int descend);
/*
* Semi-private functions...
*/
extern void mxml_error(const char *format, ...)
# ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 1, 2)))
# endif /* __GNUC__ */
;
extern mxml_type_t mxml_ignore_cb(mxml_node_t *node);
extern mxml_type_t mxml_integer_cb(mxml_node_t *node);
extern mxml_type_t mxml_opaque_cb(mxml_node_t *node);
extern mxml_type_t mxml_real_cb(mxml_node_t *node);
/*
* C++ support...
*/
# ifdef __cplusplus
}
# endif /* __cplusplus */
#endif /* !_mxml_h_ */