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();
46 // Allow virtual (in-memory) config by passing empty string
47 if (override_file
&& !*override_file
)
49 keyfile_name
= g_strdup("");
53 keyfiledirs
[0] = getenv("HOME");
54 keyfiledirs
[1] = NULL
;
55 // XXXKF add proper error handling
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
);
66 keyfile_name
= g_strdup(override_file
);
67 g_message("User config pathname is %s", keyfile_name
);
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
);
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
);
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
;
123 if (!section
|| !key
)
125 result
= g_key_file_get_integer(config_keyfile
, section
, key
, &error
);
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
;
144 if (!section
|| !key
)
146 result
= g_key_file_get_double(config_keyfile
, section
, key
, &error
);
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
;
165 if (!section
|| !key
)
167 result
= g_key_file_get_double(config_keyfile
, section
, key
, &error
);
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
)
184 gchar
**groups
= g_key_file_get_groups (config_keyfile
, &length
);
187 for (i
= 0; i
< length
; i
++)
189 process(user_data
, groups
[i
]);
194 void cbox_config_foreach_key(void (*process
)(void *user_data
, const char *key
), const char *section
, void *user_data
)
197 gchar
**keys
= g_key_file_get_keys (config_keyfile
, section
, &length
, NULL
);
200 for (i
= 0; i
< length
; i
++)
202 process(user_data
, keys
[i
]);
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
)
220 gchar
*data
= g_key_file_to_data(config_keyfile
, &len
, error
);
224 if (filename
== NULL
)
225 filename
= keyfile_name
;
227 gboolean ok
= g_file_set_contents(filename
, data
, len
, error
);
239 struct cbox_cfgfile
*cbox_cfgfile_get_by_libname(const char *name
)
247 struct cbox_cfgfile
*cfgfile
;
251 static struct cbox_sectref
*cbox_sectref_lookup(const char *name
)
253 struct cbox_sectref
*sr
= g_hash_table_lookup(config_sections_hash
, name
);
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
);
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
, '@');
269 sr
->section
= g_strndup(refname
, p
- refname
);
270 sr
->cfgfile
= cbox_cfgfile_get_by_libname(p
+ 1);
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
)
284 gchar
*tmpsect
= NULL
;
285 const char *p
= strchr(refname
, '@');
287 tmpsect
= g_strdup_printf("%s%s", prefix
, refname
);
289 tmpsect
= g_strdup_printf("%s%s@%s", prefix
, refname
, sect
->cfgfile
->libname
);
290 struct cbox_sectref
*sr
= cbox_sectref_lookup(tmpsect
);
296 sr
= malloc(sizeof(struct cbox_sectref
));
297 cbox_sectref_set_from_string(sr
, tmpsect
, sect
? sect
->cfgfile
: NULL
);
299 cbox_sectref_keep(sr
);
303 struct cbox_sectref
*cbox_config_get_sectref(struct cbox_sectref
*sect
, const char *prefix
, const char *key
)
307 //const char *sectname = cbox_config_get_string(sect, key);
308 const char *sectname
= cbox_config_get_string(sect
->section
, key
);
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
)
318 gchar
*tmp
= g_strdup_printf("%s%d", key
, index
);
319 struct cbox_sectref
*sr
= cbox_config_get_sectref(sect
, prefix
, tmp
);
324 struct cbox_sectref
*cbox_config_get_sectref_suffix(struct cbox_sectref
*sect
, const char *prefix
, const char *key
, const char *suffix
)
328 gchar
*tmp
= g_strdup_printf("%s%s", key
, suffix
? suffix
: "");
329 struct cbox_sectref
*sr
= cbox_config_get_sectref(sect
, prefix
, tmp
);
334 void cbox_config_close()
336 if (config_sections_hash
)
338 g_hash_table_destroy(config_sections_hash
);
339 config_sections_hash
= NULL
;
343 g_key_file_free(config_keyfile
);
344 config_keyfile
= NULL
;
348 g_string_chunk_free(cfg_strings
);