From: hackbard Date: Thu, 28 Oct 2004 12:43:44 +0000 (+0000) Subject: completed minimal list api X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8458ffac23aab55ba5316d28dd803dd34297181;p=my-code%2Fapi.git completed minimal list api --- diff --git a/list/list.c b/list/list.c index 64a282a..895cf26 100644 --- a/list/list.c +++ b/list/list.c @@ -8,35 +8,52 @@ int list_init(t_list *list,int outfd) { - dprintf(outfd,"[list] list initilaization ...\n"); + dprintf(outfd,"[list] list initialization ...\n"); list->outfd=outfd; list->start=NULL; - list->current=NULL; + list_reset(list); return L_SUCCESS; } -int list_del_next(t_list *list,t_list_element *element) { +int list_shutdown(t_list *list) { - t_list_element *tmp; + if(list->start!=NULL) list_destroy(list); + + dprintf(list->outfd,"[list] shutdown\n"); + + return L_SUCCESS; +} + +int list_next(t_list *list) { + + list->current=list->current->next; + + return L_SUCCESS; +} + +int list_reset(t_list *list) { + + list->current=list->start; + + return L_SUCCESS; +} + +int list_del_element(t_list *list,t_list_element *element) { if(element==NULL) { dprintf(list->outfd,"[list] empty element\n"); return L_INVALID_ELEMENT; } - if(element->next!=NULL) { - dprintf(list->outfd,"[list] destroying element number %d\n" - tmp=element->next->next; - if(element->next->data!=NULL) free(element->next->data); - free(element->next); - element->next=tmp; - } - else { - dprintf(list->outfd,"[list] no next element\n"); - return L_NO_NEXT_ELEMENT; - } + if(element->next!=NULL) element->next->prev=element->prev; + if(element->prev!=NULL) element->prev->next=element->next; + if(element==list->start) list->start=element->next; + list_reset(list); + if(element->data!=NULL) free(element->data); + free(element); + dprintf(list->outfd,"[list] element deleted\n"); return L_SUCCESS; } @@ -48,72 +65,91 @@ int list_destroy(t_list *list) { return L_EMPTY_LIST; } - list->current=list->start; + list_reset(list); - while(list->current->next!=NULL) - list_del_next(list,list->current); + while(list->current!=NULL) list_del_element(list,list->current); return L_SUCCESS; } -int list_del_nr(t_list *list,int nr) { - - if(list->start==NULL) { - dprintf(list->outfd,"[list] empty list\n"); - return L_EMPTY_LIST; - } - - if(list->start->number==nr) { +int list_search_data(t_list *list,void *data,int first_bytes) { - } + list_reset(list); - list->current=&(list->start); - - while(list->current->next!=NULL) { - if(list->current->next->number==nr) { - list_del_next(list,list->current); - return L_SUCCESS; - } - list->current=list->current->next; + while(list->current!=NULL) { + if(first_bytes<=list->current->data_len) + if(!(memcmp(list->current->data,data,first_bytes))) return L_SUCCESS; + list_next(list); } return L_NO_SUCH_ELEMENT; } -int list_search_data(t_list *list,void *data,int data_len) { +int list_add_element(t_list *list,void *data,int data_len) { - t_element *help; + list_reset(list); - help=&(list->start); - - while(help->next!=NULL) { - if(help->next->data_len==data_len) { - if(!(memcmp(help->next->data,data,data_len)) { - list->current=help->next; - return L_SUCCESS; + while(list->current!=NULL) { + if(data_len==list->current->data_len) { + if(!memcmp(list->current->data,data,data_len)) { + dprintf(list->outfd,"[list] element already in list\n"); + return L_ELEMENT_IN_LIST; } } - list->current=list->current->next; - } - - return L_NO_SUCH_ELEMENT; -} - -int list_search_nr(t_list *list,int nr) { + if(list->current->next==NULL) { + list->current->next=(t_list_element *)malloc(sizeof(t_list_element)); + if(list->current->next==NULL) { + dprintf(list->outfd,"[list] unable to allocate element memory\n"); + return L_E_MEM; + } + list->current->next->prev=list->current; + list->current->next->next=NULL; + if((list->current->next->data=malloc(data_len))==NULL) { + dprintf(list->outfd,"[list] unable to allocate data memory\n"); + return L_E_MEM; + } + memcpy(list->current->next->data,data,data_len); + list->current->next->data_len=data_len; - list->current=&(list->start); + dprintf(list->outfd,"[list] element added\n"); - while(list->current->next!=NULL) { - if(list->current->next->number==nr) { - list->current=list->current->next; return L_SUCCESS; } - list->current=list->current->next; + else list_next(list); } - return L_NO_SUCH_ELEMENT; + /* if this is the first element */ + list->start=(t_list_element *)malloc(sizeof(t_list_element)); + if(list->start==NULL) { + dprintf(list->outfd,"[list] unable to allocate list element memory\n"); + return L_E_MEM; + } + if((list->start->data=malloc(data_len))==NULL) { + dprintf(list->outfd,"[list] unable to allocate data memory\n"); + return L_E_MEM; + } + memcpy(list->start->data,data,data_len); + list->start->data_len=data_len; + list->start->next=NULL; + list->start->prev=NULL; + + dprintf(list->outfd,"[list] added initial element\n"); + + return L_SUCCESS; } -int list_append_element(t_list *list,t_list_element *element) { +int list_count(t_list *list) { - list->current=&(list->start); + int count; + + count=0; + + list_reset(list); + + while(list->current!=NULL) { + list_next(list); + ++count; + } + + return count; +} diff --git a/list/list.h b/list/list.h index 467e751..5355a16 100644 --- a/list/list.h +++ b/list/list.h @@ -1,6 +1,9 @@ /* list.h -- list headers */ +#define _GNU_SOURCE #include +#include +#include #define L_SUCCESS 1 #define L_ERROR -1 @@ -8,19 +11,30 @@ #define L_NO_SUCH_ELEMENT -3 #define L_INVALID_ELEMENT -4 #define L_EMPTY_LIST -5 +#define L_ELEMENT_IN_LIST -6 +#define L_E_MEM -7 typedef struct s_list_element { + struct s_list_element *prev; struct s_list_element *next; void *data; int data_len; - int number; } t_list_element; typedef struct s_list { + int outfd; t_list_element *start; t_list_element *current; } t_list; /* function prototypes */ -int list_init(t_list *list); +int list_init(t_list *list,int outfd); +int list_shutdown(t_list *list); +int list_reset(t_list *list); +int list_next(t_list *list); +int list_del_element(t_list *list,t_list_element *element); +int list_destroy(t_list *list); +int list_search_data(t_list *list,void *data,int first_bytes); +int list_add_element(t_list *list,void *data,int data_len); +int list_count(t_list *list);