]> andersk Git - splint.git/blob - src/cstringSList.c
8fd4b715ffb6ecccbb4776d35ec1e1d1fdd911b6
[splint.git] / src / cstringSList.c
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 ** 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 "lclintMacros.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_unparse (cstringSList s)
107 {
108   return cstringSList_unparseSep (s, cstring_makeLiteralTemp (", "));
109 }
110
111 cstring
112 cstringSList_unparseSep (cstringSList s, cstring sep)
113 {
114    cstring st = cstring_undefined;
115
116    if (cstringSList_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 cstringSList_printSpaced (cstringSList s, int indent, int gap, int linelen)
136 {
137   if (cstringSList_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 cstringSList_unparseAbbrev (cstringSList s)
194 {
195    cstring st = cstring_undefined;
196
197    if (cstringSList_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 cstringSList_free (cstringSList s)
224 {
225   if (cstringSList_isDefined (s))
226     {
227       /*
228       ** A modification of observer message is reported here, since
229       ** *s->elements is an observer.  But sfree doesn't REALLY modify
230       ** the value of this object.
231       */
232
233       /*@-modobserver@*/ 
234       sfree (s->elements);
235       /*@=modobserver@*/
236
237       sfree (s);
238     }
239 }
240
241 void
242 cstringSList_alphabetize (cstringSList s)
243 {
244   if (cstringSList_isDefined (s))
245     {
246       /*@-modobserver@*/
247       qsort (s->elements, (size_t) s->nelements, 
248              sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare);
249       /*@=modobserver@*/
250     }
251 }
252
This page took 0.112096 seconds and 3 git commands to generate.