1 /* Licensed under GPL - see LICENSE file for details */
24 #define XML_ALL 0xFFFFFFFF
37 /* Allocate a new XmlNode */
38 static XmlNode* xml_new(char * name)
40 XmlNode * ret = malloc(sizeof(XmlNode));
45 ret->child = ret->next = NULL;
51 /* free a previously allocated XmlNode */
52 void xml_free(XmlNode *target)
55 for(i=0; i<target->nattrib*2; i++)
57 free(target->attrib[i]);
59 if(target->attrib)free(target->attrib);
60 if(target->child)xml_free(target->child);
61 if(target->next)xml_free(target->next);
66 /* Raise flags if we have a character of special meaning.
67 * This is where I've hidden the switch statements :-p
69 static int is_special(char item)
71 if((item >= 'a' && item <= 'z') || (item >= 'A' && item <='Z'))
73 if( item >= '0' && item <='9' )
75 if( item == 0x20 || item == '\t' || item == 0x0D || item == 0x0A )
85 if( item == '"' || item == '\'' )
90 /* Refresh the buffer, if possible */
91 static void xml_read_file(XMLBUF *xml)
97 size = fread( xml->buf, 1, xml->len, xml->fptr);
98 if( size != xml->len )
107 /* All reading of the XML buffer done through these two functions */
108 /*** read a byte without advancing the offset */
109 static char xml_peek(XMLBUF *xml)
111 return xml->buf[xml->read_index];
114 /*** read a byte and advance the offset */
115 static char xml_read_byte(XMLBUF *xml)
117 char ret = xml_peek(xml);
119 if(xml->read_index >= xml->len)
123 xml->read_index = xml->len;
126 xml->read_index = 0 ;
133 /* skip over bytes matching the is_special mask */
134 static void xml_skip( XMLBUF *xml, int mask)
136 while( is_special(xml_peek(xml)) & mask && !(xml->eof && xml->read_index >= xml->len) )
141 /* character matching tests for the feed functions */
142 static char quotechar = 0;
143 static int test_quote(const char x)
145 static int escaped=0;
146 if( escaped || '\\' == x )
156 static int feed_mask = 0;
157 static int test_mask(const char x)
159 return !(is_special(x) & feed_mask);
163 * char* xml_feed(x, test)
165 * Reads as many contiguous chars that pass test() into a newly allocated
168 * Instead of calling xml_read_byte and flogging realloc() for each byte,
169 * it checks the buffer itself.
171 static char* xml_feed( XMLBUF *xml, int (*test)(char) )
173 int offset = xml->read_index;
178 /* perform first and N middle realloc()'s */
179 while( test(xml->buf[offset]) )
183 if(offset >= xml->len)
185 delta = offset - xml->read_index;
186 ret = realloc(ret, size + delta + 1);
187 memcpy(ret+size, xml->buf + xml->read_index, delta);
190 if(xml->eof)return ret;
196 /* perform final realloc() if needed */
197 if(offset > xml->read_index)
199 delta = offset - xml->read_index;
200 ret = realloc(ret, size + delta + 1);
201 memcpy(ret+size, xml->buf + xml->read_index, delta);
202 xml->read_index = offset;
209 /* this reads attributes from tags, of the form...
211 * <tag attr1="some arguments" attr2=argument>
213 * It is aware of quotes, and will allow anything inside quoted arguments
215 static void xml_read_attr(struct XMLBUF *xml, XmlNode *node)
219 // how does this tag finish?
222 if( is_special(xml_peek(xml)) & (XML_CLOSE | XML_SLASH) )
226 node->attrib = realloc(node->attrib, n * 2 * sizeof(char*) );
227 node->attrib[--n*2+1] = 0;
229 feed_mask = XML_EQUALS | XML_SPACE | XML_CLOSE | XML_SLASH;
230 node->attrib[n*2] = xml_feed(xml, test_mask );
231 if( xml_peek(xml) == '=' )
234 if( is_special(xml_peek(xml)) & XML_QUOTE )
236 quotechar = xml_read_byte(xml);
237 node->attrib[n*2+1] = xml_feed(xml, test_quote);
242 feed_mask = XML_SPACE | XML_CLOSE | XML_SLASH;
243 node->attrib[n*2+1] = xml_feed(xml, test_mask);
246 xml_skip(xml, XML_SPACE);
250 /* The big decision maker, is it a regular node, or a text node.
251 * If it's a node, it will check if it should have children, and if so
252 * will recurse over them.
253 * Text nodes don't have children, so no recursing.
255 static XmlNode* xml_parse(struct XMLBUF *xml)
260 XmlNode **this, *ret = NULL;
264 xml_skip(xml, XML_SPACE); // skip whitespace
266 while( (xml->read_index < xml->len) || !xml->eof )
268 switch(is_special(xml_peek(xml)))
272 if(xml_peek(xml) == '/')
273 return ret; // parents close tag
275 feed_mask = XML_SPACE | XML_SLASH | XML_CLOSE;
276 *this = xml_new( xml_feed(xml, test_mask));
277 xml_skip(xml, XML_SPACE); // skip any whitespace
279 xml_read_attr(xml, *this); // read attributes
281 // how does this tag finish?
282 switch(is_special(xml_peek(xml)))
284 case XML_CLOSE: // child-nodes ahead
286 (*this)->child = xml_parse(xml);
287 xml_skip(xml, XML_ALL ^ XML_CLOSE);
290 case XML_SLASH: // self closing tag
297 default: // text node
299 xml_skip(xml, XML_SPACE); // skip any whitespace
300 feed_mask = XML_OPEN;
302 (*this)->attrib = malloc(sizeof(char*)*2);
303 (*this)->attrib[1] = NULL;
304 tmp = (*this)->attrib[0] = xml_feed(xml, test_mask);
306 /* trim the whitespace off the end of text nodes,
307 * by overwriting the spaces will null termination. */
308 toff = strlen(tmp)-1;
309 while( ( is_special(tmp[toff]) & XML_SPACE ) )
317 this = &(*this)->next;
318 xml_skip(xml, XML_SPACE); // skip whitespace
325 /* bootstrap the structures for xml_parse() to be able to get started */
326 XmlNode* xml_load(const char * filename)
331 // printf("xml_load(\"%s\");\n", filename);
335 xml.fptr = fopen(filename, "rb");
339 xml.buf = malloc(BUFFER+1);
343 goto xml_load_fail_malloc_buf;
347 ret = xml_parse(&xml);
350 xml_load_fail_malloc_buf:
355 /* very basic function that will get you the first node with a given name */
356 XmlNode * xml_find(XmlNode *xml, const char *name)
359 if(xml->name)if(!strcmp(xml->name, name))return xml;
362 ret = xml_find(xml->child, name);
367 ret = xml_find(xml->next, name);
373 /* very basic attribute lookup function */
374 char* xml_attr(XmlNode *x, const char *name)
377 for(i=0; i<x->nattrib; i++)
379 if(!strcmp(x->attrib[i*2], name))
380 return x->attrib[i*2+1];