]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ** LCLint - annotation-assisted static program checker | |
3 | ** Copyright (C) 1994-2000 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 lclint: lclint-request@cs.virginia.edu | |
21 | ** To report a bug: lclint-bug@cs.virginia.edu | |
22 | ** For more information: http://lclint.cs.virginia.edu | |
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 "lclintMacros.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 void | |
54 | cstringList_grow (/*@notnull@*/ cstringList s) | |
55 | { | |
56 | int i; | |
57 | cstring *newelements; | |
58 | ||
59 | s->nspace += cstringListBASESIZE; | |
60 | ||
61 | newelements = (cstring *) dmalloc (sizeof (*newelements) | |
62 | * (s->nelements + s->nspace)); | |
63 | ||
64 | ||
65 | if (newelements == (cstring *) 0) | |
66 | { | |
67 | llfatalerror (cstring_makeLiteral ("cstringList_grow: out of memory!")); | |
68 | } | |
69 | ||
70 | for (i = 0; i < s->nelements; i++) | |
71 | { | |
72 | newelements[i] = s->elements[i]; | |
73 | } | |
74 | ||
75 | sfree (s->elements); | |
76 | s->elements = newelements; | |
77 | } | |
78 | ||
79 | cstringList cstringList_single (/*@keep@*/ cstring el) | |
80 | { | |
81 | cstringList s = cstringList_new (); | |
82 | s = cstringList_add (s, el); | |
83 | return s; | |
84 | } | |
85 | ||
86 | cstringList cstringList_add (cstringList s, /*@keep@*/ cstring el) | |
87 | { | |
88 | if (!cstringList_isDefined (s)) | |
89 | { | |
90 | s = cstringList_newEmpty (); | |
91 | } | |
92 | ||
93 | if (s->nspace <= 0) | |
94 | { | |
95 | cstringList_grow (s); | |
96 | } | |
97 | ||
98 | s->nspace--; | |
99 | s->elements[s->nelements] = el; | |
100 | s->nelements++; | |
101 | ||
102 | return s; | |
103 | } | |
104 | ||
105 | cstring | |
106 | cstringList_unparse (cstringList s) | |
107 | { | |
108 | return cstringList_unparseSep (s, cstring_makeLiteralTemp (", ")); | |
109 | } | |
110 | ||
111 | cstring | |
112 | cstringList_unparseSep (cstringList s, cstring sep) | |
113 | { | |
114 | cstring st = cstring_undefined; | |
115 | ||
116 | if (cstringList_isDefined (s)) | |
117 | { | |
118 | int i; | |
119 | ||
120 | for (i = 0; i < s->nelements; i++) | |
121 | { | |
122 | if (i == 0) | |
123 | { | |
124 | st = cstring_copy (s->elements[i]); | |
125 | } | |
126 | else | |
127 | st = message ("%q%s%s", st, sep, s->elements[i]); | |
128 | } | |
129 | } | |
130 | ||
131 | return st; | |
132 | } | |
133 | ||
134 | void | |
135 | cstringList_printSpaced (cstringList s, int indent, int gap, int linelen) | |
136 | { | |
137 | if (cstringList_isDefined (s)) | |
138 | { | |
139 | cstring line = cstring_undefined; | |
140 | cstring istring = cstring_fill (cstring_undefined, indent); | |
141 | cstring gstring = cstring_fill (cstring_undefined, gap); | |
142 | int numcol; | |
143 | int longest = 0; | |
144 | int i; | |
145 | ||
146 | /* | |
147 | ** find the longest string | |
148 | */ | |
149 | ||
150 | for (i = 0; i < s->nelements; i++) | |
151 | { | |
152 | int len = cstring_length (s->elements[i]); | |
153 | ||
154 | if (len > longest) | |
155 | { | |
156 | longest = len; | |
157 | } | |
158 | } | |
159 | ||
160 | numcol = (linelen - indent) / (longest + gap); | |
161 | ||
162 | if (numcol <= 1) | |
163 | { | |
164 | numcol = 1; | |
165 | } | |
166 | ||
167 | for (i = 0; i < s->nelements; i++) | |
168 | { | |
169 | if (i % numcol == 0) | |
170 | { | |
171 | if (i != 0) | |
172 | { | |
173 | llmsg (line); | |
174 | } | |
175 | ||
176 | line = message ("%s%q", istring, | |
177 | cstring_fill (s->elements[i], longest)); | |
178 | } | |
179 | else | |
180 | { | |
181 | line = message ("%q%s%q", line, gstring, | |
182 | cstring_fill (s->elements[i], longest)); | |
183 | } | |
184 | } | |
185 | ||
186 | cstring_free (line); | |
187 | cstring_free (istring); | |
188 | cstring_free (gstring); | |
189 | } | |
190 | } | |
191 | ||
192 | /*@only@*/ cstring | |
193 | cstringList_unparseAbbrev (cstringList s) | |
194 | { | |
195 | cstring st = cstring_undefined; | |
196 | ||
197 | if (cstringList_isDefined (s)) | |
198 | { | |
199 | int i; | |
200 | ||
201 | for (i = 0; i < s->nelements; i++) | |
202 | { | |
203 | if (i == 0) | |
204 | { | |
205 | st = cstring_copy (s->elements[i]); | |
206 | } | |
207 | else if (i > 3 && s->nelements > 5) | |
208 | { | |
209 | st = message ("%q, ...", st); | |
210 | break; | |
211 | } | |
212 | else | |
213 | { | |
214 | st = message ("%q, %s", st, s->elements[i]); | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | return st; | |
220 | } | |
221 | ||
222 | void | |
223 | cstringList_free (cstringList s) | |
224 | { | |
225 | if (cstringList_isDefined (s)) | |
226 | { | |
227 | sfree (s->elements); | |
228 | sfree (s); | |
229 | } | |
230 | } | |
231 | ||
232 | void | |
233 | cstringList_alphabetize (cstringList s) | |
234 | { | |
235 | if (cstringList_isDefined (s)) | |
236 | { | |
237 | /*@-modobserver@*/ | |
238 | qsort (s->elements, (size_t) s->nelements, | |
239 | sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare); | |
240 | /*@=modobserver@*/ | |
241 | } | |
242 | } | |
243 |