1 /*
2 * "$Id: mxml.h 427 2011-01-03 02:03:29Z mike $"
3 *
4 * Header file for Mini-XML, a small XML-like file parsing library.
5 *
6 * Copyright 2003-2011 by Michael R Sweet.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Michael R Sweet and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "COPYING"
11 * which should have been included with this file. If this file is
12 * missing or damaged, see the license at:
13 *
14 * http://www.minixml.org/
15 */
17 /*
18 * Prevent multiple inclusion...
19 */
21 #ifndef _mxml_h_
22 # define _mxml_h_
24 /*
25 * Include necessary headers...
26 */
28 # include <stdio.h>
29 # include <stdlib.h>
30 # include <string.h>
31 # include <ctype.h>
32 # include <errno.h>
35 /*
36 * Constants...
37 */
39 # define MXML_TAB 8 /* Tabs every N columns */
41 # define MXML_NO_CALLBACK 0 /* Don't use a type callback */
42 # define MXML_INTEGER_CALLBACK mxml_integer_cb
43 /* Treat all data as integers */
44 # define MXML_OPAQUE_CALLBACK mxml_opaque_cb
45 /* Treat all data as opaque */
46 # define MXML_REAL_CALLBACK mxml_real_cb
47 /* Treat all data as real numbers */
48 # define MXML_TEXT_CALLBACK 0 /* Treat all data as text */
49 # define MXML_IGNORE_CALLBACK mxml_ignore_cb
50 /* Ignore all non-element content */
52 # define MXML_NO_PARENT 0 /* No parent for the node */
54 # define MXML_DESCEND 1 /* Descend when finding/walking */
55 # define MXML_NO_DESCEND 0 /* Don't descend when finding/walking */
56 # define MXML_DESCEND_FIRST -1 /* Descend for first find */
58 # define MXML_WS_BEFORE_OPEN 0 /* Callback for before open tag */
59 # define MXML_WS_AFTER_OPEN 1 /* Callback for after open tag */
60 # define MXML_WS_BEFORE_CLOSE 2 /* Callback for before close tag */
61 # define MXML_WS_AFTER_CLOSE 3 /* Callback for after close tag */
63 # define MXML_ADD_BEFORE 0 /* Add node before specified node */
64 # define MXML_ADD_AFTER 1 /* Add node after specified node */
65 # define MXML_ADD_TO_PARENT NULL /* Add node relative to parent */
68 /*
69 * Data types...
70 */
72 typedef enum mxml_sax_event_e /**** SAX event type. ****/
73 {
74 MXML_SAX_CDATA, /* CDATA node */
75 MXML_SAX_COMMENT, /* Comment node */
76 MXML_SAX_DATA, /* Data node */
77 MXML_SAX_DIRECTIVE, /* Processing directive node */
78 MXML_SAX_ELEMENT_CLOSE, /* Element closed */
79 MXML_SAX_ELEMENT_OPEN /* Element opened */
80 } mxml_sax_event_t;
82 typedef enum mxml_type_e /**** The XML node type. ****/
83 {
84 MXML_IGNORE = -1, /* Ignore/throw away node @since Mini-XML 2.3@ */
85 MXML_ELEMENT, /* XML element with attributes */
86 MXML_INTEGER, /* Integer value */
87 MXML_OPAQUE, /* Opaque string */
88 MXML_REAL, /* Real value */
89 MXML_TEXT, /* Text fragment */
90 MXML_CUSTOM /* Custom data @since Mini-XML 2.1@ */
91 } mxml_type_t;
93 typedef void (*mxml_custom_destroy_cb_t)(void *);
94 /**** Custom data destructor ****/
96 typedef void (*mxml_error_cb_t)(const char *);
97 /**** Error callback function ****/
99 typedef struct mxml_attr_s /**** An XML element attribute value. @private@ ****/
100 {
101 char *name; /* Attribute name */
102 char *value; /* Attribute value */
103 } mxml_attr_t;
105 typedef struct mxml_element_s /**** An XML element value. @private@ ****/
106 {
107 char *name; /* Name of element */
108 int num_attrs; /* Number of attributes */
109 mxml_attr_t *attrs; /* Attributes */
110 } mxml_element_t;
112 typedef struct mxml_text_s /**** An XML text value. @private@ ****/
113 {
114 int whitespace; /* Leading whitespace? */
115 char *string; /* Fragment string */
116 } mxml_text_t;
118 typedef struct mxml_custom_s /**** An XML custom value. @private@ ****/
119 {
120 void *data; /* Pointer to (allocated) custom data */
121 mxml_custom_destroy_cb_t destroy; /* Pointer to destructor function */
122 } mxml_custom_t;
124 typedef union mxml_value_u /**** An XML node value. @private@ ****/
125 {
126 mxml_element_t element; /* Element */
127 int integer; /* Integer number */
128 char *opaque; /* Opaque string */
129 double real; /* Real number */
130 mxml_text_t text; /* Text fragment */
131 mxml_custom_t custom; /* Custom data @since Mini-XML 2.1@ */
132 } mxml_value_t;
134 struct mxml_node_s /**** An XML node. @private@ ****/
135 {
136 mxml_type_t type; /* Node type */
137 struct mxml_node_s *next; /* Next node under same parent */
138 struct mxml_node_s *prev; /* Previous node under same parent */
139 struct mxml_node_s *parent; /* Parent node */
140 struct mxml_node_s *child; /* First child node */
141 struct mxml_node_s *last_child; /* Last child node */
142 mxml_value_t value; /* Node value */
143 int ref_count; /* Use count */
144 void *user_data; /* User data */
145 };
147 typedef struct mxml_node_s mxml_node_t; /**** An XML node. ****/
149 struct mxml_index_s /**** An XML node index. @private@ ****/
150 {
151 char *attr; /* Attribute used for indexing or NULL */
152 int num_nodes; /* Number of nodes in index */
153 int alloc_nodes; /* Allocated nodes in index */
154 int cur_node; /* Current node */
155 mxml_node_t **nodes; /* Node array */
156 };
158 typedef struct mxml_index_s mxml_index_t;
159 /**** An XML node index. ****/
161 typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *);
162 /**** Custom data load callback function ****/
164 typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *);
165 /**** Custom data save callback function ****/
167 typedef int (*mxml_entity_cb_t)(const char *);
168 /**** Entity callback function */
170 typedef mxml_type_t (*mxml_load_cb_t)(mxml_node_t *);
171 /**** Load callback function ****/
173 typedef const char *(*mxml_save_cb_t)(mxml_node_t *, int);
174 /**** Save callback function ****/
176 typedef void (*mxml_sax_cb_t)(mxml_node_t *, mxml_sax_event_t, void *);
177 /**** SAX callback function ****/
180 /*
181 * C++ support...
182 */
184 # ifdef __cplusplus
185 extern "C" {
186 # endif /* __cplusplus */
188 /*
189 * Prototypes...
190 */
192 extern void mxmlAdd(mxml_node_t *parent, int where,
193 mxml_node_t *child, mxml_node_t *node);
194 extern void mxmlDelete(mxml_node_t *node);
195 extern void mxmlElementDeleteAttr(mxml_node_t *node,
196 const char *name);
197 extern const char *mxmlElementGetAttr(mxml_node_t *node, const char *name);
198 extern void mxmlElementSetAttr(mxml_node_t *node, const char *name,
199 const char *value);
200 extern void mxmlElementSetAttrf(mxml_node_t *node, const char *name,
201 const char *format, ...)
202 # ifdef __GNUC__
203 __attribute__ ((__format__ (__printf__, 3, 4)))
204 # endif /* __GNUC__ */
205 ;
206 extern int mxmlEntityAddCallback(mxml_entity_cb_t cb);
207 extern const char *mxmlEntityGetName(int val);
208 extern int mxmlEntityGetValue(const char *name);
209 extern void mxmlEntityRemoveCallback(mxml_entity_cb_t cb);
210 extern mxml_node_t *mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
211 const char *name, const char *attr,
212 const char *value, int descend);
213 extern mxml_node_t *mxmlFindPath(mxml_node_t *node, const char *path);
214 extern const char *mxmlGetCDATA(mxml_node_t *node);
215 extern const void *mxmlGetCustom(mxml_node_t *node);
216 extern const char *mxmlGetElement(mxml_node_t *node);
217 extern mxml_node_t *mxmlGetFirstChild(mxml_node_t *node);
218 extern int mxmlGetInteger(mxml_node_t *node);
219 extern mxml_node_t *mxmlGetLastChild(mxml_node_t *node);
220 extern mxml_node_t *mxmlGetNextSibling(mxml_node_t *node);
221 extern const char *mxmlGetOpaque(mxml_node_t *node);
222 extern mxml_node_t *mxmlGetParent(mxml_node_t *node);
223 extern mxml_node_t *mxmlGetPrevSibling(mxml_node_t *node);
224 extern double mxmlGetReal(mxml_node_t *node);
225 extern int mxmlGetRefCount(mxml_node_t *node);
226 extern const char *mxmlGetText(mxml_node_t *node, int *whitespace);
227 extern mxml_type_t mxmlGetType(mxml_node_t *node);
228 extern void *mxmlGetUserData(mxml_node_t *node);
229 extern void mxmlIndexDelete(mxml_index_t *ind);
230 extern mxml_node_t *mxmlIndexEnum(mxml_index_t *ind);
231 extern mxml_node_t *mxmlIndexFind(mxml_index_t *ind,
232 const char *element,
233 const char *value);
234 extern int mxmlIndexGetCount(mxml_index_t *ind);
235 extern mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element,
236 const char *attr);
237 extern mxml_node_t *mxmlIndexReset(mxml_index_t *ind);
238 extern mxml_node_t *mxmlLoadFd(mxml_node_t *top, int fd,
239 mxml_type_t (*cb)(mxml_node_t *));
240 extern mxml_node_t *mxmlLoadFile(mxml_node_t *top, FILE *fp,
241 mxml_type_t (*cb)(mxml_node_t *));
242 extern mxml_node_t *mxmlLoadString(mxml_node_t *top, const char *s,
243 mxml_type_t (*cb)(mxml_node_t *));
244 extern mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string);
245 extern mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data,
246 mxml_custom_destroy_cb_t destroy);
247 extern mxml_node_t *mxmlNewElement(mxml_node_t *parent, const char *name);
248 extern mxml_node_t *mxmlNewInteger(mxml_node_t *parent, int integer);
249 extern mxml_node_t *mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
250 extern mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real);
251 extern mxml_node_t *mxmlNewText(mxml_node_t *parent, int whitespace,
252 const char *string);
253 extern mxml_node_t *mxmlNewTextf(mxml_node_t *parent, int whitespace,
254 const char *format, ...)
255 # ifdef __GNUC__
256 __attribute__ ((__format__ (__printf__, 3, 4)))
257 # endif /* __GNUC__ */
258 ;
259 extern mxml_node_t *mxmlNewXML(const char *version);
260 extern int mxmlRelease(mxml_node_t *node);
261 extern void mxmlRemove(mxml_node_t *node);
262 extern int mxmlRetain(mxml_node_t *node);
263 extern char *mxmlSaveAllocString(mxml_node_t *node,
264 mxml_save_cb_t cb);
265 extern int mxmlSaveFd(mxml_node_t *node, int fd,
266 mxml_save_cb_t cb);
267 extern int mxmlSaveFile(mxml_node_t *node, FILE *fp,
268 mxml_save_cb_t cb);
269 extern int mxmlSaveString(mxml_node_t *node, char *buffer,
270 int bufsize, mxml_save_cb_t cb);
271 extern mxml_node_t *mxmlSAXLoadFd(mxml_node_t *top, int fd,
272 mxml_type_t (*cb)(mxml_node_t *),
273 mxml_sax_cb_t sax, void *sax_data);
274 extern mxml_node_t *mxmlSAXLoadFile(mxml_node_t *top, FILE *fp,
275 mxml_type_t (*cb)(mxml_node_t *),
276 mxml_sax_cb_t sax, void *sax_data);
277 extern mxml_node_t *mxmlSAXLoadString(mxml_node_t *top, const char *s,
278 mxml_type_t (*cb)(mxml_node_t *),
279 mxml_sax_cb_t sax, void *sax_data);
280 extern int mxmlSetCDATA(mxml_node_t *node, const char *data);
281 extern int mxmlSetCustom(mxml_node_t *node, void *data,
282 mxml_custom_destroy_cb_t destroy);
283 extern void mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
284 mxml_custom_save_cb_t save);
285 extern int mxmlSetElement(mxml_node_t *node, const char *name);
286 extern void mxmlSetErrorCallback(mxml_error_cb_t cb);
287 extern int mxmlSetInteger(mxml_node_t *node, int integer);
288 extern int mxmlSetOpaque(mxml_node_t *node, const char *opaque);
289 extern int mxmlSetReal(mxml_node_t *node, double real);
290 extern int mxmlSetText(mxml_node_t *node, int whitespace,
291 const char *string);
292 extern int mxmlSetTextf(mxml_node_t *node, int whitespace,
293 const char *format, ...)
294 # ifdef __GNUC__
295 __attribute__ ((__format__ (__printf__, 3, 4)))
296 # endif /* __GNUC__ */
297 ;
298 extern int mxmlSetUserData(mxml_node_t *node, void *data);
299 extern void mxmlSetWrapMargin(int column);
300 extern mxml_node_t *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
301 int descend);
302 extern mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
303 int descend);
306 /*
307 * Semi-private functions...
308 */
310 extern void mxml_error(const char *format, ...);
311 extern mxml_type_t mxml_ignore_cb(mxml_node_t *node);
312 extern mxml_type_t mxml_integer_cb(mxml_node_t *node);
313 extern mxml_type_t mxml_opaque_cb(mxml_node_t *node);
314 extern mxml_type_t mxml_real_cb(mxml_node_t *node);
317 /*
318 * C++ support...
319 */
321 # ifdef __cplusplus
322 }
323 # endif /* __cplusplus */
324 #endif /* !_mxml_h_ */
327 /*
328 * End of "$Id: mxml.h 427 2011-01-03 02:03:29Z mike $".
329 */