root / logic / trunk / src / mxml / mxml.h @ 60
History | View | Annotate | Download (10.8 KB)
1 | 49 | Janez1 | /*
|
---|---|---|---|
2 | * "$Id: mxml.h 385 2009-03-19 05:38:52Z mike $"
|
||
3 | *
|
||
4 | * Header file for Mini-XML, a small XML-like file parsing library.
|
||
5 | *
|
||
6 | * Copyright 2003-2009 by Michael Sweet.
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU Library General Public
|
||
10 | * License as published by the Free Software Foundation; either
|
||
11 | * version 2, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | */
|
||
18 | |||
19 | /*
|
||
20 | * Prevent multiple inclusion...
|
||
21 | */
|
||
22 | |||
23 | #ifndef _mxml_h_
|
||
24 | # define _mxml_h_
|
||
25 | |||
26 | /*
|
||
27 | * Include necessary headers...
|
||
28 | */
|
||
29 | |||
30 | # include <stdio.h> |
||
31 | # include <stdlib.h> |
||
32 | # include <string.h> |
||
33 | # include <ctype.h> |
||
34 | # include <errno.h> |
||
35 | |||
36 | |||
37 | /*
|
||
38 | * Constants...
|
||
39 | */
|
||
40 | |||
41 | # define MXML_TAB 8 /* Tabs every N columns */ |
||
42 | |||
43 | # define MXML_NO_CALLBACK 0 /* Don't use a type callback */ |
||
44 | # define MXML_INTEGER_CALLBACK mxml_integer_cb
|
||
45 | /* Treat all data as integers */
|
||
46 | # define MXML_OPAQUE_CALLBACK mxml_opaque_cb
|
||
47 | /* Treat all data as opaque */
|
||
48 | # define MXML_REAL_CALLBACK mxml_real_cb
|
||
49 | /* Treat all data as real numbers */
|
||
50 | # define MXML_TEXT_CALLBACK 0 /* Treat all data as text */ |
||
51 | # define MXML_IGNORE_CALLBACK mxml_ignore_cb
|
||
52 | /* Ignore all non-element content */
|
||
53 | |||
54 | # define MXML_NO_PARENT 0 /* No parent for the node */ |
||
55 | |||
56 | # define MXML_DESCEND 1 /* Descend when finding/walking */ |
||
57 | # define MXML_NO_DESCEND 0 /* Don't descend when finding/walking */ |
||
58 | # define MXML_DESCEND_FIRST -1 /* Descend for first find */ |
||
59 | |||
60 | # define MXML_WS_BEFORE_OPEN 0 /* Callback for before open tag */ |
||
61 | # define MXML_WS_AFTER_OPEN 1 /* Callback for after open tag */ |
||
62 | # define MXML_WS_BEFORE_CLOSE 2 /* Callback for before close tag */ |
||
63 | # define MXML_WS_AFTER_CLOSE 3 /* Callback for after close tag */ |
||
64 | |||
65 | # define MXML_ADD_BEFORE 0 /* Add node before specified node */ |
||
66 | # define MXML_ADD_AFTER 1 /* Add node after specified node */ |
||
67 | # define MXML_ADD_TO_PARENT NULL /* Add node relative to parent */ |
||
68 | |||
69 | |||
70 | /*
|
||
71 | * Data types...
|
||
72 | */
|
||
73 | |||
74 | typedef enum mxml_sax_event_e /**** SAX event type. ****/ |
||
75 | { |
||
76 | MXML_SAX_CDATA, /* CDATA node */
|
||
77 | MXML_SAX_COMMENT, /* Comment node */
|
||
78 | MXML_SAX_DATA, /* Data node */
|
||
79 | MXML_SAX_DIRECTIVE, /* Processing directive node */
|
||
80 | MXML_SAX_ELEMENT_CLOSE, /* Element closed */
|
||
81 | MXML_SAX_ELEMENT_OPEN /* Element opened */
|
||
82 | } mxml_sax_event_t; |
||
83 | |||
84 | typedef enum mxml_type_e /**** The XML node type. ****/ |
||
85 | { |
||
86 | MXML_IGNORE = -1, /* Ignore/throw away node @since Mini-XML 2.3@ */ |
||
87 | MXML_ELEMENT, /* XML element with attributes */
|
||
88 | MXML_INTEGER, /* Integer value */
|
||
89 | MXML_OPAQUE, /* Opaque string */
|
||
90 | MXML_REAL, /* Real value */
|
||
91 | MXML_TEXT, /* Text fragment */
|
||
92 | MXML_CUSTOM /* Custom data @since Mini-XML 2.1@ */
|
||
93 | } mxml_type_t; |
||
94 | |||
95 | typedef void (*mxml_custom_destroy_cb_t)(void *); |
||
96 | /**** Custom data destructor ****/
|
||
97 | |||
98 | typedef void (*mxml_error_cb_t)(const char *); |
||
99 | /**** Error callback function ****/
|
||
100 | |||
101 | typedef struct mxml_attr_s /**** An XML element attribute value. ****/ |
||
102 | { |
||
103 | char *name; /* Attribute name */ |
||
104 | char *value; /* Attribute value */ |
||
105 | } mxml_attr_t; |
||
106 | |||
107 | typedef struct mxml_element_s /**** An XML element value. ****/ |
||
108 | { |
||
109 | char *name; /* Name of element */ |
||
110 | int num_attrs; /* Number of attributes */ |
||
111 | mxml_attr_t *attrs; /* Attributes */
|
||
112 | } mxml_element_t; |
||
113 | |||
114 | typedef struct mxml_text_s /**** An XML text value. ****/ |
||
115 | { |
||
116 | int whitespace; /* Leading whitespace? */ |
||
117 | char *string; /* Fragment string */ |
||
118 | } mxml_text_t; |
||
119 | |||
120 | typedef struct mxml_custom_s /**** An XML custom value. @since Mini-XML 2.1@ ****/ |
||
121 | { |
||
122 | void *data; /* Pointer to (allocated) custom data */ |
||
123 | mxml_custom_destroy_cb_t destroy; /* Pointer to destructor function */
|
||
124 | } mxml_custom_t; |
||
125 | |||
126 | typedef union mxml_value_u /**** An XML node value. ****/ |
||
127 | { |
||
128 | mxml_element_t element; /* Element */
|
||
129 | int integer; /* Integer number */ |
||
130 | char *opaque; /* Opaque string */ |
||
131 | double real; /* Real number */ |
||
132 | mxml_text_t text; /* Text fragment */
|
||
133 | mxml_custom_t custom; /* Custom data @since Mini-XML 2.1@ */
|
||
134 | } mxml_value_t; |
||
135 | |||
136 | typedef struct mxml_node_s /**** An XML node. ****/ |
||
137 | { |
||
138 | mxml_type_t type; /* Node type */
|
||
139 | struct mxml_node_s *next; /* Next node under same parent */ |
||
140 | struct mxml_node_s *prev; /* Previous node under same parent */ |
||
141 | struct mxml_node_s *parent; /* Parent node */ |
||
142 | struct mxml_node_s *child; /* First child node */ |
||
143 | struct mxml_node_s *last_child; /* Last child node */ |
||
144 | mxml_value_t value; /* Node value */
|
||
145 | int ref_count; /* Use count */ |
||
146 | void *user_data; /* User data */ |
||
147 | } mxml_node_t; |
||
148 | |||
149 | typedef struct mxml_index_s /**** An XML node index. ****/ |
||
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 | } mxml_index_t; |
||
157 | |||
158 | typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *); |
||
159 | /**** Custom data load callback function ****/
|
||
160 | |||
161 | typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *); |
||
162 | /**** Custom data save callback function ****/
|
||
163 | |||
164 | typedef int (*mxml_entity_cb_t)(const char *); |
||
165 | /**** Entity callback function */
|
||
166 | |||
167 | typedef mxml_type_t (*mxml_load_cb_t)(mxml_node_t *);
|
||
168 | /**** Load callback function ****/
|
||
169 | |||
170 | typedef const char *(*mxml_save_cb_t)(mxml_node_t *, int); |
||
171 | /**** Save callback function ****/
|
||
172 | |||
173 | typedef void (*mxml_sax_cb_t)(mxml_node_t *, mxml_sax_event_t, void *); |
||
174 | /**** SAX callback function ****/
|
||
175 | |||
176 | |||
177 | /*
|
||
178 | * C++ support...
|
||
179 | */
|
||
180 | |||
181 | # ifdef __cplusplus
|
||
182 | extern "C" { |
||
183 | # endif /* __cplusplus */ |
||
184 | |||
185 | /*
|
||
186 | * Prototypes...
|
||
187 | */
|
||
188 | |||
189 | extern void mxmlAdd(mxml_node_t *parent, int where, |
||
190 | mxml_node_t *child, mxml_node_t *node); |
||
191 | extern void mxmlDelete(mxml_node_t *node); |
||
192 | extern void mxmlElementDeleteAttr(mxml_node_t *node, |
||
193 | const char *name); |
||
194 | extern const char *mxmlElementGetAttr(mxml_node_t *node, const char *name); |
||
195 | extern void mxmlElementSetAttr(mxml_node_t *node, const char *name, |
||
196 | const char *value); |
||
197 | extern void mxmlElementSetAttrf(mxml_node_t *node, const char *name, |
||
198 | const char *format, ...) |
||
199 | # ifdef __GNUC__
|
||
200 | __attribute__ ((__format__ (__printf__, 3, 4))) |
||
201 | # endif /* __GNUC__ */ |
||
202 | ; |
||
203 | extern int mxmlEntityAddCallback(mxml_entity_cb_t cb); |
||
204 | extern const char *mxmlEntityGetName(int val); |
||
205 | extern int mxmlEntityGetValue(const char *name); |
||
206 | extern void mxmlEntityRemoveCallback(mxml_entity_cb_t cb); |
||
207 | extern mxml_node_t *mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
|
||
208 | const char *name, const char *attr, |
||
209 | const char *value, int descend); |
||
210 | extern void mxmlIndexDelete(mxml_index_t *ind); |
||
211 | extern mxml_node_t *mxmlIndexEnum(mxml_index_t *ind);
|
||
212 | extern mxml_node_t *mxmlIndexFind(mxml_index_t *ind,
|
||
213 | const char *element, |
||
214 | const char *value); |
||
215 | extern mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element, |
||
216 | const char *attr); |
||
217 | extern mxml_node_t *mxmlIndexReset(mxml_index_t *ind);
|
||
218 | extern mxml_node_t *mxmlLoadFd(mxml_node_t *top, int fd, |
||
219 | mxml_type_t (*cb)(mxml_node_t *)); |
||
220 | extern mxml_node_t *mxmlLoadFile(mxml_node_t *top, FILE *fp,
|
||
221 | mxml_type_t (*cb)(mxml_node_t *)); |
||
222 | extern mxml_node_t *mxmlLoadString(mxml_node_t *top, const char *s, |
||
223 | mxml_type_t (*cb)(mxml_node_t *)); |
||
224 | extern mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string); |
||
225 | extern mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data, |
||
226 | mxml_custom_destroy_cb_t destroy); |
||
227 | extern mxml_node_t *mxmlNewElement(mxml_node_t *parent, const char *name); |
||
228 | extern mxml_node_t *mxmlNewInteger(mxml_node_t *parent, int integer); |
||
229 | extern mxml_node_t *mxmlNewOpaque(mxml_node_t *parent, const char *opaque); |
||
230 | extern mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real); |
||
231 | extern mxml_node_t *mxmlNewText(mxml_node_t *parent, int whitespace, |
||
232 | const char *string); |
||
233 | extern mxml_node_t *mxmlNewTextf(mxml_node_t *parent, int whitespace, |
||
234 | const char *format, ...) |
||
235 | # ifdef __GNUC__
|
||
236 | __attribute__ ((__format__ (__printf__, 3, 4))) |
||
237 | # endif /* __GNUC__ */ |
||
238 | ; |
||
239 | extern mxml_node_t *mxmlNewXML(const char *version); |
||
240 | extern int mxmlRelease(mxml_node_t *node); |
||
241 | extern void mxmlRemove(mxml_node_t *node); |
||
242 | extern int mxmlRetain(mxml_node_t *node); |
||
243 | extern char *mxmlSaveAllocString(mxml_node_t *node, |
||
244 | mxml_save_cb_t cb); |
||
245 | extern int mxmlSaveFd(mxml_node_t *node, int fd, |
||
246 | mxml_save_cb_t cb); |
||
247 | extern int mxmlSaveFile(mxml_node_t *node, FILE *fp, |
||
248 | mxml_save_cb_t cb); |
||
249 | extern int mxmlSaveString(mxml_node_t *node, char *buffer, |
||
250 | int bufsize, mxml_save_cb_t cb);
|
||
251 | extern mxml_node_t *mxmlSAXLoadFd(mxml_node_t *top, int fd, |
||
252 | mxml_type_t (*cb)(mxml_node_t *), |
||
253 | mxml_sax_cb_t sax, void *sax_data);
|
||
254 | extern mxml_node_t *mxmlSAXLoadFile(mxml_node_t *top, FILE *fp,
|
||
255 | mxml_type_t (*cb)(mxml_node_t *), |
||
256 | mxml_sax_cb_t sax, void *sax_data);
|
||
257 | extern mxml_node_t *mxmlSAXLoadString(mxml_node_t *top, const char *s, |
||
258 | mxml_type_t (*cb)(mxml_node_t *), |
||
259 | mxml_sax_cb_t sax, void *sax_data);
|
||
260 | extern int mxmlSetCDATA(mxml_node_t *node, const char *data); |
||
261 | extern int mxmlSetCustom(mxml_node_t *node, void *data, |
||
262 | mxml_custom_destroy_cb_t destroy); |
||
263 | extern void mxmlSetCustomHandlers(mxml_custom_load_cb_t load, |
||
264 | mxml_custom_save_cb_t save); |
||
265 | extern int mxmlSetElement(mxml_node_t *node, const char *name); |
||
266 | extern void mxmlSetErrorCallback(mxml_error_cb_t cb); |
||
267 | extern int mxmlSetInteger(mxml_node_t *node, int integer); |
||
268 | extern int mxmlSetOpaque(mxml_node_t *node, const char *opaque); |
||
269 | extern int mxmlSetReal(mxml_node_t *node, double real); |
||
270 | extern int mxmlSetText(mxml_node_t *node, int whitespace, |
||
271 | const char *string); |
||
272 | extern int mxmlSetTextf(mxml_node_t *node, int whitespace, |
||
273 | const char *format, ...) |
||
274 | # ifdef __GNUC__
|
||
275 | __attribute__ ((__format__ (__printf__, 3, 4))) |
||
276 | # endif /* __GNUC__ */ |
||
277 | ; |
||
278 | extern void mxmlSetWrapMargin(int column); |
||
279 | extern mxml_node_t *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
|
||
280 | int descend);
|
||
281 | extern mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
|
||
282 | int descend);
|
||
283 | |||
284 | |||
285 | /*
|
||
286 | * Semi-private functions...
|
||
287 | */
|
||
288 | |||
289 | extern void mxml_error(const char *format, ...); |
||
290 | extern mxml_type_t mxml_ignore_cb(mxml_node_t *node);
|
||
291 | extern mxml_type_t mxml_integer_cb(mxml_node_t *node);
|
||
292 | extern mxml_type_t mxml_opaque_cb(mxml_node_t *node);
|
||
293 | extern mxml_type_t mxml_real_cb(mxml_node_t *node);
|
||
294 | |||
295 | |||
296 | /*
|
||
297 | * C++ support...
|
||
298 | */
|
||
299 | |||
300 | # ifdef __cplusplus
|
||
301 | } |
||
302 | # endif /* __cplusplus */ |
||
303 | #endif /* !_mxml_h_ */ |
||
304 | |||
305 | |||
306 | /*
|
||
307 | * End of "$Id: mxml.h 385 2009-03-19 05:38:52Z mike $".
|
||
308 | */ |