00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "config.h"
00037 #include "mxml.h"
00038
00039
00040
00041
00042
00043
00044 static mxml_node_t *mxml_new(mxml_node_t *parent, mxml_type_t type);
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 void
00059 mxmlAdd(mxml_node_t *parent,
00060 int where,
00061 mxml_node_t *child,
00062 mxml_node_t *node)
00063 {
00064 #ifdef DEBUG
00065 fprintf(stderr, "mxmlAdd(parent=%p, where=%d, child=%p, node=%p)\n", parent,
00066 where, child, node);
00067 #endif
00068
00069
00070
00071
00072
00073 if (!parent || !node)
00074 return;
00075
00076 #if DEBUG > 1
00077 fprintf(stderr, " BEFORE: node->parent=%p\n", node->parent);
00078 if (parent)
00079 {
00080 fprintf(stderr, " BEFORE: parent->child=%p\n", parent->child);
00081 fprintf(stderr, " BEFORE: parent->last_child=%p\n", parent->last_child);
00082 fprintf(stderr, " BEFORE: parent->prev=%p\n", parent->prev);
00083 fprintf(stderr, " BEFORE: parent->next=%p\n", parent->next);
00084 }
00085 #endif
00086
00087
00088
00089
00090
00091 if (node->parent)
00092 mxmlRemove(node);
00093
00094
00095
00096
00097
00098 node->parent = parent;
00099
00100 switch (where)
00101 {
00102 case MXML_ADD_BEFORE :
00103 if (!child || child == parent->child || child->parent != parent)
00104 {
00105
00106
00107
00108
00109 node->next = parent->child;
00110
00111 if (parent->child)
00112 parent->child->prev = node;
00113 else
00114 parent->last_child = node;
00115
00116 parent->child = node;
00117 }
00118 else
00119 {
00120
00121
00122
00123
00124 node->next = child;
00125 node->prev = child->prev;
00126
00127 if (child->prev)
00128 child->prev->next = node;
00129 else
00130 parent->child = node;
00131
00132 child->prev = node;
00133 }
00134 break;
00135
00136 case MXML_ADD_AFTER :
00137 if (!child || child == parent->last_child || child->parent != parent)
00138 {
00139
00140
00141
00142
00143 node->parent = parent;
00144 node->prev = parent->last_child;
00145
00146 if (parent->last_child)
00147 parent->last_child->next = node;
00148 else
00149 parent->child = node;
00150
00151 parent->last_child = node;
00152 }
00153 else
00154 {
00155
00156
00157
00158
00159 node->prev = child;
00160 node->next = child->next;
00161
00162 if (child->next)
00163 child->next->prev = node;
00164 else
00165 parent->last_child = node;
00166
00167 child->next = node;
00168 }
00169 break;
00170 }
00171
00172 #if DEBUG > 1
00173 fprintf(stderr, " AFTER: node->parent=%p\n", node->parent);
00174 if (parent)
00175 {
00176 fprintf(stderr, " AFTER: parent->child=%p\n", parent->child);
00177 fprintf(stderr, " AFTER: parent->last_child=%p\n", parent->last_child);
00178 fprintf(stderr, " AFTER: parent->prev=%p\n", parent->prev);
00179 fprintf(stderr, " AFTER: parent->next=%p\n", parent->next);
00180 }
00181 #endif
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 void
00193 mxmlDelete(mxml_node_t *node)
00194 {
00195 int i;
00196
00197
00198 #ifdef DEBUG
00199 fprintf(stderr, "mxmlDelete(node=%p)\n", node);
00200 #endif
00201
00202
00203
00204
00205
00206 if (!node)
00207 return;
00208
00209
00210
00211
00212
00213 mxmlRemove(node);
00214
00215
00216
00217
00218
00219 while (node->child)
00220 mxmlDelete(node->child);
00221
00222
00223
00224
00225
00226 switch (node->type)
00227 {
00228 case MXML_ELEMENT :
00229 if (node->value.element.name)
00230 free(node->value.element.name);
00231
00232 if (node->value.element.num_attrs)
00233 {
00234 for (i = 0; i < node->value.element.num_attrs; i ++)
00235 {
00236 if (node->value.element.attrs[i].name)
00237 free(node->value.element.attrs[i].name);
00238 if (node->value.element.attrs[i].value)
00239 free(node->value.element.attrs[i].value);
00240 }
00241
00242 free(node->value.element.attrs);
00243 }
00244 break;
00245 case MXML_INTEGER :
00246
00247 break;
00248 case MXML_OPAQUE :
00249 if (node->value.opaque)
00250 free(node->value.opaque);
00251 break;
00252 case MXML_REAL :
00253
00254 break;
00255 case MXML_TEXT :
00256 if (node->value.text.string)
00257 free(node->value.text.string);
00258 break;
00259 case MXML_CUSTOM :
00260 if (node->value.custom.data &&
00261 node->value.custom.destroy)
00262 (*(node->value.custom.destroy))(node->value.custom.data);
00263 break;
00264 }
00265
00266
00267
00268
00269
00270 free(node);
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 mxml_node_t *
00284 mxmlNewCustom(mxml_node_t *parent,
00285 void *data,
00286 void (*destroy)(void *))
00287
00288 {
00289 mxml_node_t *node;
00290
00291
00292 #ifdef DEBUG
00293 fprintf(stderr, "mxmlNewCustom(parent=%p, data=%p, destroy=%p)\n", parent,
00294 data, destroy);
00295 #endif
00296
00297
00298
00299
00300
00301 if ((node = mxml_new(parent, MXML_CUSTOM)) != NULL)
00302 {
00303 node->value.custom.data = data;
00304 node->value.custom.destroy = destroy;
00305 }
00306
00307 return (node);
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 mxml_node_t *
00320 mxmlNewElement(mxml_node_t *parent,
00321 const char *name)
00322 {
00323 mxml_node_t *node;
00324
00325
00326 #ifdef DEBUG
00327 fprintf(stderr, "mxmlNewElement(parent=%p, name=\"%s\")\n", parent,
00328 name ? name : "(null)");
00329 #endif
00330
00331
00332
00333
00334
00335 if (!name)
00336 return (NULL);
00337
00338
00339
00340
00341
00342 if ((node = mxml_new(parent, MXML_ELEMENT)) != NULL)
00343 node->value.element.name = strdup(name);
00344
00345 return (node);
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 mxml_node_t *
00358 mxmlNewInteger(mxml_node_t *parent,
00359 int integer)
00360 {
00361 mxml_node_t *node;
00362
00363
00364 #ifdef DEBUG
00365 fprintf(stderr, "mxmlNewInteger(parent=%p, integer=%d)\n", parent, integer);
00366 #endif
00367
00368
00369
00370
00371
00372 if ((node = mxml_new(parent, MXML_INTEGER)) != NULL)
00373 node->value.integer = integer;
00374
00375 return (node);
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 mxml_node_t *
00389 mxmlNewOpaque(mxml_node_t *parent,
00390 const char *opaque)
00391 {
00392 mxml_node_t *node;
00393
00394
00395 #ifdef DEBUG
00396 fprintf(stderr, "mxmlNewOpaque(parent=%p, opaque=\"%s\")\n", parent,
00397 opaque ? opaque : "(null)");
00398 #endif
00399
00400
00401
00402
00403
00404 if (!opaque)
00405 return (NULL);
00406
00407
00408
00409
00410
00411 if ((node = mxml_new(parent, MXML_OPAQUE)) != NULL)
00412 node->value.opaque = strdup(opaque);
00413
00414 return (node);
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 mxml_node_t *
00427 mxmlNewReal(mxml_node_t *parent,
00428 double real)
00429 {
00430 mxml_node_t *node;
00431
00432
00433 #ifdef DEBUG
00434 fprintf(stderr, "mxmlNewReal(parent=%p, real=%g)\n", parent, real);
00435 #endif
00436
00437
00438
00439
00440
00441 if ((node = mxml_new(parent, MXML_REAL)) != NULL)
00442 node->value.real = real;
00443
00444 return (node);
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458 mxml_node_t *
00459 mxmlNewText(mxml_node_t *parent,
00460 int whitespace,
00461 const char *string)
00462 {
00463 mxml_node_t *node;
00464
00465
00466 #ifdef DEBUG
00467 fprintf(stderr, "mxmlNewText(parent=%p, whitespace=%d, string=\"%s\")\n",
00468 parent, whitespace, string ? string : "(null)");
00469 #endif
00470
00471
00472
00473
00474
00475 if (!string)
00476 return (NULL);
00477
00478
00479
00480
00481
00482 if ((node = mxml_new(parent, MXML_TEXT)) != NULL)
00483 {
00484 node->value.text.whitespace = whitespace;
00485 node->value.text.string = strdup(string);
00486 }
00487
00488 return (node);
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 mxml_node_t *
00503 mxmlNewTextf(mxml_node_t *parent,
00504 int whitespace,
00505 const char *format,
00506 ...)
00507 {
00508 mxml_node_t *node;
00509 va_list ap;
00510
00511
00512 #ifdef DEBUG
00513 fprintf(stderr, "mxmlNewTextf(parent=%p, whitespace=%d, format=\"%s\", ...)\n",
00514 parent, whitespace, format ? format : "(null)");
00515 #endif
00516
00517
00518
00519
00520
00521 if (!format)
00522 return (NULL);
00523
00524
00525
00526
00527
00528 if ((node = mxml_new(parent, MXML_TEXT)) != NULL)
00529 {
00530 va_start(ap, format);
00531
00532 node->value.text.whitespace = whitespace;
00533 node->value.text.string = mxml_strdupf(format, ap);
00534
00535 va_end(ap);
00536 }
00537
00538 return (node);
00539 }
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549 void
00550 mxmlRemove(mxml_node_t *node)
00551 {
00552 #ifdef DEBUG
00553 fprintf(stderr, "mxmlRemove(node=%p)\n", node);
00554 #endif
00555
00556
00557
00558
00559
00560 if (!node || !node->parent)
00561 return;
00562
00563
00564
00565
00566
00567 #if DEBUG > 1
00568 fprintf(stderr, " BEFORE: node->parent=%p\n", node->parent);
00569 if (node->parent)
00570 {
00571 fprintf(stderr, " BEFORE: node->parent->child=%p\n", node->parent->child);
00572 fprintf(stderr, " BEFORE: node->parent->last_child=%p\n", node->parent->last_child);
00573 }
00574 fprintf(stderr, " BEFORE: node->child=%p\n", node->child);
00575 fprintf(stderr, " BEFORE: node->last_child=%p\n", node->last_child);
00576 fprintf(stderr, " BEFORE: node->prev=%p\n", node->prev);
00577 fprintf(stderr, " BEFORE: node->next=%p\n", node->next);
00578 #endif
00579
00580 if (node->prev)
00581 node->prev->next = node->next;
00582 else
00583 node->parent->child = node->next;
00584
00585 if (node->next)
00586 node->next->prev = node->prev;
00587 else
00588 node->parent->last_child = node->prev;
00589
00590 node->parent = NULL;
00591 node->prev = NULL;
00592 node->next = NULL;
00593
00594 #if DEBUG > 1
00595 fprintf(stderr, " AFTER: node->parent=%p\n", node->parent);
00596 if (node->parent)
00597 {
00598 fprintf(stderr, " AFTER: node->parent->child=%p\n", node->parent->child);
00599 fprintf(stderr, " AFTER: node->parent->last_child=%p\n", node->parent->last_child);
00600 }
00601 fprintf(stderr, " AFTER: node->child=%p\n", node->child);
00602 fprintf(stderr, " AFTER: node->last_child=%p\n", node->last_child);
00603 fprintf(stderr, " AFTER: node->prev=%p\n", node->prev);
00604 fprintf(stderr, " AFTER: node->next=%p\n", node->next);
00605 #endif
00606 }
00607
00608
00609
00610
00611
00612
00613 static mxml_node_t *
00614 mxml_new(mxml_node_t *parent,
00615 mxml_type_t type)
00616 {
00617 mxml_node_t *node;
00618
00619
00620 #if DEBUG > 1
00621 fprintf(stderr, "mxml_new(parent=%p, type=%d)\n", parent, type);
00622 #endif
00623
00624
00625
00626
00627
00628 if ((node = calloc(1, sizeof(mxml_node_t))) == NULL)
00629 {
00630 #if DEBUG > 1
00631 fputs(" returning NULL\n", stderr);
00632 #endif
00633
00634 return (NULL);
00635 }
00636
00637 #if DEBUG > 1
00638 fprintf(stderr, " returning %p\n", node);
00639 #endif
00640
00641
00642
00643
00644
00645 node->type = type;
00646
00647
00648
00649
00650
00651 if (parent)
00652 mxmlAdd(parent, MXML_ADD_AFTER, MXML_ADD_TO_PARENT, node);
00653
00654
00655
00656
00657
00658 return (node);
00659 }
00660
00661
00662
00663
00664