664dd001ab911d66566edb4a6b85492d8bf634cf
[my-code/api.git] / list / list.c
1 /* list.c -- list management szuff
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *
5  */
6
7 #include "list.h"
8
9 int list_init(t_list *list,int outfd) {
10
11   dprintf(outfd,"[list] list initialization ...\n");
12
13   list->outfd=outfd;
14   list->start=NULL;
15   list_reset(list);
16
17   return L_SUCCESS;
18 }
19
20 int list_shutdown(t_list *list) {
21
22   if(list->start!=NULL) list_destroy(list);
23
24   dprintf(list->outfd,"[list] shutdown\n");
25
26   return L_SUCCESS;
27 }
28
29 int list_next(t_list *list) {
30
31   list->current=list->current->next;
32
33   return L_SUCCESS;
34 }
35
36 int list_reset(t_list *list) {
37
38   list->current=list->start;
39
40   return L_SUCCESS;
41 }
42
43 int list_del_element(t_list *list,t_list_element *element) {
44
45   if(element==NULL) {
46     dprintf(list->outfd,"[list] empty element\n");
47     return L_INVALID_ELEMENT;
48   }
49
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;
53   list_reset(list);
54   if(element->data!=NULL) free(element->data);
55   free(element);
56   dprintf(list->outfd,"[list] element deleted\n");
57
58   return L_SUCCESS;
59 }
60
61 int list_del_current(t_list *list) {
62
63   return(list_del_element(list,list->current));
64 }
65
66 int list_destroy(t_list *list) {
67
68   if(list->start==NULL) {
69     dprintf(list->outfd,"[list] empty list\n");
70     return L_EMPTY_LIST;
71   }
72
73   list_reset(list);
74
75   while(list->current!=NULL) list_del_element(list,list->current);
76
77   return L_SUCCESS;
78 }
79
80 int list_search_data(t_list *list,void *data,int first_bytes) {
81
82   list_reset(list);
83
84   while(list->current!=NULL) {
85     if(first_bytes<=list->current->data_len)
86       if(!(memcmp(list->current->data,data,first_bytes))) return L_SUCCESS;
87     list_next(list);
88   }
89
90   return L_NO_SUCH_ELEMENT;
91 }
92
93 int list_add_element(t_list *list,void *data,int data_len) {
94
95   list_reset(list);
96
97   while(list->current!=NULL) {
98     if(data_len==list->current->data_len) {
99       if(!memcmp(list->current->data,data,data_len)) {
100         dprintf(list->outfd,"[list] element already in list\n");
101         return L_ELEMENT_IN_LIST;
102       }
103     }
104     if(list->current->next==NULL) {
105       list->current->next=(t_list_element *)malloc(sizeof(t_list_element));
106       if(list->current->next==NULL) {
107         dprintf(list->outfd,"[list] unable to allocate element memory\n");
108         return L_E_MEM;
109       }
110       list->current->next->prev=list->current;
111       list->current->next->next=NULL;
112       if((list->current->next->data=malloc(data_len))==NULL) {
113         dprintf(list->outfd,"[list] unable to allocate data memory\n");
114         return L_E_MEM;
115       }
116       memcpy(list->current->next->data,data,data_len);
117       list->current->next->data_len=data_len;
118
119       dprintf(list->outfd,"[list] element added\n");
120
121       return L_SUCCESS;
122     }
123     else list_next(list);
124   }
125
126   /* if this is the first element */
127   list->start=(t_list_element *)malloc(sizeof(t_list_element));
128   if(list->start==NULL) {
129     dprintf(list->outfd,"[list] unable to allocate list element memory\n");
130     return L_E_MEM;
131   }
132   if((list->start->data=malloc(data_len))==NULL) {
133     dprintf(list->outfd,"[list] unable to allocate data memory\n");
134     return L_E_MEM;
135   }
136   memcpy(list->start->data,data,data_len);
137   list->start->data_len=data_len;
138   list->start->next=NULL;
139   list->start->prev=NULL;
140
141   dprintf(list->outfd,"[list] added initial element\n");
142
143   return L_SUCCESS;
144 }
145
146 int list_count(t_list *list) {
147
148   int count;
149
150   count=0;
151
152   list_reset(list);
153
154   while(list->current!=NULL) {
155     list_next(list);
156     ++count;
157   }
158
159   return count;
160 }