1 //==========================================================================
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2005 eCosCentric Ltd
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): Aaron Voisine
44 // Contributors: Matt Jerdonek
49 // This code is part of eCos (tm).
51 //####DESCRIPTIONEND####
53 //==========================================================================
57 * Copyright 2004, 2005 Aaron Voisine <aaron@voisine.org>
59 * Permission is hereby granted, free of charge, to any person obtaining
60 * a copy of this software and associated documentation files (the
61 * "Software"), to deal in the Software without restriction, including
62 * without limitation the rights to use, copy, modify, merge, publish,
63 * distribute, sublicense, and/or sell copies of the Software, and to
64 * permit persons to whom the Software is furnished to do so, subject to
65 * the following conditions:
67 * The above copyright notice and this permission notice shall be included
68 * in all copies or substantial portions of the Software.
70 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
71 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
72 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
73 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
74 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
75 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
76 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
91 #define EZXML_BUFSIZE 1024 // size of internal memory buffers
92 #define EZXML_NAMEM 0x80 // name is malloced
93 #define EZXML_TXTM 0x40 // txt is malloced
94 #define EZXML_DUP 0x20 // attribute name and value are strduped
96 typedef struct ezxml *ezxml_t;
98 char *name; // tag name
99 char **attr; // tag attributes { name, value, name, value, ... NULL }
100 char *txt; // tag character content, empty string if none
101 size_t off; // tag offset from start of parent tag character content
102 ezxml_t next; // next tag with same name in this section at this depth
103 ezxml_t sibling; // next tag with different name in same section and depth
104 ezxml_t ordered; // next tag, same section and depth, in original order
105 ezxml_t child; // head of sub tag list, NULL if none
106 ezxml_t parent; // parent tag, NULL if current tag is root tag
107 short flags; // additional information
110 // Given a string of xml data and its length, parses it and creates an ezxml
111 // structure. For efficiency, modifies the data by adding null terminators
112 // and decoding ampersand sequences. If you don't want this, copy the data and
113 // pass in the copy. Returns NULL on failure.
114 ezxml_t ezxml_parse_str(char *s, size_t len);
116 // A wrapper for ezxml_parse_str() that accepts a file descriptor. First
117 // attempts to mem map the file. Failing that, reads the file into memory.
118 // Returns NULL on failure.
119 ezxml_t ezxml_parse_fd(int fd);
121 // a wrapper for ezxml_parse_fd() that accepts a file name
122 ezxml_t ezxml_parse_file(const char *file);
124 // Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
125 // stream into memory and then parses it. For xml files, use ezxml_parse_file()
126 // or ezxml_parse_fd()
127 ezxml_t ezxml_parse_fp(FILE *fp);
129 // returns the first child tag (one level deeper) with the given name or NULL
131 ezxml_t ezxml_child(ezxml_t xml, const char *name);
133 // returns the next tag of the same name in the same section and depth or NULL
135 #define ezxml_next(xml) ((xml) ? xml->next : NULL)
137 // Returns the Nth tag with the same name in the same section at the same depth
138 // or NULL if not found. An index of 0 returns the tag given.
139 ezxml_t ezxml_idx(ezxml_t xml, int idx);
141 // returns the name of the given tag
142 #define ezxml_name(xml) ((xml) ? xml->name : NULL)
144 // returns the given tag's character content or empty string if none
145 #define ezxml_txt(xml) ((xml) ? xml->txt : "")
147 // returns the value of the requested tag attribute, or NULL if not found
148 const char *ezxml_attr(ezxml_t xml, const char *attr);
150 // Traverses the ezxml sturcture to retrieve a specific subtag. Takes a
151 // variable length list of tag names and indexes. The argument list must be
152 // terminated by either an index of -1 or an empty string tag name. Example:
153 // title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
154 // This retrieves the title of the 3rd book on the 1st shelf of library.
155 // Returns NULL if not found.
156 ezxml_t ezxml_get(ezxml_t xml, ...);
158 // Converts an ezxml structure back to xml. Returns a string of xml data that
160 char *ezxml_toxml(ezxml_t xml);
162 // returns a NULL terminated array of processing instructions for the given
164 const char **ezxml_pi(ezxml_t xml, const char *target);
166 // frees the memory allocated for an ezxml structure
167 void ezxml_free(ezxml_t xml);
169 // returns parser error message or empty string if none
170 const char *ezxml_error(ezxml_t xml);
172 // returns a new empty ezxml structure with the given root tag name
173 ezxml_t ezxml_new(const char *name);
175 // wrapper for ezxml_new() that strdup()s name
176 #define ezxml_new_d(name) ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
178 // Adds a child tag. off is the offset of the child tag relative to the start
179 // of the parent tag's character content. Returns the child tag.
180 ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off);
182 // wrapper for ezxml_add_child() that strdup()s name
183 #define ezxml_add_child_d(xml, name, off) \
184 ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
186 // sets the character content for the given tag and returns the tag
187 ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt);
189 // wrapper for ezxml_set_txt() that strdup()s txt
190 #define ezxml_set_txt_d(xml, txt) \
191 ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
193 // Sets the given tag attribute or adds a new attribute if not found. A value
194 // of NULL will remove the specified attribute.
195 void ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
197 // Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL
198 #define ezxml_set_attr_d(xml, name, value) \
199 ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
201 // sets a flag for the given tag and returns the tag
202 ezxml_t ezxml_set_flag(ezxml_t xml, short flag);
204 // removes a tag along with all its subtags
205 void ezxml_remove(ezxml_t xml);