Merge pull request #1 from atsampson/master
[calfbox.git] / config-api.c
blobb97ac2e9854246b999c2c835694f37db47e4d99e
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010 Krzysztof Foltman
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "config-api.h"
21 #include <errno.h>
22 #include <math.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 static GKeyFile *config_keyfile;
28 static gchar *keyfile_name;
29 static GStringChunk *cfg_strings = NULL;
30 static GHashTable *config_sections_hash = NULL;
32 void cbox_config_init(const char *override_file)
34 const gchar *keyfiledirs[3];
35 const gchar *keyfilename = ".cboxrc";
36 GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
37 GError *error = NULL;
38 if (config_keyfile)
39 return;
41 config_sections_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
43 cfg_strings = g_string_chunk_new(100);
44 config_keyfile = g_key_file_new();
46 // Allow virtual (in-memory) config by passing empty string
47 if (override_file && !*override_file)
49 keyfile_name = g_strdup("");
50 return;
53 keyfiledirs[0] = getenv("HOME");
54 keyfiledirs[1] = NULL;
55 // XXXKF add proper error handling
57 if (override_file)
59 if (!g_key_file_load_from_file(config_keyfile, override_file, flags, &error))
61 g_warning("Could not read user config: %s", error->message);
62 g_error_free(error);
64 else
66 keyfile_name = g_strdup(override_file);
67 g_message("User config pathname is %s", keyfile_name);
68 return;
72 if (!g_key_file_load_from_dirs(config_keyfile, keyfilename, keyfiledirs, &keyfile_name, flags, &error))
74 g_warning("Could not read cboxrc: %s, search dir = %s, filename = %s", error->message, keyfiledirs[0], keyfilename);
75 g_error_free(error);
77 else
79 g_message("Config pathname is %s", keyfile_name);
83 int cbox_config_has_section(const char *section)
85 return section && g_key_file_has_group(config_keyfile, section);
88 char *cbox_config_get_string(const char *section, const char *key)
90 return cbox_config_get_string_with_default(section, key, NULL);
93 void cbox_config_set_string(const char *section, const char *key, const char *value)
95 g_key_file_set_string(config_keyfile, section, key, value);
98 char *cbox_config_permify(const char *temporary)
100 return g_string_chunk_insert(cfg_strings, temporary);
103 char *cbox_config_get_string_with_default(const char *section, const char *key, char *def_value)
105 if (section && key && g_key_file_has_key(config_keyfile, section, key, NULL))
107 gchar *tmp = g_key_file_get_string(config_keyfile, section, key, NULL);
108 gchar *perm = g_string_chunk_insert(cfg_strings, tmp);
109 g_free(tmp);
110 return perm;
112 else
114 return def_value ? g_string_chunk_insert(cfg_strings, def_value) : NULL;
118 int cbox_config_get_int(const char *section, const char *key, int def_value)
120 GError *error = NULL;
121 int result;
123 if (!section || !key)
124 return def_value;
125 result = g_key_file_get_integer(config_keyfile, section, key, &error);
126 if (error)
128 g_error_free(error);
129 return def_value;
131 return result;
134 void cbox_config_set_int(const char *section, const char *key, int value)
136 g_key_file_set_integer(config_keyfile, section, key, value);
139 float cbox_config_get_float(const char *section, const char *key, float def_value)
141 GError *error = NULL;
142 float result;
144 if (!section || !key)
145 return def_value;
146 result = g_key_file_get_double(config_keyfile, section, key, &error);
147 if (error)
149 g_error_free(error);
150 return def_value;
152 return result;
155 void cbox_config_set_float(const char *section, const char *key, double value)
157 g_key_file_set_double(config_keyfile, section, key, value);
160 float cbox_config_get_gain(const char *section, const char *key, float def_value)
162 GError *error = NULL;
163 float result;
165 if (!section || !key)
166 return def_value;
167 result = g_key_file_get_double(config_keyfile, section, key, &error);
168 if (error)
170 g_error_free(error);
171 return def_value;
173 return pow(2.0, result / 6.0);
176 float cbox_config_get_gain_db(const char *section, const char *key, float def_value)
178 return cbox_config_get_gain(section, key, pow(2.0, def_value / 6.0));
181 void cbox_config_foreach_section(void (*process)(void *user_data, const char *section), void *user_data)
183 gsize i, length = 0;
184 gchar **groups = g_key_file_get_groups (config_keyfile, &length);
185 if (!groups)
186 return;
187 for (i = 0; i < length; i++)
189 process(user_data, groups[i]);
191 g_strfreev(groups);
194 void cbox_config_foreach_key(void (*process)(void *user_data, const char *key), const char *section, void *user_data)
196 gsize i, length = 0;
197 gchar **keys = g_key_file_get_keys (config_keyfile, section, &length, NULL);
198 if (!keys)
199 return;
200 for (i = 0; i < length; i++)
202 process(user_data, keys[i]);
204 g_strfreev(keys);
207 int cbox_config_remove_section(const char *section)
209 return 0 != g_key_file_remove_group(config_keyfile, section, NULL);
212 int cbox_config_remove_key(const char *section, const char *key)
214 return 0 != g_key_file_remove_key(config_keyfile, section, key, NULL);
217 gboolean cbox_config_save(const char *filename, GError **error)
219 gsize len = 0;
220 gchar *data = g_key_file_to_data(config_keyfile, &len, error);
221 if (!data)
222 return FALSE;
224 if (filename == NULL)
225 filename = keyfile_name;
227 gboolean ok = g_file_set_contents(filename, data, len, error);
228 g_free(data);
229 return ok;
232 struct cbox_cfgfile
234 gchar *libname;
235 gchar *filename;
236 GKeyFile *keyfile;
239 struct cbox_cfgfile *cbox_cfgfile_get_by_libname(const char *name)
241 // XXXKF implement
242 return NULL;
245 struct cbox_sectref
247 struct cbox_cfgfile *cfgfile;
248 gchar *section;
251 static struct cbox_sectref *cbox_sectref_lookup(const char *name)
253 struct cbox_sectref *sr = g_hash_table_lookup(config_sections_hash, name);
254 return sr;
257 static void cbox_sectref_keep(struct cbox_sectref *sect)
259 gchar *tmp = g_strdup_printf("%s@%s", sect->section, sect->cfgfile->libname);
260 g_hash_table_insert(config_sections_hash, tmp, sect);
261 g_free(tmp);
264 static void cbox_sectref_set_from_string(struct cbox_sectref *sr, const gchar *refname, struct cbox_cfgfile *default_lib)
266 const gchar *p = strchr(refname, '@');
267 if (p)
269 sr->section = g_strndup(refname, p - refname);
270 sr->cfgfile = cbox_cfgfile_get_by_libname(p + 1);
272 else
274 sr->section = g_strndup(refname, p - refname);
275 sr->cfgfile = default_lib;
279 // find the section 'prefix+refname.section' in the config file - either the one referenced by sect, or refname.file
280 struct cbox_sectref *cbox_config_sectref(struct cbox_sectref *sect, const char *prefix, const char *refname)
282 if (!prefix)
283 prefix = "";
284 gchar *tmpsect = NULL;
285 const char *p = strchr(refname, '@');
286 if (p)
287 tmpsect = g_strdup_printf("%s%s", prefix, refname);
288 else
289 tmpsect = g_strdup_printf("%s%s@%s", prefix, refname, sect->cfgfile->libname);
290 struct cbox_sectref *sr = cbox_sectref_lookup(tmpsect);
291 if (sr)
293 g_free(tmpsect);
294 return sr;
296 sr = malloc(sizeof(struct cbox_sectref));
297 cbox_sectref_set_from_string(sr, tmpsect, sect ? sect->cfgfile : NULL);
298 g_free(tmpsect);
299 cbox_sectref_keep(sr);
300 return sr;
303 struct cbox_sectref *cbox_config_get_sectref(struct cbox_sectref *sect, const char *prefix, const char *key)
305 if (!key || !sect)
306 return NULL;
307 //const char *sectname = cbox_config_get_string(sect, key);
308 const char *sectname = cbox_config_get_string(sect->section, key);
309 if (!sectname)
310 return NULL;
311 return cbox_config_sectref(sect, prefix, sectname);
314 struct cbox_sectref *cbox_config_get_sectref_n(struct cbox_sectref *sect, const char *prefix, const char *key, int index)
316 if (!key || !sect)
317 return NULL;
318 gchar *tmp = g_strdup_printf("%s%d", key, index);
319 struct cbox_sectref *sr = cbox_config_get_sectref(sect, prefix, tmp);
320 g_free(tmp);
321 return sr;
324 struct cbox_sectref *cbox_config_get_sectref_suffix(struct cbox_sectref *sect, const char *prefix, const char *key, const char *suffix)
326 if (!key || !sect)
327 return NULL;
328 gchar *tmp = g_strdup_printf("%s%s", key, suffix ? suffix : "");
329 struct cbox_sectref *sr = cbox_config_get_sectref(sect, prefix, tmp);
330 g_free(tmp);
331 return sr;
334 void cbox_config_close()
336 if (config_sections_hash)
338 g_hash_table_destroy(config_sections_hash);
339 config_sections_hash = NULL;
341 if (config_keyfile)
343 g_key_file_free(config_keyfile);
344 config_keyfile = NULL;
346 if (cfg_strings)
348 g_string_chunk_free(cfg_strings);
349 cfg_strings = NULL;