[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gnue/geas/src/classdef classdef.c classdef.h
From: |
Reinhard Mueller |
Subject: |
gnue/geas/src/classdef classdef.c classdef.h |
Date: |
Fri, 28 Sep 2001 14:37:37 -0400 |
CVSROOT: /cvsroot/gnue
Module name: gnue
Changes by: Reinhard Mueller <address@hidden> 01/09/28 14:37:37
Modified files:
geas/src/classdef: classdef.c classdef.h
Log message:
Started with the routines to build up field info in memory.
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/geas/src/classdef/classdef.c.diff?tr1=1.11&tr2=1.12&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue/geas/src/classdef/classdef.h.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
Patches:
Index: gnue/geas/src/classdef/classdef.c
diff -u gnue/geas/src/classdef/classdef.c:1.11
gnue/geas/src/classdef/classdef.c:1.12
--- gnue/geas/src/classdef/classdef.c:1.11 Sat Sep 22 16:12:04 2001
+++ gnue/geas/src/classdef/classdef.c Fri Sep 28 14:37:37 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.11 2001/09/22 20:12:04 reinhard Exp $
+ $Id: classdef.c,v 1.12 2001/09/28 18:37:37 reinhard Exp $
*/
#include "config.h"
@@ -34,7 +34,7 @@
struct _geas_cd_module
{
- gchar *name;
+ char *name;
geas_cd_access access;
GHashTable *classes;
GHashTable *types;
@@ -42,21 +42,22 @@
struct _geas_cd_class
{
- geas_cd_module *module;
- gchar *name; /* without module */
- gchar *name_full; /* with module */
+ GHashTable *parent; /* hash table containing this class */
+ char *name; /* without module */
+ char *name_full; /* with module */
+ char *name_db; /* table name for database */
geas_cd_access access;
- gchar *filename;
+ char *filename;
GHashTable *fields;
};
struct _geas_cd_type
{
- geas_cd_module *module;
- gchar *name; /* without module */
- gchar *name_full; /* with module */
+ GHashTable *parent; /* hash table containing this type */
+ char *name; /* without module */
+ char *name_full; /* with module */
geas_cd_access access;
- gchar *filename;
+ char *filename;
geas_cd_datatype datatype;
int format; /* only for char types */
geas_cd_class *refclass; /* only for reference and list types */
@@ -65,7 +66,16 @@
struct _geas_cd_field
{
- gchar *name;
+ GHashTable *parent; /* hash table containing this field */
+ char *name; /* without module */
+ char *name_full; /* with module */
+ char *name_db; /* name of column in the database */
+ geas_cd_datatype datatype;
+ int format; /* only for char types */
+ geas_cd_class *refclass; /* only for reference and list types */
+ GHashTable *fields; /* only for compound types */
+ char *default_val; /* not for ref., list and compound */
+ int property;
};
/* ========================================================================= *\
@@ -79,90 +89,18 @@
static GHashTable *_modules = NULL;
/* ========================================================================= *\
- * Private functions
+ * Prototypes
\* ========================================================================= */
-
-/* ------------------------------------------------------------------------- *\
- * Free the memory allocated for a class
-\* ------------------------------------------------------------------------- */
-static void
-_free_class (geas_cd_class *c)
-{
- g_free (c->name);
- /* TODO: free all fields */
- g_hash_table_destroy (c->fields);
- g_free (c);
-}
-
-/* ------------------------------------------------------------------------- *\
- * Free the memory allocated for a type
-\* ------------------------------------------------------------------------- */
-static void
-_free_type (geas_cd_type *t)
-{
- g_free (t->name);
- if (t->fields)
- {
- /* TODO: free all fields */
- g_hash_table_destroy (t->fields);
- }
- g_free (t);
-}
-
-/* ------------------------------------------------------------------------- *\
- * Free a class (called in g_hash_table_foreach_remove)
-\* ------------------------------------------------------------------------- */
-static gboolean
-_hr_free_class (gpointer key, gpointer value, gpointer user_data)
-{
- _free_class ((geas_cd_class *)value);
- return (TRUE);
-}
-
-/* ------------------------------------------------------------------------- *\
- * Free a type (called in g_hash_table_foreach_remove)
-\* ------------------------------------------------------------------------- */
-static gboolean
-_hr_free_type (gpointer key, gpointer value, gpointer user_data)
-{
- _free_type ((geas_cd_type *)value);
- return (TRUE);
-}
-/* ------------------------------------------------------------------------- *\
- * Free the memory allocated for a module
-\* ------------------------------------------------------------------------- */
-static void
-_free_module (geas_cd_module *m)
-{
- g_free (m->name);
-
- g_hash_table_foreach_remove (m->classes, _hr_free_class, NULL);
- g_hash_table_foreach_remove (m->types, _hr_free_type, NULL);
+static gboolean _hr_free_module (gpointer key, gpointer value,
+ gpointer user_data);
+static gboolean _hr_free_class (gpointer key, gpointer value,
+ 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);
- g_hash_table_destroy (m->classes);
- g_hash_table_destroy (m->types);
-
- g_free (m);
-}
-
-/* ------------------------------------------------------------------------- *\
- * Free a module except "builtin" (called in g_hash_table_foreach_remove)
-\* ------------------------------------------------------------------------- */
-static gboolean
-_hr_free_module (gpointer key, gpointer value, gpointer user_data)
-{
- if (strcmp (key, "builtin"))
- {
- _free_module ((geas_cd_module *)value);
- return (TRUE);
- }
- else
- {
- return (FALSE);
- }
-}
-
/* ========================================================================= *\
* General functions
\* ========================================================================= */
@@ -204,6 +142,8 @@
void
geas_cd_clean (void)
{
+ g_return_if_fail (_modules);
+
g_hash_table_foreach_remove (_modules, _hr_free_module, NULL);
g_assert (g_hash_table_size (_modules) == 1);
}
@@ -273,6 +213,7 @@
{
geas_cd_class *c;
+ g_return_val_if_fail (m, NULL);
g_return_val_if_fail (name, NULL);
c = geas_cd_module_find_class (m, name);
@@ -280,9 +221,10 @@
{
c = g_new0 (geas_cd_class, 1);
- c->module = m;
+ c->parent = m->classes;
c->name = g_strdup (name);
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);
@@ -298,6 +240,9 @@
geas_cd_class *
geas_cd_module_find_class (const geas_cd_module *m, const char *name)
{
+ g_return_val_if_fail (m, NULL);
+ g_return_val_if_fail (name, NULL);
+
return (g_hash_table_lookup (m->classes, name));
}
@@ -310,6 +255,7 @@
{
geas_cd_type *t;
+ g_return_val_if_fail (m, NULL);
g_return_val_if_fail (name, NULL);
t = geas_cd_module_find_type (m, name);
@@ -317,7 +263,7 @@
{
t = g_new0 (geas_cd_type, 1);
- t->module = m;
+ t->parent = m->types;
t->name = g_strdup (name);
t->name_full = g_strconcat (m->name, "::", name, NULL);
t->access = (access == GEAS_CD_ACCESS_DEFAULT) ? m->access : access;
@@ -339,10 +285,47 @@
geas_cd_type *
geas_cd_module_find_type (const geas_cd_module *m, const char *name)
{
+ g_return_val_if_fail (m, NULL);
+ g_return_val_if_fail (name, NULL);
+
return (g_hash_table_lookup (m->types, name));
}
/* ------------------------------------------------------------------------- *\
+ * Free the memory allocated for a module
+\* ------------------------------------------------------------------------- */
+static void
+_free_module (geas_cd_module *m)
+{
+ g_free (m->name);
+
+ g_hash_table_foreach_remove (m->classes, _hr_free_class, NULL);
+ g_hash_table_foreach_remove (m->types, _hr_free_type, NULL);
+
+ g_hash_table_destroy (m->classes);
+ g_hash_table_destroy (m->types);
+
+ g_free (m);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * Free a module except "builtin" (called in g_hash_table_foreach_remove)
+\* ------------------------------------------------------------------------- */
+static gboolean
+_hr_free_module (gpointer key, gpointer value, gpointer user_data)
+{
+ if (strcmp (key, "builtin"))
+ {
+ _free_module ((geas_cd_module *)value);
+ return (TRUE);
+ }
+ else
+ {
+ return (FALSE);
+ }
+}
+
+/* ------------------------------------------------------------------------- *\
* Free a module
\* ------------------------------------------------------------------------- */
void
@@ -394,6 +377,17 @@
}
/* ------------------------------------------------------------------------- *\
+ * Get the name of the table in the database belonging to this class
+\* ------------------------------------------------------------------------- */
+const char *
+geas_cd_class_get_name_db (const geas_cd_class *c)
+{
+ g_return_val_if_fail (c, NULL);
+
+ return (c->name_db);
+}
+
+/* ------------------------------------------------------------------------- *\
* Get the filename where a class is stored
\* ------------------------------------------------------------------------- */
const char *
@@ -405,6 +399,100 @@
}
/* ------------------------------------------------------------------------- *\
+ * Allocate a new field as a member of a class
+ * One of the parameters type, reference or list must be filled in
+\* ------------------------------------------------------------------------- */
+geas_cd_field *
+geas_cd_class_new_field (geas_cd_class *c, const geas_cd_module *m,
+ const char *name, const geas_cd_type *type,
+ geas_cd_class *reference, geas_cd_class *list)
+{
+ geas_cd_field *f;
+
+ g_return_val_if_fail (c, NULL);
+ g_return_val_if_fail (m, NULL);
+ g_return_val_if_fail (name, NULL);
+ g_return_val_if_fail (type || reference || list, NULL);
+
+ f = geas_cd_class_find_field (c, name);
+ if (!f)
+ {
+ f = g_new0 (geas_cd_field, 1);
+
+ f->parent = c->fields;
+ f->name = g_strdup (name);
+ f->name_full = g_strconcat (m->name, "::", name, NULL);
+ f->name_db = g_strconcat (m->name, "__", name, NULL);
+
+ if (type)
+ {
+ f->datatype = type->datatype;
+ f->format = type->format;
+ f->refclass = type->refclass;
+
+ if (f->datatype == GEAS_CD_DATATYPE_COMPOUND)
+ {
+ f->fields = g_hash_table_new (g_str_hash, g_str_equal);
+ }
+ }
+ else if (reference)
+ {
+ f->datatype = GEAS_CD_DATATYPE_REFERENCE;
+ f->refclass = reference;
+ }
+ else if (list)
+ {
+ f->datatype = GEAS_CD_DATATYPE_LIST;
+ f->refclass = list;
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+
+ g_hash_table_insert (c->fields, f->name, f);
+ }
+
+ return (f);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * Find an existing field in a class by name
+\* ------------------------------------------------------------------------- */
+geas_cd_field *
+geas_cd_class_find_field (const geas_cd_class *c, const char *name)
+{
+ g_return_val_if_fail (c, NULL);
+ g_return_val_if_fail (name, NULL);
+
+ return (g_hash_table_lookup (c->fields, name));
+}
+
+/* ------------------------------------------------------------------------- *\
+ * Free the memory allocated for a class
+\* ------------------------------------------------------------------------- */
+static void
+_free_class (geas_cd_class *c)
+{
+ 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);
+ g_free (c);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * Free a class (called in g_hash_table_foreach_remove)
+\* ------------------------------------------------------------------------- */
+static gboolean
+_hr_free_class (gpointer key, gpointer value, gpointer user_data)
+{
+ _free_class ((geas_cd_class *)value);
+ return (TRUE);
+}
+
+/* ------------------------------------------------------------------------- *\
* Free a class
\* ------------------------------------------------------------------------- */
void
@@ -412,7 +500,7 @@
{
g_return_if_fail (c);
- g_hash_table_remove (c->module->classes, c->name);
+ g_hash_table_remove (c->parent, c->name);
_free_class (c);
}
@@ -500,6 +588,32 @@
}
/* ------------------------------------------------------------------------- *\
+ * Free the memory allocated for a type
+\* ------------------------------------------------------------------------- */
+static void
+_free_type (geas_cd_type *t)
+{
+ g_free (t->name);
+ g_free (t->name_full);
+ if (t->fields)
+ {
+ g_hash_table_foreach_remove (t->fields, _hr_free_field, NULL);
+ g_hash_table_destroy (t->fields);
+ }
+ g_free (t);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * Free a type (called in g_hash_table_foreach_remove)
+\* ------------------------------------------------------------------------- */
+static gboolean
+_hr_free_type (gpointer key, gpointer value, gpointer user_data)
+{
+ _free_type ((geas_cd_type *)value);
+ return (TRUE);
+}
+
+/* ------------------------------------------------------------------------- *\
* Free a type
\* ------------------------------------------------------------------------- */
void
@@ -507,16 +621,52 @@
{
g_return_if_fail (t);
- g_hash_table_remove (t->module->types, t->name);
+ g_hash_table_remove (t->parent, t->name);
_free_type (t);
}
-/* TODO: */
-/*
-geas_cd_field *
-geas_cd_field_new (const char *name)
+/* ========================================================================= *\
+ * Fields
+\* ========================================================================= */
+
+/* ------------------------------------------------------------------------- *\
+ * Free the memory allocated for a field
+\* ------------------------------------------------------------------------- */
+static void
+_free_field (geas_cd_field *f)
+{
+ g_free (f->name);
+ g_free (f->name_full);
+ g_free (f->name_db);
+ g_free (f->default_val);
+ if (f->fields)
+ {
+ g_hash_table_foreach_remove (f->fields, _hr_free_field, NULL);
+ g_hash_table_destroy (f->fields);
+ }
+ g_free (f);
+}
+/* ------------------------------------------------------------------------- *\
+ * 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);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * Free a field
+\* ------------------------------------------------------------------------- */
void
geas_cd_field_free (geas_cd_field *f)
-*/
+{
+ g_return_if_fail (f);
+
+ g_hash_table_remove (f->parent, f->name);
+
+ _free_field (f);
+}
Index: gnue/geas/src/classdef/classdef.h
diff -u gnue/geas/src/classdef/classdef.h:1.8
gnue/geas/src/classdef/classdef.h:1.9
--- gnue/geas/src/classdef/classdef.h:1.8 Sat Sep 22 18:32:13 2001
+++ gnue/geas/src/classdef/classdef.h Fri Sep 28 14:37:37 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.h,v 1.8 2001/09/22 22:32:13 reinhard Exp $
+ $Id: classdef.h,v 1.9 2001/09/28 18:37:37 reinhard Exp $
*/
/* ------------------------------------------------------------------------- *\
@@ -87,11 +87,21 @@
* Classes
\* ------------------------------------------------------------------------- */
-void geas_cd_class_set_filename (geas_cd_class *c, const char
*filename);
-const char *geas_cd_class_get_name (const geas_cd_class *c);
-const char *geas_cd_class_get_name_full (const geas_cd_class *c);
-const char *geas_cd_class_get_filename (const geas_cd_class *c);
-void geas_cd_class_free (geas_cd_class *c);
+void geas_cd_class_set_filename (geas_cd_class *c,
+ const char *filename);
+const char *geas_cd_class_get_name (const geas_cd_class *c);
+const char *geas_cd_class_get_name_full (const geas_cd_class *c);
+const char *geas_cd_class_get_name_db (const geas_cd_class *c);
+const char *geas_cd_class_get_filename (const geas_cd_class *c);
+geas_cd_field *geas_cd_class_new_field (geas_cd_class *c,
+ const geas_cd_module *m,
+ const char *name,
+ const geas_cd_type *type,
+ geas_cd_class *reference,
+ geas_cd_class *list);
+geas_cd_field *geas_cd_class_find_field (const geas_cd_class *c,
+ const char *name);
+void geas_cd_class_free (geas_cd_class *c);
/* ------------------------------------------------------------------------- *\
* Types (can be plain type or structured type)