0
|
1 /* This program is free software; you can redistribute it and/or modify
|
|
2 * it under the terms of the GNU General Public License as published by
|
|
3 * the Free Software Foundation; either version 2 of the License, or
|
|
4 * (at your option) any later version.
|
|
5 *
|
|
6 * This program is distributed in the hope that it will be useful,
|
|
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9 * GNU General Public License for more details.
|
|
10 *
|
|
11 * You should have received a copy of the GNU General Public License
|
|
12 * along with this program; if not, write to the Free Software
|
|
13 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
14 */
|
|
15
|
|
16 #include "rcfile.h"
|
|
17
|
|
18 #include <stdio.h>
|
|
19 #include <string.h>
|
|
20 #include <stdlib.h>
|
|
21 #include <locale.h>
|
|
22
|
|
23 #include <unistd.h>
|
|
24 #include <sys/stat.h>
|
|
25
|
|
26
|
|
27 static RcSection *bmp_rcfile_create_section(RcFile * file,
|
|
28 const gchar * name);
|
|
29 static RcLine *bmp_rcfile_create_string(RcSection * section,
|
|
30 const gchar * key,
|
|
31 const gchar * value);
|
|
32 static RcSection *bmp_rcfile_find_section(RcFile * file, const gchar * name);
|
|
33 static RcLine *bmp_rcfile_find_string(RcSection * section, const gchar * key);
|
|
34
|
|
35
|
|
36 RcFile *
|
|
37 bmp_rcfile_new(void)
|
|
38 {
|
|
39 return g_new0(RcFile, 1);
|
|
40 }
|
|
41
|
|
42 void
|
|
43 bmp_rcfile_free(RcFile * file)
|
|
44 {
|
|
45 RcSection *section;
|
|
46 RcLine *line;
|
|
47 GList *section_list, *line_list;
|
|
48
|
|
49 if (file == NULL)
|
|
50 return;
|
|
51
|
|
52 section_list = file->sections;
|
|
53 while (section_list) {
|
|
54 section = (RcSection *) section_list->data;
|
|
55 g_free(section->name);
|
|
56
|
|
57 line_list = section->lines;
|
|
58 while (line_list) {
|
|
59 line = (RcLine *) line_list->data;
|
|
60 g_free(line->key);
|
|
61 g_free(line->value);
|
|
62 g_free(line);
|
|
63 line_list = g_list_next(line_list);
|
|
64 }
|
|
65 g_list_free(section->lines);
|
|
66 g_free(section);
|
|
67
|
|
68 section_list = g_list_next(section_list);
|
|
69 }
|
|
70 g_list_free(file->sections);
|
|
71 g_free(file);
|
|
72 }
|
|
73
|
|
74 RcFile *
|
|
75 bmp_rcfile_open(const gchar * filename)
|
|
76 {
|
|
77 RcFile *file;
|
|
78
|
|
79 gchar *buffer, **lines, *tmp;
|
|
80 gint i;
|
|
81 RcSection *section = NULL;
|
|
82
|
|
83 g_return_val_if_fail(filename != NULL, FALSE);
|
|
84 g_return_val_if_fail(strlen(filename) > 0, FALSE);
|
|
85
|
|
86 if (!g_file_get_contents(filename, &buffer, NULL, NULL))
|
|
87 return NULL;
|
|
88
|
|
89 file = g_malloc0(sizeof(RcFile));
|
|
90 lines = g_strsplit(buffer, "\n", 0);
|
|
91 g_free(buffer);
|
|
92 i = 0;
|
|
93 while (lines[i]) {
|
|
94 if (lines[i][0] == '[') {
|
|
95 if ((tmp = strchr(lines[i], ']'))) {
|
|
96 *tmp = '\0';
|
|
97 section = bmp_rcfile_create_section(file, &lines[i][1]);
|
|
98 }
|
|
99 }
|
|
100 else if (lines[i][0] != '#' && section) {
|
|
101 if ((tmp = strchr(lines[i], '='))) {
|
|
102 gchar **frags;
|
|
103 frags = g_strsplit(lines[i], "=", 0);
|
|
104 if (strlen(frags[1]) > 0) {
|
|
105 bmp_rcfile_create_string(section, frags[0], frags[1]);
|
|
106 };
|
|
107 }
|
|
108 }
|
|
109 i++;
|
|
110 }
|
|
111 g_strfreev(lines);
|
|
112 return file;
|
|
113 }
|
|
114
|
|
115 gboolean
|
|
116 bmp_rcfile_write(RcFile * file, const gchar * filename)
|
|
117 {
|
|
118 FILE *fp;
|
|
119 GList *section_list, *line_list;
|
|
120 RcSection *section;
|
|
121 RcLine *line;
|
|
122
|
|
123 g_return_val_if_fail(file != NULL, FALSE);
|
|
124 g_return_val_if_fail(filename != NULL, FALSE);
|
|
125
|
|
126 if (!(fp = fopen(filename, "w")))
|
|
127 return FALSE;
|
|
128
|
|
129 section_list = file->sections;
|
|
130 while (section_list) {
|
|
131 section = (RcSection *) section_list->data;
|
|
132 if (section->lines) {
|
|
133 fprintf(fp, "[%s]\n", section->name);
|
|
134 line_list = section->lines;
|
|
135 while (line_list) {
|
|
136 line = (RcLine *) line_list->data;
|
|
137 fprintf(fp, "%s=%s\n", line->key, line->value);
|
|
138 line_list = g_list_next(line_list);
|
|
139 }
|
|
140 fprintf(fp, "\n");
|
|
141 }
|
|
142 section_list = g_list_next(section_list);
|
|
143 }
|
|
144 fclose(fp);
|
|
145 return TRUE;
|
|
146 }
|
|
147
|
|
148 gboolean
|
|
149 bmp_rcfile_read_string(RcFile * file, const gchar * section,
|
|
150 const gchar * key, gchar ** value)
|
|
151 {
|
|
152 RcSection *sect;
|
|
153 RcLine *line;
|
|
154
|
|
155 g_return_val_if_fail(file != NULL, FALSE);
|
|
156 g_return_val_if_fail(section != NULL, FALSE);
|
|
157 g_return_val_if_fail(key != NULL, FALSE);
|
|
158 g_return_val_if_fail(value != NULL, FALSE);
|
|
159
|
|
160 if (!(sect = bmp_rcfile_find_section(file, section)))
|
|
161 return FALSE;
|
|
162 if (!(line = bmp_rcfile_find_string(sect, key)))
|
|
163 return FALSE;
|
|
164 *value = g_strdup(line->value);
|
|
165 return TRUE;
|
|
166 }
|
|
167
|
|
168 gboolean
|
|
169 bmp_rcfile_read_int(RcFile * file, const gchar * section,
|
|
170 const gchar * key, gint * value)
|
|
171 {
|
|
172 gchar *str;
|
|
173
|
|
174 g_return_val_if_fail(file != NULL, FALSE);
|
|
175 g_return_val_if_fail(section != NULL, FALSE);
|
|
176 g_return_val_if_fail(key != NULL, FALSE);
|
|
177 g_return_val_if_fail(value != NULL, FALSE);
|
|
178
|
|
179 if (!bmp_rcfile_read_string(file, section, key, &str))
|
|
180 return FALSE;
|
|
181 *value = atoi(str);
|
|
182 g_free(str);
|
|
183
|
|
184 return TRUE;
|
|
185 }
|
|
186
|
|
187 gboolean
|
|
188 bmp_rcfile_read_bool(RcFile * file, const gchar * section,
|
|
189 const gchar * key, gboolean * value)
|
|
190 {
|
|
191 gchar *str;
|
|
192
|
|
193 g_return_val_if_fail(file != NULL, FALSE);
|
|
194 g_return_val_if_fail(section != NULL, FALSE);
|
|
195 g_return_val_if_fail(key != NULL, FALSE);
|
|
196 g_return_val_if_fail(value != NULL, FALSE);
|
|
197
|
|
198 if (!bmp_rcfile_read_string(file, section, key, &str))
|
|
199 return FALSE;
|
|
200 if (!strcasecmp(str, "TRUE"))
|
|
201 *value = TRUE;
|
|
202 else
|
|
203 *value = FALSE;
|
|
204 g_free(str);
|
|
205 return TRUE;
|
|
206 }
|
|
207
|
|
208 gboolean
|
|
209 bmp_rcfile_read_float(RcFile * file, const gchar * section,
|
|
210 const gchar * key, gfloat * value)
|
|
211 {
|
|
212 gchar *str, *locale;
|
|
213
|
|
214 g_return_val_if_fail(file != NULL, FALSE);
|
|
215 g_return_val_if_fail(section != NULL, FALSE);
|
|
216 g_return_val_if_fail(key != NULL, FALSE);
|
|
217 g_return_val_if_fail(value != NULL, FALSE);
|
|
218
|
|
219 if (!bmp_rcfile_read_string(file, section, key, &str))
|
|
220 return FALSE;
|
|
221
|
|
222 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
223 setlocale(LC_NUMERIC, "C");
|
|
224 *value = strtod(str, NULL);
|
|
225 setlocale(LC_NUMERIC, locale);
|
|
226 g_free(locale);
|
|
227 g_free(str);
|
|
228
|
|
229 return TRUE;
|
|
230 }
|
|
231
|
|
232 gboolean
|
|
233 bmp_rcfile_read_double(RcFile * file, const gchar * section,
|
|
234 const gchar * key, gdouble * value)
|
|
235 {
|
|
236 gchar *str, *locale;
|
|
237
|
|
238 g_return_val_if_fail(file != NULL, FALSE);
|
|
239 g_return_val_if_fail(section != NULL, FALSE);
|
|
240 g_return_val_if_fail(key != NULL, FALSE);
|
|
241 g_return_val_if_fail(value != NULL, FALSE);
|
|
242
|
|
243 if (!bmp_rcfile_read_string(file, section, key, &str))
|
|
244 return FALSE;
|
|
245
|
|
246 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
247 setlocale(LC_NUMERIC, "C");
|
|
248 *value = strtod(str, NULL);
|
|
249 setlocale(LC_NUMERIC, locale);
|
|
250 g_free(locale);
|
|
251 g_free(str);
|
|
252
|
|
253 return TRUE;
|
|
254 }
|
|
255
|
|
256 void
|
|
257 bmp_rcfile_write_string(RcFile * file, const gchar * section,
|
|
258 const gchar * key, const gchar * value)
|
|
259 {
|
|
260 RcSection *sect;
|
|
261 RcLine *line;
|
|
262
|
|
263 g_return_if_fail(file != NULL);
|
|
264 g_return_if_fail(section != NULL);
|
|
265 g_return_if_fail(key != NULL);
|
|
266 g_return_if_fail(value != NULL);
|
|
267
|
|
268 sect = bmp_rcfile_find_section(file, section);
|
|
269 if (!sect)
|
|
270 sect = bmp_rcfile_create_section(file, section);
|
|
271 if ((line = bmp_rcfile_find_string(sect, key))) {
|
|
272 g_free(line->value);
|
|
273 line->value = g_strstrip(g_strdup(value));
|
|
274 }
|
|
275 else
|
|
276 bmp_rcfile_create_string(sect, key, value);
|
|
277 }
|
|
278
|
|
279 void
|
|
280 bmp_rcfile_write_int(RcFile * file, const gchar * section,
|
|
281 const gchar * key, gint value)
|
|
282 {
|
|
283 gchar *strvalue;
|
|
284
|
|
285 g_return_if_fail(file != NULL);
|
|
286 g_return_if_fail(section != NULL);
|
|
287 g_return_if_fail(key != NULL);
|
|
288
|
|
289 strvalue = g_strdup_printf("%d", value);
|
|
290 bmp_rcfile_write_string(file, section, key, strvalue);
|
|
291 g_free(strvalue);
|
|
292 }
|
|
293
|
|
294 void
|
|
295 bmp_rcfile_write_boolean(RcFile * file, const gchar * section,
|
|
296 const gchar * key, gboolean value)
|
|
297 {
|
|
298 g_return_if_fail(file != NULL);
|
|
299 g_return_if_fail(section != NULL);
|
|
300 g_return_if_fail(key != NULL);
|
|
301
|
|
302 if (value)
|
|
303 bmp_rcfile_write_string(file, section, key, "TRUE");
|
|
304 else
|
|
305 bmp_rcfile_write_string(file, section, key, "FALSE");
|
|
306 }
|
|
307
|
|
308 void
|
|
309 bmp_rcfile_write_float(RcFile * file, const gchar * section,
|
|
310 const gchar * key, gfloat value)
|
|
311 {
|
|
312 gchar *strvalue, *locale;
|
|
313
|
|
314 g_return_if_fail(file != NULL);
|
|
315 g_return_if_fail(section != NULL);
|
|
316 g_return_if_fail(key != NULL);
|
|
317
|
|
318 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
319 setlocale(LC_NUMERIC, "C");
|
|
320 strvalue = g_strdup_printf("%g", value);
|
|
321 setlocale(LC_NUMERIC, locale);
|
|
322 bmp_rcfile_write_string(file, section, key, strvalue);
|
|
323 g_free(locale);
|
|
324 g_free(strvalue);
|
|
325 }
|
|
326
|
|
327 void
|
|
328 bmp_rcfile_write_double(RcFile * file, const gchar * section,
|
|
329 const gchar * key, gdouble value)
|
|
330 {
|
|
331 gchar *strvalue, *locale;
|
|
332
|
|
333 g_return_if_fail(file != NULL);
|
|
334 g_return_if_fail(section != NULL);
|
|
335 g_return_if_fail(key != NULL);
|
|
336
|
|
337 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
338 setlocale(LC_NUMERIC, "C");
|
|
339 strvalue = g_strdup_printf("%g", value);
|
|
340 setlocale(LC_NUMERIC, locale);
|
|
341 bmp_rcfile_write_string(file, section, key, strvalue);
|
|
342 g_free(locale);
|
|
343 g_free(strvalue);
|
|
344 }
|
|
345
|
|
346 void
|
|
347 bmp_rcfile_remove_key(RcFile * file, const gchar * section, const gchar * key)
|
|
348 {
|
|
349 RcSection *sect;
|
|
350 RcLine *line;
|
|
351
|
|
352 g_return_if_fail(file != NULL);
|
|
353 g_return_if_fail(section != NULL);
|
|
354 g_return_if_fail(key != NULL);
|
|
355
|
|
356 if ((sect = bmp_rcfile_find_section(file, section)) != NULL) {
|
|
357 if ((line = bmp_rcfile_find_string(sect, key)) != NULL) {
|
|
358 g_free(line->key);
|
|
359 g_free(line->value);
|
|
360 g_free(line);
|
|
361 sect->lines = g_list_remove(sect->lines, line);
|
|
362 }
|
|
363 }
|
|
364 }
|
|
365
|
|
366 static RcSection *
|
|
367 bmp_rcfile_create_section(RcFile * file, const gchar * name)
|
|
368 {
|
|
369 RcSection *section;
|
|
370
|
|
371 section = g_new0(RcSection, 1);
|
|
372 section->name = g_strdup(name);
|
|
373 file->sections = g_list_append(file->sections, section);
|
|
374
|
|
375 return section;
|
|
376 }
|
|
377
|
|
378 static RcLine *
|
|
379 bmp_rcfile_create_string(RcSection * section,
|
|
380 const gchar * key, const gchar * value)
|
|
381 {
|
|
382 RcLine *line;
|
|
383
|
|
384 line = g_new0(RcLine, 1);
|
|
385 line->key = g_strstrip(g_strdup(key));
|
|
386 line->value = g_strstrip(g_strdup(value));
|
|
387 section->lines = g_list_append(section->lines, line);
|
|
388
|
|
389 return line;
|
|
390 }
|
|
391
|
|
392 static RcSection *
|
|
393 bmp_rcfile_find_section(RcFile * file, const gchar * name)
|
|
394 {
|
|
395 RcSection *section;
|
|
396 GList *list;
|
|
397
|
|
398 list = file->sections;
|
|
399 while (list) {
|
|
400 section = (RcSection *) list->data;
|
|
401 if (!strcasecmp(section->name, name))
|
|
402 return section;
|
|
403 list = g_list_next(list);
|
|
404 }
|
|
405 return NULL;
|
|
406 }
|
|
407
|
|
408 static RcLine *
|
|
409 bmp_rcfile_find_string(RcSection * section, const gchar * key)
|
|
410 {
|
|
411 RcLine *line;
|
|
412 GList *list;
|
|
413
|
|
414 list = section->lines;
|
|
415 while (list) {
|
|
416 line = (RcLine *) list->data;
|
|
417 if (!strcasecmp(line->key, key))
|
|
418 return line;
|
|
419 list = g_list_next(list);
|
|
420 }
|
|
421 return NULL;
|
|
422 }
|