Some consistency changes to library & headers flags.
[splint-patched.git] / src / ltokenList.c
blob6d320346cc2eaf49ac7588488d66eba55fbc87a8
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 ** ltokenList.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 /*@notnull@*/ /*@only@*/ ltokenList
36 ltokenList_new (void)
38 ltokenList s = (ltokenList) dmalloc (sizeof (*s));
40 s->nelements = 0;
41 s->nspace = ltokenListBASESIZE;
42 s->elements = (ltoken *)
43 dmalloc (sizeof (*s->elements) * ltokenListBASESIZE);
44 s->current = 0;
46 return (s);
49 /*@notnull@*/ /*@only@*/ ltokenList
50 ltokenList_singleton (ltoken l)
52 ltokenList s = (ltokenList) dmalloc (sizeof (*s));
54 s->nelements = 1;
55 s->nspace = ltokenListBASESIZE - 1;
56 s->elements = (ltoken *) dmalloc (sizeof (*s->elements) * ltokenListBASESIZE);
57 s->elements[0] = l;
58 s->current = 0;
60 return (s);
63 static void
64 ltokenList_grow (/*@notnull@*/ ltokenList s)
66 int i;
67 ltoken *newelements;
69 s->nspace += ltokenListBASESIZE;
71 newelements = (ltoken *) dmalloc (sizeof (*newelements)
72 * (s->nelements + s->nspace));
74 for (i = 0; i < s->nelements; i++)
76 newelements[i] = s->elements[i];
79 sfree (s->elements);
80 s->elements = newelements;
83 ltokenList
84 ltokenList_push (/*@returned@*/ ltokenList s, ltoken el)
86 ltokenList_addh (s, el);
87 return s;
90 void
91 ltokenList_addh (ltokenList s, ltoken el)
93 llassert (ltokenList_isDefined (s));
95 if (s->nspace <= 0)
96 ltokenList_grow (s);
98 s->nspace--;
99 s->elements[s->nelements] = el;
100 s->nelements++;
103 void
104 ltokenList_reset (ltokenList s)
106 if (ltokenList_isDefined (s))
108 s->current = 0;
112 bool
113 ltokenList_isFinished (ltokenList s)
115 return (ltokenList_isUndefined(s) || (s->current == s->nelements));
118 void
119 ltokenList_advance (ltokenList s)
121 if (ltokenList_isDefined (s))
123 s->current++;
124 llassert (s->current <= s->nelements);
128 ltoken
129 ltokenList_head (ltokenList s)
131 llassert (ltokenList_isDefined (s) && s->nelements > 0);
132 return (s->elements[0]);
135 bool
136 ltokenList_equal (ltokenList s1, ltokenList s2)
138 if (ltokenList_isUndefined (s1))
140 return (ltokenList_isEmpty (s2));
142 else
144 if (ltokenList_isUndefined (s2))
146 return ltokenList_isEmpty (s1);
148 else
150 int i;
151 int size = s1->nelements;
153 if (s2->nelements != size)
154 return FALSE;
156 for (i = 0; i < size; i++)
158 if (!ltoken_similar (s1->elements[i], s2->elements[i]))
159 return FALSE;
161 return TRUE;
166 /*@only@*/ ltokenList
167 ltokenList_copy (ltokenList s)
169 ltokenList r = ltokenList_new ();
171 ltokenList_elements (s, x)
173 ltokenList_addh (r, ltoken_copy (x));
174 } end_ltokenList_elements;
176 return r;
179 void
180 ltokenList_removeCurrent (ltokenList s)
182 int i;
183 llassert (ltokenList_isDefined (s) && s->current >= 0 && s->current < s->nelements);
185 for (i = s->current; i < s->nelements - 1; i++)
187 s->elements[i] = s->elements[i+1];
190 s->nelements--;
191 s->nspace++;
194 ltoken
195 ltokenList_current (ltokenList s)
197 llassert (ltokenList_isDefined (s) && s->current >= 0 && s->current < s->nelements);
198 return (s->elements[s->current]);
201 /*@only@*/ cstring
202 ltokenList_unparse (ltokenList s)
204 int i;
205 cstring st = cstring_undefined;
207 if (ltokenList_isDefined (s))
209 for (i = 0; i < s->nelements; i++)
211 if (i == 0)
213 st = cstring_copy (ltoken_unparse (s->elements[i]));
215 else
216 st = message ("%q, %s", st, ltoken_unparse (s->elements[i]));
220 return st;
223 void
224 ltokenList_free (ltokenList s)
226 if (ltokenList_isDefined (s))
228 sfree (s->elements);
229 sfree (s);