/[LeafOK_CVS]/pvpgn-1.7.4/src/common/list.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/common/list.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

1 /*
2 * Copyright (C) 1998,1999,2000 Ross Combs (rocombs@cs.nmsu.edu)
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #define LIST_INTERNAL_ACCESS
19 #include "common/setup_before.h"
20 #ifdef HAVE_STDDEF_H
21 # include <stddef.h>
22 #else
23 # ifndef NULL
24 # define NULL ((void *)0)
25 # endif
26 #endif
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 #else
30 # ifdef HAVE_MALLOC_H
31 # include <malloc.h>
32 # endif
33 #endif
34 #ifdef HAVE_ASSERT_H
35 # include <assert.h>
36 #endif
37 #include "common/eventlog.h"
38 #include "common/xalloc.h"
39 #include "common/list.h"
40 #include "common/setup_after.h"
41
42
43 static t_elem listhead;
44
45
46 extern t_list * list_create(void)
47 {
48 t_list * new;
49
50 new = xmalloc(sizeof(t_list));
51 new->head = NULL;
52 new->tail = NULL;
53 new->len = 0;
54
55 return new;
56 }
57
58
59 extern int list_destroy(t_list * list)
60 {
61 if (!list)
62 {
63 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
64 return -1;
65 }
66
67 if (list->head)
68 eventlog(eventlog_level_error,__FUNCTION__,"got non-empty list");
69
70 xfree(list);
71
72 return 0;
73 }
74
75 extern unsigned int list_get_length(t_list const * list)
76 {
77 if (!list)
78 {
79 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
80 return 0;
81 }
82
83 return list->len;
84 }
85
86
87 extern int list_prepend_data(t_list * list, void * data)
88 {
89 t_elem * elem;
90
91 assert(list != NULL);
92
93 elem = xmalloc(sizeof(t_elem));
94 elem->data = data;
95
96 if (list->head)
97 list->head->prev = elem;
98 elem->next = list->head;
99 elem->prev = NULL;
100 list->head = elem;
101 if (!list->tail)
102 list->tail = elem;
103 list->len++;
104
105 return 0;
106 }
107
108
109 extern int list_append_data(t_list * list, void * data)
110 {
111 t_elem * elem;
112
113 assert(list != NULL);
114
115 elem = xmalloc(sizeof(t_elem));
116 elem->data = data;
117
118 elem->next = NULL;
119 if (!list->head)
120 {
121 list->head = elem;
122 elem->prev = NULL;
123 }
124 if (list->tail)
125 {
126 elem->prev = list->tail;
127 list->tail->next = elem;
128 }
129 list->tail = elem;
130 list->len++;
131
132 return 0;
133 }
134
135
136 extern t_elem * list_get_elem_by_data(t_list const * list, void const * data)
137 {
138 t_elem * curr;
139
140 if (!list)
141 {
142 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
143 return NULL;
144 }
145
146 LIST_TRAVERSE(list,curr)
147 if (curr->data==data)
148 return curr;
149
150 return NULL;
151 }
152
153
154 extern t_elem const * list_get_elem_by_data_const(t_list const * list, void const * data)
155 {
156 t_elem const * curr;
157
158 if (!list)
159 {
160 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
161 return NULL;
162 }
163
164 LIST_TRAVERSE_CONST(list,curr)
165 if (curr->data==data)
166 return curr;
167
168 return NULL;
169 }
170
171
172 extern int list_remove_elem(t_list * list, t_elem ** elem)
173 {
174 t_elem * target;
175
176 if (!list)
177 {
178 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
179 return -1;
180 }
181
182 if (!elem)
183 {
184 eventlog(eventlog_level_error,__FUNCTION__,"got NULL *elem");
185 return -1;
186 }
187
188 target = *elem;
189
190 if (target->prev)
191 {
192 target->prev->next = target->next;
193 }
194 if (target->next)
195 {
196 target->next->prev = target->prev;
197 }
198
199 if (target == list->tail)
200 {
201 list->tail = target->prev;
202 }
203 if (target == list->head)
204 {
205 list->head = target->next;
206 *elem = &listhead;
207 }
208 else
209 *elem = target->prev;
210
211 target->next = NULL;
212 target->prev = NULL;
213 xfree(target);
214
215 list->len--;
216
217 return 0;
218 }
219
220
221 extern int list_remove_data(t_list * list, void const * data, t_elem ** elem)
222 {
223 if (!list)
224 {
225 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
226 return -1;
227 }
228
229 if (!(*elem = list_get_elem_by_data(list,data)))
230 return -1;
231
232 return list_remove_elem(list,elem);
233 }
234
235
236 extern int elem_set_data(t_elem * elem, void * data)
237 {
238 if (!elem)
239 {
240 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem");
241 return -1;
242 }
243
244 elem->data = data;
245
246 return 0;
247 }
248
249
250 extern void * elem_get_data(t_elem const * elem)
251 {
252 if (!elem)
253 {
254 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem");
255 return NULL;
256 }
257
258 return elem->data;
259 }
260
261
262 extern void * list_get_data_by_pos(t_list const * list, unsigned int pos)
263 {
264 t_elem const * curr;
265 unsigned int len;
266
267 len = 0;
268 LIST_TRAVERSE_CONST(list,curr)
269 if (len++==pos)
270 return curr->data;
271
272 eventlog(eventlog_level_error,__FUNCTION__,"requested position %u but len=%u",pos,len);
273 return NULL;
274 }
275
276
277 #ifdef LIST_DEBUG
278 extern t_elem * list_get_first_real(t_list const * list, char const * fn, unsigned int ln)
279 #else
280 extern t_elem * list_get_first(t_list const * list)
281 #endif
282 {
283 if (!list)
284 {
285 #ifdef LIST_DEBUG
286 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list from %s:%u",fn,ln);
287 #else
288 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
289 #endif
290 return NULL;
291 }
292
293
294 return list->head;
295 }
296
297
298 #ifdef LIST_DEBUG
299 extern t_elem const * list_get_first_const_real(t_list const * list, char const * fn, unsigned int ln)
300 #else
301 extern t_elem const * list_get_first_const(t_list const * list)
302 #endif
303 {
304 if (!list)
305 {
306 #ifdef LIST_DEBUG
307 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list from %s:%u",fn,ln);
308 #else
309 eventlog(eventlog_level_error,__FUNCTION__,"got NULL list");
310 #endif
311 return NULL;
312 }
313
314 return list->head;
315 }
316
317
318 extern t_elem * elem_get_next_real(t_list const * list, t_elem const * elem, char const * fn, unsigned int ln)
319 {
320 if (!elem)
321 {
322 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem from %s:%u",fn,ln);
323 return NULL;
324 }
325
326 if (elem == &listhead)
327 return list->head;
328 else
329 return elem->next;
330 }
331
332
333 extern t_elem const * elem_get_next_const(t_list const * list, t_elem const * elem)
334 {
335 if (!elem)
336 {
337 eventlog(eventlog_level_error,__FUNCTION__,"got NULL elem");
338 return NULL;
339 }
340
341 if (elem == &listhead)
342 return list->head;
343 else
344 return elem->next;
345 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1