1 /*****************************************************************************
2 * xlist.c : a trivial parser for XML-like tags
3 *****************************************************************************
4 * Copyright (C) 2003-2004 Commonwealth Scientific and Industrial Research
5 * Organisation (CSIRO) Australia
6 * Copyright (C) 2000-2004 the VideoLAN team
10 * Authors: Conrad Parker <Conrad.Parker@csiro.au>
11 * Andre Pang <Andre.Pang@csiro.au>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
48 #define MIN(a,b) ((a)<(b)?(a):(b))
51 #define MAX(a,b) ((a)>(b)?(a):(b))
53 typedef struct _XTag XTag;
54 typedef struct _XAttribute XAttribute;
55 typedef struct _XTagParser XTagParser;
58 * struct _XTag is kind of a union ... it normally represents a whole
59 * tag (and its children), but it could alternatively represent some
60 * PCDATA. Basically, if tag->pcdata is non-NULL, interpret only it and
61 * ignore the name, attributes and inner_tags.
69 XList * current_child;
78 int valid; /* boolean */
84 XTag * xtag_free (XTag * xtag);
85 XTag * xtag_new_parse (const char * s, int n);
86 char * xtag_get_name (XTag * xtag);
87 char * xtag_get_pcdata (XTag * xtag);
88 char * xtag_get_attribute (XTag * xtag, char * attribute);
89 XTag * xtag_first_child (XTag * xtag, char * name);
90 XTag * xtag_next_child (XTag * xtag, char * name);
91 int xtag_snprint (char * buf, int n, XTag * xtag);
93 /* Character classes */
95 #define X_WHITESPACE 1<<0
96 #define X_OPENTAG 1<<1
97 #define X_CLOSETAG 1<<2
104 xtag_cin (char c, int char_class)
106 if (char_class & X_WHITESPACE)
107 if (isspace(c)) return TRUE;
109 if (char_class & X_OPENTAG)
110 if (c == '<') return TRUE;
112 if (char_class & X_CLOSETAG)
113 if (c == '>') return TRUE;
115 if (char_class & X_DQUOTE)
116 if (c == '"') return TRUE;
118 if (char_class & X_SQUOTE)
119 if (c == '\'') return TRUE;
121 if (char_class & X_EQUAL)
122 if (c == '=') return TRUE;
124 if (char_class & X_SLASH)
125 if (c == '/') return TRUE;
131 xtag_index (XTagParser * parser, int char_class)
138 for (i = 0; s[i] && s != parser->end; i++) {
139 if (xtag_cin(s[i], char_class)) return i;
146 xtag_skip_over (XTagParser * parser, int char_class)
151 if (!parser->valid) return;
153 s = (char *)parser->start;
155 for (i = 0; s[i] && s != parser->end; i++) {
156 if (!xtag_cin(s[i], char_class)) {
157 parser->start = &s[i];
166 xtag_skip_whitespace (XTagParser * parser)
168 xtag_skip_over (parser, X_WHITESPACE);
173 xtag_skip_to (XTagParser * parser, int char_class)
178 if (!parser->valid) return;
180 s = (char *)parser->start;
182 for (i = 0; s[i] && s != parser->end; i++) {
183 if (xtag_cin(s[i], char_class)) {
184 parser->start = &s[i];
194 xtag_slurp_to (XTagParser * parser, int good_end, int bad_end)
199 if (!parser->valid) return NULL;
203 xi = xtag_index (parser, good_end | bad_end);
205 if (xi > 0 && xtag_cin (s[xi], good_end)) {
206 ret = malloc ((xi+1) * sizeof(char));
207 strncpy (ret, s, xi);
209 parser->start = &s[xi];
217 xtag_assert_and_pass (XTagParser * parser, int char_class)
221 if (!parser->valid) return FALSE;
225 if (!xtag_cin (s[0], char_class)) {
226 parser->valid = FALSE;
230 parser->start = &s[1];
236 xtag_slurp_quoted (XTagParser * parser)
239 int quote = X_DQUOTE; /* quote char to match on */
242 if (!parser->valid) return NULL;
244 xtag_skip_whitespace (parser);
248 if (xtag_cin (s[0], X_SQUOTE)) quote = X_SQUOTE;
250 if (!xtag_assert_and_pass (parser, quote)) return NULL;
254 for (xi = 0; s[xi]; xi++) {
255 if (xtag_cin (s[xi], quote)) {
256 if (!(xi > 1 && s[xi-1] == '\\')) break;
260 ret = malloc ((xi+1) * sizeof(char));
261 strncpy (ret, s, xi);
263 parser->start = &s[xi];
265 if (!xtag_assert_and_pass (parser, quote)) return NULL;
271 xtag_parse_attribute (XTagParser * parser)
274 char * name, * value;
277 if (!parser->valid) return NULL;
279 xtag_skip_whitespace (parser);
281 name = xtag_slurp_to (parser, X_WHITESPACE | X_EQUAL, X_SLASH | X_CLOSETAG);
283 if (name == NULL) return NULL;
285 xtag_skip_whitespace (parser);
288 if (!xtag_assert_and_pass (parser, X_EQUAL)) {
290 printf ("xtag: attr failed EQUAL on <%s>\n", name);
295 xtag_skip_whitespace (parser);
297 value = xtag_slurp_quoted (parser);
301 printf ("Got NULL quoted attribute value\n");
306 attr = malloc (sizeof (*attr));
315 parser->valid = FALSE;
321 xtag_parse_tag (XTagParser * parser)
329 if (!parser->valid) return NULL;
331 if ((pcdata = xtag_slurp_to (parser, X_OPENTAG, X_NONE)) != NULL) {
332 tag = malloc (sizeof (*tag));
334 tag->pcdata = pcdata;
335 tag->parent = parser->current_tag;
336 tag->attributes = NULL;
337 tag->children = NULL;
338 tag->current_child = NULL;
345 /* if this starts a close tag, return NULL and let the parent take it */
346 if (xtag_cin (s[0], X_OPENTAG) && xtag_cin (s[1], X_SLASH))
349 if (!xtag_assert_and_pass (parser, X_OPENTAG)) return NULL;
351 name = xtag_slurp_to (parser, X_WHITESPACE | X_SLASH | X_CLOSETAG, X_NONE);
353 if (name == NULL) return NULL;
356 printf ("<%s ...\n", name);
359 tag = malloc (sizeof (*tag));
362 tag->parent = parser->current_tag;
363 tag->attributes = NULL;
364 tag->children = NULL;
365 tag->current_child = NULL;
369 if (xtag_cin (s[0], X_WHITESPACE)) {
370 while ((attr = xtag_parse_attribute (parser)) != NULL) {
371 tag->attributes = xlist_append (tag->attributes, attr);
375 xtag_skip_whitespace (parser);
379 if (xtag_cin (s[0], X_CLOSETAG)) {
380 parser->current_tag = tag;
382 xtag_assert_and_pass (parser, X_CLOSETAG);
384 while ((inner = xtag_parse_tag (parser)) != NULL) {
385 tag->children = xlist_append (tag->children, inner);
388 xtag_skip_whitespace (parser);
390 xtag_assert_and_pass (parser, X_OPENTAG);
391 xtag_assert_and_pass (parser, X_SLASH);
392 name = xtag_slurp_to (parser, X_WHITESPACE | X_CLOSETAG, X_NONE);
394 if (name && tag->name && strcmp (name, tag->name)) {
396 printf ("got %s expected %s\n", name, tag->name);
398 parser->valid = FALSE;
403 xtag_skip_whitespace (parser);
404 xtag_assert_and_pass (parser, X_CLOSETAG);
407 xtag_assert_and_pass (parser, X_SLASH);
408 xtag_assert_and_pass (parser, X_CLOSETAG);
416 xtag_free (XTag * xtag)
422 if (xtag == NULL) return NULL;
424 if (xtag->name) free (xtag->name);
425 if (xtag->pcdata) free (xtag->pcdata);
427 for (l = xtag->attributes; l; l = l->next) {
428 if ((attr = (XAttribute *)l->data) != NULL) {
429 if (attr->name) free (attr->name);
430 if (attr->value) free (attr->value);
434 xlist_free (xtag->attributes);
436 for (l = xtag->children; l; l = l->next) {
437 child = (XTag *)l->data;
440 xlist_free (xtag->children);
448 xtag_new_parse (const char * s, int n)
451 XTag * tag, * ttag, * wrapper;
454 parser.current_tag = NULL;
455 parser.start = (char *)s;
462 parser.end = (char *)&s[n];
464 tag = xtag_parse_tag (&parser);
471 if ((ttag = xtag_parse_tag (&parser)) != NULL) {
478 wrapper = malloc (sizeof (XTag));
479 wrapper->name = NULL;
480 wrapper->pcdata = NULL;
481 wrapper->parent = NULL;
482 wrapper->attributes = NULL;
483 wrapper->children = NULL;
484 wrapper->current_child = NULL;
486 wrapper->children = xlist_append (wrapper->children, tag);
487 wrapper->children = xlist_append (wrapper->children, ttag);
489 while ((ttag = xtag_parse_tag (&parser)) != NULL) {
496 wrapper->children = xlist_append (wrapper->children, ttag);
505 xtag_get_name (XTag * xtag)
507 return xtag ? xtag->name : NULL;
511 xtag_get_pcdata (XTag * xtag)
516 if (xtag == NULL) return NULL;
518 for (l = xtag->children; l; l = l->next) {
519 child = (XTag *)l->data;
520 if (child->pcdata != NULL) {
521 return child->pcdata;
529 xtag_get_attribute (XTag * xtag, char * attribute)
534 if (xtag == NULL) return NULL;
536 for (l = xtag->attributes; l; l = l->next) {
537 if ((attr = (XAttribute *)l->data) != NULL) {
538 if (attr->name && attribute && !strcmp (attr->name, attribute))
547 xtag_first_child (XTag * xtag, char * name)
552 if (xtag == NULL) return NULL;
554 if ((l = xtag->children) == NULL) return NULL;
557 xtag->current_child = l;
558 return (XTag *)l->data;
561 for (; l; l = l->next) {
562 child = (XTag *)l->data;
564 if (child->name && name && !strcmp(child->name, name)) {
565 xtag->current_child = l;
570 xtag->current_child = NULL;
576 xtag_next_child (XTag * xtag, char * name)
581 if (xtag == NULL) return NULL;
583 if ((l = xtag->current_child) == NULL)
584 return xtag_first_child (xtag, name);
586 if ((l = l->next) == NULL)
590 xtag->current_child = l;
591 return (XTag *)l->data;
594 for (; l; l = l->next) {
595 child = (XTag *)l->data;
597 if (child->name && name && !strcmp(child->name, name)) {
598 xtag->current_child = l;
603 xtag->current_child = NULL;
609 * This snprints function takes a variable list of char *, the last of
610 * which must be NULL, and prints each in turn to buf.
611 * Returns C99-style total length that would have been written, even if
612 * this is larger than n.
615 xtag_snprints (char * buf, int n, ...)
619 int len, to_copy, total = 0;
623 for (s = va_arg (ap, char *); s; s = va_arg (ap, char *)) {
626 if ((to_copy = MIN (n, len)) > 0) {
627 memcpy (buf, s, to_copy);
641 xtag_snprint (char * buf, int n, XTag * xtag)
654 if (n > 0) buf[0] = '\0';
659 nn = xtag_snprints (buf, n, xtag->pcdata, NULL);
666 nn = xtag_snprints (buf, n, "<", xtag->name, NULL);
669 for (l = xtag->attributes; l; l = l->next) {
670 attr = (XAttribute *)l->data;
672 nn = xtag_snprints (buf, n, " ", attr->name, "=\"", attr->value, "\"",
677 if (xtag->children == NULL) {
678 nn = xtag_snprints (buf, n, "/>", NULL);
684 nn = xtag_snprints (buf, n, ">", NULL);
688 for (l = xtag->children; l; l = l->next) {
689 child = (XTag *)l->data;
691 nn = xtag_snprint (buf, n, child);
696 nn = xtag_snprints (buf, n, "</", xtag->name, ">", NULL);