blob: 79c711f4c80fd59674e8273ea6866d37ec534476 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/*
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 */
16
17/*
18 * Prevent multiple inclusion...
19 */
20
21#ifndef _mxml_h_
22# define _mxml_h_
23
24/*
25 * Include necessary headers...
26 */
27
28# include <stdio.h>
29# include <stdlib.h>
30# include <string.h>
31# include <ctype.h>
32# include <errno.h>
33
34
35/*
36 * Constants...
37 */
38
39# define MXML_TAB 8 /* Tabs every N columns */
40
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 */
51
52# define MXML_NO_PARENT 0 /* No parent for the node */
53
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 */
57
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 */
62
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 */
66
67
68/*
69 * Data types...
70 */
71
72typedef 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;
81
82typedef 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;
92
93typedef void (*mxml_custom_destroy_cb_t)(void *);
94 /**** Custom data destructor ****/
95
96typedef void (*mxml_error_cb_t)(const char *);
97 /**** Error callback function ****/
98
99typedef 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;
104
105typedef 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;
111
112typedef struct mxml_text_s /**** An XML text value. @private@ ****/
113{
114 int whitespace; /* Leading whitespace? */
115 char *string; /* Fragment string */
116} mxml_text_t;
117
118typedef 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;
123
124typedef 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;
133
134struct 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};
146
147typedef struct mxml_node_s mxml_node_t; /**** An XML node. ****/
148
149struct 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};
157
158typedef struct mxml_index_s mxml_index_t;
159 /**** An XML node index. ****/
160
161typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *);
162 /**** Custom data load callback function ****/
163
164typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *);
165 /**** Custom data save callback function ****/
166
167typedef int (*mxml_entity_cb_t)(const char *);
168 /**** Entity callback function */
169
170typedef mxml_type_t (*mxml_load_cb_t)(mxml_node_t *);
171 /**** Load callback function ****/
172
173typedef const char *(*mxml_save_cb_t)(mxml_node_t *, int);
174 /**** Save callback function ****/
175
176typedef void (*mxml_sax_cb_t)(mxml_node_t *, mxml_sax_event_t, void *);
177 /**** SAX callback function ****/
178
179
180/*
181 * C++ support...
182 */
183
184# ifdef __cplusplus
185extern "C" {
186# endif /* __cplusplus */
187
188/*
189 * Prototypes...
190 */
191
192extern void mxmlAdd(mxml_node_t *parent, int where,
193 mxml_node_t *child, mxml_node_t *node);
194extern void mxmlDelete(mxml_node_t *node);
195extern void mxmlElementDeleteAttr(mxml_node_t *node,
196 const char *name);
197extern const char *mxmlElementGetAttr(mxml_node_t *node, const char *name);
198extern void mxmlElementSetAttr(mxml_node_t *node, const char *name,
199 const char *value);
200extern 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;
206extern int mxmlEntityAddCallback(mxml_entity_cb_t cb);
207extern const char *mxmlEntityGetName(int val);
208extern int mxmlEntityGetValue(const char *name);
209extern void mxmlEntityRemoveCallback(mxml_entity_cb_t cb);
210extern 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);
213extern mxml_node_t *mxmlFindPath(mxml_node_t *node, const char *path);
214extern const char *mxmlGetCDATA(mxml_node_t *node);
215extern const void *mxmlGetCustom(mxml_node_t *node);
216extern const char *mxmlGetElement(mxml_node_t *node);
217extern mxml_node_t *mxmlGetFirstChild(mxml_node_t *node);
218extern int mxmlGetInteger(mxml_node_t *node);
219extern mxml_node_t *mxmlGetLastChild(mxml_node_t *node);
220extern mxml_node_t *mxmlGetNextSibling(mxml_node_t *node);
221extern const char *mxmlGetOpaque(mxml_node_t *node);
222extern mxml_node_t *mxmlGetParent(mxml_node_t *node);
223extern mxml_node_t *mxmlGetPrevSibling(mxml_node_t *node);
224extern double mxmlGetReal(mxml_node_t *node);
225extern int mxmlGetRefCount(mxml_node_t *node);
226extern const char *mxmlGetText(mxml_node_t *node, int *whitespace);
227extern mxml_type_t mxmlGetType(mxml_node_t *node);
228extern void *mxmlGetUserData(mxml_node_t *node);
229extern void mxmlIndexDelete(mxml_index_t *ind);
230extern mxml_node_t *mxmlIndexEnum(mxml_index_t *ind);
231extern mxml_node_t *mxmlIndexFind(mxml_index_t *ind,
232 const char *element,
233 const char *value);
234extern int mxmlIndexGetCount(mxml_index_t *ind);
235extern mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element,
236 const char *attr);
237extern mxml_node_t *mxmlIndexReset(mxml_index_t *ind);
238extern mxml_node_t *mxmlLoadFd(mxml_node_t *top, int fd,
239 mxml_type_t (*cb)(mxml_node_t *));
240extern mxml_node_t *mxmlLoadFile(mxml_node_t *top, FILE *fp,
241 mxml_type_t (*cb)(mxml_node_t *));
242extern mxml_node_t *mxmlLoadString(mxml_node_t *top, const char *s,
243 mxml_type_t (*cb)(mxml_node_t *));
244extern mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string);
245extern mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data,
246 mxml_custom_destroy_cb_t destroy);
247extern mxml_node_t *mxmlNewElement(mxml_node_t *parent, const char *name);
248extern mxml_node_t *mxmlNewInteger(mxml_node_t *parent, int integer);
249extern mxml_node_t *mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
250extern mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real);
251extern mxml_node_t *mxmlNewText(mxml_node_t *parent, int whitespace,
252 const char *string);
253extern 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;
259extern mxml_node_t *mxmlNewXML(const char *version);
260extern int mxmlRelease(mxml_node_t *node);
261extern void mxmlRemove(mxml_node_t *node);
262extern int mxmlRetain(mxml_node_t *node);
263extern char *mxmlSaveAllocString(mxml_node_t *node,
264 mxml_save_cb_t cb);
265extern int mxmlSaveFd(mxml_node_t *node, int fd,
266 mxml_save_cb_t cb);
267extern int mxmlSaveFile(mxml_node_t *node, FILE *fp,
268 mxml_save_cb_t cb);
269extern int mxmlSaveString(mxml_node_t *node, char *buffer,
270 int bufsize, mxml_save_cb_t cb);
271extern 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);
274extern 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);
277extern 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);
280extern int mxmlSetCDATA(mxml_node_t *node, const char *data);
281extern int mxmlSetCustom(mxml_node_t *node, void *data,
282 mxml_custom_destroy_cb_t destroy);
283extern void mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
284 mxml_custom_save_cb_t save);
285extern int mxmlSetElement(mxml_node_t *node, const char *name);
286extern void mxmlSetErrorCallback(mxml_error_cb_t cb);
287extern int mxmlSetInteger(mxml_node_t *node, int integer);
288extern int mxmlSetOpaque(mxml_node_t *node, const char *opaque);
289extern int mxmlSetReal(mxml_node_t *node, double real);
290extern int mxmlSetText(mxml_node_t *node, int whitespace,
291 const char *string);
292extern 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;
298extern int mxmlSetUserData(mxml_node_t *node, void *data);
299extern void mxmlSetWrapMargin(int column);
300extern mxml_node_t *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
301 int descend);
302extern mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
303 int descend);
304
305
306/*
307 * Semi-private functions...
308 */
309
310extern void mxml_error(const char *format, ...);
311extern mxml_type_t mxml_ignore_cb(mxml_node_t *node);
312extern mxml_type_t mxml_integer_cb(mxml_node_t *node);
313extern mxml_type_t mxml_opaque_cb(mxml_node_t *node);
314extern mxml_type_t mxml_real_cb(mxml_node_t *node);
315
316
317/*
318 * C++ support...
319 */
320
321# ifdef __cplusplus
322}
323# endif /* __cplusplus */
324#endif /* !_mxml_h_ */
325
326
327/*
328 * End of "$Id: mxml.h 427 2011-01-03 02:03:29Z mike $".
329 */