1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
4 * Copyright 2009-2010 Collabora Ltd.
5 * Copyright 2009 Nokia Corporation
7 * Written by Ray Strode <rstrode@redhat.com>
8 * Matthias Clasen <mclasen@redhat.com>
10 * SPDX-License-Identifier: LGPL-2.1-or-later
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this library; if not, see <http://www.gnu.org/licenses/>.
37 #include <sys/types.h>
46 #define fstat(a,b) _fstati64(a,b)
51 #define S_ISREG(mode) ((mode)&_S_IFREG)
54 #endif /* G_OS_WIN23 */
59 #include "gfileutils.h"
65 #include "gmessages.h"
68 #include "gstrfuncs.h"
74 * @title: Key-value file parser
75 * @short_description: parses .ini-like config files
77 * #GKeyFile lets you parse, edit or create files containing groups of
78 * key-value pairs, which we call "key files" for lack of a better name.
79 * Several freedesktop.org specifications use key files now, e.g the
80 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
82 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
84 * The syntax of key files is described in detail in the
85 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
86 * here is a quick summary: Key files
87 * consists of groups of key-value pairs, interspersed with comments.
90 * # this is just an example
91 * # there can be comments before the first group
95 * Name=Key File Example\tthis value shows\nescaping
97 * # localized strings are stored in multiple key-value pairs
100 * Welcome[fr_FR]=Bonjour
102 * Welcome[be@latin]=Hello
106 * Numbers=2;20;-200;0
108 * Booleans=true;false;true;true
111 * Lines beginning with a '#' and blank lines are considered comments.
113 * Groups are started by a header line containing the group name enclosed
114 * in '[' and ']', and ended implicitly by the start of the next group or
115 * the end of the file. Each key-value pair must be contained in a group.
117 * Key-value pairs generally have the form `key=value`, with the
118 * exception of localized strings, which have the form
119 * `key[locale]=value`, with a locale identifier of the
120 * form `lang_COUNTRY@MODIFIER` where `COUNTRY` and `MODIFIER`
122 * Space before and after the '=' character are ignored. Newline, tab,
123 * carriage return and backslash characters in value are escaped as \n,
124 * \t, \r, and \\\\, respectively. To preserve leading spaces in values,
125 * these can also be escaped as \s.
127 * Key files can store strings (possibly with localized variants), integers,
128 * booleans and lists of these. Lists are separated by a separator character,
129 * typically ';' or ','. To use the list separator character in a value in
130 * a list, it has to be escaped by prefixing it with a backslash.
132 * This syntax is obviously inspired by the .ini files commonly met
133 * on Windows, but there are some important differences:
135 * - .ini files use the ';' character to begin comments,
136 * key files use the '#' character.
138 * - Key files do not allow for ungrouped keys meaning only
139 * comments can precede the first group.
141 * - Key files are always encoded in UTF-8.
143 * - Key and Group names are case-sensitive. For example, a group called
144 * [GROUP] is a different from [group].
146 * - .ini files don't have a strongly typed boolean entry type,
147 * they only have GetProfileInt(). In key files, only
148 * true and false (in lower case) are allowed.
150 * Note that in contrast to the
151 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
152 * groups in key files may contain the same
153 * key multiple times; the last entry wins. Key files may also contain
154 * multiple groups with the same name; they are merged together.
155 * Another difference is that keys and group names in key files are not
156 * restricted to ASCII characters.
158 * Here is an example of loading a key file and reading a value:
159 * |[<!-- language="C" -->
160 * g_autoptr(GError) error = NULL;
161 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
163 * if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
165 * if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
166 * g_warning ("Error loading key file: %s", error->message);
170 * g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
172 * !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
174 * g_warning ("Error finding key in key file: %s", error->message);
177 * else if (val == NULL)
179 * // Fall back to a default value.
180 * val = g_strdup ("default-value");
184 * Here is an example of creating and saving a key file:
185 * |[<!-- language="C" -->
186 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
187 * const gchar *val = …;
188 * g_autoptr(GError) error = NULL;
190 * g_key_file_set_string (key_file, "Group Name", "SomeKey", val);
193 * if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
195 * g_warning ("Error saving key file: %s", error->message);
199 * // Or store to a GBytes for use elsewhere.
201 * g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
204 * g_warning ("Error saving key file: %s", error->message);
207 * g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);
214 * Error domain for key file parsing. Errors in this domain will
215 * be from the #GKeyFileError enumeration.
217 * See #GError for information on error domains.
222 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
223 * an unknown encoding
224 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
225 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
226 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
227 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
228 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
230 * Error codes returned by key file parsing.
235 * @G_KEY_FILE_NONE: No flags, default behaviour
236 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
237 * (possibly modified) contents of the key file back to a file;
238 * otherwise all comments will be lost when the key file is
240 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
241 * (possibly modified) contents of the key file back to a file;
242 * otherwise only the translations for the current language will be
245 * Flags which influence the parsing.
249 * G_KEY_FILE_DESKTOP_GROUP:
251 * The name of the main group of a desktop entry file, as defined in the
252 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
253 * Consult the specification for more
254 * details about the meanings of the keys below.
260 * G_KEY_FILE_DESKTOP_KEY_TYPE:
262 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
263 * giving the type of the desktop entry.
265 * Usually %G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
266 * %G_KEY_FILE_DESKTOP_TYPE_LINK, or
267 * %G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
273 * G_KEY_FILE_DESKTOP_KEY_VERSION:
275 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
276 * giving the version of the Desktop Entry Specification used for
277 * the desktop entry file.
283 * G_KEY_FILE_DESKTOP_KEY_NAME:
285 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
286 * string giving the specific name of the desktop entry.
292 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
294 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
295 * string giving the generic name of the desktop entry.
301 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
303 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
304 * stating whether the desktop entry should be shown in menus.
310 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
312 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
313 * string giving the tooltip for the desktop entry.
319 * G_KEY_FILE_DESKTOP_KEY_ICON:
321 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
322 * string giving the name of the icon to be displayed for the desktop
329 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
331 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
332 * stating whether the desktop entry has been deleted by the user.
338 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
340 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
341 * strings identifying the environments that should display the
348 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
350 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
351 * strings identifying the environments that should not display the
358 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
360 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
361 * giving the file name of a binary on disk used to determine if the
362 * program is actually installed. It is only valid for desktop entries
363 * with the `Application` type.
369 * G_KEY_FILE_DESKTOP_KEY_EXEC:
371 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
372 * giving the command line to execute. It is only valid for desktop
373 * entries with the `Application` type.
379 * G_KEY_FILE_DESKTOP_KEY_PATH:
381 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
382 * containing the working directory to run the program in. It is only
383 * valid for desktop entries with the `Application` type.
389 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
391 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
392 * stating whether the program should be run in a terminal window.
394 * It is only valid for desktop entries with the `Application` type.
400 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
402 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list
403 * of strings giving the MIME types supported by this desktop entry.
409 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
411 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list
412 * of strings giving the categories in which the desktop entry
413 * should be shown in a menu.
419 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
421 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
422 * stating whether the application supports the
423 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
429 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
431 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is string
432 * identifying the WM class or name hint of a window that the application
433 * will create, which can be used to emulate Startup Notification with
434 * older applications.
440 * G_KEY_FILE_DESKTOP_KEY_URL:
442 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
443 * giving the URL to access. It is only valid for desktop entries
444 * with the `Link` type.
450 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
452 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
453 * set to true if the application is D-Bus activatable.
459 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
461 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
462 * giving the available application actions.
468 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
470 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
471 * entries representing applications.
477 * G_KEY_FILE_DESKTOP_TYPE_LINK:
479 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
480 * entries representing links to documents.
486 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
488 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
489 * entries representing directories.
494 typedef struct _GKeyFileGroup GKeyFileGroup;
499 * The GKeyFile struct contains only private data
500 * and should not be accessed directly.
505 GHashTable *group_hash;
507 GKeyFileGroup *start_group;
508 GKeyFileGroup *current_group;
510 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
512 gchar list_separator;
516 gboolean checked_locales; /* TRUE if @locales has been initialised */
517 gchar **locales; /* (nullable) */
519 gint ref_count; /* (atomic) */
522 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
524 struct _GKeyFileGroup
526 const gchar *name; /* NULL for above first group (which will be comments) */
528 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
530 GList *key_value_pairs;
532 /* Used in parallel with key_value_pairs for
533 * increased lookup performance
535 GHashTable *lookup_map;
538 struct _GKeyFileKeyValuePair
540 gchar *key; /* NULL for comments */
544 static gint find_file_in_data_dirs (const gchar *file,
545 const gchar **data_dirs,
548 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
552 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
553 const gchar *group_name);
554 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
555 const gchar *group_name);
557 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
558 GKeyFileGroup *group,
560 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
561 GKeyFileGroup *group,
564 static void g_key_file_remove_group_node (GKeyFile *key_file,
566 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
567 GKeyFileGroup *group,
570 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
571 GKeyFileGroup *group,
572 GKeyFileKeyValuePair *pair);
573 static void g_key_file_add_key (GKeyFile *key_file,
574 GKeyFileGroup *group,
577 static void g_key_file_add_group (GKeyFile *key_file,
578 const gchar *group_name);
579 static gboolean g_key_file_is_group_name (const gchar *name);
580 static gboolean g_key_file_is_key_name (const gchar *name,
582 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
583 static gboolean g_key_file_line_is_comment (const gchar *line);
584 static gboolean g_key_file_line_is_group (const gchar *line);
585 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
586 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
590 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
592 gboolean escape_separator);
593 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
596 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
598 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
601 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
604 static const gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
606 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
608 gboolean is_final_line);
609 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
610 const gchar *comment);
611 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
615 static void g_key_file_parse_comment (GKeyFile *key_file,
619 static void g_key_file_parse_group (GKeyFile *key_file,
623 static const gchar *key_get_locale (const gchar *key,
625 static void g_key_file_parse_data (GKeyFile *key_file,
629 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
632 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
635 g_key_file_init (GKeyFile *key_file)
637 key_file->current_group = g_slice_new0 (GKeyFileGroup);
638 key_file->groups = g_list_prepend (NULL, key_file->current_group);
639 key_file->group_hash = NULL;
640 key_file->start_group = NULL;
641 key_file->parse_buffer = NULL;
642 key_file->list_separator = ';';
647 g_key_file_clear (GKeyFile *key_file)
649 GList *tmp, *group_node;
651 if (key_file->locales)
653 g_strfreev (key_file->locales);
654 key_file->locales = NULL;
656 key_file->checked_locales = FALSE;
658 if (key_file->parse_buffer)
660 g_string_free (key_file->parse_buffer, TRUE);
661 key_file->parse_buffer = NULL;
664 tmp = key_file->groups;
669 g_key_file_remove_group_node (key_file, group_node);
672 if (key_file->group_hash != NULL)
674 g_hash_table_destroy (key_file->group_hash);
675 key_file->group_hash = NULL;
678 g_warn_if_fail (key_file->groups == NULL);
685 * Creates a new empty #GKeyFile object. Use
686 * g_key_file_load_from_file(), g_key_file_load_from_data(),
687 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
688 * read an existing key file.
690 * Returns: (transfer full): an empty #GKeyFile.
695 g_key_file_new (void)
699 key_file = g_slice_new0 (GKeyFile);
700 key_file->ref_count = 1;
701 g_key_file_init (key_file);
707 * g_key_file_set_list_separator:
708 * @key_file: a #GKeyFile
709 * @separator: the separator
711 * Sets the character which is used to separate
712 * values in lists. Typically ';' or ',' are used
713 * as separators. The default list separator is ';'.
718 g_key_file_set_list_separator (GKeyFile *key_file,
721 g_return_if_fail (key_file != NULL);
723 key_file->list_separator = separator;
727 /* Iterates through all the directories in *dirs trying to
728 * open file. When it successfully locates and opens a file it
729 * returns the file descriptor to the open file. It also
730 * outputs the absolute path of the file in output_file.
733 find_file_in_data_dirs (const gchar *file,
738 const gchar **data_dirs, *data_dir;
750 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
752 const gchar *candidate_file;
755 candidate_file = file;
756 sub_dir = g_strdup ("");
757 while (candidate_file != NULL && fd == -1)
761 path = g_build_filename (data_dir, sub_dir,
762 candidate_file, NULL);
764 fd = g_open (path, O_RDONLY, 0);
772 candidate_file = strchr (candidate_file, '-');
774 if (candidate_file == NULL)
780 sub_dir = g_strndup (file, candidate_file - file - 1);
782 for (p = sub_dir; *p != '\0'; p++)
785 *p = G_DIR_SEPARATOR;
794 g_set_error_literal (error, G_KEY_FILE_ERROR,
795 G_KEY_FILE_ERROR_NOT_FOUND,
796 _("Valid key file could not be "
797 "found in search dirs"));
800 if (output_file != NULL && fd != -1)
801 *output_file = g_strdup (path);
809 g_key_file_load_from_fd (GKeyFile *key_file,
814 GError *key_file_error = NULL;
816 struct stat stat_buf;
817 gchar read_buf[4096];
818 gchar list_separator;
820 if (fstat (fd, &stat_buf) < 0)
823 g_set_error_literal (error, G_FILE_ERROR,
824 g_file_error_from_errno (errsv),
829 if (!S_ISREG (stat_buf.st_mode))
831 g_set_error_literal (error, G_KEY_FILE_ERROR,
832 G_KEY_FILE_ERROR_PARSE,
833 _("Not a regular file"));
837 list_separator = key_file->list_separator;
838 g_key_file_clear (key_file);
839 g_key_file_init (key_file);
840 key_file->list_separator = list_separator;
841 key_file->flags = flags;
847 bytes_read = read (fd, read_buf, 4096);
850 if (bytes_read == 0) /* End of File */
855 if (errsv == EINTR || errsv == EAGAIN)
858 g_set_error_literal (error, G_FILE_ERROR,
859 g_file_error_from_errno (errsv),
864 g_key_file_parse_data (key_file,
865 read_buf, bytes_read,
868 while (!key_file_error);
872 g_propagate_error (error, key_file_error);
876 g_key_file_flush_parse_buffer (key_file, &key_file_error);
880 g_propagate_error (error, key_file_error);
888 * g_key_file_load_from_file:
889 * @key_file: an empty #GKeyFile struct
890 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
891 * @flags: flags from #GKeyFileFlags
892 * @error: return location for a #GError, or %NULL
894 * Loads a key file into an empty #GKeyFile structure.
896 * If the OS returns an error when opening or reading the file, a
897 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
898 * %G_KEY_FILE_ERROR is returned.
900 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
901 * @file is not found, %G_FILE_ERROR_NOENT is returned.
903 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
908 g_key_file_load_from_file (GKeyFile *key_file,
913 GError *key_file_error = NULL;
917 g_return_val_if_fail (key_file != NULL, FALSE);
918 g_return_val_if_fail (file != NULL, FALSE);
920 fd = g_open (file, O_RDONLY, 0);
925 g_set_error_literal (error, G_FILE_ERROR,
926 g_file_error_from_errno (errsv),
931 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
936 g_propagate_error (error, key_file_error);
944 * g_key_file_load_from_data:
945 * @key_file: an empty #GKeyFile struct
946 * @data: key file loaded in memory
947 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
948 * @flags: flags from #GKeyFileFlags
949 * @error: return location for a #GError, or %NULL
951 * Loads a key file from memory into an empty #GKeyFile structure.
952 * If the object cannot be created then %error is set to a #GKeyFileError.
954 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
959 g_key_file_load_from_data (GKeyFile *key_file,
965 GError *key_file_error = NULL;
966 gchar list_separator;
968 g_return_val_if_fail (key_file != NULL, FALSE);
969 g_return_val_if_fail (data != NULL || length == 0, FALSE);
971 if (length == (gsize)-1)
972 length = strlen (data);
974 list_separator = key_file->list_separator;
975 g_key_file_clear (key_file);
976 g_key_file_init (key_file);
977 key_file->list_separator = list_separator;
978 key_file->flags = flags;
980 g_key_file_parse_data (key_file, data, length, &key_file_error);
984 g_propagate_error (error, key_file_error);
988 g_key_file_flush_parse_buffer (key_file, &key_file_error);
992 g_propagate_error (error, key_file_error);
1000 * g_key_file_load_from_bytes:
1001 * @key_file: an empty #GKeyFile struct
1003 * @flags: flags from #GKeyFileFlags
1004 * @error: return location for a #GError, or %NULL
1006 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
1007 * If the object cannot be created then %error is set to a #GKeyFileError.
1009 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1014 g_key_file_load_from_bytes (GKeyFile *key_file,
1016 GKeyFileFlags flags,
1022 g_return_val_if_fail (key_file != NULL, FALSE);
1023 g_return_val_if_fail (bytes != NULL, FALSE);
1025 data = g_bytes_get_data (bytes, &size);
1026 return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
1030 * g_key_file_load_from_dirs:
1031 * @key_file: an empty #GKeyFile struct
1032 * @file: (type filename): a relative path to a filename to open and parse
1033 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
1034 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1035 * of the file, or %NULL
1036 * @flags: flags from #GKeyFileFlags
1037 * @error: return location for a #GError, or %NULL
1039 * This function looks for a key file named @file in the paths
1040 * specified in @search_dirs, loads the file into @key_file and
1041 * returns the file's full path in @full_path.
1043 * If the file could not be found in any of the @search_dirs,
1044 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
1045 * the file is found but the OS returns an error when opening or reading the
1046 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
1047 * %G_KEY_FILE_ERROR is returned.
1049 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1054 g_key_file_load_from_dirs (GKeyFile *key_file,
1056 const gchar **search_dirs,
1058 GKeyFileFlags flags,
1061 GError *key_file_error = NULL;
1062 const gchar **data_dirs;
1065 gboolean found_file;
1067 g_return_val_if_fail (key_file != NULL, FALSE);
1068 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1069 g_return_val_if_fail (search_dirs != NULL, FALSE);
1072 data_dirs = search_dirs;
1074 while (*data_dirs != NULL && !found_file)
1076 g_free (output_path);
1079 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
1085 g_propagate_error (error, key_file_error);
1089 found_file = g_key_file_load_from_fd (key_file, fd, flags,
1095 g_propagate_error (error, key_file_error);
1100 if (found_file && full_path)
1101 *full_path = output_path;
1103 g_free (output_path);
1109 * g_key_file_load_from_data_dirs:
1110 * @key_file: an empty #GKeyFile struct
1111 * @file: (type filename): a relative path to a filename to open and parse
1112 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1113 * of the file, or %NULL
1114 * @flags: flags from #GKeyFileFlags
1115 * @error: return location for a #GError, or %NULL
1117 * This function looks for a key file named @file in the paths
1118 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1119 * loads the file into @key_file and returns the file's full path in
1120 * @full_path. If the file could not be loaded then an %error is
1121 * set to either a #GFileError or #GKeyFileError.
1123 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1127 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1130 GKeyFileFlags flags,
1133 gchar **all_data_dirs;
1134 const gchar * user_data_dir;
1135 const gchar * const * system_data_dirs;
1137 gboolean found_file;
1139 g_return_val_if_fail (key_file != NULL, FALSE);
1140 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1142 user_data_dir = g_get_user_data_dir ();
1143 system_data_dirs = g_get_system_data_dirs ();
1144 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1147 all_data_dirs[i++] = g_strdup (user_data_dir);
1150 while (system_data_dirs[j] != NULL)
1151 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1152 all_data_dirs[i] = NULL;
1154 found_file = g_key_file_load_from_dirs (key_file,
1156 (const gchar **)all_data_dirs,
1161 g_strfreev (all_data_dirs);
1167 * g_key_file_ref: (skip)
1168 * @key_file: a #GKeyFile
1170 * Increases the reference count of @key_file.
1172 * Returns: the same @key_file.
1177 g_key_file_ref (GKeyFile *key_file)
1179 g_return_val_if_fail (key_file != NULL, NULL);
1181 g_atomic_int_inc (&key_file->ref_count);
1187 * g_key_file_free: (skip)
1188 * @key_file: a #GKeyFile
1190 * Clears all keys and groups from @key_file, and decreases the
1191 * reference count by 1. If the reference count reaches zero,
1192 * frees the key file and all its allocated memory.
1197 g_key_file_free (GKeyFile *key_file)
1199 g_return_if_fail (key_file != NULL);
1201 g_key_file_clear (key_file);
1203 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1204 g_slice_free (GKeyFile, key_file);
1206 g_key_file_init (key_file);
1211 * @key_file: a #GKeyFile
1213 * Decreases the reference count of @key_file by 1. If the reference count
1214 * reaches zero, frees the key file and all its allocated memory.
1219 g_key_file_unref (GKeyFile *key_file)
1221 g_return_if_fail (key_file != NULL);
1223 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1225 g_key_file_clear (key_file);
1226 g_slice_free (GKeyFile, key_file);
1230 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1231 * true for locales that match those in g_get_language_names().
1234 g_key_file_locale_is_interesting (GKeyFile *key_file,
1235 const gchar *locale,
1240 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1243 if (!key_file->checked_locales)
1245 g_assert (key_file->locales == NULL);
1246 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
1247 key_file->checked_locales = TRUE;
1250 for (i = 0; key_file->locales[i] != NULL; i++)
1252 if (g_ascii_strncasecmp (key_file->locales[i], locale, locale_len) == 0 &&
1253 key_file->locales[i][locale_len] == '\0')
1261 g_key_file_parse_line (GKeyFile *key_file,
1266 GError *parse_error = NULL;
1267 const gchar *line_start;
1269 g_return_if_fail (key_file != NULL);
1270 g_return_if_fail (line != NULL);
1273 while (g_ascii_isspace (*line_start))
1276 if (g_key_file_line_is_comment (line_start))
1277 g_key_file_parse_comment (key_file, line, length, &parse_error);
1278 else if (g_key_file_line_is_group (line_start))
1279 g_key_file_parse_group (key_file, line_start,
1280 length - (line_start - line),
1282 else if (g_key_file_line_is_key_value_pair (line_start))
1283 g_key_file_parse_key_value_pair (key_file, line_start,
1284 length - (line_start - line),
1288 gchar *line_utf8 = g_utf8_make_valid (line, length);
1289 g_set_error (error, G_KEY_FILE_ERROR,
1290 G_KEY_FILE_ERROR_PARSE,
1291 _("Key file contains line “%s” which is not "
1292 "a key-value pair, group, or comment"),
1300 g_propagate_error (error, parse_error);
1304 g_key_file_parse_comment (GKeyFile *key_file,
1309 GKeyFileKeyValuePair *pair;
1311 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1314 g_warn_if_fail (key_file->current_group != NULL);
1316 pair = g_slice_new (GKeyFileKeyValuePair);
1318 pair->value = g_strndup (line, length);
1320 key_file->current_group->key_value_pairs =
1321 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1325 g_key_file_parse_group (GKeyFile *key_file,
1331 const gchar *group_name_start, *group_name_end;
1333 /* advance past opening '['
1335 group_name_start = line + 1;
1336 group_name_end = line + length - 1;
1338 while (*group_name_end != ']')
1341 group_name = g_strndup (group_name_start,
1342 group_name_end - group_name_start);
1344 if (!g_key_file_is_group_name (group_name))
1346 g_set_error (error, G_KEY_FILE_ERROR,
1347 G_KEY_FILE_ERROR_PARSE,
1348 _("Invalid group name: %s"), group_name);
1349 g_free (group_name);
1353 g_key_file_add_group (key_file, group_name);
1354 g_free (group_name);
1358 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1363 gchar *key, *key_end, *value_start;
1364 const gchar *locale;
1366 gsize key_len, value_len;
1368 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1370 g_set_error_literal (error, G_KEY_FILE_ERROR,
1371 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1372 _("Key file does not start with a group"));
1376 key_end = value_start = strchr (line, '=');
1378 g_warn_if_fail (key_end != NULL);
1383 /* Pull the key name from the line (chomping trailing whitespace)
1385 while (g_ascii_isspace (*key_end))
1388 key_len = key_end - line + 2;
1390 g_warn_if_fail (key_len <= length);
1392 if (!g_key_file_is_key_name (line, key_len - 1))
1394 g_set_error (error, G_KEY_FILE_ERROR,
1395 G_KEY_FILE_ERROR_PARSE,
1396 _("Invalid key name: %.*s"), (int) key_len - 1, line);
1400 key = g_strndup (line, key_len - 1);
1402 /* Pull the value from the line (chugging leading whitespace)
1404 while (g_ascii_isspace (*value_start))
1407 value_len = line + length - value_start;
1409 g_warn_if_fail (key_file->start_group != NULL);
1411 /* Checked on entry to this function */
1412 g_assert (key_file->current_group != NULL);
1413 g_assert (key_file->current_group->name != NULL);
1415 if (key_file->start_group == key_file->current_group
1416 && strcmp (key, "Encoding") == 0)
1418 if (value_len != strlen ("UTF-8") ||
1419 g_ascii_strncasecmp (value_start, "UTF-8", value_len) != 0)
1421 gchar *value_utf8 = g_utf8_make_valid (value_start, value_len);
1422 g_set_error (error, G_KEY_FILE_ERROR,
1423 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1424 _("Key file contains unsupported "
1425 "encoding “%s”"), value_utf8);
1426 g_free (value_utf8);
1433 /* Is this key a translation? If so, is it one that we care about?
1435 locale = key_get_locale (key, &locale_len);
1437 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale, locale_len))
1439 GKeyFileKeyValuePair *pair;
1441 pair = g_slice_new (GKeyFileKeyValuePair);
1442 pair->key = g_steal_pointer (&key);
1443 pair->value = g_strndup (value_start, value_len);
1445 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1451 static const gchar *
1452 key_get_locale (const gchar *key,
1455 const gchar *locale;
1458 locale = g_strrstr (key, "[");
1460 locale_len = strlen (locale);
1466 locale++; /* skip `[` */
1467 locale_len -= 2; /* drop `[` and `]` */
1475 *len_out = locale_len;
1480 g_key_file_parse_data (GKeyFile *key_file,
1485 GError *parse_error;
1488 g_return_if_fail (key_file != NULL);
1489 g_return_if_fail (data != NULL || length == 0);
1493 if (!key_file->parse_buffer)
1494 key_file->parse_buffer = g_string_sized_new (128);
1499 if (data[i] == '\n')
1501 if (key_file->parse_buffer->len > 0
1502 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1504 g_string_erase (key_file->parse_buffer,
1505 key_file->parse_buffer->len - 1,
1508 /* When a newline is encountered flush the parse buffer so that the
1509 * line can be parsed. Note that completely blank lines won't show
1510 * up in the parse buffer, so they get parsed directly.
1512 if (key_file->parse_buffer->len > 0)
1513 g_key_file_flush_parse_buffer (key_file, &parse_error);
1515 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1519 g_propagate_error (error, parse_error);
1526 const gchar *start_of_line;
1527 const gchar *end_of_line;
1530 start_of_line = data + i;
1531 end_of_line = memchr (start_of_line, '\n', length - i);
1533 if (end_of_line == NULL)
1534 end_of_line = data + length;
1536 line_length = end_of_line - start_of_line;
1538 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1545 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1548 GError *file_error = NULL;
1550 g_return_if_fail (key_file != NULL);
1552 if (!key_file->parse_buffer)
1557 if (key_file->parse_buffer->len > 0)
1559 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1560 key_file->parse_buffer->len,
1562 g_string_erase (key_file->parse_buffer, 0, -1);
1566 g_propagate_error (error, file_error);
1573 * g_key_file_to_data:
1574 * @key_file: a #GKeyFile
1575 * @length: (out) (optional): return location for the length of the
1576 * returned string, or %NULL
1577 * @error: return location for a #GError, or %NULL
1579 * This function outputs @key_file as a string.
1581 * Note that this function never reports an error,
1582 * so it is safe to pass %NULL as @error.
1584 * Returns: a newly allocated string holding
1585 * the contents of the #GKeyFile
1590 g_key_file_to_data (GKeyFile *key_file,
1594 GString *data_string;
1595 GList *group_node, *key_file_node;
1597 g_return_val_if_fail (key_file != NULL, NULL);
1599 data_string = g_string_new (NULL);
1601 for (group_node = g_list_last (key_file->groups);
1603 group_node = group_node->prev)
1605 GKeyFileGroup *group;
1607 group = (GKeyFileGroup *) group_node->data;
1609 /* separate groups by at least an empty line */
1610 if (data_string->len >= 2 &&
1611 data_string->str[data_string->len - 2] != '\n')
1612 g_string_append_c (data_string, '\n');
1614 if (group->comment != NULL)
1615 g_string_append_printf (data_string, "%s\n", group->comment->value);
1617 if (group->name != NULL)
1618 g_string_append_printf (data_string, "[%s]\n", group->name);
1620 for (key_file_node = g_list_last (group->key_value_pairs);
1621 key_file_node != NULL;
1622 key_file_node = key_file_node->prev)
1624 GKeyFileKeyValuePair *pair;
1626 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1628 if (pair->key != NULL)
1629 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1631 g_string_append_printf (data_string, "%s\n", pair->value);
1636 *length = data_string->len;
1638 return g_string_free (data_string, FALSE);
1642 * g_key_file_get_keys:
1643 * @key_file: a #GKeyFile
1644 * @group_name: a group name
1645 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1646 * @error: return location for a #GError, or %NULL
1648 * Returns all keys for the group name @group_name. The array of
1649 * returned keys will be %NULL-terminated, so @length may
1650 * optionally be %NULL. In the event that the @group_name cannot
1651 * be found, %NULL is returned and @error is set to
1652 * %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1654 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1655 * Use g_strfreev() to free it.
1660 g_key_file_get_keys (GKeyFile *key_file,
1661 const gchar *group_name,
1665 GKeyFileGroup *group;
1670 g_return_val_if_fail (key_file != NULL, NULL);
1671 g_return_val_if_fail (group_name != NULL, NULL);
1673 group = g_key_file_lookup_group (key_file, group_name);
1677 g_set_error (error, G_KEY_FILE_ERROR,
1678 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1679 _("Key file does not have group “%s”"),
1685 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1687 GKeyFileKeyValuePair *pair;
1689 pair = (GKeyFileKeyValuePair *) tmp->data;
1695 keys = g_new (gchar *, num_keys + 1);
1698 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1700 GKeyFileKeyValuePair *pair;
1702 pair = (GKeyFileKeyValuePair *) tmp->data;
1706 keys[i] = g_strdup (pair->key);
1711 keys[num_keys] = NULL;
1720 * g_key_file_get_start_group:
1721 * @key_file: a #GKeyFile
1723 * Returns the name of the start group of the file.
1725 * Returns: (nullable): The start group of the key file.
1730 g_key_file_get_start_group (GKeyFile *key_file)
1732 g_return_val_if_fail (key_file != NULL, NULL);
1734 if (key_file->start_group)
1735 return g_strdup (key_file->start_group->name);
1741 * g_key_file_get_groups:
1742 * @key_file: a #GKeyFile
1743 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1745 * Returns all groups in the key file loaded with @key_file.
1746 * The array of returned groups will be %NULL-terminated, so
1747 * @length may optionally be %NULL.
1749 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1750 * Use g_strfreev() to free it.
1754 g_key_file_get_groups (GKeyFile *key_file,
1759 gsize i, num_groups;
1761 g_return_val_if_fail (key_file != NULL, NULL);
1763 num_groups = g_list_length (key_file->groups);
1765 g_return_val_if_fail (num_groups > 0, NULL);
1767 group_node = g_list_last (key_file->groups);
1769 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1771 /* Only need num_groups instead of num_groups + 1
1772 * because the first group of the file (last in the
1773 * list) is always the comment group at the top,
1776 groups = g_new (gchar *, num_groups);
1780 for (group_node = group_node->prev;
1782 group_node = group_node->prev)
1784 GKeyFileGroup *group;
1786 group = (GKeyFileGroup *) group_node->data;
1788 g_warn_if_fail (group->name != NULL);
1790 groups[i++] = g_strdup (group->name);
1801 set_not_found_key_error (const char *group_name,
1805 g_set_error (error, G_KEY_FILE_ERROR,
1806 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1807 _("Key file does not have key “%s” in group “%s”"),
1812 * g_key_file_get_value:
1813 * @key_file: a #GKeyFile
1814 * @group_name: a group name
1816 * @error: return location for a #GError, or %NULL
1818 * Returns the raw value associated with @key under @group_name.
1819 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1821 * In the event the key cannot be found, %NULL is returned and
1822 * @error is set to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1823 * event that the @group_name cannot be found, %NULL is returned
1824 * and @error is set to %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1827 * Returns: a newly allocated string or %NULL if the specified
1828 * key cannot be found.
1833 g_key_file_get_value (GKeyFile *key_file,
1834 const gchar *group_name,
1838 GKeyFileGroup *group;
1839 GKeyFileKeyValuePair *pair;
1840 gchar *value = NULL;
1842 g_return_val_if_fail (key_file != NULL, NULL);
1843 g_return_val_if_fail (group_name != NULL, NULL);
1844 g_return_val_if_fail (key != NULL, NULL);
1846 group = g_key_file_lookup_group (key_file, group_name);
1850 g_set_error (error, G_KEY_FILE_ERROR,
1851 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1852 _("Key file does not have group “%s”"),
1857 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1860 value = g_strdup (pair->value);
1862 set_not_found_key_error (group_name, key, error);
1868 * g_key_file_set_value:
1869 * @key_file: a #GKeyFile
1870 * @group_name: a group name
1874 * Associates a new value with @key under @group_name.
1876 * If @key cannot be found then it is created. If @group_name cannot
1877 * be found then it is created. To set an UTF-8 string which may contain
1878 * characters that need escaping (such as newlines or spaces), use
1879 * g_key_file_set_string().
1884 g_key_file_set_value (GKeyFile *key_file,
1885 const gchar *group_name,
1889 GKeyFileGroup *group;
1890 GKeyFileKeyValuePair *pair;
1892 g_return_if_fail (key_file != NULL);
1893 g_return_if_fail (group_name != NULL && g_key_file_is_group_name (group_name));
1894 g_return_if_fail (key != NULL && g_key_file_is_key_name (key, strlen (key)));
1895 g_return_if_fail (value != NULL);
1897 group = g_key_file_lookup_group (key_file, group_name);
1901 g_key_file_add_group (key_file, group_name);
1902 group = (GKeyFileGroup *) key_file->groups->data;
1904 g_key_file_add_key (key_file, group, key, value);
1908 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1911 g_key_file_add_key (key_file, group, key, value);
1914 g_free (pair->value);
1915 pair->value = g_strdup (value);
1921 * g_key_file_get_string:
1922 * @key_file: a #GKeyFile
1923 * @group_name: a group name
1925 * @error: return location for a #GError, or %NULL
1927 * Returns the string value associated with @key under @group_name.
1928 * Unlike g_key_file_get_value(), this function handles escape sequences
1931 * In the event the key cannot be found, %NULL is returned and
1932 * @error is set to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1933 * event that the @group_name cannot be found, %NULL is returned
1934 * and @error is set to %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1936 * Returns: a newly allocated string or %NULL if the specified
1937 * key cannot be found.
1942 g_key_file_get_string (GKeyFile *key_file,
1943 const gchar *group_name,
1947 gchar *value, *string_value;
1948 GError *key_file_error;
1950 g_return_val_if_fail (key_file != NULL, NULL);
1951 g_return_val_if_fail (group_name != NULL, NULL);
1952 g_return_val_if_fail (key != NULL, NULL);
1954 key_file_error = NULL;
1956 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1960 g_propagate_error (error, key_file_error);
1964 if (!g_utf8_validate (value, -1, NULL))
1966 gchar *value_utf8 = g_utf8_make_valid (value, -1);
1967 g_set_error (error, G_KEY_FILE_ERROR,
1968 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1969 _("Key file contains key “%s” with value “%s” "
1970 "which is not UTF-8"), key, value_utf8);
1971 g_free (value_utf8);
1977 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1983 if (g_error_matches (key_file_error,
1985 G_KEY_FILE_ERROR_INVALID_VALUE))
1987 g_set_error (error, G_KEY_FILE_ERROR,
1988 G_KEY_FILE_ERROR_INVALID_VALUE,
1989 _("Key file contains key “%s” "
1990 "which has a value that cannot be interpreted."),
1992 g_error_free (key_file_error);
1995 g_propagate_error (error, key_file_error);
1998 return string_value;
2002 * g_key_file_set_string:
2003 * @key_file: a #GKeyFile
2004 * @group_name: a group name
2008 * Associates a new string value with @key under @group_name.
2009 * If @key cannot be found then it is created.
2010 * If @group_name cannot be found then it is created.
2011 * Unlike g_key_file_set_value(), this function handles characters
2012 * that need escaping, such as newlines.
2017 g_key_file_set_string (GKeyFile *key_file,
2018 const gchar *group_name,
2020 const gchar *string)
2024 g_return_if_fail (key_file != NULL);
2025 g_return_if_fail (string != NULL);
2027 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2028 g_key_file_set_value (key_file, group_name, key, value);
2033 * g_key_file_get_string_list:
2034 * @key_file: a #GKeyFile
2035 * @group_name: a group name
2037 * @length: (out) (optional): return location for the number of returned strings, or %NULL
2038 * @error: return location for a #GError, or %NULL
2040 * Returns the values associated with @key under @group_name.
2042 * In the event the key cannot be found, %NULL is returned and
2043 * @error is set to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
2044 * event that the @group_name cannot be found, %NULL is returned
2045 * and @error is set to %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
2047 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2048 * a %NULL-terminated string array or %NULL if the specified
2049 * key cannot be found. The array should be freed with g_strfreev().
2054 g_key_file_get_string_list (GKeyFile *key_file,
2055 const gchar *group_name,
2060 GError *key_file_error = NULL;
2061 gchar *value, *string_value, **values;
2063 GSList *p, *pieces = NULL;
2065 g_return_val_if_fail (key_file != NULL, NULL);
2066 g_return_val_if_fail (group_name != NULL, NULL);
2067 g_return_val_if_fail (key != NULL, NULL);
2072 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2076 g_propagate_error (error, key_file_error);
2080 if (!g_utf8_validate (value, -1, NULL))
2082 gchar *value_utf8 = g_utf8_make_valid (value, -1);
2083 g_set_error (error, G_KEY_FILE_ERROR,
2084 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
2085 _("Key file contains key “%s” with value “%s” "
2086 "which is not UTF-8"), key, value_utf8);
2087 g_free (value_utf8);
2093 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
2095 g_free (string_value);
2099 if (g_error_matches (key_file_error,
2101 G_KEY_FILE_ERROR_INVALID_VALUE))
2103 g_set_error (error, G_KEY_FILE_ERROR,
2104 G_KEY_FILE_ERROR_INVALID_VALUE,
2105 _("Key file contains key “%s” "
2106 "which has a value that cannot be interpreted."),
2108 g_error_free (key_file_error);
2111 g_propagate_error (error, key_file_error);
2113 g_slist_free_full (pieces, g_free);
2117 len = g_slist_length (pieces);
2118 values = g_new (gchar *, len + 1);
2119 for (p = pieces, i = 0; p; p = p->next)
2120 values[i++] = p->data;
2123 g_slist_free (pieces);
2132 * g_key_file_set_string_list:
2133 * @key_file: a #GKeyFile
2134 * @group_name: a group name
2136 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2137 * @length: number of string values in @list
2139 * Associates a list of string values for @key under @group_name.
2140 * If @key cannot be found then it is created.
2141 * If @group_name cannot be found then it is created.
2146 g_key_file_set_string_list (GKeyFile *key_file,
2147 const gchar *group_name,
2149 const gchar * const list[],
2152 GString *value_list;
2155 g_return_if_fail (key_file != NULL);
2156 g_return_if_fail (list != NULL || length == 0);
2158 value_list = g_string_sized_new (length * 128);
2159 for (i = 0; i < length && list[i] != NULL; i++)
2163 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2164 g_string_append (value_list, value);
2165 g_string_append_c (value_list, key_file->list_separator);
2170 g_key_file_set_value (key_file, group_name, key, value_list->str);
2171 g_string_free (value_list, TRUE);
2175 * g_key_file_set_locale_string:
2176 * @key_file: a #GKeyFile
2177 * @group_name: a group name
2179 * @locale: a locale identifier
2182 * Associates a string value for @key and @locale under @group_name.
2183 * If the translation for @key cannot be found then it is created.
2188 g_key_file_set_locale_string (GKeyFile *key_file,
2189 const gchar *group_name,
2191 const gchar *locale,
2192 const gchar *string)
2194 gchar *full_key, *value;
2196 g_return_if_fail (key_file != NULL);
2197 g_return_if_fail (key != NULL);
2198 g_return_if_fail (locale != NULL);
2199 g_return_if_fail (string != NULL);
2201 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2202 full_key = g_strdup_printf ("%s[%s]", key, locale);
2203 g_key_file_set_value (key_file, group_name, full_key, value);
2209 * g_key_file_get_locale_string:
2210 * @key_file: a #GKeyFile
2211 * @group_name: a group name
2213 * @locale: (nullable): a locale identifier or %NULL
2214 * @error: return location for a #GError, or %NULL
2216 * Returns the value associated with @key under @group_name
2217 * translated in the given @locale if available. If @locale is
2218 * %NULL then the current locale is assumed.
2220 * If @locale is to be non-%NULL, or if the current locale will change over
2221 * the lifetime of the #GKeyFile, it must be loaded with
2222 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2224 * If @key cannot be found then %NULL is returned and @error is set
2225 * to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2226 * with @key cannot be interpreted or no suitable translation can
2227 * be found then the untranslated value is returned.
2229 * Returns: a newly allocated string or %NULL if the specified
2230 * key cannot be found.
2235 g_key_file_get_locale_string (GKeyFile *key_file,
2236 const gchar *group_name,
2238 const gchar *locale,
2241 gchar *candidate_key, *translated_value;
2242 GError *key_file_error;
2244 gboolean free_languages = FALSE;
2247 g_return_val_if_fail (key_file != NULL, NULL);
2248 g_return_val_if_fail (group_name != NULL, NULL);
2249 g_return_val_if_fail (key != NULL, NULL);
2251 candidate_key = NULL;
2252 translated_value = NULL;
2253 key_file_error = NULL;
2257 languages = g_get_locale_variants (locale);
2258 free_languages = TRUE;
2262 languages = (gchar **) g_get_language_names ();
2263 free_languages = FALSE;
2266 for (i = 0; languages[i]; i++)
2268 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2270 translated_value = g_key_file_get_string (key_file,
2272 candidate_key, NULL);
2273 g_free (candidate_key);
2275 if (translated_value)
2279 /* Fallback to untranslated key
2281 if (!translated_value)
2283 translated_value = g_key_file_get_string (key_file, group_name, key,
2286 if (!translated_value)
2287 g_propagate_error (error, key_file_error);
2291 g_strfreev (languages);
2293 return translated_value;
2297 * g_key_file_get_locale_for_key:
2298 * @key_file: a #GKeyFile
2299 * @group_name: a group name
2301 * @locale: (nullable): a locale identifier or %NULL
2303 * Returns the actual locale which the result of
2304 * g_key_file_get_locale_string() or g_key_file_get_locale_string_list()
2307 * If calling g_key_file_get_locale_string() or
2308 * g_key_file_get_locale_string_list() with exactly the same @key_file,
2309 * @group_name, @key and @locale, the result of those functions will
2310 * have originally been tagged with the locale that is the result of
2313 * Returns: (nullable): the locale from the file, or %NULL if the key was not
2314 * found or the entry in the file was was untranslated
2319 g_key_file_get_locale_for_key (GKeyFile *key_file,
2320 const gchar *group_name,
2322 const gchar *locale)
2324 gchar **languages_allocated = NULL;
2325 const gchar * const *languages;
2326 gchar *result = NULL;
2329 g_return_val_if_fail (key_file != NULL, NULL);
2330 g_return_val_if_fail (group_name != NULL, NULL);
2331 g_return_val_if_fail (key != NULL, NULL);
2335 languages_allocated = g_get_locale_variants (locale);
2336 languages = (const gchar * const *) languages_allocated;
2339 languages = g_get_language_names ();
2341 for (i = 0; languages[i] != NULL; i++)
2343 gchar *candidate_key, *translated_value;
2345 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2346 translated_value = g_key_file_get_string (key_file, group_name, candidate_key, NULL);
2347 g_free (translated_value);
2348 g_free (candidate_key);
2350 if (translated_value != NULL)
2354 result = g_strdup (languages[i]);
2356 g_strfreev (languages_allocated);
2362 * g_key_file_get_locale_string_list:
2363 * @key_file: a #GKeyFile
2364 * @group_name: a group name
2366 * @locale: (nullable): a locale identifier or %NULL
2367 * @length: (out) (optional): return location for the number of returned strings or %NULL
2368 * @error: return location for a #GError or %NULL
2370 * Returns the values associated with @key under @group_name
2371 * translated in the given @locale if available. If @locale is
2372 * %NULL then the current locale is assumed.
2374 * If @locale is to be non-%NULL, or if the current locale will change over
2375 * the lifetime of the #GKeyFile, it must be loaded with
2376 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2378 * If @key cannot be found then %NULL is returned and @error is set
2379 * to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2380 * with @key cannot be interpreted or no suitable translations
2381 * can be found then the untranslated values are returned. The
2382 * returned array is %NULL-terminated, so @length may optionally
2385 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2386 * or %NULL if the key isn't found. The string array should be freed
2387 * with g_strfreev().
2392 g_key_file_get_locale_string_list (GKeyFile *key_file,
2393 const gchar *group_name,
2395 const gchar *locale,
2399 GError *key_file_error;
2400 gchar **values, *value;
2401 char list_separator[2];
2404 g_return_val_if_fail (key_file != NULL, NULL);
2405 g_return_val_if_fail (group_name != NULL, NULL);
2406 g_return_val_if_fail (key != NULL, NULL);
2408 key_file_error = NULL;
2410 value = g_key_file_get_locale_string (key_file, group_name,
2415 g_propagate_error (error, key_file_error);
2424 len = strlen (value);
2425 if (value[len - 1] == key_file->list_separator)
2426 value[len - 1] = '\0';
2428 list_separator[0] = key_file->list_separator;
2429 list_separator[1] = '\0';
2430 values = g_strsplit (value, list_separator, 0);
2435 *length = g_strv_length (values);
2441 * g_key_file_set_locale_string_list:
2442 * @key_file: a #GKeyFile
2443 * @group_name: a group name
2445 * @locale: a locale identifier
2446 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2447 * @length: the length of @list
2449 * Associates a list of string values for @key and @locale under
2450 * @group_name. If the translation for @key cannot be found then
2456 g_key_file_set_locale_string_list (GKeyFile *key_file,
2457 const gchar *group_name,
2459 const gchar *locale,
2460 const gchar * const list[],
2463 GString *value_list;
2467 g_return_if_fail (key_file != NULL);
2468 g_return_if_fail (key != NULL);
2469 g_return_if_fail (locale != NULL);
2470 g_return_if_fail (length != 0);
2472 value_list = g_string_sized_new (length * 128);
2473 for (i = 0; i < length && list[i] != NULL; i++)
2477 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2478 g_string_append (value_list, value);
2479 g_string_append_c (value_list, key_file->list_separator);
2484 full_key = g_strdup_printf ("%s[%s]", key, locale);
2485 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2487 g_string_free (value_list, TRUE);
2491 * g_key_file_get_boolean:
2492 * @key_file: a #GKeyFile
2493 * @group_name: a group name
2495 * @error: return location for a #GError
2497 * Returns the value associated with @key under @group_name as a
2500 * If @key cannot be found then %FALSE is returned and @error is set
2501 * to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2502 * associated with @key cannot be interpreted as a boolean then %FALSE
2503 * is returned and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2505 * Returns: the value associated with the key as a boolean,
2506 * or %FALSE if the key was not found or could not be parsed.
2511 g_key_file_get_boolean (GKeyFile *key_file,
2512 const gchar *group_name,
2516 GError *key_file_error = NULL;
2518 gboolean bool_value;
2520 g_return_val_if_fail (key_file != NULL, FALSE);
2521 g_return_val_if_fail (group_name != NULL, FALSE);
2522 g_return_val_if_fail (key != NULL, FALSE);
2524 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2528 g_propagate_error (error, key_file_error);
2532 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2538 if (g_error_matches (key_file_error,
2540 G_KEY_FILE_ERROR_INVALID_VALUE))
2542 g_set_error (error, G_KEY_FILE_ERROR,
2543 G_KEY_FILE_ERROR_INVALID_VALUE,
2544 _("Key file contains key “%s” "
2545 "which has a value that cannot be interpreted."),
2547 g_error_free (key_file_error);
2550 g_propagate_error (error, key_file_error);
2557 * g_key_file_set_boolean:
2558 * @key_file: a #GKeyFile
2559 * @group_name: a group name
2561 * @value: %TRUE or %FALSE
2563 * Associates a new boolean value with @key under @group_name.
2564 * If @key cannot be found then it is created.
2569 g_key_file_set_boolean (GKeyFile *key_file,
2570 const gchar *group_name,
2574 const gchar *result;
2576 g_return_if_fail (key_file != NULL);
2578 result = g_key_file_parse_boolean_as_value (key_file, value);
2579 g_key_file_set_value (key_file, group_name, key, result);
2583 * g_key_file_get_boolean_list:
2584 * @key_file: a #GKeyFile
2585 * @group_name: a group name
2587 * @length: (out): the number of booleans returned
2588 * @error: return location for a #GError
2590 * Returns the values associated with @key under @group_name as
2593 * If @key cannot be found then %NULL is returned and @error is set to
2594 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2595 * with @key cannot be interpreted as booleans then %NULL is returned
2596 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2598 * Returns: (array length=length) (element-type gboolean) (transfer container):
2599 * the values associated with the key as a list of booleans, or %NULL if the
2600 * key was not found or could not be parsed. The returned list of booleans
2601 * should be freed with g_free() when no longer needed.
2606 g_key_file_get_boolean_list (GKeyFile *key_file,
2607 const gchar *group_name,
2612 GError *key_file_error;
2614 gboolean *bool_values;
2617 g_return_val_if_fail (key_file != NULL, NULL);
2618 g_return_val_if_fail (group_name != NULL, NULL);
2619 g_return_val_if_fail (key != NULL, NULL);
2624 key_file_error = NULL;
2626 values = g_key_file_get_string_list (key_file, group_name, key,
2627 &num_bools, &key_file_error);
2630 g_propagate_error (error, key_file_error);
2635 bool_values = g_new (gboolean, num_bools);
2637 for (i = 0; i < num_bools; i++)
2639 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2645 g_propagate_error (error, key_file_error);
2646 g_strfreev (values);
2647 g_free (bool_values);
2652 g_strfreev (values);
2655 *length = num_bools;
2661 * g_key_file_set_boolean_list:
2662 * @key_file: a #GKeyFile
2663 * @group_name: a group name
2665 * @list: (array length=length): an array of boolean values
2666 * @length: length of @list
2668 * Associates a list of boolean values with @key under @group_name.
2669 * If @key cannot be found then it is created.
2670 * If @group_name is %NULL, the start_group is used.
2675 g_key_file_set_boolean_list (GKeyFile *key_file,
2676 const gchar *group_name,
2681 GString *value_list;
2684 g_return_if_fail (key_file != NULL);
2685 g_return_if_fail (list != NULL);
2687 value_list = g_string_sized_new (length * 8);
2688 for (i = 0; i < length; i++)
2692 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2694 g_string_append (value_list, value);
2695 g_string_append_c (value_list, key_file->list_separator);
2698 g_key_file_set_value (key_file, group_name, key, value_list->str);
2699 g_string_free (value_list, TRUE);
2703 * g_key_file_get_integer:
2704 * @key_file: a #GKeyFile
2705 * @group_name: a group name
2707 * @error: return location for a #GError
2709 * Returns the value associated with @key under @group_name as an
2712 * If @key cannot be found then 0 is returned and @error is set to
2713 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2714 * with @key cannot be interpreted as an integer, or is out of range
2715 * for a #gint, then 0 is returned
2716 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2718 * Returns: the value associated with the key as an integer, or
2719 * 0 if the key was not found or could not be parsed.
2724 g_key_file_get_integer (GKeyFile *key_file,
2725 const gchar *group_name,
2729 GError *key_file_error;
2733 g_return_val_if_fail (key_file != NULL, -1);
2734 g_return_val_if_fail (group_name != NULL, -1);
2735 g_return_val_if_fail (key != NULL, -1);
2737 key_file_error = NULL;
2739 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2743 g_propagate_error (error, key_file_error);
2747 int_value = g_key_file_parse_value_as_integer (key_file, value,
2753 if (g_error_matches (key_file_error,
2755 G_KEY_FILE_ERROR_INVALID_VALUE))
2757 g_set_error (error, G_KEY_FILE_ERROR,
2758 G_KEY_FILE_ERROR_INVALID_VALUE,
2759 _("Key file contains key “%s” in group “%s” "
2760 "which has a value that cannot be interpreted."),
2762 g_error_free (key_file_error);
2765 g_propagate_error (error, key_file_error);
2772 * g_key_file_set_integer:
2773 * @key_file: a #GKeyFile
2774 * @group_name: a group name
2776 * @value: an integer value
2778 * Associates a new integer value with @key under @group_name.
2779 * If @key cannot be found then it is created.
2784 g_key_file_set_integer (GKeyFile *key_file,
2785 const gchar *group_name,
2791 g_return_if_fail (key_file != NULL);
2793 result = g_key_file_parse_integer_as_value (key_file, value);
2794 g_key_file_set_value (key_file, group_name, key, result);
2799 * g_key_file_get_int64:
2800 * @key_file: a non-%NULL #GKeyFile
2801 * @group_name: a non-%NULL group name
2802 * @key: a non-%NULL key
2803 * @error: return location for a #GError
2805 * Returns the value associated with @key under @group_name as a signed
2806 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2807 * 64-bit results without truncation.
2809 * Returns: the value associated with the key as a signed 64-bit integer, or
2810 * 0 if the key was not found or could not be parsed.
2815 g_key_file_get_int64 (GKeyFile *key_file,
2816 const gchar *group_name,
2823 g_return_val_if_fail (key_file != NULL, -1);
2824 g_return_val_if_fail (group_name != NULL, -1);
2825 g_return_val_if_fail (key != NULL, -1);
2827 s = g_key_file_get_value (key_file, group_name, key, error);
2832 v = g_ascii_strtoll (s, &end, 10);
2834 if (*s == '\0' || *end != '\0')
2836 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2837 _("Key “%s” in group “%s” has value “%s” "
2838 "where %s was expected"),
2839 key, group_name, s, "int64");
2849 * g_key_file_set_int64:
2850 * @key_file: a #GKeyFile
2851 * @group_name: a group name
2853 * @value: an integer value
2855 * Associates a new integer value with @key under @group_name.
2856 * If @key cannot be found then it is created.
2861 g_key_file_set_int64 (GKeyFile *key_file,
2862 const gchar *group_name,
2868 g_return_if_fail (key_file != NULL);
2870 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2871 g_key_file_set_value (key_file, group_name, key, result);
2876 * g_key_file_get_uint64:
2877 * @key_file: a non-%NULL #GKeyFile
2878 * @group_name: a non-%NULL group name
2879 * @key: a non-%NULL key
2880 * @error: return location for a #GError
2882 * Returns the value associated with @key under @group_name as an unsigned
2883 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2884 * large positive results without truncation.
2886 * Returns: the value associated with the key as an unsigned 64-bit integer,
2887 * or 0 if the key was not found or could not be parsed.
2892 g_key_file_get_uint64 (GKeyFile *key_file,
2893 const gchar *group_name,
2900 g_return_val_if_fail (key_file != NULL, -1);
2901 g_return_val_if_fail (group_name != NULL, -1);
2902 g_return_val_if_fail (key != NULL, -1);
2904 s = g_key_file_get_value (key_file, group_name, key, error);
2909 v = g_ascii_strtoull (s, &end, 10);
2911 if (*s == '\0' || *end != '\0')
2913 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2914 _("Key “%s” in group “%s” has value “%s” "
2915 "where %s was expected"),
2916 key, group_name, s, "uint64");
2926 * g_key_file_set_uint64:
2927 * @key_file: a #GKeyFile
2928 * @group_name: a group name
2930 * @value: an integer value
2932 * Associates a new integer value with @key under @group_name.
2933 * If @key cannot be found then it is created.
2938 g_key_file_set_uint64 (GKeyFile *key_file,
2939 const gchar *group_name,
2945 g_return_if_fail (key_file != NULL);
2947 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2948 g_key_file_set_value (key_file, group_name, key, result);
2953 * g_key_file_get_integer_list:
2954 * @key_file: a #GKeyFile
2955 * @group_name: a group name
2957 * @length: (out): the number of integers returned
2958 * @error: return location for a #GError
2960 * Returns the values associated with @key under @group_name as
2963 * If @key cannot be found then %NULL is returned and @error is set to
2964 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2965 * with @key cannot be interpreted as integers, or are out of range for
2966 * #gint, then %NULL is returned
2967 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2969 * Returns: (array length=length) (element-type gint) (transfer container):
2970 * the values associated with the key as a list of integers, or %NULL if
2971 * the key was not found or could not be parsed. The returned list of
2972 * integers should be freed with g_free() when no longer needed.
2977 g_key_file_get_integer_list (GKeyFile *key_file,
2978 const gchar *group_name,
2983 GError *key_file_error = NULL;
2988 g_return_val_if_fail (key_file != NULL, NULL);
2989 g_return_val_if_fail (group_name != NULL, NULL);
2990 g_return_val_if_fail (key != NULL, NULL);
2995 values = g_key_file_get_string_list (key_file, group_name, key,
2996 &num_ints, &key_file_error);
2999 g_propagate_error (error, key_file_error);
3004 int_values = g_new (gint, num_ints);
3006 for (i = 0; i < num_ints; i++)
3008 int_values[i] = g_key_file_parse_value_as_integer (key_file,
3014 g_propagate_error (error, key_file_error);
3015 g_strfreev (values);
3016 g_free (int_values);
3021 g_strfreev (values);
3030 * g_key_file_set_integer_list:
3031 * @key_file: a #GKeyFile
3032 * @group_name: a group name
3034 * @list: (array length=length): an array of integer values
3035 * @length: number of integer values in @list
3037 * Associates a list of integer values with @key under @group_name.
3038 * If @key cannot be found then it is created.
3043 g_key_file_set_integer_list (GKeyFile *key_file,
3044 const gchar *group_name,
3052 g_return_if_fail (key_file != NULL);
3053 g_return_if_fail (list != NULL);
3055 values = g_string_sized_new (length * 16);
3056 for (i = 0; i < length; i++)
3060 value = g_key_file_parse_integer_as_value (key_file, list[i]);
3062 g_string_append (values, value);
3063 g_string_append_c (values, key_file->list_separator);
3068 g_key_file_set_value (key_file, group_name, key, values->str);
3069 g_string_free (values, TRUE);
3073 * g_key_file_get_double:
3074 * @key_file: a #GKeyFile
3075 * @group_name: a group name
3077 * @error: return location for a #GError
3079 * Returns the value associated with @key under @group_name as a
3080 * double. If @group_name is %NULL, the start_group is used.
3082 * If @key cannot be found then 0.0 is returned and @error is set to
3083 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
3084 * with @key cannot be interpreted as a double then 0.0 is returned
3085 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
3087 * Returns: the value associated with the key as a double, or
3088 * 0.0 if the key was not found or could not be parsed.
3093 g_key_file_get_double (GKeyFile *key_file,
3094 const gchar *group_name,
3098 GError *key_file_error;
3100 gdouble double_value;
3102 g_return_val_if_fail (key_file != NULL, -1);
3103 g_return_val_if_fail (group_name != NULL, -1);
3104 g_return_val_if_fail (key != NULL, -1);
3106 key_file_error = NULL;
3108 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3112 g_propagate_error (error, key_file_error);
3116 double_value = g_key_file_parse_value_as_double (key_file, value,
3122 if (g_error_matches (key_file_error,
3124 G_KEY_FILE_ERROR_INVALID_VALUE))
3126 g_set_error (error, G_KEY_FILE_ERROR,
3127 G_KEY_FILE_ERROR_INVALID_VALUE,
3128 _("Key file contains key “%s” in group “%s” "
3129 "which has a value that cannot be interpreted."),
3131 g_error_free (key_file_error);
3134 g_propagate_error (error, key_file_error);
3137 return double_value;
3141 * g_key_file_set_double:
3142 * @key_file: a #GKeyFile
3143 * @group_name: a group name
3145 * @value: a double value
3147 * Associates a new double value with @key under @group_name.
3148 * If @key cannot be found then it is created.
3153 g_key_file_set_double (GKeyFile *key_file,
3154 const gchar *group_name,
3158 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3160 g_return_if_fail (key_file != NULL);
3162 g_ascii_dtostr (result, sizeof (result), value);
3163 g_key_file_set_value (key_file, group_name, key, result);
3167 * g_key_file_get_double_list:
3168 * @key_file: a #GKeyFile
3169 * @group_name: a group name
3171 * @length: (out): the number of doubles returned
3172 * @error: return location for a #GError
3174 * Returns the values associated with @key under @group_name as
3177 * If @key cannot be found then %NULL is returned and @error is set to
3178 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3179 * with @key cannot be interpreted as doubles then %NULL is returned
3180 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
3182 * Returns: (array length=length) (element-type gdouble) (transfer container):
3183 * the values associated with the key as a list of doubles, or %NULL if the
3184 * key was not found or could not be parsed. The returned list of doubles
3185 * should be freed with g_free() when no longer needed.
3190 g_key_file_get_double_list (GKeyFile *key_file,
3191 const gchar *group_name,
3196 GError *key_file_error = NULL;
3198 gdouble *double_values;
3199 gsize i, num_doubles;
3201 g_return_val_if_fail (key_file != NULL, NULL);
3202 g_return_val_if_fail (group_name != NULL, NULL);
3203 g_return_val_if_fail (key != NULL, NULL);
3208 values = g_key_file_get_string_list (key_file, group_name, key,
3209 &num_doubles, &key_file_error);
3212 g_propagate_error (error, key_file_error);
3217 double_values = g_new (gdouble, num_doubles);
3219 for (i = 0; i < num_doubles; i++)
3221 double_values[i] = g_key_file_parse_value_as_double (key_file,
3227 g_propagate_error (error, key_file_error);
3228 g_strfreev (values);
3229 g_free (double_values);
3234 g_strfreev (values);
3237 *length = num_doubles;
3239 return double_values;
3243 * g_key_file_set_double_list:
3244 * @key_file: a #GKeyFile
3245 * @group_name: a group name
3247 * @list: (array length=length): an array of double values
3248 * @length: number of double values in @list
3250 * Associates a list of double values with @key under
3251 * @group_name. If @key cannot be found then it is created.
3256 g_key_file_set_double_list (GKeyFile *key_file,
3257 const gchar *group_name,
3265 g_return_if_fail (key_file != NULL);
3266 g_return_if_fail (list != NULL);
3268 values = g_string_sized_new (length * 16);
3269 for (i = 0; i < length; i++)
3271 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3273 g_ascii_dtostr( result, sizeof (result), list[i] );
3275 g_string_append (values, result);
3276 g_string_append_c (values, key_file->list_separator);
3279 g_key_file_set_value (key_file, group_name, key, values->str);
3280 g_string_free (values, TRUE);
3284 g_key_file_set_key_comment (GKeyFile *key_file,
3285 const gchar *group_name,
3287 const gchar *comment,
3290 GKeyFileGroup *group;
3291 GKeyFileKeyValuePair *pair;
3292 GList *key_node, *comment_node, *tmp;
3294 group = g_key_file_lookup_group (key_file, group_name);
3297 g_set_error (error, G_KEY_FILE_ERROR,
3298 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3299 _("Key file does not have group “%s”"),
3300 group_name ? group_name : "(null)");
3305 /* First find the key the comments are supposed to be
3308 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3310 if (key_node == NULL)
3312 set_not_found_key_error (group->name, key, error);
3316 /* Then find all the comments already associated with the
3319 tmp = key_node->next;
3322 pair = (GKeyFileKeyValuePair *) tmp->data;
3324 if (pair->key != NULL)
3329 g_key_file_remove_key_value_pair_node (key_file, group,
3333 if (comment == NULL)
3336 /* Now we can add our new comment
3338 pair = g_slice_new (GKeyFileKeyValuePair);
3340 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3342 key_node = g_list_insert (key_node, pair, 1);
3349 g_key_file_set_group_comment (GKeyFile *key_file,
3350 const gchar *group_name,
3351 const gchar *comment,
3354 GKeyFileGroup *group;
3356 g_return_val_if_fail (group_name != NULL && g_key_file_is_group_name (group_name), FALSE);
3358 group = g_key_file_lookup_group (key_file, group_name);
3361 g_set_error (error, G_KEY_FILE_ERROR,
3362 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3363 _("Key file does not have group “%s”"),
3369 /* First remove any existing comment
3373 g_key_file_key_value_pair_free (group->comment);
3374 group->comment = NULL;
3377 if (comment == NULL)
3380 /* Now we can add our new comment
3382 group->comment = g_slice_new (GKeyFileKeyValuePair);
3383 group->comment->key = NULL;
3384 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3390 g_key_file_set_top_comment (GKeyFile *key_file,
3391 const gchar *comment,
3395 GKeyFileGroup *group;
3396 GKeyFileKeyValuePair *pair;
3398 /* The last group in the list should be the top (comments only)
3401 g_warn_if_fail (key_file->groups != NULL);
3402 group_node = g_list_last (key_file->groups);
3403 group = (GKeyFileGroup *) group_node->data;
3404 g_warn_if_fail (group->name == NULL);
3406 /* Note all keys must be comments at the top of
3407 * the file, so we can just free it all.
3409 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3410 group->key_value_pairs = NULL;
3412 if (comment == NULL)
3415 pair = g_slice_new (GKeyFileKeyValuePair);
3417 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3419 group->key_value_pairs =
3420 g_list_prepend (group->key_value_pairs, pair);
3426 * g_key_file_set_comment:
3427 * @key_file: a #GKeyFile
3428 * @group_name: (nullable): a group name, or %NULL
3429 * @key: (nullable): a key
3430 * @comment: a comment
3431 * @error: return location for a #GError
3433 * Places a comment above @key from @group_name.
3435 * If @key is %NULL then @comment will be written above @group_name.
3436 * If both @key and @group_name are %NULL, then @comment will be
3437 * written above the first group in the file.
3439 * Note that this function prepends a '#' comment marker to
3440 * each line of @comment.
3442 * Returns: %TRUE if the comment was written, %FALSE otherwise
3447 g_key_file_set_comment (GKeyFile *key_file,
3448 const gchar *group_name,
3450 const gchar *comment,
3453 g_return_val_if_fail (key_file != NULL, FALSE);
3455 if (group_name != NULL && key != NULL)
3457 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3460 else if (group_name != NULL)
3462 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3467 if (!g_key_file_set_top_comment (key_file, comment, error))
3475 g_key_file_get_key_comment (GKeyFile *key_file,
3476 const gchar *group_name,
3480 GKeyFileGroup *group;
3481 GKeyFileKeyValuePair *pair;
3482 GList *key_node, *tmp;
3486 g_return_val_if_fail (group_name != NULL && g_key_file_is_group_name (group_name), NULL);
3488 group = g_key_file_lookup_group (key_file, group_name);
3491 g_set_error (error, G_KEY_FILE_ERROR,
3492 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3493 _("Key file does not have group “%s”"),
3494 group_name ? group_name : "(null)");
3499 /* First find the key the comments are supposed to be
3502 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3504 if (key_node == NULL)
3506 set_not_found_key_error (group->name, key, error);
3512 /* Then find all the comments already associated with the
3513 * key and concatenate them.
3515 tmp = key_node->next;
3516 if (!key_node->next)
3519 pair = (GKeyFileKeyValuePair *) tmp->data;
3520 if (pair->key != NULL)
3525 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3527 if (pair->key != NULL)
3533 while (tmp != key_node)
3535 pair = (GKeyFileKeyValuePair *) tmp->data;
3538 string = g_string_sized_new (512);
3540 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3541 (tmp->prev == key_node));
3542 g_string_append (string, comment);
3550 comment = string->str;
3551 g_string_free (string, FALSE);
3560 get_group_comment (GKeyFile *key_file,
3561 GKeyFileGroup *group,
3570 tmp = group->key_value_pairs;
3573 GKeyFileKeyValuePair *pair;
3575 pair = (GKeyFileKeyValuePair *) tmp->data;
3577 if (pair->key != NULL)
3583 if (tmp->next == NULL)
3591 GKeyFileKeyValuePair *pair;
3593 pair = (GKeyFileKeyValuePair *) tmp->data;
3596 string = g_string_sized_new (512);
3598 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3599 (tmp->prev == NULL));
3600 g_string_append (string, comment);
3607 return g_string_free (string, FALSE);
3613 g_key_file_get_group_comment (GKeyFile *key_file,
3614 const gchar *group_name,
3618 GKeyFileGroup *group;
3620 group = g_key_file_lookup_group (key_file, group_name);
3623 g_set_error (error, G_KEY_FILE_ERROR,
3624 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3625 _("Key file does not have group “%s”"),
3626 group_name ? group_name : "(null)");
3632 return g_strdup (group->comment->value);
3634 group_node = g_key_file_lookup_group_node (key_file, group_name);
3635 group_node = group_node->next;
3636 group = (GKeyFileGroup *)group_node->data;
3637 return get_group_comment (key_file, group, error);
3641 g_key_file_get_top_comment (GKeyFile *key_file,
3645 GKeyFileGroup *group;
3647 /* The last group in the list should be the top (comments only)
3650 g_warn_if_fail (key_file->groups != NULL);
3651 group_node = g_list_last (key_file->groups);
3652 group = (GKeyFileGroup *) group_node->data;
3653 g_warn_if_fail (group->name == NULL);
3655 return get_group_comment (key_file, group, error);
3659 * g_key_file_get_comment:
3660 * @key_file: a #GKeyFile
3661 * @group_name: (nullable): a group name, or %NULL
3662 * @key: (nullable): a key
3663 * @error: return location for a #GError
3665 * Retrieves a comment above @key from @group_name.
3666 * If @key is %NULL then @comment will be read from above
3667 * @group_name. If both @key and @group_name are %NULL, then
3668 * @comment will be read from above the first group in the file.
3670 * Note that the returned string does not include the '#' comment markers,
3671 * but does include any whitespace after them (on each line). It includes
3672 * the line breaks between lines, but does not include the final line break.
3674 * Returns: a comment that should be freed with g_free()
3679 g_key_file_get_comment (GKeyFile *key_file,
3680 const gchar *group_name,
3684 g_return_val_if_fail (key_file != NULL, NULL);
3686 if (group_name != NULL && key != NULL)
3687 return g_key_file_get_key_comment (key_file, group_name, key, error);
3688 else if (group_name != NULL)
3689 return g_key_file_get_group_comment (key_file, group_name, error);
3691 return g_key_file_get_top_comment (key_file, error);
3695 * g_key_file_remove_comment:
3696 * @key_file: a #GKeyFile
3697 * @group_name: (nullable): a group name, or %NULL
3698 * @key: (nullable): a key
3699 * @error: return location for a #GError
3701 * Removes a comment above @key from @group_name.
3702 * If @key is %NULL then @comment will be removed above @group_name.
3703 * If both @key and @group_name are %NULL, then @comment will
3704 * be removed above the first group in the file.
3706 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3712 g_key_file_remove_comment (GKeyFile *key_file,
3713 const gchar *group_name,
3717 g_return_val_if_fail (key_file != NULL, FALSE);
3719 if (group_name != NULL && key != NULL)
3720 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3721 else if (group_name != NULL)
3722 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3724 return g_key_file_set_top_comment (key_file, NULL, error);
3728 * g_key_file_has_group:
3729 * @key_file: a #GKeyFile
3730 * @group_name: a group name
3732 * Looks whether the key file has the group @group_name.
3734 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3739 g_key_file_has_group (GKeyFile *key_file,
3740 const gchar *group_name)
3742 g_return_val_if_fail (key_file != NULL, FALSE);
3743 g_return_val_if_fail (group_name != NULL, FALSE);
3745 return g_key_file_lookup_group (key_file, group_name) != NULL;
3748 /* This code remains from a historical attempt to add a new public API
3749 * which respects the GError rules.
3752 g_key_file_has_key_full (GKeyFile *key_file,
3753 const gchar *group_name,
3758 GKeyFileKeyValuePair *pair;
3759 GKeyFileGroup *group;
3761 g_return_val_if_fail (key_file != NULL, FALSE);
3762 g_return_val_if_fail (group_name != NULL, FALSE);
3763 g_return_val_if_fail (key != NULL, FALSE);
3765 group = g_key_file_lookup_group (key_file, group_name);
3769 g_set_error (error, G_KEY_FILE_ERROR,
3770 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3771 _("Key file does not have group “%s”"),
3777 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3780 *has_key = pair != NULL;
3785 * g_key_file_has_key: (skip)
3786 * @key_file: a #GKeyFile
3787 * @group_name: a group name
3789 * @error: return location for a #GError
3791 * Looks whether the key file has the key @key in the group
3794 * Note that this function does not follow the rules for #GError strictly;
3795 * the return value both carries meaning and signals an error. To use
3796 * this function, you must pass a #GError pointer in @error, and check
3797 * whether it is not %NULL to see if an error occurred.
3799 * Language bindings should use g_key_file_get_value() to test whether
3800 * or not a key exists.
3802 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3807 g_key_file_has_key (GKeyFile *key_file,
3808 const gchar *group_name,
3812 GError *temp_error = NULL;
3815 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3821 g_propagate_error (error, temp_error);
3827 g_key_file_add_group (GKeyFile *key_file,
3828 const gchar *group_name)
3830 GKeyFileGroup *group;
3832 g_return_if_fail (key_file != NULL);
3833 g_return_if_fail (group_name != NULL && g_key_file_is_group_name (group_name));
3835 group = g_key_file_lookup_group (key_file, group_name);
3838 key_file->current_group = group;
3842 group = g_slice_new0 (GKeyFileGroup);
3843 group->name = g_strdup (group_name);
3844 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3845 key_file->groups = g_list_prepend (key_file->groups, group);
3846 key_file->current_group = group;
3848 if (key_file->start_group == NULL)
3849 key_file->start_group = group;
3851 if (!key_file->group_hash)
3852 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
3854 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3858 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3863 g_free (pair->value);
3864 g_slice_free (GKeyFileKeyValuePair, pair);
3868 /* Be careful not to call this function on a node with data in the
3869 * lookup map without removing it from the lookup map, first.
3871 * Some current cases where this warning is not a concern are
3873 * - the node being removed is a comment node
3874 * - the entire lookup map is getting destroyed soon after
3878 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3879 GKeyFileGroup *group,
3883 GKeyFileKeyValuePair *pair;
3885 pair = (GKeyFileKeyValuePair *) pair_node->data;
3887 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3889 g_warn_if_fail (pair->value != NULL);
3891 g_key_file_key_value_pair_free (pair);
3893 g_list_free_1 (pair_node);
3897 g_key_file_remove_group_node (GKeyFile *key_file,
3900 GKeyFileGroup *group;
3903 group = (GKeyFileGroup *) group_node->data;
3907 g_assert (key_file->group_hash);
3908 g_hash_table_remove (key_file->group_hash, group->name);
3911 /* If the current group gets deleted make the current group the last
3914 if (key_file->current_group == group)
3916 /* groups should always contain at least the top comment group,
3917 * unless g_key_file_clear has been called
3919 if (key_file->groups)
3920 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3922 key_file->current_group = NULL;
3925 /* If the start group gets deleted make the start group the first
3928 if (key_file->start_group == group)
3930 tmp = g_list_last (key_file->groups);
3933 if (tmp != group_node &&
3934 ((GKeyFileGroup *) tmp->data)->name != NULL)
3941 key_file->start_group = (GKeyFileGroup *) tmp->data;
3943 key_file->start_group = NULL;
3946 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3948 tmp = group->key_value_pairs;
3955 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3958 g_warn_if_fail (group->key_value_pairs == NULL);
3962 g_key_file_key_value_pair_free (group->comment);
3963 group->comment = NULL;
3966 if (group->lookup_map)
3968 g_hash_table_destroy (group->lookup_map);
3969 group->lookup_map = NULL;
3972 g_free ((gchar *) group->name);
3973 g_slice_free (GKeyFileGroup, group);
3974 g_list_free_1 (group_node);
3978 * g_key_file_remove_group:
3979 * @key_file: a #GKeyFile
3980 * @group_name: a group name
3981 * @error: return location for a #GError or %NULL
3983 * Removes the specified group, @group_name,
3984 * from the key file.
3986 * Returns: %TRUE if the group was removed, %FALSE otherwise
3991 g_key_file_remove_group (GKeyFile *key_file,
3992 const gchar *group_name,
3997 g_return_val_if_fail (key_file != NULL, FALSE);
3998 g_return_val_if_fail (group_name != NULL, FALSE);
4000 group_node = g_key_file_lookup_group_node (key_file, group_name);
4004 g_set_error (error, G_KEY_FILE_ERROR,
4005 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4006 _("Key file does not have group “%s”"),
4011 g_key_file_remove_group_node (key_file, group_node);
4017 g_key_file_add_key_value_pair (GKeyFile *key_file,
4018 GKeyFileGroup *group,
4019 GKeyFileKeyValuePair *pair)
4021 g_hash_table_replace (group->lookup_map, pair->key, pair);
4022 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
4026 g_key_file_add_key (GKeyFile *key_file,
4027 GKeyFileGroup *group,
4031 GKeyFileKeyValuePair *pair;
4033 pair = g_slice_new (GKeyFileKeyValuePair);
4034 pair->key = g_strdup (key);
4035 pair->value = g_strdup (value);
4037 g_key_file_add_key_value_pair (key_file, group, pair);
4041 * g_key_file_remove_key:
4042 * @key_file: a #GKeyFile
4043 * @group_name: a group name
4044 * @key: a key name to remove
4045 * @error: return location for a #GError or %NULL
4047 * Removes @key in @group_name from the key file.
4049 * Returns: %TRUE if the key was removed, %FALSE otherwise
4054 g_key_file_remove_key (GKeyFile *key_file,
4055 const gchar *group_name,
4059 GKeyFileGroup *group;
4060 GKeyFileKeyValuePair *pair;
4062 g_return_val_if_fail (key_file != NULL, FALSE);
4063 g_return_val_if_fail (group_name != NULL, FALSE);
4064 g_return_val_if_fail (key != NULL, FALSE);
4068 group = g_key_file_lookup_group (key_file, group_name);
4071 g_set_error (error, G_KEY_FILE_ERROR,
4072 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4073 _("Key file does not have group “%s”"),
4078 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
4082 set_not_found_key_error (group->name, key, error);
4086 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
4087 g_hash_table_remove (group->lookup_map, pair->key);
4088 g_key_file_key_value_pair_free (pair);
4094 g_key_file_lookup_group_node (GKeyFile *key_file,
4095 const gchar *group_name)
4097 GKeyFileGroup *group;
4099 group = g_key_file_lookup_group (key_file, group_name);
4103 return g_list_find (key_file->groups, group);
4106 static GKeyFileGroup *
4107 g_key_file_lookup_group (GKeyFile *key_file,
4108 const gchar *group_name)
4110 if (!key_file->group_hash)
4113 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4117 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4118 GKeyFileGroup *group,
4123 for (key_node = group->key_value_pairs;
4125 key_node = key_node->next)
4127 GKeyFileKeyValuePair *pair;
4129 pair = (GKeyFileKeyValuePair *) key_node->data;
4131 if (pair->key && strcmp (pair->key, key) == 0)
4138 static GKeyFileKeyValuePair *
4139 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4140 GKeyFileGroup *group,
4143 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4146 /* Lines starting with # or consisting entirely of whitespace are merely
4147 * recorded, not parsed. This function assumes all leading whitespace
4148 * has been stripped.
4151 g_key_file_line_is_comment (const gchar *line)
4153 return (*line == '#' || *line == '\0' || *line == '\n');
4157 g_key_file_is_group_name (const gchar *name)
4161 g_assert (name != NULL);
4164 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4165 q = g_utf8_find_next_char (q, NULL);
4167 if (*q != '\0' || q == p)
4174 g_key_file_is_key_name (const gchar *name,
4177 const gchar *p, *q, *end;
4179 g_assert (name != NULL);
4184 /* We accept a little more than the desktop entry spec says,
4185 * since gnome-vfs uses mime-types as keys in its cache.
4187 while (q < end && *q && *q != '=' && *q != '[' && *q != ']')
4189 q = g_utf8_find_next_char (q, end);
4194 /* No empty keys, please */
4198 /* We accept spaces in the middle of keys to not break
4199 * existing apps, but we don't tolerate initial or final
4200 * spaces, which would lead to silent corruption when
4201 * rereading the file.
4203 if (*p == ' ' || q[-1] == ' ')
4211 (g_unichar_isalnum (g_utf8_get_char_validated (q, end - q)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4213 q = g_utf8_find_next_char (q, end);
4233 /* A group in a key file is made up of a starting '[' followed by one
4234 * or more letters making up the group name followed by ']'.
4237 g_key_file_line_is_group (const gchar *line)
4247 while (*p && *p != ']')
4248 p = g_utf8_find_next_char (p, NULL);
4253 /* silently accept whitespace after the ] */
4254 p = g_utf8_find_next_char (p, NULL);
4255 while (*p == ' ' || *p == '\t')
4256 p = g_utf8_find_next_char (p, NULL);
4265 g_key_file_line_is_key_value_pair (const gchar *line)
4269 p = g_utf8_strchr (line, -1, '=');
4274 /* Key must be non-empty
4283 g_key_file_parse_value_as_string (GKeyFile *key_file,
4288 gchar *string_value, *q0, *q;
4291 string_value = g_new (gchar, strlen (value) + 1);
4294 q0 = q = string_value;
4324 g_set_error_literal (error, G_KEY_FILE_ERROR,
4325 G_KEY_FILE_ERROR_INVALID_VALUE,
4326 _("Key file contains escape character "
4331 if (pieces && *p == key_file->list_separator)
4332 *q = key_file->list_separator;
4346 g_set_error (error, G_KEY_FILE_ERROR,
4347 G_KEY_FILE_ERROR_INVALID_VALUE,
4348 _("Key file contains invalid escape "
4349 "sequence “%s”"), sequence);
4358 if (pieces && (*p == key_file->list_separator))
4360 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4376 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4377 *pieces = g_slist_reverse (*pieces);
4380 return string_value;
4384 g_key_file_parse_string_as_value (GKeyFile *key_file,
4385 const gchar *string,
4386 gboolean escape_separator)
4391 gboolean parsing_leading_space;
4393 length = strlen (string) + 1;
4395 /* Worst case would be that every character needs to be escaped.
4396 * In other words every character turns to two characters
4398 value = g_new (gchar, 2 * length);
4402 parsing_leading_space = TRUE;
4403 while (p < (string + length - 1))
4405 gchar escaped_character[3] = { '\\', 0, 0 };
4410 if (parsing_leading_space)
4412 escaped_character[1] = 's';
4413 strcpy (q, escaped_character);
4423 if (parsing_leading_space)
4425 escaped_character[1] = 't';
4426 strcpy (q, escaped_character);
4436 escaped_character[1] = 'n';
4437 strcpy (q, escaped_character);
4441 escaped_character[1] = 'r';
4442 strcpy (q, escaped_character);
4446 escaped_character[1] = '\\';
4447 strcpy (q, escaped_character);
4449 parsing_leading_space = FALSE;
4452 if (escape_separator && *p == key_file->list_separator)
4454 escaped_character[1] = key_file->list_separator;
4455 strcpy (q, escaped_character);
4457 parsing_leading_space = TRUE;
4463 parsing_leading_space = FALSE;
4475 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4485 long_value = strtol (value, &eof_int, 10);
4488 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4490 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4491 g_set_error (error, G_KEY_FILE_ERROR,
4492 G_KEY_FILE_ERROR_INVALID_VALUE,
4493 _("Value “%s” cannot be interpreted "
4494 "as a number."), value_utf8);
4495 g_free (value_utf8);
4500 int_value = long_value;
4501 if (int_value != long_value || errsv == ERANGE)
4503 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4506 G_KEY_FILE_ERROR_INVALID_VALUE,
4507 _("Integer value “%s” out of range"),
4509 g_free (value_utf8);
4518 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4522 return g_strdup_printf ("%d", value);
4526 g_key_file_parse_value_as_double (GKeyFile *key_file,
4530 gchar *end_of_valid_d;
4531 gdouble double_value = 0;
4533 double_value = g_ascii_strtod (value, &end_of_valid_d);
4535 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4537 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4538 g_set_error (error, G_KEY_FILE_ERROR,
4539 G_KEY_FILE_ERROR_INVALID_VALUE,
4540 _("Value “%s” cannot be interpreted "
4541 "as a float number."),
4543 g_free (value_utf8);
4548 return double_value;
4552 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4554 size_t len2 = strlen (s2);
4555 return strncmp (s1, s2, MAX (len1, len2));
4559 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4566 /* Count the number of non-whitespace characters */
4567 for (i = 0; value[i]; i++)
4568 if (!g_ascii_isspace (value[i]))
4571 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4573 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4576 value_utf8 = g_utf8_make_valid (value, -1);
4577 g_set_error (error, G_KEY_FILE_ERROR,
4578 G_KEY_FILE_ERROR_INVALID_VALUE,
4579 _("Value “%s” cannot be interpreted "
4580 "as a boolean."), value_utf8);
4581 g_free (value_utf8);
4586 static const gchar *
4587 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4597 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4599 gboolean is_final_line)
4605 string = g_string_sized_new (512);
4607 lines = g_strsplit (value, "\n", 0);
4609 for (i = 0; lines[i] != NULL; i++)
4611 const gchar *line = lines[i];
4614 g_string_append_c (string, '\n');
4618 g_string_append (string, line);
4622 /* This function gets called once per line of a comment, but we don’t want
4623 * to add a trailing newline. */
4625 g_string_append_c (string, '\n');
4627 return g_string_free (string, FALSE);
4631 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4632 const gchar *comment)
4638 string = g_string_sized_new (512);
4640 lines = g_strsplit (comment, "\n", 0);
4642 for (i = 0; lines[i] != NULL; i++)
4643 g_string_append_printf (string, "#%s%s", lines[i],
4644 lines[i + 1] == NULL? "" : "\n");
4647 return g_string_free (string, FALSE);
4651 * g_key_file_save_to_file:
4652 * @key_file: a #GKeyFile
4653 * @filename: the name of the file to write to
4654 * @error: a pointer to a %NULL #GError, or %NULL
4656 * Writes the contents of @key_file to @filename using
4657 * g_file_set_contents(). If you need stricter guarantees about durability of
4658 * the written file than are provided by g_file_set_contents(), use
4659 * g_file_set_contents_full() with the return value of g_key_file_to_data().
4661 * This function can fail for any of the reasons that
4662 * g_file_set_contents() may fail.
4664 * Returns: %TRUE if successful, else %FALSE with @error set
4669 g_key_file_save_to_file (GKeyFile *key_file,
4670 const gchar *filename,
4677 g_return_val_if_fail (key_file != NULL, FALSE);
4678 g_return_val_if_fail (filename != NULL, FALSE);
4679 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4681 contents = g_key_file_to_data (key_file, &length, NULL);
4682 g_assert (contents != NULL);
4684 success = g_file_set_contents (filename, contents, length, error);