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