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 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, see <http://www.gnu.org/licenses/>.
35 #include <sys/types.h>
44 #define fstat(a,b) _fstati64(a,b)
49 #define S_ISREG(mode) ((mode)&_S_IFREG)
52 #endif /* G_OS_WIN23 */
57 #include "gfileutils.h"
63 #include "gmessages.h"
66 #include "gstrfuncs.h"
72 * @title: Key-value file parser
73 * @short_description: parses .ini-like config files
75 * #GKeyFile lets you parse, edit or create files containing groups of
76 * key-value pairs, which we call "key files" for lack of a better name.
77 * Several freedesktop.org specifications use key files now, e.g the
78 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
80 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
82 * The syntax of key files is described in detail in the
83 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
84 * here is a quick summary: Key files
85 * consists of groups of key-value pairs, interspersed with comments.
88 * # this is just an example
89 * # there can be comments before the first group
93 * Name=Key File Example\tthis value shows\nescaping
95 * # localized strings are stored in multiple key-value pairs
98 * Welcome[fr_FR]=Bonjour
100 * Welcome[be@latin]=Hello
104 * Numbers=2;20;-200;0
106 * Booleans=true;false;true;true
109 * Lines beginning with a '#' and blank lines are considered comments.
111 * Groups are started by a header line containing the group name enclosed
112 * in '[' and ']', and ended implicitly by the start of the next group or
113 * the end of the file. Each key-value pair must be contained in a group.
115 * Key-value pairs generally have the form `key=value`, with the
116 * exception of localized strings, which have the form
117 * `key[locale]=value`, with a locale identifier of the
118 * form `lang_COUNTRY@MODIFIER` where `COUNTRY` and `MODIFIER`
120 * Space before and after the '=' character are ignored. Newline, tab,
121 * carriage return and backslash characters in value are escaped as \n,
122 * \t, \r, and \\\\, respectively. To preserve leading spaces in values,
123 * these can also be escaped as \s.
125 * Key files can store strings (possibly with localized variants), integers,
126 * booleans and lists of these. Lists are separated by a separator character,
127 * typically ';' or ','. To use the list separator character in a value in
128 * a list, it has to be escaped by prefixing it with a backslash.
130 * This syntax is obviously inspired by the .ini files commonly met
131 * on Windows, but there are some important differences:
133 * - .ini files use the ';' character to begin comments,
134 * key files use the '#' character.
136 * - Key files do not allow for ungrouped keys meaning only
137 * comments can precede the first group.
139 * - Key files are always encoded in UTF-8.
141 * - Key and Group names are case-sensitive. For example, a group called
142 * [GROUP] is a different from [group].
144 * - .ini files don't have a strongly typed boolean entry type,
145 * they only have GetProfileInt(). In key files, only
146 * true and false (in lower case) are allowed.
148 * Note that in contrast to the
149 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
150 * groups in key files may contain the same
151 * key multiple times; the last entry wins. Key files may also contain
152 * multiple groups with the same name; they are merged together.
153 * Another difference is that keys and group names in key files are not
154 * restricted to ASCII characters.
156 * Here is an example of loading a key file and reading a value:
157 * |[<!-- language="C" -->
158 * g_autoptr(GError) error = NULL;
159 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
161 * if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
163 * if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
164 * g_warning ("Error loading key file: %s", error->message);
168 * g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
170 * !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
172 * g_warning ("Error finding key in key file: %s", error->message);
175 * else if (val == NULL)
177 * // Fall back to a default value.
178 * val = g_strdup ("default-value");
182 * Here is an example of creating and saving a key file:
183 * |[<!-- language="C" -->
184 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
185 * const gchar *val = …;
186 * g_autoptr(GError) error = NULL;
188 * g_key_file_set_string (key_file, "Group Name", "SomeKey", val);
191 * if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
193 * g_warning ("Error saving key file: %s", error->message);
197 * // Or store to a GBytes for use elsewhere.
199 * g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
202 * g_warning ("Error saving key file: %s", error->message);
205 * g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);
212 * Error domain for key file parsing. Errors in this domain will
213 * be from the #GKeyFileError enumeration.
215 * See #GError for information on error domains.
220 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
221 * an unknown encoding
222 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
223 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
224 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
225 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
226 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
228 * Error codes returned by key file parsing.
233 * @G_KEY_FILE_NONE: No flags, default behaviour
234 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
235 * (possibly modified) contents of the key file back to a file;
236 * otherwise all comments will be lost when the key file is
238 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
239 * (possibly modified) contents of the key file back to a file;
240 * otherwise only the translations for the current language will be
243 * Flags which influence the parsing.
247 * G_KEY_FILE_DESKTOP_GROUP:
249 * The name of the main group of a desktop entry file, as defined in the
250 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
251 * Consult the specification for more
252 * details about the meanings of the keys below.
258 * G_KEY_FILE_DESKTOP_KEY_TYPE:
260 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
261 * giving the type of the desktop entry.
263 * Usually %G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
264 * %G_KEY_FILE_DESKTOP_TYPE_LINK, or
265 * %G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
271 * G_KEY_FILE_DESKTOP_KEY_VERSION:
273 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
274 * giving the version of the Desktop Entry Specification used for
275 * the desktop entry file.
281 * G_KEY_FILE_DESKTOP_KEY_NAME:
283 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
284 * string giving the specific name of the desktop entry.
290 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
292 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
293 * string giving the generic name of the desktop entry.
299 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
301 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
302 * stating whether the desktop entry should be shown in menus.
308 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
310 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
311 * string giving the tooltip for the desktop entry.
317 * G_KEY_FILE_DESKTOP_KEY_ICON:
319 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
320 * string giving the name of the icon to be displayed for the desktop
327 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
329 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
330 * stating whether the desktop entry has been deleted by the user.
336 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
338 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
339 * strings identifying the environments that should display the
346 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
348 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
349 * strings identifying the environments that should not display the
356 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
358 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
359 * giving the file name of a binary on disk used to determine if the
360 * program is actually installed. It is only valid for desktop entries
361 * with the `Application` type.
367 * G_KEY_FILE_DESKTOP_KEY_EXEC:
369 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
370 * giving the command line to execute. It is only valid for desktop
371 * entries with the `Application` type.
377 * G_KEY_FILE_DESKTOP_KEY_PATH:
379 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
380 * containing the working directory to run the program in. It is only
381 * valid for desktop entries with the `Application` type.
387 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
389 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
390 * stating whether the program should be run in a terminal window.
392 * It is only valid for desktop entries with the `Application` type.
398 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
400 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list
401 * of strings giving the MIME types supported by this desktop entry.
407 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
409 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a list
410 * of strings giving the categories in which the desktop entry
411 * should be shown in a menu.
417 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
419 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
420 * stating whether the application supports the
421 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
427 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
429 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is string
430 * identifying the WM class or name hint of a window that the application
431 * will create, which can be used to emulate Startup Notification with
432 * older applications.
438 * G_KEY_FILE_DESKTOP_KEY_URL:
440 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string
441 * giving the URL to access. It is only valid for desktop entries
442 * with the `Link` type.
448 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
450 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
451 * set to true if the application is D-Bus activatable.
457 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
459 * A key under %G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
460 * giving the available application actions.
466 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
468 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
469 * entries representing applications.
475 * G_KEY_FILE_DESKTOP_TYPE_LINK:
477 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
478 * entries representing links to documents.
484 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
486 * The value of the %G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
487 * entries representing directories.
492 typedef struct _GKeyFileGroup GKeyFileGroup;
497 * The GKeyFile struct contains only private data
498 * and should not be accessed directly.
503 GHashTable *group_hash;
505 GKeyFileGroup *start_group;
506 GKeyFileGroup *current_group;
508 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
510 gchar list_separator;
514 gboolean checked_locales;
517 gint ref_count; /* (atomic) */
520 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
522 struct _GKeyFileGroup
524 const gchar *name; /* NULL for above first group (which will be comments) */
526 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
528 GList *key_value_pairs;
530 /* Used in parallel with key_value_pairs for
531 * increased lookup performance
533 GHashTable *lookup_map;
536 struct _GKeyFileKeyValuePair
538 gchar *key; /* NULL for comments */
542 static gint find_file_in_data_dirs (const gchar *file,
543 const gchar **data_dirs,
546 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
550 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
551 const gchar *group_name);
552 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
553 const gchar *group_name);
555 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
556 GKeyFileGroup *group,
558 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
559 GKeyFileGroup *group,
562 static void g_key_file_remove_group_node (GKeyFile *key_file,
564 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
565 GKeyFileGroup *group,
568 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
569 GKeyFileGroup *group,
570 GKeyFileKeyValuePair *pair);
571 static void g_key_file_add_key (GKeyFile *key_file,
572 GKeyFileGroup *group,
575 static void g_key_file_add_group (GKeyFile *key_file,
576 const gchar *group_name);
577 static gboolean g_key_file_is_group_name (const gchar *name);
578 static gboolean g_key_file_is_key_name (const gchar *name);
579 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
580 static gboolean g_key_file_line_is_comment (const gchar *line);
581 static gboolean g_key_file_line_is_group (const gchar *line);
582 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
583 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
587 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
589 gboolean escape_separator);
590 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
593 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
595 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
598 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
601 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
603 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
605 gboolean is_final_line);
606 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
607 const gchar *comment);
608 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
612 static void g_key_file_parse_comment (GKeyFile *key_file,
616 static void g_key_file_parse_group (GKeyFile *key_file,
620 static gchar *key_get_locale (const gchar *key);
621 static void g_key_file_parse_data (GKeyFile *key_file,
625 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
628 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
631 g_key_file_init (GKeyFile *key_file)
633 key_file->current_group = g_slice_new0 (GKeyFileGroup);
634 key_file->groups = g_list_prepend (NULL, key_file->current_group);
635 key_file->group_hash = NULL;
636 key_file->start_group = NULL;
637 key_file->parse_buffer = NULL;
638 key_file->list_separator = ';';
643 g_key_file_clear (GKeyFile *key_file)
645 GList *tmp, *group_node;
647 if (key_file->locales)
649 g_strfreev (key_file->locales);
650 key_file->locales = NULL;
652 key_file->checked_locales = FALSE;
654 if (key_file->parse_buffer)
656 g_string_free (key_file->parse_buffer, TRUE);
657 key_file->parse_buffer = NULL;
660 tmp = key_file->groups;
665 g_key_file_remove_group_node (key_file, group_node);
668 if (key_file->group_hash != NULL)
670 g_hash_table_destroy (key_file->group_hash);
671 key_file->group_hash = NULL;
674 g_warn_if_fail (key_file->groups == NULL);
681 * Creates a new empty #GKeyFile object. Use
682 * g_key_file_load_from_file(), g_key_file_load_from_data(),
683 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
684 * read an existing key file.
686 * Returns: (transfer full): an empty #GKeyFile.
691 g_key_file_new (void)
695 key_file = g_slice_new0 (GKeyFile);
696 key_file->ref_count = 1;
697 g_key_file_init (key_file);
703 * g_key_file_set_list_separator:
704 * @key_file: a #GKeyFile
705 * @separator: the separator
707 * Sets the character which is used to separate
708 * values in lists. Typically ';' or ',' are used
709 * as separators. The default list separator is ';'.
714 g_key_file_set_list_separator (GKeyFile *key_file,
717 g_return_if_fail (key_file != NULL);
719 key_file->list_separator = separator;
723 /* Iterates through all the directories in *dirs trying to
724 * open file. When it successfully locates and opens a file it
725 * returns the file descriptor to the open file. It also
726 * outputs the absolute path of the file in output_file.
729 find_file_in_data_dirs (const gchar *file,
734 const gchar **data_dirs, *data_dir;
746 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
748 gchar *candidate_file, *sub_dir;
750 candidate_file = (gchar *) file;
751 sub_dir = g_strdup ("");
752 while (candidate_file != NULL && fd == -1)
756 path = g_build_filename (data_dir, sub_dir,
757 candidate_file, NULL);
759 fd = g_open (path, O_RDONLY, 0);
767 candidate_file = strchr (candidate_file, '-');
769 if (candidate_file == NULL)
775 sub_dir = g_strndup (file, candidate_file - file - 1);
777 for (p = sub_dir; *p != '\0'; p++)
780 *p = G_DIR_SEPARATOR;
789 g_set_error_literal (error, G_KEY_FILE_ERROR,
790 G_KEY_FILE_ERROR_NOT_FOUND,
791 _("Valid key file could not be "
792 "found in search dirs"));
795 if (output_file != NULL && fd != -1)
796 *output_file = g_strdup (path);
804 g_key_file_load_from_fd (GKeyFile *key_file,
809 GError *key_file_error = NULL;
811 struct stat stat_buf;
812 gchar read_buf[4096];
813 gchar list_separator;
815 if (fstat (fd, &stat_buf) < 0)
818 g_set_error_literal (error, G_FILE_ERROR,
819 g_file_error_from_errno (errsv),
824 if (!S_ISREG (stat_buf.st_mode))
826 g_set_error_literal (error, G_KEY_FILE_ERROR,
827 G_KEY_FILE_ERROR_PARSE,
828 _("Not a regular file"));
832 list_separator = key_file->list_separator;
833 g_key_file_clear (key_file);
834 g_key_file_init (key_file);
835 key_file->list_separator = list_separator;
836 key_file->flags = flags;
842 bytes_read = read (fd, read_buf, 4096);
845 if (bytes_read == 0) /* End of File */
850 if (errsv == EINTR || errsv == EAGAIN)
853 g_set_error_literal (error, G_FILE_ERROR,
854 g_file_error_from_errno (errsv),
859 g_key_file_parse_data (key_file,
860 read_buf, bytes_read,
863 while (!key_file_error);
867 g_propagate_error (error, key_file_error);
871 g_key_file_flush_parse_buffer (key_file, &key_file_error);
875 g_propagate_error (error, key_file_error);
883 * g_key_file_load_from_file:
884 * @key_file: an empty #GKeyFile struct
885 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
886 * @flags: flags from #GKeyFileFlags
887 * @error: return location for a #GError, or %NULL
889 * Loads a key file into an empty #GKeyFile structure.
891 * If the OS returns an error when opening or reading the file, a
892 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
893 * %G_KEY_FILE_ERROR is returned.
895 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
896 * @file is not found, %G_FILE_ERROR_NOENT is returned.
898 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
903 g_key_file_load_from_file (GKeyFile *key_file,
908 GError *key_file_error = NULL;
912 g_return_val_if_fail (key_file != NULL, FALSE);
913 g_return_val_if_fail (file != NULL, FALSE);
915 fd = g_open (file, O_RDONLY, 0);
920 g_set_error_literal (error, G_FILE_ERROR,
921 g_file_error_from_errno (errsv),
926 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
931 g_propagate_error (error, key_file_error);
939 * g_key_file_load_from_data:
940 * @key_file: an empty #GKeyFile struct
941 * @data: key file loaded in memory
942 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
943 * @flags: flags from #GKeyFileFlags
944 * @error: return location for a #GError, or %NULL
946 * Loads a key file from memory into an empty #GKeyFile structure.
947 * If the object cannot be created then %error is set to a #GKeyFileError.
949 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
954 g_key_file_load_from_data (GKeyFile *key_file,
960 GError *key_file_error = NULL;
961 gchar list_separator;
963 g_return_val_if_fail (key_file != NULL, FALSE);
964 g_return_val_if_fail (data != NULL || length == 0, FALSE);
966 if (length == (gsize)-1)
967 length = strlen (data);
969 list_separator = key_file->list_separator;
970 g_key_file_clear (key_file);
971 g_key_file_init (key_file);
972 key_file->list_separator = list_separator;
973 key_file->flags = flags;
975 g_key_file_parse_data (key_file, data, length, &key_file_error);
979 g_propagate_error (error, key_file_error);
983 g_key_file_flush_parse_buffer (key_file, &key_file_error);
987 g_propagate_error (error, key_file_error);
995 * g_key_file_load_from_bytes:
996 * @key_file: an empty #GKeyFile struct
998 * @flags: flags from #GKeyFileFlags
999 * @error: return location for a #GError, or %NULL
1001 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
1002 * If the object cannot be created then %error is set to a #GKeyFileError.
1004 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1009 g_key_file_load_from_bytes (GKeyFile *key_file,
1011 GKeyFileFlags flags,
1017 g_return_val_if_fail (key_file != NULL, FALSE);
1018 g_return_val_if_fail (bytes != NULL, FALSE);
1020 data = g_bytes_get_data (bytes, &size);
1021 return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
1025 * g_key_file_load_from_dirs:
1026 * @key_file: an empty #GKeyFile struct
1027 * @file: (type filename): a relative path to a filename to open and parse
1028 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
1029 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1030 * of the file, or %NULL
1031 * @flags: flags from #GKeyFileFlags
1032 * @error: return location for a #GError, or %NULL
1034 * This function looks for a key file named @file in the paths
1035 * specified in @search_dirs, loads the file into @key_file and
1036 * returns the file's full path in @full_path.
1038 * If the file could not be found in any of the @search_dirs,
1039 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
1040 * the file is found but the OS returns an error when opening or reading the
1041 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
1042 * %G_KEY_FILE_ERROR is returned.
1044 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1049 g_key_file_load_from_dirs (GKeyFile *key_file,
1051 const gchar **search_dirs,
1053 GKeyFileFlags flags,
1056 GError *key_file_error = NULL;
1057 const gchar **data_dirs;
1060 gboolean found_file;
1062 g_return_val_if_fail (key_file != NULL, FALSE);
1063 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1064 g_return_val_if_fail (search_dirs != NULL, FALSE);
1067 data_dirs = search_dirs;
1069 while (*data_dirs != NULL && !found_file)
1071 g_free (output_path);
1074 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
1080 g_propagate_error (error, key_file_error);
1084 found_file = g_key_file_load_from_fd (key_file, fd, flags,
1090 g_propagate_error (error, key_file_error);
1095 if (found_file && full_path)
1096 *full_path = output_path;
1098 g_free (output_path);
1104 * g_key_file_load_from_data_dirs:
1105 * @key_file: an empty #GKeyFile struct
1106 * @file: (type filename): a relative path to a filename to open and parse
1107 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1108 * of the file, or %NULL
1109 * @flags: flags from #GKeyFileFlags
1110 * @error: return location for a #GError, or %NULL
1112 * This function looks for a key file named @file in the paths
1113 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1114 * loads the file into @key_file and returns the file's full path in
1115 * @full_path. If the file could not be loaded then an %error is
1116 * set to either a #GFileError or #GKeyFileError.
1118 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1122 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1125 GKeyFileFlags flags,
1128 gchar **all_data_dirs;
1129 const gchar * user_data_dir;
1130 const gchar * const * system_data_dirs;
1132 gboolean found_file;
1134 g_return_val_if_fail (key_file != NULL, FALSE);
1135 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1137 user_data_dir = g_get_user_data_dir ();
1138 system_data_dirs = g_get_system_data_dirs ();
1139 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1142 all_data_dirs[i++] = g_strdup (user_data_dir);
1145 while (system_data_dirs[j] != NULL)
1146 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1147 all_data_dirs[i] = NULL;
1149 found_file = g_key_file_load_from_dirs (key_file,
1151 (const gchar **)all_data_dirs,
1156 g_strfreev (all_data_dirs);
1162 * g_key_file_ref: (skip)
1163 * @key_file: a #GKeyFile
1165 * Increases the reference count of @key_file.
1167 * Returns: the same @key_file.
1172 g_key_file_ref (GKeyFile *key_file)
1174 g_return_val_if_fail (key_file != NULL, NULL);
1176 g_atomic_int_inc (&key_file->ref_count);
1182 * g_key_file_free: (skip)
1183 * @key_file: a #GKeyFile
1185 * Clears all keys and groups from @key_file, and decreases the
1186 * reference count by 1. If the reference count reaches zero,
1187 * frees the key file and all its allocated memory.
1192 g_key_file_free (GKeyFile *key_file)
1194 g_return_if_fail (key_file != NULL);
1196 g_key_file_clear (key_file);
1198 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1199 g_slice_free (GKeyFile, key_file);
1201 g_key_file_init (key_file);
1206 * @key_file: a #GKeyFile
1208 * Decreases the reference count of @key_file by 1. If the reference count
1209 * reaches zero, frees the key file and all its allocated memory.
1214 g_key_file_unref (GKeyFile *key_file)
1216 g_return_if_fail (key_file != NULL);
1218 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1220 g_key_file_clear (key_file);
1221 g_slice_free (GKeyFile, key_file);
1225 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1226 * true for locales that match those in g_get_language_names().
1229 g_key_file_locale_is_interesting (GKeyFile *key_file,
1230 const gchar *locale)
1234 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1237 if (!key_file->checked_locales)
1239 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
1240 key_file->checked_locales = TRUE;
1243 for (i = 0; key_file->locales[i] != NULL; i++)
1245 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1253 g_key_file_parse_line (GKeyFile *key_file,
1258 GError *parse_error = NULL;
1261 g_return_if_fail (key_file != NULL);
1262 g_return_if_fail (line != NULL);
1264 line_start = (gchar *) line;
1265 while (g_ascii_isspace (*line_start))
1268 if (g_key_file_line_is_comment (line_start))
1269 g_key_file_parse_comment (key_file, line, length, &parse_error);
1270 else if (g_key_file_line_is_group (line_start))
1271 g_key_file_parse_group (key_file, line_start,
1272 length - (line_start - line),
1274 else if (g_key_file_line_is_key_value_pair (line_start))
1275 g_key_file_parse_key_value_pair (key_file, line_start,
1276 length - (line_start - line),
1280 gchar *line_utf8 = g_utf8_make_valid (line, length);
1281 g_set_error (error, G_KEY_FILE_ERROR,
1282 G_KEY_FILE_ERROR_PARSE,
1283 _("Key file contains line “%s” which is not "
1284 "a key-value pair, group, or comment"),
1292 g_propagate_error (error, parse_error);
1296 g_key_file_parse_comment (GKeyFile *key_file,
1301 GKeyFileKeyValuePair *pair;
1303 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1306 g_warn_if_fail (key_file->current_group != NULL);
1308 pair = g_slice_new (GKeyFileKeyValuePair);
1310 pair->value = g_strndup (line, length);
1312 key_file->current_group->key_value_pairs =
1313 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1317 g_key_file_parse_group (GKeyFile *key_file,
1323 const gchar *group_name_start, *group_name_end;
1325 /* advance past opening '['
1327 group_name_start = line + 1;
1328 group_name_end = line + length - 1;
1330 while (*group_name_end != ']')
1333 group_name = g_strndup (group_name_start,
1334 group_name_end - group_name_start);
1336 if (!g_key_file_is_group_name (group_name))
1338 g_set_error (error, G_KEY_FILE_ERROR,
1339 G_KEY_FILE_ERROR_PARSE,
1340 _("Invalid group name: %s"), group_name);
1341 g_free (group_name);
1345 g_key_file_add_group (key_file, group_name);
1346 g_free (group_name);
1350 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1355 gchar *key, *value, *key_end, *value_start, *locale;
1356 gsize key_len, value_len;
1358 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1360 g_set_error_literal (error, G_KEY_FILE_ERROR,
1361 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1362 _("Key file does not start with a group"));
1366 key_end = value_start = strchr (line, '=');
1368 g_warn_if_fail (key_end != NULL);
1373 /* Pull the key name from the line (chomping trailing whitespace)
1375 while (g_ascii_isspace (*key_end))
1378 key_len = key_end - line + 2;
1380 g_warn_if_fail (key_len <= length);
1382 key = g_strndup (line, key_len - 1);
1384 if (!g_key_file_is_key_name (key))
1386 g_set_error (error, G_KEY_FILE_ERROR,
1387 G_KEY_FILE_ERROR_PARSE,
1388 _("Invalid key name: %s"), key);
1393 /* Pull the value from the line (chugging leading whitespace)
1395 while (g_ascii_isspace (*value_start))
1398 value_len = line + length - value_start + 1;
1400 value = g_strndup (value_start, value_len);
1402 g_warn_if_fail (key_file->start_group != NULL);
1404 if (key_file->current_group
1405 && key_file->current_group->name
1406 && strcmp (key_file->start_group->name,
1407 key_file->current_group->name) == 0
1408 && strcmp (key, "Encoding") == 0)
1410 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1412 gchar *value_utf8 = g_utf8_make_valid (value, value_len);
1413 g_set_error (error, G_KEY_FILE_ERROR,
1414 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1415 _("Key file contains unsupported "
1416 "encoding “%s”"), value_utf8);
1417 g_free (value_utf8);
1425 /* Is this key a translation? If so, is it one that we care about?
1427 locale = key_get_locale (key);
1429 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1431 GKeyFileKeyValuePair *pair;
1433 pair = g_slice_new (GKeyFileKeyValuePair);
1435 pair->value = value;
1437 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1449 key_get_locale (const gchar *key)
1453 locale = g_strrstr (key, "[");
1455 if (locale && strlen (locale) <= 2)
1459 locale = g_strndup (locale + 1, strlen (locale) - 2);
1465 g_key_file_parse_data (GKeyFile *key_file,
1470 GError *parse_error;
1473 g_return_if_fail (key_file != NULL);
1474 g_return_if_fail (data != NULL || length == 0);
1478 if (!key_file->parse_buffer)
1479 key_file->parse_buffer = g_string_sized_new (128);
1484 if (data[i] == '\n')
1486 if (key_file->parse_buffer->len > 0
1487 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1489 g_string_erase (key_file->parse_buffer,
1490 key_file->parse_buffer->len - 1,
1493 /* When a newline is encountered flush the parse buffer so that the
1494 * line can be parsed. Note that completely blank lines won't show
1495 * up in the parse buffer, so they get parsed directly.
1497 if (key_file->parse_buffer->len > 0)
1498 g_key_file_flush_parse_buffer (key_file, &parse_error);
1500 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1504 g_propagate_error (error, parse_error);
1511 const gchar *start_of_line;
1512 const gchar *end_of_line;
1515 start_of_line = data + i;
1516 end_of_line = memchr (start_of_line, '\n', length - i);
1518 if (end_of_line == NULL)
1519 end_of_line = data + length;
1521 line_length = end_of_line - start_of_line;
1523 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1530 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1533 GError *file_error = NULL;
1535 g_return_if_fail (key_file != NULL);
1537 if (!key_file->parse_buffer)
1542 if (key_file->parse_buffer->len > 0)
1544 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1545 key_file->parse_buffer->len,
1547 g_string_erase (key_file->parse_buffer, 0, -1);
1551 g_propagate_error (error, file_error);
1558 * g_key_file_to_data:
1559 * @key_file: a #GKeyFile
1560 * @length: (out) (optional): return location for the length of the
1561 * returned string, or %NULL
1562 * @error: return location for a #GError, or %NULL
1564 * This function outputs @key_file as a string.
1566 * Note that this function never reports an error,
1567 * so it is safe to pass %NULL as @error.
1569 * Returns: a newly allocated string holding
1570 * the contents of the #GKeyFile
1575 g_key_file_to_data (GKeyFile *key_file,
1579 GString *data_string;
1580 GList *group_node, *key_file_node;
1582 g_return_val_if_fail (key_file != NULL, NULL);
1584 data_string = g_string_new (NULL);
1586 for (group_node = g_list_last (key_file->groups);
1588 group_node = group_node->prev)
1590 GKeyFileGroup *group;
1592 group = (GKeyFileGroup *) group_node->data;
1594 /* separate groups by at least an empty line */
1595 if (data_string->len >= 2 &&
1596 data_string->str[data_string->len - 2] != '\n')
1597 g_string_append_c (data_string, '\n');
1599 if (group->comment != NULL)
1600 g_string_append_printf (data_string, "%s\n", group->comment->value);
1602 if (group->name != NULL)
1603 g_string_append_printf (data_string, "[%s]\n", group->name);
1605 for (key_file_node = g_list_last (group->key_value_pairs);
1606 key_file_node != NULL;
1607 key_file_node = key_file_node->prev)
1609 GKeyFileKeyValuePair *pair;
1611 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1613 if (pair->key != NULL)
1614 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1616 g_string_append_printf (data_string, "%s\n", pair->value);
1621 *length = data_string->len;
1623 return g_string_free (data_string, FALSE);
1627 * g_key_file_get_keys:
1628 * @key_file: a #GKeyFile
1629 * @group_name: a group name
1630 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1631 * @error: return location for a #GError, or %NULL
1633 * Returns all keys for the group name @group_name. The array of
1634 * returned keys will be %NULL-terminated, so @length may
1635 * optionally be %NULL. In the event that the @group_name cannot
1636 * be found, %NULL is returned and @error is set to
1637 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1639 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1640 * Use g_strfreev() to free it.
1645 g_key_file_get_keys (GKeyFile *key_file,
1646 const gchar *group_name,
1650 GKeyFileGroup *group;
1655 g_return_val_if_fail (key_file != NULL, NULL);
1656 g_return_val_if_fail (group_name != NULL, NULL);
1658 group = g_key_file_lookup_group (key_file, group_name);
1662 g_set_error (error, G_KEY_FILE_ERROR,
1663 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1664 _("Key file does not have group “%s”"),
1670 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1672 GKeyFileKeyValuePair *pair;
1674 pair = (GKeyFileKeyValuePair *) tmp->data;
1680 keys = g_new (gchar *, num_keys + 1);
1683 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1685 GKeyFileKeyValuePair *pair;
1687 pair = (GKeyFileKeyValuePair *) tmp->data;
1691 keys[i] = g_strdup (pair->key);
1696 keys[num_keys] = NULL;
1705 * g_key_file_get_start_group:
1706 * @key_file: a #GKeyFile
1708 * Returns the name of the start group of the file.
1710 * Returns: (nullable): The start group of the key file.
1715 g_key_file_get_start_group (GKeyFile *key_file)
1717 g_return_val_if_fail (key_file != NULL, NULL);
1719 if (key_file->start_group)
1720 return g_strdup (key_file->start_group->name);
1726 * g_key_file_get_groups:
1727 * @key_file: a #GKeyFile
1728 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1730 * Returns all groups in the key file loaded with @key_file.
1731 * The array of returned groups will be %NULL-terminated, so
1732 * @length may optionally be %NULL.
1734 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1735 * Use g_strfreev() to free it.
1739 g_key_file_get_groups (GKeyFile *key_file,
1744 gsize i, num_groups;
1746 g_return_val_if_fail (key_file != NULL, NULL);
1748 num_groups = g_list_length (key_file->groups);
1750 g_return_val_if_fail (num_groups > 0, NULL);
1752 group_node = g_list_last (key_file->groups);
1754 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1756 /* Only need num_groups instead of num_groups + 1
1757 * because the first group of the file (last in the
1758 * list) is always the comment group at the top,
1761 groups = g_new (gchar *, num_groups);
1765 for (group_node = group_node->prev;
1767 group_node = group_node->prev)
1769 GKeyFileGroup *group;
1771 group = (GKeyFileGroup *) group_node->data;
1773 g_warn_if_fail (group->name != NULL);
1775 groups[i++] = g_strdup (group->name);
1786 set_not_found_key_error (const char *group_name,
1790 g_set_error (error, G_KEY_FILE_ERROR,
1791 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1792 _("Key file does not have key “%s” in group “%s”"),
1797 * g_key_file_get_value:
1798 * @key_file: a #GKeyFile
1799 * @group_name: a group name
1801 * @error: return location for a #GError, or %NULL
1803 * Returns the raw value associated with @key under @group_name.
1804 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1806 * In the event the key cannot be found, %NULL is returned and
1807 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1808 * event that the @group_name cannot be found, %NULL is returned
1809 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1812 * Returns: a newly allocated string or %NULL if the specified
1813 * key cannot be found.
1818 g_key_file_get_value (GKeyFile *key_file,
1819 const gchar *group_name,
1823 GKeyFileGroup *group;
1824 GKeyFileKeyValuePair *pair;
1825 gchar *value = NULL;
1827 g_return_val_if_fail (key_file != NULL, NULL);
1828 g_return_val_if_fail (group_name != NULL, NULL);
1829 g_return_val_if_fail (key != NULL, NULL);
1831 group = g_key_file_lookup_group (key_file, group_name);
1835 g_set_error (error, G_KEY_FILE_ERROR,
1836 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1837 _("Key file does not have group “%s”"),
1842 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1845 value = g_strdup (pair->value);
1847 set_not_found_key_error (group_name, key, error);
1853 * g_key_file_set_value:
1854 * @key_file: a #GKeyFile
1855 * @group_name: a group name
1859 * Associates a new value with @key under @group_name.
1861 * If @key cannot be found then it is created. If @group_name cannot
1862 * be found then it is created. To set an UTF-8 string which may contain
1863 * characters that need escaping (such as newlines or spaces), use
1864 * g_key_file_set_string().
1869 g_key_file_set_value (GKeyFile *key_file,
1870 const gchar *group_name,
1874 GKeyFileGroup *group;
1875 GKeyFileKeyValuePair *pair;
1877 g_return_if_fail (key_file != NULL);
1878 g_return_if_fail (g_key_file_is_group_name (group_name));
1879 g_return_if_fail (g_key_file_is_key_name (key));
1880 g_return_if_fail (value != NULL);
1882 group = g_key_file_lookup_group (key_file, group_name);
1886 g_key_file_add_group (key_file, group_name);
1887 group = (GKeyFileGroup *) key_file->groups->data;
1889 g_key_file_add_key (key_file, group, key, value);
1893 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1896 g_key_file_add_key (key_file, group, key, value);
1899 g_free (pair->value);
1900 pair->value = g_strdup (value);
1906 * g_key_file_get_string:
1907 * @key_file: a #GKeyFile
1908 * @group_name: a group name
1910 * @error: return location for a #GError, or %NULL
1912 * Returns the string value associated with @key under @group_name.
1913 * Unlike g_key_file_get_value(), this function handles escape sequences
1916 * In the event the key cannot be found, %NULL is returned and
1917 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1918 * event that the @group_name cannot be found, %NULL is returned
1919 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1921 * Returns: a newly allocated string or %NULL if the specified
1922 * key cannot be found.
1927 g_key_file_get_string (GKeyFile *key_file,
1928 const gchar *group_name,
1932 gchar *value, *string_value;
1933 GError *key_file_error;
1935 g_return_val_if_fail (key_file != NULL, NULL);
1936 g_return_val_if_fail (group_name != NULL, NULL);
1937 g_return_val_if_fail (key != NULL, NULL);
1939 key_file_error = NULL;
1941 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1945 g_propagate_error (error, key_file_error);
1949 if (!g_utf8_validate (value, -1, NULL))
1951 gchar *value_utf8 = g_utf8_make_valid (value, -1);
1952 g_set_error (error, G_KEY_FILE_ERROR,
1953 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1954 _("Key file contains key “%s” with value “%s” "
1955 "which is not UTF-8"), key, value_utf8);
1956 g_free (value_utf8);
1962 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1968 if (g_error_matches (key_file_error,
1970 G_KEY_FILE_ERROR_INVALID_VALUE))
1972 g_set_error (error, G_KEY_FILE_ERROR,
1973 G_KEY_FILE_ERROR_INVALID_VALUE,
1974 _("Key file contains key “%s” "
1975 "which has a value that cannot be interpreted."),
1977 g_error_free (key_file_error);
1980 g_propagate_error (error, key_file_error);
1983 return string_value;
1987 * g_key_file_set_string:
1988 * @key_file: a #GKeyFile
1989 * @group_name: a group name
1993 * Associates a new string value with @key under @group_name.
1994 * If @key cannot be found then it is created.
1995 * If @group_name cannot be found then it is created.
1996 * Unlike g_key_file_set_value(), this function handles characters
1997 * that need escaping, such as newlines.
2002 g_key_file_set_string (GKeyFile *key_file,
2003 const gchar *group_name,
2005 const gchar *string)
2009 g_return_if_fail (key_file != NULL);
2010 g_return_if_fail (string != NULL);
2012 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2013 g_key_file_set_value (key_file, group_name, key, value);
2018 * g_key_file_get_string_list:
2019 * @key_file: a #GKeyFile
2020 * @group_name: a group name
2022 * @length: (out) (optional): return location for the number of returned strings, or %NULL
2023 * @error: return location for a #GError, or %NULL
2025 * Returns the values associated with @key under @group_name.
2027 * In the event the key cannot be found, %NULL is returned and
2028 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
2029 * event that the @group_name cannot be found, %NULL is returned
2030 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
2032 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2033 * a %NULL-terminated string array or %NULL if the specified
2034 * key cannot be found. The array should be freed with g_strfreev().
2039 g_key_file_get_string_list (GKeyFile *key_file,
2040 const gchar *group_name,
2045 GError *key_file_error = NULL;
2046 gchar *value, *string_value, **values;
2048 GSList *p, *pieces = NULL;
2050 g_return_val_if_fail (key_file != NULL, NULL);
2051 g_return_val_if_fail (group_name != NULL, NULL);
2052 g_return_val_if_fail (key != NULL, NULL);
2057 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2061 g_propagate_error (error, key_file_error);
2065 if (!g_utf8_validate (value, -1, NULL))
2067 gchar *value_utf8 = g_utf8_make_valid (value, -1);
2068 g_set_error (error, G_KEY_FILE_ERROR,
2069 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
2070 _("Key file contains key “%s” with value “%s” "
2071 "which is not UTF-8"), key, value_utf8);
2072 g_free (value_utf8);
2078 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
2080 g_free (string_value);
2084 if (g_error_matches (key_file_error,
2086 G_KEY_FILE_ERROR_INVALID_VALUE))
2088 g_set_error (error, G_KEY_FILE_ERROR,
2089 G_KEY_FILE_ERROR_INVALID_VALUE,
2090 _("Key file contains key “%s” "
2091 "which has a value that cannot be interpreted."),
2093 g_error_free (key_file_error);
2096 g_propagate_error (error, key_file_error);
2098 g_slist_free_full (pieces, g_free);
2102 len = g_slist_length (pieces);
2103 values = g_new (gchar *, len + 1);
2104 for (p = pieces, i = 0; p; p = p->next)
2105 values[i++] = p->data;
2108 g_slist_free (pieces);
2117 * g_key_file_set_string_list:
2118 * @key_file: a #GKeyFile
2119 * @group_name: a group name
2121 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2122 * @length: number of string values in @list
2124 * Associates a list of string values for @key under @group_name.
2125 * If @key cannot be found then it is created.
2126 * If @group_name cannot be found then it is created.
2131 g_key_file_set_string_list (GKeyFile *key_file,
2132 const gchar *group_name,
2134 const gchar * const list[],
2137 GString *value_list;
2140 g_return_if_fail (key_file != NULL);
2141 g_return_if_fail (list != NULL || length == 0);
2143 value_list = g_string_sized_new (length * 128);
2144 for (i = 0; i < length && list[i] != NULL; i++)
2148 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2149 g_string_append (value_list, value);
2150 g_string_append_c (value_list, key_file->list_separator);
2155 g_key_file_set_value (key_file, group_name, key, value_list->str);
2156 g_string_free (value_list, TRUE);
2160 * g_key_file_set_locale_string:
2161 * @key_file: a #GKeyFile
2162 * @group_name: a group name
2164 * @locale: a locale identifier
2167 * Associates a string value for @key and @locale under @group_name.
2168 * If the translation for @key cannot be found then it is created.
2173 g_key_file_set_locale_string (GKeyFile *key_file,
2174 const gchar *group_name,
2176 const gchar *locale,
2177 const gchar *string)
2179 gchar *full_key, *value;
2181 g_return_if_fail (key_file != NULL);
2182 g_return_if_fail (key != NULL);
2183 g_return_if_fail (locale != NULL);
2184 g_return_if_fail (string != NULL);
2186 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2187 full_key = g_strdup_printf ("%s[%s]", key, locale);
2188 g_key_file_set_value (key_file, group_name, full_key, value);
2194 * g_key_file_get_locale_string:
2195 * @key_file: a #GKeyFile
2196 * @group_name: a group name
2198 * @locale: (nullable): a locale identifier or %NULL
2199 * @error: return location for a #GError, or %NULL
2201 * Returns the value associated with @key under @group_name
2202 * translated in the given @locale if available. If @locale is
2203 * %NULL then the current locale is assumed.
2205 * If @locale is to be non-%NULL, or if the current locale will change over
2206 * the lifetime of the #GKeyFile, it must be loaded with
2207 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2209 * If @key cannot be found then %NULL is returned and @error is set
2210 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2211 * with @key cannot be interpreted or no suitable translation can
2212 * be found then the untranslated value is returned.
2214 * Returns: a newly allocated string or %NULL if the specified
2215 * key cannot be found.
2220 g_key_file_get_locale_string (GKeyFile *key_file,
2221 const gchar *group_name,
2223 const gchar *locale,
2226 gchar *candidate_key, *translated_value;
2227 GError *key_file_error;
2229 gboolean free_languages = FALSE;
2232 g_return_val_if_fail (key_file != NULL, NULL);
2233 g_return_val_if_fail (group_name != NULL, NULL);
2234 g_return_val_if_fail (key != NULL, NULL);
2236 candidate_key = NULL;
2237 translated_value = NULL;
2238 key_file_error = NULL;
2242 languages = g_get_locale_variants (locale);
2243 free_languages = TRUE;
2247 languages = (gchar **) g_get_language_names ();
2248 free_languages = FALSE;
2251 for (i = 0; languages[i]; i++)
2253 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2255 translated_value = g_key_file_get_string (key_file,
2257 candidate_key, NULL);
2258 g_free (candidate_key);
2260 if (translated_value)
2264 /* Fallback to untranslated key
2266 if (!translated_value)
2268 translated_value = g_key_file_get_string (key_file, group_name, key,
2271 if (!translated_value)
2272 g_propagate_error (error, key_file_error);
2276 g_strfreev (languages);
2278 return translated_value;
2282 * g_key_file_get_locale_for_key:
2283 * @key_file: a #GKeyFile
2284 * @group_name: a group name
2286 * @locale: (nullable): a locale identifier or %NULL
2288 * Returns the actual locale which the result of
2289 * g_key_file_get_locale_string() or g_key_file_get_locale_string_list()
2292 * If calling g_key_file_get_locale_string() or
2293 * g_key_file_get_locale_string_list() with exactly the same @key_file,
2294 * @group_name, @key and @locale, the result of those functions will
2295 * have originally been tagged with the locale that is the result of
2298 * Returns: (nullable): the locale from the file, or %NULL if the key was not
2299 * found or the entry in the file was was untranslated
2304 g_key_file_get_locale_for_key (GKeyFile *key_file,
2305 const gchar *group_name,
2307 const gchar *locale)
2309 gchar **languages_allocated = NULL;
2310 const gchar * const *languages;
2311 gchar *result = NULL;
2314 g_return_val_if_fail (key_file != NULL, NULL);
2315 g_return_val_if_fail (group_name != NULL, NULL);
2316 g_return_val_if_fail (key != NULL, NULL);
2320 languages_allocated = g_get_locale_variants (locale);
2321 languages = (const gchar * const *) languages_allocated;
2324 languages = g_get_language_names ();
2326 for (i = 0; languages[i] != NULL; i++)
2328 gchar *candidate_key, *translated_value;
2330 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2331 translated_value = g_key_file_get_string (key_file, group_name, candidate_key, NULL);
2332 g_free (translated_value);
2333 g_free (candidate_key);
2335 if (translated_value != NULL)
2339 result = g_strdup (languages[i]);
2341 g_strfreev (languages_allocated);
2347 * g_key_file_get_locale_string_list:
2348 * @key_file: a #GKeyFile
2349 * @group_name: a group name
2351 * @locale: (nullable): a locale identifier or %NULL
2352 * @length: (out) (optional): return location for the number of returned strings or %NULL
2353 * @error: return location for a #GError or %NULL
2355 * Returns the values associated with @key under @group_name
2356 * translated in the given @locale if available. If @locale is
2357 * %NULL then the current locale is assumed.
2359 * If @locale is to be non-%NULL, or if the current locale will change over
2360 * the lifetime of the #GKeyFile, it must be loaded with
2361 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2363 * If @key cannot be found then %NULL is returned and @error is set
2364 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2365 * with @key cannot be interpreted or no suitable translations
2366 * can be found then the untranslated values are returned. The
2367 * returned array is %NULL-terminated, so @length may optionally
2370 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2371 * or %NULL if the key isn't found. The string array should be freed
2372 * with g_strfreev().
2377 g_key_file_get_locale_string_list (GKeyFile *key_file,
2378 const gchar *group_name,
2380 const gchar *locale,
2384 GError *key_file_error;
2385 gchar **values, *value;
2386 char list_separator[2];
2389 g_return_val_if_fail (key_file != NULL, NULL);
2390 g_return_val_if_fail (group_name != NULL, NULL);
2391 g_return_val_if_fail (key != NULL, NULL);
2393 key_file_error = NULL;
2395 value = g_key_file_get_locale_string (key_file, group_name,
2400 g_propagate_error (error, key_file_error);
2409 len = strlen (value);
2410 if (value[len - 1] == key_file->list_separator)
2411 value[len - 1] = '\0';
2413 list_separator[0] = key_file->list_separator;
2414 list_separator[1] = '\0';
2415 values = g_strsplit (value, list_separator, 0);
2420 *length = g_strv_length (values);
2426 * g_key_file_set_locale_string_list:
2427 * @key_file: a #GKeyFile
2428 * @group_name: a group name
2430 * @locale: a locale identifier
2431 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2432 * @length: the length of @list
2434 * Associates a list of string values for @key and @locale under
2435 * @group_name. If the translation for @key cannot be found then
2441 g_key_file_set_locale_string_list (GKeyFile *key_file,
2442 const gchar *group_name,
2444 const gchar *locale,
2445 const gchar * const list[],
2448 GString *value_list;
2452 g_return_if_fail (key_file != NULL);
2453 g_return_if_fail (key != NULL);
2454 g_return_if_fail (locale != NULL);
2455 g_return_if_fail (length != 0);
2457 value_list = g_string_sized_new (length * 128);
2458 for (i = 0; i < length && list[i] != NULL; i++)
2462 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2463 g_string_append (value_list, value);
2464 g_string_append_c (value_list, key_file->list_separator);
2469 full_key = g_strdup_printf ("%s[%s]", key, locale);
2470 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2472 g_string_free (value_list, TRUE);
2476 * g_key_file_get_boolean:
2477 * @key_file: a #GKeyFile
2478 * @group_name: a group name
2480 * @error: return location for a #GError
2482 * Returns the value associated with @key under @group_name as a
2485 * If @key cannot be found then %FALSE is returned and @error is set
2486 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2487 * associated with @key cannot be interpreted as a boolean then %FALSE
2488 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2490 * Returns: the value associated with the key as a boolean,
2491 * or %FALSE if the key was not found or could not be parsed.
2496 g_key_file_get_boolean (GKeyFile *key_file,
2497 const gchar *group_name,
2501 GError *key_file_error = NULL;
2503 gboolean bool_value;
2505 g_return_val_if_fail (key_file != NULL, FALSE);
2506 g_return_val_if_fail (group_name != NULL, FALSE);
2507 g_return_val_if_fail (key != NULL, FALSE);
2509 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2513 g_propagate_error (error, key_file_error);
2517 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2523 if (g_error_matches (key_file_error,
2525 G_KEY_FILE_ERROR_INVALID_VALUE))
2527 g_set_error (error, G_KEY_FILE_ERROR,
2528 G_KEY_FILE_ERROR_INVALID_VALUE,
2529 _("Key file contains key “%s” "
2530 "which has a value that cannot be interpreted."),
2532 g_error_free (key_file_error);
2535 g_propagate_error (error, key_file_error);
2542 * g_key_file_set_boolean:
2543 * @key_file: a #GKeyFile
2544 * @group_name: a group name
2546 * @value: %TRUE or %FALSE
2548 * Associates a new boolean value with @key under @group_name.
2549 * If @key cannot be found then it is created.
2554 g_key_file_set_boolean (GKeyFile *key_file,
2555 const gchar *group_name,
2561 g_return_if_fail (key_file != NULL);
2563 result = g_key_file_parse_boolean_as_value (key_file, value);
2564 g_key_file_set_value (key_file, group_name, key, result);
2569 * g_key_file_get_boolean_list:
2570 * @key_file: a #GKeyFile
2571 * @group_name: a group name
2573 * @length: (out): the number of booleans returned
2574 * @error: return location for a #GError
2576 * Returns the values associated with @key under @group_name as
2579 * If @key cannot be found then %NULL is returned and @error is set to
2580 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2581 * with @key cannot be interpreted as booleans then %NULL is returned
2582 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2584 * Returns: (array length=length) (element-type gboolean) (transfer container):
2585 * the values associated with the key as a list of booleans, or %NULL if the
2586 * key was not found or could not be parsed. The returned list of booleans
2587 * should be freed with g_free() when no longer needed.
2592 g_key_file_get_boolean_list (GKeyFile *key_file,
2593 const gchar *group_name,
2598 GError *key_file_error;
2600 gboolean *bool_values;
2603 g_return_val_if_fail (key_file != NULL, NULL);
2604 g_return_val_if_fail (group_name != NULL, NULL);
2605 g_return_val_if_fail (key != NULL, NULL);
2610 key_file_error = NULL;
2612 values = g_key_file_get_string_list (key_file, group_name, key,
2613 &num_bools, &key_file_error);
2616 g_propagate_error (error, key_file_error);
2621 bool_values = g_new (gboolean, num_bools);
2623 for (i = 0; i < num_bools; i++)
2625 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2631 g_propagate_error (error, key_file_error);
2632 g_strfreev (values);
2633 g_free (bool_values);
2638 g_strfreev (values);
2641 *length = num_bools;
2647 * g_key_file_set_boolean_list:
2648 * @key_file: a #GKeyFile
2649 * @group_name: a group name
2651 * @list: (array length=length): an array of boolean values
2652 * @length: length of @list
2654 * Associates a list of boolean values with @key under @group_name.
2655 * If @key cannot be found then it is created.
2656 * If @group_name is %NULL, the start_group is used.
2661 g_key_file_set_boolean_list (GKeyFile *key_file,
2662 const gchar *group_name,
2667 GString *value_list;
2670 g_return_if_fail (key_file != NULL);
2671 g_return_if_fail (list != NULL);
2673 value_list = g_string_sized_new (length * 8);
2674 for (i = 0; i < length; i++)
2678 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2680 g_string_append (value_list, value);
2681 g_string_append_c (value_list, key_file->list_separator);
2686 g_key_file_set_value (key_file, group_name, key, value_list->str);
2687 g_string_free (value_list, TRUE);
2691 * g_key_file_get_integer:
2692 * @key_file: a #GKeyFile
2693 * @group_name: a group name
2695 * @error: return location for a #GError
2697 * Returns the value associated with @key under @group_name as an
2700 * If @key cannot be found then 0 is returned and @error is set to
2701 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2702 * with @key cannot be interpreted as an integer, or is out of range
2703 * for a #gint, then 0 is returned
2704 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2706 * Returns: the value associated with the key as an integer, or
2707 * 0 if the key was not found or could not be parsed.
2712 g_key_file_get_integer (GKeyFile *key_file,
2713 const gchar *group_name,
2717 GError *key_file_error;
2721 g_return_val_if_fail (key_file != NULL, -1);
2722 g_return_val_if_fail (group_name != NULL, -1);
2723 g_return_val_if_fail (key != NULL, -1);
2725 key_file_error = NULL;
2727 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2731 g_propagate_error (error, key_file_error);
2735 int_value = g_key_file_parse_value_as_integer (key_file, value,
2741 if (g_error_matches (key_file_error,
2743 G_KEY_FILE_ERROR_INVALID_VALUE))
2745 g_set_error (error, G_KEY_FILE_ERROR,
2746 G_KEY_FILE_ERROR_INVALID_VALUE,
2747 _("Key file contains key “%s” in group “%s” "
2748 "which has a value that cannot be interpreted."),
2750 g_error_free (key_file_error);
2753 g_propagate_error (error, key_file_error);
2760 * g_key_file_set_integer:
2761 * @key_file: a #GKeyFile
2762 * @group_name: a group name
2764 * @value: an integer value
2766 * Associates a new integer value with @key under @group_name.
2767 * If @key cannot be found then it is created.
2772 g_key_file_set_integer (GKeyFile *key_file,
2773 const gchar *group_name,
2779 g_return_if_fail (key_file != NULL);
2781 result = g_key_file_parse_integer_as_value (key_file, value);
2782 g_key_file_set_value (key_file, group_name, key, result);
2787 * g_key_file_get_int64:
2788 * @key_file: a non-%NULL #GKeyFile
2789 * @group_name: a non-%NULL group name
2790 * @key: a non-%NULL key
2791 * @error: return location for a #GError
2793 * Returns the value associated with @key under @group_name as a signed
2794 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2795 * 64-bit results without truncation.
2797 * Returns: the value associated with the key as a signed 64-bit integer, or
2798 * 0 if the key was not found or could not be parsed.
2803 g_key_file_get_int64 (GKeyFile *key_file,
2804 const gchar *group_name,
2811 g_return_val_if_fail (key_file != NULL, -1);
2812 g_return_val_if_fail (group_name != NULL, -1);
2813 g_return_val_if_fail (key != NULL, -1);
2815 s = g_key_file_get_value (key_file, group_name, key, error);
2820 v = g_ascii_strtoll (s, &end, 10);
2822 if (*s == '\0' || *end != '\0')
2824 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2825 _("Key “%s” in group “%s” has value “%s” "
2826 "where %s was expected"),
2827 key, group_name, s, "int64");
2837 * g_key_file_set_int64:
2838 * @key_file: a #GKeyFile
2839 * @group_name: a group name
2841 * @value: an integer value
2843 * Associates a new integer value with @key under @group_name.
2844 * If @key cannot be found then it is created.
2849 g_key_file_set_int64 (GKeyFile *key_file,
2850 const gchar *group_name,
2856 g_return_if_fail (key_file != NULL);
2858 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2859 g_key_file_set_value (key_file, group_name, key, result);
2864 * g_key_file_get_uint64:
2865 * @key_file: a non-%NULL #GKeyFile
2866 * @group_name: a non-%NULL group name
2867 * @key: a non-%NULL key
2868 * @error: return location for a #GError
2870 * Returns the value associated with @key under @group_name as an unsigned
2871 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2872 * large positive results without truncation.
2874 * Returns: the value associated with the key as an unsigned 64-bit integer,
2875 * or 0 if the key was not found or could not be parsed.
2880 g_key_file_get_uint64 (GKeyFile *key_file,
2881 const gchar *group_name,
2888 g_return_val_if_fail (key_file != NULL, -1);
2889 g_return_val_if_fail (group_name != NULL, -1);
2890 g_return_val_if_fail (key != NULL, -1);
2892 s = g_key_file_get_value (key_file, group_name, key, error);
2897 v = g_ascii_strtoull (s, &end, 10);
2899 if (*s == '\0' || *end != '\0')
2901 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2902 _("Key “%s” in group “%s” has value “%s” "
2903 "where %s was expected"),
2904 key, group_name, s, "uint64");
2914 * g_key_file_set_uint64:
2915 * @key_file: a #GKeyFile
2916 * @group_name: a group name
2918 * @value: an integer value
2920 * Associates a new integer value with @key under @group_name.
2921 * If @key cannot be found then it is created.
2926 g_key_file_set_uint64 (GKeyFile *key_file,
2927 const gchar *group_name,
2933 g_return_if_fail (key_file != NULL);
2935 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2936 g_key_file_set_value (key_file, group_name, key, result);
2941 * g_key_file_get_integer_list:
2942 * @key_file: a #GKeyFile
2943 * @group_name: a group name
2945 * @length: (out): the number of integers returned
2946 * @error: return location for a #GError
2948 * Returns the values associated with @key under @group_name as
2951 * If @key cannot be found then %NULL is returned and @error is set to
2952 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2953 * with @key cannot be interpreted as integers, or are out of range for
2954 * #gint, then %NULL is returned
2955 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2957 * Returns: (array length=length) (element-type gint) (transfer container):
2958 * the values associated with the key as a list of integers, or %NULL if
2959 * the key was not found or could not be parsed. The returned list of
2960 * integers should be freed with g_free() when no longer needed.
2965 g_key_file_get_integer_list (GKeyFile *key_file,
2966 const gchar *group_name,
2971 GError *key_file_error = NULL;
2976 g_return_val_if_fail (key_file != NULL, NULL);
2977 g_return_val_if_fail (group_name != NULL, NULL);
2978 g_return_val_if_fail (key != NULL, NULL);
2983 values = g_key_file_get_string_list (key_file, group_name, key,
2984 &num_ints, &key_file_error);
2987 g_propagate_error (error, key_file_error);
2992 int_values = g_new (gint, num_ints);
2994 for (i = 0; i < num_ints; i++)
2996 int_values[i] = g_key_file_parse_value_as_integer (key_file,
3002 g_propagate_error (error, key_file_error);
3003 g_strfreev (values);
3004 g_free (int_values);
3009 g_strfreev (values);
3018 * g_key_file_set_integer_list:
3019 * @key_file: a #GKeyFile
3020 * @group_name: a group name
3022 * @list: (array length=length): an array of integer values
3023 * @length: number of integer values in @list
3025 * Associates a list of integer values with @key under @group_name.
3026 * If @key cannot be found then it is created.
3031 g_key_file_set_integer_list (GKeyFile *key_file,
3032 const gchar *group_name,
3040 g_return_if_fail (key_file != NULL);
3041 g_return_if_fail (list != NULL);
3043 values = g_string_sized_new (length * 16);
3044 for (i = 0; i < length; i++)
3048 value = g_key_file_parse_integer_as_value (key_file, list[i]);
3050 g_string_append (values, value);
3051 g_string_append_c (values, key_file->list_separator);
3056 g_key_file_set_value (key_file, group_name, key, values->str);
3057 g_string_free (values, TRUE);
3061 * g_key_file_get_double:
3062 * @key_file: a #GKeyFile
3063 * @group_name: a group name
3065 * @error: return location for a #GError
3067 * Returns the value associated with @key under @group_name as a
3068 * double. If @group_name is %NULL, the start_group is used.
3070 * If @key cannot be found then 0.0 is returned and @error is set to
3071 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
3072 * with @key cannot be interpreted as a double then 0.0 is returned
3073 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3075 * Returns: the value associated with the key as a double, or
3076 * 0.0 if the key was not found or could not be parsed.
3081 g_key_file_get_double (GKeyFile *key_file,
3082 const gchar *group_name,
3086 GError *key_file_error;
3088 gdouble double_value;
3090 g_return_val_if_fail (key_file != NULL, -1);
3091 g_return_val_if_fail (group_name != NULL, -1);
3092 g_return_val_if_fail (key != NULL, -1);
3094 key_file_error = NULL;
3096 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3100 g_propagate_error (error, key_file_error);
3104 double_value = g_key_file_parse_value_as_double (key_file, value,
3110 if (g_error_matches (key_file_error,
3112 G_KEY_FILE_ERROR_INVALID_VALUE))
3114 g_set_error (error, G_KEY_FILE_ERROR,
3115 G_KEY_FILE_ERROR_INVALID_VALUE,
3116 _("Key file contains key “%s” in group “%s” "
3117 "which has a value that cannot be interpreted."),
3119 g_error_free (key_file_error);
3122 g_propagate_error (error, key_file_error);
3125 return double_value;
3129 * g_key_file_set_double:
3130 * @key_file: a #GKeyFile
3131 * @group_name: a group name
3133 * @value: a double value
3135 * Associates a new double value with @key under @group_name.
3136 * If @key cannot be found then it is created.
3141 g_key_file_set_double (GKeyFile *key_file,
3142 const gchar *group_name,
3146 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3148 g_return_if_fail (key_file != NULL);
3150 g_ascii_dtostr (result, sizeof (result), value);
3151 g_key_file_set_value (key_file, group_name, key, result);
3155 * g_key_file_get_double_list:
3156 * @key_file: a #GKeyFile
3157 * @group_name: a group name
3159 * @length: (out): the number of doubles returned
3160 * @error: return location for a #GError
3162 * Returns the values associated with @key under @group_name as
3165 * If @key cannot be found then %NULL is returned and @error is set to
3166 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3167 * with @key cannot be interpreted as doubles then %NULL is returned
3168 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3170 * Returns: (array length=length) (element-type gdouble) (transfer container):
3171 * the values associated with the key as a list of doubles, or %NULL if the
3172 * key was not found or could not be parsed. The returned list of doubles
3173 * should be freed with g_free() when no longer needed.
3178 g_key_file_get_double_list (GKeyFile *key_file,
3179 const gchar *group_name,
3184 GError *key_file_error = NULL;
3186 gdouble *double_values;
3187 gsize i, num_doubles;
3189 g_return_val_if_fail (key_file != NULL, NULL);
3190 g_return_val_if_fail (group_name != NULL, NULL);
3191 g_return_val_if_fail (key != NULL, NULL);
3196 values = g_key_file_get_string_list (key_file, group_name, key,
3197 &num_doubles, &key_file_error);
3200 g_propagate_error (error, key_file_error);
3205 double_values = g_new (gdouble, num_doubles);
3207 for (i = 0; i < num_doubles; i++)
3209 double_values[i] = g_key_file_parse_value_as_double (key_file,
3215 g_propagate_error (error, key_file_error);
3216 g_strfreev (values);
3217 g_free (double_values);
3222 g_strfreev (values);
3225 *length = num_doubles;
3227 return double_values;
3231 * g_key_file_set_double_list:
3232 * @key_file: a #GKeyFile
3233 * @group_name: a group name
3235 * @list: (array length=length): an array of double values
3236 * @length: number of double values in @list
3238 * Associates a list of double values with @key under
3239 * @group_name. If @key cannot be found then it is created.
3244 g_key_file_set_double_list (GKeyFile *key_file,
3245 const gchar *group_name,
3253 g_return_if_fail (key_file != NULL);
3254 g_return_if_fail (list != NULL);
3256 values = g_string_sized_new (length * 16);
3257 for (i = 0; i < length; i++)
3259 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3261 g_ascii_dtostr( result, sizeof (result), list[i] );
3263 g_string_append (values, result);
3264 g_string_append_c (values, key_file->list_separator);
3267 g_key_file_set_value (key_file, group_name, key, values->str);
3268 g_string_free (values, TRUE);
3272 g_key_file_set_key_comment (GKeyFile *key_file,
3273 const gchar *group_name,
3275 const gchar *comment,
3278 GKeyFileGroup *group;
3279 GKeyFileKeyValuePair *pair;
3280 GList *key_node, *comment_node, *tmp;
3282 group = g_key_file_lookup_group (key_file, group_name);
3285 g_set_error (error, G_KEY_FILE_ERROR,
3286 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3287 _("Key file does not have group “%s”"),
3288 group_name ? group_name : "(null)");
3293 /* First find the key the comments are supposed to be
3296 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3298 if (key_node == NULL)
3300 set_not_found_key_error (group->name, key, error);
3304 /* Then find all the comments already associated with the
3307 tmp = key_node->next;
3310 pair = (GKeyFileKeyValuePair *) tmp->data;
3312 if (pair->key != NULL)
3317 g_key_file_remove_key_value_pair_node (key_file, group,
3321 if (comment == NULL)
3324 /* Now we can add our new comment
3326 pair = g_slice_new (GKeyFileKeyValuePair);
3328 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3330 key_node = g_list_insert (key_node, pair, 1);
3337 g_key_file_set_group_comment (GKeyFile *key_file,
3338 const gchar *group_name,
3339 const gchar *comment,
3342 GKeyFileGroup *group;
3344 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3346 group = g_key_file_lookup_group (key_file, group_name);
3349 g_set_error (error, G_KEY_FILE_ERROR,
3350 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3351 _("Key file does not have group “%s”"),
3352 group_name ? group_name : "(null)");
3357 /* First remove any existing comment
3361 g_key_file_key_value_pair_free (group->comment);
3362 group->comment = NULL;
3365 if (comment == NULL)
3368 /* Now we can add our new comment
3370 group->comment = g_slice_new (GKeyFileKeyValuePair);
3371 group->comment->key = NULL;
3372 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3378 g_key_file_set_top_comment (GKeyFile *key_file,
3379 const gchar *comment,
3383 GKeyFileGroup *group;
3384 GKeyFileKeyValuePair *pair;
3386 /* The last group in the list should be the top (comments only)
3389 g_warn_if_fail (key_file->groups != NULL);
3390 group_node = g_list_last (key_file->groups);
3391 group = (GKeyFileGroup *) group_node->data;
3392 g_warn_if_fail (group->name == NULL);
3394 /* Note all keys must be comments at the top of
3395 * the file, so we can just free it all.
3397 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3398 group->key_value_pairs = NULL;
3400 if (comment == NULL)
3403 pair = g_slice_new (GKeyFileKeyValuePair);
3405 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3407 group->key_value_pairs =
3408 g_list_prepend (group->key_value_pairs, pair);
3414 * g_key_file_set_comment:
3415 * @key_file: a #GKeyFile
3416 * @group_name: (nullable): a group name, or %NULL
3417 * @key: (nullable): a key
3418 * @comment: a comment
3419 * @error: return location for a #GError
3421 * Places a comment above @key from @group_name.
3423 * If @key is %NULL then @comment will be written above @group_name.
3424 * If both @key and @group_name are %NULL, then @comment will be
3425 * written above the first group in the file.
3427 * Note that this function prepends a '#' comment marker to
3428 * each line of @comment.
3430 * Returns: %TRUE if the comment was written, %FALSE otherwise
3435 g_key_file_set_comment (GKeyFile *key_file,
3436 const gchar *group_name,
3438 const gchar *comment,
3441 g_return_val_if_fail (key_file != NULL, FALSE);
3443 if (group_name != NULL && key != NULL)
3445 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3448 else if (group_name != NULL)
3450 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3455 if (!g_key_file_set_top_comment (key_file, comment, error))
3463 g_key_file_get_key_comment (GKeyFile *key_file,
3464 const gchar *group_name,
3468 GKeyFileGroup *group;
3469 GKeyFileKeyValuePair *pair;
3470 GList *key_node, *tmp;
3474 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3476 group = g_key_file_lookup_group (key_file, group_name);
3479 g_set_error (error, G_KEY_FILE_ERROR,
3480 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3481 _("Key file does not have group “%s”"),
3482 group_name ? group_name : "(null)");
3487 /* First find the key the comments are supposed to be
3490 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3492 if (key_node == NULL)
3494 set_not_found_key_error (group->name, key, error);
3500 /* Then find all the comments already associated with the
3501 * key and concatenate them.
3503 tmp = key_node->next;
3504 if (!key_node->next)
3507 pair = (GKeyFileKeyValuePair *) tmp->data;
3508 if (pair->key != NULL)
3513 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3515 if (pair->key != NULL)
3521 while (tmp != key_node)
3523 pair = (GKeyFileKeyValuePair *) tmp->data;
3526 string = g_string_sized_new (512);
3528 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3529 (tmp->prev == key_node));
3530 g_string_append (string, comment);
3538 comment = string->str;
3539 g_string_free (string, FALSE);
3548 get_group_comment (GKeyFile *key_file,
3549 GKeyFileGroup *group,
3558 tmp = group->key_value_pairs;
3561 GKeyFileKeyValuePair *pair;
3563 pair = (GKeyFileKeyValuePair *) tmp->data;
3565 if (pair->key != NULL)
3571 if (tmp->next == NULL)
3579 GKeyFileKeyValuePair *pair;
3581 pair = (GKeyFileKeyValuePair *) tmp->data;
3584 string = g_string_sized_new (512);
3586 comment = g_key_file_parse_value_as_comment (key_file, pair->value,
3587 (tmp->prev == NULL));
3588 g_string_append (string, comment);
3595 return g_string_free (string, FALSE);
3601 g_key_file_get_group_comment (GKeyFile *key_file,
3602 const gchar *group_name,
3606 GKeyFileGroup *group;
3608 group = g_key_file_lookup_group (key_file, group_name);
3611 g_set_error (error, G_KEY_FILE_ERROR,
3612 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3613 _("Key file does not have group “%s”"),
3614 group_name ? group_name : "(null)");
3620 return g_strdup (group->comment->value);
3622 group_node = g_key_file_lookup_group_node (key_file, group_name);
3623 group_node = group_node->next;
3624 group = (GKeyFileGroup *)group_node->data;
3625 return get_group_comment (key_file, group, error);
3629 g_key_file_get_top_comment (GKeyFile *key_file,
3633 GKeyFileGroup *group;
3635 /* The last group in the list should be the top (comments only)
3638 g_warn_if_fail (key_file->groups != NULL);
3639 group_node = g_list_last (key_file->groups);
3640 group = (GKeyFileGroup *) group_node->data;
3641 g_warn_if_fail (group->name == NULL);
3643 return get_group_comment (key_file, group, error);
3647 * g_key_file_get_comment:
3648 * @key_file: a #GKeyFile
3649 * @group_name: (nullable): a group name, or %NULL
3650 * @key: (nullable): a key
3651 * @error: return location for a #GError
3653 * Retrieves a comment above @key from @group_name.
3654 * If @key is %NULL then @comment will be read from above
3655 * @group_name. If both @key and @group_name are %NULL, then
3656 * @comment will be read from above the first group in the file.
3658 * Note that the returned string does not include the '#' comment markers,
3659 * but does include any whitespace after them (on each line). It includes
3660 * the line breaks between lines, but does not include the final line break.
3662 * Returns: a comment that should be freed with g_free()
3667 g_key_file_get_comment (GKeyFile *key_file,
3668 const gchar *group_name,
3672 g_return_val_if_fail (key_file != NULL, NULL);
3674 if (group_name != NULL && key != NULL)
3675 return g_key_file_get_key_comment (key_file, group_name, key, error);
3676 else if (group_name != NULL)
3677 return g_key_file_get_group_comment (key_file, group_name, error);
3679 return g_key_file_get_top_comment (key_file, error);
3683 * g_key_file_remove_comment:
3684 * @key_file: a #GKeyFile
3685 * @group_name: (nullable): a group name, or %NULL
3686 * @key: (nullable): a key
3687 * @error: return location for a #GError
3689 * Removes a comment above @key from @group_name.
3690 * If @key is %NULL then @comment will be removed above @group_name.
3691 * If both @key and @group_name are %NULL, then @comment will
3692 * be removed above the first group in the file.
3694 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3700 g_key_file_remove_comment (GKeyFile *key_file,
3701 const gchar *group_name,
3705 g_return_val_if_fail (key_file != NULL, FALSE);
3707 if (group_name != NULL && key != NULL)
3708 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3709 else if (group_name != NULL)
3710 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3712 return g_key_file_set_top_comment (key_file, NULL, error);
3716 * g_key_file_has_group:
3717 * @key_file: a #GKeyFile
3718 * @group_name: a group name
3720 * Looks whether the key file has the group @group_name.
3722 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3727 g_key_file_has_group (GKeyFile *key_file,
3728 const gchar *group_name)
3730 g_return_val_if_fail (key_file != NULL, FALSE);
3731 g_return_val_if_fail (group_name != NULL, FALSE);
3733 return g_key_file_lookup_group (key_file, group_name) != NULL;
3736 /* This code remains from a historical attempt to add a new public API
3737 * which respects the GError rules.
3740 g_key_file_has_key_full (GKeyFile *key_file,
3741 const gchar *group_name,
3746 GKeyFileKeyValuePair *pair;
3747 GKeyFileGroup *group;
3749 g_return_val_if_fail (key_file != NULL, FALSE);
3750 g_return_val_if_fail (group_name != NULL, FALSE);
3751 g_return_val_if_fail (key != NULL, FALSE);
3753 group = g_key_file_lookup_group (key_file, group_name);
3757 g_set_error (error, G_KEY_FILE_ERROR,
3758 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3759 _("Key file does not have group “%s”"),
3765 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3768 *has_key = pair != NULL;
3773 * g_key_file_has_key: (skip)
3774 * @key_file: a #GKeyFile
3775 * @group_name: a group name
3777 * @error: return location for a #GError
3779 * Looks whether the key file has the key @key in the group
3782 * Note that this function does not follow the rules for #GError strictly;
3783 * the return value both carries meaning and signals an error. To use
3784 * this function, you must pass a #GError pointer in @error, and check
3785 * whether it is not %NULL to see if an error occurred.
3787 * Language bindings should use g_key_file_get_value() to test whether
3788 * or not a key exists.
3790 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3795 g_key_file_has_key (GKeyFile *key_file,
3796 const gchar *group_name,
3800 GError *temp_error = NULL;
3803 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3809 g_propagate_error (error, temp_error);
3815 g_key_file_add_group (GKeyFile *key_file,
3816 const gchar *group_name)
3818 GKeyFileGroup *group;
3820 g_return_if_fail (key_file != NULL);
3821 g_return_if_fail (g_key_file_is_group_name (group_name));
3823 group = g_key_file_lookup_group (key_file, group_name);
3826 key_file->current_group = group;
3830 group = g_slice_new0 (GKeyFileGroup);
3831 group->name = g_strdup (group_name);
3832 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3833 key_file->groups = g_list_prepend (key_file->groups, group);
3834 key_file->current_group = group;
3836 if (key_file->start_group == NULL)
3837 key_file->start_group = group;
3839 if (!key_file->group_hash)
3840 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
3842 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3846 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3851 g_free (pair->value);
3852 g_slice_free (GKeyFileKeyValuePair, pair);
3856 /* Be careful not to call this function on a node with data in the
3857 * lookup map without removing it from the lookup map, first.
3859 * Some current cases where this warning is not a concern are
3861 * - the node being removed is a comment node
3862 * - the entire lookup map is getting destroyed soon after
3866 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3867 GKeyFileGroup *group,
3871 GKeyFileKeyValuePair *pair;
3873 pair = (GKeyFileKeyValuePair *) pair_node->data;
3875 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3877 g_warn_if_fail (pair->value != NULL);
3879 g_key_file_key_value_pair_free (pair);
3881 g_list_free_1 (pair_node);
3885 g_key_file_remove_group_node (GKeyFile *key_file,
3888 GKeyFileGroup *group;
3891 group = (GKeyFileGroup *) group_node->data;
3895 g_assert (key_file->group_hash);
3896 g_hash_table_remove (key_file->group_hash, group->name);
3899 /* If the current group gets deleted make the current group the last
3902 if (key_file->current_group == group)
3904 /* groups should always contain at least the top comment group,
3905 * unless g_key_file_clear has been called
3907 if (key_file->groups)
3908 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3910 key_file->current_group = NULL;
3913 /* If the start group gets deleted make the start group the first
3916 if (key_file->start_group == group)
3918 tmp = g_list_last (key_file->groups);
3921 if (tmp != group_node &&
3922 ((GKeyFileGroup *) tmp->data)->name != NULL)
3929 key_file->start_group = (GKeyFileGroup *) tmp->data;
3931 key_file->start_group = NULL;
3934 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3936 tmp = group->key_value_pairs;
3943 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3946 g_warn_if_fail (group->key_value_pairs == NULL);
3950 g_key_file_key_value_pair_free (group->comment);
3951 group->comment = NULL;
3954 if (group->lookup_map)
3956 g_hash_table_destroy (group->lookup_map);
3957 group->lookup_map = NULL;
3960 g_free ((gchar *) group->name);
3961 g_slice_free (GKeyFileGroup, group);
3962 g_list_free_1 (group_node);
3966 * g_key_file_remove_group:
3967 * @key_file: a #GKeyFile
3968 * @group_name: a group name
3969 * @error: return location for a #GError or %NULL
3971 * Removes the specified group, @group_name,
3972 * from the key file.
3974 * Returns: %TRUE if the group was removed, %FALSE otherwise
3979 g_key_file_remove_group (GKeyFile *key_file,
3980 const gchar *group_name,
3985 g_return_val_if_fail (key_file != NULL, FALSE);
3986 g_return_val_if_fail (group_name != NULL, FALSE);
3988 group_node = g_key_file_lookup_group_node (key_file, group_name);
3992 g_set_error (error, G_KEY_FILE_ERROR,
3993 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3994 _("Key file does not have group “%s”"),
3999 g_key_file_remove_group_node (key_file, group_node);
4005 g_key_file_add_key_value_pair (GKeyFile *key_file,
4006 GKeyFileGroup *group,
4007 GKeyFileKeyValuePair *pair)
4009 g_hash_table_replace (group->lookup_map, pair->key, pair);
4010 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
4014 g_key_file_add_key (GKeyFile *key_file,
4015 GKeyFileGroup *group,
4019 GKeyFileKeyValuePair *pair;
4021 pair = g_slice_new (GKeyFileKeyValuePair);
4022 pair->key = g_strdup (key);
4023 pair->value = g_strdup (value);
4025 g_key_file_add_key_value_pair (key_file, group, pair);
4029 * g_key_file_remove_key:
4030 * @key_file: a #GKeyFile
4031 * @group_name: a group name
4032 * @key: a key name to remove
4033 * @error: return location for a #GError or %NULL
4035 * Removes @key in @group_name from the key file.
4037 * Returns: %TRUE if the key was removed, %FALSE otherwise
4042 g_key_file_remove_key (GKeyFile *key_file,
4043 const gchar *group_name,
4047 GKeyFileGroup *group;
4048 GKeyFileKeyValuePair *pair;
4050 g_return_val_if_fail (key_file != NULL, FALSE);
4051 g_return_val_if_fail (group_name != NULL, FALSE);
4052 g_return_val_if_fail (key != NULL, FALSE);
4056 group = g_key_file_lookup_group (key_file, group_name);
4059 g_set_error (error, G_KEY_FILE_ERROR,
4060 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4061 _("Key file does not have group “%s”"),
4066 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
4070 set_not_found_key_error (group->name, key, error);
4074 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
4075 g_hash_table_remove (group->lookup_map, pair->key);
4076 g_key_file_key_value_pair_free (pair);
4082 g_key_file_lookup_group_node (GKeyFile *key_file,
4083 const gchar *group_name)
4085 GKeyFileGroup *group;
4088 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
4090 group = (GKeyFileGroup *) tmp->data;
4092 if (group && group->name && strcmp (group->name, group_name) == 0)
4099 static GKeyFileGroup *
4100 g_key_file_lookup_group (GKeyFile *key_file,
4101 const gchar *group_name)
4103 if (!key_file->group_hash)
4106 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4110 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4111 GKeyFileGroup *group,
4116 for (key_node = group->key_value_pairs;
4118 key_node = key_node->next)
4120 GKeyFileKeyValuePair *pair;
4122 pair = (GKeyFileKeyValuePair *) key_node->data;
4124 if (pair->key && strcmp (pair->key, key) == 0)
4131 static GKeyFileKeyValuePair *
4132 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4133 GKeyFileGroup *group,
4136 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4139 /* Lines starting with # or consisting entirely of whitespace are merely
4140 * recorded, not parsed. This function assumes all leading whitespace
4141 * has been stripped.
4144 g_key_file_line_is_comment (const gchar *line)
4146 return (*line == '#' || *line == '\0' || *line == '\n');
4150 g_key_file_is_group_name (const gchar *name)
4157 p = q = (gchar *) name;
4158 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4159 q = g_utf8_find_next_char (q, NULL);
4161 if (*q != '\0' || q == p)
4168 g_key_file_is_key_name (const gchar *name)
4175 p = q = (gchar *) name;
4176 /* We accept a little more than the desktop entry spec says,
4177 * since gnome-vfs uses mime-types as keys in its cache.
4179 while (*q && *q != '=' && *q != '[' && *q != ']')
4180 q = g_utf8_find_next_char (q, NULL);
4182 /* No empty keys, please */
4186 /* We accept spaces in the middle of keys to not break
4187 * existing apps, but we don't tolerate initial or final
4188 * spaces, which would lead to silent corruption when
4189 * rereading the file.
4191 if (*p == ' ' || q[-1] == ' ')
4197 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4198 q = g_utf8_find_next_char (q, NULL);
4212 /* A group in a key file is made up of a starting '[' followed by one
4213 * or more letters making up the group name followed by ']'.
4216 g_key_file_line_is_group (const gchar *line)
4226 while (*p && *p != ']')
4227 p = g_utf8_find_next_char (p, NULL);
4232 /* silently accept whitespace after the ] */
4233 p = g_utf8_find_next_char (p, NULL);
4234 while (*p == ' ' || *p == '\t')
4235 p = g_utf8_find_next_char (p, NULL);
4244 g_key_file_line_is_key_value_pair (const gchar *line)
4248 p = (gchar *) g_utf8_strchr (line, -1, '=');
4253 /* Key must be non-empty
4262 g_key_file_parse_value_as_string (GKeyFile *key_file,
4267 gchar *string_value, *p, *q0, *q;
4269 string_value = g_new (gchar, strlen (value) + 1);
4271 p = (gchar *) value;
4272 q0 = q = string_value;
4302 g_set_error_literal (error, G_KEY_FILE_ERROR,
4303 G_KEY_FILE_ERROR_INVALID_VALUE,
4304 _("Key file contains escape character "
4309 if (pieces && *p == key_file->list_separator)
4310 *q = key_file->list_separator;
4324 g_set_error (error, G_KEY_FILE_ERROR,
4325 G_KEY_FILE_ERROR_INVALID_VALUE,
4326 _("Key file contains invalid escape "
4327 "sequence “%s”"), sequence);
4336 if (pieces && (*p == key_file->list_separator))
4338 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4354 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4355 *pieces = g_slist_reverse (*pieces);
4358 return string_value;
4362 g_key_file_parse_string_as_value (GKeyFile *key_file,
4363 const gchar *string,
4364 gboolean escape_separator)
4366 gchar *value, *p, *q;
4368 gboolean parsing_leading_space;
4370 length = strlen (string) + 1;
4372 /* Worst case would be that every character needs to be escaped.
4373 * In other words every character turns to two characters
4375 value = g_new (gchar, 2 * length);
4377 p = (gchar *) string;
4379 parsing_leading_space = TRUE;
4380 while (p < (string + length - 1))
4382 gchar escaped_character[3] = { '\\', 0, 0 };
4387 if (parsing_leading_space)
4389 escaped_character[1] = 's';
4390 strcpy (q, escaped_character);
4400 if (parsing_leading_space)
4402 escaped_character[1] = 't';
4403 strcpy (q, escaped_character);
4413 escaped_character[1] = 'n';
4414 strcpy (q, escaped_character);
4418 escaped_character[1] = 'r';
4419 strcpy (q, escaped_character);
4423 escaped_character[1] = '\\';
4424 strcpy (q, escaped_character);
4426 parsing_leading_space = FALSE;
4429 if (escape_separator && *p == key_file->list_separator)
4431 escaped_character[1] = key_file->list_separator;
4432 strcpy (q, escaped_character);
4434 parsing_leading_space = TRUE;
4440 parsing_leading_space = FALSE;
4452 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4462 long_value = strtol (value, &eof_int, 10);
4465 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4467 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4468 g_set_error (error, G_KEY_FILE_ERROR,
4469 G_KEY_FILE_ERROR_INVALID_VALUE,
4470 _("Value “%s” cannot be interpreted "
4471 "as a number."), value_utf8);
4472 g_free (value_utf8);
4477 int_value = long_value;
4478 if (int_value != long_value || errsv == ERANGE)
4480 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4483 G_KEY_FILE_ERROR_INVALID_VALUE,
4484 _("Integer value “%s” out of range"),
4486 g_free (value_utf8);
4495 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4499 return g_strdup_printf ("%d", value);
4503 g_key_file_parse_value_as_double (GKeyFile *key_file,
4507 gchar *end_of_valid_d;
4508 gdouble double_value = 0;
4510 double_value = g_ascii_strtod (value, &end_of_valid_d);
4512 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4514 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4515 g_set_error (error, G_KEY_FILE_ERROR,
4516 G_KEY_FILE_ERROR_INVALID_VALUE,
4517 _("Value “%s” cannot be interpreted "
4518 "as a float number."),
4520 g_free (value_utf8);
4525 return double_value;
4529 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4531 size_t len2 = strlen (s2);
4532 return strncmp (s1, s2, MAX (len1, len2));
4536 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4543 /* Count the number of non-whitespace characters */
4544 for (i = 0; value[i]; i++)
4545 if (!g_ascii_isspace (value[i]))
4548 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4550 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4553 value_utf8 = g_utf8_make_valid (value, -1);
4554 g_set_error (error, G_KEY_FILE_ERROR,
4555 G_KEY_FILE_ERROR_INVALID_VALUE,
4556 _("Value “%s” cannot be interpreted "
4557 "as a boolean."), value_utf8);
4558 g_free (value_utf8);
4564 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4568 return g_strdup ("true");
4570 return g_strdup ("false");
4574 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4576 gboolean is_final_line)
4582 string = g_string_sized_new (512);
4584 lines = g_strsplit (value, "\n", 0);
4586 for (i = 0; lines[i] != NULL; i++)
4588 const gchar *line = lines[i];
4591 g_string_append_c (string, '\n');
4595 g_string_append (string, line);
4599 /* This function gets called once per line of a comment, but we don’t want
4600 * to add a trailing newline. */
4602 g_string_append_c (string, '\n');
4604 return g_string_free (string, FALSE);
4608 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4609 const gchar *comment)
4615 string = g_string_sized_new (512);
4617 lines = g_strsplit (comment, "\n", 0);
4619 for (i = 0; lines[i] != NULL; i++)
4620 g_string_append_printf (string, "#%s%s", lines[i],
4621 lines[i + 1] == NULL? "" : "\n");
4624 return g_string_free (string, FALSE);
4628 * g_key_file_save_to_file:
4629 * @key_file: a #GKeyFile
4630 * @filename: the name of the file to write to
4631 * @error: a pointer to a %NULL #GError, or %NULL
4633 * Writes the contents of @key_file to @filename using
4634 * g_file_set_contents(). If you need stricter guarantees about durability of
4635 * the written file than are provided by g_file_set_contents(), use
4636 * g_file_set_contents_full() with the return value of g_key_file_to_data().
4638 * This function can fail for any of the reasons that
4639 * g_file_set_contents() may fail.
4641 * Returns: %TRUE if successful, else %FALSE with @error set
4646 g_key_file_save_to_file (GKeyFile *key_file,
4647 const gchar *filename,
4654 g_return_val_if_fail (key_file != NULL, FALSE);
4655 g_return_val_if_fail (filename != NULL, FALSE);
4656 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4658 contents = g_key_file_to_data (key_file, &length, NULL);
4659 g_assert (contents != NULL);
4661 success = g_file_set_contents (filename, contents, length, error);