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
|
1459
|
13 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
0
|
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;
|
1769
|
103 frags = g_strsplit(lines[i], "=", 2);
|
0
|
104 if (strlen(frags[1]) > 0) {
|
|
105 bmp_rcfile_create_string(section, frags[0], frags[1]);
|
|
106 };
|
1327
|
107 g_strfreev(frags);
|
0
|
108 }
|
|
109 }
|
|
110 i++;
|
|
111 }
|
|
112 g_strfreev(lines);
|
|
113 return file;
|
|
114 }
|
|
115
|
|
116 gboolean
|
|
117 bmp_rcfile_write(RcFile * file, const gchar * filename)
|
|
118 {
|
|
119 FILE *fp;
|
|
120 GList *section_list, *line_list;
|
|
121 RcSection *section;
|
|
122 RcLine *line;
|
|
123
|
|
124 g_return_val_if_fail(file != NULL, FALSE);
|
|
125 g_return_val_if_fail(filename != NULL, FALSE);
|
|
126
|
|
127 if (!(fp = fopen(filename, "w")))
|
|
128 return FALSE;
|
|
129
|
|
130 section_list = file->sections;
|
|
131 while (section_list) {
|
|
132 section = (RcSection *) section_list->data;
|
|
133 if (section->lines) {
|
|
134 fprintf(fp, "[%s]\n", section->name);
|
|
135 line_list = section->lines;
|
|
136 while (line_list) {
|
|
137 line = (RcLine *) line_list->data;
|
|
138 fprintf(fp, "%s=%s\n", line->key, line->value);
|
|
139 line_list = g_list_next(line_list);
|
|
140 }
|
|
141 fprintf(fp, "\n");
|
|
142 }
|
|
143 section_list = g_list_next(section_list);
|
|
144 }
|
|
145 fclose(fp);
|
|
146 return TRUE;
|
|
147 }
|
|
148
|
|
149 gboolean
|
|
150 bmp_rcfile_read_string(RcFile * file, const gchar * section,
|
|
151 const gchar * key, gchar ** value)
|
|
152 {
|
|
153 RcSection *sect;
|
|
154 RcLine *line;
|
|
155
|
|
156 g_return_val_if_fail(file != NULL, FALSE);
|
|
157 g_return_val_if_fail(section != NULL, FALSE);
|
|
158 g_return_val_if_fail(key != NULL, FALSE);
|
|
159 g_return_val_if_fail(value != NULL, FALSE);
|
|
160
|
|
161 if (!(sect = bmp_rcfile_find_section(file, section)))
|
|
162 return FALSE;
|
|
163 if (!(line = bmp_rcfile_find_string(sect, key)))
|
|
164 return FALSE;
|
|
165 *value = g_strdup(line->value);
|
|
166 return TRUE;
|
|
167 }
|
|
168
|
|
169 gboolean
|
|
170 bmp_rcfile_read_int(RcFile * file, const gchar * section,
|
|
171 const gchar * key, gint * value)
|
|
172 {
|
|
173 gchar *str;
|
|
174
|
|
175 g_return_val_if_fail(file != NULL, FALSE);
|
|
176 g_return_val_if_fail(section != NULL, FALSE);
|
|
177 g_return_val_if_fail(key != NULL, FALSE);
|
|
178 g_return_val_if_fail(value != NULL, FALSE);
|
|
179
|
|
180 if (!bmp_rcfile_read_string(file, section, key, &str))
|
|
181 return FALSE;
|
|
182 *value = atoi(str);
|
|
183 g_free(str);
|
|
184
|
|
185 return TRUE;
|
|
186 }
|
|
187
|
|
188 gboolean
|
|
189 bmp_rcfile_read_bool(RcFile * file, const gchar * section,
|
|
190 const gchar * key, gboolean * value)
|
|
191 {
|
|
192 gchar *str;
|
|
193
|
|
194 g_return_val_if_fail(file != NULL, FALSE);
|
|
195 g_return_val_if_fail(section != NULL, FALSE);
|
|
196 g_return_val_if_fail(key != NULL, FALSE);
|
|
197 g_return_val_if_fail(value != NULL, FALSE);
|
|
198
|
|
199 if (!bmp_rcfile_read_string(file, section, key, &str))
|
|
200 return FALSE;
|
|
201 if (!strcasecmp(str, "TRUE"))
|
|
202 *value = TRUE;
|
|
203 else
|
|
204 *value = FALSE;
|
|
205 g_free(str);
|
|
206 return TRUE;
|
|
207 }
|
|
208
|
|
209 gboolean
|
|
210 bmp_rcfile_read_float(RcFile * file, const gchar * section,
|
|
211 const gchar * key, gfloat * value)
|
|
212 {
|
|
213 gchar *str, *locale;
|
|
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 (!bmp_rcfile_read_string(file, section, key, &str))
|
|
221 return FALSE;
|
|
222
|
|
223 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
224 setlocale(LC_NUMERIC, "C");
|
|
225 *value = strtod(str, NULL);
|
|
226 setlocale(LC_NUMERIC, locale);
|
|
227 g_free(locale);
|
|
228 g_free(str);
|
|
229
|
|
230 return TRUE;
|
|
231 }
|
|
232
|
|
233 gboolean
|
|
234 bmp_rcfile_read_double(RcFile * file, const gchar * section,
|
|
235 const gchar * key, gdouble * value)
|
|
236 {
|
|
237 gchar *str, *locale;
|
|
238
|
|
239 g_return_val_if_fail(file != NULL, FALSE);
|
|
240 g_return_val_if_fail(section != NULL, FALSE);
|
|
241 g_return_val_if_fail(key != NULL, FALSE);
|
|
242 g_return_val_if_fail(value != NULL, FALSE);
|
|
243
|
|
244 if (!bmp_rcfile_read_string(file, section, key, &str))
|
|
245 return FALSE;
|
|
246
|
|
247 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
248 setlocale(LC_NUMERIC, "C");
|
|
249 *value = strtod(str, NULL);
|
|
250 setlocale(LC_NUMERIC, locale);
|
|
251 g_free(locale);
|
|
252 g_free(str);
|
|
253
|
|
254 return TRUE;
|
|
255 }
|
|
256
|
|
257 void
|
|
258 bmp_rcfile_write_string(RcFile * file, const gchar * section,
|
|
259 const gchar * key, const gchar * value)
|
|
260 {
|
|
261 RcSection *sect;
|
|
262 RcLine *line;
|
|
263
|
|
264 g_return_if_fail(file != NULL);
|
|
265 g_return_if_fail(section != NULL);
|
|
266 g_return_if_fail(key != NULL);
|
|
267 g_return_if_fail(value != NULL);
|
|
268
|
|
269 sect = bmp_rcfile_find_section(file, section);
|
|
270 if (!sect)
|
|
271 sect = bmp_rcfile_create_section(file, section);
|
|
272 if ((line = bmp_rcfile_find_string(sect, key))) {
|
|
273 g_free(line->value);
|
|
274 line->value = g_strstrip(g_strdup(value));
|
|
275 }
|
|
276 else
|
|
277 bmp_rcfile_create_string(sect, key, value);
|
|
278 }
|
|
279
|
|
280 void
|
|
281 bmp_rcfile_write_int(RcFile * file, const gchar * section,
|
|
282 const gchar * key, gint value)
|
|
283 {
|
|
284 gchar *strvalue;
|
|
285
|
|
286 g_return_if_fail(file != NULL);
|
|
287 g_return_if_fail(section != NULL);
|
|
288 g_return_if_fail(key != NULL);
|
|
289
|
|
290 strvalue = g_strdup_printf("%d", value);
|
|
291 bmp_rcfile_write_string(file, section, key, strvalue);
|
|
292 g_free(strvalue);
|
|
293 }
|
|
294
|
|
295 void
|
|
296 bmp_rcfile_write_boolean(RcFile * file, const gchar * section,
|
|
297 const gchar * key, gboolean value)
|
|
298 {
|
|
299 g_return_if_fail(file != NULL);
|
|
300 g_return_if_fail(section != NULL);
|
|
301 g_return_if_fail(key != NULL);
|
|
302
|
|
303 if (value)
|
|
304 bmp_rcfile_write_string(file, section, key, "TRUE");
|
|
305 else
|
|
306 bmp_rcfile_write_string(file, section, key, "FALSE");
|
|
307 }
|
|
308
|
|
309 void
|
|
310 bmp_rcfile_write_float(RcFile * file, const gchar * section,
|
|
311 const gchar * key, gfloat value)
|
|
312 {
|
|
313 gchar *strvalue, *locale;
|
|
314
|
|
315 g_return_if_fail(file != NULL);
|
|
316 g_return_if_fail(section != NULL);
|
|
317 g_return_if_fail(key != NULL);
|
|
318
|
|
319 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
320 setlocale(LC_NUMERIC, "C");
|
|
321 strvalue = g_strdup_printf("%g", value);
|
|
322 setlocale(LC_NUMERIC, locale);
|
|
323 bmp_rcfile_write_string(file, section, key, strvalue);
|
|
324 g_free(locale);
|
|
325 g_free(strvalue);
|
|
326 }
|
|
327
|
|
328 void
|
|
329 bmp_rcfile_write_double(RcFile * file, const gchar * section,
|
|
330 const gchar * key, gdouble value)
|
|
331 {
|
|
332 gchar *strvalue, *locale;
|
|
333
|
|
334 g_return_if_fail(file != NULL);
|
|
335 g_return_if_fail(section != NULL);
|
|
336 g_return_if_fail(key != NULL);
|
|
337
|
|
338 locale = g_strdup(setlocale(LC_NUMERIC, NULL));
|
|
339 setlocale(LC_NUMERIC, "C");
|
|
340 strvalue = g_strdup_printf("%g", value);
|
|
341 setlocale(LC_NUMERIC, locale);
|
|
342 bmp_rcfile_write_string(file, section, key, strvalue);
|
|
343 g_free(locale);
|
|
344 g_free(strvalue);
|
|
345 }
|
|
346
|
|
347 void
|
|
348 bmp_rcfile_remove_key(RcFile * file, const gchar * section, const gchar * key)
|
|
349 {
|
|
350 RcSection *sect;
|
|
351 RcLine *line;
|
|
352
|
|
353 g_return_if_fail(file != NULL);
|
|
354 g_return_if_fail(section != NULL);
|
|
355 g_return_if_fail(key != NULL);
|
|
356
|
|
357 if ((sect = bmp_rcfile_find_section(file, section)) != NULL) {
|
|
358 if ((line = bmp_rcfile_find_string(sect, key)) != NULL) {
|
|
359 g_free(line->key);
|
|
360 g_free(line->value);
|
|
361 g_free(line);
|
|
362 sect->lines = g_list_remove(sect->lines, line);
|
|
363 }
|
|
364 }
|
|
365 }
|
|
366
|
|
367 static RcSection *
|
|
368 bmp_rcfile_create_section(RcFile * file, const gchar * name)
|
|
369 {
|
|
370 RcSection *section;
|
|
371
|
|
372 section = g_new0(RcSection, 1);
|
|
373 section->name = g_strdup(name);
|
|
374 file->sections = g_list_append(file->sections, section);
|
|
375
|
|
376 return section;
|
|
377 }
|
|
378
|
|
379 static RcLine *
|
|
380 bmp_rcfile_create_string(RcSection * section,
|
|
381 const gchar * key, const gchar * value)
|
|
382 {
|
|
383 RcLine *line;
|
|
384
|
|
385 line = g_new0(RcLine, 1);
|
|
386 line->key = g_strstrip(g_strdup(key));
|
|
387 line->value = g_strstrip(g_strdup(value));
|
|
388 section->lines = g_list_append(section->lines, line);
|
|
389
|
|
390 return line;
|
|
391 }
|
|
392
|
|
393 static RcSection *
|
|
394 bmp_rcfile_find_section(RcFile * file, const gchar * name)
|
|
395 {
|
|
396 RcSection *section;
|
|
397 GList *list;
|
|
398
|
|
399 list = file->sections;
|
|
400 while (list) {
|
|
401 section = (RcSection *) list->data;
|
|
402 if (!strcasecmp(section->name, name))
|
|
403 return section;
|
|
404 list = g_list_next(list);
|
|
405 }
|
|
406 return NULL;
|
|
407 }
|
|
408
|
|
409 static RcLine *
|
|
410 bmp_rcfile_find_string(RcSection * section, const gchar * key)
|
|
411 {
|
|
412 RcLine *line;
|
|
413 GList *list;
|
|
414
|
|
415 list = section->lines;
|
|
416 while (list) {
|
|
417 line = (RcLine *) list->data;
|
|
418 if (!strcasecmp(line->key, key))
|
|
419 return line;
|
|
420 list = g_list_next(list);
|
|
421 }
|
|
422 return NULL;
|
|
423 }
|