blob: 10814390d3a067a0c8a46247b4bbd747ac39c86e [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/*
Jon Medhurst96b56152014-10-30 18:01:15 +00002 * "$Id: mxml-index.c 451 2014-01-04 21:50:06Z msweet $"
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
4 * Index support code for Mini-XML, a small XML-like file parsing library.
5 *
Jon Medhurst96b56152014-10-30 18:01:15 +00006 * Copyright 2003-2014 by Michael R Sweet.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01007 *
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:
13 *
Jon Medhurst96b56152014-10-30 18:01:15 +000014 * http://www.msweet.org/projects.php/Mini-XML
Jon Medhurstaaf37a32013-06-11 12:10:56 +010015 */
16
17/*
18 * Include necessary headers...
19 */
20
21#include "config.h"
22#include "mxml.h"
23
24
25/*
26 * Sort functions...
27 */
28
29static int index_compare(mxml_index_t *ind, mxml_node_t *first,
30 mxml_node_t *second);
31static int index_find(mxml_index_t *ind, const char *element,
32 const char *value, mxml_node_t *node);
33static void index_sort(mxml_index_t *ind, int left, int right);
34
35
36/*
37 * 'mxmlIndexDelete()' - Delete an index.
38 */
39
40void
41mxmlIndexDelete(mxml_index_t *ind) /* I - Index to delete */
42{
43 /*
44 * Range check input..
45 */
46
47 if (!ind)
48 return;
49
50 /*
51 * Free memory...
52 */
53
54 if (ind->attr)
55 free(ind->attr);
56
57 if (ind->alloc_nodes)
58 free(ind->nodes);
59
60 free(ind);
61}
62
63
64/*
65 * 'mxmlIndexEnum()' - Return the next node in the index.
66 *
67 * Nodes are returned in the sorted order of the index.
68 */
69
70mxml_node_t * /* O - Next node or NULL if there is none */
71mxmlIndexEnum(mxml_index_t *ind) /* I - Index to enumerate */
72{
73 /*
74 * Range check input...
75 */
76
77 if (!ind)
78 return (NULL);
79
80 /*
81 * Return the next node...
82 */
83
84 if (ind->cur_node < ind->num_nodes)
85 return (ind->nodes[ind->cur_node ++]);
86 else
87 return (NULL);
88}
89
90
91/*
92 * 'mxmlIndexFind()' - Find the next matching node.
93 *
94 * You should call mxmlIndexReset() prior to using this function for
95 * the first time with a particular set of "element" and "value"
96 * strings. Passing NULL for both "element" and "value" is equivalent
97 * to calling mxmlIndexEnum().
98 */
99
100mxml_node_t * /* O - Node or NULL if none found */
101mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
102 const char *element, /* I - Element name to find, if any */
103 const char *value) /* I - Attribute value, if any */
104{
105 int diff, /* Difference between names */
106 current, /* Current entity in search */
107 first, /* First entity in search */
108 last; /* Last entity in search */
109
110
111#ifdef DEBUG
112 printf("mxmlIndexFind(ind=%p, element=\"%s\", value=\"%s\")\n",
113 ind, element ? element : "(null)", value ? value : "(null)");
114#endif /* DEBUG */
115
116 /*
117 * Range check input...
118 */
119
120 if (!ind || (!ind->attr && value))
121 {
122#ifdef DEBUG
123 puts(" returning NULL...");
124 printf(" ind->attr=\"%s\"\n", ind->attr ? ind->attr : "(null)");
125#endif /* DEBUG */
126
127 return (NULL);
128 }
129
130 /*
131 * If both element and value are NULL, just enumerate the nodes in the
132 * index...
133 */
134
135 if (!element && !value)
136 return (mxmlIndexEnum(ind));
137
138 /*
139 * If there are no nodes in the index, return NULL...
140 */
141
142 if (!ind->num_nodes)
143 {
144#ifdef DEBUG
145 puts(" returning NULL...");
146 puts(" no nodes!");
147#endif /* DEBUG */
148
149 return (NULL);
150 }
151
152 /*
153 * If cur_node == 0, then find the first matching node...
154 */
155
156 if (ind->cur_node == 0)
157 {
158 /*
159 * Find the first node using a modified binary search algorithm...
160 */
161
162 first = 0;
163 last = ind->num_nodes - 1;
164
165#ifdef DEBUG
166 printf(" find first time, num_nodes=%d...\n", ind->num_nodes);
167#endif /* DEBUG */
168
169 while ((last - first) > 1)
170 {
171 current = (first + last) / 2;
172
173#ifdef DEBUG
174 printf(" first=%d, last=%d, current=%d\n", first, last, current);
175#endif /* DEBUG */
176
177 if ((diff = index_find(ind, element, value, ind->nodes[current])) == 0)
178 {
179 /*
180 * Found a match, move back to find the first...
181 */
182
183#ifdef DEBUG
184 puts(" match!");
185#endif /* DEBUG */
186
187 while (current > 0 &&
188 !index_find(ind, element, value, ind->nodes[current - 1]))
189 current --;
190
191#ifdef DEBUG
192 printf(" returning first match=%d\n", current);
193#endif /* DEBUG */
194
195 /*
196 * Return the first match and save the index to the next...
197 */
198
199 ind->cur_node = current + 1;
200
201 return (ind->nodes[current]);
202 }
203 else if (diff < 0)
204 last = current;
205 else
206 first = current;
207
208#ifdef DEBUG
209 printf(" diff=%d\n", diff);
210#endif /* DEBUG */
211 }
212
213 /*
214 * If we get this far, then we found exactly 0 or 1 matches...
215 */
216
217 for (current = first; current <= last; current ++)
218 if (!index_find(ind, element, value, ind->nodes[current]))
219 {
220 /*
221 * Found exactly one (or possibly two) match...
222 */
223
224#ifdef DEBUG
225 printf(" returning only match %d...\n", current);
226#endif /* DEBUG */
227
228 ind->cur_node = current + 1;
229
230 return (ind->nodes[current]);
231 }
232
233 /*
234 * No matches...
235 */
236
237 ind->cur_node = ind->num_nodes;
238
239#ifdef DEBUG
240 puts(" returning NULL...");
241#endif /* DEBUG */
242
243 return (NULL);
244 }
245 else if (ind->cur_node < ind->num_nodes &&
246 !index_find(ind, element, value, ind->nodes[ind->cur_node]))
247 {
248 /*
249 * Return the next matching node...
250 */
251
252#ifdef DEBUG
253 printf(" returning next match %d...\n", ind->cur_node);
254#endif /* DEBUG */
255
256 return (ind->nodes[ind->cur_node ++]);
257 }
258
259 /*
260 * If we get this far, then we have no matches...
261 */
262
263 ind->cur_node = ind->num_nodes;
264
265#ifdef DEBUG
266 puts(" returning NULL...");
267#endif /* DEBUG */
268
269 return (NULL);
270}
271
272
273/*
274 * 'mxmlIndexGetCount()' - Get the number of nodes in an index.
275 *
276 * @since Mini-XML 2.7@
277 */
278
279int /* I - Number of nodes in index */
280mxmlIndexGetCount(mxml_index_t *ind) /* I - Index of nodes */
281{
282 /*
283 * Range check input...
284 */
285
286 if (!ind)
287 return (0);
288
289 /*
290 * Return the number of nodes in the index...
291 */
292
293 return (ind->num_nodes);
294}
295
296
297/*
298 * 'mxmlIndexNew()' - Create a new index.
299 *
300 * The index will contain all nodes that contain the named element and/or
301 * attribute. If both "element" and "attr" are NULL, then the index will
302 * contain a sorted list of the elements in the node tree. Nodes are
303 * sorted by element name and optionally by attribute value if the "attr"
304 * argument is not NULL.
305 */
306
307mxml_index_t * /* O - New index */
308mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */
309 const char *element, /* I - Element to index or NULL for all */
310 const char *attr) /* I - Attribute to index or NULL for none */
311{
312 mxml_index_t *ind; /* New index */
313 mxml_node_t *current, /* Current node in index */
314 **temp; /* Temporary node pointer array */
315
316
317 /*
318 * Range check input...
319 */
320
321#ifdef DEBUG
322 printf("mxmlIndexNew(node=%p, element=\"%s\", attr=\"%s\")\n",
323 node, element ? element : "(null)", attr ? attr : "(null)");
324#endif /* DEBUG */
325
326 if (!node)
327 return (NULL);
328
329 /*
330 * Create a new index...
331 */
332
333 if ((ind = calloc(1, sizeof(mxml_index_t))) == NULL)
334 {
335 mxml_error("Unable to allocate %d bytes for index - %s",
336 sizeof(mxml_index_t), strerror(errno));
337 return (NULL);
338 }
339
340 if (attr)
341 ind->attr = strdup(attr);
342
343 if (!element && !attr)
344 current = node;
345 else
346 current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND);
347
348 while (current)
349 {
350 if (ind->num_nodes >= ind->alloc_nodes)
351 {
352 if (!ind->alloc_nodes)
353 temp = malloc(64 * sizeof(mxml_node_t *));
354 else
355 temp = realloc(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *));
356
357 if (!temp)
358 {
359 /*
360 * Unable to allocate memory for the index, so abort...
361 */
362
363 mxml_error("Unable to allocate %d bytes for index: %s",
364 (ind->alloc_nodes + 64) * sizeof(mxml_node_t *),
365 strerror(errno));
366
367 mxmlIndexDelete(ind);
368 return (NULL);
369 }
370
371 ind->nodes = temp;
372 ind->alloc_nodes += 64;
373 }
374
375 ind->nodes[ind->num_nodes ++] = current;
376
377 current = mxmlFindElement(current, node, element, attr, NULL, MXML_DESCEND);
378 }
379
380 /*
381 * Sort nodes based upon the search criteria...
382 */
383
384#ifdef DEBUG
385 {
386 int i; /* Looping var */
387
388
389 printf("%d node(s) in index.\n\n", ind->num_nodes);
390
391 if (attr)
392 {
393 printf("Node Address Element %s\n", attr);
394 puts("-------- -------- -------------- ------------------------------");
395
396 for (i = 0; i < ind->num_nodes; i ++)
397 printf("%8d %-8p %-14.14s %s\n", i, ind->nodes[i],
398 ind->nodes[i]->value.element.name,
399 mxmlElementGetAttr(ind->nodes[i], attr));
400 }
401 else
402 {
403 puts("Node Address Element");
404 puts("-------- -------- --------------");
405
406 for (i = 0; i < ind->num_nodes; i ++)
407 printf("%8d %-8p %s\n", i, ind->nodes[i],
408 ind->nodes[i]->value.element.name);
409 }
410
411 putchar('\n');
412 }
413#endif /* DEBUG */
414
415 if (ind->num_nodes > 1)
416 index_sort(ind, 0, ind->num_nodes - 1);
417
418#ifdef DEBUG
419 {
420 int i; /* Looping var */
421
422
423 puts("After sorting:\n");
424
425 if (attr)
426 {
427 printf("Node Address Element %s\n", attr);
428 puts("-------- -------- -------------- ------------------------------");
429
430 for (i = 0; i < ind->num_nodes; i ++)
431 printf("%8d %-8p %-14.14s %s\n", i, ind->nodes[i],
432 ind->nodes[i]->value.element.name,
433 mxmlElementGetAttr(ind->nodes[i], attr));
434 }
435 else
436 {
437 puts("Node Address Element");
438 puts("-------- -------- --------------");
439
440 for (i = 0; i < ind->num_nodes; i ++)
441 printf("%8d %-8p %s\n", i, ind->nodes[i],
442 ind->nodes[i]->value.element.name);
443 }
444
445 putchar('\n');
446 }
447#endif /* DEBUG */
448
449 /*
450 * Return the new index...
451 */
452
453 return (ind);
454}
455
456
457/*
458 * 'mxmlIndexReset()' - Reset the enumeration/find pointer in the index and
459 * return the first node in the index.
460 *
461 * This function should be called prior to using mxmlIndexEnum() or
462 * mxmlIndexFind() for the first time.
463 */
464
465mxml_node_t * /* O - First node or NULL if there is none */
466mxmlIndexReset(mxml_index_t *ind) /* I - Index to reset */
467{
468#ifdef DEBUG
469 printf("mxmlIndexReset(ind=%p)\n", ind);
470#endif /* DEBUG */
471
472 /*
473 * Range check input...
474 */
475
476 if (!ind)
477 return (NULL);
478
479 /*
480 * Set the index to the first element...
481 */
482
483 ind->cur_node = 0;
484
485 /*
486 * Return the first node...
487 */
488
489 if (ind->num_nodes)
490 return (ind->nodes[0]);
491 else
492 return (NULL);
493}
494
495
496/*
497 * 'index_compare()' - Compare two nodes.
498 */
499
500static int /* O - Result of comparison */
501index_compare(mxml_index_t *ind, /* I - Index */
502 mxml_node_t *first, /* I - First node */
503 mxml_node_t *second) /* I - Second node */
504{
505 int diff; /* Difference */
506
507
508 /*
509 * Check the element name...
510 */
511
512 if ((diff = strcmp(first->value.element.name,
513 second->value.element.name)) != 0)
514 return (diff);
515
516 /*
517 * Check the attribute value...
518 */
519
520 if (ind->attr)
521 {
522 if ((diff = strcmp(mxmlElementGetAttr(first, ind->attr),
523 mxmlElementGetAttr(second, ind->attr))) != 0)
524 return (diff);
525 }
526
527 /*
528 * No difference, return 0...
529 */
530
531 return (0);
532}
533
534
535/*
536 * 'index_find()' - Compare a node with index values.
537 */
538
539static int /* O - Result of comparison */
540index_find(mxml_index_t *ind, /* I - Index */
541 const char *element, /* I - Element name or NULL */
542 const char *value, /* I - Attribute value or NULL */
543 mxml_node_t *node) /* I - Node */
544{
545 int diff; /* Difference */
546
547
548 /*
549 * Check the element name...
550 */
551
552 if (element)
553 {
554 if ((diff = strcmp(element, node->value.element.name)) != 0)
555 return (diff);
556 }
557
558 /*
559 * Check the attribute value...
560 */
561
562 if (value)
563 {
564 if ((diff = strcmp(value, mxmlElementGetAttr(node, ind->attr))) != 0)
565 return (diff);
566 }
567
568 /*
569 * No difference, return 0...
570 */
571
572 return (0);
573}
574
575
576/*
577 * 'index_sort()' - Sort the nodes in the index...
578 *
579 * This function implements the classic quicksort algorithm...
580 */
581
582static void
583index_sort(mxml_index_t *ind, /* I - Index to sort */
584 int left, /* I - Left node in partition */
585 int right) /* I - Right node in partition */
586{
587 mxml_node_t *pivot, /* Pivot node */
588 *temp; /* Swap node */
589 int templ, /* Temporary left node */
590 tempr; /* Temporary right node */
591
592
593 /*
594 * Loop until we have sorted all the way to the right...
595 */
596
597 do
598 {
599 /*
600 * Sort the pivot in the current partition...
601 */
602
603 pivot = ind->nodes[left];
604
605 for (templ = left, tempr = right; templ < tempr;)
606 {
607 /*
608 * Move left while left node <= pivot node...
609 */
610
611 while ((templ < right) &&
612 index_compare(ind, ind->nodes[templ], pivot) <= 0)
613 templ ++;
614
615 /*
616 * Move right while right node > pivot node...
617 */
618
619 while ((tempr > left) &&
620 index_compare(ind, ind->nodes[tempr], pivot) > 0)
621 tempr --;
622
623 /*
624 * Swap nodes if needed...
625 */
626
627 if (templ < tempr)
628 {
629 temp = ind->nodes[templ];
630 ind->nodes[templ] = ind->nodes[tempr];
631 ind->nodes[tempr] = temp;
632 }
633 }
634
635 /*
636 * When we get here, the right (tempr) node is the new position for the
637 * pivot node...
638 */
639
640 if (index_compare(ind, pivot, ind->nodes[tempr]) > 0)
641 {
642 ind->nodes[left] = ind->nodes[tempr];
643 ind->nodes[tempr] = pivot;
644 }
645
646 /*
647 * Recursively sort the left partition as needed...
648 */
649
650 if (left < (tempr - 1))
651 index_sort(ind, left, tempr - 1);
652 }
653 while (right > (left = tempr + 1));
654}
655
656
657/*
Jon Medhurst96b56152014-10-30 18:01:15 +0000658 * End of "$Id: mxml-index.c 451 2014-01-04 21:50:06Z msweet $".
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100659 */