2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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
27 ** based on list_template.c
29 ** where T has T_equal (or change this) and T_unparse
32 # include "splintMacros.nf"
38 return cstringList_undefined;
41 static /*@notnull@*/ cstringList
42 cstringList_newEmpty (void)
44 cstringList s = (cstringList) dmalloc (sizeof (*s));
47 s->nspace = cstringListBASESIZE;
48 s->elements = (cstring *) dmalloc (sizeof (*s->elements) * cstringListBASESIZE);
53 static /*@notnull@*/ cstringList
54 cstringList_newPredict (int size)
56 cstringList s = (cstringList) dmalloc (sizeof (*s));
60 s->elements = (cstring *) dmalloc (sizeof (*s->elements) * size);
66 cstringList_grow (/*@notnull@*/ cstringList s)
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];
88 s->elements = newelements;
91 cstringList cstringList_single (/*@keep@*/ cstring el)
93 cstringList s = cstringList_new ();
94 s = cstringList_add (s, el);
98 cstringList cstringList_add (cstringList s, /*@keep@*/ cstring el)
100 if (!cstringList_isDefined (s))
102 s = cstringList_newEmpty ();
107 cstringList_grow (s);
111 s->elements[s->nelements] = el;
117 cstringList cstringList_prepend (cstringList s, /*@keep@*/ cstring el)
121 DPRINTF (("Prepend: %s + %s",
122 cstringList_unparse (s), cstring_toCharsSafe (el)));
124 if (!cstringList_isDefined (s))
126 return cstringList_single (el);
131 cstringList_grow (s);
136 for (i = s->nelements; i > 0; i--)
138 s->elements[i] = s->elements [i - 1];
148 cstringList_unparse (cstringList s)
150 return cstringList_unparseSep (s, cstring_makeLiteralTemp (", "));
154 cstringList_unparseSep (cstringList s, cstring sep)
156 cstring st = cstring_undefined;
158 if (cstringList_isDefined (s))
162 for (i = 0; i < s->nelements; i++)
166 st = cstring_copy (s->elements[i]);
169 st = message ("%q%s%s", st, sep, s->elements[i]);
177 cstringList_printSpaced (cstringList s, size_t indent, size_t gap, int linelen)
179 if (cstringList_isDefined (s))
181 cstring line = cstring_undefined;
182 cstring istring = cstring_fill (cstring_undefined, indent);
183 cstring gstring = cstring_fill (cstring_undefined, gap);
189 ** find the longest string
192 for (i = 0; i < s->nelements; i++)
194 size_t len = cstring_length (s->elements[i]);
202 numcol = (linelen - indent) / (longest + gap);
209 for (i = 0; i < s->nelements; i++)
218 line = message ("%s%q", istring,
219 cstring_fill (s->elements[i], longest));
223 line = message ("%q%s%q", line, gstring,
224 cstring_fill (s->elements[i], longest));
229 cstring_free (istring);
230 cstring_free (gstring);
235 cstringList_unparseAbbrev (cstringList s)
237 cstring st = cstring_undefined;
239 if (cstringList_isDefined (s))
243 for (i = 0; i < s->nelements; i++)
247 st = cstring_copy (s->elements[i]);
249 else if (i > 3 && s->nelements > 5)
251 st = message ("%q, ...", st);
256 st = message ("%q, %s", st, s->elements[i]);
265 cstringList_free (cstringList s)
267 if (cstringList_isDefined (s))
271 DPRINTF (("cstringList free: [%p] %s",
272 s, cstringList_unparse (s)));
274 /* evans 2002-07-12: this was missing, not detected because of reldef */
275 for (i = 0; i < s->nelements; i++)
277 cstring_free (s->elements[i]);
286 cstringList_alphabetize (cstringList s)
288 if (cstringList_isDefined (s))
291 qsort (s->elements, (size_t) s->nelements,
292 sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare);
297 int cstringList_getIndex (cstringList s, cstring key)
301 cstringList_elements (s, el)
303 if (cstring_equal (el, key))
309 } end_cstringList_elements ;
314 bool cstringList_contains (cstringList s, cstring key)
318 cstringList_elements (s, el)
320 if (cstring_equal (el, key))
326 } end_cstringList_elements ;
331 cstringList cstringList_copy (cstringList s)
333 cstringList res = cstringList_newPredict (cstringList_size (s));
335 cstringList_elements (s, el)
337 res = cstringList_add (res, cstring_copy (el));
338 } end_cstringList_elements ;
344 cstringList_get (cstringList s, int index)
346 llassertretnull (s != NULL);
347 llassertretnull (index >= 0);
348 llassertretnull (index < s->nelements);
349 return s->elements[index];
353 cstringList_getElements (cstringList s)
355 if (cstringList_isDefined (s))
357 /*@i423@*/ return s->elements;