0
|
1 /* XMMS - Cross-platform multimedia player
|
|
2 * Copyright (C) 1998-2002 Peter Alm, Mikael Alm, Olle Hallnas,
|
|
3 * Thomas Nilsson and 4Front Technologies
|
|
4 * Copyright (C) 1999-2002 Haavard Kvaalen
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
19 */
|
|
20
|
|
21 #if defined(HAVE_CONFIG_H)
|
|
22 #include "../config.h"
|
|
23 #endif
|
|
24
|
1017
|
25 /* bypass the poisoning of the symbols we need */
|
|
26 #define I_AM_A_THIRD_PARTY_DEVELOPER_WHO_NEEDS_TO_BE_KICKED_IN_THE_HEAD_BY_CHUCK_NORRIS
|
|
27
|
0
|
28 #include "configfile.h"
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include <string.h>
|
|
32 #include <stdlib.h>
|
|
33 #include <locale.h>
|
|
34
|
|
35 #include <unistd.h>
|
|
36 #include <sys/stat.h>
|
|
37
|
|
38 typedef gboolean(*XmmsCfgValueReadFunc) (ConfigFile * config_file,
|
|
39 const gchar * section,
|
|
40 const gchar * key,
|
|
41 gpointer * value);
|
|
42
|
|
43 typedef void (*XmmsCfgValueWriteFunc) (ConfigFile * config_file,
|
|
44 const gchar * section,
|
|
45 const gchar * key,
|
|
46 gpointer * value);
|
|
47
|
|
48 struct _XmmsCfgValueTypeInfo {
|
|
49 XmmsCfgValueReadFunc read;
|
|
50 XmmsCfgValueWriteFunc write;
|
|
51 };
|
|
52
|
|
53 typedef struct _XmmsCfgValueTypeInfo XmmsCfgValueTypeInfo;
|
|
54
|
|
55
|
|
56 static XmmsCfgValueTypeInfo xmms_cfg_value_type_func[] = {
|
|
57 {(XmmsCfgValueReadFunc) xmms_cfg_read_int,
|
|
58 (XmmsCfgValueWriteFunc) xmms_cfg_write_int},
|
|
59 {(XmmsCfgValueReadFunc) xmms_cfg_read_float,
|
|
60 (XmmsCfgValueWriteFunc) xmms_cfg_write_float},
|
|
61 {(XmmsCfgValueReadFunc) xmms_cfg_read_boolean,
|
|
62 (XmmsCfgValueWriteFunc) xmms_cfg_write_boolean},
|
|
63 {(XmmsCfgValueReadFunc) xmms_cfg_read_string,
|
|
64 (XmmsCfgValueWriteFunc) xmms_cfg_write_string}
|
|
65 };
|
|
66
|
|
67
|
|
68 static ConfigSection *xmms_cfg_create_section(ConfigFile * cfg,
|
|
69 const gchar * name);
|
|
70 static ConfigLine *xmms_cfg_create_string(ConfigSection * section,
|
|
71 const gchar * key,
|
|
72 const gchar * value);
|
|
73 static ConfigSection *xmms_cfg_find_section(ConfigFile * cfg,
|
|
74 const gchar * name);
|
|
75 static ConfigLine *xmms_cfg_find_string(ConfigSection * section,
|
|
76 const gchar * key);
|
|
77
|
|
78
|
|
79 ConfigFile *xmms_cfg_new(void)
|
|
80 {
|
|
81 return g_new0(ConfigFile, 1);
|
|
82 }
|
|
83
|
|
84 gboolean xmms_cfg_read_value(ConfigFile * config_file,
|
|
85 const gchar * section, const gchar * key,
|
|
86 XmmsCfgValueType value_type, gpointer * value)
|
|
87 {
|
|
88 return xmms_cfg_value_type_func[value_type].read(config_file,
|
|
89 section, key, value);
|
|
90 }
|
|
91
|
|
92 void xmms_cfg_write_value(ConfigFile * config_file,
|
|
93 const gchar * section, const gchar * key,
|
|
94 XmmsCfgValueType value_type, gpointer * value)
|
|
95 {
|
|
96 xmms_cfg_value_type_func[value_type].read(config_file,
|
|
97 section, key, value);
|
|
98 }
|
|
99
|
|
100 ConfigFile *xmms_cfg_open_file(const gchar * filename)
|
|
101 {
|
|
102 ConfigFile *cfg;
|
|
103
|
|
104 gchar *buffer, **lines, *tmp;
|
|
105 gint i;
|
|
106 ConfigSection *section = NULL;
|
|
107
|
|
108 g_return_val_if_fail(filename != NULL, FALSE);
|
|
109
|
|
110 if (!g_file_get_contents(filename, &buffer, NULL, NULL))
|
|
111 return NULL;
|
|
112
|
|
113 cfg = g_malloc0(sizeof(ConfigFile));
|
|
114 lines = g_strsplit(buffer, "\n", 0);
|
|
115 g_free(buffer);
|
|
116 i = 0;
|
|
117 while (lines[i]) {
|
|
118 if (lines[i][0] == '[') {
|
|
119 if ((tmp = strchr(lines[i], ']'))) {
|
|
120 *tmp = '\0';
|
|
121 section = xmms_cfg_create_section(cfg, &lines[i][1]);
|
|
122 }
|
|
123 } else if (lines[i][0] != '#' && section) {
|
|
124 if ((tmp = strchr(lines[i], '='))) {
|
|
125 *tmp = '\0';
|
|
126 tmp++;
|
|
127 xmms_cfg_create_string(section, lines[i], tmp);
|
|
128 }
|
|
129 }
|
|
130 i++;
|
|
131 }
|
|
132 g_strfreev(lines);
|
|
133 return cfg;
|
|
134 }
|
|
135
|
|
136 gchar *xmms_cfg_get_default_filename(void)
|
|
137 {
|
|
138 static gchar *filename = NULL;
|
|
139 if (!filename)
|
|
140 filename =
|
|
141 g_strconcat(g_get_home_dir(), "/", BMP_RCPATH, "/config",
|
|
142 NULL);
|
|
143 return filename;
|
|
144 }
|
|
145
|
|
146 ConfigFile *xmms_cfg_open_default_file(void)
|
|
147 {
|
|
148 ConfigFile *ret;
|
|
149
|
|
150 ret = xmms_cfg_open_file(xmms_cfg_get_default_filename());
|
|
151 if (!ret)
|
|
152 ret = xmms_cfg_new();
|
|
153 return ret;
|
|
154 }
|
|
155
|
|
156 gboolean xmms_cfg_write_file(ConfigFile * cfg, const gchar * filename)
|
|
157 {
|
|
158 FILE *file;
|
|
159 GList *section_list, *line_list;
|
|
160 ConfigSection *section;
|
|
161 ConfigLine *line;
|
|
162
|
|
163 g_return_val_if_fail(cfg != NULL, FALSE);
|
|
164 g_return_val_if_fail(filename != NULL, FALSE);
|
|
165
|
|
166 if (!(file = fopen(filename, "w")))
|
|
167 return FALSE;
|
|
168
|
|
169 section_list = cfg->sections;
|
|
170 while (section_list) {
|
|
171 section = (ConfigSection *) section_list->data;
|
|
172 if (section->lines) {
|
|
173 fprintf(file, "[%s]\n", section->name);
|
|
174 line_list = section->lines;
|
|
175 while (line_list) {
|
|
176 line = (ConfigLine *) line_list->data;
|
|
177 fprintf(file, "%s=%s\n", line->key, line->value);
|
|
178 line_list = g_list_next(line_list);
|
|
179 }
|
|
180 fprintf(file, "\n");
|
|
181 }
|
|
182 section_list = g_list_next(section_list);
|
|
183 }
|
|
184 fclose(file);
|
|
185 return TRUE;
|
|
186 }
|
|
187
|
|
188 gboolean xmms_cfg_write_default_file(ConfigFile * cfg)
|
|
189 {
|
|
190 return xmms_cfg_write_file(cfg, xmms_cfg_get_default_filename());
|
|
191 }
|
|
192
|
|
193 gboolean xmms_cfg_read_string(ConfigFile * cfg, const gchar * section,
|
|
194 const gchar * key, gchar ** value)
|
|
195 {
|
|
196 ConfigSection *sect;
|
|
197 ConfigLine *line;
|
|
198
|
|
199 g_return_val_if_fail(cfg != NULL, FALSE);
|
|
200 g_return_val_if_fail(section != NULL, FALSE);
|
|
201 g_return_val_if_fail(key != NULL, FALSE);
|
|
202 g_return_val_if_fail(value != NULL, FALSE);
|
|
203
|
|
204 if (!(sect = xmms_cfg_find_section(cfg, section)))
|
|
205 return FALSE;
|
|
206 if (!(line = xmms_cfg_find_string(sect, key)))
|
|
207 return FALSE;
|
|
208 *value = g_strdup(line->value);
|
|
209 return TRUE;
|
|
210 }
|
|
211
|
|
212 gboolean xmms_cfg_read_int(ConfigFile * cfg, const gchar * section,
|
|
213 const gchar * key, gint * value)
|
|
214 {
|
|
215 gchar *str;
|
|
216
|
|
217 g_return_val_if_fail(cfg != NULL, FALSE);
|
|
218 g_return_val_if_fail(section != NULL, FALSE);
|
|
219 g_return_val_if_fail(key != NULL, FALSE);
|
|
220 g_return_val_if_fail(value != NULL, FALSE);
|
|
221
|
|
222 if (!xmms_cfg_read_string(cfg, section, key, &str))
|
|
223 return FALSE;
|
|
224 *value = atoi(str);
|
|
225 g_free(str);
|
|
226
|
|
227 return TRUE;
|
|
228 }
|
|
229
|
|
230 gboolean xmms_cfg_read_boolean(ConfigFile * cfg,
|
|
231 const gchar * section, const gchar * key,
|
|
232 gboolean * value)
|
|
233 {
|
|
234 gchar *str;
|
|
235
|
|
236 g_return_val_if_fail(cfg != NULL, FALSE);
|
|
237 g_return_val_if_fail(section != NULL, FALSE);
|
|
238 g_return_val_if_fail(key != NULL, FALSE);
|
|
239 g_return_val_if_fail(value != NULL, FALSE);
|
|
240
|
|
241 if (!xmms_cfg_read_string(cfg, section, key, &str))
|
|
242 return FALSE;
|
|
243 if (!strcasecmp(str, "TRUE"))
|
|
244 *value = TRUE;
|
|
245 else
|
|
246 *value = FALSE;
|
|
247 g_free(str);
|
|
248 return TRUE;
|
|
249 }
|
|
250
|
|
251 gboolean xmms_cfg_read_float(ConfigFile * cfg,
|
|
252 const gchar * section, const gchar * key,
|
|
253 gfloat * value)
|
|
254 {
|
|
255 gchar *str, *locale;
|
|
256
|
|
257 g_return_val_if_fail(cfg != NULL, FALSE);
|
|
258 g_return_val_if_fail(section != NULL, FALSE);
|
|
259 g_return_val_if_fail(key != NULL, FALSE);
|
|
260 g_return_val_if_fail(value != NULL, FALSE);
|
|
261
|
|
262 if (!xmms_cfg_read_string(cfg, section, key, &str))
|
|
263 return FALSE;
|
|
264
|
|
265 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
266 setlocale(LC_NUMERIC, "C");
|
|
267 *value = strtod(str, NULL);
|
|
268 setlocale(LC_NUMERIC, locale);
|
|
269 g_free(locale);
|
|
270 g_free(str);
|
|
271
|
|
272 return TRUE;
|
|
273 }
|
|
274
|
|
275 gboolean xmms_cfg_read_double(ConfigFile * cfg,
|
|
276 const gchar * section, const gchar * key,
|
|
277 gdouble * value)
|
|
278 {
|
|
279 gchar *str, *locale;
|
|
280
|
|
281 g_return_val_if_fail(cfg != NULL, FALSE);
|
|
282 g_return_val_if_fail(section != NULL, FALSE);
|
|
283 g_return_val_if_fail(key != NULL, FALSE);
|
|
284 g_return_val_if_fail(value != NULL, FALSE);
|
|
285
|
|
286 if (!xmms_cfg_read_string(cfg, section, key, &str))
|
|
287 return FALSE;
|
|
288
|
|
289 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
290 setlocale(LC_NUMERIC, "C");
|
|
291 *value = strtod(str, NULL);
|
|
292 setlocale(LC_NUMERIC, locale);
|
|
293 g_free(locale);
|
|
294 g_free(str);
|
|
295
|
|
296 return TRUE;
|
|
297 }
|
|
298
|
|
299 void xmms_cfg_write_string(ConfigFile * cfg,
|
|
300 const gchar * section, const gchar * key,
|
|
301 gchar * value)
|
|
302 {
|
|
303 ConfigSection *sect;
|
|
304 ConfigLine *line;
|
|
305
|
|
306 g_return_if_fail(cfg != NULL);
|
|
307 g_return_if_fail(section != NULL);
|
|
308 g_return_if_fail(key != NULL);
|
|
309 g_return_if_fail(value != NULL);
|
|
310
|
|
311 sect = xmms_cfg_find_section(cfg, section);
|
|
312 if (!sect)
|
|
313 sect = xmms_cfg_create_section(cfg, section);
|
|
314 if ((line = xmms_cfg_find_string(sect, key))) {
|
|
315 g_free(line->value);
|
|
316 line->value = g_strstrip(g_strdup(value));
|
|
317 } else
|
|
318 xmms_cfg_create_string(sect, key, value);
|
|
319 }
|
|
320
|
|
321 void xmms_cfg_write_int(ConfigFile * cfg,
|
|
322 const gchar * section, const gchar * key,
|
|
323 gint value)
|
|
324 {
|
|
325 gchar *strvalue;
|
|
326
|
|
327 g_return_if_fail(cfg != NULL);
|
|
328 g_return_if_fail(section != NULL);
|
|
329 g_return_if_fail(key != NULL);
|
|
330
|
|
331 strvalue = g_strdup_printf("%d", value);
|
|
332 xmms_cfg_write_string(cfg, section, key, strvalue);
|
|
333 g_free(strvalue);
|
|
334 }
|
|
335
|
|
336 void xmms_cfg_write_boolean(ConfigFile * cfg,
|
|
337 const gchar * section, const gchar * key,
|
|
338 gboolean value)
|
|
339 {
|
|
340 g_return_if_fail(cfg != NULL);
|
|
341 g_return_if_fail(section != NULL);
|
|
342 g_return_if_fail(key != NULL);
|
|
343
|
|
344 if (value)
|
|
345 xmms_cfg_write_string(cfg, section, key, "TRUE");
|
|
346 else
|
|
347 xmms_cfg_write_string(cfg, section, key, "FALSE");
|
|
348 }
|
|
349
|
|
350 void xmms_cfg_write_float(ConfigFile * cfg,
|
|
351 const gchar * section, const gchar * key,
|
|
352 gfloat value)
|
|
353 {
|
|
354 gchar *strvalue, *locale;
|
|
355
|
|
356 g_return_if_fail(cfg != NULL);
|
|
357 g_return_if_fail(section != NULL);
|
|
358 g_return_if_fail(key != NULL);
|
|
359
|
|
360 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
361 setlocale(LC_NUMERIC, "C");
|
|
362 strvalue = g_strdup_printf("%g", value);
|
|
363 setlocale(LC_NUMERIC, locale);
|
|
364 xmms_cfg_write_string(cfg, section, key, strvalue);
|
|
365 g_free(locale);
|
|
366 g_free(strvalue);
|
|
367 }
|
|
368
|
|
369 void xmms_cfg_write_double(ConfigFile * cfg,
|
|
370 const gchar * section, const gchar * key,
|
|
371 gdouble value)
|
|
372 {
|
|
373 gchar *strvalue, *locale;
|
|
374
|
|
375 g_return_if_fail(cfg != NULL);
|
|
376 g_return_if_fail(section != NULL);
|
|
377 g_return_if_fail(key != NULL);
|
|
378
|
|
379 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
380 setlocale(LC_NUMERIC, "C");
|
|
381 strvalue = g_strdup_printf("%g", value);
|
|
382 setlocale(LC_NUMERIC, locale);
|
|
383 xmms_cfg_write_string(cfg, section, key, strvalue);
|
|
384 g_free(locale);
|
|
385 g_free(strvalue);
|
|
386 }
|
|
387
|
|
388 void xmms_cfg_remove_key(ConfigFile * cfg,
|
|
389 const gchar * section, const gchar * key)
|
|
390 {
|
|
391 ConfigSection *sect;
|
|
392 ConfigLine *line;
|
|
393
|
|
394 g_return_if_fail(cfg != NULL);
|
|
395 g_return_if_fail(section != NULL);
|
|
396 g_return_if_fail(key != NULL);
|
|
397
|
|
398 if ((sect = xmms_cfg_find_section(cfg, section)) != NULL) {
|
|
399 if ((line = xmms_cfg_find_string(sect, key)) != NULL) {
|
|
400 g_free(line->key);
|
|
401 g_free(line->value);
|
|
402 g_free(line);
|
|
403 sect->lines = g_list_remove(sect->lines, line);
|
|
404 }
|
|
405 }
|
|
406 }
|
|
407
|
|
408 void xmms_cfg_free(ConfigFile * cfg)
|
|
409 {
|
|
410 ConfigSection *section;
|
|
411 ConfigLine *line;
|
|
412 GList *section_list, *line_list;
|
|
413
|
|
414 if (cfg == NULL)
|
|
415 return;
|
|
416
|
|
417 section_list = cfg->sections;
|
|
418 while (section_list) {
|
|
419 section = (ConfigSection *) section_list->data;
|
|
420 g_free(section->name);
|
|
421
|
|
422 line_list = section->lines;
|
|
423 while (line_list) {
|
|
424 line = (ConfigLine *) line_list->data;
|
|
425 g_free(line->key);
|
|
426 g_free(line->value);
|
|
427 g_free(line);
|
|
428 line_list = g_list_next(line_list);
|
|
429 }
|
|
430 g_list_free(section->lines);
|
|
431 g_free(section);
|
|
432
|
|
433 section_list = g_list_next(section_list);
|
|
434 }
|
|
435 g_list_free(cfg->sections);
|
|
436 g_free(cfg);
|
|
437 }
|
|
438
|
|
439 static ConfigSection *xmms_cfg_create_section(ConfigFile * cfg,
|
|
440 const gchar * name)
|
|
441 {
|
|
442 ConfigSection *section;
|
|
443
|
|
444 section = g_new0(ConfigSection, 1);
|
|
445 section->name = g_strdup(name);
|
|
446 cfg->sections = g_list_append(cfg->sections, section);
|
|
447
|
|
448 return section;
|
|
449 }
|
|
450
|
|
451 static ConfigLine *xmms_cfg_create_string(ConfigSection * section,
|
|
452 const gchar * key,
|
|
453 const gchar * value)
|
|
454 {
|
|
455 ConfigLine *line;
|
|
456
|
|
457 line = g_new0(ConfigLine, 1);
|
|
458 line->key = g_strstrip(g_strdup(key));
|
|
459 line->value = g_strstrip(g_strdup(value));
|
|
460 section->lines = g_list_append(section->lines, line);
|
|
461
|
|
462 return line;
|
|
463 }
|
|
464
|
|
465 static ConfigSection *xmms_cfg_find_section(ConfigFile * cfg,
|
|
466 const gchar * name)
|
|
467 {
|
|
468 ConfigSection *section;
|
|
469 GList *list;
|
|
470
|
|
471 list = cfg->sections;
|
|
472 while (list) {
|
|
473 section = (ConfigSection *) list->data;
|
|
474 if (!strcasecmp(section->name, name))
|
|
475 return section;
|
|
476 list = g_list_next(list);
|
|
477 }
|
|
478 return NULL;
|
|
479 }
|
|
480
|
|
481 static ConfigLine *xmms_cfg_find_string(ConfigSection * section,
|
|
482 const gchar * key)
|
|
483 {
|
|
484 ConfigLine *line;
|
|
485 GList *list;
|
|
486
|
|
487 list = section->lines;
|
|
488 while (list) {
|
|
489 line = (ConfigLine *) list->data;
|
|
490 if (!strcasecmp(line->key, key))
|
|
491 return line;
|
|
492 list = g_list_next(list);
|
|
493 }
|
|
494 return NULL;
|
|
495 }
|