|
|
@ -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. |
|
|
|
* 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 */ |
|
|
|
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... |
|
|
|
* Range check input... |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
if (!ind || (!ind->attr && value)) |
|
|
|
if (!ind || (!ind->attr && value)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
puts(" returning NULL..."); |
|
|
|
|
|
|
|
printf(" ind->attr=\"%s\"\n", ind->attr ? ind->attr : "(null)"); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
return (NULL); |
|
|
|
return (NULL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* If both element and value are NULL, just enumerate the nodes in the |
|
|
|
* 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) |
|
|
|
if (!ind->num_nodes) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
puts(" returning NULL..."); |
|
|
|
|
|
|
|
puts(" no nodes!"); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
return (NULL); |
|
|
|
return (NULL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* If cur_node == 0, then find the first matching node... |
|
|
|
* 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; |
|
|
|
first = 0; |
|
|
|
last = ind->num_nodes - 1; |
|
|
|
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; |
|
|
|
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) |
|
|
|
if ((diff = index_find(ind, element, value, ind->nodes[current])) == 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Found a match, move back to find the first... |
|
|
|
* Found a match, move back to find the first... |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
puts(" match!"); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
while (current > 0 && |
|
|
|
while (current > 0 && |
|
|
|
!index_find(ind, element, value, ind->nodes[current - 1])) |
|
|
|
!index_find(ind, element, value, ind->nodes[current - 1])) |
|
|
|
current --; |
|
|
|
current --; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
printf(" returning first match=%d\n", current); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Return the first match and save the index to the next... |
|
|
|
* Return the first match and save the index to the next... |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -183,32 +218,44 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */ |
|
|
|
last = current; |
|
|
|
last = current; |
|
|
|
else |
|
|
|
else |
|
|
|
first = current; |
|
|
|
first = current; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
printf(" diff=%d\n", diff); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* If we get this far, then we found exactly 0 or 1 matches... |
|
|
|
* If we get this far, then we found exactly 0 or 1 matches... |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
current = (first + last) / 2; |
|
|
|
for (current = first; current <= last; current ++) |
|
|
|
ind->cur_node = ind->num_nodes; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!index_find(ind, element, value, ind->nodes[current])) |
|
|
|
if (!index_find(ind, element, value, ind->nodes[current])) |
|
|
|
{ |
|
|
|
{ |
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Found exactly one match... |
|
|
|
* 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]); |
|
|
|
return (ind->nodes[current]); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* No matches... |
|
|
|
* No matches... |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ind->cur_node = ind->num_nodes; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
puts(" returning NULL..."); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
return (NULL); |
|
|
|
return (NULL); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else if (ind->cur_node < ind->num_nodes && |
|
|
|
else if (ind->cur_node < ind->num_nodes && |
|
|
|
!index_find(ind, element, value, ind->nodes[ind->cur_node])) |
|
|
|
!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... |
|
|
|
* Return the next matching node... |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
printf(" returning next match %d...\n", ind->cur_node); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
return (ind->nodes[ind->cur_node ++]); |
|
|
|
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; |
|
|
|
ind->cur_node = ind->num_nodes; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
puts(" returning NULL..."); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
return (NULL); |
|
|
|
return (NULL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -253,6 +308,11 @@ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */ |
|
|
|
* Range check input... |
|
|
|
* 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) |
|
|
|
if (!node) |
|
|
|
return (NULL); |
|
|
|
return (NULL); |
|
|
|
|
|
|
|
|
|
|
@ -260,12 +320,22 @@ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */ |
|
|
|
* Create a new index... |
|
|
|
* 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); |
|
|
|
return (NULL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (attr) |
|
|
|
|
|
|
|
ind->attr = strdup(attr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!element && !attr) |
|
|
|
|
|
|
|
current = node; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND); |
|
|
|
|
|
|
|
|
|
|
|
for (current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND); |
|
|
|
while (current) |
|
|
|
current; |
|
|
|
|
|
|
|
current = mxmlFindElement(current, node, element, attr, NULL, MXML_DESCEND)) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ind->num_nodes >= ind->alloc_nodes) |
|
|
|
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; |
|
|
|
ind->nodes[ind->num_nodes ++] = current; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current = mxmlFindElement(current, node, element, attr, NULL, MXML_DESCEND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Sort nodes based upon the search criteria... |
|
|
|
* 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) |
|
|
|
if (ind->num_nodes > 1) |
|
|
|
index_sort(ind, 0, 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... |
|
|
|
* 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 */ |
|
|
|
mxml_node_t * /* O - First node or NULL if there is none */ |
|
|
|
mxmlIndexReset(mxml_index_t *ind) /* I - Index to reset */ |
|
|
|
mxmlIndexReset(mxml_index_t *ind) /* I - Index to reset */ |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
|
|
|
printf("mxmlIndexReset(ind=%p)\n", ind); |
|
|
|
|
|
|
|
#endif /* DEBUG */ |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Range check input... |
|
|
|
* Range check input... |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -483,8 +621,11 @@ index_sort(mxml_index_t *ind, /* I - Index to sort */ |
|
|
|
* pivot node... |
|
|
|
* pivot node... |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (index_compare(ind, pivot, ind->nodes[tempr]) > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
ind->nodes[left] = ind->nodes[tempr]; |
|
|
|
ind->nodes[left] = ind->nodes[tempr]; |
|
|
|
ind->nodes[tempr] = pivot; |
|
|
|
ind->nodes[tempr] = pivot; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Recursively sort the left and right partitions as needed... |
|
|
|
* 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 $". |
|
|
|
*/ |
|
|
|
*/ |
|
|
|