2
* "$Id: mxml-attr.c 308 2007-09-15 20:04:56Z mike $"
4
* Attribute support code for Mini-XML, a small XML-like file parsing library.
6
* Copyright 2003-2007 by Michael Sweet.
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Library General Public
10
* License as published by the Free Software Foundation; either
11
* version 2, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
20
* mxmlElementDeleteAttr() - Delete an attribute.
21
* mxmlElementGetAttr() - Get an attribute.
22
* mxmlElementSetAttr() - Set an attribute.
23
* mxmlElementSetAttrf() - Set an attribute with a formatted value.
24
* mxml_set_attr() - Set or add an attribute name/value pair.
28
* Include necessary headers...
31
#include "mxml-config.h"
39
static int mxml_set_attr(mxml_node_t *node, const char *name,
44
* 'mxmlElementDeleteAttr()' - Delete an attribute.
46
* @since Mini-XML 2.4@
50
mxmlElementDeleteAttr(mxml_node_t *node,/* I - Element */
51
const char *name)/* I - Attribute name */
53
int i; /* Looping var */
54
mxml_attr_t *attr; /* Cirrent attribute */
58
fprintf(stderr, "mxmlElementDeleteAttr(node=%p, name=\"%s\")\n",
59
node, name ? name : "(null)");
63
* Range check input...
66
if (!node || node->type != MXML_ELEMENT || !name)
70
* Look for the attribute...
73
for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
78
printf(" %s=\"%s\"\n", attr->name, attr->value);
81
if (!strcmp(attr->name, name))
84
* Delete this attribute...
92
memmove(attr, attr + 1, i * sizeof(mxml_attr_t));
94
node->value.element.num_attrs --;
102
* 'mxmlElementGetAttr()' - Get an attribute.
104
* This function returns NULL if the node is not an element or the
105
* named attribute does not exist.
108
const char * /* O - Attribute value or NULL */
109
mxmlElementGetAttr(mxml_node_t *node, /* I - Element node */
110
const char *name) /* I - Name of attribute */
112
int i; /* Looping var */
113
mxml_attr_t *attr; /* Cirrent attribute */
117
fprintf(stderr, "mxmlElementGetAttr(node=%p, name=\"%s\")\n",
118
node, name ? name : "(null)");
122
* Range check input...
125
if (!node || node->type != MXML_ELEMENT || !name)
129
* Look for the attribute...
132
for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
137
printf(" %s=\"%s\"\n", attr->name, attr->value);
140
if (!strcmp(attr->name, name))
143
printf(" Returning \"%s\"!\n", attr->value);
145
return (attr->value);
150
* Didn't find attribute, so return NULL...
154
puts(" Returning NULL!\n");
162
* 'mxmlElementSetAttr()' - Set an attribute.
164
* If the named attribute already exists, the value of the attribute
165
* is replaced by the new string value. The string value is copied
166
* into the element node. This function does nothing if the node is
171
mxmlElementSetAttr(mxml_node_t *node, /* I - Element node */
172
const char *name, /* I - Name of attribute */
173
const char *value) /* I - Attribute value */
175
char *valuec; /* Copy of value */
179
fprintf(stderr, "mxmlElementSetAttr(node=%p, name=\"%s\", value=\"%s\")\n",
180
node, name ? name : "(null)", value ? value : "(null)");
184
* Range check input...
187
if (!node || node->type != MXML_ELEMENT || !name)
191
valuec = strdup(value);
195
if (mxml_set_attr(node, name, valuec))
201
* 'mxmlElementSetAttrf()' - Set an attribute with a formatted value.
203
* If the named attribute already exists, the value of the attribute
204
* is replaced by the new formatted string. The formatted string value is
205
* copied into the element node. This function does nothing if the node
208
* @since Mini-XML 2.3@
212
mxmlElementSetAttrf(mxml_node_t *node, /* I - Element node */
213
const char *name, /* I - Name of attribute */
214
const char *format,/* I - Printf-style attribute value */
215
...) /* I - Additional arguments as needed */
217
va_list ap; /* Argument pointer */
218
char *value; /* Value */
223
"mxmlElementSetAttrf(node=%p, name=\"%s\", format=\"%s\", ...)\n",
224
node, name ? name : "(null)", format ? format : "(null)");
228
* Range check input...
231
if (!node || node->type != MXML_ELEMENT || !name || !format)
235
* Format the value...
238
va_start(ap, format);
239
value = _mxml_vstrdupf(format, ap);
243
mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
244
name, node->value.element.name);
245
else if (mxml_set_attr(node, name, value))
251
* 'mxml_set_attr()' - Set or add an attribute name/value pair.
254
static int /* O - 0 on success, -1 on failure */
255
mxml_set_attr(mxml_node_t *node, /* I - Element node */
256
const char *name, /* I - Attribute name */
257
char *value) /* I - Attribute value */
259
int i; /* Looping var */
260
mxml_attr_t *attr; /* New attribute */
264
* Look for the attribute...
267
for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
270
if (!strcmp(attr->name, name))
273
* Free the old value as needed...
285
* Add a new attribute...
288
if (node->value.element.num_attrs == 0)
289
attr = malloc(sizeof(mxml_attr_t));
291
attr = realloc(node->value.element.attrs,
292
(node->value.element.num_attrs + 1) * sizeof(mxml_attr_t));
296
mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
297
name, node->value.element.name);
301
node->value.element.attrs = attr;
302
attr += node->value.element.num_attrs;
304
if ((attr->name = strdup(name)) == NULL)
306
mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
307
name, node->value.element.name);
313
node->value.element.num_attrs ++;
320
* End of "$Id: mxml-attr.c 308 2007-09-15 20:04:56Z mike $".