completed minimal list api
[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_destroy(t_list *list) {
62
63   if(list->start==NULL) {
64     dprintf(list->outfd,"[list] empty list\n");
65     return L_EMPTY_LIST;
66   }
67
68   list_reset(list);
69
70   while(list->current!=NULL) list_del_element(list,list->current);
71
72   return L_SUCCESS;
73 }
74
75 int list_search_data(t_list *list,void *data,int first_bytes) {
76
77   list_reset(list);
78
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;
82     list_next(list);
83   }
84
85   return L_NO_SUCH_ELEMENT;
86 }
87
88 int list_add_element(t_list *list,void *data,int data_len) {
89
90   list_reset(list);
91
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;
97       }
98     }
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");
103         return L_E_MEM;
104       }
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");
109         return L_E_MEM;
110       }
111       memcpy(list->current->next->data,data,data_len);
112       list->current->next->data_len=data_len;
113
114       dprintf(list->outfd,"[list] element added\n");
115
116       return L_SUCCESS;
117     }
118     else list_next(list);
119   }
120
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");
125     return L_E_MEM;
126   }
127   if((list->start->data=malloc(data_len))==NULL) {
128     dprintf(list->outfd,"[list] unable to allocate data memory\n");
129     return L_E_MEM;
130   }
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;
135
136   dprintf(list->outfd,"[list] added initial element\n");
137
138   return L_SUCCESS;
139 }
140
141 int list_count(t_list *list) {
142
143   int count;
144
145   count=0;
146
147   list_reset(list);
148
149   while(list->current!=NULL) {
150     list_next(list);
151     ++count;
152   }
153
154   return count;
155 }