annotate lwlib/lw_stack.c @ 423:079d43b6967b 3.0-beta2 3.0-beta2

Added missing file
author lost@l-w.ca
date Sun, 19 Sep 2010 01:44:47 -0600
parents c15cca3ae6a2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
329
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
1 /*
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
2 lw_stack.c
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
3
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
4 Copyright © 2010 William Astle
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
5
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
6 This file is part of LWTOOLS.
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
7
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
11 version.
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
12
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
16 more details.
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
17
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
20 */
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
21
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
22 #include <config.h>
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
23
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
24 #define ___lw_stack_c_seen___
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
25 #include <lw_stack.h>
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
26 #include <lw_alloc.h>
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
27
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
28 /* this is technically valid but dubious */
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
29 #define NULL 0
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
30
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
31 lw_stack_t lw_stack_create(void (*freefn)(void *d))
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
32 {
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
33 lw_stack_t S;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
34
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
35 S = lw_alloc(sizeof(lw_stack_t));
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
36 S -> head = NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
37 S -> freefn = freefn;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
38 return S;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
39 }
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
40
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
41 void *lw_stack_pop(lw_stack_t S)
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
42 {
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
43 if (S -> head)
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
44 {
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
45 void *ret, *r2;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
46
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
47 ret = S -> head -> data;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
48 r2 = S -> head;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
49 S -> head = S -> head -> next;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
50 lw_free(r2);
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
51 return ret;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
52 }
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
53 return NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
54 }
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
55
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
56 void lw_stack_destroy(lw_stack_t S)
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
57 {
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
58 void *d;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
59
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
60 while (d = lw_stack_pop(S))
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
61 (S->freefn)(d);
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
62 lw_free(S);
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
63 }
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
64
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
65 void *lw_stack_top(lw_stack_t S)
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
66 {
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
67 if (S -> head)
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
68 return S -> head -> data;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
69 return NULL;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
70 }
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
71
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
72 void lw_stack_push(lw_stack_t S, void *item)
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
73 {
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
74 struct lw_stack_node_priv *t;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
75
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
76 t = lw_alloc(sizeof(struct lw_stack_node_priv));
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
77 t -> next = S -> head;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
78 S -> head = t;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
79 t -> data = item;
c15cca3ae6a2 Created first pass of input layer
lost
parents:
diff changeset
80 }