X-Git-Url: https://hackdaworld.org/gitweb/?p=my-code%2Fapi.git;a=blobdiff_plain;f=list%2Flist.c;h=79b41d2ff9cf4288c03c6e6feb48b04f62a8bde2;hp=64a104a1cd3f7e0eb651e60cfc5782e07b898960;hb=5b7483c4678684b08837d5377646d1bd396e3cd7;hpb=dbb174b5acb9d83fba8fdbd29f4a4426f152bd7e diff --git a/list/list.c b/list/list.c index 64a104a..79b41d2 100644 --- a/list/list.c +++ b/list/list.c @@ -28,7 +28,8 @@ int list_shutdown(t_list *list) { int list_next(t_list *list) { - if(list->current->next==NULL) return L_NO_NEXT_ELEMENT; + if(list->current->next==NULL) + return L_NO_NEXT_ELEMENT; list->current=list->current->next; @@ -53,7 +54,7 @@ int list_del_element(t_list *list,t_list_element *element) { 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); + if((element->data!=NULL)&&(element->data_len)) free(element->data); free(element); dprintf(list->outfd,"[list] element deleted\n"); @@ -75,6 +76,7 @@ int list_destroy(t_list *list) { list_reset(list); while(list->current!=NULL) list_del_element(list,list->current); + list->start=NULL; return L_SUCCESS; } @@ -97,6 +99,81 @@ int list_search_data(t_list *list,void *data,int first_bytes) { return L_NO_SUCH_ELEMENT; } +int list_add_immediate_ptr(t_list *list,void *ptr) { + + /* if this is the first element */ + if(list->start==NULL) { + 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; + } + list->start->data=ptr; + list->start->data_len=0; + list->start->next=NULL; + list->start->prev=NULL; + list->current=list->start; + dprintf(list->outfd,"[list] added initial element\n"); + return L_SUCCESS; + } + + 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; + list->current->next->data=ptr; + list->current->next->data_len=0; + + list->current=list->current->next; + + return L_SUCCESS; + +} + +int list_add_immediate(t_list *list,void *data,int data_len) { + + /* if this is the first element */ + if(list->start==NULL) { + 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; + list->current=list->start; + dprintf(list->outfd,"[list] added initial element\n"); + return L_SUCCESS; + } + + 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->current->next; + + return L_SUCCESS; +} + int list_add_element(t_list *list,void *data,int data_len) { list_reset(list);