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