mxml-index.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Index support code for Mini-XML, a small XML file parsing library.
  3. *
  4. * https://www.msweet.org/mxml
  5. *
  6. * Copyright © 2003-2021 by Michael R Sweet.
  7. *
  8. * Licensed under Apache License v2.0. See the file "LICENSE" for more
  9. * information.
  10. */
  11. /*
  12. * Include necessary headers...
  13. */
  14. #include "config.h"
  15. #include "mxml-private.h"
  16. /*
  17. * Sort functions...
  18. */
  19. static int index_compare(mxml_index_t *ind, mxml_node_t *first,
  20. mxml_node_t *second);
  21. static int index_find(mxml_index_t *ind, const char *element,
  22. const char *value, mxml_node_t *node);
  23. static void index_sort(mxml_index_t *ind, int left, int right);
  24. /*
  25. * 'mxmlIndexDelete()' - Delete an index.
  26. */
  27. void
  28. mxmlIndexDelete(mxml_index_t *ind) /* I - Index to delete */
  29. {
  30. /*
  31. * Range check input..
  32. */
  33. if (!ind)
  34. return;
  35. /*
  36. * Free memory...
  37. */
  38. free(ind->attr);
  39. free(ind->nodes);
  40. free(ind);
  41. }
  42. /*
  43. * 'mxmlIndexEnum()' - Return the next node in the index.
  44. *
  45. * You should call @link mxmlIndexReset@ prior to using this function to get
  46. * the first node in the index. Nodes are returned in the sorted order of the
  47. * index.
  48. */
  49. mxml_node_t * /* O - Next node or @code NULL@ if there is none */
  50. mxmlIndexEnum(mxml_index_t *ind) /* I - Index to enumerate */
  51. {
  52. /*
  53. * Range check input...
  54. */
  55. if (!ind)
  56. return (NULL);
  57. /*
  58. * Return the next node...
  59. */
  60. if (ind->cur_node < ind->num_nodes)
  61. return (ind->nodes[ind->cur_node ++]);
  62. else
  63. return (NULL);
  64. }
  65. /*
  66. * 'mxmlIndexFind()' - Find the next matching node.
  67. *
  68. * You should call @link mxmlIndexReset@ prior to using this function for
  69. * the first time with a particular set of "element" and "value"
  70. * strings. Passing @code NULL@ for both "element" and "value" is equivalent
  71. * to calling @link mxmlIndexEnum@.
  72. */
  73. mxml_node_t * /* O - Node or @code NULL@ if none found */
  74. mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
  75. const char *element, /* I - Element name to find, if any */
  76. const char *value) /* I - Attribute value, if any */
  77. {
  78. int diff, /* Difference between names */
  79. current, /* Current entity in search */
  80. first, /* First entity in search */
  81. last; /* Last entity in search */
  82. #ifdef DEBUG
  83. printf("mxmlIndexFind(ind=%p, element=\"%s\", value=\"%s\")\n",
  84. ind, element ? element : "(null)", value ? value : "(null)");
  85. #endif /* DEBUG */
  86. /*
  87. * Range check input...
  88. */
  89. if (!ind || (!ind->attr && value))
  90. {
  91. #ifdef DEBUG
  92. puts(" returning NULL...");
  93. if (ind)
  94. printf(" ind->attr=\"%s\"\n", ind->attr ? ind->attr : "(null)");
  95. #endif /* DEBUG */
  96. return (NULL);
  97. }
  98. /*
  99. * If both element and value are NULL, just enumerate the nodes in the
  100. * index...
  101. */
  102. if (!element && !value)
  103. return (mxmlIndexEnum(ind));
  104. /*
  105. * If there are no nodes in the index, return NULL...
  106. */
  107. if (!ind->num_nodes)
  108. {
  109. #ifdef DEBUG
  110. puts(" returning NULL...");
  111. puts(" no nodes!");
  112. #endif /* DEBUG */
  113. return (NULL);
  114. }
  115. /*
  116. * If cur_node == 0, then find the first matching node...
  117. */
  118. if (ind->cur_node == 0)
  119. {
  120. /*
  121. * Find the first node using a modified binary search algorithm...
  122. */
  123. first = 0;
  124. last = ind->num_nodes - 1;
  125. #ifdef DEBUG
  126. printf(" find first time, num_nodes=%d...\n", ind->num_nodes);
  127. #endif /* DEBUG */
  128. while ((last - first) > 1)
  129. {
  130. current = (first + last) / 2;
  131. #ifdef DEBUG
  132. printf(" first=%d, last=%d, current=%d\n", first, last, current);
  133. #endif /* DEBUG */
  134. if ((diff = index_find(ind, element, value, ind->nodes[current])) == 0)
  135. {
  136. /*
  137. * Found a match, move back to find the first...
  138. */
  139. #ifdef DEBUG
  140. puts(" match!");
  141. #endif /* DEBUG */
  142. while (current > 0 &&
  143. !index_find(ind, element, value, ind->nodes[current - 1]))
  144. current --;
  145. #ifdef DEBUG
  146. printf(" returning first match=%d\n", current);
  147. #endif /* DEBUG */
  148. /*
  149. * Return the first match and save the index to the next...
  150. */
  151. ind->cur_node = current + 1;
  152. return (ind->nodes[current]);
  153. }
  154. else if (diff < 0)
  155. last = current;
  156. else
  157. first = current;
  158. #ifdef DEBUG
  159. printf(" diff=%d\n", diff);
  160. #endif /* DEBUG */
  161. }
  162. /*
  163. * If we get this far, then we found exactly 0 or 1 matches...
  164. */
  165. for (current = first; current <= last; current ++)
  166. if (!index_find(ind, element, value, ind->nodes[current]))
  167. {
  168. /*
  169. * Found exactly one (or possibly two) match...
  170. */
  171. #ifdef DEBUG
  172. printf(" returning only match %d...\n", current);
  173. #endif /* DEBUG */
  174. ind->cur_node = current + 1;
  175. return (ind->nodes[current]);
  176. }
  177. /*
  178. * No matches...
  179. */
  180. ind->cur_node = ind->num_nodes;
  181. #ifdef DEBUG
  182. puts(" returning NULL...");
  183. #endif /* DEBUG */
  184. return (NULL);
  185. }
  186. else if (ind->cur_node < ind->num_nodes &&
  187. !index_find(ind, element, value, ind->nodes[ind->cur_node]))
  188. {
  189. /*
  190. * Return the next matching node...
  191. */
  192. #ifdef DEBUG
  193. printf(" returning next match %d...\n", ind->cur_node);
  194. #endif /* DEBUG */
  195. return (ind->nodes[ind->cur_node ++]);
  196. }
  197. /*
  198. * If we get this far, then we have no matches...
  199. */
  200. ind->cur_node = ind->num_nodes;
  201. #ifdef DEBUG
  202. puts(" returning NULL...");
  203. #endif /* DEBUG */
  204. return (NULL);
  205. }
  206. /*
  207. * 'mxmlIndexGetCount()' - Get the number of nodes in an index.
  208. *
  209. * @since Mini-XML 2.7@
  210. */
  211. int /* I - Number of nodes in index */
  212. mxmlIndexGetCount(mxml_index_t *ind) /* I - Index of nodes */
  213. {
  214. /*
  215. * Range check input...
  216. */
  217. if (!ind)
  218. return (0);
  219. /*
  220. * Return the number of nodes in the index...
  221. */
  222. return (ind->num_nodes);
  223. }
  224. /*
  225. * 'mxmlIndexNew()' - Create a new index.
  226. *
  227. * The index will contain all nodes that contain the named element and/or
  228. * attribute. If both "element" and "attr" are @code NULL@, then the index will
  229. * contain a sorted list of the elements in the node tree. Nodes are
  230. * sorted by element name and optionally by attribute value if the "attr"
  231. * argument is not NULL.
  232. */
  233. mxml_index_t * /* O - New index */
  234. mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */
  235. const char *element, /* I - Element to index or @code NULL@ for all */
  236. const char *attr) /* I - Attribute to index or @code NULL@ for none */
  237. {
  238. mxml_index_t *ind; /* New index */
  239. mxml_node_t *current, /* Current node in index */
  240. **temp; /* Temporary node pointer array */
  241. /*
  242. * Range check input...
  243. */
  244. #ifdef DEBUG
  245. printf("mxmlIndexNew(node=%p, element=\"%s\", attr=\"%s\")\n",
  246. node, element ? element : "(null)", attr ? attr : "(null)");
  247. #endif /* DEBUG */
  248. if (!node)
  249. return (NULL);
  250. /*
  251. * Create a new index...
  252. */
  253. if ((ind = calloc(1, sizeof(mxml_index_t))) == NULL)
  254. {
  255. mxml_error("Unable to allocate memory for index.");
  256. return (NULL);
  257. }
  258. if (attr)
  259. {
  260. if ((ind->attr = strdup(attr)) == NULL)
  261. {
  262. mxml_error("Unable to allocate memory for index attribute.");
  263. free(ind);
  264. return (NULL);
  265. }
  266. }
  267. if (!element && !attr)
  268. current = node;
  269. else
  270. current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND);
  271. while (current)
  272. {
  273. if (ind->num_nodes >= ind->alloc_nodes)
  274. {
  275. if (!ind->alloc_nodes)
  276. temp = malloc(64 * sizeof(mxml_node_t *));
  277. else
  278. temp = realloc(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *));
  279. if (!temp)
  280. {
  281. /*
  282. * Unable to allocate memory for the index, so abort...
  283. */
  284. mxml_error("Unable to allocate memory for index nodes.");
  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 @link mxmlIndexEnum@ or
  357. * @link mxmlIndexFind@ for the first time.
  358. */
  359. mxml_node_t * /* O - First node or @code 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 @code NULL@ */
  417. const char *value, /* I - Attribute value or @code 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. }