Moved OS dependent constants from constants.h to osd.h
[splint-patched.git] / src / termNodeList.c
blob35a49d707e315bf3430840cb34fa359e8f42bd0d
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
15 **
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
25 ** termNodeList.c
27 ** based on list_template.c
29 ** where T has T_equal (or change this) and T_unparse
32 # include "splintMacros.nf"
33 # include "basic.h"
35 termNodeList termNodeList_new ()
37 termNodeList s = (termNodeList) dmalloc (sizeof (*s));
39 s->nelements = 0;
40 s->nspacelow = termNodeListGROWLOW;
41 s->nspacehigh = termNodeListGROWHI;
42 s->elementsroot = (termNode *) dmalloc (sizeof (*s->elements) * (s->nspacelow + s->nspacehigh));
43 s->elements = s->elementsroot + termNodeListGROWLOW;
44 s->current = 0;
46 return (s);
49 static void
50 termNodeList_grow (termNodeList s)
52 int i;
53 termNode *newelements = (termNode *) dmalloc (sizeof (*newelements)
54 * (s->nelements + termNodeListBASESIZE));
56 for (i = 0; i < s->nelements; i++)
58 newelements[i + termNodeListGROWLOW] = s->elements[i];
61 sfree (s->elementsroot);
63 s->nspacelow = termNodeListGROWLOW;
64 s->nspacehigh = termNodeListGROWHI;
66 s->elementsroot = newelements;
67 s->elements = s->elementsroot + s->nspacelow;
70 void
71 termNodeList_addh (termNodeList s, termNode el)
73 llassert (termNodeListGROWHI > 0);
75 if (s->nspacehigh <= 0)
76 termNodeList_grow (s);
78 s->nspacehigh--;
79 s->elements[s->nelements] = el;
80 s->nelements++;
83 termNodeList
84 termNodeList_push (termNodeList s, termNode el)
86 termNodeList_addh (s, el);
87 return s;
90 void
91 termNodeList_addl (termNodeList s, termNode el)
93 llassert (termNodeListGROWLOW > 0);
95 if (s->nspacelow <= 0)
96 termNodeList_grow (s);
98 s->nspacelow--;
99 s->elements--;
100 s->elements[0] = el;
101 s->current++;
102 s->nelements++;
105 void
106 termNodeList_reset (termNodeList s)
108 s->current = 0;
111 void
112 termNodeList_finish (termNodeList s)
114 s->current = s->nelements - 1;
117 void
118 termNodeList_advance (termNodeList s)
120 s->current++;
121 llassert (s->current < s->nelements);
124 /*@exposed@*/ termNode
125 termNodeList_head (termNodeList s)
127 llassert (s->nelements > 0);
128 return (s->elements[0]);
131 /*@only@*/ termNodeList
132 termNodeList_copy (termNodeList s)
134 termNodeList r = termNodeList_new ();
136 termNodeList_elements (s, x)
138 termNodeList_addh (r, termNode_copySafe (x));
139 } end_termNodeList_elements;
141 return r;
144 /*@exposed@*/ termNode
145 termNodeList_current (termNodeList s)
147 llassert (!(s->current >= s->nelements));
148 return (s->elements[s->current]);
151 termNode
152 termNodeList_getN (termNodeList s, int n)
154 llassert (n >= 0 && n < s->nelements);
156 return (s->elements[n]);
159 /*@only@*/ cstring
160 termNodeList_unparse (termNodeList s)
162 bool first = TRUE;
163 cstring st = cstring_undefined;
165 termNodeList_elements (s, current)
167 if (first)
169 st = termNode_unparse (current);
170 first = FALSE;
172 else
173 st = message ("%q, %q", st, termNode_unparse (current));
174 } end_termNodeList_elements;
176 return st;
179 /*@only@*/ cstring
180 termNodeList_unparseTail (termNodeList s)
182 bool head = TRUE;
183 bool first = TRUE;
184 cstring st = cstring_undefined;
186 termNodeList_elements (s, current)
188 if (head)
190 head = FALSE;
192 else
194 if (first)
196 st = termNode_unparse (current);
197 first = FALSE;
199 else
200 st = message ("%q, %q", st, termNode_unparse (current));
202 } end_termNodeList_elements;
204 return st;
207 /*@only@*/ cstring
208 termNodeList_unparseToCurrent (termNodeList s)
210 int i;
211 cstring st = cstring_undefined;
213 for (i = 0; i < s->current; i++)
215 termNode current = s->elements[i];
217 if (i == 0)
218 st = termNode_unparse (current);
219 else
220 st = message ("%q, %q", st, termNode_unparse (current));
223 return st;
226 /*@only@*/ cstring
227 termNodeList_unparseSecondToCurrent (termNodeList s)
229 int i;
230 cstring st = cstring_undefined;
232 for (i = 1; i < s->current; i++)
234 termNode current = s->elements[i];
236 if (i == 1)
238 st = termNode_unparse (current);
240 else
242 st = message ("%q, %q", st, termNode_unparse (current));
246 return st;
249 void
250 termNodeList_free (termNodeList s)
252 int i;
253 for (i = 0; i < s->nelements; i++)
255 termNode_free (s->elements[i]);
258 sfree (s->elementsroot);
259 sfree (s);