]> andersk Git - splint.git/blob - src/enumNameList.c
7d32aae9bff0174ba16d59eb8c2d21b1cc3336ca
[splint.git] / src / enumNameList.c
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.
19 **
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
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
34 # include "splintMacros.nf"
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);
58         /*drl bee: dm*/
59   s->elements[0] = t;
60
61   return (s);
62 }
63
64 bool
65 enumNameList_match (enumNameList e1, enumNameList e2)
66 {
67   int i;
68
69   if (e1->nelements != e2->nelements) return FALSE;
70
71   for (i = 0; i < e1->nelements; i++)
72     {
73             /*drl bee: si*/
74             /*drl bee: si*/
75       if (!cstring_equal (e1->elements[i], e2->elements[i]))
76         return FALSE;
77     }
78   return TRUE;
79 }
80
81 static void
82 enumNameList_grow (enumNameList s)
83 {
84   int i;
85   enumName *newelements;
86
87   s->nspace += enumNameListBASESIZE;
88   newelements = (enumName *) dmalloc (sizeof (*newelements) * (s->nelements + s->nspace));
89
90   if (newelements == (enumName *) 0)
91     {
92       llfatalerror (cstring_makeLiteral ("enumNameList_grow: out of memory!"));
93     }
94
95   for (i = 0; i < s->nelements; i++)
96     {
97             /*drl bee: si*/
98             /*drl bee: si*/
99       newelements[i] = s->elements[i];
100     }
101
102   sfree (s->elements);
103   s->elements = newelements;
104 }
105
106 void 
107 enumNameList_addh (enumNameList s, /*@keep@*/ enumName el)
108 {
109   if (s->nspace <= 0)
110     enumNameList_grow (s);
111
112   s->nspace--;
113   /*drl bee: si*/
114   s->elements[s->nelements] = el;
115   s->nelements++;
116 }
117
118 enumNameList 
119 enumNameList_push (/*@returned@*/ enumNameList s, /*@only@*/ enumName el)
120 {
121   enumNameList_addh (s, el);
122   return s;
123 }
124
125 /*@only@*/ enumNameList 
126 enumNameList_copy (enumNameList s)
127 {
128   enumNameList r = enumNameList_new ();
129
130   enumNameList_elements (s, x)
131   {
132     enumNameList_addh (r, cstring_copy (x));
133   } end_enumNameList_elements;
134
135   return r;
136 }
137
138 bool
139 enumNameList_member (enumNameList s, cstring m)
140 {
141   enumNameList_elements (s, x)
142   {
143     if (cstring_equal (m, x)) return TRUE;
144   } end_enumNameList_elements;
145
146   return FALSE;
147 }
148
149 /*@only@*/ enumNameList
150 enumNameList_subtract (enumNameList source, enumNameList del)
151 {
152   enumNameList ret = enumNameList_new ();
153
154   enumNameList_elements (source, el)
155     {
156       if (!enumNameList_member (del, el))
157         {
158           enumNameList_addh (ret, cstring_copy (el));
159         }
160     } end_enumNameList_elements;
161
162   return ret;
163 }
164
165 cstring
166 enumNameList_unparse (enumNameList s)
167 {
168   int i;
169   cstring st = cstring_undefined;
170
171   for (i = 0; i < s->nelements; i++)
172     {
173       if (i == 0) 
174         {
175                 /*drl bee: si*/
176           st = cstring_copy (s->elements[i]);
177         }
178       else
179         {
180                 /*drl bee: si*/
181           st = message ("%q, %s", st, s->elements[i]);
182         }
183     }
184
185   return st;
186 }
187
188 cstring enumNameList_unparseBrief (enumNameList s)
189 {
190   int i;
191   cstring st = cstring_undefined;
192
193   for (i = 0; i < s->nelements; i++)
194     {
195       if (i == 0)
196         {
197                 /*drl bee: si*/
198           st = cstring_copy (s->elements[i]);
199         }
200       else if (i == 3 && s->nelements > 5)
201         {
202           st = message ("%q, ...", st);
203           i = s->nelements - 2;
204         }
205       else
206         {
207                 /*drl bee: si*/
208           st = message ("%q, %s", st, s->elements[i]);
209         }
210     }
211   
212   return st;
213 }
214
215 /*@only@*/ cstring
216 enumNameList_dump (enumNameList s)
217 {
218   int i;
219   cstring st = cstring_undefined;
220
221   for (i = 0; i < s->nelements; i++)
222     {
223       if (i == 0)
224         {
225                 /*drl bee: si*/
226           st = cstring_copy (s->elements[i]);
227         }
228       else
229               /*drl bee: si*/
230         st = message ("%q,%s", st, s->elements[i]);
231     }
232   return st;
233 }
234
235 /*@only@*/ enumNameList 
236 enumNameList_undump (d_char *s)
237 {
238   enumNameList e = enumNameList_new ();
239
240   if (**s == '}')
241     (*s)++;
242   else
243     {
244       while (TRUE)
245         {
246           char *t = strchr (*s, ',');
247           char mt;
248
249           if (t == NULL)
250             {
251               t = strchr (*s, '}');
252
253               if (t == NULL)
254                 {
255                   llcontbug (message ("enumNameList_undump: bad line: %s", cstring_fromChars (*s)));
256                   return e; 
257                 }
258             }
259           
260           mt = *t;
261           *t = '\0';
262           
263           enumNameList_addh (e, cstring_fromChars (mstring_copy (*s)));
264           *s = t + 1;
265           if (mt == '}')
266             break;
267         }
268     }
269   return e;
270 }
271
272 void
273 enumNameList_free (enumNameList s)
274 {
275   int i;
276
277   
278   for (i = 0; i < s->nelements; i++)
279     {
280       cstring_free (s->elements[i]); 
281     }
282
283   sfree (s->elements); 
284   sfree (s);
285 }
286
287
This page took 0.062703 seconds and 3 git commands to generate.