]> andersk Git - splint.git/blame - src/cstringList.c
Fixed line numbering when multi-line macro parameters are used.
[splint.git] / src / cstringList.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
77d37419 3** Copyright (C) 1994-2002 University of Virginia,
616915dd 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**
155af98d 20** For information on splint: info@splint.org
21** To report a bug: splint-bug@splint.org
11db3170 22** For more information: http://www.splint.org
616915dd 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
1b8ae690 32# include "splintMacros.nf"
616915dd 33# include "basic.h"
34
35cstringList
36cstringList_new ()
37{
38 return cstringList_undefined;
39}
40
41static /*@notnull@*/ cstringList
42cstringList_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
28bf4b0b 53static /*@notnull@*/ cstringList
54cstringList_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
616915dd 65static void
66cstringList_grow (/*@notnull@*/ cstringList s)
67{
68 int i;
69 cstring *newelements;
70
71 s->nspace += cstringListBASESIZE;
28bf4b0b 72
616915dd 73 newelements = (cstring *) dmalloc (sizeof (*newelements)
74 * (s->nelements + s->nspace));
28bf4b0b 75
616915dd 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
91cstringList cstringList_single (/*@keep@*/ cstring el)
92{
93 cstringList s = cstringList_new ();
94 s = cstringList_add (s, el);
95 return s;
96}
97
98cstringList 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
28bf4b0b 117cstringList 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
616915dd 147cstring
148cstringList_unparse (cstringList s)
149{
150 return cstringList_unparseSep (s, cstring_makeLiteralTemp (", "));
151}
152
153cstring
154cstringList_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
176void
abd7f895 177cstringList_printSpaced (cstringList s, size_t indent, size_t gap, int linelen)
616915dd 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;
abd7f895 185 size_t longest = 0;
616915dd 186 int i;
187
188 /*
189 ** find the longest string
190 */
191
192 for (i = 0; i < s->nelements; i++)
193 {
abd7f895 194 size_t len = cstring_length (s->elements[i]);
616915dd 195
196 if (len > longest)
197 {
198 longest = len;
199 }
200 }
201
202 numcol = (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
235cstringList_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
264void
265cstringList_free (cstringList s)
266{
267 if (cstringList_isDefined (s))
268 {
28bf4b0b 269 DPRINTF (("cstringList free: [%p] %s",
270 s, cstringList_unparse (s)));
616915dd 271 sfree (s->elements);
272 sfree (s);
273 }
274}
275
276void
277cstringList_alphabetize (cstringList s)
278{
279 if (cstringList_isDefined (s))
280 {
281 /*@-modobserver@*/
282 qsort (s->elements, (size_t) s->nelements,
283 sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare);
284 /*@=modobserver@*/
285 }
286}
287
28bf4b0b 288int cstringList_getIndex (cstringList s, cstring key)
289{
290 int index = 0;
291
292 cstringList_elements (s, el)
293 {
294 if (cstring_equal (el, key))
295 {
296 return index;
297 }
298
299 index++;
300 } end_cstringList_elements ;
301
8250fa4a 302 BADBRANCHRET (0);
28bf4b0b 303}
304
305bool cstringList_contains (cstringList s, cstring key)
306{
307 int index = 0;
308
309 cstringList_elements (s, el)
310 {
311 if (cstring_equal (el, key))
312 {
313 return TRUE;
314 }
315
316 index++;
317 } end_cstringList_elements ;
318
319 return FALSE;
320}
321
322cstringList cstringList_copy (cstringList s)
323{
324 cstringList res = cstringList_newPredict (cstringList_size (s));
325
326 cstringList_elements (s, el)
327 {
328 res = cstringList_add (res, cstring_copy (el));
329 } end_cstringList_elements ;
330
331 return res;
332}
333
334cstring
335cstringList_get (cstringList s, int index)
336{
337 llassertretnull (s != NULL);
338 llassertretnull (index >= 0);
339 llassertretnull (index < s->nelements);
340 return s->elements[index];
341}
This page took 0.102725 seconds and 5 git commands to generate.