new list stuff (still in work)
[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 initilaization ...\n");
12
13   list->outfd=outfd;
14   list->start=NULL;
15   list->current=NULL;
16
17   return L_SUCCESS;
18 }
19
20 int list_del_next(t_list *list,t_list_element *element) {
21
22   t_list_element *tmp;
23
24   if(element==NULL) {
25     dprintf(list->outfd,"[list] empty element\n");
26     return L_INVALID_ELEMENT;
27   }
28
29   if(element->next!=NULL) {
30     dprintf(list->outfd,"[list] destroying element number %d\n"
31     tmp=element->next->next;
32     if(element->next->data!=NULL) free(element->next->data);
33     free(element->next);
34     element->next=tmp;
35   }
36   else {
37     dprintf(list->outfd,"[list] no next element\n");
38     return L_NO_NEXT_ELEMENT;
39   }
40
41   return L_SUCCESS;
42 }
43
44 int list_destroy(t_list *list) {
45
46   if(list->start==NULL) {
47     dprintf(list->outfd,"[list] empty list\n");
48     return L_EMPTY_LIST;
49   }
50
51   list->current=list->start;
52
53   while(list->current->next!=NULL)
54     list_del_next(list,list->current);
55
56   return L_SUCCESS;
57 }
58
59 int list_del_nr(t_list *list,int nr) {
60
61   if(list->start==NULL) {
62     dprintf(list->outfd,"[list] empty list\n");
63     return L_EMPTY_LIST;
64   }
65
66   if(list->start->number==nr) {
67
68   }  
69
70   list->current=&(list->start);
71
72   while(list->current->next!=NULL) {
73     if(list->current->next->number==nr) {
74       list_del_next(list,list->current);
75       return L_SUCCESS;
76     }
77     list->current=list->current->next;
78   }
79
80   return L_NO_SUCH_ELEMENT;
81 }
82
83 int list_search_data(t_list *list,void *data,int data_len) {
84
85   t_element *help;
86
87   help=&(list->start);
88
89   while(help->next!=NULL) {
90     if(help->next->data_len==data_len) {
91       if(!(memcmp(help->next->data,data,data_len)) {
92         list->current=help->next;
93         return L_SUCCESS;
94       }
95     }
96     list->current=list->current->next;
97   }
98
99   return L_NO_SUCH_ELEMENT;
100 }
101
102 int list_search_nr(t_list *list,int nr) {
103
104   list->current=&(list->start);
105
106   while(list->current->next!=NULL) {
107     if(list->current->next->number==nr) {
108       list->current=list->current->next;
109       return L_SUCCESS;
110     }
111     list->current=list->current->next;
112   }
113
114   return L_NO_SUCH_ELEMENT;
115 }
116
117 int list_append_element(t_list *list,t_list_element *element) {
118
119   list->current=&(list->start);