]> andersk Git - splint.git/blob - src/cstringList.c
Fixed all /*@i...@*/ tags (except 1).
[splint.git] / src / cstringList.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 ** cstringList.c
26 **
27 ** based on list_template.c
28 **
29 ** where T has T_equal (or change this) and T_unparse
30 */
31
32 # include "splintMacros.nf"
33 # include "basic.h"
34
35 cstringList
36 cstringList_new ()
37 {
38   return cstringList_undefined;
39 }
40
41 static /*@notnull@*/ cstringList
42 cstringList_newEmpty (void)
43 {
44   cstringList s = (cstringList) dmalloc (sizeof (*s));
45   
46   s->nelements = 0;
47   s->nspace = cstringListBASESIZE; 
48   s->elements = (cstring *) dmalloc (sizeof (*s->elements) * cstringListBASESIZE);
49
50   return (s);
51 }
52
53 static /*@notnull@*/ cstringList
54 cstringList_newPredict (int size)
55 {
56   cstringList s = (cstringList) dmalloc (sizeof (*s));
57   
58   s->nelements = 0;
59   s->nspace = size; 
60   s->elements = (cstring *) dmalloc (sizeof (*s->elements) * size);
61
62   return (s);
63 }
64
65 static void
66 cstringList_grow (/*@notnull@*/ cstringList s)
67 {
68   int i;
69   cstring *newelements;
70   
71   s->nspace += cstringListBASESIZE;
72   
73   newelements = (cstring *) dmalloc (sizeof (*newelements) 
74                                      * (s->nelements + s->nspace));
75   
76
77   if (newelements == (cstring *) 0)
78     {
79       llfatalerror (cstring_makeLiteral ("cstringList_grow: out of memory!"));
80     }
81
82   for (i = 0; i < s->nelements; i++)
83     {
84       newelements[i] = s->elements[i];
85     }
86   
87   sfree (s->elements); 
88   s->elements = newelements;
89 }
90
91 cstringList cstringList_single (/*@keep@*/ cstring el) 
92 {
93   cstringList s = cstringList_new ();
94   s = cstringList_add (s, el);
95   return s;
96 }
97
98 cstringList cstringList_add (cstringList s, /*@keep@*/ cstring el)
99 {
100   if (!cstringList_isDefined (s))
101     {
102       s = cstringList_newEmpty ();
103     }
104
105   if (s->nspace <= 0)
106     {
107       cstringList_grow (s);
108     }
109   
110   s->nspace--;
111   s->elements[s->nelements] = el;
112   s->nelements++;
113
114   return s;
115 }
116
117 cstringList cstringList_prepend (cstringList s, /*@keep@*/ cstring el)
118 {
119   int i;
120
121   DPRINTF (("Prepend:  %s + %s",
122             cstringList_unparse (s), cstring_toCharsSafe (el)));
123
124   if (!cstringList_isDefined (s))
125     {
126       return cstringList_single (el);
127     }
128
129   if (s->nspace <= 0)
130     {
131       cstringList_grow (s);
132     }
133   
134   s->nspace--;
135
136   for (i = s->nelements; i > 0; i--) 
137     {
138       s->elements[i] = s->elements [i - 1];
139     }
140
141   s->elements[0] = el;
142   s->nelements++;
143
144   return s;
145 }
146
147 cstring
148 cstringList_unparse (cstringList s)
149 {
150   return cstringList_unparseSep (s, cstring_makeLiteralTemp (", "));
151 }
152
153 cstring
154 cstringList_unparseSep (cstringList s, cstring sep)
155 {
156    cstring st = cstring_undefined;
157
158    if (cstringList_isDefined (s))
159      {
160        int i;
161
162        for (i = 0; i < s->nelements; i++)
163          {
164            if (i == 0)
165              {
166                st = cstring_copy (s->elements[i]);
167              }
168            else
169              st = message ("%q%s%s", st, sep, s->elements[i]);
170          }
171      }
172
173    return st;
174 }
175
176 void
177 cstringList_printSpaced (cstringList s, size_t indent, size_t gap, int linelen)
178 {
179   if (cstringList_isDefined (s))
180     {
181       cstring line = cstring_undefined;
182       cstring istring = cstring_fill (cstring_undefined, indent);
183       cstring gstring = cstring_fill (cstring_undefined, gap);
184       int numcol;
185       size_t longest = 0;
186       int i;
187  
188       /*
189       ** find the longest string
190       */
191
192       for (i = 0; i < s->nelements; i++)
193         {
194           size_t len = cstring_length (s->elements[i]);
195
196           if (len > longest)
197             {
198               longest = len;
199             }
200         }
201
202       numcol = size_toInt ((linelen - indent) / (longest + gap));
203       
204       if (numcol <= 1) 
205         {
206           numcol = 1;
207         }
208
209       for (i = 0; i < s->nelements; i++)
210         {
211           if (i % numcol == 0)
212             {
213               if (i != 0)
214                 {
215                   llmsg (line);
216                 }
217               
218               line = message ("%s%q", istring,
219                               cstring_fill (s->elements[i], longest));
220             }
221           else
222             {
223               line = message ("%q%s%q", line, gstring, 
224                               cstring_fill (s->elements[i], longest));
225             }
226         }
227
228       cstring_free (line);
229       cstring_free (istring);
230       cstring_free (gstring);
231     }
232 }
233
234 /*@only@*/ cstring
235 cstringList_unparseAbbrev (cstringList s)
236 {
237    cstring st = cstring_undefined;
238
239    if (cstringList_isDefined (s))
240      {
241        int i;
242        
243        for (i = 0; i < s->nelements; i++)
244          {
245            if (i == 0)
246              {
247                st = cstring_copy (s->elements[i]);
248              }
249            else if (i > 3 && s->nelements > 5)
250              {
251                st = message ("%q, ...", st);
252                break;
253              }
254            else
255              {
256                st = message ("%q, %s", st, s->elements[i]);
257              }
258          }
259      }
260
261    return st;
262 }
263
264 void
265 cstringList_free (cstringList s)
266 {
267   if (cstringList_isDefined (s))
268     {
269       int i;
270
271       DPRINTF (("cstringList free: [%p] %s",
272                 s, cstringList_unparse (s)));
273
274       /* evans 2002-07-12: this was missing, not detected because of reldef */
275       for (i = 0; i < s->nelements; i++)
276         {
277           cstring_free (s->elements[i]);
278         }
279
280       sfree (s->elements);
281       sfree (s);
282     }
283 }
284
285 void
286 cstringList_alphabetize (cstringList s)
287 {
288   if (cstringList_isDefined (s))
289     {
290       /*@-modobserver@*/
291       qsort (s->elements, (size_t) s->nelements, 
292              sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare);
293       /*@=modobserver@*/
294     }
295 }
296
297 int cstringList_getIndex (cstringList s, cstring key)
298 {
299   int index = 0;
300
301   cstringList_elements (s, el) 
302     {
303       if (cstring_equal (el, key))
304         {
305           return index;
306         }
307
308       index++;
309     } end_cstringList_elements ;
310
311   BADBRANCHRET (0);
312 }
313
314 bool cstringList_contains (cstringList s, cstring key)
315 {
316   int index = 0;
317
318   cstringList_elements (s, el) 
319     {
320       if (cstring_equal (el, key))
321         {
322           return TRUE;
323         }
324
325       index++;
326     } end_cstringList_elements ;
327
328   return FALSE;
329 }
330
331 cstringList cstringList_copy (cstringList s)
332 {
333   cstringList res = cstringList_newPredict (cstringList_size (s));
334
335   cstringList_elements (s, el)
336     {
337       res = cstringList_add (res, cstring_copy (el));
338     } end_cstringList_elements ;
339
340   return res;
341 }
342
343 cstring
344 cstringList_get (cstringList s, int index)
345 {
346   llassertretnull (s != NULL);
347   llassertretnull (index >= 0);
348   llassertretnull (index < s->nelements);
349   return s->elements[index];
350 }
351
352 ob_cstring *
353 cstringList_getElements (cstringList s)
354 {
355   if (cstringList_isDefined (s))
356     {
357       /*@-compmempass@*/
358       return s->elements;
359       /*@=compmempass@*/ /* This is exposed */
360     }
361   else
362     {
363       return NULL;
364     }
365 }
This page took 0.208532 seconds and 5 git commands to generate.