VPR-6.0

libvpr/include/ezxml.h File Reference

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
Include dependency graph for ezxml.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ezxml
struct  ezxml_root

Defines

#define EZXML_BUFSIZE   1024
#define EZXML_NAMEM   0x80
#define EZXML_TXTM   0x40
#define EZXML_DUP   0x20
#define EZXML_ERRL   128
#define ezxml_next(xml)   ((xml) ? xml->next : NULL)
#define ezxml_name(xml)   ((xml) ? xml->name : NULL)
#define ezxml_txt(xml)   ((xml) ? xml->txt : "")
#define ezxml_new_d(name)   ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
#define ezxml_add_child_d(xml, name, off)   ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
#define ezxml_set_txt_d(xml, txt)   ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
#define ezxml_set_attr_d(xml, name, value)   ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
#define ezxml_move(xml, dest, off)   ezxml_insert(ezxml_cut(xml), dest, off)
#define ezxml_remove(xml)   ezxml_free(ezxml_cut(xml))

Typedefs

typedef struct ezxmlezxml_t
typedef struct ezxml_rootezxml_root_t

Functions

ezxml_t ezxml_parse_str (char *s, size_t len)
ezxml_t ezxml_parse_fd (int fd)
ezxml_t ezxml_parse_file (const char *file)
ezxml_t ezxml_parse_fp (FILE *fp)
ezxml_t ezxml_child (ezxml_t xml, const char *name)
ezxml_t ezxml_idx (ezxml_t xml, int idx)
const char * ezxml_attr (ezxml_t xml, const char *attr)
ezxml_t ezxml_get (ezxml_t xml,...)
char * ezxml_toxml (ezxml_t xml)
const char ** ezxml_pi (ezxml_t xml, const char *target)
void ezxml_free (ezxml_t xml)
const char * ezxml_error (ezxml_t xml)
ezxml_t ezxml_new (const char *name)
ezxml_t ezxml_add_child (ezxml_t xml, const char *name, size_t off)
ezxml_t ezxml_set_txt (ezxml_t xml, const char *txt)
ezxml_t ezxml_set_attr (ezxml_t xml, const char *name, const char *value)
ezxml_t ezxml_set_flag (ezxml_t xml, short flag)
ezxml_t ezxml_cut (ezxml_t xml)
ezxml_t ezxml_insert (ezxml_t xml, ezxml_t dest, size_t off)

Detailed Description

Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Definition in file ezxml.h.


Define Documentation

#define ezxml_add_child_d (   xml,
  name,
  off 
)    ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)

wrapper for ezxml_add_child() that strdup()s name

Definition at line 174 of file ezxml.h.

#define EZXML_BUFSIZE   1024

size of internal memory buffers

Definition at line 39 of file ezxml.h.

#define EZXML_DUP   0x20

attribute name and value are strduped

Definition at line 42 of file ezxml.h.

#define EZXML_ERRL   128

maximum error string length

Definition at line 43 of file ezxml.h.

#define ezxml_move (   xml,
  dest,
  off 
)    ezxml_insert(ezxml_cut(xml), dest, off)

Moves an existing tag to become a subtag of dest at the given offset from the start of dest's character content. Returns the moved tag.

Definition at line 211 of file ezxml.h.

#define ezxml_name (   xml)    ((xml) ? xml->name : NULL)

returns the name of the given tag

Definition at line 123 of file ezxml.h.

#define EZXML_NAMEM   0x80

name is malloced

Definition at line 40 of file ezxml.h.

#define ezxml_new_d (   name)    ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)

wrapper for ezxml_new() that strdup()s name

Definition at line 163 of file ezxml.h.

#define ezxml_next (   xml)    ((xml) ? xml->next : NULL)

returns the next tag of the same name in the same section and depth or NULL if not found

Definition at line 114 of file ezxml.h.

#define ezxml_remove (   xml)    ezxml_free(ezxml_cut(xml))

removes a tag along with all its subtags

Definition at line 214 of file ezxml.h.

#define ezxml_set_attr_d (   xml,
  name,
  value 
)    ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))

Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL

Definition at line 193 of file ezxml.h.

#define ezxml_set_txt_d (   xml,
  txt 
)    ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)

wrapper for ezxml_set_txt() that strdup()s txt

Definition at line 182 of file ezxml.h.

#define ezxml_txt (   xml)    ((xml) ? xml->txt : "")

returns the given tag's character content or empty string if none

Definition at line 126 of file ezxml.h.

#define EZXML_TXTM   0x40

txt is malloced

Definition at line 41 of file ezxml.h.


Typedef Documentation

typedef struct ezxml_root* ezxml_root_t

additional data for the root tag

Definition at line 64 of file ezxml.h.

typedef struct ezxml* ezxml_t

Definition at line 45 of file ezxml.h.


Function Documentation

ezxml_t ezxml_add_child ( ezxml_t  xml,
const char *  name,
size_t  off 
)

Adds a child tag. off is the offset of the child tag relative to the start of the parent tag's character content. Returns the child tag.

Definition at line 1317 of file ezxml.c.

{
    ezxml_t child;

    if(!xml)
        return NULL;
    child = (ezxml_t) memset(malloc(sizeof(struct ezxml)), '\0',
                             sizeof(struct ezxml));
    child->name = (char *)name;
    child->attr = EZXML_NIL;
    child->txt = "";

    return ezxml_insert(child, xml, off);
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* ezxml_attr ( ezxml_t  xml,
const char *  attr 
)

returns the value of the requested tag attribute, or NULL if not found

returns the value of the requested tag attribute or NULL if not found

Definition at line 93 of file ezxml.c.

{
    int i = 0, j = 1;
    ezxml_root_t root = (ezxml_root_t) xml;

    if(!xml || !xml->attr)
        return NULL;
    while(xml->attr[i] && strcmp(attr, xml->attr[i]))
        i += 2;
    if(xml->attr[i])
        return xml->attr[i + 1];        /* found attribute */

    while(root->xml.parent)
        root = (ezxml_root_t) root->xml.parent; /* root tag */
    for(i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++);
    if(!root->attr[i])
        return NULL;            /* no matching default attributes */
    while(root->attr[i][j] && strcmp(attr, root->attr[i][j]))
        j += 3;
    return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL;    /* found default */
}

Here is the caller graph for this function:

ezxml_t ezxml_child ( ezxml_t  xml,
const char *  name 
)

returns the first child tag (one level deeper) with the given name or NULL if not found

returns the first child tag with the given name or NULL if not found

Definition at line 71 of file ezxml.c.

{
    xml = (xml) ? xml->child : NULL;
    while(xml && strcmp(name, xml->name))
        xml = xml->sibling;
    return xml;
}

Here is the caller graph for this function:

ezxml_t ezxml_cut ( ezxml_t  xml)

removes a tag along with its subtags without freeing its memory

Definition at line 1426 of file ezxml.c.

{
    ezxml_t cur;

    if(!xml)
        return NULL;            /* nothing to do */
    if(xml->next)
        xml->next->sibling = xml->sibling;      /* patch sibling list */

    if(xml->parent)
        {                       /* not root tag */
            cur = xml->parent->child;   /* find head of subtag list */
            if(cur == xml)
                xml->parent->child = xml->ordered;      /* first subtag */
            else
                {               /* not first subtag */
                    while(cur->ordered != xml)
                        cur = cur->ordered;
                    cur->ordered = cur->ordered->ordered;       /* patch ordered list */

                    cur = xml->parent->child;   /* go back to head of subtag list */
                    if(strcmp(cur->name, xml->name))
                        {       /* not in first sibling list */
                            while(strcmp(cur->sibling->name, xml->name))
                                cur = cur->sibling;
                            if(cur->sibling == xml)
                                {       /* first of a sibling list */
                                    cur->sibling = (xml->next) ? xml->next
                                        : cur->sibling->sibling;
                                }
                            else
                                cur = cur->sibling;     /* not first of a sibling list */
                        }

                    while(cur->next && cur->next != xml)
                        cur = cur->next;
                    if(cur->next)
                        cur->next = cur->next->next;    /* patch next list */
                }
        }
    xml->ordered = xml->sibling = xml->next = NULL;
    return xml;
}
const char* ezxml_error ( ezxml_t  xml)

returns parser error message or empty string if none

return parser error message or empty string if none

Definition at line 1233 of file ezxml.c.

{
    while(xml && xml->parent)
        xml = xml->parent;      /* find root tag */
    return (xml) ? ((ezxml_root_t) xml)->err : "";
}
void ezxml_free ( ezxml_t  xml)

frees the memory allocated for an ezxml structure

free the memory allocated for the ezxml structure

Definition at line 1174 of file ezxml.c.

{
    ezxml_root_t root = (ezxml_root_t) xml;
    int i, j;
    char **a, *s;

    if(!xml)
        return;
    ezxml_free(xml->child);
    ezxml_free(xml->ordered);

    if(!xml->parent)
        {                       /* free root tag allocations */
            for(i = 10; root->ent[i]; i += 2)   /* 0 - 9 are default entites (<>&"') */
                if((s = root->ent[i + 1]) < root->s || s > root->e)
                    free(s);
            free(root->ent);    /* free list of general entities */

            for(i = 0; root->attr[i]; i++)
                {
                    /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
                    a = root->attr[i];
                    for(j = 1; a[j++]; j += 2)  /* free malloced attribute values */
                        if(a[j] && (a[j] < root->s || a[j] > root->e))
                            free(a[j]);
                    free(a);
                }
            if(root->attr[0])
                free(root->attr);       /* free default attribute list */

            for(i = 0; root->pi[i]; i++)
                {
                    for(j = 1; root->pi[i][j]; j++);
                    free(root->pi[i][j + 1]);
                    free(root->pi[i]);
                }
            if(root->pi[0])
                free(root->pi); /* free processing instructions */

            if(root->len == -1)
                free(root->m);  /* malloced xml data */
#ifndef EZXML_NOMMAP
            else if(root->len)
                munmap(root->m, root->len);     /* mem mapped xml data */
#endif /* EZXML_NOMMAP */
            if(root->u)
                free(root->u);  /* utf8 conversion */
        }

    ezxml_free_attr(xml->attr); /* tag attributes */
    if((xml->flags & EZXML_TXTM))
        free(xml->txt);         /* character content */
    if((xml->flags & EZXML_NAMEM))
        free(xml->name);        /* tag name */
    free(xml);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ezxml_t ezxml_get ( ezxml_t  xml,
  ... 
)

Traverses the ezxml sturcture to retrieve a specific subtag. Takes a variable length list of tag names and indexes. The argument list must be terminated by either an index of -1 or an empty string tag name. Example: title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1); This retrieves the title of the 3rd book on the 1st shelf of library. Returns NULL if not found.

Traverses the xml tree to retrieve a specific subtag. Takes a variable length list of tag names and indexes. The argument list must be terminated by either an index of -1 or an empty string tag name. Example: title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1); This retrieves the title of the 3rd book on the 1st shelf of library. Returns NULL if not found.

Definition at line 141 of file ezxml.c.

{
    va_list ap;
    ezxml_t r;

    va_start(ap, xml);
    r = ezxml_vget(xml, ap);
    va_end(ap);
    return r;
}

Here is the call graph for this function:

ezxml_t ezxml_idx ( ezxml_t  xml,
int  idx 
)

Returns the Nth tag with the same name in the same section at the same depth or NULL if not found. An index of 0 returns the tag given.

returns the Nth tag with the same name in the same subsection or NULL if not found

Definition at line 83 of file ezxml.c.

{
    for(; xml && idx; idx--)
        xml = xml->next;
    return xml;
}

Here is the caller graph for this function:

ezxml_t ezxml_insert ( ezxml_t  xml,
ezxml_t  dest,
size_t  off 
)

inserts an existing tag into an ezxml structure

Definition at line 1261 of file ezxml.c.

{
    ezxml_t cur, prev, head;

    xml->next = xml->sibling = xml->ordered = NULL;
    xml->off = off;
    xml->parent = dest;

    /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
    head = dest->child;
    if(head)
        {                       /* already have sub tags */
            if(head->off <= off)
                {               /* not first subtag */
                    for(cur = head; cur->ordered && cur->ordered->off <= off;
                        cur = cur->ordered);
                    xml->ordered = cur->ordered;
                    cur->ordered = xml;
                }
            else
                {               /* first subtag */
                    xml->ordered = head;
                    dest->child = xml;
                }

            for(cur = head, prev = NULL; cur && strcmp(cur->name, xml->name); prev = cur, cur = cur->sibling);  /* find tag type */
            if(cur && cur->off <= off)
                {               /* not first of type */
                    while(cur->next && cur->next->off <= off)
                        cur = cur->next;
                    xml->next = cur->next;
                    cur->next = xml;
                }
            else
                {               /* first tag of this type */
                    if(prev && cur)
                        prev->sibling = cur->sibling;   /* remove old first */
                    xml->next = cur;    /* old first tag is now next */
                    for(cur = head, prev = NULL; cur && cur->off <= off; prev = cur, cur = cur->sibling);       /* new sibling insert point */
                    xml->sibling = cur;
                    if(prev)
                        prev->sibling = xml;
                }
        }
    else
        dest->child = xml;      /* only sub tag */

    return xml;
}

Here is the caller graph for this function:

ezxml_t ezxml_new ( const char *  name)

returns a new empty ezxml structure with the given root tag name

Definition at line 1242 of file ezxml.c.

{
    static char *ent[] = { "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
        "apos;", "&#39;", "amp;", "&#38;", NULL
    };
    ezxml_root_t root =
        (ezxml_root_t) memset(malloc(sizeof(struct ezxml_root)),
                              '\0', sizeof(struct ezxml_root));

    root->xml.name = (char *)name;
    root->cur = &root->xml;
    strcpy(root->err, root->xml.txt = "");
    root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent));
    root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL);
    return &root->xml;
}

Here is the caller graph for this function:

ezxml_t ezxml_parse_fd ( int  fd)

A wrapper for ezxml_parse_str() that accepts a file descriptor. First attempts to mem map the file. Failing that, reads the file into memory. Returns NULL on failure.

Definition at line 941 of file ezxml.c.

{
    ezxml_root_t root;
    struct stat st;
    size_t l;
    void *m;

    if(fd < 0)
        return NULL;
    fstat(fd, &st);

#ifndef EZXML_NOMMAP
    l = (st.st_size + sysconf(_SC_PAGESIZE) -
         1) & ~(sysconf(_SC_PAGESIZE) - 1);
    if((m =
        mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,
             0)) != MAP_FAILED)
        {
            madvise(m, l, MADV_SEQUENTIAL);     /* optimize for sequential access */
            root = (ezxml_root_t) ezxml_parse_str(m, st.st_size);
            madvise(m, root->len = l, MADV_NORMAL);     /* put it back to normal */
        }
    else
        {                       /* mmap failed, read file into memory */
#endif /* EZXML_NOMMAP */
            l = read(fd, m = malloc(st.st_size), st.st_size);
            root = (ezxml_root_t) ezxml_parse_str(m, l);
            /* Ted Campbell, Aug 14, 2007. Added explicit cast. */
            root->len = (size_t) (-1);  /* so we know to free s in ezxml_free() */
#ifndef EZXML_NOMMAP
        }
#endif /* EZXML_NOMMAP */
    return &root->xml;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ezxml_t ezxml_parse_file ( const char *  file)

a wrapper for ezxml_parse_fd() that accepts a file name

a wrapper for ezxml_parse_fd that accepts a file name

Definition at line 978 of file ezxml.c.

{
    int fd = open(file, O_RDONLY, 0);
    ezxml_t xml = ezxml_parse_fd(fd);

    if(fd >= 0)
        close(fd);
    return xml;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ezxml_t ezxml_parse_fp ( FILE *  fp)

Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire stream into memory and then parses it. For xml files, use ezxml_parse_file() or ezxml_parse_fd()

Definition at line 910 of file ezxml.c.

{
    ezxml_root_t root;
    size_t l, len = 0;
    char *s;

    /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
    s = malloc(EZXML_BUFSIZE);
    if(!s)
        return NULL;
    do
        {
            len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
            if(l == EZXML_BUFSIZE)
                s = realloc(s, len + EZXML_BUFSIZE);
        }
    while(s && l == EZXML_BUFSIZE);

    if(!s)
        return NULL;
    root = (ezxml_root_t) ezxml_parse_str(s, len);
    /* Ted Campbell, Aug 14, 2007. Added explicit cast. */
    root->len = (size_t) (-1);  /* so we know to free s in ezxml_free() */
    return &root->xml;
}

Here is the call graph for this function:

ezxml_t ezxml_parse_str ( char *  s,
size_t  len 
)

Given a string of xml data and its length, parses it and creates an ezxml structure. For efficiency, modifies the data by adding null terminators and decoding ampersand sequences. If you don't want this, copy the data and pass in the copy. Returns NULL on failure.

parse the given xml string and return an ezxml structure. Jason Luu June 22, 2010, Added line number support

Definition at line 687 of file ezxml.c.

{
    ezxml_root_t root = (ezxml_root_t) ezxml_new(NULL);
    char q, e, *d, **attr, **a = NULL;  /* initialize a to avoid compile warning */
    int l, i, j;
        int line = 1;

    root->m = s;
    if(!len)
        return ezxml_err(root, NULL, "root tag missing");
    root->u = ezxml_str2utf8(&s, &len); /* convert utf-16 to utf-8 */
    root->e = (root->s = s) + len;      /* record start and end of work area */

    e = s[len - 1];             /* save end char */
    s[len - 1] = '\0';          /* turn end char into null terminator */

    while(*s && *s != '<')
        s++;                    /* find first tag */
    if(!*s)
        return ezxml_err(root, s, "root tag missing");

    for(;;)
        {
            attr = (char **)EZXML_NIL;
            d = ++s;
                
            if(isalpha(*s) || *s == '_' || *s == ':' || *s < '\0')
                {               /* new tag */
                    if(!root->cur)
                        return ezxml_err(root, d,
                                         "markup outside of root element");

                    s += strcspn(s, EZXML_WS "/>");
                        while(isspace(*s)) {
                                if(*s == '\n')
                                        line++;
                                *(s++) = '\0';  /* null terminate tag name */
                        }

                    if(*s && *s != '/' && *s != '>')
                        {       /* find tag in default attr list */
                            /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
                            a = root->attr[0];
                            for(i = 0; a && strcmp(a[0], d); i++)
                                {
                                    a = root->attr[i];
                                }
                        }

                    for(l = 0; *s && *s != '/' && *s != '>'; l += 2)
                        {       /* new attrib */
                            attr = (l) ? realloc(attr, (l + 4) * sizeof(char *)) : malloc(4 * sizeof(char *));  /* allocate space */
                            attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2) : malloc(2);  /* mem for list of maloced vals */
                            strcpy(attr[l + 3] + (l / 2), " "); /* value is not malloced */
                            attr[l + 2] = NULL; /* null terminate list */
                            attr[l + 1] = "";   /* temporary attribute value */
                            attr[l] = s;        /* set attribute name */

                            s += strcspn(s, EZXML_WS "=/>");
                            if(*s == '=' || isspace(*s))
                                {
                                        if(*s == '\n')
                                                line++;
                                    *(s++) = '\0';      /* null terminate tag attribute name */
                                    q = *(s += strspn(s, EZXML_WS "="));
                                    if(q == '"' || q == '\'')
                                        {       /* attribute value */
                                            attr[l + 1] = ++s;
                                            while(*s && *s != q)
                                                s++;
                                            if(*s)
                                                *(s++) = '\0';  /* null terminate attribute val */
                                            else
                                                {
                                                    ezxml_free_attr(attr);
                                                    return ezxml_err(root, d,
                                                                     "missing %c",
                                                                     q);
                                                }

                                            for(j = 1; a && a[j]
                                                && strcmp(a[j], attr[l]);
                                                j += 3);
                                            attr[l + 1] =
                                                ezxml_decode(&line, attr[l + 1],
                                                             root->ent, (a
                                                                         &&
                                                                         a[j])
                                                             ? *a[j +
                                                                  2] : ' ');
                                            if(attr[l + 1] < d
                                               || attr[l + 1] > s)
                                                attr[l + 3][l / 2] = EZXML_TXTM;        /* value malloced */
                                        }
                                }
                                while(isspace(*s)) {
                                        if(*s == '\n')
                                                line++;
                                        s++;
                                }
                        }

                    if(*s == '/')
                        {       /* self closing tag */
                            *(s++) = '\0';
                            if((*s && *s != '>') || (!*s && e != '>'))
                                {
                                    if(l)
                                        ezxml_free_attr(attr);
                                    return ezxml_err(root, d, "missing >");
                                }
                            ezxml_open_tag(root, line, d, attr);
                            ezxml_close_tag(root, d, s);
                        }
                    else if((q = *s) == '>' || (!*s && e == '>'))
                        {       /* open tag */
                            *s = '\0';  /* temporarily null terminate tag name */
                            ezxml_open_tag(root, line, d, attr);
                            *s = q;
                        }
                    else
                        {
                            if(l)
                                ezxml_free_attr(attr);
                            return ezxml_err(root, d, "missing >");
                        }
                }
            else if(*s == '/')
                {               /* close tag */
                    s += strcspn(d = s + 1, EZXML_WS ">") + 1;
                    /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
                    q = *s;
                    if(!q && e != '>')
                        return ezxml_err(root, d, "missing >");
                    *s = '\0';  /* temporarily null terminate tag name */
                    if(ezxml_close_tag(root, d, s))
                        return &root->xml;
                        if(isspace(*s = q)){
                                if(*s == '\n')
                                        line++;
                                s += strspn(s, EZXML_WS);
                        }
                }
            else if(!strncmp(s, "!--", 3))
                {               /* xml comment */
                    s = strstr(s + 3, "--");
                    if(!s || (*(s += 2) != '>' && *s) || (!*s && e != '>'))
                        return ezxml_err(root, d, "unclosed <!--");
                }
            else if(!strncmp(s, "![CDATA[", 8))
                {               /* cdata */
                    /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
                    s = strstr(s, "]]>");
                    if(s)
                        ezxml_char_content(root, &line, d + 8, (s += 2) - d - 10,
                                           'c');
                    else
                        return ezxml_err(root, d, "unclosed <![CDATA[");
                }
            else if(!strncmp(s, "!DOCTYPE", 8))
                {               /* dtd */
                    for(l = 0; *s && ((!l && *s != '>') || (l && (*s != ']' ||
                                                                  *(s +
                                                                    strspn(s +
                                                                           1,
                                                                           EZXML_WS)
                                                                    + 1) !=
                                                                  '>')));
                        l = (*s == '[') ? 1 : l)
                        s += strcspn(s + 1, "[]>") + 1;
                    if(!*s && e != '>')
                        return ezxml_err(root, d, "unclosed <!DOCTYPE");
                    d = (l) ? strchr(d, '[') + 1 : d;
                    if(l && !ezxml_internal_dtd(root, &line, d, s++ - d))
                        return &root->xml;
                }
            else if(*s == '?')
                {               /* <?...?> processing instructions */
                    do
                        {
                            s = strchr(s, '?');
                        }
                    while(s && *(++s) && *s != '>');
                    if(!s || (!*s && e != '>'))
                        return ezxml_err(root, d, "unclosed <?");
                    else
                        ezxml_proc_inst(root, d + 1, s - d - 2);
                }
            else
                return ezxml_err(root, d, "unexpected <");

            if(!s || !*s)
                break;
            *s = '\0';
            d = ++s;
            if(*s && *s != '<')
                {               /* tag character content */
                    while(*s && *s != '<')
                        s++;
                    if(*s)
                        ezxml_char_content(root, &line, d, s - d, '&');
                    else
                        break;
                }
            else if(!*s)
                break;
        }

    if(!root->cur)
        return &root->xml;
    else if(!root->cur->name)
        return ezxml_err(root, d, "root tag missing");
    else
        return ezxml_err(root, d, "unclosed tag <%s>", root->cur->name);
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char** ezxml_pi ( ezxml_t  xml,
const char *  target 
)

returns a NULL terminated array of processing instructions for the given target

returns a null terminated array of processing instructions for the given target

Definition at line 157 of file ezxml.c.

{
    ezxml_root_t root = (ezxml_root_t) xml;
    int i = 0;

    if(!root)
        return (const char **)EZXML_NIL;
    while(root->xml.parent)
        root = (ezxml_root_t) root->xml.parent; /* root tag */
    while(root->pi[i] && strcmp(target, root->pi[i][0]))
        i++;                    /* find target */
    return (const char **)((root->pi[i]) ? root->pi[i] + 1 : EZXML_NIL);
}
ezxml_t ezxml_set_attr ( ezxml_t  xml,
const char *  name,
const char *  value 
)

Sets the given tag attribute or adds a new attribute if not found. A value of NULL will remove the specified attribute. Returns the tag given.

Definition at line 1352 of file ezxml.c.

{
    int l = 0, c;

    if(!xml)
        return NULL;
    while(xml->attr[l] && strcmp(xml->attr[l], name))
        l += 2;
    if(!xml->attr[l])
        {                       /* not found, add as new attribute */
            if(!value)
                return xml;     /* nothing to do */
            if(xml->attr == EZXML_NIL)
                {               /* first attribute */
                    xml->attr = malloc(4 * sizeof(char *));
                    /* Ted Campbell, Aug 14, 2007. Changed to use 'my_strdup' */
                    xml->attr[1] = my_strdup("");       /* empty list of malloced names/vals */
                }
            else
                xml->attr = realloc(xml->attr, (l + 4) * sizeof(char *));

            xml->attr[l] = (char *)name;        /* set attribute name */
            xml->attr[l + 2] = NULL;    /* null terminate attribute list */
            xml->attr[l + 3] = realloc(xml->attr[l + 1],
                                       (c = strlen(xml->attr[l + 1])) + 2);
            strcpy(xml->attr[l + 3] + c, " ");  /* set name/value as not malloced */
            if(xml->flags & EZXML_DUP)
                xml->attr[l + 3][c] = (char)(unsigned char)EZXML_NAMEM;
        }
    else if(xml->flags & EZXML_DUP)
        free((char *)name);     /* name was strduped */

    for(c = l; xml->attr[c]; c += 2);   /* find end of attribute list */
    if(xml->attr[c + 1][l / 2] & EZXML_TXTM)
        free(xml->attr[l + 1]); /*old val */
    if(xml->flags & EZXML_DUP)
        xml->attr[c + 1][l / 2] |= EZXML_TXTM;
    else
        xml->attr[c + 1][l / 2] &= ~EZXML_TXTM;

    if(value)
        xml->attr[l + 1] = (char *)value;       /* set attribute value */
    else
        {                       /* remove attribute */
            if(xml->attr[c + 1][l / 2] & EZXML_NAMEM)
                free(xml->attr[l]);
            /* Ted Campbell, Aug 14, 2007. It seems that the size should be 
             * (c + 2) - (l + 2) = (c - l) */
            memmove(xml->attr + l, xml->attr + l + 2,
                    (c - l) * sizeof(char *));
            /* Ted Campbell, Aug 14, 2007. We need to adjust c to point to new
             * location it was moved to since its old location is undefined */
            c -= 2;             /* We have one less elements */
            xml->attr = realloc(xml->attr, (c + 2) * sizeof(char *));
            memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1, (c / 2) - (l / 2));     /* fix list of which name/vals are malloced */
        }
    xml->flags &= ~EZXML_DUP;   /* clear strdup() flag */
    return xml;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ezxml_t ezxml_set_flag ( ezxml_t  xml,
short  flag 
)

sets a flag for the given tag and returns the tag

Definition at line 1416 of file ezxml.c.

{
    if(xml)
        xml->flags |= flag;
    return xml;
}

Here is the caller graph for this function:

ezxml_t ezxml_set_txt ( ezxml_t  xml,
const char *  txt 
)

sets the character content for the given tag and returns the tag

Definition at line 1336 of file ezxml.c.

{
    if(!xml)
        return NULL;
    if(xml->flags & EZXML_TXTM)
        free(xml->txt);         /* existing txt was malloced */
    xml->flags &= ~EZXML_TXTM;
    xml->txt = (char *)txt;
    return xml;
}

Here is the caller graph for this function:

char* ezxml_toxml ( ezxml_t  xml)

Converts an ezxml structure back to xml. Returns a string of xml data that must be freed.

Definition at line 1115 of file ezxml.c.

{
    ezxml_t p = (xml) ? xml->parent : NULL,
      o = (xml) ? xml->ordered : NULL;
    ezxml_root_t root = (ezxml_root_t) xml;
    size_t len = 0, max = EZXML_BUFSIZE;
    char *s = strcpy(malloc(max), ""),
     *t,
     *n;
    int i, j, k;

    if(!xml || !xml->name)
        return realloc(s, len + 1);
    while(root->xml.parent)
        root = (ezxml_root_t) root->xml.parent; /* root tag */

    for(i = 0; !p && root->pi[i]; i++)
        {                       /* pre-root processing instructions */
            for(k = 2; root->pi[i][k - 1]; k++);
            for(j = 1; root->pi[i][j]; j++)
                {
                    /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
                    n = root->pi[i][j];
                    if(root->pi[i][k][j - 1] == '>')
                        continue;       /* not pre-root */
                    while(len + strlen(t = root->pi[i][0]) + strlen(n) + 7 >
                          max)
                        s = realloc(s, max += EZXML_BUFSIZE);
                    len +=
                        sprintf(s + len, "<?%s%s%s?>\n", t, *n ? " " : "", n);
                }
        }

    xml->parent = xml->ordered = NULL;
    s = ezxml_toxml_r(xml, &s, &len, &max, 0, root->attr);
    xml->parent = p;
    xml->ordered = o;

    for(i = 0; !p && root->pi[i]; i++)
        {                       /* post-root processing instructions */
            for(k = 2; root->pi[i][k - 1]; k++);
            for(j = 1; root->pi[i][j]; j++)
                {
                    /* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
                    n = root->pi[i][j];
                    if(root->pi[i][k][j - 1] == '<')
                        continue;       /* not post-root */
                    while(len + strlen(t = root->pi[i][0]) + strlen(n) + 7 >
                          max)
                        s = realloc(s, max += EZXML_BUFSIZE);
                    len +=
                        sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
                }
        }
    return realloc(s, len + 1);
}

Here is the call graph for this function: