Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

cstringSList.c

Go to the documentation of this file.
00001 /*
00002 ** LCLint - annotation-assisted static program checker
00003 ** Copyright (C) 1994-2000 University of Virginia,
00004 **         Massachusetts Institute of Technology
00005 **
00006 ** This program is free software; you can redistribute it and/or modify it
00007 ** under the terms of the GNU General Public License as published by the
00008 ** Free Software Foundation; either version 2 of the License, or (at your
00009 ** option) any later version.
00010 ** 
00011 ** This program is distributed in the hope that it will be useful, but
00012 ** WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 ** General Public License for more details.
00015 ** 
00016 ** The GNU General Public License is available from http://www.gnu.org/ or
00017 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00018 ** MA 02111-1307, USA.
00019 **
00020 ** For information on lclint: lclint-request@cs.virginia.edu
00021 ** To report a bug: lclint-bug@cs.virginia.edu
00022 ** For more information: http://lclint.cs.virginia.edu
00023 */
00024 /*
00025 ** cstringSList.c
00026 **
00027 ** based on list_template.c
00028 **
00029 ** where T has T_equal (or change this) and T_unparse
00030 */
00031 
00032 # include "lclintMacros.nf"
00033 # include "basic.h"
00034 
00035 cstringSList
00036 cstringSList_new ()
00037 {
00038   return cstringSList_undefined;
00039 }
00040 
00041 static /*@notnull@*/ cstringSList
00042 cstringSList_newEmpty (void)
00043 {
00044   cstringSList s = (cstringSList) dmalloc (sizeof (*s));
00045   
00046   s->nelements = 0;
00047   s->nspace = cstringSListBASESIZE; 
00048   s->elements = (cstring *) dmalloc (sizeof (*s->elements) * cstringSListBASESIZE);
00049 
00050   return (s);
00051 }
00052 
00053 static void
00054 cstringSList_grow (/*@notnull@*/ cstringSList s)
00055 {
00056   int i;
00057   cstring *newelements;
00058   
00059   s->nspace += cstringSListBASESIZE;
00060 
00061   newelements = (cstring *) dmalloc (sizeof (*newelements) 
00062                                      * (s->nelements + s->nspace));
00063 
00064 
00065   if (newelements == (cstring *) 0)
00066     {
00067       llfatalerror (cstring_makeLiteral ("cstringSList_grow: out of memory!"));
00068     }
00069 
00070   for (i = 0; i < s->nelements; i++)
00071     {
00072       newelements[i] = s->elements[i];
00073     }
00074   
00075   sfree (s->elements); 
00076   s->elements = newelements;
00077 }
00078 
00079 cstringSList cstringSList_single (/*@exposed@*/ cstring el) 
00080 {
00081   cstringSList s = cstringSList_new ();
00082   s = cstringSList_add (s, el);
00083   return s;
00084 }
00085 
00086 cstringSList cstringSList_add (cstringSList s, /*@exposed@*/ cstring el)
00087 {
00088   if (!cstringSList_isDefined (s))
00089     {
00090       s = cstringSList_newEmpty ();
00091     }
00092 
00093   if (s->nspace <= 0)
00094     {
00095       cstringSList_grow (s);
00096     }
00097   
00098   s->nspace--;
00099   s->elements[s->nelements] = el;
00100   s->nelements++;
00101 
00102   return s;
00103 }
00104 
00105 cstring
00106 cstringSList_unparse (cstringSList s)
00107 {
00108   return cstringSList_unparseSep (s, cstring_makeLiteralTemp (", "));
00109 }
00110 
00111 cstring
00112 cstringSList_unparseSep (cstringSList s, cstring sep)
00113 {
00114    cstring st = cstring_undefined;
00115 
00116    if (cstringSList_isDefined (s))
00117      {
00118        int i;
00119 
00120        for (i = 0; i < s->nelements; i++)
00121          {
00122            if (i == 0)
00123              {
00124                st = cstring_copy (s->elements[i]);
00125              }
00126            else
00127              st = message ("%q%s%s", st, sep, s->elements[i]);
00128          }
00129      }
00130 
00131    return st;
00132 }
00133 
00134 void
00135 cstringSList_printSpaced (cstringSList s, int indent, int gap, int linelen)
00136 {
00137   if (cstringSList_isDefined (s))
00138     {
00139       cstring line = cstring_undefined;
00140       cstring istring = cstring_fill (cstring_undefined, indent);
00141       cstring gstring = cstring_fill (cstring_undefined, gap);
00142       int numcol;
00143       int longest = 0;
00144       int i;
00145  
00146       /*
00147       ** find the longest string
00148       */
00149 
00150       for (i = 0; i < s->nelements; i++)
00151         {
00152           int len = cstring_length (s->elements[i]);
00153 
00154           if (len > longest)
00155             {
00156               longest = len;
00157             }
00158         }
00159 
00160       numcol = (linelen - indent) / (longest + gap);
00161       
00162       if (numcol <= 1) 
00163         {
00164           numcol = 1;
00165         }
00166 
00167       for (i = 0; i < s->nelements; i++)
00168         {
00169           if (i % numcol == 0)
00170             {
00171               if (i != 0)
00172                 {
00173                   llmsg (line);
00174                 }
00175               
00176               line = message ("%s%q", istring,
00177                               cstring_fill (s->elements[i], longest));
00178             }
00179           else
00180             {
00181               line = message ("%q%s%q", line, gstring, 
00182                               cstring_fill (s->elements[i], longest));
00183             }
00184         }
00185 
00186       cstring_free (line);
00187       cstring_free (istring);
00188       cstring_free (gstring);
00189     }
00190 }
00191 
00192 /*@only@*/ cstring
00193 cstringSList_unparseAbbrev (cstringSList s)
00194 {
00195    cstring st = cstring_undefined;
00196 
00197    if (cstringSList_isDefined (s))
00198      {
00199        int i;
00200        
00201        for (i = 0; i < s->nelements; i++)
00202          {
00203            if (i == 0)
00204              {
00205                st = cstring_copy (s->elements[i]);
00206              }
00207            else if (i > 3 && s->nelements > 5)
00208              {
00209                st = message ("%q, ...", st);
00210                break;
00211              }
00212            else
00213              {
00214                st = message ("%q, %s", st, s->elements[i]);
00215              }
00216          }
00217      }
00218 
00219    return st;
00220 }
00221 
00222 void
00223 cstringSList_free (cstringSList s)
00224 {
00225   if (cstringSList_isDefined (s))
00226     {
00227       /*
00228       ** A modification of observer message is reported here, since
00229       ** *s->elements is an observer.  But sfree doesn't REALLY modify
00230       ** the value of this object.
00231       */
00232 
00233       /*@-modobserver@*/ 
00234       sfree (s->elements);
00235       /*@=modobserver@*/
00236 
00237       sfree (s);
00238     }
00239 }
00240 
00241 void
00242 cstringSList_alphabetize (cstringSList s)
00243 {
00244   if (cstringSList_isDefined (s))
00245     {
00246       /*@-modobserver@*/
00247       qsort (s->elements, (size_t) s->nelements, 
00248              sizeof (*s->elements), (int (*)(const void *, const void *)) cstring_xcompare);
00249       /*@=modobserver@*/
00250     }
00251 }
00252 

Generated at Fri Nov 3 18:57:40 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000