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. Usually
262 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
263 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
264 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
270 * G_KEY_FILE_DESKTOP_KEY_VERSION:
272 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
273 * giving the version of the Desktop Entry Specification used for
274 * the desktop entry file.
280 * G_KEY_FILE_DESKTOP_KEY_NAME:
282 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
283 * string giving the specific name of the desktop entry.
289 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
291 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
292 * string giving the generic name of the desktop entry.
298 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
300 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
301 * stating whether the desktop entry should be shown in menus.
307 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
309 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
310 * string giving the tooltip for the desktop entry.
316 * G_KEY_FILE_DESKTOP_KEY_ICON:
318 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
319 * string giving the name of the icon to be displayed for the desktop
326 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
328 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
329 * stating whether the desktop entry has been deleted by the user.
335 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
337 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
338 * strings identifying the environments that should display the
345 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
347 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
348 * strings identifying the environments that should not display the
355 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
357 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
358 * giving the file name of a binary on disk used to determine if the
359 * program is actually installed. It is only valid for desktop entries
360 * with the `Application` type.
366 * G_KEY_FILE_DESKTOP_KEY_EXEC:
368 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
369 * giving the command line to execute. It is only valid for desktop
370 * entries with the `Application` type.
376 * G_KEY_FILE_DESKTOP_KEY_PATH:
378 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
379 * containing the working directory to run the program in. It is only
380 * valid for desktop entries with the `Application` type.
386 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
388 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
389 * stating whether the program should be run in a terminal window.
390 * It is only valid for desktop entries with the
391 * `Application` type.
397 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
399 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
400 * of strings giving the MIME types supported by this desktop entry.
406 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
408 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
409 * of strings giving the categories in which the desktop entry
410 * should be shown in a menu.
416 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
418 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
419 * stating whether the application supports the
420 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
426 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
428 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
429 * identifying the WM class or name hint of a window that the application
430 * will create, which can be used to emulate Startup Notification with
431 * older applications.
437 * G_KEY_FILE_DESKTOP_KEY_URL:
439 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
440 * giving the URL to access. It is only valid for desktop entries
441 * with the `Link` type.
447 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
449 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to true
450 * if the application is D-Bus activatable.
456 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
458 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
459 * giving the available application actions.
465 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
467 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
468 * entries representing applications.
474 * G_KEY_FILE_DESKTOP_TYPE_LINK:
476 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
477 * entries representing links to documents.
483 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
485 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
486 * entries representing directories.
491 typedef struct _GKeyFileGroup GKeyFileGroup
;
496 * The GKeyFile struct contains only private data
497 * and should not be accessed directly.
502 GHashTable
*group_hash
;
504 GKeyFileGroup
*start_group
;
505 GKeyFileGroup
*current_group
;
507 GString
*parse_buffer
; /* Holds up to one line of not-yet-parsed data */
509 gchar list_separator
;
515 volatile gint ref_count
;
518 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair
;
520 struct _GKeyFileGroup
522 const gchar
*name
; /* NULL for above first group (which will be comments) */
524 GKeyFileKeyValuePair
*comment
; /* Special comment that is stuck to the top of a group */
526 GList
*key_value_pairs
;
528 /* Used in parallel with key_value_pairs for
529 * increased lookup performance
531 GHashTable
*lookup_map
;
534 struct _GKeyFileKeyValuePair
536 gchar
*key
; /* NULL for comments */
540 static gint
find_file_in_data_dirs (const gchar
*file
,
541 const gchar
**data_dirs
,
544 static gboolean
g_key_file_load_from_fd (GKeyFile
*key_file
,
548 static GList
*g_key_file_lookup_group_node (GKeyFile
*key_file
,
549 const gchar
*group_name
);
550 static GKeyFileGroup
*g_key_file_lookup_group (GKeyFile
*key_file
,
551 const gchar
*group_name
);
553 static GList
*g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
554 GKeyFileGroup
*group
,
556 static GKeyFileKeyValuePair
*g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
557 GKeyFileGroup
*group
,
560 static void g_key_file_remove_group_node (GKeyFile
*key_file
,
562 static void g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
563 GKeyFileGroup
*group
,
566 static void g_key_file_add_key_value_pair (GKeyFile
*key_file
,
567 GKeyFileGroup
*group
,
568 GKeyFileKeyValuePair
*pair
);
569 static void g_key_file_add_key (GKeyFile
*key_file
,
570 GKeyFileGroup
*group
,
573 static void g_key_file_add_group (GKeyFile
*key_file
,
574 const gchar
*group_name
);
575 static gboolean
g_key_file_is_group_name (const gchar
*name
);
576 static gboolean
g_key_file_is_key_name (const gchar
*name
);
577 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
);
578 static gboolean
g_key_file_line_is_comment (const gchar
*line
);
579 static gboolean
g_key_file_line_is_group (const gchar
*line
);
580 static gboolean
g_key_file_line_is_key_value_pair (const gchar
*line
);
581 static gchar
*g_key_file_parse_value_as_string (GKeyFile
*key_file
,
585 static gchar
*g_key_file_parse_string_as_value (GKeyFile
*key_file
,
587 gboolean escape_separator
);
588 static gint
g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
591 static gchar
*g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
593 static gdouble
g_key_file_parse_value_as_double (GKeyFile
*key_file
,
596 static gboolean
g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
599 static gchar
*g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
601 static gchar
*g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
603 static gchar
*g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
604 const gchar
*comment
);
605 static void g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
609 static void g_key_file_parse_comment (GKeyFile
*key_file
,
613 static void g_key_file_parse_group (GKeyFile
*key_file
,
617 static gchar
*key_get_locale (const gchar
*key
);
618 static void g_key_file_parse_data (GKeyFile
*key_file
,
622 static void g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
625 G_DEFINE_QUARK (g
-key
-file
-error
-quark
, g_key_file_error
)
628 g_key_file_init (GKeyFile
*key_file
)
630 key_file
->current_group
= g_slice_new0 (GKeyFileGroup
);
631 key_file
->groups
= g_list_prepend (NULL
, key_file
->current_group
);
632 key_file
->group_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
633 key_file
->start_group
= NULL
;
634 key_file
->parse_buffer
= g_string_sized_new (128);
635 key_file
->list_separator
= ';';
637 key_file
->locales
= g_strdupv ((gchar
**)g_get_language_names ());
641 g_key_file_clear (GKeyFile
*key_file
)
643 GList
*tmp
, *group_node
;
645 if (key_file
->locales
)
647 g_strfreev (key_file
->locales
);
648 key_file
->locales
= NULL
;
651 if (key_file
->parse_buffer
)
653 g_string_free (key_file
->parse_buffer
, TRUE
);
654 key_file
->parse_buffer
= NULL
;
657 tmp
= key_file
->groups
;
662 g_key_file_remove_group_node (key_file
, group_node
);
665 if (key_file
->group_hash
!= NULL
)
667 g_hash_table_destroy (key_file
->group_hash
);
668 key_file
->group_hash
= NULL
;
671 g_warn_if_fail (key_file
->groups
== NULL
);
678 * Creates a new empty #GKeyFile object. Use
679 * g_key_file_load_from_file(), g_key_file_load_from_data(),
680 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
681 * read an existing key file.
683 * Returns: (transfer full): an empty #GKeyFile.
688 g_key_file_new (void)
692 key_file
= g_slice_new0 (GKeyFile
);
693 key_file
->ref_count
= 1;
694 g_key_file_init (key_file
);
700 * g_key_file_set_list_separator:
701 * @key_file: a #GKeyFile
702 * @separator: the separator
704 * Sets the character which is used to separate
705 * values in lists. Typically ';' or ',' are used
706 * as separators. The default list separator is ';'.
711 g_key_file_set_list_separator (GKeyFile
*key_file
,
714 g_return_if_fail (key_file
!= NULL
);
716 key_file
->list_separator
= separator
;
720 /* Iterates through all the directories in *dirs trying to
721 * open file. When it successfully locates and opens a file it
722 * returns the file descriptor to the open file. It also
723 * outputs the absolute path of the file in output_file.
726 find_file_in_data_dirs (const gchar
*file
,
731 const gchar
**data_dirs
, *data_dir
;
743 while (data_dirs
&& (data_dir
= *data_dirs
) && fd
== -1)
745 gchar
*candidate_file
, *sub_dir
;
747 candidate_file
= (gchar
*) file
;
748 sub_dir
= g_strdup ("");
749 while (candidate_file
!= NULL
&& fd
== -1)
753 path
= g_build_filename (data_dir
, sub_dir
,
754 candidate_file
, NULL
);
756 fd
= g_open (path
, O_RDONLY
, 0);
764 candidate_file
= strchr (candidate_file
, '-');
766 if (candidate_file
== NULL
)
772 sub_dir
= g_strndup (file
, candidate_file
- file
- 1);
774 for (p
= sub_dir
; *p
!= '\0'; p
++)
777 *p
= G_DIR_SEPARATOR
;
786 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
787 G_KEY_FILE_ERROR_NOT_FOUND
,
788 _("Valid key file could not be "
789 "found in search dirs"));
792 if (output_file
!= NULL
&& fd
!= -1)
793 *output_file
= g_strdup (path
);
801 g_key_file_load_from_fd (GKeyFile
*key_file
,
806 GError
*key_file_error
= NULL
;
808 struct stat stat_buf
;
809 gchar read_buf
[4096];
810 gchar list_separator
;
812 if (fstat (fd
, &stat_buf
) < 0)
815 g_set_error_literal (error
, G_FILE_ERROR
,
816 g_file_error_from_errno (errsv
),
821 if (!S_ISREG (stat_buf
.st_mode
))
823 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
824 G_KEY_FILE_ERROR_PARSE
,
825 _("Not a regular file"));
829 list_separator
= key_file
->list_separator
;
830 g_key_file_clear (key_file
);
831 g_key_file_init (key_file
);
832 key_file
->list_separator
= list_separator
;
833 key_file
->flags
= flags
;
839 bytes_read
= read (fd
, read_buf
, 4096);
842 if (bytes_read
== 0) /* End of File */
847 if (errsv
== EINTR
|| errsv
== EAGAIN
)
850 g_set_error_literal (error
, G_FILE_ERROR
,
851 g_file_error_from_errno (errsv
),
856 g_key_file_parse_data (key_file
,
857 read_buf
, bytes_read
,
860 while (!key_file_error
);
864 g_propagate_error (error
, key_file_error
);
868 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
872 g_propagate_error (error
, key_file_error
);
880 * g_key_file_load_from_file:
881 * @key_file: an empty #GKeyFile struct
882 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
883 * @flags: flags from #GKeyFileFlags
884 * @error: return location for a #GError, or %NULL
886 * Loads a key file into an empty #GKeyFile structure.
888 * If the OS returns an error when opening or reading the file, a
889 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
890 * %G_KEY_FILE_ERROR is returned.
892 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
893 * @file is not found, %G_FILE_ERROR_NOENT is returned.
895 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
900 g_key_file_load_from_file (GKeyFile
*key_file
,
905 GError
*key_file_error
= NULL
;
909 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
910 g_return_val_if_fail (file
!= NULL
, FALSE
);
912 fd
= g_open (file
, O_RDONLY
, 0);
917 g_set_error_literal (error
, G_FILE_ERROR
,
918 g_file_error_from_errno (errsv
),
923 g_key_file_load_from_fd (key_file
, fd
, flags
, &key_file_error
);
928 g_propagate_error (error
, key_file_error
);
936 * g_key_file_load_from_data:
937 * @key_file: an empty #GKeyFile struct
938 * @data: key file loaded in memory
939 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
940 * @flags: flags from #GKeyFileFlags
941 * @error: return location for a #GError, or %NULL
943 * Loads a key file from memory into an empty #GKeyFile structure.
944 * If the object cannot be created then %error is set to a #GKeyFileError.
946 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
951 g_key_file_load_from_data (GKeyFile
*key_file
,
957 GError
*key_file_error
= NULL
;
958 gchar list_separator
;
960 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
961 g_return_val_if_fail (data
!= NULL
|| length
== 0, FALSE
);
963 if (length
== (gsize
)-1)
964 length
= strlen (data
);
966 list_separator
= key_file
->list_separator
;
967 g_key_file_clear (key_file
);
968 g_key_file_init (key_file
);
969 key_file
->list_separator
= list_separator
;
970 key_file
->flags
= flags
;
972 g_key_file_parse_data (key_file
, data
, length
, &key_file_error
);
976 g_propagate_error (error
, key_file_error
);
980 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
984 g_propagate_error (error
, key_file_error
);
992 * g_key_file_load_from_bytes:
993 * @key_file: an empty #GKeyFile struct
995 * @flags: flags from #GKeyFileFlags
996 * @error: return location for a #GError, or %NULL
998 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
999 * If the object cannot be created then %error is set to a #GKeyFileError.
1001 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1006 g_key_file_load_from_bytes (GKeyFile
*key_file
,
1008 GKeyFileFlags flags
,
1014 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
1015 g_return_val_if_fail (bytes
!= NULL
, FALSE
);
1017 data
= g_bytes_get_data (bytes
, &size
);
1018 return g_key_file_load_from_data (key_file
, (const gchar
*) data
, size
, flags
, error
);
1022 * g_key_file_load_from_dirs:
1023 * @key_file: an empty #GKeyFile struct
1024 * @file: (type filename): a relative path to a filename to open and parse
1025 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
1026 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1027 * of the file, or %NULL
1028 * @flags: flags from #GKeyFileFlags
1029 * @error: return location for a #GError, or %NULL
1031 * This function looks for a key file named @file in the paths
1032 * specified in @search_dirs, loads the file into @key_file and
1033 * returns the file's full path in @full_path.
1035 * If the file could not be found in any of the @search_dirs,
1036 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
1037 * the file is found but the OS returns an error when opening or reading the
1038 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
1039 * %G_KEY_FILE_ERROR is returned.
1041 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1046 g_key_file_load_from_dirs (GKeyFile
*key_file
,
1048 const gchar
**search_dirs
,
1050 GKeyFileFlags flags
,
1053 GError
*key_file_error
= NULL
;
1054 const gchar
**data_dirs
;
1057 gboolean found_file
;
1059 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
1060 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
1061 g_return_val_if_fail (search_dirs
!= NULL
, FALSE
);
1064 data_dirs
= search_dirs
;
1066 while (*data_dirs
!= NULL
&& !found_file
)
1068 g_free (output_path
);
1071 fd
= find_file_in_data_dirs (file
, data_dirs
, &output_path
,
1077 g_propagate_error (error
, key_file_error
);
1081 found_file
= g_key_file_load_from_fd (key_file
, fd
, flags
,
1087 g_propagate_error (error
, key_file_error
);
1092 if (found_file
&& full_path
)
1093 *full_path
= output_path
;
1095 g_free (output_path
);
1101 * g_key_file_load_from_data_dirs:
1102 * @key_file: an empty #GKeyFile struct
1103 * @file: (type filename): a relative path to a filename to open and parse
1104 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1105 * of the file, or %NULL
1106 * @flags: flags from #GKeyFileFlags
1107 * @error: return location for a #GError, or %NULL
1109 * This function looks for a key file named @file in the paths
1110 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1111 * loads the file into @key_file and returns the file's full path in
1112 * @full_path. If the file could not be loaded then an %error is
1113 * set to either a #GFileError or #GKeyFileError.
1115 * Returns: %TRUE if a key file could be loaded, %FALSE othewise
1119 g_key_file_load_from_data_dirs (GKeyFile
*key_file
,
1122 GKeyFileFlags flags
,
1125 gchar
**all_data_dirs
;
1126 const gchar
* user_data_dir
;
1127 const gchar
* const * system_data_dirs
;
1129 gboolean found_file
;
1131 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
1132 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
1134 user_data_dir
= g_get_user_data_dir ();
1135 system_data_dirs
= g_get_system_data_dirs ();
1136 all_data_dirs
= g_new (gchar
*, g_strv_length ((gchar
**)system_data_dirs
) + 2);
1139 all_data_dirs
[i
++] = g_strdup (user_data_dir
);
1142 while (system_data_dirs
[j
] != NULL
)
1143 all_data_dirs
[i
++] = g_strdup (system_data_dirs
[j
++]);
1144 all_data_dirs
[i
] = NULL
;
1146 found_file
= g_key_file_load_from_dirs (key_file
,
1148 (const gchar
**)all_data_dirs
,
1153 g_strfreev (all_data_dirs
);
1159 * g_key_file_ref: (skip)
1160 * @key_file: a #GKeyFile
1162 * Increases the reference count of @key_file.
1164 * Returns: the same @key_file.
1169 g_key_file_ref (GKeyFile
*key_file
)
1171 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1173 g_atomic_int_inc (&key_file
->ref_count
);
1179 * g_key_file_free: (skip)
1180 * @key_file: a #GKeyFile
1182 * Clears all keys and groups from @key_file, and decreases the
1183 * reference count by 1. If the reference count reaches zero,
1184 * frees the key file and all its allocated memory.
1189 g_key_file_free (GKeyFile
*key_file
)
1191 g_return_if_fail (key_file
!= NULL
);
1193 g_key_file_clear (key_file
);
1194 g_key_file_unref (key_file
);
1199 * @key_file: a #GKeyFile
1201 * Decreases the reference count of @key_file by 1. If the reference count
1202 * reaches zero, frees the key file and all its allocated memory.
1207 g_key_file_unref (GKeyFile
*key_file
)
1209 g_return_if_fail (key_file
!= NULL
);
1211 if (g_atomic_int_dec_and_test (&key_file
->ref_count
))
1213 g_key_file_clear (key_file
);
1214 g_slice_free (GKeyFile
, key_file
);
1218 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1219 * true for locales that match those in g_get_language_names().
1222 g_key_file_locale_is_interesting (GKeyFile
*key_file
,
1223 const gchar
*locale
)
1227 if (key_file
->flags
& G_KEY_FILE_KEEP_TRANSLATIONS
)
1230 for (i
= 0; key_file
->locales
[i
] != NULL
; i
++)
1232 if (g_ascii_strcasecmp (key_file
->locales
[i
], locale
) == 0)
1240 g_key_file_parse_line (GKeyFile
*key_file
,
1245 GError
*parse_error
= NULL
;
1248 g_return_if_fail (key_file
!= NULL
);
1249 g_return_if_fail (line
!= NULL
);
1251 line_start
= (gchar
*) line
;
1252 while (g_ascii_isspace (*line_start
))
1255 if (g_key_file_line_is_comment (line_start
))
1256 g_key_file_parse_comment (key_file
, line
, length
, &parse_error
);
1257 else if (g_key_file_line_is_group (line_start
))
1258 g_key_file_parse_group (key_file
, line_start
,
1259 length
- (line_start
- line
),
1261 else if (g_key_file_line_is_key_value_pair (line_start
))
1262 g_key_file_parse_key_value_pair (key_file
, line_start
,
1263 length
- (line_start
- line
),
1267 gchar
*line_utf8
= g_utf8_make_valid (line
, length
);
1268 g_set_error (error
, G_KEY_FILE_ERROR
,
1269 G_KEY_FILE_ERROR_PARSE
,
1270 _("Key file contains line “%s” which is not "
1271 "a key-value pair, group, or comment"),
1279 g_propagate_error (error
, parse_error
);
1283 g_key_file_parse_comment (GKeyFile
*key_file
,
1288 GKeyFileKeyValuePair
*pair
;
1290 if (!(key_file
->flags
& G_KEY_FILE_KEEP_COMMENTS
))
1293 g_warn_if_fail (key_file
->current_group
!= NULL
);
1295 pair
= g_slice_new (GKeyFileKeyValuePair
);
1297 pair
->value
= g_strndup (line
, length
);
1299 key_file
->current_group
->key_value_pairs
=
1300 g_list_prepend (key_file
->current_group
->key_value_pairs
, pair
);
1304 g_key_file_parse_group (GKeyFile
*key_file
,
1310 const gchar
*group_name_start
, *group_name_end
;
1312 /* advance past opening '['
1314 group_name_start
= line
+ 1;
1315 group_name_end
= line
+ length
- 1;
1317 while (*group_name_end
!= ']')
1320 group_name
= g_strndup (group_name_start
,
1321 group_name_end
- group_name_start
);
1323 if (!g_key_file_is_group_name (group_name
))
1325 g_set_error (error
, G_KEY_FILE_ERROR
,
1326 G_KEY_FILE_ERROR_PARSE
,
1327 _("Invalid group name: %s"), group_name
);
1328 g_free (group_name
);
1332 g_key_file_add_group (key_file
, group_name
);
1333 g_free (group_name
);
1337 g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
1342 gchar
*key
, *value
, *key_end
, *value_start
, *locale
;
1343 gsize key_len
, value_len
;
1345 if (key_file
->current_group
== NULL
|| key_file
->current_group
->name
== NULL
)
1347 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
1348 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1349 _("Key file does not start with a group"));
1353 key_end
= value_start
= strchr (line
, '=');
1355 g_warn_if_fail (key_end
!= NULL
);
1360 /* Pull the key name from the line (chomping trailing whitespace)
1362 while (g_ascii_isspace (*key_end
))
1365 key_len
= key_end
- line
+ 2;
1367 g_warn_if_fail (key_len
<= length
);
1369 key
= g_strndup (line
, key_len
- 1);
1371 if (!g_key_file_is_key_name (key
))
1373 g_set_error (error
, G_KEY_FILE_ERROR
,
1374 G_KEY_FILE_ERROR_PARSE
,
1375 _("Invalid key name: %s"), key
);
1380 /* Pull the value from the line (chugging leading whitespace)
1382 while (g_ascii_isspace (*value_start
))
1385 value_len
= line
+ length
- value_start
+ 1;
1387 value
= g_strndup (value_start
, value_len
);
1389 g_warn_if_fail (key_file
->start_group
!= NULL
);
1391 if (key_file
->current_group
1392 && key_file
->current_group
->name
1393 && strcmp (key_file
->start_group
->name
,
1394 key_file
->current_group
->name
) == 0
1395 && strcmp (key
, "Encoding") == 0)
1397 if (g_ascii_strcasecmp (value
, "UTF-8") != 0)
1399 gchar
*value_utf8
= g_utf8_make_valid (value
, value_len
);
1400 g_set_error (error
, G_KEY_FILE_ERROR
,
1401 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1402 _("Key file contains unsupported "
1403 "encoding “%s”"), value_utf8
);
1404 g_free (value_utf8
);
1412 /* Is this key a translation? If so, is it one that we care about?
1414 locale
= key_get_locale (key
);
1416 if (locale
== NULL
|| g_key_file_locale_is_interesting (key_file
, locale
))
1418 GKeyFileKeyValuePair
*pair
;
1420 pair
= g_slice_new (GKeyFileKeyValuePair
);
1422 pair
->value
= value
;
1424 g_key_file_add_key_value_pair (key_file
, key_file
->current_group
, pair
);
1436 key_get_locale (const gchar
*key
)
1440 locale
= g_strrstr (key
, "[");
1442 if (locale
&& strlen (locale
) <= 2)
1446 locale
= g_strndup (locale
+ 1, strlen (locale
) - 2);
1452 g_key_file_parse_data (GKeyFile
*key_file
,
1457 GError
*parse_error
;
1460 g_return_if_fail (key_file
!= NULL
);
1461 g_return_if_fail (data
!= NULL
|| length
== 0);
1468 if (data
[i
] == '\n')
1470 if (key_file
->parse_buffer
->len
> 0
1471 && (key_file
->parse_buffer
->str
[key_file
->parse_buffer
->len
- 1]
1473 g_string_erase (key_file
->parse_buffer
,
1474 key_file
->parse_buffer
->len
- 1,
1477 /* When a newline is encountered flush the parse buffer so that the
1478 * line can be parsed. Note that completely blank lines won't show
1479 * up in the parse buffer, so they get parsed directly.
1481 if (key_file
->parse_buffer
->len
> 0)
1482 g_key_file_flush_parse_buffer (key_file
, &parse_error
);
1484 g_key_file_parse_comment (key_file
, "", 1, &parse_error
);
1488 g_propagate_error (error
, parse_error
);
1495 const gchar
*start_of_line
;
1496 const gchar
*end_of_line
;
1499 start_of_line
= data
+ i
;
1500 end_of_line
= memchr (start_of_line
, '\n', length
- i
);
1502 if (end_of_line
== NULL
)
1503 end_of_line
= data
+ length
;
1505 line_length
= end_of_line
- start_of_line
;
1507 g_string_append_len (key_file
->parse_buffer
, start_of_line
, line_length
);
1514 g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
1517 GError
*file_error
= NULL
;
1519 g_return_if_fail (key_file
!= NULL
);
1523 if (key_file
->parse_buffer
->len
> 0)
1525 g_key_file_parse_line (key_file
, key_file
->parse_buffer
->str
,
1526 key_file
->parse_buffer
->len
,
1528 g_string_erase (key_file
->parse_buffer
, 0, -1);
1532 g_propagate_error (error
, file_error
);
1539 * g_key_file_to_data:
1540 * @key_file: a #GKeyFile
1541 * @length: (out) (optional): return location for the length of the
1542 * returned string, or %NULL
1543 * @error: return location for a #GError, or %NULL
1545 * This function outputs @key_file as a string.
1547 * Note that this function never reports an error,
1548 * so it is safe to pass %NULL as @error.
1550 * Returns: a newly allocated string holding
1551 * the contents of the #GKeyFile
1556 g_key_file_to_data (GKeyFile
*key_file
,
1560 GString
*data_string
;
1561 GList
*group_node
, *key_file_node
;
1563 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1565 data_string
= g_string_new (NULL
);
1567 for (group_node
= g_list_last (key_file
->groups
);
1569 group_node
= group_node
->prev
)
1571 GKeyFileGroup
*group
;
1573 group
= (GKeyFileGroup
*) group_node
->data
;
1575 /* separate groups by at least an empty line */
1576 if (data_string
->len
>= 2 &&
1577 data_string
->str
[data_string
->len
- 2] != '\n')
1578 g_string_append_c (data_string
, '\n');
1580 if (group
->comment
!= NULL
)
1581 g_string_append_printf (data_string
, "%s\n", group
->comment
->value
);
1583 if (group
->name
!= NULL
)
1584 g_string_append_printf (data_string
, "[%s]\n", group
->name
);
1586 for (key_file_node
= g_list_last (group
->key_value_pairs
);
1587 key_file_node
!= NULL
;
1588 key_file_node
= key_file_node
->prev
)
1590 GKeyFileKeyValuePair
*pair
;
1592 pair
= (GKeyFileKeyValuePair
*) key_file_node
->data
;
1594 if (pair
->key
!= NULL
)
1595 g_string_append_printf (data_string
, "%s=%s\n", pair
->key
, pair
->value
);
1597 g_string_append_printf (data_string
, "%s\n", pair
->value
);
1602 *length
= data_string
->len
;
1604 return g_string_free (data_string
, FALSE
);
1608 * g_key_file_get_keys:
1609 * @key_file: a #GKeyFile
1610 * @group_name: a group name
1611 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1612 * @error: return location for a #GError, or %NULL
1614 * Returns all keys for the group name @group_name. The array of
1615 * returned keys will be %NULL-terminated, so @length may
1616 * optionally be %NULL. In the event that the @group_name cannot
1617 * be found, %NULL is returned and @error is set to
1618 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1620 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1621 * Use g_strfreev() to free it.
1626 g_key_file_get_keys (GKeyFile
*key_file
,
1627 const gchar
*group_name
,
1631 GKeyFileGroup
*group
;
1636 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1637 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1639 group
= g_key_file_lookup_group (key_file
, group_name
);
1643 g_set_error (error
, G_KEY_FILE_ERROR
,
1644 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1645 _("Key file does not have group “%s”"),
1651 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1653 GKeyFileKeyValuePair
*pair
;
1655 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1661 keys
= g_new (gchar
*, num_keys
+ 1);
1664 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1666 GKeyFileKeyValuePair
*pair
;
1668 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1672 keys
[i
] = g_strdup (pair
->key
);
1677 keys
[num_keys
] = NULL
;
1686 * g_key_file_get_start_group:
1687 * @key_file: a #GKeyFile
1689 * Returns the name of the start group of the file.
1691 * Returns: The start group of the key file.
1696 g_key_file_get_start_group (GKeyFile
*key_file
)
1698 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1700 if (key_file
->start_group
)
1701 return g_strdup (key_file
->start_group
->name
);
1707 * g_key_file_get_groups:
1708 * @key_file: a #GKeyFile
1709 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1711 * Returns all groups in the key file loaded with @key_file.
1712 * The array of returned groups will be %NULL-terminated, so
1713 * @length may optionally be %NULL.
1715 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1716 * Use g_strfreev() to free it.
1720 g_key_file_get_groups (GKeyFile
*key_file
,
1725 gsize i
, num_groups
;
1727 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1729 num_groups
= g_list_length (key_file
->groups
);
1731 g_return_val_if_fail (num_groups
> 0, NULL
);
1733 group_node
= g_list_last (key_file
->groups
);
1735 g_return_val_if_fail (((GKeyFileGroup
*) group_node
->data
)->name
== NULL
, NULL
);
1737 /* Only need num_groups instead of num_groups + 1
1738 * because the first group of the file (last in the
1739 * list) is always the comment group at the top,
1742 groups
= g_new (gchar
*, num_groups
);
1746 for (group_node
= group_node
->prev
;
1748 group_node
= group_node
->prev
)
1750 GKeyFileGroup
*group
;
1752 group
= (GKeyFileGroup
*) group_node
->data
;
1754 g_warn_if_fail (group
->name
!= NULL
);
1756 groups
[i
++] = g_strdup (group
->name
);
1767 set_not_found_key_error (const char *group_name
,
1771 g_set_error (error
, G_KEY_FILE_ERROR
,
1772 G_KEY_FILE_ERROR_KEY_NOT_FOUND
,
1773 _("Key file does not have key “%s” in group “%s”"),
1778 * g_key_file_get_value:
1779 * @key_file: a #GKeyFile
1780 * @group_name: a group name
1782 * @error: return location for a #GError, or %NULL
1784 * Returns the raw value associated with @key under @group_name.
1785 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1787 * In the event the key cannot be found, %NULL is returned and
1788 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1789 * event that the @group_name cannot be found, %NULL is returned
1790 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1793 * Returns: a newly allocated string or %NULL if the specified
1794 * key cannot be found.
1799 g_key_file_get_value (GKeyFile
*key_file
,
1800 const gchar
*group_name
,
1804 GKeyFileGroup
*group
;
1805 GKeyFileKeyValuePair
*pair
;
1806 gchar
*value
= NULL
;
1808 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1809 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1810 g_return_val_if_fail (key
!= NULL
, NULL
);
1812 group
= g_key_file_lookup_group (key_file
, group_name
);
1816 g_set_error (error
, G_KEY_FILE_ERROR
,
1817 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1818 _("Key file does not have group “%s”"),
1823 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1826 value
= g_strdup (pair
->value
);
1828 set_not_found_key_error (group_name
, key
, error
);
1834 * g_key_file_set_value:
1835 * @key_file: a #GKeyFile
1836 * @group_name: a group name
1840 * Associates a new value with @key under @group_name.
1842 * If @key cannot be found then it is created. If @group_name cannot
1843 * be found then it is created. To set an UTF-8 string which may contain
1844 * characters that need escaping (such as newlines or spaces), use
1845 * g_key_file_set_string().
1850 g_key_file_set_value (GKeyFile
*key_file
,
1851 const gchar
*group_name
,
1855 GKeyFileGroup
*group
;
1856 GKeyFileKeyValuePair
*pair
;
1858 g_return_if_fail (key_file
!= NULL
);
1859 g_return_if_fail (g_key_file_is_group_name (group_name
));
1860 g_return_if_fail (g_key_file_is_key_name (key
));
1861 g_return_if_fail (value
!= NULL
);
1863 group
= g_key_file_lookup_group (key_file
, group_name
);
1867 g_key_file_add_group (key_file
, group_name
);
1868 group
= (GKeyFileGroup
*) key_file
->groups
->data
;
1870 g_key_file_add_key (key_file
, group
, key
, value
);
1874 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1877 g_key_file_add_key (key_file
, group
, key
, value
);
1880 g_free (pair
->value
);
1881 pair
->value
= g_strdup (value
);
1887 * g_key_file_get_string:
1888 * @key_file: a #GKeyFile
1889 * @group_name: a group name
1891 * @error: return location for a #GError, or %NULL
1893 * Returns the string value associated with @key under @group_name.
1894 * Unlike g_key_file_get_value(), this function handles escape sequences
1897 * In the event the key cannot be found, %NULL is returned and
1898 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1899 * event that the @group_name cannot be found, %NULL is returned
1900 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1902 * Returns: a newly allocated string or %NULL if the specified
1903 * key cannot be found.
1908 g_key_file_get_string (GKeyFile
*key_file
,
1909 const gchar
*group_name
,
1913 gchar
*value
, *string_value
;
1914 GError
*key_file_error
;
1916 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1917 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1918 g_return_val_if_fail (key
!= NULL
, NULL
);
1920 key_file_error
= NULL
;
1922 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1926 g_propagate_error (error
, key_file_error
);
1930 if (!g_utf8_validate (value
, -1, NULL
))
1932 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
1933 g_set_error (error
, G_KEY_FILE_ERROR
,
1934 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1935 _("Key file contains key “%s” with value “%s” "
1936 "which is not UTF-8"), key
, value_utf8
);
1937 g_free (value_utf8
);
1943 string_value
= g_key_file_parse_value_as_string (key_file
, value
, NULL
,
1949 if (g_error_matches (key_file_error
,
1951 G_KEY_FILE_ERROR_INVALID_VALUE
))
1953 g_set_error (error
, G_KEY_FILE_ERROR
,
1954 G_KEY_FILE_ERROR_INVALID_VALUE
,
1955 _("Key file contains key “%s” "
1956 "which has a value that cannot be interpreted."),
1958 g_error_free (key_file_error
);
1961 g_propagate_error (error
, key_file_error
);
1964 return string_value
;
1968 * g_key_file_set_string:
1969 * @key_file: a #GKeyFile
1970 * @group_name: a group name
1974 * Associates a new string value with @key under @group_name.
1975 * If @key cannot be found then it is created.
1976 * If @group_name cannot be found then it is created.
1977 * Unlike g_key_file_set_value(), this function handles characters
1978 * that need escaping, such as newlines.
1983 g_key_file_set_string (GKeyFile
*key_file
,
1984 const gchar
*group_name
,
1986 const gchar
*string
)
1990 g_return_if_fail (key_file
!= NULL
);
1991 g_return_if_fail (string
!= NULL
);
1993 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
1994 g_key_file_set_value (key_file
, group_name
, key
, value
);
1999 * g_key_file_get_string_list:
2000 * @key_file: a #GKeyFile
2001 * @group_name: a group name
2003 * @length: (out) (optional): return location for the number of returned strings, or %NULL
2004 * @error: return location for a #GError, or %NULL
2006 * Returns the values associated with @key under @group_name.
2008 * In the event the key cannot be found, %NULL is returned and
2009 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
2010 * event that the @group_name cannot be found, %NULL is returned
2011 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
2013 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2014 * a %NULL-terminated string array or %NULL if the specified
2015 * key cannot be found. The array should be freed with g_strfreev().
2020 g_key_file_get_string_list (GKeyFile
*key_file
,
2021 const gchar
*group_name
,
2026 GError
*key_file_error
= NULL
;
2027 gchar
*value
, *string_value
, **values
;
2029 GSList
*p
, *pieces
= NULL
;
2031 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2032 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2033 g_return_val_if_fail (key
!= NULL
, NULL
);
2038 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2042 g_propagate_error (error
, key_file_error
);
2046 if (!g_utf8_validate (value
, -1, NULL
))
2048 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
2049 g_set_error (error
, G_KEY_FILE_ERROR
,
2050 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
2051 _("Key file contains key “%s” with value “%s” "
2052 "which is not UTF-8"), key
, value_utf8
);
2053 g_free (value_utf8
);
2059 string_value
= g_key_file_parse_value_as_string (key_file
, value
, &pieces
, &key_file_error
);
2061 g_free (string_value
);
2065 if (g_error_matches (key_file_error
,
2067 G_KEY_FILE_ERROR_INVALID_VALUE
))
2069 g_set_error (error
, G_KEY_FILE_ERROR
,
2070 G_KEY_FILE_ERROR_INVALID_VALUE
,
2071 _("Key file contains key “%s” "
2072 "which has a value that cannot be interpreted."),
2074 g_error_free (key_file_error
);
2077 g_propagate_error (error
, key_file_error
);
2079 g_slist_free_full (pieces
, g_free
);
2083 len
= g_slist_length (pieces
);
2084 values
= g_new (gchar
*, len
+ 1);
2085 for (p
= pieces
, i
= 0; p
; p
= p
->next
)
2086 values
[i
++] = p
->data
;
2089 g_slist_free (pieces
);
2098 * g_key_file_set_string_list:
2099 * @key_file: a #GKeyFile
2100 * @group_name: a group name
2102 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2103 * @length: number of string values in @list
2105 * Associates a list of string values for @key under @group_name.
2106 * If @key cannot be found then it is created.
2107 * If @group_name cannot be found then it is created.
2112 g_key_file_set_string_list (GKeyFile
*key_file
,
2113 const gchar
*group_name
,
2115 const gchar
* const list
[],
2118 GString
*value_list
;
2121 g_return_if_fail (key_file
!= NULL
);
2122 g_return_if_fail (list
!= NULL
|| length
== 0);
2124 value_list
= g_string_sized_new (length
* 128);
2125 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
2129 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
2130 g_string_append (value_list
, value
);
2131 g_string_append_c (value_list
, key_file
->list_separator
);
2136 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
2137 g_string_free (value_list
, TRUE
);
2141 * g_key_file_set_locale_string:
2142 * @key_file: a #GKeyFile
2143 * @group_name: a group name
2145 * @locale: a locale identifier
2148 * Associates a string value for @key and @locale under @group_name.
2149 * If the translation for @key cannot be found then it is created.
2154 g_key_file_set_locale_string (GKeyFile
*key_file
,
2155 const gchar
*group_name
,
2157 const gchar
*locale
,
2158 const gchar
*string
)
2160 gchar
*full_key
, *value
;
2162 g_return_if_fail (key_file
!= NULL
);
2163 g_return_if_fail (key
!= NULL
);
2164 g_return_if_fail (locale
!= NULL
);
2165 g_return_if_fail (string
!= NULL
);
2167 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
2168 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
2169 g_key_file_set_value (key_file
, group_name
, full_key
, value
);
2175 * g_key_file_get_locale_string:
2176 * @key_file: a #GKeyFile
2177 * @group_name: a group name
2179 * @locale: (nullable): a locale identifier or %NULL
2180 * @error: return location for a #GError, or %NULL
2182 * Returns the value associated with @key under @group_name
2183 * translated in the given @locale if available. If @locale is
2184 * %NULL then the current locale is assumed.
2186 * If @locale is to be non-%NULL, or if the current locale will change over
2187 * the lifetime of the #GKeyFile, it must be loaded with
2188 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2190 * If @key cannot be found then %NULL is returned and @error is set
2191 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2192 * with @key cannot be interpreted or no suitable translation can
2193 * be found then the untranslated value is returned.
2195 * Returns: a newly allocated string or %NULL if the specified
2196 * key cannot be found.
2201 g_key_file_get_locale_string (GKeyFile
*key_file
,
2202 const gchar
*group_name
,
2204 const gchar
*locale
,
2207 gchar
*candidate_key
, *translated_value
;
2208 GError
*key_file_error
;
2210 gboolean free_languages
= FALSE
;
2213 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2214 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2215 g_return_val_if_fail (key
!= NULL
, NULL
);
2217 candidate_key
= NULL
;
2218 translated_value
= NULL
;
2219 key_file_error
= NULL
;
2223 languages
= g_get_locale_variants (locale
);
2224 free_languages
= TRUE
;
2228 languages
= (gchar
**) g_get_language_names ();
2229 free_languages
= FALSE
;
2232 for (i
= 0; languages
[i
]; i
++)
2234 candidate_key
= g_strdup_printf ("%s[%s]", key
, languages
[i
]);
2236 translated_value
= g_key_file_get_string (key_file
,
2238 candidate_key
, NULL
);
2239 g_free (candidate_key
);
2241 if (translated_value
)
2244 g_free (translated_value
);
2245 translated_value
= NULL
;
2248 /* Fallback to untranslated key
2250 if (!translated_value
)
2252 translated_value
= g_key_file_get_string (key_file
, group_name
, key
,
2255 if (!translated_value
)
2256 g_propagate_error (error
, key_file_error
);
2260 g_strfreev (languages
);
2262 return translated_value
;
2266 * g_key_file_get_locale_for_key:
2267 * @key_file: a #GKeyFile
2268 * @group_name: a group name
2270 * @locale: (nullable): a locale identifier or %NULL
2272 * Returns the actual locale which the result of
2273 * g_key_file_get_locale_string() or g_key_file_get_locale_string_list()
2276 * If calling g_key_file_get_locale_string() or
2277 * g_key_file_get_locale_string_list() with exactly the same @key_file,
2278 * @group_name, @key and @locale, the result of those functions will
2279 * have originally been tagged with the locale that is the result of
2282 * Returns: (nullable): the locale from the file, or %NULL if the key was not
2283 * found or the entry in the file was was untranslated
2288 g_key_file_get_locale_for_key (GKeyFile
*key_file
,
2289 const gchar
*group_name
,
2291 const gchar
*locale
)
2293 gchar
**languages_allocated
= NULL
;
2294 const gchar
* const *languages
;
2295 gchar
*result
= NULL
;
2298 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2299 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2300 g_return_val_if_fail (key
!= NULL
, NULL
);
2304 languages_allocated
= g_get_locale_variants (locale
);
2305 languages
= (const gchar
* const *) languages_allocated
;
2308 languages
= g_get_language_names ();
2310 for (i
= 0; languages
[i
] != NULL
; i
++)
2312 gchar
*candidate_key
, *translated_value
;
2314 candidate_key
= g_strdup_printf ("%s[%s]", key
, languages
[i
]);
2315 translated_value
= g_key_file_get_string (key_file
, group_name
, candidate_key
, NULL
);
2316 g_free (translated_value
);
2317 g_free (candidate_key
);
2319 if (translated_value
!= NULL
)
2323 result
= g_strdup (languages
[i
]);
2325 g_strfreev (languages_allocated
);
2331 * g_key_file_get_locale_string_list:
2332 * @key_file: a #GKeyFile
2333 * @group_name: a group name
2335 * @locale: (nullable): a locale identifier or %NULL
2336 * @length: (out) (optional): return location for the number of returned strings or %NULL
2337 * @error: return location for a #GError or %NULL
2339 * Returns the values associated with @key under @group_name
2340 * translated in the given @locale if available. If @locale is
2341 * %NULL then the current locale is assumed.
2343 * If @locale is to be non-%NULL, or if the current locale will change over
2344 * the lifetime of the #GKeyFile, it must be loaded with
2345 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2347 * If @key cannot be found then %NULL is returned and @error is set
2348 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2349 * with @key cannot be interpreted or no suitable translations
2350 * can be found then the untranslated values are returned. The
2351 * returned array is %NULL-terminated, so @length may optionally
2354 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2355 * or %NULL if the key isn't found. The string array should be freed
2356 * with g_strfreev().
2361 g_key_file_get_locale_string_list (GKeyFile
*key_file
,
2362 const gchar
*group_name
,
2364 const gchar
*locale
,
2368 GError
*key_file_error
;
2369 gchar
**values
, *value
;
2370 char list_separator
[2];
2373 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2374 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2375 g_return_val_if_fail (key
!= NULL
, NULL
);
2377 key_file_error
= NULL
;
2379 value
= g_key_file_get_locale_string (key_file
, group_name
,
2384 g_propagate_error (error
, key_file_error
);
2393 len
= strlen (value
);
2394 if (value
[len
- 1] == key_file
->list_separator
)
2395 value
[len
- 1] = '\0';
2397 list_separator
[0] = key_file
->list_separator
;
2398 list_separator
[1] = '\0';
2399 values
= g_strsplit (value
, list_separator
, 0);
2404 *length
= g_strv_length (values
);
2410 * g_key_file_set_locale_string_list:
2411 * @key_file: a #GKeyFile
2412 * @group_name: a group name
2414 * @locale: a locale identifier
2415 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2416 * @length: the length of @list
2418 * Associates a list of string values for @key and @locale under
2419 * @group_name. If the translation for @key cannot be found then
2425 g_key_file_set_locale_string_list (GKeyFile
*key_file
,
2426 const gchar
*group_name
,
2428 const gchar
*locale
,
2429 const gchar
* const list
[],
2432 GString
*value_list
;
2436 g_return_if_fail (key_file
!= NULL
);
2437 g_return_if_fail (key
!= NULL
);
2438 g_return_if_fail (locale
!= NULL
);
2439 g_return_if_fail (length
!= 0);
2441 value_list
= g_string_sized_new (length
* 128);
2442 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
2446 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
2447 g_string_append (value_list
, value
);
2448 g_string_append_c (value_list
, key_file
->list_separator
);
2453 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
2454 g_key_file_set_value (key_file
, group_name
, full_key
, value_list
->str
);
2456 g_string_free (value_list
, TRUE
);
2460 * g_key_file_get_boolean:
2461 * @key_file: a #GKeyFile
2462 * @group_name: a group name
2464 * @error: return location for a #GError
2466 * Returns the value associated with @key under @group_name as a
2469 * If @key cannot be found then %FALSE is returned and @error is set
2470 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2471 * associated with @key cannot be interpreted as a boolean then %FALSE
2472 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2474 * Returns: the value associated with the key as a boolean,
2475 * or %FALSE if the key was not found or could not be parsed.
2480 g_key_file_get_boolean (GKeyFile
*key_file
,
2481 const gchar
*group_name
,
2485 GError
*key_file_error
= NULL
;
2487 gboolean bool_value
;
2489 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
2490 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
2491 g_return_val_if_fail (key
!= NULL
, FALSE
);
2493 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2497 g_propagate_error (error
, key_file_error
);
2501 bool_value
= g_key_file_parse_value_as_boolean (key_file
, value
,
2507 if (g_error_matches (key_file_error
,
2509 G_KEY_FILE_ERROR_INVALID_VALUE
))
2511 g_set_error (error
, G_KEY_FILE_ERROR
,
2512 G_KEY_FILE_ERROR_INVALID_VALUE
,
2513 _("Key file contains key “%s” "
2514 "which has a value that cannot be interpreted."),
2516 g_error_free (key_file_error
);
2519 g_propagate_error (error
, key_file_error
);
2526 * g_key_file_set_boolean:
2527 * @key_file: a #GKeyFile
2528 * @group_name: a group name
2530 * @value: %TRUE or %FALSE
2532 * Associates a new boolean value with @key under @group_name.
2533 * If @key cannot be found then it is created.
2538 g_key_file_set_boolean (GKeyFile
*key_file
,
2539 const gchar
*group_name
,
2545 g_return_if_fail (key_file
!= NULL
);
2547 result
= g_key_file_parse_boolean_as_value (key_file
, value
);
2548 g_key_file_set_value (key_file
, group_name
, key
, result
);
2553 * g_key_file_get_boolean_list:
2554 * @key_file: a #GKeyFile
2555 * @group_name: a group name
2557 * @length: (out): the number of booleans returned
2558 * @error: return location for a #GError
2560 * Returns the values associated with @key under @group_name as
2563 * If @key cannot be found then %NULL is returned and @error is set to
2564 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2565 * with @key cannot be interpreted as booleans then %NULL is returned
2566 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2568 * Returns: (array length=length) (element-type gboolean) (transfer container):
2569 * the values associated with the key as a list of booleans, or %NULL if the
2570 * key was not found or could not be parsed. The returned list of booleans
2571 * should be freed with g_free() when no longer needed.
2576 g_key_file_get_boolean_list (GKeyFile
*key_file
,
2577 const gchar
*group_name
,
2582 GError
*key_file_error
;
2584 gboolean
*bool_values
;
2587 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2588 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2589 g_return_val_if_fail (key
!= NULL
, NULL
);
2594 key_file_error
= NULL
;
2596 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2597 &num_bools
, &key_file_error
);
2600 g_propagate_error (error
, key_file_error
);
2605 bool_values
= g_new (gboolean
, num_bools
);
2607 for (i
= 0; i
< num_bools
; i
++)
2609 bool_values
[i
] = g_key_file_parse_value_as_boolean (key_file
,
2615 g_propagate_error (error
, key_file_error
);
2616 g_strfreev (values
);
2617 g_free (bool_values
);
2622 g_strfreev (values
);
2625 *length
= num_bools
;
2631 * g_key_file_set_boolean_list:
2632 * @key_file: a #GKeyFile
2633 * @group_name: a group name
2635 * @list: (array length=length): an array of boolean values
2636 * @length: length of @list
2638 * Associates a list of boolean values with @key under @group_name.
2639 * If @key cannot be found then it is created.
2640 * If @group_name is %NULL, the start_group is used.
2645 g_key_file_set_boolean_list (GKeyFile
*key_file
,
2646 const gchar
*group_name
,
2651 GString
*value_list
;
2654 g_return_if_fail (key_file
!= NULL
);
2655 g_return_if_fail (list
!= NULL
);
2657 value_list
= g_string_sized_new (length
* 8);
2658 for (i
= 0; i
< length
; i
++)
2662 value
= g_key_file_parse_boolean_as_value (key_file
, list
[i
]);
2664 g_string_append (value_list
, value
);
2665 g_string_append_c (value_list
, key_file
->list_separator
);
2670 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
2671 g_string_free (value_list
, TRUE
);
2675 * g_key_file_get_integer:
2676 * @key_file: a #GKeyFile
2677 * @group_name: a group name
2679 * @error: return location for a #GError
2681 * Returns the value associated with @key under @group_name as an
2684 * If @key cannot be found then 0 is returned and @error is set to
2685 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2686 * with @key cannot be interpreted as an integer, or is out of range
2687 * for a #gint, then 0 is returned
2688 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2690 * Returns: the value associated with the key as an integer, or
2691 * 0 if the key was not found or could not be parsed.
2696 g_key_file_get_integer (GKeyFile
*key_file
,
2697 const gchar
*group_name
,
2701 GError
*key_file_error
;
2705 g_return_val_if_fail (key_file
!= NULL
, -1);
2706 g_return_val_if_fail (group_name
!= NULL
, -1);
2707 g_return_val_if_fail (key
!= NULL
, -1);
2709 key_file_error
= NULL
;
2711 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2715 g_propagate_error (error
, key_file_error
);
2719 int_value
= g_key_file_parse_value_as_integer (key_file
, value
,
2725 if (g_error_matches (key_file_error
,
2727 G_KEY_FILE_ERROR_INVALID_VALUE
))
2729 g_set_error (error
, G_KEY_FILE_ERROR
,
2730 G_KEY_FILE_ERROR_INVALID_VALUE
,
2731 _("Key file contains key “%s” in group “%s” "
2732 "which has a value that cannot be interpreted."),
2734 g_error_free (key_file_error
);
2737 g_propagate_error (error
, key_file_error
);
2744 * g_key_file_set_integer:
2745 * @key_file: a #GKeyFile
2746 * @group_name: a group name
2748 * @value: an integer value
2750 * Associates a new integer value with @key under @group_name.
2751 * If @key cannot be found then it is created.
2756 g_key_file_set_integer (GKeyFile
*key_file
,
2757 const gchar
*group_name
,
2763 g_return_if_fail (key_file
!= NULL
);
2765 result
= g_key_file_parse_integer_as_value (key_file
, value
);
2766 g_key_file_set_value (key_file
, group_name
, key
, result
);
2771 * g_key_file_get_int64:
2772 * @key_file: a non-%NULL #GKeyFile
2773 * @group_name: a non-%NULL group name
2774 * @key: a non-%NULL key
2775 * @error: return location for a #GError
2777 * Returns the value associated with @key under @group_name as a signed
2778 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2779 * 64-bit results without truncation.
2781 * Returns: the value associated with the key as a signed 64-bit integer, or
2782 * 0 if the key was not found or could not be parsed.
2787 g_key_file_get_int64 (GKeyFile
*key_file
,
2788 const gchar
*group_name
,
2795 g_return_val_if_fail (key_file
!= NULL
, -1);
2796 g_return_val_if_fail (group_name
!= NULL
, -1);
2797 g_return_val_if_fail (key
!= NULL
, -1);
2799 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2804 v
= g_ascii_strtoll (s
, &end
, 10);
2806 if (*s
== '\0' || *end
!= '\0')
2808 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2809 _("Key “%s” in group “%s” has value “%s” "
2810 "where %s was expected"),
2811 key
, group_name
, s
, "int64");
2821 * g_key_file_set_int64:
2822 * @key_file: a #GKeyFile
2823 * @group_name: a group name
2825 * @value: an integer value
2827 * Associates a new integer value with @key under @group_name.
2828 * If @key cannot be found then it is created.
2833 g_key_file_set_int64 (GKeyFile
*key_file
,
2834 const gchar
*group_name
,
2840 g_return_if_fail (key_file
!= NULL
);
2842 result
= g_strdup_printf ("%" G_GINT64_FORMAT
, value
);
2843 g_key_file_set_value (key_file
, group_name
, key
, result
);
2848 * g_key_file_get_uint64:
2849 * @key_file: a non-%NULL #GKeyFile
2850 * @group_name: a non-%NULL group name
2851 * @key: a non-%NULL key
2852 * @error: return location for a #GError
2854 * Returns the value associated with @key under @group_name as an unsigned
2855 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2856 * large positive results without truncation.
2858 * Returns: the value associated with the key as an unsigned 64-bit integer,
2859 * or 0 if the key was not found or could not be parsed.
2864 g_key_file_get_uint64 (GKeyFile
*key_file
,
2865 const gchar
*group_name
,
2872 g_return_val_if_fail (key_file
!= NULL
, -1);
2873 g_return_val_if_fail (group_name
!= NULL
, -1);
2874 g_return_val_if_fail (key
!= NULL
, -1);
2876 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2881 v
= g_ascii_strtoull (s
, &end
, 10);
2883 if (*s
== '\0' || *end
!= '\0')
2885 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2886 _("Key “%s” in group “%s” has value “%s” "
2887 "where %s was expected"),
2888 key
, group_name
, s
, "uint64");
2898 * g_key_file_set_uint64:
2899 * @key_file: a #GKeyFile
2900 * @group_name: a group name
2902 * @value: an integer value
2904 * Associates a new integer value with @key under @group_name.
2905 * If @key cannot be found then it is created.
2910 g_key_file_set_uint64 (GKeyFile
*key_file
,
2911 const gchar
*group_name
,
2917 g_return_if_fail (key_file
!= NULL
);
2919 result
= g_strdup_printf ("%" G_GUINT64_FORMAT
, value
);
2920 g_key_file_set_value (key_file
, group_name
, key
, result
);
2925 * g_key_file_get_integer_list:
2926 * @key_file: a #GKeyFile
2927 * @group_name: a group name
2929 * @length: (out): the number of integers returned
2930 * @error: return location for a #GError
2932 * Returns the values associated with @key under @group_name as
2935 * If @key cannot be found then %NULL is returned and @error is set to
2936 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2937 * with @key cannot be interpreted as integers, or are out of range for
2938 * #gint, then %NULL is returned
2939 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2941 * Returns: (array length=length) (element-type gint) (transfer container):
2942 * the values associated with the key as a list of integers, or %NULL if
2943 * the key was not found or could not be parsed. The returned list of
2944 * integers should be freed with g_free() when no longer needed.
2949 g_key_file_get_integer_list (GKeyFile
*key_file
,
2950 const gchar
*group_name
,
2955 GError
*key_file_error
= NULL
;
2960 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2961 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2962 g_return_val_if_fail (key
!= NULL
, NULL
);
2967 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2968 &num_ints
, &key_file_error
);
2971 g_propagate_error (error
, key_file_error
);
2976 int_values
= g_new (gint
, num_ints
);
2978 for (i
= 0; i
< num_ints
; i
++)
2980 int_values
[i
] = g_key_file_parse_value_as_integer (key_file
,
2986 g_propagate_error (error
, key_file_error
);
2987 g_strfreev (values
);
2988 g_free (int_values
);
2993 g_strfreev (values
);
3002 * g_key_file_set_integer_list:
3003 * @key_file: a #GKeyFile
3004 * @group_name: a group name
3006 * @list: (array length=length): an array of integer values
3007 * @length: number of integer values in @list
3009 * Associates a list of integer values with @key under @group_name.
3010 * If @key cannot be found then it is created.
3015 g_key_file_set_integer_list (GKeyFile
*key_file
,
3016 const gchar
*group_name
,
3024 g_return_if_fail (key_file
!= NULL
);
3025 g_return_if_fail (list
!= NULL
);
3027 values
= g_string_sized_new (length
* 16);
3028 for (i
= 0; i
< length
; i
++)
3032 value
= g_key_file_parse_integer_as_value (key_file
, list
[i
]);
3034 g_string_append (values
, value
);
3035 g_string_append_c (values
, key_file
->list_separator
);
3040 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
3041 g_string_free (values
, TRUE
);
3045 * g_key_file_get_double:
3046 * @key_file: a #GKeyFile
3047 * @group_name: a group name
3049 * @error: return location for a #GError
3051 * Returns the value associated with @key under @group_name as a
3052 * double. If @group_name is %NULL, the start_group is used.
3054 * If @key cannot be found then 0.0 is returned and @error is set to
3055 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
3056 * with @key cannot be interpreted as a double then 0.0 is returned
3057 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3059 * Returns: the value associated with the key as a double, or
3060 * 0.0 if the key was not found or could not be parsed.
3065 g_key_file_get_double (GKeyFile
*key_file
,
3066 const gchar
*group_name
,
3070 GError
*key_file_error
;
3072 gdouble double_value
;
3074 g_return_val_if_fail (key_file
!= NULL
, -1);
3075 g_return_val_if_fail (group_name
!= NULL
, -1);
3076 g_return_val_if_fail (key
!= NULL
, -1);
3078 key_file_error
= NULL
;
3080 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
3084 g_propagate_error (error
, key_file_error
);
3088 double_value
= g_key_file_parse_value_as_double (key_file
, value
,
3094 if (g_error_matches (key_file_error
,
3096 G_KEY_FILE_ERROR_INVALID_VALUE
))
3098 g_set_error (error
, G_KEY_FILE_ERROR
,
3099 G_KEY_FILE_ERROR_INVALID_VALUE
,
3100 _("Key file contains key “%s” in group “%s” "
3101 "which has a value that cannot be interpreted."),
3103 g_error_free (key_file_error
);
3106 g_propagate_error (error
, key_file_error
);
3109 return double_value
;
3113 * g_key_file_set_double:
3114 * @key_file: a #GKeyFile
3115 * @group_name: a group name
3117 * @value: an double value
3119 * Associates a new double value with @key under @group_name.
3120 * If @key cannot be found then it is created.
3125 g_key_file_set_double (GKeyFile
*key_file
,
3126 const gchar
*group_name
,
3130 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
3132 g_return_if_fail (key_file
!= NULL
);
3134 g_ascii_dtostr (result
, sizeof (result
), value
);
3135 g_key_file_set_value (key_file
, group_name
, key
, result
);
3139 * g_key_file_get_double_list:
3140 * @key_file: a #GKeyFile
3141 * @group_name: a group name
3143 * @length: (out): the number of doubles returned
3144 * @error: return location for a #GError
3146 * Returns the values associated with @key under @group_name as
3149 * If @key cannot be found then %NULL is returned and @error is set to
3150 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3151 * with @key cannot be interpreted as doubles then %NULL is returned
3152 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3154 * Returns: (array length=length) (element-type gdouble) (transfer container):
3155 * the values associated with the key as a list of doubles, or %NULL if the
3156 * key was not found or could not be parsed. The returned list of doubles
3157 * should be freed with g_free() when no longer needed.
3162 g_key_file_get_double_list (GKeyFile
*key_file
,
3163 const gchar
*group_name
,
3168 GError
*key_file_error
= NULL
;
3170 gdouble
*double_values
;
3171 gsize i
, num_doubles
;
3173 g_return_val_if_fail (key_file
!= NULL
, NULL
);
3174 g_return_val_if_fail (group_name
!= NULL
, NULL
);
3175 g_return_val_if_fail (key
!= NULL
, NULL
);
3180 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
3181 &num_doubles
, &key_file_error
);
3184 g_propagate_error (error
, key_file_error
);
3189 double_values
= g_new (gdouble
, num_doubles
);
3191 for (i
= 0; i
< num_doubles
; i
++)
3193 double_values
[i
] = g_key_file_parse_value_as_double (key_file
,
3199 g_propagate_error (error
, key_file_error
);
3200 g_strfreev (values
);
3201 g_free (double_values
);
3206 g_strfreev (values
);
3209 *length
= num_doubles
;
3211 return double_values
;
3215 * g_key_file_set_double_list:
3216 * @key_file: a #GKeyFile
3217 * @group_name: a group name
3219 * @list: (array length=length): an array of double values
3220 * @length: number of double values in @list
3222 * Associates a list of double values with @key under
3223 * @group_name. If @key cannot be found then it is created.
3228 g_key_file_set_double_list (GKeyFile
*key_file
,
3229 const gchar
*group_name
,
3237 g_return_if_fail (key_file
!= NULL
);
3238 g_return_if_fail (list
!= NULL
);
3240 values
= g_string_sized_new (length
* 16);
3241 for (i
= 0; i
< length
; i
++)
3243 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
3245 g_ascii_dtostr( result
, sizeof (result
), list
[i
] );
3247 g_string_append (values
, result
);
3248 g_string_append_c (values
, key_file
->list_separator
);
3251 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
3252 g_string_free (values
, TRUE
);
3256 g_key_file_set_key_comment (GKeyFile
*key_file
,
3257 const gchar
*group_name
,
3259 const gchar
*comment
,
3262 GKeyFileGroup
*group
;
3263 GKeyFileKeyValuePair
*pair
;
3264 GList
*key_node
, *comment_node
, *tmp
;
3266 group
= g_key_file_lookup_group (key_file
, group_name
);
3269 g_set_error (error
, G_KEY_FILE_ERROR
,
3270 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3271 _("Key file does not have group “%s”"),
3272 group_name
? group_name
: "(null)");
3277 /* First find the key the comments are supposed to be
3280 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
3282 if (key_node
== NULL
)
3284 set_not_found_key_error (group
->name
, key
, error
);
3288 /* Then find all the comments already associated with the
3291 tmp
= key_node
->next
;
3294 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3296 if (pair
->key
!= NULL
)
3301 g_key_file_remove_key_value_pair_node (key_file
, group
,
3305 if (comment
== NULL
)
3308 /* Now we can add our new comment
3310 pair
= g_slice_new (GKeyFileKeyValuePair
);
3312 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3314 key_node
= g_list_insert (key_node
, pair
, 1);
3320 g_key_file_set_group_comment (GKeyFile
*key_file
,
3321 const gchar
*group_name
,
3322 const gchar
*comment
,
3325 GKeyFileGroup
*group
;
3327 g_return_val_if_fail (g_key_file_is_group_name (group_name
), FALSE
);
3329 group
= g_key_file_lookup_group (key_file
, group_name
);
3332 g_set_error (error
, G_KEY_FILE_ERROR
,
3333 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3334 _("Key file does not have group “%s”"),
3335 group_name
? group_name
: "(null)");
3340 /* First remove any existing comment
3344 g_key_file_key_value_pair_free (group
->comment
);
3345 group
->comment
= NULL
;
3348 if (comment
== NULL
)
3351 /* Now we can add our new comment
3353 group
->comment
= g_slice_new (GKeyFileKeyValuePair
);
3354 group
->comment
->key
= NULL
;
3355 group
->comment
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3361 g_key_file_set_top_comment (GKeyFile
*key_file
,
3362 const gchar
*comment
,
3366 GKeyFileGroup
*group
;
3367 GKeyFileKeyValuePair
*pair
;
3369 /* The last group in the list should be the top (comments only)
3372 g_warn_if_fail (key_file
->groups
!= NULL
);
3373 group_node
= g_list_last (key_file
->groups
);
3374 group
= (GKeyFileGroup
*) group_node
->data
;
3375 g_warn_if_fail (group
->name
== NULL
);
3377 /* Note all keys must be comments at the top of
3378 * the file, so we can just free it all.
3380 g_list_free_full (group
->key_value_pairs
, (GDestroyNotify
) g_key_file_key_value_pair_free
);
3381 group
->key_value_pairs
= NULL
;
3383 if (comment
== NULL
)
3386 pair
= g_slice_new (GKeyFileKeyValuePair
);
3388 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3390 group
->key_value_pairs
=
3391 g_list_prepend (group
->key_value_pairs
, pair
);
3397 * g_key_file_set_comment:
3398 * @key_file: a #GKeyFile
3399 * @group_name: (nullable): a group name, or %NULL
3400 * @key: (nullable): a key
3401 * @comment: a comment
3402 * @error: return location for a #GError
3404 * Places a comment above @key from @group_name.
3406 * If @key is %NULL then @comment will be written above @group_name.
3407 * If both @key and @group_name are %NULL, then @comment will be
3408 * written above the first group in the file.
3410 * Note that this function prepends a '#' comment marker to
3411 * each line of @comment.
3413 * Returns: %TRUE if the comment was written, %FALSE otherwise
3418 g_key_file_set_comment (GKeyFile
*key_file
,
3419 const gchar
*group_name
,
3421 const gchar
*comment
,
3424 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3426 if (group_name
!= NULL
&& key
!= NULL
)
3428 if (!g_key_file_set_key_comment (key_file
, group_name
, key
, comment
, error
))
3431 else if (group_name
!= NULL
)
3433 if (!g_key_file_set_group_comment (key_file
, group_name
, comment
, error
))
3438 if (!g_key_file_set_top_comment (key_file
, comment
, error
))
3446 g_key_file_get_key_comment (GKeyFile
*key_file
,
3447 const gchar
*group_name
,
3451 GKeyFileGroup
*group
;
3452 GKeyFileKeyValuePair
*pair
;
3453 GList
*key_node
, *tmp
;
3457 g_return_val_if_fail (g_key_file_is_group_name (group_name
), NULL
);
3459 group
= g_key_file_lookup_group (key_file
, group_name
);
3462 g_set_error (error
, G_KEY_FILE_ERROR
,
3463 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3464 _("Key file does not have group “%s”"),
3465 group_name
? group_name
: "(null)");
3470 /* First find the key the comments are supposed to be
3473 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
3475 if (key_node
== NULL
)
3477 set_not_found_key_error (group
->name
, key
, error
);
3483 /* Then find all the comments already associated with the
3484 * key and concatentate them.
3486 tmp
= key_node
->next
;
3487 if (!key_node
->next
)
3490 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3491 if (pair
->key
!= NULL
)
3496 pair
= (GKeyFileKeyValuePair
*) tmp
->next
->data
;
3498 if (pair
->key
!= NULL
)
3504 while (tmp
!= key_node
)
3506 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3509 string
= g_string_sized_new (512);
3511 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
3512 g_string_append (string
, comment
);
3520 comment
= string
->str
;
3521 g_string_free (string
, FALSE
);
3530 get_group_comment (GKeyFile
*key_file
,
3531 GKeyFileGroup
*group
,
3540 tmp
= group
->key_value_pairs
;
3543 GKeyFileKeyValuePair
*pair
;
3545 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3547 if (pair
->key
!= NULL
)
3553 if (tmp
->next
== NULL
)
3561 GKeyFileKeyValuePair
*pair
;
3563 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3566 string
= g_string_sized_new (512);
3568 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
3569 g_string_append (string
, comment
);
3576 return g_string_free (string
, FALSE
);
3582 g_key_file_get_group_comment (GKeyFile
*key_file
,
3583 const gchar
*group_name
,
3587 GKeyFileGroup
*group
;
3589 group
= g_key_file_lookup_group (key_file
, group_name
);
3592 g_set_error (error
, G_KEY_FILE_ERROR
,
3593 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3594 _("Key file does not have group “%s”"),
3595 group_name
? group_name
: "(null)");
3601 return g_strdup (group
->comment
->value
);
3603 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3604 group_node
= group_node
->next
;
3605 group
= (GKeyFileGroup
*)group_node
->data
;
3606 return get_group_comment (key_file
, group
, error
);
3610 g_key_file_get_top_comment (GKeyFile
*key_file
,
3614 GKeyFileGroup
*group
;
3616 /* The last group in the list should be the top (comments only)
3619 g_warn_if_fail (key_file
->groups
!= NULL
);
3620 group_node
= g_list_last (key_file
->groups
);
3621 group
= (GKeyFileGroup
*) group_node
->data
;
3622 g_warn_if_fail (group
->name
== NULL
);
3624 return get_group_comment (key_file
, group
, error
);
3628 * g_key_file_get_comment:
3629 * @key_file: a #GKeyFile
3630 * @group_name: (nullable): a group name, or %NULL
3632 * @error: return location for a #GError
3634 * Retrieves a comment above @key from @group_name.
3635 * If @key is %NULL then @comment will be read from above
3636 * @group_name. If both @key and @group_name are %NULL, then
3637 * @comment will be read from above the first group in the file.
3639 * Note that the returned string includes the '#' comment markers.
3641 * Returns: a comment that should be freed with g_free()
3646 g_key_file_get_comment (GKeyFile
*key_file
,
3647 const gchar
*group_name
,
3651 g_return_val_if_fail (key_file
!= NULL
, NULL
);
3653 if (group_name
!= NULL
&& key
!= NULL
)
3654 return g_key_file_get_key_comment (key_file
, group_name
, key
, error
);
3655 else if (group_name
!= NULL
)
3656 return g_key_file_get_group_comment (key_file
, group_name
, error
);
3658 return g_key_file_get_top_comment (key_file
, error
);
3662 * g_key_file_remove_comment:
3663 * @key_file: a #GKeyFile
3664 * @group_name: (nullable): a group name, or %NULL
3665 * @key: (nullable): a key
3666 * @error: return location for a #GError
3668 * Removes a comment above @key from @group_name.
3669 * If @key is %NULL then @comment will be removed above @group_name.
3670 * If both @key and @group_name are %NULL, then @comment will
3671 * be removed above the first group in the file.
3673 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3679 g_key_file_remove_comment (GKeyFile
*key_file
,
3680 const gchar
*group_name
,
3684 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3686 if (group_name
!= NULL
&& key
!= NULL
)
3687 return g_key_file_set_key_comment (key_file
, group_name
, key
, NULL
, error
);
3688 else if (group_name
!= NULL
)
3689 return g_key_file_set_group_comment (key_file
, group_name
, NULL
, error
);
3691 return g_key_file_set_top_comment (key_file
, NULL
, error
);
3695 * g_key_file_has_group:
3696 * @key_file: a #GKeyFile
3697 * @group_name: a group name
3699 * Looks whether the key file has the group @group_name.
3701 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3706 g_key_file_has_group (GKeyFile
*key_file
,
3707 const gchar
*group_name
)
3709 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3710 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3712 return g_key_file_lookup_group (key_file
, group_name
) != NULL
;
3715 /* This code remains from a historical attempt to add a new public API
3716 * which respects the GError rules.
3719 g_key_file_has_key_full (GKeyFile
*key_file
,
3720 const gchar
*group_name
,
3725 GKeyFileKeyValuePair
*pair
;
3726 GKeyFileGroup
*group
;
3728 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3729 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3730 g_return_val_if_fail (key
!= NULL
, FALSE
);
3732 group
= g_key_file_lookup_group (key_file
, group_name
);
3736 g_set_error (error
, G_KEY_FILE_ERROR
,
3737 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3738 _("Key file does not have group “%s”"),
3744 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3747 *has_key
= pair
!= NULL
;
3752 * g_key_file_has_key: (skip)
3753 * @key_file: a #GKeyFile
3754 * @group_name: a group name
3756 * @error: return location for a #GError
3758 * Looks whether the key file has the key @key in the group
3761 * Note that this function does not follow the rules for #GError strictly;
3762 * the return value both carries meaning and signals an error. To use
3763 * this function, you must pass a #GError pointer in @error, and check
3764 * whether it is not %NULL to see if an error occurred.
3766 * Language bindings should use g_key_file_get_value() to test whether
3767 * or not a key exists.
3769 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3774 g_key_file_has_key (GKeyFile
*key_file
,
3775 const gchar
*group_name
,
3779 GError
*temp_error
= NULL
;
3782 if (g_key_file_has_key_full (key_file
, group_name
, key
, &has_key
, &temp_error
))
3788 g_propagate_error (error
, temp_error
);
3794 g_key_file_add_group (GKeyFile
*key_file
,
3795 const gchar
*group_name
)
3797 GKeyFileGroup
*group
;
3799 g_return_if_fail (key_file
!= NULL
);
3800 g_return_if_fail (g_key_file_is_group_name (group_name
));
3802 group
= g_key_file_lookup_group (key_file
, group_name
);
3805 key_file
->current_group
= group
;
3809 group
= g_slice_new0 (GKeyFileGroup
);
3810 group
->name
= g_strdup (group_name
);
3811 group
->lookup_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
3812 key_file
->groups
= g_list_prepend (key_file
->groups
, group
);
3813 key_file
->current_group
= group
;
3815 if (key_file
->start_group
== NULL
)
3816 key_file
->start_group
= group
;
3818 g_hash_table_insert (key_file
->group_hash
, (gpointer
)group
->name
, group
);
3822 g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
)
3827 g_free (pair
->value
);
3828 g_slice_free (GKeyFileKeyValuePair
, pair
);
3832 /* Be careful not to call this function on a node with data in the
3833 * lookup map without removing it from the lookup map, first.
3835 * Some current cases where this warning is not a concern are
3837 * - the node being removed is a comment node
3838 * - the entire lookup map is getting destroyed soon after
3842 g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
3843 GKeyFileGroup
*group
,
3847 GKeyFileKeyValuePair
*pair
;
3849 pair
= (GKeyFileKeyValuePair
*) pair_node
->data
;
3851 group
->key_value_pairs
= g_list_remove_link (group
->key_value_pairs
, pair_node
);
3853 g_warn_if_fail (pair
->value
!= NULL
);
3855 g_key_file_key_value_pair_free (pair
);
3857 g_list_free_1 (pair_node
);
3861 g_key_file_remove_group_node (GKeyFile
*key_file
,
3864 GKeyFileGroup
*group
;
3867 group
= (GKeyFileGroup
*) group_node
->data
;
3870 g_hash_table_remove (key_file
->group_hash
, group
->name
);
3872 /* If the current group gets deleted make the current group the last
3875 if (key_file
->current_group
== group
)
3877 /* groups should always contain at least the top comment group,
3878 * unless g_key_file_clear has been called
3880 if (key_file
->groups
)
3881 key_file
->current_group
= (GKeyFileGroup
*) key_file
->groups
->data
;
3883 key_file
->current_group
= NULL
;
3886 /* If the start group gets deleted make the start group the first
3889 if (key_file
->start_group
== group
)
3891 tmp
= g_list_last (key_file
->groups
);
3894 if (tmp
!= group_node
&&
3895 ((GKeyFileGroup
*) tmp
->data
)->name
!= NULL
)
3902 key_file
->start_group
= (GKeyFileGroup
*) tmp
->data
;
3904 key_file
->start_group
= NULL
;
3907 key_file
->groups
= g_list_remove_link (key_file
->groups
, group_node
);
3909 tmp
= group
->key_value_pairs
;
3916 g_key_file_remove_key_value_pair_node (key_file
, group
, pair_node
);
3919 g_warn_if_fail (group
->key_value_pairs
== NULL
);
3923 g_key_file_key_value_pair_free (group
->comment
);
3924 group
->comment
= NULL
;
3927 if (group
->lookup_map
)
3929 g_hash_table_destroy (group
->lookup_map
);
3930 group
->lookup_map
= NULL
;
3933 g_free ((gchar
*) group
->name
);
3934 g_slice_free (GKeyFileGroup
, group
);
3935 g_list_free_1 (group_node
);
3939 * g_key_file_remove_group:
3940 * @key_file: a #GKeyFile
3941 * @group_name: a group name
3942 * @error: return location for a #GError or %NULL
3944 * Removes the specified group, @group_name,
3945 * from the key file.
3947 * Returns: %TRUE if the group was removed, %FALSE otherwise
3952 g_key_file_remove_group (GKeyFile
*key_file
,
3953 const gchar
*group_name
,
3958 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3959 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3961 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3965 g_set_error (error
, G_KEY_FILE_ERROR
,
3966 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3967 _("Key file does not have group “%s”"),
3972 g_key_file_remove_group_node (key_file
, group_node
);
3978 g_key_file_add_key_value_pair (GKeyFile
*key_file
,
3979 GKeyFileGroup
*group
,
3980 GKeyFileKeyValuePair
*pair
)
3982 g_hash_table_replace (group
->lookup_map
, pair
->key
, pair
);
3983 group
->key_value_pairs
= g_list_prepend (group
->key_value_pairs
, pair
);
3987 g_key_file_add_key (GKeyFile
*key_file
,
3988 GKeyFileGroup
*group
,
3992 GKeyFileKeyValuePair
*pair
;
3994 pair
= g_slice_new (GKeyFileKeyValuePair
);
3995 pair
->key
= g_strdup (key
);
3996 pair
->value
= g_strdup (value
);
3998 g_key_file_add_key_value_pair (key_file
, group
, pair
);
4002 * g_key_file_remove_key:
4003 * @key_file: a #GKeyFile
4004 * @group_name: a group name
4005 * @key: a key name to remove
4006 * @error: return location for a #GError or %NULL
4008 * Removes @key in @group_name from the key file.
4010 * Returns: %TRUE if the key was removed, %FALSE otherwise
4015 g_key_file_remove_key (GKeyFile
*key_file
,
4016 const gchar
*group_name
,
4020 GKeyFileGroup
*group
;
4021 GKeyFileKeyValuePair
*pair
;
4023 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
4024 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
4025 g_return_val_if_fail (key
!= NULL
, FALSE
);
4029 group
= g_key_file_lookup_group (key_file
, group_name
);
4032 g_set_error (error
, G_KEY_FILE_ERROR
,
4033 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
4034 _("Key file does not have group “%s”"),
4039 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
4043 set_not_found_key_error (group
->name
, key
, error
);
4047 group
->key_value_pairs
= g_list_remove (group
->key_value_pairs
, pair
);
4048 g_hash_table_remove (group
->lookup_map
, pair
->key
);
4049 g_key_file_key_value_pair_free (pair
);
4055 g_key_file_lookup_group_node (GKeyFile
*key_file
,
4056 const gchar
*group_name
)
4058 GKeyFileGroup
*group
;
4061 for (tmp
= key_file
->groups
; tmp
!= NULL
; tmp
= tmp
->next
)
4063 group
= (GKeyFileGroup
*) tmp
->data
;
4065 if (group
&& group
->name
&& strcmp (group
->name
, group_name
) == 0)
4072 static GKeyFileGroup
*
4073 g_key_file_lookup_group (GKeyFile
*key_file
,
4074 const gchar
*group_name
)
4076 return (GKeyFileGroup
*)g_hash_table_lookup (key_file
->group_hash
, group_name
);
4080 g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
4081 GKeyFileGroup
*group
,
4086 for (key_node
= group
->key_value_pairs
;
4088 key_node
= key_node
->next
)
4090 GKeyFileKeyValuePair
*pair
;
4092 pair
= (GKeyFileKeyValuePair
*) key_node
->data
;
4094 if (pair
->key
&& strcmp (pair
->key
, key
) == 0)
4101 static GKeyFileKeyValuePair
*
4102 g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
4103 GKeyFileGroup
*group
,
4106 return (GKeyFileKeyValuePair
*) g_hash_table_lookup (group
->lookup_map
, key
);
4109 /* Lines starting with # or consisting entirely of whitespace are merely
4110 * recorded, not parsed. This function assumes all leading whitespace
4111 * has been stripped.
4114 g_key_file_line_is_comment (const gchar
*line
)
4116 return (*line
== '#' || *line
== '\0' || *line
== '\n');
4120 g_key_file_is_group_name (const gchar
*name
)
4127 p
= q
= (gchar
*) name
;
4128 while (*q
&& *q
!= ']' && *q
!= '[' && !g_ascii_iscntrl (*q
))
4129 q
= g_utf8_find_next_char (q
, NULL
);
4131 if (*q
!= '\0' || q
== p
)
4138 g_key_file_is_key_name (const gchar
*name
)
4145 p
= q
= (gchar
*) name
;
4146 /* We accept a little more than the desktop entry spec says,
4147 * since gnome-vfs uses mime-types as keys in its cache.
4149 while (*q
&& *q
!= '=' && *q
!= '[' && *q
!= ']')
4150 q
= g_utf8_find_next_char (q
, NULL
);
4152 /* No empty keys, please */
4156 /* We accept spaces in the middle of keys to not break
4157 * existing apps, but we don't tolerate initial or final
4158 * spaces, which would lead to silent corruption when
4159 * rereading the file.
4161 if (*p
== ' ' || q
[-1] == ' ')
4167 while (*q
&& (g_unichar_isalnum (g_utf8_get_char_validated (q
, -1)) || *q
== '-' || *q
== '_' || *q
== '.' || *q
== '@'))
4168 q
= g_utf8_find_next_char (q
, NULL
);
4182 /* A group in a key file is made up of a starting '[' followed by one
4183 * or more letters making up the group name followed by ']'.
4186 g_key_file_line_is_group (const gchar
*line
)
4196 while (*p
&& *p
!= ']')
4197 p
= g_utf8_find_next_char (p
, NULL
);
4202 /* silently accept whitespace after the ] */
4203 p
= g_utf8_find_next_char (p
, NULL
);
4204 while (*p
== ' ' || *p
== '\t')
4205 p
= g_utf8_find_next_char (p
, NULL
);
4214 g_key_file_line_is_key_value_pair (const gchar
*line
)
4218 p
= (gchar
*) g_utf8_strchr (line
, -1, '=');
4223 /* Key must be non-empty
4232 g_key_file_parse_value_as_string (GKeyFile
*key_file
,
4237 gchar
*string_value
, *p
, *q0
, *q
;
4239 string_value
= g_new (gchar
, strlen (value
) + 1);
4241 p
= (gchar
*) value
;
4242 q0
= q
= string_value
;
4272 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
4273 G_KEY_FILE_ERROR_INVALID_VALUE
,
4274 _("Key file contains escape character "
4279 if (pieces
&& *p
== key_file
->list_separator
)
4280 *q
= key_file
->list_separator
;
4294 g_set_error (error
, G_KEY_FILE_ERROR
,
4295 G_KEY_FILE_ERROR_INVALID_VALUE
,
4296 _("Key file contains invalid escape "
4297 "sequence “%s”"), sequence
);
4306 if (pieces
&& (*p
== key_file
->list_separator
))
4308 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4324 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4325 *pieces
= g_slist_reverse (*pieces
);
4328 return string_value
;
4332 g_key_file_parse_string_as_value (GKeyFile
*key_file
,
4333 const gchar
*string
,
4334 gboolean escape_separator
)
4336 gchar
*value
, *p
, *q
;
4338 gboolean parsing_leading_space
;
4340 length
= strlen (string
) + 1;
4342 /* Worst case would be that every character needs to be escaped.
4343 * In other words every character turns to two characters
4345 value
= g_new (gchar
, 2 * length
);
4347 p
= (gchar
*) string
;
4349 parsing_leading_space
= TRUE
;
4350 while (p
< (string
+ length
- 1))
4352 gchar escaped_character
[3] = { '\\', 0, 0 };
4357 if (parsing_leading_space
)
4359 escaped_character
[1] = 's';
4360 strcpy (q
, escaped_character
);
4370 if (parsing_leading_space
)
4372 escaped_character
[1] = 't';
4373 strcpy (q
, escaped_character
);
4383 escaped_character
[1] = 'n';
4384 strcpy (q
, escaped_character
);
4388 escaped_character
[1] = 'r';
4389 strcpy (q
, escaped_character
);
4393 escaped_character
[1] = '\\';
4394 strcpy (q
, escaped_character
);
4396 parsing_leading_space
= FALSE
;
4399 if (escape_separator
&& *p
== key_file
->list_separator
)
4401 escaped_character
[1] = key_file
->list_separator
;
4402 strcpy (q
, escaped_character
);
4404 parsing_leading_space
= TRUE
;
4410 parsing_leading_space
= FALSE
;
4422 g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
4432 long_value
= strtol (value
, &eof_int
, 10);
4435 if (*value
== '\0' || (*eof_int
!= '\0' && !g_ascii_isspace(*eof_int
)))
4437 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
4438 g_set_error (error
, G_KEY_FILE_ERROR
,
4439 G_KEY_FILE_ERROR_INVALID_VALUE
,
4440 _("Value “%s” cannot be interpreted "
4441 "as a number."), value_utf8
);
4442 g_free (value_utf8
);
4447 int_value
= long_value
;
4448 if (int_value
!= long_value
|| errsv
== ERANGE
)
4450 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
4453 G_KEY_FILE_ERROR_INVALID_VALUE
,
4454 _("Integer value “%s” out of range"),
4456 g_free (value_utf8
);
4465 g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
4469 return g_strdup_printf ("%d", value
);
4473 g_key_file_parse_value_as_double (GKeyFile
*key_file
,
4477 gchar
*end_of_valid_d
;
4478 gdouble double_value
= 0;
4480 double_value
= g_ascii_strtod (value
, &end_of_valid_d
);
4482 if (*end_of_valid_d
!= '\0' || end_of_valid_d
== value
)
4484 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
4485 g_set_error (error
, G_KEY_FILE_ERROR
,
4486 G_KEY_FILE_ERROR_INVALID_VALUE
,
4487 _("Value “%s” cannot be interpreted "
4488 "as a float number."),
4490 g_free (value_utf8
);
4495 return double_value
;
4499 strcmp_sized (const gchar
*s1
, size_t len1
, const gchar
*s2
)
4501 size_t len2
= strlen (s2
);
4502 return strncmp (s1
, s2
, MAX (len1
, len2
));
4506 g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
4513 /* Count the number of non-whitespace characters */
4514 for (i
= 0; value
[i
]; i
++)
4515 if (!g_ascii_isspace (value
[i
]))
4518 if (strcmp_sized (value
, length
, "true") == 0 || strcmp_sized (value
, length
, "1") == 0)
4520 else if (strcmp_sized (value
, length
, "false") == 0 || strcmp_sized (value
, length
, "0") == 0)
4523 value_utf8
= g_utf8_make_valid (value
, -1);
4524 g_set_error (error
, G_KEY_FILE_ERROR
,
4525 G_KEY_FILE_ERROR_INVALID_VALUE
,
4526 _("Value “%s” cannot be interpreted "
4527 "as a boolean."), value_utf8
);
4528 g_free (value_utf8
);
4534 g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
4538 return g_strdup ("true");
4540 return g_strdup ("false");
4544 g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
4551 string
= g_string_sized_new (512);
4553 lines
= g_strsplit (value
, "\n", 0);
4555 for (i
= 0; lines
[i
] != NULL
; i
++)
4557 if (lines
[i
][0] != '#')
4558 g_string_append_printf (string
, "%s\n", lines
[i
]);
4560 g_string_append_printf (string
, "%s\n", lines
[i
] + 1);
4564 return g_string_free (string
, FALSE
);
4568 g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
4569 const gchar
*comment
)
4575 string
= g_string_sized_new (512);
4577 lines
= g_strsplit (comment
, "\n", 0);
4579 for (i
= 0; lines
[i
] != NULL
; i
++)
4580 g_string_append_printf (string
, "#%s%s", lines
[i
],
4581 lines
[i
+ 1] == NULL
? "" : "\n");
4584 return g_string_free (string
, FALSE
);
4588 * g_key_file_save_to_file:
4589 * @key_file: a #GKeyFile
4590 * @filename: the name of the file to write to
4591 * @error: a pointer to a %NULL #GError, or %NULL
4593 * Writes the contents of @key_file to @filename using
4594 * g_file_set_contents().
4596 * This function can fail for any of the reasons that
4597 * g_file_set_contents() may fail.
4599 * Returns: %TRUE if successful, else %FALSE with @error set
4604 g_key_file_save_to_file (GKeyFile
*key_file
,
4605 const gchar
*filename
,
4612 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
4613 g_return_val_if_fail (filename
!= NULL
, FALSE
);
4614 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
4616 contents
= g_key_file_to_data (key_file
, &length
, NULL
);
4617 g_assert (contents
!= NULL
);
4619 success
= g_file_set_contents (filename
, contents
, length
, error
);