summaryrefslogtreecommitdiffstats
path: root/libopkg/void_list.c
blob: 4c9d8971f402ef52b8f3e48ba19f93488ed6ba26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* void_list.c - the opkg package management system

   Carl D. Worth

   Copyright (C) 2001 University of Southern California

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include "void_list.h"
#include "libbb/libbb.h"

void void_list_elt_init(void_list_elt_t *elt, void *data)
{
    INIT_LIST_HEAD(&elt->node);
    elt->data = data;
}

static void_list_elt_t * void_list_elt_new (void *data) {
    void_list_elt_t *elt;
    /* freed in void_list_elt_deinit */
    elt = xcalloc(1, sizeof(void_list_elt_t));
    void_list_elt_init(elt, data);
    return elt;
}

void void_list_elt_deinit(void_list_elt_t *elt)
{
    list_del_init(&elt->node);
    void_list_elt_init(elt, NULL);
    free(elt);
}

void void_list_init(void_list_t *list)
{
    INIT_LIST_HEAD(&list->head);
}

void void_list_deinit(void_list_t *list)
{
    void_list_elt_t *elt;

    while (!void_list_empty(list)) {
	elt = void_list_pop(list);
	void_list_elt_deinit(elt);
    }
    INIT_LIST_HEAD(&list->head);
}

void void_list_append(void_list_t *list, void *data)
{
    void_list_elt_t *elt = void_list_elt_new(data);
    list_add_tail(&elt->node, &list->head);
}

void void_list_push(void_list_t *list, void *data)
{
    void_list_elt_t *elt = void_list_elt_new(data);
    list_add(&elt->node, &list->head);
}

void_list_elt_t *void_list_pop(void_list_t *list)
{
    struct list_head *node;

    if (void_list_empty(list))
        return NULL;
    node = list->head.next;
    list_del_init(node);
    return list_entry(node, void_list_elt_t, node);
}

void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
{
    void_list_elt_t *pos, *n;
    void_list_elt_t *old_elt;
    void *old_data = NULL;

    old_elt = *iter;
    if (!old_elt)
        return old_data;
    old_data = old_elt->data;

    list_for_each_entry_safe(pos, n, &list->head, node) {
        if (pos == old_elt)
            break;
    }
    if ( pos != old_elt) {
        opkg_msg(ERROR, "Internal error: element not found in list.\n");
        return NULL;
    }

    *iter = list_entry(pos->node.prev, void_list_elt_t, node);
    void_list_elt_deinit(pos);

    return old_data;
}

/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
{
    void_list_elt_t *pos, *n;
    void *old_data = NULL;
    list_for_each_entry_safe(pos, n, &list->head, node) {
        if ( pos->data && cmp(pos->data, target_data)==0 ){
            old_data = pos->data;
            void_list_elt_deinit(pos);
            break;
        }
    }
    return old_data;
}

void_list_elt_t *void_list_first(void_list_t *list) {
    struct list_head *elt;
    if (!list)
        return NULL;
    elt = list->head.next;
    if (elt == &list->head ) {
        return NULL;
    }
    return list_entry(elt, void_list_elt_t, node);
}

void_list_elt_t *void_list_prev(void_list_t *list, void_list_elt_t *node) {
    struct list_head *elt;
    if (!list || !node)
        return NULL;
    elt = node->node.prev;
    if (elt == &list->head ) {
        return NULL;
    }
    return list_entry(elt, void_list_elt_t, node);
}

void_list_elt_t *void_list_next(void_list_t *list, void_list_elt_t *node) {
    struct list_head *elt;
    if (!list || !node)
        return NULL;
    elt = node->node.next;
    if (elt == &list->head ) {
        return NULL;
    }
    return list_entry(elt, void_list_elt_t, node);
}

void_list_elt_t *void_list_last(void_list_t *list) {
    struct list_head *elt;
    if (!list)
        return NULL;
    elt = list->head.prev;
    if (elt == &list->head ) {
        return NULL;
    }
    return list_entry(elt, void_list_elt_t, node);
}