2 * "$Id: mxml-set.c 451 2014-01-04 21:50:06Z msweet $"
4 * Node set functions 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...
26 * 'mxmlSetCDATA()' - Set the element name of a CDATA node.
28 * The node is not changed if it (or its first child) is not a CDATA element node.
30 * @since Mini-XML 2.3@
33 int /* O - 0 on success, -1 on failure */
34 mxmlSetCDATA(mxml_node_t *node, /* I - Node to set */
35 const char *data) /* I - New data string */
38 * Range check input...
41 if (node && node->type == MXML_ELEMENT &&
42 strncmp(node->value.element.name, "![CDATA[", 8) &&
43 node->child && node->child->type == MXML_ELEMENT &&
44 !strncmp(node->child->value.element.name, "![CDATA[", 8))
47 if (!node || node->type != MXML_ELEMENT || !data ||
48 strncmp(node->value.element.name, "![CDATA[", 8))
52 * Free any old element value and set the new value...
55 if (node->value.element.name)
56 free(node->value.element.name);
58 node->value.element.name = _mxml_strdupf("![CDATA[%s]]", data);
65 * 'mxmlSetCustom()' - Set the data and destructor of a custom data node.
67 * The node is not changed if it (or its first child) is not a custom node.
69 * @since Mini-XML 2.1@
72 int /* O - 0 on success, -1 on failure */
74 mxml_node_t *node, /* I - Node to set */
75 void *data, /* I - New data pointer */
76 mxml_custom_destroy_cb_t destroy) /* I - New destructor function */
79 * Range check input...
82 if (node && node->type == MXML_ELEMENT &&
83 node->child && node->child->type == MXML_CUSTOM)
86 if (!node || node->type != MXML_CUSTOM)
90 * Free any old element value and set the new value...
93 if (node->value.custom.data && node->value.custom.destroy)
94 (*(node->value.custom.destroy))(node->value.custom.data);
96 node->value.custom.data = data;
97 node->value.custom.destroy = destroy;
104 * 'mxmlSetElement()' - Set the name of an element node.
106 * The node is not changed if it is not an element node.
109 int /* O - 0 on success, -1 on failure */
110 mxmlSetElement(mxml_node_t *node, /* I - Node to set */
111 const char *name) /* I - New name string */
114 * Range check input...
117 if (!node || node->type != MXML_ELEMENT || !name)
121 * Free any old element value and set the new value...
124 if (node->value.element.name)
125 free(node->value.element.name);
127 node->value.element.name = strdup(name);
134 * 'mxmlSetInteger()' - Set the value of an integer node.
136 * The node is not changed if it (or its first child) is not an integer node.
139 int /* O - 0 on success, -1 on failure */
140 mxmlSetInteger(mxml_node_t *node, /* I - Node to set */
141 int integer) /* I - Integer value */
144 * Range check input...
147 if (node && node->type == MXML_ELEMENT &&
148 node->child && node->child->type == MXML_INTEGER)
151 if (!node || node->type != MXML_INTEGER)
155 * Set the new value and return...
158 node->value.integer = integer;
165 * 'mxmlSetOpaque()' - Set the value of an opaque node.
167 * The node is not changed if it (or its first child) is not an opaque node.
170 int /* O - 0 on success, -1 on failure */
171 mxmlSetOpaque(mxml_node_t *node, /* I - Node to set */
172 const char *opaque) /* I - Opaque string */
175 * Range check input...
178 if (node && node->type == MXML_ELEMENT &&
179 node->child && node->child->type == MXML_OPAQUE)
182 if (!node || node->type != MXML_OPAQUE || !opaque)
186 * Free any old opaque value and set the new value...
189 if (node->value.opaque)
190 free(node->value.opaque);
192 node->value.opaque = strdup(opaque);
199 * 'mxmlSetReal()' - Set the value of a real number node.
201 * The node is not changed if it (or its first child) is not a real number node.
204 int /* O - 0 on success, -1 on failure */
205 mxmlSetReal(mxml_node_t *node, /* I - Node to set */
206 double real) /* I - Real number value */
209 * Range check input...
212 if (node && node->type == MXML_ELEMENT &&
213 node->child && node->child->type == MXML_REAL)
216 if (!node || node->type != MXML_REAL)
220 * Set the new value and return...
223 node->value.real = real;
230 * 'mxmlSetText()' - Set the value of a text node.
232 * The node is not changed if it (or its first child) is not a text node.
235 int /* O - 0 on success, -1 on failure */
236 mxmlSetText(mxml_node_t *node, /* I - Node to set */
237 int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
238 const char *string) /* I - String */
241 * Range check input...
244 if (node && node->type == MXML_ELEMENT &&
245 node->child && node->child->type == MXML_TEXT)
248 if (!node || node->type != MXML_TEXT || !string)
252 * Free any old string value and set the new value...
255 if (node->value.text.string)
256 free(node->value.text.string);
258 node->value.text.whitespace = whitespace;
259 node->value.text.string = strdup(string);
266 * 'mxmlSetTextf()' - Set the value of a text node to a formatted string.
268 * The node is not changed if it (or its first child) is not a text node.
271 int /* O - 0 on success, -1 on failure */
272 mxmlSetTextf(mxml_node_t *node, /* I - Node to set */
273 int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
274 const char *format, /* I - Printf-style format string */
275 ...) /* I - Additional arguments as needed */
277 va_list ap; /* Pointer to arguments */
281 * Range check input...
284 if (node && node->type == MXML_ELEMENT &&
285 node->child && node->child->type == MXML_TEXT)
288 if (!node || node->type != MXML_TEXT || !format)
292 * Free any old string value and set the new value...
295 if (node->value.text.string)
296 free(node->value.text.string);
298 va_start(ap, format);
300 node->value.text.whitespace = whitespace;
301 node->value.text.string = _mxml_strdupf(format, ap);
310 * 'mxmlSetUserData()' - Set the user data pointer for a node.
312 * @since Mini-XML 2.7@
315 int /* O - 0 on success, -1 on failure */
316 mxmlSetUserData(mxml_node_t *node, /* I - Node to set */
317 void *data) /* I - User data pointer */
320 * Range check input...
327 * Set the user data pointer and return...
330 node->user_data = data;
336 * End of "$Id: mxml-set.c 451 2014-01-04 21:50:06Z msweet $".