diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2007-07-31 12:01:58 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2007-07-31 12:01:58 +0000 |
commit | 8fd4a3e82d354fa3b8efbd8c99015ae56db69fb5 (patch) | |
tree | 8958d01c4081f16f6fecce0262f9b0ac759ba52e /linkedlist.c | |
parent | 45d05a7a5b73de744991e9b6a813d7cc2911d07a (diff) | |
download | rsyslog-8fd4a3e82d354fa3b8efbd8c99015ae56db69fb5.tar.gz rsyslog-8fd4a3e82d354fa3b8efbd8c99015ae56db69fb5.tar.xz rsyslog-8fd4a3e82d354fa3b8efbd8c99015ae56db69fb5.zip |
added some more functions to linkedList class
Diffstat (limited to 'linkedlist.c')
-rw-r--r-- | linkedlist.c | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/linkedlist.c b/linkedlist.c index d8a98286..1238ea08 100644 --- a/linkedlist.c +++ b/linkedlist.c @@ -41,7 +41,7 @@ /* Initialize an existing linkedList_t structure */ -rsRetVal llInit(linkedList_t *pThis, rsRetVal (*pEltDestructor)(void*)) +rsRetVal llInit(linkedList_t *pThis, rsRetVal (*pEltDestructor)(void*, void*)) { assert(pThis != NULL); assert(pEltDestructor != NULL); @@ -74,7 +74,7 @@ rsRetVal llDestroy(linkedList_t *pThis) /* we ignore errors during destruction, as we need to try * finish the linked list in any case. */ - pThis->pEltDestruct(pEltPrev->pData); + pThis->pEltDestruct(pEltPrev->pData, pEltPrev->pKey); free(pEltPrev); } @@ -114,6 +114,81 @@ rsRetVal llGetNextElt(linkedList_t *pThis, linkedListCookie_t *ppElt, void **ppU return iRet; } + +/* construct a new llElt_t + */ +static rsRetVal llEltConstruct(llElt_t **ppThis, void *pKey, void *pData) +{ + DEFiRet; + llElt_t *pThis; + + assert(ppThis != NULL); + + if((pThis = (llElt_t*) calloc(1, sizeof(llElt_t))) == NULL) { + ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); + } + + pThis->pKey = pKey; + pThis->pData = pData; + +finalize_it: + *ppThis = pThis; + return iRet; +} + + +/* append a user element to the end of the linked list. This includes setting a key. If no + * key is desired, simply pass in a NULL pointer for it. + */ +rsRetVal llAppend(linkedList_t *pThis, void *pKey, void *pData) +{ + llElt_t *pElt; + DEFiRet; + + CHKiRet(llEltConstruct(&pElt, pKey, pData)); + + pThis->iNumElts++; /* one more */ + if(pThis->pLast == NULL) { + pThis->pRoot = pElt; + } else { + pThis->pLast->pNext = pElt; + } + pThis->pLast = pElt; + +finalize_it: + return iRet; +} + + +/* find a user element based on the provided key + */ +rsRetVal llFind(linkedList_t *pThis, void *pKey, void **ppData) +{ + DEFiRet; + llElt_t *pElt; + int bFound = 0; + + assert(pThis != NULL); + assert(pKey != NULL); + assert(ppData != NULL); + + pElt = pThis->pRoot; + while(pElt != NULL && bFound == 0) { + if(pThis->cmpOp(pKey, pElt->pKey) == 0) + bFound = 1; + else + pElt = pElt->pNext; + } + + if(bFound == 1) { + *ppData = pElt->pData; + } else { + iRet = RS_RET_NOT_FOUND; + } + + return iRet; +} + /* * vi:set ai: */ |