VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
ezxml.h
Go to the documentation of this file.
1 /* ezxml.h
2  *
3  * Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef _EZXML_H
26 #define _EZXML_H
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <fcntl.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define EZXML_BUFSIZE 1024 /* size of internal memory buffers */
38 #define EZXML_NAMEM 0x80 /* name is malloced */
39 #define EZXML_TXTM 0x40 /* txt is malloced */
40 #define EZXML_DUP 0x20 /* attribute name and value are strduped */
41 #define EZXML_ERRL 128 /* maximum error string length */
42 
43 typedef struct ezxml *ezxml_t;
44 struct ezxml {
45  char *name; /* tag name */
46  char **attr; /* tag attributes { name, value, name, value, ... NULL } */
47  char *txt; /* tag character content, empty string if none */
48  size_t off; /* tag offset from start of parent tag character content */
49  ezxml_t next; /* next tag with same name in this section at this depth */
50  ezxml_t sibling; /* next tag with different name in same section and depth */
51  ezxml_t ordered; /* next tag, same section and depth, in original order */
52  ezxml_t child; /* head of sub tag list, NULL if none */
53  ezxml_t parent; /* parent tag, NULL if current tag is root tag */
54  short flags; /* additional information */
55  /* Jason Luu June 22, 2010, Added line number support */
56  int line;
57 };
58 
59 /* Jason Luu June 22, 2010, Moved root definition to header */
60 typedef struct ezxml_root *ezxml_root_t;
61 struct ezxml_root { /* additional data for the root tag */
62  struct ezxml xml; /* is a super-struct built on top of ezxml struct */
63  ezxml_t cur; /* current xml tree insertion point */
64  char *m; /* original xml string */
65  size_t len; /* length of allocated memory for mmap, -1 for malloc */
66  char *u; /* UTF-8 conversion of string if original was UTF-16 */
67  char *s; /* start of work area */
68  char *e; /* end of work area */
69  char **ent; /* general entities (ampersand sequences) */
70  char ***attr; /* default attributes */
71  char ***pi; /* processing instructions */
72  short standalone; /* non-zero if <?xml standalone="yes"?> */
73  char err[EZXML_ERRL]; /* error string */
74 };
75 
76 /* Given a string of xml data and its length, parses it and creates an ezxml */
77 /* structure. For efficiency, modifies the data by adding null terminators */
78 /* and decoding ampersand sequences. If you don't want this, copy the data and */
79 /* pass in the copy. Returns NULL on failure. */
80 ezxml_t ezxml_parse_str(char *s, size_t len);
81 
82 /* A wrapper for ezxml_parse_str() that accepts a file descriptor. First */
83 /* attempts to mem map the file. Failing that, reads the file into memory. */
84 /* Returns NULL on failure. */
85 ezxml_t ezxml_parse_fd(int fd);
86 
87 /* a wrapper for ezxml_parse_fd() that accepts a file name */
88 ezxml_t ezxml_parse_file(const char *file);
89 
90 /* Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire */
91 /* stream into memory and then parses it. For xml files, use ezxml_parse_file() */
92 /* or ezxml_parse_fd() */
93 ezxml_t ezxml_parse_fp(FILE * fp);
94 
95 /* returns the first child tag (one level deeper) with the given name or NULL */
96 /* if not found */
97 ezxml_t ezxml_child(ezxml_t xml, const char *name);
98 
99 /* returns the next tag of the same name in the same section and depth or NULL */
100 /* if not found */
101 #define ezxml_next(xml) ((xml) ? xml->next : NULL)
102 
103 /* Returns the Nth tag with the same name in the same section at the same depth */
104 /* or NULL if not found. An index of 0 returns the tag given. */
105 ezxml_t ezxml_idx(ezxml_t xml, int idx);
106 
107 /* returns the name of the given tag */
108 #define ezxml_name(xml) ((xml) ? xml->name : NULL)
109 
110 /* returns the given tag's character content or empty string if none */
111 #define ezxml_txt(xml) ((xml) ? xml->txt : "")
112 
113 /* returns the value of the requested tag attribute, or NULL if not found */
114 const char *ezxml_attr(ezxml_t xml, const char *attr);
115 
116 /* Traverses the ezxml sturcture to retrieve a specific subtag. Takes a */
117 /* variable length list of tag names and indexes. The argument list must be */
118 /* terminated by either an index of -1 or an empty string tag name. Example: */
119 /* title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1); */
120 /* This retrieves the title of the 3rd book on the 1st shelf of library. */
121 /* Returns NULL if not found. */
122 ezxml_t ezxml_get(ezxml_t xml, ...);
123 
124 /* Converts an ezxml structure back to xml. Returns a string of xml data that */
125 /* must be freed. */
126 char *ezxml_toxml(ezxml_t xml);
127 
128 /* returns a NULL terminated array of processing instructions for the given */
129 /* target */
130 char **ezxml_pi(ezxml_t xml, const char *target);
131 
132 /* frees the memory allocated for an ezxml structure */
133 void ezxml_free(ezxml_t xml);
134 
135 /* returns parser error message or empty string if none */
136 const char *ezxml_error(ezxml_t xml);
137 
138 /* returns a new empty ezxml structure with the given root tag name */
139 ezxml_t ezxml_new(char *name);
140 
141 /* wrapper for ezxml_new() that strdup()s name */
142 #define ezxml_new_d(name) ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
143 
144 /* Adds a child tag. off is the offset of the child tag relative to the start */
145 /* of the parent tag's character content. Returns the child tag. */
146 ezxml_t ezxml_add_child(ezxml_t xml, char *name, size_t off);
147 
148 /* wrapper for ezxml_add_child() that strdup()s name */
149 #define ezxml_add_child_d(xml, name, off) \
150  ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
151 
152 /* sets the character content for the given tag and returns the tag */
153 ezxml_t ezxml_set_txt(ezxml_t xml, char *txt);
154 
155 /* wrapper for ezxml_set_txt() that strdup()s txt */
156 #define ezxml_set_txt_d(xml, txt) \
157  ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
158 
159 /* Sets the given tag attribute or adds a new attribute if not found. A value */
160 /* of NULL will remove the specified attribute. Returns the tag given. */
161 ezxml_t ezxml_set_attr(ezxml_t xml, char *name, char *value);
162 
163 /* Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL */
164 #define ezxml_set_attr_d(xml, name, value) \
165  ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
166 
167 /* sets a flag for the given tag and returns the tag */
168 ezxml_t ezxml_set_flag(ezxml_t xml, short flag);
169 
170 /* removes a tag along with its subtags without freeing its memory */
171 ezxml_t ezxml_cut(ezxml_t xml);
172 
173 /* inserts an existing tag into an ezxml structure */
174 ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off);
175 
176 /* Moves an existing tag to become a subtag of dest at the given offset from */
177 /* the start of dest's character content. Returns the moved tag. */
178 #define ezxml_move(xml, dest, off) ezxml_insert(ezxml_cut(xml), dest, off)
179 
180 /* removes a tag along with all its subtags */
181 #define ezxml_remove(xml) ezxml_free(ezxml_cut(xml))
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 #endif /* _EZXML_H */
char err[EZXML_ERRL]
Definition: ezxml.h:73
ezxml_t ezxml_child(ezxml_t xml, const char *name)
Definition: ezxml.c:85
Definition: ezxml.h:44
size_t len
Definition: ezxml.h:65
char * u
Definition: ezxml.h:66
struct ezxml_root * ezxml_root_t
Definition: ezxml.h:60
const char * ezxml_error(ezxml_t xml)
Definition: ezxml.c:1067
char * txt
Definition: ezxml.h:47
ezxml_t ezxml_set_attr(ezxml_t xml, char *name, char *value)
Definition: ezxml.c:1165
ezxml_t ezxml_new(char *name)
Definition: ezxml.c:1074
ezxml_t ezxml_set_txt(ezxml_t xml, char *txt)
Definition: ezxml.c:1152
#define EZXML_ERRL
Definition: ezxml.h:41
void ezxml_free(ezxml_t xml)
Definition: ezxml.c:1011
ezxml_t ezxml_parse_file(const char *file)
Definition: ezxml.c:846
char *** attr
Definition: ezxml.h:70
ezxml_t next
Definition: ezxml.h:49
ezxml_t ezxml_parse_fp(FILE *fp)
Definition: ezxml.c:790
ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off)
Definition: ezxml.c:1089
ezxml_t sibling
Definition: ezxml.h:50
ezxml_t cur
Definition: ezxml.h:63
const char * ezxml_attr(ezxml_t xml, const char *attr)
Definition: ezxml.c:102
char * e
Definition: ezxml.h:68
char ** ezxml_pi(ezxml_t xml, const char *target)
Definition: ezxml.c:156
int line
Definition: ezxml.h:56
ezxml_t ezxml_parse_fd(int fd)
Definition: ezxml.c:816
char ** ent
Definition: ezxml.h:69
ezxml_t ordered
Definition: ezxml.h:51
ezxml_t ezxml_idx(ezxml_t xml, int idx)
Definition: ezxml.c:94
char * m
Definition: ezxml.h:64
ezxml_t parent
Definition: ezxml.h:53
struct ezxml xml
Definition: ezxml.h:62
char * name
Definition: ezxml.h:45
char * s
Definition: ezxml.h:67
char * ezxml_toxml(ezxml_t xml)
Definition: ezxml.c:963
char ** attr
Definition: ezxml.h:46
size_t off
Definition: ezxml.h:48
short standalone
Definition: ezxml.h:72
ezxml_t ezxml_parse_str(char *s, size_t len)
Definition: ezxml.c:596
struct ezxml * ezxml_t
Definition: ezxml.h:43
short flags
Definition: ezxml.h:54
char *** pi
Definition: ezxml.h:71
ezxml_t ezxml_get(ezxml_t xml,...)
Definition: ezxml.c:143
ezxml_t ezxml_add_child(ezxml_t xml, char *name, size_t off)
Definition: ezxml.c:1137
ezxml_t child
Definition: ezxml.h:52
ezxml_t ezxml_set_flag(ezxml_t xml, short flag)
Definition: ezxml.c:1220
ezxml_t ezxml_cut(ezxml_t xml)
Definition: ezxml.c:1227