[Refdb-cvs] CVS: refdb/src linklist.c,1.9,1.9.2.1 linklist.h,1.7,1.7.2.1
Status: Beta
Brought to you by:
mhoenicka
From: Markus H. <mho...@us...> - 2004-09-26 21:07:17
|
Update of /cvsroot/refdb/refdb/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6964/src Modified Files: Tag: Release_0_9_5_stable linklist.c linklist.h Log Message: added linked list for fixed-size strings: struct lilifstring, append_lilifstring(), get_next_lilifstring(), delete_all_lilifstring() Index: linklist.c =================================================================== RCS file: /cvsroot/refdb/refdb/src/linklist.c,v retrieving revision 1.9 retrieving revision 1.9.2.1 diff -u -U2 -r1.9 -r1.9.2.1 --- linklist.c 2 Sep 2003 00:25:22 -0000 1.9 +++ linklist.c 26 Sep 2004 21:07:05 -0000 1.9.2.1 @@ -847,2 +847,96 @@ return counter; } + +/********************************************************************* + linked list for fixed-size strings. The list keeps the original order + of items +********************************************************************/ + +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + append_lilifstring(): appends a new member into the list. The + implementation is not very efficient but is ok + for a low number of entries + + int append_lilifstring returns 0 if ok, or 1 if an error occurred + if an error occurs + + Lilifstring *ptr_first pointer to sentinel + + char* token ptr to token + + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +int append_lilifstring(Lilifstring *ptr_first, char* token) { + Lilifstring* ptr_new_item; + Lilifstring* ptr_last_item; + + ptr_new_item = (Lilifstring*)malloc(sizeof(Lilifstring)); + + if (!ptr_new_item) { /* malloc failed */ + return 1; + } + + if (token) { + strncpy(ptr_new_item->token, token, LILIFSTRING_SIZE); + (ptr_new_item->token)[LILIFSTRING_SIZE-1] = '\0'; + } + else { + (ptr_new_item->token)[0] = '\0'; + } + + ptr_new_item->ptr_next = NULL; + + ptr_last_item = ptr_first; + + /* this is going to be inefficient for a large number of entries */ + while (ptr_last_item->ptr_next) { + ptr_last_item = ptr_last_item->ptr_next; + } + ptr_last_item->ptr_next = ptr_new_item; + + return 0; +} + +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + get_next_lilifstring(): retrieves next member of the list + + Lilifstring* get_next_lilifstring returns a ptr to the next member or NULL + if we're at the end of the list. Use this fn + to loop over all list entries. In the first + iteration, pass a ptr to the sentinel. In + subsequent iterations, pass the ptr that the + previous iteration returns. + + + Lilifstring *ptr_first pointer to sentinel + + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +Lilifstring* get_next_lilifstring(Lilifstring* ptr_first) { + return ptr_first->ptr_next; +} + +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + delete_all_lilifstring(): deletes all members from the list + + int delete_all_lilifstring returns 0 if ok, or 1 if an error occurred + + Lilifstring *ptr_first pointer to sentinel + + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +int delete_all_lilifstring(Lilifstring *ptr_first) { + Lilifstring* ptr_curr_item; + Lilifstring* ptr_next_item; + + ptr_curr_item = ptr_first->ptr_next; + ptr_first->ptr_next = NULL; + + while (ptr_curr_item) { + ptr_next_item = ptr_curr_item->ptr_next; +/* fprintf(stderr, "freeing %s\n", ptr_curr_item->varname); */ + free(ptr_curr_item); /* free the memory allocated for the list member */ + + ptr_curr_item = ptr_next_item; + } + return 0; +} + + Index: linklist.h =================================================================== RCS file: /cvsroot/refdb/refdb/src/linklist.h,v retrieving revision 1.7 retrieving revision 1.7.2.1 diff -u -U2 -r1.7 -r1.7.2.1 --- linklist.h 2 Sep 2003 00:25:22 -0000 1.7 +++ linklist.h 26 Sep 2004 21:07:05 -0000 1.7.2.1 @@ -20,4 +20,6 @@ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +#define LILIFSTRING_SIZE 256 + /********************************************************************* linked list for file descriptors @@ -125,3 +127,19 @@ int count_lilistring(Lilistring* ptr_first); +/********************************************************************* + linked list for fixed-size strings +********************************************************************/ + +typedef struct lilifstring { + char token[LILIFSTRING_SIZE]; /* ptr to allocated memory with the value of the item */ + struct lilifstring *ptr_next; /* pointer to the next element in the list */ +} Lilifstring; + +int append_lilifstring(Lilifstring *ptr_first, char* token); + +Lilifstring* get_next_lilifstring(Lilifstring* ptr_first); + +int delete_all_lilifstring(Lilifstring *ptr_first); + + |