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

ltokenList.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 ** ltokenList.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 "llbasic.h"
00034 
00035 /*@notnull@*/ /*@only@*/ ltokenList
00036 ltokenList_new ()
00037 {
00038   ltokenList s = (ltokenList) dmalloc (sizeof (*s));
00039   
00040   s->nelements = 0;
00041   s->nspace = ltokenListBASESIZE;
00042   s->elements = (ltoken *) 
00043     dmalloc (sizeof (*s->elements) * ltokenListBASESIZE);
00044   s->current = 0;
00045 
00046   return (s);
00047 }
00048 
00049 /*@notnull@*/ /*@only@*/ ltokenList
00050 ltokenList_singleton (ltoken l)
00051 {
00052   ltokenList s = (ltokenList) dmalloc (sizeof (*s));
00053 
00054   s->nelements = 1;
00055   s->nspace = ltokenListBASESIZE - 1;
00056   s->elements = (ltoken *) dmalloc (sizeof (*s->elements) * ltokenListBASESIZE);
00057   s->elements[0] = l;
00058   s->current = 0;
00059 
00060   return (s);
00061 }
00062 
00063 static void
00064 ltokenList_grow (/*@notnull@*/ ltokenList s)
00065 {
00066   int i;
00067   ltoken *newelements;
00068 
00069   s->nspace += ltokenListBASESIZE;
00070 
00071   newelements = (ltoken *) dmalloc (sizeof (*newelements)
00072                                     * (s->nelements + s->nspace));
00073 
00074   for (i = 0; i < s->nelements; i++)
00075     {
00076       newelements[i] =  s->elements[i]; 
00077     }
00078 
00079   sfree (s->elements); 
00080   s->elements = newelements;
00081 }
00082 
00083 ltokenList 
00084 ltokenList_push (/*@returned@*/ ltokenList s, ltoken el)
00085 {
00086   ltokenList_addh (s, el);
00087   return s;
00088 }
00089 
00090 void 
00091 ltokenList_addh (ltokenList s, ltoken el)
00092 {
00093   llassert (ltokenList_isDefined (s));
00094 
00095   if (s->nspace <= 0)
00096     ltokenList_grow (s);
00097 
00098   s->nspace--;
00099   s->elements[s->nelements] = el;
00100   s->nelements++;
00101 }
00102 
00103 void 
00104 ltokenList_reset (ltokenList s)
00105 {
00106   if (ltokenList_isDefined (s))
00107     {
00108       s->current = 0;
00109     }
00110 }
00111 
00112 bool
00113 ltokenList_isFinished (ltokenList s)
00114 {
00115   return (ltokenList_isUndefined(s) || (s->current == s->nelements));
00116 }
00117 
00118 void 
00119 ltokenList_advance (ltokenList s)
00120 {
00121   if (ltokenList_isDefined (s))
00122     {
00123       s->current++;
00124       llassert (s->current <= s->nelements);
00125     }
00126 }
00127 
00128 ltoken 
00129 ltokenList_head (ltokenList s)
00130 {
00131   llassert (ltokenList_isDefined (s) && s->nelements > 0);
00132   return (s->elements[0]);
00133 }
00134 
00135 bool 
00136 ltokenList_equal (ltokenList s1, ltokenList s2)
00137 {
00138   if (ltokenList_isUndefined (s1))
00139     {
00140       return (ltokenList_isEmpty (s2));
00141     }
00142   else
00143     {
00144       if (ltokenList_isUndefined (s2))
00145         {
00146           return ltokenList_isEmpty (s1);
00147         }
00148       else
00149         {
00150           int i;
00151           int size = s1->nelements;
00152           
00153           if (s2->nelements != size)
00154             return FALSE;
00155           
00156           for (i = 0; i < size; i++)
00157             {
00158               if (!ltoken_similar (s1->elements[i], s2->elements[i]))
00159                 return FALSE;
00160             }
00161           return TRUE;
00162         }
00163     }
00164 }
00165 
00166 /*@only@*/ ltokenList 
00167 ltokenList_copy (ltokenList s)
00168 {
00169   ltokenList r = ltokenList_new ();
00170 
00171   ltokenList_elements (s, x)
00172   {
00173     ltokenList_addh (r, ltoken_copy (x));
00174   } end_ltokenList_elements;
00175 
00176   return r;
00177 }
00178 
00179 void
00180 ltokenList_removeCurrent (ltokenList s)
00181 {
00182   int i;
00183   llassert (ltokenList_isDefined (s) && s->current >= 0 && s->current < s->nelements);
00184 
00185   for (i = s->current; i < s->nelements - 1; i++)
00186     {
00187       s->elements[i] = s->elements[i+1];
00188     }
00189 
00190   s->nelements--;
00191   s->nspace++;
00192 }
00193 
00194 ltoken 
00195 ltokenList_current (ltokenList s)
00196 {
00197   llassert (ltokenList_isDefined (s) && s->current >= 0 && s->current < s->nelements);
00198   return (s->elements[s->current]);
00199 }
00200 
00201 /*@only@*/ cstring
00202 ltokenList_unparse (ltokenList s)
00203 {
00204   int i;
00205   cstring st = cstring_undefined;
00206 
00207   if (ltokenList_isDefined (s))
00208     {
00209       for (i = 0; i < s->nelements; i++)
00210         {
00211           if (i == 0)
00212             {
00213               st = cstring_copy (ltoken_unparse (s->elements[i]));
00214             }
00215           else
00216             st = message ("%q, %s", st, ltoken_unparse (s->elements[i]));
00217         }
00218     }
00219 
00220   return st;
00221 }
00222 
00223 void
00224 ltokenList_free (ltokenList s)
00225 {
00226   if (ltokenList_isDefined (s))
00227     {
00228       sfree (s->elements);
00229       sfree (s);
00230     }
00231 }

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