mxml-index.c 13 KB

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