1 /* list.c -- list management szuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int list_init(t_list *list,int outfd) {
11 dprintf(outfd,"[list] list initialization ...\n");
20 int list_shutdown(t_list *list) {
22 if(list->start!=NULL) list_destroy(list);
24 dprintf(list->outfd,"[list] shutdown\n");
29 int list_next(t_list *list) {
31 list->current=list->current->next;
36 int list_reset(t_list *list) {
38 list->current=list->start;
43 int list_del_element(t_list *list,t_list_element *element) {
46 dprintf(list->outfd,"[list] empty element\n");
47 return L_INVALID_ELEMENT;
50 if(element->next!=NULL) element->next->prev=element->prev;
51 if(element->prev!=NULL) element->prev->next=element->next;
52 if(element==list->start) list->start=element->next;
54 if(element->data!=NULL) free(element->data);
56 dprintf(list->outfd,"[list] element deleted\n");
61 int list_destroy(t_list *list) {
63 if(list->start==NULL) {
64 dprintf(list->outfd,"[list] empty list\n");
70 while(list->current!=NULL) list_del_element(list,list->current);
75 int list_search_data(t_list *list,void *data,int first_bytes) {
79 while(list->current!=NULL) {
80 if(first_bytes<=list->current->data_len)
81 if(!(memcmp(list->current->data,data,first_bytes))) return L_SUCCESS;
85 return L_NO_SUCH_ELEMENT;
88 int list_add_element(t_list *list,void *data,int data_len) {
92 while(list->current!=NULL) {
93 if(data_len==list->current->data_len) {
94 if(!memcmp(list->current->data,data,data_len)) {
95 dprintf(list->outfd,"[list] element already in list\n");
96 return L_ELEMENT_IN_LIST;
99 if(list->current->next==NULL) {
100 list->current->next=(t_list_element *)malloc(sizeof(t_list_element));
101 if(list->current->next==NULL) {
102 dprintf(list->outfd,"[list] unable to allocate element memory\n");
105 list->current->next->prev=list->current;
106 list->current->next->next=NULL;
107 if((list->current->next->data=malloc(data_len))==NULL) {
108 dprintf(list->outfd,"[list] unable to allocate data memory\n");
111 memcpy(list->current->next->data,data,data_len);
112 list->current->next->data_len=data_len;
114 dprintf(list->outfd,"[list] element added\n");
118 else list_next(list);
121 /* if this is the first element */
122 list->start=(t_list_element *)malloc(sizeof(t_list_element));
123 if(list->start==NULL) {
124 dprintf(list->outfd,"[list] unable to allocate list element memory\n");
127 if((list->start->data=malloc(data_len))==NULL) {
128 dprintf(list->outfd,"[list] unable to allocate data memory\n");
131 memcpy(list->start->data,data,data_len);
132 list->start->data_len=data_len;
133 list->start->next=NULL;
134 list->start->prev=NULL;
136 dprintf(list->outfd,"[list] added initial element\n");
141 int list_count(t_list *list) {
149 while(list->current!=NULL) {