Debian Codegraph @

src/main/mxml-node.c

raw symbols SCIP
1 /*
2 * Node support code for mini-XML, a small XML-like file parsing library.
3 *
4 * Copyright 2003 by Michael Sweet.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * Contents:
17 *
18 * stp_mxmlAdd() - Add a node to a tree.
19 * stp_mxmlDelete() - Delete a node and all of its children.
20 * stp_mxmlNewElement() - Create a new element node.
21 * stp_mxmlNewInteger() - Create a new integer node.
22 * stp_mxmlNewOpaque() - Create a new opaque string.
23 * stp_mxmlNewReal() - Create a new real number node.
24 * stp_mxmlNewDimension() - Create a new dimension node.
25 * stp_mxmlNewText() - Create a new text fragment node.
26 * stp_mxmlRemove() - Remove a node from its parent.
27 * mxml_new() - Create a new node.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include <gutenprint/util.h>
35 #include <gutenprint/mxml.h>
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40
41 /*
42 * Local functions...
43 */
44
45 static stp_mxml_node_t *mxml_new(stp_mxml_node_t *parent, stp_mxml_type_t type);
46
47
48 /*
49 * 'stp_mxmlAdd()' - Add a node to a tree.
50 *
51 * Adds the specified node to the parent. If the child argument is not
52 * NULL, puts the new node before or after the specified child depending
53 * on the value of the where argument. If the child argument is NULL,
54 * puts the new node at the beginning of the child list (STP_MXML_ADD_BEFORE)
55 * or at the end of the child list (STP_MXML_ADD_AFTER). The constant
56 * STP_MXML_ADD_TO_PARENT can be used to specify a NULL child pointer.
57 */
58
59 void
60 stp_mxmlAdd(stp_mxml_node_t *parent, /* I - Parent node */
61 int where, /* I - Where to add, STP_MXML_ADD_BEFORE or STP_MXML_ADD_AFTER */
62 stp_mxml_node_t *child, /* I - Child node for where or STP_MXML_ADD_TO_PARENT */
63 stp_mxml_node_t *node) /* I - Node to add */
64 {
65 /* fprintf(stderr, "stp_mxmlAdd(parent=%p, where=%d, child=%p, node=%p)\n", parent,
66 where, child, node);*/
67
68 /*
69 * Range check input...
70 */
71
72 if (!parent || !node)
73 return;
74
75 /*
76 * Remove the node from any existing parent...
77 */
78
79 if (node->parent)
80 stp_mxmlRemove(node);
81
82 /*
83 * Reset pointers...
84 */
85
86 node->parent = parent;
87
88 switch (where)
89 {
90 case STP_MXML_ADD_BEFORE :
91 if (!child || child == parent->child || child->parent != parent)
92 {
93 /*
94 * Insert as first node under parent...
95 */
96
97 node->next = parent->child;
98
99 if (parent->child)
100 parent->child->prev = node;
101 else
102 parent->last_child = node;
103
104 parent->child = node;
105 }
106 else
107 {
108 /*
109 * Insert node before this child...
110 */
111
112 node->next = child;
113 node->prev = child->prev;
114
115 if (child->prev)
116 child->prev->next = node;
117 else
118 parent->child = node;
119
120 child->prev = node;
121 }
122 break;
123
124 case STP_MXML_ADD_AFTER :
125 if (!child || child == parent->last_child || child->parent != parent)
126 {
127 /*
128 * Insert as last node under parent...
129 */
130
131 node->parent = parent;
132 node->prev = parent->last_child;
133
134 if (parent->last_child)
135 parent->last_child->next = node;
136 else
137 parent->child = node;
138
139 parent->last_child = node;
140 }
141 else
142 {
143 /*
144 * Insert node after this child...
145 */
146
147 node->prev = child;
148 node->next = child->next;
149
150 if (child->next)
151 child->next->prev = node;
152 else
153 parent->last_child = node;
154
155 child->next = node;
156 }
157 break;
158 }
159 }
160
161
162 /*
163 * 'stp_mxmlDelete()' - Delete a node and all of its children.
164 *
165 * If the specified node has a parent, this function first removes the
166 * node from its parent using the stp_mxmlRemove() function.
167 */
168
169 void
170 stp_mxmlDelete(stp_mxml_node_t *node) /* I - Node to delete */
171 {
172 int i; /* Looping var */
173
174
175 /* fprintf(stderr, "stp_mxmlDelete(node=%p)\n", node);*/
176
177 /*
178 * Range check input...
179 */
180
181 if (!node)
182 return;
183
184 /*
185 * Remove the node from its parent, if any...
186 */
187
188 stp_mxmlRemove(node);
189
190 /*
191 * Delete children...
192 */
193
194 while (node->child)
195 stp_mxmlDelete(node->child);
196
197 /*
198 * Now delete any node data...
199 */
200
201 switch (node->type)
202 {
203 case STP_MXML_ELEMENT :
204 if (node->value.element.name)
205 free(node->value.element.name);
206
207 if (node->value.element.num_attrs)
208 {
209 for (i = 0; i < node->value.element.num_attrs; i ++)
210 {
211 if (node->value.element.attrs[i].name)
212 free(node->value.element.attrs[i].name);
213 if (node->value.element.attrs[i].value)
214 free(node->value.element.attrs[i].value);
215 }
216
217 free(node->value.element.attrs);
218 }
219 break;
220 case STP_MXML_INTEGER :
221 /* Nothing to do */
222 break;
223 case STP_MXML_OPAQUE :
224 if (node->value.opaque)
225 free(node->value.opaque);
226 break;
227 case STP_MXML_REAL :
228 /* Nothing to do */
229 break;
230 case STP_MXML_DIMENSION :
231 /* Nothing to do */
232 break;
233 case STP_MXML_TEXT :
234 if (node->value.text.string)
235 free(node->value.text.string);
236 break;
237 }
238
239 /*
240 * Free this node...
241 */
242
243 free(node);
244 }
245
246 /*
247 * 'stp_mxmlParent()' - Return the parent of a node or NULL if none.
248 */
249
250 stp_mxml_node_t *
251 stp_mxmlParent(stp_mxml_node_t *node) /* I - node to return parent from */
252 {
253 return node->parent;
254 }
255
256 /*
257 * 'stp_mxmlNewElement()' - Create a new element node.
258 *
259 * The new element node is added to the end of the specified parent's child
260 * list. The constant STP_MXML_NO_PARENT can be used to specify that the new
261 * element node has no parent.
262 */
263
264 stp_mxml_node_t * /* O - New node */
265 stp_mxmlNewElement(stp_mxml_node_t *parent, /* I - Parent node or STP_MXML_NO_PARENT */
266 const char *name) /* I - Name of element */
267 {
268 stp_mxml_node_t *node; /* New node */
269
270
271 /*
272 * Range check input...
273 */
274
275 if (!name)
276 return (NULL);
277
278 /*
279 * Create the node and set the element name...
280 */
281
282 if ((node = mxml_new(parent, STP_MXML_ELEMENT)) != NULL)
283 node->value.element.name = stp_strdup(name);
284
285 return (node);
286 }
287
288
289 /*
290 * 'stp_mxmlNewInteger()' - Create a new integer node.
291 *
292 * The new integer node is added to the end of the specified parent's child
293 * list. The constant STP_MXML_NO_PARENT can be used to specify that the new
294 * integer node has no parent.
295 */
296
297 stp_mxml_node_t * /* O - New node */
298 stp_mxmlNewInteger(stp_mxml_node_t *parent, /* I - Parent node or STP_MXML_NO_PARENT */
299 int integer) /* I - Integer value */
300 {
301 stp_mxml_node_t *node; /* New node */
302
303
304 /*
305 * Range check input...
306 */
307
308 if (!parent)
309 return (NULL);
310
311 /*
312 * Create the node and set the element name...
313 */
314
315 if ((node = mxml_new(parent, STP_MXML_INTEGER)) != NULL)
316 node->value.integer = integer;
317
318 return (node);
319 }
320
321
322 /*
323 * 'stp_mxmlNewOpaque()' - Create a new opaque string.
324 *
325 * The new opaque node is added to the end of the specified parent's child
326 * list. The constant STP_MXML_NO_PARENT can be used to specify that the new
327 * opaque node has no parent. The opaque string must be nul-terminated and
328 * is copied into the new node.
329 */
330
331 stp_mxml_node_t * /* O - New node */
332 stp_mxmlNewOpaque(stp_mxml_node_t *parent, /* I - Parent node or STP_MXML_NO_PARENT */
333 const char *opaque) /* I - Opaque string */
334 {
335 stp_mxml_node_t *node; /* New node */
336
337
338 /*
339 * Range check input...
340 */
341
342 if (!parent || !opaque)
343 return (NULL);
344
345 /*
346 * Create the node and set the element name...
347 */
348
349 if ((node = mxml_new(parent, STP_MXML_OPAQUE)) != NULL)
350 node->value.opaque = stp_strdup(opaque);
351
352 return (node);
353 }
354
355
356 /*
357 * 'stp_mxmlNewReal()' - Create a new real number node.
358 *
359 * The new real number node is added to the end of the specified parent's
360 * child list. The constant STP_MXML_NO_PARENT can be used to specify that
361 * the new real number node has no parent.
362 */
363
364 stp_mxml_node_t * /* O - New node */
365 stp_mxmlNewReal(stp_mxml_node_t *parent, /* I - Parent node or STP_MXML_NO_PARENT */
366 double real) /* I - Real number value */
367 {
368 stp_mxml_node_t *node; /* New node */
369
370
371 /*
372 * Range check input...
373 */
374
375 if (!parent)
376 return (NULL);
377
378 /*
379 * Create the node and set the element name...
380 */
381
382 if ((node = mxml_new(parent, STP_MXML_REAL)) != NULL)
383 node->value.real = real;
384
385 return (node);
386 }
387
388 stp_mxml_node_t * /* O - New node */
389 stp_mxmlNewDimension(stp_mxml_node_t *parent, /* I - Parent node or STP_MXML_NO_PARENT */
390 stp_dimension_t dimension) /* I - Dimension value */
391 {
392 return stp_mxmlNewReal(parent, (double) dimension);
393 }
394
395
396 /*
397 * 'stp_mxmlNewText()' - Create a new text fragment node.
398 *
399 * The new text node is added to the end of the specified parent's child
400 * list. The constant STP_MXML_NO_PARENT can be used to specify that the new
401 * text node has no parent. The whitespace parameter is used to specify
402 * whether leading whitespace is present before the node. The text
403 * string must be nul-terminated and is copied into the new node.
404 */
405
406 stp_mxml_node_t * /* O - New node */
407 stp_mxmlNewText(stp_mxml_node_t *parent, /* I - Parent node or STP_MXML_NO_PARENT */
408 int whitespace, /* I - 1 = leading whitespace, 0 = no whitespace */
409 const char *string) /* I - String */
410 {
411 stp_mxml_node_t *node; /* New node */
412
413
414 /*
415 * Range check input...
416 */
417
418 if (!parent || !string)
419 return (NULL);
420
421 /*
422 * Create the node and set the text value...
423 */
424
425 if ((node = mxml_new(parent, STP_MXML_TEXT)) != NULL)
426 {
427 node->value.text.whitespace = whitespace;
428 node->value.text.string = stp_strdup(string);
429 }
430
431 return (node);
432 }
433
434
435 /*
436 * 'stp_mxmlRemove()' - Remove a node from its parent.
437 *
438 * Does not free memory used by the node - use stp_mxmlDelete() for that.
439 * This function does nothing if the node has no parent.
440 */
441
442 void
443 stp_mxmlRemove(stp_mxml_node_t *node) /* I - Node to remove */
444 {
445 /*
446 * Range check input...
447 */
448
449 /* fprintf(stderr, "stp_mxmlRemove(node=%p)\n", node);*/
450
451 if (!node || !node->parent)
452 return;
453
454 /*
455 * Remove from parent...
456 */
457
458 if (node->prev)
459 node->prev->next = node->next;
460 else
461 node->parent->child = node->next;
462
463 if (node->next)
464 node->next->prev = node->prev;
465 else
466 node->parent->last_child = node->prev;
467
468 node->parent = NULL;
469 node->prev = NULL;
470 node->next = NULL;
471 }
472
473
474 /*
475 * 'mxml_new()' - Create a new node.
476 */
477
478 static stp_mxml_node_t * /* O - New node */
479 mxml_new(stp_mxml_node_t *parent, /* I - Parent node */
480 stp_mxml_type_t type) /* I - Node type */
481 {
482 stp_mxml_node_t *node; /* New node */
483
484
485 /*
486 * Allocate memory for the node...
487 */
488
489 if ((node = calloc(1, sizeof(stp_mxml_node_t))) == NULL)
490 return (NULL);
491
492 /*
493 * Set the node type...
494 */
495
496 node->type = type;
497
498 /*
499 * Add to the parent if present...
500 */
501
502 if (parent)
503 stp_mxmlAdd(parent, STP_MXML_ADD_AFTER, STP_MXML_ADD_TO_PARENT, node);
504
505 /*
506 * Return the new node...
507 */
508
509 return (node);
510 }