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"
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
;
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
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
);
58 keyfile_name
= g_strdup(override_file
);
59 g_message("User config pathname is %s", keyfile_name
);
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
);
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
);
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
;
115 if (!section
|| !key
)
117 result
= g_key_file_get_integer(config_keyfile
, section
, key
, &error
);
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
;
136 if (!section
|| !key
)
138 result
= g_key_file_get_double(config_keyfile
, section
, key
, &error
);
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
;
157 if (!section
|| !key
)
159 result
= g_key_file_get_double(config_keyfile
, section
, key
, &error
);
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
)
176 gchar
**groups
= g_key_file_get_groups (config_keyfile
, &length
);
179 for (i
= 0; i
< length
; i
++)
181 process(user_data
, groups
[i
]);
186 void cbox_config_foreach_key(void (*process
)(void *user_data
, const char *key
), const char *section
, void *user_data
)
189 gchar
**keys
= g_key_file_get_keys (config_keyfile
, section
, &length
, NULL
);
192 for (i
= 0; i
< length
; i
++)
194 process(user_data
, keys
[i
]);
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
)
212 gchar
*data
= g_key_file_to_data(config_keyfile
, &len
, error
);
216 if (filename
== NULL
)
217 filename
= keyfile_name
;
219 gboolean ok
= g_file_set_contents(filename
, data
, len
, error
);
231 struct cbox_cfgfile
*cbox_cfgfile_get_by_libname(const char *name
)
239 struct cbox_cfgfile
*cfgfile
;
243 static struct cbox_sectref
*cbox_sectref_lookup(const char *name
)
245 struct cbox_sectref
*sr
= g_hash_table_lookup(config_sections_hash
, name
);
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
);
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
, '@');
261 sr
->section
= g_strndup(refname
, p
- refname
);
262 sr
->cfgfile
= cbox_cfgfile_get_by_libname(p
+ 1);
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
)
276 gchar
*tmpsect
= NULL
;
277 const char *p
= strchr(refname
, '@');
279 tmpsect
= g_strdup_printf("%s%s", prefix
, refname
);
281 tmpsect
= g_strdup_printf("%s%s@%s", prefix
, refname
, sect
->cfgfile
->libname
);
282 struct cbox_sectref
*sr
= cbox_sectref_lookup(tmpsect
);
288 sr
= malloc(sizeof(struct cbox_sectref
));
289 cbox_sectref_set_from_string(sr
, tmpsect
, sect
? sect
->cfgfile
: NULL
);
291 cbox_sectref_keep(sr
);
295 struct cbox_sectref
*cbox_config_get_sectref(struct cbox_sectref
*sect
, const char *prefix
, const char *key
)
299 //const char *sectname = cbox_config_get_string(sect, key);
300 const char *sectname
= cbox_config_get_string(sect
->section
, key
);
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
)
310 gchar
*tmp
= g_strdup_printf("%s%d", key
, index
);
311 struct cbox_sectref
*sr
= cbox_config_get_sectref(sect
, prefix
, tmp
);
316 struct cbox_sectref
*cbox_config_get_sectref_suffix(struct cbox_sectref
*sect
, const char *prefix
, const char *key
, const char *suffix
)
320 gchar
*tmp
= g_strdup_printf("%s%s", key
, suffix
? suffix
: "");
321 struct cbox_sectref
*sr
= cbox_config_get_sectref(sect
, prefix
, tmp
);
326 void cbox_config_close()
328 if (config_sections_hash
)
330 g_hash_table_destroy(config_sections_hash
);
331 config_sections_hash
= NULL
;
335 g_key_file_free(config_keyfile
);
336 config_keyfile
= NULL
;
340 g_string_chunk_free(cfg_strings
);