X-Git-Url: https://hackdaworld.org/gitweb/?p=my-code%2Fapi.git;a=blobdiff_plain;f=list%2Flist.c;fp=list%2Flist.c;h=64a282a14e1f33dfd505fdaec215f5668bb8701a;hp=221b2fb01fe0212348a6f62e6cfc5982721663f2;hb=40946b581d43a81ef427115bc284a4240c389e8f;hpb=a21023e93104fb6e96b57afa23ab4628c8e5e055 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);