From f62abb6ec40a3d996f20b866364cc5e499d0ba20 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Tue, 6 Jan 2009 22:05:49 -0200 Subject: [PATCH] Added configuration backend using hash tables 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 | 1 + config/Makefile.am | 2 +- config/ghashtable/Makefile.am | 3 ++ config/ghashtable/ghashtable.c | 93 ++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 config/ghashtable/Makefile.am create mode 100644 config/ghashtable/ghashtable.c diff --git a/atom/Makefile.am b/atom/Makefile.am index c929f60..ad734e5 100644 --- a/atom/Makefile.am +++ b/atom/Makefile.am @@ -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 diff --git a/config/Makefile.am b/config/Makefile.am index b92f8f5..c81fcf3 100644 --- a/config/Makefile.am +++ b/config/Makefile.am @@ -1 +1 @@ -SUBDIRS = gkeyfile +SUBDIRS = gkeyfile ghashtable diff --git a/config/ghashtable/Makefile.am b/config/ghashtable/Makefile.am new file mode 100644 index 0000000..765562f --- /dev/null +++ b/config/ghashtable/Makefile.am @@ -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 index 0000000..59f42f4 --- /dev/null +++ b/config/ghashtable/ghashtable.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2007-2009 Thadeu Lima de Souza Cascardo + * + * + * 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 +#include + +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; +} -- 2.20.1