mxml-index.c 13 KB

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