Let configuration mechanism be extensible
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 10 Dec 2008 12:02:47 +0000 (10:02 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 10 Dec 2008 12:29:46 +0000 (10:29 -0200)
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
atom/ctx.c
config/gkeyfile.c
include/atompub/config.h
include/atompub/ctx.h
src/main.c

index 833ae18..52755b0 100644 (file)
 
 #include <atompub/atom.h>
 
-extern void gkeyfile_atom_config_init (AtomCtx *);
-extern char *gkeyfile_atom_config_get_str (AtomCtx *, char *, char *);
+#include <glib.h>
+
+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;
 }
index 37a5a0d..d5011e2 100644 (file)
@@ -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 *
index 891f4b5..358354d 100644 (file)
@@ -1,5 +1,6 @@
 /*
- *  Copyright (C) 2007  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *  Copyright (C) 2007-2008 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
 #include <atompub/atom.h>
 #include <glib.h>
 
-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);
 }
index d5f3fad..071f31a 100644 (file)
 
 #include <atompub/ctx.h>
 
-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
index 35ecca6..243ef5c 100644 (file)
@@ -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
index a47a352..659f86a 100644 (file)
@@ -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)