Added configuration backend using hash tables
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 7 Jan 2009 00:05:49 +0000 (22:05 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 7 Jan 2009 00:12:01 +0000 (22:12 -0200)
Using GHashTable from GLib, store the configuration in memory, requiring
the user to set all required configuration values by the configuration
users.

atom/Makefile.am
config/Makefile.am
config/ghashtable/Makefile.am [new file with mode: 0644]
config/ghashtable/ghashtable.c [new file with mode: 0644]

index c929f60..ad734e5 100644 (file)
@@ -10,4 +10,5 @@ libatom_la_LIBADD += -L$(top_builddir)/backend/gio -lgio
 libatom_la_LIBADD += -L$(top_builddir)/backend/soup -lsoup
 libatom_la_LIBADD += -L$(top_builddir)/frontend/cgi -lcgi
 libatom_la_LIBADD += -L$(top_builddir)/config/gkeyfile -lgkeyfile
+libatom_la_LIBADD += -L$(top_builddir)/config/ghashtable -lghashtable
 libatom_la_LIBADD += -L$(top_builddir)/iri -liri
index b92f8f5..c81fcf3 100644 (file)
@@ -1 +1 @@
-SUBDIRS = gkeyfile
+SUBDIRS = gkeyfile ghashtable
diff --git a/config/ghashtable/Makefile.am b/config/ghashtable/Makefile.am
new file mode 100644 (file)
index 0000000..765562f
--- /dev/null
@@ -0,0 +1,3 @@
+noinst_LTLIBRARIES = libghashtable.la
+libghashtable_la_SOURCES = ghashtable.c
+libghashtable_la_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS)
diff --git a/config/ghashtable/ghashtable.c b/config/ghashtable/ghashtable.c
new file mode 100644 (file)
index 0000000..59f42f4
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ *  Copyright (C) 2007-2009 Thadeu Lima de Souza Cascardo
+ *  <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <atompub/atom.h>
+#include <glib.h>
+
+static void
+ghashtable_atom_config_set (AtomCtx *ctx, char *group, char *key,
+                            gpointer val)
+{
+  GHashTable *table;
+  GHashTable *grouptable;
+  gpointer value;
+  table = atom_config_data (atom_config (ctx));
+  if (!g_hash_table_lookup_extended (table, group, NULL,
+                                     (gpointer *) &grouptable))
+    {
+      grouptable = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                          g_free, NULL);
+      g_hash_table_replace (table, g_strdup (group), grouptable);
+    }
+  g_hash_table_replace (table, g_strdup (key), val);
+}
+
+void
+ghashtable_atom_config_set_str (AtomCtx *ctx, char *group, char *key,
+                                char *val)
+{
+  ghashtable_atom_config_set (ctx, group, key, val);
+}
+
+static gpointer
+ghashtable_atom_config_get (AtomCtx *ctx, char *group, char *key)
+{
+  GHashTable *table;
+  GHashTable *grouptable;
+  gpointer value;
+  table = atom_config_data (atom_config (ctx));
+  if (!g_hash_table_lookup_extended (table, group, NULL,
+                                     (gpointer *) &grouptable))
+    return NULL;
+  if (!g_hash_table_lookup_extended (grouptable, key, NULL, &value))
+    return NULL;
+  return value;
+}
+
+static gchar *
+ghashtable_atom_config_get_str (AtomCtx *ctx, gchar *group, gchar * key)
+{
+  GHashTable *table;
+  GHashTable *grouptable;
+  gchar *value;
+  value = ghashtable_atom_config_get (ctx, group, key);
+  if (value == NULL)
+    {
+      AtomError *aerr = atom_error_new ();
+      atom_error_code_set (aerr, 500);
+      atom_error_message_set (aerr, "Could not find configuration value");
+      atom_error_set (ctx, aerr);
+    }
+  return value;
+}
+
+AtomConfig *
+ghashtable_atom_config_init (AtomCtx *ctx)
+{
+  AtomConfig *config;
+  GHashTable *table;
+  GError *error = NULL;
+  table = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                 g_free, g_hash_table_destroy);
+  config = atom_config_new ();
+  atom_config_get_str_set (config, ghashtable_atom_config_get_str);
+  atom_config_data_set (config, table);
+  return config;
+}