2003-06-03 19:46:29 +00:00
|
|
|
/*
|
2005-01-29 07:21:44 +00:00
|
|
|
* "$Id$"
|
2003-06-03 19:46:29 +00:00
|
|
|
*
|
2004-05-02 16:04:40 +00:00
|
|
|
* Search/navigation functions for Mini-XML, a small XML-like file
|
2003-06-03 19:46:29 +00:00
|
|
|
* parsing library.
|
|
|
|
*
|
2010-09-19 05:26:46 +00:00
|
|
|
* Copyright 2003-2010 by Michael R Sweet.
|
2003-06-03 19:46:29 +00:00
|
|
|
*
|
2010-09-19 05:26:46 +00:00
|
|
|
* These coded instructions, statements, and computer programs are the
|
|
|
|
* property of Michael R Sweet and are protected by Federal copyright
|
|
|
|
* law. Distribution and use rights are outlined in the file "COPYING"
|
|
|
|
* which should have been included with this file. If this file is
|
|
|
|
* missing or damaged, see the license at:
|
2003-06-03 19:46:29 +00:00
|
|
|
*
|
2010-09-19 05:26:46 +00:00
|
|
|
* http://www.minixml.org/
|
2003-06-03 19:46:29 +00:00
|
|
|
*
|
|
|
|
* Contents:
|
|
|
|
*
|
|
|
|
* mxmlFindElement() - Find the named element.
|
2010-11-08 16:07:05 +00:00
|
|
|
* mxmlFindValue() - Find a value with the given path.
|
2003-06-03 19:46:29 +00:00
|
|
|
* mxmlWalkNext() - Walk to the next logical node in the tree.
|
|
|
|
* mxmlWalkPrev() - Walk to the previous logical node in the tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Include necessary headers...
|
|
|
|
*/
|
|
|
|
|
2003-07-20 13:41:17 +00:00
|
|
|
#include "config.h"
|
2003-07-27 23:11:40 +00:00
|
|
|
#include "mxml.h"
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlFindElement()' - Find the named element.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* The search is constrained by the name, attribute name, and value; any
|
|
|
|
* NULL names or values are treated as wildcards, so different kinds of
|
|
|
|
* searches can be implemented by looking for all elements of a given name
|
|
|
|
* or all elements with a specific attribute. The descend argument determines
|
|
|
|
* whether the search descends into child nodes; normally you will use
|
|
|
|
* MXML_DESCEND_FIRST for the initial search and MXML_NO_DESCEND to find
|
|
|
|
* additional direct descendents of the node. The top node argument
|
|
|
|
* constrains the search to a particular node's children.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - Element node or NULL */
|
|
|
|
mxmlFindElement(mxml_node_t *node, /* I - Current node */
|
|
|
|
mxml_node_t *top, /* I - Top node */
|
2003-06-04 16:30:40 +00:00
|
|
|
const char *name, /* I - Element name or NULL for any */
|
|
|
|
const char *attr, /* I - Attribute name, or NULL for none */
|
|
|
|
const char *value, /* I - Attribute value, or NULL for any */
|
2003-06-14 23:56:47 +00:00
|
|
|
int descend) /* I - Descend into tree - MXML_DESCEND, MXML_NO_DESCEND, or MXML_DESCEND_FIRST */
|
2003-06-03 19:46:29 +00:00
|
|
|
{
|
2003-06-04 16:30:40 +00:00
|
|
|
const char *temp; /* Current attribute value */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!node || !top || (!attr && value))
|
|
|
|
return (NULL);
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Start with the next node...
|
|
|
|
*/
|
|
|
|
|
2003-06-04 16:30:40 +00:00
|
|
|
node = mxmlWalkNext(node, top, descend);
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop until we find a matching element...
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (node != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* See if this node matches...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (node->type == MXML_ELEMENT &&
|
|
|
|
node->value.element.name &&
|
2003-06-04 16:30:40 +00:00
|
|
|
(!name || !strcmp(node->value.element.name, name)))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* See if we need to check for an attribute...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
return (node); /* No attribute search, return it... */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the attribute...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((temp = mxmlElementGetAttr(node, attr)) != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* OK, we have the attribute, does it match?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!value || !strcmp(value, temp))
|
|
|
|
return (node); /* Yes, return it... */
|
|
|
|
}
|
|
|
|
}
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
/*
|
2003-06-04 16:30:40 +00:00
|
|
|
* No match, move on to the next node...
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-04 16:30:40 +00:00
|
|
|
if (descend == MXML_DESCEND)
|
|
|
|
node = mxmlWalkNext(node, top, MXML_DESCEND);
|
|
|
|
else
|
|
|
|
node = node->next;
|
2003-06-03 19:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-08 16:07:05 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlFindValue()' - Find a value with the given path.
|
|
|
|
*
|
|
|
|
* The "path" is a slash-separated list of element names. The name "*" is
|
|
|
|
* considered a wildcard for one or more levels of elements. For example,
|
|
|
|
* "foo/one/two", "bar/two/one", "*\/one", and so forth.
|
|
|
|
*
|
|
|
|
* @since Mini-XML 2.7@
|
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - First value node or NULL */
|
|
|
|
mxmlFindValue(mxml_node_t *top, /* I - Top node */
|
|
|
|
const char *path) /* I - Path to element */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* Current node */
|
|
|
|
char element[256], /* Current element name */
|
|
|
|
*pathsep; /* Separator in path */
|
|
|
|
int descend; /* mxmlFindElement option */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!top || !path || !*path)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search each element in the path...
|
|
|
|
*/
|
|
|
|
|
|
|
|
node = top;
|
|
|
|
while (*path)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Handle wildcards...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!strncmp(path, "*/", 2))
|
|
|
|
{
|
|
|
|
path += 2;
|
|
|
|
descend = MXML_DESCEND;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
descend = MXML_DESCEND_FIRST;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next element in the path...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((pathsep = strchr(path, '/')) == NULL)
|
|
|
|
pathsep = path + strlen(path);
|
|
|
|
|
|
|
|
if (pathsep == path || (pathsep - path) >= sizeof(element))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
memcpy(element, path, pathsep - path);
|
|
|
|
element[pathsep - path] = '\0';
|
|
|
|
|
|
|
|
if (*pathsep)
|
|
|
|
path = pathsep + 1;
|
|
|
|
else
|
|
|
|
path = pathsep;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for the element...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxmlFindElement(node, node, element, NULL, NULL,
|
|
|
|
descend)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we get this far, return the first child of the current node...
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (node->child);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlWalkNext()' - Walk to the next logical node in the tree.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* The descend argument controls whether the first child is considered
|
|
|
|
* to be the next node. The top node argument constrains the walk to
|
|
|
|
* the node's children.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - Next node or NULL */
|
|
|
|
mxmlWalkNext(mxml_node_t *node, /* I - Current node */
|
2003-06-04 16:30:40 +00:00
|
|
|
mxml_node_t *top, /* I - Top node */
|
2003-06-14 23:56:47 +00:00
|
|
|
int descend) /* I - Descend into tree - MXML_DESCEND, MXML_NO_DESCEND, or MXML_DESCEND_FIRST */
|
2003-06-03 19:46:29 +00:00
|
|
|
{
|
2003-06-04 16:30:40 +00:00
|
|
|
if (!node)
|
|
|
|
return (NULL);
|
|
|
|
else if (node->child && descend)
|
|
|
|
return (node->child);
|
2007-09-09 07:16:52 +00:00
|
|
|
else if (node == top)
|
|
|
|
return (NULL);
|
2003-06-04 16:30:40 +00:00
|
|
|
else if (node->next)
|
|
|
|
return (node->next);
|
2003-06-04 21:19:00 +00:00
|
|
|
else if (node->parent && node->parent != top)
|
2003-06-04 16:30:40 +00:00
|
|
|
{
|
|
|
|
node = node->parent;
|
|
|
|
|
|
|
|
while (!node->next)
|
2003-06-06 03:09:31 +00:00
|
|
|
if (node->parent == top || !node->parent)
|
2003-06-04 16:30:40 +00:00
|
|
|
return (NULL);
|
|
|
|
else
|
|
|
|
node = node->parent;
|
|
|
|
|
|
|
|
return (node->next);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (NULL);
|
2003-06-03 19:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlWalkPrev()' - Walk to the previous logical node in the tree.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* The descend argument controls whether the previous node's last child
|
|
|
|
* is considered to be the previous node. The top node argument constrains
|
|
|
|
* the walk to the node's children.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - Previous node or NULL */
|
|
|
|
mxmlWalkPrev(mxml_node_t *node, /* I - Current node */
|
2003-06-04 16:30:40 +00:00
|
|
|
mxml_node_t *top, /* I - Top node */
|
2003-06-14 23:56:47 +00:00
|
|
|
int descend) /* I - Descend into tree - MXML_DESCEND, MXML_NO_DESCEND, or MXML_DESCEND_FIRST */
|
2003-06-03 19:46:29 +00:00
|
|
|
{
|
2007-09-09 07:16:52 +00:00
|
|
|
if (!node || node == top)
|
2003-06-03 19:46:29 +00:00
|
|
|
return (NULL);
|
|
|
|
else if (node->prev)
|
2003-06-04 16:30:40 +00:00
|
|
|
{
|
|
|
|
if (node->prev->last_child && descend)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Find the last child under the previous node...
|
|
|
|
*/
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2003-06-04 16:30:40 +00:00
|
|
|
node = node->prev->last_child;
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2003-06-04 16:30:40 +00:00
|
|
|
while (node->last_child)
|
|
|
|
node = node->last_child;
|
2003-06-03 20:40:01 +00:00
|
|
|
|
2003-06-04 16:30:40 +00:00
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (node->prev);
|
|
|
|
}
|
2003-06-03 20:40:01 +00:00
|
|
|
else if (node->parent != top)
|
2003-06-04 16:30:40 +00:00
|
|
|
return (node->parent);
|
2003-06-03 20:40:01 +00:00
|
|
|
else
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-01-29 07:21:44 +00:00
|
|
|
* End of "$Id$".
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|