Insert key and value in the group table, not the main table.
[cascardo/atompub.git] / config / ghashtable / ghashtable.c
1 /*
2  *  Copyright (C) 2007-2009 Thadeu Lima de Souza Cascardo
3  *  <cascardo@holoscopio.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20
21 #include <atompub/atom.h>
22 #include <glib.h>
23
24 static void
25 ghashtable_atom_config_set (AtomCtx *ctx, char *group, char *key,
26                             gpointer val)
27 {
28   GHashTable *table;
29   GHashTable *grouptable;
30   gpointer value;
31   table = atom_config_data (atom_config (ctx));
32   if (!g_hash_table_lookup_extended (table, group, NULL,
33                                      (gpointer *) &grouptable))
34     {
35       grouptable = g_hash_table_new_full (g_str_hash, g_str_equal,
36                                           g_free, NULL);
37       g_hash_table_replace (table, g_strdup (group), grouptable);
38     }
39   g_hash_table_replace (grouptable, g_strdup (key), val);
40 }
41
42 void
43 ghashtable_atom_config_set_str (AtomCtx *ctx, char *group, char *key,
44                                 char *val)
45 {
46   ghashtable_atom_config_set (ctx, group, key, val);
47 }
48
49 static gpointer
50 ghashtable_atom_config_get (AtomCtx *ctx, char *group, char *key)
51 {
52   GHashTable *table;
53   GHashTable *grouptable;
54   gpointer value;
55   table = atom_config_data (atom_config (ctx));
56   if (!g_hash_table_lookup_extended (table, group, NULL,
57                                      (gpointer *) &grouptable))
58     return NULL;
59   if (!g_hash_table_lookup_extended (grouptable, key, NULL, &value))
60     return NULL;
61   return value;
62 }
63
64 static gchar *
65 ghashtable_atom_config_get_str (AtomCtx *ctx, gchar *group, gchar * key)
66 {
67   GHashTable *table;
68   GHashTable *grouptable;
69   gchar *value;
70   value = ghashtable_atom_config_get (ctx, group, key);
71   if (value == NULL)
72     {
73       AtomError *aerr = atom_error_new ();
74       atom_error_code_set (aerr, 500);
75       atom_error_message_set (aerr, "Could not find configuration value");
76       atom_error_set (ctx, aerr);
77     }
78   return value;
79 }
80
81 AtomConfig *
82 ghashtable_atom_config_init (AtomCtx *ctx)
83 {
84   AtomConfig *config;
85   GHashTable *table;
86   GError *error = NULL;
87   table = g_hash_table_new_full (g_str_hash, g_str_equal,
88                                  g_free, g_hash_table_destroy);
89   config = atom_config_new ();
90   atom_config_get_str_set (config, ghashtable_atom_config_get_str);
91   atom_config_data_set (config, table);
92   return config;
93 }