2 * "$Id: mxml-attr.c 451 2014-01-04 21:50:06Z msweet $"
4 * Attribute support code for Mini-XML, a small XML-like file parsing library.
6 * Copyright 2003-2014 by Michael R Sweet.
8 * These coded instructions, statements, and computer programs are the
9 * property of Michael R Sweet and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "COPYING"
11 * which should have been included with this file. If this file is
12 * missing or damaged, see the license at:
14 * http://www.msweet.org/projects.php/Mini-XML
18 * Include necessary headers...
29 static int mxml_set_attr(mxml_node_t *node, const char *name,
34 * 'mxmlElementDeleteAttr()' - Delete an attribute.
36 * @since Mini-XML 2.4@
40 mxmlElementDeleteAttr(mxml_node_t *node,/* I - Element */
41 const char *name)/* I - Attribute name */
43 int i; /* Looping var */
44 mxml_attr_t *attr; /* Cirrent attribute */
48 fprintf(stderr, "mxmlElementDeleteAttr(node=%p, name=\"%s\")\n",
49 node, name ? name : "(null)");
53 * Range check input...
56 if (!node || node->type != MXML_ELEMENT || !name)
60 * Look for the attribute...
63 for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
68 printf(" %s=\"%s\"\n", attr->name, attr->value);
71 if (!strcmp(attr->name, name))
74 * Delete this attribute...
82 memmove(attr, attr + 1, i * sizeof(mxml_attr_t));
84 node->value.element.num_attrs --;
86 if (node->value.element.num_attrs == 0)
87 free(node->value.element.attrs);
95 * 'mxmlElementGetAttr()' - Get an attribute.
97 * This function returns NULL if the node is not an element or the
98 * named attribute does not exist.
101 const char * /* O - Attribute value or NULL */
102 mxmlElementGetAttr(mxml_node_t *node, /* I - Element node */
103 const char *name) /* I - Name of attribute */
105 int i; /* Looping var */
106 mxml_attr_t *attr; /* Cirrent attribute */
110 fprintf(stderr, "mxmlElementGetAttr(node=%p, name=\"%s\")\n",
111 node, name ? name : "(null)");
115 * Range check input...
118 if (!node || node->type != MXML_ELEMENT || !name)
122 * Look for the attribute...
125 for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
130 printf(" %s=\"%s\"\n", attr->name, attr->value);
133 if (!strcmp(attr->name, name))
136 printf(" Returning \"%s\"!\n", attr->value);
138 return (attr->value);
143 * Didn't find attribute, so return NULL...
147 puts(" Returning NULL!\n");
155 * 'mxmlElementSetAttr()' - Set an attribute.
157 * If the named attribute already exists, the value of the attribute
158 * is replaced by the new string value. The string value is copied
159 * into the element node. This function does nothing if the node is
164 mxmlElementSetAttr(mxml_node_t *node, /* I - Element node */
165 const char *name, /* I - Name of attribute */
166 const char *value) /* I - Attribute value */
168 char *valuec; /* Copy of value */
172 fprintf(stderr, "mxmlElementSetAttr(node=%p, name=\"%s\", value=\"%s\")\n",
173 node, name ? name : "(null)", value ? value : "(null)");
177 * Range check input...
180 if (!node || node->type != MXML_ELEMENT || !name)
184 valuec = strdup(value);
188 if (mxml_set_attr(node, name, valuec))
194 * 'mxmlElementSetAttrf()' - Set an attribute with a formatted value.
196 * If the named attribute already exists, the value of the attribute
197 * is replaced by the new formatted string. The formatted string value is
198 * copied into the element node. This function does nothing if the node
201 * @since Mini-XML 2.3@
205 mxmlElementSetAttrf(mxml_node_t *node, /* I - Element node */
206 const char *name, /* I - Name of attribute */
207 const char *format,/* I - Printf-style attribute value */
208 ...) /* I - Additional arguments as needed */
210 va_list ap; /* Argument pointer */
211 char *value; /* Value */
216 "mxmlElementSetAttrf(node=%p, name=\"%s\", format=\"%s\", ...)\n",
217 node, name ? name : "(null)", format ? format : "(null)");
221 * Range check input...
224 if (!node || node->type != MXML_ELEMENT || !name || !format)
228 * Format the value...
231 va_start(ap, format);
232 value = _mxml_vstrdupf(format, ap);
236 mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
237 name, node->value.element.name);
238 else if (mxml_set_attr(node, name, value))
244 * 'mxml_set_attr()' - Set or add an attribute name/value pair.
247 static int /* O - 0 on success, -1 on failure */
248 mxml_set_attr(mxml_node_t *node, /* I - Element node */
249 const char *name, /* I - Attribute name */
250 char *value) /* I - Attribute value */
252 int i; /* Looping var */
253 mxml_attr_t *attr; /* New attribute */
257 * Look for the attribute...
260 for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
263 if (!strcmp(attr->name, name))
266 * Free the old value as needed...
278 * Add a new attribute...
281 if (node->value.element.num_attrs == 0)
282 attr = malloc(sizeof(mxml_attr_t));
284 attr = realloc(node->value.element.attrs,
285 (node->value.element.num_attrs + 1) * sizeof(mxml_attr_t));
289 mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
290 name, node->value.element.name);
294 node->value.element.attrs = attr;
295 attr += node->value.element.num_attrs;
297 if ((attr->name = strdup(name)) == NULL)
299 mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
300 name, node->value.element.name);
306 node->value.element.num_attrs ++;
313 * End of "$Id: mxml-attr.c 451 2014-01-04 21:50:06Z msweet $".