Added configuration backend using hash tables
[cascardo/atompub.git] / config / ghashtable / ghashtable.c
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;
+}