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 * GLib is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * GLib 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
21 * License along with GLib; see the file COPYING.LIB. If not,
22 * see <http://www.gnu.org/licenses/>.
36 #include <sys/types.h>
45 #define fstat(a,b) _fstati64(a,b)
50 #define S_ISREG(mode) ((mode)&_S_IFREG)
53 #endif /* G_OS_WIN23 */
58 #include "gfileutils.h"
64 #include "gmessages.h"
67 #include "gstrfuncs.h"
73 * @title: Key-value file parser
74 * @short_description: parses .ini-like config files
76 * #GKeyFile lets you parse, edit or create files containing groups of
77 * key-value pairs, which we call "key files" for lack of a better name.
78 * Several freedesktop.org specifications use key files now, e.g the
79 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
81 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
83 * The syntax of key files is described in detail in the
84 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
85 * here is a quick summary: Key files
86 * consists of groups of key-value pairs, interspersed with comments.
89 * # this is just an example
90 * # there can be comments before the first group
94 * Name=Key File Example\tthis value shows\nescaping
96 * # localized strings are stored in multiple key-value pairs
99 * Welcome[fr_FR]=Bonjour
101 * Welcome[be@latin]=Hello
105 * Numbers=2;20;-200;0
107 * Booleans=true;false;true;true
110 * Lines beginning with a '#' and blank lines are considered comments.
112 * Groups are started by a header line containing the group name enclosed
113 * in '[' and ']', and ended implicitly by the start of the next group or
114 * the end of the file. Each key-value pair must be contained in a group.
116 * Key-value pairs generally have the form `key=value`, with the
117 * exception of localized strings, which have the form
118 * `key[locale]=value`, with a locale identifier of the
119 * form `lang_COUNTRY@MODIFIER` where `COUNTRY` and `MODIFIER`
121 * Space before and after the '=' character are ignored. Newline, tab,
122 * carriage return and backslash characters in value are escaped as \n,
123 * \t, \r, and \\, respectively. To preserve leading spaces in values,
124 * these can also be escaped as \s.
126 * Key files can store strings (possibly with localized variants), integers,
127 * booleans and lists of these. Lists are separated by a separator character,
128 * typically ';' or ','. To use the list separator character in a value in
129 * a list, it has to be escaped by prefixing it with a backslash.
131 * This syntax is obviously inspired by the .ini files commonly met
132 * on Windows, but there are some important differences:
134 * - .ini files use the ';' character to begin comments,
135 * key files use the '#' character.
137 * - Key files do not allow for ungrouped keys meaning only
138 * comments can precede the first group.
140 * - Key files are always encoded in UTF-8.
142 * - Key and Group names are case-sensitive. For example, a group called
143 * [GROUP] is a different from [group].
145 * - .ini files don't have a strongly typed boolean entry type,
146 * they only have GetProfileInt(). In key files, only
147 * true and false (in lower case) are allowed.
149 * Note that in contrast to the
150 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
151 * groups in key files may contain the same
152 * key multiple times; the last entry wins. Key files may also contain
153 * multiple groups with the same name; they are merged together.
154 * Another difference is that keys and group names in key files are not
155 * restricted to ASCII characters.
161 * Error domain for key file parsing. Errors in this domain will
162 * be from the #GKeyFileError enumeration.
164 * See #GError for information on error domains.
169 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
170 * an unknown encoding
171 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
172 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
173 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
174 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
175 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
177 * Error codes returned by key file parsing.
182 * @G_KEY_FILE_NONE: No flags, default behaviour
183 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
184 * (possibly modified) contents of the key file back to a file;
185 * otherwise all comments will be lost when the key file is
187 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
188 * (possibly modified) contents of the key file back to a file;
189 * otherwise only the translations for the current language will be
192 * Flags which influence the parsing.
196 * G_KEY_FILE_DESKTOP_GROUP:
198 * The name of the main group of a desktop entry file, as defined in the
199 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
200 * Consult the specification for more
201 * details about the meanings of the keys below.
207 * G_KEY_FILE_DESKTOP_KEY_TYPE:
209 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
210 * giving the type of the desktop entry. Usually
211 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
212 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
213 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
219 * G_KEY_FILE_DESKTOP_KEY_VERSION:
221 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
222 * giving the version of the Desktop Entry Specification used for
223 * the desktop entry file.
229 * G_KEY_FILE_DESKTOP_KEY_NAME:
231 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
232 * string giving the specific name of the desktop entry.
238 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
240 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
241 * string giving the generic name of the desktop entry.
247 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
249 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
250 * stating whether the desktop entry should be shown in menus.
256 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
258 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
259 * string giving the tooltip for the desktop entry.
265 * G_KEY_FILE_DESKTOP_KEY_ICON:
267 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
268 * string giving the name of the icon to be displayed for the desktop
275 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
277 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
278 * stating whether the desktop entry has been deleted by the user.
284 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
286 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
287 * strings identifying the environments that should display the
294 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
296 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
297 * strings identifying the environments that should not display the
304 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
306 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
307 * giving the file name of a binary on disk used to determine if the
308 * program is actually installed. It is only valid for desktop entries
309 * with the `Application` type.
315 * G_KEY_FILE_DESKTOP_KEY_EXEC:
317 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
318 * giving the command line to execute. It is only valid for desktop
319 * entries with the `Application` type.
325 * G_KEY_FILE_DESKTOP_KEY_PATH:
327 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
328 * containing the working directory to run the program in. It is only
329 * valid for desktop entries with the `Application` type.
335 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
337 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
338 * stating whether the program should be run in a terminal window.
339 * It is only valid for desktop entries with the
340 * `Application` type.
346 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
348 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
349 * of strings giving the MIME types supported by this desktop entry.
355 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
357 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
358 * of strings giving the categories in which the desktop entry
359 * should be shown in a menu.
365 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
367 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
368 * stating whether the application supports the
369 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
375 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
377 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
378 * identifying the WM class or name hint of a window that the application
379 * will create, which can be used to emulate Startup Notification with
380 * older applications.
386 * G_KEY_FILE_DESKTOP_KEY_URL:
388 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
389 * giving the URL to access. It is only valid for desktop entries
390 * with the `Link` type.
396 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
398 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to true
399 * if the application is D-Bus activatable.
405 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
407 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
408 * giving the available application actions.
414 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
416 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
417 * entries representing applications.
423 * G_KEY_FILE_DESKTOP_TYPE_LINK:
425 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
426 * entries representing links to documents.
432 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
434 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
435 * entries representing directories.
440 typedef struct _GKeyFileGroup GKeyFileGroup
;
445 * The GKeyFile struct contains only private data
446 * and should not be accessed directly.
451 GHashTable
*group_hash
;
453 GKeyFileGroup
*start_group
;
454 GKeyFileGroup
*current_group
;
456 GString
*parse_buffer
; /* Holds up to one line of not-yet-parsed data */
458 gchar list_separator
;
464 volatile gint ref_count
;
467 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair
;
469 struct _GKeyFileGroup
471 const gchar
*name
; /* NULL for above first group (which will be comments) */
473 GKeyFileKeyValuePair
*comment
; /* Special comment that is stuck to the top of a group */
475 GList
*key_value_pairs
;
477 /* Used in parallel with key_value_pairs for
478 * increased lookup performance
480 GHashTable
*lookup_map
;
483 struct _GKeyFileKeyValuePair
485 gchar
*key
; /* NULL for comments */
489 static gint
find_file_in_data_dirs (const gchar
*file
,
490 const gchar
**data_dirs
,
493 static gboolean
g_key_file_load_from_fd (GKeyFile
*key_file
,
497 static GList
*g_key_file_lookup_group_node (GKeyFile
*key_file
,
498 const gchar
*group_name
);
499 static GKeyFileGroup
*g_key_file_lookup_group (GKeyFile
*key_file
,
500 const gchar
*group_name
);
502 static GList
*g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
503 GKeyFileGroup
*group
,
505 static GKeyFileKeyValuePair
*g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
506 GKeyFileGroup
*group
,
509 static void g_key_file_remove_group_node (GKeyFile
*key_file
,
511 static void g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
512 GKeyFileGroup
*group
,
515 static void g_key_file_add_key_value_pair (GKeyFile
*key_file
,
516 GKeyFileGroup
*group
,
517 GKeyFileKeyValuePair
*pair
);
518 static void g_key_file_add_key (GKeyFile
*key_file
,
519 GKeyFileGroup
*group
,
522 static void g_key_file_add_group (GKeyFile
*key_file
,
523 const gchar
*group_name
);
524 static gboolean
g_key_file_is_group_name (const gchar
*name
);
525 static gboolean
g_key_file_is_key_name (const gchar
*name
);
526 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
);
527 static gboolean
g_key_file_line_is_comment (const gchar
*line
);
528 static gboolean
g_key_file_line_is_group (const gchar
*line
);
529 static gboolean
g_key_file_line_is_key_value_pair (const gchar
*line
);
530 static gchar
*g_key_file_parse_value_as_string (GKeyFile
*key_file
,
534 static gchar
*g_key_file_parse_string_as_value (GKeyFile
*key_file
,
536 gboolean escape_separator
);
537 static gint
g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
540 static gchar
*g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
542 static gdouble
g_key_file_parse_value_as_double (GKeyFile
*key_file
,
545 static gboolean
g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
548 static gchar
*g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
550 static gchar
*g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
552 static gchar
*g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
553 const gchar
*comment
);
554 static void g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
558 static void g_key_file_parse_comment (GKeyFile
*key_file
,
562 static void g_key_file_parse_group (GKeyFile
*key_file
,
566 static gchar
*key_get_locale (const gchar
*key
);
567 static void g_key_file_parse_data (GKeyFile
*key_file
,
571 static void g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
574 G_DEFINE_QUARK (g
-key
-file
-error
-quark
, g_key_file_error
)
577 g_key_file_init (GKeyFile
*key_file
)
579 key_file
->current_group
= g_slice_new0 (GKeyFileGroup
);
580 key_file
->groups
= g_list_prepend (NULL
, key_file
->current_group
);
581 key_file
->group_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
582 key_file
->start_group
= NULL
;
583 key_file
->parse_buffer
= g_string_sized_new (128);
584 key_file
->list_separator
= ';';
586 key_file
->locales
= g_strdupv ((gchar
**)g_get_language_names ());
590 g_key_file_clear (GKeyFile
*key_file
)
592 GList
*tmp
, *group_node
;
594 if (key_file
->locales
)
596 g_strfreev (key_file
->locales
);
597 key_file
->locales
= NULL
;
600 if (key_file
->parse_buffer
)
602 g_string_free (key_file
->parse_buffer
, TRUE
);
603 key_file
->parse_buffer
= NULL
;
606 tmp
= key_file
->groups
;
611 g_key_file_remove_group_node (key_file
, group_node
);
614 if (key_file
->group_hash
!= NULL
)
616 g_hash_table_destroy (key_file
->group_hash
);
617 key_file
->group_hash
= NULL
;
620 g_warn_if_fail (key_file
->groups
== NULL
);
627 * Creates a new empty #GKeyFile object. Use
628 * g_key_file_load_from_file(), g_key_file_load_from_data(),
629 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
630 * read an existing key file.
632 * Returns: (transfer full): an empty #GKeyFile.
637 g_key_file_new (void)
641 key_file
= g_slice_new0 (GKeyFile
);
642 key_file
->ref_count
= 1;
643 g_key_file_init (key_file
);
649 * g_key_file_set_list_separator:
650 * @key_file: a #GKeyFile
651 * @separator: the separator
653 * Sets the character which is used to separate
654 * values in lists. Typically ';' or ',' are used
655 * as separators. The default list separator is ';'.
660 g_key_file_set_list_separator (GKeyFile
*key_file
,
663 g_return_if_fail (key_file
!= NULL
);
665 key_file
->list_separator
= separator
;
669 /* Iterates through all the directories in *dirs trying to
670 * open file. When it successfully locates and opens a file it
671 * returns the file descriptor to the open file. It also
672 * outputs the absolute path of the file in output_file.
675 find_file_in_data_dirs (const gchar
*file
,
680 const gchar
**data_dirs
, *data_dir
;
692 while (data_dirs
&& (data_dir
= *data_dirs
) && fd
== -1)
694 gchar
*candidate_file
, *sub_dir
;
696 candidate_file
= (gchar
*) file
;
697 sub_dir
= g_strdup ("");
698 while (candidate_file
!= NULL
&& fd
== -1)
702 path
= g_build_filename (data_dir
, sub_dir
,
703 candidate_file
, NULL
);
705 fd
= g_open (path
, O_RDONLY
, 0);
713 candidate_file
= strchr (candidate_file
, '-');
715 if (candidate_file
== NULL
)
721 sub_dir
= g_strndup (file
, candidate_file
- file
- 1);
723 for (p
= sub_dir
; *p
!= '\0'; p
++)
726 *p
= G_DIR_SEPARATOR
;
735 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
736 G_KEY_FILE_ERROR_NOT_FOUND
,
737 _("Valid key file could not be "
738 "found in search dirs"));
741 if (output_file
!= NULL
&& fd
> 0)
742 *output_file
= g_strdup (path
);
750 g_key_file_load_from_fd (GKeyFile
*key_file
,
755 GError
*key_file_error
= NULL
;
757 struct stat stat_buf
;
758 gchar read_buf
[4096];
759 gchar list_separator
;
761 if (fstat (fd
, &stat_buf
) < 0)
763 g_set_error_literal (error
, G_FILE_ERROR
,
764 g_file_error_from_errno (errno
),
769 if (!S_ISREG (stat_buf
.st_mode
))
771 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
772 G_KEY_FILE_ERROR_PARSE
,
773 _("Not a regular file"));
777 list_separator
= key_file
->list_separator
;
778 g_key_file_clear (key_file
);
779 g_key_file_init (key_file
);
780 key_file
->list_separator
= list_separator
;
781 key_file
->flags
= flags
;
785 bytes_read
= read (fd
, read_buf
, 4096);
787 if (bytes_read
== 0) /* End of File */
792 if (errno
== EINTR
|| errno
== EAGAIN
)
795 g_set_error_literal (error
, G_FILE_ERROR
,
796 g_file_error_from_errno (errno
),
801 g_key_file_parse_data (key_file
,
802 read_buf
, bytes_read
,
805 while (!key_file_error
);
809 g_propagate_error (error
, key_file_error
);
813 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
817 g_propagate_error (error
, key_file_error
);
825 * g_key_file_load_from_file:
826 * @key_file: an empty #GKeyFile struct
827 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
828 * @flags: flags from #GKeyFileFlags
829 * @error: return location for a #GError, or %NULL
831 * Loads a key file into an empty #GKeyFile structure.
832 * If the file could not be loaded then @error is set to
833 * either a #GFileError or #GKeyFileError.
835 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
840 g_key_file_load_from_file (GKeyFile
*key_file
,
845 GError
*key_file_error
= NULL
;
848 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
849 g_return_val_if_fail (file
!= NULL
, FALSE
);
851 fd
= g_open (file
, O_RDONLY
, 0);
855 g_set_error_literal (error
, G_FILE_ERROR
,
856 g_file_error_from_errno (errno
),
861 g_key_file_load_from_fd (key_file
, fd
, flags
, &key_file_error
);
866 g_propagate_error (error
, key_file_error
);
874 * g_key_file_load_from_data:
875 * @key_file: an empty #GKeyFile struct
876 * @data: key file loaded in memory
877 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
878 * @flags: flags from #GKeyFileFlags
879 * @error: return location for a #GError, or %NULL
881 * Loads a key file from memory into an empty #GKeyFile structure.
882 * If the object cannot be created then %error is set to a #GKeyFileError.
884 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
889 g_key_file_load_from_data (GKeyFile
*key_file
,
895 GError
*key_file_error
= NULL
;
896 gchar list_separator
;
898 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
899 g_return_val_if_fail (data
!= NULL
|| length
== 0, FALSE
);
901 if (length
== (gsize
)-1)
902 length
= strlen (data
);
904 list_separator
= key_file
->list_separator
;
905 g_key_file_clear (key_file
);
906 g_key_file_init (key_file
);
907 key_file
->list_separator
= list_separator
;
908 key_file
->flags
= flags
;
910 g_key_file_parse_data (key_file
, data
, length
, &key_file_error
);
914 g_propagate_error (error
, key_file_error
);
918 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
922 g_propagate_error (error
, key_file_error
);
930 * g_key_file_load_from_dirs:
931 * @key_file: an empty #GKeyFile struct
932 * @file: (type filename): a relative path to a filename to open and parse
933 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
934 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
935 * of the file, or %NULL
936 * @flags: flags from #GKeyFileFlags
937 * @error: return location for a #GError, or %NULL
939 * This function looks for a key file named @file in the paths
940 * specified in @search_dirs, loads the file into @key_file and
941 * returns the file's full path in @full_path. If the file could not
942 * be loaded then an %error is set to either a #GFileError or
945 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
950 g_key_file_load_from_dirs (GKeyFile
*key_file
,
952 const gchar
**search_dirs
,
957 GError
*key_file_error
= NULL
;
958 const gchar
**data_dirs
;
963 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
964 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
965 g_return_val_if_fail (search_dirs
!= NULL
, FALSE
);
968 data_dirs
= search_dirs
;
970 while (*data_dirs
!= NULL
&& !found_file
)
972 g_free (output_path
);
974 fd
= find_file_in_data_dirs (file
, data_dirs
, &output_path
,
980 g_propagate_error (error
, key_file_error
);
984 found_file
= g_key_file_load_from_fd (key_file
, fd
, flags
,
990 g_propagate_error (error
, key_file_error
);
995 if (found_file
&& full_path
)
996 *full_path
= output_path
;
998 g_free (output_path
);
1004 * g_key_file_load_from_data_dirs:
1005 * @key_file: an empty #GKeyFile struct
1006 * @file: (type filename): a relative path to a filename to open and parse
1007 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
1008 * of the file, or %NULL
1009 * @flags: flags from #GKeyFileFlags
1010 * @error: return location for a #GError, or %NULL
1012 * This function looks for a key file named @file in the paths
1013 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1014 * loads the file into @key_file and returns the file's full path in
1015 * @full_path. If the file could not be loaded then an %error is
1016 * set to either a #GFileError or #GKeyFileError.
1018 * Returns: %TRUE if a key file could be loaded, %FALSE othewise
1022 g_key_file_load_from_data_dirs (GKeyFile
*key_file
,
1025 GKeyFileFlags flags
,
1028 gchar
**all_data_dirs
;
1029 const gchar
* user_data_dir
;
1030 const gchar
* const * system_data_dirs
;
1032 gboolean found_file
;
1034 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
1035 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
1037 user_data_dir
= g_get_user_data_dir ();
1038 system_data_dirs
= g_get_system_data_dirs ();
1039 all_data_dirs
= g_new (gchar
*, g_strv_length ((gchar
**)system_data_dirs
) + 2);
1042 all_data_dirs
[i
++] = g_strdup (user_data_dir
);
1045 while (system_data_dirs
[j
] != NULL
)
1046 all_data_dirs
[i
++] = g_strdup (system_data_dirs
[j
++]);
1047 all_data_dirs
[i
] = NULL
;
1049 found_file
= g_key_file_load_from_dirs (key_file
,
1051 (const gchar
**)all_data_dirs
,
1056 g_strfreev (all_data_dirs
);
1062 * g_key_file_ref: (skip)
1063 * @key_file: a #GKeyFile
1065 * Increases the reference count of @key_file.
1067 * Returns: the same @key_file.
1072 g_key_file_ref (GKeyFile
*key_file
)
1074 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1076 g_atomic_int_inc (&key_file
->ref_count
);
1082 * g_key_file_free: (skip)
1083 * @key_file: a #GKeyFile
1085 * Clears all keys and groups from @key_file, and decreases the
1086 * reference count by 1. If the reference count reaches zero,
1087 * frees the key file and all its allocated memory.
1092 g_key_file_free (GKeyFile
*key_file
)
1094 g_return_if_fail (key_file
!= NULL
);
1096 g_key_file_clear (key_file
);
1097 g_key_file_unref (key_file
);
1102 * @key_file: a #GKeyFile
1104 * Decreases the reference count of @key_file by 1. If the reference count
1105 * reaches zero, frees the key file and all its allocated memory.
1110 g_key_file_unref (GKeyFile
*key_file
)
1112 g_return_if_fail (key_file
!= NULL
);
1114 if (g_atomic_int_dec_and_test (&key_file
->ref_count
))
1116 g_key_file_clear (key_file
);
1117 g_slice_free (GKeyFile
, key_file
);
1121 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1122 * true for locales that match those in g_get_language_names().
1125 g_key_file_locale_is_interesting (GKeyFile
*key_file
,
1126 const gchar
*locale
)
1130 if (key_file
->flags
& G_KEY_FILE_KEEP_TRANSLATIONS
)
1133 for (i
= 0; key_file
->locales
[i
] != NULL
; i
++)
1135 if (g_ascii_strcasecmp (key_file
->locales
[i
], locale
) == 0)
1143 g_key_file_parse_line (GKeyFile
*key_file
,
1148 GError
*parse_error
= NULL
;
1151 g_return_if_fail (key_file
!= NULL
);
1152 g_return_if_fail (line
!= NULL
);
1154 line_start
= (gchar
*) line
;
1155 while (g_ascii_isspace (*line_start
))
1158 if (g_key_file_line_is_comment (line_start
))
1159 g_key_file_parse_comment (key_file
, line
, length
, &parse_error
);
1160 else if (g_key_file_line_is_group (line_start
))
1161 g_key_file_parse_group (key_file
, line_start
,
1162 length
- (line_start
- line
),
1164 else if (g_key_file_line_is_key_value_pair (line_start
))
1165 g_key_file_parse_key_value_pair (key_file
, line_start
,
1166 length
- (line_start
- line
),
1170 gchar
*line_utf8
= _g_utf8_make_valid (line
);
1171 g_set_error (error
, G_KEY_FILE_ERROR
,
1172 G_KEY_FILE_ERROR_PARSE
,
1173 _("Key file contains line '%s' which is not "
1174 "a key-value pair, group, or comment"),
1182 g_propagate_error (error
, parse_error
);
1186 g_key_file_parse_comment (GKeyFile
*key_file
,
1191 GKeyFileKeyValuePair
*pair
;
1193 if (!(key_file
->flags
& G_KEY_FILE_KEEP_COMMENTS
))
1196 g_warn_if_fail (key_file
->current_group
!= NULL
);
1198 pair
= g_slice_new (GKeyFileKeyValuePair
);
1200 pair
->value
= g_strndup (line
, length
);
1202 key_file
->current_group
->key_value_pairs
=
1203 g_list_prepend (key_file
->current_group
->key_value_pairs
, pair
);
1207 g_key_file_parse_group (GKeyFile
*key_file
,
1213 const gchar
*group_name_start
, *group_name_end
;
1215 /* advance past opening '['
1217 group_name_start
= line
+ 1;
1218 group_name_end
= line
+ length
- 1;
1220 while (*group_name_end
!= ']')
1223 group_name
= g_strndup (group_name_start
,
1224 group_name_end
- group_name_start
);
1226 if (!g_key_file_is_group_name (group_name
))
1228 g_set_error (error
, G_KEY_FILE_ERROR
,
1229 G_KEY_FILE_ERROR_PARSE
,
1230 _("Invalid group name: %s"), group_name
);
1231 g_free (group_name
);
1235 g_key_file_add_group (key_file
, group_name
);
1236 g_free (group_name
);
1240 g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
1245 gchar
*key
, *value
, *key_end
, *value_start
, *locale
;
1246 gsize key_len
, value_len
;
1248 if (key_file
->current_group
== NULL
|| key_file
->current_group
->name
== NULL
)
1250 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
1251 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1252 _("Key file does not start with a group"));
1256 key_end
= value_start
= strchr (line
, '=');
1258 g_warn_if_fail (key_end
!= NULL
);
1263 /* Pull the key name from the line (chomping trailing whitespace)
1265 while (g_ascii_isspace (*key_end
))
1268 key_len
= key_end
- line
+ 2;
1270 g_warn_if_fail (key_len
<= length
);
1272 key
= g_strndup (line
, key_len
- 1);
1274 if (!g_key_file_is_key_name (key
))
1276 g_set_error (error
, G_KEY_FILE_ERROR
,
1277 G_KEY_FILE_ERROR_PARSE
,
1278 _("Invalid key name: %s"), key
);
1283 /* Pull the value from the line (chugging leading whitespace)
1285 while (g_ascii_isspace (*value_start
))
1288 value_len
= line
+ length
- value_start
+ 1;
1290 value
= g_strndup (value_start
, value_len
);
1292 g_warn_if_fail (key_file
->start_group
!= NULL
);
1294 if (key_file
->current_group
1295 && key_file
->current_group
->name
1296 && strcmp (key_file
->start_group
->name
,
1297 key_file
->current_group
->name
) == 0
1298 && strcmp (key
, "Encoding") == 0)
1300 if (g_ascii_strcasecmp (value
, "UTF-8") != 0)
1302 gchar
*value_utf8
= _g_utf8_make_valid (value
);
1303 g_set_error (error
, G_KEY_FILE_ERROR
,
1304 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1305 _("Key file contains unsupported "
1306 "encoding '%s'"), value_utf8
);
1307 g_free (value_utf8
);
1315 /* Is this key a translation? If so, is it one that we care about?
1317 locale
= key_get_locale (key
);
1319 if (locale
== NULL
|| g_key_file_locale_is_interesting (key_file
, locale
))
1321 GKeyFileKeyValuePair
*pair
;
1323 pair
= g_slice_new (GKeyFileKeyValuePair
);
1325 pair
->value
= value
;
1327 g_key_file_add_key_value_pair (key_file
, key_file
->current_group
, pair
);
1339 key_get_locale (const gchar
*key
)
1343 locale
= g_strrstr (key
, "[");
1345 if (locale
&& strlen (locale
) <= 2)
1349 locale
= g_strndup (locale
+ 1, strlen (locale
) - 2);
1355 g_key_file_parse_data (GKeyFile
*key_file
,
1360 GError
*parse_error
;
1363 g_return_if_fail (key_file
!= NULL
);
1364 g_return_if_fail (data
!= NULL
|| length
== 0);
1371 if (data
[i
] == '\n')
1373 if (key_file
->parse_buffer
->len
> 0
1374 && (key_file
->parse_buffer
->str
[key_file
->parse_buffer
->len
- 1]
1376 g_string_erase (key_file
->parse_buffer
,
1377 key_file
->parse_buffer
->len
- 1,
1380 /* When a newline is encountered flush the parse buffer so that the
1381 * line can be parsed. Note that completely blank lines won't show
1382 * up in the parse buffer, so they get parsed directly.
1384 if (key_file
->parse_buffer
->len
> 0)
1385 g_key_file_flush_parse_buffer (key_file
, &parse_error
);
1387 g_key_file_parse_comment (key_file
, "", 1, &parse_error
);
1391 g_propagate_error (error
, parse_error
);
1398 const gchar
*start_of_line
;
1399 const gchar
*end_of_line
;
1402 start_of_line
= data
+ i
;
1403 end_of_line
= memchr (start_of_line
, '\n', length
- i
);
1405 if (end_of_line
== NULL
)
1406 end_of_line
= data
+ length
;
1408 line_length
= end_of_line
- start_of_line
;
1410 g_string_append_len (key_file
->parse_buffer
, start_of_line
, line_length
);
1417 g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
1420 GError
*file_error
= NULL
;
1422 g_return_if_fail (key_file
!= NULL
);
1426 if (key_file
->parse_buffer
->len
> 0)
1428 g_key_file_parse_line (key_file
, key_file
->parse_buffer
->str
,
1429 key_file
->parse_buffer
->len
,
1431 g_string_erase (key_file
->parse_buffer
, 0, -1);
1435 g_propagate_error (error
, file_error
);
1442 * g_key_file_to_data:
1443 * @key_file: a #GKeyFile
1444 * @length: (out) (allow-none): return location for the length of the
1445 * returned string, or %NULL
1446 * @error: return location for a #GError, or %NULL
1448 * This function outputs @key_file as a string.
1450 * Note that this function never reports an error,
1451 * so it is safe to pass %NULL as @error.
1453 * Returns: a newly allocated string holding
1454 * the contents of the #GKeyFile
1459 g_key_file_to_data (GKeyFile
*key_file
,
1463 GString
*data_string
;
1464 GList
*group_node
, *key_file_node
;
1466 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1468 data_string
= g_string_new (NULL
);
1470 for (group_node
= g_list_last (key_file
->groups
);
1472 group_node
= group_node
->prev
)
1474 GKeyFileGroup
*group
;
1476 group
= (GKeyFileGroup
*) group_node
->data
;
1478 /* separate groups by at least an empty line */
1479 if (data_string
->len
>= 2 &&
1480 data_string
->str
[data_string
->len
- 2] != '\n')
1481 g_string_append_c (data_string
, '\n');
1483 if (group
->comment
!= NULL
)
1484 g_string_append_printf (data_string
, "%s\n", group
->comment
->value
);
1486 if (group
->name
!= NULL
)
1487 g_string_append_printf (data_string
, "[%s]\n", group
->name
);
1489 for (key_file_node
= g_list_last (group
->key_value_pairs
);
1490 key_file_node
!= NULL
;
1491 key_file_node
= key_file_node
->prev
)
1493 GKeyFileKeyValuePair
*pair
;
1495 pair
= (GKeyFileKeyValuePair
*) key_file_node
->data
;
1497 if (pair
->key
!= NULL
)
1498 g_string_append_printf (data_string
, "%s=%s\n", pair
->key
, pair
->value
);
1500 g_string_append_printf (data_string
, "%s\n", pair
->value
);
1505 *length
= data_string
->len
;
1507 return g_string_free (data_string
, FALSE
);
1511 * g_key_file_get_keys:
1512 * @key_file: a #GKeyFile
1513 * @group_name: a group name
1514 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1515 * @error: return location for a #GError, or %NULL
1517 * Returns all keys for the group name @group_name. The array of
1518 * returned keys will be %NULL-terminated, so @length may
1519 * optionally be %NULL. In the event that the @group_name cannot
1520 * be found, %NULL is returned and @error is set to
1521 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1523 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1524 * Use g_strfreev() to free it.
1529 g_key_file_get_keys (GKeyFile
*key_file
,
1530 const gchar
*group_name
,
1534 GKeyFileGroup
*group
;
1539 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1540 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1542 group
= g_key_file_lookup_group (key_file
, group_name
);
1546 g_set_error (error
, G_KEY_FILE_ERROR
,
1547 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1548 _("Key file does not have group '%s'"),
1554 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1556 GKeyFileKeyValuePair
*pair
;
1558 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1564 keys
= g_new (gchar
*, num_keys
+ 1);
1567 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1569 GKeyFileKeyValuePair
*pair
;
1571 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1575 keys
[i
] = g_strdup (pair
->key
);
1580 keys
[num_keys
] = NULL
;
1589 * g_key_file_get_start_group:
1590 * @key_file: a #GKeyFile
1592 * Returns the name of the start group of the file.
1594 * Returns: The start group of the key file.
1599 g_key_file_get_start_group (GKeyFile
*key_file
)
1601 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1603 if (key_file
->start_group
)
1604 return g_strdup (key_file
->start_group
->name
);
1610 * g_key_file_get_groups:
1611 * @key_file: a #GKeyFile
1612 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1614 * Returns all groups in the key file loaded with @key_file.
1615 * The array of returned groups will be %NULL-terminated, so
1616 * @length may optionally be %NULL.
1618 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1619 * Use g_strfreev() to free it.
1623 g_key_file_get_groups (GKeyFile
*key_file
,
1628 gsize i
, num_groups
;
1630 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1632 num_groups
= g_list_length (key_file
->groups
);
1634 g_return_val_if_fail (num_groups
> 0, NULL
);
1636 group_node
= g_list_last (key_file
->groups
);
1638 g_return_val_if_fail (((GKeyFileGroup
*) group_node
->data
)->name
== NULL
, NULL
);
1640 /* Only need num_groups instead of num_groups + 1
1641 * because the first group of the file (last in the
1642 * list) is always the comment group at the top,
1645 groups
= g_new (gchar
*, num_groups
);
1649 for (group_node
= group_node
->prev
;
1651 group_node
= group_node
->prev
)
1653 GKeyFileGroup
*group
;
1655 group
= (GKeyFileGroup
*) group_node
->data
;
1657 g_warn_if_fail (group
->name
!= NULL
);
1659 groups
[i
++] = g_strdup (group
->name
);
1670 set_not_found_key_error (const char *group_name
,
1674 g_set_error (error
, G_KEY_FILE_ERROR
,
1675 G_KEY_FILE_ERROR_KEY_NOT_FOUND
,
1676 _("Key file does not have key '%s' in group '%s'"),
1681 * g_key_file_get_value:
1682 * @key_file: a #GKeyFile
1683 * @group_name: a group name
1685 * @error: return location for a #GError, or %NULL
1687 * Returns the raw value associated with @key under @group_name.
1688 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1690 * In the event the key cannot be found, %NULL is returned and
1691 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1692 * event that the @group_name cannot be found, %NULL is returned
1693 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1696 * Returns: a newly allocated string or %NULL if the specified
1697 * key cannot be found.
1702 g_key_file_get_value (GKeyFile
*key_file
,
1703 const gchar
*group_name
,
1707 GKeyFileGroup
*group
;
1708 GKeyFileKeyValuePair
*pair
;
1709 gchar
*value
= NULL
;
1711 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1712 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1713 g_return_val_if_fail (key
!= NULL
, NULL
);
1715 group
= g_key_file_lookup_group (key_file
, group_name
);
1719 g_set_error (error
, G_KEY_FILE_ERROR
,
1720 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1721 _("Key file does not have group '%s'"),
1726 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1729 value
= g_strdup (pair
->value
);
1731 set_not_found_key_error (group_name
, key
, error
);
1737 * g_key_file_set_value:
1738 * @key_file: a #GKeyFile
1739 * @group_name: a group name
1743 * Associates a new value with @key under @group_name.
1745 * If @key cannot be found then it is created. If @group_name cannot
1746 * be found then it is created. To set an UTF-8 string which may contain
1747 * characters that need escaping (such as newlines or spaces), use
1748 * g_key_file_set_string().
1753 g_key_file_set_value (GKeyFile
*key_file
,
1754 const gchar
*group_name
,
1758 GKeyFileGroup
*group
;
1759 GKeyFileKeyValuePair
*pair
;
1761 g_return_if_fail (key_file
!= NULL
);
1762 g_return_if_fail (g_key_file_is_group_name (group_name
));
1763 g_return_if_fail (g_key_file_is_key_name (key
));
1764 g_return_if_fail (value
!= NULL
);
1766 group
= g_key_file_lookup_group (key_file
, group_name
);
1770 g_key_file_add_group (key_file
, group_name
);
1771 group
= (GKeyFileGroup
*) key_file
->groups
->data
;
1773 g_key_file_add_key (key_file
, group
, key
, value
);
1777 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1780 g_key_file_add_key (key_file
, group
, key
, value
);
1783 g_free (pair
->value
);
1784 pair
->value
= g_strdup (value
);
1790 * g_key_file_get_string:
1791 * @key_file: a #GKeyFile
1792 * @group_name: a group name
1794 * @error: return location for a #GError, or %NULL
1796 * Returns the string value associated with @key under @group_name.
1797 * Unlike g_key_file_get_value(), this function handles escape sequences
1800 * In the event the key cannot be found, %NULL is returned and
1801 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1802 * event that the @group_name cannot be found, %NULL is returned
1803 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1805 * Returns: a newly allocated string or %NULL if the specified
1806 * key cannot be found.
1811 g_key_file_get_string (GKeyFile
*key_file
,
1812 const gchar
*group_name
,
1816 gchar
*value
, *string_value
;
1817 GError
*key_file_error
;
1819 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1820 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1821 g_return_val_if_fail (key
!= NULL
, NULL
);
1823 key_file_error
= NULL
;
1825 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1829 g_propagate_error (error
, key_file_error
);
1833 if (!g_utf8_validate (value
, -1, NULL
))
1835 gchar
*value_utf8
= _g_utf8_make_valid (value
);
1836 g_set_error (error
, G_KEY_FILE_ERROR
,
1837 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1838 _("Key file contains key '%s' with value '%s' "
1839 "which is not UTF-8"), key
, value_utf8
);
1840 g_free (value_utf8
);
1846 string_value
= g_key_file_parse_value_as_string (key_file
, value
, NULL
,
1852 if (g_error_matches (key_file_error
,
1854 G_KEY_FILE_ERROR_INVALID_VALUE
))
1856 g_set_error (error
, G_KEY_FILE_ERROR
,
1857 G_KEY_FILE_ERROR_INVALID_VALUE
,
1858 _("Key file contains key '%s' "
1859 "which has a value that cannot be interpreted."),
1861 g_error_free (key_file_error
);
1864 g_propagate_error (error
, key_file_error
);
1867 return string_value
;
1871 * g_key_file_set_string:
1872 * @key_file: a #GKeyFile
1873 * @group_name: a group name
1877 * Associates a new string value with @key under @group_name.
1878 * If @key cannot be found then it is created.
1879 * If @group_name cannot be found then it is created.
1880 * Unlike g_key_file_set_value(), this function handles characters
1881 * that need escaping, such as newlines.
1886 g_key_file_set_string (GKeyFile
*key_file
,
1887 const gchar
*group_name
,
1889 const gchar
*string
)
1893 g_return_if_fail (key_file
!= NULL
);
1894 g_return_if_fail (string
!= NULL
);
1896 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
1897 g_key_file_set_value (key_file
, group_name
, key
, value
);
1902 * g_key_file_get_string_list:
1903 * @key_file: a #GKeyFile
1904 * @group_name: a group name
1906 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1907 * @error: return location for a #GError, or %NULL
1909 * Returns the values associated with @key under @group_name.
1911 * In the event the key cannot be found, %NULL is returned and
1912 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1913 * event that the @group_name cannot be found, %NULL is returned
1914 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1916 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1917 * a %NULL-terminated string array or %NULL if the specified
1918 * key cannot be found. The array should be freed with g_strfreev().
1923 g_key_file_get_string_list (GKeyFile
*key_file
,
1924 const gchar
*group_name
,
1929 GError
*key_file_error
= NULL
;
1930 gchar
*value
, *string_value
, **values
;
1932 GSList
*p
, *pieces
= NULL
;
1934 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1935 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1936 g_return_val_if_fail (key
!= NULL
, NULL
);
1941 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1945 g_propagate_error (error
, key_file_error
);
1949 if (!g_utf8_validate (value
, -1, NULL
))
1951 gchar
*value_utf8
= _g_utf8_make_valid (value
);
1952 g_set_error (error
, G_KEY_FILE_ERROR
,
1953 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1954 _("Key file contains key '%s' with value '%s' "
1955 "which is not UTF-8"), key
, value_utf8
);
1956 g_free (value_utf8
);
1962 string_value
= g_key_file_parse_value_as_string (key_file
, value
, &pieces
, &key_file_error
);
1964 g_free (string_value
);
1968 if (g_error_matches (key_file_error
,
1970 G_KEY_FILE_ERROR_INVALID_VALUE
))
1972 g_set_error (error
, G_KEY_FILE_ERROR
,
1973 G_KEY_FILE_ERROR_INVALID_VALUE
,
1974 _("Key file contains key '%s' "
1975 "which has a value that cannot be interpreted."),
1977 g_error_free (key_file_error
);
1980 g_propagate_error (error
, key_file_error
);
1982 g_slist_free_full (pieces
, g_free
);
1986 len
= g_slist_length (pieces
);
1987 values
= g_new (gchar
*, len
+ 1);
1988 for (p
= pieces
, i
= 0; p
; p
= p
->next
)
1989 values
[i
++] = p
->data
;
1992 g_slist_free (pieces
);
2001 * g_key_file_set_string_list:
2002 * @key_file: a #GKeyFile
2003 * @group_name: a group name
2005 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2006 * @length: number of string values in @list
2008 * Associates a list of string values for @key under @group_name.
2009 * If @key cannot be found then it is created.
2010 * If @group_name cannot be found then it is created.
2015 g_key_file_set_string_list (GKeyFile
*key_file
,
2016 const gchar
*group_name
,
2018 const gchar
* const list
[],
2021 GString
*value_list
;
2024 g_return_if_fail (key_file
!= NULL
);
2025 g_return_if_fail (list
!= NULL
|| length
== 0);
2027 value_list
= g_string_sized_new (length
* 128);
2028 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
2032 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
2033 g_string_append (value_list
, value
);
2034 g_string_append_c (value_list
, key_file
->list_separator
);
2039 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
2040 g_string_free (value_list
, TRUE
);
2044 * g_key_file_set_locale_string:
2045 * @key_file: a #GKeyFile
2046 * @group_name: a group name
2048 * @locale: a locale identifier
2051 * Associates a string value for @key and @locale under @group_name.
2052 * If the translation for @key cannot be found then it is created.
2057 g_key_file_set_locale_string (GKeyFile
*key_file
,
2058 const gchar
*group_name
,
2060 const gchar
*locale
,
2061 const gchar
*string
)
2063 gchar
*full_key
, *value
;
2065 g_return_if_fail (key_file
!= NULL
);
2066 g_return_if_fail (key
!= NULL
);
2067 g_return_if_fail (locale
!= NULL
);
2068 g_return_if_fail (string
!= NULL
);
2070 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
2071 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
2072 g_key_file_set_value (key_file
, group_name
, full_key
, value
);
2078 * g_key_file_get_locale_string:
2079 * @key_file: a #GKeyFile
2080 * @group_name: a group name
2082 * @locale: (allow-none): a locale identifier or %NULL
2083 * @error: return location for a #GError, or %NULL
2085 * Returns the value associated with @key under @group_name
2086 * translated in the given @locale if available. If @locale is
2087 * %NULL then the current locale is assumed.
2089 * If @key cannot be found then %NULL is returned and @error is set
2090 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2091 * with @key cannot be interpreted or no suitable translation can
2092 * be found then the untranslated value is returned.
2094 * Returns: a newly allocated string or %NULL if the specified
2095 * key cannot be found.
2100 g_key_file_get_locale_string (GKeyFile
*key_file
,
2101 const gchar
*group_name
,
2103 const gchar
*locale
,
2106 gchar
*candidate_key
, *translated_value
;
2107 GError
*key_file_error
;
2109 gboolean free_languages
= FALSE
;
2112 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2113 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2114 g_return_val_if_fail (key
!= NULL
, NULL
);
2116 candidate_key
= NULL
;
2117 translated_value
= NULL
;
2118 key_file_error
= NULL
;
2122 languages
= g_get_locale_variants (locale
);
2123 free_languages
= TRUE
;
2127 languages
= (gchar
**) g_get_language_names ();
2128 free_languages
= FALSE
;
2131 for (i
= 0; languages
[i
]; i
++)
2133 candidate_key
= g_strdup_printf ("%s[%s]", key
, languages
[i
]);
2135 translated_value
= g_key_file_get_string (key_file
,
2137 candidate_key
, NULL
);
2138 g_free (candidate_key
);
2140 if (translated_value
)
2143 g_free (translated_value
);
2144 translated_value
= NULL
;
2147 /* Fallback to untranslated key
2149 if (!translated_value
)
2151 translated_value
= g_key_file_get_string (key_file
, group_name
, key
,
2154 if (!translated_value
)
2155 g_propagate_error (error
, key_file_error
);
2159 g_strfreev (languages
);
2161 return translated_value
;
2165 * g_key_file_get_locale_string_list:
2166 * @key_file: a #GKeyFile
2167 * @group_name: a group name
2169 * @locale: (allow-none): a locale identifier or %NULL
2170 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2171 * @error: return location for a #GError or %NULL
2173 * Returns the values associated with @key under @group_name
2174 * translated in the given @locale if available. If @locale is
2175 * %NULL then the current locale is assumed.
2177 * If @key cannot be found then %NULL is returned and @error is set
2178 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2179 * with @key cannot be interpreted or no suitable translations
2180 * can be found then the untranslated values are returned. The
2181 * returned array is %NULL-terminated, so @length may optionally
2184 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2185 * or %NULL if the key isn't found. The string array should be freed
2186 * with g_strfreev().
2191 g_key_file_get_locale_string_list (GKeyFile
*key_file
,
2192 const gchar
*group_name
,
2194 const gchar
*locale
,
2198 GError
*key_file_error
;
2199 gchar
**values
, *value
;
2200 char list_separator
[2];
2203 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2204 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2205 g_return_val_if_fail (key
!= NULL
, NULL
);
2207 key_file_error
= NULL
;
2209 value
= g_key_file_get_locale_string (key_file
, group_name
,
2214 g_propagate_error (error
, key_file_error
);
2223 len
= strlen (value
);
2224 if (value
[len
- 1] == key_file
->list_separator
)
2225 value
[len
- 1] = '\0';
2227 list_separator
[0] = key_file
->list_separator
;
2228 list_separator
[1] = '\0';
2229 values
= g_strsplit (value
, list_separator
, 0);
2234 *length
= g_strv_length (values
);
2240 * g_key_file_set_locale_string_list:
2241 * @key_file: a #GKeyFile
2242 * @group_name: a group name
2244 * @locale: a locale identifier
2245 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2246 * @length: the length of @list
2248 * Associates a list of string values for @key and @locale under
2249 * @group_name. If the translation for @key cannot be found then
2255 g_key_file_set_locale_string_list (GKeyFile
*key_file
,
2256 const gchar
*group_name
,
2258 const gchar
*locale
,
2259 const gchar
* const list
[],
2262 GString
*value_list
;
2266 g_return_if_fail (key_file
!= NULL
);
2267 g_return_if_fail (key
!= NULL
);
2268 g_return_if_fail (locale
!= NULL
);
2269 g_return_if_fail (length
!= 0);
2271 value_list
= g_string_sized_new (length
* 128);
2272 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
2276 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
2277 g_string_append (value_list
, value
);
2278 g_string_append_c (value_list
, key_file
->list_separator
);
2283 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
2284 g_key_file_set_value (key_file
, group_name
, full_key
, value_list
->str
);
2286 g_string_free (value_list
, TRUE
);
2290 * g_key_file_get_boolean:
2291 * @key_file: a #GKeyFile
2292 * @group_name: a group name
2294 * @error: return location for a #GError
2296 * Returns the value associated with @key under @group_name as a
2299 * If @key cannot be found then %FALSE is returned and @error is set
2300 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2301 * associated with @key cannot be interpreted as a boolean then %FALSE
2302 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2304 * Returns: the value associated with the key as a boolean,
2305 * or %FALSE if the key was not found or could not be parsed.
2310 g_key_file_get_boolean (GKeyFile
*key_file
,
2311 const gchar
*group_name
,
2315 GError
*key_file_error
= NULL
;
2317 gboolean bool_value
;
2319 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
2320 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
2321 g_return_val_if_fail (key
!= NULL
, FALSE
);
2323 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2327 g_propagate_error (error
, key_file_error
);
2331 bool_value
= g_key_file_parse_value_as_boolean (key_file
, value
,
2337 if (g_error_matches (key_file_error
,
2339 G_KEY_FILE_ERROR_INVALID_VALUE
))
2341 g_set_error (error
, G_KEY_FILE_ERROR
,
2342 G_KEY_FILE_ERROR_INVALID_VALUE
,
2343 _("Key file contains key '%s' "
2344 "which has a value that cannot be interpreted."),
2346 g_error_free (key_file_error
);
2349 g_propagate_error (error
, key_file_error
);
2356 * g_key_file_set_boolean:
2357 * @key_file: a #GKeyFile
2358 * @group_name: a group name
2360 * @value: %TRUE or %FALSE
2362 * Associates a new boolean value with @key under @group_name.
2363 * If @key cannot be found then it is created.
2368 g_key_file_set_boolean (GKeyFile
*key_file
,
2369 const gchar
*group_name
,
2375 g_return_if_fail (key_file
!= NULL
);
2377 result
= g_key_file_parse_boolean_as_value (key_file
, value
);
2378 g_key_file_set_value (key_file
, group_name
, key
, result
);
2383 * g_key_file_get_boolean_list:
2384 * @key_file: a #GKeyFile
2385 * @group_name: a group name
2387 * @length: (out): the number of booleans returned
2388 * @error: return location for a #GError
2390 * Returns the values associated with @key under @group_name as
2393 * If @key cannot be found then %NULL is returned and @error is set to
2394 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2395 * with @key cannot be interpreted as booleans then %NULL is returned
2396 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2398 * Returns: (array length=length) (element-type gboolean) (transfer container):
2399 * the values associated with the key as a list of booleans, or %NULL if the
2400 * key was not found or could not be parsed. The returned list of booleans
2401 * should be freed with g_free() when no longer needed.
2406 g_key_file_get_boolean_list (GKeyFile
*key_file
,
2407 const gchar
*group_name
,
2412 GError
*key_file_error
;
2414 gboolean
*bool_values
;
2417 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2418 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2419 g_return_val_if_fail (key
!= NULL
, NULL
);
2424 key_file_error
= NULL
;
2426 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2427 &num_bools
, &key_file_error
);
2430 g_propagate_error (error
, key_file_error
);
2435 bool_values
= g_new (gboolean
, num_bools
);
2437 for (i
= 0; i
< num_bools
; i
++)
2439 bool_values
[i
] = g_key_file_parse_value_as_boolean (key_file
,
2445 g_propagate_error (error
, key_file_error
);
2446 g_strfreev (values
);
2447 g_free (bool_values
);
2452 g_strfreev (values
);
2455 *length
= num_bools
;
2461 * g_key_file_set_boolean_list:
2462 * @key_file: a #GKeyFile
2463 * @group_name: a group name
2465 * @list: (array length=length): an array of boolean values
2466 * @length: length of @list
2468 * Associates a list of boolean values with @key under @group_name.
2469 * If @key cannot be found then it is created.
2470 * If @group_name is %NULL, the start_group is used.
2475 g_key_file_set_boolean_list (GKeyFile
*key_file
,
2476 const gchar
*group_name
,
2481 GString
*value_list
;
2484 g_return_if_fail (key_file
!= NULL
);
2485 g_return_if_fail (list
!= NULL
);
2487 value_list
= g_string_sized_new (length
* 8);
2488 for (i
= 0; i
< length
; i
++)
2492 value
= g_key_file_parse_boolean_as_value (key_file
, list
[i
]);
2494 g_string_append (value_list
, value
);
2495 g_string_append_c (value_list
, key_file
->list_separator
);
2500 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
2501 g_string_free (value_list
, TRUE
);
2505 * g_key_file_get_integer:
2506 * @key_file: a #GKeyFile
2507 * @group_name: a group name
2509 * @error: return location for a #GError
2511 * Returns the value associated with @key under @group_name as an
2514 * If @key cannot be found then 0 is returned and @error is set to
2515 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2516 * with @key cannot be interpreted as an integer then 0 is returned
2517 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2519 * Returns: the value associated with the key as an integer, or
2520 * 0 if the key was not found or could not be parsed.
2525 g_key_file_get_integer (GKeyFile
*key_file
,
2526 const gchar
*group_name
,
2530 GError
*key_file_error
;
2534 g_return_val_if_fail (key_file
!= NULL
, -1);
2535 g_return_val_if_fail (group_name
!= NULL
, -1);
2536 g_return_val_if_fail (key
!= NULL
, -1);
2538 key_file_error
= NULL
;
2540 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2544 g_propagate_error (error
, key_file_error
);
2548 int_value
= g_key_file_parse_value_as_integer (key_file
, value
,
2554 if (g_error_matches (key_file_error
,
2556 G_KEY_FILE_ERROR_INVALID_VALUE
))
2558 g_set_error (error
, G_KEY_FILE_ERROR
,
2559 G_KEY_FILE_ERROR_INVALID_VALUE
,
2560 _("Key file contains key '%s' in group '%s' "
2561 "which has a value that cannot be interpreted."),
2563 g_error_free (key_file_error
);
2566 g_propagate_error (error
, key_file_error
);
2573 * g_key_file_set_integer:
2574 * @key_file: a #GKeyFile
2575 * @group_name: a group name
2577 * @value: an integer value
2579 * Associates a new integer value with @key under @group_name.
2580 * If @key cannot be found then it is created.
2585 g_key_file_set_integer (GKeyFile
*key_file
,
2586 const gchar
*group_name
,
2592 g_return_if_fail (key_file
!= NULL
);
2594 result
= g_key_file_parse_integer_as_value (key_file
, value
);
2595 g_key_file_set_value (key_file
, group_name
, key
, result
);
2600 * g_key_file_get_int64:
2601 * @key_file: a non-%NULL #GKeyFile
2602 * @group_name: a non-%NULL group name
2603 * @key: a non-%NULL key
2604 * @error: return location for a #GError
2606 * Returns the value associated with @key under @group_name as a signed
2607 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2608 * 64-bit results without truncation.
2610 * Returns: the value associated with the key as a signed 64-bit integer, or
2611 * 0 if the key was not found or could not be parsed.
2616 g_key_file_get_int64 (GKeyFile
*key_file
,
2617 const gchar
*group_name
,
2624 g_return_val_if_fail (key_file
!= NULL
, -1);
2625 g_return_val_if_fail (group_name
!= NULL
, -1);
2626 g_return_val_if_fail (key
!= NULL
, -1);
2628 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2633 v
= g_ascii_strtoll (s
, &end
, 10);
2635 if (*s
== '\0' || *end
!= '\0')
2637 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2638 _("Key '%s' in group '%s' has value '%s' "
2639 "where %s was expected"),
2640 key
, group_name
, s
, "int64");
2650 * g_key_file_set_int64:
2651 * @key_file: a #GKeyFile
2652 * @group_name: a group name
2654 * @value: an integer value
2656 * Associates a new integer value with @key under @group_name.
2657 * If @key cannot be found then it is created.
2662 g_key_file_set_int64 (GKeyFile
*key_file
,
2663 const gchar
*group_name
,
2669 g_return_if_fail (key_file
!= NULL
);
2671 result
= g_strdup_printf ("%" G_GINT64_FORMAT
, value
);
2672 g_key_file_set_value (key_file
, group_name
, key
, result
);
2677 * g_key_file_get_uint64:
2678 * @key_file: a non-%NULL #GKeyFile
2679 * @group_name: a non-%NULL group name
2680 * @key: a non-%NULL key
2681 * @error: return location for a #GError
2683 * Returns the value associated with @key under @group_name as an unsigned
2684 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2685 * large positive results without truncation.
2687 * Returns: the value associated with the key as an unsigned 64-bit integer,
2688 * or 0 if the key was not found or could not be parsed.
2693 g_key_file_get_uint64 (GKeyFile
*key_file
,
2694 const gchar
*group_name
,
2701 g_return_val_if_fail (key_file
!= NULL
, -1);
2702 g_return_val_if_fail (group_name
!= NULL
, -1);
2703 g_return_val_if_fail (key
!= NULL
, -1);
2705 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2710 v
= g_ascii_strtoull (s
, &end
, 10);
2712 if (*s
== '\0' || *end
!= '\0')
2714 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2715 _("Key '%s' in group '%s' has value '%s' "
2716 "where %s was expected"),
2717 key
, group_name
, s
, "uint64");
2727 * g_key_file_set_uint64:
2728 * @key_file: a #GKeyFile
2729 * @group_name: a group name
2731 * @value: an integer value
2733 * Associates a new integer value with @key under @group_name.
2734 * If @key cannot be found then it is created.
2739 g_key_file_set_uint64 (GKeyFile
*key_file
,
2740 const gchar
*group_name
,
2746 g_return_if_fail (key_file
!= NULL
);
2748 result
= g_strdup_printf ("%" G_GUINT64_FORMAT
, value
);
2749 g_key_file_set_value (key_file
, group_name
, key
, result
);
2754 * g_key_file_get_integer_list:
2755 * @key_file: a #GKeyFile
2756 * @group_name: a group name
2758 * @length: (out): the number of integers returned
2759 * @error: return location for a #GError
2761 * Returns the values associated with @key under @group_name as
2764 * If @key cannot be found then %NULL is returned and @error is set to
2765 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2766 * with @key cannot be interpreted as integers then %NULL is returned
2767 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2769 * Returns: (array length=length) (element-type gint) (transfer container):
2770 * the values associated with the key as a list of integers, or %NULL if
2771 * the key was not found or could not be parsed. The returned list of
2772 * integers should be freed with g_free() when no longer needed.
2777 g_key_file_get_integer_list (GKeyFile
*key_file
,
2778 const gchar
*group_name
,
2783 GError
*key_file_error
= NULL
;
2788 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2789 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2790 g_return_val_if_fail (key
!= NULL
, NULL
);
2795 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2796 &num_ints
, &key_file_error
);
2799 g_propagate_error (error
, key_file_error
);
2804 int_values
= g_new (gint
, num_ints
);
2806 for (i
= 0; i
< num_ints
; i
++)
2808 int_values
[i
] = g_key_file_parse_value_as_integer (key_file
,
2814 g_propagate_error (error
, key_file_error
);
2815 g_strfreev (values
);
2816 g_free (int_values
);
2821 g_strfreev (values
);
2830 * g_key_file_set_integer_list:
2831 * @key_file: a #GKeyFile
2832 * @group_name: a group name
2834 * @list: (array length=length): an array of integer values
2835 * @length: number of integer values in @list
2837 * Associates a list of integer values with @key under @group_name.
2838 * If @key cannot be found then it is created.
2843 g_key_file_set_integer_list (GKeyFile
*key_file
,
2844 const gchar
*group_name
,
2852 g_return_if_fail (key_file
!= NULL
);
2853 g_return_if_fail (list
!= NULL
);
2855 values
= g_string_sized_new (length
* 16);
2856 for (i
= 0; i
< length
; i
++)
2860 value
= g_key_file_parse_integer_as_value (key_file
, list
[i
]);
2862 g_string_append (values
, value
);
2863 g_string_append_c (values
, key_file
->list_separator
);
2868 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
2869 g_string_free (values
, TRUE
);
2873 * g_key_file_get_double:
2874 * @key_file: a #GKeyFile
2875 * @group_name: a group name
2877 * @error: return location for a #GError
2879 * Returns the value associated with @key under @group_name as a
2880 * double. If @group_name is %NULL, the start_group is used.
2882 * If @key cannot be found then 0.0 is returned and @error is set to
2883 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2884 * with @key cannot be interpreted as a double then 0.0 is returned
2885 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2887 * Returns: the value associated with the key as a double, or
2888 * 0.0 if the key was not found or could not be parsed.
2893 g_key_file_get_double (GKeyFile
*key_file
,
2894 const gchar
*group_name
,
2898 GError
*key_file_error
;
2900 gdouble double_value
;
2902 g_return_val_if_fail (key_file
!= NULL
, -1);
2903 g_return_val_if_fail (group_name
!= NULL
, -1);
2904 g_return_val_if_fail (key
!= NULL
, -1);
2906 key_file_error
= NULL
;
2908 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2912 g_propagate_error (error
, key_file_error
);
2916 double_value
= g_key_file_parse_value_as_double (key_file
, value
,
2922 if (g_error_matches (key_file_error
,
2924 G_KEY_FILE_ERROR_INVALID_VALUE
))
2926 g_set_error (error
, G_KEY_FILE_ERROR
,
2927 G_KEY_FILE_ERROR_INVALID_VALUE
,
2928 _("Key file contains key '%s' in group '%s' "
2929 "which has a value that cannot be interpreted."),
2931 g_error_free (key_file_error
);
2934 g_propagate_error (error
, key_file_error
);
2937 return double_value
;
2941 * g_key_file_set_double:
2942 * @key_file: a #GKeyFile
2943 * @group_name: a group name
2945 * @value: an double value
2947 * Associates a new double value with @key under @group_name.
2948 * If @key cannot be found then it is created.
2953 g_key_file_set_double (GKeyFile
*key_file
,
2954 const gchar
*group_name
,
2958 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
2960 g_return_if_fail (key_file
!= NULL
);
2962 g_ascii_dtostr (result
, sizeof (result
), value
);
2963 g_key_file_set_value (key_file
, group_name
, key
, result
);
2967 * g_key_file_get_double_list:
2968 * @key_file: a #GKeyFile
2969 * @group_name: a group name
2971 * @length: (out): the number of doubles returned
2972 * @error: return location for a #GError
2974 * Returns the values associated with @key under @group_name as
2977 * If @key cannot be found then %NULL is returned and @error is set to
2978 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2979 * with @key cannot be interpreted as doubles then %NULL is returned
2980 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2982 * Returns: (array length=length) (element-type gdouble) (transfer container):
2983 * the values associated with the key as a list of doubles, or %NULL if the
2984 * key was not found or could not be parsed. The returned list of doubles
2985 * should be freed with g_free() when no longer needed.
2990 g_key_file_get_double_list (GKeyFile
*key_file
,
2991 const gchar
*group_name
,
2996 GError
*key_file_error
= NULL
;
2998 gdouble
*double_values
;
2999 gsize i
, num_doubles
;
3001 g_return_val_if_fail (key_file
!= NULL
, NULL
);
3002 g_return_val_if_fail (group_name
!= NULL
, NULL
);
3003 g_return_val_if_fail (key
!= NULL
, NULL
);
3008 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
3009 &num_doubles
, &key_file_error
);
3012 g_propagate_error (error
, key_file_error
);
3017 double_values
= g_new (gdouble
, num_doubles
);
3019 for (i
= 0; i
< num_doubles
; i
++)
3021 double_values
[i
] = g_key_file_parse_value_as_double (key_file
,
3027 g_propagate_error (error
, key_file_error
);
3028 g_strfreev (values
);
3029 g_free (double_values
);
3034 g_strfreev (values
);
3037 *length
= num_doubles
;
3039 return double_values
;
3043 * g_key_file_set_double_list:
3044 * @key_file: a #GKeyFile
3045 * @group_name: a group name
3047 * @list: (array length=length): an array of double values
3048 * @length: number of double values in @list
3050 * Associates a list of double values with @key under
3051 * @group_name. If @key cannot be found then it is created.
3056 g_key_file_set_double_list (GKeyFile
*key_file
,
3057 const gchar
*group_name
,
3065 g_return_if_fail (key_file
!= NULL
);
3066 g_return_if_fail (list
!= NULL
);
3068 values
= g_string_sized_new (length
* 16);
3069 for (i
= 0; i
< length
; i
++)
3071 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
3073 g_ascii_dtostr( result
, sizeof (result
), list
[i
] );
3075 g_string_append (values
, result
);
3076 g_string_append_c (values
, key_file
->list_separator
);
3079 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
3080 g_string_free (values
, TRUE
);
3084 g_key_file_set_key_comment (GKeyFile
*key_file
,
3085 const gchar
*group_name
,
3087 const gchar
*comment
,
3090 GKeyFileGroup
*group
;
3091 GKeyFileKeyValuePair
*pair
;
3092 GList
*key_node
, *comment_node
, *tmp
;
3094 group
= g_key_file_lookup_group (key_file
, group_name
);
3097 g_set_error (error
, G_KEY_FILE_ERROR
,
3098 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3099 _("Key file does not have group '%s'"),
3100 group_name
? group_name
: "(null)");
3105 /* First find the key the comments are supposed to be
3108 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
3110 if (key_node
== NULL
)
3112 set_not_found_key_error (group
->name
, key
, error
);
3116 /* Then find all the comments already associated with the
3119 tmp
= key_node
->next
;
3122 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3124 if (pair
->key
!= NULL
)
3129 g_key_file_remove_key_value_pair_node (key_file
, group
,
3133 if (comment
== NULL
)
3136 /* Now we can add our new comment
3138 pair
= g_slice_new (GKeyFileKeyValuePair
);
3140 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3142 key_node
= g_list_insert (key_node
, pair
, 1);
3148 g_key_file_set_group_comment (GKeyFile
*key_file
,
3149 const gchar
*group_name
,
3150 const gchar
*comment
,
3153 GKeyFileGroup
*group
;
3155 g_return_val_if_fail (g_key_file_is_group_name (group_name
), FALSE
);
3157 group
= g_key_file_lookup_group (key_file
, group_name
);
3160 g_set_error (error
, G_KEY_FILE_ERROR
,
3161 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3162 _("Key file does not have group '%s'"),
3163 group_name
? group_name
: "(null)");
3168 /* First remove any existing comment
3172 g_key_file_key_value_pair_free (group
->comment
);
3173 group
->comment
= NULL
;
3176 if (comment
== NULL
)
3179 /* Now we can add our new comment
3181 group
->comment
= g_slice_new (GKeyFileKeyValuePair
);
3182 group
->comment
->key
= NULL
;
3183 group
->comment
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3189 g_key_file_set_top_comment (GKeyFile
*key_file
,
3190 const gchar
*comment
,
3194 GKeyFileGroup
*group
;
3195 GKeyFileKeyValuePair
*pair
;
3197 /* The last group in the list should be the top (comments only)
3200 g_warn_if_fail (key_file
->groups
!= NULL
);
3201 group_node
= g_list_last (key_file
->groups
);
3202 group
= (GKeyFileGroup
*) group_node
->data
;
3203 g_warn_if_fail (group
->name
== NULL
);
3205 /* Note all keys must be comments at the top of
3206 * the file, so we can just free it all.
3208 g_list_free_full (group
->key_value_pairs
, (GDestroyNotify
) g_key_file_key_value_pair_free
);
3209 group
->key_value_pairs
= NULL
;
3211 if (comment
== NULL
)
3214 pair
= g_slice_new (GKeyFileKeyValuePair
);
3216 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3218 group
->key_value_pairs
=
3219 g_list_prepend (group
->key_value_pairs
, pair
);
3225 * g_key_file_set_comment:
3226 * @key_file: a #GKeyFile
3227 * @group_name: (allow-none): a group name, or %NULL
3228 * @key: (allow-none): a key
3229 * @comment: a comment
3230 * @error: return location for a #GError
3232 * Places a comment above @key from @group_name.
3234 * If @key is %NULL then @comment will be written above @group_name.
3235 * If both @key and @group_name are %NULL, then @comment will be
3236 * written above the first group in the file.
3238 * Note that this function prepends a '#' comment marker to
3239 * each line of @comment.
3241 * Returns: %TRUE if the comment was written, %FALSE otherwise
3246 g_key_file_set_comment (GKeyFile
*key_file
,
3247 const gchar
*group_name
,
3249 const gchar
*comment
,
3252 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3254 if (group_name
!= NULL
&& key
!= NULL
)
3256 if (!g_key_file_set_key_comment (key_file
, group_name
, key
, comment
, error
))
3259 else if (group_name
!= NULL
)
3261 if (!g_key_file_set_group_comment (key_file
, group_name
, comment
, error
))
3266 if (!g_key_file_set_top_comment (key_file
, comment
, error
))
3274 g_key_file_get_key_comment (GKeyFile
*key_file
,
3275 const gchar
*group_name
,
3279 GKeyFileGroup
*group
;
3280 GKeyFileKeyValuePair
*pair
;
3281 GList
*key_node
, *tmp
;
3285 g_return_val_if_fail (g_key_file_is_group_name (group_name
), NULL
);
3287 group
= g_key_file_lookup_group (key_file
, group_name
);
3290 g_set_error (error
, G_KEY_FILE_ERROR
,
3291 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3292 _("Key file does not have group '%s'"),
3293 group_name
? group_name
: "(null)");
3298 /* First find the key the comments are supposed to be
3301 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
3303 if (key_node
== NULL
)
3305 set_not_found_key_error (group
->name
, key
, error
);
3311 /* Then find all the comments already associated with the
3312 * key and concatentate them.
3314 tmp
= key_node
->next
;
3315 if (!key_node
->next
)
3318 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3319 if (pair
->key
!= NULL
)
3324 pair
= (GKeyFileKeyValuePair
*) tmp
->next
->data
;
3326 if (pair
->key
!= NULL
)
3332 while (tmp
!= key_node
)
3334 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3337 string
= g_string_sized_new (512);
3339 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
3340 g_string_append (string
, comment
);
3348 comment
= string
->str
;
3349 g_string_free (string
, FALSE
);
3358 get_group_comment (GKeyFile
*key_file
,
3359 GKeyFileGroup
*group
,
3368 tmp
= group
->key_value_pairs
;
3371 GKeyFileKeyValuePair
*pair
;
3373 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3375 if (pair
->key
!= NULL
)
3381 if (tmp
->next
== NULL
)
3389 GKeyFileKeyValuePair
*pair
;
3391 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3394 string
= g_string_sized_new (512);
3396 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
3397 g_string_append (string
, comment
);
3404 return g_string_free (string
, FALSE
);
3410 g_key_file_get_group_comment (GKeyFile
*key_file
,
3411 const gchar
*group_name
,
3415 GKeyFileGroup
*group
;
3417 group
= g_key_file_lookup_group (key_file
, group_name
);
3420 g_set_error (error
, G_KEY_FILE_ERROR
,
3421 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3422 _("Key file does not have group '%s'"),
3423 group_name
? group_name
: "(null)");
3429 return g_strdup (group
->comment
->value
);
3431 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3432 group_node
= group_node
->next
;
3433 group
= (GKeyFileGroup
*)group_node
->data
;
3434 return get_group_comment (key_file
, group
, error
);
3438 g_key_file_get_top_comment (GKeyFile
*key_file
,
3442 GKeyFileGroup
*group
;
3444 /* The last group in the list should be the top (comments only)
3447 g_warn_if_fail (key_file
->groups
!= NULL
);
3448 group_node
= g_list_last (key_file
->groups
);
3449 group
= (GKeyFileGroup
*) group_node
->data
;
3450 g_warn_if_fail (group
->name
== NULL
);
3452 return get_group_comment (key_file
, group
, error
);
3456 * g_key_file_get_comment:
3457 * @key_file: a #GKeyFile
3458 * @group_name: (allow-none): a group name, or %NULL
3460 * @error: return location for a #GError
3462 * Retrieves a comment above @key from @group_name.
3463 * If @key is %NULL then @comment will be read from above
3464 * @group_name. If both @key and @group_name are %NULL, then
3465 * @comment will be read from above the first group in the file.
3467 * Note that the returned string includes the '#' comment markers.
3469 * Returns: a comment that should be freed with g_free()
3474 g_key_file_get_comment (GKeyFile
*key_file
,
3475 const gchar
*group_name
,
3479 g_return_val_if_fail (key_file
!= NULL
, NULL
);
3481 if (group_name
!= NULL
&& key
!= NULL
)
3482 return g_key_file_get_key_comment (key_file
, group_name
, key
, error
);
3483 else if (group_name
!= NULL
)
3484 return g_key_file_get_group_comment (key_file
, group_name
, error
);
3486 return g_key_file_get_top_comment (key_file
, error
);
3490 * g_key_file_remove_comment:
3491 * @key_file: a #GKeyFile
3492 * @group_name: (allow-none): a group name, or %NULL
3493 * @key: (allow-none): a key
3494 * @error: return location for a #GError
3496 * Removes a comment above @key from @group_name.
3497 * If @key is %NULL then @comment will be removed above @group_name.
3498 * If both @key and @group_name are %NULL, then @comment will
3499 * be removed above the first group in the file.
3501 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3507 g_key_file_remove_comment (GKeyFile
*key_file
,
3508 const gchar
*group_name
,
3512 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3514 if (group_name
!= NULL
&& key
!= NULL
)
3515 return g_key_file_set_key_comment (key_file
, group_name
, key
, NULL
, error
);
3516 else if (group_name
!= NULL
)
3517 return g_key_file_set_group_comment (key_file
, group_name
, NULL
, error
);
3519 return g_key_file_set_top_comment (key_file
, NULL
, error
);
3523 * g_key_file_has_group:
3524 * @key_file: a #GKeyFile
3525 * @group_name: a group name
3527 * Looks whether the key file has the group @group_name.
3529 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3534 g_key_file_has_group (GKeyFile
*key_file
,
3535 const gchar
*group_name
)
3537 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3538 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3540 return g_key_file_lookup_group (key_file
, group_name
) != NULL
;
3543 /* This code remains from a historical attempt to add a new public API
3544 * which respects the GError rules.
3547 g_key_file_has_key_full (GKeyFile
*key_file
,
3548 const gchar
*group_name
,
3553 GKeyFileKeyValuePair
*pair
;
3554 GKeyFileGroup
*group
;
3556 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3557 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3558 g_return_val_if_fail (key
!= NULL
, FALSE
);
3560 group
= g_key_file_lookup_group (key_file
, group_name
);
3564 g_set_error (error
, G_KEY_FILE_ERROR
,
3565 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3566 _("Key file does not have group '%s'"),
3572 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3575 *has_key
= pair
!= NULL
;
3580 * g_key_file_has_key: (skip)
3581 * @key_file: a #GKeyFile
3582 * @group_name: a group name
3584 * @error: return location for a #GError
3586 * Looks whether the key file has the key @key in the group
3589 * Note that this function does not follow the rules for #GError strictly;
3590 * the return value both carries meaning and signals an error. To use
3591 * this function, you must pass a #GError pointer in @error, and check
3592 * whether it is not %NULL to see if an error occurred.
3594 * Language bindings should use g_key_file_get_value() to test whether
3595 * or not a key exists.
3597 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3602 g_key_file_has_key (GKeyFile
*key_file
,
3603 const gchar
*group_name
,
3607 GError
*temp_error
= NULL
;
3610 if (g_key_file_has_key_full (key_file
, group_name
, key
, &has_key
, &temp_error
))
3616 g_propagate_error (error
, temp_error
);
3622 g_key_file_add_group (GKeyFile
*key_file
,
3623 const gchar
*group_name
)
3625 GKeyFileGroup
*group
;
3627 g_return_if_fail (key_file
!= NULL
);
3628 g_return_if_fail (g_key_file_is_group_name (group_name
));
3630 group
= g_key_file_lookup_group (key_file
, group_name
);
3633 key_file
->current_group
= group
;
3637 group
= g_slice_new0 (GKeyFileGroup
);
3638 group
->name
= g_strdup (group_name
);
3639 group
->lookup_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
3640 key_file
->groups
= g_list_prepend (key_file
->groups
, group
);
3641 key_file
->current_group
= group
;
3643 if (key_file
->start_group
== NULL
)
3644 key_file
->start_group
= group
;
3646 g_hash_table_insert (key_file
->group_hash
, (gpointer
)group
->name
, group
);
3650 g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
)
3655 g_free (pair
->value
);
3656 g_slice_free (GKeyFileKeyValuePair
, pair
);
3660 /* Be careful not to call this function on a node with data in the
3661 * lookup map without removing it from the lookup map, first.
3663 * Some current cases where this warning is not a concern are
3665 * - the node being removed is a comment node
3666 * - the entire lookup map is getting destroyed soon after
3670 g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
3671 GKeyFileGroup
*group
,
3675 GKeyFileKeyValuePair
*pair
;
3677 pair
= (GKeyFileKeyValuePair
*) pair_node
->data
;
3679 group
->key_value_pairs
= g_list_remove_link (group
->key_value_pairs
, pair_node
);
3681 g_warn_if_fail (pair
->value
!= NULL
);
3683 g_key_file_key_value_pair_free (pair
);
3685 g_list_free_1 (pair_node
);
3689 g_key_file_remove_group_node (GKeyFile
*key_file
,
3692 GKeyFileGroup
*group
;
3695 group
= (GKeyFileGroup
*) group_node
->data
;
3698 g_hash_table_remove (key_file
->group_hash
, group
->name
);
3700 /* If the current group gets deleted make the current group the last
3703 if (key_file
->current_group
== group
)
3705 /* groups should always contain at least the top comment group,
3706 * unless g_key_file_clear has been called
3708 if (key_file
->groups
)
3709 key_file
->current_group
= (GKeyFileGroup
*) key_file
->groups
->data
;
3711 key_file
->current_group
= NULL
;
3714 /* If the start group gets deleted make the start group the first
3717 if (key_file
->start_group
== group
)
3719 tmp
= g_list_last (key_file
->groups
);
3722 if (tmp
!= group_node
&&
3723 ((GKeyFileGroup
*) tmp
->data
)->name
!= NULL
)
3730 key_file
->start_group
= (GKeyFileGroup
*) tmp
->data
;
3732 key_file
->start_group
= NULL
;
3735 key_file
->groups
= g_list_remove_link (key_file
->groups
, group_node
);
3737 tmp
= group
->key_value_pairs
;
3744 g_key_file_remove_key_value_pair_node (key_file
, group
, pair_node
);
3747 g_warn_if_fail (group
->key_value_pairs
== NULL
);
3751 g_key_file_key_value_pair_free (group
->comment
);
3752 group
->comment
= NULL
;
3755 if (group
->lookup_map
)
3757 g_hash_table_destroy (group
->lookup_map
);
3758 group
->lookup_map
= NULL
;
3761 g_free ((gchar
*) group
->name
);
3762 g_slice_free (GKeyFileGroup
, group
);
3763 g_list_free_1 (group_node
);
3767 * g_key_file_remove_group:
3768 * @key_file: a #GKeyFile
3769 * @group_name: a group name
3770 * @error: return location for a #GError or %NULL
3772 * Removes the specified group, @group_name,
3773 * from the key file.
3775 * Returns: %TRUE if the group was removed, %FALSE otherwise
3780 g_key_file_remove_group (GKeyFile
*key_file
,
3781 const gchar
*group_name
,
3786 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3787 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3789 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3793 g_set_error (error
, G_KEY_FILE_ERROR
,
3794 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3795 _("Key file does not have group '%s'"),
3800 g_key_file_remove_group_node (key_file
, group_node
);
3806 g_key_file_add_key_value_pair (GKeyFile
*key_file
,
3807 GKeyFileGroup
*group
,
3808 GKeyFileKeyValuePair
*pair
)
3810 g_hash_table_replace (group
->lookup_map
, pair
->key
, pair
);
3811 group
->key_value_pairs
= g_list_prepend (group
->key_value_pairs
, pair
);
3815 g_key_file_add_key (GKeyFile
*key_file
,
3816 GKeyFileGroup
*group
,
3820 GKeyFileKeyValuePair
*pair
;
3822 pair
= g_slice_new (GKeyFileKeyValuePair
);
3823 pair
->key
= g_strdup (key
);
3824 pair
->value
= g_strdup (value
);
3826 g_key_file_add_key_value_pair (key_file
, group
, pair
);
3830 * g_key_file_remove_key:
3831 * @key_file: a #GKeyFile
3832 * @group_name: a group name
3833 * @key: a key name to remove
3834 * @error: return location for a #GError or %NULL
3836 * Removes @key in @group_name from the key file.
3838 * Returns: %TRUE if the key was removed, %FALSE otherwise
3843 g_key_file_remove_key (GKeyFile
*key_file
,
3844 const gchar
*group_name
,
3848 GKeyFileGroup
*group
;
3849 GKeyFileKeyValuePair
*pair
;
3851 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3852 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3853 g_return_val_if_fail (key
!= NULL
, FALSE
);
3857 group
= g_key_file_lookup_group (key_file
, group_name
);
3860 g_set_error (error
, G_KEY_FILE_ERROR
,
3861 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3862 _("Key file does not have group '%s'"),
3867 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3871 set_not_found_key_error (group
->name
, key
, error
);
3875 group
->key_value_pairs
= g_list_remove (group
->key_value_pairs
, pair
);
3876 g_hash_table_remove (group
->lookup_map
, pair
->key
);
3877 g_key_file_key_value_pair_free (pair
);
3883 g_key_file_lookup_group_node (GKeyFile
*key_file
,
3884 const gchar
*group_name
)
3886 GKeyFileGroup
*group
;
3889 for (tmp
= key_file
->groups
; tmp
!= NULL
; tmp
= tmp
->next
)
3891 group
= (GKeyFileGroup
*) tmp
->data
;
3893 if (group
&& group
->name
&& strcmp (group
->name
, group_name
) == 0)
3900 static GKeyFileGroup
*
3901 g_key_file_lookup_group (GKeyFile
*key_file
,
3902 const gchar
*group_name
)
3904 return (GKeyFileGroup
*)g_hash_table_lookup (key_file
->group_hash
, group_name
);
3908 g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
3909 GKeyFileGroup
*group
,
3914 for (key_node
= group
->key_value_pairs
;
3916 key_node
= key_node
->next
)
3918 GKeyFileKeyValuePair
*pair
;
3920 pair
= (GKeyFileKeyValuePair
*) key_node
->data
;
3922 if (pair
->key
&& strcmp (pair
->key
, key
) == 0)
3929 static GKeyFileKeyValuePair
*
3930 g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
3931 GKeyFileGroup
*group
,
3934 return (GKeyFileKeyValuePair
*) g_hash_table_lookup (group
->lookup_map
, key
);
3937 /* Lines starting with # or consisting entirely of whitespace are merely
3938 * recorded, not parsed. This function assumes all leading whitespace
3939 * has been stripped.
3942 g_key_file_line_is_comment (const gchar
*line
)
3944 return (*line
== '#' || *line
== '\0' || *line
== '\n');
3948 g_key_file_is_group_name (const gchar
*name
)
3955 p
= q
= (gchar
*) name
;
3956 while (*q
&& *q
!= ']' && *q
!= '[' && !g_ascii_iscntrl (*q
))
3957 q
= g_utf8_find_next_char (q
, NULL
);
3959 if (*q
!= '\0' || q
== p
)
3966 g_key_file_is_key_name (const gchar
*name
)
3973 p
= q
= (gchar
*) name
;
3974 /* We accept a little more than the desktop entry spec says,
3975 * since gnome-vfs uses mime-types as keys in its cache.
3977 while (*q
&& *q
!= '=' && *q
!= '[' && *q
!= ']')
3978 q
= g_utf8_find_next_char (q
, NULL
);
3980 /* No empty keys, please */
3984 /* We accept spaces in the middle of keys to not break
3985 * existing apps, but we don't tolerate initial or final
3986 * spaces, which would lead to silent corruption when
3987 * rereading the file.
3989 if (*p
== ' ' || q
[-1] == ' ')
3995 while (*q
&& (g_unichar_isalnum (g_utf8_get_char_validated (q
, -1)) || *q
== '-' || *q
== '_' || *q
== '.' || *q
== '@'))
3996 q
= g_utf8_find_next_char (q
, NULL
);
4010 /* A group in a key file is made up of a starting '[' followed by one
4011 * or more letters making up the group name followed by ']'.
4014 g_key_file_line_is_group (const gchar
*line
)
4024 while (*p
&& *p
!= ']')
4025 p
= g_utf8_find_next_char (p
, NULL
);
4030 /* silently accept whitespace after the ] */
4031 p
= g_utf8_find_next_char (p
, NULL
);
4032 while (*p
== ' ' || *p
== '\t')
4033 p
= g_utf8_find_next_char (p
, NULL
);
4042 g_key_file_line_is_key_value_pair (const gchar
*line
)
4046 p
= (gchar
*) g_utf8_strchr (line
, -1, '=');
4051 /* Key must be non-empty
4060 g_key_file_parse_value_as_string (GKeyFile
*key_file
,
4065 gchar
*string_value
, *p
, *q0
, *q
;
4067 string_value
= g_new (gchar
, strlen (value
) + 1);
4069 p
= (gchar
*) value
;
4070 q0
= q
= string_value
;
4100 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
4101 G_KEY_FILE_ERROR_INVALID_VALUE
,
4102 _("Key file contains escape character "
4107 if (pieces
&& *p
== key_file
->list_separator
)
4108 *q
= key_file
->list_separator
;
4122 g_set_error (error
, G_KEY_FILE_ERROR
,
4123 G_KEY_FILE_ERROR_INVALID_VALUE
,
4124 _("Key file contains invalid escape "
4125 "sequence '%s'"), sequence
);
4134 if (pieces
&& (*p
== key_file
->list_separator
))
4136 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4152 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4153 *pieces
= g_slist_reverse (*pieces
);
4156 return string_value
;
4160 g_key_file_parse_string_as_value (GKeyFile
*key_file
,
4161 const gchar
*string
,
4162 gboolean escape_separator
)
4164 gchar
*value
, *p
, *q
;
4166 gboolean parsing_leading_space
;
4168 length
= strlen (string
) + 1;
4170 /* Worst case would be that every character needs to be escaped.
4171 * In other words every character turns to two characters
4173 value
= g_new (gchar
, 2 * length
);
4175 p
= (gchar
*) string
;
4177 parsing_leading_space
= TRUE
;
4178 while (p
< (string
+ length
- 1))
4180 gchar escaped_character
[3] = { '\\', 0, 0 };
4185 if (parsing_leading_space
)
4187 escaped_character
[1] = 's';
4188 strcpy (q
, escaped_character
);
4198 if (parsing_leading_space
)
4200 escaped_character
[1] = 't';
4201 strcpy (q
, escaped_character
);
4211 escaped_character
[1] = 'n';
4212 strcpy (q
, escaped_character
);
4216 escaped_character
[1] = 'r';
4217 strcpy (q
, escaped_character
);
4221 escaped_character
[1] = '\\';
4222 strcpy (q
, escaped_character
);
4224 parsing_leading_space
= FALSE
;
4227 if (escape_separator
&& *p
== key_file
->list_separator
)
4229 escaped_character
[1] = key_file
->list_separator
;
4230 strcpy (q
, escaped_character
);
4232 parsing_leading_space
= TRUE
;
4238 parsing_leading_space
= FALSE
;
4250 g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
4259 long_value
= strtol (value
, &eof_int
, 10);
4261 if (*value
== '\0' || (*eof_int
!= '\0' && !g_ascii_isspace(*eof_int
)))
4263 gchar
*value_utf8
= _g_utf8_make_valid (value
);
4264 g_set_error (error
, G_KEY_FILE_ERROR
,
4265 G_KEY_FILE_ERROR_INVALID_VALUE
,
4266 _("Value '%s' cannot be interpreted "
4267 "as a number."), value_utf8
);
4268 g_free (value_utf8
);
4273 int_value
= long_value
;
4274 if (int_value
!= long_value
|| errno
== ERANGE
)
4276 gchar
*value_utf8
= _g_utf8_make_valid (value
);
4279 G_KEY_FILE_ERROR_INVALID_VALUE
,
4280 _("Integer value '%s' out of range"),
4282 g_free (value_utf8
);
4291 g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
4295 return g_strdup_printf ("%d", value
);
4299 g_key_file_parse_value_as_double (GKeyFile
*key_file
,
4303 gchar
*end_of_valid_d
;
4304 gdouble double_value
= 0;
4306 double_value
= g_ascii_strtod (value
, &end_of_valid_d
);
4308 if (*end_of_valid_d
!= '\0' || end_of_valid_d
== value
)
4310 gchar
*value_utf8
= _g_utf8_make_valid (value
);
4311 g_set_error (error
, G_KEY_FILE_ERROR
,
4312 G_KEY_FILE_ERROR_INVALID_VALUE
,
4313 _("Value '%s' cannot be interpreted "
4314 "as a float number."),
4316 g_free (value_utf8
);
4319 return double_value
;
4323 strcmp_sized (const gchar
*s1
, size_t len1
, const gchar
*s2
)
4325 size_t len2
= strlen (s2
);
4326 return strncmp (s1
, s2
, MAX (len1
, len2
));
4330 g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
4337 /* Count the number of non-whitespace characters */
4338 for (i
= 0; value
[i
]; i
++)
4339 if (!g_ascii_isspace (value
[i
]))
4342 if (strcmp_sized (value
, length
, "true") == 0 || strcmp_sized (value
, length
, "1") == 0)
4344 else if (strcmp_sized (value
, length
, "false") == 0 || strcmp_sized (value
, length
, "0") == 0)
4347 value_utf8
= _g_utf8_make_valid (value
);
4348 g_set_error (error
, G_KEY_FILE_ERROR
,
4349 G_KEY_FILE_ERROR_INVALID_VALUE
,
4350 _("Value '%s' cannot be interpreted "
4351 "as a boolean."), value_utf8
);
4352 g_free (value_utf8
);
4358 g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
4362 return g_strdup ("true");
4364 return g_strdup ("false");
4368 g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
4375 string
= g_string_sized_new (512);
4377 lines
= g_strsplit (value
, "\n", 0);
4379 for (i
= 0; lines
[i
] != NULL
; i
++)
4381 if (lines
[i
][0] != '#')
4382 g_string_append_printf (string
, "%s\n", lines
[i
]);
4384 g_string_append_printf (string
, "%s\n", lines
[i
] + 1);
4388 return g_string_free (string
, FALSE
);
4392 g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
4393 const gchar
*comment
)
4399 string
= g_string_sized_new (512);
4401 lines
= g_strsplit (comment
, "\n", 0);
4403 for (i
= 0; lines
[i
] != NULL
; i
++)
4404 g_string_append_printf (string
, "#%s%s", lines
[i
],
4405 lines
[i
+ 1] == NULL
? "" : "\n");
4408 return g_string_free (string
, FALSE
);
4412 * g_key_file_save_to_file:
4413 * @key_file: a #GKeyFile
4414 * @filename: the name of the file to write to
4415 * @error: a pointer to a %NULL #GError, or %NULL
4417 * Writes the contents of @key_file to @filename using
4418 * g_file_set_contents().
4420 * This function can fail for any of the reasons that
4421 * g_file_set_contents() may fail.
4423 * Returns: %TRUE if successful, else %FALSE with @error set
4428 g_key_file_save_to_file (GKeyFile
*key_file
,
4429 const gchar
*filename
,
4436 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
4437 g_return_val_if_fail (filename
!= NULL
, FALSE
);
4438 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
4440 contents
= g_key_file_to_data (key_file
, &length
, NULL
);
4441 g_assert (contents
!= NULL
);
4443 success
= g_file_set_contents (filename
, contents
, length
, error
);