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 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.
160 * Error domain for key file parsing. Errors in this domain will
161 * be from the #GKeyFileError enumeration.
163 * See #GError for information on error domains.
168 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
169 * an unknown encoding
170 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
171 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
172 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
173 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
174 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
176 * Error codes returned by key file parsing.
181 * @G_KEY_FILE_NONE: No flags, default behaviour
182 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
183 * (possibly modified) contents of the key file back to a file;
184 * otherwise all comments will be lost when the key file is
186 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
187 * (possibly modified) contents of the key file back to a file;
188 * otherwise only the translations for the current language will be
191 * Flags which influence the parsing.
195 * G_KEY_FILE_DESKTOP_GROUP:
197 * The name of the main group of a desktop entry file, as defined in the
198 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
199 * Consult the specification for more
200 * details about the meanings of the keys below.
206 * G_KEY_FILE_DESKTOP_KEY_TYPE:
208 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
209 * giving the type of the desktop entry. Usually
210 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
211 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
212 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
218 * G_KEY_FILE_DESKTOP_KEY_VERSION:
220 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
221 * giving the version of the Desktop Entry Specification used for
222 * the desktop entry file.
228 * G_KEY_FILE_DESKTOP_KEY_NAME:
230 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
231 * string giving the specific name of the desktop entry.
237 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
239 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
240 * string giving the generic name of the desktop entry.
246 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
248 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
249 * stating whether the desktop entry should be shown in menus.
255 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
257 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
258 * string giving the tooltip for the desktop entry.
264 * G_KEY_FILE_DESKTOP_KEY_ICON:
266 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
267 * string giving the name of the icon to be displayed for the desktop
274 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
276 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
277 * stating whether the desktop entry has been deleted by the user.
283 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
285 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
286 * strings identifying the environments that should display the
293 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
295 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
296 * strings identifying the environments that should not display the
303 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
305 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
306 * giving the file name of a binary on disk used to determine if the
307 * program is actually installed. It is only valid for desktop entries
308 * with the `Application` type.
314 * G_KEY_FILE_DESKTOP_KEY_EXEC:
316 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
317 * giving the command line to execute. It is only valid for desktop
318 * entries with the `Application` type.
324 * G_KEY_FILE_DESKTOP_KEY_PATH:
326 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
327 * containing the working directory to run the program in. It is only
328 * valid for desktop entries with the `Application` type.
334 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
336 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
337 * stating whether the program should be run in a terminal window.
338 * It is only valid for desktop entries with the
339 * `Application` type.
345 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
347 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
348 * of strings giving the MIME types supported by this desktop entry.
354 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
356 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
357 * of strings giving the categories in which the desktop entry
358 * should be shown in a menu.
364 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
366 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
367 * stating whether the application supports the
368 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
374 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
376 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
377 * identifying the WM class or name hint of a window that the application
378 * will create, which can be used to emulate Startup Notification with
379 * older applications.
385 * G_KEY_FILE_DESKTOP_KEY_URL:
387 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
388 * giving the URL to access. It is only valid for desktop entries
389 * with the `Link` type.
395 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
397 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to true
398 * if the application is D-Bus activatable.
404 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
406 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
407 * giving the available application actions.
413 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
415 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
416 * entries representing applications.
422 * G_KEY_FILE_DESKTOP_TYPE_LINK:
424 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
425 * entries representing links to documents.
431 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
433 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
434 * entries representing directories.
439 typedef struct _GKeyFileGroup GKeyFileGroup
;
444 * The GKeyFile struct contains only private data
445 * and should not be accessed directly.
450 GHashTable
*group_hash
;
452 GKeyFileGroup
*start_group
;
453 GKeyFileGroup
*current_group
;
455 GString
*parse_buffer
; /* Holds up to one line of not-yet-parsed data */
457 gchar list_separator
;
463 volatile gint ref_count
;
466 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair
;
468 struct _GKeyFileGroup
470 const gchar
*name
; /* NULL for above first group (which will be comments) */
472 GKeyFileKeyValuePair
*comment
; /* Special comment that is stuck to the top of a group */
474 GList
*key_value_pairs
;
476 /* Used in parallel with key_value_pairs for
477 * increased lookup performance
479 GHashTable
*lookup_map
;
482 struct _GKeyFileKeyValuePair
484 gchar
*key
; /* NULL for comments */
488 static gint
find_file_in_data_dirs (const gchar
*file
,
489 const gchar
**data_dirs
,
492 static gboolean
g_key_file_load_from_fd (GKeyFile
*key_file
,
496 static GList
*g_key_file_lookup_group_node (GKeyFile
*key_file
,
497 const gchar
*group_name
);
498 static GKeyFileGroup
*g_key_file_lookup_group (GKeyFile
*key_file
,
499 const gchar
*group_name
);
501 static GList
*g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
502 GKeyFileGroup
*group
,
504 static GKeyFileKeyValuePair
*g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
505 GKeyFileGroup
*group
,
508 static void g_key_file_remove_group_node (GKeyFile
*key_file
,
510 static void g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
511 GKeyFileGroup
*group
,
514 static void g_key_file_add_key_value_pair (GKeyFile
*key_file
,
515 GKeyFileGroup
*group
,
516 GKeyFileKeyValuePair
*pair
);
517 static void g_key_file_add_key (GKeyFile
*key_file
,
518 GKeyFileGroup
*group
,
521 static void g_key_file_add_group (GKeyFile
*key_file
,
522 const gchar
*group_name
);
523 static gboolean
g_key_file_is_group_name (const gchar
*name
);
524 static gboolean
g_key_file_is_key_name (const gchar
*name
);
525 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
);
526 static gboolean
g_key_file_line_is_comment (const gchar
*line
);
527 static gboolean
g_key_file_line_is_group (const gchar
*line
);
528 static gboolean
g_key_file_line_is_key_value_pair (const gchar
*line
);
529 static gchar
*g_key_file_parse_value_as_string (GKeyFile
*key_file
,
533 static gchar
*g_key_file_parse_string_as_value (GKeyFile
*key_file
,
535 gboolean escape_separator
);
536 static gint
g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
539 static gchar
*g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
541 static gdouble
g_key_file_parse_value_as_double (GKeyFile
*key_file
,
544 static gboolean
g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
547 static gchar
*g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
549 static gchar
*g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
551 static gchar
*g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
552 const gchar
*comment
);
553 static void g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
557 static void g_key_file_parse_comment (GKeyFile
*key_file
,
561 static void g_key_file_parse_group (GKeyFile
*key_file
,
565 static gchar
*key_get_locale (const gchar
*key
);
566 static void g_key_file_parse_data (GKeyFile
*key_file
,
570 static void g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
573 G_DEFINE_QUARK (g
-key
-file
-error
-quark
, g_key_file_error
)
576 g_key_file_init (GKeyFile
*key_file
)
578 key_file
->current_group
= g_slice_new0 (GKeyFileGroup
);
579 key_file
->groups
= g_list_prepend (NULL
, key_file
->current_group
);
580 key_file
->group_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
581 key_file
->start_group
= NULL
;
582 key_file
->parse_buffer
= g_string_sized_new (128);
583 key_file
->list_separator
= ';';
585 key_file
->locales
= g_strdupv ((gchar
**)g_get_language_names ());
589 g_key_file_clear (GKeyFile
*key_file
)
591 GList
*tmp
, *group_node
;
593 if (key_file
->locales
)
595 g_strfreev (key_file
->locales
);
596 key_file
->locales
= NULL
;
599 if (key_file
->parse_buffer
)
601 g_string_free (key_file
->parse_buffer
, TRUE
);
602 key_file
->parse_buffer
= NULL
;
605 tmp
= key_file
->groups
;
610 g_key_file_remove_group_node (key_file
, group_node
);
613 if (key_file
->group_hash
!= NULL
)
615 g_hash_table_destroy (key_file
->group_hash
);
616 key_file
->group_hash
= NULL
;
619 g_warn_if_fail (key_file
->groups
== NULL
);
626 * Creates a new empty #GKeyFile object. Use
627 * g_key_file_load_from_file(), g_key_file_load_from_data(),
628 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
629 * read an existing key file.
631 * Returns: (transfer full): an empty #GKeyFile.
636 g_key_file_new (void)
640 key_file
= g_slice_new0 (GKeyFile
);
641 key_file
->ref_count
= 1;
642 g_key_file_init (key_file
);
648 * g_key_file_set_list_separator:
649 * @key_file: a #GKeyFile
650 * @separator: the separator
652 * Sets the character which is used to separate
653 * values in lists. Typically ';' or ',' are used
654 * as separators. The default list separator is ';'.
659 g_key_file_set_list_separator (GKeyFile
*key_file
,
662 g_return_if_fail (key_file
!= NULL
);
664 key_file
->list_separator
= separator
;
668 /* Iterates through all the directories in *dirs trying to
669 * open file. When it successfully locates and opens a file it
670 * returns the file descriptor to the open file. It also
671 * outputs the absolute path of the file in output_file.
674 find_file_in_data_dirs (const gchar
*file
,
679 const gchar
**data_dirs
, *data_dir
;
691 while (data_dirs
&& (data_dir
= *data_dirs
) && fd
== -1)
693 gchar
*candidate_file
, *sub_dir
;
695 candidate_file
= (gchar
*) file
;
696 sub_dir
= g_strdup ("");
697 while (candidate_file
!= NULL
&& fd
== -1)
701 path
= g_build_filename (data_dir
, sub_dir
,
702 candidate_file
, NULL
);
704 fd
= g_open (path
, O_RDONLY
, 0);
712 candidate_file
= strchr (candidate_file
, '-');
714 if (candidate_file
== NULL
)
720 sub_dir
= g_strndup (file
, candidate_file
- file
- 1);
722 for (p
= sub_dir
; *p
!= '\0'; p
++)
725 *p
= G_DIR_SEPARATOR
;
734 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
735 G_KEY_FILE_ERROR_NOT_FOUND
,
736 _("Valid key file could not be "
737 "found in search dirs"));
740 if (output_file
!= NULL
&& fd
> 0)
741 *output_file
= g_strdup (path
);
749 g_key_file_load_from_fd (GKeyFile
*key_file
,
754 GError
*key_file_error
= NULL
;
756 struct stat stat_buf
;
757 gchar read_buf
[4096];
758 gchar list_separator
;
760 if (fstat (fd
, &stat_buf
) < 0)
762 g_set_error_literal (error
, G_FILE_ERROR
,
763 g_file_error_from_errno (errno
),
768 if (!S_ISREG (stat_buf
.st_mode
))
770 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
771 G_KEY_FILE_ERROR_PARSE
,
772 _("Not a regular file"));
776 list_separator
= key_file
->list_separator
;
777 g_key_file_clear (key_file
);
778 g_key_file_init (key_file
);
779 key_file
->list_separator
= list_separator
;
780 key_file
->flags
= flags
;
784 bytes_read
= read (fd
, read_buf
, 4096);
786 if (bytes_read
== 0) /* End of File */
791 if (errno
== EINTR
|| errno
== EAGAIN
)
794 g_set_error_literal (error
, G_FILE_ERROR
,
795 g_file_error_from_errno (errno
),
800 g_key_file_parse_data (key_file
,
801 read_buf
, bytes_read
,
804 while (!key_file_error
);
808 g_propagate_error (error
, key_file_error
);
812 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
816 g_propagate_error (error
, key_file_error
);
824 * g_key_file_load_from_file:
825 * @key_file: an empty #GKeyFile struct
826 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
827 * @flags: flags from #GKeyFileFlags
828 * @error: return location for a #GError, or %NULL
830 * Loads a key file into an empty #GKeyFile structure.
832 * If the OS returns an error when opening or reading the file, a
833 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
834 * %G_KEY_FILE_ERROR is returned.
836 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
837 * @file is not found, %G_FILE_ERROR_NOENT is returned.
839 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
844 g_key_file_load_from_file (GKeyFile
*key_file
,
849 GError
*key_file_error
= NULL
;
852 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
853 g_return_val_if_fail (file
!= NULL
, FALSE
);
855 fd
= g_open (file
, O_RDONLY
, 0);
859 g_set_error_literal (error
, G_FILE_ERROR
,
860 g_file_error_from_errno (errno
),
865 g_key_file_load_from_fd (key_file
, fd
, flags
, &key_file_error
);
870 g_propagate_error (error
, key_file_error
);
878 * g_key_file_load_from_data:
879 * @key_file: an empty #GKeyFile struct
880 * @data: key file loaded in memory
881 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
882 * @flags: flags from #GKeyFileFlags
883 * @error: return location for a #GError, or %NULL
885 * Loads a key file from memory into an empty #GKeyFile structure.
886 * If the object cannot be created then %error is set to a #GKeyFileError.
888 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
893 g_key_file_load_from_data (GKeyFile
*key_file
,
899 GError
*key_file_error
= NULL
;
900 gchar list_separator
;
902 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
903 g_return_val_if_fail (data
!= NULL
|| length
== 0, FALSE
);
905 if (length
== (gsize
)-1)
906 length
= strlen (data
);
908 list_separator
= key_file
->list_separator
;
909 g_key_file_clear (key_file
);
910 g_key_file_init (key_file
);
911 key_file
->list_separator
= list_separator
;
912 key_file
->flags
= flags
;
914 g_key_file_parse_data (key_file
, data
, length
, &key_file_error
);
918 g_propagate_error (error
, key_file_error
);
922 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
926 g_propagate_error (error
, key_file_error
);
934 * g_key_file_load_from_bytes:
935 * @key_file: an empty #GKeyFile struct
937 * @flags: flags from #GKeyFileFlags
938 * @error: return location for a #GError, or %NULL
940 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
941 * If the object cannot be created then %error is set to a #GKeyFileError.
943 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
948 g_key_file_load_from_bytes (GKeyFile
*key_file
,
956 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
957 g_return_val_if_fail (bytes
!= NULL
, FALSE
);
959 data
= g_bytes_get_data (bytes
, &size
);
960 return g_key_file_load_from_data (key_file
, (const gchar
*) data
, size
, flags
, error
);
964 * g_key_file_load_from_dirs:
965 * @key_file: an empty #GKeyFile struct
966 * @file: (type filename): a relative path to a filename to open and parse
967 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
968 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
969 * of the file, or %NULL
970 * @flags: flags from #GKeyFileFlags
971 * @error: return location for a #GError, or %NULL
973 * This function looks for a key file named @file in the paths
974 * specified in @search_dirs, loads the file into @key_file and
975 * returns the file's full path in @full_path.
977 * If the file could not be found in any of the @search_dirs,
978 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
979 * the file is found but the OS returns an error when opening or reading the
980 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
981 * %G_KEY_FILE_ERROR is returned.
983 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
988 g_key_file_load_from_dirs (GKeyFile
*key_file
,
990 const gchar
**search_dirs
,
995 GError
*key_file_error
= NULL
;
996 const gchar
**data_dirs
;
1001 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
1002 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
1003 g_return_val_if_fail (search_dirs
!= NULL
, FALSE
);
1006 data_dirs
= search_dirs
;
1008 while (*data_dirs
!= NULL
&& !found_file
)
1010 g_free (output_path
);
1013 fd
= find_file_in_data_dirs (file
, data_dirs
, &output_path
,
1019 g_propagate_error (error
, key_file_error
);
1023 found_file
= g_key_file_load_from_fd (key_file
, fd
, flags
,
1029 g_propagate_error (error
, key_file_error
);
1034 if (found_file
&& full_path
)
1035 *full_path
= output_path
;
1037 g_free (output_path
);
1043 * g_key_file_load_from_data_dirs:
1044 * @key_file: an empty #GKeyFile struct
1045 * @file: (type filename): a relative path to a filename to open and parse
1046 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1047 * of the file, or %NULL
1048 * @flags: flags from #GKeyFileFlags
1049 * @error: return location for a #GError, or %NULL
1051 * This function looks for a key file named @file in the paths
1052 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1053 * loads the file into @key_file and returns the file's full path in
1054 * @full_path. If the file could not be loaded then an %error is
1055 * set to either a #GFileError or #GKeyFileError.
1057 * Returns: %TRUE if a key file could be loaded, %FALSE othewise
1061 g_key_file_load_from_data_dirs (GKeyFile
*key_file
,
1064 GKeyFileFlags flags
,
1067 gchar
**all_data_dirs
;
1068 const gchar
* user_data_dir
;
1069 const gchar
* const * system_data_dirs
;
1071 gboolean found_file
;
1073 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
1074 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
1076 user_data_dir
= g_get_user_data_dir ();
1077 system_data_dirs
= g_get_system_data_dirs ();
1078 all_data_dirs
= g_new (gchar
*, g_strv_length ((gchar
**)system_data_dirs
) + 2);
1081 all_data_dirs
[i
++] = g_strdup (user_data_dir
);
1084 while (system_data_dirs
[j
] != NULL
)
1085 all_data_dirs
[i
++] = g_strdup (system_data_dirs
[j
++]);
1086 all_data_dirs
[i
] = NULL
;
1088 found_file
= g_key_file_load_from_dirs (key_file
,
1090 (const gchar
**)all_data_dirs
,
1095 g_strfreev (all_data_dirs
);
1101 * g_key_file_ref: (skip)
1102 * @key_file: a #GKeyFile
1104 * Increases the reference count of @key_file.
1106 * Returns: the same @key_file.
1111 g_key_file_ref (GKeyFile
*key_file
)
1113 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1115 g_atomic_int_inc (&key_file
->ref_count
);
1121 * g_key_file_free: (skip)
1122 * @key_file: a #GKeyFile
1124 * Clears all keys and groups from @key_file, and decreases the
1125 * reference count by 1. If the reference count reaches zero,
1126 * frees the key file and all its allocated memory.
1131 g_key_file_free (GKeyFile
*key_file
)
1133 g_return_if_fail (key_file
!= NULL
);
1135 g_key_file_clear (key_file
);
1136 g_key_file_unref (key_file
);
1141 * @key_file: a #GKeyFile
1143 * Decreases the reference count of @key_file by 1. If the reference count
1144 * reaches zero, frees the key file and all its allocated memory.
1149 g_key_file_unref (GKeyFile
*key_file
)
1151 g_return_if_fail (key_file
!= NULL
);
1153 if (g_atomic_int_dec_and_test (&key_file
->ref_count
))
1155 g_key_file_clear (key_file
);
1156 g_slice_free (GKeyFile
, key_file
);
1160 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1161 * true for locales that match those in g_get_language_names().
1164 g_key_file_locale_is_interesting (GKeyFile
*key_file
,
1165 const gchar
*locale
)
1169 if (key_file
->flags
& G_KEY_FILE_KEEP_TRANSLATIONS
)
1172 for (i
= 0; key_file
->locales
[i
] != NULL
; i
++)
1174 if (g_ascii_strcasecmp (key_file
->locales
[i
], locale
) == 0)
1182 g_key_file_parse_line (GKeyFile
*key_file
,
1187 GError
*parse_error
= NULL
;
1190 g_return_if_fail (key_file
!= NULL
);
1191 g_return_if_fail (line
!= NULL
);
1193 line_start
= (gchar
*) line
;
1194 while (g_ascii_isspace (*line_start
))
1197 if (g_key_file_line_is_comment (line_start
))
1198 g_key_file_parse_comment (key_file
, line
, length
, &parse_error
);
1199 else if (g_key_file_line_is_group (line_start
))
1200 g_key_file_parse_group (key_file
, line_start
,
1201 length
- (line_start
- line
),
1203 else if (g_key_file_line_is_key_value_pair (line_start
))
1204 g_key_file_parse_key_value_pair (key_file
, line_start
,
1205 length
- (line_start
- line
),
1209 gchar
*line_utf8
= g_utf8_make_valid (line
, length
);
1210 g_set_error (error
, G_KEY_FILE_ERROR
,
1211 G_KEY_FILE_ERROR_PARSE
,
1212 _("Key file contains line “%s” which is not "
1213 "a key-value pair, group, or comment"),
1221 g_propagate_error (error
, parse_error
);
1225 g_key_file_parse_comment (GKeyFile
*key_file
,
1230 GKeyFileKeyValuePair
*pair
;
1232 if (!(key_file
->flags
& G_KEY_FILE_KEEP_COMMENTS
))
1235 g_warn_if_fail (key_file
->current_group
!= NULL
);
1237 pair
= g_slice_new (GKeyFileKeyValuePair
);
1239 pair
->value
= g_strndup (line
, length
);
1241 key_file
->current_group
->key_value_pairs
=
1242 g_list_prepend (key_file
->current_group
->key_value_pairs
, pair
);
1246 g_key_file_parse_group (GKeyFile
*key_file
,
1252 const gchar
*group_name_start
, *group_name_end
;
1254 /* advance past opening '['
1256 group_name_start
= line
+ 1;
1257 group_name_end
= line
+ length
- 1;
1259 while (*group_name_end
!= ']')
1262 group_name
= g_strndup (group_name_start
,
1263 group_name_end
- group_name_start
);
1265 if (!g_key_file_is_group_name (group_name
))
1267 g_set_error (error
, G_KEY_FILE_ERROR
,
1268 G_KEY_FILE_ERROR_PARSE
,
1269 _("Invalid group name: %s"), group_name
);
1270 g_free (group_name
);
1274 g_key_file_add_group (key_file
, group_name
);
1275 g_free (group_name
);
1279 g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
1284 gchar
*key
, *value
, *key_end
, *value_start
, *locale
;
1285 gsize key_len
, value_len
;
1287 if (key_file
->current_group
== NULL
|| key_file
->current_group
->name
== NULL
)
1289 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
1290 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1291 _("Key file does not start with a group"));
1295 key_end
= value_start
= strchr (line
, '=');
1297 g_warn_if_fail (key_end
!= NULL
);
1302 /* Pull the key name from the line (chomping trailing whitespace)
1304 while (g_ascii_isspace (*key_end
))
1307 key_len
= key_end
- line
+ 2;
1309 g_warn_if_fail (key_len
<= length
);
1311 key
= g_strndup (line
, key_len
- 1);
1313 if (!g_key_file_is_key_name (key
))
1315 g_set_error (error
, G_KEY_FILE_ERROR
,
1316 G_KEY_FILE_ERROR_PARSE
,
1317 _("Invalid key name: %s"), key
);
1322 /* Pull the value from the line (chugging leading whitespace)
1324 while (g_ascii_isspace (*value_start
))
1327 value_len
= line
+ length
- value_start
+ 1;
1329 value
= g_strndup (value_start
, value_len
);
1331 g_warn_if_fail (key_file
->start_group
!= NULL
);
1333 if (key_file
->current_group
1334 && key_file
->current_group
->name
1335 && strcmp (key_file
->start_group
->name
,
1336 key_file
->current_group
->name
) == 0
1337 && strcmp (key
, "Encoding") == 0)
1339 if (g_ascii_strcasecmp (value
, "UTF-8") != 0)
1341 gchar
*value_utf8
= g_utf8_make_valid (value
, value_len
);
1342 g_set_error (error
, G_KEY_FILE_ERROR
,
1343 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1344 _("Key file contains unsupported "
1345 "encoding “%s”"), value_utf8
);
1346 g_free (value_utf8
);
1354 /* Is this key a translation? If so, is it one that we care about?
1356 locale
= key_get_locale (key
);
1358 if (locale
== NULL
|| g_key_file_locale_is_interesting (key_file
, locale
))
1360 GKeyFileKeyValuePair
*pair
;
1362 pair
= g_slice_new (GKeyFileKeyValuePair
);
1364 pair
->value
= value
;
1366 g_key_file_add_key_value_pair (key_file
, key_file
->current_group
, pair
);
1378 key_get_locale (const gchar
*key
)
1382 locale
= g_strrstr (key
, "[");
1384 if (locale
&& strlen (locale
) <= 2)
1388 locale
= g_strndup (locale
+ 1, strlen (locale
) - 2);
1394 g_key_file_parse_data (GKeyFile
*key_file
,
1399 GError
*parse_error
;
1402 g_return_if_fail (key_file
!= NULL
);
1403 g_return_if_fail (data
!= NULL
|| length
== 0);
1410 if (data
[i
] == '\n')
1412 if (key_file
->parse_buffer
->len
> 0
1413 && (key_file
->parse_buffer
->str
[key_file
->parse_buffer
->len
- 1]
1415 g_string_erase (key_file
->parse_buffer
,
1416 key_file
->parse_buffer
->len
- 1,
1419 /* When a newline is encountered flush the parse buffer so that the
1420 * line can be parsed. Note that completely blank lines won't show
1421 * up in the parse buffer, so they get parsed directly.
1423 if (key_file
->parse_buffer
->len
> 0)
1424 g_key_file_flush_parse_buffer (key_file
, &parse_error
);
1426 g_key_file_parse_comment (key_file
, "", 1, &parse_error
);
1430 g_propagate_error (error
, parse_error
);
1437 const gchar
*start_of_line
;
1438 const gchar
*end_of_line
;
1441 start_of_line
= data
+ i
;
1442 end_of_line
= memchr (start_of_line
, '\n', length
- i
);
1444 if (end_of_line
== NULL
)
1445 end_of_line
= data
+ length
;
1447 line_length
= end_of_line
- start_of_line
;
1449 g_string_append_len (key_file
->parse_buffer
, start_of_line
, line_length
);
1456 g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
1459 GError
*file_error
= NULL
;
1461 g_return_if_fail (key_file
!= NULL
);
1465 if (key_file
->parse_buffer
->len
> 0)
1467 g_key_file_parse_line (key_file
, key_file
->parse_buffer
->str
,
1468 key_file
->parse_buffer
->len
,
1470 g_string_erase (key_file
->parse_buffer
, 0, -1);
1474 g_propagate_error (error
, file_error
);
1481 * g_key_file_to_data:
1482 * @key_file: a #GKeyFile
1483 * @length: (out) (optional): return location for the length of the
1484 * returned string, or %NULL
1485 * @error: return location for a #GError, or %NULL
1487 * This function outputs @key_file as a string.
1489 * Note that this function never reports an error,
1490 * so it is safe to pass %NULL as @error.
1492 * Returns: a newly allocated string holding
1493 * the contents of the #GKeyFile
1498 g_key_file_to_data (GKeyFile
*key_file
,
1502 GString
*data_string
;
1503 GList
*group_node
, *key_file_node
;
1505 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1507 data_string
= g_string_new (NULL
);
1509 for (group_node
= g_list_last (key_file
->groups
);
1511 group_node
= group_node
->prev
)
1513 GKeyFileGroup
*group
;
1515 group
= (GKeyFileGroup
*) group_node
->data
;
1517 /* separate groups by at least an empty line */
1518 if (data_string
->len
>= 2 &&
1519 data_string
->str
[data_string
->len
- 2] != '\n')
1520 g_string_append_c (data_string
, '\n');
1522 if (group
->comment
!= NULL
)
1523 g_string_append_printf (data_string
, "%s\n", group
->comment
->value
);
1525 if (group
->name
!= NULL
)
1526 g_string_append_printf (data_string
, "[%s]\n", group
->name
);
1528 for (key_file_node
= g_list_last (group
->key_value_pairs
);
1529 key_file_node
!= NULL
;
1530 key_file_node
= key_file_node
->prev
)
1532 GKeyFileKeyValuePair
*pair
;
1534 pair
= (GKeyFileKeyValuePair
*) key_file_node
->data
;
1536 if (pair
->key
!= NULL
)
1537 g_string_append_printf (data_string
, "%s=%s\n", pair
->key
, pair
->value
);
1539 g_string_append_printf (data_string
, "%s\n", pair
->value
);
1544 *length
= data_string
->len
;
1546 return g_string_free (data_string
, FALSE
);
1550 * g_key_file_get_keys:
1551 * @key_file: a #GKeyFile
1552 * @group_name: a group name
1553 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1554 * @error: return location for a #GError, or %NULL
1556 * Returns all keys for the group name @group_name. The array of
1557 * returned keys will be %NULL-terminated, so @length may
1558 * optionally be %NULL. In the event that the @group_name cannot
1559 * be found, %NULL is returned and @error is set to
1560 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1562 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1563 * Use g_strfreev() to free it.
1568 g_key_file_get_keys (GKeyFile
*key_file
,
1569 const gchar
*group_name
,
1573 GKeyFileGroup
*group
;
1578 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1579 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1581 group
= g_key_file_lookup_group (key_file
, group_name
);
1585 g_set_error (error
, G_KEY_FILE_ERROR
,
1586 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1587 _("Key file does not have group “%s”"),
1593 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1595 GKeyFileKeyValuePair
*pair
;
1597 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1603 keys
= g_new (gchar
*, num_keys
+ 1);
1606 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1608 GKeyFileKeyValuePair
*pair
;
1610 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1614 keys
[i
] = g_strdup (pair
->key
);
1619 keys
[num_keys
] = NULL
;
1628 * g_key_file_get_start_group:
1629 * @key_file: a #GKeyFile
1631 * Returns the name of the start group of the file.
1633 * Returns: The start group of the key file.
1638 g_key_file_get_start_group (GKeyFile
*key_file
)
1640 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1642 if (key_file
->start_group
)
1643 return g_strdup (key_file
->start_group
->name
);
1649 * g_key_file_get_groups:
1650 * @key_file: a #GKeyFile
1651 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1653 * Returns all groups in the key file loaded with @key_file.
1654 * The array of returned groups will be %NULL-terminated, so
1655 * @length may optionally be %NULL.
1657 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1658 * Use g_strfreev() to free it.
1662 g_key_file_get_groups (GKeyFile
*key_file
,
1667 gsize i
, num_groups
;
1669 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1671 num_groups
= g_list_length (key_file
->groups
);
1673 g_return_val_if_fail (num_groups
> 0, NULL
);
1675 group_node
= g_list_last (key_file
->groups
);
1677 g_return_val_if_fail (((GKeyFileGroup
*) group_node
->data
)->name
== NULL
, NULL
);
1679 /* Only need num_groups instead of num_groups + 1
1680 * because the first group of the file (last in the
1681 * list) is always the comment group at the top,
1684 groups
= g_new (gchar
*, num_groups
);
1688 for (group_node
= group_node
->prev
;
1690 group_node
= group_node
->prev
)
1692 GKeyFileGroup
*group
;
1694 group
= (GKeyFileGroup
*) group_node
->data
;
1696 g_warn_if_fail (group
->name
!= NULL
);
1698 groups
[i
++] = g_strdup (group
->name
);
1709 set_not_found_key_error (const char *group_name
,
1713 g_set_error (error
, G_KEY_FILE_ERROR
,
1714 G_KEY_FILE_ERROR_KEY_NOT_FOUND
,
1715 _("Key file does not have key “%s” in group “%s”"),
1720 * g_key_file_get_value:
1721 * @key_file: a #GKeyFile
1722 * @group_name: a group name
1724 * @error: return location for a #GError, or %NULL
1726 * Returns the raw value associated with @key under @group_name.
1727 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1729 * In the event the key cannot be found, %NULL is returned and
1730 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1731 * event that the @group_name cannot be found, %NULL is returned
1732 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1735 * Returns: a newly allocated string or %NULL if the specified
1736 * key cannot be found.
1741 g_key_file_get_value (GKeyFile
*key_file
,
1742 const gchar
*group_name
,
1746 GKeyFileGroup
*group
;
1747 GKeyFileKeyValuePair
*pair
;
1748 gchar
*value
= NULL
;
1750 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1751 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1752 g_return_val_if_fail (key
!= NULL
, NULL
);
1754 group
= g_key_file_lookup_group (key_file
, group_name
);
1758 g_set_error (error
, G_KEY_FILE_ERROR
,
1759 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1760 _("Key file does not have group “%s”"),
1765 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1768 value
= g_strdup (pair
->value
);
1770 set_not_found_key_error (group_name
, key
, error
);
1776 * g_key_file_set_value:
1777 * @key_file: a #GKeyFile
1778 * @group_name: a group name
1782 * Associates a new value with @key under @group_name.
1784 * If @key cannot be found then it is created. If @group_name cannot
1785 * be found then it is created. To set an UTF-8 string which may contain
1786 * characters that need escaping (such as newlines or spaces), use
1787 * g_key_file_set_string().
1792 g_key_file_set_value (GKeyFile
*key_file
,
1793 const gchar
*group_name
,
1797 GKeyFileGroup
*group
;
1798 GKeyFileKeyValuePair
*pair
;
1800 g_return_if_fail (key_file
!= NULL
);
1801 g_return_if_fail (g_key_file_is_group_name (group_name
));
1802 g_return_if_fail (g_key_file_is_key_name (key
));
1803 g_return_if_fail (value
!= NULL
);
1805 group
= g_key_file_lookup_group (key_file
, group_name
);
1809 g_key_file_add_group (key_file
, group_name
);
1810 group
= (GKeyFileGroup
*) key_file
->groups
->data
;
1812 g_key_file_add_key (key_file
, group
, key
, value
);
1816 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1819 g_key_file_add_key (key_file
, group
, key
, value
);
1822 g_free (pair
->value
);
1823 pair
->value
= g_strdup (value
);
1829 * g_key_file_get_string:
1830 * @key_file: a #GKeyFile
1831 * @group_name: a group name
1833 * @error: return location for a #GError, or %NULL
1835 * Returns the string value associated with @key under @group_name.
1836 * Unlike g_key_file_get_value(), this function handles escape sequences
1839 * In the event the key cannot be found, %NULL is returned and
1840 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1841 * event that the @group_name cannot be found, %NULL is returned
1842 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1844 * Returns: a newly allocated string or %NULL if the specified
1845 * key cannot be found.
1850 g_key_file_get_string (GKeyFile
*key_file
,
1851 const gchar
*group_name
,
1855 gchar
*value
, *string_value
;
1856 GError
*key_file_error
;
1858 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1859 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1860 g_return_val_if_fail (key
!= NULL
, NULL
);
1862 key_file_error
= NULL
;
1864 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1868 g_propagate_error (error
, key_file_error
);
1872 if (!g_utf8_validate (value
, -1, NULL
))
1874 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
1875 g_set_error (error
, G_KEY_FILE_ERROR
,
1876 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1877 _("Key file contains key “%s” with value “%s” "
1878 "which is not UTF-8"), key
, value_utf8
);
1879 g_free (value_utf8
);
1885 string_value
= g_key_file_parse_value_as_string (key_file
, value
, NULL
,
1891 if (g_error_matches (key_file_error
,
1893 G_KEY_FILE_ERROR_INVALID_VALUE
))
1895 g_set_error (error
, G_KEY_FILE_ERROR
,
1896 G_KEY_FILE_ERROR_INVALID_VALUE
,
1897 _("Key file contains key “%s” "
1898 "which has a value that cannot be interpreted."),
1900 g_error_free (key_file_error
);
1903 g_propagate_error (error
, key_file_error
);
1906 return string_value
;
1910 * g_key_file_set_string:
1911 * @key_file: a #GKeyFile
1912 * @group_name: a group name
1916 * Associates a new string value with @key under @group_name.
1917 * If @key cannot be found then it is created.
1918 * If @group_name cannot be found then it is created.
1919 * Unlike g_key_file_set_value(), this function handles characters
1920 * that need escaping, such as newlines.
1925 g_key_file_set_string (GKeyFile
*key_file
,
1926 const gchar
*group_name
,
1928 const gchar
*string
)
1932 g_return_if_fail (key_file
!= NULL
);
1933 g_return_if_fail (string
!= NULL
);
1935 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
1936 g_key_file_set_value (key_file
, group_name
, key
, value
);
1941 * g_key_file_get_string_list:
1942 * @key_file: a #GKeyFile
1943 * @group_name: a group name
1945 * @length: (out) (optional): return location for the number of returned strings, or %NULL
1946 * @error: return location for a #GError, or %NULL
1948 * Returns the values associated with @key under @group_name.
1950 * In the event the key cannot be found, %NULL is returned and
1951 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1952 * event that the @group_name cannot be found, %NULL is returned
1953 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1955 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1956 * a %NULL-terminated string array or %NULL if the specified
1957 * key cannot be found. The array should be freed with g_strfreev().
1962 g_key_file_get_string_list (GKeyFile
*key_file
,
1963 const gchar
*group_name
,
1968 GError
*key_file_error
= NULL
;
1969 gchar
*value
, *string_value
, **values
;
1971 GSList
*p
, *pieces
= NULL
;
1973 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1974 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1975 g_return_val_if_fail (key
!= NULL
, NULL
);
1980 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1984 g_propagate_error (error
, key_file_error
);
1988 if (!g_utf8_validate (value
, -1, NULL
))
1990 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
1991 g_set_error (error
, G_KEY_FILE_ERROR
,
1992 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1993 _("Key file contains key “%s” with value “%s” "
1994 "which is not UTF-8"), key
, value_utf8
);
1995 g_free (value_utf8
);
2001 string_value
= g_key_file_parse_value_as_string (key_file
, value
, &pieces
, &key_file_error
);
2003 g_free (string_value
);
2007 if (g_error_matches (key_file_error
,
2009 G_KEY_FILE_ERROR_INVALID_VALUE
))
2011 g_set_error (error
, G_KEY_FILE_ERROR
,
2012 G_KEY_FILE_ERROR_INVALID_VALUE
,
2013 _("Key file contains key “%s” "
2014 "which has a value that cannot be interpreted."),
2016 g_error_free (key_file_error
);
2019 g_propagate_error (error
, key_file_error
);
2021 g_slist_free_full (pieces
, g_free
);
2025 len
= g_slist_length (pieces
);
2026 values
= g_new (gchar
*, len
+ 1);
2027 for (p
= pieces
, i
= 0; p
; p
= p
->next
)
2028 values
[i
++] = p
->data
;
2031 g_slist_free (pieces
);
2040 * g_key_file_set_string_list:
2041 * @key_file: a #GKeyFile
2042 * @group_name: a group name
2044 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2045 * @length: number of string values in @list
2047 * Associates a list of string values for @key under @group_name.
2048 * If @key cannot be found then it is created.
2049 * If @group_name cannot be found then it is created.
2054 g_key_file_set_string_list (GKeyFile
*key_file
,
2055 const gchar
*group_name
,
2057 const gchar
* const list
[],
2060 GString
*value_list
;
2063 g_return_if_fail (key_file
!= NULL
);
2064 g_return_if_fail (list
!= NULL
|| length
== 0);
2066 value_list
= g_string_sized_new (length
* 128);
2067 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
2071 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
2072 g_string_append (value_list
, value
);
2073 g_string_append_c (value_list
, key_file
->list_separator
);
2078 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
2079 g_string_free (value_list
, TRUE
);
2083 * g_key_file_set_locale_string:
2084 * @key_file: a #GKeyFile
2085 * @group_name: a group name
2087 * @locale: a locale identifier
2090 * Associates a string value for @key and @locale under @group_name.
2091 * If the translation for @key cannot be found then it is created.
2096 g_key_file_set_locale_string (GKeyFile
*key_file
,
2097 const gchar
*group_name
,
2099 const gchar
*locale
,
2100 const gchar
*string
)
2102 gchar
*full_key
, *value
;
2104 g_return_if_fail (key_file
!= NULL
);
2105 g_return_if_fail (key
!= NULL
);
2106 g_return_if_fail (locale
!= NULL
);
2107 g_return_if_fail (string
!= NULL
);
2109 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
2110 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
2111 g_key_file_set_value (key_file
, group_name
, full_key
, value
);
2117 * g_key_file_get_locale_string:
2118 * @key_file: a #GKeyFile
2119 * @group_name: a group name
2121 * @locale: (nullable): a locale identifier or %NULL
2122 * @error: return location for a #GError, or %NULL
2124 * Returns the value associated with @key under @group_name
2125 * translated in the given @locale if available. If @locale is
2126 * %NULL then the current locale is assumed.
2128 * If @key cannot be found then %NULL is returned and @error is set
2129 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2130 * with @key cannot be interpreted or no suitable translation can
2131 * be found then the untranslated value is returned.
2133 * Returns: a newly allocated string or %NULL if the specified
2134 * key cannot be found.
2139 g_key_file_get_locale_string (GKeyFile
*key_file
,
2140 const gchar
*group_name
,
2142 const gchar
*locale
,
2145 gchar
*candidate_key
, *translated_value
;
2146 GError
*key_file_error
;
2148 gboolean free_languages
= FALSE
;
2151 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2152 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2153 g_return_val_if_fail (key
!= NULL
, NULL
);
2155 candidate_key
= NULL
;
2156 translated_value
= NULL
;
2157 key_file_error
= NULL
;
2161 languages
= g_get_locale_variants (locale
);
2162 free_languages
= TRUE
;
2166 languages
= (gchar
**) g_get_language_names ();
2167 free_languages
= FALSE
;
2170 for (i
= 0; languages
[i
]; i
++)
2172 candidate_key
= g_strdup_printf ("%s[%s]", key
, languages
[i
]);
2174 translated_value
= g_key_file_get_string (key_file
,
2176 candidate_key
, NULL
);
2177 g_free (candidate_key
);
2179 if (translated_value
)
2182 g_free (translated_value
);
2183 translated_value
= NULL
;
2186 /* Fallback to untranslated key
2188 if (!translated_value
)
2190 translated_value
= g_key_file_get_string (key_file
, group_name
, key
,
2193 if (!translated_value
)
2194 g_propagate_error (error
, key_file_error
);
2198 g_strfreev (languages
);
2200 return translated_value
;
2204 * g_key_file_get_locale_string_list:
2205 * @key_file: a #GKeyFile
2206 * @group_name: a group name
2208 * @locale: (nullable): a locale identifier or %NULL
2209 * @length: (out) (optional): return location for the number of returned strings or %NULL
2210 * @error: return location for a #GError or %NULL
2212 * Returns the values associated with @key under @group_name
2213 * translated in the given @locale if available. If @locale is
2214 * %NULL then the current locale is assumed.
2216 * If @key cannot be found then %NULL is returned and @error is set
2217 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2218 * with @key cannot be interpreted or no suitable translations
2219 * can be found then the untranslated values are returned. The
2220 * returned array is %NULL-terminated, so @length may optionally
2223 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2224 * or %NULL if the key isn't found. The string array should be freed
2225 * with g_strfreev().
2230 g_key_file_get_locale_string_list (GKeyFile
*key_file
,
2231 const gchar
*group_name
,
2233 const gchar
*locale
,
2237 GError
*key_file_error
;
2238 gchar
**values
, *value
;
2239 char list_separator
[2];
2242 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2243 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2244 g_return_val_if_fail (key
!= NULL
, NULL
);
2246 key_file_error
= NULL
;
2248 value
= g_key_file_get_locale_string (key_file
, group_name
,
2253 g_propagate_error (error
, key_file_error
);
2262 len
= strlen (value
);
2263 if (value
[len
- 1] == key_file
->list_separator
)
2264 value
[len
- 1] = '\0';
2266 list_separator
[0] = key_file
->list_separator
;
2267 list_separator
[1] = '\0';
2268 values
= g_strsplit (value
, list_separator
, 0);
2273 *length
= g_strv_length (values
);
2279 * g_key_file_set_locale_string_list:
2280 * @key_file: a #GKeyFile
2281 * @group_name: a group name
2283 * @locale: a locale identifier
2284 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2285 * @length: the length of @list
2287 * Associates a list of string values for @key and @locale under
2288 * @group_name. If the translation for @key cannot be found then
2294 g_key_file_set_locale_string_list (GKeyFile
*key_file
,
2295 const gchar
*group_name
,
2297 const gchar
*locale
,
2298 const gchar
* const list
[],
2301 GString
*value_list
;
2305 g_return_if_fail (key_file
!= NULL
);
2306 g_return_if_fail (key
!= NULL
);
2307 g_return_if_fail (locale
!= NULL
);
2308 g_return_if_fail (length
!= 0);
2310 value_list
= g_string_sized_new (length
* 128);
2311 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
2315 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
2316 g_string_append (value_list
, value
);
2317 g_string_append_c (value_list
, key_file
->list_separator
);
2322 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
2323 g_key_file_set_value (key_file
, group_name
, full_key
, value_list
->str
);
2325 g_string_free (value_list
, TRUE
);
2329 * g_key_file_get_boolean:
2330 * @key_file: a #GKeyFile
2331 * @group_name: a group name
2333 * @error: return location for a #GError
2335 * Returns the value associated with @key under @group_name as a
2338 * If @key cannot be found then %FALSE is returned and @error is set
2339 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2340 * associated with @key cannot be interpreted as a boolean then %FALSE
2341 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2343 * Returns: the value associated with the key as a boolean,
2344 * or %FALSE if the key was not found or could not be parsed.
2349 g_key_file_get_boolean (GKeyFile
*key_file
,
2350 const gchar
*group_name
,
2354 GError
*key_file_error
= NULL
;
2356 gboolean bool_value
;
2358 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
2359 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
2360 g_return_val_if_fail (key
!= NULL
, FALSE
);
2362 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2366 g_propagate_error (error
, key_file_error
);
2370 bool_value
= g_key_file_parse_value_as_boolean (key_file
, value
,
2376 if (g_error_matches (key_file_error
,
2378 G_KEY_FILE_ERROR_INVALID_VALUE
))
2380 g_set_error (error
, G_KEY_FILE_ERROR
,
2381 G_KEY_FILE_ERROR_INVALID_VALUE
,
2382 _("Key file contains key “%s” "
2383 "which has a value that cannot be interpreted."),
2385 g_error_free (key_file_error
);
2388 g_propagate_error (error
, key_file_error
);
2395 * g_key_file_set_boolean:
2396 * @key_file: a #GKeyFile
2397 * @group_name: a group name
2399 * @value: %TRUE or %FALSE
2401 * Associates a new boolean value with @key under @group_name.
2402 * If @key cannot be found then it is created.
2407 g_key_file_set_boolean (GKeyFile
*key_file
,
2408 const gchar
*group_name
,
2414 g_return_if_fail (key_file
!= NULL
);
2416 result
= g_key_file_parse_boolean_as_value (key_file
, value
);
2417 g_key_file_set_value (key_file
, group_name
, key
, result
);
2422 * g_key_file_get_boolean_list:
2423 * @key_file: a #GKeyFile
2424 * @group_name: a group name
2426 * @length: (out): the number of booleans returned
2427 * @error: return location for a #GError
2429 * Returns the values associated with @key under @group_name as
2432 * If @key cannot be found then %NULL is returned and @error is set to
2433 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2434 * with @key cannot be interpreted as booleans then %NULL is returned
2435 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2437 * Returns: (array length=length) (element-type gboolean) (transfer container):
2438 * the values associated with the key as a list of booleans, or %NULL if the
2439 * key was not found or could not be parsed. The returned list of booleans
2440 * should be freed with g_free() when no longer needed.
2445 g_key_file_get_boolean_list (GKeyFile
*key_file
,
2446 const gchar
*group_name
,
2451 GError
*key_file_error
;
2453 gboolean
*bool_values
;
2456 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2457 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2458 g_return_val_if_fail (key
!= NULL
, NULL
);
2463 key_file_error
= NULL
;
2465 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2466 &num_bools
, &key_file_error
);
2469 g_propagate_error (error
, key_file_error
);
2474 bool_values
= g_new (gboolean
, num_bools
);
2476 for (i
= 0; i
< num_bools
; i
++)
2478 bool_values
[i
] = g_key_file_parse_value_as_boolean (key_file
,
2484 g_propagate_error (error
, key_file_error
);
2485 g_strfreev (values
);
2486 g_free (bool_values
);
2491 g_strfreev (values
);
2494 *length
= num_bools
;
2500 * g_key_file_set_boolean_list:
2501 * @key_file: a #GKeyFile
2502 * @group_name: a group name
2504 * @list: (array length=length): an array of boolean values
2505 * @length: length of @list
2507 * Associates a list of boolean values with @key under @group_name.
2508 * If @key cannot be found then it is created.
2509 * If @group_name is %NULL, the start_group is used.
2514 g_key_file_set_boolean_list (GKeyFile
*key_file
,
2515 const gchar
*group_name
,
2520 GString
*value_list
;
2523 g_return_if_fail (key_file
!= NULL
);
2524 g_return_if_fail (list
!= NULL
);
2526 value_list
= g_string_sized_new (length
* 8);
2527 for (i
= 0; i
< length
; i
++)
2531 value
= g_key_file_parse_boolean_as_value (key_file
, list
[i
]);
2533 g_string_append (value_list
, value
);
2534 g_string_append_c (value_list
, key_file
->list_separator
);
2539 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
2540 g_string_free (value_list
, TRUE
);
2544 * g_key_file_get_integer:
2545 * @key_file: a #GKeyFile
2546 * @group_name: a group name
2548 * @error: return location for a #GError
2550 * Returns the value associated with @key under @group_name as an
2553 * If @key cannot be found then 0 is returned and @error is set to
2554 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2555 * with @key cannot be interpreted as an integer, or is out of range
2556 * for a #gint, then 0 is returned
2557 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2559 * Returns: the value associated with the key as an integer, or
2560 * 0 if the key was not found or could not be parsed.
2565 g_key_file_get_integer (GKeyFile
*key_file
,
2566 const gchar
*group_name
,
2570 GError
*key_file_error
;
2574 g_return_val_if_fail (key_file
!= NULL
, -1);
2575 g_return_val_if_fail (group_name
!= NULL
, -1);
2576 g_return_val_if_fail (key
!= NULL
, -1);
2578 key_file_error
= NULL
;
2580 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2584 g_propagate_error (error
, key_file_error
);
2588 int_value
= g_key_file_parse_value_as_integer (key_file
, value
,
2594 if (g_error_matches (key_file_error
,
2596 G_KEY_FILE_ERROR_INVALID_VALUE
))
2598 g_set_error (error
, G_KEY_FILE_ERROR
,
2599 G_KEY_FILE_ERROR_INVALID_VALUE
,
2600 _("Key file contains key “%s” in group “%s” "
2601 "which has a value that cannot be interpreted."),
2603 g_error_free (key_file_error
);
2606 g_propagate_error (error
, key_file_error
);
2613 * g_key_file_set_integer:
2614 * @key_file: a #GKeyFile
2615 * @group_name: a group name
2617 * @value: an integer value
2619 * Associates a new integer value with @key under @group_name.
2620 * If @key cannot be found then it is created.
2625 g_key_file_set_integer (GKeyFile
*key_file
,
2626 const gchar
*group_name
,
2632 g_return_if_fail (key_file
!= NULL
);
2634 result
= g_key_file_parse_integer_as_value (key_file
, value
);
2635 g_key_file_set_value (key_file
, group_name
, key
, result
);
2640 * g_key_file_get_int64:
2641 * @key_file: a non-%NULL #GKeyFile
2642 * @group_name: a non-%NULL group name
2643 * @key: a non-%NULL key
2644 * @error: return location for a #GError
2646 * Returns the value associated with @key under @group_name as a signed
2647 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2648 * 64-bit results without truncation.
2650 * Returns: the value associated with the key as a signed 64-bit integer, or
2651 * 0 if the key was not found or could not be parsed.
2656 g_key_file_get_int64 (GKeyFile
*key_file
,
2657 const gchar
*group_name
,
2664 g_return_val_if_fail (key_file
!= NULL
, -1);
2665 g_return_val_if_fail (group_name
!= NULL
, -1);
2666 g_return_val_if_fail (key
!= NULL
, -1);
2668 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2673 v
= g_ascii_strtoll (s
, &end
, 10);
2675 if (*s
== '\0' || *end
!= '\0')
2677 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2678 _("Key “%s” in group “%s” has value “%s” "
2679 "where %s was expected"),
2680 key
, group_name
, s
, "int64");
2690 * g_key_file_set_int64:
2691 * @key_file: a #GKeyFile
2692 * @group_name: a group name
2694 * @value: an integer value
2696 * Associates a new integer value with @key under @group_name.
2697 * If @key cannot be found then it is created.
2702 g_key_file_set_int64 (GKeyFile
*key_file
,
2703 const gchar
*group_name
,
2709 g_return_if_fail (key_file
!= NULL
);
2711 result
= g_strdup_printf ("%" G_GINT64_FORMAT
, value
);
2712 g_key_file_set_value (key_file
, group_name
, key
, result
);
2717 * g_key_file_get_uint64:
2718 * @key_file: a non-%NULL #GKeyFile
2719 * @group_name: a non-%NULL group name
2720 * @key: a non-%NULL key
2721 * @error: return location for a #GError
2723 * Returns the value associated with @key under @group_name as an unsigned
2724 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2725 * large positive results without truncation.
2727 * Returns: the value associated with the key as an unsigned 64-bit integer,
2728 * or 0 if the key was not found or could not be parsed.
2733 g_key_file_get_uint64 (GKeyFile
*key_file
,
2734 const gchar
*group_name
,
2741 g_return_val_if_fail (key_file
!= NULL
, -1);
2742 g_return_val_if_fail (group_name
!= NULL
, -1);
2743 g_return_val_if_fail (key
!= NULL
, -1);
2745 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2750 v
= g_ascii_strtoull (s
, &end
, 10);
2752 if (*s
== '\0' || *end
!= '\0')
2754 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2755 _("Key “%s” in group “%s” has value “%s” "
2756 "where %s was expected"),
2757 key
, group_name
, s
, "uint64");
2767 * g_key_file_set_uint64:
2768 * @key_file: a #GKeyFile
2769 * @group_name: a group name
2771 * @value: an integer value
2773 * Associates a new integer value with @key under @group_name.
2774 * If @key cannot be found then it is created.
2779 g_key_file_set_uint64 (GKeyFile
*key_file
,
2780 const gchar
*group_name
,
2786 g_return_if_fail (key_file
!= NULL
);
2788 result
= g_strdup_printf ("%" G_GUINT64_FORMAT
, value
);
2789 g_key_file_set_value (key_file
, group_name
, key
, result
);
2794 * g_key_file_get_integer_list:
2795 * @key_file: a #GKeyFile
2796 * @group_name: a group name
2798 * @length: (out): the number of integers returned
2799 * @error: return location for a #GError
2801 * Returns the values associated with @key under @group_name as
2804 * If @key cannot be found then %NULL is returned and @error is set to
2805 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2806 * with @key cannot be interpreted as integers, or are out of range for
2807 * #gint, then %NULL is returned
2808 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2810 * Returns: (array length=length) (element-type gint) (transfer container):
2811 * the values associated with the key as a list of integers, or %NULL if
2812 * the key was not found or could not be parsed. The returned list of
2813 * integers should be freed with g_free() when no longer needed.
2818 g_key_file_get_integer_list (GKeyFile
*key_file
,
2819 const gchar
*group_name
,
2824 GError
*key_file_error
= NULL
;
2829 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2830 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2831 g_return_val_if_fail (key
!= NULL
, NULL
);
2836 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2837 &num_ints
, &key_file_error
);
2840 g_propagate_error (error
, key_file_error
);
2845 int_values
= g_new (gint
, num_ints
);
2847 for (i
= 0; i
< num_ints
; i
++)
2849 int_values
[i
] = g_key_file_parse_value_as_integer (key_file
,
2855 g_propagate_error (error
, key_file_error
);
2856 g_strfreev (values
);
2857 g_free (int_values
);
2862 g_strfreev (values
);
2871 * g_key_file_set_integer_list:
2872 * @key_file: a #GKeyFile
2873 * @group_name: a group name
2875 * @list: (array length=length): an array of integer values
2876 * @length: number of integer values in @list
2878 * Associates a list of integer values with @key under @group_name.
2879 * If @key cannot be found then it is created.
2884 g_key_file_set_integer_list (GKeyFile
*key_file
,
2885 const gchar
*group_name
,
2893 g_return_if_fail (key_file
!= NULL
);
2894 g_return_if_fail (list
!= NULL
);
2896 values
= g_string_sized_new (length
* 16);
2897 for (i
= 0; i
< length
; i
++)
2901 value
= g_key_file_parse_integer_as_value (key_file
, list
[i
]);
2903 g_string_append (values
, value
);
2904 g_string_append_c (values
, key_file
->list_separator
);
2909 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
2910 g_string_free (values
, TRUE
);
2914 * g_key_file_get_double:
2915 * @key_file: a #GKeyFile
2916 * @group_name: a group name
2918 * @error: return location for a #GError
2920 * Returns the value associated with @key under @group_name as a
2921 * double. If @group_name is %NULL, the start_group is used.
2923 * If @key cannot be found then 0.0 is returned and @error is set to
2924 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2925 * with @key cannot be interpreted as a double then 0.0 is returned
2926 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2928 * Returns: the value associated with the key as a double, or
2929 * 0.0 if the key was not found or could not be parsed.
2934 g_key_file_get_double (GKeyFile
*key_file
,
2935 const gchar
*group_name
,
2939 GError
*key_file_error
;
2941 gdouble double_value
;
2943 g_return_val_if_fail (key_file
!= NULL
, -1);
2944 g_return_val_if_fail (group_name
!= NULL
, -1);
2945 g_return_val_if_fail (key
!= NULL
, -1);
2947 key_file_error
= NULL
;
2949 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2953 g_propagate_error (error
, key_file_error
);
2957 double_value
= g_key_file_parse_value_as_double (key_file
, value
,
2963 if (g_error_matches (key_file_error
,
2965 G_KEY_FILE_ERROR_INVALID_VALUE
))
2967 g_set_error (error
, G_KEY_FILE_ERROR
,
2968 G_KEY_FILE_ERROR_INVALID_VALUE
,
2969 _("Key file contains key “%s” in group “%s” "
2970 "which has a value that cannot be interpreted."),
2972 g_error_free (key_file_error
);
2975 g_propagate_error (error
, key_file_error
);
2978 return double_value
;
2982 * g_key_file_set_double:
2983 * @key_file: a #GKeyFile
2984 * @group_name: a group name
2986 * @value: an double value
2988 * Associates a new double value with @key under @group_name.
2989 * If @key cannot be found then it is created.
2994 g_key_file_set_double (GKeyFile
*key_file
,
2995 const gchar
*group_name
,
2999 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
3001 g_return_if_fail (key_file
!= NULL
);
3003 g_ascii_dtostr (result
, sizeof (result
), value
);
3004 g_key_file_set_value (key_file
, group_name
, key
, result
);
3008 * g_key_file_get_double_list:
3009 * @key_file: a #GKeyFile
3010 * @group_name: a group name
3012 * @length: (out): the number of doubles returned
3013 * @error: return location for a #GError
3015 * Returns the values associated with @key under @group_name as
3018 * If @key cannot be found then %NULL is returned and @error is set to
3019 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3020 * with @key cannot be interpreted as doubles then %NULL is returned
3021 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3023 * Returns: (array length=length) (element-type gdouble) (transfer container):
3024 * the values associated with the key as a list of doubles, or %NULL if the
3025 * key was not found or could not be parsed. The returned list of doubles
3026 * should be freed with g_free() when no longer needed.
3031 g_key_file_get_double_list (GKeyFile
*key_file
,
3032 const gchar
*group_name
,
3037 GError
*key_file_error
= NULL
;
3039 gdouble
*double_values
;
3040 gsize i
, num_doubles
;
3042 g_return_val_if_fail (key_file
!= NULL
, NULL
);
3043 g_return_val_if_fail (group_name
!= NULL
, NULL
);
3044 g_return_val_if_fail (key
!= NULL
, NULL
);
3049 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
3050 &num_doubles
, &key_file_error
);
3053 g_propagate_error (error
, key_file_error
);
3058 double_values
= g_new (gdouble
, num_doubles
);
3060 for (i
= 0; i
< num_doubles
; i
++)
3062 double_values
[i
] = g_key_file_parse_value_as_double (key_file
,
3068 g_propagate_error (error
, key_file_error
);
3069 g_strfreev (values
);
3070 g_free (double_values
);
3075 g_strfreev (values
);
3078 *length
= num_doubles
;
3080 return double_values
;
3084 * g_key_file_set_double_list:
3085 * @key_file: a #GKeyFile
3086 * @group_name: a group name
3088 * @list: (array length=length): an array of double values
3089 * @length: number of double values in @list
3091 * Associates a list of double values with @key under
3092 * @group_name. If @key cannot be found then it is created.
3097 g_key_file_set_double_list (GKeyFile
*key_file
,
3098 const gchar
*group_name
,
3106 g_return_if_fail (key_file
!= NULL
);
3107 g_return_if_fail (list
!= NULL
);
3109 values
= g_string_sized_new (length
* 16);
3110 for (i
= 0; i
< length
; i
++)
3112 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
3114 g_ascii_dtostr( result
, sizeof (result
), list
[i
] );
3116 g_string_append (values
, result
);
3117 g_string_append_c (values
, key_file
->list_separator
);
3120 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
3121 g_string_free (values
, TRUE
);
3125 g_key_file_set_key_comment (GKeyFile
*key_file
,
3126 const gchar
*group_name
,
3128 const gchar
*comment
,
3131 GKeyFileGroup
*group
;
3132 GKeyFileKeyValuePair
*pair
;
3133 GList
*key_node
, *comment_node
, *tmp
;
3135 group
= g_key_file_lookup_group (key_file
, group_name
);
3138 g_set_error (error
, G_KEY_FILE_ERROR
,
3139 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3140 _("Key file does not have group “%s”"),
3141 group_name
? group_name
: "(null)");
3146 /* First find the key the comments are supposed to be
3149 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
3151 if (key_node
== NULL
)
3153 set_not_found_key_error (group
->name
, key
, error
);
3157 /* Then find all the comments already associated with the
3160 tmp
= key_node
->next
;
3163 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3165 if (pair
->key
!= NULL
)
3170 g_key_file_remove_key_value_pair_node (key_file
, group
,
3174 if (comment
== NULL
)
3177 /* Now we can add our new comment
3179 pair
= g_slice_new (GKeyFileKeyValuePair
);
3181 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3183 key_node
= g_list_insert (key_node
, pair
, 1);
3189 g_key_file_set_group_comment (GKeyFile
*key_file
,
3190 const gchar
*group_name
,
3191 const gchar
*comment
,
3194 GKeyFileGroup
*group
;
3196 g_return_val_if_fail (g_key_file_is_group_name (group_name
), FALSE
);
3198 group
= g_key_file_lookup_group (key_file
, group_name
);
3201 g_set_error (error
, G_KEY_FILE_ERROR
,
3202 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3203 _("Key file does not have group “%s”"),
3204 group_name
? group_name
: "(null)");
3209 /* First remove any existing comment
3213 g_key_file_key_value_pair_free (group
->comment
);
3214 group
->comment
= NULL
;
3217 if (comment
== NULL
)
3220 /* Now we can add our new comment
3222 group
->comment
= g_slice_new (GKeyFileKeyValuePair
);
3223 group
->comment
->key
= NULL
;
3224 group
->comment
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3230 g_key_file_set_top_comment (GKeyFile
*key_file
,
3231 const gchar
*comment
,
3235 GKeyFileGroup
*group
;
3236 GKeyFileKeyValuePair
*pair
;
3238 /* The last group in the list should be the top (comments only)
3241 g_warn_if_fail (key_file
->groups
!= NULL
);
3242 group_node
= g_list_last (key_file
->groups
);
3243 group
= (GKeyFileGroup
*) group_node
->data
;
3244 g_warn_if_fail (group
->name
== NULL
);
3246 /* Note all keys must be comments at the top of
3247 * the file, so we can just free it all.
3249 g_list_free_full (group
->key_value_pairs
, (GDestroyNotify
) g_key_file_key_value_pair_free
);
3250 group
->key_value_pairs
= NULL
;
3252 if (comment
== NULL
)
3255 pair
= g_slice_new (GKeyFileKeyValuePair
);
3257 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3259 group
->key_value_pairs
=
3260 g_list_prepend (group
->key_value_pairs
, pair
);
3266 * g_key_file_set_comment:
3267 * @key_file: a #GKeyFile
3268 * @group_name: (nullable): a group name, or %NULL
3269 * @key: (nullable): a key
3270 * @comment: a comment
3271 * @error: return location for a #GError
3273 * Places a comment above @key from @group_name.
3275 * If @key is %NULL then @comment will be written above @group_name.
3276 * If both @key and @group_name are %NULL, then @comment will be
3277 * written above the first group in the file.
3279 * Note that this function prepends a '#' comment marker to
3280 * each line of @comment.
3282 * Returns: %TRUE if the comment was written, %FALSE otherwise
3287 g_key_file_set_comment (GKeyFile
*key_file
,
3288 const gchar
*group_name
,
3290 const gchar
*comment
,
3293 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3295 if (group_name
!= NULL
&& key
!= NULL
)
3297 if (!g_key_file_set_key_comment (key_file
, group_name
, key
, comment
, error
))
3300 else if (group_name
!= NULL
)
3302 if (!g_key_file_set_group_comment (key_file
, group_name
, comment
, error
))
3307 if (!g_key_file_set_top_comment (key_file
, comment
, error
))
3315 g_key_file_get_key_comment (GKeyFile
*key_file
,
3316 const gchar
*group_name
,
3320 GKeyFileGroup
*group
;
3321 GKeyFileKeyValuePair
*pair
;
3322 GList
*key_node
, *tmp
;
3326 g_return_val_if_fail (g_key_file_is_group_name (group_name
), NULL
);
3328 group
= g_key_file_lookup_group (key_file
, group_name
);
3331 g_set_error (error
, G_KEY_FILE_ERROR
,
3332 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3333 _("Key file does not have group “%s”"),
3334 group_name
? group_name
: "(null)");
3339 /* First find the key the comments are supposed to be
3342 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
3344 if (key_node
== NULL
)
3346 set_not_found_key_error (group
->name
, key
, error
);
3352 /* Then find all the comments already associated with the
3353 * key and concatentate them.
3355 tmp
= key_node
->next
;
3356 if (!key_node
->next
)
3359 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3360 if (pair
->key
!= NULL
)
3365 pair
= (GKeyFileKeyValuePair
*) tmp
->next
->data
;
3367 if (pair
->key
!= NULL
)
3373 while (tmp
!= key_node
)
3375 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3378 string
= g_string_sized_new (512);
3380 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
3381 g_string_append (string
, comment
);
3389 comment
= string
->str
;
3390 g_string_free (string
, FALSE
);
3399 get_group_comment (GKeyFile
*key_file
,
3400 GKeyFileGroup
*group
,
3409 tmp
= group
->key_value_pairs
;
3412 GKeyFileKeyValuePair
*pair
;
3414 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3416 if (pair
->key
!= NULL
)
3422 if (tmp
->next
== NULL
)
3430 GKeyFileKeyValuePair
*pair
;
3432 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3435 string
= g_string_sized_new (512);
3437 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
3438 g_string_append (string
, comment
);
3445 return g_string_free (string
, FALSE
);
3451 g_key_file_get_group_comment (GKeyFile
*key_file
,
3452 const gchar
*group_name
,
3456 GKeyFileGroup
*group
;
3458 group
= g_key_file_lookup_group (key_file
, group_name
);
3461 g_set_error (error
, G_KEY_FILE_ERROR
,
3462 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3463 _("Key file does not have group “%s”"),
3464 group_name
? group_name
: "(null)");
3470 return g_strdup (group
->comment
->value
);
3472 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3473 group_node
= group_node
->next
;
3474 group
= (GKeyFileGroup
*)group_node
->data
;
3475 return get_group_comment (key_file
, group
, error
);
3479 g_key_file_get_top_comment (GKeyFile
*key_file
,
3483 GKeyFileGroup
*group
;
3485 /* The last group in the list should be the top (comments only)
3488 g_warn_if_fail (key_file
->groups
!= NULL
);
3489 group_node
= g_list_last (key_file
->groups
);
3490 group
= (GKeyFileGroup
*) group_node
->data
;
3491 g_warn_if_fail (group
->name
== NULL
);
3493 return get_group_comment (key_file
, group
, error
);
3497 * g_key_file_get_comment:
3498 * @key_file: a #GKeyFile
3499 * @group_name: (nullable): a group name, or %NULL
3501 * @error: return location for a #GError
3503 * Retrieves a comment above @key from @group_name.
3504 * If @key is %NULL then @comment will be read from above
3505 * @group_name. If both @key and @group_name are %NULL, then
3506 * @comment will be read from above the first group in the file.
3508 * Note that the returned string includes the '#' comment markers.
3510 * Returns: a comment that should be freed with g_free()
3515 g_key_file_get_comment (GKeyFile
*key_file
,
3516 const gchar
*group_name
,
3520 g_return_val_if_fail (key_file
!= NULL
, NULL
);
3522 if (group_name
!= NULL
&& key
!= NULL
)
3523 return g_key_file_get_key_comment (key_file
, group_name
, key
, error
);
3524 else if (group_name
!= NULL
)
3525 return g_key_file_get_group_comment (key_file
, group_name
, error
);
3527 return g_key_file_get_top_comment (key_file
, error
);
3531 * g_key_file_remove_comment:
3532 * @key_file: a #GKeyFile
3533 * @group_name: (nullable): a group name, or %NULL
3534 * @key: (nullable): a key
3535 * @error: return location for a #GError
3537 * Removes a comment above @key from @group_name.
3538 * If @key is %NULL then @comment will be removed above @group_name.
3539 * If both @key and @group_name are %NULL, then @comment will
3540 * be removed above the first group in the file.
3542 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3548 g_key_file_remove_comment (GKeyFile
*key_file
,
3549 const gchar
*group_name
,
3553 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3555 if (group_name
!= NULL
&& key
!= NULL
)
3556 return g_key_file_set_key_comment (key_file
, group_name
, key
, NULL
, error
);
3557 else if (group_name
!= NULL
)
3558 return g_key_file_set_group_comment (key_file
, group_name
, NULL
, error
);
3560 return g_key_file_set_top_comment (key_file
, NULL
, error
);
3564 * g_key_file_has_group:
3565 * @key_file: a #GKeyFile
3566 * @group_name: a group name
3568 * Looks whether the key file has the group @group_name.
3570 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3575 g_key_file_has_group (GKeyFile
*key_file
,
3576 const gchar
*group_name
)
3578 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3579 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3581 return g_key_file_lookup_group (key_file
, group_name
) != NULL
;
3584 /* This code remains from a historical attempt to add a new public API
3585 * which respects the GError rules.
3588 g_key_file_has_key_full (GKeyFile
*key_file
,
3589 const gchar
*group_name
,
3594 GKeyFileKeyValuePair
*pair
;
3595 GKeyFileGroup
*group
;
3597 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3598 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3599 g_return_val_if_fail (key
!= NULL
, FALSE
);
3601 group
= g_key_file_lookup_group (key_file
, group_name
);
3605 g_set_error (error
, G_KEY_FILE_ERROR
,
3606 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3607 _("Key file does not have group “%s”"),
3613 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3616 *has_key
= pair
!= NULL
;
3621 * g_key_file_has_key: (skip)
3622 * @key_file: a #GKeyFile
3623 * @group_name: a group name
3625 * @error: return location for a #GError
3627 * Looks whether the key file has the key @key in the group
3630 * Note that this function does not follow the rules for #GError strictly;
3631 * the return value both carries meaning and signals an error. To use
3632 * this function, you must pass a #GError pointer in @error, and check
3633 * whether it is not %NULL to see if an error occurred.
3635 * Language bindings should use g_key_file_get_value() to test whether
3636 * or not a key exists.
3638 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3643 g_key_file_has_key (GKeyFile
*key_file
,
3644 const gchar
*group_name
,
3648 GError
*temp_error
= NULL
;
3651 if (g_key_file_has_key_full (key_file
, group_name
, key
, &has_key
, &temp_error
))
3657 g_propagate_error (error
, temp_error
);
3663 g_key_file_add_group (GKeyFile
*key_file
,
3664 const gchar
*group_name
)
3666 GKeyFileGroup
*group
;
3668 g_return_if_fail (key_file
!= NULL
);
3669 g_return_if_fail (g_key_file_is_group_name (group_name
));
3671 group
= g_key_file_lookup_group (key_file
, group_name
);
3674 key_file
->current_group
= group
;
3678 group
= g_slice_new0 (GKeyFileGroup
);
3679 group
->name
= g_strdup (group_name
);
3680 group
->lookup_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
3681 key_file
->groups
= g_list_prepend (key_file
->groups
, group
);
3682 key_file
->current_group
= group
;
3684 if (key_file
->start_group
== NULL
)
3685 key_file
->start_group
= group
;
3687 g_hash_table_insert (key_file
->group_hash
, (gpointer
)group
->name
, group
);
3691 g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
)
3696 g_free (pair
->value
);
3697 g_slice_free (GKeyFileKeyValuePair
, pair
);
3701 /* Be careful not to call this function on a node with data in the
3702 * lookup map without removing it from the lookup map, first.
3704 * Some current cases where this warning is not a concern are
3706 * - the node being removed is a comment node
3707 * - the entire lookup map is getting destroyed soon after
3711 g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
3712 GKeyFileGroup
*group
,
3716 GKeyFileKeyValuePair
*pair
;
3718 pair
= (GKeyFileKeyValuePair
*) pair_node
->data
;
3720 group
->key_value_pairs
= g_list_remove_link (group
->key_value_pairs
, pair_node
);
3722 g_warn_if_fail (pair
->value
!= NULL
);
3724 g_key_file_key_value_pair_free (pair
);
3726 g_list_free_1 (pair_node
);
3730 g_key_file_remove_group_node (GKeyFile
*key_file
,
3733 GKeyFileGroup
*group
;
3736 group
= (GKeyFileGroup
*) group_node
->data
;
3739 g_hash_table_remove (key_file
->group_hash
, group
->name
);
3741 /* If the current group gets deleted make the current group the last
3744 if (key_file
->current_group
== group
)
3746 /* groups should always contain at least the top comment group,
3747 * unless g_key_file_clear has been called
3749 if (key_file
->groups
)
3750 key_file
->current_group
= (GKeyFileGroup
*) key_file
->groups
->data
;
3752 key_file
->current_group
= NULL
;
3755 /* If the start group gets deleted make the start group the first
3758 if (key_file
->start_group
== group
)
3760 tmp
= g_list_last (key_file
->groups
);
3763 if (tmp
!= group_node
&&
3764 ((GKeyFileGroup
*) tmp
->data
)->name
!= NULL
)
3771 key_file
->start_group
= (GKeyFileGroup
*) tmp
->data
;
3773 key_file
->start_group
= NULL
;
3776 key_file
->groups
= g_list_remove_link (key_file
->groups
, group_node
);
3778 tmp
= group
->key_value_pairs
;
3785 g_key_file_remove_key_value_pair_node (key_file
, group
, pair_node
);
3788 g_warn_if_fail (group
->key_value_pairs
== NULL
);
3792 g_key_file_key_value_pair_free (group
->comment
);
3793 group
->comment
= NULL
;
3796 if (group
->lookup_map
)
3798 g_hash_table_destroy (group
->lookup_map
);
3799 group
->lookup_map
= NULL
;
3802 g_free ((gchar
*) group
->name
);
3803 g_slice_free (GKeyFileGroup
, group
);
3804 g_list_free_1 (group_node
);
3808 * g_key_file_remove_group:
3809 * @key_file: a #GKeyFile
3810 * @group_name: a group name
3811 * @error: return location for a #GError or %NULL
3813 * Removes the specified group, @group_name,
3814 * from the key file.
3816 * Returns: %TRUE if the group was removed, %FALSE otherwise
3821 g_key_file_remove_group (GKeyFile
*key_file
,
3822 const gchar
*group_name
,
3827 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3828 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3830 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3834 g_set_error (error
, G_KEY_FILE_ERROR
,
3835 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3836 _("Key file does not have group “%s”"),
3841 g_key_file_remove_group_node (key_file
, group_node
);
3847 g_key_file_add_key_value_pair (GKeyFile
*key_file
,
3848 GKeyFileGroup
*group
,
3849 GKeyFileKeyValuePair
*pair
)
3851 g_hash_table_replace (group
->lookup_map
, pair
->key
, pair
);
3852 group
->key_value_pairs
= g_list_prepend (group
->key_value_pairs
, pair
);
3856 g_key_file_add_key (GKeyFile
*key_file
,
3857 GKeyFileGroup
*group
,
3861 GKeyFileKeyValuePair
*pair
;
3863 pair
= g_slice_new (GKeyFileKeyValuePair
);
3864 pair
->key
= g_strdup (key
);
3865 pair
->value
= g_strdup (value
);
3867 g_key_file_add_key_value_pair (key_file
, group
, pair
);
3871 * g_key_file_remove_key:
3872 * @key_file: a #GKeyFile
3873 * @group_name: a group name
3874 * @key: a key name to remove
3875 * @error: return location for a #GError or %NULL
3877 * Removes @key in @group_name from the key file.
3879 * Returns: %TRUE if the key was removed, %FALSE otherwise
3884 g_key_file_remove_key (GKeyFile
*key_file
,
3885 const gchar
*group_name
,
3889 GKeyFileGroup
*group
;
3890 GKeyFileKeyValuePair
*pair
;
3892 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3893 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3894 g_return_val_if_fail (key
!= NULL
, FALSE
);
3898 group
= g_key_file_lookup_group (key_file
, group_name
);
3901 g_set_error (error
, G_KEY_FILE_ERROR
,
3902 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3903 _("Key file does not have group “%s”"),
3908 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3912 set_not_found_key_error (group
->name
, key
, error
);
3916 group
->key_value_pairs
= g_list_remove (group
->key_value_pairs
, pair
);
3917 g_hash_table_remove (group
->lookup_map
, pair
->key
);
3918 g_key_file_key_value_pair_free (pair
);
3924 g_key_file_lookup_group_node (GKeyFile
*key_file
,
3925 const gchar
*group_name
)
3927 GKeyFileGroup
*group
;
3930 for (tmp
= key_file
->groups
; tmp
!= NULL
; tmp
= tmp
->next
)
3932 group
= (GKeyFileGroup
*) tmp
->data
;
3934 if (group
&& group
->name
&& strcmp (group
->name
, group_name
) == 0)
3941 static GKeyFileGroup
*
3942 g_key_file_lookup_group (GKeyFile
*key_file
,
3943 const gchar
*group_name
)
3945 return (GKeyFileGroup
*)g_hash_table_lookup (key_file
->group_hash
, group_name
);
3949 g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
3950 GKeyFileGroup
*group
,
3955 for (key_node
= group
->key_value_pairs
;
3957 key_node
= key_node
->next
)
3959 GKeyFileKeyValuePair
*pair
;
3961 pair
= (GKeyFileKeyValuePair
*) key_node
->data
;
3963 if (pair
->key
&& strcmp (pair
->key
, key
) == 0)
3970 static GKeyFileKeyValuePair
*
3971 g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
3972 GKeyFileGroup
*group
,
3975 return (GKeyFileKeyValuePair
*) g_hash_table_lookup (group
->lookup_map
, key
);
3978 /* Lines starting with # or consisting entirely of whitespace are merely
3979 * recorded, not parsed. This function assumes all leading whitespace
3980 * has been stripped.
3983 g_key_file_line_is_comment (const gchar
*line
)
3985 return (*line
== '#' || *line
== '\0' || *line
== '\n');
3989 g_key_file_is_group_name (const gchar
*name
)
3996 p
= q
= (gchar
*) name
;
3997 while (*q
&& *q
!= ']' && *q
!= '[' && !g_ascii_iscntrl (*q
))
3998 q
= g_utf8_find_next_char (q
, NULL
);
4000 if (*q
!= '\0' || q
== p
)
4007 g_key_file_is_key_name (const gchar
*name
)
4014 p
= q
= (gchar
*) name
;
4015 /* We accept a little more than the desktop entry spec says,
4016 * since gnome-vfs uses mime-types as keys in its cache.
4018 while (*q
&& *q
!= '=' && *q
!= '[' && *q
!= ']')
4019 q
= g_utf8_find_next_char (q
, NULL
);
4021 /* No empty keys, please */
4025 /* We accept spaces in the middle of keys to not break
4026 * existing apps, but we don't tolerate initial or final
4027 * spaces, which would lead to silent corruption when
4028 * rereading the file.
4030 if (*p
== ' ' || q
[-1] == ' ')
4036 while (*q
&& (g_unichar_isalnum (g_utf8_get_char_validated (q
, -1)) || *q
== '-' || *q
== '_' || *q
== '.' || *q
== '@'))
4037 q
= g_utf8_find_next_char (q
, NULL
);
4051 /* A group in a key file is made up of a starting '[' followed by one
4052 * or more letters making up the group name followed by ']'.
4055 g_key_file_line_is_group (const gchar
*line
)
4065 while (*p
&& *p
!= ']')
4066 p
= g_utf8_find_next_char (p
, NULL
);
4071 /* silently accept whitespace after the ] */
4072 p
= g_utf8_find_next_char (p
, NULL
);
4073 while (*p
== ' ' || *p
== '\t')
4074 p
= g_utf8_find_next_char (p
, NULL
);
4083 g_key_file_line_is_key_value_pair (const gchar
*line
)
4087 p
= (gchar
*) g_utf8_strchr (line
, -1, '=');
4092 /* Key must be non-empty
4101 g_key_file_parse_value_as_string (GKeyFile
*key_file
,
4106 gchar
*string_value
, *p
, *q0
, *q
;
4108 string_value
= g_new (gchar
, strlen (value
) + 1);
4110 p
= (gchar
*) value
;
4111 q0
= q
= string_value
;
4141 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
4142 G_KEY_FILE_ERROR_INVALID_VALUE
,
4143 _("Key file contains escape character "
4148 if (pieces
&& *p
== key_file
->list_separator
)
4149 *q
= key_file
->list_separator
;
4163 g_set_error (error
, G_KEY_FILE_ERROR
,
4164 G_KEY_FILE_ERROR_INVALID_VALUE
,
4165 _("Key file contains invalid escape "
4166 "sequence “%s”"), sequence
);
4175 if (pieces
&& (*p
== key_file
->list_separator
))
4177 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4193 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4194 *pieces
= g_slist_reverse (*pieces
);
4197 return string_value
;
4201 g_key_file_parse_string_as_value (GKeyFile
*key_file
,
4202 const gchar
*string
,
4203 gboolean escape_separator
)
4205 gchar
*value
, *p
, *q
;
4207 gboolean parsing_leading_space
;
4209 length
= strlen (string
) + 1;
4211 /* Worst case would be that every character needs to be escaped.
4212 * In other words every character turns to two characters
4214 value
= g_new (gchar
, 2 * length
);
4216 p
= (gchar
*) string
;
4218 parsing_leading_space
= TRUE
;
4219 while (p
< (string
+ length
- 1))
4221 gchar escaped_character
[3] = { '\\', 0, 0 };
4226 if (parsing_leading_space
)
4228 escaped_character
[1] = 's';
4229 strcpy (q
, escaped_character
);
4239 if (parsing_leading_space
)
4241 escaped_character
[1] = 't';
4242 strcpy (q
, escaped_character
);
4252 escaped_character
[1] = 'n';
4253 strcpy (q
, escaped_character
);
4257 escaped_character
[1] = 'r';
4258 strcpy (q
, escaped_character
);
4262 escaped_character
[1] = '\\';
4263 strcpy (q
, escaped_character
);
4265 parsing_leading_space
= FALSE
;
4268 if (escape_separator
&& *p
== key_file
->list_separator
)
4270 escaped_character
[1] = key_file
->list_separator
;
4271 strcpy (q
, escaped_character
);
4273 parsing_leading_space
= TRUE
;
4279 parsing_leading_space
= FALSE
;
4291 g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
4300 long_value
= strtol (value
, &eof_int
, 10);
4302 if (*value
== '\0' || (*eof_int
!= '\0' && !g_ascii_isspace(*eof_int
)))
4304 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
4305 g_set_error (error
, G_KEY_FILE_ERROR
,
4306 G_KEY_FILE_ERROR_INVALID_VALUE
,
4307 _("Value “%s” cannot be interpreted "
4308 "as a number."), value_utf8
);
4309 g_free (value_utf8
);
4314 int_value
= long_value
;
4315 if (int_value
!= long_value
|| errno
== ERANGE
)
4317 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
4320 G_KEY_FILE_ERROR_INVALID_VALUE
,
4321 _("Integer value “%s” out of range"),
4323 g_free (value_utf8
);
4332 g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
4336 return g_strdup_printf ("%d", value
);
4340 g_key_file_parse_value_as_double (GKeyFile
*key_file
,
4344 gchar
*end_of_valid_d
;
4345 gdouble double_value
= 0;
4347 double_value
= g_ascii_strtod (value
, &end_of_valid_d
);
4349 if (*end_of_valid_d
!= '\0' || end_of_valid_d
== value
)
4351 gchar
*value_utf8
= g_utf8_make_valid (value
, -1);
4352 g_set_error (error
, G_KEY_FILE_ERROR
,
4353 G_KEY_FILE_ERROR_INVALID_VALUE
,
4354 _("Value “%s” cannot be interpreted "
4355 "as a float number."),
4357 g_free (value_utf8
);
4362 return double_value
;
4366 strcmp_sized (const gchar
*s1
, size_t len1
, const gchar
*s2
)
4368 size_t len2
= strlen (s2
);
4369 return strncmp (s1
, s2
, MAX (len1
, len2
));
4373 g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
4380 /* Count the number of non-whitespace characters */
4381 for (i
= 0; value
[i
]; i
++)
4382 if (!g_ascii_isspace (value
[i
]))
4385 if (strcmp_sized (value
, length
, "true") == 0 || strcmp_sized (value
, length
, "1") == 0)
4387 else if (strcmp_sized (value
, length
, "false") == 0 || strcmp_sized (value
, length
, "0") == 0)
4390 value_utf8
= g_utf8_make_valid (value
, -1);
4391 g_set_error (error
, G_KEY_FILE_ERROR
,
4392 G_KEY_FILE_ERROR_INVALID_VALUE
,
4393 _("Value “%s” cannot be interpreted "
4394 "as a boolean."), value_utf8
);
4395 g_free (value_utf8
);
4401 g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
4405 return g_strdup ("true");
4407 return g_strdup ("false");
4411 g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
4418 string
= g_string_sized_new (512);
4420 lines
= g_strsplit (value
, "\n", 0);
4422 for (i
= 0; lines
[i
] != NULL
; i
++)
4424 if (lines
[i
][0] != '#')
4425 g_string_append_printf (string
, "%s\n", lines
[i
]);
4427 g_string_append_printf (string
, "%s\n", lines
[i
] + 1);
4431 return g_string_free (string
, FALSE
);
4435 g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
4436 const gchar
*comment
)
4442 string
= g_string_sized_new (512);
4444 lines
= g_strsplit (comment
, "\n", 0);
4446 for (i
= 0; lines
[i
] != NULL
; i
++)
4447 g_string_append_printf (string
, "#%s%s", lines
[i
],
4448 lines
[i
+ 1] == NULL
? "" : "\n");
4451 return g_string_free (string
, FALSE
);
4455 * g_key_file_save_to_file:
4456 * @key_file: a #GKeyFile
4457 * @filename: the name of the file to write to
4458 * @error: a pointer to a %NULL #GError, or %NULL
4460 * Writes the contents of @key_file to @filename using
4461 * g_file_set_contents().
4463 * This function can fail for any of the reasons that
4464 * g_file_set_contents() may fail.
4466 * Returns: %TRUE if successful, else %FALSE with @error set
4471 g_key_file_save_to_file (GKeyFile
*key_file
,
4472 const gchar
*filename
,
4479 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
4480 g_return_val_if_fail (filename
!= NULL
, FALSE
);
4481 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
4483 contents
= g_key_file_to_data (key_file
, &length
, NULL
);
4484 g_assert (contents
!= NULL
);
4486 success
= g_file_set_contents (filename
, contents
, length
, error
);