logging tweak
[l2tpns.git] / ll.c
1 // L2TPNS Linked List Stuff
2
3 char const *cvs_id_ll = "$Id: ll.c,v 1.6 2004/11/18 08:12:55 bodea Exp $";
4
5 #include <stdio.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <malloc.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include "ll.h"
16
17 linked_list *ll_init()
18 {
19 return (linked_list *)calloc(sizeof(linked_list), 1);
20 }
21
22 void ll_done(linked_list *l)
23 {
24 li *i = l->head, *n;
25
26 while (i)
27 {
28 n = i->next;
29 free(i);
30 i = n;
31 }
32
33 free(l);
34 }
35
36 li *ll_push(linked_list *l, void *data)
37 {
38 li *i;
39
40 if (!l) return NULL;
41 if (!(i = (li *)calloc(sizeof(li), 1))) return NULL;
42
43 i->data = data;
44 i->next = NULL;
45 if (l->end)
46 l->end->next = i;
47 else
48 l->head = i;
49 l->end = i;
50
51 return i;
52 }
53
54 void *ll_pop(linked_list *l)
55 {
56 li *i;
57 void *data;
58
59 if (!l) return NULL;
60 if (!l->head)
61 return NULL;
62
63 data = l->head->data;
64 i = l->head->next;
65 free(l->head);
66 l->head = i;
67 return data;
68 }
69
70 void ll_iterate(linked_list *l, int(*func)(void *))
71 {
72 li *i;
73 if (!l || !func) return;
74
75 for (i = l->head; i; i = i->next)
76 {
77 if (i->data && !func(i->data))
78 break;
79 }
80 }
81
82 void ll_reset(linked_list *l)
83 {
84 if (!l) return;
85 l->current = NULL;
86 }
87
88 void *ll_next(linked_list *l)
89 {
90 if (!l) return NULL;
91 if (!l->current)
92 l->current = l->head;
93 else
94 l->current = l->current->next;
95 if (!l->current)
96 return NULL;
97 return l->current->data;
98 }
99
100 void ll_delete(linked_list *l, void *data)
101 {
102 li *i = l->head, *p = NULL;
103
104 while (i)
105 {
106 if (i->data == data)
107 {
108 if (l->head == i) l->head = i->next;
109 if (l->end == i) l->end = p;
110 if (p) p->next = i->next;
111 free(i);
112 l->current = NULL;
113 return;
114 }
115 p = i;
116 i = i->next;
117 }
118 }
119
120 int ll_size(linked_list *l)
121 {
122 int count = 0;
123 li *i;
124
125 if (!l) return 0;
126
127 for (i = l->head; i; i = i->next)
128 if (i->data) count++;
129
130 return count;
131 }
132
133 int ll_contains(linked_list *l, void *search)
134 {
135 li *i;
136 for (i = l->head; i; i = i->next)
137 if (i->data == search)
138 return 1;
139 return 0;
140 }
141