aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/include/eina_simple_xml_parser.h
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/include/eina_simple_xml_parser.h')
-rw-r--r--libraries/eina/src/include/eina_simple_xml_parser.h386
1 files changed, 386 insertions, 0 deletions
diff --git a/libraries/eina/src/include/eina_simple_xml_parser.h b/libraries/eina/src/include/eina_simple_xml_parser.h
new file mode 100644
index 0000000..db9a4e1
--- /dev/null
+++ b/libraries/eina/src/include/eina_simple_xml_parser.h
@@ -0,0 +1,386 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2011 Gustavo Sverzut Barbieri
3 * Cedric Bail
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library;
17 * if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef EINA_SIMPLE_XML_H_
21#define EINA_SIMPLE_XML_H_
22
23#include "eina_config.h"
24
25#include "eina_types.h"
26#include "eina_magic.h"
27#include "eina_inlist.h"
28
29/**
30 * @defgroup Eina_Simple_XML_Group Simple_XML
31 *
32 * Simplistic relaxed SAX-like XML parser.
33 *
34 * This parser is far from being compliant with XML standard, but will
35 * do for most XMLs out there. If you know that your format is simple
36 * and will not vary in future with strange corner cases, then you can
37 * use it safely.
38 *
39 * The parser is SAX like, that is, it will tokenize contents and call
40 * you back so you can take some action. No contents are allocated
41 * during this parser work and it's not recursive, so you can use it
42 * with a very large document without worries.
43 *
44 * It will not validate the document anyhow, neither it will create a
45 * tree hierarchy. That's up to you.
46 *
47 * Accordingly to XML, open tags may contain attributes. This parser
48 * will not tokenize this. If you want you can use
49 * eina_simple_xml_tag_attributes_find() and then
50 * eina_simple_xml_attributes_parse().
51 */
52
53/**
54 * @addtogroup Eina_Tools_Group Tools
55 *
56 * @{
57 */
58
59/**
60 * @defgroup Eina_Simple_XML_Group Simple_XML
61 *
62 * @{
63 */
64
65typedef struct _Eina_Simple_XML_Node Eina_Simple_XML_Node;
66typedef struct _Eina_Simple_XML_Node_Tag Eina_Simple_XML_Node_Root;
67typedef struct _Eina_Simple_XML_Node_Tag Eina_Simple_XML_Node_Tag;
68typedef struct _Eina_Simple_XML_Node_Data Eina_Simple_XML_Node_Data;
69typedef struct _Eina_Simple_XML_Node_Data Eina_Simple_XML_Node_CData;
70typedef struct _Eina_Simple_XML_Node_Data Eina_Simple_XML_Node_Processing;
71typedef struct _Eina_Simple_XML_Node_Data Eina_Simple_XML_Node_Doctype;
72typedef struct _Eina_Simple_XML_Node_Data Eina_Simple_XML_Node_Comment;
73typedef struct _Eina_Simple_XML_Attribute Eina_Simple_XML_Attribute;
74
75struct _Eina_Simple_XML_Attribute
76{
77 EINA_INLIST;
78 EINA_MAGIC;
79
80 Eina_Simple_XML_Node_Tag *parent;
81 const char *key;
82 const char *value;
83};
84
85typedef enum _Eina_Simple_XML_Node_Type
86{
87 EINA_SIMPLE_XML_NODE_ROOT = 0,
88 EINA_SIMPLE_XML_NODE_TAG,
89 EINA_SIMPLE_XML_NODE_DATA,
90 EINA_SIMPLE_XML_NODE_CDATA,
91 EINA_SIMPLE_XML_NODE_PROCESSING,
92 EINA_SIMPLE_XML_NODE_DOCTYPE,
93 EINA_SIMPLE_XML_NODE_COMMENT
94} Eina_Simple_XML_Node_Type;
95
96struct _Eina_Simple_XML_Node
97{
98 EINA_INLIST;
99 EINA_MAGIC;
100
101 Eina_Simple_XML_Node_Tag *parent;
102 Eina_Simple_XML_Node_Type type;
103};
104
105struct _Eina_Simple_XML_Node_Tag
106{
107 Eina_Simple_XML_Node base;
108 Eina_Inlist *children;
109 Eina_Inlist *attributes;
110 const char *name;
111};
112
113struct _Eina_Simple_XML_Node_Data
114{
115 Eina_Simple_XML_Node base;
116 size_t length;
117 char data[];
118};
119
120typedef enum _Eina_Simple_XML_Type
121{
122 EINA_SIMPLE_XML_OPEN = 0, /* <tag attribute="value"> */
123 EINA_SIMPLE_XML_OPEN_EMPTY, /* <tag attribute="value" /> */
124 EINA_SIMPLE_XML_CLOSE, /* </tag> */
125 EINA_SIMPLE_XML_DATA, /* tag text data */
126 EINA_SIMPLE_XML_CDATA, /* <![CDATA[something]]> */
127 EINA_SIMPLE_XML_ERROR, /* error contents */
128 EINA_SIMPLE_XML_PROCESSING, /* <?xml ... ?> <?php .. ?> */
129 EINA_SIMPLE_XML_DOCTYPE, /* <!DOCTYPE html */
130 EINA_SIMPLE_XML_COMMENT, /* <!-- something --> */
131 EINA_SIMPLE_XML_IGNORED /* whatever is ignored by parser, like whitespace */
132} Eina_Simple_XML_Type;
133
134typedef Eina_Bool (*Eina_Simple_XML_Cb)(void *data, Eina_Simple_XML_Type type, const char *content, unsigned offset, unsigned length);
135typedef Eina_Bool (*Eina_Simple_XML_Attribute_Cb)(void *data, const char *key, const char *value);
136
137
138/**
139 * Parse a section of XML string text
140 *
141 * @param buf the input string. May not contain \0 terminator.
142 * @param buflen the input string size.
143 * @param strip whenever this parser should strip leading and trailing
144 * whitespace. These whitespace will still be issued, but as type
145 * #EINA_SIMPLE_XML_IGNORED.
146 * @param func what to call back while parse to do some action. The
147 * first parameter is the given user @a data, the second is the
148 * token type, the third is the pointer to content start (it's
149 * not a NULL terminated string!), the forth is where this
150 * content is located inside @a buf (does not include tag
151 * start, for instance "<!DOCTYPE value>" the offset points at
152 * "value"), the fifth is the size of the content. Whenever this
153 * function return EINA_FALSE the parser will abort. @param
154 * data what to give as context to @a func.
155 *
156 * @return EINA_TRUE on success or EINA_FALSE if it was aborted by user or
157 * parsing error.
158 */
159EAPI Eina_Bool eina_simple_xml_parse(const char *buf, unsigned buflen,
160 Eina_Bool strip,
161 Eina_Simple_XML_Cb func, const void *data);
162
163
164/**
165 * Given the contents of a tag, find where the attributes start.
166 *
167 * The tag contents is returned by eina_simple_xml_parse() when
168 * type is #EINA_SIMPLE_XML_OPEN or #EINA_SIMPLE_XML_OPEN_EMPTY.
169 *
170 * @return pointer to the start of attributes, it can be used
171 * to feed eina_simple_xml_attributes_parse(). NULL is returned
172 * if no attributes were found.
173 */
174EAPI const char * eina_simple_xml_tag_attributes_find(const char *buf, unsigned buflen);
175
176/**
177 * Given a buffer with xml attributes, parse them to key=value pairs.
178 *
179 * @param buf the input string. May not contain \0 terminator.
180 * @param buflen the input string size.
181 * @param func what to call back while parse to do some action. The
182 * first parameter is the given user @a data, the second is the
183 * key (null-terminated) and the last is the value (null
184 * terminated). These strings should not be modified and
185 * reference is just valid until the function return.
186 *
187 * @return EINA_TRUE on success or EINA_FALSE if it was aborted by user or
188 * parsing error.
189 */
190EAPI Eina_Bool eina_simple_xml_attributes_parse(const char *buf, unsigned buflen,
191 Eina_Simple_XML_Attribute_Cb func, const void *data);
192
193/**
194 * Create (and append) new attribute to tag.
195 *
196 * @param parent if provided, will be set in the resulting structure
197 * as well as the attribute will be appended to attributes list.
198 * @param key null-terminated string. Must not be NULL.
199 * @param value null-terminated string. If NULL, the empty string will be used.
200 *
201 * @return newly allocated memory or NULL on error. This memory should be
202 * released with eina_simple_xml_attribute_free() or indirectly
203 * with eina_simple_xml_node_tag_free().
204 */
205EAPI Eina_Simple_XML_Attribute * eina_simple_xml_attribute_new(Eina_Simple_XML_Node_Tag *parent, const char *key, const char *value);
206
207/**
208 * Remove attribute from parent and delete it.
209 *
210 * @param attr attribute to release memory.
211 */
212EAPI void eina_simple_xml_attribute_free(Eina_Simple_XML_Attribute *attr);
213
214
215/**
216 * Create new tag. If parent is provided, it is automatically appended.
217 *
218 * @param parent if provided, will be set in the resulting structure
219 * as well as the tag will be appended to children list.
220 * @param name null-terminated string. Must not be NULL.
221 *
222 * @return newly allocated memory or NULL on error. This memory should be
223 * released with eina_simple_xml_node_tag_free() or indirectly
224 * with eina_simple_xml_node_tag_free() of the parent.
225 */
226EAPI Eina_Simple_XML_Node_Tag * eina_simple_xml_node_tag_new(Eina_Simple_XML_Node_Tag *parent, const char *name);
227
228/**
229 * Remove tag from parent and delete it.
230 *
231 * @param tag to release memory.
232 */
233EAPI void eina_simple_xml_node_tag_free(Eina_Simple_XML_Node_Tag *tag);
234
235
236/**
237 * Create new data. If parent is provided, it is automatically appended.
238 *
239 * @param parent if provided, will be set in the resulting structure
240 * as well as the data will be appended to children list.
241 * @param content string to be used. Must not be NULL.
242 * @param length size in bytes of @a content.
243 *
244 * @return newly allocated memory or NULL on error. This memory should be
245 * released with eina_simple_xml_node_data_free() or indirectly
246 * with eina_simple_xml_node_tag_free() of the parent.
247 */
248EAPI Eina_Simple_XML_Node_Data * eina_simple_xml_node_data_new(Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length);
249
250/**
251 * Remove data from parent and delete it.
252 *
253 * @param data to release memory.
254 */
255EAPI void eina_simple_xml_node_data_free(Eina_Simple_XML_Node_Data *node);
256
257
258/**
259 * Create new cdata. If parent is provided, it is automatically appended.
260 *
261 * @param parent if provided, will be set in the resulting structure
262 * as well as the cdata will be appended to children list.
263 * @param content string to be used. Must not be NULL.
264 * @param length size in bytes of @a content.
265 *
266 * @return newly allocated memory or NULL on error. This memory should be
267 * released with eina_simple_xml_node_cdata_free() or indirectly
268 * with eina_simple_xml_node_tag_free() of the parent.
269 */
270EAPI Eina_Simple_XML_Node_CData * eina_simple_xml_node_cdata_new(Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length);
271
272/**
273 * Remove cdata from parent and delete it.
274 *
275 * @param cdata to release memory.
276 */
277EAPI void eina_simple_xml_node_cdata_free(Eina_Simple_XML_Node_Data *node);
278
279
280/**
281 * Create new processing. If parent is provided, it is automatically appended.
282 *
283 * @param parent if provided, will be set in the resulting structure
284 * as well as the processing will be appended to children list.
285 * @param content string to be used. Must not be NULL.
286 * @param length size in bytes of @a content.
287 *
288 * @return newly allocated memory or NULL on error. This memory should be
289 * released with eina_simple_xml_node_processing_free() or indirectly
290 * with eina_simple_xml_node_tag_free() of the parent.
291 */
292EAPI Eina_Simple_XML_Node_Processing * eina_simple_xml_node_processing_new(Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length);
293
294/**
295 * Remove processing from parent and delete it.
296 *
297 * @param processing to release memory.
298 */
299EAPI void eina_simple_xml_node_processing_free(Eina_Simple_XML_Node_Data *node);
300
301
302/**
303 * Create new doctype. If parent is provided, it is automatically appended.
304 *
305 * @param parent if provided, will be set in the resulting structure
306 * as well as the doctype will be appended to children list.
307 * @param content string to be used. Must not be NULL.
308 * @param length size in bytes of @a content.
309 *
310 * @return newly allocated memory or NULL on error. This memory should be
311 * released with eina_simple_xml_node_doctype_free() or indirectly
312 * with eina_simple_xml_node_tag_free() of the parent.
313 */
314EAPI Eina_Simple_XML_Node_Doctype * eina_simple_xml_node_doctype_new(Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length);
315
316/**
317 * Remove doctype from parent and delete it.
318 *
319 * @param doctype to release memory.
320 */
321EAPI void eina_simple_xml_node_doctype_free(Eina_Simple_XML_Node_Data *node);
322
323
324/**
325 * Create new comment. If parent is provided, it is automatically appended.
326 *
327 * @param parent if provided, will be set in the resulting structure
328 * as well as the comment will be appended to children list.
329 * @param content string to be used. Must not be NULL.
330 * @param length size in bytes of @a content.
331 *
332 * @return newly allocated memory or NULL on error. This memory should be
333 * released with eina_simple_xml_node_comment_free() or indirectly
334 * with eina_simple_xml_node_tag_free() of the parent.
335 */
336EAPI Eina_Simple_XML_Node_Comment * eina_simple_xml_node_comment_new(Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length);
337
338/**
339 * Remove comment from parent and delete it.
340 *
341 * @param comment to release memory.
342 */
343EAPI void eina_simple_xml_node_comment_free(Eina_Simple_XML_Node_Data *node);
344
345
346/**
347 * Load a XML node tree based on the given string.
348 *
349 * @param buf the input string. May not contain \0 terminator.
350 * @param buflen the input string size.
351 * @param strip whenever this parser should strip leading and trailing
352 * whitespace.
353 *
354 * @return document root with children tags, or NULL on errors.
355 * Document with errors may return partial tree instead of NULL,
356 * we'll do our best to avoid returning nothing.
357 */
358EAPI Eina_Simple_XML_Node_Root * eina_simple_xml_node_load(const char *buf, unsigned buflen, Eina_Bool strip);
359
360/**
361 * Free node tree build with eina_simple_xml_node_load()
362 *
363 * @param root memory returned by eina_simple_xml_node_load()
364 */
365EAPI void eina_simple_xml_node_root_free(Eina_Simple_XML_Node_Root *root);
366
367/**
368 * Converts the node tree under the given element to a XML string.
369 *
370 * @param node the base node to convert.
371 * @param indent indentation string, or NULL to disable it.
372 *
373 * @param NULL on errors or a newly allocated string on success.
374 */
375EAPI char * eina_simple_xml_node_dump(Eina_Simple_XML_Node *node, const char *indent);
376
377
378/**
379 * @}
380 */
381
382/**
383 * @}
384 */
385
386#endif /* EINA_SIMPLE_XML_H_ */