]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/lib/llist.c
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / curl / lib / llist.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  * 
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id: llist.c,v 1.12 2004/01/07 09:19:35 bagder Exp $
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "llist.h"
30
31 #ifdef CURLDEBUG
32 /* this must be the last include file */
33 #include "memdebug.h"
34 #endif
35 void 
36 Curl_llist_init(curl_llist *l, curl_llist_dtor dtor)
37 {
38   l->size = 0;
39   l->dtor = dtor;
40   l->head = NULL;
41   l->tail = NULL;
42 }
43
44 curl_llist *
45 Curl_llist_alloc(curl_llist_dtor dtor)
46 {
47   curl_llist *list;
48
49   list = (curl_llist *)malloc(sizeof(curl_llist));
50   if(NULL == list)
51     return NULL;
52
53   Curl_llist_init(list, dtor);
54
55   return list;
56 }
57
58 int
59 Curl_llist_insert_next(curl_llist *list, curl_llist_element *e, const void *p)
60 {
61   curl_llist_element  *ne;
62
63   ne = (curl_llist_element *) malloc(sizeof(curl_llist_element));
64   ne->ptr = (void *) p;
65   if (list->size == 0) {
66     list->head = ne;
67     list->head->prev = NULL;
68     list->head->next = NULL;
69     list->tail = ne;
70   } else {
71     ne->next = e->next;
72     ne->prev = e;
73     if (e->next) {
74       e->next->prev = ne;
75     } else {
76       list->tail = ne;
77     }
78     e->next = ne;
79   }
80
81   ++list->size;
82
83   return 1;
84 }
85
86 #if 0
87 int 
88 Curl_llist_insert_prev(curl_llist *list, curl_llist_element *e, const void *p)
89 {
90   curl_llist_element *ne;
91
92   ne = (curl_llist_element *) malloc(sizeof(curl_llist_element));
93   ne->ptr = (void *) p;
94   if (list->size == 0) {
95     list->head = ne;
96     list->head->prev = NULL;
97     list->head->next = NULL;
98     list->tail = ne;
99   } else {
100     ne->next = e;
101     ne->prev = e->prev;
102     if (e->prev)
103       e->prev->next = ne;
104     else
105       list->head = ne;
106     e->prev = ne;
107   }
108
109   ++list->size;
110
111   return 1;
112 }
113 #endif
114
115 int 
116 Curl_llist_remove(curl_llist *list, curl_llist_element *e, void *user)
117 {
118   if (e == NULL || list->size == 0)
119     return 1;
120
121   if (e == list->head) {
122     list->head = e->next;
123
124     if (list->head == NULL)
125       list->tail = NULL;
126     else
127       e->next->prev = NULL;
128   } else {
129     e->prev->next = e->next;
130     if (!e->next)
131       list->tail = e->prev;
132     else
133       e->next->prev = e->prev;
134   }
135
136   list->dtor(user, e->ptr);
137   free(e);
138   --list->size;
139
140   return 1;
141 }
142
143 #if 0
144 int 
145 Curl_llist_remove_next(curl_llist *list, curl_llist_element *e, void *user)
146 {
147   return Curl_llist_remove(list, e->next, user);
148 }
149
150 int 
151 Curl_llist_remove_prev(curl_llist *list, curl_llist_element *e, void *user)
152 {
153   return Curl_llist_remove(list, e->prev, user);
154 }
155
156 size_t 
157 Curl_llist_count(curl_llist *list)
158 {
159   return list->size;
160 }
161 #endif
162
163 void 
164 Curl_llist_destroy(curl_llist *list, void *user)
165 {
166   if(list) {
167     while (list->size > 0)
168       Curl_llist_remove(list, list->tail, user);
169
170     free(list);
171   }
172 }