There is no need to use a buffer at all.
[oweals/opkg-lede.git] / libopkg / void_list.c
1 /* void_list.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include <errno.h>
20
21 #include "void_list.h"
22 #include "libbb/libbb.h"
23
24 void void_list_elt_init(void_list_elt_t *elt, void *data)
25 {
26     INIT_LIST_HEAD(&elt->node);
27     elt->data = data;
28 }
29
30 void_list_elt_t * void_list_elt_new (void *data) {
31     void_list_elt_t *elt;
32     /* freed in void_list_elt_deinit */
33     elt = xcalloc(1, sizeof(void_list_elt_t));
34     void_list_elt_init(elt, data);
35     return elt;
36 }
37
38 void void_list_elt_deinit(void_list_elt_t *elt)
39 {
40     list_del_init(&elt->node);
41     void_list_elt_init(elt, NULL);
42     free(elt);
43 }
44
45 void void_list_init(void_list_t *list)
46 {
47     INIT_LIST_HEAD(&list->head);
48 }
49
50 void void_list_deinit(void_list_t *list)
51 {
52     void_list_elt_t *elt;
53
54     while (!void_list_empty(list)) {
55         elt = void_list_pop(list);
56         void_list_elt_deinit(elt);
57     }
58     INIT_LIST_HEAD(&list->head);
59 }
60
61 void void_list_append(void_list_t *list, void *data)
62 {
63     void_list_elt_t *elt = void_list_elt_new(data);
64     list_add_tail(&elt->node, &list->head);
65 }
66
67 void void_list_push(void_list_t *list, void *data)
68 {
69     void_list_elt_t *elt = void_list_elt_new(data);
70     list_add(&elt->node, &list->head);
71 }
72
73 void_list_elt_t *void_list_pop(void_list_t *list)
74 {
75     struct list_head *node;
76
77     if (void_list_empty(list)) 
78         return NULL;
79     node = list->head.next;
80     list_del_init(node);
81     return list_entry(node, void_list_elt_t, node);
82 }
83
84 void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
85 {
86     void_list_elt_t *pos, *n;
87     void_list_elt_t *old_elt;
88     void *old_data = NULL;
89
90     old_elt = *iter;
91     if (!old_elt)
92         return old_data;
93     old_data = old_elt->data;
94
95     list_for_each_entry_safe(pos, n, &list->head, node) {
96         if (pos == old_elt)
97             break;
98     }
99     if ( pos != old_elt) {
100         fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
101         return NULL;
102     }
103
104     *iter = list_entry(pos->node.prev, void_list_elt_t, node);
105     void_list_elt_deinit(pos);
106
107     return old_data;
108 }
109
110 /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
111 void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
112 {
113     void_list_elt_t *pos, *n;
114     void *old_data = NULL;
115     list_for_each_entry_safe(pos, n, &list->head, node) {
116         if ( pos->data && cmp(pos->data, target_data)==0 ){
117             old_data = pos->data;
118             void_list_elt_deinit(pos);
119             break;
120         }
121     }
122     return old_data;
123 }
124
125 void_list_elt_t *void_list_first(void_list_t *list) {
126     struct list_head *elt;
127     if (!list)
128         return NULL;
129     elt = list->head.next;
130     if (elt == &list->head ) {
131         return NULL;
132     }
133     return list_entry(elt, void_list_elt_t, node);
134 }
135
136 void_list_elt_t *void_list_prev(void_list_t *list, void_list_elt_t *node) {
137     struct list_head *elt;
138     if (!list || !node)
139         return NULL;
140     elt = node->node.prev;
141     if (elt == &list->head ) {
142         return NULL;
143     }
144     return list_entry(elt, void_list_elt_t, node);
145 }
146
147 void_list_elt_t *void_list_next(void_list_t *list, void_list_elt_t *node) {
148     struct list_head *elt;
149     if (!list || !node)
150         return NULL;
151     elt = node->node.next;
152     if (elt == &list->head ) {
153         return NULL;
154     }
155     return list_entry(elt, void_list_elt_t, node);
156 }
157
158 void_list_elt_t *void_list_last(void_list_t *list) {
159     struct list_head *elt;
160     if (!list)
161         return NULL;
162     elt = list->head.prev;
163     if (elt == &list->head ) {
164         return NULL;
165     }
166     return list_entry(elt, void_list_elt_t, node);
167 }
168