Two useless/empty bison reductions removed.
[splint-patched.git] / src / enumNameList.c
blob85fb30df2acfd6d9454335f6c35c470f19cede90
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 ** enumNameList.c
27 ** based on list_template.c
29 ** where T has T_equal (or change this) and T_unparse
31 ** used to be cenum.c
34 # include "splintMacros.nf"
35 # include "basic.h"
38 /*@constant int enumNameListBASESIZE;@*/
39 # define enumNameListBASESIZE SMALLBASESIZE
41 enumNameList
42 enumNameList_new (void)
44 enumNameList s = (enumNameList) dmalloc (sizeof (*s));
46 s->nelements = 0;
47 s->nspace = enumNameListBASESIZE;
48 s->elements = (enumName *)
49 dmalloc (sizeof (*s->elements) * enumNameListBASESIZE);
51 return (s);
54 /*@only@*/ enumNameList
55 enumNameList_single (/*@keep@*/ enumName t)
57 enumNameList s = (enumNameList) dmalloc (sizeof (*s));
59 s->nelements = 1;
60 s->nspace = enumNameListBASESIZE - 1;
61 s->elements = (enumName *) dmalloc (sizeof (*s->elements) * enumNameListBASESIZE);
62 s->elements[0] = t;
64 return (s);
67 bool
68 enumNameList_match (enumNameList e1, enumNameList e2)
70 int i;
72 if (e1->nelements != e2->nelements) return FALSE;
74 for (i = 0; i < e1->nelements; i++)
76 if (!cstring_equal (e1->elements[i], e2->elements[i]))
77 return FALSE;
79 return TRUE;
82 static void
83 enumNameList_grow (enumNameList s)
85 int i;
86 enumName *newelements;
88 s->nspace += enumNameListBASESIZE;
89 newelements = (enumName *) dmalloc (sizeof (*newelements) * (s->nelements + s->nspace));
91 if (newelements == (enumName *) 0)
93 llfatalerror (cstring_makeLiteral ("enumNameList_grow: out of memory!"));
96 for (i = 0; i < s->nelements; i++)
98 newelements[i] = s->elements[i];
101 sfree (s->elements);
102 s->elements = newelements;
105 void
106 enumNameList_addh (enumNameList s, /*@keep@*/ enumName el)
108 if (s->nspace <= 0)
109 enumNameList_grow (s);
111 s->nspace--;
112 s->elements[s->nelements] = el;
113 s->nelements++;
116 enumNameList
117 enumNameList_push (/*@returned@*/ enumNameList s, /*@only@*/ enumName el)
119 enumNameList_addh (s, el);
120 return s;
123 #ifdef DEADCODE
124 /*@only@*/ enumNameList
125 enumNameList_copy (enumNameList s)
127 enumNameList r = enumNameList_new ();
129 enumNameList_elements (s, x)
131 enumNameList_addh (r, cstring_copy (x));
132 } end_enumNameList_elements;
134 return r;
136 #endif
138 bool
139 enumNameList_member (enumNameList s, cstring m)
141 enumNameList_elements (s, x)
143 if (cstring_equal (m, x)) return TRUE;
144 } end_enumNameList_elements;
146 return FALSE;
149 /*@only@*/ enumNameList
150 enumNameList_subtract (enumNameList source, enumNameList del)
152 enumNameList ret = enumNameList_new ();
154 enumNameList_elements (source, el)
156 if (!enumNameList_member (del, el))
158 enumNameList_addh (ret, cstring_copy (el));
160 } end_enumNameList_elements;
162 return ret;
165 cstring
166 enumNameList_unparse (enumNameList s)
168 int i;
169 cstring st = cstring_undefined;
171 for (i = 0; i < s->nelements; i++)
173 if (i == 0)
175 st = cstring_copy (s->elements[i]);
177 else
179 st = message ("%q, %s", st, s->elements[i]);
183 return st;
186 cstring enumNameList_unparseBrief (enumNameList s)
188 int i;
189 cstring st = cstring_undefined;
191 for (i = 0; i < s->nelements; i++)
193 if (i == 0)
195 st = cstring_copy (s->elements[i]);
197 else if (i == 3 && s->nelements > 5)
199 st = message ("%q, ...", st);
200 i = s->nelements - 2;
202 else
204 st = message ("%q, %s", st, s->elements[i]);
208 return st;
211 /*@only@*/ cstring
212 enumNameList_dump (enumNameList s)
214 int i;
215 cstring st = cstring_undefined;
217 for (i = 0; i < s->nelements; i++)
219 if (i == 0)
221 st = cstring_copy (s->elements[i]);
223 else
224 st = message ("%q,%s", st, s->elements[i]);
226 return st;
229 /*@only@*/ enumNameList
230 enumNameList_undump (d_char *s)
232 enumNameList e = enumNameList_new ();
234 if (**s == '}')
235 (*s)++;
236 else
238 while (TRUE)
240 char *t = strchr (*s, ',');
241 char mt;
243 if (t == NULL)
245 t = strchr (*s, '}');
247 if (t == NULL)
249 llcontbug (message ("enumNameList_undump: bad line: %s", cstring_fromChars (*s)));
250 return e;
254 mt = *t;
255 *t = '\0';
257 enumNameList_addh (e, cstring_fromChars (mstring_copy (*s)));
258 *s = t + 1;
259 if (mt == '}')
260 break;
263 return e;
266 void
267 enumNameList_free (enumNameList s)
269 int i;
272 for (i = 0; i < s->nelements; i++)
274 cstring_free (s->elements[i]);
277 sfree (s->elements);
278 sfree (s);