Various minor fixes for compiler/linter (other then splint itself) warnings.
[splint-patched.git] / src / cstringList.c
blob82748fd95933e49a4e1f8d6807c34c4b3264e5f2
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 ** cstringList.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 cstringList
36 cstringList_new (void)
38 return cstringList_undefined;
41 static /*@notnull@*/ cstringList
42 cstringList_newEmpty (void)
44 cstringList s = (cstringList) dmalloc (sizeof (*s));
46 s->nelements = 0;
47 s->nspace = cstringListBASESIZE;
48 s->elements = (cstring *) dmalloc (sizeof (*s->elements) * cstringListBASESIZE);
50 return (s);
53 static /*@notnull@*/ cstringList
54 cstringList_newPredict (int size)
56 cstringList s = (cstringList) dmalloc (sizeof (*s));
58 s->nelements = 0;
59 s->nspace = size;
60 s->elements = (cstring *) dmalloc (sizeof (*s->elements) * size);
62 return (s);
65 static void
66 cstringList_grow (/*@notnull@*/ cstringList s)
68 int i;
69 cstring *newelements;
71 s->nspace += cstringListBASESIZE;
73 newelements = (cstring *) dmalloc (sizeof (*newelements)
74 * (s->nelements + s->nspace));
77 if (newelements == (cstring *) 0)
79 llfatalerror (cstring_makeLiteral ("cstringList_grow: out of memory!"));
82 for (i = 0; i < s->nelements; i++)
84 newelements[i] = s->elements[i];
87 sfree (s->elements);
88 s->elements = newelements;
91 cstringList cstringList_single (/*@keep@*/ cstring el)
93 cstringList s = cstringList_new ();
94 s = cstringList_add (s, el);
95 return s;
98 cstringList cstringList_add (cstringList s, /*@keep@*/ cstring el)
100 if (!cstringList_isDefined (s))
102 s = cstringList_newEmpty ();
105 if (s->nspace <= 0)
107 cstringList_grow (s);
110 s->nspace--;
111 s->elements[s->nelements] = el;
112 s->nelements++;
114 return s;
117 cstringList cstringList_prepend (cstringList s, /*@keep@*/ cstring el)
119 int i;
121 DPRINTF (("Prepend: %s + %s",
122 cstringList_unparse (s), cstring_toCharsSafe (el)));
124 if (!cstringList_isDefined (s))
126 return cstringList_single (el);
129 if (s->nspace <= 0)
131 cstringList_grow (s);
134 s->nspace--;
136 for (i = s->nelements; i > 0; i--)
138 s->elements[i] = s->elements [i - 1];
141 s->elements[0] = el;
142 s->nelements++;
144 return s;
147 cstring
148 cstringList_unparse (cstringList s)
150 return cstringList_unparseSep (s, cstring_makeLiteralTemp (", "));
153 cstring
154 cstringList_unparseSep (cstringList s, cstring sep)
156 cstring st = cstring_undefined;
158 if (cstringList_isDefined (s))
160 int i;
162 for (i = 0; i < s->nelements; i++)
164 if (i == 0)
166 st = cstring_copy (s->elements[i]);
168 else
169 st = message ("%q%s%s", st, sep, s->elements[i]);
173 return st;
176 # ifdef DEADCODE
177 void
178 cstringList_printSpaced (cstringList s, size_t indent, size_t gap, int linelen)
180 if (cstringList_isDefined (s))
182 cstring line = cstring_undefined;
183 cstring istring = cstring_fill (cstring_undefined, indent);
184 cstring gstring = cstring_fill (cstring_undefined, gap);
185 int numcol;
186 size_t longest = 0;
187 int i;
190 ** find the longest string
193 for (i = 0; i < s->nelements; i++)
195 size_t len = cstring_length (s->elements[i]);
197 if (len > longest)
199 longest = len;
203 numcol = size_toInt ((linelen - indent) / (longest + gap));
205 if (numcol <= 1)
207 numcol = 1;
210 for (i = 0; i < s->nelements; i++)
212 if (i % numcol == 0)
214 if (i != 0)
216 llmsg (line);
219 line = message ("%s%q", istring,
220 cstring_fill (s->elements[i], longest));
222 else
224 line = message ("%q%s%q", line, gstring,
225 cstring_fill (s->elements[i], longest));
229 cstring_free (line);
230 cstring_free (istring);
231 cstring_free (gstring);
235 /*@only@*/ cstring
236 cstringList_unparseAbbrev (cstringList s)
238 cstring st = cstring_undefined;
240 if (cstringList_isDefined (s))
242 int i;
244 for (i = 0; i < s->nelements; i++)
246 if (i == 0)
248 st = cstring_copy (s->elements[i]);
250 else if (i > 3 && s->nelements > 5)
252 st = message ("%q, ...", st);
253 break;
255 else
257 st = message ("%q, %s", st, s->elements[i]);
262 return st;
264 # endif /* DEADCODE */
266 void
267 cstringList_free (cstringList s)
269 if (cstringList_isDefined (s))
271 int i;
273 DPRINTF (("cstringList free: [%p] %s",
274 s, cstringList_unparse (s)));
276 /* evans 2002-07-12: this was missing, not detected because of reldef */
277 for (i = 0; i < s->nelements; i++)
279 cstring_free (s->elements[i]);
282 sfree (s->elements);
283 sfree (s);
287 # ifdef DEADCODE
288 void
289 cstringList_alphabetize (cstringList s)
291 if (cstringList_isDefined (s))
293 /*@-modobserver@*/
294 qsort (s->elements, (size_t) s->nelements,
295 sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare);
296 /*@=modobserver@*/
299 # endif /* DEADCODE */
301 int cstringList_getIndex (cstringList s, cstring key)
303 int index = 0;
305 cstringList_elements (s, el)
307 if (cstring_equal (el, key))
309 return index;
312 index++;
313 } end_cstringList_elements ;
315 BADBRANCHRET (0);
318 bool cstringList_contains (cstringList s, cstring key)
320 int index = 0;
322 cstringList_elements (s, el)
324 if (cstring_equal (el, key))
326 return TRUE;
329 index++;
330 } end_cstringList_elements ;
332 return FALSE;
335 cstringList cstringList_copy (cstringList s)
337 cstringList res = cstringList_newPredict (cstringList_size (s));
339 cstringList_elements (s, el)
341 res = cstringList_add (res, cstring_copy (el));
342 } end_cstringList_elements ;
344 return res;
347 cstring
348 cstringList_get (cstringList s, int index)
350 llassertretnull (s != NULL);
351 llassertretnull (index >= 0);
352 llassertretnull (index < s->nelements);
353 return s->elements[index];
356 ob_cstring *
357 cstringList_getElements (cstringList s)
359 if (cstringList_isDefined (s))
361 /*@-compmempass@*/
362 return s->elements;
363 /*@=compmempass@*/ /* This is exposed */
365 else
367 return NULL;