Process Hacker
mxml-set.c
Go to the documentation of this file.
1 /*
2  * "$Id: mxml-set.c 270 2007-04-23 21:48:03Z mike $"
3  *
4  * Node set functions for Mini-XML, a small XML-like file parsing library.
5  *
6  * Copyright 2003-2007 by Michael Sweet.
7  *
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.
12  *
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.
17  *
18  * Contents:
19  *
20  * mxmlSetCustom() - Set the data and destructor of a custom data node.
21  * mxmlSetCDATA() - Set the element name of a CDATA node.
22  * mxmlSetElement() - Set the name of an element node.
23  * mxmlSetInteger() - Set the value of an integer node.
24  * mxmlSetOpaque() - Set the value of an opaque node.
25  * mxmlSetReal() - Set the value of a real number node.
26  * mxmlSetText() - Set the value of a text node.
27  * mxmlSetTextf() - Set the value of a text node to a formatted string.
28  */
29 
30 /*
31  * Include necessary headers...
32  */
33 
34 #include <phbase.h>
35 #include "config.h"
36 #include "mxml.h"
37 
38 
39 /*
40  * 'mxmlSetCustom()' - Set the data and destructor of a custom data node.
41  *
42  * The node is not changed if it is not a custom node.
43  *
44  * @since Mini-XML 2.1@
45  */
46 
47 int /* O - 0 on success, -1 on failure */
49  mxml_node_t *node, /* I - Node to set */
50  void *data, /* I - New data pointer */
51  mxml_custom_destroy_cb_t destroy) /* I - New destructor function */
52 {
53  /*
54  * Range check input...
55  */
56 
57  if (!node || node->type != MXML_CUSTOM)
58  return (-1);
59 
60  /*
61  * Free any old element value and set the new value...
62  */
63 
64  if (node->value.custom.data && node->value.custom.destroy)
65  (*(node->value.custom.destroy))(node->value.custom.data);
66 
67  node->value.custom.data = data;
68  node->value.custom.destroy = destroy;
69 
70  return (0);
71 }
72 
73 
74 /*
75  * 'mxmlSetCDATA()' - Set the element name of a CDATA node.
76  *
77  * The node is not changed if it is not a CDATA element node.
78  *
79  * @since Mini-XML 2.3@
80  */
81 
82 int /* O - 0 on success, -1 on failure */
83 mxmlSetCDATA(mxml_node_t *node, /* I - Node to set */
84  const char *data) /* I - New data string */
85 {
86  /*
87  * Range check input...
88  */
89 
90  if (!node || node->type != MXML_ELEMENT || !data ||
91  strncmp(node->value.element.name, "![CDATA[", 8))
92  return (-1);
93 
94  /*
95  * Free any old element value and set the new value...
96  */
97 
98  if (node->value.element.name)
99  PhFree(node->value.element.name);
100 
101  node->value.element.name = _mxml_strdupf("![CDATA[%s]]", data);
102 
103  return (0);
104 }
105 
106 
107 /*
108  * 'mxmlSetElement()' - Set the name of an element node.
109  *
110  * The node is not changed if it is not an element node.
111  */
112 
113 int /* O - 0 on success, -1 on failure */
114 mxmlSetElement(mxml_node_t *node, /* I - Node to set */
115  const char *name) /* I - New name string */
116 {
117  /*
118  * Range check input...
119  */
120 
121  if (!node || node->type != MXML_ELEMENT || !name)
122  return (-1);
123 
124  /*
125  * Free any old element value and set the new value...
126  */
127 
128  if (node->value.element.name)
129  PhFree(node->value.element.name);
130 
131  node->value.element.name = PhDuplicateBytesZSafe((char *)name);
132 
133  return (0);
134 }
135 
136 
137 /*
138  * 'mxmlSetInteger()' - Set the value of an integer node.
139  *
140  * The node is not changed if it is not an integer node.
141  */
142 
143 int /* O - 0 on success, -1 on failure */
144 mxmlSetInteger(mxml_node_t *node, /* I - Node to set */
145  int integer) /* I - Integer value */
146 {
147  /*
148  * Range check input...
149  */
150 
151  if (!node || node->type != MXML_INTEGER)
152  return (-1);
153 
154  /*
155  * Set the new value and return...
156  */
157 
158  node->value.integer = integer;
159 
160  return (0);
161 }
162 
163 
164 /*
165  * 'mxmlSetOpaque()' - Set the value of an opaque node.
166  *
167  * The node is not changed if it is not an opaque node.
168  */
169 
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 */
173 {
174  /*
175  * Range check input...
176  */
177 
178  if (!node || node->type != MXML_OPAQUE || !opaque)
179  return (-1);
180 
181  /*
182  * Free any old opaque value and set the new value...
183  */
184 
185  if (node->value.opaque)
186  PhFree(node->value.opaque);
187 
188  node->value.opaque = PhDuplicateBytesZSafe((char *)opaque);
189 
190  return (0);
191 }
192 
193 
194 /*
195  * 'mxmlSetReal()' - Set the value of a real number node.
196  *
197  * The node is not changed if it is not a real number node.
198  */
199 
200 int /* O - 0 on success, -1 on failure */
201 mxmlSetReal(mxml_node_t *node, /* I - Node to set */
202  double real) /* I - Real number value */
203 {
204  /*
205  * Range check input...
206  */
207 
208  if (!node || node->type != MXML_REAL)
209  return (-1);
210 
211  /*
212  * Set the new value and return...
213  */
214 
215  node->value.real = real;
216 
217  return (0);
218 }
219 
220 
221 /*
222  * 'mxmlSetText()' - Set the value of a text node.
223  *
224  * The node is not changed if it is not a text node.
225  */
226 
227 int /* O - 0 on success, -1 on failure */
228 mxmlSetText(mxml_node_t *node, /* I - Node to set */
229  int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
230  const char *string) /* I - String */
231 {
232  /*
233  * Range check input...
234  */
235 
236  if (!node || node->type != MXML_TEXT || !string)
237  return (-1);
238 
239  /*
240  * Free any old string value and set the new value...
241  */
242 
243  if (node->value.text.string)
244  PhFree(node->value.text.string);
245 
246  node->value.text.whitespace = whitespace;
247  node->value.text.string = PhDuplicateBytesZSafe((char *)string);
248 
249  return (0);
250 }
251 
252 
253 /*
254  * 'mxmlSetTextf()' - Set the value of a text node to a formatted string.
255  *
256  * The node is not changed if it is not a text node.
257  */
258 
259 int /* O - 0 on success, -1 on failure */
260 mxmlSetTextf(mxml_node_t *node, /* I - Node to set */
261  int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
262  const char *format, /* I - Printf-style format string */
263  ...) /* I - Additional arguments as needed */
264 {
265  va_list ap; /* Pointer to arguments */
266 
267 
268  /*
269  * Range check input...
270  */
271 
272  if (!node || node->type != MXML_TEXT || !format)
273  return (-1);
274 
275  /*
276  * Free any old string value and set the new value...
277  */
278 
279  if (node->value.text.string)
280  PhFree(node->value.text.string);
281 
282  va_start(ap, format);
283 
284  node->value.text.whitespace = whitespace;
285  node->value.text.string = _mxml_strdupf(format, ap);
286 
287  va_end(ap);
288 
289  return (0);
290 }
291 
292 
293 /*
294  * End of "$Id: mxml-set.c 270 2007-04-23 21:48:03Z mike $".
295  */