commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnue/geas/src/classdef classdef.c


From: Reinhard Mueller
Subject: gnue/geas/src/classdef classdef.c
Date: Sat, 20 Oct 2001 06:46:19 -0400

CVSROOT:        /cvsroot/gnue
Module name:    gnue
Changes by:     Reinhard Mueller <address@hidden>       01/10/20 06:46:19

Modified files:
        geas/src/classdef: classdef.c 

Log message:
        Changed implementation of class, type and field to use a GList to hold 
the list of fields instead a GHashTable. GList keeps the order of the fields, 
GHashTable didn't.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/geas/src/classdef/classdef.c.diff?tr1=1.19&tr2=1.20&r1=text&r2=text

Patches:
Index: gnue/geas/src/classdef/classdef.c
diff -u gnue/geas/src/classdef/classdef.c:1.19 
gnue/geas/src/classdef/classdef.c:1.20
--- gnue/geas/src/classdef/classdef.c:1.19      Sat Oct 13 17:46:33 2001
+++ gnue/geas/src/classdef/classdef.c   Sat Oct 20 06:46:19 2001
@@ -19,7 +19,7 @@
    along with GEAS; if not, write to the Free Software Foundation, Inc.,
    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-   $Id: classdef.c,v 1.19 2001/10/13 21:46:33 reinhard Exp $
+   $Id: classdef.c,v 1.20 2001/10/20 10:46:19 reinhard Exp $
 */
 
 #include "config.h"
@@ -54,7 +54,7 @@
   char           *name_db;              /* table name for database */
   geas_cd_access  access;
   char           *filename;
-  GHashTable     *fields;
+  GList          *fields;
 };
 
 struct _geas_cd_type
@@ -67,18 +67,19 @@
   geas_cd_datatype datatype;
   int              format;              /* only for char types */
   geas_cd_class   *otherclass;          /* only for reference and list types */
-  GHashTable      *fields;              /* only for compound types */
+  GList           *fields;              /* only for compound types */
 };
 
 struct _geas_cd_fieldlist
 {
-  GList *list;
+  GList *root;
+  GList *stack;
   GList *current;
 };
 
 struct _geas_cd_field
 {
-  GHashTable      *parent;              /* hash table containing this field */
+  GList          **parent;              /* list containing this field */
   geas_cd_module  *module;              /* module that has defined this field 
*/
   char            *name;                /* without module */
   char            *name_full;           /* with module */
@@ -90,7 +91,7 @@
   geas_cd_field   *thisfield;           /* only for reference and list types: 
*/
   geas_cd_class   *otherclass;          /*  thisfield = otherclass.otherfield 
*/
   geas_cd_field   *otherfield;
-  GHashTable      *fields;              /* only for compound types */
+  GList           *fields;              /* only for compound types */
   char            *default_val;         /* not for ref., list and compound */
   int              property;
 };
@@ -120,8 +121,7 @@
                                 gpointer user_data);
 static gboolean _hr_free_type (gpointer key, gpointer value,
                                gpointer user_data);
-static gboolean _hr_free_field (gpointer key, gpointer value,
-                                gpointer user_data);
+static void _free_field (geas_cd_field *f);
 
 /* ========================================================================= *\
  * General functions
@@ -259,7 +259,6 @@
       c->name_full = g_strconcat (m->name, "::", name, NULL);
       c->name_db   = g_strconcat (m->name, "__", name, NULL);
       c->access    = (access == GEAS_CD_ACCESS_DEFAULT) ? m->access : access;
-      c->fields    = g_hash_table_new (g_str_hash, g_str_equal);
 
       g_hash_table_insert (m->classes, c->name, c);
 
@@ -312,10 +311,6 @@
       t->name_full = g_strconcat (m->name, "::", name, NULL);
       t->access    = (access == GEAS_CD_ACCESS_DEFAULT) ? m->access : access;
       t->datatype  = datatype;
-      if (datatype == GEAS_CD_DATATYPE_COMPOUND)
-        {
-          t->fields = g_hash_table_new (g_str_hash, g_str_equal);
-        }
 
       g_hash_table_insert (m->types, t->name, t);
     }
@@ -519,7 +514,7 @@
  * Create a field
 \* ------------------------------------------------------------------------- */
 static geas_cd_field *
-_create_field (GHashTable *parent, geas_cd_class *cls, geas_cd_module *module,
+_create_field (GList **parent, geas_cd_class *cls, geas_cd_module *module,
                char *name, geas_cd_datatype datatype)
 {
   geas_cd_field *f;
@@ -531,7 +526,7 @@
   f->name      = name;
   f->datatype  = datatype;
   f->name_full = g_strconcat (f->module->name, "::", f->name, NULL);
-  g_hash_table_insert (f->parent, f->name, f);
+  *(f->parent) = g_list_append (*(f->parent), f);
   return (f);
 }
 
@@ -582,7 +577,7 @@
              create a shadow field in the otherclass */
           geas_cd_field *shadow;
 
-          shadow = _create_field (f->otherclass->fields, f->otherclass,
+          shadow = _create_field (&(f->otherclass->fields), f->otherclass,
                                   f->module,
                                   g_strconcat ("_(", f->thisclass->name_full,
                                                ").", f->name, NULL),
@@ -605,13 +600,13 @@
  * Copy a single field into a class (called in g_hash_table_foreach)
 \* ------------------------------------------------------------------------- */
 static void
-_hf_copy_field (gpointer key, gpointer value, gpointer user_data)
+_lf_copy_field (gpointer data, gpointer user_data)
 {
   geas_cd_field *f;
-  geas_cd_field *original = (geas_cd_field *)value;
+  geas_cd_field *original = (geas_cd_field *)data;
   geas_cd_field *compound = (geas_cd_field *)user_data;
 
-  f = _create_field (compound->fields, compound->thisclass, compound->module,
+  f = _create_field (&(compound->fields), compound->thisclass, 
compound->module,
                      g_strconcat (compound->name, ".", original->name, NULL),
                      original->datatype);
   f->name_db     = g_strconcat (compound->name_db,  "__", original->name, 
NULL);
@@ -623,8 +618,7 @@
   if ((f->type) && (f->type->fields))
     {
       /* copy recursively */
-      f->fields = g_hash_table_new (g_str_hash, g_str_equal);
-      g_hash_table_foreach (f->type->fields, _hf_copy_field, f);
+      g_list_foreach (f->type->fields, _lf_copy_field, f);
     }
   _create_shadow_fields (f);
 }
@@ -650,7 +644,8 @@
     {
       if (type)
         {
-          f = _create_field (c->fields, c, m, g_strdup (name), type->datatype);
+          f = _create_field (&(c->fields), c, m, g_strdup (name),
+                             type->datatype);
           f->name_db    = g_strconcat (m->name, "__", name, NULL);
           f->type       = type;
           f->format     = type->format;
@@ -659,20 +654,19 @@
           if (type->fields)
             {
               /* Copy the fields from typedef to actual class */
-              f->fields = g_hash_table_new (g_str_hash, g_str_equal);
-              g_hash_table_foreach (type->fields, _hf_copy_field, f);
+              g_list_foreach (type->fields, _lf_copy_field, f);
             }
         }
       else if (reference)
         {
-          f = _create_field (c->fields, c, m, g_strdup (name),
+          f = _create_field (&(c->fields), c, m, g_strdup (name),
                              GEAS_CD_DATATYPE_REFERENCE);
           f->name_db    = g_strconcat (m->name, "__", name, NULL);
           f->otherclass = reference;
         }
       else if (list)
         {
-          f = _create_field (c->fields, c, m, g_strdup (name),
+          f = _create_field (&(c->fields), c, m, g_strdup (name),
                              GEAS_CD_DATATYPE_LIST);
           f->name_db    = g_strconcat (m->name, "__", name, NULL);
           f->otherclass = list;
@@ -694,12 +688,23 @@
 geas_cd_class_find_field (const geas_cd_class *c, const geas_cd_module *m,
                           const char *name)
 {
+  GList *l;
+
   g_return_val_if_fail (c, NULL);
   g_return_val_if_fail (name, NULL);
 
   /* TODO: multiple fields with the same fieldname should be allowed if the
      modulename differs */
-  return (g_hash_table_lookup (c->fields, name));
+  l = g_list_first (c->fields);
+  while (l)
+    {
+      if (!strcmp (geas_cd_field_get_name((geas_cd_field *)(l->data)), name))
+        {
+          return ((geas_cd_field *)(l->data));
+        }
+      l = g_list_next (l);
+    }
+  return (NULL);
 }
 
 /* ------------------------------------------------------------------------- *\
@@ -708,11 +713,20 @@
 static void
 _free_class (geas_cd_class *c)
 {
+  GList *l;
+
   g_free (c->name);
   g_free (c->name_full);
   g_free (c->name_db);
-  g_hash_table_foreach_remove (c->fields, _hr_free_field, NULL);
-  g_hash_table_destroy (c->fields);
+
+  l = g_list_first (c->fields);
+  while (l)
+    {
+      _free_field ((geas_cd_field *)(l->data));
+      l = g_list_next (l);
+    }
+  g_list_free (c->fields);
+
   g_free (c);
 }
 
@@ -831,7 +845,7 @@
     {
       f = g_new0 (geas_cd_field, 1);
 
-      f->parent    = t->fields;
+      f->parent    = &(t->fields);
       f->name      = g_strdup (name);
       f->name_full = g_strdup (name);
 
@@ -857,7 +871,7 @@
           g_assert_not_reached ();
         }
 
-      g_hash_table_insert (t->fields, f->name, f);
+      t->fields = g_list_append (t->fields, f);
     }
 
   return (f);
@@ -869,10 +883,21 @@
 geas_cd_field *
 geas_cd_type_find_field (const geas_cd_type *t, const char *name)
 {
+  GList *l;
+
   g_return_val_if_fail (t, NULL);
   g_return_val_if_fail (name, NULL);
 
-  return (g_hash_table_lookup (t->fields, name));
+  l = g_list_first (t->fields);
+  while (l)
+    {
+      if (!strcmp (geas_cd_field_get_name((geas_cd_field *)(l->data)), name))
+        {
+          return ((geas_cd_field *)(l->data));
+        }
+      l = g_list_next (l);
+    }
+  return (NULL);
 }
 
 /* ------------------------------------------------------------------------- *\
@@ -881,13 +906,19 @@
 static void
 _free_type (geas_cd_type *t)
 {
+  GList *l;
+
   g_free (t->name);
   g_free (t->name_full);
-  if (t->fields)
+
+  l = g_list_first (t->fields);
+  while (l)
     {
-      g_hash_table_foreach_remove (t->fields, _hr_free_field, NULL);
-      g_hash_table_destroy (t->fields);
+      _free_field ((geas_cd_field *)(l->data));
+      l = g_list_next (l);
     }
+  g_list_free (t->fields);
+
   g_free (t);
 }
 
@@ -919,20 +950,6 @@
 \* ========================================================================= */
 
 /* ------------------------------------------------------------------------- *\
- * Append a field to a GList (called by g_hash_table_foreach)
-\* ------------------------------------------------------------------------- */
-static void
-_hf_append_field (gpointer key, gpointer value, gpointer user_data)
-{
-  *(GList **)user_data = g_list_append (*(GList **)user_data, value);
-  if (((geas_cd_field *)value)->fields)
-    {
-      g_hash_table_foreach (((geas_cd_field *)value)->fields, _hf_append_field,
-                            user_data);
-    }
-}
-
-/* ------------------------------------------------------------------------- *\
  * Create the fieldlist for the class
 \* ------------------------------------------------------------------------- */
 geas_cd_fieldlist *
@@ -941,7 +958,7 @@
   geas_cd_fieldlist *fl;
 
   fl = g_new0 (geas_cd_fieldlist, 1);
-  g_hash_table_foreach (c->fields, _hf_append_field, &(fl->list));
+  fl->root = c->fields;
   return (fl);
 }
 
@@ -954,13 +971,28 @@
   g_return_val_if_fail (fl, NULL);
 
   if (!fl->current)
+    {
+      /* We are standing at the beginning of the list */
+      fl->current = fl->root;
+    }
+  else if (((geas_cd_field *)fl->current->data)->fields)
     {
-      fl->current = fl->list;
+      /* This is a compound type: we have to go deeper */
+      fl->stack = g_list_prepend (fl->stack, fl->current);
+      fl->current = ((geas_cd_field *)fl->current->data)->fields;
     }
   else
     {
+      while (!fl->current->next && fl->stack)
+        {
+          /* No more fields for this type: Climb back up the stack */
+          fl->current = (GList *)(fl->stack->data);
+          fl->stack = g_list_remove (fl->stack, fl->current);
+        }
+      /* Normal iterating through the list */
       fl->current = fl->current->next;
     }
+
   if (fl->current)
     {
       return (geas_cd_field *)(fl->current->data);
@@ -977,7 +1009,6 @@
 void
 geas_cd_fieldlist_free (geas_cd_fieldlist *fl)
 {
-  g_list_free (fl->list);
   g_free (fl);
 }
 
@@ -1111,26 +1142,22 @@
 static void
 _free_field (geas_cd_field *f)
 {
+  GList *l;
+
   g_free (f->name);
   g_free (f->name_full);
   g_free (f->name_db);
   g_free (f->default_val);
-  if (f->fields)
+
+  l = g_list_first (f->fields);
+  while (l)
     {
-      g_hash_table_foreach_remove (f->fields, _hr_free_field, NULL);
-      g_hash_table_destroy (f->fields);
+      _free_field ((geas_cd_field *)(l->data));
+      l = g_list_next (l);
     }
-  g_free (f);
-}
+  g_list_free (f->fields);
 
-/* ------------------------------------------------------------------------- *\
- * Free a field (called in g_hash_table_foreach_remove)
-\* ------------------------------------------------------------------------- */
-static gboolean
-_hr_free_field (gpointer key, gpointer value, gpointer user_data)
-{
-  _free_field ((geas_cd_field *)value);
-  return (TRUE);
+  g_free (f);
 }
 
 /* ------------------------------------------------------------------------- *\
@@ -1141,7 +1168,7 @@
 {
   g_return_if_fail (f);
 
-  g_hash_table_remove (f->parent, f->name);
+  *(f->parent) = g_list_remove (*(f->parent), f);
 
   _free_field (f);
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]