-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig-api.c
357 lines (311 loc) · 9.61 KB
/
config-api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Calf Box, an open source musical instrument.
Copyright (C) 2010 Krzysztof Foltman
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config-api.h"
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static GKeyFile *config_keyfile;
static gchar *keyfile_name;
static GStringChunk *cfg_strings = NULL;
static GHashTable *config_sections_hash = NULL;
void cbox_config_init(const char *override_file)
{
const gchar *keyfiledirs[3];
const gchar *keyfilename = ".cboxrc";
GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
GError *error = NULL;
if (config_keyfile)
return;
config_sections_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
cfg_strings = g_string_chunk_new(100);
config_keyfile = g_key_file_new();
// Allow virtual (in-memory) config by passing empty string
if (override_file && !*override_file)
{
keyfile_name = g_strdup("");
return;
}
keyfiledirs[0] = getenv("HOME");
keyfiledirs[1] = NULL;
// XXXKF add proper error handling
if (override_file)
{
if (!g_key_file_load_from_file(config_keyfile, override_file, flags, &error))
{
g_warning("Could not read user config: %s", error->message);
g_error_free(error);
}
else
{
keyfile_name = g_strdup(override_file);
g_message("User config pathname is %s", keyfile_name);
return;
}
}
if (!g_key_file_load_from_dirs(config_keyfile, keyfilename, keyfiledirs, &keyfile_name, flags, &error))
{
g_warning("Could not read cboxrc: %s, search dir = %s, filename = %s", error->message, keyfiledirs[0], keyfilename);
g_error_free(error);
}
else
{
g_message("Config pathname is %s", keyfile_name);
}
}
int cbox_config_has_section(const char *section)
{
return section && g_key_file_has_group(config_keyfile, section);
}
char *cbox_config_get_string(const char *section, const char *key)
{
return cbox_config_get_string_with_default(section, key, NULL);
}
void cbox_config_set_string(const char *section, const char *key, const char *value)
{
g_key_file_set_string(config_keyfile, section, key, value);
}
char *cbox_config_permify(const char *temporary)
{
return g_string_chunk_insert(cfg_strings, temporary);
}
char *cbox_config_get_string_with_default(const char *section, const char *key, char *def_value)
{
if (section && key && g_key_file_has_key(config_keyfile, section, key, NULL))
{
gchar *tmp = g_key_file_get_string(config_keyfile, section, key, NULL);
gchar *perm = g_string_chunk_insert(cfg_strings, tmp);
g_free(tmp);
return perm;
}
else
{
return def_value ? g_string_chunk_insert(cfg_strings, def_value) : NULL;
}
}
int cbox_config_get_int(const char *section, const char *key, int def_value)
{
GError *error = NULL;
int result;
if (!section || !key)
return def_value;
result = g_key_file_get_integer(config_keyfile, section, key, &error);
if (error)
{
g_error_free(error);
return def_value;
}
return result;
}
void cbox_config_set_int(const char *section, const char *key, int value)
{
g_key_file_set_integer(config_keyfile, section, key, value);
}
float cbox_config_get_float(const char *section, const char *key, float def_value)
{
GError *error = NULL;
float result;
if (!section || !key)
return def_value;
result = g_key_file_get_double(config_keyfile, section, key, &error);
if (error)
{
g_error_free(error);
return def_value;
}
return result;
}
void cbox_config_set_float(const char *section, const char *key, double value)
{
g_key_file_set_double(config_keyfile, section, key, value);
}
float cbox_config_get_gain(const char *section, const char *key, float def_value)
{
GError *error = NULL;
float result;
if (!section || !key)
return def_value;
result = g_key_file_get_double(config_keyfile, section, key, &error);
if (error)
{
g_error_free(error);
return def_value;
}
return pow(2.0, result / 6.0);
}
float cbox_config_get_gain_db(const char *section, const char *key, float def_value)
{
return cbox_config_get_gain(section, key, pow(2.0, def_value / 6.0));
}
void cbox_config_foreach_section(void (*process)(void *user_data, const char *section), void *user_data)
{
gsize i, length = 0;
gchar **groups = g_key_file_get_groups (config_keyfile, &length);
if (!groups)
return;
for (i = 0; i < length; i++)
{
process(user_data, groups[i]);
}
g_strfreev(groups);
}
void cbox_config_foreach_key(void (*process)(void *user_data, const char *key), const char *section, void *user_data)
{
gsize i, length = 0;
gchar **keys = g_key_file_get_keys (config_keyfile, section, &length, NULL);
if (!keys)
return;
for (i = 0; i < length; i++)
{
process(user_data, keys[i]);
}
g_strfreev(keys);
}
int cbox_config_remove_section(const char *section)
{
return 0 != g_key_file_remove_group(config_keyfile, section, NULL);
}
int cbox_config_remove_key(const char *section, const char *key)
{
return 0 != g_key_file_remove_key(config_keyfile, section, key, NULL);
}
gboolean cbox_config_save(const char *filename, GError **error)
{
gsize len = 0;
gchar *data = g_key_file_to_data(config_keyfile, &len, error);
if (!data)
return FALSE;
if (filename == NULL)
filename = keyfile_name;
gboolean ok = g_file_set_contents(filename, data, len, error);
g_free(data);
return ok;
}
struct cbox_cfgfile
{
gchar *libname;
gchar *filename;
GKeyFile *keyfile;
};
struct cbox_cfgfile *cbox_cfgfile_get_by_libname(const char *name)
{
// XXXKF implement
return NULL;
}
struct cbox_sectref
{
struct cbox_cfgfile *cfgfile;
gchar *section;
};
static struct cbox_sectref *cbox_sectref_lookup(const char *name)
{
struct cbox_sectref *sr = g_hash_table_lookup(config_sections_hash, name);
return sr;
}
static void cbox_sectref_keep(struct cbox_sectref *sect)
{
gchar *tmp = g_strdup_printf("%s@%s", sect->section, sect->cfgfile->libname);
g_hash_table_insert(config_sections_hash, tmp, sect);
g_free(tmp);
}
static void cbox_sectref_set_from_string(struct cbox_sectref *sr, const gchar *refname, struct cbox_cfgfile *default_lib)
{
const gchar *p = strchr(refname, '@');
if (p)
{
sr->section = g_strndup(refname, p - refname);
sr->cfgfile = cbox_cfgfile_get_by_libname(p + 1);
}
else
{
sr->section = g_strndup(refname, p - refname);
sr->cfgfile = default_lib;
}
}
// find the section 'prefix+refname.section' in the config file - either the one referenced by sect, or refname.file
struct cbox_sectref *cbox_config_sectref(struct cbox_sectref *sect, const char *prefix, const char *refname)
{
if (!prefix)
prefix = "";
gchar *tmpsect = NULL;
const char *p = strchr(refname, '@');
if (p)
tmpsect = g_strdup_printf("%s%s", prefix, refname);
else
tmpsect = g_strdup_printf("%s%s@%s", prefix, refname, sect->cfgfile->libname);
struct cbox_sectref *sr = cbox_sectref_lookup(tmpsect);
if (sr)
{
g_free(tmpsect);
return sr;
}
sr = malloc(sizeof(struct cbox_sectref));
cbox_sectref_set_from_string(sr, tmpsect, sect ? sect->cfgfile : NULL);
g_free(tmpsect);
cbox_sectref_keep(sr);
return sr;
}
struct cbox_sectref *cbox_config_get_sectref(struct cbox_sectref *sect, const char *prefix, const char *key)
{
if (!key || !sect)
return NULL;
//const char *sectname = cbox_config_get_string(sect, key);
const char *sectname = cbox_config_get_string(sect->section, key);
if (!sectname)
return NULL;
return cbox_config_sectref(sect, prefix, sectname);
}
struct cbox_sectref *cbox_config_get_sectref_n(struct cbox_sectref *sect, const char *prefix, const char *key, int index)
{
if (!key || !sect)
return NULL;
gchar *tmp = g_strdup_printf("%s%d", key, index);
struct cbox_sectref *sr = cbox_config_get_sectref(sect, prefix, tmp);
g_free(tmp);
return sr;
}
struct cbox_sectref *cbox_config_get_sectref_suffix(struct cbox_sectref *sect, const char *prefix, const char *key, const char *suffix)
{
if (!key || !sect)
return NULL;
gchar *tmp = g_strdup_printf("%s%s", key, suffix ? suffix : "");
struct cbox_sectref *sr = cbox_config_get_sectref(sect, prefix, tmp);
g_free(tmp);
return sr;
}
void cbox_config_close()
{
if (config_sections_hash)
{
g_hash_table_destroy(config_sections_hash);
config_sections_hash = NULL;
}
if (config_keyfile)
{
g_key_file_free(config_keyfile);
config_keyfile = NULL;
}
if (cfg_strings)
{
g_string_chunk_free(cfg_strings);
cfg_strings = NULL;
}
if (keyfile_name)
{
g_free(keyfile_name);
keyfile_name = NULL;
}
}