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

enumNameList.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 ** enumNameList.c
00026 **
00027 ** based on list_template.c
00028 **
00029 ** where T has T_equal (or change this) and T_unparse
00030 **
00031 ** used to be cenum.c
00032 */
00033 
00034 # include "lclintMacros.nf"
00035 # include "basic.h"
00036 
00037 enumNameList
00038   enumNameList_new ()
00039 {
00040   enumNameList s = (enumNameList) dmalloc (sizeof (*s));
00041 
00042   s->nelements = 0;
00043   s->nspace = enumNameListBASESIZE;
00044   s->elements = (enumName *)
00045     dmalloc (sizeof (*s->elements) * enumNameListBASESIZE);
00046 
00047   return (s);
00048 }
00049 
00050 /*@only@*/ enumNameList
00051 enumNameList_single (/*@keep@*/ enumName t)
00052 {
00053   enumNameList s = (enumNameList) dmalloc (sizeof (*s));
00054   
00055   s->nelements = 1;
00056   s->nspace = enumNameListBASESIZE - 1;
00057   s->elements = (enumName *) dmalloc (sizeof (*s->elements) * enumNameListBASESIZE);
00058   s->elements[0] = t;
00059 
00060   return (s);
00061 }
00062 
00063 bool
00064 enumNameList_match (enumNameList e1, enumNameList e2)
00065 {
00066   int i;
00067 
00068   if (e1->nelements != e2->nelements) return FALSE;
00069 
00070   for (i = 0; i < e1->nelements; i++)
00071     {
00072       if (!cstring_equal (e1->elements[i], e2->elements[i]))
00073         return FALSE;
00074     }
00075   return TRUE;
00076 }
00077 
00078 static void
00079 enumNameList_grow (enumNameList s)
00080 {
00081   int i;
00082   enumName *newelements;
00083 
00084   s->nspace += enumNameListBASESIZE;
00085   newelements = (enumName *) dmalloc (sizeof (*newelements) * (s->nelements + s->nspace));
00086 
00087   if (newelements == (enumName *) 0)
00088     {
00089       llfatalerror (cstring_makeLiteral ("enumNameList_grow: out of memory!"));
00090     }
00091 
00092   for (i = 0; i < s->nelements; i++)
00093     {
00094       newelements[i] = s->elements[i];
00095     }
00096 
00097   sfree (s->elements);
00098   s->elements = newelements;
00099 }
00100 
00101 void 
00102 enumNameList_addh (enumNameList s, /*@keep@*/ enumName el)
00103 {
00104   if (s->nspace <= 0)
00105     enumNameList_grow (s);
00106 
00107   s->nspace--;
00108   s->elements[s->nelements] = el;
00109   s->nelements++;
00110 }
00111 
00112 enumNameList 
00113 enumNameList_push (/*@returned@*/ enumNameList s, /*@only@*/ enumName el)
00114 {
00115   enumNameList_addh (s, el);
00116   return s;
00117 }
00118 
00119 /*@only@*/ enumNameList 
00120 enumNameList_copy (enumNameList s)
00121 {
00122   enumNameList r = enumNameList_new ();
00123 
00124   enumNameList_elements (s, x)
00125   {
00126     enumNameList_addh (r, cstring_copy (x));
00127   } end_enumNameList_elements;
00128 
00129   return r;
00130 }
00131 
00132 bool
00133 enumNameList_member (enumNameList s, cstring m)
00134 {
00135   enumNameList_elements (s, x)
00136   {
00137     if (cstring_equal (m, x)) return TRUE;
00138   } end_enumNameList_elements;
00139 
00140   return FALSE;
00141 }
00142 
00143 /*@only@*/ enumNameList
00144 enumNameList_subtract (enumNameList source, enumNameList del)
00145 {
00146   enumNameList ret = enumNameList_new ();
00147 
00148   enumNameList_elements (source, el)
00149     {
00150       if (!enumNameList_member (del, el))
00151         {
00152           enumNameList_addh (ret, cstring_copy (el));
00153         }
00154     } end_enumNameList_elements;
00155 
00156   return ret;
00157 }
00158 
00159 cstring
00160 enumNameList_unparse (enumNameList s)
00161 {
00162   int i;
00163   cstring st = cstring_undefined;
00164 
00165   for (i = 0; i < s->nelements; i++)
00166     {
00167       if (i == 0) 
00168         {
00169           st = cstring_copy (s->elements[i]);
00170         }
00171       else
00172         {
00173           st = message ("%q, %s", st, s->elements[i]);
00174         }
00175     }
00176 
00177   return st;
00178 }
00179 
00180 cstring enumNameList_unparseBrief (enumNameList s)
00181 {
00182   int i;
00183   cstring st = cstring_undefined;
00184 
00185   for (i = 0; i < s->nelements; i++)
00186     {
00187       if (i == 0)
00188         {
00189           st = cstring_copy (s->elements[i]);
00190         }
00191       else if (i == 3 && s->nelements > 5)
00192         {
00193           st = message ("%q, ...", st);
00194           i = s->nelements - 2;
00195         }
00196       else
00197         {
00198           st = message ("%q, %s", st, s->elements[i]);
00199         }
00200     }
00201   
00202   return st;
00203 }
00204 
00205 /*@only@*/ cstring
00206 enumNameList_dump (enumNameList s)
00207 {
00208   int i;
00209   cstring st = cstring_undefined;
00210 
00211   for (i = 0; i < s->nelements; i++)
00212     {
00213       if (i == 0)
00214         {
00215           st = cstring_copy (s->elements[i]);
00216         }
00217       else
00218         st = message ("%q,%s", st, s->elements[i]);
00219     }
00220   return st;
00221 }
00222 
00223 /*@only@*/ enumNameList 
00224 enumNameList_undump (d_char *s)
00225 {
00226   enumNameList e = enumNameList_new ();
00227 
00228   if (**s == '}')
00229     (*s)++;
00230   else
00231     {
00232       while (TRUE)
00233         {
00234           char *t = strchr (*s, ',');
00235           char mt;
00236 
00237           if (t == NULL)
00238             {
00239               t = strchr (*s, '}');
00240 
00241               if (t == NULL)
00242                 {
00243                   llcontbug (message ("enumNameList_undump: bad line: %s", cstring_fromChars (*s)));
00244                   return e; 
00245                 }
00246             }
00247           
00248           mt = *t;
00249           *t = '\0';
00250           
00251           enumNameList_addh (e, cstring_fromChars (mstring_copy (*s)));
00252           *s = t + 1;
00253           if (mt == '}')
00254             break;
00255         }
00256     }
00257   return e;
00258 }
00259 
00260 void
00261 enumNameList_free (enumNameList s)
00262 {
00263   int i;
00264 
00265   
00266   for (i = 0; i < s->nelements; i++)
00267     {
00268       cstring_free (s->elements[i]); 
00269     }
00270 
00271   sfree (s->elements); 
00272   sfree (s);
00273 }
00274 
00275 

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