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 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
37 #include <sys/types.h>
45 #define fstat(a,b) _fstati64(a,b)
49 #define S_ISREG(mode) ((mode)&_S_IFREG)
52 #endif /* G_OS_WIN23 */
57 #include "gfileutils.h"
63 #include "gmessages.h"
66 #include "gstrfuncs.h"
72 * @title: Key-value file parser
73 * @short_description: parses .ini-like config files
75 * #GKeyFile lets you parse, edit or create files containing groups of
76 * key-value pairs, which we call <firstterm>key files</firstterm> for
77 * lack of a better name. Several freedesktop.org specifications use
78 * key files now, e.g the
79 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
80 * Entry Specification</ulink> and the
81 * <ulink url="http://freedesktop.org/Standards/icon-theme-spec">Icon
82 * Theme Specification</ulink>.
84 * The syntax of key files is described in detail in the
85 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
86 * Entry Specification</ulink>, here is a quick summary: Key files
87 * consists of groups of key-value pairs, interspersed with comments.
90 * # this is just an example
91 * # there can be comments before the first group
95 * Name=Key File Example\tthis value shows\nescaping
97 * # localized strings are stored in multiple key-value pairs
100 * Welcome[fr_FR]=Bonjour
102 * Welcome[be@latin]=Hello
106 * Numbers=2;20;-200;0
108 * Booleans=true;false;true;true
111 * Lines beginning with a '#' and blank lines are considered comments.
113 * Groups are started by a header line containing the group name enclosed
114 * in '[' and ']', and ended implicitly by the start of the next group or
115 * the end of the file. Each key-value pair must be contained in a group.
117 * Key-value pairs generally have the form <literal>key=value</literal>,
118 * with the exception of localized strings, which have the form
119 * <literal>key[locale]=value</literal>, with a locale identifier of the
120 * form <literal>lang_COUNTRY@MODIFIER</literal> where
121 * <literal>COUNTRY</literal> and <literal>MODIFIER</literal> are optional.
122 * Space before and after the '=' character are ignored. Newline, tab,
123 * carriage return and backslash characters in value are escaped as \n,
124 * \t, \r, and \\, respectively. To preserve leading spaces in values,
125 * these can also be escaped as \s.
127 * Key files can store strings (possibly with localized variants), integers,
128 * booleans and lists of these. Lists are separated by a separator character,
129 * typically ';' or ','. To use the list separator character in a value in
130 * a list, it has to be escaped by prefixing it with a backslash.
132 * This syntax is obviously inspired by the .ini files commonly met
133 * on Windows, but there are some important differences:
135 * <listitem>.ini files use the ';' character to begin comments,
136 * key files use the '#' character.</listitem>
137 * <listitem>Key files do not allow for ungrouped keys meaning only
138 * comments can precede the first group.</listitem>
139 * <listitem>Key files are always encoded in UTF-8.</listitem>
140 * <listitem>Key and Group names are case-sensitive. For example, a
141 * group called <literal>[GROUP]</literal> is a different from
142 * <literal>[group]</literal>.</listitem>
143 * <listitem>.ini files don't have a strongly typed boolean entry type,
144 * they only have GetProfileInt(). In key files, only
145 * <literal>true</literal> and <literal>false</literal> (in lower case)
146 * are allowed.</listitem>
149 * Note that in contrast to the
150 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
151 * Entry Specification</ulink>, 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 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
200 * Entry Specification</ulink>. 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 <literal>Application</literal> 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 <literal>Application</literal> 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 <literal>Application</literal> 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 * <literal>Application</literal> 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 <ulink
369 * url="http://www.freedesktop.org/Standards/startup-notification-spec">Startup
370 * Notification Protocol Specification</ulink>.
376 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
378 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
379 * identifying the WM class or name hint of a window that the application
380 * will create, which can be used to emulate Startup Notification with
381 * older applications.
387 * G_KEY_FILE_DESKTOP_KEY_URL :
389 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
390 * giving the URL to access. It is only valid for desktop entries
391 * with the <literal>Link</literal> type.
397 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
399 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
400 * entries representing applications.
406 * G_KEY_FILE_DESKTOP_TYPE_LINK:
408 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
409 * entries representing links to documents.
415 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
417 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
418 * entries representing directories.
423 typedef struct _GKeyFileGroup GKeyFileGroup;
428 * The GKeyFile struct contains only private data
429 * and should not be accessed directly.
434 GHashTable *group_hash;
436 GKeyFileGroup *start_group;
437 GKeyFileGroup *current_group;
439 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
441 /* Used for sizing the output buffer during serialization */
442 gsize approximate_size;
444 gchar list_separator;
450 volatile gint ref_count;
453 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
455 struct _GKeyFileGroup
457 const gchar *name; /* NULL for above first group (which will be comments) */
459 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
460 gboolean has_trailing_blank_line;
462 GList *key_value_pairs;
464 /* Used in parallel with key_value_pairs for
465 * increased lookup performance
467 GHashTable *lookup_map;
470 struct _GKeyFileKeyValuePair
472 gchar *key; /* NULL for comments */
476 static gint find_file_in_data_dirs (const gchar *file,
477 const gchar **data_dirs,
480 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
484 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
485 const gchar *group_name);
486 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
487 const gchar *group_name);
489 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
490 GKeyFileGroup *group,
492 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
493 GKeyFileGroup *group,
496 static void g_key_file_remove_group_node (GKeyFile *key_file,
498 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
499 GKeyFileGroup *group,
502 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
503 GKeyFileGroup *group,
504 GKeyFileKeyValuePair *pair);
505 static void g_key_file_add_key (GKeyFile *key_file,
506 GKeyFileGroup *group,
509 static void g_key_file_add_group (GKeyFile *key_file,
510 const gchar *group_name);
511 static gboolean g_key_file_is_group_name (const gchar *name);
512 static gboolean g_key_file_is_key_name (const gchar *name);
513 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
514 static gboolean g_key_file_line_is_comment (const gchar *line);
515 static gboolean g_key_file_line_is_group (const gchar *line);
516 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
517 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
521 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
523 gboolean escape_separator);
524 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
527 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
529 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
532 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
535 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
537 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
539 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
540 const gchar *comment);
541 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
545 static void g_key_file_parse_comment (GKeyFile *key_file,
549 static void g_key_file_parse_group (GKeyFile *key_file,
553 static gchar *key_get_locale (const gchar *key);
554 static void g_key_file_parse_data (GKeyFile *key_file,
558 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
563 g_key_file_error_quark (void)
565 return g_quark_from_static_string ("g-key-file-error-quark");
569 g_key_file_init (GKeyFile *key_file)
571 key_file->current_group = g_slice_new0 (GKeyFileGroup);
572 key_file->groups = g_list_prepend (NULL, key_file->current_group);
573 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
574 key_file->start_group = NULL;
575 key_file->parse_buffer = g_string_sized_new (128);
576 key_file->approximate_size = 0;
577 key_file->list_separator = ';';
579 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
583 g_key_file_clear (GKeyFile *key_file)
585 GList *tmp, *group_node;
587 if (key_file->locales)
589 g_strfreev (key_file->locales);
590 key_file->locales = NULL;
593 if (key_file->parse_buffer)
595 g_string_free (key_file->parse_buffer, TRUE);
596 key_file->parse_buffer = NULL;
599 tmp = key_file->groups;
604 g_key_file_remove_group_node (key_file, group_node);
607 if (key_file->group_hash != NULL)
609 g_hash_table_destroy (key_file->group_hash);
610 key_file->group_hash = NULL;
613 g_warn_if_fail (key_file->groups == NULL);
620 * Creates a new empty #GKeyFile object. Use
621 * g_key_file_load_from_file(), g_key_file_load_from_data(),
622 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
623 * read an existing key file.
625 * Return value: (transfer full): an empty #GKeyFile.
630 g_key_file_new (void)
634 key_file = g_slice_new0 (GKeyFile);
635 key_file->ref_count = 1;
636 g_key_file_init (key_file);
642 * g_key_file_set_list_separator:
643 * @key_file: a #GKeyFile
644 * @separator: the separator
646 * Sets the character which is used to separate
647 * values in lists. Typically ';' or ',' are used
648 * as separators. The default list separator is ';'.
653 g_key_file_set_list_separator (GKeyFile *key_file,
656 g_return_if_fail (key_file != NULL);
658 key_file->list_separator = separator;
662 /* Iterates through all the directories in *dirs trying to
663 * open file. When it successfully locates and opens a file it
664 * returns the file descriptor to the open file. It also
665 * outputs the absolute path of the file in output_file.
668 find_file_in_data_dirs (const gchar *file,
673 const gchar **data_dirs, *data_dir;
685 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
687 gchar *candidate_file, *sub_dir;
689 candidate_file = (gchar *) file;
690 sub_dir = g_strdup ("");
691 while (candidate_file != NULL && fd == -1)
695 path = g_build_filename (data_dir, sub_dir,
696 candidate_file, NULL);
698 fd = g_open (path, O_RDONLY, 0);
706 candidate_file = strchr (candidate_file, '-');
708 if (candidate_file == NULL)
714 sub_dir = g_strndup (file, candidate_file - file - 1);
716 for (p = sub_dir; *p != '\0'; p++)
719 *p = G_DIR_SEPARATOR;
728 g_set_error_literal (error, G_KEY_FILE_ERROR,
729 G_KEY_FILE_ERROR_NOT_FOUND,
730 _("Valid key file could not be "
731 "found in search dirs"));
734 if (output_file != NULL && fd > 0)
735 *output_file = g_strdup (path);
743 g_key_file_load_from_fd (GKeyFile *key_file,
748 GError *key_file_error = NULL;
750 struct stat stat_buf;
751 gchar read_buf[4096];
753 if (fstat (fd, &stat_buf) < 0)
755 g_set_error_literal (error, G_FILE_ERROR,
756 g_file_error_from_errno (errno),
761 if (!S_ISREG (stat_buf.st_mode))
763 g_set_error_literal (error, G_KEY_FILE_ERROR,
764 G_KEY_FILE_ERROR_PARSE,
765 _("Not a regular file"));
769 if (stat_buf.st_size == 0)
771 g_set_error_literal (error, G_KEY_FILE_ERROR,
772 G_KEY_FILE_ERROR_PARSE,
777 if (key_file->approximate_size > 0)
779 g_key_file_clear (key_file);
780 g_key_file_init (key_file);
782 key_file->flags = flags;
786 bytes_read = read (fd, read_buf, 4096);
788 if (bytes_read == 0) /* End of File */
793 if (errno == EINTR || errno == EAGAIN)
796 g_set_error_literal (error, G_FILE_ERROR,
797 g_file_error_from_errno (errno),
802 g_key_file_parse_data (key_file,
803 read_buf, bytes_read,
806 while (!key_file_error);
810 g_propagate_error (error, key_file_error);
814 g_key_file_flush_parse_buffer (key_file, &key_file_error);
818 g_propagate_error (error, key_file_error);
826 * g_key_file_load_from_file:
827 * @key_file: an empty #GKeyFile struct
828 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
829 * @flags: flags from #GKeyFileFlags
830 * @error: return location for a #GError, or %NULL
832 * Loads a key file into an empty #GKeyFile structure.
833 * If the file could not be loaded then %error is set to
834 * either a #GFileError or #GKeyFileError.
836 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
841 g_key_file_load_from_file (GKeyFile *key_file,
846 GError *key_file_error = NULL;
849 g_return_val_if_fail (key_file != NULL, FALSE);
850 g_return_val_if_fail (file != NULL, FALSE);
852 fd = g_open (file, O_RDONLY, 0);
856 g_set_error_literal (error, G_FILE_ERROR,
857 g_file_error_from_errno (errno),
862 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
867 g_propagate_error (error, key_file_error);
875 * g_key_file_load_from_data:
876 * @key_file: an empty #GKeyFile struct
877 * @data: (array length=length): key file loaded in memory
878 * @length: the length of @data in bytes
879 * @flags: flags from #GKeyFileFlags
880 * @error: return location for a #GError, or %NULL
882 * Loads a key file from memory into an empty #GKeyFile structure.
883 * If the object cannot be created then %error is set to a #GKeyFileError.
885 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
890 g_key_file_load_from_data (GKeyFile *key_file,
896 GError *key_file_error = NULL;
898 g_return_val_if_fail (key_file != NULL, FALSE);
899 g_return_val_if_fail (data != NULL, FALSE);
900 g_return_val_if_fail (length != 0, FALSE);
902 if (length == (gsize)-1)
903 length = strlen (data);
905 if (key_file->approximate_size > 0)
907 g_key_file_clear (key_file);
908 g_key_file_init (key_file);
910 key_file->flags = flags;
912 g_key_file_parse_data (key_file, data, length, &key_file_error);
916 g_propagate_error (error, key_file_error);
920 g_key_file_flush_parse_buffer (key_file, &key_file_error);
924 g_propagate_error (error, key_file_error);
932 * g_key_file_load_from_dirs:
933 * @key_file: an empty #GKeyFile struct
934 * @file: (type filename): a relative path to a filename to open and parse
935 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
936 * @full_path: (out) (type filename): return location for a string containing the full path
937 * of the file, or %NULL
938 * @flags: flags from #GKeyFileFlags
939 * @error: return location for a #GError, or %NULL
941 * This function looks for a key file named @file in the paths
942 * specified in @search_dirs, loads the file into @key_file and
943 * returns the file's full path in @full_path. If the file could not
944 * be loaded then an %error is set to either a #GFileError or
947 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
952 g_key_file_load_from_dirs (GKeyFile *key_file,
954 const gchar **search_dirs,
959 GError *key_file_error = NULL;
960 const gchar **data_dirs;
965 g_return_val_if_fail (key_file != NULL, FALSE);
966 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
967 g_return_val_if_fail (search_dirs != NULL, FALSE);
970 data_dirs = search_dirs;
972 while (*data_dirs != NULL && !found_file)
974 g_free (output_path);
976 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
982 g_propagate_error (error, key_file_error);
986 found_file = g_key_file_load_from_fd (key_file, fd, flags,
992 g_propagate_error (error, key_file_error);
997 if (found_file && full_path)
998 *full_path = output_path;
1000 g_free (output_path);
1006 * g_key_file_load_from_data_dirs:
1007 * @key_file: an empty #GKeyFile struct
1008 * @file: (type filename): a relative path to a filename to open and parse
1009 * @full_path: (out) (type filename): return location for a string containing the full path
1010 * of the file, or %NULL
1011 * @flags: flags from #GKeyFileFlags
1012 * @error: return location for a #GError, or %NULL
1014 * This function looks for a key file named @file in the paths
1015 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1016 * loads the file into @key_file and returns the file's full path in
1017 * @full_path. If the file could not be loaded then an %error is
1018 * set to either a #GFileError or #GKeyFileError.
1020 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1024 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1027 GKeyFileFlags flags,
1030 gchar **all_data_dirs;
1031 const gchar * user_data_dir;
1032 const gchar * const * system_data_dirs;
1034 gboolean found_file;
1036 g_return_val_if_fail (key_file != NULL, FALSE);
1037 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1039 user_data_dir = g_get_user_data_dir ();
1040 system_data_dirs = g_get_system_data_dirs ();
1041 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1044 all_data_dirs[i++] = g_strdup (user_data_dir);
1047 while (system_data_dirs[j] != NULL)
1048 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1049 all_data_dirs[i] = NULL;
1051 found_file = g_key_file_load_from_dirs (key_file,
1053 (const gchar **)all_data_dirs,
1058 g_strfreev (all_data_dirs);
1064 * g_key_file_ref: (skip)
1065 * @key_file: a #GKeyFile
1067 * Increases the reference count of @key_file.
1069 * Returns: the same @key_file.
1074 g_key_file_ref (GKeyFile *key_file)
1076 g_return_val_if_fail (key_file != NULL, NULL);
1078 g_atomic_int_inc (&key_file->ref_count);
1084 * g_key_file_free: (skip)
1085 * @key_file: a #GKeyFile
1087 * Clears all keys and groups from @key_file, and decreases the
1088 * reference count by 1. If the reference count reaches zero,
1089 * frees the key file and all its allocated memory.
1094 g_key_file_free (GKeyFile *key_file)
1096 g_return_if_fail (key_file != NULL);
1098 g_key_file_clear (key_file);
1099 g_key_file_unref (key_file);
1104 * @key_file: a #GKeyFile
1106 * Decreases the reference count of @key_file by 1. If the reference count
1107 * reaches zero, frees the key file and all its allocated memory.
1112 g_key_file_unref (GKeyFile *key_file)
1114 g_return_if_fail (key_file != NULL);
1116 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1118 g_key_file_clear (key_file);
1119 g_slice_free (GKeyFile, key_file);
1123 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1124 * true for locales that match those in g_get_language_names().
1127 g_key_file_locale_is_interesting (GKeyFile *key_file,
1128 const gchar *locale)
1132 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1135 for (i = 0; key_file->locales[i] != NULL; i++)
1137 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1145 g_key_file_parse_line (GKeyFile *key_file,
1150 GError *parse_error = NULL;
1153 g_return_if_fail (key_file != NULL);
1154 g_return_if_fail (line != NULL);
1156 line_start = (gchar *) line;
1157 while (g_ascii_isspace (*line_start))
1160 if (g_key_file_line_is_comment (line_start))
1161 g_key_file_parse_comment (key_file, line, length, &parse_error);
1162 else if (g_key_file_line_is_group (line_start))
1163 g_key_file_parse_group (key_file, line_start,
1164 length - (line_start - line),
1166 else if (g_key_file_line_is_key_value_pair (line_start))
1167 g_key_file_parse_key_value_pair (key_file, line_start,
1168 length - (line_start - line),
1172 gchar *line_utf8 = _g_utf8_make_valid (line);
1173 g_set_error (error, G_KEY_FILE_ERROR,
1174 G_KEY_FILE_ERROR_PARSE,
1175 _("Key file contains line '%s' which is not "
1176 "a key-value pair, group, or comment"),
1184 g_propagate_error (error, parse_error);
1188 g_key_file_parse_comment (GKeyFile *key_file,
1193 GKeyFileKeyValuePair *pair;
1195 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1198 g_warn_if_fail (key_file->current_group != NULL);
1200 pair = g_slice_new (GKeyFileKeyValuePair);
1202 pair->value = g_strndup (line, length);
1204 key_file->current_group->key_value_pairs =
1205 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1207 if (length == 0 || line[0] != '#')
1208 key_file->current_group->has_trailing_blank_line = TRUE;
1212 g_key_file_parse_group (GKeyFile *key_file,
1218 const gchar *group_name_start, *group_name_end;
1220 /* advance past opening '['
1222 group_name_start = line + 1;
1223 group_name_end = line + length - 1;
1225 while (*group_name_end != ']')
1228 group_name = g_strndup (group_name_start,
1229 group_name_end - group_name_start);
1231 if (!g_key_file_is_group_name (group_name))
1233 g_set_error (error, G_KEY_FILE_ERROR,
1234 G_KEY_FILE_ERROR_PARSE,
1235 _("Invalid group name: %s"), group_name);
1236 g_free (group_name);
1240 g_key_file_add_group (key_file, group_name);
1241 g_free (group_name);
1245 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1250 gchar *key, *value, *key_end, *value_start, *locale;
1251 gsize key_len, value_len;
1253 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1255 g_set_error_literal (error, G_KEY_FILE_ERROR,
1256 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1257 _("Key file does not start with a group"));
1261 key_end = value_start = strchr (line, '=');
1263 g_warn_if_fail (key_end != NULL);
1268 /* Pull the key name from the line (chomping trailing whitespace)
1270 while (g_ascii_isspace (*key_end))
1273 key_len = key_end - line + 2;
1275 g_warn_if_fail (key_len <= length);
1277 key = g_strndup (line, key_len - 1);
1279 if (!g_key_file_is_key_name (key))
1281 g_set_error (error, G_KEY_FILE_ERROR,
1282 G_KEY_FILE_ERROR_PARSE,
1283 _("Invalid key name: %s"), key);
1288 /* Pull the value from the line (chugging leading whitespace)
1290 while (g_ascii_isspace (*value_start))
1293 value_len = line + length - value_start + 1;
1295 value = g_strndup (value_start, value_len);
1297 g_warn_if_fail (key_file->start_group != NULL);
1299 if (key_file->current_group
1300 && key_file->current_group->name
1301 && strcmp (key_file->start_group->name,
1302 key_file->current_group->name) == 0
1303 && strcmp (key, "Encoding") == 0)
1305 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1307 gchar *value_utf8 = _g_utf8_make_valid (value);
1308 g_set_error (error, G_KEY_FILE_ERROR,
1309 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1310 _("Key file contains unsupported "
1311 "encoding '%s'"), value_utf8);
1312 g_free (value_utf8);
1320 /* Is this key a translation? If so, is it one that we care about?
1322 locale = key_get_locale (key);
1324 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1326 GKeyFileKeyValuePair *pair;
1328 pair = g_slice_new (GKeyFileKeyValuePair);
1330 pair->value = value;
1332 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1344 key_get_locale (const gchar *key)
1348 locale = g_strrstr (key, "[");
1350 if (locale && strlen (locale) <= 2)
1354 locale = g_strndup (locale + 1, strlen (locale) - 2);
1360 g_key_file_parse_data (GKeyFile *key_file,
1365 GError *parse_error;
1368 g_return_if_fail (key_file != NULL);
1369 g_return_if_fail (data != NULL);
1376 if (data[i] == '\n')
1378 if (key_file->parse_buffer->len > 0
1379 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1381 g_string_erase (key_file->parse_buffer,
1382 key_file->parse_buffer->len - 1,
1385 /* When a newline is encountered flush the parse buffer so that the
1386 * line can be parsed. Note that completely blank lines won't show
1387 * up in the parse buffer, so they get parsed directly.
1389 if (key_file->parse_buffer->len > 0)
1390 g_key_file_flush_parse_buffer (key_file, &parse_error);
1392 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1396 g_propagate_error (error, parse_error);
1403 const gchar *start_of_line;
1404 const gchar *end_of_line;
1407 start_of_line = data + i;
1408 end_of_line = memchr (start_of_line, '\n', length - i);
1410 if (end_of_line == NULL)
1411 end_of_line = data + length;
1413 line_length = end_of_line - start_of_line;
1415 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1420 key_file->approximate_size += length;
1424 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1427 GError *file_error = NULL;
1429 g_return_if_fail (key_file != NULL);
1433 if (key_file->parse_buffer->len > 0)
1435 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1436 key_file->parse_buffer->len,
1438 g_string_erase (key_file->parse_buffer, 0, -1);
1442 g_propagate_error (error, file_error);
1449 * g_key_file_to_data:
1450 * @key_file: a #GKeyFile
1451 * @length: (out) (allow-none): return location for the length of the
1452 * returned string, or %NULL
1453 * @error: return location for a #GError, or %NULL
1455 * This function outputs @key_file as a string.
1457 * Note that this function never reports an error,
1458 * so it is safe to pass %NULL as @error.
1460 * Return value: a newly allocated string holding
1461 * the contents of the #GKeyFile
1466 g_key_file_to_data (GKeyFile *key_file,
1470 GString *data_string;
1471 GList *group_node, *key_file_node;
1472 gboolean has_blank_line = TRUE;
1474 g_return_val_if_fail (key_file != NULL, NULL);
1476 data_string = g_string_sized_new (2 * key_file->approximate_size);
1478 for (group_node = g_list_last (key_file->groups);
1480 group_node = group_node->prev)
1482 GKeyFileGroup *group;
1484 group = (GKeyFileGroup *) group_node->data;
1486 /* separate groups by at least an empty line */
1487 if (!has_blank_line)
1488 g_string_append_c (data_string, '\n');
1489 has_blank_line = group->has_trailing_blank_line;
1491 if (group->comment != NULL)
1492 g_string_append_printf (data_string, "%s\n", group->comment->value);
1494 if (group->name != NULL)
1495 g_string_append_printf (data_string, "[%s]\n", group->name);
1497 for (key_file_node = g_list_last (group->key_value_pairs);
1498 key_file_node != NULL;
1499 key_file_node = key_file_node->prev)
1501 GKeyFileKeyValuePair *pair;
1503 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1505 if (pair->key != NULL)
1506 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1508 g_string_append_printf (data_string, "%s\n", pair->value);
1513 *length = data_string->len;
1515 return g_string_free (data_string, FALSE);
1519 * g_key_file_get_keys:
1520 * @key_file: a #GKeyFile
1521 * @group_name: a group name
1522 * @length: (out): return location for the number of keys returned, or %NULL
1523 * @error: return location for a #GError, or %NULL
1525 * Returns all keys for the group name @group_name. The array of
1526 * returned keys will be %NULL-terminated, so @length may
1527 * optionally be %NULL. In the event that the @group_name cannot
1528 * be found, %NULL is returned and @error is set to
1529 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1531 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1532 * Use g_strfreev() to free it.
1537 g_key_file_get_keys (GKeyFile *key_file,
1538 const gchar *group_name,
1542 GKeyFileGroup *group;
1547 g_return_val_if_fail (key_file != NULL, NULL);
1548 g_return_val_if_fail (group_name != NULL, NULL);
1550 group = g_key_file_lookup_group (key_file, group_name);
1554 g_set_error (error, G_KEY_FILE_ERROR,
1555 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1556 _("Key file does not have group '%s'"),
1557 group_name ? group_name : "(null)");
1562 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1564 GKeyFileKeyValuePair *pair;
1566 pair = (GKeyFileKeyValuePair *) tmp->data;
1572 keys = g_new (gchar *, num_keys + 1);
1575 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1577 GKeyFileKeyValuePair *pair;
1579 pair = (GKeyFileKeyValuePair *) tmp->data;
1583 keys[i] = g_strdup (pair->key);
1588 keys[num_keys] = NULL;
1597 * g_key_file_get_start_group:
1598 * @key_file: a #GKeyFile
1600 * Returns the name of the start group of the file.
1602 * Return value: The start group of the key file.
1607 g_key_file_get_start_group (GKeyFile *key_file)
1609 g_return_val_if_fail (key_file != NULL, NULL);
1611 if (key_file->start_group)
1612 return g_strdup (key_file->start_group->name);
1618 * g_key_file_get_groups:
1619 * @key_file: a #GKeyFile
1620 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1622 * Returns all groups in the key file loaded with @key_file.
1623 * The array of returned groups will be %NULL-terminated, so
1624 * @length may optionally be %NULL.
1626 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1627 * Use g_strfreev() to free it.
1631 g_key_file_get_groups (GKeyFile *key_file,
1636 gsize i, num_groups;
1638 g_return_val_if_fail (key_file != NULL, NULL);
1640 num_groups = g_list_length (key_file->groups);
1642 g_return_val_if_fail (num_groups > 0, NULL);
1644 group_node = g_list_last (key_file->groups);
1646 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1648 /* Only need num_groups instead of num_groups + 1
1649 * because the first group of the file (last in the
1650 * list) is always the comment group at the top,
1653 groups = g_new (gchar *, num_groups);
1657 for (group_node = group_node->prev;
1659 group_node = group_node->prev)
1661 GKeyFileGroup *group;
1663 group = (GKeyFileGroup *) group_node->data;
1665 g_warn_if_fail (group->name != NULL);
1667 groups[i++] = g_strdup (group->name);
1678 * g_key_file_get_value:
1679 * @key_file: a #GKeyFile
1680 * @group_name: a group name
1682 * @error: return location for a #GError, or %NULL
1684 * Returns the raw value associated with @key under @group_name.
1685 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1687 * In the event the key cannot be found, %NULL is returned and
1688 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1689 * event that the @group_name cannot be found, %NULL is returned
1690 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1693 * Return value: a newly allocated string or %NULL if the specified
1694 * key cannot be found.
1699 g_key_file_get_value (GKeyFile *key_file,
1700 const gchar *group_name,
1704 GKeyFileGroup *group;
1705 GKeyFileKeyValuePair *pair;
1706 gchar *value = NULL;
1708 g_return_val_if_fail (key_file != NULL, NULL);
1709 g_return_val_if_fail (group_name != NULL, NULL);
1710 g_return_val_if_fail (key != NULL, NULL);
1712 group = g_key_file_lookup_group (key_file, group_name);
1716 g_set_error (error, G_KEY_FILE_ERROR,
1717 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1718 _("Key file does not have group '%s'"),
1719 group_name ? group_name : "(null)");
1723 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1726 value = g_strdup (pair->value);
1728 g_set_error (error, G_KEY_FILE_ERROR,
1729 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1730 _("Key file does not have key '%s'"), key);
1736 * g_key_file_set_value:
1737 * @key_file: a #GKeyFile
1738 * @group_name: a group name
1742 * Associates a new value with @key under @group_name.
1744 * If @key cannot be found then it is created. If @group_name cannot
1745 * be found then it is created. To set an UTF-8 string which may contain
1746 * characters that need escaping (such as newlines or spaces), use
1747 * g_key_file_set_string().
1752 g_key_file_set_value (GKeyFile *key_file,
1753 const gchar *group_name,
1757 GKeyFileGroup *group;
1758 GKeyFileKeyValuePair *pair;
1760 g_return_if_fail (key_file != NULL);
1761 g_return_if_fail (g_key_file_is_group_name (group_name));
1762 g_return_if_fail (g_key_file_is_key_name (key));
1763 g_return_if_fail (value != NULL);
1765 group = g_key_file_lookup_group (key_file, group_name);
1769 g_key_file_add_group (key_file, group_name);
1770 group = (GKeyFileGroup *) key_file->groups->data;
1772 g_key_file_add_key (key_file, group, key, value);
1776 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1779 g_key_file_add_key (key_file, group, key, value);
1782 g_free (pair->value);
1783 pair->value = g_strdup (value);
1789 * g_key_file_get_string:
1790 * @key_file: a #GKeyFile
1791 * @group_name: a group name
1793 * @error: return location for a #GError, or %NULL
1795 * Returns the string value associated with @key under @group_name.
1796 * Unlike g_key_file_get_value(), this function handles escape sequences
1799 * In the event the key cannot be found, %NULL is returned and
1800 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1801 * event that the @group_name cannot be found, %NULL is returned
1802 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1804 * Return value: a newly allocated string or %NULL if the specified
1805 * key cannot be found.
1810 g_key_file_get_string (GKeyFile *key_file,
1811 const gchar *group_name,
1815 gchar *value, *string_value;
1816 GError *key_file_error;
1818 g_return_val_if_fail (key_file != NULL, NULL);
1819 g_return_val_if_fail (group_name != NULL, NULL);
1820 g_return_val_if_fail (key != NULL, NULL);
1822 key_file_error = NULL;
1824 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1828 g_propagate_error (error, key_file_error);
1832 if (!g_utf8_validate (value, -1, NULL))
1834 gchar *value_utf8 = _g_utf8_make_valid (value);
1835 g_set_error (error, G_KEY_FILE_ERROR,
1836 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1837 _("Key file contains key '%s' with value '%s' "
1838 "which is not UTF-8"), key, value_utf8);
1839 g_free (value_utf8);
1845 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1851 if (g_error_matches (key_file_error,
1853 G_KEY_FILE_ERROR_INVALID_VALUE))
1855 g_set_error (error, G_KEY_FILE_ERROR,
1856 G_KEY_FILE_ERROR_INVALID_VALUE,
1857 _("Key file contains key '%s' "
1858 "which has a value that cannot be interpreted."),
1860 g_error_free (key_file_error);
1863 g_propagate_error (error, key_file_error);
1866 return string_value;
1870 * g_key_file_set_string:
1871 * @key_file: a #GKeyFile
1872 * @group_name: a group name
1876 * Associates a new string value with @key under @group_name.
1877 * If @key cannot be found then it is created.
1878 * If @group_name cannot be found then it is created.
1879 * Unlike g_key_file_set_value(), this function handles characters
1880 * that need escaping, such as newlines.
1885 g_key_file_set_string (GKeyFile *key_file,
1886 const gchar *group_name,
1888 const gchar *string)
1892 g_return_if_fail (key_file != NULL);
1893 g_return_if_fail (string != NULL);
1895 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1896 g_key_file_set_value (key_file, group_name, key, value);
1901 * g_key_file_get_string_list:
1902 * @key_file: a #GKeyFile
1903 * @group_name: a group name
1905 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1906 * @error: return location for a #GError, or %NULL
1908 * Returns the values associated with @key under @group_name.
1910 * In the event the key cannot be found, %NULL is returned and
1911 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1912 * event that the @group_name cannot be found, %NULL is returned
1913 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1915 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1916 * a %NULL-terminated string array or %NULL if the specified
1917 * key cannot be found. The array should be freed with g_strfreev().
1922 g_key_file_get_string_list (GKeyFile *key_file,
1923 const gchar *group_name,
1928 GError *key_file_error = NULL;
1929 gchar *value, *string_value, **values;
1931 GSList *p, *pieces = NULL;
1933 g_return_val_if_fail (key_file != NULL, NULL);
1934 g_return_val_if_fail (group_name != NULL, NULL);
1935 g_return_val_if_fail (key != NULL, NULL);
1940 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1944 g_propagate_error (error, key_file_error);
1948 if (!g_utf8_validate (value, -1, NULL))
1950 gchar *value_utf8 = _g_utf8_make_valid (value);
1951 g_set_error (error, G_KEY_FILE_ERROR,
1952 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1953 _("Key file contains key '%s' with value '%s' "
1954 "which is not UTF-8"), key, value_utf8);
1955 g_free (value_utf8);
1961 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1963 g_free (string_value);
1967 if (g_error_matches (key_file_error,
1969 G_KEY_FILE_ERROR_INVALID_VALUE))
1971 g_set_error (error, G_KEY_FILE_ERROR,
1972 G_KEY_FILE_ERROR_INVALID_VALUE,
1973 _("Key file contains key '%s' "
1974 "which has a value that cannot be interpreted."),
1976 g_error_free (key_file_error);
1979 g_propagate_error (error, key_file_error);
1984 len = g_slist_length (pieces);
1985 values = g_new (gchar *, len + 1);
1986 for (p = pieces, i = 0; p; p = p->next)
1987 values[i++] = p->data;
1990 g_slist_free (pieces);
1999 * g_key_file_set_string_list:
2000 * @key_file: a #GKeyFile
2001 * @group_name: a group name
2003 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2004 * @length: number of string values in @list
2006 * Associates a list of string values for @key under @group_name.
2007 * If @key cannot be found then it is created.
2008 * If @group_name cannot be found then it is created.
2013 g_key_file_set_string_list (GKeyFile *key_file,
2014 const gchar *group_name,
2016 const gchar * const list[],
2019 GString *value_list;
2022 g_return_if_fail (key_file != NULL);
2023 g_return_if_fail (list != NULL || length == 0);
2025 value_list = g_string_sized_new (length * 128);
2026 for (i = 0; i < length && list[i] != NULL; i++)
2030 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2031 g_string_append (value_list, value);
2032 g_string_append_c (value_list, key_file->list_separator);
2037 g_key_file_set_value (key_file, group_name, key, value_list->str);
2038 g_string_free (value_list, TRUE);
2042 * g_key_file_set_locale_string:
2043 * @key_file: a #GKeyFile
2044 * @group_name: a group name
2046 * @locale: a locale identifier
2049 * Associates a string value for @key and @locale under @group_name.
2050 * If the translation for @key cannot be found then it is created.
2055 g_key_file_set_locale_string (GKeyFile *key_file,
2056 const gchar *group_name,
2058 const gchar *locale,
2059 const gchar *string)
2061 gchar *full_key, *value;
2063 g_return_if_fail (key_file != NULL);
2064 g_return_if_fail (key != NULL);
2065 g_return_if_fail (locale != NULL);
2066 g_return_if_fail (string != NULL);
2068 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2069 full_key = g_strdup_printf ("%s[%s]", key, locale);
2070 g_key_file_set_value (key_file, group_name, full_key, value);
2076 * g_key_file_get_locale_string:
2077 * @key_file: a #GKeyFile
2078 * @group_name: a group name
2080 * @locale: (allow-none): a locale identifier or %NULL
2081 * @error: return location for a #GError, or %NULL
2083 * Returns the value associated with @key under @group_name
2084 * translated in the given @locale if available. If @locale is
2085 * %NULL then the current locale is assumed.
2087 * If @key cannot be found then %NULL is returned and @error is set
2088 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2089 * with @key cannot be interpreted or no suitable translation can
2090 * be found then the untranslated value is returned.
2092 * Return value: a newly allocated string or %NULL if the specified
2093 * key cannot be found.
2098 g_key_file_get_locale_string (GKeyFile *key_file,
2099 const gchar *group_name,
2101 const gchar *locale,
2104 gchar *candidate_key, *translated_value;
2105 GError *key_file_error;
2107 gboolean free_languages = FALSE;
2110 g_return_val_if_fail (key_file != NULL, NULL);
2111 g_return_val_if_fail (group_name != NULL, NULL);
2112 g_return_val_if_fail (key != NULL, NULL);
2114 candidate_key = NULL;
2115 translated_value = NULL;
2116 key_file_error = NULL;
2120 languages = g_get_locale_variants (locale);
2121 free_languages = TRUE;
2125 languages = (gchar **) g_get_language_names ();
2126 free_languages = FALSE;
2129 for (i = 0; languages[i]; i++)
2131 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2133 translated_value = g_key_file_get_string (key_file,
2135 candidate_key, NULL);
2136 g_free (candidate_key);
2138 if (translated_value)
2141 g_free (translated_value);
2142 translated_value = NULL;
2145 /* Fallback to untranslated key
2147 if (!translated_value)
2149 translated_value = g_key_file_get_string (key_file, group_name, key,
2152 if (!translated_value)
2153 g_propagate_error (error, key_file_error);
2157 g_strfreev (languages);
2159 return translated_value;
2163 * g_key_file_get_locale_string_list:
2164 * @key_file: a #GKeyFile
2165 * @group_name: a group name
2167 * @locale: (allow-none): a locale identifier or %NULL
2168 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2169 * @error: return location for a #GError or %NULL
2171 * Returns the values associated with @key under @group_name
2172 * translated in the given @locale if available. If @locale is
2173 * %NULL then the current locale is assumed.
2175 * If @key cannot be found then %NULL is returned and @error is set
2176 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2177 * with @key cannot be interpreted or no suitable translations
2178 * can be found then the untranslated values are returned. The
2179 * returned array is %NULL-terminated, so @length may optionally
2182 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2183 * or %NULL if the key isn't found. The string array should be freed
2184 * with g_strfreev().
2189 g_key_file_get_locale_string_list (GKeyFile *key_file,
2190 const gchar *group_name,
2192 const gchar *locale,
2196 GError *key_file_error;
2197 gchar **values, *value;
2198 char list_separator[2];
2201 g_return_val_if_fail (key_file != NULL, NULL);
2202 g_return_val_if_fail (group_name != NULL, NULL);
2203 g_return_val_if_fail (key != NULL, NULL);
2205 key_file_error = NULL;
2207 value = g_key_file_get_locale_string (key_file, group_name,
2212 g_propagate_error (error, key_file_error);
2221 len = strlen (value);
2222 if (value[len - 1] == key_file->list_separator)
2223 value[len - 1] = '\0';
2225 list_separator[0] = key_file->list_separator;
2226 list_separator[1] = '\0';
2227 values = g_strsplit (value, list_separator, 0);
2232 *length = g_strv_length (values);
2238 * g_key_file_set_locale_string_list:
2239 * @key_file: a #GKeyFile
2240 * @group_name: a group name
2242 * @locale: a locale identifier
2243 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2244 * @length: the length of @list
2246 * Associates a list of string values for @key and @locale under
2247 * @group_name. If the translation for @key cannot be found then
2253 g_key_file_set_locale_string_list (GKeyFile *key_file,
2254 const gchar *group_name,
2256 const gchar *locale,
2257 const gchar * const list[],
2260 GString *value_list;
2264 g_return_if_fail (key_file != NULL);
2265 g_return_if_fail (key != NULL);
2266 g_return_if_fail (locale != NULL);
2267 g_return_if_fail (length != 0);
2269 value_list = g_string_sized_new (length * 128);
2270 for (i = 0; i < length && list[i] != NULL; i++)
2274 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2275 g_string_append (value_list, value);
2276 g_string_append_c (value_list, key_file->list_separator);
2281 full_key = g_strdup_printf ("%s[%s]", key, locale);
2282 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2284 g_string_free (value_list, TRUE);
2288 * g_key_file_get_boolean:
2289 * @key_file: a #GKeyFile
2290 * @group_name: a group name
2292 * @error: return location for a #GError
2294 * Returns the value associated with @key under @group_name as a
2297 * If @key cannot be found then %FALSE is returned and @error is set
2298 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2299 * associated with @key cannot be interpreted as a boolean then %FALSE
2300 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2302 * Return value: the value associated with the key as a boolean,
2303 * or %FALSE if the key was not found or could not be parsed.
2308 g_key_file_get_boolean (GKeyFile *key_file,
2309 const gchar *group_name,
2313 GError *key_file_error = NULL;
2315 gboolean bool_value;
2317 g_return_val_if_fail (key_file != NULL, FALSE);
2318 g_return_val_if_fail (group_name != NULL, FALSE);
2319 g_return_val_if_fail (key != NULL, FALSE);
2321 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2325 g_propagate_error (error, key_file_error);
2329 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2335 if (g_error_matches (key_file_error,
2337 G_KEY_FILE_ERROR_INVALID_VALUE))
2339 g_set_error (error, G_KEY_FILE_ERROR,
2340 G_KEY_FILE_ERROR_INVALID_VALUE,
2341 _("Key file contains key '%s' "
2342 "which has a value that cannot be interpreted."),
2344 g_error_free (key_file_error);
2347 g_propagate_error (error, key_file_error);
2354 * g_key_file_set_boolean:
2355 * @key_file: a #GKeyFile
2356 * @group_name: a group name
2358 * @value: %TRUE or %FALSE
2360 * Associates a new boolean value with @key under @group_name.
2361 * If @key cannot be found then it is created.
2366 g_key_file_set_boolean (GKeyFile *key_file,
2367 const gchar *group_name,
2373 g_return_if_fail (key_file != NULL);
2375 result = g_key_file_parse_boolean_as_value (key_file, value);
2376 g_key_file_set_value (key_file, group_name, key, result);
2381 * g_key_file_get_boolean_list:
2382 * @key_file: a #GKeyFile
2383 * @group_name: a group name
2385 * @length: (out): the number of booleans returned
2386 * @error: return location for a #GError
2388 * Returns the values associated with @key under @group_name as
2391 * If @key cannot be found then %NULL is returned and @error is set to
2392 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2393 * with @key cannot be interpreted as booleans then %NULL is returned
2394 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2396 * Return value: (array length=length) (element-type gboolean) (transfer container):
2397 * the values associated with the key as a list of booleans, or %NULL if the
2398 * key was not found or could not be parsed. The returned list of booleans
2399 * should be freed with g_free() when no longer needed.
2404 g_key_file_get_boolean_list (GKeyFile *key_file,
2405 const gchar *group_name,
2410 GError *key_file_error;
2412 gboolean *bool_values;
2415 g_return_val_if_fail (key_file != NULL, NULL);
2416 g_return_val_if_fail (group_name != NULL, NULL);
2417 g_return_val_if_fail (key != NULL, NULL);
2422 key_file_error = NULL;
2424 values = g_key_file_get_string_list (key_file, group_name, key,
2425 &num_bools, &key_file_error);
2428 g_propagate_error (error, key_file_error);
2433 bool_values = g_new (gboolean, num_bools);
2435 for (i = 0; i < num_bools; i++)
2437 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2443 g_propagate_error (error, key_file_error);
2444 g_strfreev (values);
2445 g_free (bool_values);
2450 g_strfreev (values);
2453 *length = num_bools;
2459 * g_key_file_set_boolean_list:
2460 * @key_file: a #GKeyFile
2461 * @group_name: a group name
2463 * @list: (array length=length): an array of boolean values
2464 * @length: length of @list
2466 * Associates a list of boolean values with @key under @group_name.
2467 * If @key cannot be found then it is created.
2468 * If @group_name is %NULL, the start_group is used.
2473 g_key_file_set_boolean_list (GKeyFile *key_file,
2474 const gchar *group_name,
2479 GString *value_list;
2482 g_return_if_fail (key_file != NULL);
2483 g_return_if_fail (list != NULL);
2485 value_list = g_string_sized_new (length * 8);
2486 for (i = 0; i < length; i++)
2490 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2492 g_string_append (value_list, value);
2493 g_string_append_c (value_list, key_file->list_separator);
2498 g_key_file_set_value (key_file, group_name, key, value_list->str);
2499 g_string_free (value_list, TRUE);
2503 * g_key_file_get_integer:
2504 * @key_file: a #GKeyFile
2505 * @group_name: a group name
2507 * @error: return location for a #GError
2509 * Returns the value associated with @key under @group_name as an
2512 * If @key cannot be found then 0 is returned and @error is set to
2513 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2514 * with @key cannot be interpreted as an integer then 0 is returned
2515 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2517 * Return value: the value associated with the key as an integer, or
2518 * 0 if the key was not found or could not be parsed.
2523 g_key_file_get_integer (GKeyFile *key_file,
2524 const gchar *group_name,
2528 GError *key_file_error;
2532 g_return_val_if_fail (key_file != NULL, -1);
2533 g_return_val_if_fail (group_name != NULL, -1);
2534 g_return_val_if_fail (key != NULL, -1);
2536 key_file_error = NULL;
2538 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2542 g_propagate_error (error, key_file_error);
2546 int_value = g_key_file_parse_value_as_integer (key_file, value,
2552 if (g_error_matches (key_file_error,
2554 G_KEY_FILE_ERROR_INVALID_VALUE))
2556 g_set_error (error, G_KEY_FILE_ERROR,
2557 G_KEY_FILE_ERROR_INVALID_VALUE,
2558 _("Key file contains key '%s' in group '%s' "
2559 "which has a value that cannot be interpreted."),
2561 g_error_free (key_file_error);
2564 g_propagate_error (error, key_file_error);
2571 * g_key_file_set_integer:
2572 * @key_file: a #GKeyFile
2573 * @group_name: a group name
2575 * @value: an integer value
2577 * Associates a new integer value with @key under @group_name.
2578 * If @key cannot be found then it is created.
2583 g_key_file_set_integer (GKeyFile *key_file,
2584 const gchar *group_name,
2590 g_return_if_fail (key_file != NULL);
2592 result = g_key_file_parse_integer_as_value (key_file, value);
2593 g_key_file_set_value (key_file, group_name, key, result);
2598 * g_key_file_get_int64:
2599 * @key_file: a non-%NULL #GKeyFile
2600 * @group_name: a non-%NULL group name
2601 * @key: a non-%NULL key
2602 * @error: return location for a #GError
2604 * Returns the value associated with @key under @group_name as a signed
2605 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2606 * 64-bit results without truncation.
2608 * Returns: the value associated with the key as a signed 64-bit integer, or
2609 * 0 if the key was not found or could not be parsed.
2614 g_key_file_get_int64 (GKeyFile *key_file,
2615 const gchar *group_name,
2622 g_return_val_if_fail (key_file != NULL, -1);
2623 g_return_val_if_fail (group_name != NULL, -1);
2624 g_return_val_if_fail (key != NULL, -1);
2626 s = g_key_file_get_value (key_file, group_name, key, error);
2631 v = g_ascii_strtoll (s, &end, 10);
2633 if (*s == '\0' || *end != '\0')
2635 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2636 _("Key '%s' in group '%s' has value '%s' "
2637 "where %s was expected"),
2638 key, group_name, s, "int64");
2647 * g_key_file_set_int64:
2648 * @key_file: a #GKeyFile
2649 * @group_name: a group name
2651 * @value: an integer value
2653 * Associates a new integer value with @key under @group_name.
2654 * If @key cannot be found then it is created.
2659 g_key_file_set_int64 (GKeyFile *key_file,
2660 const gchar *group_name,
2666 g_return_if_fail (key_file != NULL);
2668 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2669 g_key_file_set_value (key_file, group_name, key, result);
2674 * g_key_file_get_uint64:
2675 * @key_file: a non-%NULL #GKeyFile
2676 * @group_name: a non-%NULL group name
2677 * @key: a non-%NULL key
2678 * @error: return location for a #GError
2680 * Returns the value associated with @key under @group_name as an unsigned
2681 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2682 * large positive results without truncation.
2684 * Returns: the value associated with the key as an unsigned 64-bit integer,
2685 * or 0 if the key was not found or could not be parsed.
2690 g_key_file_get_uint64 (GKeyFile *key_file,
2691 const gchar *group_name,
2698 g_return_val_if_fail (key_file != NULL, -1);
2699 g_return_val_if_fail (group_name != NULL, -1);
2700 g_return_val_if_fail (key != NULL, -1);
2702 s = g_key_file_get_value (key_file, group_name, key, error);
2707 v = g_ascii_strtoull (s, &end, 10);
2709 if (*s == '\0' || *end != '\0')
2711 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2712 _("Key '%s' in group '%s' has value '%s' "
2713 "where %s was expected"),
2714 key, group_name, s, "uint64");
2723 * g_key_file_set_uint64:
2724 * @key_file: a #GKeyFile
2725 * @group_name: a group name
2727 * @value: an integer value
2729 * Associates a new integer value with @key under @group_name.
2730 * If @key cannot be found then it is created.
2735 g_key_file_set_uint64 (GKeyFile *key_file,
2736 const gchar *group_name,
2742 g_return_if_fail (key_file != NULL);
2744 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2745 g_key_file_set_value (key_file, group_name, key, result);
2750 * g_key_file_get_integer_list:
2751 * @key_file: a #GKeyFile
2752 * @group_name: a group name
2754 * @length: (out): the number of integers returned
2755 * @error: return location for a #GError
2757 * Returns the values associated with @key under @group_name as
2760 * If @key cannot be found then %NULL is returned and @error is set to
2761 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2762 * with @key cannot be interpreted as integers then %NULL is returned
2763 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2765 * Return value: (array length=length) (element-type gint) (transfer container):
2766 * the values associated with the key as a list of integers, or %NULL if
2767 * the key was not found or could not be parsed. The returned list of
2768 * integers should be freed with g_free() when no longer needed.
2773 g_key_file_get_integer_list (GKeyFile *key_file,
2774 const gchar *group_name,
2779 GError *key_file_error = NULL;
2784 g_return_val_if_fail (key_file != NULL, NULL);
2785 g_return_val_if_fail (group_name != NULL, NULL);
2786 g_return_val_if_fail (key != NULL, NULL);
2791 values = g_key_file_get_string_list (key_file, group_name, key,
2792 &num_ints, &key_file_error);
2795 g_propagate_error (error, key_file_error);
2800 int_values = g_new (gint, num_ints);
2802 for (i = 0; i < num_ints; i++)
2804 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2810 g_propagate_error (error, key_file_error);
2811 g_strfreev (values);
2812 g_free (int_values);
2817 g_strfreev (values);
2826 * g_key_file_set_integer_list:
2827 * @key_file: a #GKeyFile
2828 * @group_name: a group name
2830 * @list: (array length=length): an array of integer values
2831 * @length: number of integer values in @list
2833 * Associates a list of integer values with @key under @group_name.
2834 * If @key cannot be found then it is created.
2839 g_key_file_set_integer_list (GKeyFile *key_file,
2840 const gchar *group_name,
2848 g_return_if_fail (key_file != NULL);
2849 g_return_if_fail (list != NULL);
2851 values = g_string_sized_new (length * 16);
2852 for (i = 0; i < length; i++)
2856 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2858 g_string_append (values, value);
2859 g_string_append_c (values, key_file->list_separator);
2864 g_key_file_set_value (key_file, group_name, key, values->str);
2865 g_string_free (values, TRUE);
2869 * g_key_file_get_double:
2870 * @key_file: a #GKeyFile
2871 * @group_name: a group name
2873 * @error: return location for a #GError
2875 * Returns the value associated with @key under @group_name as a
2876 * double. If @group_name is %NULL, the start_group is used.
2878 * If @key cannot be found then 0.0 is returned and @error is set to
2879 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2880 * with @key cannot be interpreted as a double then 0.0 is returned
2881 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2883 * Return value: the value associated with the key as a double, or
2884 * 0.0 if the key was not found or could not be parsed.
2889 g_key_file_get_double (GKeyFile *key_file,
2890 const gchar *group_name,
2894 GError *key_file_error;
2896 gdouble double_value;
2898 g_return_val_if_fail (key_file != NULL, -1);
2899 g_return_val_if_fail (group_name != NULL, -1);
2900 g_return_val_if_fail (key != NULL, -1);
2902 key_file_error = NULL;
2904 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2908 g_propagate_error (error, key_file_error);
2912 double_value = g_key_file_parse_value_as_double (key_file, value,
2918 if (g_error_matches (key_file_error,
2920 G_KEY_FILE_ERROR_INVALID_VALUE))
2922 g_set_error (error, G_KEY_FILE_ERROR,
2923 G_KEY_FILE_ERROR_INVALID_VALUE,
2924 _("Key file contains key '%s' in group '%s' "
2925 "which has a value that cannot be interpreted."),
2927 g_error_free (key_file_error);
2930 g_propagate_error (error, key_file_error);
2933 return double_value;
2937 * g_key_file_set_double:
2938 * @key_file: a #GKeyFile
2939 * @group_name: a group name
2941 * @value: an double value
2943 * Associates a new double value with @key under @group_name.
2944 * If @key cannot be found then it is created.
2949 g_key_file_set_double (GKeyFile *key_file,
2950 const gchar *group_name,
2954 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2956 g_return_if_fail (key_file != NULL);
2958 g_ascii_dtostr (result, sizeof (result), value);
2959 g_key_file_set_value (key_file, group_name, key, result);
2963 * g_key_file_get_double_list:
2964 * @key_file: a #GKeyFile
2965 * @group_name: a group name
2967 * @length: (out): the number of doubles returned
2968 * @error: return location for a #GError
2970 * Returns the values associated with @key under @group_name as
2973 * If @key cannot be found then %NULL is returned and @error is set to
2974 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2975 * with @key cannot be interpreted as doubles then %NULL is returned
2976 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2978 * Return value: (array length=length) (element-type gdouble) (transfer container):
2979 * the values associated with the key as a list of doubles, or %NULL if the
2980 * key was not found or could not be parsed. The returned list of doubles
2981 * should be freed with g_free() when no longer needed.
2986 g_key_file_get_double_list (GKeyFile *key_file,
2987 const gchar *group_name,
2992 GError *key_file_error = NULL;
2994 gdouble *double_values;
2995 gsize i, num_doubles;
2997 g_return_val_if_fail (key_file != NULL, NULL);
2998 g_return_val_if_fail (group_name != NULL, NULL);
2999 g_return_val_if_fail (key != NULL, NULL);
3004 values = g_key_file_get_string_list (key_file, group_name, key,
3005 &num_doubles, &key_file_error);
3008 g_propagate_error (error, key_file_error);
3013 double_values = g_new (gdouble, num_doubles);
3015 for (i = 0; i < num_doubles; i++)
3017 double_values[i] = g_key_file_parse_value_as_double (key_file,
3023 g_propagate_error (error, key_file_error);
3024 g_strfreev (values);
3025 g_free (double_values);
3030 g_strfreev (values);
3033 *length = num_doubles;
3035 return double_values;
3039 * g_key_file_set_double_list:
3040 * @key_file: a #GKeyFile
3041 * @group_name: a group name
3043 * @list: (array length=length): an array of double values
3044 * @length: number of double values in @list
3046 * Associates a list of double values with @key under
3047 * @group_name. If @key cannot be found then it is created.
3052 g_key_file_set_double_list (GKeyFile *key_file,
3053 const gchar *group_name,
3061 g_return_if_fail (key_file != NULL);
3062 g_return_if_fail (list != NULL);
3064 values = g_string_sized_new (length * 16);
3065 for (i = 0; i < length; i++)
3067 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3069 g_ascii_dtostr( result, sizeof (result), list[i] );
3071 g_string_append (values, result);
3072 g_string_append_c (values, key_file->list_separator);
3075 g_key_file_set_value (key_file, group_name, key, values->str);
3076 g_string_free (values, TRUE);
3080 g_key_file_set_key_comment (GKeyFile *key_file,
3081 const gchar *group_name,
3083 const gchar *comment,
3086 GKeyFileGroup *group;
3087 GKeyFileKeyValuePair *pair;
3088 GList *key_node, *comment_node, *tmp;
3090 group = g_key_file_lookup_group (key_file, group_name);
3093 g_set_error (error, G_KEY_FILE_ERROR,
3094 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3095 _("Key file does not have group '%s'"),
3096 group_name ? group_name : "(null)");
3101 /* First find the key the comments are supposed to be
3104 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3106 if (key_node == NULL)
3108 g_set_error (error, G_KEY_FILE_ERROR,
3109 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3110 _("Key file does not have key '%s' in group '%s'"),
3115 /* Then find all the comments already associated with the
3118 tmp = key_node->next;
3121 pair = (GKeyFileKeyValuePair *) tmp->data;
3123 if (pair->key != NULL)
3128 g_key_file_remove_key_value_pair_node (key_file, group,
3132 if (comment == NULL)
3135 /* Now we can add our new comment
3137 pair = g_slice_new (GKeyFileKeyValuePair);
3139 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3141 key_node = g_list_insert (key_node, pair, 1);
3147 g_key_file_set_group_comment (GKeyFile *key_file,
3148 const gchar *group_name,
3149 const gchar *comment,
3152 GKeyFileGroup *group;
3154 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3156 group = g_key_file_lookup_group (key_file, group_name);
3159 g_set_error (error, G_KEY_FILE_ERROR,
3160 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3161 _("Key file does not have group '%s'"),
3162 group_name ? group_name : "(null)");
3167 /* First remove any existing comment
3171 g_key_file_key_value_pair_free (group->comment);
3172 group->comment = NULL;
3175 if (comment == NULL)
3178 /* Now we can add our new comment
3180 group->comment = g_slice_new (GKeyFileKeyValuePair);
3181 group->comment->key = NULL;
3182 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3188 g_key_file_set_top_comment (GKeyFile *key_file,
3189 const gchar *comment,
3193 GKeyFileGroup *group;
3194 GKeyFileKeyValuePair *pair;
3196 /* The last group in the list should be the top (comments only)
3199 g_warn_if_fail (key_file->groups != NULL);
3200 group_node = g_list_last (key_file->groups);
3201 group = (GKeyFileGroup *) group_node->data;
3202 g_warn_if_fail (group->name == NULL);
3204 /* Note all keys must be comments at the top of
3205 * the file, so we can just free it all.
3207 if (group->key_value_pairs != NULL)
3209 g_list_foreach (group->key_value_pairs,
3210 (GFunc) g_key_file_key_value_pair_free,
3212 g_list_free (group->key_value_pairs);
3213 group->key_value_pairs = NULL;
3216 if (comment == NULL)
3219 pair = g_slice_new (GKeyFileKeyValuePair);
3221 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3223 group->key_value_pairs =
3224 g_list_prepend (group->key_value_pairs, pair);
3230 * g_key_file_set_comment:
3231 * @key_file: a #GKeyFile
3232 * @group_name: (allow-none): a group name, or %NULL
3233 * @key: (allow-none): a key
3234 * @comment: a comment
3235 * @error: return location for a #GError
3237 * Places a comment above @key from @group_name.
3238 * If @key is %NULL then @comment will be written above @group_name.
3239 * If both @key and @group_name are %NULL, then @comment will be
3240 * written above the first group in the file.
3242 * Returns: %TRUE if the comment was written, %FALSE otherwise
3247 g_key_file_set_comment (GKeyFile *key_file,
3248 const gchar *group_name,
3250 const gchar *comment,
3253 g_return_val_if_fail (key_file != NULL, FALSE);
3255 if (group_name != NULL && key != NULL)
3257 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3260 else if (group_name != NULL)
3262 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3267 if (!g_key_file_set_top_comment (key_file, comment, error))
3271 if (comment != NULL)
3272 key_file->approximate_size += strlen (comment);
3278 g_key_file_get_key_comment (GKeyFile *key_file,
3279 const gchar *group_name,
3283 GKeyFileGroup *group;
3284 GKeyFileKeyValuePair *pair;
3285 GList *key_node, *tmp;
3289 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3291 group = g_key_file_lookup_group (key_file, group_name);
3294 g_set_error (error, G_KEY_FILE_ERROR,
3295 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3296 _("Key file does not have group '%s'"),
3297 group_name ? group_name : "(null)");
3302 /* First find the key the comments are supposed to be
3305 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3307 if (key_node == NULL)
3309 g_set_error (error, G_KEY_FILE_ERROR,
3310 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3311 _("Key file does not have key '%s' in group '%s'"),
3318 /* Then find all the comments already associated with the
3319 * key and concatentate them.
3321 tmp = key_node->next;
3322 if (!key_node->next)
3325 pair = (GKeyFileKeyValuePair *) tmp->data;
3326 if (pair->key != NULL)
3331 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3333 if (pair->key != NULL)
3339 while (tmp != key_node)
3341 pair = (GKeyFileKeyValuePair *) tmp->data;
3344 string = g_string_sized_new (512);
3346 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3347 g_string_append (string, comment);
3355 comment = string->str;
3356 g_string_free (string, FALSE);
3365 get_group_comment (GKeyFile *key_file,
3366 GKeyFileGroup *group,
3375 tmp = group->key_value_pairs;
3378 GKeyFileKeyValuePair *pair;
3380 pair = (GKeyFileKeyValuePair *) tmp->data;
3382 if (pair->key != NULL)
3388 if (tmp->next == NULL)
3396 GKeyFileKeyValuePair *pair;
3398 pair = (GKeyFileKeyValuePair *) tmp->data;
3401 string = g_string_sized_new (512);
3403 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3404 g_string_append (string, comment);
3411 return g_string_free (string, FALSE);
3417 g_key_file_get_group_comment (GKeyFile *key_file,
3418 const gchar *group_name,
3422 GKeyFileGroup *group;
3424 group = g_key_file_lookup_group (key_file, group_name);
3427 g_set_error (error, G_KEY_FILE_ERROR,
3428 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3429 _("Key file does not have group '%s'"),
3430 group_name ? group_name : "(null)");
3436 return g_strdup (group->comment->value);
3438 group_node = g_key_file_lookup_group_node (key_file, group_name);
3439 group_node = group_node->next;
3440 group = (GKeyFileGroup *)group_node->data;
3441 return get_group_comment (key_file, group, error);
3445 g_key_file_get_top_comment (GKeyFile *key_file,
3449 GKeyFileGroup *group;
3451 /* The last group in the list should be the top (comments only)
3454 g_warn_if_fail (key_file->groups != NULL);
3455 group_node = g_list_last (key_file->groups);
3456 group = (GKeyFileGroup *) group_node->data;
3457 g_warn_if_fail (group->name == NULL);
3459 return get_group_comment (key_file, group, error);
3463 * g_key_file_get_comment:
3464 * @key_file: a #GKeyFile
3465 * @group_name: a group name, or %NULL
3467 * @error: return location for a #GError
3469 * Retrieves a comment above @key from @group_name.
3470 * If @key is %NULL then @comment will be read from above
3471 * @group_name. If both @key and @group_name are %NULL, then
3472 * @comment will be read from above the first group in the file.
3474 * Returns: a comment that should be freed with g_free()
3479 g_key_file_get_comment (GKeyFile *key_file,
3480 const gchar *group_name,
3484 g_return_val_if_fail (key_file != NULL, NULL);
3486 if (group_name != NULL && key != NULL)
3487 return g_key_file_get_key_comment (key_file, group_name, key, error);
3488 else if (group_name != NULL)
3489 return g_key_file_get_group_comment (key_file, group_name, error);
3491 return g_key_file_get_top_comment (key_file, error);
3495 * g_key_file_remove_comment:
3496 * @key_file: a #GKeyFile
3497 * @group_name: (allow-none): a group name, or %NULL
3498 * @key: (allow-none): a key
3499 * @error: return location for a #GError
3501 * Removes a comment above @key from @group_name.
3502 * If @key is %NULL then @comment will be removed above @group_name.
3503 * If both @key and @group_name are %NULL, then @comment will
3504 * be removed above the first group in the file.
3506 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3512 g_key_file_remove_comment (GKeyFile *key_file,
3513 const gchar *group_name,
3517 g_return_val_if_fail (key_file != NULL, FALSE);
3519 if (group_name != NULL && key != NULL)
3520 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3521 else if (group_name != NULL)
3522 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3524 return g_key_file_set_top_comment (key_file, NULL, error);
3528 * g_key_file_has_group:
3529 * @key_file: a #GKeyFile
3530 * @group_name: a group name
3532 * Looks whether the key file has the group @group_name.
3534 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3539 g_key_file_has_group (GKeyFile *key_file,
3540 const gchar *group_name)
3542 g_return_val_if_fail (key_file != NULL, FALSE);
3543 g_return_val_if_fail (group_name != NULL, FALSE);
3545 return g_key_file_lookup_group (key_file, group_name) != NULL;
3548 /* This code remains from a historical attempt to add a new public API
3549 * which respects the GError rules.
3552 g_key_file_has_key_full (GKeyFile *key_file,
3553 const gchar *group_name,
3558 GKeyFileKeyValuePair *pair;
3559 GKeyFileGroup *group;
3561 g_return_val_if_fail (key_file != NULL, FALSE);
3562 g_return_val_if_fail (group_name != NULL, FALSE);
3563 g_return_val_if_fail (key != NULL, FALSE);
3565 group = g_key_file_lookup_group (key_file, group_name);
3569 g_set_error (error, G_KEY_FILE_ERROR,
3570 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3571 _("Key file does not have group '%s'"),
3572 group_name ? group_name : "(null)");
3577 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3580 *has_key = pair != NULL;
3585 * g_key_file_has_key: (skip)
3586 * @key_file: a #GKeyFile
3587 * @group_name: a group name
3589 * @error: return location for a #GError
3591 * Looks whether the key file has the key @key in the group
3594 * <note>This function does not follow the rules for #GError strictly;
3595 * the return value both carries meaning and signals an error. To use
3596 * this function, you must pass a #GError pointer in @error, and check
3597 * whether it is not %NULL to see if an error occurred.</note>
3599 * Language bindings should use g_key_file_get_value() to test whether
3600 * or not a key exists.
3602 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3608 g_key_file_has_key (GKeyFile *key_file,
3609 const gchar *group_name,
3613 GError *temp_error = NULL;
3616 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3622 g_propagate_error (error, temp_error);
3628 g_key_file_add_group (GKeyFile *key_file,
3629 const gchar *group_name)
3631 GKeyFileGroup *group;
3633 g_return_if_fail (key_file != NULL);
3634 g_return_if_fail (g_key_file_is_group_name (group_name));
3636 group = g_key_file_lookup_group (key_file, group_name);
3639 key_file->current_group = group;
3643 group = g_slice_new0 (GKeyFileGroup);
3644 group->name = g_strdup (group_name);
3645 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3646 key_file->groups = g_list_prepend (key_file->groups, group);
3647 key_file->approximate_size += strlen (group_name) + 3;
3648 key_file->current_group = group;
3650 if (key_file->start_group == NULL)
3651 key_file->start_group = group;
3653 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3657 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3662 g_free (pair->value);
3663 g_slice_free (GKeyFileKeyValuePair, pair);
3667 /* Be careful not to call this function on a node with data in the
3668 * lookup map without removing it from the lookup map, first.
3670 * Some current cases where this warning is not a concern are
3672 * - the node being removed is a comment node
3673 * - the entire lookup map is getting destroyed soon after
3677 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3678 GKeyFileGroup *group,
3682 GKeyFileKeyValuePair *pair;
3684 pair = (GKeyFileKeyValuePair *) pair_node->data;
3686 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3688 if (pair->key != NULL)
3689 key_file->approximate_size -= strlen (pair->key) + 1;
3691 g_warn_if_fail (pair->value != NULL);
3692 key_file->approximate_size -= strlen (pair->value);
3694 g_key_file_key_value_pair_free (pair);
3696 g_list_free_1 (pair_node);
3700 g_key_file_remove_group_node (GKeyFile *key_file,
3703 GKeyFileGroup *group;
3706 group = (GKeyFileGroup *) group_node->data;
3709 g_hash_table_remove (key_file->group_hash, group->name);
3711 /* If the current group gets deleted make the current group the last
3714 if (key_file->current_group == group)
3716 /* groups should always contain at least the top comment group,
3717 * unless g_key_file_clear has been called
3719 if (key_file->groups)
3720 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3722 key_file->current_group = NULL;
3725 /* If the start group gets deleted make the start group the first
3728 if (key_file->start_group == group)
3730 tmp = g_list_last (key_file->groups);
3733 if (tmp != group_node &&
3734 ((GKeyFileGroup *) tmp->data)->name != NULL)
3741 key_file->start_group = (GKeyFileGroup *) tmp->data;
3743 key_file->start_group = NULL;
3746 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3748 if (group->name != NULL)
3749 key_file->approximate_size -= strlen (group->name) + 3;
3751 tmp = group->key_value_pairs;
3758 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3761 g_warn_if_fail (group->key_value_pairs == NULL);
3763 if (group->lookup_map)
3765 g_hash_table_destroy (group->lookup_map);
3766 group->lookup_map = NULL;
3769 g_free ((gchar *) group->name);
3770 g_slice_free (GKeyFileGroup, group);
3771 g_list_free_1 (group_node);
3775 * g_key_file_remove_group:
3776 * @key_file: a #GKeyFile
3777 * @group_name: a group name
3778 * @error: return location for a #GError or %NULL
3780 * Removes the specified group, @group_name,
3781 * from the key file.
3783 * Returns: %TRUE if the group was removed, %FALSE otherwise
3788 g_key_file_remove_group (GKeyFile *key_file,
3789 const gchar *group_name,
3794 g_return_val_if_fail (key_file != NULL, FALSE);
3795 g_return_val_if_fail (group_name != NULL, FALSE);
3797 group_node = g_key_file_lookup_group_node (key_file, group_name);
3801 g_set_error (error, G_KEY_FILE_ERROR,
3802 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3803 _("Key file does not have group '%s'"),
3808 g_key_file_remove_group_node (key_file, group_node);
3814 g_key_file_add_key_value_pair (GKeyFile *key_file,
3815 GKeyFileGroup *group,
3816 GKeyFileKeyValuePair *pair)
3818 g_hash_table_replace (group->lookup_map, pair->key, pair);
3819 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3820 group->has_trailing_blank_line = FALSE;
3821 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3825 g_key_file_add_key (GKeyFile *key_file,
3826 GKeyFileGroup *group,
3830 GKeyFileKeyValuePair *pair;
3832 pair = g_slice_new (GKeyFileKeyValuePair);
3833 pair->key = g_strdup (key);
3834 pair->value = g_strdup (value);
3836 g_key_file_add_key_value_pair (key_file, group, pair);
3840 * g_key_file_remove_key:
3841 * @key_file: a #GKeyFile
3842 * @group_name: a group name
3843 * @key: a key name to remove
3844 * @error: return location for a #GError or %NULL
3846 * Removes @key in @group_name from the key file.
3848 * Returns: %TRUE if the key was removed, %FALSE otherwise
3853 g_key_file_remove_key (GKeyFile *key_file,
3854 const gchar *group_name,
3858 GKeyFileGroup *group;
3859 GKeyFileKeyValuePair *pair;
3861 g_return_val_if_fail (key_file != NULL, FALSE);
3862 g_return_val_if_fail (group_name != NULL, FALSE);
3863 g_return_val_if_fail (key != NULL, FALSE);
3867 group = g_key_file_lookup_group (key_file, group_name);
3870 g_set_error (error, G_KEY_FILE_ERROR,
3871 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3872 _("Key file does not have group '%s'"),
3873 group_name ? group_name : "(null)");
3877 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3881 g_set_error (error, G_KEY_FILE_ERROR,
3882 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3883 _("Key file does not have key '%s' in group '%s'"),
3888 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3890 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3891 g_hash_table_remove (group->lookup_map, pair->key);
3892 g_key_file_key_value_pair_free (pair);
3898 g_key_file_lookup_group_node (GKeyFile *key_file,
3899 const gchar *group_name)
3901 GKeyFileGroup *group;
3904 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3906 group = (GKeyFileGroup *) tmp->data;
3908 if (group && group->name && strcmp (group->name, group_name) == 0)
3915 static GKeyFileGroup *
3916 g_key_file_lookup_group (GKeyFile *key_file,
3917 const gchar *group_name)
3919 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3923 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3924 GKeyFileGroup *group,
3929 for (key_node = group->key_value_pairs;
3931 key_node = key_node->next)
3933 GKeyFileKeyValuePair *pair;
3935 pair = (GKeyFileKeyValuePair *) key_node->data;
3937 if (pair->key && strcmp (pair->key, key) == 0)
3944 static GKeyFileKeyValuePair *
3945 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3946 GKeyFileGroup *group,
3949 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3952 /* Lines starting with # or consisting entirely of whitespace are merely
3953 * recorded, not parsed. This function assumes all leading whitespace
3954 * has been stripped.
3957 g_key_file_line_is_comment (const gchar *line)
3959 return (*line == '#' || *line == '\0' || *line == '\n');
3963 g_key_file_is_group_name (const gchar *name)
3970 p = q = (gchar *) name;
3971 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3972 q = g_utf8_find_next_char (q, NULL);
3974 if (*q != '\0' || q == p)
3981 g_key_file_is_key_name (const gchar *name)
3988 p = q = (gchar *) name;
3989 /* We accept a little more than the desktop entry spec says,
3990 * since gnome-vfs uses mime-types as keys in its cache.
3992 while (*q && *q != '=' && *q != '[' && *q != ']')
3993 q = g_utf8_find_next_char (q, NULL);
3995 /* No empty keys, please */
3999 /* We accept spaces in the middle of keys to not break
4000 * existing apps, but we don't tolerate initial or final
4001 * spaces, which would lead to silent corruption when
4002 * rereading the file.
4004 if (*p == ' ' || q[-1] == ' ')
4010 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4011 q = g_utf8_find_next_char (q, NULL);
4025 /* A group in a key file is made up of a starting '[' followed by one
4026 * or more letters making up the group name followed by ']'.
4029 g_key_file_line_is_group (const gchar *line)
4039 while (*p && *p != ']')
4040 p = g_utf8_find_next_char (p, NULL);
4045 /* silently accept whitespace after the ] */
4046 p = g_utf8_find_next_char (p, NULL);
4047 while (*p == ' ' || *p == '\t')
4048 p = g_utf8_find_next_char (p, NULL);
4057 g_key_file_line_is_key_value_pair (const gchar *line)
4061 p = (gchar *) g_utf8_strchr (line, -1, '=');
4066 /* Key must be non-empty
4075 g_key_file_parse_value_as_string (GKeyFile *key_file,
4080 gchar *string_value, *p, *q0, *q;
4082 string_value = g_new (gchar, strlen (value) + 1);
4084 p = (gchar *) value;
4085 q0 = q = string_value;
4115 g_set_error_literal (error, G_KEY_FILE_ERROR,
4116 G_KEY_FILE_ERROR_INVALID_VALUE,
4117 _("Key file contains escape character "
4122 if (pieces && *p == key_file->list_separator)
4123 *q = key_file->list_separator;
4137 g_set_error (error, G_KEY_FILE_ERROR,
4138 G_KEY_FILE_ERROR_INVALID_VALUE,
4139 _("Key file contains invalid escape "
4140 "sequence '%s'"), sequence);
4149 if (pieces && (*p == key_file->list_separator))
4151 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4167 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4168 *pieces = g_slist_reverse (*pieces);
4171 return string_value;
4175 g_key_file_parse_string_as_value (GKeyFile *key_file,
4176 const gchar *string,
4177 gboolean escape_separator)
4179 gchar *value, *p, *q;
4181 gboolean parsing_leading_space;
4183 length = strlen (string) + 1;
4185 /* Worst case would be that every character needs to be escaped.
4186 * In other words every character turns to two characters
4188 value = g_new (gchar, 2 * length);
4190 p = (gchar *) string;
4192 parsing_leading_space = TRUE;
4193 while (p < (string + length - 1))
4195 gchar escaped_character[3] = { '\\', 0, 0 };
4200 if (parsing_leading_space)
4202 escaped_character[1] = 's';
4203 strcpy (q, escaped_character);
4213 if (parsing_leading_space)
4215 escaped_character[1] = 't';
4216 strcpy (q, escaped_character);
4226 escaped_character[1] = 'n';
4227 strcpy (q, escaped_character);
4231 escaped_character[1] = 'r';
4232 strcpy (q, escaped_character);
4236 escaped_character[1] = '\\';
4237 strcpy (q, escaped_character);
4239 parsing_leading_space = FALSE;
4242 if (escape_separator && *p == key_file->list_separator)
4244 escaped_character[1] = key_file->list_separator;
4245 strcpy (q, escaped_character);
4247 parsing_leading_space = TRUE;
4253 parsing_leading_space = FALSE;
4265 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4274 long_value = strtol (value, &eof_int, 10);
4276 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4278 gchar *value_utf8 = _g_utf8_make_valid (value);
4279 g_set_error (error, G_KEY_FILE_ERROR,
4280 G_KEY_FILE_ERROR_INVALID_VALUE,
4281 _("Value '%s' cannot be interpreted "
4282 "as a number."), value_utf8);
4283 g_free (value_utf8);
4288 int_value = long_value;
4289 if (int_value != long_value || errno == ERANGE)
4291 gchar *value_utf8 = _g_utf8_make_valid (value);
4294 G_KEY_FILE_ERROR_INVALID_VALUE,
4295 _("Integer value '%s' out of range"),
4297 g_free (value_utf8);
4306 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4310 return g_strdup_printf ("%d", value);
4314 g_key_file_parse_value_as_double (GKeyFile *key_file,
4318 gchar *end_of_valid_d;
4319 gdouble double_value = 0;
4321 double_value = g_ascii_strtod (value, &end_of_valid_d);
4323 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4325 gchar *value_utf8 = _g_utf8_make_valid (value);
4326 g_set_error (error, G_KEY_FILE_ERROR,
4327 G_KEY_FILE_ERROR_INVALID_VALUE,
4328 _("Value '%s' cannot be interpreted "
4329 "as a float number."),
4331 g_free (value_utf8);
4334 return double_value;
4338 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4344 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4346 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4349 value_utf8 = _g_utf8_make_valid (value);
4350 g_set_error (error, G_KEY_FILE_ERROR,
4351 G_KEY_FILE_ERROR_INVALID_VALUE,
4352 _("Value '%s' cannot be interpreted "
4353 "as a boolean."), value_utf8);
4354 g_free (value_utf8);
4360 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4364 return g_strdup ("true");
4366 return g_strdup ("false");
4370 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4377 string = g_string_sized_new (512);
4379 lines = g_strsplit (value, "\n", 0);
4381 for (i = 0; lines[i] != NULL; i++)
4383 if (lines[i][0] != '#')
4384 g_string_append_printf (string, "%s\n", lines[i]);
4386 g_string_append_printf (string, "%s\n", lines[i] + 1);
4390 return g_string_free (string, FALSE);
4394 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4395 const gchar *comment)
4401 string = g_string_sized_new (512);
4403 lines = g_strsplit (comment, "\n", 0);
4405 for (i = 0; lines[i] != NULL; i++)
4406 g_string_append_printf (string, "#%s%s", lines[i],
4407 lines[i + 1] == NULL? "" : "\n");
4410 return g_string_free (string, FALSE);