]> andersk Git - splint.git/blob - src/cstringSList.c
noexpand always false.
[splint.git] / src / cstringSList.c
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.
19 **
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
23 */
24 /*
25 ** cstringSList.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 "splintMacros.nf"
33 # include "basic.h"
34
35 cstringSList
36 cstringSList_new ()
37 {
38   return cstringSList_undefined;
39 }
40
41 static /*@notnull@*/ cstringSList
42 cstringSList_newEmpty (void)
43 {
44   cstringSList s = (cstringSList) dmalloc (sizeof (*s));
45   
46   s->nelements = 0;
47   s->nspace = cstringSListBASESIZE; 
48   s->elements = (cstring *) dmalloc (sizeof (*s->elements) * cstringSListBASESIZE);
49
50   return (s);
51 }
52
53 static void
54 cstringSList_grow (/*@notnull@*/ cstringSList s)
55 {
56   int i;
57   cstring *newelements;
58   
59   s->nspace += cstringSListBASESIZE;
60
61   newelements = (cstring *) dmalloc (sizeof (*newelements) 
62                                      * (s->nelements + s->nspace));
63
64
65   if (newelements == (cstring *) 0)
66     {
67       llfatalerror (cstring_makeLiteral ("cstringSList_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 cstringSList cstringSList_single (/*@exposed@*/ cstring el) 
80 {
81   cstringSList s = cstringSList_new ();
82   s = cstringSList_add (s, el);
83   return s;
84 }
85
86 cstringSList cstringSList_add (cstringSList s, /*@exposed@*/ cstring el)
87 {
88   if (!cstringSList_isDefined (s))
89     {
90       s = cstringSList_newEmpty ();
91     }
92
93   if (s->nspace <= 0)
94     {
95       cstringSList_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 cstringSList_get (cstringSList s, int index)
107 {
108   llassert (s != NULL);
109   llassert (index < s->nelements);
110   return s->elements[index];
111 }
112
113 cstring
114 cstringSList_unparse (cstringSList s)
115 {
116   return cstringSList_unparseSep (s, cstring_makeLiteralTemp (", "));
117 }
118
119 cstring
120 cstringSList_unparseSep (cstringSList s, cstring sep)
121 {
122    cstring st = cstring_undefined;
123
124    if (cstringSList_isDefined (s))
125      {
126        int i;
127
128        for (i = 0; i < s->nelements; i++)
129          {
130            if (i == 0)
131              {
132                st = cstring_copy (s->elements[i]);
133              }
134            else
135              st = message ("%q%s%s", st, sep, s->elements[i]);
136          }
137      }
138
139    return st;
140 }
141
142 void
143 cstringSList_printSpaced (cstringSList s, size_t indent, size_t gap, int linelen)
144 {
145   if (cstringSList_isDefined (s))
146     {
147       cstring line = cstring_undefined;
148       cstring istring = cstring_fill (cstring_undefined, indent);
149       cstring gstring = cstring_fill (cstring_undefined, gap);
150       int numcol;
151       size_t longest = 0;
152       int i;
153  
154       /*
155       ** find the longest string
156       */
157
158       for (i = 0; i < s->nelements; i++)
159         {
160           size_t len = cstring_length (s->elements[i]);
161
162           if (len > longest)
163             {
164               longest = len;
165             }
166         }
167
168       numcol = size_toInt ((linelen - indent) / (longest + gap));
169       
170       if (numcol <= 1) 
171         {
172           numcol = 1;
173         }
174
175       for (i = 0; i < s->nelements; i++)
176         {
177           if (i % numcol == 0)
178             {
179               if (i != 0)
180                 {
181                   llmsg (line);
182                 }
183               
184               line = message ("%s%q", istring,
185                               cstring_fill (s->elements[i], longest));
186             }
187           else
188             {
189               line = message ("%q%s%q", line, gstring, 
190                               cstring_fill (s->elements[i], longest));
191             }
192         }
193
194       cstring_free (line);
195       cstring_free (istring);
196       cstring_free (gstring);
197     }
198 }
199
200 /*@only@*/ cstring
201 cstringSList_unparseAbbrev (cstringSList s)
202 {
203    cstring st = cstring_undefined;
204
205    if (cstringSList_isDefined (s))
206      {
207        int i;
208        
209        for (i = 0; i < s->nelements; i++)
210          {
211            if (i == 0)
212              {
213                st = cstring_copy (s->elements[i]);
214              }
215            else if (i > 3 && s->nelements > 5)
216              {
217                st = message ("%q, ...", st);
218                break;
219              }
220            else
221              {
222                st = message ("%q, %s", st, s->elements[i]);
223              }
224          }
225      }
226
227    return st;
228 }
229
230 void
231 cstringSList_free (cstringSList s)
232 {
233   if (cstringSList_isDefined (s))
234     {
235       /*
236       ** A modification of observer message is reported here, since
237       ** *s->elements is an observer.  But sfree doesn't REALLY modify
238       ** the value of this object.
239       */
240
241       /*@-modobserver@*/ 
242       sfree (s->elements);
243       /*@=modobserver@*/
244
245       sfree (s);
246     }
247 }
248
249 void
250 cstringSList_alphabetize (cstringSList s)
251 {
252   if (cstringSList_isDefined (s))
253     {
254       /*@-modobserver@*/
255       qsort (s->elements, (size_t) s->nelements, 
256              sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare);
257       /*@=modobserver@*/
258     }
259 }
260
This page took 0.139142 seconds and 5 git commands to generate.