]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
c59f5181 | 3 | ** Copyright (C) 1994-2003 University of Virginia, |
616915dd | 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. | |
19 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | /* | |
25 | ** enumNameList.c | |
26 | ** | |
27 | ** based on list_template.c | |
28 | ** | |
29 | ** where T has T_equal (or change this) and T_unparse | |
30 | ** | |
31 | ** used to be cenum.c | |
32 | */ | |
33 | ||
1b8ae690 | 34 | # include "splintMacros.nf" |
616915dd | 35 | # include "basic.h" |
36 | ||
37 | enumNameList | |
38 | enumNameList_new () | |
39 | { | |
40 | enumNameList s = (enumNameList) dmalloc (sizeof (*s)); | |
41 | ||
42 | s->nelements = 0; | |
43 | s->nspace = enumNameListBASESIZE; | |
44 | s->elements = (enumName *) | |
45 | dmalloc (sizeof (*s->elements) * enumNameListBASESIZE); | |
46 | ||
47 | return (s); | |
48 | } | |
49 | ||
50 | /*@only@*/ enumNameList | |
51 | enumNameList_single (/*@keep@*/ enumName t) | |
52 | { | |
53 | enumNameList s = (enumNameList) dmalloc (sizeof (*s)); | |
54 | ||
55 | s->nelements = 1; | |
56 | s->nspace = enumNameListBASESIZE - 1; | |
57 | s->elements = (enumName *) dmalloc (sizeof (*s->elements) * enumNameListBASESIZE); | |
8fd556fb | 58 | s->elements[0] = t; |
616915dd | 59 | |
60 | return (s); | |
61 | } | |
62 | ||
63 | bool | |
64 | enumNameList_match (enumNameList e1, enumNameList e2) | |
65 | { | |
66 | int i; | |
67 | ||
68 | if (e1->nelements != e2->nelements) return FALSE; | |
69 | ||
70 | for (i = 0; i < e1->nelements; i++) | |
71 | { | |
8fd556fb | 72 | if (!cstring_equal (e1->elements[i], e2->elements[i])) |
616915dd | 73 | return FALSE; |
74 | } | |
75 | return TRUE; | |
76 | } | |
77 | ||
78 | static void | |
79 | enumNameList_grow (enumNameList s) | |
80 | { | |
81 | int i; | |
82 | enumName *newelements; | |
83 | ||
84 | s->nspace += enumNameListBASESIZE; | |
85 | newelements = (enumName *) dmalloc (sizeof (*newelements) * (s->nelements + s->nspace)); | |
86 | ||
87 | if (newelements == (enumName *) 0) | |
88 | { | |
89 | llfatalerror (cstring_makeLiteral ("enumNameList_grow: out of memory!")); | |
90 | } | |
91 | ||
92 | for (i = 0; i < s->nelements; i++) | |
93 | { | |
8fd556fb | 94 | newelements[i] = s->elements[i]; |
616915dd | 95 | } |
96 | ||
97 | sfree (s->elements); | |
98 | s->elements = newelements; | |
99 | } | |
100 | ||
101 | void | |
102 | enumNameList_addh (enumNameList s, /*@keep@*/ enumName el) | |
103 | { | |
104 | if (s->nspace <= 0) | |
105 | enumNameList_grow (s); | |
106 | ||
107 | s->nspace--; | |
8fd556fb | 108 | s->elements[s->nelements] = el; |
616915dd | 109 | s->nelements++; |
110 | } | |
111 | ||
112 | enumNameList | |
113 | enumNameList_push (/*@returned@*/ enumNameList s, /*@only@*/ enumName el) | |
114 | { | |
115 | enumNameList_addh (s, el); | |
116 | return s; | |
117 | } | |
118 | ||
119 | /*@only@*/ enumNameList | |
120 | enumNameList_copy (enumNameList s) | |
121 | { | |
122 | enumNameList r = enumNameList_new (); | |
123 | ||
124 | enumNameList_elements (s, x) | |
125 | { | |
126 | enumNameList_addh (r, cstring_copy (x)); | |
127 | } end_enumNameList_elements; | |
128 | ||
129 | return r; | |
130 | } | |
131 | ||
132 | bool | |
133 | enumNameList_member (enumNameList s, cstring m) | |
134 | { | |
135 | enumNameList_elements (s, x) | |
136 | { | |
137 | if (cstring_equal (m, x)) return TRUE; | |
138 | } end_enumNameList_elements; | |
139 | ||
140 | return FALSE; | |
141 | } | |
142 | ||
143 | /*@only@*/ enumNameList | |
144 | enumNameList_subtract (enumNameList source, enumNameList del) | |
145 | { | |
146 | enumNameList ret = enumNameList_new (); | |
147 | ||
148 | enumNameList_elements (source, el) | |
149 | { | |
150 | if (!enumNameList_member (del, el)) | |
151 | { | |
152 | enumNameList_addh (ret, cstring_copy (el)); | |
153 | } | |
154 | } end_enumNameList_elements; | |
155 | ||
156 | return ret; | |
157 | } | |
158 | ||
159 | cstring | |
160 | enumNameList_unparse (enumNameList s) | |
161 | { | |
162 | int i; | |
163 | cstring st = cstring_undefined; | |
164 | ||
165 | for (i = 0; i < s->nelements; i++) | |
166 | { | |
167 | if (i == 0) | |
168 | { | |
8fd556fb | 169 | st = cstring_copy (s->elements[i]); |
616915dd | 170 | } |
171 | else | |
172 | { | |
8fd556fb | 173 | st = message ("%q, %s", st, s->elements[i]); |
616915dd | 174 | } |
175 | } | |
176 | ||
177 | return st; | |
178 | } | |
179 | ||
180 | cstring enumNameList_unparseBrief (enumNameList s) | |
181 | { | |
182 | int i; | |
183 | cstring st = cstring_undefined; | |
184 | ||
185 | for (i = 0; i < s->nelements; i++) | |
186 | { | |
187 | if (i == 0) | |
188 | { | |
8fd556fb | 189 | st = cstring_copy (s->elements[i]); |
616915dd | 190 | } |
191 | else if (i == 3 && s->nelements > 5) | |
192 | { | |
193 | st = message ("%q, ...", st); | |
194 | i = s->nelements - 2; | |
195 | } | |
196 | else | |
197 | { | |
8fd556fb | 198 | st = message ("%q, %s", st, s->elements[i]); |
616915dd | 199 | } |
200 | } | |
201 | ||
202 | return st; | |
203 | } | |
204 | ||
205 | /*@only@*/ cstring | |
206 | enumNameList_dump (enumNameList s) | |
207 | { | |
208 | int i; | |
209 | cstring st = cstring_undefined; | |
210 | ||
211 | for (i = 0; i < s->nelements; i++) | |
212 | { | |
213 | if (i == 0) | |
214 | { | |
8fd556fb | 215 | st = cstring_copy (s->elements[i]); |
616915dd | 216 | } |
217 | else | |
8fd556fb | 218 | st = message ("%q,%s", st, s->elements[i]); |
616915dd | 219 | } |
220 | return st; | |
221 | } | |
222 | ||
223 | /*@only@*/ enumNameList | |
224 | enumNameList_undump (d_char *s) | |
225 | { | |
226 | enumNameList e = enumNameList_new (); | |
227 | ||
228 | if (**s == '}') | |
229 | (*s)++; | |
230 | else | |
231 | { | |
232 | while (TRUE) | |
233 | { | |
234 | char *t = strchr (*s, ','); | |
235 | char mt; | |
236 | ||
237 | if (t == NULL) | |
238 | { | |
239 | t = strchr (*s, '}'); | |
240 | ||
241 | if (t == NULL) | |
242 | { | |
243 | llcontbug (message ("enumNameList_undump: bad line: %s", cstring_fromChars (*s))); | |
244 | return e; | |
245 | } | |
246 | } | |
247 | ||
248 | mt = *t; | |
249 | *t = '\0'; | |
250 | ||
251 | enumNameList_addh (e, cstring_fromChars (mstring_copy (*s))); | |
252 | *s = t + 1; | |
253 | if (mt == '}') | |
254 | break; | |
255 | } | |
256 | } | |
257 | return e; | |
258 | } | |
259 | ||
260 | void | |
261 | enumNameList_free (enumNameList s) | |
262 | { | |
263 | int i; | |
264 | ||
265 | ||
266 | for (i = 0; i < s->nelements; i++) | |
267 | { | |
268 | cstring_free (s->elements[i]); | |
269 | } | |
270 | ||
271 | sfree (s->elements); | |
272 | sfree (s); | |
273 | } | |
274 | ||
275 |