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