From da983bb34f38c4744cc6da34bbb2bd16c95a2543 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Wed, 10 Dec 2008 10:02:47 -0200 Subject: [PATCH] Let configuration mechanism be extensible The configuration system now may be implemented with different backends. The only function provided by now is still the get_str function. For the keyfile configuration system, the filename may also be provided now. --- atom/config.c | 48 +++++++++++++++++++++++++++++++++++----- atom/ctx.c | 14 ++++++------ config/gkeyfile.c | 18 +++++++++------ include/atompub/config.h | 13 +++++++++-- include/atompub/ctx.h | 2 -- src/main.c | 3 ++- 6 files changed, 74 insertions(+), 24 deletions(-) diff --git a/atom/config.c b/atom/config.c index 833ae18..52755b0 100644 --- a/atom/config.c +++ b/atom/config.c @@ -19,17 +19,55 @@ #include -extern void gkeyfile_atom_config_init (AtomCtx *); -extern char *gkeyfile_atom_config_get_str (AtomCtx *, char *, char *); +#include + +struct _atom_config +{ + void * data; + char * (*get_str) (AtomCtx *, char *, char *); +}; + +AtomConfig * +atom_config_new (void) +{ + AtomConfig * config; + config = g_slice_new (AtomConfig); + config->data = NULL; + config->get_str = NULL; + return config; +} + +void +atom_config_delete (AtomConfig *config) +{ + g_slice_free (AtomConfig, config); +} + +void +atom_config_get_str_set (AtomConfig * config, + char * get_str (AtomCtx *, char *, char *)) +{ + config->get_str = get_str; +} void -atom_config_init (AtomCtx *ctx) +atom_config_data_set (AtomConfig * config, void * data) +{ + config->data = data; +} + +void * +atom_config_data (AtomConfig * config) { - gkeyfile_atom_config_init (ctx); + return config->data; } char * atom_config_get_str (AtomCtx *ctx, char *namespace, char *key) { - return gkeyfile_atom_config_get_str (ctx, namespace, key); + AtomConfig *config; + config = atom_config (ctx); + if (config && config->get_str) + return config->get_str (ctx, namespace, key); + return NULL; } diff --git a/atom/ctx.c b/atom/ctx.c index 37a5a0d..d5011e2 100644 --- a/atom/ctx.c +++ b/atom/ctx.c @@ -24,7 +24,7 @@ struct _atom_ctx { AtomError *error; - gpointer config_data; + AtomConfig *config; AtomBackend *backend; AtomFrontend *frontend; GHashTable *bemap; @@ -37,7 +37,7 @@ atom_ctx_new () AtomCtx *ctx; ctx = g_slice_new (AtomCtx); ctx->error = NULL; - ctx->config_data = NULL; + ctx->config = NULL; ctx->backend = NULL; ctx->frontend = NULL; ctx->bemap = g_hash_table_new_full (g_str_hash, g_str_equal, @@ -77,16 +77,16 @@ atom_error_get (AtomCtx *ctx) return ctx->error; } -void * -atom_config_data (AtomCtx *ctx) +AtomConfig * +atom_config (AtomCtx *ctx) { - return ctx->config_data; + return ctx->config; } void -atom_config_data_set (AtomCtx *ctx, void *data) +atom_config_set (AtomCtx *ctx, AtomConfig *config) { - ctx->config_data = data; + ctx->config = config; } AtomBackend * diff --git a/config/gkeyfile.c b/config/gkeyfile.c index 891f4b5..358354d 100644 --- a/config/gkeyfile.c +++ b/config/gkeyfile.c @@ -1,5 +1,6 @@ /* - * Copyright (C) 2007 Thadeu Lima de Souza Cascardo + * Copyright (C) 2007-2008 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 @@ -20,13 +21,13 @@ #include #include -gchar * +static gchar * gkeyfile_atom_config_get_str (AtomCtx *ctx, gchar *group, gchar * key) { GKeyFile *keyfile; gchar *value; GError *error = NULL; - keyfile = atom_config_data (ctx); + keyfile = atom_config_data (atom_config (ctx)); value = g_key_file_get_string (keyfile, group, key, &error); if (value == NULL) { @@ -39,13 +40,14 @@ gkeyfile_atom_config_get_str (AtomCtx *ctx, gchar *group, gchar * key) return value; } -void -gkeyfile_atom_config_init (AtomCtx *ctx) +AtomConfig * +gkeyfile_atom_config_init (AtomCtx *ctx, char *name) { + AtomConfig *config; GKeyFile *keyfile; GError *error = NULL; keyfile = g_key_file_new (); - if (!g_key_file_load_from_file (keyfile, "atompub.conf", + if (!g_key_file_load_from_file (keyfile, name, G_KEY_FILE_NONE, &error)) { AtomError *aerr = atom_error_new (); @@ -55,5 +57,7 @@ gkeyfile_atom_config_init (AtomCtx *ctx) g_error_free (error); return; } - atom_config_data_set (ctx, keyfile); + config = atom_config_new (); + atom_config_get_str_set (config, gkeyfile_atom_config_get_str); + atom_config_data_set (config, keyfile); } diff --git a/include/atompub/config.h b/include/atompub/config.h index d5f3fad..071f31a 100644 --- a/include/atompub/config.h +++ b/include/atompub/config.h @@ -22,7 +22,16 @@ #include -void atom_config_init (AtomCtx *); -char *atom_config_get_str (AtomCtx *, char *, char *); +typedef struct _atom_config AtomConfig; + +AtomConfig * atom_config_new (void); +void atom_config_delete (AtomConfig *); +void atom_config_get_str_set (AtomConfig *, + char * (AtomCtx *, char *, char *)); +void atom_config_data_set (AtomConfig *, void *); +void * atom_config_data (AtomConfig *); +char * atom_config_get_str (AtomCtx *, char *, char *); +AtomConfig * atom_config (AtomCtx *); +void atom_config_set (AtomCtx *, AtomConfig *); #endif diff --git a/include/atompub/ctx.h b/include/atompub/ctx.h index 35ecca6..243ef5c 100644 --- a/include/atompub/ctx.h +++ b/include/atompub/ctx.h @@ -24,7 +24,5 @@ typedef struct _atom_ctx AtomCtx; AtomCtx * atom_ctx_new (void); void atom_ctx_delete (AtomCtx *); -void * atom_config_data (AtomCtx *); -void atom_config_data_set (AtomCtx *, void *); #endif diff --git a/src/main.c b/src/main.c index a47a352..659f86a 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,7 @@ extern void cgi_serve_request (AtomCtx *ctx); extern AtomBackend * gio_backend (void); extern AtomFrontend * cgi_frontend (void); +extern AtomConfig * gkeyfile_atom_config_init (AtomCtx *, char *); int main (int argc, char **argv) @@ -35,7 +36,7 @@ main (int argc, char **argv) AtomError *error; g_type_init (); ctx = atom_ctx_new (); - atom_config_init (ctx); + atom_config_set (ctx, gkeyfile_atom_config_init (ctx, "atompub.conf")); atom_backend_set (ctx, gio_backend ()); atom_frontend_set (ctx, cgi_frontend ()); if ((error = atom_error_get (ctx)) != NULL) -- 2.20.1