From: hackbard Date: Thu, 21 Oct 2004 23:55:37 +0000 (+0000) Subject: new list stuff (still in work) X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=40946b581d43a81ef427115bc284a4240c389e8f;p=my-code%2Fapi.git new list stuff (still in work) --- diff --git a/list/list.c b/list/list.c index 221b2fb..64a282a 100644 --- a/list/list.c +++ b/list/list.c @@ -6,23 +6,114 @@ #include "list.h" -int list_init(t_list *list) { +int list_init(t_list *list,int outfd) { - list-> - list->current=&(list->start); + dprintf(outfd,"[list] list initilaization ...\n"); + + list->outfd=outfd; + list->start=NULL; + list->current=NULL; + + return L_SUCCESS; +} + +int list_del_next(t_list *list,t_list_element *element) { + + t_list_element *tmp; + + 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; + } + + return L_SUCCESS; +} + +int list_destroy(t_list *list) { + + if(list->start==NULL) { + dprintf(list->outfd,"[list] empty list\n"); + return L_EMPTY_LIST; + } + + list->current=list->start; + + while(list->current->next!=NULL) + list_del_next(list,list->current); return L_SUCCESS; } -int list_add_element(t_list *list,int nr,void *data,int len) { +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) { - if(list->method&C_NUMBER) { - while(list->current->next!=NULL) { - if(list->current->number==nr) return L_EXISTS; - else list->current=list->current->next; + } + + 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; + } + + return L_NO_SUCH_ELEMENT; +} + +int list_search_data(t_list *list,void *data,int data_len) { + + t_element *help; + + 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; + } } - if(list->current->number==nr) return L_EXISTS; + list->current=list->current->next; + } + + return L_NO_SUCH_ELEMENT; +} - /* new element */ - if((list->current->next=(t_list *)malloc(sizeof(t_list)))==NULL) { - fprintf(stderr,"[list] +int list_search_nr(t_list *list,int nr) { + + list->current=&(list->start); + + while(list->current->next!=NULL) { + if(list->current->next->number==nr) { + list->current=list->current->next; + return L_SUCCESS; + } + list->current=list->current->next; + } + + return L_NO_SUCH_ELEMENT; +} + +int list_append_element(t_list *list,t_list_element *element) { + + list->current=&(list->start); diff --git a/list/list.h b/list/list.h index b415f90..467e751 100644 --- a/list/list.h +++ b/list/list.h @@ -4,18 +4,23 @@ #define L_SUCCESS 1 #define L_ERROR -1 - -#define C_NUMBER (1<<0) -#define C_DATA (1<<1) +#define L_NO_NEXT_ELEMENT -2 +#define L_NO_SUCH_ELEMENT -3 +#define L_INVALID_ELEMENT -4 +#define L_EMPTY_LIST -5 typedef struct s_list_element { - struct s_list_element *next; - void *data; - int number; + struct s_list_element *next; + void *data; + int data_len; + int number; } t_list_element; typedef struct s_list { - t_list_element start; - unsigned char method; - t_list_element *current; + t_list_element *start; + t_list_element *current; } t_list; + +/* function prototypes */ +int list_init(t_list *list); +