Mercurial > audlegacy
annotate src/audacious/rcfile.c @ 3016:e84073b61ba5 trunk
fix tinyplayer skin
| author | Tomasz Mon <desowin@gmail.com> |
|---|---|
| date | Mon, 09 Jul 2007 13:02:50 +0200 |
| parents | d226b83fa329 |
| children | 4c758281fe8f |
| rev | line source |
|---|---|
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
1 /* |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
2 * audacious: Cross-platform multimedia player. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
3 * rcfile.c: Reading and manipulation of .ini-like files. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
4 * |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
5 * Copyright (c) 2005-2007 Audacious development team. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
6 * Copyright (c) 2003-2005 BMP development team. |
| 2537 | 7 * |
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
8 * Redistribution and use in source and binary forms, with or without |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
9 * modification, are permitted provided that the following conditions are |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
10 * met: |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
11 * |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
12 * 1. Redistributions of source code must retain the above copyright notice, |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
13 * this list of conditions and the following disclaimer. |
| 2537 | 14 * |
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
15 * 2. Redistributions in binary form must reproduce the above copyright |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
16 * notice, this list of conditions and the following disclaimer in the |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
17 * documentation and/or other materials provided with the distribution. |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
18 * |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
19 * 3. The name of the author may not be used to endorse or promote products |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
20 * derived from this software without specific prior written permission. |
| 2537 | 21 * |
|
2865
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
d226b83fa329
BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents:
2537
diff
changeset
|
32 * POSSIBILITY OF SUCH DAMAGE. |
| 2537 | 33 */ |
| 34 | |
| 35 #include "rcfile.h" | |
| 36 | |
| 37 #include <stdio.h> | |
| 38 #include <string.h> | |
| 39 #include <stdlib.h> | |
| 40 #include <locale.h> | |
| 41 | |
| 42 #include <unistd.h> | |
| 43 #include <sys/stat.h> | |
| 44 | |
| 45 | |
| 46 static RcSection *bmp_rcfile_create_section(RcFile * file, | |
| 47 const gchar * name); | |
| 48 static RcLine *bmp_rcfile_create_string(RcSection * section, | |
| 49 const gchar * key, | |
| 50 const gchar * value); | |
| 51 static RcSection *bmp_rcfile_find_section(RcFile * file, const gchar * name); | |
| 52 static RcLine *bmp_rcfile_find_string(RcSection * section, const gchar * key); | |
| 53 | |
| 54 /** | |
| 55 * bmp_rcfile_new: | |
| 56 * | |
| 57 * #RcFile object factory. | |
| 58 * | |
| 59 * Return value: A #RcFile object. | |
| 60 **/ | |
| 61 RcFile * | |
| 62 bmp_rcfile_new(void) | |
| 63 { | |
| 64 return g_new0(RcFile, 1); | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * bmp_rcfile_free: | |
| 69 * @file: A #RcFile object to destroy. | |
| 70 * | |
| 71 * #RcFile object destructor. | |
| 72 **/ | |
| 73 void | |
| 74 bmp_rcfile_free(RcFile * file) | |
| 75 { | |
| 76 RcSection *section; | |
| 77 RcLine *line; | |
| 78 GList *section_list, *line_list; | |
| 79 | |
| 80 if (file == NULL) | |
| 81 return; | |
| 82 | |
| 83 section_list = file->sections; | |
| 84 while (section_list) { | |
| 85 section = (RcSection *) section_list->data; | |
| 86 g_free(section->name); | |
| 87 | |
| 88 line_list = section->lines; | |
| 89 while (line_list) { | |
| 90 line = (RcLine *) line_list->data; | |
| 91 g_free(line->key); | |
| 92 g_free(line->value); | |
| 93 g_free(line); | |
| 94 line_list = g_list_next(line_list); | |
| 95 } | |
| 96 g_list_free(section->lines); | |
| 97 g_free(section); | |
| 98 | |
| 99 section_list = g_list_next(section_list); | |
| 100 } | |
| 101 g_list_free(file->sections); | |
| 102 g_free(file); | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * bmp_rcfile_open: | |
| 107 * @filename: Path to rcfile to open. | |
| 108 * | |
| 109 * Opens an rcfile and returns an #RcFile object representing it. | |
| 110 * | |
| 111 * Return value: An #RcFile object representing the rcfile given. | |
| 112 **/ | |
| 113 RcFile * | |
| 114 bmp_rcfile_open(const gchar * filename) | |
| 115 { | |
| 116 RcFile *file; | |
| 117 | |
| 118 gchar *buffer, **lines, *tmp; | |
| 119 gint i; | |
| 120 RcSection *section = NULL; | |
| 121 | |
| 122 g_return_val_if_fail(filename != NULL, FALSE); | |
| 123 g_return_val_if_fail(strlen(filename) > 0, FALSE); | |
| 124 | |
| 125 if (!g_file_get_contents(filename, &buffer, NULL, NULL)) | |
| 126 return NULL; | |
| 127 | |
| 128 file = bmp_rcfile_new(); | |
| 129 lines = g_strsplit(buffer, "\n", 0); | |
| 130 g_free(buffer); | |
| 131 i = 0; | |
| 132 while (lines[i]) { | |
| 133 if (lines[i][0] == '[') { | |
| 134 if ((tmp = strchr(lines[i], ']'))) { | |
| 135 *tmp = '\0'; | |
| 136 section = bmp_rcfile_create_section(file, &lines[i][1]); | |
| 137 } | |
| 138 } | |
| 139 else if (lines[i][0] != '#' && section) { | |
| 140 if ((tmp = strchr(lines[i], '='))) { | |
| 141 gchar **frags; | |
| 142 frags = g_strsplit(lines[i], "=", 2); | |
| 143 if (strlen(frags[1]) > 0) { | |
| 144 bmp_rcfile_create_string(section, frags[0], frags[1]); | |
| 145 }; | |
| 146 g_strfreev(frags); | |
| 147 } | |
| 148 } | |
| 149 i++; | |
| 150 } | |
| 151 g_strfreev(lines); | |
| 152 return file; | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * bmp_rcfile_write: | |
| 157 * @file: A #RcFile object to write to disk. | |
| 158 * @filename: A path to write the #RcFile object's data to. | |
| 159 * | |
| 160 * Writes the contents of a #RcFile object to disk. | |
| 161 * | |
| 162 * Return value: TRUE on success, FALSE otherwise. | |
| 163 **/ | |
| 164 gboolean | |
| 165 bmp_rcfile_write(RcFile * file, const gchar * filename) | |
| 166 { | |
| 167 FILE *fp; | |
| 168 GList *section_list, *line_list; | |
| 169 RcSection *section; | |
| 170 RcLine *line; | |
| 171 | |
| 172 g_return_val_if_fail(file != NULL, FALSE); | |
| 173 g_return_val_if_fail(filename != NULL, FALSE); | |
| 174 | |
| 175 if (!(fp = fopen(filename, "w"))) | |
| 176 return FALSE; | |
| 177 | |
| 178 section_list = file->sections; | |
| 179 while (section_list) { | |
| 180 section = (RcSection *) section_list->data; | |
| 181 if (section->lines) { | |
| 182 fprintf(fp, "[%s]\n", section->name); | |
| 183 line_list = section->lines; | |
| 184 while (line_list) { | |
| 185 line = (RcLine *) line_list->data; | |
| 186 fprintf(fp, "%s=%s\n", line->key, line->value); | |
| 187 line_list = g_list_next(line_list); | |
| 188 } | |
| 189 fprintf(fp, "\n"); | |
| 190 } | |
| 191 section_list = g_list_next(section_list); | |
| 192 } | |
| 193 fclose(fp); | |
| 194 return TRUE; | |
| 195 } | |
| 196 | |
| 197 /** | |
| 198 * bmp_rcfile_read_string: | |
| 199 * @file: A #RcFile object to write to disk. | |
| 200 * @section: The section of the RcFile to look in. | |
| 201 * @key: The name of the identifier to look up. | |
| 202 * @value: A pointer to a memory location to place the data. | |
| 203 * | |
| 204 * Looks up a value in an RcFile and places it in %value. | |
| 205 * | |
| 206 * Return value: TRUE on success, FALSE otherwise. | |
| 207 **/ | |
| 208 gboolean | |
| 209 bmp_rcfile_read_string(RcFile * file, const gchar * section, | |
| 210 const gchar * key, gchar ** value) | |
| 211 { | |
| 212 RcSection *sect; | |
| 213 RcLine *line; | |
| 214 | |
| 215 g_return_val_if_fail(file != NULL, FALSE); | |
| 216 g_return_val_if_fail(section != NULL, FALSE); | |
| 217 g_return_val_if_fail(key != NULL, FALSE); | |
| 218 g_return_val_if_fail(value != NULL, FALSE); | |
| 219 | |
| 220 if (!(sect = bmp_rcfile_find_section(file, section))) | |
| 221 return FALSE; | |
| 222 if (!(line = bmp_rcfile_find_string(sect, key))) | |
| 223 return FALSE; | |
| 224 *value = g_strdup(line->value); | |
| 225 return TRUE; | |
| 226 } | |
| 227 | |
| 228 /** | |
| 229 * bmp_rcfile_read_int: | |
| 230 * @file: A #RcFile object to write to disk. | |
| 231 * @section: The section of the RcFile to look in. | |
| 232 * @key: The name of the identifier to look up. | |
| 233 * @value: A pointer to a memory location to place the data. | |
| 234 * | |
| 235 * Looks up a value in an RcFile and places it in %value. | |
| 236 * | |
| 237 * Return value: TRUE on success, FALSE otherwise. | |
| 238 **/ | |
| 239 gboolean | |
| 240 bmp_rcfile_read_int(RcFile * file, const gchar * section, | |
| 241 const gchar * key, gint * value) | |
| 242 { | |
| 243 gchar *str; | |
| 244 | |
| 245 g_return_val_if_fail(file != NULL, FALSE); | |
| 246 g_return_val_if_fail(section != NULL, FALSE); | |
| 247 g_return_val_if_fail(key != NULL, FALSE); | |
| 248 g_return_val_if_fail(value != NULL, FALSE); | |
| 249 | |
| 250 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
| 251 return FALSE; | |
| 252 *value = atoi(str); | |
| 253 g_free(str); | |
| 254 | |
| 255 return TRUE; | |
| 256 } | |
| 257 | |
| 258 /** | |
| 259 * bmp_rcfile_read_bool: | |
| 260 * @file: A #RcFile object to write to disk. | |
| 261 * @section: The section of the RcFile to look in. | |
| 262 * @key: The name of the identifier to look up. | |
| 263 * @value: A pointer to a memory location to place the data. | |
| 264 * | |
| 265 * Looks up a value in an RcFile and places it in %value. | |
| 266 * | |
| 267 * Return value: TRUE on success, FALSE otherwise. | |
| 268 **/ | |
| 269 gboolean | |
| 270 bmp_rcfile_read_bool(RcFile * file, const gchar * section, | |
| 271 const gchar * key, gboolean * value) | |
| 272 { | |
| 273 gchar *str; | |
| 274 | |
| 275 g_return_val_if_fail(file != NULL, FALSE); | |
| 276 g_return_val_if_fail(section != NULL, FALSE); | |
| 277 g_return_val_if_fail(key != NULL, FALSE); | |
| 278 g_return_val_if_fail(value != NULL, FALSE); | |
| 279 | |
| 280 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
| 281 return FALSE; | |
| 282 if (!strcasecmp(str, "TRUE")) | |
| 283 *value = TRUE; | |
| 284 else | |
| 285 *value = FALSE; | |
| 286 g_free(str); | |
| 287 return TRUE; | |
| 288 } | |
| 289 | |
| 290 /** | |
| 291 * bmp_rcfile_read_float: | |
| 292 * @file: A #RcFile object to write to disk. | |
| 293 * @section: The section of the RcFile to look in. | |
| 294 * @key: The name of the identifier to look up. | |
| 295 * @value: A pointer to a memory location to place the data. | |
| 296 * | |
| 297 * Looks up a value in an RcFile and places it in %value. | |
| 298 * | |
| 299 * Return value: TRUE on success, FALSE otherwise. | |
| 300 **/ | |
| 301 gboolean | |
| 302 bmp_rcfile_read_float(RcFile * file, const gchar * section, | |
| 303 const gchar * key, gfloat * value) | |
| 304 { | |
| 305 gchar *str, *locale; | |
| 306 | |
| 307 g_return_val_if_fail(file != NULL, FALSE); | |
| 308 g_return_val_if_fail(section != NULL, FALSE); | |
| 309 g_return_val_if_fail(key != NULL, FALSE); | |
| 310 g_return_val_if_fail(value != NULL, FALSE); | |
| 311 | |
| 312 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
| 313 return FALSE; | |
| 314 | |
| 315 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
| 316 setlocale(LC_NUMERIC, "C"); | |
| 317 *value = strtod(str, NULL); | |
| 318 setlocale(LC_NUMERIC, locale); | |
| 319 g_free(locale); | |
| 320 g_free(str); | |
| 321 | |
| 322 return TRUE; | |
| 323 } | |
| 324 | |
| 325 /** | |
| 326 * bmp_rcfile_read_double: | |
| 327 * @file: A #RcFile object to write to disk. | |
| 328 * @section: The section of the RcFile to look in. | |
| 329 * @key: The name of the identifier to look up. | |
| 330 * @value: A pointer to a memory location to place the data. | |
| 331 * | |
| 332 * Looks up a value in an RcFile and places it in %value. | |
| 333 * | |
| 334 * Return value: TRUE on success, FALSE otherwise. | |
| 335 **/ | |
| 336 gboolean | |
| 337 bmp_rcfile_read_double(RcFile * file, const gchar * section, | |
| 338 const gchar * key, gdouble * value) | |
| 339 { | |
| 340 gchar *str, *locale; | |
| 341 | |
| 342 g_return_val_if_fail(file != NULL, FALSE); | |
| 343 g_return_val_if_fail(section != NULL, FALSE); | |
| 344 g_return_val_if_fail(key != NULL, FALSE); | |
| 345 g_return_val_if_fail(value != NULL, FALSE); | |
| 346 | |
| 347 if (!bmp_rcfile_read_string(file, section, key, &str)) | |
| 348 return FALSE; | |
| 349 | |
| 350 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
| 351 setlocale(LC_NUMERIC, "C"); | |
| 352 *value = strtod(str, NULL); | |
| 353 setlocale(LC_NUMERIC, locale); | |
| 354 g_free(locale); | |
| 355 g_free(str); | |
| 356 | |
| 357 return TRUE; | |
| 358 } | |
| 359 | |
| 360 /** | |
| 361 * bmp_rcfile_write_string: | |
| 362 * @file: A #RcFile object to write to disk. | |
| 363 * @section: The section of the RcFile to set the key in. | |
| 364 * @key: The name of the identifier to set. | |
| 365 * @value: The value to set for that identifier. | |
| 366 * | |
| 367 * Sets a value in an RcFile for %key. | |
| 368 **/ | |
| 369 void | |
| 370 bmp_rcfile_write_string(RcFile * file, const gchar * section, | |
| 371 const gchar * key, const gchar * value) | |
| 372 { | |
| 373 RcSection *sect; | |
| 374 RcLine *line; | |
| 375 | |
| 376 g_return_if_fail(file != NULL); | |
| 377 g_return_if_fail(section != NULL); | |
| 378 g_return_if_fail(key != NULL); | |
| 379 g_return_if_fail(value != NULL); | |
| 380 | |
| 381 sect = bmp_rcfile_find_section(file, section); | |
| 382 if (!sect) | |
| 383 sect = bmp_rcfile_create_section(file, section); | |
| 384 if ((line = bmp_rcfile_find_string(sect, key))) { | |
| 385 g_free(line->value); | |
| 386 line->value = g_strstrip(g_strdup(value)); | |
| 387 } | |
| 388 else | |
| 389 bmp_rcfile_create_string(sect, key, value); | |
| 390 } | |
| 391 | |
| 392 /** | |
| 393 * bmp_rcfile_write_int: | |
| 394 * @file: A #RcFile object to write to disk. | |
| 395 * @section: The section of the RcFile to set the key in. | |
| 396 * @key: The name of the identifier to set. | |
| 397 * @value: The value to set for that identifier. | |
| 398 * | |
| 399 * Sets a value in an RcFile for %key. | |
| 400 **/ | |
| 401 void | |
| 402 bmp_rcfile_write_int(RcFile * file, const gchar * section, | |
| 403 const gchar * key, gint value) | |
| 404 { | |
| 405 gchar *strvalue; | |
| 406 | |
| 407 g_return_if_fail(file != NULL); | |
| 408 g_return_if_fail(section != NULL); | |
| 409 g_return_if_fail(key != NULL); | |
| 410 | |
| 411 strvalue = g_strdup_printf("%d", value); | |
| 412 bmp_rcfile_write_string(file, section, key, strvalue); | |
| 413 g_free(strvalue); | |
| 414 } | |
| 415 | |
| 416 /** | |
| 417 * bmp_rcfile_write_boolean: | |
| 418 * @file: A #RcFile object to write to disk. | |
| 419 * @section: The section of the RcFile to set the key in. | |
| 420 * @key: The name of the identifier to set. | |
| 421 * @value: The value to set for that identifier. | |
| 422 * | |
| 423 * Sets a value in an RcFile for %key. | |
| 424 **/ | |
| 425 void | |
| 426 bmp_rcfile_write_boolean(RcFile * file, const gchar * section, | |
| 427 const gchar * key, gboolean value) | |
| 428 { | |
| 429 g_return_if_fail(file != NULL); | |
| 430 g_return_if_fail(section != NULL); | |
| 431 g_return_if_fail(key != NULL); | |
| 432 | |
| 433 if (value) | |
| 434 bmp_rcfile_write_string(file, section, key, "TRUE"); | |
| 435 else | |
| 436 bmp_rcfile_write_string(file, section, key, "FALSE"); | |
| 437 } | |
| 438 | |
| 439 /** | |
| 440 * bmp_rcfile_write_float: | |
| 441 * @file: A #RcFile object to write to disk. | |
| 442 * @section: The section of the RcFile to set the key in. | |
| 443 * @key: The name of the identifier to set. | |
| 444 * @value: The value to set for that identifier. | |
| 445 * | |
| 446 * Sets a value in an RcFile for %key. | |
| 447 **/ | |
| 448 void | |
| 449 bmp_rcfile_write_float(RcFile * file, const gchar * section, | |
| 450 const gchar * key, gfloat value) | |
| 451 { | |
| 452 gchar *strvalue, *locale; | |
| 453 | |
| 454 g_return_if_fail(file != NULL); | |
| 455 g_return_if_fail(section != NULL); | |
| 456 g_return_if_fail(key != NULL); | |
| 457 | |
| 458 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
| 459 setlocale(LC_NUMERIC, "C"); | |
| 460 strvalue = g_strdup_printf("%g", value); | |
| 461 setlocale(LC_NUMERIC, locale); | |
| 462 bmp_rcfile_write_string(file, section, key, strvalue); | |
| 463 g_free(locale); | |
| 464 g_free(strvalue); | |
| 465 } | |
| 466 | |
| 467 /** | |
| 468 * bmp_rcfile_write_double: | |
| 469 * @file: A #RcFile object to write to disk. | |
| 470 * @section: The section of the RcFile to set the key in. | |
| 471 * @key: The name of the identifier to set. | |
| 472 * @value: The value to set for that identifier. | |
| 473 * | |
| 474 * Sets a value in an RcFile for %key. | |
| 475 **/ | |
| 476 void | |
| 477 bmp_rcfile_write_double(RcFile * file, const gchar * section, | |
| 478 const gchar * key, gdouble value) | |
| 479 { | |
| 480 gchar *strvalue, *locale; | |
| 481 | |
| 482 g_return_if_fail(file != NULL); | |
| 483 g_return_if_fail(section != NULL); | |
| 484 g_return_if_fail(key != NULL); | |
| 485 | |
| 486 locale = g_strdup(setlocale(LC_NUMERIC, NULL)); | |
| 487 setlocale(LC_NUMERIC, "C"); | |
| 488 strvalue = g_strdup_printf("%g", value); | |
| 489 setlocale(LC_NUMERIC, locale); | |
| 490 bmp_rcfile_write_string(file, section, key, strvalue); | |
| 491 g_free(locale); | |
| 492 g_free(strvalue); | |
| 493 } | |
| 494 | |
| 495 /** | |
| 496 * bmp_rcfile_remove_key: | |
| 497 * @file: A #RcFile object to write to disk. | |
| 498 * @section: The section of the RcFile to set the key in. | |
| 499 * @key: The name of the identifier to remove. | |
| 500 * | |
| 501 * Removes %key from an #RcFile object. | |
| 502 **/ | |
| 503 void | |
| 504 bmp_rcfile_remove_key(RcFile * file, const gchar * section, const gchar * key) | |
| 505 { | |
| 506 RcSection *sect; | |
| 507 RcLine *line; | |
| 508 | |
| 509 g_return_if_fail(file != NULL); | |
| 510 g_return_if_fail(section != NULL); | |
| 511 g_return_if_fail(key != NULL); | |
| 512 | |
| 513 if ((sect = bmp_rcfile_find_section(file, section)) != NULL) { | |
| 514 if ((line = bmp_rcfile_find_string(sect, key)) != NULL) { | |
| 515 g_free(line->key); | |
| 516 g_free(line->value); | |
| 517 g_free(line); | |
| 518 sect->lines = g_list_remove(sect->lines, line); | |
| 519 } | |
| 520 } | |
| 521 } | |
| 522 | |
| 523 static RcSection * | |
| 524 bmp_rcfile_create_section(RcFile * file, const gchar * name) | |
| 525 { | |
| 526 RcSection *section; | |
| 527 | |
| 528 section = g_new0(RcSection, 1); | |
| 529 section->name = g_strdup(name); | |
| 530 file->sections = g_list_append(file->sections, section); | |
| 531 | |
| 532 return section; | |
| 533 } | |
| 534 | |
| 535 static RcLine * | |
| 536 bmp_rcfile_create_string(RcSection * section, | |
| 537 const gchar * key, const gchar * value) | |
| 538 { | |
| 539 RcLine *line; | |
| 540 | |
| 541 line = g_new0(RcLine, 1); | |
| 542 line->key = g_strstrip(g_strdup(key)); | |
| 543 line->value = g_strstrip(g_strdup(value)); | |
| 544 section->lines = g_list_append(section->lines, line); | |
| 545 | |
| 546 return line; | |
| 547 } | |
| 548 | |
| 549 static RcSection * | |
| 550 bmp_rcfile_find_section(RcFile * file, const gchar * name) | |
| 551 { | |
| 552 RcSection *section; | |
| 553 GList *list; | |
| 554 | |
| 555 list = file->sections; | |
| 556 while (list) { | |
| 557 section = (RcSection *) list->data; | |
| 558 if (!strcasecmp(section->name, name)) | |
| 559 return section; | |
| 560 list = g_list_next(list); | |
| 561 } | |
| 562 return NULL; | |
| 563 } | |
| 564 | |
| 565 static RcLine * | |
| 566 bmp_rcfile_find_string(RcSection * section, const gchar * key) | |
| 567 { | |
| 568 RcLine *line; | |
| 569 GList *list; | |
| 570 | |
| 571 list = section->lines; | |
| 572 while (list) { | |
| 573 line = (RcLine *) list->data; | |
| 574 if (!strcasecmp(line->key, key)) | |
| 575 return line; | |
| 576 list = g_list_next(list); | |
| 577 } | |
| 578 return NULL; | |
| 579 } |
