Mercurial > audlegacy
annotate libaudacious/rcfile.c @ 2304:b0e49d195c64 trunk
[svn] Fuck, I forgot to quote the $ in the commit msg, so my home was in the ChangeLog.
author | js |
---|---|
date | Tue, 09 Jan 2007 14:53:19 -0800 |
parents | f18a5b617c34 |
children |
rev | line source |
---|---|
2057 | 1 /* Audacious |
2 * Copyright (c) 2005-2007 Audacious team | |
3 * | |
4 * BMP | |
5 * Copyright (c) 2003-2005 BMP team | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
0 | 8 * it under the terms of the GNU General Public License as published by |
2105
f18a5b617c34
[svn] - move to GPLv2-only. Based on my interpretation of the license, we are
nenolod
parents:
2057
diff
changeset
|
9 * the Free Software Foundation; under version 2 of the License. |
0 | 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 | |
1459 | 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 19 */ |
20 | |
21 #include "rcfile.h" | |
22 | |
23 #include <stdio.h> | |
24 #include <string.h> | |
25 #include <stdlib.h> | |
26 #include <locale.h> | |
27 | |
28 #include <unistd.h> | |
29 #include <sys/stat.h> | |
30 | |
31 | |
32 static RcSection *bmp_rcfile_create_section(RcFile * file, | |
33 const gchar * name); | |
34 static RcLine *bmp_rcfile_create_string(RcSection * section, | |
35 const gchar * key, | |
36 const gchar * value); | |
37 static RcSection *bmp_rcfile_find_section(RcFile * file, const gchar * name); | |
38 static RcLine *bmp_rcfile_find_string(RcSection * section, const gchar * key); | |
39 | |
2057 | 40 /** |
41 * bmp_rcfile_new: | |
42 * | |
43 * #RcFile object factory. | |
44 * | |
45 * Return value: A #RcFile object. | |
46 **/ | |
0 | 47 RcFile * |
48 bmp_rcfile_new(void) | |
49 { | |
50 return g_new0(RcFile, 1); | |
51 } | |
52 | |
2057 | 53 /** |
54 * bmp_rcfile_free: | |
55 * @file: A #RcFile object to destroy. | |
56 * | |
57 * #RcFile object destructor. | |
58 **/ | |
0 | 59 void |
60 bmp_rcfile_free(RcFile * file) | |
61 { | |
62 RcSection *section; | |
63 RcLine *line; | |
64 GList *section_list, *line_list; | |
65 | |
66 if (file == NULL) | |
67 return; | |
68 | |
69 section_list = file->sections; | |
70 while (section_list) { | |
71 section = (RcSection *) section_list->data; | |
72 g_free(section->name); | |
73 | |
74 line_list = section->lines; | |
75 while (line_list) { | |
76 line = (RcLine *) line_list->data; | |
77 g_free(line->key); | |
78 g_free(line->value); | |
79 g_free(line); | |
80 line_list = g_list_next(line_list); | |
81 } | |
82 g_list_free(section->lines); | |
83 g_free(section); | |
84 | |
85 section_list = g_list_next(section_list); | |
86 } | |
87 g_list_free(file->sections); | |
88 g_free(file); | |
89 } | |
90 | |
2057 | 91 /** |
92 * bmp_rcfile_open: | |
93 * @filename: Path to rcfile to open. | |
94 * | |
95 * Opens an rcfile and returns an #RcFile object representing it. | |
96 * | |
97 * Return value: An #RcFile object representing the rcfile given. | |
98 **/ | |
0 | 99 RcFile * |
100 bmp_rcfile_open(const gchar * filename) | |
101 { | |
102 RcFile *file; | |
103 | |
104 gchar *buffer, **lines, *tmp; | |
105 gint i; | |
106 RcSection *section = NULL; | |
107 | |
108 g_return_val_if_fail(filename != NULL, FALSE); | |
109 g_return_val_if_fail(strlen(filename) > 0, FALSE); | |
110 | |
111 if (!g_file_get_contents(filename, &buffer, NULL, NULL)) | |
112 return NULL; | |
113 | |
2057 | 114 file = bmp_rcfile_new(); |
0 | 115 lines = g_strsplit(buffer, "\n", 0); |
116 g_free(buffer); | |
117 i = 0; | |
118 while (lines[i]) { | |
119 if (lines[i][0] == '[') { | |
120 if ((tmp = strchr(lines[i], ']'))) { | |
121 *tmp = '\0'; | |
122 section = bmp_rcfile_create_section(file, &lines[i][1]); | |
123 } | |
124 } | |
125 else if (lines[i][0] != '#' && section) { | |
126 if ((tmp = strchr(lines[i], '='))) { | |
127 gchar **frags; | |
1769 | 128 frags = g_strsplit(lines[i], "=", 2); |
0 | 129 if (strlen(frags[1]) > 0) { |
130 bmp_rcfile_create_string(section, frags[0], frags[1]); | |
131 }; | |
1327 | 132 g_strfreev(frags); |
0 | 133 } |
134 } | |
135 i++; | |
136 } | |
137 g_strfreev(lines); | |
138 return file; | |
139 } | |
140 | |
2057 | 141 /** |
142 * bmp_rcfile_write: | |
143 * @file: A #RcFile object to write to disk. | |
144 * @filename: A path to write the #RcFile object's data to. | |
145 * | |
146 * Writes the contents of a #RcFile object to disk. | |
147 * | |
148 * Return value: TRUE on success, FALSE otherwise. | |
149 **/ | |
0 | 150 gboolean |
151 bmp_rcfile_write(RcFile * file, const gchar * filename) | |
152 { | |
153 FILE *fp; | |
154 GList *section_list, *line_list; | |
155 RcSection *section; | |
156 RcLine *line; | |
157 | |
158 g_return_val_if_fail(file != NULL, FALSE); | |
159 g_return_val_if_fail(filename != NULL, FALSE); | |
160 | |
161 if (!(fp = fopen(filename, "w"))) | |
162 return FALSE; | |
163 | |
164 section_list = file->sections; | |
165 while (section_list) { | |
166 section = (RcSection *) section_list->data; | |
167 if (section->lines) { | |
168 fprintf(fp, "[%s]\n", section->name); | |
169 line_list = section->lines; | |
170 while (line_list) { | |
171 line = (RcLine *) line_list->data; | |
172 fprintf(fp, "%s=%s\n", line->key, line->value); | |
173 line_list = g_list_next(line_list); | |
174 } | |
175 fprintf(fp, "\n"); | |
176 } | |
177 section_list = g_list_next(section_list); | |
178 } | |
179 fclose(fp); | |
180 return TRUE; | |
181 } | |
182 | |
2057 | 183 /** |
184 * bmp_rcfile_read_string: | |
185 * @file: A #RcFile object to write to disk. | |
186 * @section: The section of the RcFile to look in. | |
187 * @key: The name of the identifier to look up. | |
188 * @value: A pointer to a memory location to place the data. | |
189 * | |
190 * Looks up a value in an RcFile and places it in %value. | |
191 * | |
192 * Return value: TRUE on success, FALSE otherwise. | |
193 **/ | |
0 | 194 gboolean |
195 bmp_rcfile_read_string(RcFile * file, const gchar * section, | |
196 const gchar * key, gchar ** value) | |
197 { | |
198 RcSection *sect; | |
199 RcLine *line; | |
200 | |
201 g_return_val_if_fail(file != NULL, FALSE); | |
202 g_return_val_if_fail(section != NULL, FALSE); | |
203 g_return_val_if_fail(key != NULL, FALSE); | |
204 g_return_val_if_fail(value != NULL, FALSE); | |
205 | |
206 if (!(sect = bmp_rcfile_find_section(file, section))) | |
207 return FALSE; | |
208 if (!(line = bmp_rcfile_find_string(sect, key))) | |
209 return FALSE; | |
210 *value = g_strdup(line->value); | |
211 return TRUE; | |
212 } | |
213 | |
2057 | 214 /** |
215 * bmp_rcfile_read_int: | |
216 * @file: A #RcFile object to write to disk. | |
217 * @section: The section of the RcFile to look in. | |
218 * @key: The name of the identifier to look up. | |
219 * @value: A pointer to a memory location to place the data. | |
220 * | |
221 * Looks up a value in an RcFile and places it in %value. | |
222 * | |
223 * Return value: TRUE on success, FALSE otherwise. | |
224 **/ | |
0 | 225 gboolean |
226 bmp_rcfile_read_int(RcFile * file, const gchar * section, | |
227 const gchar * key, gint * value) | |
228 { | |
229 gchar *str; | |
230 | |
231 g_return_val_if_fail(file != NULL, FALSE); | |
232 g_return_val_if_fail(section != NULL, FALSE); | |
233 g_return_val_if_fail(key != NULL, FALSE); | |
234 g_return_val_if_fail(value != NULL, FALSE); | |
235 | |
236 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
237 return FALSE; | |
238 *value = atoi(str); | |
239 g_free(str); | |
240 | |
241 return TRUE; | |
242 } | |
243 | |
2057 | 244 /** |
245 * bmp_rcfile_read_bool: | |
246 * @file: A #RcFile object to write to disk. | |
247 * @section: The section of the RcFile to look in. | |
248 * @key: The name of the identifier to look up. | |
249 * @value: A pointer to a memory location to place the data. | |
250 * | |
251 * Looks up a value in an RcFile and places it in %value. | |
252 * | |
253 * Return value: TRUE on success, FALSE otherwise. | |
254 **/ | |
0 | 255 gboolean |
256 bmp_rcfile_read_bool(RcFile * file, const gchar * section, | |
257 const gchar * key, gboolean * value) | |
258 { | |
259 gchar *str; | |
260 | |
261 g_return_val_if_fail(file != NULL, FALSE); | |
262 g_return_val_if_fail(section != NULL, FALSE); | |
263 g_return_val_if_fail(key != NULL, FALSE); | |
264 g_return_val_if_fail(value != NULL, FALSE); | |
265 | |
266 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
267 return FALSE; | |
268 if (!strcasecmp(str, "TRUE")) | |
269 *value = TRUE; | |
270 else | |
271 *value = FALSE; | |
272 g_free(str); | |
273 return TRUE; | |
274 } | |
275 | |
2057 | 276 /** |
277 * bmp_rcfile_read_float: | |
278 * @file: A #RcFile object to write to disk. | |
279 * @section: The section of the RcFile to look in. | |
280 * @key: The name of the identifier to look up. | |
281 * @value: A pointer to a memory location to place the data. | |
282 * | |
283 * Looks up a value in an RcFile and places it in %value. | |
284 * | |
285 * Return value: TRUE on success, FALSE otherwise. | |
286 **/ | |
0 | 287 gboolean |
288 bmp_rcfile_read_float(RcFile * file, const gchar * section, | |
289 const gchar * key, gfloat * value) | |
290 { | |
291 gchar *str, *locale; | |
292 | |
293 g_return_val_if_fail(file != NULL, FALSE); | |
294 g_return_val_if_fail(section != NULL, FALSE); | |
295 g_return_val_if_fail(key != NULL, FALSE); | |
296 g_return_val_if_fail(value != NULL, FALSE); | |
297 | |
298 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
299 return FALSE; | |
300 | |
301 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
302 setlocale(LC_NUMERIC, "C"); | |
303 *value = strtod(str, NULL); | |
304 setlocale(LC_NUMERIC, locale); | |
305 g_free(locale); | |
306 g_free(str); | |
307 | |
308 return TRUE; | |
309 } | |
310 | |
2057 | 311 /** |
312 * bmp_rcfile_read_double: | |
313 * @file: A #RcFile object to write to disk. | |
314 * @section: The section of the RcFile to look in. | |
315 * @key: The name of the identifier to look up. | |
316 * @value: A pointer to a memory location to place the data. | |
317 * | |
318 * Looks up a value in an RcFile and places it in %value. | |
319 * | |
320 * Return value: TRUE on success, FALSE otherwise. | |
321 **/ | |
0 | 322 gboolean |
323 bmp_rcfile_read_double(RcFile * file, const gchar * section, | |
324 const gchar * key, gdouble * value) | |
325 { | |
326 gchar *str, *locale; | |
327 | |
328 g_return_val_if_fail(file != NULL, FALSE); | |
329 g_return_val_if_fail(section != NULL, FALSE); | |
330 g_return_val_if_fail(key != NULL, FALSE); | |
331 g_return_val_if_fail(value != NULL, FALSE); | |
332 | |
333 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
334 return FALSE; | |
335 | |
336 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
337 setlocale(LC_NUMERIC, "C"); | |
338 *value = strtod(str, NULL); | |
339 setlocale(LC_NUMERIC, locale); | |
340 g_free(locale); | |
341 g_free(str); | |
342 | |
343 return TRUE; | |
344 } | |
345 | |
2057 | 346 /** |
347 * bmp_rcfile_write_string: | |
348 * @file: A #RcFile object to write to disk. | |
349 * @section: The section of the RcFile to set the key in. | |
350 * @key: The name of the identifier to set. | |
351 * @value: The value to set for that identifier. | |
352 * | |
353 * Sets a value in an RcFile for %key. | |
354 **/ | |
0 | 355 void |
356 bmp_rcfile_write_string(RcFile * file, const gchar * section, | |
357 const gchar * key, const gchar * value) | |
358 { | |
359 RcSection *sect; | |
360 RcLine *line; | |
361 | |
362 g_return_if_fail(file != NULL); | |
363 g_return_if_fail(section != NULL); | |
364 g_return_if_fail(key != NULL); | |
365 g_return_if_fail(value != NULL); | |
366 | |
367 sect = bmp_rcfile_find_section(file, section); | |
368 if (!sect) | |
369 sect = bmp_rcfile_create_section(file, section); | |
370 if ((line = bmp_rcfile_find_string(sect, key))) { | |
371 g_free(line->value); | |
372 line->value = g_strstrip(g_strdup(value)); | |
373 } | |
374 else | |
375 bmp_rcfile_create_string(sect, key, value); | |
376 } | |
377 | |
2057 | 378 /** |
379 * bmp_rcfile_write_int: | |
380 * @file: A #RcFile object to write to disk. | |
381 * @section: The section of the RcFile to set the key in. | |
382 * @key: The name of the identifier to set. | |
383 * @value: The value to set for that identifier. | |
384 * | |
385 * Sets a value in an RcFile for %key. | |
386 **/ | |
0 | 387 void |
388 bmp_rcfile_write_int(RcFile * file, const gchar * section, | |
389 const gchar * key, gint value) | |
390 { | |
391 gchar *strvalue; | |
392 | |
393 g_return_if_fail(file != NULL); | |
394 g_return_if_fail(section != NULL); | |
395 g_return_if_fail(key != NULL); | |
396 | |
397 strvalue = g_strdup_printf("%d", value); | |
398 bmp_rcfile_write_string(file, section, key, strvalue); | |
399 g_free(strvalue); | |
400 } | |
401 | |
2057 | 402 /** |
403 * bmp_rcfile_write_boolean: | |
404 * @file: A #RcFile object to write to disk. | |
405 * @section: The section of the RcFile to set the key in. | |
406 * @key: The name of the identifier to set. | |
407 * @value: The value to set for that identifier. | |
408 * | |
409 * Sets a value in an RcFile for %key. | |
410 **/ | |
0 | 411 void |
412 bmp_rcfile_write_boolean(RcFile * file, const gchar * section, | |
413 const gchar * key, gboolean value) | |
414 { | |
415 g_return_if_fail(file != NULL); | |
416 g_return_if_fail(section != NULL); | |
417 g_return_if_fail(key != NULL); | |
418 | |
419 if (value) | |
420 bmp_rcfile_write_string(file, section, key, "TRUE"); | |
421 else | |
422 bmp_rcfile_write_string(file, section, key, "FALSE"); | |
423 } | |
424 | |
2057 | 425 /** |
426 * bmp_rcfile_write_float: | |
427 * @file: A #RcFile object to write to disk. | |
428 * @section: The section of the RcFile to set the key in. | |
429 * @key: The name of the identifier to set. | |
430 * @value: The value to set for that identifier. | |
431 * | |
432 * Sets a value in an RcFile for %key. | |
433 **/ | |
0 | 434 void |
435 bmp_rcfile_write_float(RcFile * file, const gchar * section, | |
436 const gchar * key, gfloat value) | |
437 { | |
438 gchar *strvalue, *locale; | |
439 | |
440 g_return_if_fail(file != NULL); | |
441 g_return_if_fail(section != NULL); | |
442 g_return_if_fail(key != NULL); | |
443 | |
444 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
445 setlocale(LC_NUMERIC, "C"); | |
446 strvalue = g_strdup_printf("%g", value); | |
447 setlocale(LC_NUMERIC, locale); | |
448 bmp_rcfile_write_string(file, section, key, strvalue); | |
449 g_free(locale); | |
450 g_free(strvalue); | |
451 } | |
452 | |
2057 | 453 /** |
454 * bmp_rcfile_write_double: | |
455 * @file: A #RcFile object to write to disk. | |
456 * @section: The section of the RcFile to set the key in. | |
457 * @key: The name of the identifier to set. | |
458 * @value: The value to set for that identifier. | |
459 * | |
460 * Sets a value in an RcFile for %key. | |
461 **/ | |
0 | 462 void |
463 bmp_rcfile_write_double(RcFile * file, const gchar * section, | |
464 const gchar * key, gdouble value) | |
465 { | |
466 gchar *strvalue, *locale; | |
467 | |
468 g_return_if_fail(file != NULL); | |
469 g_return_if_fail(section != NULL); | |
470 g_return_if_fail(key != NULL); | |
471 | |
472 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
473 setlocale(LC_NUMERIC, "C"); | |
474 strvalue = g_strdup_printf("%g", value); | |
475 setlocale(LC_NUMERIC, locale); | |
476 bmp_rcfile_write_string(file, section, key, strvalue); | |
477 g_free(locale); | |
478 g_free(strvalue); | |
479 } | |
480 | |
2057 | 481 /** |
482 * bmp_rcfile_remove_key: | |
483 * @file: A #RcFile object to write to disk. | |
484 * @section: The section of the RcFile to set the key in. | |
485 * @key: The name of the identifier to remove. | |
486 * | |
487 * Removes %key from an #RcFile object. | |
488 **/ | |
0 | 489 void |
490 bmp_rcfile_remove_key(RcFile * file, const gchar * section, const gchar * key) | |
491 { | |
492 RcSection *sect; | |
493 RcLine *line; | |
494 | |
495 g_return_if_fail(file != NULL); | |
496 g_return_if_fail(section != NULL); | |
497 g_return_if_fail(key != NULL); | |
498 | |
499 if ((sect = bmp_rcfile_find_section(file, section)) != NULL) { | |
500 if ((line = bmp_rcfile_find_string(sect, key)) != NULL) { | |
501 g_free(line->key); | |
502 g_free(line->value); | |
503 g_free(line); | |
504 sect->lines = g_list_remove(sect->lines, line); | |
505 } | |
506 } | |
507 } | |
508 | |
509 static RcSection * | |
510 bmp_rcfile_create_section(RcFile * file, const gchar * name) | |
511 { | |
512 RcSection *section; | |
513 | |
514 section = g_new0(RcSection, 1); | |
515 section->name = g_strdup(name); | |
516 file->sections = g_list_append(file->sections, section); | |
517 | |
518 return section; | |
519 } | |
520 | |
521 static RcLine * | |
522 bmp_rcfile_create_string(RcSection * section, | |
523 const gchar * key, const gchar * value) | |
524 { | |
525 RcLine *line; | |
526 | |
527 line = g_new0(RcLine, 1); | |
528 line->key = g_strstrip(g_strdup(key)); | |
529 line->value = g_strstrip(g_strdup(value)); | |
530 section->lines = g_list_append(section->lines, line); | |
531 | |
532 return line; | |
533 } | |
534 | |
535 static RcSection * | |
536 bmp_rcfile_find_section(RcFile * file, const gchar * name) | |
537 { | |
538 RcSection *section; | |
539 GList *list; | |
540 | |
541 list = file->sections; | |
542 while (list) { | |
543 section = (RcSection *) list->data; | |
544 if (!strcasecmp(section->name, name)) | |
545 return section; | |
546 list = g_list_next(list); | |
547 } | |
548 return NULL; | |
549 } | |
550 | |
551 static RcLine * | |
552 bmp_rcfile_find_string(RcSection * section, const gchar * key) | |
553 { | |
554 RcLine *line; | |
555 GList *list; | |
556 | |
557 list = section->lines; | |
558 while (list) { | |
559 line = (RcLine *) list->data; | |
560 if (!strcasecmp(line->key, key)) | |
561 return line; | |
562 list = g_list_next(list); | |
563 } | |
564 return NULL; | |
565 } |