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.
#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;
}
struct _atom_ctx
{
AtomError *error;
- gpointer config_data;
+ AtomConfig *config;
AtomBackend *backend;
AtomFrontend *frontend;
GHashTable *bemap;
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,
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 *
/*
- * 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)
{
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 ();
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);
}
#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
AtomCtx * atom_ctx_new (void);
void atom_ctx_delete (AtomCtx *);
-void * atom_config_data (AtomCtx *);
-void atom_config_data_set (AtomCtx *, void *);
#endif
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)
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)