Start with empty scene as last resort option.
[calfbox.git] / config-api.c
blobba27fa89c7ad33a345b6b8cfce2f038f6da9a7bf
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();
45 keyfiledirs[0] = getenv("HOME");
46 keyfiledirs[1] = NULL;
47 // XXXKF add proper error handling
49 if (override_file)
51 if (!g_key_file_load_from_file(config_keyfile, override_file, flags, &error))
53 g_warning("Could not read user config: %s", error->message);
54 g_error_free(error);
56 else
58 keyfile_name = g_strdup(override_file);
59 g_message("User config pathname is %s", keyfile_name);
60 return;
64 if (!g_key_file_load_from_dirs(config_keyfile, keyfilename, keyfiledirs, &keyfile_name, flags, &error))
66 g_warning("Could not read cboxrc: %s, search dir = %s, filename = %s", error->message, keyfiledirs[0], keyfilename);
67 g_error_free(error);
69 else
71 g_message("Config pathname is %s", keyfile_name);
75 int cbox_config_has_section(const char *section)
77 return section && g_key_file_has_group(config_keyfile, section);
80 char *cbox_config_get_string(const char *section, const char *key)
82 return cbox_config_get_string_with_default(section, key, NULL);
85 void cbox_config_set_string(const char *section, const char *key, const char *value)
87 g_key_file_set_string(config_keyfile, section, key, value);
90 char *cbox_config_permify(const char *temporary)
92 return g_string_chunk_insert(cfg_strings, temporary);
95 char *cbox_config_get_string_with_default(const char *section, const char *key, char *def_value)
97 if (section && key && g_key_file_has_key(config_keyfile, section, key, NULL))
99 gchar *tmp = g_key_file_get_string(config_keyfile, section, key, NULL);
100 gchar *perm = g_string_chunk_insert(cfg_strings, tmp);
101 g_free(tmp);
102 return perm;
104 else
106 return def_value ? g_string_chunk_insert(cfg_strings, def_value) : NULL;
110 int cbox_config_get_int(const char *section, const char *key, int def_value)
112 GError *error = NULL;
113 int result;
115 if (!section || !key)
116 return def_value;
117 result = g_key_file_get_integer(config_keyfile, section, key, &error);
118 if (error)
120 g_error_free(error);
121 return def_value;
123 return result;
126 void cbox_config_set_int(const char *section, const char *key, int value)
128 g_key_file_set_integer(config_keyfile, section, key, value);
131 float cbox_config_get_float(const char *section, const char *key, float def_value)
133 GError *error = NULL;
134 float result;
136 if (!section || !key)
137 return def_value;
138 result = g_key_file_get_double(config_keyfile, section, key, &error);
139 if (error)
141 g_error_free(error);
142 return def_value;
144 return result;
147 void cbox_config_set_float(const char *section, const char *key, double value)
149 g_key_file_set_double(config_keyfile, section, key, value);
152 float cbox_config_get_gain(const char *section, const char *key, float def_value)
154 GError *error = NULL;
155 float result;
157 if (!section || !key)
158 return def_value;
159 result = g_key_file_get_double(config_keyfile, section, key, &error);
160 if (error)
162 g_error_free(error);
163 return def_value;
165 return pow(2.0, result / 6.0);
168 float cbox_config_get_gain_db(const char *section, const char *key, float def_value)
170 return cbox_config_get_gain(section, key, pow(2.0, def_value / 6.0));
173 void cbox_config_foreach_section(void (*process)(void *user_data, const char *section), void *user_data)
175 gsize i, length = 0;
176 gchar **groups = g_key_file_get_groups (config_keyfile, &length);
177 if (!groups)
178 return;
179 for (i = 0; i < length; i++)
181 process(user_data, groups[i]);
183 g_strfreev(groups);
186 void cbox_config_foreach_key(void (*process)(void *user_data, const char *key), const char *section, void *user_data)
188 gsize i, length = 0;
189 gchar **keys = g_key_file_get_keys (config_keyfile, section, &length, NULL);
190 if (!keys)
191 return;
192 for (i = 0; i < length; i++)
194 process(user_data, keys[i]);
196 g_strfreev(keys);
199 int cbox_config_remove_section(const char *section)
201 return 0 != g_key_file_remove_group(config_keyfile, section, NULL);
204 int cbox_config_remove_key(const char *section, const char *key)
206 return 0 != g_key_file_remove_key(config_keyfile, section, key, NULL);
209 gboolean cbox_config_save(const char *filename, GError **error)
211 gsize len = 0;
212 gchar *data = g_key_file_to_data(config_keyfile, &len, error);
213 if (!data)
214 return FALSE;
216 if (filename == NULL)
217 filename = keyfile_name;
219 gboolean ok = g_file_set_contents(filename, data, len, error);
220 g_free(data);
221 return ok;
224 struct cbox_cfgfile
226 gchar *libname;
227 gchar *filename;
228 GKeyFile *keyfile;
231 struct cbox_cfgfile *cbox_cfgfile_get_by_libname(const char *name)
233 // XXXKF implement
234 return NULL;
237 struct cbox_sectref
239 struct cbox_cfgfile *cfgfile;
240 gchar *section;
243 static struct cbox_sectref *cbox_sectref_lookup(const char *name)
245 struct cbox_sectref *sr = g_hash_table_lookup(config_sections_hash, name);
246 return sr;
249 static void cbox_sectref_keep(struct cbox_sectref *sect)
251 gchar *tmp = g_strdup_printf("%s@%s", sect->section, sect->cfgfile->libname);
252 g_hash_table_insert(config_sections_hash, tmp, sect);
253 g_free(tmp);
256 static void cbox_sectref_set_from_string(struct cbox_sectref *sr, const gchar *refname, struct cbox_cfgfile *default_lib)
258 const gchar *p = strchr(refname, '@');
259 if (p)
261 sr->section = g_strndup(refname, p - refname);
262 sr->cfgfile = cbox_cfgfile_get_by_libname(p + 1);
264 else
266 sr->section = g_strndup(refname, p - refname);
267 sr->cfgfile = default_lib;
271 // find the section 'prefix+refname.section' in the config file - either the one referenced by sect, or refname.file
272 struct cbox_sectref *cbox_config_sectref(struct cbox_sectref *sect, const char *prefix, const char *refname)
274 if (!prefix)
275 prefix = "";
276 gchar *tmpsect = NULL;
277 const char *p = strchr(refname, '@');
278 if (p)
279 tmpsect = g_strdup_printf("%s%s", prefix, refname);
280 else
281 tmpsect = g_strdup_printf("%s%s@%s", prefix, refname, sect->cfgfile->libname);
282 struct cbox_sectref *sr = cbox_sectref_lookup(tmpsect);
283 if (sr)
285 g_free(tmpsect);
286 return sr;
288 sr = malloc(sizeof(struct cbox_sectref));
289 cbox_sectref_set_from_string(sr, tmpsect, sect ? sect->cfgfile : NULL);
290 g_free(tmpsect);
291 cbox_sectref_keep(sr);
292 return sr;
295 struct cbox_sectref *cbox_config_get_sectref(struct cbox_sectref *sect, const char *prefix, const char *key)
297 if (!key || !sect)
298 return NULL;
299 //const char *sectname = cbox_config_get_string(sect, key);
300 const char *sectname = cbox_config_get_string(sect->section, key);
301 if (!sectname)
302 return NULL;
303 return cbox_config_sectref(sect, prefix, sectname);
306 struct cbox_sectref *cbox_config_get_sectref_n(struct cbox_sectref *sect, const char *prefix, const char *key, int index)
308 if (!key || !sect)
309 return NULL;
310 gchar *tmp = g_strdup_printf("%s%d", key, index);
311 struct cbox_sectref *sr = cbox_config_get_sectref(sect, prefix, tmp);
312 g_free(tmp);
313 return sr;
316 struct cbox_sectref *cbox_config_get_sectref_suffix(struct cbox_sectref *sect, const char *prefix, const char *key, const char *suffix)
318 if (!key || !sect)
319 return NULL;
320 gchar *tmp = g_strdup_printf("%s%s", key, suffix ? suffix : "");
321 struct cbox_sectref *sr = cbox_config_get_sectref(sect, prefix, tmp);
322 g_free(tmp);
323 return sr;
326 void cbox_config_close()
328 if (config_sections_hash)
330 g_hash_table_destroy(config_sections_hash);
331 config_sections_hash = NULL;
333 if (config_keyfile)
335 g_key_file_free(config_keyfile);
336 config_keyfile = NULL;
338 if (cfg_strings)
340 g_string_chunk_free(cfg_strings);
341 cfg_strings = NULL;