This source file includes following definitions.
- xmlLinkDeallocator
- xmlLinkCompare
- xmlListLowerSearch
- xmlListHigherSearch
- xmlListLinkSearch
- xmlListLinkReverseSearch
- xmlListCreate
- xmlListSearch
- xmlListReverseSearch
- xmlListInsert
- xmlListAppend
- xmlListDelete
- xmlListRemoveFirst
- xmlListRemoveLast
- xmlListRemoveAll
- xmlListClear
- xmlListEmpty
- xmlListFront
- xmlListEnd
- xmlListSize
- xmlListPopFront
- xmlListPopBack
- xmlListPushFront
- xmlListPushBack
- xmlLinkGetData
- xmlListReverse
- xmlListSort
- xmlListWalk
- xmlListReverseWalk
- xmlListMerge
- xmlListDup
- xmlListCopy
#define IN_LIBXML
#include "libxml.h"
#include <stdlib.h>
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/list.h>
#include <libxml/globals.h>
struct _xmlLink
{
struct _xmlLink *next;
struct _xmlLink *prev;
void *data;
};
struct _xmlList
{
xmlLinkPtr sentinel;
void (*linkDeallocator)(xmlLinkPtr );
int (*linkCompare)(const void *, const void*);
};
static void
xmlLinkDeallocator(xmlListPtr l, xmlLinkPtr lk)
{
(lk->prev)->next = lk->next;
(lk->next)->prev = lk->prev;
if(l->linkDeallocator)
l->linkDeallocator(lk);
xmlFree(lk);
}
static int
xmlLinkCompare(const void *data0, const void *data1)
{
if (data0 < data1)
return (-1);
else if (data0 == data1)
return (0);
return (1);
}
static xmlLinkPtr
xmlListLowerSearch(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(NULL);
for(lk = l->sentinel->next;lk != l->sentinel && l->linkCompare(lk->data, data) <0 ;lk = lk->next);
return lk;
}
static xmlLinkPtr
xmlListHigherSearch(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(NULL);
for(lk = l->sentinel->prev;lk != l->sentinel && l->linkCompare(lk->data, data) >0 ;lk = lk->prev);
return lk;
}
static xmlLinkPtr
xmlListLinkSearch(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(NULL);
lk = xmlListLowerSearch(l, data);
if (lk == l->sentinel)
return NULL;
else {
if (l->linkCompare(lk->data, data) ==0)
return lk;
return NULL;
}
}
static xmlLinkPtr
xmlListLinkReverseSearch(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(NULL);
lk = xmlListHigherSearch(l, data);
if (lk == l->sentinel)
return NULL;
else {
if (l->linkCompare(lk->data, data) ==0)
return lk;
return NULL;
}
}
xmlListPtr
xmlListCreate(xmlListDeallocator deallocator, xmlListDataCompare compare)
{
xmlListPtr l;
if (NULL == (l = (xmlListPtr )xmlMalloc( sizeof(xmlList)))) {
xmlGenericError(xmlGenericErrorContext,
"Cannot initialize memory for list");
return (NULL);
}
memset(l, 0, sizeof(xmlList));
if (NULL ==(l->sentinel = (xmlLinkPtr )xmlMalloc(sizeof(xmlLink)))) {
xmlGenericError(xmlGenericErrorContext,
"Cannot initialize memory for sentinel");
xmlFree(l);
return (NULL);
}
l->sentinel->next = l->sentinel;
l->sentinel->prev = l->sentinel;
l->sentinel->data = NULL;
if (deallocator != NULL)
l->linkDeallocator = deallocator;
if (compare != NULL)
l->linkCompare = compare;
else
l->linkCompare = xmlLinkCompare;
return l;
}
void *
xmlListSearch(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(NULL);
lk = xmlListLinkSearch(l, data);
if (lk)
return (lk->data);
return NULL;
}
void *
xmlListReverseSearch(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(NULL);
lk = xmlListLinkReverseSearch(l, data);
if (lk)
return (lk->data);
return NULL;
}
int
xmlListInsert(xmlListPtr l, void *data)
{
xmlLinkPtr lkPlace, lkNew;
if (l == NULL)
return(1);
lkPlace = xmlListLowerSearch(l, data);
lkNew = (xmlLinkPtr) xmlMalloc(sizeof(xmlLink));
if (lkNew == NULL) {
xmlGenericError(xmlGenericErrorContext,
"Cannot initialize memory for new link");
return (1);
}
lkNew->data = data;
lkPlace = lkPlace->prev;
lkNew->next = lkPlace->next;
(lkPlace->next)->prev = lkNew;
lkPlace->next = lkNew;
lkNew->prev = lkPlace;
return 0;
}
int xmlListAppend(xmlListPtr l, void *data)
{
xmlLinkPtr lkPlace, lkNew;
if (l == NULL)
return(1);
lkPlace = xmlListHigherSearch(l, data);
lkNew = (xmlLinkPtr) xmlMalloc(sizeof(xmlLink));
if (lkNew == NULL) {
xmlGenericError(xmlGenericErrorContext,
"Cannot initialize memory for new link");
return (1);
}
lkNew->data = data;
lkNew->next = lkPlace->next;
(lkPlace->next)->prev = lkNew;
lkPlace->next = lkNew;
lkNew->prev = lkPlace;
return 0;
}
void xmlListDelete(xmlListPtr l)
{
if (l == NULL)
return;
xmlListClear(l);
xmlFree(l->sentinel);
xmlFree(l);
}
int
xmlListRemoveFirst(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(0);
lk = xmlListLinkSearch(l, data);
if (lk != NULL) {
xmlLinkDeallocator(l, lk);
return 1;
}
return 0;
}
int
xmlListRemoveLast(xmlListPtr l, void *data)
{
xmlLinkPtr lk;
if (l == NULL)
return(0);
lk = xmlListLinkReverseSearch(l, data);
if (lk != NULL) {
xmlLinkDeallocator(l, lk);
return 1;
}
return 0;
}
int
xmlListRemoveAll(xmlListPtr l, void *data)
{
int count=0;
if (l == NULL)
return(0);
while(xmlListRemoveFirst(l, data))
count++;
return count;
}
void
xmlListClear(xmlListPtr l)
{
xmlLinkPtr lk;
if (l == NULL)
return;
lk = l->sentinel->next;
while(lk != l->sentinel) {
xmlLinkPtr next = lk->next;
xmlLinkDeallocator(l, lk);
lk = next;
}
}
int
xmlListEmpty(xmlListPtr l)
{
if (l == NULL)
return(-1);
return (l->sentinel->next == l->sentinel);
}
xmlLinkPtr
xmlListFront(xmlListPtr l)
{
if (l == NULL)
return(NULL);
return (l->sentinel->next);
}
xmlLinkPtr
xmlListEnd(xmlListPtr l)
{
if (l == NULL)
return(NULL);
return (l->sentinel->prev);
}
int
xmlListSize(xmlListPtr l)
{
xmlLinkPtr lk;
int count=0;
if (l == NULL)
return(-1);
for(lk = l->sentinel->next; lk != l->sentinel; lk = lk->next, count++);
return count;
}
void
xmlListPopFront(xmlListPtr l)
{
if(!xmlListEmpty(l))
xmlLinkDeallocator(l, l->sentinel->next);
}
void
xmlListPopBack(xmlListPtr l)
{
if(!xmlListEmpty(l))
xmlLinkDeallocator(l, l->sentinel->prev);
}
int
xmlListPushFront(xmlListPtr l, void *data)
{
xmlLinkPtr lkPlace, lkNew;
if (l == NULL)
return(0);
lkPlace = l->sentinel;
lkNew = (xmlLinkPtr) xmlMalloc(sizeof(xmlLink));
if (lkNew == NULL) {
xmlGenericError(xmlGenericErrorContext,
"Cannot initialize memory for new link");
return (0);
}
lkNew->data = data;
lkNew->next = lkPlace->next;
(lkPlace->next)->prev = lkNew;
lkPlace->next = lkNew;
lkNew->prev = lkPlace;
return 1;
}
int
xmlListPushBack(xmlListPtr l, void *data)
{
xmlLinkPtr lkPlace, lkNew;
if (l == NULL)
return(0);
lkPlace = l->sentinel->prev;
if (NULL ==(lkNew = (xmlLinkPtr )xmlMalloc(sizeof(xmlLink)))) {
xmlGenericError(xmlGenericErrorContext,
"Cannot initialize memory for new link");
return (0);
}
lkNew->data = data;
lkNew->next = lkPlace->next;
(lkPlace->next)->prev = lkNew;
lkPlace->next = lkNew;
lkNew->prev = lkPlace;
return 1;
}
void *
xmlLinkGetData(xmlLinkPtr lk)
{
if (lk == NULL)
return(NULL);
return lk->data;
}
void
xmlListReverse(xmlListPtr l)
{
xmlLinkPtr lk;
xmlLinkPtr lkPrev;
if (l == NULL)
return;
lkPrev = l->sentinel;
for (lk = l->sentinel->next; lk != l->sentinel; lk = lk->next) {
lkPrev->next = lkPrev->prev;
lkPrev->prev = lk;
lkPrev = lk;
}
lkPrev->next = lkPrev->prev;
lkPrev->prev = lk;
}
void
xmlListSort(xmlListPtr l)
{
xmlListPtr lTemp;
if (l == NULL)
return;
if(xmlListEmpty(l))
return;
if (NULL ==(lTemp = xmlListDup(l)))
return;
xmlListClear(l);
xmlListMerge(l, lTemp);
xmlListDelete(lTemp);
return;
}
void
xmlListWalk(xmlListPtr l, xmlListWalker walker, const void *user) {
xmlLinkPtr lk;
if ((l == NULL) || (walker == NULL))
return;
for(lk = l->sentinel->next; lk != l->sentinel; lk = lk->next) {
if((walker(lk->data, user)) == 0)
break;
}
}
void
xmlListReverseWalk(xmlListPtr l, xmlListWalker walker, const void *user) {
xmlLinkPtr lk;
if ((l == NULL) || (walker == NULL))
return;
for(lk = l->sentinel->prev; lk != l->sentinel; lk = lk->prev) {
if((walker(lk->data, user)) == 0)
break;
}
}
void
xmlListMerge(xmlListPtr l1, xmlListPtr l2)
{
xmlListCopy(l1, l2);
xmlListClear(l2);
}
xmlListPtr
xmlListDup(const xmlListPtr old)
{
xmlListPtr cur;
if (old == NULL)
return(NULL);
if (NULL ==(cur = xmlListCreate(NULL, old->linkCompare)))
return (NULL);
if (0 != xmlListCopy(cur, old))
return NULL;
return cur;
}
int
xmlListCopy(xmlListPtr cur, const xmlListPtr old)
{
xmlLinkPtr lk;
if ((old == NULL) || (cur == NULL))
return(1);
for(lk = old->sentinel->next; lk != old->sentinel; lk = lk->next) {
if (0 !=xmlListInsert(cur, lk->data)) {
xmlListDelete(cur);
return (1);
}
}
return (0);
}
#define bottom_list
#include "elfgcchack.h"