1 /* void_list.c - the itsy package management system
5 Copyright (C) 2001 University of Southern California
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.
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.
21 #include "void_list.h"
23 int void_list_elt_init(void_list_elt_t *elt, void *data)
31 void void_list_elt_deinit(void_list_elt_t *elt)
33 void_list_elt_init(elt, NULL);
36 int void_list_init(void_list_t *list)
38 void_list_elt_init(&list->pre_head, NULL);
40 list->pre_head.next = list->head;
46 void void_list_deinit(void_list_t *list)
51 elt = void_list_pop(list);
52 void_list_elt_deinit(elt);
53 /* malloced in void_list_append */
58 int void_list_append(void_list_t *list, void *data)
62 /* freed in void_list_deinit */
63 elt = malloc(sizeof(void_list_elt_t));
65 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
69 void_list_elt_init(elt, data);
72 list->tail->next = elt;
76 list->pre_head.next = list->head;
83 int void_list_push(void_list_t *list, void *data)
87 elt = malloc(sizeof(void_list_elt_t));
89 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
93 void_list_elt_init(elt, data);
95 elt->next = list->head;
96 list->head->next = elt;
97 if (list->tail == NULL) {
98 list->tail = list->head;
104 void_list_elt_t *void_list_pop(void_list_t *list)
106 void_list_elt_t *elt;
111 list->head = list->head->next;
112 list->pre_head.next = list->head;
113 if (list->head == NULL) {
121 void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
123 void_list_elt_t *prior;
124 void_list_elt_t *old_elt;
128 old_data = old_elt->data;
130 if (old_elt == list->head) {
131 prior = &list->pre_head;
134 for (prior = list->head; prior; prior = prior->next) {
135 if (prior->next == old_elt) {
139 if (prior == NULL || prior->next != old_elt) {
140 fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
143 prior->next = old_elt->next;
145 if (old_elt == list->tail) {
150 void_list_elt_deinit(old_elt);
156 /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
157 void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
159 void_list_elt_t *prior;
160 void_list_elt_t *old_elt = NULL;
161 void *old_data = NULL;
164 if (list->head && list->head->data && (cmp(list->head->data, target_data) == 0)) {
165 old_elt = list->head;
166 old_data = list->head->data;
170 for (prior = list->head; prior && prior->next; prior = prior->next) {
171 if (prior->next->data && (cmp(prior->next->data, target_data) == 0)) {
172 old_elt = prior->next;
173 old_data = old_elt->data;
181 prior->next = old_elt->next;
183 if (old_elt == list->tail) {
188 void_list_elt_deinit(old_elt);