Statistics
| Revision:

root / logic / trunk / src / mxml / mxml-set.c @ 49

History | View | Annotate | Download (6.42 KB)

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