2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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"
36 cstringList_new (void)
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
]);
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
);
190 ** find the longest string
193 for (i
= 0; i
< s
->nelements
; i
++)
195 size_t len
= cstring_length (s
->elements
[i
]);
203 numcol
= size_toInt ((linelen
- indent
) / (longest
+ gap
));
210 for (i
= 0; i
< s
->nelements
; i
++)
219 line
= message ("%s%q", istring
,
220 cstring_fill (s
->elements
[i
], longest
));
224 line
= message ("%q%s%q", line
, gstring
,
225 cstring_fill (s
->elements
[i
], longest
));
230 cstring_free (istring
);
231 cstring_free (gstring
);
236 cstringList_unparseAbbrev (cstringList s
)
238 cstring st
= cstring_undefined
;
240 if (cstringList_isDefined (s
))
244 for (i
= 0; i
< s
->nelements
; i
++)
248 st
= cstring_copy (s
->elements
[i
]);
250 else if (i
> 3 && s
->nelements
> 5)
252 st
= message ("%q, ...", st
);
257 st
= message ("%q, %s", st
, s
->elements
[i
]);
264 # endif /* DEADCODE */
267 cstringList_free (cstringList s
)
269 if (cstringList_isDefined (s
))
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
]);
289 cstringList_alphabetize (cstringList s
)
291 if (cstringList_isDefined (s
))
294 qsort (s
->elements
, (size_t) s
->nelements
,
295 sizeof (*s
->elements
), (int (*)(const void *, const void *)) cstring_xcompare
);
299 # endif /* DEADCODE */
301 int cstringList_getIndex (cstringList s
, cstring key
)
305 cstringList_elements (s
, el
)
307 if (cstring_equal (el
, key
))
313 } end_cstringList_elements
;
318 bool cstringList_contains (cstringList s
, cstring key
)
322 cstringList_elements (s
, el
)
324 if (cstring_equal (el
, key
))
330 } end_cstringList_elements
;
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
;
348 cstringList_get (cstringList s
, int index
)
350 llassertretval (s
!= NULL
, cstring_undefined
);
351 llassertretval (index
>= 0, cstring_undefined
);
352 llassertretval (index
< s
->nelements
, cstring_undefined
);
353 return s
->elements
[index
];
357 cstringList_getElements (cstringList s
)
359 if (cstringList_isDefined (s
))
363 /*@=compmempass@*/ /* This is exposed */