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

messageLog.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 ** messageLog.c (from slist_template.c)
00026 */
00027 
00028 # include "lclintMacros.nf"
00029 # include "basic.h"
00030 
00031 /*@only@*/ messageLog
00032 messageLog_new ()
00033 {
00034   messageLog s = (messageLog) dmalloc (sizeof (*s));
00035   
00036   s->nelements = 0;
00037   s->nspace = messageLogBASESIZE;
00038   s->elements = (msgentry *) dmalloc (sizeof (*s->elements) * messageLogBASESIZE);
00039 
00040   return (s);
00041 }
00042 
00043 static /*@only@*/ msgentry
00044 msgentry_create (fileloc loc, cstring mess)
00045 {
00046   msgentry msg = (msgentry) dmalloc (sizeof (*msg));
00047 
00048   msg->loc = fileloc_copy (loc);
00049   msg->msg = cstring_copy (mess);
00050 
00051   return msg;
00052 }
00053 
00054 static void msgentry_free (/*@only@*/ msgentry msg)
00055 {
00056   fileloc_free (msg->loc);
00057   cstring_free (msg->msg);
00058   sfree (msg);
00059 }
00060 
00061 /*
00062 ** returns TRUE if m1 < m2
00063 */
00064 
00065 static bool
00066 msgentry_lessthan (msgentry m1, msgentry m2)
00067 {
00068   return (fileloc_lessthan (m1->loc, m2->loc)
00069           || (fileloc_equal (m1->loc, m2->loc) 
00070               && (cstring_lessthan (m1->msg, m2->msg))));
00071 }
00072 
00073 static bool
00074 msgentry_equal (msgentry m1, msgentry m2)
00075 {
00076   return (fileloc_equal (m1->loc, m2->loc) &&
00077           cstring_equal (m1->msg, m2->msg));
00078 }
00079 
00080 /*
00081 ** returns highest index of element less than msg
00082 */
00083 
00084 static int
00085 messageLog_index (messageLog s, msgentry msg)
00086 {
00087   int high;
00088   int low  = 0;
00089 
00090   llassert (messageLog_isDefined (s));
00091 
00092   high = s->nelements - 1;
00093 
00094   for (low = high; low >= 0; low--)
00095     {
00096       if (msgentry_lessthan (s->elements[low], msg))
00097         {
00098           return low;
00099         }
00100     }
00101 
00102   return -1;
00103 # if 0      
00104   while (low < high)
00105     {
00106       int mid = (low + high + 1) / 2;
00107 
00108       if (msgentry_lessthan (s->elements[mid], msg)) /* mid < msg */
00109         {
00110           if (high == mid) break;
00111           high = mid;
00112         }
00113       else
00114         {
00115           if (low == mid) break;
00116           low = mid;
00117         }
00118     }
00119 
00120   return low - 1;
00121 # endif
00122 }
00123 
00124 static void
00125 messageLog_grow (/*@notnull@*/ messageLog s)
00126 {
00127   int i;
00128   msgentry *newelements;
00129   
00130   s->nspace += messageLogBASESIZE; 
00131   newelements = (msgentry *) dmalloc (sizeof (*newelements) * (s->nelements + s->nspace));
00132   
00133   for (i = 0; i < s->nelements; i++)
00134     {
00135       newelements[i] = s->elements[i];
00136     }
00137   
00138   sfree (s->elements);
00139   s->elements = newelements;
00140 }
00141 
00142 bool messageLog_add (messageLog s, fileloc fl, cstring mess)
00143 {
00144   msgentry msg = msgentry_create (fl, mess);
00145   int ind, i;
00146 
00147   llassert (messageLog_isDefined (s));
00148 
00149   ind = messageLog_index (s, msg);
00150 
00151   if (ind + 1 < s->nelements)
00152     {
00153       if (msgentry_equal (msg, s->elements[ind + 1]))
00154         {
00155           msgentry_free (msg);
00156           return FALSE;
00157         }
00158     }
00159 
00160   if (s->nspace <= 0) {
00161     messageLog_grow (s);
00162   }
00163 
00164   for (i = s->nelements; i > ind + 1; i--)
00165     {
00166       s->elements[i] = s->elements[i-1];
00167     }
00168   
00169   s->elements[ind + 1] = msg;
00170   s->nspace--;
00171   s->nelements++;
00172 
00173   return TRUE;
00174 }
00175 
00176 /*@only@*/ cstring
00177 messageLog_unparse (messageLog s)
00178 {
00179    int i;
00180    cstring st = cstring_makeLiteral ("[");
00181 
00182    if (messageLog_isDefined (s))
00183      {
00184        for (i = 0; i < s->nelements; i++)
00185          {
00186            if (i == 0)
00187              {
00188                st = message ("%q %q", st, fileloc_unparseDirect (s->elements[i]->loc));
00189              }
00190            else
00191              st = message ("%q, %q", st, fileloc_unparseDirect (s->elements[i]->loc));
00192          }
00193      }
00194 
00195    st = message ("%q ]", st);
00196    return st;
00197 }
00198 
00199 void
00200 messageLog_free (messageLog s)
00201 {
00202   if (s != NULL)
00203     {
00204       int i;
00205 
00206       for (i = 0; i < s->nelements; i++)
00207         {
00208           msgentry_free (s->elements[i]);
00209         }
00210       
00211       sfree (s->elements); 
00212       sfree (s);
00213     }
00214 }

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