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 (key_file->approximate_size > 0)
771 g_key_file_clear (key_file);
772 g_key_file_init (key_file);
774 key_file->flags = flags;
778 bytes_read = read (fd, read_buf, 4096);
780 if (bytes_read == 0) /* End of File */
785 if (errno == EINTR || errno == EAGAIN)
788 g_set_error_literal (error, G_FILE_ERROR,
789 g_file_error_from_errno (errno),
794 g_key_file_parse_data (key_file,
795 read_buf, bytes_read,
798 while (!key_file_error);
802 g_propagate_error (error, key_file_error);
806 g_key_file_flush_parse_buffer (key_file, &key_file_error);
810 g_propagate_error (error, key_file_error);
818 * g_key_file_load_from_file:
819 * @key_file: an empty #GKeyFile struct
820 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
821 * @flags: flags from #GKeyFileFlags
822 * @error: return location for a #GError, or %NULL
824 * Loads a key file into an empty #GKeyFile structure.
825 * If the file could not be loaded then %error is set to
826 * either a #GFileError or #GKeyFileError.
828 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
833 g_key_file_load_from_file (GKeyFile *key_file,
838 GError *key_file_error = NULL;
841 g_return_val_if_fail (key_file != NULL, FALSE);
842 g_return_val_if_fail (file != NULL, FALSE);
844 fd = g_open (file, O_RDONLY, 0);
848 g_set_error_literal (error, G_FILE_ERROR,
849 g_file_error_from_errno (errno),
854 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
859 g_propagate_error (error, key_file_error);
867 * g_key_file_load_from_data:
868 * @key_file: an empty #GKeyFile struct
869 * @data: (array length=length): key file loaded in memory
870 * @length: the length of @data in bytes
871 * @flags: flags from #GKeyFileFlags
872 * @error: return location for a #GError, or %NULL
874 * Loads a key file from memory into an empty #GKeyFile structure.
875 * If the object cannot be created then %error is set to a #GKeyFileError.
877 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
882 g_key_file_load_from_data (GKeyFile *key_file,
888 GError *key_file_error = NULL;
890 g_return_val_if_fail (key_file != NULL, FALSE);
891 g_return_val_if_fail (data != NULL, FALSE);
892 g_return_val_if_fail (length != 0, FALSE);
894 if (length == (gsize)-1)
895 length = strlen (data);
897 if (key_file->approximate_size > 0)
899 g_key_file_clear (key_file);
900 g_key_file_init (key_file);
902 key_file->flags = flags;
904 g_key_file_parse_data (key_file, data, length, &key_file_error);
908 g_propagate_error (error, key_file_error);
912 g_key_file_flush_parse_buffer (key_file, &key_file_error);
916 g_propagate_error (error, key_file_error);
924 * g_key_file_load_from_dirs:
925 * @key_file: an empty #GKeyFile struct
926 * @file: (type filename): a relative path to a filename to open and parse
927 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
928 * @full_path: (out) (type filename): return location for a string containing the full path
929 * of the file, or %NULL
930 * @flags: flags from #GKeyFileFlags
931 * @error: return location for a #GError, or %NULL
933 * This function looks for a key file named @file in the paths
934 * specified in @search_dirs, loads the file into @key_file and
935 * returns the file's full path in @full_path. If the file could not
936 * be loaded then an %error is set to either a #GFileError or
939 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
944 g_key_file_load_from_dirs (GKeyFile *key_file,
946 const gchar **search_dirs,
951 GError *key_file_error = NULL;
952 const gchar **data_dirs;
957 g_return_val_if_fail (key_file != NULL, FALSE);
958 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
959 g_return_val_if_fail (search_dirs != NULL, FALSE);
962 data_dirs = search_dirs;
964 while (*data_dirs != NULL && !found_file)
966 g_free (output_path);
968 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
974 g_propagate_error (error, key_file_error);
978 found_file = g_key_file_load_from_fd (key_file, fd, flags,
984 g_propagate_error (error, key_file_error);
989 if (found_file && full_path)
990 *full_path = output_path;
992 g_free (output_path);
998 * g_key_file_load_from_data_dirs:
999 * @key_file: an empty #GKeyFile struct
1000 * @file: (type filename): a relative path to a filename to open and parse
1001 * @full_path: (out) (type filename): return location for a string containing the full path
1002 * of the file, or %NULL
1003 * @flags: flags from #GKeyFileFlags
1004 * @error: return location for a #GError, or %NULL
1006 * This function looks for a key file named @file in the paths
1007 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1008 * loads the file into @key_file and returns the file's full path in
1009 * @full_path. If the file could not be loaded then an %error is
1010 * set to either a #GFileError or #GKeyFileError.
1012 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1016 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1019 GKeyFileFlags flags,
1022 gchar **all_data_dirs;
1023 const gchar * user_data_dir;
1024 const gchar * const * system_data_dirs;
1026 gboolean found_file;
1028 g_return_val_if_fail (key_file != NULL, FALSE);
1029 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1031 user_data_dir = g_get_user_data_dir ();
1032 system_data_dirs = g_get_system_data_dirs ();
1033 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1036 all_data_dirs[i++] = g_strdup (user_data_dir);
1039 while (system_data_dirs[j] != NULL)
1040 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1041 all_data_dirs[i] = NULL;
1043 found_file = g_key_file_load_from_dirs (key_file,
1045 (const gchar **)all_data_dirs,
1050 g_strfreev (all_data_dirs);
1056 * g_key_file_ref: (skip)
1057 * @key_file: a #GKeyFile
1059 * Increases the reference count of @key_file.
1061 * Returns: the same @key_file.
1066 g_key_file_ref (GKeyFile *key_file)
1068 g_return_val_if_fail (key_file != NULL, NULL);
1070 g_atomic_int_inc (&key_file->ref_count);
1076 * g_key_file_free: (skip)
1077 * @key_file: a #GKeyFile
1079 * Clears all keys and groups from @key_file, and decreases the
1080 * reference count by 1. If the reference count reaches zero,
1081 * frees the key file and all its allocated memory.
1086 g_key_file_free (GKeyFile *key_file)
1088 g_return_if_fail (key_file != NULL);
1090 g_key_file_clear (key_file);
1091 g_key_file_unref (key_file);
1096 * @key_file: a #GKeyFile
1098 * Decreases the reference count of @key_file by 1. If the reference count
1099 * reaches zero, frees the key file and all its allocated memory.
1104 g_key_file_unref (GKeyFile *key_file)
1106 g_return_if_fail (key_file != NULL);
1108 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1110 g_key_file_clear (key_file);
1111 g_slice_free (GKeyFile, key_file);
1115 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1116 * true for locales that match those in g_get_language_names().
1119 g_key_file_locale_is_interesting (GKeyFile *key_file,
1120 const gchar *locale)
1124 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1127 for (i = 0; key_file->locales[i] != NULL; i++)
1129 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1137 g_key_file_parse_line (GKeyFile *key_file,
1142 GError *parse_error = NULL;
1145 g_return_if_fail (key_file != NULL);
1146 g_return_if_fail (line != NULL);
1148 line_start = (gchar *) line;
1149 while (g_ascii_isspace (*line_start))
1152 if (g_key_file_line_is_comment (line_start))
1153 g_key_file_parse_comment (key_file, line, length, &parse_error);
1154 else if (g_key_file_line_is_group (line_start))
1155 g_key_file_parse_group (key_file, line_start,
1156 length - (line_start - line),
1158 else if (g_key_file_line_is_key_value_pair (line_start))
1159 g_key_file_parse_key_value_pair (key_file, line_start,
1160 length - (line_start - line),
1164 gchar *line_utf8 = _g_utf8_make_valid (line);
1165 g_set_error (error, G_KEY_FILE_ERROR,
1166 G_KEY_FILE_ERROR_PARSE,
1167 _("Key file contains line '%s' which is not "
1168 "a key-value pair, group, or comment"),
1176 g_propagate_error (error, parse_error);
1180 g_key_file_parse_comment (GKeyFile *key_file,
1185 GKeyFileKeyValuePair *pair;
1187 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1190 g_warn_if_fail (key_file->current_group != NULL);
1192 pair = g_slice_new (GKeyFileKeyValuePair);
1194 pair->value = g_strndup (line, length);
1196 key_file->current_group->key_value_pairs =
1197 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1199 if (length == 0 || line[0] != '#')
1200 key_file->current_group->has_trailing_blank_line = TRUE;
1204 g_key_file_parse_group (GKeyFile *key_file,
1210 const gchar *group_name_start, *group_name_end;
1212 /* advance past opening '['
1214 group_name_start = line + 1;
1215 group_name_end = line + length - 1;
1217 while (*group_name_end != ']')
1220 group_name = g_strndup (group_name_start,
1221 group_name_end - group_name_start);
1223 if (!g_key_file_is_group_name (group_name))
1225 g_set_error (error, G_KEY_FILE_ERROR,
1226 G_KEY_FILE_ERROR_PARSE,
1227 _("Invalid group name: %s"), group_name);
1228 g_free (group_name);
1232 g_key_file_add_group (key_file, group_name);
1233 g_free (group_name);
1237 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1242 gchar *key, *value, *key_end, *value_start, *locale;
1243 gsize key_len, value_len;
1245 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1247 g_set_error_literal (error, G_KEY_FILE_ERROR,
1248 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1249 _("Key file does not start with a group"));
1253 key_end = value_start = strchr (line, '=');
1255 g_warn_if_fail (key_end != NULL);
1260 /* Pull the key name from the line (chomping trailing whitespace)
1262 while (g_ascii_isspace (*key_end))
1265 key_len = key_end - line + 2;
1267 g_warn_if_fail (key_len <= length);
1269 key = g_strndup (line, key_len - 1);
1271 if (!g_key_file_is_key_name (key))
1273 g_set_error (error, G_KEY_FILE_ERROR,
1274 G_KEY_FILE_ERROR_PARSE,
1275 _("Invalid key name: %s"), key);
1280 /* Pull the value from the line (chugging leading whitespace)
1282 while (g_ascii_isspace (*value_start))
1285 value_len = line + length - value_start + 1;
1287 value = g_strndup (value_start, value_len);
1289 g_warn_if_fail (key_file->start_group != NULL);
1291 if (key_file->current_group
1292 && key_file->current_group->name
1293 && strcmp (key_file->start_group->name,
1294 key_file->current_group->name) == 0
1295 && strcmp (key, "Encoding") == 0)
1297 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1299 gchar *value_utf8 = _g_utf8_make_valid (value);
1300 g_set_error (error, G_KEY_FILE_ERROR,
1301 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1302 _("Key file contains unsupported "
1303 "encoding '%s'"), value_utf8);
1304 g_free (value_utf8);
1312 /* Is this key a translation? If so, is it one that we care about?
1314 locale = key_get_locale (key);
1316 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1318 GKeyFileKeyValuePair *pair;
1320 pair = g_slice_new (GKeyFileKeyValuePair);
1322 pair->value = value;
1324 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1336 key_get_locale (const gchar *key)
1340 locale = g_strrstr (key, "[");
1342 if (locale && strlen (locale) <= 2)
1346 locale = g_strndup (locale + 1, strlen (locale) - 2);
1352 g_key_file_parse_data (GKeyFile *key_file,
1357 GError *parse_error;
1360 g_return_if_fail (key_file != NULL);
1361 g_return_if_fail (data != NULL);
1368 if (data[i] == '\n')
1370 if (key_file->parse_buffer->len > 0
1371 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1373 g_string_erase (key_file->parse_buffer,
1374 key_file->parse_buffer->len - 1,
1377 /* When a newline is encountered flush the parse buffer so that the
1378 * line can be parsed. Note that completely blank lines won't show
1379 * up in the parse buffer, so they get parsed directly.
1381 if (key_file->parse_buffer->len > 0)
1382 g_key_file_flush_parse_buffer (key_file, &parse_error);
1384 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1388 g_propagate_error (error, parse_error);
1395 const gchar *start_of_line;
1396 const gchar *end_of_line;
1399 start_of_line = data + i;
1400 end_of_line = memchr (start_of_line, '\n', length - i);
1402 if (end_of_line == NULL)
1403 end_of_line = data + length;
1405 line_length = end_of_line - start_of_line;
1407 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1412 key_file->approximate_size += length;
1416 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1419 GError *file_error = NULL;
1421 g_return_if_fail (key_file != NULL);
1425 if (key_file->parse_buffer->len > 0)
1427 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1428 key_file->parse_buffer->len,
1430 g_string_erase (key_file->parse_buffer, 0, -1);
1434 g_propagate_error (error, file_error);
1441 * g_key_file_to_data:
1442 * @key_file: a #GKeyFile
1443 * @length: (out) (allow-none): return location for the length of the
1444 * returned string, or %NULL
1445 * @error: return location for a #GError, or %NULL
1447 * This function outputs @key_file as a string.
1449 * Note that this function never reports an error,
1450 * so it is safe to pass %NULL as @error.
1452 * Return value: a newly allocated string holding
1453 * the contents of the #GKeyFile
1458 g_key_file_to_data (GKeyFile *key_file,
1462 GString *data_string;
1463 GList *group_node, *key_file_node;
1464 gboolean has_blank_line = TRUE;
1466 g_return_val_if_fail (key_file != NULL, NULL);
1468 data_string = g_string_sized_new (2 * key_file->approximate_size);
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 (!has_blank_line)
1480 g_string_append_c (data_string, '\n');
1481 has_blank_line = group->has_trailing_blank_line;
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): 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 * Return value: (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'"),
1549 group_name ? group_name : "(null)");
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 * Return value: 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 * Return value: (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 * g_key_file_get_value:
1671 * @key_file: a #GKeyFile
1672 * @group_name: a group name
1674 * @error: return location for a #GError, or %NULL
1676 * Returns the raw value associated with @key under @group_name.
1677 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1679 * In the event the key cannot be found, %NULL is returned and
1680 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1681 * event that the @group_name cannot be found, %NULL is returned
1682 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1685 * Return value: a newly allocated string or %NULL if the specified
1686 * key cannot be found.
1691 g_key_file_get_value (GKeyFile *key_file,
1692 const gchar *group_name,
1696 GKeyFileGroup *group;
1697 GKeyFileKeyValuePair *pair;
1698 gchar *value = NULL;
1700 g_return_val_if_fail (key_file != NULL, NULL);
1701 g_return_val_if_fail (group_name != NULL, NULL);
1702 g_return_val_if_fail (key != NULL, NULL);
1704 group = g_key_file_lookup_group (key_file, group_name);
1708 g_set_error (error, G_KEY_FILE_ERROR,
1709 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1710 _("Key file does not have group '%s'"),
1711 group_name ? group_name : "(null)");
1715 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1718 value = g_strdup (pair->value);
1720 g_set_error (error, G_KEY_FILE_ERROR,
1721 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1722 _("Key file does not have key '%s'"), key);
1728 * g_key_file_set_value:
1729 * @key_file: a #GKeyFile
1730 * @group_name: a group name
1734 * Associates a new value with @key under @group_name.
1736 * If @key cannot be found then it is created. If @group_name cannot
1737 * be found then it is created. To set an UTF-8 string which may contain
1738 * characters that need escaping (such as newlines or spaces), use
1739 * g_key_file_set_string().
1744 g_key_file_set_value (GKeyFile *key_file,
1745 const gchar *group_name,
1749 GKeyFileGroup *group;
1750 GKeyFileKeyValuePair *pair;
1752 g_return_if_fail (key_file != NULL);
1753 g_return_if_fail (g_key_file_is_group_name (group_name));
1754 g_return_if_fail (g_key_file_is_key_name (key));
1755 g_return_if_fail (value != NULL);
1757 group = g_key_file_lookup_group (key_file, group_name);
1761 g_key_file_add_group (key_file, group_name);
1762 group = (GKeyFileGroup *) key_file->groups->data;
1764 g_key_file_add_key (key_file, group, key, value);
1768 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1771 g_key_file_add_key (key_file, group, key, value);
1774 g_free (pair->value);
1775 pair->value = g_strdup (value);
1781 * g_key_file_get_string:
1782 * @key_file: a #GKeyFile
1783 * @group_name: a group name
1785 * @error: return location for a #GError, or %NULL
1787 * Returns the string value associated with @key under @group_name.
1788 * Unlike g_key_file_get_value(), this function handles escape sequences
1791 * In the event the key cannot be found, %NULL is returned and
1792 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1793 * event that the @group_name cannot be found, %NULL is returned
1794 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1796 * Return value: a newly allocated string or %NULL if the specified
1797 * key cannot be found.
1802 g_key_file_get_string (GKeyFile *key_file,
1803 const gchar *group_name,
1807 gchar *value, *string_value;
1808 GError *key_file_error;
1810 g_return_val_if_fail (key_file != NULL, NULL);
1811 g_return_val_if_fail (group_name != NULL, NULL);
1812 g_return_val_if_fail (key != NULL, NULL);
1814 key_file_error = NULL;
1816 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1820 g_propagate_error (error, key_file_error);
1824 if (!g_utf8_validate (value, -1, NULL))
1826 gchar *value_utf8 = _g_utf8_make_valid (value);
1827 g_set_error (error, G_KEY_FILE_ERROR,
1828 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1829 _("Key file contains key '%s' with value '%s' "
1830 "which is not UTF-8"), key, value_utf8);
1831 g_free (value_utf8);
1837 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1843 if (g_error_matches (key_file_error,
1845 G_KEY_FILE_ERROR_INVALID_VALUE))
1847 g_set_error (error, G_KEY_FILE_ERROR,
1848 G_KEY_FILE_ERROR_INVALID_VALUE,
1849 _("Key file contains key '%s' "
1850 "which has a value that cannot be interpreted."),
1852 g_error_free (key_file_error);
1855 g_propagate_error (error, key_file_error);
1858 return string_value;
1862 * g_key_file_set_string:
1863 * @key_file: a #GKeyFile
1864 * @group_name: a group name
1868 * Associates a new string value with @key under @group_name.
1869 * If @key cannot be found then it is created.
1870 * If @group_name cannot be found then it is created.
1871 * Unlike g_key_file_set_value(), this function handles characters
1872 * that need escaping, such as newlines.
1877 g_key_file_set_string (GKeyFile *key_file,
1878 const gchar *group_name,
1880 const gchar *string)
1884 g_return_if_fail (key_file != NULL);
1885 g_return_if_fail (string != NULL);
1887 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1888 g_key_file_set_value (key_file, group_name, key, value);
1893 * g_key_file_get_string_list:
1894 * @key_file: a #GKeyFile
1895 * @group_name: a group name
1897 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1898 * @error: return location for a #GError, or %NULL
1900 * Returns the values associated with @key under @group_name.
1902 * In the event the key cannot be found, %NULL is returned and
1903 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1904 * event that the @group_name cannot be found, %NULL is returned
1905 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1907 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1908 * a %NULL-terminated string array or %NULL if the specified
1909 * key cannot be found. The array should be freed with g_strfreev().
1914 g_key_file_get_string_list (GKeyFile *key_file,
1915 const gchar *group_name,
1920 GError *key_file_error = NULL;
1921 gchar *value, *string_value, **values;
1923 GSList *p, *pieces = NULL;
1925 g_return_val_if_fail (key_file != NULL, NULL);
1926 g_return_val_if_fail (group_name != NULL, NULL);
1927 g_return_val_if_fail (key != NULL, NULL);
1932 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1936 g_propagate_error (error, key_file_error);
1940 if (!g_utf8_validate (value, -1, NULL))
1942 gchar *value_utf8 = _g_utf8_make_valid (value);
1943 g_set_error (error, G_KEY_FILE_ERROR,
1944 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1945 _("Key file contains key '%s' with value '%s' "
1946 "which is not UTF-8"), key, value_utf8);
1947 g_free (value_utf8);
1953 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1955 g_free (string_value);
1959 if (g_error_matches (key_file_error,
1961 G_KEY_FILE_ERROR_INVALID_VALUE))
1963 g_set_error (error, G_KEY_FILE_ERROR,
1964 G_KEY_FILE_ERROR_INVALID_VALUE,
1965 _("Key file contains key '%s' "
1966 "which has a value that cannot be interpreted."),
1968 g_error_free (key_file_error);
1971 g_propagate_error (error, key_file_error);
1973 g_slist_free_full (pieces, g_free);
1977 len = g_slist_length (pieces);
1978 values = g_new (gchar *, len + 1);
1979 for (p = pieces, i = 0; p; p = p->next)
1980 values[i++] = p->data;
1983 g_slist_free (pieces);
1992 * g_key_file_set_string_list:
1993 * @key_file: a #GKeyFile
1994 * @group_name: a group name
1996 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1997 * @length: number of string values in @list
1999 * Associates a list of string values for @key under @group_name.
2000 * If @key cannot be found then it is created.
2001 * If @group_name cannot be found then it is created.
2006 g_key_file_set_string_list (GKeyFile *key_file,
2007 const gchar *group_name,
2009 const gchar * const list[],
2012 GString *value_list;
2015 g_return_if_fail (key_file != NULL);
2016 g_return_if_fail (list != NULL || length == 0);
2018 value_list = g_string_sized_new (length * 128);
2019 for (i = 0; i < length && list[i] != NULL; i++)
2023 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2024 g_string_append (value_list, value);
2025 g_string_append_c (value_list, key_file->list_separator);
2030 g_key_file_set_value (key_file, group_name, key, value_list->str);
2031 g_string_free (value_list, TRUE);
2035 * g_key_file_set_locale_string:
2036 * @key_file: a #GKeyFile
2037 * @group_name: a group name
2039 * @locale: a locale identifier
2042 * Associates a string value for @key and @locale under @group_name.
2043 * If the translation for @key cannot be found then it is created.
2048 g_key_file_set_locale_string (GKeyFile *key_file,
2049 const gchar *group_name,
2051 const gchar *locale,
2052 const gchar *string)
2054 gchar *full_key, *value;
2056 g_return_if_fail (key_file != NULL);
2057 g_return_if_fail (key != NULL);
2058 g_return_if_fail (locale != NULL);
2059 g_return_if_fail (string != NULL);
2061 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2062 full_key = g_strdup_printf ("%s[%s]", key, locale);
2063 g_key_file_set_value (key_file, group_name, full_key, value);
2069 * g_key_file_get_locale_string:
2070 * @key_file: a #GKeyFile
2071 * @group_name: a group name
2073 * @locale: (allow-none): a locale identifier or %NULL
2074 * @error: return location for a #GError, or %NULL
2076 * Returns the value associated with @key under @group_name
2077 * translated in the given @locale if available. If @locale is
2078 * %NULL then the current locale is assumed.
2080 * If @key cannot be found then %NULL is returned and @error is set
2081 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2082 * with @key cannot be interpreted or no suitable translation can
2083 * be found then the untranslated value is returned.
2085 * Return value: a newly allocated string or %NULL if the specified
2086 * key cannot be found.
2091 g_key_file_get_locale_string (GKeyFile *key_file,
2092 const gchar *group_name,
2094 const gchar *locale,
2097 gchar *candidate_key, *translated_value;
2098 GError *key_file_error;
2100 gboolean free_languages = FALSE;
2103 g_return_val_if_fail (key_file != NULL, NULL);
2104 g_return_val_if_fail (group_name != NULL, NULL);
2105 g_return_val_if_fail (key != NULL, NULL);
2107 candidate_key = NULL;
2108 translated_value = NULL;
2109 key_file_error = NULL;
2113 languages = g_get_locale_variants (locale);
2114 free_languages = TRUE;
2118 languages = (gchar **) g_get_language_names ();
2119 free_languages = FALSE;
2122 for (i = 0; languages[i]; i++)
2124 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2126 translated_value = g_key_file_get_string (key_file,
2128 candidate_key, NULL);
2129 g_free (candidate_key);
2131 if (translated_value)
2134 g_free (translated_value);
2135 translated_value = NULL;
2138 /* Fallback to untranslated key
2140 if (!translated_value)
2142 translated_value = g_key_file_get_string (key_file, group_name, key,
2145 if (!translated_value)
2146 g_propagate_error (error, key_file_error);
2150 g_strfreev (languages);
2152 return translated_value;
2156 * g_key_file_get_locale_string_list:
2157 * @key_file: a #GKeyFile
2158 * @group_name: a group name
2160 * @locale: (allow-none): a locale identifier or %NULL
2161 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2162 * @error: return location for a #GError or %NULL
2164 * Returns the values associated with @key under @group_name
2165 * translated in the given @locale if available. If @locale is
2166 * %NULL then the current locale is assumed.
2168 * If @key cannot be found then %NULL is returned and @error is set
2169 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2170 * with @key cannot be interpreted or no suitable translations
2171 * can be found then the untranslated values are returned. The
2172 * returned array is %NULL-terminated, so @length may optionally
2175 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2176 * or %NULL if the key isn't found. The string array should be freed
2177 * with g_strfreev().
2182 g_key_file_get_locale_string_list (GKeyFile *key_file,
2183 const gchar *group_name,
2185 const gchar *locale,
2189 GError *key_file_error;
2190 gchar **values, *value;
2191 char list_separator[2];
2194 g_return_val_if_fail (key_file != NULL, NULL);
2195 g_return_val_if_fail (group_name != NULL, NULL);
2196 g_return_val_if_fail (key != NULL, NULL);
2198 key_file_error = NULL;
2200 value = g_key_file_get_locale_string (key_file, group_name,
2205 g_propagate_error (error, key_file_error);
2214 len = strlen (value);
2215 if (value[len - 1] == key_file->list_separator)
2216 value[len - 1] = '\0';
2218 list_separator[0] = key_file->list_separator;
2219 list_separator[1] = '\0';
2220 values = g_strsplit (value, list_separator, 0);
2225 *length = g_strv_length (values);
2231 * g_key_file_set_locale_string_list:
2232 * @key_file: a #GKeyFile
2233 * @group_name: a group name
2235 * @locale: a locale identifier
2236 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2237 * @length: the length of @list
2239 * Associates a list of string values for @key and @locale under
2240 * @group_name. If the translation for @key cannot be found then
2246 g_key_file_set_locale_string_list (GKeyFile *key_file,
2247 const gchar *group_name,
2249 const gchar *locale,
2250 const gchar * const list[],
2253 GString *value_list;
2257 g_return_if_fail (key_file != NULL);
2258 g_return_if_fail (key != NULL);
2259 g_return_if_fail (locale != NULL);
2260 g_return_if_fail (length != 0);
2262 value_list = g_string_sized_new (length * 128);
2263 for (i = 0; i < length && list[i] != NULL; i++)
2267 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2268 g_string_append (value_list, value);
2269 g_string_append_c (value_list, key_file->list_separator);
2274 full_key = g_strdup_printf ("%s[%s]", key, locale);
2275 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2277 g_string_free (value_list, TRUE);
2281 * g_key_file_get_boolean:
2282 * @key_file: a #GKeyFile
2283 * @group_name: a group name
2285 * @error: return location for a #GError
2287 * Returns the value associated with @key under @group_name as a
2290 * If @key cannot be found then %FALSE is returned and @error is set
2291 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2292 * associated with @key cannot be interpreted as a boolean then %FALSE
2293 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2295 * Return value: the value associated with the key as a boolean,
2296 * or %FALSE if the key was not found or could not be parsed.
2301 g_key_file_get_boolean (GKeyFile *key_file,
2302 const gchar *group_name,
2306 GError *key_file_error = NULL;
2308 gboolean bool_value;
2310 g_return_val_if_fail (key_file != NULL, FALSE);
2311 g_return_val_if_fail (group_name != NULL, FALSE);
2312 g_return_val_if_fail (key != NULL, FALSE);
2314 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2318 g_propagate_error (error, key_file_error);
2322 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2328 if (g_error_matches (key_file_error,
2330 G_KEY_FILE_ERROR_INVALID_VALUE))
2332 g_set_error (error, G_KEY_FILE_ERROR,
2333 G_KEY_FILE_ERROR_INVALID_VALUE,
2334 _("Key file contains key '%s' "
2335 "which has a value that cannot be interpreted."),
2337 g_error_free (key_file_error);
2340 g_propagate_error (error, key_file_error);
2347 * g_key_file_set_boolean:
2348 * @key_file: a #GKeyFile
2349 * @group_name: a group name
2351 * @value: %TRUE or %FALSE
2353 * Associates a new boolean value with @key under @group_name.
2354 * If @key cannot be found then it is created.
2359 g_key_file_set_boolean (GKeyFile *key_file,
2360 const gchar *group_name,
2366 g_return_if_fail (key_file != NULL);
2368 result = g_key_file_parse_boolean_as_value (key_file, value);
2369 g_key_file_set_value (key_file, group_name, key, result);
2374 * g_key_file_get_boolean_list:
2375 * @key_file: a #GKeyFile
2376 * @group_name: a group name
2378 * @length: (out): the number of booleans returned
2379 * @error: return location for a #GError
2381 * Returns the values associated with @key under @group_name as
2384 * If @key cannot be found then %NULL is returned and @error is set to
2385 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2386 * with @key cannot be interpreted as booleans then %NULL is returned
2387 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2389 * Return value: (array length=length) (element-type gboolean) (transfer container):
2390 * the values associated with the key as a list of booleans, or %NULL if the
2391 * key was not found or could not be parsed. The returned list of booleans
2392 * should be freed with g_free() when no longer needed.
2397 g_key_file_get_boolean_list (GKeyFile *key_file,
2398 const gchar *group_name,
2403 GError *key_file_error;
2405 gboolean *bool_values;
2408 g_return_val_if_fail (key_file != NULL, NULL);
2409 g_return_val_if_fail (group_name != NULL, NULL);
2410 g_return_val_if_fail (key != NULL, NULL);
2415 key_file_error = NULL;
2417 values = g_key_file_get_string_list (key_file, group_name, key,
2418 &num_bools, &key_file_error);
2421 g_propagate_error (error, key_file_error);
2426 bool_values = g_new (gboolean, num_bools);
2428 for (i = 0; i < num_bools; i++)
2430 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2436 g_propagate_error (error, key_file_error);
2437 g_strfreev (values);
2438 g_free (bool_values);
2443 g_strfreev (values);
2446 *length = num_bools;
2452 * g_key_file_set_boolean_list:
2453 * @key_file: a #GKeyFile
2454 * @group_name: a group name
2456 * @list: (array length=length): an array of boolean values
2457 * @length: length of @list
2459 * Associates a list of boolean values with @key under @group_name.
2460 * If @key cannot be found then it is created.
2461 * If @group_name is %NULL, the start_group is used.
2466 g_key_file_set_boolean_list (GKeyFile *key_file,
2467 const gchar *group_name,
2472 GString *value_list;
2475 g_return_if_fail (key_file != NULL);
2476 g_return_if_fail (list != NULL);
2478 value_list = g_string_sized_new (length * 8);
2479 for (i = 0; i < length; i++)
2483 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2485 g_string_append (value_list, value);
2486 g_string_append_c (value_list, key_file->list_separator);
2491 g_key_file_set_value (key_file, group_name, key, value_list->str);
2492 g_string_free (value_list, TRUE);
2496 * g_key_file_get_integer:
2497 * @key_file: a #GKeyFile
2498 * @group_name: a group name
2500 * @error: return location for a #GError
2502 * Returns the value associated with @key under @group_name as an
2505 * If @key cannot be found then 0 is returned and @error is set to
2506 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2507 * with @key cannot be interpreted as an integer then 0 is returned
2508 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2510 * Return value: the value associated with the key as an integer, or
2511 * 0 if the key was not found or could not be parsed.
2516 g_key_file_get_integer (GKeyFile *key_file,
2517 const gchar *group_name,
2521 GError *key_file_error;
2525 g_return_val_if_fail (key_file != NULL, -1);
2526 g_return_val_if_fail (group_name != NULL, -1);
2527 g_return_val_if_fail (key != NULL, -1);
2529 key_file_error = NULL;
2531 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2535 g_propagate_error (error, key_file_error);
2539 int_value = g_key_file_parse_value_as_integer (key_file, value,
2545 if (g_error_matches (key_file_error,
2547 G_KEY_FILE_ERROR_INVALID_VALUE))
2549 g_set_error (error, G_KEY_FILE_ERROR,
2550 G_KEY_FILE_ERROR_INVALID_VALUE,
2551 _("Key file contains key '%s' in group '%s' "
2552 "which has a value that cannot be interpreted."),
2554 g_error_free (key_file_error);
2557 g_propagate_error (error, key_file_error);
2564 * g_key_file_set_integer:
2565 * @key_file: a #GKeyFile
2566 * @group_name: a group name
2568 * @value: an integer value
2570 * Associates a new integer value with @key under @group_name.
2571 * If @key cannot be found then it is created.
2576 g_key_file_set_integer (GKeyFile *key_file,
2577 const gchar *group_name,
2583 g_return_if_fail (key_file != NULL);
2585 result = g_key_file_parse_integer_as_value (key_file, value);
2586 g_key_file_set_value (key_file, group_name, key, result);
2591 * g_key_file_get_int64:
2592 * @key_file: a non-%NULL #GKeyFile
2593 * @group_name: a non-%NULL group name
2594 * @key: a non-%NULL key
2595 * @error: return location for a #GError
2597 * Returns the value associated with @key under @group_name as a signed
2598 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2599 * 64-bit results without truncation.
2601 * Returns: the value associated with the key as a signed 64-bit integer, or
2602 * 0 if the key was not found or could not be parsed.
2607 g_key_file_get_int64 (GKeyFile *key_file,
2608 const gchar *group_name,
2615 g_return_val_if_fail (key_file != NULL, -1);
2616 g_return_val_if_fail (group_name != NULL, -1);
2617 g_return_val_if_fail (key != NULL, -1);
2619 s = g_key_file_get_value (key_file, group_name, key, error);
2624 v = g_ascii_strtoll (s, &end, 10);
2626 if (*s == '\0' || *end != '\0')
2628 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2629 _("Key '%s' in group '%s' has value '%s' "
2630 "where %s was expected"),
2631 key, group_name, s, "int64");
2640 * g_key_file_set_int64:
2641 * @key_file: a #GKeyFile
2642 * @group_name: a group name
2644 * @value: an integer value
2646 * Associates a new integer value with @key under @group_name.
2647 * If @key cannot be found then it is created.
2652 g_key_file_set_int64 (GKeyFile *key_file,
2653 const gchar *group_name,
2659 g_return_if_fail (key_file != NULL);
2661 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2662 g_key_file_set_value (key_file, group_name, key, result);
2667 * g_key_file_get_uint64:
2668 * @key_file: a non-%NULL #GKeyFile
2669 * @group_name: a non-%NULL group name
2670 * @key: a non-%NULL key
2671 * @error: return location for a #GError
2673 * Returns the value associated with @key under @group_name as an unsigned
2674 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2675 * large positive results without truncation.
2677 * Returns: the value associated with the key as an unsigned 64-bit integer,
2678 * or 0 if the key was not found or could not be parsed.
2683 g_key_file_get_uint64 (GKeyFile *key_file,
2684 const gchar *group_name,
2691 g_return_val_if_fail (key_file != NULL, -1);
2692 g_return_val_if_fail (group_name != NULL, -1);
2693 g_return_val_if_fail (key != NULL, -1);
2695 s = g_key_file_get_value (key_file, group_name, key, error);
2700 v = g_ascii_strtoull (s, &end, 10);
2702 if (*s == '\0' || *end != '\0')
2704 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2705 _("Key '%s' in group '%s' has value '%s' "
2706 "where %s was expected"),
2707 key, group_name, s, "uint64");
2716 * g_key_file_set_uint64:
2717 * @key_file: a #GKeyFile
2718 * @group_name: a group name
2720 * @value: an integer value
2722 * Associates a new integer value with @key under @group_name.
2723 * If @key cannot be found then it is created.
2728 g_key_file_set_uint64 (GKeyFile *key_file,
2729 const gchar *group_name,
2735 g_return_if_fail (key_file != NULL);
2737 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2738 g_key_file_set_value (key_file, group_name, key, result);
2743 * g_key_file_get_integer_list:
2744 * @key_file: a #GKeyFile
2745 * @group_name: a group name
2747 * @length: (out): the number of integers returned
2748 * @error: return location for a #GError
2750 * Returns the values associated with @key under @group_name as
2753 * If @key cannot be found then %NULL is returned and @error is set to
2754 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2755 * with @key cannot be interpreted as integers then %NULL is returned
2756 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2758 * Return value: (array length=length) (element-type gint) (transfer container):
2759 * the values associated with the key as a list of integers, or %NULL if
2760 * the key was not found or could not be parsed. The returned list of
2761 * integers should be freed with g_free() when no longer needed.
2766 g_key_file_get_integer_list (GKeyFile *key_file,
2767 const gchar *group_name,
2772 GError *key_file_error = NULL;
2777 g_return_val_if_fail (key_file != NULL, NULL);
2778 g_return_val_if_fail (group_name != NULL, NULL);
2779 g_return_val_if_fail (key != NULL, NULL);
2784 values = g_key_file_get_string_list (key_file, group_name, key,
2785 &num_ints, &key_file_error);
2788 g_propagate_error (error, key_file_error);
2793 int_values = g_new (gint, num_ints);
2795 for (i = 0; i < num_ints; i++)
2797 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2803 g_propagate_error (error, key_file_error);
2804 g_strfreev (values);
2805 g_free (int_values);
2810 g_strfreev (values);
2819 * g_key_file_set_integer_list:
2820 * @key_file: a #GKeyFile
2821 * @group_name: a group name
2823 * @list: (array length=length): an array of integer values
2824 * @length: number of integer values in @list
2826 * Associates a list of integer values with @key under @group_name.
2827 * If @key cannot be found then it is created.
2832 g_key_file_set_integer_list (GKeyFile *key_file,
2833 const gchar *group_name,
2841 g_return_if_fail (key_file != NULL);
2842 g_return_if_fail (list != NULL);
2844 values = g_string_sized_new (length * 16);
2845 for (i = 0; i < length; i++)
2849 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2851 g_string_append (values, value);
2852 g_string_append_c (values, key_file->list_separator);
2857 g_key_file_set_value (key_file, group_name, key, values->str);
2858 g_string_free (values, TRUE);
2862 * g_key_file_get_double:
2863 * @key_file: a #GKeyFile
2864 * @group_name: a group name
2866 * @error: return location for a #GError
2868 * Returns the value associated with @key under @group_name as a
2869 * double. If @group_name is %NULL, the start_group is used.
2871 * If @key cannot be found then 0.0 is returned and @error is set to
2872 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2873 * with @key cannot be interpreted as a double then 0.0 is returned
2874 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2876 * Return value: the value associated with the key as a double, or
2877 * 0.0 if the key was not found or could not be parsed.
2882 g_key_file_get_double (GKeyFile *key_file,
2883 const gchar *group_name,
2887 GError *key_file_error;
2889 gdouble double_value;
2891 g_return_val_if_fail (key_file != NULL, -1);
2892 g_return_val_if_fail (group_name != NULL, -1);
2893 g_return_val_if_fail (key != NULL, -1);
2895 key_file_error = NULL;
2897 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2901 g_propagate_error (error, key_file_error);
2905 double_value = g_key_file_parse_value_as_double (key_file, value,
2911 if (g_error_matches (key_file_error,
2913 G_KEY_FILE_ERROR_INVALID_VALUE))
2915 g_set_error (error, G_KEY_FILE_ERROR,
2916 G_KEY_FILE_ERROR_INVALID_VALUE,
2917 _("Key file contains key '%s' in group '%s' "
2918 "which has a value that cannot be interpreted."),
2920 g_error_free (key_file_error);
2923 g_propagate_error (error, key_file_error);
2926 return double_value;
2930 * g_key_file_set_double:
2931 * @key_file: a #GKeyFile
2932 * @group_name: a group name
2934 * @value: an double value
2936 * Associates a new double value with @key under @group_name.
2937 * If @key cannot be found then it is created.
2942 g_key_file_set_double (GKeyFile *key_file,
2943 const gchar *group_name,
2947 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2949 g_return_if_fail (key_file != NULL);
2951 g_ascii_dtostr (result, sizeof (result), value);
2952 g_key_file_set_value (key_file, group_name, key, result);
2956 * g_key_file_get_double_list:
2957 * @key_file: a #GKeyFile
2958 * @group_name: a group name
2960 * @length: (out): the number of doubles returned
2961 * @error: return location for a #GError
2963 * Returns the values associated with @key under @group_name as
2966 * If @key cannot be found then %NULL is returned and @error is set to
2967 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2968 * with @key cannot be interpreted as doubles then %NULL is returned
2969 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2971 * Return value: (array length=length) (element-type gdouble) (transfer container):
2972 * the values associated with the key as a list of doubles, or %NULL if the
2973 * key was not found or could not be parsed. The returned list of doubles
2974 * should be freed with g_free() when no longer needed.
2979 g_key_file_get_double_list (GKeyFile *key_file,
2980 const gchar *group_name,
2985 GError *key_file_error = NULL;
2987 gdouble *double_values;
2988 gsize i, num_doubles;
2990 g_return_val_if_fail (key_file != NULL, NULL);
2991 g_return_val_if_fail (group_name != NULL, NULL);
2992 g_return_val_if_fail (key != NULL, NULL);
2997 values = g_key_file_get_string_list (key_file, group_name, key,
2998 &num_doubles, &key_file_error);
3001 g_propagate_error (error, key_file_error);
3006 double_values = g_new (gdouble, num_doubles);
3008 for (i = 0; i < num_doubles; i++)
3010 double_values[i] = g_key_file_parse_value_as_double (key_file,
3016 g_propagate_error (error, key_file_error);
3017 g_strfreev (values);
3018 g_free (double_values);
3023 g_strfreev (values);
3026 *length = num_doubles;
3028 return double_values;
3032 * g_key_file_set_double_list:
3033 * @key_file: a #GKeyFile
3034 * @group_name: a group name
3036 * @list: (array length=length): an array of double values
3037 * @length: number of double values in @list
3039 * Associates a list of double values with @key under
3040 * @group_name. If @key cannot be found then it is created.
3045 g_key_file_set_double_list (GKeyFile *key_file,
3046 const gchar *group_name,
3054 g_return_if_fail (key_file != NULL);
3055 g_return_if_fail (list != NULL);
3057 values = g_string_sized_new (length * 16);
3058 for (i = 0; i < length; i++)
3060 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3062 g_ascii_dtostr( result, sizeof (result), list[i] );
3064 g_string_append (values, result);
3065 g_string_append_c (values, key_file->list_separator);
3068 g_key_file_set_value (key_file, group_name, key, values->str);
3069 g_string_free (values, TRUE);
3073 g_key_file_set_key_comment (GKeyFile *key_file,
3074 const gchar *group_name,
3076 const gchar *comment,
3079 GKeyFileGroup *group;
3080 GKeyFileKeyValuePair *pair;
3081 GList *key_node, *comment_node, *tmp;
3083 group = g_key_file_lookup_group (key_file, group_name);
3086 g_set_error (error, G_KEY_FILE_ERROR,
3087 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3088 _("Key file does not have group '%s'"),
3089 group_name ? group_name : "(null)");
3094 /* First find the key the comments are supposed to be
3097 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3099 if (key_node == NULL)
3101 g_set_error (error, G_KEY_FILE_ERROR,
3102 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3103 _("Key file does not have key '%s' in group '%s'"),
3108 /* Then find all the comments already associated with the
3111 tmp = key_node->next;
3114 pair = (GKeyFileKeyValuePair *) tmp->data;
3116 if (pair->key != NULL)
3121 g_key_file_remove_key_value_pair_node (key_file, group,
3125 if (comment == NULL)
3128 /* Now we can add our new comment
3130 pair = g_slice_new (GKeyFileKeyValuePair);
3132 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3134 key_node = g_list_insert (key_node, pair, 1);
3140 g_key_file_set_group_comment (GKeyFile *key_file,
3141 const gchar *group_name,
3142 const gchar *comment,
3145 GKeyFileGroup *group;
3147 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3149 group = g_key_file_lookup_group (key_file, group_name);
3152 g_set_error (error, G_KEY_FILE_ERROR,
3153 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3154 _("Key file does not have group '%s'"),
3155 group_name ? group_name : "(null)");
3160 /* First remove any existing comment
3164 g_key_file_key_value_pair_free (group->comment);
3165 group->comment = NULL;
3168 if (comment == NULL)
3171 /* Now we can add our new comment
3173 group->comment = g_slice_new (GKeyFileKeyValuePair);
3174 group->comment->key = NULL;
3175 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3181 g_key_file_set_top_comment (GKeyFile *key_file,
3182 const gchar *comment,
3186 GKeyFileGroup *group;
3187 GKeyFileKeyValuePair *pair;
3189 /* The last group in the list should be the top (comments only)
3192 g_warn_if_fail (key_file->groups != NULL);
3193 group_node = g_list_last (key_file->groups);
3194 group = (GKeyFileGroup *) group_node->data;
3195 g_warn_if_fail (group->name == NULL);
3197 /* Note all keys must be comments at the top of
3198 * the file, so we can just free it all.
3200 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3201 group->key_value_pairs = NULL;
3203 if (comment == NULL)
3206 pair = g_slice_new (GKeyFileKeyValuePair);
3208 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3210 group->key_value_pairs =
3211 g_list_prepend (group->key_value_pairs, pair);
3217 * g_key_file_set_comment:
3218 * @key_file: a #GKeyFile
3219 * @group_name: (allow-none): a group name, or %NULL
3220 * @key: (allow-none): a key
3221 * @comment: a comment
3222 * @error: return location for a #GError
3224 * Places a comment above @key from @group_name.
3225 * If @key is %NULL then @comment will be written above @group_name.
3226 * If both @key and @group_name are %NULL, then @comment will be
3227 * written above the first group in the file.
3229 * Returns: %TRUE if the comment was written, %FALSE otherwise
3234 g_key_file_set_comment (GKeyFile *key_file,
3235 const gchar *group_name,
3237 const gchar *comment,
3240 g_return_val_if_fail (key_file != NULL, FALSE);
3242 if (group_name != NULL && key != NULL)
3244 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3247 else if (group_name != NULL)
3249 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3254 if (!g_key_file_set_top_comment (key_file, comment, error))
3258 if (comment != NULL)
3259 key_file->approximate_size += strlen (comment);
3265 g_key_file_get_key_comment (GKeyFile *key_file,
3266 const gchar *group_name,
3270 GKeyFileGroup *group;
3271 GKeyFileKeyValuePair *pair;
3272 GList *key_node, *tmp;
3276 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3278 group = g_key_file_lookup_group (key_file, group_name);
3281 g_set_error (error, G_KEY_FILE_ERROR,
3282 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3283 _("Key file does not have group '%s'"),
3284 group_name ? group_name : "(null)");
3289 /* First find the key the comments are supposed to be
3292 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3294 if (key_node == NULL)
3296 g_set_error (error, G_KEY_FILE_ERROR,
3297 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3298 _("Key file does not have key '%s' in group '%s'"),
3305 /* Then find all the comments already associated with the
3306 * key and concatentate them.
3308 tmp = key_node->next;
3309 if (!key_node->next)
3312 pair = (GKeyFileKeyValuePair *) tmp->data;
3313 if (pair->key != NULL)
3318 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3320 if (pair->key != NULL)
3326 while (tmp != key_node)
3328 pair = (GKeyFileKeyValuePair *) tmp->data;
3331 string = g_string_sized_new (512);
3333 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3334 g_string_append (string, comment);
3342 comment = string->str;
3343 g_string_free (string, FALSE);
3352 get_group_comment (GKeyFile *key_file,
3353 GKeyFileGroup *group,
3362 tmp = group->key_value_pairs;
3365 GKeyFileKeyValuePair *pair;
3367 pair = (GKeyFileKeyValuePair *) tmp->data;
3369 if (pair->key != NULL)
3375 if (tmp->next == NULL)
3383 GKeyFileKeyValuePair *pair;
3385 pair = (GKeyFileKeyValuePair *) tmp->data;
3388 string = g_string_sized_new (512);
3390 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3391 g_string_append (string, comment);
3398 return g_string_free (string, FALSE);
3404 g_key_file_get_group_comment (GKeyFile *key_file,
3405 const gchar *group_name,
3409 GKeyFileGroup *group;
3411 group = g_key_file_lookup_group (key_file, group_name);
3414 g_set_error (error, G_KEY_FILE_ERROR,
3415 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3416 _("Key file does not have group '%s'"),
3417 group_name ? group_name : "(null)");
3423 return g_strdup (group->comment->value);
3425 group_node = g_key_file_lookup_group_node (key_file, group_name);
3426 group_node = group_node->next;
3427 group = (GKeyFileGroup *)group_node->data;
3428 return get_group_comment (key_file, group, error);
3432 g_key_file_get_top_comment (GKeyFile *key_file,
3436 GKeyFileGroup *group;
3438 /* The last group in the list should be the top (comments only)
3441 g_warn_if_fail (key_file->groups != NULL);
3442 group_node = g_list_last (key_file->groups);
3443 group = (GKeyFileGroup *) group_node->data;
3444 g_warn_if_fail (group->name == NULL);
3446 return get_group_comment (key_file, group, error);
3450 * g_key_file_get_comment:
3451 * @key_file: a #GKeyFile
3452 * @group_name: a group name, or %NULL
3454 * @error: return location for a #GError
3456 * Retrieves a comment above @key from @group_name.
3457 * If @key is %NULL then @comment will be read from above
3458 * @group_name. If both @key and @group_name are %NULL, then
3459 * @comment will be read from above the first group in the file.
3461 * Returns: a comment that should be freed with g_free()
3466 g_key_file_get_comment (GKeyFile *key_file,
3467 const gchar *group_name,
3471 g_return_val_if_fail (key_file != NULL, NULL);
3473 if (group_name != NULL && key != NULL)
3474 return g_key_file_get_key_comment (key_file, group_name, key, error);
3475 else if (group_name != NULL)
3476 return g_key_file_get_group_comment (key_file, group_name, error);
3478 return g_key_file_get_top_comment (key_file, error);
3482 * g_key_file_remove_comment:
3483 * @key_file: a #GKeyFile
3484 * @group_name: (allow-none): a group name, or %NULL
3485 * @key: (allow-none): a key
3486 * @error: return location for a #GError
3488 * Removes a comment above @key from @group_name.
3489 * If @key is %NULL then @comment will be removed above @group_name.
3490 * If both @key and @group_name are %NULL, then @comment will
3491 * be removed above the first group in the file.
3493 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3499 g_key_file_remove_comment (GKeyFile *key_file,
3500 const gchar *group_name,
3504 g_return_val_if_fail (key_file != NULL, FALSE);
3506 if (group_name != NULL && key != NULL)
3507 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3508 else if (group_name != NULL)
3509 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3511 return g_key_file_set_top_comment (key_file, NULL, error);
3515 * g_key_file_has_group:
3516 * @key_file: a #GKeyFile
3517 * @group_name: a group name
3519 * Looks whether the key file has the group @group_name.
3521 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3526 g_key_file_has_group (GKeyFile *key_file,
3527 const gchar *group_name)
3529 g_return_val_if_fail (key_file != NULL, FALSE);
3530 g_return_val_if_fail (group_name != NULL, FALSE);
3532 return g_key_file_lookup_group (key_file, group_name) != NULL;
3535 /* This code remains from a historical attempt to add a new public API
3536 * which respects the GError rules.
3539 g_key_file_has_key_full (GKeyFile *key_file,
3540 const gchar *group_name,
3545 GKeyFileKeyValuePair *pair;
3546 GKeyFileGroup *group;
3548 g_return_val_if_fail (key_file != NULL, FALSE);
3549 g_return_val_if_fail (group_name != NULL, FALSE);
3550 g_return_val_if_fail (key != NULL, FALSE);
3552 group = g_key_file_lookup_group (key_file, group_name);
3556 g_set_error (error, G_KEY_FILE_ERROR,
3557 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3558 _("Key file does not have group '%s'"),
3559 group_name ? group_name : "(null)");
3564 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3567 *has_key = pair != NULL;
3572 * g_key_file_has_key: (skip)
3573 * @key_file: a #GKeyFile
3574 * @group_name: a group name
3576 * @error: return location for a #GError
3578 * Looks whether the key file has the key @key in the group
3581 * <note>This function does not follow the rules for #GError strictly;
3582 * the return value both carries meaning and signals an error. To use
3583 * this function, you must pass a #GError pointer in @error, and check
3584 * whether it is not %NULL to see if an error occurred.</note>
3586 * Language bindings should use g_key_file_get_value() to test whether
3587 * or not a key exists.
3589 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3595 g_key_file_has_key (GKeyFile *key_file,
3596 const gchar *group_name,
3600 GError *temp_error = NULL;
3603 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3609 g_propagate_error (error, temp_error);
3615 g_key_file_add_group (GKeyFile *key_file,
3616 const gchar *group_name)
3618 GKeyFileGroup *group;
3620 g_return_if_fail (key_file != NULL);
3621 g_return_if_fail (g_key_file_is_group_name (group_name));
3623 group = g_key_file_lookup_group (key_file, group_name);
3626 key_file->current_group = group;
3630 group = g_slice_new0 (GKeyFileGroup);
3631 group->name = g_strdup (group_name);
3632 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3633 key_file->groups = g_list_prepend (key_file->groups, group);
3634 key_file->approximate_size += strlen (group_name) + 3;
3635 key_file->current_group = group;
3637 if (key_file->start_group == NULL)
3638 key_file->start_group = group;
3640 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3644 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3649 g_free (pair->value);
3650 g_slice_free (GKeyFileKeyValuePair, pair);
3654 /* Be careful not to call this function on a node with data in the
3655 * lookup map without removing it from the lookup map, first.
3657 * Some current cases where this warning is not a concern are
3659 * - the node being removed is a comment node
3660 * - the entire lookup map is getting destroyed soon after
3664 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3665 GKeyFileGroup *group,
3669 GKeyFileKeyValuePair *pair;
3671 pair = (GKeyFileKeyValuePair *) pair_node->data;
3673 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3675 if (pair->key != NULL)
3676 key_file->approximate_size -= strlen (pair->key) + 1;
3678 g_warn_if_fail (pair->value != NULL);
3679 key_file->approximate_size -= strlen (pair->value);
3681 g_key_file_key_value_pair_free (pair);
3683 g_list_free_1 (pair_node);
3687 g_key_file_remove_group_node (GKeyFile *key_file,
3690 GKeyFileGroup *group;
3693 group = (GKeyFileGroup *) group_node->data;
3696 g_hash_table_remove (key_file->group_hash, group->name);
3698 /* If the current group gets deleted make the current group the last
3701 if (key_file->current_group == group)
3703 /* groups should always contain at least the top comment group,
3704 * unless g_key_file_clear has been called
3706 if (key_file->groups)
3707 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3709 key_file->current_group = NULL;
3712 /* If the start group gets deleted make the start group the first
3715 if (key_file->start_group == group)
3717 tmp = g_list_last (key_file->groups);
3720 if (tmp != group_node &&
3721 ((GKeyFileGroup *) tmp->data)->name != NULL)
3728 key_file->start_group = (GKeyFileGroup *) tmp->data;
3730 key_file->start_group = NULL;
3733 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3735 if (group->name != NULL)
3736 key_file->approximate_size -= strlen (group->name) + 3;
3738 tmp = group->key_value_pairs;
3745 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3748 g_warn_if_fail (group->key_value_pairs == NULL);
3752 g_key_file_key_value_pair_free (group->comment);
3753 group->comment = NULL;
3756 if (group->lookup_map)
3758 g_hash_table_destroy (group->lookup_map);
3759 group->lookup_map = NULL;
3762 g_free ((gchar *) group->name);
3763 g_slice_free (GKeyFileGroup, group);
3764 g_list_free_1 (group_node);
3768 * g_key_file_remove_group:
3769 * @key_file: a #GKeyFile
3770 * @group_name: a group name
3771 * @error: return location for a #GError or %NULL
3773 * Removes the specified group, @group_name,
3774 * from the key file.
3776 * Returns: %TRUE if the group was removed, %FALSE otherwise
3781 g_key_file_remove_group (GKeyFile *key_file,
3782 const gchar *group_name,
3787 g_return_val_if_fail (key_file != NULL, FALSE);
3788 g_return_val_if_fail (group_name != NULL, FALSE);
3790 group_node = g_key_file_lookup_group_node (key_file, group_name);
3794 g_set_error (error, G_KEY_FILE_ERROR,
3795 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3796 _("Key file does not have group '%s'"),
3801 g_key_file_remove_group_node (key_file, group_node);
3807 g_key_file_add_key_value_pair (GKeyFile *key_file,
3808 GKeyFileGroup *group,
3809 GKeyFileKeyValuePair *pair)
3811 g_hash_table_replace (group->lookup_map, pair->key, pair);
3812 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3813 group->has_trailing_blank_line = FALSE;
3814 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3818 g_key_file_add_key (GKeyFile *key_file,
3819 GKeyFileGroup *group,
3823 GKeyFileKeyValuePair *pair;
3825 pair = g_slice_new (GKeyFileKeyValuePair);
3826 pair->key = g_strdup (key);
3827 pair->value = g_strdup (value);
3829 g_key_file_add_key_value_pair (key_file, group, pair);
3833 * g_key_file_remove_key:
3834 * @key_file: a #GKeyFile
3835 * @group_name: a group name
3836 * @key: a key name to remove
3837 * @error: return location for a #GError or %NULL
3839 * Removes @key in @group_name from the key file.
3841 * Returns: %TRUE if the key was removed, %FALSE otherwise
3846 g_key_file_remove_key (GKeyFile *key_file,
3847 const gchar *group_name,
3851 GKeyFileGroup *group;
3852 GKeyFileKeyValuePair *pair;
3854 g_return_val_if_fail (key_file != NULL, FALSE);
3855 g_return_val_if_fail (group_name != NULL, FALSE);
3856 g_return_val_if_fail (key != NULL, FALSE);
3860 group = g_key_file_lookup_group (key_file, group_name);
3863 g_set_error (error, G_KEY_FILE_ERROR,
3864 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3865 _("Key file does not have group '%s'"),
3866 group_name ? group_name : "(null)");
3870 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3874 g_set_error (error, G_KEY_FILE_ERROR,
3875 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3876 _("Key file does not have key '%s' in group '%s'"),
3881 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3883 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3884 g_hash_table_remove (group->lookup_map, pair->key);
3885 g_key_file_key_value_pair_free (pair);
3891 g_key_file_lookup_group_node (GKeyFile *key_file,
3892 const gchar *group_name)
3894 GKeyFileGroup *group;
3897 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3899 group = (GKeyFileGroup *) tmp->data;
3901 if (group && group->name && strcmp (group->name, group_name) == 0)
3908 static GKeyFileGroup *
3909 g_key_file_lookup_group (GKeyFile *key_file,
3910 const gchar *group_name)
3912 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3916 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3917 GKeyFileGroup *group,
3922 for (key_node = group->key_value_pairs;
3924 key_node = key_node->next)
3926 GKeyFileKeyValuePair *pair;
3928 pair = (GKeyFileKeyValuePair *) key_node->data;
3930 if (pair->key && strcmp (pair->key, key) == 0)
3937 static GKeyFileKeyValuePair *
3938 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3939 GKeyFileGroup *group,
3942 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3945 /* Lines starting with # or consisting entirely of whitespace are merely
3946 * recorded, not parsed. This function assumes all leading whitespace
3947 * has been stripped.
3950 g_key_file_line_is_comment (const gchar *line)
3952 return (*line == '#' || *line == '\0' || *line == '\n');
3956 g_key_file_is_group_name (const gchar *name)
3963 p = q = (gchar *) name;
3964 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3965 q = g_utf8_find_next_char (q, NULL);
3967 if (*q != '\0' || q == p)
3974 g_key_file_is_key_name (const gchar *name)
3981 p = q = (gchar *) name;
3982 /* We accept a little more than the desktop entry spec says,
3983 * since gnome-vfs uses mime-types as keys in its cache.
3985 while (*q && *q != '=' && *q != '[' && *q != ']')
3986 q = g_utf8_find_next_char (q, NULL);
3988 /* No empty keys, please */
3992 /* We accept spaces in the middle of keys to not break
3993 * existing apps, but we don't tolerate initial or final
3994 * spaces, which would lead to silent corruption when
3995 * rereading the file.
3997 if (*p == ' ' || q[-1] == ' ')
4003 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4004 q = g_utf8_find_next_char (q, NULL);
4018 /* A group in a key file is made up of a starting '[' followed by one
4019 * or more letters making up the group name followed by ']'.
4022 g_key_file_line_is_group (const gchar *line)
4032 while (*p && *p != ']')
4033 p = g_utf8_find_next_char (p, NULL);
4038 /* silently accept whitespace after the ] */
4039 p = g_utf8_find_next_char (p, NULL);
4040 while (*p == ' ' || *p == '\t')
4041 p = g_utf8_find_next_char (p, NULL);
4050 g_key_file_line_is_key_value_pair (const gchar *line)
4054 p = (gchar *) g_utf8_strchr (line, -1, '=');
4059 /* Key must be non-empty
4068 g_key_file_parse_value_as_string (GKeyFile *key_file,
4073 gchar *string_value, *p, *q0, *q;
4075 string_value = g_new (gchar, strlen (value) + 1);
4077 p = (gchar *) value;
4078 q0 = q = string_value;
4108 g_set_error_literal (error, G_KEY_FILE_ERROR,
4109 G_KEY_FILE_ERROR_INVALID_VALUE,
4110 _("Key file contains escape character "
4115 if (pieces && *p == key_file->list_separator)
4116 *q = key_file->list_separator;
4130 g_set_error (error, G_KEY_FILE_ERROR,
4131 G_KEY_FILE_ERROR_INVALID_VALUE,
4132 _("Key file contains invalid escape "
4133 "sequence '%s'"), sequence);
4142 if (pieces && (*p == key_file->list_separator))
4144 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4160 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4161 *pieces = g_slist_reverse (*pieces);
4164 return string_value;
4168 g_key_file_parse_string_as_value (GKeyFile *key_file,
4169 const gchar *string,
4170 gboolean escape_separator)
4172 gchar *value, *p, *q;
4174 gboolean parsing_leading_space;
4176 length = strlen (string) + 1;
4178 /* Worst case would be that every character needs to be escaped.
4179 * In other words every character turns to two characters
4181 value = g_new (gchar, 2 * length);
4183 p = (gchar *) string;
4185 parsing_leading_space = TRUE;
4186 while (p < (string + length - 1))
4188 gchar escaped_character[3] = { '\\', 0, 0 };
4193 if (parsing_leading_space)
4195 escaped_character[1] = 's';
4196 strcpy (q, escaped_character);
4206 if (parsing_leading_space)
4208 escaped_character[1] = 't';
4209 strcpy (q, escaped_character);
4219 escaped_character[1] = 'n';
4220 strcpy (q, escaped_character);
4224 escaped_character[1] = 'r';
4225 strcpy (q, escaped_character);
4229 escaped_character[1] = '\\';
4230 strcpy (q, escaped_character);
4232 parsing_leading_space = FALSE;
4235 if (escape_separator && *p == key_file->list_separator)
4237 escaped_character[1] = key_file->list_separator;
4238 strcpy (q, escaped_character);
4240 parsing_leading_space = TRUE;
4246 parsing_leading_space = FALSE;
4258 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4267 long_value = strtol (value, &eof_int, 10);
4269 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4271 gchar *value_utf8 = _g_utf8_make_valid (value);
4272 g_set_error (error, G_KEY_FILE_ERROR,
4273 G_KEY_FILE_ERROR_INVALID_VALUE,
4274 _("Value '%s' cannot be interpreted "
4275 "as a number."), value_utf8);
4276 g_free (value_utf8);
4281 int_value = long_value;
4282 if (int_value != long_value || errno == ERANGE)
4284 gchar *value_utf8 = _g_utf8_make_valid (value);
4287 G_KEY_FILE_ERROR_INVALID_VALUE,
4288 _("Integer value '%s' out of range"),
4290 g_free (value_utf8);
4299 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4303 return g_strdup_printf ("%d", value);
4307 g_key_file_parse_value_as_double (GKeyFile *key_file,
4311 gchar *end_of_valid_d;
4312 gdouble double_value = 0;
4314 double_value = g_ascii_strtod (value, &end_of_valid_d);
4316 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4318 gchar *value_utf8 = _g_utf8_make_valid (value);
4319 g_set_error (error, G_KEY_FILE_ERROR,
4320 G_KEY_FILE_ERROR_INVALID_VALUE,
4321 _("Value '%s' cannot be interpreted "
4322 "as a float number."),
4324 g_free (value_utf8);
4327 return double_value;
4331 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4337 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4339 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4342 value_utf8 = _g_utf8_make_valid (value);
4343 g_set_error (error, G_KEY_FILE_ERROR,
4344 G_KEY_FILE_ERROR_INVALID_VALUE,
4345 _("Value '%s' cannot be interpreted "
4346 "as a boolean."), value_utf8);
4347 g_free (value_utf8);
4353 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4357 return g_strdup ("true");
4359 return g_strdup ("false");
4363 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4370 string = g_string_sized_new (512);
4372 lines = g_strsplit (value, "\n", 0);
4374 for (i = 0; lines[i] != NULL; i++)
4376 if (lines[i][0] != '#')
4377 g_string_append_printf (string, "%s\n", lines[i]);
4379 g_string_append_printf (string, "%s\n", lines[i] + 1);
4383 return g_string_free (string, FALSE);
4387 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4388 const gchar *comment)
4394 string = g_string_sized_new (512);
4396 lines = g_strsplit (comment, "\n", 0);
4398 for (i = 0; lines[i] != NULL; i++)
4399 g_string_append_printf (string, "#%s%s", lines[i],
4400 lines[i + 1] == NULL? "" : "\n");
4403 return g_string_free (string, FALSE);