commit-gnue
[Top][All Lists]
Advanced

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

gnue/geas/src/config configuration.c


From: Reinhard Mueller
Subject: gnue/geas/src/config configuration.c
Date: Wed, 06 Jun 2001 15:12:12 -0700

CVSROOT:        /cvs
Module name:    gnue
Changes by:     Reinhard Mueller <address@hidden>       01/06/06 15:12:12

Modified files:
        geas/src/config: configuration.c 

Log message:
        continued with adding comments and assertions

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/geas/src/config/configuration.c.diff?cvsroot=OldCVS&tr1=1.12&tr2=1.13&r1=text&r2=text

Patches:
Index: gnue/geas/src/config/configuration.c
diff -u gnue/geas/src/config/configuration.c:1.12 
gnue/geas/src/config/configuration.c:1.13
--- gnue/geas/src/config/configuration.c:1.12   Fri Jun  1 06:43:09 2001
+++ gnue/geas/src/config/configuration.c        Wed Jun  6 15:12:12 2001
@@ -19,9 +19,19 @@
    along with GEAS; if not, write to the Free Software Foundation, Inc.,
    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-   $Id: configuration.c,v 1.12 2001/06/01 13:43:09 reinhard Exp $
+   $Id: configuration.c,v 1.13 2001/06/06 22:12:12 reinhard Exp $
 */
 
+/* ------------------------------------------------------------------------- *\
+ * FIXME:
+ * Use a prefix for all symbols in this library
+ * Load configuration dynamically so that changes in the configuration file
+   become valid without restarting the server (not sure if this is good)
+ * Use GHashTable instead of GList for key/value pairs
+ * Make configuration_data external and use that instead of configuration -
+   this will obsolete several typecasts
+\* ------------------------------------------------------------------------- */
+
 /*
  * This library is an API for GEAS to use to read configuration details.
  * the internals are subject to change at any time, and will be what
@@ -74,8 +84,10 @@
 struct _configuration_data
 {
   char *name;
-  GList *database;
-  GList *globals;
+  GList *database;                      /* contains a configuration_data
+                                           structure for each database */
+  GList *globals;                       /* contains the global key/value
+                                           pairs */
 };
 
 typedef struct _configuration_data configuration_data;
@@ -93,31 +105,37 @@
 \* ========================================================================= */
 
 /* ------------------------------------------------------------------------- *\
- * 
+ * Allocate a new configuration item in memory
 \* ------------------------------------------------------------------------- */
 static configuration_data *
 alloc_configuration_data (const char *name)
 {
-  configuration_data *c =
-    (configuration_data *) g_malloc0 (sizeof (configuration_data));
-  if (c)
-    {
-      c->name = g_strdup (name);
-      c->globals = NULL;
-      c->database = NULL;
-    }
+  configuration_data *c;
+
+  g_assert (name);
+
+  c = g_new0 (configuration_data, 1);
+
+  c->name = g_strdup (name);
+  c->database = NULL;
+  c->globals = NULL;
+
   return (c);
 }
 
 /* ------------------------------------------------------------------------- *\
- * 
+ * Find the configuration for a database
 \* ------------------------------------------------------------------------- */
 static configuration_data *
 find_database (configuration_data *c, const char *name)
 {
-  configuration_data *i = NULL;
-  GList *l = c->database;
+  configuration_data *i;
+  GList *l;
 
+  g_assert (c);
+  g_assert (name);
+  
+  l = c->database;
   while (l)
     {
       i = (configuration_data *) l->data;
@@ -129,127 +147,113 @@
 }
 
 /* ------------------------------------------------------------------------- *\
- * 
+ * Free a configuration item in memory
 \* ------------------------------------------------------------------------- */
 static void
-free_config_item (config_item * item)
+free_config_item (config_item *item)
 {
   if (item)
     {
-      if (item->key)
-       g_free (item->key);
-      if (item->value)
-       g_free (item->value);
+      g_free (item->key);
+      g_free (item->value);
       g_free (item);
     }
 }
 
 /* ------------------------------------------------------------------------- *\
- * 
+ * Allocate a configuration item in memory
 \* ------------------------------------------------------------------------- */
 static config_item *
 alloc_config_item (const char *key, const char *value)
 {
-  config_item *i = (config_item *) g_malloc0 (sizeof (config_item));
+  config_item *i;
   char *p;
   char tmp = '\0';
+
+  g_assert (key);
+  g_assert (value);
+
+  i = g_new0 (config_item, 1);
+
+  i->key = g_strdup (key);
+  g_strdown (i->key);
 
-  if (i)
+  /* if there's an extra space, truncate line */
+  p = strchr (value, ' ');
+  if (p && isspace (*p))
     {
-      i->key = g_strdup (key);
-      p = i->key;
-      while (p != NULL && *p != '\0')
-       {
-         *p = tolower (*p);
-         p++;
-       }
-
-      if (value)
-       {
-         /* if there's an extra space, truncate line */
-         p = strchr (value, ' ');
-         if (p && isspace (*p))
-           {
-             fprintf (stderr,
-                      "warning: option '%s' has been truncated from '%s' ",
-                      key, value);
-             tmp = *p;
-             *p = '\0';
-             fprintf (stderr, "to '%s'\n", value);
-           }
-         i->value = g_strdup (value);
-         if (p)
-           *p = tmp;
-       }
-      else
-       i->value = g_strdup (value);
-      if (!i->key || !i->value)
-       {
-         free_config_item (i);
-         return (NULL);
-       }
+      fprintf (stderr,
+               "warning: option '%s' has been truncated from '%s' ",
+               key, value);
+      tmp = *p;
+      *p = '\0';
+      fprintf (stderr, "to '%s'\n", value);
     }
+  i->value = g_strdup (value);
+  if (p)
+    *p = tmp;
+
   return (i);
 }
 
 /* ------------------------------------------------------------------------- *\
- * use this to add a key/value pai to a section
+ * Add a key/value pair to a section
 \* ------------------------------------------------------------------------- */
 static void
-add_section_option (configuration_data * c, const char *key,
-                   const char *value)
+add_section_option (configuration_data *c, const char *key, const char *value)
 {
   config_item *i;
 
-  /* ignore duplicates */
-  if (get_global_option ((configuration) c, key) != NULL)
-    return;
-  /* allocate and store new data */
-  i = alloc_config_item (key, value);
-  if (i)
+  g_assert (c);
+  g_assert (key);
+  g_assert (value);
 
-    c->globals = g_list_append (c->globals, i);
+  if (!get_global_option ((configuration) c, key))  /* ignore duplicates */
+    {
+      i = alloc_config_item (key, value);
+      c->globals = g_list_append (c->globals, i);
+    }
 }
 
 /* ------------------------------------------------------------------------- *\
- * 
+ * Add a database to the configuration
 \* ------------------------------------------------------------------------- */
 static void
-add_database (configuration_data * c, const char *name)
+add_database (configuration_data *c, const char *name)
 {
   configuration_data *i;
+
+  g_assert (c);
+  g_assert (name);
 
-  /* ignore duplicates */
-  if (find_database ((configuration) c, name) != NULL)
-    return;
-  /* allocate and store new data */
-  i = alloc_configuration_data (name);
-  if (i)
-    c->database = g_list_append (c->database, i);
+  if (!find_database ((configuration) c, name))  /* ignore duplicates */
+    {
+      i = alloc_configuration_data (name);
+      c->database = g_list_append (c->database, i);
+    }
 }
 
 /* ------------------------------------------------------------------------- *\
- * 
+ * Add a key/value pair to a database configuration
 \* ------------------------------------------------------------------------- */
 static void
-add_database_option (configuration_data * c,
+add_database_option (configuration_data *c,
                     const char *name, const char *key, const char *value)
 {
   configuration_data *i;
+
+  g_assert (c);
+  g_assert (name);
+  g_assert (key);
+  g_assert (value);
 
-  /* ignore duplicates */
+  /* add database to config if not yet there */
   i = find_database ((configuration) c, key);
-  if (i == NULL)
+  if (!i)
     {
       add_database (c, name);
       i = find_database ((configuration) c, name);
-      if (!i)
-       {
-         printf ("error: failed to add option '%s' in database '%s'\n",
-                 key, name);
-         config_failed = 1;
-         return;
-       }
+      g_assert (i);
     }
 
   /* allocate and store new data */



reply via email to

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