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_del_current(t_list *list) {
63 return(list_del_element(list,list->current));
66 int list_destroy(t_list *list) {
68 if(list->start==NULL) {
69 dprintf(list->outfd,"[list] empty list\n");
75 while(list->current!=NULL) list_del_element(list,list->current);
80 int list_search_data(t_list *list,void *data,int first_bytes) {
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;
90 return L_NO_SUCH_ELEMENT;
93 int list_add_element(t_list *list,void *data,int data_len) {
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;
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");
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");
116 memcpy(list->current->next->data,data,data_len);
117 list->current->next->data_len=data_len;
119 dprintf(list->outfd,"[list] element added\n");
123 else list_next(list);
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");
132 if((list->start->data=malloc(data_len))==NULL) {
133 dprintf(list->outfd,"[list] unable to allocate data memory\n");
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;
141 dprintf(list->outfd,"[list] added initial element\n");
146 int list_count(t_list *list) {
154 while(list->current!=NULL) {