1 /* gkeyfile.c - key file parser
2 *
3 * Copyright 2004 Red Hat, Inc.
4 * Copyright 2009-2010 Collabora Ltd.
5 * Copyright 2009 Nokia Corporation
6 *
7 * Written by Ray Strode <rstrode@redhat.com>
8 * Matthias Clasen <mclasen@redhat.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "config.h"
25
26 #include "gkeyfile.h"
27 #include "gutils.h"
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <locale.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef G_OS_UNIX
38 #include <unistd.h>
39 #endif
40 #ifdef G_OS_WIN32
41 #include <io.h>
42
43 #undef fstat
44 #define fstat(a,b) _fstati64(a,b)
45 #undef stat
46 #define stat _stati64
47
48 #ifndef S_ISREG
49 #define S_ISREG(mode) ((mode)&_S_IFREG)
50 #endif
51
52 #endif /* G_OS_WIN23 */
53
54 #include "gconvert.h"
55 #include "gdataset.h"
56 #include "gerror.h"
57 #include "gfileutils.h"
58 #include "ghash.h"
59 #include "glibintl.h"
60 #include "glist.h"
61 #include "gslist.h"
62 #include "gmem.h"
63 #include "gmessages.h"
64 #include "gstdio.h"
65 #include "gstring.h"
66 #include "gstrfuncs.h"
67 #include "gutils.h"
68
69
70 /**
71 * SECTION:keyfile
72 * @title: Key-value file parser
73 * @short_description: parses .ini-like config files
74 *
75 * #GKeyFile lets you parse, edit or create files containing groups of
76 * key-value pairs, which we call "key files" for lack of a better name.
77 * Several freedesktop.org specifications use key files now, e.g the
78 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
79 * and the
80 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
81 *
82 * The syntax of key files is described in detail in the
83 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
84 * here is a quick summary: Key files
85 * consists of groups of key-value pairs, interspersed with comments.
86 *
87 * |[
88 * # this is just an example
89 * # there can be comments before the first group
90 *
91 * [First Group]
92 *
93 * Name=Key File Example\tthis value shows\nescaping
94 *
95 * # localized strings are stored in multiple key-value pairs
96 * Welcome=Hello
97 * Welcome[de]=Hallo
98 * Welcome[fr_FR]=Bonjour
99 * Welcome[it]=Ciao
100 * Welcome[be@latin]=Hello
101 *
102 * [Another Group]
103 *
104 * Numbers=2;20;-200;0
105 *
106 * Booleans=true;false;true;true
107 * ]|
108 *
109 * Lines beginning with a '#' and blank lines are considered comments.
110 *
111 * Groups are started by a header line containing the group name enclosed
112 * in '[' and ']', and ended implicitly by the start of the next group or
113 * the end of the file. Each key-value pair must be contained in a group.
114 *
115 * Key-value pairs generally have the form `key=value`, with the
116 * exception of localized strings, which have the form
117 * `key[locale]=value`, with a locale identifier of the
118 * form `lang_COUNTRY@MODIFIER` where `COUNTRY` and `MODIFIER`
119 * are optional.
120 * Space before and after the '=' character are ignored. Newline, tab,
121 * carriage return and backslash characters in value are escaped as \n,
122 * \t, \r, and \\\\, respectively. To preserve leading spaces in values,
123 * these can also be escaped as \s.
124 *
125 * Key files can store strings (possibly with localized variants), integers,
126 * booleans and lists of these. Lists are separated by a separator character,
127 * typically ';' or ','. To use the list separator character in a value in
128 * a list, it has to be escaped by prefixing it with a backslash.
129 *
130 * This syntax is obviously inspired by the .ini files commonly met
131 * on Windows, but there are some important differences:
132 *
133 * - .ini files use the ';' character to begin comments,
134 * key files use the '#' character.
135 *
136 * - Key files do not allow for ungrouped keys meaning only
137 * comments can precede the first group.
138 *
139 * - Key files are always encoded in UTF-8.
140 *
141 * - Key and Group names are case-sensitive. For example, a group called
142 * [GROUP] is a different from [group].
143 *
144 * - .ini files don't have a strongly typed boolean entry type,
145 * they only have GetProfileInt(). In key files, only
146 * true and false (in lower case) are allowed.
147 *
148 * Note that in contrast to the
149 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
150 * groups in key files may contain the same
151 * key multiple times; the last entry wins. Key files may also contain
152 * multiple groups with the same name; they are merged together.
153 * Another difference is that keys and group names in key files are not
154 * restricted to ASCII characters.
155 *
156 * Here is an example of loading a key file and reading a value:
157 * |[<!-- language="C" -->
158 * g_autoptr(GError) error = NULL;
159 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
160 *
161 * if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
162 * {
163 * if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
164 * g_warning ("Error loading key file: %s", error->message);
165 * return;
166 * }
167 *
168 * g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
169 * if (val == NULL &&
170 * !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
171 * {
172 * g_warning ("Error finding key in key file: %s", error->message);
173 * return;
174 * }
175 * else if (val == NULL)
176 * {
177 * // Fall back to a default value.
178 * val = g_strdup ("default-value");
179 * }
180 * ]|
181 *
182 * Here is an example of creating and saving a key file:
183 * |[<!-- language="C" -->
184 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
185 * const gchar *val = …;
186 * g_autoptr(GError) error = NULL;
187 *
188 * g_key_file_set_string (key_file, "Group Name", "SomeKey", val);
189 *
190 * // Save as a file.
191 * if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
192 * {
193 * g_warning ("Error saving key file: %s", error->message);
194 * return;
195 * }
196 *
197 * // Or store to a GBytes for use elsewhere.
198 * gsize data_len;
199 * g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
200 * if (data == NULL)
201 * {
202 * g_warning ("Error saving key file: %s", error->message);
203 * return;
204 * }
205 * g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);
206 * ]|
207 */
208
209 /**
210 * G_KEY_FILE_ERROR:
211 *
212 * Error domain for key file parsing. Errors in this domain will
213 * be from the #GKeyFileError enumeration.
214 *
215 * See #GError for information on error domains.
216 */
217
218 /**
219 * GKeyFileError:
220 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
221 * an unknown encoding
222 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
223 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
224 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
225 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
226 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
227 *
228 * Error codes returned by key file parsing.
229 */
230
231 /**
232 * GKeyFileFlags:
233 * @G_KEY_FILE_NONE: No flags, default behaviour
234 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
235 * (possibly modified) contents of the key file back to a file;
236 * otherwise all comments will be lost when the key file is
237 * written back.
238 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
239 * (possibly modified) contents of the key file back to a file;
240 * otherwise only the translations for the current language will be
241 * written back.
242 *
243 * Flags which influence the parsing.
244 */
245
246 /**
247 * G_KEY_FILE_DESKTOP_GROUP:
248 *
249 * The name of the main group of a desktop entry file, as defined in the
250 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
251 * Consult the specification for more
252 * details about the meanings of the keys below.
253 *
254 * Since: 2.14
255 */
256
257 /**
258 * G_KEY_FILE_DESKTOP_KEY_TYPE:
259 *
260 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
261 * giving the type of the desktop entry. Usually
262 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
263 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
264 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
265 *
266 * Since: 2.14
267 */
268
269 /**
270 * G_KEY_FILE_DESKTOP_KEY_VERSION:
271 *
272 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
273 * giving the version of the Desktop Entry Specification used for
274 * the desktop entry file.
275 *
276 * Since: 2.14
277 */
278
279 /**
280 * G_KEY_FILE_DESKTOP_KEY_NAME:
281 *
282 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
283 * string giving the specific name of the desktop entry.
284 *
285 * Since: 2.14
286 */
287
288 /**
289 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
290 *
291 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
292 * string giving the generic name of the desktop entry.
293 *
294 * Since: 2.14
295 */
296
297 /**
298 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
299 *
300 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
301 * stating whether the desktop entry should be shown in menus.
302 *
303 * Since: 2.14
304 */
305
306 /**
307 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
308 *
309 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
310 * string giving the tooltip for the desktop entry.
311 *
312 * Since: 2.14
313 */
314
315 /**
316 * G_KEY_FILE_DESKTOP_KEY_ICON:
317 *
318 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
319 * string giving the name of the icon to be displayed for the desktop
320 * entry.
321 *
322 * Since: 2.14
323 */
324
325 /**
326 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
327 *
328 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
329 * stating whether the desktop entry has been deleted by the user.
330 *
331 * Since: 2.14
332 */
333
334 /**
335 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
336 *
337 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
338 * strings identifying the environments that should display the
339 * desktop entry.
340 *
341 * Since: 2.14
342 */
343
344 /**
345 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
346 *
347 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
348 * strings identifying the environments that should not display the
349 * desktop entry.
350 *
351 * Since: 2.14
352 */
353
354 /**
355 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
356 *
357 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
358 * giving the file name of a binary on disk used to determine if the
359 * program is actually installed. It is only valid for desktop entries
360 * with the `Application` type.
361 *
362 * Since: 2.14
363 */
364
365 /**
366 * G_KEY_FILE_DESKTOP_KEY_EXEC:
367 *
368 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
369 * giving the command line to execute. It is only valid for desktop
370 * entries with the `Application` type.
371 *
372 * Since: 2.14
373 */
374
375 /**
376 * G_KEY_FILE_DESKTOP_KEY_PATH:
377 *
378 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
379 * containing the working directory to run the program in. It is only
380 * valid for desktop entries with the `Application` type.
381 *
382 * Since: 2.14
383 */
384
385 /**
386 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
387 *
388 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
389 * stating whether the program should be run in a terminal window.
390 * It is only valid for desktop entries with the
391 * `Application` type.
392 *
393 * Since: 2.14
394 */
395
396 /**
397 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
398 *
399 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
400 * of strings giving the MIME types supported by this desktop entry.
401 *
402 * Since: 2.14
403 */
404
405 /**
406 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
407 *
408 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
409 * of strings giving the categories in which the desktop entry
410 * should be shown in a menu.
411 *
412 * Since: 2.14
413 */
414
415 /**
416 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
417 *
418 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
419 * stating whether the application supports the
420 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
421 *
422 * Since: 2.14
423 */
424
425 /**
426 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
427 *
428 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
429 * identifying the WM class or name hint of a window that the application
430 * will create, which can be used to emulate Startup Notification with
431 * older applications.
432 *
433 * Since: 2.14
434 */
435
436 /**
437 * G_KEY_FILE_DESKTOP_KEY_URL:
438 *
439 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
440 * giving the URL to access. It is only valid for desktop entries
441 * with the `Link` type.
442 *
443 * Since: 2.14
444 */
445
446 /**
447 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
448 *
449 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to true
450 * if the application is D-Bus activatable.
451 *
452 * Since: 2.38
453 */
454
455 /**
456 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
457 *
458 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
459 * giving the available application actions.
460 *
461 * Since: 2.38
462 */
463
464 /**
465 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
466 *
467 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
468 * entries representing applications.
469 *
470 * Since: 2.14
471 */
472
473 /**
474 * G_KEY_FILE_DESKTOP_TYPE_LINK:
475 *
476 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
477 * entries representing links to documents.
478 *
479 * Since: 2.14
480 */
481
482 /**
483 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
484 *
485 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
486 * entries representing directories.
487 *
488 * Since: 2.14
489 */
490
491 typedef struct _GKeyFileGroup GKeyFileGroup;
492
493 /**
494 * GKeyFile:
495 *
496 * The GKeyFile struct contains only private data
497 * and should not be accessed directly.
498 */
499 struct _GKeyFile
500 {
501 GList *groups;
502 GHashTable *group_hash;
503
504 GKeyFileGroup *start_group;
505 GKeyFileGroup *current_group;
506
507 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
508
509 gchar list_separator;
510
511 GKeyFileFlags flags;
512
513 gchar **locales;
514
515 volatile gint ref_count;
516 };
517
518 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
519
520 struct _GKeyFileGroup
521 {
522 const gchar *name; /* NULL for above first group (which will be comments) */
523
524 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
525
526 GList *key_value_pairs;
527
528 /* Used in parallel with key_value_pairs for
529 * increased lookup performance
530 */
531 GHashTable *lookup_map;
532 };
533
534 struct _GKeyFileKeyValuePair
535 {
536 gchar *key; /* NULL for comments */
537 gchar *value;
538 };
539
540 static gint find_file_in_data_dirs (const gchar *file,
541 const gchar **data_dirs,
542 gchar **output_file,
543 GError **error);
544 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
545 gint fd,
546 GKeyFileFlags flags,
547 GError **error);
548 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
549 const gchar *group_name);
550 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
551 const gchar *group_name);
552
553 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
554 GKeyFileGroup *group,
555 const gchar *key);
556 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
557 GKeyFileGroup *group,
558 const gchar *key);
559
560 static void g_key_file_remove_group_node (GKeyFile *key_file,
561 GList *group_node);
562 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
563 GKeyFileGroup *group,
564 GList *pair_node);
565
566 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
567 GKeyFileGroup *group,
568 GKeyFileKeyValuePair *pair);
569 static void g_key_file_add_key (GKeyFile *key_file,
570 GKeyFileGroup *group,
571 const gchar *key,
572 const gchar *value);
573 static void g_key_file_add_group (GKeyFile *key_file,
574 const gchar *group_name);
575 static gboolean g_key_file_is_group_name (const gchar *name);
576 static gboolean g_key_file_is_key_name (const gchar *name);
577 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
578 static gboolean g_key_file_line_is_comment (const gchar *line);
579 static gboolean g_key_file_line_is_group (const gchar *line);
580 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
581 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
582 const gchar *value,
583 GSList **separators,
584 GError **error);
585 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
586 const gchar *string,
587 gboolean escape_separator);
588 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
589 const gchar *value,
590 GError **error);
591 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
592 gint value);
593 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
594 const gchar *value,
595 GError **error);
596 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
597 const gchar *value,
598 GError **error);
599 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
600 gboolean value);
601 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
602 const gchar *value,
603 gboolean is_final_line);
604 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
605 const gchar *comment);
606 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
607 const gchar *line,
608 gsize length,
609 GError **error);
610 static void g_key_file_parse_comment (GKeyFile *key_file,
611 const gchar *line,
612 gsize length,
613 GError **error);
614 static void g_key_file_parse_group (GKeyFile *key_file,
615 const gchar *line,
616 gsize length,
617 GError **error);
618 static gchar *key_get_locale (const gchar *key);
619 static void g_key_file_parse_data (GKeyFile *key_file,
620 const gchar *data,
621 gsize length,
622 GError **error);
623 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
624 GError **error);
625
626 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
627
628 static void
g_key_file_init(GKeyFile * key_file)629 g_key_file_init (GKeyFile *key_file)
630 {
631 key_file->current_group = g_slice_new0 (GKeyFileGroup);
632 key_file->groups = g_list_prepend (NULL, key_file->current_group);
633 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
634 key_file->start_group = NULL;
635 key_file->parse_buffer = g_string_sized_new (128);
636 key_file->list_separator = ';';
637 key_file->flags = 0;
638 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
639 }
640
641 static void
g_key_file_clear(GKeyFile * key_file)642 g_key_file_clear (GKeyFile *key_file)
643 {
644 GList *tmp, *group_node;
645
646 if (key_file->locales)
647 {
648 g_strfreev (key_file->locales);
649 key_file->locales = NULL;
650 }
651
652 if (key_file->parse_buffer)
653 {
654 g_string_free (key_file->parse_buffer, TRUE);
655 key_file->parse_buffer = NULL;
656 }
657
658 tmp = key_file->groups;
659 while (tmp != NULL)
660 {
661 group_node = tmp;
662 tmp = tmp->next;
663 g_key_file_remove_group_node (key_file, group_node);
664 }
665
666 if (key_file->group_hash != NULL)
667 {
668 g_hash_table_destroy (key_file->group_hash);
669 key_file->group_hash = NULL;
670 }
671
672 g_warn_if_fail (key_file->groups == NULL);
673 }
674
675
676 /**
677 * g_key_file_new:
678 *
679 * Creates a new empty #GKeyFile object. Use
680 * g_key_file_load_from_file(), g_key_file_load_from_data(),
681 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
682 * read an existing key file.
683 *
684 * Returns: (transfer full): an empty #GKeyFile.
685 *
686 * Since: 2.6
687 **/
688 GKeyFile *
g_key_file_new(void)689 g_key_file_new (void)
690 {
691 GKeyFile *key_file;
692
693 key_file = g_slice_new0 (GKeyFile);
694 key_file->ref_count = 1;
695 g_key_file_init (key_file);
696
697 return key_file;
698 }
699
700 /**
701 * g_key_file_set_list_separator:
702 * @key_file: a #GKeyFile
703 * @separator: the separator
704 *
705 * Sets the character which is used to separate
706 * values in lists. Typically ';' or ',' are used
707 * as separators. The default list separator is ';'.
708 *
709 * Since: 2.6
710 */
711 void
g_key_file_set_list_separator(GKeyFile * key_file,gchar separator)712 g_key_file_set_list_separator (GKeyFile *key_file,
713 gchar separator)
714 {
715 g_return_if_fail (key_file != NULL);
716
717 key_file->list_separator = separator;
718 }
719
720
721 /* Iterates through all the directories in *dirs trying to
722 * open file. When it successfully locates and opens a file it
723 * returns the file descriptor to the open file. It also
724 * outputs the absolute path of the file in output_file.
725 */
726 static gint
find_file_in_data_dirs(const gchar * file,const gchar ** dirs,gchar ** output_file,GError ** error)727 find_file_in_data_dirs (const gchar *file,
728 const gchar **dirs,
729 gchar **output_file,
730 GError **error)
731 {
732 const gchar **data_dirs, *data_dir;
733 gchar *path;
734 gint fd;
735
736 path = NULL;
737 fd = -1;
738
739 if (dirs == NULL)
740 return fd;
741
742 data_dirs = dirs;
743
744 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
745 {
746 gchar *candidate_file, *sub_dir;
747
748 candidate_file = (gchar *) file;
749 sub_dir = g_strdup ("");
750 while (candidate_file != NULL && fd == -1)
751 {
752 gchar *p;
753
754 path = g_build_filename (data_dir, sub_dir,
755 candidate_file, NULL);
756
757 fd = g_open (path, O_RDONLY, 0);
758
759 if (fd == -1)
760 {
761 g_free (path);
762 path = NULL;
763 }
764
765 candidate_file = strchr (candidate_file, '-');
766
767 if (candidate_file == NULL)
768 break;
769
770 candidate_file++;
771
772 g_free (sub_dir);
773 sub_dir = g_strndup (file, candidate_file - file - 1);
774
775 for (p = sub_dir; *p != '\0'; p++)
776 {
777 if (*p == '-')
778 *p = G_DIR_SEPARATOR;
779 }
780 }
781 g_free (sub_dir);
782 data_dirs++;
783 }
784
785 if (fd == -1)
786 {
787 g_set_error_literal (error, G_KEY_FILE_ERROR,
788 G_KEY_FILE_ERROR_NOT_FOUND,
789 _("Valid key file could not be "
790 "found in search dirs"));
791 }
792
793 if (output_file != NULL && fd != -1)
794 *output_file = g_strdup (path);
795
796 g_free (path);
797
798 return fd;
799 }
800
801 static gboolean
g_key_file_load_from_fd(GKeyFile * key_file,gint fd,GKeyFileFlags flags,GError ** error)802 g_key_file_load_from_fd (GKeyFile *key_file,
803 gint fd,
804 GKeyFileFlags flags,
805 GError **error)
806 {
807 GError *key_file_error = NULL;
808 gssize bytes_read;
809 struct stat stat_buf;
810 gchar read_buf[4096];
811 gchar list_separator;
812
813 if (fstat (fd, &stat_buf) < 0)
814 {
815 int errsv = errno;
816 g_set_error_literal (error, G_FILE_ERROR,
817 g_file_error_from_errno (errsv),
818 g_strerror (errsv));
819 return FALSE;
820 }
821
822 if (!S_ISREG (stat_buf.st_mode))
823 {
824 g_set_error_literal (error, G_KEY_FILE_ERROR,
825 G_KEY_FILE_ERROR_PARSE,
826 _("Not a regular file"));
827 return FALSE;
828 }
829
830 list_separator = key_file->list_separator;
831 g_key_file_clear (key_file);
832 g_key_file_init (key_file);
833 key_file->list_separator = list_separator;
834 key_file->flags = flags;
835
836 do
837 {
838 int errsv;
839
840 bytes_read = read (fd, read_buf, 4096);
841 errsv = errno;
842
843 if (bytes_read == 0) /* End of File */
844 break;
845
846 if (bytes_read < 0)
847 {
848 if (errsv == EINTR || errsv == EAGAIN)
849 continue;
850
851 g_set_error_literal (error, G_FILE_ERROR,
852 g_file_error_from_errno (errsv),
853 g_strerror (errsv));
854 return FALSE;
855 }
856
857 g_key_file_parse_data (key_file,
858 read_buf, bytes_read,
859 &key_file_error);
860 }
861 while (!key_file_error);
862
863 if (key_file_error)
864 {
865 g_propagate_error (error, key_file_error);
866 return FALSE;
867 }
868
869 g_key_file_flush_parse_buffer (key_file, &key_file_error);
870
871 if (key_file_error)
872 {
873 g_propagate_error (error, key_file_error);
874 return FALSE;
875 }
876
877 return TRUE;
878 }
879
880 /**
881 * g_key_file_load_from_file:
882 * @key_file: an empty #GKeyFile struct
883 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
884 * @flags: flags from #GKeyFileFlags
885 * @error: return location for a #GError, or %NULL
886 *
887 * Loads a key file into an empty #GKeyFile structure.
888 *
889 * If the OS returns an error when opening or reading the file, a
890 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
891 * %G_KEY_FILE_ERROR is returned.
892 *
893 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
894 * @file is not found, %G_FILE_ERROR_NOENT is returned.
895 *
896 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
897 *
898 * Since: 2.6
899 **/
900 gboolean
g_key_file_load_from_file(GKeyFile * key_file,const gchar * file,GKeyFileFlags flags,GError ** error)901 g_key_file_load_from_file (GKeyFile *key_file,
902 const gchar *file,
903 GKeyFileFlags flags,
904 GError **error)
905 {
906 GError *key_file_error = NULL;
907 gint fd;
908 int errsv;
909
910 g_return_val_if_fail (key_file != NULL, FALSE);
911 g_return_val_if_fail (file != NULL, FALSE);
912
913 fd = g_open (file, O_RDONLY, 0);
914 errsv = errno;
915
916 if (fd == -1)
917 {
918 g_set_error_literal (error, G_FILE_ERROR,
919 g_file_error_from_errno (errsv),
920 g_strerror (errsv));
921 return FALSE;
922 }
923
924 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
925 close (fd);
926
927 if (key_file_error)
928 {
929 g_propagate_error (error, key_file_error);
930 return FALSE;
931 }
932
933 return TRUE;
934 }
935
936 /**
937 * g_key_file_load_from_data:
938 * @key_file: an empty #GKeyFile struct
939 * @data: key file loaded in memory
940 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
941 * @flags: flags from #GKeyFileFlags
942 * @error: return location for a #GError, or %NULL
943 *
944 * Loads a key file from memory into an empty #GKeyFile structure.
945 * If the object cannot be created then %error is set to a #GKeyFileError.
946 *
947 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
948 *
949 * Since: 2.6
950 **/
951 gboolean
g_key_file_load_from_data(GKeyFile * key_file,const gchar * data,gsize length,GKeyFileFlags flags,GError ** error)952 g_key_file_load_from_data (GKeyFile *key_file,
953 const gchar *data,
954 gsize length,
955 GKeyFileFlags flags,
956 GError **error)
957 {
958 GError *key_file_error = NULL;
959 gchar list_separator;
960
961 g_return_val_if_fail (key_file != NULL, FALSE);
962 g_return_val_if_fail (data != NULL || length == 0, FALSE);
963
964 if (length == (gsize)-1)
965 length = strlen (data);
966
967 list_separator = key_file->list_separator;
968 g_key_file_clear (key_file);
969 g_key_file_init (key_file);
970 key_file->list_separator = list_separator;
971 key_file->flags = flags;
972
973 g_key_file_parse_data (key_file, data, length, &key_file_error);
974
975 if (key_file_error)
976 {
977 g_propagate_error (error, key_file_error);
978 return FALSE;
979 }
980
981 g_key_file_flush_parse_buffer (key_file, &key_file_error);
982
983 if (key_file_error)
984 {
985 g_propagate_error (error, key_file_error);
986 return FALSE;
987 }
988
989 return TRUE;
990 }
991
992 /**
993 * g_key_file_load_from_bytes:
994 * @key_file: an empty #GKeyFile struct
995 * @bytes: a #GBytes
996 * @flags: flags from #GKeyFileFlags
997 * @error: return location for a #GError, or %NULL
998 *
999 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
1000 * If the object cannot be created then %error is set to a #GKeyFileError.
1001 *
1002 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1003 *
1004 * Since: 2.50
1005 **/
1006 gboolean
g_key_file_load_from_bytes(GKeyFile * key_file,GBytes * bytes,GKeyFileFlags flags,GError ** error)1007 g_key_file_load_from_bytes (GKeyFile *key_file,
1008 GBytes *bytes,
1009 GKeyFileFlags flags,
1010 GError **error)
1011 {
1012 const guchar *data;
1013 gsize size;
1014
1015 g_return_val_if_fail (key_file != NULL, FALSE);
1016 g_return_val_if_fail (bytes != NULL, FALSE);
1017
1018 data = g_bytes_get_data (bytes, &size);
1019 return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
1020 }
1021
1022 /**
1023 * g_key_file_load_from_dirs:
1024 * @key_file: an empty #GKeyFile struct
1025 * @file: (type filename): a relative path to a filename to open and parse
1026 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
1027 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1028 * of the file, or %NULL
1029 * @flags: flags from #GKeyFileFlags
1030 * @error: return location for a #GError, or %NULL
1031 *
1032 * This function looks for a key file named @file in the paths
1033 * specified in @search_dirs, loads the file into @key_file and
1034 * returns the file's full path in @full_path.
1035 *
1036 * If the file could not be found in any of the @search_dirs,
1037 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
1038 * the file is found but the OS returns an error when opening or reading the
1039 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
1040 * %G_KEY_FILE_ERROR is returned.
1041 *
1042 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1043 *
1044 * Since: 2.14
1045 **/
1046 gboolean
g_key_file_load_from_dirs(GKeyFile * key_file,const gchar * file,const gchar ** search_dirs,gchar ** full_path,GKeyFileFlags flags,GError ** error)1047 g_key_file_load_from_dirs (GKeyFile *key_file,
1048 const gchar *file,
1049 const gchar **search_dirs,
1050 gchar **full_path,
1051 GKeyFileFlags flags,
1052 GError **error)
1053 {
1054 GError *key_file_error = NULL;
1055 const gchar **data_dirs;
1056 gchar *output_path;
1057 gint fd;
1058 gboolean found_file;
1059
1060 g_return_val_if_fail (key_file != NULL, FALSE);
1061 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1062 g_return_val_if_fail (search_dirs != NULL, FALSE);
1063
1064 found_file = FALSE;
1065 data_dirs = search_dirs;
1066 output_path = NULL;
1067 while (*data_dirs != NULL && !found_file)
1068 {
1069 g_free (output_path);
1070 output_path = NULL;
1071
1072 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
1073 &key_file_error);
1074
1075 if (fd == -1)
1076 {
1077 if (key_file_error)
1078 g_propagate_error (error, key_file_error);
1079 break;
1080 }
1081
1082 found_file = g_key_file_load_from_fd (key_file, fd, flags,
1083 &key_file_error);
1084 close (fd);
1085
1086 if (key_file_error)
1087 {
1088 g_propagate_error (error, key_file_error);
1089 break;
1090 }
1091 }
1092
1093 if (found_file && full_path)
1094 *full_path = output_path;
1095 else
1096 g_free (output_path);
1097
1098 return found_file;
1099 }
1100
1101 /**
1102 * g_key_file_load_from_data_dirs:
1103 * @key_file: an empty #GKeyFile struct
1104 * @file: (type filename): a relative path to a filename to open and parse
1105 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1106 * of the file, or %NULL
1107 * @flags: flags from #GKeyFileFlags
1108 * @error: return location for a #GError, or %NULL
1109 *
1110 * This function looks for a key file named @file in the paths
1111 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1112 * loads the file into @key_file and returns the file's full path in
1113 * @full_path. If the file could not be loaded then an %error is
1114 * set to either a #GFileError or #GKeyFileError.
1115 *
1116 * Returns: %TRUE if a key file could be loaded, %FALSE othewise
1117 * Since: 2.6
1118 **/
1119 gboolean
g_key_file_load_from_data_dirs(GKeyFile * key_file,const gchar * file,gchar ** full_path,GKeyFileFlags flags,GError ** error)1120 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1121 const gchar *file,
1122 gchar **full_path,
1123 GKeyFileFlags flags,
1124 GError **error)
1125 {
1126 gchar **all_data_dirs;
1127 const gchar * user_data_dir;
1128 const gchar * const * system_data_dirs;
1129 gsize i, j;
1130 gboolean found_file;
1131
1132 g_return_val_if_fail (key_file != NULL, FALSE);
1133 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1134
1135 user_data_dir = g_get_user_data_dir ();
1136 system_data_dirs = g_get_system_data_dirs ();
1137 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1138
1139 i = 0;
1140 all_data_dirs[i++] = g_strdup (user_data_dir);
1141
1142 j = 0;
1143 while (system_data_dirs[j] != NULL)
1144 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1145 all_data_dirs[i] = NULL;
1146
1147 found_file = g_key_file_load_from_dirs (key_file,
1148 file,
1149 (const gchar **)all_data_dirs,
1150 full_path,
1151 flags,
1152 error);
1153
1154 g_strfreev (all_data_dirs);
1155
1156 return found_file;
1157 }
1158
1159 /**
1160 * g_key_file_ref: (skip)
1161 * @key_file: a #GKeyFile
1162 *
1163 * Increases the reference count of @key_file.
1164 *
1165 * Returns: the same @key_file.
1166 *
1167 * Since: 2.32
1168 **/
1169 GKeyFile *
g_key_file_ref(GKeyFile * key_file)1170 g_key_file_ref (GKeyFile *key_file)
1171 {
1172 g_return_val_if_fail (key_file != NULL, NULL);
1173
1174 g_atomic_int_inc (&key_file->ref_count);
1175
1176 return key_file;
1177 }
1178
1179 /**
1180 * g_key_file_free: (skip)
1181 * @key_file: a #GKeyFile
1182 *
1183 * Clears all keys and groups from @key_file, and decreases the
1184 * reference count by 1. If the reference count reaches zero,
1185 * frees the key file and all its allocated memory.
1186 *
1187 * Since: 2.6
1188 **/
1189 void
g_key_file_free(GKeyFile * key_file)1190 g_key_file_free (GKeyFile *key_file)
1191 {
1192 g_return_if_fail (key_file != NULL);
1193
1194 g_key_file_clear (key_file);
1195
1196 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1197 g_slice_free (GKeyFile, key_file);
1198 else
1199 g_key_file_init (key_file);
1200 }
1201
1202 /**
1203 * g_key_file_unref:
1204 * @key_file: a #GKeyFile
1205 *
1206 * Decreases the reference count of @key_file by 1. If the reference count
1207 * reaches zero, frees the key file and all its allocated memory.
1208 *
1209 * Since: 2.32
1210 **/
1211 void
g_key_file_unref(GKeyFile * key_file)1212 g_key_file_unref (GKeyFile *key_file)
1213 {
1214 g_return_if_fail (key_file != NULL);
1215
1216 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1217 {
1218 g_key_file_clear (key_file);
1219 g_slice_free (GKeyFile, key_file);
1220 }
1221 }
1222
1223 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1224 * true for locales that match those in g_get_language_names().
1225 */
1226 static gboolean
g_key_file_locale_is_interesting(GKeyFile * key_file,const gchar * locale)1227 g_key_file_locale_is_interesting (GKeyFile *key_file,
1228 const gchar *locale)
1229 {
1230 gsize i;
1231
1232 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1233 return TRUE;
1234
1235 for (i = 0; key_file->locales[i] != NULL; i++)
1236 {
1237 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1238 return TRUE;
1239 }
1240
1241 return FALSE;
1242 }
1243
1244 static void
g_key_file_parse_line(GKeyFile * key_file,const gchar * line,gsize length,GError ** error)1245 g_key_file_parse_line (GKeyFile *key_file,
1246 const gchar *line,
1247 gsize length,
1248 GError **error)
1249 {
1250 GError *parse_error = NULL;
1251 gchar *line_start;
1252
1253 g_return_if_fail (key_file != NULL);
1254 g_return_if_fail (line != NULL);
1255
1256 line_start = (gchar *) line;
1257 while (g_ascii_isspace (*line_start))
1258 line_start++;
1259
1260 if (g_key_file_line_is_comment (line_start))
1261 g_key_file_parse_comment (key_file, line, length, &parse_error);
1262 else if (g_key_file_line_is_group (line_start))
1263 g_key_file_parse_group (key_file, line_start,
1264 length - (line_start - line),
1265 &parse_error);
1266 else if (g_key_file_line_is_key_value_pair (line_start))
1267 g_key_file_parse_key_value_pair (key_file, line_start,
1268 length - (line_start - line),
1269 &parse_error);
1270 else
1271 {
1272 gchar *line_utf8 = g_utf8_make_valid (line, length);
1273 g_set_error (error, G_KEY_FILE_ERROR,
1274 G_KEY_FILE_ERROR_PARSE,
1275 _("Key file contains line “%s” which is not "
1276 "a key-value pair, group, or comment"),
1277 line_utf8);
1278 g_free (line_utf8);
1279
1280 return;
1281 }
1282
1283 if (parse_error)
1284 g_propagate_error (error, parse_error);
1285 }
1286
1287 static void
g_key_file_parse_comment(GKeyFile * key_file,const gchar * line,gsize length,GError ** error)1288 g_key_file_parse_comment (GKeyFile *key_file,
1289 const gchar *line,
1290 gsize length,
1291 GError **error)
1292 {
1293 GKeyFileKeyValuePair *pair;
1294
1295 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1296 return;
1297
1298 g_warn_if_fail (key_file->current_group != NULL);
1299
1300 pair = g_slice_new (GKeyFileKeyValuePair);
1301 pair->key = NULL;
1302 pair->value = g_strndup (line, length);
1303
1304 key_file->current_group->key_value_pairs =
1305 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1306 }
1307
1308 static void
g_key_file_parse_group(GKeyFile * key_file,const gchar * line,gsize length,GError ** error)1309 g_key_file_parse_group (GKeyFile *key_file,
1310 const gchar *line,
1311 gsize length,
1312 GError **error)
1313 {
1314 gchar *group_name;
1315 const gchar *group_name_start, *group_name_end;
1316
1317 /* advance past opening '['
1318 */
1319 group_name_start = line + 1;
1320 group_name_end = line + length - 1;
1321
1322 while (*group_name_end != ']')
1323 group_name_end--;
1324
1325 group_name = g_strndup (group_name_start,
1326 group_name_end - group_name_start);
1327
1328 if (!g_key_file_is_group_name (group_name))
1329 {
1330 g_set_error (error, G_KEY_FILE_ERROR,
1331 G_KEY_FILE_ERROR_PARSE,
1332 _("Invalid group name: %s"), group_name);
1333 g_free (group_name);
1334 return;
1335 }
1336
1337 g_key_file_add_group (key_file, group_name);
1338 g_free (group_name);
1339 }
1340
1341 static void
g_key_file_parse_key_value_pair(GKeyFile * key_file,const gchar * line,gsize length,GError ** error)1342 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1343 const gchar *line,
1344 gsize length,
1345 GError **error)
1346 {
1347 gchar *key, *value, *key_end, *value_start, *locale;
1348 gsize key_len, value_len;
1349
1350 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1351 {
1352 g_set_error_literal (error, G_KEY_FILE_ERROR,
1353 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1354 _("Key file does not start with a group"));
1355 return;
1356 }
1357
1358 key_end = value_start = strchr (line, '=');
1359
1360 g_warn_if_fail (key_end != NULL);
1361
1362 key_end--;
1363 value_start++;
1364
1365 /* Pull the key name from the line (chomping trailing whitespace)
1366 */
1367 while (g_ascii_isspace (*key_end))
1368 key_end--;
1369
1370 key_len = key_end - line + 2;
1371
1372 g_warn_if_fail (key_len <= length);
1373
1374 key = g_strndup (line, key_len - 1);
1375
1376 if (!g_key_file_is_key_name (key))
1377 {
1378 g_set_error (error, G_KEY_FILE_ERROR,
1379 G_KEY_FILE_ERROR_PARSE,
1380 _("Invalid key name: %s"), key);
1381 g_free (key);
1382 return;
1383 }
1384
1385 /* Pull the value from the line (chugging leading whitespace)
1386 */
1387 while (g_ascii_isspace (*value_start))
1388 value_start++;
1389
1390 value_len = line + length - value_start + 1;
1391
1392 value = g_strndup (value_start, value_len);
1393
1394 g_warn_if_fail (key_file->start_group != NULL);
1395
1396 if (key_file->current_group
1397 && key_file->current_group->name
1398 && strcmp (key_file->start_group->name,
1399 key_file->current_group->name) == 0
1400 && strcmp (key, "Encoding") == 0)
1401 {
1402 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1403 {
1404 gchar *value_utf8 = g_utf8_make_valid (value, value_len);
1405 g_set_error (error, G_KEY_FILE_ERROR,
1406 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1407 _("Key file contains unsupported "
1408 "encoding “%s”"), value_utf8);
1409 g_free (value_utf8);
1410
1411 g_free (key);
1412 g_free (value);
1413 return;
1414 }
1415 }
1416
1417 /* Is this key a translation? If so, is it one that we care about?
1418 */
1419 locale = key_get_locale (key);
1420
1421 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1422 {
1423 GKeyFileKeyValuePair *pair;
1424
1425 pair = g_slice_new (GKeyFileKeyValuePair);
1426 pair->key = key;
1427 pair->value = value;
1428
1429 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1430 }
1431 else
1432 {
1433 g_free (key);
1434 g_free (value);
1435 }
1436
1437 g_free (locale);
1438 }
1439
1440 static gchar *
key_get_locale(const gchar * key)1441 key_get_locale (const gchar *key)
1442 {
1443 gchar *locale;
1444
1445 locale = g_strrstr (key, "[");
1446
1447 if (locale && strlen (locale) <= 2)
1448 locale = NULL;
1449
1450 if (locale)
1451 locale = g_strndup (locale + 1, strlen (locale) - 2);
1452
1453 return locale;
1454 }
1455
1456 static void
g_key_file_parse_data(GKeyFile * key_file,const gchar * data,gsize length,GError ** error)1457 g_key_file_parse_data (GKeyFile *key_file,
1458 const gchar *data,
1459 gsize length,
1460 GError **error)
1461 {
1462 GError *parse_error;
1463 gsize i;
1464
1465 g_return_if_fail (key_file != NULL);
1466 g_return_if_fail (data != NULL || length == 0);
1467
1468 parse_error = NULL;
1469
1470 i = 0;
1471 while (i < length)
1472 {
1473 if (data[i] == '\n')
1474 {
1475 if (key_file->parse_buffer->len > 0
1476 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1477 == '\r'))
1478 g_string_erase (key_file->parse_buffer,
1479 key_file->parse_buffer->len - 1,
1480 1);
1481
1482 /* When a newline is encountered flush the parse buffer so that the
1483 * line can be parsed. Note that completely blank lines won't show
1484 * up in the parse buffer, so they get parsed directly.
1485 */
1486 if (key_file->parse_buffer->len > 0)
1487 g_key_file_flush_parse_buffer (key_file, &parse_error);
1488 else
1489 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1490
1491 if (parse_error)
1492 {
1493 g_propagate_error (error, parse_error);
1494 return;
1495 }
1496 i++;
1497 }
1498 else
1499 {
1500 const gchar *start_of_line;
1501 const gchar *end_of_line;
1502 gsize line_length;
1503
1504 start_of_line = data + i;
1505 end_of_line = memchr (start_of_line, '\n', length - i);
1506
1507 if (end_of_line == NULL)
1508 end_of_line = data + length;
1509
1510 line_length = end_of_line - start_of_line;
1511
1512 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1513 i += line_length;
1514 }
1515 }
1516 }
1517
1518 static void
g_key_file_flush_parse_buffer(GKeyFile * key_file,GError ** error)1519 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1520 GError **error)
1521 {
1522 GError *file_error = NULL;
1523
1524 g_return_if_fail (key_file != NULL);
1525
1526 file_error = NULL;
1527
1528 if (key_file->parse_buffer->len > 0)
1529 {
1530 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1531 key_file->parse_buffer->len,
1532 &file_error);
1533 g_string_erase (key_file->parse_buffer, 0, -1);
1534
1535 if (file_error)
1536 {
1537 g_propagate_error (error, file_error);
1538 return;
1539 }
1540 }
1541 }
1542
1543 /**
1544 * g_key_file_to_data:
1545 * @key_file: a #GKeyFile
1546 * @length: (out) (optional): return location for the length of the
1547 * returned string, or %NULL
1548 * @error: return location for a #GError, or %NULL
1549 *
1550 * This function outputs @key_file as a string.
1551 *
1552 * Note that this function never reports an error,
1553 * so it is safe to pass %NULL as @error.
1554 *
1555 * Returns: a newly allocated string holding
1556 * the contents of the #GKeyFile
1557 *
1558 * Since: 2.6
1559 **/
1560 gchar *
g_key_file_to_data(GKeyFile * key_file,gsize * length,GError ** error)1561 g_key_file_to_data (GKeyFile *key_file,
1562 gsize *length,
1563 GError **error)
1564 {
1565 GString *data_string;
1566 GList *group_node, *key_file_node;
1567
1568 g_return_val_if_fail (key_file != NULL, NULL);
1569
1570 data_string = g_string_new (NULL);
1571
1572 for (group_node = g_list_last (key_file->groups);
1573 group_node != NULL;
1574 group_node = group_node->prev)
1575 {
1576 GKeyFileGroup *group;
1577
1578 group = (GKeyFileGroup *) group_node->data;
1579
1580 /* separate groups by at least an empty line */
1581 if (data_string->len >= 2 &&
1582 data_string->str[data_string->len - 2] != '\n')
1583 g_string_append_c (data_string, '\n');
1584
1585 if (group->comment != NULL)
1586 g_string_append_printf (data_string, "%s\n", group->comment->value);
1587
1588 if (group->name != NULL)
1589 g_string_append_printf (data_string, "[%s]\n", group->name);
1590
1591 for (key_file_node = g_list_last (group->key_value_pairs);
1592 key_file_node != NULL;
1593 key_file_node = key_file_node->prev)
1594 {
1595 GKeyFileKeyValuePair *pair;
1596
1597 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1598
1599 if (pair->key != NULL)
1600 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1601 else
1602 g_string_append_printf (data_string, "%s\n", pair->value);
1603 }
1604 }
1605
1606 if (length)
1607 *length = data_string->len;
1608
1609 return g_string_free (data_string, FALSE);
1610 }
1611
1612 /**
1613 * g_key_file_get_keys:
1614 * @key_file: a #GKeyFile
1615 * @group_name: a group name
1616 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1617 * @error: return location for a #GError, or %NULL
1618 *
1619 * Returns all keys for the group name @group_name. The array of
1620 * returned keys will be %NULL-terminated, so @length may
1621 * optionally be %NULL. In the event that the @group_name cannot
1622 * be found, %NULL is returned and @error is set to
1623 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1624 *
1625 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1626 * Use g_strfreev() to free it.
1627 *
1628 * Since: 2.6
1629 **/
1630 gchar **
g_key_file_get_keys(GKeyFile * key_file,const gchar * group_name,gsize * length,GError ** error)1631 g_key_file_get_keys (GKeyFile *key_file,
1632 const gchar *group_name,
1633 gsize *length,
1634 GError **error)
1635 {
1636 GKeyFileGroup *group;
1637 GList *tmp;
1638 gchar **keys;
1639 gsize i, num_keys;
1640
1641 g_return_val_if_fail (key_file != NULL, NULL);
1642 g_return_val_if_fail (group_name != NULL, NULL);
1643
1644 group = g_key_file_lookup_group (key_file, group_name);
1645
1646 if (!group)
1647 {
1648 g_set_error (error, G_KEY_FILE_ERROR,
1649 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1650 _("Key file does not have group “%s”"),
1651 group_name);
1652 return NULL;
1653 }
1654
1655 num_keys = 0;
1656 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1657 {
1658 GKeyFileKeyValuePair *pair;
1659
1660 pair = (GKeyFileKeyValuePair *) tmp->data;
1661
1662 if (pair->key)
1663 num_keys++;
1664 }
1665
1666 keys = g_new (gchar *, num_keys + 1);
1667
1668 i = num_keys - 1;
1669 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1670 {
1671 GKeyFileKeyValuePair *pair;
1672
1673 pair = (GKeyFileKeyValuePair *) tmp->data;
1674
1675 if (pair->key)
1676 {
1677 keys[i] = g_strdup (pair->key);
1678 i--;
1679 }
1680 }
1681
1682 keys[num_keys] = NULL;
1683
1684 if (length)
1685 *length = num_keys;
1686
1687 return keys;
1688 }
1689
1690 /**
1691 * g_key_file_get_start_group:
1692 * @key_file: a #GKeyFile
1693 *
1694 * Returns the name of the start group of the file.
1695 *
1696 * Returns: The start group of the key file.
1697 *
1698 * Since: 2.6
1699 **/
1700 gchar *
g_key_file_get_start_group(GKeyFile * key_file)1701 g_key_file_get_start_group (GKeyFile *key_file)
1702 {
1703 g_return_val_if_fail (key_file != NULL, NULL);
1704
1705 if (key_file->start_group)
1706 return g_strdup (key_file->start_group->name);
1707
1708 return NULL;
1709 }
1710
1711 /**
1712 * g_key_file_get_groups:
1713 * @key_file: a #GKeyFile
1714 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1715 *
1716 * Returns all groups in the key file loaded with @key_file.
1717 * The array of returned groups will be %NULL-terminated, so
1718 * @length may optionally be %NULL.
1719 *
1720 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1721 * Use g_strfreev() to free it.
1722 * Since: 2.6
1723 **/
1724 gchar **
g_key_file_get_groups(GKeyFile * key_file,gsize * length)1725 g_key_file_get_groups (GKeyFile *key_file,
1726 gsize *length)
1727 {
1728 GList *group_node;
1729 gchar **groups;
1730 gsize i, num_groups;
1731
1732 g_return_val_if_fail (key_file != NULL, NULL);
1733
1734 num_groups = g_list_length (key_file->groups);
1735
1736 g_return_val_if_fail (num_groups > 0, NULL);
1737
1738 group_node = g_list_last (key_file->groups);
1739
1740 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1741
1742 /* Only need num_groups instead of num_groups + 1
1743 * because the first group of the file (last in the
1744 * list) is always the comment group at the top,
1745 * which we skip
1746 */
1747 groups = g_new (gchar *, num_groups);
1748
1749
1750 i = 0;
1751 for (group_node = group_node->prev;
1752 group_node != NULL;
1753 group_node = group_node->prev)
1754 {
1755 GKeyFileGroup *group;
1756
1757 group = (GKeyFileGroup *) group_node->data;
1758
1759 g_warn_if_fail (group->name != NULL);
1760
1761 groups[i++] = g_strdup (group->name);
1762 }
1763 groups[i] = NULL;
1764
1765 if (length)
1766 *length = i;
1767
1768 return groups;
1769 }
1770
1771 static void
set_not_found_key_error(const char * group_name,const char * key,GError ** error)1772 set_not_found_key_error (const char *group_name,
1773 const char *key,
1774 GError **error)
1775 {
1776 g_set_error (error, G_KEY_FILE_ERROR,
1777 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1778 _("Key file does not have key “%s” in group “%s”"),
1779 key, group_name);
1780 }
1781
1782 /**
1783 * g_key_file_get_value:
1784 * @key_file: a #GKeyFile
1785 * @group_name: a group name
1786 * @key: a key
1787 * @error: return location for a #GError, or %NULL
1788 *
1789 * Returns the raw value associated with @key under @group_name.
1790 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1791 *
1792 * In the event the key cannot be found, %NULL is returned and
1793 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1794 * event that the @group_name cannot be found, %NULL is returned
1795 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1796 *
1797 *
1798 * Returns: a newly allocated string or %NULL if the specified
1799 * key cannot be found.
1800 *
1801 * Since: 2.6
1802 **/
1803 gchar *
g_key_file_get_value(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)1804 g_key_file_get_value (GKeyFile *key_file,
1805 const gchar *group_name,
1806 const gchar *key,
1807 GError **error)
1808 {
1809 GKeyFileGroup *group;
1810 GKeyFileKeyValuePair *pair;
1811 gchar *value = NULL;
1812
1813 g_return_val_if_fail (key_file != NULL, NULL);
1814 g_return_val_if_fail (group_name != NULL, NULL);
1815 g_return_val_if_fail (key != NULL, NULL);
1816
1817 group = g_key_file_lookup_group (key_file, group_name);
1818
1819 if (!group)
1820 {
1821 g_set_error (error, G_KEY_FILE_ERROR,
1822 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1823 _("Key file does not have group “%s”"),
1824 group_name);
1825 return NULL;
1826 }
1827
1828 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1829
1830 if (pair)
1831 value = g_strdup (pair->value);
1832 else
1833 set_not_found_key_error (group_name, key, error);
1834
1835 return value;
1836 }
1837
1838 /**
1839 * g_key_file_set_value:
1840 * @key_file: a #GKeyFile
1841 * @group_name: a group name
1842 * @key: a key
1843 * @value: a string
1844 *
1845 * Associates a new value with @key under @group_name.
1846 *
1847 * If @key cannot be found then it is created. If @group_name cannot
1848 * be found then it is created. To set an UTF-8 string which may contain
1849 * characters that need escaping (such as newlines or spaces), use
1850 * g_key_file_set_string().
1851 *
1852 * Since: 2.6
1853 **/
1854 void
g_key_file_set_value(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * value)1855 g_key_file_set_value (GKeyFile *key_file,
1856 const gchar *group_name,
1857 const gchar *key,
1858 const gchar *value)
1859 {
1860 GKeyFileGroup *group;
1861 GKeyFileKeyValuePair *pair;
1862
1863 g_return_if_fail (key_file != NULL);
1864 g_return_if_fail (g_key_file_is_group_name (group_name));
1865 g_return_if_fail (g_key_file_is_key_name (key));
1866 g_return_if_fail (value != NULL);
1867
1868 group = g_key_file_lookup_group (key_file, group_name);
1869
1870 if (!group)
1871 {
1872 g_key_file_add_group (key_file, group_name);
1873 group = (GKeyFileGroup *) key_file->groups->data;
1874
1875 g_key_file_add_key (key_file, group, key, value);
1876 }
1877 else
1878 {
1879 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1880
1881 if (!pair)
1882 g_key_file_add_key (key_file, group, key, value);
1883 else
1884 {
1885 g_free (pair->value);
1886 pair->value = g_strdup (value);
1887 }
1888 }
1889 }
1890
1891 /**
1892 * g_key_file_get_string:
1893 * @key_file: a #GKeyFile
1894 * @group_name: a group name
1895 * @key: a key
1896 * @error: return location for a #GError, or %NULL
1897 *
1898 * Returns the string value associated with @key under @group_name.
1899 * Unlike g_key_file_get_value(), this function handles escape sequences
1900 * like \s.
1901 *
1902 * In the event the key cannot be found, %NULL is returned and
1903 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1904 * event that the @group_name cannot be found, %NULL is returned
1905 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1906 *
1907 * Returns: a newly allocated string or %NULL if the specified
1908 * key cannot be found.
1909 *
1910 * Since: 2.6
1911 **/
1912 gchar *
g_key_file_get_string(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)1913 g_key_file_get_string (GKeyFile *key_file,
1914 const gchar *group_name,
1915 const gchar *key,
1916 GError **error)
1917 {
1918 gchar *value, *string_value;
1919 GError *key_file_error;
1920
1921 g_return_val_if_fail (key_file != NULL, NULL);
1922 g_return_val_if_fail (group_name != NULL, NULL);
1923 g_return_val_if_fail (key != NULL, NULL);
1924
1925 key_file_error = NULL;
1926
1927 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1928
1929 if (key_file_error)
1930 {
1931 g_propagate_error (error, key_file_error);
1932 return NULL;
1933 }
1934
1935 if (!g_utf8_validate (value, -1, NULL))
1936 {
1937 gchar *value_utf8 = g_utf8_make_valid (value, -1);
1938 g_set_error (error, G_KEY_FILE_ERROR,
1939 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1940 _("Key file contains key “%s” with value “%s” "
1941 "which is not UTF-8"), key, value_utf8);
1942 g_free (value_utf8);
1943 g_free (value);
1944
1945 return NULL;
1946 }
1947
1948 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1949 &key_file_error);
1950 g_free (value);
1951
1952 if (key_file_error)
1953 {
1954 if (g_error_matches (key_file_error,
1955 G_KEY_FILE_ERROR,
1956 G_KEY_FILE_ERROR_INVALID_VALUE))
1957 {
1958 g_set_error (error, G_KEY_FILE_ERROR,
1959 G_KEY_FILE_ERROR_INVALID_VALUE,
1960 _("Key file contains key “%s” "
1961 "which has a value that cannot be interpreted."),
1962 key);
1963 g_error_free (key_file_error);
1964 }
1965 else
1966 g_propagate_error (error, key_file_error);
1967 }
1968
1969 return string_value;
1970 }
1971
1972 /**
1973 * g_key_file_set_string:
1974 * @key_file: a #GKeyFile
1975 * @group_name: a group name
1976 * @key: a key
1977 * @string: a string
1978 *
1979 * Associates a new string value with @key under @group_name.
1980 * If @key cannot be found then it is created.
1981 * If @group_name cannot be found then it is created.
1982 * Unlike g_key_file_set_value(), this function handles characters
1983 * that need escaping, such as newlines.
1984 *
1985 * Since: 2.6
1986 **/
1987 void
g_key_file_set_string(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * string)1988 g_key_file_set_string (GKeyFile *key_file,
1989 const gchar *group_name,
1990 const gchar *key,
1991 const gchar *string)
1992 {
1993 gchar *value;
1994
1995 g_return_if_fail (key_file != NULL);
1996 g_return_if_fail (string != NULL);
1997
1998 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1999 g_key_file_set_value (key_file, group_name, key, value);
2000 g_free (value);
2001 }
2002
2003 /**
2004 * g_key_file_get_string_list:
2005 * @key_file: a #GKeyFile
2006 * @group_name: a group name
2007 * @key: a key
2008 * @length: (out) (optional): return location for the number of returned strings, or %NULL
2009 * @error: return location for a #GError, or %NULL
2010 *
2011 * Returns the values associated with @key under @group_name.
2012 *
2013 * In the event the key cannot be found, %NULL is returned and
2014 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
2015 * event that the @group_name cannot be found, %NULL is returned
2016 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
2017 *
2018 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2019 * a %NULL-terminated string array or %NULL if the specified
2020 * key cannot be found. The array should be freed with g_strfreev().
2021 *
2022 * Since: 2.6
2023 **/
2024 gchar **
g_key_file_get_string_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,gsize * length,GError ** error)2025 g_key_file_get_string_list (GKeyFile *key_file,
2026 const gchar *group_name,
2027 const gchar *key,
2028 gsize *length,
2029 GError **error)
2030 {
2031 GError *key_file_error = NULL;
2032 gchar *value, *string_value, **values;
2033 gint i, len;
2034 GSList *p, *pieces = NULL;
2035
2036 g_return_val_if_fail (key_file != NULL, NULL);
2037 g_return_val_if_fail (group_name != NULL, NULL);
2038 g_return_val_if_fail (key != NULL, NULL);
2039
2040 if (length)
2041 *length = 0;
2042
2043 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2044
2045 if (key_file_error)
2046 {
2047 g_propagate_error (error, key_file_error);
2048 return NULL;
2049 }
2050
2051 if (!g_utf8_validate (value, -1, NULL))
2052 {
2053 gchar *value_utf8 = g_utf8_make_valid (value, -1);
2054 g_set_error (error, G_KEY_FILE_ERROR,
2055 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
2056 _("Key file contains key “%s” with value “%s” "
2057 "which is not UTF-8"), key, value_utf8);
2058 g_free (value_utf8);
2059 g_free (value);
2060
2061 return NULL;
2062 }
2063
2064 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
2065 g_free (value);
2066 g_free (string_value);
2067
2068 if (key_file_error)
2069 {
2070 if (g_error_matches (key_file_error,
2071 G_KEY_FILE_ERROR,
2072 G_KEY_FILE_ERROR_INVALID_VALUE))
2073 {
2074 g_set_error (error, G_KEY_FILE_ERROR,
2075 G_KEY_FILE_ERROR_INVALID_VALUE,
2076 _("Key file contains key “%s” "
2077 "which has a value that cannot be interpreted."),
2078 key);
2079 g_error_free (key_file_error);
2080 }
2081 else
2082 g_propagate_error (error, key_file_error);
2083
2084 g_slist_free_full (pieces, g_free);
2085 return NULL;
2086 }
2087
2088 len = g_slist_length (pieces);
2089 values = g_new (gchar *, len + 1);
2090 for (p = pieces, i = 0; p; p = p->next)
2091 values[i++] = p->data;
2092 values[len] = NULL;
2093
2094 g_slist_free (pieces);
2095
2096 if (length)
2097 *length = len;
2098
2099 return values;
2100 }
2101
2102 /**
2103 * g_key_file_set_string_list:
2104 * @key_file: a #GKeyFile
2105 * @group_name: a group name
2106 * @key: a key
2107 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2108 * @length: number of string values in @list
2109 *
2110 * Associates a list of string values for @key under @group_name.
2111 * If @key cannot be found then it is created.
2112 * If @group_name cannot be found then it is created.
2113 *
2114 * Since: 2.6
2115 **/
2116 void
g_key_file_set_string_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * const list[],gsize length)2117 g_key_file_set_string_list (GKeyFile *key_file,
2118 const gchar *group_name,
2119 const gchar *key,
2120 const gchar * const list[],
2121 gsize length)
2122 {
2123 GString *value_list;
2124 gsize i;
2125
2126 g_return_if_fail (key_file != NULL);
2127 g_return_if_fail (list != NULL || length == 0);
2128
2129 value_list = g_string_sized_new (length * 128);
2130 for (i = 0; i < length && list[i] != NULL; i++)
2131 {
2132 gchar *value;
2133
2134 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2135 g_string_append (value_list, value);
2136 g_string_append_c (value_list, key_file->list_separator);
2137
2138 g_free (value);
2139 }
2140
2141 g_key_file_set_value (key_file, group_name, key, value_list->str);
2142 g_string_free (value_list, TRUE);
2143 }
2144
2145 /**
2146 * g_key_file_set_locale_string:
2147 * @key_file: a #GKeyFile
2148 * @group_name: a group name
2149 * @key: a key
2150 * @locale: a locale identifier
2151 * @string: a string
2152 *
2153 * Associates a string value for @key and @locale under @group_name.
2154 * If the translation for @key cannot be found then it is created.
2155 *
2156 * Since: 2.6
2157 **/
2158 void
g_key_file_set_locale_string(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * locale,const gchar * string)2159 g_key_file_set_locale_string (GKeyFile *key_file,
2160 const gchar *group_name,
2161 const gchar *key,
2162 const gchar *locale,
2163 const gchar *string)
2164 {
2165 gchar *full_key, *value;
2166
2167 g_return_if_fail (key_file != NULL);
2168 g_return_if_fail (key != NULL);
2169 g_return_if_fail (locale != NULL);
2170 g_return_if_fail (string != NULL);
2171
2172 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2173 full_key = g_strdup_printf ("%s[%s]", key, locale);
2174 g_key_file_set_value (key_file, group_name, full_key, value);
2175 g_free (full_key);
2176 g_free (value);
2177 }
2178
2179 /**
2180 * g_key_file_get_locale_string:
2181 * @key_file: a #GKeyFile
2182 * @group_name: a group name
2183 * @key: a key
2184 * @locale: (nullable): a locale identifier or %NULL
2185 * @error: return location for a #GError, or %NULL
2186 *
2187 * Returns the value associated with @key under @group_name
2188 * translated in the given @locale if available. If @locale is
2189 * %NULL then the current locale is assumed.
2190 *
2191 * If @locale is to be non-%NULL, or if the current locale will change over
2192 * the lifetime of the #GKeyFile, it must be loaded with
2193 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2194 *
2195 * If @key cannot be found then %NULL is returned and @error is set
2196 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2197 * with @key cannot be interpreted or no suitable translation can
2198 * be found then the untranslated value is returned.
2199 *
2200 * Returns: a newly allocated string or %NULL if the specified
2201 * key cannot be found.
2202 *
2203 * Since: 2.6
2204 **/
2205 gchar *
g_key_file_get_locale_string(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * locale,GError ** error)2206 g_key_file_get_locale_string (GKeyFile *key_file,
2207 const gchar *group_name,
2208 const gchar *key,
2209 const gchar *locale,
2210 GError **error)
2211 {
2212 gchar *candidate_key, *translated_value;
2213 GError *key_file_error;
2214 gchar **languages;
2215 gboolean free_languages = FALSE;
2216 gint i;
2217
2218 g_return_val_if_fail (key_file != NULL, NULL);
2219 g_return_val_if_fail (group_name != NULL, NULL);
2220 g_return_val_if_fail (key != NULL, NULL);
2221
2222 candidate_key = NULL;
2223 translated_value = NULL;
2224 key_file_error = NULL;
2225
2226 if (locale)
2227 {
2228 languages = g_get_locale_variants (locale);
2229 free_languages = TRUE;
2230 }
2231 else
2232 {
2233 languages = (gchar **) g_get_language_names ();
2234 free_languages = FALSE;
2235 }
2236
2237 for (i = 0; languages[i]; i++)
2238 {
2239 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2240
2241 translated_value = g_key_file_get_string (key_file,
2242 group_name,
2243 candidate_key, NULL);
2244 g_free (candidate_key);
2245
2246 if (translated_value)
2247 break;
2248
2249 g_free (translated_value);
2250 translated_value = NULL;
2251 }
2252
2253 /* Fallback to untranslated key
2254 */
2255 if (!translated_value)
2256 {
2257 translated_value = g_key_file_get_string (key_file, group_name, key,
2258 &key_file_error);
2259
2260 if (!translated_value)
2261 g_propagate_error (error, key_file_error);
2262 }
2263
2264 if (free_languages)
2265 g_strfreev (languages);
2266
2267 return translated_value;
2268 }
2269
2270 /**
2271 * g_key_file_get_locale_for_key:
2272 * @key_file: a #GKeyFile
2273 * @group_name: a group name
2274 * @key: a key
2275 * @locale: (nullable): a locale identifier or %NULL
2276 *
2277 * Returns the actual locale which the result of
2278 * g_key_file_get_locale_string() or g_key_file_get_locale_string_list()
2279 * came from.
2280 *
2281 * If calling g_key_file_get_locale_string() or
2282 * g_key_file_get_locale_string_list() with exactly the same @key_file,
2283 * @group_name, @key and @locale, the result of those functions will
2284 * have originally been tagged with the locale that is the result of
2285 * this function.
2286 *
2287 * Returns: (nullable): the locale from the file, or %NULL if the key was not
2288 * found or the entry in the file was was untranslated
2289 *
2290 * Since: 2.56
2291 */
2292 gchar *
g_key_file_get_locale_for_key(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * locale)2293 g_key_file_get_locale_for_key (GKeyFile *key_file,
2294 const gchar *group_name,
2295 const gchar *key,
2296 const gchar *locale)
2297 {
2298 gchar **languages_allocated = NULL;
2299 const gchar * const *languages;
2300 gchar *result = NULL;
2301 gsize i;
2302
2303 g_return_val_if_fail (key_file != NULL, NULL);
2304 g_return_val_if_fail (group_name != NULL, NULL);
2305 g_return_val_if_fail (key != NULL, NULL);
2306
2307 if (locale != NULL)
2308 {
2309 languages_allocated = g_get_locale_variants (locale);
2310 languages = (const gchar * const *) languages_allocated;
2311 }
2312 else
2313 languages = g_get_language_names ();
2314
2315 for (i = 0; languages[i] != NULL; i++)
2316 {
2317 gchar *candidate_key, *translated_value;
2318
2319 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2320 translated_value = g_key_file_get_string (key_file, group_name, candidate_key, NULL);
2321 g_free (translated_value);
2322 g_free (candidate_key);
2323
2324 if (translated_value != NULL)
2325 break;
2326 }
2327
2328 result = g_strdup (languages[i]);
2329
2330 g_strfreev (languages_allocated);
2331
2332 return result;
2333 }
2334
2335 /**
2336 * g_key_file_get_locale_string_list:
2337 * @key_file: a #GKeyFile
2338 * @group_name: a group name
2339 * @key: a key
2340 * @locale: (nullable): a locale identifier or %NULL
2341 * @length: (out) (optional): return location for the number of returned strings or %NULL
2342 * @error: return location for a #GError or %NULL
2343 *
2344 * Returns the values associated with @key under @group_name
2345 * translated in the given @locale if available. If @locale is
2346 * %NULL then the current locale is assumed.
2347 *
2348 * If @locale is to be non-%NULL, or if the current locale will change over
2349 * the lifetime of the #GKeyFile, it must be loaded with
2350 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2351 *
2352 * If @key cannot be found then %NULL is returned and @error is set
2353 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2354 * with @key cannot be interpreted or no suitable translations
2355 * can be found then the untranslated values are returned. The
2356 * returned array is %NULL-terminated, so @length may optionally
2357 * be %NULL.
2358 *
2359 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2360 * or %NULL if the key isn't found. The string array should be freed
2361 * with g_strfreev().
2362 *
2363 * Since: 2.6
2364 **/
2365 gchar **
g_key_file_get_locale_string_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * locale,gsize * length,GError ** error)2366 g_key_file_get_locale_string_list (GKeyFile *key_file,
2367 const gchar *group_name,
2368 const gchar *key,
2369 const gchar *locale,
2370 gsize *length,
2371 GError **error)
2372 {
2373 GError *key_file_error;
2374 gchar **values, *value;
2375 char list_separator[2];
2376 gsize len;
2377
2378 g_return_val_if_fail (key_file != NULL, NULL);
2379 g_return_val_if_fail (group_name != NULL, NULL);
2380 g_return_val_if_fail (key != NULL, NULL);
2381
2382 key_file_error = NULL;
2383
2384 value = g_key_file_get_locale_string (key_file, group_name,
2385 key, locale,
2386 &key_file_error);
2387
2388 if (key_file_error)
2389 g_propagate_error (error, key_file_error);
2390
2391 if (!value)
2392 {
2393 if (length)
2394 *length = 0;
2395 return NULL;
2396 }
2397
2398 len = strlen (value);
2399 if (value[len - 1] == key_file->list_separator)
2400 value[len - 1] = '\0';
2401
2402 list_separator[0] = key_file->list_separator;
2403 list_separator[1] = '\0';
2404 values = g_strsplit (value, list_separator, 0);
2405
2406 g_free (value);
2407
2408 if (length)
2409 *length = g_strv_length (values);
2410
2411 return values;
2412 }
2413
2414 /**
2415 * g_key_file_set_locale_string_list:
2416 * @key_file: a #GKeyFile
2417 * @group_name: a group name
2418 * @key: a key
2419 * @locale: a locale identifier
2420 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2421 * @length: the length of @list
2422 *
2423 * Associates a list of string values for @key and @locale under
2424 * @group_name. If the translation for @key cannot be found then
2425 * it is created.
2426 *
2427 * Since: 2.6
2428 **/
2429 void
g_key_file_set_locale_string_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * locale,const gchar * const list[],gsize length)2430 g_key_file_set_locale_string_list (GKeyFile *key_file,
2431 const gchar *group_name,
2432 const gchar *key,
2433 const gchar *locale,
2434 const gchar * const list[],
2435 gsize length)
2436 {
2437 GString *value_list;
2438 gchar *full_key;
2439 gsize i;
2440
2441 g_return_if_fail (key_file != NULL);
2442 g_return_if_fail (key != NULL);
2443 g_return_if_fail (locale != NULL);
2444 g_return_if_fail (length != 0);
2445
2446 value_list = g_string_sized_new (length * 128);
2447 for (i = 0; i < length && list[i] != NULL; i++)
2448 {
2449 gchar *value;
2450
2451 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2452 g_string_append (value_list, value);
2453 g_string_append_c (value_list, key_file->list_separator);
2454
2455 g_free (value);
2456 }
2457
2458 full_key = g_strdup_printf ("%s[%s]", key, locale);
2459 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2460 g_free (full_key);
2461 g_string_free (value_list, TRUE);
2462 }
2463
2464 /**
2465 * g_key_file_get_boolean:
2466 * @key_file: a #GKeyFile
2467 * @group_name: a group name
2468 * @key: a key
2469 * @error: return location for a #GError
2470 *
2471 * Returns the value associated with @key under @group_name as a
2472 * boolean.
2473 *
2474 * If @key cannot be found then %FALSE is returned and @error is set
2475 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2476 * associated with @key cannot be interpreted as a boolean then %FALSE
2477 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2478 *
2479 * Returns: the value associated with the key as a boolean,
2480 * or %FALSE if the key was not found or could not be parsed.
2481 *
2482 * Since: 2.6
2483 **/
2484 gboolean
g_key_file_get_boolean(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)2485 g_key_file_get_boolean (GKeyFile *key_file,
2486 const gchar *group_name,
2487 const gchar *key,
2488 GError **error)
2489 {
2490 GError *key_file_error = NULL;
2491 gchar *value;
2492 gboolean bool_value;
2493
2494 g_return_val_if_fail (key_file != NULL, FALSE);
2495 g_return_val_if_fail (group_name != NULL, FALSE);
2496 g_return_val_if_fail (key != NULL, FALSE);
2497
2498 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2499
2500 if (!value)
2501 {
2502 g_propagate_error (error, key_file_error);
2503 return FALSE;
2504 }
2505
2506 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2507 &key_file_error);
2508 g_free (value);
2509
2510 if (key_file_error)
2511 {
2512 if (g_error_matches (key_file_error,
2513 G_KEY_FILE_ERROR,
2514 G_KEY_FILE_ERROR_INVALID_VALUE))
2515 {
2516 g_set_error (error, G_KEY_FILE_ERROR,
2517 G_KEY_FILE_ERROR_INVALID_VALUE,
2518 _("Key file contains key “%s” "
2519 "which has a value that cannot be interpreted."),
2520 key);
2521 g_error_free (key_file_error);
2522 }
2523 else
2524 g_propagate_error (error, key_file_error);
2525 }
2526
2527 return bool_value;
2528 }
2529
2530 /**
2531 * g_key_file_set_boolean:
2532 * @key_file: a #GKeyFile
2533 * @group_name: a group name
2534 * @key: a key
2535 * @value: %TRUE or %FALSE
2536 *
2537 * Associates a new boolean value with @key under @group_name.
2538 * If @key cannot be found then it is created.
2539 *
2540 * Since: 2.6
2541 **/
2542 void
g_key_file_set_boolean(GKeyFile * key_file,const gchar * group_name,const gchar * key,gboolean value)2543 g_key_file_set_boolean (GKeyFile *key_file,
2544 const gchar *group_name,
2545 const gchar *key,
2546 gboolean value)
2547 {
2548 gchar *result;
2549
2550 g_return_if_fail (key_file != NULL);
2551
2552 result = g_key_file_parse_boolean_as_value (key_file, value);
2553 g_key_file_set_value (key_file, group_name, key, result);
2554 g_free (result);
2555 }
2556
2557 /**
2558 * g_key_file_get_boolean_list:
2559 * @key_file: a #GKeyFile
2560 * @group_name: a group name
2561 * @key: a key
2562 * @length: (out): the number of booleans returned
2563 * @error: return location for a #GError
2564 *
2565 * Returns the values associated with @key under @group_name as
2566 * booleans.
2567 *
2568 * If @key cannot be found then %NULL is returned and @error is set to
2569 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2570 * with @key cannot be interpreted as booleans then %NULL is returned
2571 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2572 *
2573 * Returns: (array length=length) (element-type gboolean) (transfer container):
2574 * the values associated with the key as a list of booleans, or %NULL if the
2575 * key was not found or could not be parsed. The returned list of booleans
2576 * should be freed with g_free() when no longer needed.
2577 *
2578 * Since: 2.6
2579 **/
2580 gboolean *
g_key_file_get_boolean_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,gsize * length,GError ** error)2581 g_key_file_get_boolean_list (GKeyFile *key_file,
2582 const gchar *group_name,
2583 const gchar *key,
2584 gsize *length,
2585 GError **error)
2586 {
2587 GError *key_file_error;
2588 gchar **values;
2589 gboolean *bool_values;
2590 gsize i, num_bools;
2591
2592 g_return_val_if_fail (key_file != NULL, NULL);
2593 g_return_val_if_fail (group_name != NULL, NULL);
2594 g_return_val_if_fail (key != NULL, NULL);
2595
2596 if (length)
2597 *length = 0;
2598
2599 key_file_error = NULL;
2600
2601 values = g_key_file_get_string_list (key_file, group_name, key,
2602 &num_bools, &key_file_error);
2603
2604 if (key_file_error)
2605 g_propagate_error (error, key_file_error);
2606
2607 if (!values)
2608 return NULL;
2609
2610 bool_values = g_new (gboolean, num_bools);
2611
2612 for (i = 0; i < num_bools; i++)
2613 {
2614 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2615 values[i],
2616 &key_file_error);
2617
2618 if (key_file_error)
2619 {
2620 g_propagate_error (error, key_file_error);
2621 g_strfreev (values);
2622 g_free (bool_values);
2623
2624 return NULL;
2625 }
2626 }
2627 g_strfreev (values);
2628
2629 if (length)
2630 *length = num_bools;
2631
2632 return bool_values;
2633 }
2634
2635 /**
2636 * g_key_file_set_boolean_list:
2637 * @key_file: a #GKeyFile
2638 * @group_name: a group name
2639 * @key: a key
2640 * @list: (array length=length): an array of boolean values
2641 * @length: length of @list
2642 *
2643 * Associates a list of boolean values with @key under @group_name.
2644 * If @key cannot be found then it is created.
2645 * If @group_name is %NULL, the start_group is used.
2646 *
2647 * Since: 2.6
2648 **/
2649 void
g_key_file_set_boolean_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,gboolean list[],gsize length)2650 g_key_file_set_boolean_list (GKeyFile *key_file,
2651 const gchar *group_name,
2652 const gchar *key,
2653 gboolean list[],
2654 gsize length)
2655 {
2656 GString *value_list;
2657 gsize i;
2658
2659 g_return_if_fail (key_file != NULL);
2660 g_return_if_fail (list != NULL);
2661
2662 value_list = g_string_sized_new (length * 8);
2663 for (i = 0; i < length; i++)
2664 {
2665 gchar *value;
2666
2667 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2668
2669 g_string_append (value_list, value);
2670 g_string_append_c (value_list, key_file->list_separator);
2671
2672 g_free (value);
2673 }
2674
2675 g_key_file_set_value (key_file, group_name, key, value_list->str);
2676 g_string_free (value_list, TRUE);
2677 }
2678
2679 /**
2680 * g_key_file_get_integer:
2681 * @key_file: a #GKeyFile
2682 * @group_name: a group name
2683 * @key: a key
2684 * @error: return location for a #GError
2685 *
2686 * Returns the value associated with @key under @group_name as an
2687 * integer.
2688 *
2689 * If @key cannot be found then 0 is returned and @error is set to
2690 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2691 * with @key cannot be interpreted as an integer, or is out of range
2692 * for a #gint, then 0 is returned
2693 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2694 *
2695 * Returns: the value associated with the key as an integer, or
2696 * 0 if the key was not found or could not be parsed.
2697 *
2698 * Since: 2.6
2699 **/
2700 gint
g_key_file_get_integer(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)2701 g_key_file_get_integer (GKeyFile *key_file,
2702 const gchar *group_name,
2703 const gchar *key,
2704 GError **error)
2705 {
2706 GError *key_file_error;
2707 gchar *value;
2708 gint int_value;
2709
2710 g_return_val_if_fail (key_file != NULL, -1);
2711 g_return_val_if_fail (group_name != NULL, -1);
2712 g_return_val_if_fail (key != NULL, -1);
2713
2714 key_file_error = NULL;
2715
2716 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2717
2718 if (key_file_error)
2719 {
2720 g_propagate_error (error, key_file_error);
2721 return 0;
2722 }
2723
2724 int_value = g_key_file_parse_value_as_integer (key_file, value,
2725 &key_file_error);
2726 g_free (value);
2727
2728 if (key_file_error)
2729 {
2730 if (g_error_matches (key_file_error,
2731 G_KEY_FILE_ERROR,
2732 G_KEY_FILE_ERROR_INVALID_VALUE))
2733 {
2734 g_set_error (error, G_KEY_FILE_ERROR,
2735 G_KEY_FILE_ERROR_INVALID_VALUE,
2736 _("Key file contains key “%s” in group “%s” "
2737 "which has a value that cannot be interpreted."),
2738 key, group_name);
2739 g_error_free (key_file_error);
2740 }
2741 else
2742 g_propagate_error (error, key_file_error);
2743 }
2744
2745 return int_value;
2746 }
2747
2748 /**
2749 * g_key_file_set_integer:
2750 * @key_file: a #GKeyFile
2751 * @group_name: a group name
2752 * @key: a key
2753 * @value: an integer value
2754 *
2755 * Associates a new integer value with @key under @group_name.
2756 * If @key cannot be found then it is created.
2757 *
2758 * Since: 2.6
2759 **/
2760 void
g_key_file_set_integer(GKeyFile * key_file,const gchar * group_name,const gchar * key,gint value)2761 g_key_file_set_integer (GKeyFile *key_file,
2762 const gchar *group_name,
2763 const gchar *key,
2764 gint value)
2765 {
2766 gchar *result;
2767
2768 g_return_if_fail (key_file != NULL);
2769
2770 result = g_key_file_parse_integer_as_value (key_file, value);
2771 g_key_file_set_value (key_file, group_name, key, result);
2772 g_free (result);
2773 }
2774
2775 /**
2776 * g_key_file_get_int64:
2777 * @key_file: a non-%NULL #GKeyFile
2778 * @group_name: a non-%NULL group name
2779 * @key: a non-%NULL key
2780 * @error: return location for a #GError
2781 *
2782 * Returns the value associated with @key under @group_name as a signed
2783 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2784 * 64-bit results without truncation.
2785 *
2786 * Returns: the value associated with the key as a signed 64-bit integer, or
2787 * 0 if the key was not found or could not be parsed.
2788 *
2789 * Since: 2.26
2790 */
2791 gint64
g_key_file_get_int64(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)2792 g_key_file_get_int64 (GKeyFile *key_file,
2793 const gchar *group_name,
2794 const gchar *key,
2795 GError **error)
2796 {
2797 gchar *s, *end;
2798 gint64 v;
2799
2800 g_return_val_if_fail (key_file != NULL, -1);
2801 g_return_val_if_fail (group_name != NULL, -1);
2802 g_return_val_if_fail (key != NULL, -1);
2803
2804 s = g_key_file_get_value (key_file, group_name, key, error);
2805
2806 if (s == NULL)
2807 return 0;
2808
2809 v = g_ascii_strtoll (s, &end, 10);
2810
2811 if (*s == '\0' || *end != '\0')
2812 {
2813 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2814 _("Key “%s” in group “%s” has value “%s” "
2815 "where %s was expected"),
2816 key, group_name, s, "int64");
2817 g_free (s);
2818 return 0;
2819 }
2820
2821 g_free (s);
2822 return v;
2823 }
2824
2825 /**
2826 * g_key_file_set_int64:
2827 * @key_file: a #GKeyFile
2828 * @group_name: a group name
2829 * @key: a key
2830 * @value: an integer value
2831 *
2832 * Associates a new integer value with @key under @group_name.
2833 * If @key cannot be found then it is created.
2834 *
2835 * Since: 2.26
2836 **/
2837 void
g_key_file_set_int64(GKeyFile * key_file,const gchar * group_name,const gchar * key,gint64 value)2838 g_key_file_set_int64 (GKeyFile *key_file,
2839 const gchar *group_name,
2840 const gchar *key,
2841 gint64 value)
2842 {
2843 gchar *result;
2844
2845 g_return_if_fail (key_file != NULL);
2846
2847 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2848 g_key_file_set_value (key_file, group_name, key, result);
2849 g_free (result);
2850 }
2851
2852 /**
2853 * g_key_file_get_uint64:
2854 * @key_file: a non-%NULL #GKeyFile
2855 * @group_name: a non-%NULL group name
2856 * @key: a non-%NULL key
2857 * @error: return location for a #GError
2858 *
2859 * Returns the value associated with @key under @group_name as an unsigned
2860 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2861 * large positive results without truncation.
2862 *
2863 * Returns: the value associated with the key as an unsigned 64-bit integer,
2864 * or 0 if the key was not found or could not be parsed.
2865 *
2866 * Since: 2.26
2867 */
2868 guint64
g_key_file_get_uint64(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)2869 g_key_file_get_uint64 (GKeyFile *key_file,
2870 const gchar *group_name,
2871 const gchar *key,
2872 GError **error)
2873 {
2874 gchar *s, *end;
2875 guint64 v;
2876
2877 g_return_val_if_fail (key_file != NULL, -1);
2878 g_return_val_if_fail (group_name != NULL, -1);
2879 g_return_val_if_fail (key != NULL, -1);
2880
2881 s = g_key_file_get_value (key_file, group_name, key, error);
2882
2883 if (s == NULL)
2884 return 0;
2885
2886 v = g_ascii_strtoull (s, &end, 10);
2887
2888 if (*s == '\0' || *end != '\0')
2889 {
2890 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2891 _("Key “%s” in group “%s” has value “%s” "
2892 "where %s was expected"),
2893 key, group_name, s, "uint64");
2894 g_free (s);
2895 return 0;
2896 }
2897
2898 g_free (s);
2899 return v;
2900 }
2901
2902 /**
2903 * g_key_file_set_uint64:
2904 * @key_file: a #GKeyFile
2905 * @group_name: a group name
2906 * @key: a key
2907 * @value: an integer value
2908 *
2909 * Associates a new integer value with @key under @group_name.
2910 * If @key cannot be found then it is created.
2911 *
2912 * Since: 2.26
2913 **/
2914 void
g_key_file_set_uint64(GKeyFile * key_file,const gchar * group_name,const gchar * key,guint64 value)2915 g_key_file_set_uint64 (GKeyFile *key_file,
2916 const gchar *group_name,
2917 const gchar *key,
2918 guint64 value)
2919 {
2920 gchar *result;
2921
2922 g_return_if_fail (key_file != NULL);
2923
2924 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2925 g_key_file_set_value (key_file, group_name, key, result);
2926 g_free (result);
2927 }
2928
2929 /**
2930 * g_key_file_get_integer_list:
2931 * @key_file: a #GKeyFile
2932 * @group_name: a group name
2933 * @key: a key
2934 * @length: (out): the number of integers returned
2935 * @error: return location for a #GError
2936 *
2937 * Returns the values associated with @key under @group_name as
2938 * integers.
2939 *
2940 * If @key cannot be found then %NULL is returned and @error is set to
2941 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2942 * with @key cannot be interpreted as integers, or are out of range for
2943 * #gint, then %NULL is returned
2944 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2945 *
2946 * Returns: (array length=length) (element-type gint) (transfer container):
2947 * the values associated with the key as a list of integers, or %NULL if
2948 * the key was not found or could not be parsed. The returned list of
2949 * integers should be freed with g_free() when no longer needed.
2950 *
2951 * Since: 2.6
2952 **/
2953 gint *
g_key_file_get_integer_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,gsize * length,GError ** error)2954 g_key_file_get_integer_list (GKeyFile *key_file,
2955 const gchar *group_name,
2956 const gchar *key,
2957 gsize *length,
2958 GError **error)
2959 {
2960 GError *key_file_error = NULL;
2961 gchar **values;
2962 gint *int_values;
2963 gsize i, num_ints;
2964
2965 g_return_val_if_fail (key_file != NULL, NULL);
2966 g_return_val_if_fail (group_name != NULL, NULL);
2967 g_return_val_if_fail (key != NULL, NULL);
2968
2969 if (length)
2970 *length = 0;
2971
2972 values = g_key_file_get_string_list (key_file, group_name, key,
2973 &num_ints, &key_file_error);
2974
2975 if (key_file_error)
2976 g_propagate_error (error, key_file_error);
2977
2978 if (!values)
2979 return NULL;
2980
2981 int_values = g_new (gint, num_ints);
2982
2983 for (i = 0; i < num_ints; i++)
2984 {
2985 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2986 values[i],
2987 &key_file_error);
2988
2989 if (key_file_error)
2990 {
2991 g_propagate_error (error, key_file_error);
2992 g_strfreev (values);
2993 g_free (int_values);
2994
2995 return NULL;
2996 }
2997 }
2998 g_strfreev (values);
2999
3000 if (length)
3001 *length = num_ints;
3002
3003 return int_values;
3004 }
3005
3006 /**
3007 * g_key_file_set_integer_list:
3008 * @key_file: a #GKeyFile
3009 * @group_name: a group name
3010 * @key: a key
3011 * @list: (array length=length): an array of integer values
3012 * @length: number of integer values in @list
3013 *
3014 * Associates a list of integer values with @key under @group_name.
3015 * If @key cannot be found then it is created.
3016 *
3017 * Since: 2.6
3018 **/
3019 void
g_key_file_set_integer_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,gint list[],gsize length)3020 g_key_file_set_integer_list (GKeyFile *key_file,
3021 const gchar *group_name,
3022 const gchar *key,
3023 gint list[],
3024 gsize length)
3025 {
3026 GString *values;
3027 gsize i;
3028
3029 g_return_if_fail (key_file != NULL);
3030 g_return_if_fail (list != NULL);
3031
3032 values = g_string_sized_new (length * 16);
3033 for (i = 0; i < length; i++)
3034 {
3035 gchar *value;
3036
3037 value = g_key_file_parse_integer_as_value (key_file, list[i]);
3038
3039 g_string_append (values, value);
3040 g_string_append_c (values, key_file->list_separator);
3041
3042 g_free (value);
3043 }
3044
3045 g_key_file_set_value (key_file, group_name, key, values->str);
3046 g_string_free (values, TRUE);
3047 }
3048
3049 /**
3050 * g_key_file_get_double:
3051 * @key_file: a #GKeyFile
3052 * @group_name: a group name
3053 * @key: a key
3054 * @error: return location for a #GError
3055 *
3056 * Returns the value associated with @key under @group_name as a
3057 * double. If @group_name is %NULL, the start_group is used.
3058 *
3059 * If @key cannot be found then 0.0 is returned and @error is set to
3060 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
3061 * with @key cannot be interpreted as a double then 0.0 is returned
3062 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3063 *
3064 * Returns: the value associated with the key as a double, or
3065 * 0.0 if the key was not found or could not be parsed.
3066 *
3067 * Since: 2.12
3068 **/
3069 gdouble
g_key_file_get_double(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)3070 g_key_file_get_double (GKeyFile *key_file,
3071 const gchar *group_name,
3072 const gchar *key,
3073 GError **error)
3074 {
3075 GError *key_file_error;
3076 gchar *value;
3077 gdouble double_value;
3078
3079 g_return_val_if_fail (key_file != NULL, -1);
3080 g_return_val_if_fail (group_name != NULL, -1);
3081 g_return_val_if_fail (key != NULL, -1);
3082
3083 key_file_error = NULL;
3084
3085 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3086
3087 if (key_file_error)
3088 {
3089 g_propagate_error (error, key_file_error);
3090 return 0;
3091 }
3092
3093 double_value = g_key_file_parse_value_as_double (key_file, value,
3094 &key_file_error);
3095 g_free (value);
3096
3097 if (key_file_error)
3098 {
3099 if (g_error_matches (key_file_error,
3100 G_KEY_FILE_ERROR,
3101 G_KEY_FILE_ERROR_INVALID_VALUE))
3102 {
3103 g_set_error (error, G_KEY_FILE_ERROR,
3104 G_KEY_FILE_ERROR_INVALID_VALUE,
3105 _("Key file contains key “%s” in group “%s” "
3106 "which has a value that cannot be interpreted."),
3107 key, group_name);
3108 g_error_free (key_file_error);
3109 }
3110 else
3111 g_propagate_error (error, key_file_error);
3112 }
3113
3114 return double_value;
3115 }
3116
3117 /**
3118 * g_key_file_set_double:
3119 * @key_file: a #GKeyFile
3120 * @group_name: a group name
3121 * @key: a key
3122 * @value: a double value
3123 *
3124 * Associates a new double value with @key under @group_name.
3125 * If @key cannot be found then it is created.
3126 *
3127 * Since: 2.12
3128 **/
3129 void
g_key_file_set_double(GKeyFile * key_file,const gchar * group_name,const gchar * key,gdouble value)3130 g_key_file_set_double (GKeyFile *key_file,
3131 const gchar *group_name,
3132 const gchar *key,
3133 gdouble value)
3134 {
3135 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3136
3137 g_return_if_fail (key_file != NULL);
3138
3139 g_ascii_dtostr (result, sizeof (result), value);
3140 g_key_file_set_value (key_file, group_name, key, result);
3141 }
3142
3143 /**
3144 * g_key_file_get_double_list:
3145 * @key_file: a #GKeyFile
3146 * @group_name: a group name
3147 * @key: a key
3148 * @length: (out): the number of doubles returned
3149 * @error: return location for a #GError
3150 *
3151 * Returns the values associated with @key under @group_name as
3152 * doubles.
3153 *
3154 * If @key cannot be found then %NULL is returned and @error is set to
3155 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3156 * with @key cannot be interpreted as doubles then %NULL is returned
3157 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3158 *
3159 * Returns: (array length=length) (element-type gdouble) (transfer container):
3160 * the values associated with the key as a list of doubles, or %NULL if the
3161 * key was not found or could not be parsed. The returned list of doubles
3162 * should be freed with g_free() when no longer needed.
3163 *
3164 * Since: 2.12
3165 **/
3166 gdouble *
g_key_file_get_double_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,gsize * length,GError ** error)3167 g_key_file_get_double_list (GKeyFile *key_file,
3168 const gchar *group_name,
3169 const gchar *key,
3170 gsize *length,
3171 GError **error)
3172 {
3173 GError *key_file_error = NULL;
3174 gchar **values;
3175 gdouble *double_values;
3176 gsize i, num_doubles;
3177
3178 g_return_val_if_fail (key_file != NULL, NULL);
3179 g_return_val_if_fail (group_name != NULL, NULL);
3180 g_return_val_if_fail (key != NULL, NULL);
3181
3182 if (length)
3183 *length = 0;
3184
3185 values = g_key_file_get_string_list (key_file, group_name, key,
3186 &num_doubles, &key_file_error);
3187
3188 if (key_file_error)
3189 g_propagate_error (error, key_file_error);
3190
3191 if (!values)
3192 return NULL;
3193
3194 double_values = g_new (gdouble, num_doubles);
3195
3196 for (i = 0; i < num_doubles; i++)
3197 {
3198 double_values[i] = g_key_file_parse_value_as_double (key_file,
3199 values[i],
3200 &key_file_error);
3201
3202 if (key_file_error)
3203 {
3204 g_propagate_error (error, key_file_error);
3205 g_strfreev (values);
3206 g_free (double_values);
3207
3208 return NULL;
3209 }
3210 }
3211 g_strfreev (values);
3212
3213 if (length)
3214 *length = num_doubles;
3215
3216 return double_values;
3217 }
3218
3219 /**
3220 * g_key_file_set_double_list:
3221 * @key_file: a #GKeyFile
3222 * @group_name: a group name
3223 * @key: a key
3224 * @list: (array length=length): an array of double values
3225 * @length: number of double values in @list
3226 *
3227 * Associates a list of double values with @key under
3228 * @group_name. If @key cannot be found then it is created.
3229 *
3230 * Since: 2.12
3231 **/
3232 void
g_key_file_set_double_list(GKeyFile * key_file,const gchar * group_name,const gchar * key,gdouble list[],gsize length)3233 g_key_file_set_double_list (GKeyFile *key_file,
3234 const gchar *group_name,
3235 const gchar *key,
3236 gdouble list[],
3237 gsize length)
3238 {
3239 GString *values;
3240 gsize i;
3241
3242 g_return_if_fail (key_file != NULL);
3243 g_return_if_fail (list != NULL);
3244
3245 values = g_string_sized_new (length * 16);
3246 for (i = 0; i < length; i++)
3247 {
3248 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3249
3250 g_ascii_dtostr( result, sizeof (result), list[i] );
3251
3252 g_string_append (values, result);
3253 g_string_append_c (values, key_file->list_separator);
3254 }
3255
3256 g_key_file_set_value (key_file, group_name, key, values->str);
3257 g_string_free (values, TRUE);
3258 }
3259
3260 static gboolean
g_key_file_set_key_comment(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * comment,GError ** error)3261 g_key_file_set_key_comment (GKeyFile *key_file,
3262 const gchar *group_name,
3263 const gchar *key,
3264 const gchar *comment,
3265 GError **error)
3266 {
3267 GKeyFileGroup *group;
3268 GKeyFileKeyValuePair *pair;
3269 GList *key_node, *comment_node, *tmp;
3270
3271 group = g_key_file_lookup_group (key_file, group_name);
3272 if (!group)
3273 {
3274 g_set_error (error, G_KEY_FILE_ERROR,
3275 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3276 _("Key file does not have group “%s”"),
3277 group_name ? group_name : "(null)");
3278
3279 return FALSE;
3280 }
3281
3282 /* First find the key the comments are supposed to be
3283 * associated with
3284 */
3285 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3286
3287 if (key_node == NULL)
3288 {
3289 set_not_found_key_error (group->name, key, error);
3290 return FALSE;
3291 }
3292
3293 /* Then find all the comments already associated with the
3294 * key and free them
3295 */
3296 tmp = key_node->next;
3297 while (tmp != NULL)
3298 {
3299 pair = (GKeyFileKeyValuePair *) tmp->data;
3300
3301 if (pair->key != NULL)
3302 break;
3303
3304 comment_node = tmp;
3305 tmp = tmp->next;
3306 g_key_file_remove_key_value_pair_node (key_file, group,
3307 comment_node);
3308 }
3309
3310 if (comment == NULL)
3311 return TRUE;
3312
3313 /* Now we can add our new comment
3314 */
3315 pair = g_slice_new (GKeyFileKeyValuePair);
3316 pair->key = NULL;
3317 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3318
3319 key_node = g_list_insert (key_node, pair, 1);
3320
3321 return TRUE;
3322 }
3323
3324 static gboolean
g_key_file_set_group_comment(GKeyFile * key_file,const gchar * group_name,const gchar * comment,GError ** error)3325 g_key_file_set_group_comment (GKeyFile *key_file,
3326 const gchar *group_name,
3327 const gchar *comment,
3328 GError **error)
3329 {
3330 GKeyFileGroup *group;
3331
3332 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3333
3334 group = g_key_file_lookup_group (key_file, group_name);
3335 if (!group)
3336 {
3337 g_set_error (error, G_KEY_FILE_ERROR,
3338 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3339 _("Key file does not have group “%s”"),
3340 group_name ? group_name : "(null)");
3341
3342 return FALSE;
3343 }
3344
3345 /* First remove any existing comment
3346 */
3347 if (group->comment)
3348 {
3349 g_key_file_key_value_pair_free (group->comment);
3350 group->comment = NULL;
3351 }
3352
3353 if (comment == NULL)
3354 return TRUE;
3355
3356 /* Now we can add our new comment
3357 */
3358 group->comment = g_slice_new (GKeyFileKeyValuePair);
3359 group->comment->key = NULL;
3360 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3361
3362 return TRUE;
3363 }
3364
3365 static gboolean
g_key_file_set_top_comment(GKeyFile * key_file,const gchar * comment,GError ** error)3366 g_key_file_set_top_comment (GKeyFile *key_file,
3367 const gchar *comment,
3368 GError **error)
3369 {
3370 GList *group_node;
3371 GKeyFileGroup *group;
3372 GKeyFileKeyValuePair *pair;
3373
3374 /* The last group in the list should be the top (comments only)
3375 * group in the file
3376 */
3377 g_warn_if_fail (key_file->groups != NULL);
3378 group_node = g_list_last (key_file->groups);
3379 group = (GKeyFileGroup *) group_node->data;
3380 g_warn_if_fail (group->name == NULL);
3381
3382 /* Note all keys must be comments at the top of
3383 * the file, so we can just free it all.
3384 */
3385 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3386 group->key_value_pairs = NULL;
3387
3388 if (comment == NULL)
3389 return TRUE;
3390
3391 pair = g_slice_new (GKeyFileKeyValuePair);
3392 pair->key = NULL;
3393 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3394
3395 group->key_value_pairs =
3396 g_list_prepend (group->key_value_pairs, pair);
3397
3398 return TRUE;
3399 }
3400
3401 /**
3402 * g_key_file_set_comment:
3403 * @key_file: a #GKeyFile
3404 * @group_name: (nullable): a group name, or %NULL
3405 * @key: (nullable): a key
3406 * @comment: a comment
3407 * @error: return location for a #GError
3408 *
3409 * Places a comment above @key from @group_name.
3410 *
3411 * If @key is %NULL then @comment will be written above @group_name.
3412 * If both @key and @group_name are %NULL, then @comment will be
3413 * written above the first group in the file.
3414 *
3415 * Note that this function prepends a '#' comment marker to
3416 * each line of @comment.
3417 *
3418 * Returns: %TRUE if the comment was written, %FALSE otherwise
3419 *
3420 * Since: 2.6
3421 **/
3422 gboolean
g_key_file_set_comment(GKeyFile * key_file,const gchar * group_name,const gchar * key,const gchar * comment,GError ** error)3423 g_key_file_set_comment (GKeyFile *key_file,
3424 const gchar *group_name,
3425 const gchar *key,
3426 const gchar *comment,
3427 GError **error)
3428 {
3429 g_return_val_if_fail (key_file != NULL, FALSE);
3430
3431 if (group_name != NULL && key != NULL)
3432 {
3433 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3434 return FALSE;
3435 }
3436 else if (group_name != NULL)
3437 {
3438 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3439 return FALSE;
3440 }
3441 else
3442 {
3443 if (!g_key_file_set_top_comment (key_file, comment, error))
3444 return FALSE;
3445 }
3446
3447 return TRUE;
3448 }
3449
3450 static gchar *
g_key_file_get_key_comment(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)3451 g_key_file_get_key_comment (GKeyFile *key_file,
3452 const gchar *group_name,
3453 const gchar *key,
3454 GError **error)
3455 {
3456 GKeyFileGroup *group;
3457 GKeyFileKeyValuePair *pair;
3458 GList *key_node, *tmp;
3459 GString *string;
3460 gchar *comment;
3461
3462 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3463
3464 group = g_key_file_lookup_group (key_file, group_name);
3465 if (!group)
3466 {
3467 g_set_error (error, G_KEY_FILE_ERROR,
3468 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3469 _("Key file does not have group “%s”"),
3470 group_name ? group_name : "(null)");
3471
3472 return NULL;
3473 }
3474
3475 /* First find the key the comments are supposed to be
3476 * associated with
3477 */
3478 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3479
3480 if (key_node == NULL)
3481 {
3482 set_not_found_key_error (group->name, key, error);
3483 return NULL;
3484 }
3485
3486 string = NULL;
3487
3488 /* Then find all the comments already associated with the
3489 * key and concatentate them.
3490 */
3491 tmp = key_node->next;
3492 if (!key_node->next)
3493 return NULL;
3494
3495 pair = (GKeyFileKeyValuePair *) tmp->data;
3496 if (pair->key != NULL)
3497 return NULL;
3498
3499 while (tmp->next)
3500 {
3501 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3502
3503 if (pair->key != NULL)
3504 break;
3505
3506 tmp = tmp->next;
3507 }
3508
3509 while (tmp != key_node)
3510 {
3511 pair = (GKeyFileKeyValuePair *) tmp->data;
3512
3513 if (string == NULL)
3514 string = g_string_sized_new (512);
3515
3516 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3517 (tmp->prev == key_node));
3518 g_string_append (string, comment);
3519 g_free (comment);
3520
3521 tmp = tmp->prev;
3522 }
3523
3524 if (string != NULL)
3525 {
3526 comment = string->str;
3527 g_string_free (string, FALSE);
3528 }
3529 else
3530 comment = NULL;
3531
3532 return comment;
3533 }
3534
3535 static gchar *
get_group_comment(GKeyFile * key_file,GKeyFileGroup * group,GError ** error)3536 get_group_comment (GKeyFile *key_file,
3537 GKeyFileGroup *group,
3538 GError **error)
3539 {
3540 GString *string;
3541 GList *tmp;
3542 gchar *comment;
3543
3544 string = NULL;
3545
3546 tmp = group->key_value_pairs;
3547 while (tmp)
3548 {
3549 GKeyFileKeyValuePair *pair;
3550
3551 pair = (GKeyFileKeyValuePair *) tmp->data;
3552
3553 if (pair->key != NULL)
3554 {
3555 tmp = tmp->prev;
3556 break;
3557 }
3558
3559 if (tmp->next == NULL)
3560 break;
3561
3562 tmp = tmp->next;
3563 }
3564
3565 while (tmp != NULL)
3566 {
3567 GKeyFileKeyValuePair *pair;
3568
3569 pair = (GKeyFileKeyValuePair *) tmp->data;
3570
3571 if (string == NULL)
3572 string = g_string_sized_new (512);
3573
3574 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3575 (tmp->prev == NULL));
3576 g_string_append (string, comment);
3577 g_free (comment);
3578
3579 tmp = tmp->prev;
3580 }
3581
3582 if (string != NULL)
3583 return g_string_free (string, FALSE);
3584
3585 return NULL;
3586 }
3587
3588 static gchar *
g_key_file_get_group_comment(GKeyFile * key_file,const gchar * group_name,GError ** error)3589 g_key_file_get_group_comment (GKeyFile *key_file,
3590 const gchar *group_name,
3591 GError **error)
3592 {
3593 GList *group_node;
3594 GKeyFileGroup *group;
3595
3596 group = g_key_file_lookup_group (key_file, group_name);
3597 if (!group)
3598 {
3599 g_set_error (error, G_KEY_FILE_ERROR,
3600 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3601 _("Key file does not have group “%s”"),
3602 group_name ? group_name : "(null)");
3603
3604 return NULL;
3605 }
3606
3607 if (group->comment)
3608 return g_strdup (group->comment->value);
3609
3610 group_node = g_key_file_lookup_group_node (key_file, group_name);
3611 group_node = group_node->next;
3612 group = (GKeyFileGroup *)group_node->data;
3613 return get_group_comment (key_file, group, error);
3614 }
3615
3616 static gchar *
g_key_file_get_top_comment(GKeyFile * key_file,GError ** error)3617 g_key_file_get_top_comment (GKeyFile *key_file,
3618 GError **error)
3619 {
3620 GList *group_node;
3621 GKeyFileGroup *group;
3622
3623 /* The last group in the list should be the top (comments only)
3624 * group in the file
3625 */
3626 g_warn_if_fail (key_file->groups != NULL);
3627 group_node = g_list_last (key_file->groups);
3628 group = (GKeyFileGroup *) group_node->data;
3629 g_warn_if_fail (group->name == NULL);
3630
3631 return get_group_comment (key_file, group, error);
3632 }
3633
3634 /**
3635 * g_key_file_get_comment:
3636 * @key_file: a #GKeyFile
3637 * @group_name: (nullable): a group name, or %NULL
3638 * @key: a key
3639 * @error: return location for a #GError
3640 *
3641 * Retrieves a comment above @key from @group_name.
3642 * If @key is %NULL then @comment will be read from above
3643 * @group_name. If both @key and @group_name are %NULL, then
3644 * @comment will be read from above the first group in the file.
3645 *
3646 * Note that the returned string does not include the '#' comment markers,
3647 * but does include any whitespace after them (on each line). It includes
3648 * the line breaks between lines, but does not include the final line break.
3649 *
3650 * Returns: a comment that should be freed with g_free()
3651 *
3652 * Since: 2.6
3653 **/
3654 gchar *
g_key_file_get_comment(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)3655 g_key_file_get_comment (GKeyFile *key_file,
3656 const gchar *group_name,
3657 const gchar *key,
3658 GError **error)
3659 {
3660 g_return_val_if_fail (key_file != NULL, NULL);
3661
3662 if (group_name != NULL && key != NULL)
3663 return g_key_file_get_key_comment (key_file, group_name, key, error);
3664 else if (group_name != NULL)
3665 return g_key_file_get_group_comment (key_file, group_name, error);
3666 else
3667 return g_key_file_get_top_comment (key_file, error);
3668 }
3669
3670 /**
3671 * g_key_file_remove_comment:
3672 * @key_file: a #GKeyFile
3673 * @group_name: (nullable): a group name, or %NULL
3674 * @key: (nullable): a key
3675 * @error: return location for a #GError
3676 *
3677 * Removes a comment above @key from @group_name.
3678 * If @key is %NULL then @comment will be removed above @group_name.
3679 * If both @key and @group_name are %NULL, then @comment will
3680 * be removed above the first group in the file.
3681 *
3682 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3683 *
3684 * Since: 2.6
3685 **/
3686
3687 gboolean
g_key_file_remove_comment(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)3688 g_key_file_remove_comment (GKeyFile *key_file,
3689 const gchar *group_name,
3690 const gchar *key,
3691 GError **error)
3692 {
3693 g_return_val_if_fail (key_file != NULL, FALSE);
3694
3695 if (group_name != NULL && key != NULL)
3696 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3697 else if (group_name != NULL)
3698 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3699 else
3700 return g_key_file_set_top_comment (key_file, NULL, error);
3701 }
3702
3703 /**
3704 * g_key_file_has_group:
3705 * @key_file: a #GKeyFile
3706 * @group_name: a group name
3707 *
3708 * Looks whether the key file has the group @group_name.
3709 *
3710 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3711 * otherwise.
3712 * Since: 2.6
3713 **/
3714 gboolean
g_key_file_has_group(GKeyFile * key_file,const gchar * group_name)3715 g_key_file_has_group (GKeyFile *key_file,
3716 const gchar *group_name)
3717 {
3718 g_return_val_if_fail (key_file != NULL, FALSE);
3719 g_return_val_if_fail (group_name != NULL, FALSE);
3720
3721 return g_key_file_lookup_group (key_file, group_name) != NULL;
3722 }
3723
3724 /* This code remains from a historical attempt to add a new public API
3725 * which respects the GError rules.
3726 */
3727 static gboolean
g_key_file_has_key_full(GKeyFile * key_file,const gchar * group_name,const gchar * key,gboolean * has_key,GError ** error)3728 g_key_file_has_key_full (GKeyFile *key_file,
3729 const gchar *group_name,
3730 const gchar *key,
3731 gboolean *has_key,
3732 GError **error)
3733 {
3734 GKeyFileKeyValuePair *pair;
3735 GKeyFileGroup *group;
3736
3737 g_return_val_if_fail (key_file != NULL, FALSE);
3738 g_return_val_if_fail (group_name != NULL, FALSE);
3739 g_return_val_if_fail (key != NULL, FALSE);
3740
3741 group = g_key_file_lookup_group (key_file, group_name);
3742
3743 if (!group)
3744 {
3745 g_set_error (error, G_KEY_FILE_ERROR,
3746 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3747 _("Key file does not have group “%s”"),
3748 group_name);
3749
3750 return FALSE;
3751 }
3752
3753 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3754
3755 if (has_key)
3756 *has_key = pair != NULL;
3757 return TRUE;
3758 }
3759
3760 /**
3761 * g_key_file_has_key: (skip)
3762 * @key_file: a #GKeyFile
3763 * @group_name: a group name
3764 * @key: a key name
3765 * @error: return location for a #GError
3766 *
3767 * Looks whether the key file has the key @key in the group
3768 * @group_name.
3769 *
3770 * Note that this function does not follow the rules for #GError strictly;
3771 * the return value both carries meaning and signals an error. To use
3772 * this function, you must pass a #GError pointer in @error, and check
3773 * whether it is not %NULL to see if an error occurred.
3774 *
3775 * Language bindings should use g_key_file_get_value() to test whether
3776 * or not a key exists.
3777 *
3778 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3779 *
3780 * Since: 2.6
3781 **/
3782 gboolean
g_key_file_has_key(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)3783 g_key_file_has_key (GKeyFile *key_file,
3784 const gchar *group_name,
3785 const gchar *key,
3786 GError **error)
3787 {
3788 GError *temp_error = NULL;
3789 gboolean has_key;
3790
3791 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3792 {
3793 return has_key;
3794 }
3795 else
3796 {
3797 g_propagate_error (error, temp_error);
3798 return FALSE;
3799 }
3800 }
3801
3802 static void
g_key_file_add_group(GKeyFile * key_file,const gchar * group_name)3803 g_key_file_add_group (GKeyFile *key_file,
3804 const gchar *group_name)
3805 {
3806 GKeyFileGroup *group;
3807
3808 g_return_if_fail (key_file != NULL);
3809 g_return_if_fail (g_key_file_is_group_name (group_name));
3810
3811 group = g_key_file_lookup_group (key_file, group_name);
3812 if (group != NULL)
3813 {
3814 key_file->current_group = group;
3815 return;
3816 }
3817
3818 group = g_slice_new0 (GKeyFileGroup);
3819 group->name = g_strdup (group_name);
3820 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3821 key_file->groups = g_list_prepend (key_file->groups, group);
3822 key_file->current_group = group;
3823
3824 if (key_file->start_group == NULL)
3825 key_file->start_group = group;
3826
3827 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3828 }
3829
3830 static void
g_key_file_key_value_pair_free(GKeyFileKeyValuePair * pair)3831 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3832 {
3833 if (pair != NULL)
3834 {
3835 g_free (pair->key);
3836 g_free (pair->value);
3837 g_slice_free (GKeyFileKeyValuePair, pair);
3838 }
3839 }
3840
3841 /* Be careful not to call this function on a node with data in the
3842 * lookup map without removing it from the lookup map, first.
3843 *
3844 * Some current cases where this warning is not a concern are
3845 * when:
3846 * - the node being removed is a comment node
3847 * - the entire lookup map is getting destroyed soon after
3848 * anyway.
3849 */
3850 static void
g_key_file_remove_key_value_pair_node(GKeyFile * key_file,GKeyFileGroup * group,GList * pair_node)3851 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3852 GKeyFileGroup *group,
3853 GList *pair_node)
3854 {
3855
3856 GKeyFileKeyValuePair *pair;
3857
3858 pair = (GKeyFileKeyValuePair *) pair_node->data;
3859
3860 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3861
3862 g_warn_if_fail (pair->value != NULL);
3863
3864 g_key_file_key_value_pair_free (pair);
3865
3866 g_list_free_1 (pair_node);
3867 }
3868
3869 static void
g_key_file_remove_group_node(GKeyFile * key_file,GList * group_node)3870 g_key_file_remove_group_node (GKeyFile *key_file,
3871 GList *group_node)
3872 {
3873 GKeyFileGroup *group;
3874 GList *tmp;
3875
3876 group = (GKeyFileGroup *) group_node->data;
3877
3878 if (group->name)
3879 g_hash_table_remove (key_file->group_hash, group->name);
3880
3881 /* If the current group gets deleted make the current group the last
3882 * added group.
3883 */
3884 if (key_file->current_group == group)
3885 {
3886 /* groups should always contain at least the top comment group,
3887 * unless g_key_file_clear has been called
3888 */
3889 if (key_file->groups)
3890 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3891 else
3892 key_file->current_group = NULL;
3893 }
3894
3895 /* If the start group gets deleted make the start group the first
3896 * added group.
3897 */
3898 if (key_file->start_group == group)
3899 {
3900 tmp = g_list_last (key_file->groups);
3901 while (tmp != NULL)
3902 {
3903 if (tmp != group_node &&
3904 ((GKeyFileGroup *) tmp->data)->name != NULL)
3905 break;
3906
3907 tmp = tmp->prev;
3908 }
3909
3910 if (tmp)
3911 key_file->start_group = (GKeyFileGroup *) tmp->data;
3912 else
3913 key_file->start_group = NULL;
3914 }
3915
3916 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3917
3918 tmp = group->key_value_pairs;
3919 while (tmp != NULL)
3920 {
3921 GList *pair_node;
3922
3923 pair_node = tmp;
3924 tmp = tmp->next;
3925 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3926 }
3927
3928 g_warn_if_fail (group->key_value_pairs == NULL);
3929
3930 if (group->comment)
3931 {
3932 g_key_file_key_value_pair_free (group->comment);
3933 group->comment = NULL;
3934 }
3935
3936 if (group->lookup_map)
3937 {
3938 g_hash_table_destroy (group->lookup_map);
3939 group->lookup_map = NULL;
3940 }
3941
3942 g_free ((gchar *) group->name);
3943 g_slice_free (GKeyFileGroup, group);
3944 g_list_free_1 (group_node);
3945 }
3946
3947 /**
3948 * g_key_file_remove_group:
3949 * @key_file: a #GKeyFile
3950 * @group_name: a group name
3951 * @error: return location for a #GError or %NULL
3952 *
3953 * Removes the specified group, @group_name,
3954 * from the key file.
3955 *
3956 * Returns: %TRUE if the group was removed, %FALSE otherwise
3957 *
3958 * Since: 2.6
3959 **/
3960 gboolean
g_key_file_remove_group(GKeyFile * key_file,const gchar * group_name,GError ** error)3961 g_key_file_remove_group (GKeyFile *key_file,
3962 const gchar *group_name,
3963 GError **error)
3964 {
3965 GList *group_node;
3966
3967 g_return_val_if_fail (key_file != NULL, FALSE);
3968 g_return_val_if_fail (group_name != NULL, FALSE);
3969
3970 group_node = g_key_file_lookup_group_node (key_file, group_name);
3971
3972 if (!group_node)
3973 {
3974 g_set_error (error, G_KEY_FILE_ERROR,
3975 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3976 _("Key file does not have group “%s”"),
3977 group_name);
3978 return FALSE;
3979 }
3980
3981 g_key_file_remove_group_node (key_file, group_node);
3982
3983 return TRUE;
3984 }
3985
3986 static void
g_key_file_add_key_value_pair(GKeyFile * key_file,GKeyFileGroup * group,GKeyFileKeyValuePair * pair)3987 g_key_file_add_key_value_pair (GKeyFile *key_file,
3988 GKeyFileGroup *group,
3989 GKeyFileKeyValuePair *pair)
3990 {
3991 g_hash_table_replace (group->lookup_map, pair->key, pair);
3992 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3993 }
3994
3995 static void
g_key_file_add_key(GKeyFile * key_file,GKeyFileGroup * group,const gchar * key,const gchar * value)3996 g_key_file_add_key (GKeyFile *key_file,
3997 GKeyFileGroup *group,
3998 const gchar *key,
3999 const gchar *value)
4000 {
4001 GKeyFileKeyValuePair *pair;
4002
4003 pair = g_slice_new (GKeyFileKeyValuePair);
4004 pair->key = g_strdup (key);
4005 pair->value = g_strdup (value);
4006
4007 g_key_file_add_key_value_pair (key_file, group, pair);
4008 }
4009
4010 /**
4011 * g_key_file_remove_key:
4012 * @key_file: a #GKeyFile
4013 * @group_name: a group name
4014 * @key: a key name to remove
4015 * @error: return location for a #GError or %NULL
4016 *
4017 * Removes @key in @group_name from the key file.
4018 *
4019 * Returns: %TRUE if the key was removed, %FALSE otherwise
4020 *
4021 * Since: 2.6
4022 **/
4023 gboolean
g_key_file_remove_key(GKeyFile * key_file,const gchar * group_name,const gchar * key,GError ** error)4024 g_key_file_remove_key (GKeyFile *key_file,
4025 const gchar *group_name,
4026 const gchar *key,
4027 GError **error)
4028 {
4029 GKeyFileGroup *group;
4030 GKeyFileKeyValuePair *pair;
4031
4032 g_return_val_if_fail (key_file != NULL, FALSE);
4033 g_return_val_if_fail (group_name != NULL, FALSE);
4034 g_return_val_if_fail (key != NULL, FALSE);
4035
4036 pair = NULL;
4037
4038 group = g_key_file_lookup_group (key_file, group_name);
4039 if (!group)
4040 {
4041 g_set_error (error, G_KEY_FILE_ERROR,
4042 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4043 _("Key file does not have group “%s”"),
4044 group_name);
4045 return FALSE;
4046 }
4047
4048 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
4049
4050 if (!pair)
4051 {
4052 set_not_found_key_error (group->name, key, error);
4053 return FALSE;
4054 }
4055
4056 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
4057 g_hash_table_remove (group->lookup_map, pair->key);
4058 g_key_file_key_value_pair_free (pair);
4059
4060 return TRUE;
4061 }
4062
4063 static GList *
g_key_file_lookup_group_node(GKeyFile * key_file,const gchar * group_name)4064 g_key_file_lookup_group_node (GKeyFile *key_file,
4065 const gchar *group_name)
4066 {
4067 GKeyFileGroup *group;
4068 GList *tmp;
4069
4070 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
4071 {
4072 group = (GKeyFileGroup *) tmp->data;
4073
4074 if (group && group->name && strcmp (group->name, group_name) == 0)
4075 break;
4076 }
4077
4078 return tmp;
4079 }
4080
4081 static GKeyFileGroup *
g_key_file_lookup_group(GKeyFile * key_file,const gchar * group_name)4082 g_key_file_lookup_group (GKeyFile *key_file,
4083 const gchar *group_name)
4084 {
4085 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4086 }
4087
4088 static GList *
g_key_file_lookup_key_value_pair_node(GKeyFile * key_file,GKeyFileGroup * group,const gchar * key)4089 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4090 GKeyFileGroup *group,
4091 const gchar *key)
4092 {
4093 GList *key_node;
4094
4095 for (key_node = group->key_value_pairs;
4096 key_node != NULL;
4097 key_node = key_node->next)
4098 {
4099 GKeyFileKeyValuePair *pair;
4100
4101 pair = (GKeyFileKeyValuePair *) key_node->data;
4102
4103 if (pair->key && strcmp (pair->key, key) == 0)
4104 break;
4105 }
4106
4107 return key_node;
4108 }
4109
4110 static GKeyFileKeyValuePair *
g_key_file_lookup_key_value_pair(GKeyFile * key_file,GKeyFileGroup * group,const gchar * key)4111 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4112 GKeyFileGroup *group,
4113 const gchar *key)
4114 {
4115 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4116 }
4117
4118 /* Lines starting with # or consisting entirely of whitespace are merely
4119 * recorded, not parsed. This function assumes all leading whitespace
4120 * has been stripped.
4121 */
4122 static gboolean
g_key_file_line_is_comment(const gchar * line)4123 g_key_file_line_is_comment (const gchar *line)
4124 {
4125 return (*line == '#' || *line == '\0' || *line == '\n');
4126 }
4127
4128 static gboolean
g_key_file_is_group_name(const gchar * name)4129 g_key_file_is_group_name (const gchar *name)
4130 {
4131 gchar *p, *q;
4132
4133 if (name == NULL)
4134 return FALSE;
4135
4136 p = q = (gchar *) name;
4137 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4138 q = g_utf8_find_next_char (q, NULL);
4139
4140 if (*q != '\0' || q == p)
4141 return FALSE;
4142
4143 return TRUE;
4144 }
4145
4146 static gboolean
g_key_file_is_key_name(const gchar * name)4147 g_key_file_is_key_name (const gchar *name)
4148 {
4149 gchar *p, *q;
4150
4151 if (name == NULL)
4152 return FALSE;
4153
4154 p = q = (gchar *) name;
4155 /* We accept a little more than the desktop entry spec says,
4156 * since gnome-vfs uses mime-types as keys in its cache.
4157 */
4158 while (*q && *q != '=' && *q != '[' && *q != ']')
4159 q = g_utf8_find_next_char (q, NULL);
4160
4161 /* No empty keys, please */
4162 if (q == p)
4163 return FALSE;
4164
4165 /* We accept spaces in the middle of keys to not break
4166 * existing apps, but we don't tolerate initial or final
4167 * spaces, which would lead to silent corruption when
4168 * rereading the file.
4169 */
4170 if (*p == ' ' || q[-1] == ' ')
4171 return FALSE;
4172
4173 if (*q == '[')
4174 {
4175 q++;
4176 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4177 q = g_utf8_find_next_char (q, NULL);
4178
4179 if (*q != ']')
4180 return FALSE;
4181
4182 q++;
4183 }
4184
4185 if (*q != '\0')
4186 return FALSE;
4187
4188 return TRUE;
4189 }
4190
4191 /* A group in a key file is made up of a starting '[' followed by one
4192 * or more letters making up the group name followed by ']'.
4193 */
4194 static gboolean
g_key_file_line_is_group(const gchar * line)4195 g_key_file_line_is_group (const gchar *line)
4196 {
4197 gchar *p;
4198
4199 p = (gchar *) line;
4200 if (*p != '[')
4201 return FALSE;
4202
4203 p++;
4204
4205 while (*p && *p != ']')
4206 p = g_utf8_find_next_char (p, NULL);
4207
4208 if (*p != ']')
4209 return FALSE;
4210
4211 /* silently accept whitespace after the ] */
4212 p = g_utf8_find_next_char (p, NULL);
4213 while (*p == ' ' || *p == '\t')
4214 p = g_utf8_find_next_char (p, NULL);
4215
4216 if (*p)
4217 return FALSE;
4218
4219 return TRUE;
4220 }
4221
4222 static gboolean
g_key_file_line_is_key_value_pair(const gchar * line)4223 g_key_file_line_is_key_value_pair (const gchar *line)
4224 {
4225 gchar *p;
4226
4227 p = (gchar *) g_utf8_strchr (line, -1, '=');
4228
4229 if (!p)
4230 return FALSE;
4231
4232 /* Key must be non-empty
4233 */
4234 if (*p == line[0])
4235 return FALSE;
4236
4237 return TRUE;
4238 }
4239
4240 static gchar *
g_key_file_parse_value_as_string(GKeyFile * key_file,const gchar * value,GSList ** pieces,GError ** error)4241 g_key_file_parse_value_as_string (GKeyFile *key_file,
4242 const gchar *value,
4243 GSList **pieces,
4244 GError **error)
4245 {
4246 gchar *string_value, *p, *q0, *q;
4247
4248 string_value = g_new (gchar, strlen (value) + 1);
4249
4250 p = (gchar *) value;
4251 q0 = q = string_value;
4252 while (*p)
4253 {
4254 if (*p == '\\')
4255 {
4256 p++;
4257
4258 switch (*p)
4259 {
4260 case 's':
4261 *q = ' ';
4262 break;
4263
4264 case 'n':
4265 *q = '\n';
4266 break;
4267
4268 case 't':
4269 *q = '\t';
4270 break;
4271
4272 case 'r':
4273 *q = '\r';
4274 break;
4275
4276 case '\\':
4277 *q = '\\';
4278 break;
4279
4280 case '\0':
4281 g_set_error_literal (error, G_KEY_FILE_ERROR,
4282 G_KEY_FILE_ERROR_INVALID_VALUE,
4283 _("Key file contains escape character "
4284 "at end of line"));
4285 break;
4286
4287 default:
4288 if (pieces && *p == key_file->list_separator)
4289 *q = key_file->list_separator;
4290 else
4291 {
4292 *q++ = '\\';
4293 *q = *p;
4294
4295 if (*error == NULL)
4296 {
4297 gchar sequence[3];
4298
4299 sequence[0] = '\\';
4300 sequence[1] = *p;
4301 sequence[2] = '\0';
4302
4303 g_set_error (error, G_KEY_FILE_ERROR,
4304 G_KEY_FILE_ERROR_INVALID_VALUE,
4305 _("Key file contains invalid escape "
4306 "sequence “%s”"), sequence);
4307 }
4308 }
4309 break;
4310 }
4311 }
4312 else
4313 {
4314 *q = *p;
4315 if (pieces && (*p == key_file->list_separator))
4316 {
4317 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4318 q0 = q + 1;
4319 }
4320 }
4321
4322 if (*p == '\0')
4323 break;
4324
4325 q++;
4326 p++;
4327 }
4328
4329 *q = '\0';
4330 if (pieces)
4331 {
4332 if (q0 < q)
4333 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4334 *pieces = g_slist_reverse (*pieces);
4335 }
4336
4337 return string_value;
4338 }
4339
4340 static gchar *
g_key_file_parse_string_as_value(GKeyFile * key_file,const gchar * string,gboolean escape_separator)4341 g_key_file_parse_string_as_value (GKeyFile *key_file,
4342 const gchar *string,
4343 gboolean escape_separator)
4344 {
4345 gchar *value, *p, *q;
4346 gsize length;
4347 gboolean parsing_leading_space;
4348
4349 length = strlen (string) + 1;
4350
4351 /* Worst case would be that every character needs to be escaped.
4352 * In other words every character turns to two characters
4353 */
4354 value = g_new (gchar, 2 * length);
4355
4356 p = (gchar *) string;
4357 q = value;
4358 parsing_leading_space = TRUE;
4359 while (p < (string + length - 1))
4360 {
4361 gchar escaped_character[3] = { '\\', 0, 0 };
4362
4363 switch (*p)
4364 {
4365 case ' ':
4366 if (parsing_leading_space)
4367 {
4368 escaped_character[1] = 's';
4369 strcpy (q, escaped_character);
4370 q += 2;
4371 }
4372 else
4373 {
4374 *q = *p;
4375 q++;
4376 }
4377 break;
4378 case '\t':
4379 if (parsing_leading_space)
4380 {
4381 escaped_character[1] = 't';
4382 strcpy (q, escaped_character);
4383 q += 2;
4384 }
4385 else
4386 {
4387 *q = *p;
4388 q++;
4389 }
4390 break;
4391 case '\n':
4392 escaped_character[1] = 'n';
4393 strcpy (q, escaped_character);
4394 q += 2;
4395 break;
4396 case '\r':
4397 escaped_character[1] = 'r';
4398 strcpy (q, escaped_character);
4399 q += 2;
4400 break;
4401 case '\\':
4402 escaped_character[1] = '\\';
4403 strcpy (q, escaped_character);
4404 q += 2;
4405 parsing_leading_space = FALSE;
4406 break;
4407 default:
4408 if (escape_separator && *p == key_file->list_separator)
4409 {
4410 escaped_character[1] = key_file->list_separator;
4411 strcpy (q, escaped_character);
4412 q += 2;
4413 parsing_leading_space = TRUE;
4414 }
4415 else
4416 {
4417 *q = *p;
4418 q++;
4419 parsing_leading_space = FALSE;
4420 }
4421 break;
4422 }
4423 p++;
4424 }
4425 *q = '\0';
4426
4427 return value;
4428 }
4429
4430 static gint
g_key_file_parse_value_as_integer(GKeyFile * key_file,const gchar * value,GError ** error)4431 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4432 const gchar *value,
4433 GError **error)
4434 {
4435 gchar *eof_int;
4436 glong long_value;
4437 gint int_value;
4438 int errsv;
4439
4440 errno = 0;
4441 long_value = strtol (value, &eof_int, 10);
4442 errsv = errno;
4443
4444 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4445 {
4446 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4447 g_set_error (error, G_KEY_FILE_ERROR,
4448 G_KEY_FILE_ERROR_INVALID_VALUE,
4449 _("Value “%s” cannot be interpreted "
4450 "as a number."), value_utf8);
4451 g_free (value_utf8);
4452
4453 return 0;
4454 }
4455
4456 int_value = long_value;
4457 if (int_value != long_value || errsv == ERANGE)
4458 {
4459 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4460 g_set_error (error,
4461 G_KEY_FILE_ERROR,
4462 G_KEY_FILE_ERROR_INVALID_VALUE,
4463 _("Integer value “%s” out of range"),
4464 value_utf8);
4465 g_free (value_utf8);
4466
4467 return 0;
4468 }
4469
4470 return int_value;
4471 }
4472
4473 static gchar *
g_key_file_parse_integer_as_value(GKeyFile * key_file,gint value)4474 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4475 gint value)
4476
4477 {
4478 return g_strdup_printf ("%d", value);
4479 }
4480
4481 static gdouble
g_key_file_parse_value_as_double(GKeyFile * key_file,const gchar * value,GError ** error)4482 g_key_file_parse_value_as_double (GKeyFile *key_file,
4483 const gchar *value,
4484 GError **error)
4485 {
4486 gchar *end_of_valid_d;
4487 gdouble double_value = 0;
4488
4489 double_value = g_ascii_strtod (value, &end_of_valid_d);
4490
4491 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4492 {
4493 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4494 g_set_error (error, G_KEY_FILE_ERROR,
4495 G_KEY_FILE_ERROR_INVALID_VALUE,
4496 _("Value “%s” cannot be interpreted "
4497 "as a float number."),
4498 value_utf8);
4499 g_free (value_utf8);
4500
4501 double_value = 0;
4502 }
4503
4504 return double_value;
4505 }
4506
4507 static gint
strcmp_sized(const gchar * s1,size_t len1,const gchar * s2)4508 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4509 {
4510 size_t len2 = strlen (s2);
4511 return strncmp (s1, s2, MAX (len1, len2));
4512 }
4513
4514 static gboolean
g_key_file_parse_value_as_boolean(GKeyFile * key_file,const gchar * value,GError ** error)4515 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4516 const gchar *value,
4517 GError **error)
4518 {
4519 gchar *value_utf8;
4520 gint i, length = 0;
4521
4522 /* Count the number of non-whitespace characters */
4523 for (i = 0; value[i]; i++)
4524 if (!g_ascii_isspace (value[i]))
4525 length = i + 1;
4526
4527 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4528 return TRUE;
4529 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4530 return FALSE;
4531
4532 value_utf8 = g_utf8_make_valid (value, -1);
4533 g_set_error (error, G_KEY_FILE_ERROR,
4534 G_KEY_FILE_ERROR_INVALID_VALUE,
4535 _("Value “%s” cannot be interpreted "
4536 "as a boolean."), value_utf8);
4537 g_free (value_utf8);
4538
4539 return FALSE;
4540 }
4541
4542 static gchar *
g_key_file_parse_boolean_as_value(GKeyFile * key_file,gboolean value)4543 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4544 gboolean value)
4545 {
4546 if (value)
4547 return g_strdup ("true");
4548 else
4549 return g_strdup ("false");
4550 }
4551
4552 static gchar *
g_key_file_parse_value_as_comment(GKeyFile * key_file,const gchar * value,gboolean is_final_line)4553 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4554 const gchar *value,
4555 gboolean is_final_line)
4556 {
4557 GString *string;
4558 gchar **lines;
4559 gsize i;
4560
4561 string = g_string_sized_new (512);
4562
4563 lines = g_strsplit (value, "\n", 0);
4564
4565 for (i = 0; lines[i] != NULL; i++)
4566 {
4567 const gchar *line = lines[i];
4568
4569 if (i != 0)
4570 g_string_append_c (string, '\n');
4571
4572 if (line[0] == '#')
4573 line++;
4574 g_string_append (string, line);
4575 }
4576 g_strfreev (lines);
4577
4578 /* This function gets called once per line of a comment, but we don’t want
4579 * to add a trailing newline. */
4580 if (!is_final_line)
4581 g_string_append_c (string, '\n');
4582
4583 return g_string_free (string, FALSE);
4584 }
4585
4586 static gchar *
g_key_file_parse_comment_as_value(GKeyFile * key_file,const gchar * comment)4587 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4588 const gchar *comment)
4589 {
4590 GString *string;
4591 gchar **lines;
4592 gsize i;
4593
4594 string = g_string_sized_new (512);
4595
4596 lines = g_strsplit (comment, "\n", 0);
4597
4598 for (i = 0; lines[i] != NULL; i++)
4599 g_string_append_printf (string, "#%s%s", lines[i],
4600 lines[i + 1] == NULL? "" : "\n");
4601 g_strfreev (lines);
4602
4603 return g_string_free (string, FALSE);
4604 }
4605
4606 /**
4607 * g_key_file_save_to_file:
4608 * @key_file: a #GKeyFile
4609 * @filename: the name of the file to write to
4610 * @error: a pointer to a %NULL #GError, or %NULL
4611 *
4612 * Writes the contents of @key_file to @filename using
4613 * g_file_set_contents().
4614 *
4615 * This function can fail for any of the reasons that
4616 * g_file_set_contents() may fail.
4617 *
4618 * Returns: %TRUE if successful, else %FALSE with @error set
4619 *
4620 * Since: 2.40
4621 */
4622 gboolean
g_key_file_save_to_file(GKeyFile * key_file,const gchar * filename,GError ** error)4623 g_key_file_save_to_file (GKeyFile *key_file,
4624 const gchar *filename,
4625 GError **error)
4626 {
4627 gchar *contents;
4628 gboolean success;
4629 gsize length;
4630
4631 g_return_val_if_fail (key_file != NULL, FALSE);
4632 g_return_val_if_fail (filename != NULL, FALSE);
4633 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4634
4635 contents = g_key_file_to_data (key_file, &length, NULL);
4636 g_assert (contents != NULL);
4637
4638 success = g_file_set_contents (filename, contents, length, error);
4639 g_free (contents);
4640
4641 return success;
4642 }
4643