04066bfa021fcdb31335570b289da44f2cc30ece
[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
20 #include "void_list.h"
21 #include "libbb/libbb.h"
22
23 void void_list_elt_init(void_list_elt_t *elt, void *data)
24 {
25     INIT_LIST_HEAD(&elt->node);
26     elt->data = data;
27 }
28
29 static void_list_elt_t * void_list_elt_new (void *data) {
30     void_list_elt_t *elt;
31     /* freed in void_list_elt_deinit */
32     elt = xcalloc(1, sizeof(void_list_elt_t));
33     void_list_elt_init(elt, data);
34     return elt;
35 }
36
37 void void_list_elt_deinit(void_list_elt_t *elt)
38 {
39     list_del_init(&elt->node);
40     void_list_elt_init(elt, NULL);
41     free(elt);
42 }
43
44 void void_list_init(void_list_t *list)
45 {
46     INIT_LIST_HEAD(&list->head);
47 }
48
49 void void_list_deinit(void_list_t *list)
50 {
51     void_list_elt_t *elt;
52
53     while (!void_list_empty(list)) {
54         elt = void_list_pop(list);
55         void_list_elt_deinit(elt);
56     }
57     INIT_LIST_HEAD(&list->head);
58 }
59
60 void void_list_append(void_list_t *list, void *data)
61 {
62     void_list_elt_t *elt = void_list_elt_new(data);
63     list_add_tail(&elt->node, &list->head);
64 }
65
66 void void_list_push(void_list_t *list, void *data)
67 {
68     void_list_elt_t *elt = void_list_elt_new(data);
69     list_add(&elt->node, &list->head);
70 }
71
72 void_list_elt_t *void_list_pop(void_list_t *list)
73 {
74     struct list_head *node;
75
76     if (void_list_empty(list)) 
77         return NULL;
78     node = list->head.next;
79     list_del_init(node);
80     return list_entry(node, void_list_elt_t, node);
81 }
82
83 void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
84 {
85     void_list_elt_t *pos, *n;
86     void_list_elt_t *old_elt;
87     void *old_data = NULL;
88
89     old_elt = *iter;
90     if (!old_elt)
91         return old_data;
92     old_data = old_elt->data;
93
94     list_for_each_entry_safe(pos, n, &list->head, node) {
95         if (pos == old_elt)
96             break;
97     }
98     if ( pos != old_elt) {
99         opkg_msg(ERROR, "Internal error: element not found in list.\n");
100         return NULL;
101     }
102
103     *iter = list_entry(pos->node.prev, void_list_elt_t, node);
104     void_list_elt_deinit(pos);
105
106     return old_data;
107 }
108
109 /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
110 void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
111 {
112     void_list_elt_t *pos, *n;
113     void *old_data = NULL;
114     list_for_each_entry_safe(pos, n, &list->head, node) {
115         if ( pos->data && cmp(pos->data, target_data)==0 ){
116             old_data = pos->data;
117             void_list_elt_deinit(pos);
118             break;
119         }
120     }
121     return old_data;
122 }
123
124 void_list_elt_t *void_list_first(void_list_t *list) {
125     struct list_head *elt;
126     if (!list)
127         return NULL;
128     elt = list->head.next;
129     if (elt == &list->head ) {
130         return NULL;
131     }
132     return list_entry(elt, void_list_elt_t, node);
133 }
134
135 void_list_elt_t *void_list_prev(void_list_t *list, void_list_elt_t *node) {
136     struct list_head *elt;
137     if (!list || !node)
138         return NULL;
139     elt = node->node.prev;
140     if (elt == &list->head ) {
141         return NULL;
142     }
143     return list_entry(elt, void_list_elt_t, node);
144 }
145
146 void_list_elt_t *void_list_next(void_list_t *list, void_list_elt_t *node) {
147     struct list_head *elt;
148     if (!list || !node)
149         return NULL;
150     elt = node->node.next;
151     if (elt == &list->head ) {
152         return NULL;
153     }
154     return list_entry(elt, void_list_elt_t, node);
155 }
156
157 void_list_elt_t *void_list_last(void_list_t *list) {
158     struct list_head *elt;
159     if (!list)
160         return NULL;
161     elt = list->head.prev;
162     if (elt == &list->head ) {
163         return NULL;
164     }
165     return list_entry(elt, void_list_elt_t, node);
166 }
167