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 | |
parent | 45d05a7a5b73de744991e9b6a813d7cc2911d07a (diff) | |
download | rsyslog-8fd4a3e82d354fa3b8efbd8c99015ae56db69fb5.tar.gz rsyslog-8fd4a3e82d354fa3b8efbd8c99015ae56db69fb5.tar.xz rsyslog-8fd4a3e82d354fa3b8efbd8c99015ae56db69fb5.zip |
added some more functions to linkedList class
-rw-r--r-- | linkedlist.c | 79 | ||||
-rw-r--r-- | linkedlist.h | 8 | ||||
-rw-r--r-- | rsyslog.h | 2 |
3 files changed, 84 insertions, 5 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: */ diff --git a/linkedlist.h b/linkedlist.h index ab44262f..046c7ba9 100644 --- a/linkedlist.h +++ b/linkedlist.h @@ -28,6 +28,7 @@ */ struct llElt_s { /* config file sysline parse entry */ struct llElt_s *pNext; + void *pKey; /* key for this element */ void *pData; /* user-supplied data pointer */ }; typedef struct llElt_s llElt_t; @@ -39,8 +40,8 @@ typedef struct llElt_s llElt_t; */ struct linkedList_s { /* config file sysline parse entry */ int iNumElts; /* number of elements in list */ - rsRetVal (*pEltDestruct)(void*); /* destructor for user pointer in llElt_t's */ - rsRetVal (*cmpOp)(void*, void*); /* pointer to compare operation function */ + rsRetVal (*pEltDestruct)(void*pData, void*pKey); /* destructor for user pointer in llElt_t's */ + int (*cmpOp)(void*, void*); /* pointer to key compare operation function, retval like strcmp */ void *pKey; /* the list key (searchable, if set) */ llElt_t *pRoot; /* list root */ llElt_t *pLast; /* list tail */ @@ -50,8 +51,9 @@ typedef struct linkedList_s linkedList_t; typedef llElt_t* linkedListCookie_t; /* this type avoids exposing internals and keeps us flexible */ /* prototypes */ -rsRetVal llInit(linkedList_t *pThis, rsRetVal (*pEltDestructor)(void*)); +rsRetVal llInit(linkedList_t *pThis, rsRetVal (*pEltDestructor)(void*, void*)); rsRetVal llDestroy(linkedList_t *pThis); rsRetVal llGetNextElt(linkedList_t *pThis, linkedListCookie_t *ppElt, void **ppUsr); +rsRetVal llAppend(linkedList_t *pThis, void *pKey, void *pData); #endif /* #ifndef LINKEDLIST_H_INCLUDED */ @@ -65,6 +65,8 @@ enum rsRetVal_ /** return value. All methods return this if not specified oth RS_RET_VAL_OUT_OF_RANGE = -2012, /**< value out of range */ RS_RET_FOPEN_FAILURE = -2013, /**< failure during fopen, for example file not found - see errno */ RS_RET_END_OF_LINKEDLIST = -2014, /**< end of linked list, not an error, but a status */ + RS_RET_IS_EQUAL = -2015, /**< outcome of a compare is equal */ + RS_RET_IS_NOT_EQUAL = -2016, /**< outcome of a compare is not equal */ RS_RET_OK = 0 /**< operation successful */ }; typedef enum rsRetVal_ rsRetVal; /**< friendly type for global return value */ |