2003-06-03 19:46:29 +00:00
|
|
|
/*
|
2017-03-22 18:51:16 +00:00
|
|
|
* Node support code for Mini-XML, a small XML file parsing library.
|
2003-06-03 19:46:29 +00:00
|
|
|
*
|
2017-03-22 18:51:16 +00:00
|
|
|
* Copyright 2003-2017 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
|
|
|
*
|
2017-03-22 18:51:16 +00:00
|
|
|
* https://michaelrsweet.github.io/mxml
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local functions...
|
|
|
|
*/
|
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
static void mxml_free(mxml_node_t *node);
|
2003-06-03 19:46:29 +00:00
|
|
|
static mxml_node_t *mxml_new(mxml_node_t *parent, mxml_type_t type);
|
|
|
|
|
|
|
|
|
2003-06-04 17:37:23 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlAdd()' - Add a node to a tree.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
2017-04-24 14:42:03 +00:00
|
|
|
* Adds the specified node to the parent. If the child argument is not
|
|
|
|
* @code NULL@, puts the new node before or after the specified child depending
|
|
|
|
* on the value of the where argument. If the child argument is @code NULL@,
|
|
|
|
* puts the new node at the beginning of the child list (@code MXML_ADD_BEFORE@)
|
|
|
|
* or at the end of the child list (@code MXML_ADD_AFTER@). The constant
|
|
|
|
* @code MXML_ADD_TO_PARENT@ can be used to specify a @code NULL@ child pointer.
|
2003-06-04 17:37:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
mxmlAdd(mxml_node_t *parent, /* I - Parent node */
|
2017-04-24 14:42:03 +00:00
|
|
|
int where, /* I - Where to add, @code MXML_ADD_BEFORE@ or @code MXML_ADD_AFTER@ */
|
|
|
|
mxml_node_t *child, /* I - Child node for where or @code MXML_ADD_TO_PARENT@ */
|
2003-06-04 17:37:23 +00:00
|
|
|
mxml_node_t *node) /* I - Node to add */
|
|
|
|
{
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlAdd(parent=%p, where=%d, child=%p, node=%p)\n", parent,
|
|
|
|
where, child, node);
|
|
|
|
#endif /* DEBUG */
|
2003-06-05 03:06:20 +00:00
|
|
|
|
2003-06-04 21:19:00 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!parent || !node)
|
2003-06-04 17:37:23 +00:00
|
|
|
return;
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#if DEBUG > 1
|
|
|
|
fprintf(stderr, " BEFORE: node->parent=%p\n", node->parent);
|
2003-12-04 04:55:57 +00:00
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " BEFORE: parent->child=%p\n", parent->child);
|
|
|
|
fprintf(stderr, " BEFORE: parent->last_child=%p\n", parent->last_child);
|
|
|
|
fprintf(stderr, " BEFORE: parent->prev=%p\n", parent->prev);
|
|
|
|
fprintf(stderr, " BEFORE: parent->next=%p\n", parent->next);
|
|
|
|
}
|
2003-12-03 03:59:04 +00:00
|
|
|
#endif /* DEBUG > 1 */
|
|
|
|
|
2003-06-04 21:19:00 +00:00
|
|
|
/*
|
|
|
|
* Remove the node from any existing parent...
|
|
|
|
*/
|
|
|
|
|
2003-06-04 17:37:23 +00:00
|
|
|
if (node->parent)
|
|
|
|
mxmlRemove(node);
|
|
|
|
|
2003-06-04 21:19:00 +00:00
|
|
|
/*
|
|
|
|
* Reset pointers...
|
|
|
|
*/
|
|
|
|
|
2003-06-04 17:37:23 +00:00
|
|
|
node->parent = parent;
|
|
|
|
|
|
|
|
switch (where)
|
|
|
|
{
|
|
|
|
case MXML_ADD_BEFORE :
|
|
|
|
if (!child || child == parent->child || child->parent != parent)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Insert as first node under parent...
|
|
|
|
*/
|
|
|
|
|
|
|
|
node->next = parent->child;
|
|
|
|
|
|
|
|
if (parent->child)
|
|
|
|
parent->child->prev = node;
|
|
|
|
else
|
|
|
|
parent->last_child = node;
|
|
|
|
|
|
|
|
parent->child = node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Insert node before this child...
|
|
|
|
*/
|
|
|
|
|
|
|
|
node->next = child;
|
|
|
|
node->prev = child->prev;
|
|
|
|
|
|
|
|
if (child->prev)
|
|
|
|
child->prev->next = node;
|
|
|
|
else
|
|
|
|
parent->child = node;
|
|
|
|
|
|
|
|
child->prev = node;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MXML_ADD_AFTER :
|
|
|
|
if (!child || child == parent->last_child || child->parent != parent)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Insert as last node under parent...
|
|
|
|
*/
|
|
|
|
|
|
|
|
node->parent = parent;
|
|
|
|
node->prev = parent->last_child;
|
|
|
|
|
|
|
|
if (parent->last_child)
|
|
|
|
parent->last_child->next = node;
|
|
|
|
else
|
|
|
|
parent->child = node;
|
|
|
|
|
|
|
|
parent->last_child = node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Insert node after this child...
|
|
|
|
*/
|
|
|
|
|
|
|
|
node->prev = child;
|
|
|
|
node->next = child->next;
|
|
|
|
|
|
|
|
if (child->next)
|
|
|
|
child->next->prev = node;
|
|
|
|
else
|
|
|
|
parent->last_child = node;
|
|
|
|
|
|
|
|
child->next = node;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-12-03 03:59:04 +00:00
|
|
|
|
|
|
|
#if DEBUG > 1
|
|
|
|
fprintf(stderr, " AFTER: node->parent=%p\n", node->parent);
|
2003-12-04 04:55:57 +00:00
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " AFTER: parent->child=%p\n", parent->child);
|
|
|
|
fprintf(stderr, " AFTER: parent->last_child=%p\n", parent->last_child);
|
|
|
|
fprintf(stderr, " AFTER: parent->prev=%p\n", parent->prev);
|
|
|
|
fprintf(stderr, " AFTER: parent->next=%p\n", parent->next);
|
|
|
|
}
|
2003-12-03 03:59:04 +00:00
|
|
|
#endif /* DEBUG > 1 */
|
2003-06-04 17:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlDelete()' - Delete a node and all of its children.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* If the specified node has a parent, this function first removes the
|
2017-04-24 14:42:03 +00:00
|
|
|
* node from its parent using the @link mxmlRemove@ function.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2003-06-14 23:56:47 +00:00
|
|
|
mxmlDelete(mxml_node_t *node) /* I - Node to delete */
|
2003-06-03 19:46:29 +00:00
|
|
|
{
|
2016-06-11 20:51:49 +00:00
|
|
|
mxml_node_t *current, /* Current node */
|
|
|
|
*next; /* Next node */
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlDelete(node=%p)\n", node);
|
|
|
|
#endif /* DEBUG */
|
2003-06-05 03:06:20 +00:00
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
2003-06-04 17:37:23 +00:00
|
|
|
* Remove the node from its parent, if any...
|
|
|
|
*/
|
|
|
|
|
|
|
|
mxmlRemove(node);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete children...
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
for (current = node->child; current; current = next)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Get the next node...
|
|
|
|
*/
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
if ((next = current->child) != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Free parent nodes after child nodes have been freed...
|
|
|
|
*/
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
current->child = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
if ((next = current->next) == NULL)
|
|
|
|
{
|
2017-03-29 22:18:22 +00:00
|
|
|
/*
|
|
|
|
* Next node is the parent, which we'll free as needed...
|
|
|
|
*/
|
2016-06-11 20:51:49 +00:00
|
|
|
|
2017-03-29 22:18:22 +00:00
|
|
|
if ((next = current->parent) == node)
|
2016-06-11 20:51:49 +00:00
|
|
|
next = NULL;
|
|
|
|
}
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2017-03-29 22:18:22 +00:00
|
|
|
/*
|
|
|
|
* Free child...
|
|
|
|
*/
|
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
mxml_free(current);
|
2003-06-03 19:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-03-29 22:18:22 +00:00
|
|
|
* Then free the memory used by the parent node...
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
mxml_free(node);
|
2003-06-03 19:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-03 02:03:29 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlGetRefCount()' - Get the current reference (use) count for a node.
|
|
|
|
*
|
|
|
|
* The initial reference count of new nodes is 1. Use the @link mxmlRetain@
|
|
|
|
* and @link mxmlRelease@ functions to increment and decrement a node's
|
|
|
|
* reference count.
|
|
|
|
*
|
|
|
|
* @since Mini-XML 2.7@.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int /* O - Reference count */
|
|
|
|
mxmlGetRefCount(mxml_node_t *node) /* I - Node */
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the reference count...
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (node->ref_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-05 14:17:19 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlNewCDATA()' - Create a new CDATA node.
|
|
|
|
*
|
|
|
|
* The new CDATA node is added to the end of the specified parent's child
|
2017-04-24 14:42:03 +00:00
|
|
|
* list. The constant @code MXML_NO_PARENT@ can be used to specify that the new
|
|
|
|
* CDATA node has no parent. The data string must be nul-terminated and
|
|
|
|
* is copied into the new node. CDATA nodes currently use the
|
|
|
|
* @code MXML_ELEMENT@ type.
|
2005-10-13 18:27:00 +00:00
|
|
|
*
|
|
|
|
* @since Mini-XML 2.3@
|
2005-08-05 14:17:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2017-04-24 14:42:03 +00:00
|
|
|
mxmlNewCDATA(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2005-08-05 14:17:19 +00:00
|
|
|
const char *data) /* I - Data string */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewCDATA(parent=%p, data=\"%s\")\n",
|
|
|
|
parent, data ? data : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the node and set the name value...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_ELEMENT)) != NULL)
|
2005-10-13 18:27:00 +00:00
|
|
|
node->value.element.name = _mxml_strdupf("![CDATA[%s]]", data);
|
2005-08-05 14:17:19 +00:00
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-28 02:58:01 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlNewCustom()' - Create a new custom data node.
|
|
|
|
*
|
|
|
|
* The new custom node is added to the end of the specified parent's child
|
2017-04-24 14:42:03 +00:00
|
|
|
* list. The constant @code MXML_NO_PARENT@ can be used to specify that the new
|
|
|
|
* element node has no parent. @code NULL@ can be passed when the data in the
|
2004-10-28 02:58:01 +00:00
|
|
|
* node is not dynamically allocated or is separately managed.
|
2005-10-13 18:27:00 +00:00
|
|
|
*
|
|
|
|
* @since Mini-XML 2.1@
|
2004-10-28 02:58:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2007-04-23 21:48:03 +00:00
|
|
|
mxmlNewCustom(
|
2017-04-24 14:42:03 +00:00
|
|
|
mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2007-04-23 21:48:03 +00:00
|
|
|
void *data, /* I - Pointer to data */
|
|
|
|
mxml_custom_destroy_cb_t destroy) /* I - Function to destroy data */
|
2004-10-28 02:58:01 +00:00
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewCustom(parent=%p, data=%p, destroy=%p)\n", parent,
|
|
|
|
data, destroy);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the node and set the value...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_CUSTOM)) != NULL)
|
|
|
|
{
|
|
|
|
node->value.custom.data = data;
|
|
|
|
node->value.custom.destroy = destroy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlNewElement()' - Create a new element node.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* The new element node is added to the end of the specified parent's child
|
2017-04-24 14:42:03 +00:00
|
|
|
* list. The constant @code MXML_NO_PARENT@ can be used to specify that the new
|
2003-06-14 23:56:47 +00:00
|
|
|
* element node has no parent.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2017-04-24 14:42:03 +00:00
|
|
|
mxmlNewElement(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2003-06-03 19:46:29 +00:00
|
|
|
const char *name) /* I - Name of element */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewElement(parent=%p, name=\"%s\")\n", parent,
|
|
|
|
name ? name : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the node and set the element name...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_ELEMENT)) != NULL)
|
|
|
|
node->value.element.name = strdup(name);
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlNewInteger()' - Create a new integer node.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* The new integer node is added to the end of the specified parent's child
|
2017-04-24 14:42:03 +00:00
|
|
|
* list. The constant @code MXML_NO_PARENT@ can be used to specify that the new
|
2003-06-14 23:56:47 +00:00
|
|
|
* integer node has no parent.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2017-04-24 14:42:03 +00:00
|
|
|
mxmlNewInteger(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2003-06-03 19:46:29 +00:00
|
|
|
int integer) /* I - Integer value */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewInteger(parent=%p, integer=%d)\n", parent, integer);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Create the node and set the element name...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_INTEGER)) != NULL)
|
|
|
|
node->value.integer = integer;
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlNewOpaque()' - Create a new opaque string.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
2017-04-24 14:42:03 +00:00
|
|
|
* The new opaque string node is added to the end of the specified parent's
|
|
|
|
* child list. The constant @code MXML_NO_PARENT@ can be used to specify that
|
|
|
|
* the new opaque string node has no parent. The opaque string must be nul-
|
|
|
|
* terminated and is copied into the new node.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2017-04-24 14:42:03 +00:00
|
|
|
mxmlNewOpaque(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2003-06-03 19:46:29 +00:00
|
|
|
const char *opaque) /* I - Opaque string */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewOpaque(parent=%p, opaque=\"%s\")\n", parent,
|
|
|
|
opaque ? opaque : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
2005-05-14 12:34:29 +00:00
|
|
|
if (!opaque)
|
2003-06-03 19:46:29 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the node and set the element name...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_OPAQUE)) != NULL)
|
|
|
|
node->value.opaque = strdup(opaque);
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-24 14:42:03 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlNewOpaquef()' - Create a new formatted opaque string node.
|
|
|
|
*
|
|
|
|
* The new opaque string node is added to the end of the specified parent's
|
|
|
|
* child list. The constant @code MXML_NO_PARENT@ can be used to specify that
|
|
|
|
* the new opaque string node has no parent. The format string must be
|
|
|
|
* nul-terminated and is formatted into the new node.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
|
|
|
mxmlNewOpaquef(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
|
|
|
const char *format, /* I - Printf-style format string */
|
|
|
|
...) /* I - Additional args as needed */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
va_list ap; /* Pointer to arguments */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewOpaquef(parent=%p, format=\"%s\", ...)\n", parent, format ? format : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!format)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the node and set the text value...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_OPAQUE)) != NULL)
|
|
|
|
{
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
node->value.opaque = _mxml_vstrdupf(format, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlNewReal()' - Create a new real number node.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* The new real number node is added to the end of the specified parent's
|
2017-04-24 14:42:03 +00:00
|
|
|
* child list. The constant @code MXML_NO_PARENT@ can be used to specify that
|
2003-06-14 23:56:47 +00:00
|
|
|
* the new real number node has no parent.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2017-04-24 14:42:03 +00:00
|
|
|
mxmlNewReal(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2003-06-03 19:46:29 +00:00
|
|
|
double real) /* I - Real number value */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewReal(parent=%p, real=%g)\n", parent, real);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Create the node and set the element name...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_REAL)) != NULL)
|
|
|
|
node->value.real = real;
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlNewText()' - Create a new text fragment node.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* The new text node is added to the end of the specified parent's child
|
2017-04-24 14:42:03 +00:00
|
|
|
* list. The constant @code MXML_NO_PARENT@ can be used to specify that the new
|
|
|
|
* text node has no parent. The whitespace parameter is used to specify
|
|
|
|
* whether leading whitespace is present before the node. The text
|
2014-01-04 21:50:06 +00:00
|
|
|
* string must be nul-terminated and is copied into the new node.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2017-04-24 14:42:03 +00:00
|
|
|
mxmlNewText(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2003-06-14 23:56:47 +00:00
|
|
|
int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
|
2003-06-03 19:46:29 +00:00
|
|
|
const char *string) /* I - String */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewText(parent=%p, whitespace=%d, string=\"%s\")\n",
|
|
|
|
parent, whitespace, string ? string : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
2005-05-14 12:34:29 +00:00
|
|
|
if (!string)
|
2003-06-03 19:46:29 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the node and set the text value...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_TEXT)) != NULL)
|
|
|
|
{
|
|
|
|
node->value.text.whitespace = whitespace;
|
|
|
|
node->value.text.string = strdup(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlNewTextf()' - Create a new formatted text fragment node.
|
|
|
|
*
|
|
|
|
* The new text node is added to the end of the specified parent's child
|
2017-04-24 14:42:03 +00:00
|
|
|
* list. The constant @code MXML_NO_PARENT@ can be used to specify that the new
|
|
|
|
* text node has no parent. The whitespace parameter is used to specify
|
|
|
|
* whether leading whitespace is present before the node. The format
|
2014-01-04 21:50:06 +00:00
|
|
|
* string must be nul-terminated and is formatted into the new node.
|
2003-09-28 12:44:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New node */
|
2017-04-24 14:42:03 +00:00
|
|
|
mxmlNewTextf(mxml_node_t *parent, /* I - Parent node or @code MXML_NO_PARENT@ */
|
2003-09-28 12:44:39 +00:00
|
|
|
int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
|
2017-04-24 14:42:03 +00:00
|
|
|
const char *format, /* I - Printf-style format string */
|
2003-09-28 12:44:39 +00:00
|
|
|
...) /* I - Additional args as needed */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
va_list ap; /* Pointer to arguments */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlNewTextf(parent=%p, whitespace=%d, format=\"%s\", ...)\n",
|
|
|
|
parent, whitespace, format ? format : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-09-28 12:44:39 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
2005-05-14 12:34:29 +00:00
|
|
|
if (!format)
|
2003-09-28 12:44:39 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the node and set the text value...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = mxml_new(parent, MXML_TEXT)) != NULL)
|
|
|
|
{
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
node->value.text.whitespace = whitespace;
|
2005-10-13 18:27:00 +00:00
|
|
|
node->value.text.string = _mxml_vstrdupf(format, ap);
|
2003-09-28 12:44:39 +00:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-04 17:37:23 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlRemove()' - Remove a node from its parent.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
2017-04-24 14:42:03 +00:00
|
|
|
* This function does not free memory used by the node - use @link mxmlDelete@
|
|
|
|
* for that. This function does nothing if the node has no parent.
|
2003-06-04 17:37:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
mxmlRemove(mxml_node_t *node) /* I - Node to remove */
|
|
|
|
{
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlRemove(node=%p)\n", node);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-04 17:37:23 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!node || !node->parent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove from parent...
|
|
|
|
*/
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#if DEBUG > 1
|
|
|
|
fprintf(stderr, " BEFORE: node->parent=%p\n", node->parent);
|
|
|
|
if (node->parent)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " BEFORE: node->parent->child=%p\n", node->parent->child);
|
|
|
|
fprintf(stderr, " BEFORE: node->parent->last_child=%p\n", node->parent->last_child);
|
|
|
|
}
|
|
|
|
fprintf(stderr, " BEFORE: node->child=%p\n", node->child);
|
|
|
|
fprintf(stderr, " BEFORE: node->last_child=%p\n", node->last_child);
|
|
|
|
fprintf(stderr, " BEFORE: node->prev=%p\n", node->prev);
|
|
|
|
fprintf(stderr, " BEFORE: node->next=%p\n", node->next);
|
|
|
|
#endif /* DEBUG > 1 */
|
|
|
|
|
2003-06-04 17:37:23 +00:00
|
|
|
if (node->prev)
|
|
|
|
node->prev->next = node->next;
|
|
|
|
else
|
|
|
|
node->parent->child = node->next;
|
|
|
|
|
|
|
|
if (node->next)
|
|
|
|
node->next->prev = node->prev;
|
|
|
|
else
|
|
|
|
node->parent->last_child = node->prev;
|
2003-06-05 03:06:20 +00:00
|
|
|
|
|
|
|
node->parent = NULL;
|
|
|
|
node->prev = NULL;
|
|
|
|
node->next = NULL;
|
2003-12-03 03:59:04 +00:00
|
|
|
|
|
|
|
#if DEBUG > 1
|
|
|
|
fprintf(stderr, " AFTER: node->parent=%p\n", node->parent);
|
|
|
|
if (node->parent)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " AFTER: node->parent->child=%p\n", node->parent->child);
|
|
|
|
fprintf(stderr, " AFTER: node->parent->last_child=%p\n", node->parent->last_child);
|
|
|
|
}
|
|
|
|
fprintf(stderr, " AFTER: node->child=%p\n", node->child);
|
|
|
|
fprintf(stderr, " AFTER: node->last_child=%p\n", node->last_child);
|
|
|
|
fprintf(stderr, " AFTER: node->prev=%p\n", node->prev);
|
|
|
|
fprintf(stderr, " AFTER: node->next=%p\n", node->next);
|
|
|
|
#endif /* DEBUG > 1 */
|
2003-06-04 17:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-19 21:12:08 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlNewXML()' - Create a new XML document tree.
|
|
|
|
*
|
|
|
|
* The "version" argument specifies the version number to put in the
|
2017-04-24 14:42:03 +00:00
|
|
|
* ?xml element node. If @code NULL@, version "1.0" is assumed.
|
2007-04-19 21:12:08 +00:00
|
|
|
*
|
|
|
|
* @since Mini-XML 2.3@
|
|
|
|
*/
|
|
|
|
|
|
|
|
mxml_node_t * /* O - New ?xml node */
|
|
|
|
mxmlNewXML(const char *version) /* I - Version number to use */
|
|
|
|
{
|
|
|
|
char element[1024]; /* Element text */
|
|
|
|
|
|
|
|
|
2008-10-26 18:28:05 +00:00
|
|
|
snprintf(element, sizeof(element), "?xml version=\"%s\" encoding=\"utf-8\"?",
|
2007-04-19 21:12:08 +00:00
|
|
|
version ? version : "1.0");
|
|
|
|
|
|
|
|
return (mxmlNewElement(NULL, element));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-18 01:08:58 +00:00
|
|
|
/*
|
|
|
|
* 'mxmlRelease()' - Release a node.
|
|
|
|
*
|
|
|
|
* When the reference count reaches zero, the node (and any children)
|
2017-04-24 14:42:03 +00:00
|
|
|
* is deleted via @link mxmlDelete@.
|
2007-04-18 01:55:03 +00:00
|
|
|
*
|
|
|
|
* @since Mini-XML 2.3@
|
2007-04-18 01:08:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int /* O - New reference count */
|
|
|
|
mxmlRelease(mxml_node_t *node) /* I - Node */
|
|
|
|
{
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
if ((-- node->ref_count) <= 0)
|
|
|
|
{
|
|
|
|
mxmlDelete(node);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (node->ref_count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlRetain()' - Retain a node.
|
2007-04-18 01:55:03 +00:00
|
|
|
*
|
|
|
|
* @since Mini-XML 2.3@
|
2007-04-18 01:08:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int /* O - New reference count */
|
|
|
|
mxmlRetain(mxml_node_t *node) /* I - Node */
|
|
|
|
{
|
|
|
|
if (node)
|
|
|
|
return (++ node->ref_count);
|
|
|
|
else
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-11 20:51:49 +00:00
|
|
|
/*
|
|
|
|
* 'mxml_free()' - Free the memory used by a node.
|
|
|
|
*
|
|
|
|
* Note: Does not free child nodes, does not remove from parent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
mxml_free(mxml_node_t *node) /* I - Node */
|
|
|
|
{
|
|
|
|
int i; /* Looping var */
|
|
|
|
|
|
|
|
|
|
|
|
switch (node->type)
|
|
|
|
{
|
|
|
|
case MXML_ELEMENT :
|
|
|
|
if (node->value.element.name)
|
|
|
|
free(node->value.element.name);
|
|
|
|
|
|
|
|
if (node->value.element.num_attrs)
|
|
|
|
{
|
|
|
|
for (i = 0; i < node->value.element.num_attrs; i ++)
|
|
|
|
{
|
|
|
|
if (node->value.element.attrs[i].name)
|
|
|
|
free(node->value.element.attrs[i].name);
|
|
|
|
if (node->value.element.attrs[i].value)
|
|
|
|
free(node->value.element.attrs[i].value);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(node->value.element.attrs);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MXML_INTEGER :
|
|
|
|
/* Nothing to do */
|
|
|
|
break;
|
|
|
|
case MXML_OPAQUE :
|
|
|
|
if (node->value.opaque)
|
|
|
|
free(node->value.opaque);
|
|
|
|
break;
|
|
|
|
case MXML_REAL :
|
|
|
|
/* Nothing to do */
|
|
|
|
break;
|
|
|
|
case MXML_TEXT :
|
|
|
|
if (node->value.text.string)
|
|
|
|
free(node->value.text.string);
|
|
|
|
break;
|
|
|
|
case MXML_CUSTOM :
|
|
|
|
if (node->value.custom.data &&
|
|
|
|
node->value.custom.destroy)
|
|
|
|
(*(node->value.custom.destroy))(node->value.custom.data);
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free this node...
|
|
|
|
*/
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* 'mxml_new()' - Create a new node.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static mxml_node_t * /* O - New node */
|
|
|
|
mxml_new(mxml_node_t *parent, /* I - Parent node */
|
|
|
|
mxml_type_t type) /* I - Node type */
|
|
|
|
{
|
|
|
|
mxml_node_t *node; /* New node */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#if DEBUG > 1
|
|
|
|
fprintf(stderr, "mxml_new(parent=%p, type=%d)\n", parent, type);
|
|
|
|
#endif /* DEBUG > 1 */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Allocate memory for the node...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((node = calloc(1, sizeof(mxml_node_t))) == NULL)
|
2003-12-03 03:59:04 +00:00
|
|
|
{
|
|
|
|
#if DEBUG > 1
|
|
|
|
fputs(" returning NULL\n", stderr);
|
|
|
|
#endif /* DEBUG > 1 */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
return (NULL);
|
2003-12-03 03:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG > 1
|
|
|
|
fprintf(stderr, " returning %p\n", node);
|
|
|
|
#endif /* DEBUG > 1 */
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the node type...
|
|
|
|
*/
|
|
|
|
|
2007-04-18 01:08:58 +00:00
|
|
|
node->type = type;
|
|
|
|
node->ref_count = 1;
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add to the parent if present...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (parent)
|
2003-06-04 17:37:23 +00:00
|
|
|
mxmlAdd(parent, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, node);
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the new node...
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|