Mercurial > hg > index.cgi
comparison lwlib/lw_stack.c @ 0:2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author | lost@l-w.ca |
---|---|
date | Wed, 19 Jan 2011 22:27:17 -0700 |
parents | |
children | 7317fbe024af |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2c24602be78f |
---|---|
1 /* | |
2 lw_stack.c | |
3 | |
4 Copyright © 2010 William Astle | |
5 | |
6 This file is part of LWTOOLS. | |
7 | |
8 LWTOOLS is free software: you can redistribute it and/or modify it under the | |
9 terms of the GNU General Public License as published by the Free Software | |
10 Foundation, either version 3 of the License, or (at your option) any later | |
11 version. | |
12 | |
13 This program is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 more details. | |
17 | |
18 You should have received a copy of the GNU General Public License along with | |
19 this program. If not, see <http://www.gnu.org/licenses/>. | |
20 */ | |
21 | |
22 #define ___lw_stack_c_seen___ | |
23 #include "lw_stack.h" | |
24 #include "lw_alloc.h" | |
25 | |
26 /* this is technically valid but dubious */ | |
27 #define NULL 0 | |
28 | |
29 lw_stack_t lw_stack_create(void (*freefn)(void *d)) | |
30 { | |
31 lw_stack_t S; | |
32 | |
33 S = lw_alloc(sizeof(lw_stack_t)); | |
34 S -> head = NULL; | |
35 S -> freefn = freefn; | |
36 return S; | |
37 } | |
38 | |
39 void *lw_stack_pop(lw_stack_t S) | |
40 { | |
41 if (S -> head) | |
42 { | |
43 void *ret, *r2; | |
44 | |
45 ret = S -> head -> data; | |
46 r2 = S -> head; | |
47 S -> head = S -> head -> next; | |
48 lw_free(r2); | |
49 return ret; | |
50 } | |
51 return NULL; | |
52 } | |
53 | |
54 void lw_stack_destroy(lw_stack_t S) | |
55 { | |
56 void *d; | |
57 | |
58 while (d = lw_stack_pop(S)) | |
59 (S->freefn)(d); | |
60 lw_free(S); | |
61 } | |
62 | |
63 void *lw_stack_top(lw_stack_t S) | |
64 { | |
65 if (S -> head) | |
66 return S -> head -> data; | |
67 return NULL; | |
68 } | |
69 | |
70 void lw_stack_push(lw_stack_t S, void *item) | |
71 { | |
72 struct lw_stack_node_priv *t; | |
73 | |
74 t = lw_alloc(sizeof(struct lw_stack_node_priv)); | |
75 t -> next = S -> head; | |
76 S -> head = t; | |
77 t -> data = item; | |
78 } |