struct _xml_sort_hdl {
XML * sort;
XML * elem;
};
int _xml_sort_comparison (const void * a, const void * b);
XMLAPI XML * xml_sort (XML * list, XML * sort)
{
int i;
XML * child;
struct _xml_sort_hdl * array;
ELEMENTLIST * elist;
/* Count the children. */
i=0; child = xml_firstelem (list);
while (child) {
i++;
child = xml_nextelem (child);
}
if (i < 2) return list;
/* Build the array. */
array = (struct _xml_sort_hdl *) malloc (i * sizeof (struct _xml_sort_hdl));
i=0; child = xml_firstelem (list);
while (child) {
array[i].sort = sort;
array[i].elem = child;
i++;
child = xml_nextelem (child);
}
/* Sort the array. */
qsort (array, i, sizeof (struct _xml_sort_hdl), _xml_sort_comparison);
/* Rearrange the children, being very slick about it. */
i = 0; elist = list->children;
while (elist) {
if (elist->element->name) {
elist->element = array[i].elem;
i++;
}
elist = elist->next;
}
free ((void *) array);
return list;
}
|
int _xml_sort_comparison (const void * a, const void * b)
{
XML * sort;
int res;
int ia, ib;
struct _xml_sort_hdl * _a = (struct _xml_sort_hdl *) a;
struct _xml_sort_hdl * _b = (struct _xml_sort_hdl *) b;
if (a == b) return 0;
sort = _a->sort;
while (sort) {
if (!strcmp (xml_attrval (sort, "op"), "num")) {
ia = xml_attrvalnum (_a->elem, xml_attrval (sort, "field"));
ib = xml_attrvalnum (_b->elem, xml_attrval (sort, "field"));
if (ia < ib) res = -1;
if (ia > ib) res = 1;
} else {
res = strcmp (xml_attrval (_a->elem, xml_attrval (sort, "field")), xml_attrval (_b->elem, xml_attrval (sort, "field")));
}
if (!strcmp (xml_attrval (sort, "dir"), "desc")) res = - res;
if (res) return res;
sort = xml_firstelem (sort);
}
return 0;
}
|
| This code and documentation are released under the terms of the GNU license. They are copyright (c) 2000-2003, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. This presentation was created using LPML. |