mirror of
https://github.com/michaelrsweet/mxml.git
synced 2025-02-20 17:35:30 +00:00
Cleanup quicksort code to preserve the original order of nodes when they
are equal. Fix mxmlIndexFind() implementation. Add tests.
This commit is contained in:
parent
56d571d240
commit
934a9aeab3
191
mxml-index.c
191
mxml-index.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* "$Id: mxml-index.c,v 1.3 2004/05/16 13:45:56 mike Exp $"
|
||||
* "$Id: mxml-index.c,v 1.4 2004/05/16 18:25:20 mike Exp $"
|
||||
*
|
||||
* Index support code for Mini-XML, a small XML-like file parsing library.
|
||||
*
|
||||
@ -122,12 +122,24 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
|
||||
last; /* Last entity in search */
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("mxmlIndexFind(ind=%p, element=\"%s\", value=\"%s\")\n",
|
||||
ind, element ? element : "(null)", value ? value : "(null)");
|
||||
#endif /* DEBUG */
|
||||
|
||||
/*
|
||||
* Range check input...
|
||||
*/
|
||||
|
||||
if (!ind || (!ind->attr && value))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
puts(" returning NULL...");
|
||||
printf(" ind->attr=\"%s\"\n", ind->attr ? ind->attr : "(null)");
|
||||
#endif /* DEBUG */
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* If both element and value are NULL, just enumerate the nodes in the
|
||||
@ -142,7 +154,14 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
|
||||
*/
|
||||
|
||||
if (!ind->num_nodes)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
puts(" returning NULL...");
|
||||
puts(" no nodes!");
|
||||
#endif /* DEBUG */
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* If cur_node == 0, then find the first matching node...
|
||||
@ -157,20 +176,36 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
|
||||
first = 0;
|
||||
last = ind->num_nodes - 1;
|
||||
|
||||
while (last > first)
|
||||
#ifdef DEBUG
|
||||
printf(" find first time, num_nodes=%d...\n", ind->num_nodes);
|
||||
#endif /* DEBUG */
|
||||
|
||||
while ((last - first) > 1)
|
||||
{
|
||||
current = (first + last) / 2;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf(" first=%d, last=%d, current=%d\n", first, last, current);
|
||||
#endif /* DEBUG */
|
||||
|
||||
if ((diff = index_find(ind, element, value, ind->nodes[current])) == 0)
|
||||
{
|
||||
/*
|
||||
* Found a match, move back to find the first...
|
||||
*/
|
||||
|
||||
#ifdef DEBUG
|
||||
puts(" match!");
|
||||
#endif /* DEBUG */
|
||||
|
||||
while (current > 0 &&
|
||||
!index_find(ind, element, value, ind->nodes[current - 1]))
|
||||
current --;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf(" returning first match=%d\n", current);
|
||||
#endif /* DEBUG */
|
||||
|
||||
/*
|
||||
* Return the first match and save the index to the next...
|
||||
*/
|
||||
@ -183,31 +218,43 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
|
||||
last = current;
|
||||
else
|
||||
first = current;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf(" diff=%d\n", diff);
|
||||
#endif /* DEBUG */
|
||||
}
|
||||
|
||||
/*
|
||||
* If we get this far, then we found exactly 0 or 1 matches...
|
||||
*/
|
||||
|
||||
current = (first + last) / 2;
|
||||
for (current = first; current <= last; current ++)
|
||||
if (!index_find(ind, element, value, ind->nodes[current]))
|
||||
{
|
||||
/*
|
||||
* Found exactly one (or possibly two) match...
|
||||
*/
|
||||
|
||||
#ifdef DEBUG
|
||||
printf(" returning only match %d...\n", current);
|
||||
#endif /* DEBUG */
|
||||
|
||||
ind->cur_node = current + 1;
|
||||
|
||||
return (ind->nodes[current]);
|
||||
}
|
||||
|
||||
/*
|
||||
* No matches...
|
||||
*/
|
||||
|
||||
ind->cur_node = ind->num_nodes;
|
||||
|
||||
if (!index_find(ind, element, value, ind->nodes[current]))
|
||||
{
|
||||
/*
|
||||
* Found exactly one match...
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
puts(" returning NULL...");
|
||||
#endif /* DEBUG */
|
||||
|
||||
return (ind->nodes[current]);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* No matches...
|
||||
*/
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
else if (ind->cur_node < ind->num_nodes &&
|
||||
!index_find(ind, element, value, ind->nodes[ind->cur_node]))
|
||||
@ -216,6 +263,10 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
|
||||
* Return the next matching node...
|
||||
*/
|
||||
|
||||
#ifdef DEBUG
|
||||
printf(" returning next match %d...\n", ind->cur_node);
|
||||
#endif /* DEBUG */
|
||||
|
||||
return (ind->nodes[ind->cur_node ++]);
|
||||
}
|
||||
|
||||
@ -225,6 +276,10 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
|
||||
|
||||
ind->cur_node = ind->num_nodes;
|
||||
|
||||
#ifdef DEBUG
|
||||
puts(" returning NULL...");
|
||||
#endif /* DEBUG */
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@ -253,6 +308,11 @@ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */
|
||||
* Range check input...
|
||||
*/
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("mxmlIndexNew(node=%p, element=\"%s\", attr=\"%s\")\n",
|
||||
node, element ? element : "(null)", attr ? attr : "(null)");
|
||||
#endif /* DEBUG */
|
||||
|
||||
if (!node)
|
||||
return (NULL);
|
||||
|
||||
@ -260,12 +320,22 @@ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */
|
||||
* Create a new index...
|
||||
*/
|
||||
|
||||
if ((ind = calloc(1, sizeof(mxml_index_t))) != NULL)
|
||||
if ((ind = calloc(1, sizeof(mxml_index_t))) == NULL)
|
||||
{
|
||||
mxml_error("Unable to allocate %d bytes for index - %s",
|
||||
sizeof(mxml_index_t), strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
for (current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND);
|
||||
current;
|
||||
current = mxmlFindElement(current, node, element, attr, NULL, MXML_DESCEND))
|
||||
if (attr)
|
||||
ind->attr = strdup(attr);
|
||||
|
||||
if (!element && !attr)
|
||||
current = node;
|
||||
else
|
||||
current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND);
|
||||
|
||||
while (current)
|
||||
{
|
||||
if (ind->num_nodes >= ind->alloc_nodes)
|
||||
{
|
||||
@ -293,15 +363,79 @@ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */
|
||||
}
|
||||
|
||||
ind->nodes[ind->num_nodes ++] = current;
|
||||
|
||||
current = mxmlFindElement(current, node, element, attr, NULL, MXML_DESCEND);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sort nodes based upon the search criteria...
|
||||
*/
|
||||
|
||||
#ifdef DEBUG
|
||||
{
|
||||
int i; /* Looping var */
|
||||
|
||||
|
||||
printf("%d node(s) in index.\n\n", ind->num_nodes);
|
||||
|
||||
if (attr)
|
||||
{
|
||||
printf("Node Address Element %s\n", attr);
|
||||
puts("-------- -------- -------------- ------------------------------");
|
||||
|
||||
for (i = 0; i < ind->num_nodes; i ++)
|
||||
printf("%8d %-8p %-14.14s %s\n", i, ind->nodes[i],
|
||||
ind->nodes[i]->value.element.name,
|
||||
mxmlElementGetAttr(ind->nodes[i], attr));
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("Node Address Element");
|
||||
puts("-------- -------- --------------");
|
||||
|
||||
for (i = 0; i < ind->num_nodes; i ++)
|
||||
printf("%8d %-8p %s\n", i, ind->nodes[i],
|
||||
ind->nodes[i]->value.element.name);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
#endif /* DEBUG */
|
||||
|
||||
if (ind->num_nodes > 1)
|
||||
index_sort(ind, 0, ind->num_nodes - 1);
|
||||
|
||||
#ifdef DEBUG
|
||||
{
|
||||
int i; /* Looping var */
|
||||
|
||||
|
||||
puts("After sorting:\n");
|
||||
|
||||
if (attr)
|
||||
{
|
||||
printf("Node Address Element %s\n", attr);
|
||||
puts("-------- -------- -------------- ------------------------------");
|
||||
|
||||
for (i = 0; i < ind->num_nodes; i ++)
|
||||
printf("%8d %-8p %-14.14s %s\n", i, ind->nodes[i],
|
||||
ind->nodes[i]->value.element.name,
|
||||
mxmlElementGetAttr(ind->nodes[i], attr));
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("Node Address Element");
|
||||
puts("-------- -------- --------------");
|
||||
|
||||
for (i = 0; i < ind->num_nodes; i ++)
|
||||
printf("%8d %-8p %s\n", i, ind->nodes[i],
|
||||
ind->nodes[i]->value.element.name);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
#endif /* DEBUG */
|
||||
|
||||
/*
|
||||
* Return the new index...
|
||||
*/
|
||||
@ -321,6 +455,10 @@ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */
|
||||
mxml_node_t * /* O - First node or NULL if there is none */
|
||||
mxmlIndexReset(mxml_index_t *ind) /* I - Index to reset */
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("mxmlIndexReset(ind=%p)\n", ind);
|
||||
#endif /* DEBUG */
|
||||
|
||||
/*
|
||||
* Range check input...
|
||||
*/
|
||||
@ -483,8 +621,11 @@ index_sort(mxml_index_t *ind, /* I - Index to sort */
|
||||
* pivot node...
|
||||
*/
|
||||
|
||||
ind->nodes[left] = ind->nodes[tempr];
|
||||
ind->nodes[tempr] = pivot;
|
||||
if (index_compare(ind, pivot, ind->nodes[tempr]) > 0)
|
||||
{
|
||||
ind->nodes[left] = ind->nodes[tempr];
|
||||
ind->nodes[tempr] = pivot;
|
||||
}
|
||||
|
||||
/*
|
||||
* Recursively sort the left and right partitions as needed...
|
||||
@ -499,5 +640,5 @@ index_sort(mxml_index_t *ind, /* I - Index to sort */
|
||||
|
||||
|
||||
/*
|
||||
* End of "$Id: mxml-index.c,v 1.3 2004/05/16 13:45:56 mike Exp $".
|
||||
* End of "$Id: mxml-index.c,v 1.4 2004/05/16 18:25:20 mike Exp $".
|
||||
*/
|
||||
|
133
testmxml.c
133
testmxml.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* "$Id: testmxml.c,v 1.16 2004/05/02 16:04:40 mike Exp $"
|
||||
* "$Id: testmxml.c,v 1.17 2004/05/16 18:25:20 mike Exp $"
|
||||
*
|
||||
* Test program for Mini-XML, a small XML-like file parsing library.
|
||||
*
|
||||
@ -51,6 +51,7 @@ main(int argc, /* I - Number of command-line args */
|
||||
FILE *fp; /* File to read */
|
||||
mxml_node_t *tree, /* XML tree */
|
||||
*node; /* Node which should be in test.xml */
|
||||
mxml_index_t *ind; /* XML index */
|
||||
char buffer[16384]; /* Save string */
|
||||
static const char *types[] = /* Strings for node types */
|
||||
{
|
||||
@ -106,11 +107,11 @@ main(int argc, /* I - Number of command-line args */
|
||||
mxmlNewReal(tree, 123.4f);
|
||||
mxmlNewText(tree, 1, "text");
|
||||
|
||||
mxmlLoadString(tree, "<group>string string string</group>",
|
||||
mxmlLoadString(tree, "<group type='string'>string string string</group>",
|
||||
MXML_NO_CALLBACK);
|
||||
mxmlLoadString(tree, "<group>1 2 3</group>",
|
||||
mxmlLoadString(tree, "<group type='integer'>1 2 3</group>",
|
||||
MXML_INTEGER_CALLBACK);
|
||||
mxmlLoadString(tree, "<group>1.0 2.0 3.0</group>",
|
||||
mxmlLoadString(tree, "<group type='real'>1.0 2.0 3.0</group>",
|
||||
MXML_REAL_CALLBACK);
|
||||
mxmlLoadString(tree, "<group>opaque opaque opaque</group>",
|
||||
MXML_OPAQUE_CALLBACK);
|
||||
@ -240,7 +241,127 @@ main(int argc, /* I - Number of command-line args */
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Test indices...
|
||||
*/
|
||||
|
||||
ind = mxmlIndexNew(tree, NULL, NULL);
|
||||
if (!ind)
|
||||
{
|
||||
fputs("ERROR: Unable to create index of all nodes!\n", stderr);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (ind->num_nodes != 5)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Index of all nodes contains %d "
|
||||
"nodes; expected 5!\n", ind->num_nodes);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexReset(ind);
|
||||
if (!mxmlIndexFind(ind, "group", NULL))
|
||||
{
|
||||
fputs("ERROR: mxmlIndexFind for \"group\" failed!\n", stderr);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexDelete(ind);
|
||||
|
||||
ind = mxmlIndexNew(tree, "group", NULL);
|
||||
if (!ind)
|
||||
{
|
||||
fputs("ERROR: Unable to create index of groups!\n", stderr);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (ind->num_nodes != 4)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Index of groups contains %d "
|
||||
"nodes; expected 4!\n", ind->num_nodes);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexReset(ind);
|
||||
if (!mxmlIndexEnum(ind))
|
||||
{
|
||||
fputs("ERROR: mxmlIndexEnum failed!\n", stderr);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexDelete(ind);
|
||||
|
||||
ind = mxmlIndexNew(tree, NULL, "type");
|
||||
if (!ind)
|
||||
{
|
||||
fputs("ERROR: Unable to create index of type attributes!\n", stderr);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (ind->num_nodes != 3)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Index of type attributes contains %d "
|
||||
"nodes; expected 3!\n", ind->num_nodes);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexReset(ind);
|
||||
if (!mxmlIndexFind(ind, NULL, "string"))
|
||||
{
|
||||
fputs("ERROR: mxmlIndexFind for \"string\" failed!\n", stderr);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexDelete(ind);
|
||||
|
||||
ind = mxmlIndexNew(tree, "group", "type");
|
||||
if (!ind)
|
||||
{
|
||||
fputs("ERROR: Unable to create index of elements and attributes!\n", stderr);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if (ind->num_nodes != 3)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Index of elements and attributes contains %d "
|
||||
"nodes; expected 3!\n", ind->num_nodes);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexReset(ind);
|
||||
if (!mxmlIndexFind(ind, "group", "string"))
|
||||
{
|
||||
fputs("ERROR: mxmlIndexFind for \"string\" failed!\n", stderr);
|
||||
mxmlIndexDelete(ind);
|
||||
mxmlDelete(tree);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mxmlIndexDelete(ind);
|
||||
|
||||
/*
|
||||
* Check the mxmlDelete() works properly...
|
||||
*/
|
||||
|
||||
for (i = 0; i < 8; i ++)
|
||||
{
|
||||
@ -431,5 +552,5 @@ whitespace_cb(mxml_node_t *node, /* I - Element node */
|
||||
|
||||
|
||||
/*
|
||||
* End of "$Id: testmxml.c,v 1.16 2004/05/02 16:04:40 mike Exp $".
|
||||
* End of "$Id: testmxml.c,v 1.17 2004/05/16 18:25:20 mike Exp $".
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user