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");
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);
/* list.h -- list headers */
+#ifndef LIST_H
+#define LIST_H
+
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
int list_del_current(t_list *list);
int list_destroy(t_list *list);
int list_search_data(t_list *list,void *data,int first_bytes);
+int list_add_immediate_ptr(t_list *list,void *ptr);
+int list_add_immediate(t_list *list,void *data,int data_len);
int list_add_element(t_list *list,void *data,int data_len);
int list_count(t_list *list);
+#endif