1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ezxml.h
|
4 |
|
|
//
|
5 |
|
|
// Simple XML parser
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2005 Free Software Foundation, Inc.
|
12 |
|
|
//
|
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
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Aaron Voisine
|
43 |
|
|
// Contributors: Matt Jerdonek
|
44 |
|
|
// Date: 2005-01-31
|
45 |
|
|
// Purpose:
|
46 |
|
|
// Description:
|
47 |
|
|
//
|
48 |
|
|
// This code is part of eCos (tm).
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//==========================================================================
|
53 |
|
|
|
54 |
|
|
/* ezxml.h
|
55 |
|
|
*
|
56 |
|
|
* Copyright 2004, 2005 Aaron Voisine <aaron@voisine.org>
|
57 |
|
|
*
|
58 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
59 |
|
|
* a copy of this software and associated documentation files (the
|
60 |
|
|
* "Software"), to deal in the Software without restriction, including
|
61 |
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
62 |
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
63 |
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
64 |
|
|
* the following conditions:
|
65 |
|
|
*
|
66 |
|
|
* The above copyright notice and this permission notice shall be included
|
67 |
|
|
* in all copies or substantial portions of the Software.
|
68 |
|
|
*
|
69 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
70 |
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
71 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
72 |
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
73 |
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
74 |
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
75 |
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
76 |
|
|
*/
|
77 |
|
|
|
78 |
|
|
#ifndef _EZXML_H
|
79 |
|
|
#define _EZXML_H
|
80 |
|
|
|
81 |
|
|
#include <stdlib.h>
|
82 |
|
|
#include <stdio.h>
|
83 |
|
|
#include <stdarg.h>
|
84 |
|
|
#include <fcntl.h>
|
85 |
|
|
|
86 |
|
|
#ifdef __cplusplus
|
87 |
|
|
extern "C" {
|
88 |
|
|
#endif
|
89 |
|
|
|
90 |
|
|
#define EZXML_BUFSIZE 1024 // size of internal memory buffers
|
91 |
|
|
#define EZXML_NAMEM 0x80 // name is malloced
|
92 |
|
|
#define EZXML_TXTM 0x40 // txt is malloced
|
93 |
|
|
#define EZXML_DUP 0x20 // attribute name and value are strduped
|
94 |
|
|
|
95 |
|
|
typedef struct ezxml *ezxml_t;
|
96 |
|
|
struct ezxml {
|
97 |
|
|
char *name; // tag name
|
98 |
|
|
char **attr; // tag attributes { name, value, name, value, ... NULL }
|
99 |
|
|
char *txt; // tag character content, empty string if none
|
100 |
|
|
size_t off; // tag offset from start of parent tag character content
|
101 |
|
|
ezxml_t next; // next tag with same name in this section at this depth
|
102 |
|
|
ezxml_t sibling; // next tag with different name in same section and depth
|
103 |
|
|
ezxml_t ordered; // next tag, same section and depth, in original order
|
104 |
|
|
ezxml_t child; // head of sub tag list, NULL if none
|
105 |
|
|
ezxml_t parent; // parent tag, NULL if current tag is root tag
|
106 |
|
|
short flags; // additional information
|
107 |
|
|
};
|
108 |
|
|
|
109 |
|
|
// Given a string of xml data and its length, parses it and creates an ezxml
|
110 |
|
|
// structure. For efficiency, modifies the data by adding null terminators
|
111 |
|
|
// and decoding ampersand sequences. If you don't want this, copy the data and
|
112 |
|
|
// pass in the copy. Returns NULL on failure.
|
113 |
|
|
ezxml_t ezxml_parse_str(char *s, size_t len);
|
114 |
|
|
|
115 |
|
|
// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
|
116 |
|
|
// attempts to mem map the file. Failing that, reads the file into memory.
|
117 |
|
|
// Returns NULL on failure.
|
118 |
|
|
ezxml_t ezxml_parse_fd(int fd);
|
119 |
|
|
|
120 |
|
|
// a wrapper for ezxml_parse_fd() that accepts a file name
|
121 |
|
|
ezxml_t ezxml_parse_file(const char *file);
|
122 |
|
|
|
123 |
|
|
// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
|
124 |
|
|
// stream into memory and then parses it. For xml files, use ezxml_parse_file()
|
125 |
|
|
// or ezxml_parse_fd()
|
126 |
|
|
ezxml_t ezxml_parse_fp(FILE *fp);
|
127 |
|
|
|
128 |
|
|
// returns the first child tag (one level deeper) with the given name or NULL
|
129 |
|
|
// if not found
|
130 |
|
|
ezxml_t ezxml_child(ezxml_t xml, const char *name);
|
131 |
|
|
|
132 |
|
|
// returns the next tag of the same name in the same section and depth or NULL
|
133 |
|
|
// if not found
|
134 |
|
|
#define ezxml_next(xml) ((xml) ? xml->next : NULL)
|
135 |
|
|
|
136 |
|
|
// Returns the Nth tag with the same name in the same section at the same depth
|
137 |
|
|
// or NULL if not found. An index of 0 returns the tag given.
|
138 |
|
|
ezxml_t ezxml_idx(ezxml_t xml, int idx);
|
139 |
|
|
|
140 |
|
|
// returns the name of the given tag
|
141 |
|
|
#define ezxml_name(xml) ((xml) ? xml->name : NULL)
|
142 |
|
|
|
143 |
|
|
// returns the given tag's character content or empty string if none
|
144 |
|
|
#define ezxml_txt(xml) ((xml) ? xml->txt : "")
|
145 |
|
|
|
146 |
|
|
// returns the value of the requested tag attribute, or NULL if not found
|
147 |
|
|
const char *ezxml_attr(ezxml_t xml, const char *attr);
|
148 |
|
|
|
149 |
|
|
// Traverses the ezxml sturcture to retrieve a specific subtag. Takes a
|
150 |
|
|
// variable length list of tag names and indexes. The argument list must be
|
151 |
|
|
// terminated by either an index of -1 or an empty string tag name. Example:
|
152 |
|
|
// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
|
153 |
|
|
// This retrieves the title of the 3rd book on the 1st shelf of library.
|
154 |
|
|
// Returns NULL if not found.
|
155 |
|
|
ezxml_t ezxml_get(ezxml_t xml, ...);
|
156 |
|
|
|
157 |
|
|
// Converts an ezxml structure back to xml. Returns a string of xml data that
|
158 |
|
|
// must be freed.
|
159 |
|
|
char *ezxml_toxml(ezxml_t xml);
|
160 |
|
|
|
161 |
|
|
// returns a NULL terminated array of processing instructions for the given
|
162 |
|
|
// target
|
163 |
|
|
const char **ezxml_pi(ezxml_t xml, const char *target);
|
164 |
|
|
|
165 |
|
|
// frees the memory allocated for an ezxml structure
|
166 |
|
|
void ezxml_free(ezxml_t xml);
|
167 |
|
|
|
168 |
|
|
// returns parser error message or empty string if none
|
169 |
|
|
const char *ezxml_error(ezxml_t xml);
|
170 |
|
|
|
171 |
|
|
// returns a new empty ezxml structure with the given root tag name
|
172 |
|
|
ezxml_t ezxml_new(const char *name);
|
173 |
|
|
|
174 |
|
|
// wrapper for ezxml_new() that strdup()s name
|
175 |
|
|
#define ezxml_new_d(name) ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
|
176 |
|
|
|
177 |
|
|
// Adds a child tag. off is the offset of the child tag relative to the start
|
178 |
|
|
// of the parent tag's character content. Returns the child tag.
|
179 |
|
|
ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off);
|
180 |
|
|
|
181 |
|
|
// wrapper for ezxml_add_child() that strdup()s name
|
182 |
|
|
#define ezxml_add_child_d(xml, name, off) \
|
183 |
|
|
ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
|
184 |
|
|
|
185 |
|
|
// sets the character content for the given tag and returns the tag
|
186 |
|
|
ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt);
|
187 |
|
|
|
188 |
|
|
// wrapper for ezxml_set_txt() that strdup()s txt
|
189 |
|
|
#define ezxml_set_txt_d(xml, txt) \
|
190 |
|
|
ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
|
191 |
|
|
|
192 |
|
|
// Sets the given tag attribute or adds a new attribute if not found. A value
|
193 |
|
|
// of NULL will remove the specified attribute.
|
194 |
|
|
void ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
|
195 |
|
|
|
196 |
|
|
// Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL
|
197 |
|
|
#define ezxml_set_attr_d(xml, name, value) \
|
198 |
|
|
ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
|
199 |
|
|
|
200 |
|
|
// sets a flag for the given tag and returns the tag
|
201 |
|
|
ezxml_t ezxml_set_flag(ezxml_t xml, short flag);
|
202 |
|
|
|
203 |
|
|
// removes a tag along with all its subtags
|
204 |
|
|
void ezxml_remove(ezxml_t xml);
|
205 |
|
|
|
206 |
|
|
#ifdef __cplusplus
|
207 |
|
|
}
|
208 |
|
|
#endif
|
209 |
|
|
|
210 |
|
|
#endif // _EZXML_H
|