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
|
|
|
* Attribute support code for Mini-XML, a small XML-like file parsing library.
|
2003-06-03 19:46:29 +00:00
|
|
|
*
|
2005-01-29 07:21:44 +00:00
|
|
|
* Copyright 2003-2005 by Michael Sweet.
|
2003-06-03 19:46:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* Contents:
|
|
|
|
*
|
|
|
|
* mxmlElementGetAttr() - Get an attribute.
|
|
|
|
* mxmlElementSetAttr() - Set an attribute.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlElementGetAttr()' - Get an attribute.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* This function returns NULL if the node is not an element or the
|
|
|
|
* named attribute does not exist.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const char * /* O - Attribute value or NULL */
|
|
|
|
mxmlElementGetAttr(mxml_node_t *node, /* I - Element node */
|
|
|
|
const char *name) /* I - Name of attribute */
|
|
|
|
{
|
|
|
|
int i; /* Looping var */
|
|
|
|
mxml_attr_t *attr; /* Cirrent attribute */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlElementGetAttr(node=%p, name=\"%s\")\n",
|
|
|
|
node, name ? name : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!node || node->type != MXML_ELEMENT || !name)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for the attribute...
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
|
|
|
|
i > 0;
|
|
|
|
i --, attr ++)
|
|
|
|
if (!strcmp(attr->name, name))
|
|
|
|
return (attr->value);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Didn't find attribute, so return NULL...
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'mxmlElementSetAttr()' - Set an attribute.
|
2003-06-14 23:56:47 +00:00
|
|
|
*
|
|
|
|
* If the named attribute already exists, the value of the attribute
|
|
|
|
* is replaced by the new string value. The string value is copied
|
|
|
|
* into the element node. This function does nothing if the node is
|
|
|
|
* not an element.
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
mxmlElementSetAttr(mxml_node_t *node, /* I - Element node */
|
|
|
|
const char *name, /* I - Name of attribute */
|
|
|
|
const char *value) /* I - Attribute value */
|
|
|
|
{
|
|
|
|
int i; /* Looping var */
|
|
|
|
mxml_attr_t *attr; /* New attribute */
|
|
|
|
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "mxmlElementSetAttr(node=%p, name=\"%s\", value=\"%s\")\n",
|
|
|
|
node, name ? name : "(null)", value ? value : "(null)");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2003-06-03 19:46:29 +00:00
|
|
|
/*
|
|
|
|
* Range check input...
|
|
|
|
*/
|
|
|
|
|
2003-12-01 15:27:47 +00:00
|
|
|
if (!node || node->type != MXML_ELEMENT || !name)
|
2003-06-03 19:46:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for the attribute...
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
|
|
|
|
i > 0;
|
|
|
|
i --, attr ++)
|
|
|
|
if (!strcmp(attr->name, name))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Replace the attribute value and return...
|
|
|
|
*/
|
|
|
|
|
2004-06-01 20:19:34 +00:00
|
|
|
if (attr->value)
|
|
|
|
free(attr->value);
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2003-12-01 15:27:47 +00:00
|
|
|
if (value)
|
2004-06-01 20:19:34 +00:00
|
|
|
{
|
|
|
|
if ((attr->value = strdup(value)) == NULL)
|
|
|
|
mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
|
|
|
|
name, node->value.element.name);
|
|
|
|
}
|
2003-12-01 15:27:47 +00:00
|
|
|
else
|
|
|
|
attr->value = NULL;
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attribute not found, so add a new one...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (node->value.element.num_attrs == 0)
|
|
|
|
attr = malloc(sizeof(mxml_attr_t));
|
|
|
|
else
|
|
|
|
attr = realloc(node->value.element.attrs,
|
|
|
|
(node->value.element.num_attrs + 1) * sizeof(mxml_attr_t));
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
{
|
2003-12-03 03:59:04 +00:00
|
|
|
mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
|
|
|
|
name, node->value.element.name);
|
2003-06-03 19:46:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->value.element.attrs = attr;
|
|
|
|
attr += node->value.element.num_attrs;
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
attr->name = strdup(name);
|
2003-12-01 15:27:47 +00:00
|
|
|
if (value)
|
|
|
|
attr->value = strdup(value);
|
|
|
|
else
|
|
|
|
attr->value = NULL;
|
2003-06-03 19:46:29 +00:00
|
|
|
|
2003-12-01 15:27:47 +00:00
|
|
|
if (!attr->name || (!attr->value && value))
|
2003-06-03 19:46:29 +00:00
|
|
|
{
|
|
|
|
if (attr->name)
|
|
|
|
free(attr->name);
|
|
|
|
|
|
|
|
if (attr->value)
|
|
|
|
free(attr->value);
|
|
|
|
|
2003-12-03 03:59:04 +00:00
|
|
|
mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
|
|
|
|
name, node->value.element.name);
|
2003-06-03 19:46:29 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->value.element.num_attrs ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-01-29 07:21:44 +00:00
|
|
|
* End of "$Id$".
|
2003-06-03 19:46:29 +00:00
|
|
|
*/
|