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 */
63 #include "gfileutils.h"
69 #include "gmessages.h"
72 #include "gstrfuncs.h"
78 * @title: Key-value file parser
79 * @short_description: parses .ini-like config files
81 * #GKeyFile lets you parse, edit or create files containing groups of
82 * key-value pairs, which we call "key files" for lack of a better name.
83 * Several freedesktop.org specifications use key files now, e.g the
84 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
86 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
88 * The syntax of key files is described in detail in the
89 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
90 * here is a quick summary: Key files
91 * consists of groups of key-value pairs, interspersed with comments.
94 * # this is just an example
95 * # there can be comments before the first group
99 * Name=Key File Example\tthis value shows\nescaping
101 * # localized strings are stored in multiple key-value pairs
104 * Welcome[fr_FR]=Bonjour
106 * Welcome[be@latin]=Hello
110 * Numbers=2;20;-200;0
112 * Booleans=true;false;true;true
115 * Lines beginning with a '#' and blank lines are considered comments.
117 * Groups are started by a header line containing the group name enclosed
118 * in '[' and ']', and ended implicitly by the start of the next group or
119 * the end of the file. Each key-value pair must be contained in a group.
121 * Key-value pairs generally have the form `key=value`, with the
122 * exception of localized strings, which have the form
123 * `key[locale]=value`, with a locale identifier of the
124 * form `lang_COUNTRY@MODIFIER` where `COUNTRY` and `MODIFIER`
126 * Space before and after the '=' character are ignored. Newline, tab,
127 * carriage return and backslash characters in value are escaped as \n,
128 * \t, \r, and \\\\, respectively. To preserve leading spaces in values,
129 * these can also be escaped as \s.
131 * Key files can store strings (possibly with localized variants), integers,
132 * booleans and lists of these. Lists are separated by a separator character,
133 * typically ';' or ','. To use the list separator character in a value in
134 * a list, it has to be escaped by prefixing it with a backslash.
136 * This syntax is obviously inspired by the .ini files commonly met
137 * on Windows, but there are some important differences:
139 * - .ini files use the ';' character to begin comments,
140 * key files use the '#' character.
142 * - Key files do not allow for ungrouped keys meaning only
143 * comments can precede the first group.
145 * - Key files are always encoded in UTF-8.
147 * - Key and Group names are case-sensitive. For example, a group called
148 * [GROUP] is a different from [group].
150 * - .ini files don't have a strongly typed boolean entry type,
151 * they only have GetProfileInt(). In key files, only
152 * true and false (in lower case) are allowed.
154 * Note that in contrast to the
155 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
156 * groups in key files may contain the same
157 * key multiple times; the last entry wins. Key files may also contain
158 * multiple groups with the same name; they are merged together.
159 * Another difference is that keys and group names in key files are not
160 * restricted to ASCII characters.
162 * Here is an example of loading a key file and reading a value:
164 * |[<!-- language="C" -->
165 * g_autoptr(GError) error = NULL;
166 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
168 * if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
170 * if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
171 * g_warning ("Error loading key file: %s", error->message);
175 * g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
177 * !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
179 * g_warning ("Error finding key in key file: %s", error->message);
182 * else if (val == NULL)
184 * // Fall back to a default value.
185 * val = g_strdup ("default-value");
189 * Here is an example of creating and saving a key file:
191 * |[<!-- language="C" -->
192 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
193 * const gchar *val = …;
194 * g_autoptr(GError) error = NULL;
196 * g_key_file_set_string (key_file, "Group Name", "SomeKey", val);
199 * if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
201 * g_warning ("Error saving key file: %s", error->message);
205 * // Or store to a GBytes for use elsewhere.
207 * g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
210 * g_warning ("Error saving key file: %s", error->message);
213 * g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);
220 * Error domain for key file parsing. Errors in this domain will
221 * be from the #GKeyFileError enumeration.
223 * See #GError for information on error domains.
228 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
229 * an unknown encoding
230 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
231 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
232 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
233 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
234 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
236 * Error codes returned by key file parsing.
241 * @G_KEY_FILE_NONE: No flags, default behaviour
242 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
243 * (possibly modified) contents of the key file back to a file;
244 * otherwise all comments will be lost when the key file is
246 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
247 * (possibly modified) contents of the key file back to a file;
248 * otherwise only the translations for the current language will be
251 * Flags which influence the parsing.
255 * G_KEY_FILE_DESKTOP_GROUP:
257 * The name of the main group of a desktop entry file, as defined in the
258 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
259 * Consult the specification for more
260 * details about the meanings of the keys below.
266 * G_KEY_FILE_DESKTOP_KEY_TYPE:
268 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
269 * giving the type of the desktop entry.
271 * Usually %G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
272 * %G_KEY_FILE_DESKTOP_TYPE_LINK, or
273 * %G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
279 * G_KEY_FILE_DESKTOP_KEY_VERSION:
281 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
282 * giving the version of the Desktop Entry Specification used for
283 * the desktop entry file.
289 * G_KEY_FILE_DESKTOP_KEY_NAME:
291 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
292 * string giving the specific name of the desktop entry.
298 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
300 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
301 * string giving the generic name of the desktop entry.
307 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
309 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
310 * stating whether the desktop entry should be shown in menus.
316 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
318 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
319 * string giving the tooltip for the desktop entry.
325 * G_KEY_FILE_DESKTOP_KEY_ICON:
327 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
328 * string giving the name of the icon to be displayed for the desktop
335 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
337 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
338 * stating whether the desktop entry has been deleted by the user.
344 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
346 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
347 * strings identifying the environments that should display the
354 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
356 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
357 * strings identifying the environments that should not display the
364 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
366 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
367 * giving the file name of a binary on disk used to determine if the
368 * program is actually installed. It is only valid for desktop entries
369 * with the `Application` type.
375 * G_KEY_FILE_DESKTOP_KEY_EXEC:
377 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
378 * giving the command line to execute. It is only valid for desktop
379 * entries with the `Application` type.
385 * G_KEY_FILE_DESKTOP_KEY_PATH:
387 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
388 * containing the working directory to run the program in. It is only
389 * valid for desktop entries with the `Application` type.
395 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
397 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
398 * stating whether the program should be run in a terminal window.
400 * It is only valid for desktop entries with the `Application` type.
406 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
408 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list
409 * of strings giving the MIME types supported by this desktop entry.
415 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
417 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list
418 * of strings giving the categories in which the desktop entry
419 * should be shown in a menu.
425 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
427 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
428 * stating whether the application supports the
429 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
435 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
437 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is string
438 * identifying the WM class or name hint of a window that the application
439 * will create, which can be used to emulate Startup Notification with
440 * older applications.
446 * G_KEY_FILE_DESKTOP_KEY_URL:
448 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
449 * giving the URL to access. It is only valid for desktop entries
450 * with the `Link` type.
456 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
458 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
459 * set to true if the application is D-Bus activatable.
465 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
467 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
468 * giving the available application actions.
474 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
476 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
477 * entries representing applications.
483 * G_KEY_FILE_DESKTOP_TYPE_LINK:
485 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
486 * entries representing links to documents.
492 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
494 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
495 * entries representing directories.
500 typedef struct _GKeyFileGroup GKeyFileGroup;
505 * The GKeyFile struct contains only private data
506 * and should not be accessed directly.
511 GHashTable *group_hash;
513 GKeyFileGroup *start_group;
514 GKeyFileGroup *current_group;
516 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
518 gchar list_separator;
522 gboolean checked_locales; /* TRUE if @locales has been initialised */
523 gchar **locales; /* (nullable) */
525 gint ref_count; /* (atomic) */
528 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
530 struct _GKeyFileGroup
532 const gchar *name; /* NULL for above first group (which will be comments) */
534 GList *key_value_pairs;
536 /* Used in parallel with key_value_pairs for
537 * increased lookup performance
539 GHashTable *lookup_map;
542 struct _GKeyFileKeyValuePair
544 gchar *key; /* NULL for comments */
548 static gint find_file_in_data_dirs (const gchar *file,
549 const gchar **data_dirs,
552 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
556 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
557 const gchar *group_name);
558 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
559 const gchar *group_name);
561 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
562 GKeyFileGroup *group,
564 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
565 GKeyFileGroup *group,
568 static void g_key_file_remove_group_node (GKeyFile *key_file,
570 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
571 GKeyFileGroup *group,
574 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
575 GKeyFileGroup *group,
576 GKeyFileKeyValuePair *pair,
578 static void g_key_file_add_key (GKeyFile *key_file,
579 GKeyFileGroup *group,
582 static void g_key_file_add_group (GKeyFile *key_file,
583 const gchar *group_name,
585 static gboolean g_key_file_is_group_name (const gchar *name);
586 static gboolean g_key_file_is_key_name (const gchar *name,
588 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
589 static gboolean g_key_file_line_is_comment (const gchar *line);
590 static gboolean g_key_file_line_is_group (const gchar *line);
591 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
592 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
596 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
598 gboolean escape_separator);
599 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
602 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
604 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
607 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
610 static const gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
612 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
614 gboolean is_final_line);
615 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
616 const gchar *comment);
617 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
621 static void g_key_file_parse_comment (GKeyFile *key_file,
625 static void g_key_file_parse_group (GKeyFile *key_file,
629 static const gchar *key_get_locale (const gchar *key,
631 static void g_key_file_parse_data (GKeyFile *key_file,
635 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
638 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
641 g_key_file_init (GKeyFile *key_file)
643 key_file->current_group = g_new0 (GKeyFileGroup, 1);
644 key_file->groups = g_list_prepend (NULL, key_file->current_group);
645 key_file->group_hash = NULL;
646 key_file->start_group = NULL;
647 key_file->parse_buffer = NULL;
648 key_file->list_separator = ';';
653 g_key_file_clear (GKeyFile *key_file)
655 GList *tmp, *group_node;
657 if (key_file->locales)
659 g_strfreev (key_file->locales);
660 key_file->locales = NULL;
662 key_file->checked_locales = FALSE;
664 if (key_file->parse_buffer)
666 g_string_free (key_file->parse_buffer, TRUE);
667 key_file->parse_buffer = NULL;
670 tmp = key_file->groups;
675 g_key_file_remove_group_node (key_file, group_node);
678 if (key_file->group_hash != NULL)
680 g_hash_table_destroy (key_file->group_hash);
681 key_file->group_hash = NULL;
684 g_warn_if_fail (key_file->groups == NULL);
691 * Creates a new empty #GKeyFile object. Use
692 * g_key_file_load_from_file(), g_key_file_load_from_data(),
693 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
694 * read an existing key file.
696 * Returns: (transfer full): an empty #GKeyFile.
701 g_key_file_new (void)
705 key_file = g_new0 (GKeyFile, 1);
706 key_file->ref_count = 1;
707 g_key_file_init (key_file);
713 * g_key_file_set_list_separator:
714 * @key_file: a #GKeyFile
715 * @separator: the separator
717 * Sets the character which is used to separate
718 * values in lists. Typically ';' or ',' are used
719 * as separators. The default list separator is ';'.
724 g_key_file_set_list_separator (GKeyFile *key_file,
727 g_return_if_fail (key_file != NULL);
729 key_file->list_separator = separator;
733 /* Iterates through all the directories in *dirs trying to
734 * open file. When it successfully locates and opens a file it
735 * returns the file descriptor to the open file. It also
736 * outputs the absolute path of the file in output_file.
739 find_file_in_data_dirs (const gchar *file,
744 const gchar **data_dirs, *data_dir;
756 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
758 const gchar *candidate_file;
761 candidate_file = file;
762 sub_dir = g_strdup ("");
763 while (candidate_file != NULL && fd == -1)
767 path = g_build_filename (data_dir, sub_dir,
768 candidate_file, NULL);
770 fd = g_open (path, O_RDONLY | O_CLOEXEC, 0);
778 candidate_file = strchr (candidate_file, '-');
780 if (candidate_file == NULL)
786 sub_dir = g_strndup (file, candidate_file - file - 1);
788 for (p = sub_dir; *p != '\0'; p++)
791 *p = G_DIR_SEPARATOR;
800 g_set_error_literal (error, G_KEY_FILE_ERROR,
801 G_KEY_FILE_ERROR_NOT_FOUND,
802 _("Valid key file could not be "
803 "found in search dirs"));
806 if (output_file != NULL && fd != -1)
807 *output_file = g_strdup (path);
815 g_key_file_load_from_fd (GKeyFile *key_file,
820 GError *key_file_error = NULL;
822 struct stat stat_buf;
823 gchar read_buf[4096];
824 gchar list_separator;
826 if (fstat (fd, &stat_buf) < 0)
829 g_set_error_literal (error, G_FILE_ERROR,
830 g_file_error_from_errno (errsv),
835 if (!S_ISREG (stat_buf.st_mode))
837 g_set_error_literal (error, G_KEY_FILE_ERROR,
838 G_KEY_FILE_ERROR_PARSE,
839 _("Not a regular file"));
843 list_separator = key_file->list_separator;
844 g_key_file_clear (key_file);
845 g_key_file_init (key_file);
846 key_file->list_separator = list_separator;
847 key_file->flags = flags;
853 bytes_read = read (fd, read_buf, 4096);
856 if (bytes_read == 0) /* End of File */
861 if (errsv == EINTR || errsv == EAGAIN)
864 g_set_error_literal (error, G_FILE_ERROR,
865 g_file_error_from_errno (errsv),
870 g_key_file_parse_data (key_file,
871 read_buf, bytes_read,
874 while (!key_file_error);
878 g_propagate_error (error, key_file_error);
882 g_key_file_flush_parse_buffer (key_file, &key_file_error);
886 g_propagate_error (error, key_file_error);
894 * g_key_file_load_from_file:
895 * @key_file: an empty #GKeyFile struct
896 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
897 * @flags: flags from #GKeyFileFlags
898 * @error: return location for a #GError, or %NULL
900 * Loads a key file into an empty #GKeyFile structure.
902 * If the OS returns an error when opening or reading the file, a
903 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
904 * %G_KEY_FILE_ERROR is returned.
906 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
907 * @file is not found, %G_FILE_ERROR_NOENT is returned.
909 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
914 g_key_file_load_from_file (GKeyFile *key_file,
919 GError *key_file_error = NULL;
923 g_return_val_if_fail (key_file != NULL, FALSE);
924 g_return_val_if_fail (file != NULL, FALSE);
926 fd = g_open (file, O_RDONLY | O_CLOEXEC, 0);
931 g_set_error_literal (error, G_FILE_ERROR,
932 g_file_error_from_errno (errsv),
937 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
942 g_propagate_error (error, key_file_error);
950 * g_key_file_load_from_data:
951 * @key_file: an empty #GKeyFile struct
952 * @data: key file loaded in memory
953 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
954 * @flags: flags from #GKeyFileFlags
955 * @error: return location for a #GError, or %NULL
957 * Loads a key file from memory into an empty #GKeyFile structure.
958 * If the object cannot be created then %error is set to a #GKeyFileError.
960 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
965 g_key_file_load_from_data (GKeyFile *key_file,
971 GError *key_file_error = NULL;
972 gchar list_separator;
974 g_return_val_if_fail (key_file != NULL, FALSE);
975 g_return_val_if_fail (data != NULL || length == 0, FALSE);
977 if (length == (gsize)-1)
978 length = strlen (data);
980 list_separator = key_file->list_separator;
981 g_key_file_clear (key_file);
982 g_key_file_init (key_file);
983 key_file->list_separator = list_separator;
984 key_file->flags = flags;
986 g_key_file_parse_data (key_file, data, length, &key_file_error);
990 g_propagate_error (error, key_file_error);
994 g_key_file_flush_parse_buffer (key_file, &key_file_error);
998 g_propagate_error (error, key_file_error);
1006 * g_key_file_load_from_bytes:
1007 * @key_file: an empty #GKeyFile struct
1009 * @flags: flags from #GKeyFileFlags
1010 * @error: return location for a #GError, or %NULL
1012 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
1013 * If the object cannot be created then %error is set to a #GKeyFileError.
1015 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1020 g_key_file_load_from_bytes (GKeyFile *key_file,
1022 GKeyFileFlags flags,
1028 g_return_val_if_fail (key_file != NULL, FALSE);
1029 g_return_val_if_fail (bytes != NULL, FALSE);
1031 data = g_bytes_get_data (bytes, &size);
1032 return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
1036 * g_key_file_load_from_dirs:
1037 * @key_file: an empty #GKeyFile struct
1038 * @file: (type filename): a relative path to a filename to open and parse
1039 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
1040 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1041 * of the file, or %NULL
1042 * @flags: flags from #GKeyFileFlags
1043 * @error: return location for a #GError, or %NULL
1045 * This function looks for a key file named @file in the paths
1046 * specified in @search_dirs, loads the file into @key_file and
1047 * returns the file's full path in @full_path.
1049 * If the file could not be found in any of the @search_dirs,
1050 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
1051 * the file is found but the OS returns an error when opening or reading the
1052 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
1053 * %G_KEY_FILE_ERROR is returned.
1055 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1060 g_key_file_load_from_dirs (GKeyFile *key_file,
1062 const gchar **search_dirs,
1064 GKeyFileFlags flags,
1067 GError *key_file_error = NULL;
1068 const gchar **data_dirs;
1071 gboolean found_file;
1073 g_return_val_if_fail (key_file != NULL, FALSE);
1074 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1075 g_return_val_if_fail (search_dirs != NULL, FALSE);
1078 data_dirs = search_dirs;
1080 while (*data_dirs != NULL && !found_file)
1082 g_free (output_path);
1085 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
1091 g_propagate_error (error, key_file_error);
1095 found_file = g_key_file_load_from_fd (key_file, fd, flags,
1101 g_propagate_error (error, key_file_error);
1106 if (found_file && full_path)
1107 *full_path = output_path;
1109 g_free (output_path);
1115 * g_key_file_load_from_data_dirs:
1116 * @key_file: an empty #GKeyFile struct
1117 * @file: (type filename): a relative path to a filename to open and parse
1118 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1119 * of the file, or %NULL
1120 * @flags: flags from #GKeyFileFlags
1121 * @error: return location for a #GError, or %NULL
1123 * This function looks for a key file named @file in the paths
1124 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1125 * loads the file into @key_file and returns the file's full path in
1126 * @full_path. If the file could not be loaded then an %error is
1127 * set to either a #GFileError or #GKeyFileError.
1129 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1133 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1136 GKeyFileFlags flags,
1139 gchar **all_data_dirs;
1140 const gchar * user_data_dir;
1141 const gchar * const * system_data_dirs;
1143 gboolean found_file;
1145 g_return_val_if_fail (key_file != NULL, FALSE);
1146 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1148 user_data_dir = g_get_user_data_dir ();
1149 system_data_dirs = g_get_system_data_dirs ();
1150 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1153 all_data_dirs[i++] = g_strdup (user_data_dir);
1156 while (system_data_dirs[j] != NULL)
1157 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1158 all_data_dirs[i] = NULL;
1160 found_file = g_key_file_load_from_dirs (key_file,
1162 (const gchar **)all_data_dirs,
1167 g_strfreev (all_data_dirs);
1173 * g_key_file_ref: (skip)
1174 * @key_file: a #GKeyFile
1176 * Increases the reference count of @key_file.
1178 * Returns: the same @key_file.
1183 g_key_file_ref (GKeyFile *key_file)
1185 g_return_val_if_fail (key_file != NULL, NULL);
1187 g_atomic_int_inc (&key_file->ref_count);
1193 * g_key_file_free: (skip)
1194 * @key_file: a #GKeyFile
1196 * Clears all keys and groups from @key_file, and decreases the
1197 * reference count by 1. If the reference count reaches zero,
1198 * frees the key file and all its allocated memory.
1203 g_key_file_free (GKeyFile *key_file)
1205 g_return_if_fail (key_file != NULL);
1207 g_key_file_clear (key_file);
1209 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1210 g_free_sized (key_file, sizeof (GKeyFile));
1212 g_key_file_init (key_file);
1217 * @key_file: a #GKeyFile
1219 * Decreases the reference count of @key_file by 1. If the reference count
1220 * reaches zero, frees the key file and all its allocated memory.
1225 g_key_file_unref (GKeyFile *key_file)
1227 g_return_if_fail (key_file != NULL);
1229 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1231 g_key_file_clear (key_file);
1232 g_free_sized (key_file, sizeof (GKeyFile));
1236 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1237 * true for locales that match those in g_get_language_names().
1240 g_key_file_locale_is_interesting (GKeyFile *key_file,
1241 const gchar *locale,
1246 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1249 if (!key_file->checked_locales)
1251 g_assert (key_file->locales == NULL);
1252 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
1253 key_file->checked_locales = TRUE;
1256 for (i = 0; key_file->locales[i] != NULL; i++)
1258 if (g_ascii_strncasecmp (key_file->locales[i], locale, locale_len) == 0 &&
1259 key_file->locales[i][locale_len] == '\0')
1267 g_key_file_parse_line (GKeyFile *key_file,
1272 GError *parse_error = NULL;
1273 const gchar *line_start;
1275 g_return_if_fail (key_file != NULL);
1276 g_return_if_fail (line != NULL);
1279 while (g_ascii_isspace (*line_start))
1282 if (g_key_file_line_is_comment (line_start))
1283 g_key_file_parse_comment (key_file, line, length, &parse_error);
1284 else if (g_key_file_line_is_group (line_start))
1285 g_key_file_parse_group (key_file, line_start,
1286 length - (line_start - line),
1288 else if (g_key_file_line_is_key_value_pair (line_start))
1289 g_key_file_parse_key_value_pair (key_file, line_start,
1290 length - (line_start - line),
1294 gchar *line_utf8 = g_utf8_make_valid (line, length);
1295 g_set_error (error, G_KEY_FILE_ERROR,
1296 G_KEY_FILE_ERROR_PARSE,
1297 _("Key file contains line “%s” which is not "
1298 "a key-value pair, group, or comment"),
1306 g_propagate_error (error, parse_error);
1310 g_key_file_parse_comment (GKeyFile *key_file,
1315 GKeyFileKeyValuePair *pair;
1317 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1320 g_warn_if_fail (key_file->current_group != NULL);
1322 pair = g_new (GKeyFileKeyValuePair, 1);
1324 pair->value = g_strndup (line, length);
1326 key_file->current_group->key_value_pairs =
1327 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1331 g_key_file_parse_group (GKeyFile *key_file,
1337 const gchar *group_name_start, *group_name_end;
1339 /* advance past opening '['
1341 group_name_start = line + 1;
1342 group_name_end = line + length - 1;
1344 while (*group_name_end != ']')
1347 group_name = g_strndup (group_name_start,
1348 group_name_end - group_name_start);
1350 if (!g_key_file_is_group_name (group_name))
1352 g_set_error (error, G_KEY_FILE_ERROR,
1353 G_KEY_FILE_ERROR_PARSE,
1354 _("Invalid group name: %s"), group_name);
1355 g_free (group_name);
1359 g_key_file_add_group (key_file, group_name, FALSE);
1360 g_free (group_name);
1364 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1369 gchar *key, *key_end, *value_start;
1370 const gchar *locale;
1372 gsize key_len, value_len;
1374 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1376 g_set_error_literal (error, G_KEY_FILE_ERROR,
1377 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1378 _("Key file does not start with a group"));
1382 key_end = value_start = strchr (line, '=');
1384 g_warn_if_fail (key_end != NULL);
1389 /* Pull the key name from the line (chomping trailing whitespace)
1391 while (g_ascii_isspace (*key_end))
1394 key_len = key_end - line + 2;
1396 g_warn_if_fail (key_len <= length);
1398 if (!g_key_file_is_key_name (line, key_len - 1))
1400 g_set_error (error, G_KEY_FILE_ERROR,
1401 G_KEY_FILE_ERROR_PARSE,
1402 _("Invalid key name: %.*s"), (int) key_len - 1, line);
1406 key = g_strndup (line, key_len - 1);
1408 /* Pull the value from the line (chugging leading whitespace)
1410 while (g_ascii_isspace (*value_start))
1413 value_len = line + length - value_start;
1415 g_warn_if_fail (key_file->start_group != NULL);
1417 /* Checked on entry to this function */
1418 g_assert (key_file->current_group != NULL);
1419 g_assert (key_file->current_group->name != NULL);
1421 if (key_file->start_group == key_file->current_group
1422 && strcmp (key, "Encoding") == 0)
1424 if (value_len != strlen ("UTF-8") ||
1425 g_ascii_strncasecmp (value_start, "UTF-8", value_len) != 0)
1427 gchar *value_utf8 = g_utf8_make_valid (value_start, value_len);
1428 g_set_error (error, G_KEY_FILE_ERROR,
1429 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1430 _("Key file contains unsupported "
1431 "encoding “%s”"), value_utf8);
1432 g_free (value_utf8);
1439 /* Is this key a translation? If so, is it one that we care about?
1441 locale = key_get_locale (key, &locale_len);
1443 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale, locale_len))
1445 GKeyFileKeyValuePair *pair;
1447 pair = g_new (GKeyFileKeyValuePair, 1);
1448 pair->key = g_steal_pointer (&key);
1449 pair->value = g_strndup (value_start, value_len);
1451 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair,
1452 key_file->current_group->key_value_pairs);
1458 static const gchar *
1459 key_get_locale (const gchar *key,
1462 const gchar *locale;
1465 locale = g_strrstr (key, "[");
1467 locale_len = strlen (locale);
1473 locale++; /* skip `[` */
1474 locale_len -= 2; /* drop `[` and `]` */
1482 *len_out = locale_len;
1487 g_key_file_parse_data (GKeyFile *key_file,
1492 GError *parse_error;
1495 g_return_if_fail (key_file != NULL);
1496 g_return_if_fail (data != NULL || length == 0);
1500 if (!key_file->parse_buffer)
1501 key_file->parse_buffer = g_string_sized_new (128);
1506 if (data[i] == '\n')
1508 if (key_file->parse_buffer->len > 0
1509 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1511 g_string_erase (key_file->parse_buffer,
1512 key_file->parse_buffer->len - 1,
1515 /* When a newline is encountered flush the parse buffer so that the
1516 * line can be parsed. Note that completely blank lines won't show
1517 * up in the parse buffer, so they get parsed directly.
1519 if (key_file->parse_buffer->len > 0)
1520 g_key_file_flush_parse_buffer (key_file, &parse_error);
1522 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1526 g_propagate_error (error, parse_error);
1533 const gchar *start_of_line;
1534 const gchar *end_of_line;
1537 start_of_line = data + i;
1538 end_of_line = memchr (start_of_line, '\n', length - i);
1540 if (end_of_line == NULL)
1541 end_of_line = data + length;
1543 line_length = end_of_line - start_of_line;
1545 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1552 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1555 GError *file_error = NULL;
1557 g_return_if_fail (key_file != NULL);
1559 if (!key_file->parse_buffer)
1564 if (key_file->parse_buffer->len > 0)
1566 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1567 key_file->parse_buffer->len,
1569 g_string_erase (key_file->parse_buffer, 0, -1);
1573 g_propagate_error (error, file_error);
1580 * g_key_file_to_data:
1581 * @key_file: a #GKeyFile
1582 * @length: (out) (optional): return location for the length of the
1583 * returned string, or %NULL
1584 * @error: return location for a #GError, or %NULL
1586 * This function outputs @key_file as a string.
1588 * Note that this function never reports an error,
1589 * so it is safe to pass %NULL as @error.
1591 * Returns: a newly allocated string holding
1592 * the contents of the #GKeyFile
1597 g_key_file_to_data (GKeyFile *key_file,
1601 GString *data_string;
1602 GList *group_node, *key_file_node;
1604 g_return_val_if_fail (key_file != NULL, NULL);
1606 data_string = g_string_new (NULL);
1608 for (group_node = g_list_last (key_file->groups);
1610 group_node = group_node->prev)
1612 GKeyFileGroup *group;
1614 group = (GKeyFileGroup *) group_node->data;
1616 if (group->name != NULL)
1617 g_string_append_printf (data_string, "[%s]\n", group->name);
1619 for (key_file_node = g_list_last (group->key_value_pairs);
1620 key_file_node != NULL;
1621 key_file_node = key_file_node->prev)
1623 GKeyFileKeyValuePair *pair;
1625 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1627 if (pair->key != NULL)
1628 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1630 g_string_append_printf (data_string, "%s\n", pair->value);
1635 *length = data_string->len;
1637 return g_string_free (data_string, FALSE);
1641 * g_key_file_get_keys:
1642 * @key_file: a #GKeyFile
1643 * @group_name: a group name
1644 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1645 * @error: return location for a #GError, or %NULL
1647 * Returns all keys for the group name @group_name. The array of
1648 * returned keys will be %NULL-terminated, so @length may
1649 * optionally be %NULL. In the event that the @group_name cannot
1650 * be found, %NULL is returned and @error is set to
1651 * %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1653 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1654 * Use g_strfreev() to free it.
1659 g_key_file_get_keys (GKeyFile *key_file,
1660 const gchar *group_name,
1664 GKeyFileGroup *group;
1669 g_return_val_if_fail (key_file != NULL, NULL);
1670 g_return_val_if_fail (group_name != NULL, NULL);
1672 group = g_key_file_lookup_group (key_file, group_name);
1676 g_set_error (error, G_KEY_FILE_ERROR,
1677 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1678 _("Key file does not have group “%s”"),
1684 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1686 GKeyFileKeyValuePair *pair;
1688 pair = (GKeyFileKeyValuePair *) tmp->data;
1694 keys = g_new (gchar *, num_keys + 1);
1697 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1699 GKeyFileKeyValuePair *pair;
1701 pair = (GKeyFileKeyValuePair *) tmp->data;
1705 keys[i] = g_strdup (pair->key);
1710 keys[num_keys] = NULL;
1719 * g_key_file_get_start_group:
1720 * @key_file: a #GKeyFile
1722 * Returns the name of the start group of the file.
1724 * Returns: (nullable): The start group of the key file.
1729 g_key_file_get_start_group (GKeyFile *key_file)
1731 g_return_val_if_fail (key_file != NULL, NULL);
1733 if (key_file->start_group)
1734 return g_strdup (key_file->start_group->name);
1740 * g_key_file_get_groups:
1741 * @key_file: a #GKeyFile
1742 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1744 * Returns all groups in the key file loaded with @key_file.
1745 * The array of returned groups will be %NULL-terminated, so
1746 * @length may optionally be %NULL.
1748 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1749 * Use g_strfreev() to free it.
1753 g_key_file_get_groups (GKeyFile *key_file,
1758 gsize i, num_groups;
1760 g_return_val_if_fail (key_file != NULL, NULL);
1762 num_groups = g_list_length (key_file->groups);
1764 g_return_val_if_fail (num_groups > 0, NULL);
1766 group_node = g_list_last (key_file->groups);
1768 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1770 /* Only need num_groups instead of num_groups + 1
1771 * because the first group of the file (last in the
1772 * list) is always the comment group at the top,
1775 groups = g_new (gchar *, num_groups);
1779 for (group_node = group_node->prev;
1781 group_node = group_node->prev)
1783 GKeyFileGroup *group;
1785 group = (GKeyFileGroup *) group_node->data;
1787 g_warn_if_fail (group->name != NULL);
1789 groups[i++] = g_strdup (group->name);
1800 set_not_found_key_error (const char *group_name,
1804 g_set_error (error, G_KEY_FILE_ERROR,
1805 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1806 _("Key file does not have key “%s” in group “%s”"),
1811 * g_key_file_get_value:
1812 * @key_file: a #GKeyFile
1813 * @group_name: a group name
1815 * @error: return location for a #GError, or %NULL
1817 * Returns the raw value associated with @key under @group_name.
1818 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1820 * In the event the key cannot be found, %NULL is returned and
1821 * @error is set to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1822 * event that the @group_name cannot be found, %NULL is returned
1823 * and @error is set to %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1826 * Returns: a newly allocated string or %NULL if the specified
1827 * key cannot be found.
1832 g_key_file_get_value (GKeyFile *key_file,
1833 const gchar *group_name,
1837 GKeyFileGroup *group;
1838 GKeyFileKeyValuePair *pair;
1839 gchar *value = NULL;
1841 g_return_val_if_fail (key_file != NULL, NULL);
1842 g_return_val_if_fail (group_name != NULL, NULL);
1843 g_return_val_if_fail (key != NULL, NULL);
1845 group = g_key_file_lookup_group (key_file, group_name);
1849 g_set_error (error, G_KEY_FILE_ERROR,
1850 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1851 _("Key file does not have group “%s”"),
1856 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1859 value = g_strdup (pair->value);
1861 set_not_found_key_error (group_name, key, error);
1867 * g_key_file_set_value:
1868 * @key_file: a #GKeyFile
1869 * @group_name: a group name
1873 * Associates a new value with @key under @group_name.
1875 * If @key cannot be found then it is created. If @group_name cannot
1876 * be found then it is created. To set an UTF-8 string which may contain
1877 * characters that need escaping (such as newlines or spaces), use
1878 * g_key_file_set_string().
1883 g_key_file_set_value (GKeyFile *key_file,
1884 const gchar *group_name,
1888 GKeyFileGroup *group;
1889 GKeyFileKeyValuePair *pair;
1891 g_return_if_fail (key_file != NULL);
1892 g_return_if_fail (group_name != NULL && g_key_file_is_group_name (group_name));
1893 g_return_if_fail (key != NULL && g_key_file_is_key_name (key, strlen (key)));
1894 g_return_if_fail (value != NULL);
1896 group = g_key_file_lookup_group (key_file, group_name);
1900 g_key_file_add_group (key_file, group_name, TRUE);
1901 group = (GKeyFileGroup *) key_file->groups->data;
1903 g_key_file_add_key (key_file, group, key, value);
1907 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1910 g_key_file_add_key (key_file, group, key, value);
1913 g_free (pair->value);
1914 pair->value = g_strdup (value);
1920 * g_key_file_get_string:
1921 * @key_file: a #GKeyFile
1922 * @group_name: a group name
1924 * @error: return location for a #GError, or %NULL
1926 * Returns the string value associated with @key under @group_name.
1927 * Unlike g_key_file_get_value(), this function handles escape sequences
1930 * In the event the key cannot be found, %NULL is returned and
1931 * @error is set to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1932 * event that the @group_name cannot be found, %NULL is returned
1933 * and @error is set to %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1935 * Returns: a newly allocated string or %NULL if the specified
1936 * key cannot be found.
1941 g_key_file_get_string (GKeyFile *key_file,
1942 const gchar *group_name,
1946 gchar *value, *string_value;
1947 GError *key_file_error;
1949 g_return_val_if_fail (key_file != NULL, NULL);
1950 g_return_val_if_fail (group_name != NULL, NULL);
1951 g_return_val_if_fail (key != NULL, NULL);
1953 key_file_error = NULL;
1955 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1959 g_propagate_error (error, key_file_error);
1963 if (!g_utf8_validate (value, -1, NULL))
1965 gchar *value_utf8 = g_utf8_make_valid (value, -1);
1966 g_set_error (error, G_KEY_FILE_ERROR,
1967 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1968 _("Key file contains key “%s” with value “%s” "
1969 "which is not UTF-8"), key, value_utf8);
1970 g_free (value_utf8);
1976 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1982 if (g_error_matches (key_file_error,
1984 G_KEY_FILE_ERROR_INVALID_VALUE))
1986 g_set_error (error, G_KEY_FILE_ERROR,
1987 G_KEY_FILE_ERROR_INVALID_VALUE,
1988 _("Key file contains key “%s” "
1989 "which has a value that cannot be interpreted."),
1991 g_error_free (key_file_error);
1994 g_propagate_error (error, key_file_error);
1997 return string_value;
2001 * g_key_file_set_string:
2002 * @key_file: a #GKeyFile
2003 * @group_name: a group name
2007 * Associates a new string value with @key under @group_name.
2008 * If @key cannot be found then it is created.
2009 * If @group_name cannot be found then it is created.
2010 * Unlike g_key_file_set_value(), this function handles characters
2011 * that need escaping, such as newlines.
2016 g_key_file_set_string (GKeyFile *key_file,
2017 const gchar *group_name,
2019 const gchar *string)
2023 g_return_if_fail (key_file != NULL);
2024 g_return_if_fail (string != NULL);
2026 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2027 g_key_file_set_value (key_file, group_name, key, value);
2032 * g_key_file_get_string_list:
2033 * @key_file: a #GKeyFile
2034 * @group_name: a group name
2036 * @length: (out) (optional): return location for the number of returned strings, or %NULL
2037 * @error: return location for a #GError, or %NULL
2039 * Returns the values associated with @key under @group_name.
2041 * In the event the key cannot be found, %NULL is returned and
2042 * @error is set to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
2043 * event that the @group_name cannot be found, %NULL is returned
2044 * and @error is set to %G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
2046 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2047 * a %NULL-terminated string array or %NULL if the specified
2048 * key cannot be found. The array should be freed with g_strfreev().
2053 g_key_file_get_string_list (GKeyFile *key_file,
2054 const gchar *group_name,
2059 GError *key_file_error = NULL;
2060 gchar *value, *string_value, **values;
2062 GSList *p, *pieces = NULL;
2064 g_return_val_if_fail (key_file != NULL, NULL);
2065 g_return_val_if_fail (group_name != NULL, NULL);
2066 g_return_val_if_fail (key != NULL, NULL);
2071 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2075 g_propagate_error (error, key_file_error);
2079 if (!g_utf8_validate (value, -1, NULL))
2081 gchar *value_utf8 = g_utf8_make_valid (value, -1);
2082 g_set_error (error, G_KEY_FILE_ERROR,
2083 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
2084 _("Key file contains key “%s” with value “%s” "
2085 "which is not UTF-8"), key, value_utf8);
2086 g_free (value_utf8);
2092 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
2094 g_free (string_value);
2098 if (g_error_matches (key_file_error,
2100 G_KEY_FILE_ERROR_INVALID_VALUE))
2102 g_set_error (error, G_KEY_FILE_ERROR,
2103 G_KEY_FILE_ERROR_INVALID_VALUE,
2104 _("Key file contains key “%s” "
2105 "which has a value that cannot be interpreted."),
2107 g_error_free (key_file_error);
2110 g_propagate_error (error, key_file_error);
2112 g_slist_free_full (pieces, g_free);
2116 len = g_slist_length (pieces);
2117 values = g_new (gchar *, len + 1);
2118 for (p = pieces, i = 0; p; p = p->next)
2119 values[i++] = p->data;
2122 g_slist_free (pieces);
2131 * g_key_file_set_string_list:
2132 * @key_file: a #GKeyFile
2133 * @group_name: a group name
2135 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2136 * @length: number of string values in @list
2138 * Associates a list of string values for @key under @group_name.
2139 * If @key cannot be found then it is created.
2140 * If @group_name cannot be found then it is created.
2145 g_key_file_set_string_list (GKeyFile *key_file,
2146 const gchar *group_name,
2148 const gchar * const list[],
2151 GString *value_list;
2154 g_return_if_fail (key_file != NULL);
2155 g_return_if_fail (list != NULL || length == 0);
2157 value_list = g_string_sized_new (length * 128);
2158 for (i = 0; i < length && list[i] != NULL; i++)
2162 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2163 g_string_append (value_list, value);
2164 g_string_append_c (value_list, key_file->list_separator);
2169 g_key_file_set_value (key_file, group_name, key, value_list->str);
2170 g_string_free (value_list, TRUE);
2174 * g_key_file_set_locale_string:
2175 * @key_file: a #GKeyFile
2176 * @group_name: a group name
2178 * @locale: a locale identifier
2181 * Associates a string value for @key and @locale under @group_name.
2182 * If the translation for @key cannot be found then it is created.
2187 g_key_file_set_locale_string (GKeyFile *key_file,
2188 const gchar *group_name,
2190 const gchar *locale,
2191 const gchar *string)
2193 gchar *full_key, *value;
2195 g_return_if_fail (key_file != NULL);
2196 g_return_if_fail (key != NULL);
2197 g_return_if_fail (locale != NULL);
2198 g_return_if_fail (string != NULL);
2200 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2201 full_key = g_strdup_printf ("%s[%s]", key, locale);
2202 g_key_file_set_value (key_file, group_name, full_key, value);
2208 * g_key_file_get_locale_string:
2209 * @key_file: a #GKeyFile
2210 * @group_name: a group name
2212 * @locale: (nullable): a locale identifier or %NULL
2213 * @error: return location for a #GError, or %NULL
2215 * Returns the value associated with @key under @group_name
2216 * translated in the given @locale if available. If @locale is
2217 * %NULL then the current locale is assumed.
2219 * If @locale is to be non-%NULL, or if the current locale will change over
2220 * the lifetime of the #GKeyFile, it must be loaded with
2221 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2223 * If @key cannot be found then %NULL is returned and @error is set
2224 * to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2225 * with @key cannot be interpreted or no suitable translation can
2226 * be found then the untranslated value is returned.
2228 * Returns: a newly allocated string or %NULL if the specified
2229 * key cannot be found.
2234 g_key_file_get_locale_string (GKeyFile *key_file,
2235 const gchar *group_name,
2237 const gchar *locale,
2240 gchar *candidate_key, *translated_value;
2241 GError *key_file_error;
2243 gboolean free_languages = FALSE;
2246 g_return_val_if_fail (key_file != NULL, NULL);
2247 g_return_val_if_fail (group_name != NULL, NULL);
2248 g_return_val_if_fail (key != NULL, NULL);
2250 candidate_key = NULL;
2251 translated_value = NULL;
2252 key_file_error = NULL;
2256 languages = g_get_locale_variants (locale);
2257 free_languages = TRUE;
2261 languages = (gchar **) g_get_language_names ();
2262 free_languages = FALSE;
2265 for (i = 0; languages[i]; i++)
2267 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2269 translated_value = g_key_file_get_string (key_file,
2271 candidate_key, NULL);
2272 g_free (candidate_key);
2274 if (translated_value)
2278 /* Fallback to untranslated key
2280 if (!translated_value)
2282 translated_value = g_key_file_get_string (key_file, group_name, key,
2285 if (!translated_value)
2286 g_propagate_error (error, key_file_error);
2290 g_strfreev (languages);
2292 return translated_value;
2296 * g_key_file_get_locale_for_key:
2297 * @key_file: a #GKeyFile
2298 * @group_name: a group name
2300 * @locale: (nullable): a locale identifier or %NULL
2302 * Returns the actual locale which the result of
2303 * g_key_file_get_locale_string() or g_key_file_get_locale_string_list()
2306 * If calling g_key_file_get_locale_string() or
2307 * g_key_file_get_locale_string_list() with exactly the same @key_file,
2308 * @group_name, @key and @locale, the result of those functions will
2309 * have originally been tagged with the locale that is the result of
2312 * Returns: (nullable): the locale from the file, or %NULL if the key was not
2313 * found or the entry in the file was was untranslated
2318 g_key_file_get_locale_for_key (GKeyFile *key_file,
2319 const gchar *group_name,
2321 const gchar *locale)
2323 gchar **languages_allocated = NULL;
2324 const gchar * const *languages;
2325 gchar *result = NULL;
2328 g_return_val_if_fail (key_file != NULL, NULL);
2329 g_return_val_if_fail (group_name != NULL, NULL);
2330 g_return_val_if_fail (key != NULL, NULL);
2334 languages_allocated = g_get_locale_variants (locale);
2335 languages = (const gchar * const *) languages_allocated;
2338 languages = g_get_language_names ();
2340 for (i = 0; languages[i] != NULL; i++)
2342 gchar *candidate_key, *translated_value;
2344 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2345 translated_value = g_key_file_get_string (key_file, group_name, candidate_key, NULL);
2346 g_free (translated_value);
2347 g_free (candidate_key);
2349 if (translated_value != NULL)
2353 result = g_strdup (languages[i]);
2355 g_strfreev (languages_allocated);
2361 * g_key_file_get_locale_string_list:
2362 * @key_file: a #GKeyFile
2363 * @group_name: a group name
2365 * @locale: (nullable): a locale identifier or %NULL
2366 * @length: (out) (optional): return location for the number of returned strings or %NULL
2367 * @error: return location for a #GError or %NULL
2369 * Returns the values associated with @key under @group_name
2370 * translated in the given @locale if available. If @locale is
2371 * %NULL then the current locale is assumed.
2373 * If @locale is to be non-%NULL, or if the current locale will change over
2374 * the lifetime of the #GKeyFile, it must be loaded with
2375 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2377 * If @key cannot be found then %NULL is returned and @error is set
2378 * to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2379 * with @key cannot be interpreted or no suitable translations
2380 * can be found then the untranslated values are returned. The
2381 * returned array is %NULL-terminated, so @length may optionally
2384 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2385 * or %NULL if the key isn't found. The string array should be freed
2386 * with g_strfreev().
2391 g_key_file_get_locale_string_list (GKeyFile *key_file,
2392 const gchar *group_name,
2394 const gchar *locale,
2398 GError *key_file_error;
2399 gchar **values, *value;
2400 char list_separator[2];
2403 g_return_val_if_fail (key_file != NULL, NULL);
2404 g_return_val_if_fail (group_name != NULL, NULL);
2405 g_return_val_if_fail (key != NULL, NULL);
2407 key_file_error = NULL;
2409 value = g_key_file_get_locale_string (key_file, group_name,
2414 g_propagate_error (error, key_file_error);
2423 len = strlen (value);
2424 if (value[len - 1] == key_file->list_separator)
2425 value[len - 1] = '\0';
2427 list_separator[0] = key_file->list_separator;
2428 list_separator[1] = '\0';
2429 values = g_strsplit (value, list_separator, 0);
2434 *length = g_strv_length (values);
2440 * g_key_file_set_locale_string_list:
2441 * @key_file: a #GKeyFile
2442 * @group_name: a group name
2444 * @locale: a locale identifier
2445 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2446 * @length: the length of @list
2448 * Associates a list of string values for @key and @locale under
2449 * @group_name. If the translation for @key cannot be found then
2455 g_key_file_set_locale_string_list (GKeyFile *key_file,
2456 const gchar *group_name,
2458 const gchar *locale,
2459 const gchar * const list[],
2462 GString *value_list;
2466 g_return_if_fail (key_file != NULL);
2467 g_return_if_fail (key != NULL);
2468 g_return_if_fail (locale != NULL);
2469 g_return_if_fail (length != 0);
2471 value_list = g_string_sized_new (length * 128);
2472 for (i = 0; i < length && list[i] != NULL; i++)
2476 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2477 g_string_append (value_list, value);
2478 g_string_append_c (value_list, key_file->list_separator);
2483 full_key = g_strdup_printf ("%s[%s]", key, locale);
2484 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2486 g_string_free (value_list, TRUE);
2490 * g_key_file_get_boolean:
2491 * @key_file: a #GKeyFile
2492 * @group_name: a group name
2494 * @error: return location for a #GError
2496 * Returns the value associated with @key under @group_name as a
2499 * If @key cannot be found then %FALSE is returned and @error is set
2500 * to %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2501 * associated with @key cannot be interpreted as a boolean then %FALSE
2502 * is returned and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2504 * Returns: the value associated with the key as a boolean,
2505 * or %FALSE if the key was not found or could not be parsed.
2510 g_key_file_get_boolean (GKeyFile *key_file,
2511 const gchar *group_name,
2515 GError *key_file_error = NULL;
2517 gboolean bool_value;
2519 g_return_val_if_fail (key_file != NULL, FALSE);
2520 g_return_val_if_fail (group_name != NULL, FALSE);
2521 g_return_val_if_fail (key != NULL, FALSE);
2523 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2527 g_propagate_error (error, key_file_error);
2531 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2537 if (g_error_matches (key_file_error,
2539 G_KEY_FILE_ERROR_INVALID_VALUE))
2541 g_set_error (error, G_KEY_FILE_ERROR,
2542 G_KEY_FILE_ERROR_INVALID_VALUE,
2543 _("Key file contains key “%s” "
2544 "which has a value that cannot be interpreted."),
2546 g_error_free (key_file_error);
2549 g_propagate_error (error, key_file_error);
2556 * g_key_file_set_boolean:
2557 * @key_file: a #GKeyFile
2558 * @group_name: a group name
2560 * @value: %TRUE or %FALSE
2562 * Associates a new boolean value with @key under @group_name.
2563 * If @key cannot be found then it is created.
2568 g_key_file_set_boolean (GKeyFile *key_file,
2569 const gchar *group_name,
2573 const gchar *result;
2575 g_return_if_fail (key_file != NULL);
2577 result = g_key_file_parse_boolean_as_value (key_file, value);
2578 g_key_file_set_value (key_file, group_name, key, result);
2582 * g_key_file_get_boolean_list:
2583 * @key_file: a #GKeyFile
2584 * @group_name: a group name
2586 * @length: (out): the number of booleans returned
2587 * @error: return location for a #GError
2589 * Returns the values associated with @key under @group_name as
2592 * If @key cannot be found then %NULL is returned and @error is set to
2593 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2594 * with @key cannot be interpreted as booleans then %NULL is returned
2595 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2597 * Returns: (array length=length) (element-type gboolean) (transfer container):
2598 * the values associated with the key as a list of booleans, or %NULL if the
2599 * key was not found or could not be parsed. The returned list of booleans
2600 * should be freed with g_free() when no longer needed.
2605 g_key_file_get_boolean_list (GKeyFile *key_file,
2606 const gchar *group_name,
2611 GError *key_file_error;
2613 gboolean *bool_values;
2616 g_return_val_if_fail (key_file != NULL, NULL);
2617 g_return_val_if_fail (group_name != NULL, NULL);
2618 g_return_val_if_fail (key != NULL, NULL);
2623 key_file_error = NULL;
2625 values = g_key_file_get_string_list (key_file, group_name, key,
2626 &num_bools, &key_file_error);
2629 g_propagate_error (error, key_file_error);
2634 bool_values = g_new (gboolean, num_bools);
2636 for (i = 0; i < num_bools; i++)
2638 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2644 g_propagate_error (error, key_file_error);
2645 g_strfreev (values);
2646 g_free (bool_values);
2651 g_strfreev (values);
2654 *length = num_bools;
2660 * g_key_file_set_boolean_list:
2661 * @key_file: a #GKeyFile
2662 * @group_name: a group name
2664 * @list: (array length=length): an array of boolean values
2665 * @length: length of @list
2667 * Associates a list of boolean values with @key under @group_name.
2668 * If @key cannot be found then it is created.
2669 * If @group_name is %NULL, the start_group is used.
2674 g_key_file_set_boolean_list (GKeyFile *key_file,
2675 const gchar *group_name,
2680 GString *value_list;
2683 g_return_if_fail (key_file != NULL);
2684 g_return_if_fail (list != NULL);
2686 value_list = g_string_sized_new (length * 8);
2687 for (i = 0; i < length; i++)
2691 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2693 g_string_append (value_list, value);
2694 g_string_append_c (value_list, key_file->list_separator);
2697 g_key_file_set_value (key_file, group_name, key, value_list->str);
2698 g_string_free (value_list, TRUE);
2702 * g_key_file_get_integer:
2703 * @key_file: a #GKeyFile
2704 * @group_name: a group name
2706 * @error: return location for a #GError
2708 * Returns the value associated with @key under @group_name as an
2711 * If @key cannot be found then 0 is returned and @error is set to
2712 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2713 * with @key cannot be interpreted as an integer, or is out of range
2714 * for a #gint, then 0 is returned
2715 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2717 * Returns: the value associated with the key as an integer, or
2718 * 0 if the key was not found or could not be parsed.
2723 g_key_file_get_integer (GKeyFile *key_file,
2724 const gchar *group_name,
2728 GError *key_file_error;
2732 g_return_val_if_fail (key_file != NULL, -1);
2733 g_return_val_if_fail (group_name != NULL, -1);
2734 g_return_val_if_fail (key != NULL, -1);
2736 key_file_error = NULL;
2738 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2742 g_propagate_error (error, key_file_error);
2746 int_value = g_key_file_parse_value_as_integer (key_file, value,
2752 if (g_error_matches (key_file_error,
2754 G_KEY_FILE_ERROR_INVALID_VALUE))
2756 g_set_error (error, G_KEY_FILE_ERROR,
2757 G_KEY_FILE_ERROR_INVALID_VALUE,
2758 _("Key file contains key “%s” in group “%s” "
2759 "which has a value that cannot be interpreted."),
2761 g_error_free (key_file_error);
2764 g_propagate_error (error, key_file_error);
2771 * g_key_file_set_integer:
2772 * @key_file: a #GKeyFile
2773 * @group_name: a group name
2775 * @value: an integer value
2777 * Associates a new integer value with @key under @group_name.
2778 * If @key cannot be found then it is created.
2783 g_key_file_set_integer (GKeyFile *key_file,
2784 const gchar *group_name,
2790 g_return_if_fail (key_file != NULL);
2792 result = g_key_file_parse_integer_as_value (key_file, value);
2793 g_key_file_set_value (key_file, group_name, key, result);
2798 * g_key_file_get_int64:
2799 * @key_file: a non-%NULL #GKeyFile
2800 * @group_name: a non-%NULL group name
2801 * @key: a non-%NULL key
2802 * @error: return location for a #GError
2804 * Returns the value associated with @key under @group_name as a signed
2805 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2806 * 64-bit results without truncation.
2808 * Returns: the value associated with the key as a signed 64-bit integer, or
2809 * 0 if the key was not found or could not be parsed.
2814 g_key_file_get_int64 (GKeyFile *key_file,
2815 const gchar *group_name,
2822 g_return_val_if_fail (key_file != NULL, -1);
2823 g_return_val_if_fail (group_name != NULL, -1);
2824 g_return_val_if_fail (key != NULL, -1);
2826 s = g_key_file_get_value (key_file, group_name, key, error);
2831 v = g_ascii_strtoll (s, &end, 10);
2833 if (*s == '\0' || *end != '\0')
2835 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2836 _("Key “%s” in group “%s” has value “%s” "
2837 "where %s was expected"),
2838 key, group_name, s, "int64");
2848 * g_key_file_set_int64:
2849 * @key_file: a #GKeyFile
2850 * @group_name: a group name
2852 * @value: an integer value
2854 * Associates a new integer value with @key under @group_name.
2855 * If @key cannot be found then it is created.
2860 g_key_file_set_int64 (GKeyFile *key_file,
2861 const gchar *group_name,
2867 g_return_if_fail (key_file != NULL);
2869 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2870 g_key_file_set_value (key_file, group_name, key, result);
2875 * g_key_file_get_uint64:
2876 * @key_file: a non-%NULL #GKeyFile
2877 * @group_name: a non-%NULL group name
2878 * @key: a non-%NULL key
2879 * @error: return location for a #GError
2881 * Returns the value associated with @key under @group_name as an unsigned
2882 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2883 * large positive results without truncation.
2885 * Returns: the value associated with the key as an unsigned 64-bit integer,
2886 * or 0 if the key was not found or could not be parsed.
2891 g_key_file_get_uint64 (GKeyFile *key_file,
2892 const gchar *group_name,
2899 g_return_val_if_fail (key_file != NULL, -1);
2900 g_return_val_if_fail (group_name != NULL, -1);
2901 g_return_val_if_fail (key != NULL, -1);
2903 s = g_key_file_get_value (key_file, group_name, key, error);
2908 v = g_ascii_strtoull (s, &end, 10);
2910 if (*s == '\0' || *end != '\0')
2912 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2913 _("Key “%s” in group “%s” has value “%s” "
2914 "where %s was expected"),
2915 key, group_name, s, "uint64");
2925 * g_key_file_set_uint64:
2926 * @key_file: a #GKeyFile
2927 * @group_name: a group name
2929 * @value: an integer value
2931 * Associates a new integer value with @key under @group_name.
2932 * If @key cannot be found then it is created.
2937 g_key_file_set_uint64 (GKeyFile *key_file,
2938 const gchar *group_name,
2944 g_return_if_fail (key_file != NULL);
2946 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2947 g_key_file_set_value (key_file, group_name, key, result);
2952 * g_key_file_get_integer_list:
2953 * @key_file: a #GKeyFile
2954 * @group_name: a group name
2956 * @length: (out): the number of integers returned
2957 * @error: return location for a #GError
2959 * Returns the values associated with @key under @group_name as
2962 * If @key cannot be found then %NULL is returned and @error is set to
2963 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2964 * with @key cannot be interpreted as integers, or are out of range for
2965 * #gint, then %NULL is returned
2966 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
2968 * Returns: (array length=length) (element-type gint) (transfer container):
2969 * the values associated with the key as a list of integers, or %NULL if
2970 * the key was not found or could not be parsed. The returned list of
2971 * integers should be freed with g_free() when no longer needed.
2976 g_key_file_get_integer_list (GKeyFile *key_file,
2977 const gchar *group_name,
2982 GError *key_file_error = NULL;
2987 g_return_val_if_fail (key_file != NULL, NULL);
2988 g_return_val_if_fail (group_name != NULL, NULL);
2989 g_return_val_if_fail (key != NULL, NULL);
2994 values = g_key_file_get_string_list (key_file, group_name, key,
2995 &num_ints, &key_file_error);
2998 g_propagate_error (error, key_file_error);
3003 int_values = g_new (gint, num_ints);
3005 for (i = 0; i < num_ints; i++)
3007 int_values[i] = g_key_file_parse_value_as_integer (key_file,
3013 g_propagate_error (error, key_file_error);
3014 g_strfreev (values);
3015 g_free (int_values);
3020 g_strfreev (values);
3029 * g_key_file_set_integer_list:
3030 * @key_file: a #GKeyFile
3031 * @group_name: a group name
3033 * @list: (array length=length): an array of integer values
3034 * @length: number of integer values in @list
3036 * Associates a list of integer values with @key under @group_name.
3037 * If @key cannot be found then it is created.
3042 g_key_file_set_integer_list (GKeyFile *key_file,
3043 const gchar *group_name,
3051 g_return_if_fail (key_file != NULL);
3052 g_return_if_fail (list != NULL);
3054 values = g_string_sized_new (length * 16);
3055 for (i = 0; i < length; i++)
3059 value = g_key_file_parse_integer_as_value (key_file, list[i]);
3061 g_string_append (values, value);
3062 g_string_append_c (values, key_file->list_separator);
3067 g_key_file_set_value (key_file, group_name, key, values->str);
3068 g_string_free (values, TRUE);
3072 * g_key_file_get_double:
3073 * @key_file: a #GKeyFile
3074 * @group_name: a group name
3076 * @error: return location for a #GError
3078 * Returns the value associated with @key under @group_name as a
3079 * double. If @group_name is %NULL, the start_group is used.
3081 * If @key cannot be found then 0.0 is returned and @error is set to
3082 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
3083 * with @key cannot be interpreted as a double then 0.0 is returned
3084 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
3086 * Returns: the value associated with the key as a double, or
3087 * 0.0 if the key was not found or could not be parsed.
3092 g_key_file_get_double (GKeyFile *key_file,
3093 const gchar *group_name,
3097 GError *key_file_error;
3099 gdouble double_value;
3101 g_return_val_if_fail (key_file != NULL, -1);
3102 g_return_val_if_fail (group_name != NULL, -1);
3103 g_return_val_if_fail (key != NULL, -1);
3105 key_file_error = NULL;
3107 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3111 g_propagate_error (error, key_file_error);
3115 double_value = g_key_file_parse_value_as_double (key_file, value,
3121 if (g_error_matches (key_file_error,
3123 G_KEY_FILE_ERROR_INVALID_VALUE))
3125 g_set_error (error, G_KEY_FILE_ERROR,
3126 G_KEY_FILE_ERROR_INVALID_VALUE,
3127 _("Key file contains key “%s” in group “%s” "
3128 "which has a value that cannot be interpreted."),
3130 g_error_free (key_file_error);
3133 g_propagate_error (error, key_file_error);
3136 return double_value;
3140 * g_key_file_set_double:
3141 * @key_file: a #GKeyFile
3142 * @group_name: a group name
3144 * @value: a double value
3146 * Associates a new double value with @key under @group_name.
3147 * If @key cannot be found then it is created.
3152 g_key_file_set_double (GKeyFile *key_file,
3153 const gchar *group_name,
3157 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3159 g_return_if_fail (key_file != NULL);
3161 g_ascii_dtostr (result, sizeof (result), value);
3162 g_key_file_set_value (key_file, group_name, key, result);
3166 * g_key_file_get_double_list:
3167 * @key_file: a #GKeyFile
3168 * @group_name: a group name
3170 * @length: (out): the number of doubles returned
3171 * @error: return location for a #GError
3173 * Returns the values associated with @key under @group_name as
3176 * If @key cannot be found then %NULL is returned and @error is set to
3177 * %G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3178 * with @key cannot be interpreted as doubles then %NULL is returned
3179 * and @error is set to %G_KEY_FILE_ERROR_INVALID_VALUE.
3181 * Returns: (array length=length) (element-type gdouble) (transfer container):
3182 * the values associated with the key as a list of doubles, or %NULL if the
3183 * key was not found or could not be parsed. The returned list of doubles
3184 * should be freed with g_free() when no longer needed.
3189 g_key_file_get_double_list (GKeyFile *key_file,
3190 const gchar *group_name,
3195 GError *key_file_error = NULL;
3197 gdouble *double_values;
3198 gsize i, num_doubles;
3200 g_return_val_if_fail (key_file != NULL, NULL);
3201 g_return_val_if_fail (group_name != NULL, NULL);
3202 g_return_val_if_fail (key != NULL, NULL);
3207 values = g_key_file_get_string_list (key_file, group_name, key,
3208 &num_doubles, &key_file_error);
3211 g_propagate_error (error, key_file_error);
3216 double_values = g_new (gdouble, num_doubles);
3218 for (i = 0; i < num_doubles; i++)
3220 double_values[i] = g_key_file_parse_value_as_double (key_file,
3226 g_propagate_error (error, key_file_error);
3227 g_strfreev (values);
3228 g_free (double_values);
3233 g_strfreev (values);
3236 *length = num_doubles;
3238 return double_values;
3242 * g_key_file_set_double_list:
3243 * @key_file: a #GKeyFile
3244 * @group_name: a group name
3246 * @list: (array length=length): an array of double values
3247 * @length: number of double values in @list
3249 * Associates a list of double values with @key under
3250 * @group_name. If @key cannot be found then it is created.
3255 g_key_file_set_double_list (GKeyFile *key_file,
3256 const gchar *group_name,
3264 g_return_if_fail (key_file != NULL);
3265 g_return_if_fail (list != NULL);
3267 values = g_string_sized_new (length * 16);
3268 for (i = 0; i < length; i++)
3270 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3272 g_ascii_dtostr( result, sizeof (result), list[i] );
3274 g_string_append (values, result);
3275 g_string_append_c (values, key_file->list_separator);
3278 g_key_file_set_value (key_file, group_name, key, values->str);
3279 g_string_free (values, TRUE);
3283 g_key_file_set_key_comment (GKeyFile *key_file,
3284 const gchar *group_name,
3286 const gchar *comment,
3289 GKeyFileGroup *group;
3290 GKeyFileKeyValuePair *pair;
3291 GList *key_node, *comment_node, *tmp;
3293 group = g_key_file_lookup_group (key_file, group_name);
3296 g_set_error (error, G_KEY_FILE_ERROR,
3297 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3298 _("Key file does not have group “%s”"),
3299 group_name ? group_name : "(null)");
3304 /* First find the key the comments are supposed to be
3307 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3309 if (key_node == NULL)
3311 set_not_found_key_error (group->name, key, error);
3315 /* Then find all the comments already associated with the
3318 tmp = key_node->next;
3321 pair = (GKeyFileKeyValuePair *) tmp->data;
3323 if (pair->key != NULL)
3328 g_key_file_remove_key_value_pair_node (key_file, group,
3332 if (comment == NULL)
3335 /* Now we can add our new comment
3337 pair = g_new (GKeyFileKeyValuePair, 1);
3339 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3341 key_node = g_list_insert (key_node, pair, 1);
3348 g_key_file_set_top_comment (GKeyFile *key_file,
3349 const gchar *comment,
3353 GKeyFileGroup *group;
3354 GKeyFileKeyValuePair *pair;
3356 /* The last group in the list should be the top (comments only)
3359 g_warn_if_fail (key_file->groups != NULL);
3360 group_node = g_list_last (key_file->groups);
3361 group = (GKeyFileGroup *) group_node->data;
3362 g_warn_if_fail (group->name == NULL);
3364 /* Note all keys must be comments at the top of
3365 * the file, so we can just free it all.
3367 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3368 group->key_value_pairs = NULL;
3370 if (comment == NULL)
3373 pair = g_new (GKeyFileKeyValuePair, 1);
3375 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3377 group->key_value_pairs =
3378 g_list_prepend (group->key_value_pairs, pair);
3384 g_key_file_set_group_comment (GKeyFile *key_file,
3385 const gchar *group_name,
3386 const gchar *comment,
3389 GKeyFileGroup *group;
3391 GKeyFileKeyValuePair *pair;
3393 g_return_val_if_fail (group_name != NULL && g_key_file_is_group_name (group_name), FALSE);
3395 group = g_key_file_lookup_group (key_file, group_name);
3398 g_set_error (error, G_KEY_FILE_ERROR,
3399 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3400 _("Key file does not have group “%s”"),
3406 if (group == key_file->start_group)
3407 return g_key_file_set_top_comment (key_file, comment, error);
3409 /* First remove any existing comment
3411 group_node = g_key_file_lookup_group_node (key_file, group_name);
3412 group = group_node->next->data;
3413 for (GList *lp = group->key_value_pairs; lp != NULL; )
3415 GList *lnext = lp->next;
3417 if (pair->key != NULL)
3420 g_key_file_remove_key_value_pair_node (key_file, group, lp);
3424 if (comment == NULL)
3427 /* Now we can add our new comment
3429 pair = g_new (GKeyFileKeyValuePair, 1);
3431 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3432 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3438 * g_key_file_set_comment:
3439 * @key_file: a #GKeyFile
3440 * @group_name: (nullable): a group name, or %NULL
3441 * @key: (nullable): a key
3442 * @comment: a comment
3443 * @error: return location for a #GError
3445 * Places a comment above @key from @group_name.
3447 * If @key is %NULL then @comment will be written above @group_name.
3448 * If both @key and @group_name are %NULL, then @comment will be
3449 * written above the first group in the file.
3451 * Note that this function prepends a '#' comment marker to
3452 * each line of @comment.
3454 * Returns: %TRUE if the comment was written, %FALSE otherwise
3459 g_key_file_set_comment (GKeyFile *key_file,
3460 const gchar *group_name,
3462 const gchar *comment,
3465 g_return_val_if_fail (key_file != NULL, FALSE);
3467 if (group_name != NULL && key != NULL)
3469 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3472 else if (group_name != NULL)
3474 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3479 if (!g_key_file_set_top_comment (key_file, comment, error))
3487 g_key_file_get_key_comment (GKeyFile *key_file,
3488 const gchar *group_name,
3492 GKeyFileGroup *group;
3493 GKeyFileKeyValuePair *pair;
3494 GList *key_node, *tmp;
3498 g_return_val_if_fail (group_name != NULL && g_key_file_is_group_name (group_name), NULL);
3500 group = g_key_file_lookup_group (key_file, group_name);
3503 g_set_error (error, G_KEY_FILE_ERROR,
3504 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3505 _("Key file does not have group “%s”"),
3506 group_name ? group_name : "(null)");
3511 /* First find the key the comments are supposed to be
3514 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3516 if (key_node == NULL)
3518 set_not_found_key_error (group->name, key, error);
3524 /* Then find all the comments already associated with the
3525 * key and concatenate them.
3527 tmp = key_node->next;
3528 if (!key_node->next)
3531 pair = (GKeyFileKeyValuePair *) tmp->data;
3532 if (pair->key != NULL)
3537 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3539 if (pair->key != NULL)
3545 while (tmp != key_node)
3547 pair = (GKeyFileKeyValuePair *) tmp->data;
3550 string = g_string_sized_new (512);
3552 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3553 (tmp->prev == key_node));
3554 g_string_append (string, comment);
3561 comment = g_string_free_and_steal (g_steal_pointer (&string));
3569 get_group_comment (GKeyFile *key_file,
3570 GKeyFileGroup *group,
3579 tmp = group->key_value_pairs;
3582 GKeyFileKeyValuePair *pair;
3584 pair = (GKeyFileKeyValuePair *) tmp->data;
3586 if (pair->key != NULL)
3592 if (tmp->next == NULL)
3600 GKeyFileKeyValuePair *pair;
3602 pair = (GKeyFileKeyValuePair *) tmp->data;
3605 string = g_string_sized_new (512);
3607 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3608 (tmp->prev == NULL));
3609 g_string_append (string, comment);
3616 return g_string_free (string, FALSE);
3622 g_key_file_get_group_comment (GKeyFile *key_file,
3623 const gchar *group_name,
3627 GKeyFileGroup *group;
3629 group = g_key_file_lookup_group (key_file, group_name);
3632 g_set_error (error, G_KEY_FILE_ERROR,
3633 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3634 _("Key file does not have group “%s”"),
3635 group_name ? group_name : "(null)");
3640 group_node = g_key_file_lookup_group_node (key_file, group_name);
3641 group_node = group_node->next;
3642 group = (GKeyFileGroup *)group_node->data;
3643 return get_group_comment (key_file, group, error);
3647 g_key_file_get_top_comment (GKeyFile *key_file,
3651 GKeyFileGroup *group;
3653 /* The last group in the list should be the top (comments only)
3656 g_warn_if_fail (key_file->groups != NULL);
3657 group_node = g_list_last (key_file->groups);
3658 group = (GKeyFileGroup *) group_node->data;
3659 g_warn_if_fail (group->name == NULL);
3661 return get_group_comment (key_file, group, error);
3665 * g_key_file_get_comment:
3666 * @key_file: a #GKeyFile
3667 * @group_name: (nullable): a group name, or %NULL
3668 * @key: (nullable): a key
3669 * @error: return location for a #GError
3671 * Retrieves a comment above @key from @group_name.
3672 * If @key is %NULL then @comment will be read from above
3673 * @group_name. If both @key and @group_name are %NULL, then
3674 * @comment will be read from above the first group in the file.
3676 * Note that the returned string does not include the '#' comment markers,
3677 * but does include any whitespace after them (on each line). It includes
3678 * the line breaks between lines, but does not include the final line break.
3680 * Returns: a comment that should be freed with g_free()
3685 g_key_file_get_comment (GKeyFile *key_file,
3686 const gchar *group_name,
3690 g_return_val_if_fail (key_file != NULL, NULL);
3692 if (group_name != NULL && key != NULL)
3693 return g_key_file_get_key_comment (key_file, group_name, key, error);
3694 else if (group_name != NULL)
3695 return g_key_file_get_group_comment (key_file, group_name, error);
3697 return g_key_file_get_top_comment (key_file, error);
3701 * g_key_file_remove_comment:
3702 * @key_file: a #GKeyFile
3703 * @group_name: (nullable): a group name, or %NULL
3704 * @key: (nullable): a key
3705 * @error: return location for a #GError
3707 * Removes a comment above @key from @group_name.
3708 * If @key is %NULL then @comment will be removed above @group_name.
3709 * If both @key and @group_name are %NULL, then @comment will
3710 * be removed above the first group in the file.
3712 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3718 g_key_file_remove_comment (GKeyFile *key_file,
3719 const gchar *group_name,
3723 g_return_val_if_fail (key_file != NULL, FALSE);
3725 if (group_name != NULL && key != NULL)
3726 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3727 else if (group_name != NULL)
3728 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3730 return g_key_file_set_top_comment (key_file, NULL, error);
3734 * g_key_file_has_group:
3735 * @key_file: a #GKeyFile
3736 * @group_name: a group name
3738 * Looks whether the key file has the group @group_name.
3740 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3745 g_key_file_has_group (GKeyFile *key_file,
3746 const gchar *group_name)
3748 g_return_val_if_fail (key_file != NULL, FALSE);
3749 g_return_val_if_fail (group_name != NULL, FALSE);
3751 return g_key_file_lookup_group (key_file, group_name) != NULL;
3754 /* This code remains from a historical attempt to add a new public API
3755 * which respects the GError rules.
3758 g_key_file_has_key_full (GKeyFile *key_file,
3759 const gchar *group_name,
3764 GKeyFileKeyValuePair *pair;
3765 GKeyFileGroup *group;
3767 g_return_val_if_fail (key_file != NULL, FALSE);
3768 g_return_val_if_fail (group_name != NULL, FALSE);
3769 g_return_val_if_fail (key != NULL, FALSE);
3771 group = g_key_file_lookup_group (key_file, group_name);
3775 g_set_error (error, G_KEY_FILE_ERROR,
3776 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3777 _("Key file does not have group “%s”"),
3783 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3786 *has_key = pair != NULL;
3791 * g_key_file_has_key: (skip)
3792 * @key_file: a #GKeyFile
3793 * @group_name: a group name
3795 * @error: return location for a #GError
3797 * Looks whether the key file has the key @key in the group
3800 * Note that this function does not follow the rules for #GError strictly;
3801 * the return value both carries meaning and signals an error. To use
3802 * this function, you must pass a #GError pointer in @error, and check
3803 * whether it is not %NULL to see if an error occurred.
3805 * Language bindings should use g_key_file_get_value() to test whether
3806 * or not a key exists.
3808 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3813 g_key_file_has_key (GKeyFile *key_file,
3814 const gchar *group_name,
3818 GError *temp_error = NULL;
3821 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3827 g_propagate_error (error, temp_error);
3833 g_key_file_add_group (GKeyFile *key_file,
3834 const gchar *group_name,
3837 GKeyFileGroup *group;
3839 g_return_if_fail (key_file != NULL);
3840 g_return_if_fail (group_name != NULL && g_key_file_is_group_name (group_name));
3842 group = g_key_file_lookup_group (key_file, group_name);
3845 key_file->current_group = group;
3849 group = g_new0 (GKeyFileGroup, 1);
3850 group->name = g_strdup (group_name);
3851 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3852 key_file->groups = g_list_prepend (key_file->groups, group);
3853 key_file->current_group = group;
3855 if (key_file->start_group == NULL)
3857 key_file->start_group = group;
3859 else if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS) || created)
3861 /* separate groups by a blank line if we don't keep comments or group is created */
3862 GKeyFileGroup *next_group = key_file->groups->next->data;
3863 GKeyFileKeyValuePair *pair;
3864 if (next_group->key_value_pairs != NULL)
3865 pair = next_group->key_value_pairs->data;
3867 if (next_group->key_value_pairs == NULL ||
3868 (pair->key != NULL && !g_strstr_len (pair->value, -1, "\n")))
3870 GKeyFileKeyValuePair *pair = g_new (GKeyFileKeyValuePair, 1);
3872 pair->value = g_strdup ("");
3873 next_group->key_value_pairs = g_list_prepend (next_group->key_value_pairs, pair);
3877 if (!key_file->group_hash)
3878 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
3880 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3884 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3889 g_free (pair->value);
3890 g_free_sized (pair, sizeof (GKeyFileKeyValuePair));
3894 /* Be careful not to call this function on a node with data in the
3895 * lookup map without removing it from the lookup map, first.
3897 * Some current cases where this warning is not a concern are
3899 * - the node being removed is a comment node
3900 * - the entire lookup map is getting destroyed soon after
3904 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3905 GKeyFileGroup *group,
3909 GKeyFileKeyValuePair *pair;
3911 pair = (GKeyFileKeyValuePair *) pair_node->data;
3913 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3915 g_warn_if_fail (pair->value != NULL);
3917 g_key_file_key_value_pair_free (pair);
3919 g_list_free_1 (pair_node);
3923 g_key_file_remove_group_node (GKeyFile *key_file,
3926 GKeyFileGroup *group;
3929 group = (GKeyFileGroup *) group_node->data;
3933 g_assert (key_file->group_hash);
3934 g_hash_table_remove (key_file->group_hash, group->name);
3937 /* If the current group gets deleted make the current group the last
3940 if (key_file->current_group == group)
3942 /* groups should always contain at least the top comment group,
3943 * unless g_key_file_clear has been called
3945 if (key_file->groups)
3946 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3948 key_file->current_group = NULL;
3951 /* If the start group gets deleted make the start group the first
3954 if (key_file->start_group == group)
3956 tmp = g_list_last (key_file->groups);
3959 if (tmp != group_node &&
3960 ((GKeyFileGroup *) tmp->data)->name != NULL)
3967 key_file->start_group = (GKeyFileGroup *) tmp->data;
3969 key_file->start_group = NULL;
3972 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3974 tmp = group->key_value_pairs;
3981 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3984 g_warn_if_fail (group->key_value_pairs == NULL);
3986 if (group->lookup_map)
3988 g_hash_table_destroy (group->lookup_map);
3989 group->lookup_map = NULL;
3992 g_free ((gchar *) group->name);
3993 g_free_sized (group, sizeof (GKeyFileGroup));
3994 g_list_free_1 (group_node);
3998 * g_key_file_remove_group:
3999 * @key_file: a #GKeyFile
4000 * @group_name: a group name
4001 * @error: return location for a #GError or %NULL
4003 * Removes the specified group, @group_name,
4004 * from the key file.
4006 * Returns: %TRUE if the group was removed, %FALSE otherwise
4011 g_key_file_remove_group (GKeyFile *key_file,
4012 const gchar *group_name,
4017 g_return_val_if_fail (key_file != NULL, FALSE);
4018 g_return_val_if_fail (group_name != NULL, FALSE);
4020 group_node = g_key_file_lookup_group_node (key_file, group_name);
4024 g_set_error (error, G_KEY_FILE_ERROR,
4025 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4026 _("Key file does not have group “%s”"),
4031 g_key_file_remove_group_node (key_file, group_node);
4037 g_key_file_add_key_value_pair (GKeyFile *key_file,
4038 GKeyFileGroup *group,
4039 GKeyFileKeyValuePair *pair,
4042 g_hash_table_replace (group->lookup_map, pair->key, pair);
4043 group->key_value_pairs = g_list_insert_before (group->key_value_pairs, sibling, pair);
4047 g_key_file_add_key (GKeyFile *key_file,
4048 GKeyFileGroup *group,
4052 GKeyFileKeyValuePair *pair;
4055 pair = g_new (GKeyFileKeyValuePair, 1);
4056 pair->key = g_strdup (key);
4057 pair->value = g_strdup (value);
4059 /* skip group comment */
4060 lp = group->key_value_pairs;
4061 while (lp != NULL && ((GKeyFileKeyValuePair *) lp->data)->key == NULL)
4064 g_key_file_add_key_value_pair (key_file, group, pair, lp);
4068 * g_key_file_remove_key:
4069 * @key_file: a #GKeyFile
4070 * @group_name: a group name
4071 * @key: a key name to remove
4072 * @error: return location for a #GError or %NULL
4074 * Removes @key in @group_name from the key file.
4076 * Returns: %TRUE if the key was removed, %FALSE otherwise
4081 g_key_file_remove_key (GKeyFile *key_file,
4082 const gchar *group_name,
4086 GKeyFileGroup *group;
4087 GKeyFileKeyValuePair *pair;
4089 g_return_val_if_fail (key_file != NULL, FALSE);
4090 g_return_val_if_fail (group_name != NULL, FALSE);
4091 g_return_val_if_fail (key != NULL, FALSE);
4095 group = g_key_file_lookup_group (key_file, group_name);
4098 g_set_error (error, G_KEY_FILE_ERROR,
4099 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4100 _("Key file does not have group “%s”"),
4105 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
4109 set_not_found_key_error (group->name, key, error);
4113 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
4114 g_hash_table_remove (group->lookup_map, pair->key);
4115 g_key_file_key_value_pair_free (pair);
4121 g_key_file_lookup_group_node (GKeyFile *key_file,
4122 const gchar *group_name)
4124 GKeyFileGroup *group;
4126 group = g_key_file_lookup_group (key_file, group_name);
4130 return g_list_find (key_file->groups, group);
4133 static GKeyFileGroup *
4134 g_key_file_lookup_group (GKeyFile *key_file,
4135 const gchar *group_name)
4137 if (!key_file->group_hash)
4140 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4144 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4145 GKeyFileGroup *group,
4150 for (key_node = group->key_value_pairs;
4152 key_node = key_node->next)
4154 GKeyFileKeyValuePair *pair;
4156 pair = (GKeyFileKeyValuePair *) key_node->data;
4158 if (pair->key && strcmp (pair->key, key) == 0)
4165 static GKeyFileKeyValuePair *
4166 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4167 GKeyFileGroup *group,
4170 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4173 /* Lines starting with # or consisting entirely of whitespace are merely
4174 * recorded, not parsed. This function assumes all leading whitespace
4175 * has been stripped.
4178 g_key_file_line_is_comment (const gchar *line)
4180 return (*line == '#' || *line == '\0' || *line == '\n');
4184 g_key_file_is_group_name (const gchar *name)
4188 g_assert (name != NULL);
4191 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4192 q = g_utf8_find_next_char (q, NULL);
4194 if (*q != '\0' || q == p)
4201 g_key_file_is_key_name (const gchar *name,
4204 const gchar *p, *q, *end;
4206 g_assert (name != NULL);
4211 /* We accept a little more than the desktop entry spec says,
4212 * since gnome-vfs uses mime-types as keys in its cache.
4214 while (q < end && *q && *q != '=' && *q != '[' && *q != ']')
4216 q = g_utf8_find_next_char (q, end);
4221 /* No empty keys, please */
4225 /* We accept spaces in the middle of keys to not break
4226 * existing apps, but we don't tolerate initial or final
4227 * spaces, which would lead to silent corruption when
4228 * rereading the file.
4230 if (*p == ' ' || q[-1] == ' ')
4238 (g_unichar_isalnum (g_utf8_get_char_validated (q, end - q)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4240 q = g_utf8_find_next_char (q, end);
4260 /* A group in a key file is made up of a starting '[' followed by one
4261 * or more letters making up the group name followed by ']'.
4264 g_key_file_line_is_group (const gchar *line)
4274 while (*p && *p != ']')
4275 p = g_utf8_find_next_char (p, NULL);
4280 /* silently accept whitespace after the ] */
4281 p = g_utf8_find_next_char (p, NULL);
4282 while (*p == ' ' || *p == '\t')
4283 p = g_utf8_find_next_char (p, NULL);
4292 g_key_file_line_is_key_value_pair (const gchar *line)
4296 p = g_utf8_strchr (line, -1, '=');
4301 /* Key must be non-empty
4310 g_key_file_parse_value_as_string (GKeyFile *key_file,
4315 gchar *string_value, *q0, *q;
4316 GSList *tmp_pieces = NULL;
4319 g_assert (pieces == NULL || *pieces == NULL);
4321 string_value = g_new (gchar, strlen (value) + 1);
4324 q0 = q = string_value;
4354 g_clear_error (error);
4355 g_set_error_literal (error, G_KEY_FILE_ERROR,
4356 G_KEY_FILE_ERROR_INVALID_VALUE,
4357 _("Key file contains escape character "
4362 if (pieces && *p == key_file->list_separator)
4363 *q = key_file->list_separator;
4377 /* FIXME: This should be a fatal error, but there was a
4378 * bug which prevented that being reported for a long
4379 * time, so a lot of applications and in-the-field key
4380 * files use invalid escape sequences without anticipating
4381 * problems. For now (GLib 2.78), message about it; in
4382 * future, the behaviour may become fatal again.
4384 * The previous behaviour was to set the #GError but not
4385 * return failure from the function, so the caller could
4386 * explicitly check for invalid escapes, but also ignore
4387 * the error if they want. This is not how #GError is
4388 * meant to be used, but the #GKeyFile code is very old.
4390 * See https://gitlab.gnome.org/GNOME/glib/-/issues/3098 */
4391 g_clear_error (error);
4392 g_set_error (error, G_KEY_FILE_ERROR,
4393 G_KEY_FILE_ERROR_INVALID_VALUE,
4394 _("Key file contains invalid escape "
4395 "sequence “%s”"), sequence);
4404 if (pieces && (*p == key_file->list_separator))
4406 tmp_pieces = g_slist_prepend (tmp_pieces, g_strndup (q0, q - q0));
4422 tmp_pieces = g_slist_prepend (tmp_pieces, g_strndup (q0, q - q0));
4423 *pieces = g_slist_reverse (tmp_pieces);
4426 return string_value;
4429 g_free (string_value);
4430 g_slist_free_full (tmp_pieces, g_free);
4436 g_key_file_parse_string_as_value (GKeyFile *key_file,
4437 const gchar *string,
4438 gboolean escape_separator)
4443 gboolean parsing_leading_space;
4445 length = strlen (string) + 1;
4447 /* Worst case would be that every character needs to be escaped.
4448 * In other words every character turns to two characters
4450 value = g_new (gchar, 2 * length);
4454 parsing_leading_space = TRUE;
4455 while (p < (string + length - 1))
4457 gchar escaped_character[3] = { '\\', 0, 0 };
4462 if (parsing_leading_space)
4464 escaped_character[1] = 's';
4465 strcpy (q, escaped_character);
4475 if (parsing_leading_space)
4477 escaped_character[1] = 't';
4478 strcpy (q, escaped_character);
4488 escaped_character[1] = 'n';
4489 strcpy (q, escaped_character);
4493 escaped_character[1] = 'r';
4494 strcpy (q, escaped_character);
4498 escaped_character[1] = '\\';
4499 strcpy (q, escaped_character);
4501 parsing_leading_space = FALSE;
4504 if (escape_separator && *p == key_file->list_separator)
4506 escaped_character[1] = key_file->list_separator;
4507 strcpy (q, escaped_character);
4509 parsing_leading_space = TRUE;
4515 parsing_leading_space = FALSE;
4527 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4537 long_value = strtol (value, &eof_int, 10);
4540 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4542 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4543 g_set_error (error, G_KEY_FILE_ERROR,
4544 G_KEY_FILE_ERROR_INVALID_VALUE,
4545 _("Value “%s” cannot be interpreted "
4546 "as a number."), value_utf8);
4547 g_free (value_utf8);
4552 int_value = long_value;
4553 if (int_value != long_value || errsv == ERANGE)
4555 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4558 G_KEY_FILE_ERROR_INVALID_VALUE,
4559 _("Integer value “%s” out of range"),
4561 g_free (value_utf8);
4570 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4574 return g_strdup_printf ("%d", value);
4578 g_key_file_parse_value_as_double (GKeyFile *key_file,
4582 gchar *end_of_valid_d;
4583 gdouble double_value = 0;
4585 double_value = g_ascii_strtod (value, &end_of_valid_d);
4587 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4589 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4590 g_set_error (error, G_KEY_FILE_ERROR,
4591 G_KEY_FILE_ERROR_INVALID_VALUE,
4592 _("Value “%s” cannot be interpreted "
4593 "as a float number."),
4595 g_free (value_utf8);
4600 return double_value;
4604 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4606 size_t len2 = strlen (s2);
4607 return strncmp (s1, s2, MAX (len1, len2));
4611 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4618 /* Count the number of non-whitespace characters */
4619 for (i = 0; value[i]; i++)
4620 if (!g_ascii_isspace (value[i]))
4623 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4625 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4628 value_utf8 = g_utf8_make_valid (value, -1);
4629 g_set_error (error, G_KEY_FILE_ERROR,
4630 G_KEY_FILE_ERROR_INVALID_VALUE,
4631 _("Value “%s” cannot be interpreted "
4632 "as a boolean."), value_utf8);
4633 g_free (value_utf8);
4638 static const gchar *
4639 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4649 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4651 gboolean is_final_line)
4657 string = g_string_sized_new (512);
4659 lines = g_strsplit (value, "\n", 0);
4661 for (i = 0; lines[i] != NULL; i++)
4663 const gchar *line = lines[i];
4666 g_string_append_c (string, '\n');
4670 g_string_append (string, line);
4674 /* This function gets called once per line of a comment, but we don’t want
4675 * to add a trailing newline. */
4677 g_string_append_c (string, '\n');
4679 return g_string_free (string, FALSE);
4683 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4684 const gchar *comment)
4690 string = g_string_sized_new (512);
4692 lines = g_strsplit (comment, "\n", 0);
4694 for (i = 0; lines[i] != NULL; i++)
4695 g_string_append_printf (string, "#%s%s", lines[i],
4696 lines[i + 1] == NULL? "" : "\n");
4699 return g_string_free (string, FALSE);
4703 * g_key_file_save_to_file:
4704 * @key_file: a #GKeyFile
4705 * @filename: the name of the file to write to
4706 * @error: a pointer to a %NULL #GError, or %NULL
4708 * Writes the contents of @key_file to @filename using
4709 * g_file_set_contents(). If you need stricter guarantees about durability of
4710 * the written file than are provided by g_file_set_contents(), use
4711 * g_file_set_contents_full() with the return value of g_key_file_to_data().
4713 * This function can fail for any of the reasons that
4714 * g_file_set_contents() may fail.
4716 * Returns: %TRUE if successful, else %FALSE with @error set
4721 g_key_file_save_to_file (GKeyFile *key_file,
4722 const gchar *filename,
4729 g_return_val_if_fail (key_file != NULL, FALSE);
4730 g_return_val_if_fail (filename != NULL, FALSE);
4731 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4733 contents = g_key_file_to_data (key_file, &length, NULL);
4734 g_assert (contents != NULL);
4736 success = g_file_set_contents (filename, contents, length, error);