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 gchar list_separator;
447 volatile gint ref_count;
450 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
452 struct _GKeyFileGroup
454 const gchar *name; /* NULL for above first group (which will be comments) */
456 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
458 GList *key_value_pairs;
460 /* Used in parallel with key_value_pairs for
461 * increased lookup performance
463 GHashTable *lookup_map;
466 struct _GKeyFileKeyValuePair
468 gchar *key; /* NULL for comments */
472 static gint find_file_in_data_dirs (const gchar *file,
473 const gchar **data_dirs,
476 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
480 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
481 const gchar *group_name);
482 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
483 const gchar *group_name);
485 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
486 GKeyFileGroup *group,
488 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
489 GKeyFileGroup *group,
492 static void g_key_file_remove_group_node (GKeyFile *key_file,
494 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
495 GKeyFileGroup *group,
498 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
499 GKeyFileGroup *group,
500 GKeyFileKeyValuePair *pair);
501 static void g_key_file_add_key (GKeyFile *key_file,
502 GKeyFileGroup *group,
505 static void g_key_file_add_group (GKeyFile *key_file,
506 const gchar *group_name);
507 static gboolean g_key_file_is_group_name (const gchar *name);
508 static gboolean g_key_file_is_key_name (const gchar *name);
509 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
510 static gboolean g_key_file_line_is_comment (const gchar *line);
511 static gboolean g_key_file_line_is_group (const gchar *line);
512 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
513 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
517 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
519 gboolean escape_separator);
520 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
523 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
525 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
528 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
531 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
533 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
535 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
536 const gchar *comment);
537 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
541 static void g_key_file_parse_comment (GKeyFile *key_file,
545 static void g_key_file_parse_group (GKeyFile *key_file,
549 static gchar *key_get_locale (const gchar *key);
550 static void g_key_file_parse_data (GKeyFile *key_file,
554 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
557 G_DEFINE_QUARK ("g-key-file-error-quark", g_key_file_error)
560 g_key_file_init (GKeyFile *key_file)
562 key_file->current_group = g_slice_new0 (GKeyFileGroup);
563 key_file->groups = g_list_prepend (NULL, key_file->current_group);
564 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
565 key_file->start_group = NULL;
566 key_file->parse_buffer = g_string_sized_new (128);
567 key_file->list_separator = ';';
569 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
573 g_key_file_clear (GKeyFile *key_file)
575 GList *tmp, *group_node;
577 if (key_file->locales)
579 g_strfreev (key_file->locales);
580 key_file->locales = NULL;
583 if (key_file->parse_buffer)
585 g_string_free (key_file->parse_buffer, TRUE);
586 key_file->parse_buffer = NULL;
589 tmp = key_file->groups;
594 g_key_file_remove_group_node (key_file, group_node);
597 if (key_file->group_hash != NULL)
599 g_hash_table_destroy (key_file->group_hash);
600 key_file->group_hash = NULL;
603 g_warn_if_fail (key_file->groups == NULL);
610 * Creates a new empty #GKeyFile object. Use
611 * g_key_file_load_from_file(), g_key_file_load_from_data(),
612 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
613 * read an existing key file.
615 * Return value: (transfer full): an empty #GKeyFile.
620 g_key_file_new (void)
624 key_file = g_slice_new0 (GKeyFile);
625 key_file->ref_count = 1;
626 g_key_file_init (key_file);
632 * g_key_file_set_list_separator:
633 * @key_file: a #GKeyFile
634 * @separator: the separator
636 * Sets the character which is used to separate
637 * values in lists. Typically ';' or ',' are used
638 * as separators. The default list separator is ';'.
643 g_key_file_set_list_separator (GKeyFile *key_file,
646 g_return_if_fail (key_file != NULL);
648 key_file->list_separator = separator;
652 /* Iterates through all the directories in *dirs trying to
653 * open file. When it successfully locates and opens a file it
654 * returns the file descriptor to the open file. It also
655 * outputs the absolute path of the file in output_file.
658 find_file_in_data_dirs (const gchar *file,
663 const gchar **data_dirs, *data_dir;
675 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
677 gchar *candidate_file, *sub_dir;
679 candidate_file = (gchar *) file;
680 sub_dir = g_strdup ("");
681 while (candidate_file != NULL && fd == -1)
685 path = g_build_filename (data_dir, sub_dir,
686 candidate_file, NULL);
688 fd = g_open (path, O_RDONLY, 0);
696 candidate_file = strchr (candidate_file, '-');
698 if (candidate_file == NULL)
704 sub_dir = g_strndup (file, candidate_file - file - 1);
706 for (p = sub_dir; *p != '\0'; p++)
709 *p = G_DIR_SEPARATOR;
718 g_set_error_literal (error, G_KEY_FILE_ERROR,
719 G_KEY_FILE_ERROR_NOT_FOUND,
720 _("Valid key file could not be "
721 "found in search dirs"));
724 if (output_file != NULL && fd > 0)
725 *output_file = g_strdup (path);
733 g_key_file_load_from_fd (GKeyFile *key_file,
738 GError *key_file_error = NULL;
740 struct stat stat_buf;
741 gchar read_buf[4096];
742 gchar list_separator;
744 if (fstat (fd, &stat_buf) < 0)
746 g_set_error_literal (error, G_FILE_ERROR,
747 g_file_error_from_errno (errno),
752 if (!S_ISREG (stat_buf.st_mode))
754 g_set_error_literal (error, G_KEY_FILE_ERROR,
755 G_KEY_FILE_ERROR_PARSE,
756 _("Not a regular file"));
760 list_separator = key_file->list_separator;
761 g_key_file_clear (key_file);
762 g_key_file_init (key_file);
763 key_file->list_separator = list_separator;
764 key_file->flags = flags;
768 bytes_read = read (fd, read_buf, 4096);
770 if (bytes_read == 0) /* End of File */
775 if (errno == EINTR || errno == EAGAIN)
778 g_set_error_literal (error, G_FILE_ERROR,
779 g_file_error_from_errno (errno),
784 g_key_file_parse_data (key_file,
785 read_buf, bytes_read,
788 while (!key_file_error);
792 g_propagate_error (error, key_file_error);
796 g_key_file_flush_parse_buffer (key_file, &key_file_error);
800 g_propagate_error (error, key_file_error);
808 * g_key_file_load_from_file:
809 * @key_file: an empty #GKeyFile struct
810 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
811 * @flags: flags from #GKeyFileFlags
812 * @error: return location for a #GError, or %NULL
814 * Loads a key file into an empty #GKeyFile structure.
815 * If the file could not be loaded then @error is set to
816 * either a #GFileError or #GKeyFileError.
818 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
823 g_key_file_load_from_file (GKeyFile *key_file,
828 GError *key_file_error = NULL;
831 g_return_val_if_fail (key_file != NULL, FALSE);
832 g_return_val_if_fail (file != NULL, FALSE);
834 fd = g_open (file, O_RDONLY, 0);
838 g_set_error_literal (error, G_FILE_ERROR,
839 g_file_error_from_errno (errno),
844 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
849 g_propagate_error (error, key_file_error);
857 * g_key_file_load_from_data:
858 * @key_file: an empty #GKeyFile struct
859 * @data: key file loaded in memory
860 * @length: the length of @data in bytes (or -1 if data is nul-terminated)
861 * @flags: flags from #GKeyFileFlags
862 * @error: return location for a #GError, or %NULL
864 * Loads a key file from memory into an empty #GKeyFile structure.
865 * If the object cannot be created then %error is set to a #GKeyFileError.
867 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
872 g_key_file_load_from_data (GKeyFile *key_file,
878 GError *key_file_error = NULL;
879 gchar list_separator;
881 g_return_val_if_fail (key_file != NULL, FALSE);
882 g_return_val_if_fail (data != NULL || length == 0, FALSE);
884 if (length == (gsize)-1)
885 length = strlen (data);
887 list_separator = key_file->list_separator;
888 g_key_file_clear (key_file);
889 g_key_file_init (key_file);
890 key_file->list_separator = list_separator;
891 key_file->flags = flags;
893 g_key_file_parse_data (key_file, data, length, &key_file_error);
897 g_propagate_error (error, key_file_error);
901 g_key_file_flush_parse_buffer (key_file, &key_file_error);
905 g_propagate_error (error, key_file_error);
913 * g_key_file_load_from_dirs:
914 * @key_file: an empty #GKeyFile struct
915 * @file: (type filename): a relative path to a filename to open and parse
916 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
917 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
918 * of the file, or %NULL
919 * @flags: flags from #GKeyFileFlags
920 * @error: return location for a #GError, or %NULL
922 * This function looks for a key file named @file in the paths
923 * specified in @search_dirs, loads the file into @key_file and
924 * returns the file's full path in @full_path. If the file could not
925 * be loaded then an %error is set to either a #GFileError or
928 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
933 g_key_file_load_from_dirs (GKeyFile *key_file,
935 const gchar **search_dirs,
940 GError *key_file_error = NULL;
941 const gchar **data_dirs;
946 g_return_val_if_fail (key_file != NULL, FALSE);
947 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
948 g_return_val_if_fail (search_dirs != NULL, FALSE);
951 data_dirs = search_dirs;
953 while (*data_dirs != NULL && !found_file)
955 g_free (output_path);
957 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
963 g_propagate_error (error, key_file_error);
967 found_file = g_key_file_load_from_fd (key_file, fd, flags,
973 g_propagate_error (error, key_file_error);
978 if (found_file && full_path)
979 *full_path = output_path;
981 g_free (output_path);
987 * g_key_file_load_from_data_dirs:
988 * @key_file: an empty #GKeyFile struct
989 * @file: (type filename): a relative path to a filename to open and parse
990 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
991 * of the file, or %NULL
992 * @flags: flags from #GKeyFileFlags
993 * @error: return location for a #GError, or %NULL
995 * This function looks for a key file named @file in the paths
996 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
997 * loads the file into @key_file and returns the file's full path in
998 * @full_path. If the file could not be loaded then an %error is
999 * set to either a #GFileError or #GKeyFileError.
1001 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1005 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1008 GKeyFileFlags flags,
1011 gchar **all_data_dirs;
1012 const gchar * user_data_dir;
1013 const gchar * const * system_data_dirs;
1015 gboolean found_file;
1017 g_return_val_if_fail (key_file != NULL, FALSE);
1018 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1020 user_data_dir = g_get_user_data_dir ();
1021 system_data_dirs = g_get_system_data_dirs ();
1022 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1025 all_data_dirs[i++] = g_strdup (user_data_dir);
1028 while (system_data_dirs[j] != NULL)
1029 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1030 all_data_dirs[i] = NULL;
1032 found_file = g_key_file_load_from_dirs (key_file,
1034 (const gchar **)all_data_dirs,
1039 g_strfreev (all_data_dirs);
1045 * g_key_file_ref: (skip)
1046 * @key_file: a #GKeyFile
1048 * Increases the reference count of @key_file.
1050 * Returns: the same @key_file.
1055 g_key_file_ref (GKeyFile *key_file)
1057 g_return_val_if_fail (key_file != NULL, NULL);
1059 g_atomic_int_inc (&key_file->ref_count);
1065 * g_key_file_free: (skip)
1066 * @key_file: a #GKeyFile
1068 * Clears all keys and groups from @key_file, and decreases the
1069 * reference count by 1. If the reference count reaches zero,
1070 * frees the key file and all its allocated memory.
1075 g_key_file_free (GKeyFile *key_file)
1077 g_return_if_fail (key_file != NULL);
1079 g_key_file_clear (key_file);
1080 g_key_file_unref (key_file);
1085 * @key_file: a #GKeyFile
1087 * Decreases the reference count of @key_file by 1. If the reference count
1088 * reaches zero, frees the key file and all its allocated memory.
1093 g_key_file_unref (GKeyFile *key_file)
1095 g_return_if_fail (key_file != NULL);
1097 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1099 g_key_file_clear (key_file);
1100 g_slice_free (GKeyFile, key_file);
1104 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1105 * true for locales that match those in g_get_language_names().
1108 g_key_file_locale_is_interesting (GKeyFile *key_file,
1109 const gchar *locale)
1113 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1116 for (i = 0; key_file->locales[i] != NULL; i++)
1118 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1126 g_key_file_parse_line (GKeyFile *key_file,
1131 GError *parse_error = NULL;
1134 g_return_if_fail (key_file != NULL);
1135 g_return_if_fail (line != NULL);
1137 line_start = (gchar *) line;
1138 while (g_ascii_isspace (*line_start))
1141 if (g_key_file_line_is_comment (line_start))
1142 g_key_file_parse_comment (key_file, line, length, &parse_error);
1143 else if (g_key_file_line_is_group (line_start))
1144 g_key_file_parse_group (key_file, line_start,
1145 length - (line_start - line),
1147 else if (g_key_file_line_is_key_value_pair (line_start))
1148 g_key_file_parse_key_value_pair (key_file, line_start,
1149 length - (line_start - line),
1153 gchar *line_utf8 = _g_utf8_make_valid (line);
1154 g_set_error (error, G_KEY_FILE_ERROR,
1155 G_KEY_FILE_ERROR_PARSE,
1156 _("Key file contains line '%s' which is not "
1157 "a key-value pair, group, or comment"),
1165 g_propagate_error (error, parse_error);
1169 g_key_file_parse_comment (GKeyFile *key_file,
1174 GKeyFileKeyValuePair *pair;
1176 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1179 g_warn_if_fail (key_file->current_group != NULL);
1181 pair = g_slice_new (GKeyFileKeyValuePair);
1183 pair->value = g_strndup (line, length);
1185 key_file->current_group->key_value_pairs =
1186 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1190 g_key_file_parse_group (GKeyFile *key_file,
1196 const gchar *group_name_start, *group_name_end;
1198 /* advance past opening '['
1200 group_name_start = line + 1;
1201 group_name_end = line + length - 1;
1203 while (*group_name_end != ']')
1206 group_name = g_strndup (group_name_start,
1207 group_name_end - group_name_start);
1209 if (!g_key_file_is_group_name (group_name))
1211 g_set_error (error, G_KEY_FILE_ERROR,
1212 G_KEY_FILE_ERROR_PARSE,
1213 _("Invalid group name: %s"), group_name);
1214 g_free (group_name);
1218 g_key_file_add_group (key_file, group_name);
1219 g_free (group_name);
1223 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1228 gchar *key, *value, *key_end, *value_start, *locale;
1229 gsize key_len, value_len;
1231 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1233 g_set_error_literal (error, G_KEY_FILE_ERROR,
1234 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1235 _("Key file does not start with a group"));
1239 key_end = value_start = strchr (line, '=');
1241 g_warn_if_fail (key_end != NULL);
1246 /* Pull the key name from the line (chomping trailing whitespace)
1248 while (g_ascii_isspace (*key_end))
1251 key_len = key_end - line + 2;
1253 g_warn_if_fail (key_len <= length);
1255 key = g_strndup (line, key_len - 1);
1257 if (!g_key_file_is_key_name (key))
1259 g_set_error (error, G_KEY_FILE_ERROR,
1260 G_KEY_FILE_ERROR_PARSE,
1261 _("Invalid key name: %s"), key);
1266 /* Pull the value from the line (chugging leading whitespace)
1268 while (g_ascii_isspace (*value_start))
1271 value_len = line + length - value_start + 1;
1273 value = g_strndup (value_start, value_len);
1275 g_warn_if_fail (key_file->start_group != NULL);
1277 if (key_file->current_group
1278 && key_file->current_group->name
1279 && strcmp (key_file->start_group->name,
1280 key_file->current_group->name) == 0
1281 && strcmp (key, "Encoding") == 0)
1283 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1285 gchar *value_utf8 = _g_utf8_make_valid (value);
1286 g_set_error (error, G_KEY_FILE_ERROR,
1287 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1288 _("Key file contains unsupported "
1289 "encoding '%s'"), value_utf8);
1290 g_free (value_utf8);
1298 /* Is this key a translation? If so, is it one that we care about?
1300 locale = key_get_locale (key);
1302 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1304 GKeyFileKeyValuePair *pair;
1306 pair = g_slice_new (GKeyFileKeyValuePair);
1308 pair->value = value;
1310 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1322 key_get_locale (const gchar *key)
1326 locale = g_strrstr (key, "[");
1328 if (locale && strlen (locale) <= 2)
1332 locale = g_strndup (locale + 1, strlen (locale) - 2);
1338 g_key_file_parse_data (GKeyFile *key_file,
1343 GError *parse_error;
1346 g_return_if_fail (key_file != NULL);
1347 g_return_if_fail (data != NULL || length == 0);
1354 if (data[i] == '\n')
1356 if (key_file->parse_buffer->len > 0
1357 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1359 g_string_erase (key_file->parse_buffer,
1360 key_file->parse_buffer->len - 1,
1363 /* When a newline is encountered flush the parse buffer so that the
1364 * line can be parsed. Note that completely blank lines won't show
1365 * up in the parse buffer, so they get parsed directly.
1367 if (key_file->parse_buffer->len > 0)
1368 g_key_file_flush_parse_buffer (key_file, &parse_error);
1370 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1374 g_propagate_error (error, parse_error);
1381 const gchar *start_of_line;
1382 const gchar *end_of_line;
1385 start_of_line = data + i;
1386 end_of_line = memchr (start_of_line, '\n', length - i);
1388 if (end_of_line == NULL)
1389 end_of_line = data + length;
1391 line_length = end_of_line - start_of_line;
1393 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1400 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1403 GError *file_error = NULL;
1405 g_return_if_fail (key_file != NULL);
1409 if (key_file->parse_buffer->len > 0)
1411 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1412 key_file->parse_buffer->len,
1414 g_string_erase (key_file->parse_buffer, 0, -1);
1418 g_propagate_error (error, file_error);
1425 * g_key_file_to_data:
1426 * @key_file: a #GKeyFile
1427 * @length: (out) (allow-none): return location for the length of the
1428 * returned string, or %NULL
1429 * @error: return location for a #GError, or %NULL
1431 * This function outputs @key_file as a string.
1433 * Note that this function never reports an error,
1434 * so it is safe to pass %NULL as @error.
1436 * Return value: a newly allocated string holding
1437 * the contents of the #GKeyFile
1442 g_key_file_to_data (GKeyFile *key_file,
1446 GString *data_string;
1447 GList *group_node, *key_file_node;
1449 g_return_val_if_fail (key_file != NULL, NULL);
1451 data_string = g_string_new (NULL);
1453 for (group_node = g_list_last (key_file->groups);
1455 group_node = group_node->prev)
1457 GKeyFileGroup *group;
1459 group = (GKeyFileGroup *) group_node->data;
1461 /* separate groups by at least an empty line */
1462 if (data_string->len >= 2 &&
1463 data_string->str[data_string->len - 2] != '\n')
1464 g_string_append_c (data_string, '\n');
1466 if (group->comment != NULL)
1467 g_string_append_printf (data_string, "%s\n", group->comment->value);
1469 if (group->name != NULL)
1470 g_string_append_printf (data_string, "[%s]\n", group->name);
1472 for (key_file_node = g_list_last (group->key_value_pairs);
1473 key_file_node != NULL;
1474 key_file_node = key_file_node->prev)
1476 GKeyFileKeyValuePair *pair;
1478 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1480 if (pair->key != NULL)
1481 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1483 g_string_append_printf (data_string, "%s\n", pair->value);
1488 *length = data_string->len;
1490 return g_string_free (data_string, FALSE);
1494 * g_key_file_get_keys:
1495 * @key_file: a #GKeyFile
1496 * @group_name: a group name
1497 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1498 * @error: return location for a #GError, or %NULL
1500 * Returns all keys for the group name @group_name. The array of
1501 * returned keys will be %NULL-terminated, so @length may
1502 * optionally be %NULL. In the event that the @group_name cannot
1503 * be found, %NULL is returned and @error is set to
1504 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1506 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1507 * Use g_strfreev() to free it.
1512 g_key_file_get_keys (GKeyFile *key_file,
1513 const gchar *group_name,
1517 GKeyFileGroup *group;
1522 g_return_val_if_fail (key_file != NULL, NULL);
1523 g_return_val_if_fail (group_name != NULL, NULL);
1525 group = g_key_file_lookup_group (key_file, group_name);
1529 g_set_error (error, G_KEY_FILE_ERROR,
1530 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1531 _("Key file does not have group '%s'"),
1532 group_name ? group_name : "(null)");
1537 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1539 GKeyFileKeyValuePair *pair;
1541 pair = (GKeyFileKeyValuePair *) tmp->data;
1547 keys = g_new (gchar *, num_keys + 1);
1550 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1552 GKeyFileKeyValuePair *pair;
1554 pair = (GKeyFileKeyValuePair *) tmp->data;
1558 keys[i] = g_strdup (pair->key);
1563 keys[num_keys] = NULL;
1572 * g_key_file_get_start_group:
1573 * @key_file: a #GKeyFile
1575 * Returns the name of the start group of the file.
1577 * Return value: The start group of the key file.
1582 g_key_file_get_start_group (GKeyFile *key_file)
1584 g_return_val_if_fail (key_file != NULL, NULL);
1586 if (key_file->start_group)
1587 return g_strdup (key_file->start_group->name);
1593 * g_key_file_get_groups:
1594 * @key_file: a #GKeyFile
1595 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1597 * Returns all groups in the key file loaded with @key_file.
1598 * The array of returned groups will be %NULL-terminated, so
1599 * @length may optionally be %NULL.
1601 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1602 * Use g_strfreev() to free it.
1606 g_key_file_get_groups (GKeyFile *key_file,
1611 gsize i, num_groups;
1613 g_return_val_if_fail (key_file != NULL, NULL);
1615 num_groups = g_list_length (key_file->groups);
1617 g_return_val_if_fail (num_groups > 0, NULL);
1619 group_node = g_list_last (key_file->groups);
1621 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1623 /* Only need num_groups instead of num_groups + 1
1624 * because the first group of the file (last in the
1625 * list) is always the comment group at the top,
1628 groups = g_new (gchar *, num_groups);
1632 for (group_node = group_node->prev;
1634 group_node = group_node->prev)
1636 GKeyFileGroup *group;
1638 group = (GKeyFileGroup *) group_node->data;
1640 g_warn_if_fail (group->name != NULL);
1642 groups[i++] = g_strdup (group->name);
1653 * g_key_file_get_value:
1654 * @key_file: a #GKeyFile
1655 * @group_name: a group name
1657 * @error: return location for a #GError, or %NULL
1659 * Returns the raw value associated with @key under @group_name.
1660 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1662 * In the event the key cannot be found, %NULL is returned and
1663 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1664 * event that the @group_name cannot be found, %NULL is returned
1665 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1668 * Return value: a newly allocated string or %NULL if the specified
1669 * key cannot be found.
1674 g_key_file_get_value (GKeyFile *key_file,
1675 const gchar *group_name,
1679 GKeyFileGroup *group;
1680 GKeyFileKeyValuePair *pair;
1681 gchar *value = NULL;
1683 g_return_val_if_fail (key_file != NULL, NULL);
1684 g_return_val_if_fail (group_name != NULL, NULL);
1685 g_return_val_if_fail (key != NULL, NULL);
1687 group = g_key_file_lookup_group (key_file, group_name);
1691 g_set_error (error, G_KEY_FILE_ERROR,
1692 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1693 _("Key file does not have group '%s'"),
1694 group_name ? group_name : "(null)");
1698 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1701 value = g_strdup (pair->value);
1703 g_set_error (error, G_KEY_FILE_ERROR,
1704 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1705 _("Key file does not have key '%s'"), key);
1711 * g_key_file_set_value:
1712 * @key_file: a #GKeyFile
1713 * @group_name: a group name
1717 * Associates a new value with @key under @group_name.
1719 * If @key cannot be found then it is created. If @group_name cannot
1720 * be found then it is created. To set an UTF-8 string which may contain
1721 * characters that need escaping (such as newlines or spaces), use
1722 * g_key_file_set_string().
1727 g_key_file_set_value (GKeyFile *key_file,
1728 const gchar *group_name,
1732 GKeyFileGroup *group;
1733 GKeyFileKeyValuePair *pair;
1735 g_return_if_fail (key_file != NULL);
1736 g_return_if_fail (g_key_file_is_group_name (group_name));
1737 g_return_if_fail (g_key_file_is_key_name (key));
1738 g_return_if_fail (value != NULL);
1740 group = g_key_file_lookup_group (key_file, group_name);
1744 g_key_file_add_group (key_file, group_name);
1745 group = (GKeyFileGroup *) key_file->groups->data;
1747 g_key_file_add_key (key_file, group, key, value);
1751 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1754 g_key_file_add_key (key_file, group, key, value);
1757 g_free (pair->value);
1758 pair->value = g_strdup (value);
1764 * g_key_file_get_string:
1765 * @key_file: a #GKeyFile
1766 * @group_name: a group name
1768 * @error: return location for a #GError, or %NULL
1770 * Returns the string value associated with @key under @group_name.
1771 * Unlike g_key_file_get_value(), this function handles escape sequences
1774 * In the event the key cannot be found, %NULL is returned and
1775 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1776 * event that the @group_name cannot be found, %NULL is returned
1777 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1779 * Return value: a newly allocated string or %NULL if the specified
1780 * key cannot be found.
1785 g_key_file_get_string (GKeyFile *key_file,
1786 const gchar *group_name,
1790 gchar *value, *string_value;
1791 GError *key_file_error;
1793 g_return_val_if_fail (key_file != NULL, NULL);
1794 g_return_val_if_fail (group_name != NULL, NULL);
1795 g_return_val_if_fail (key != NULL, NULL);
1797 key_file_error = NULL;
1799 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1803 g_propagate_error (error, key_file_error);
1807 if (!g_utf8_validate (value, -1, NULL))
1809 gchar *value_utf8 = _g_utf8_make_valid (value);
1810 g_set_error (error, G_KEY_FILE_ERROR,
1811 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1812 _("Key file contains key '%s' with value '%s' "
1813 "which is not UTF-8"), key, value_utf8);
1814 g_free (value_utf8);
1820 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1826 if (g_error_matches (key_file_error,
1828 G_KEY_FILE_ERROR_INVALID_VALUE))
1830 g_set_error (error, G_KEY_FILE_ERROR,
1831 G_KEY_FILE_ERROR_INVALID_VALUE,
1832 _("Key file contains key '%s' "
1833 "which has a value that cannot be interpreted."),
1835 g_error_free (key_file_error);
1838 g_propagate_error (error, key_file_error);
1841 return string_value;
1845 * g_key_file_set_string:
1846 * @key_file: a #GKeyFile
1847 * @group_name: a group name
1851 * Associates a new string value with @key under @group_name.
1852 * If @key cannot be found then it is created.
1853 * If @group_name cannot be found then it is created.
1854 * Unlike g_key_file_set_value(), this function handles characters
1855 * that need escaping, such as newlines.
1860 g_key_file_set_string (GKeyFile *key_file,
1861 const gchar *group_name,
1863 const gchar *string)
1867 g_return_if_fail (key_file != NULL);
1868 g_return_if_fail (string != NULL);
1870 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1871 g_key_file_set_value (key_file, group_name, key, value);
1876 * g_key_file_get_string_list:
1877 * @key_file: a #GKeyFile
1878 * @group_name: a group name
1880 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1881 * @error: return location for a #GError, or %NULL
1883 * Returns the values associated with @key under @group_name.
1885 * In the event the key cannot be found, %NULL is returned and
1886 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1887 * event that the @group_name cannot be found, %NULL is returned
1888 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1890 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1891 * a %NULL-terminated string array or %NULL if the specified
1892 * key cannot be found. The array should be freed with g_strfreev().
1897 g_key_file_get_string_list (GKeyFile *key_file,
1898 const gchar *group_name,
1903 GError *key_file_error = NULL;
1904 gchar *value, *string_value, **values;
1906 GSList *p, *pieces = NULL;
1908 g_return_val_if_fail (key_file != NULL, NULL);
1909 g_return_val_if_fail (group_name != NULL, NULL);
1910 g_return_val_if_fail (key != NULL, NULL);
1915 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1919 g_propagate_error (error, key_file_error);
1923 if (!g_utf8_validate (value, -1, NULL))
1925 gchar *value_utf8 = _g_utf8_make_valid (value);
1926 g_set_error (error, G_KEY_FILE_ERROR,
1927 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1928 _("Key file contains key '%s' with value '%s' "
1929 "which is not UTF-8"), key, value_utf8);
1930 g_free (value_utf8);
1936 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1938 g_free (string_value);
1942 if (g_error_matches (key_file_error,
1944 G_KEY_FILE_ERROR_INVALID_VALUE))
1946 g_set_error (error, G_KEY_FILE_ERROR,
1947 G_KEY_FILE_ERROR_INVALID_VALUE,
1948 _("Key file contains key '%s' "
1949 "which has a value that cannot be interpreted."),
1951 g_error_free (key_file_error);
1954 g_propagate_error (error, key_file_error);
1956 g_slist_free_full (pieces, g_free);
1960 len = g_slist_length (pieces);
1961 values = g_new (gchar *, len + 1);
1962 for (p = pieces, i = 0; p; p = p->next)
1963 values[i++] = p->data;
1966 g_slist_free (pieces);
1975 * g_key_file_set_string_list:
1976 * @key_file: a #GKeyFile
1977 * @group_name: a group name
1979 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1980 * @length: number of string values in @list
1982 * Associates a list of string values for @key under @group_name.
1983 * If @key cannot be found then it is created.
1984 * If @group_name cannot be found then it is created.
1989 g_key_file_set_string_list (GKeyFile *key_file,
1990 const gchar *group_name,
1992 const gchar * const list[],
1995 GString *value_list;
1998 g_return_if_fail (key_file != NULL);
1999 g_return_if_fail (list != NULL || length == 0);
2001 value_list = g_string_sized_new (length * 128);
2002 for (i = 0; i < length && list[i] != NULL; i++)
2006 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2007 g_string_append (value_list, value);
2008 g_string_append_c (value_list, key_file->list_separator);
2013 g_key_file_set_value (key_file, group_name, key, value_list->str);
2014 g_string_free (value_list, TRUE);
2018 * g_key_file_set_locale_string:
2019 * @key_file: a #GKeyFile
2020 * @group_name: a group name
2022 * @locale: a locale identifier
2025 * Associates a string value for @key and @locale under @group_name.
2026 * If the translation for @key cannot be found then it is created.
2031 g_key_file_set_locale_string (GKeyFile *key_file,
2032 const gchar *group_name,
2034 const gchar *locale,
2035 const gchar *string)
2037 gchar *full_key, *value;
2039 g_return_if_fail (key_file != NULL);
2040 g_return_if_fail (key != NULL);
2041 g_return_if_fail (locale != NULL);
2042 g_return_if_fail (string != NULL);
2044 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2045 full_key = g_strdup_printf ("%s[%s]", key, locale);
2046 g_key_file_set_value (key_file, group_name, full_key, value);
2052 * g_key_file_get_locale_string:
2053 * @key_file: a #GKeyFile
2054 * @group_name: a group name
2056 * @locale: (allow-none): a locale identifier or %NULL
2057 * @error: return location for a #GError, or %NULL
2059 * Returns the value associated with @key under @group_name
2060 * translated in the given @locale if available. If @locale is
2061 * %NULL then the current locale is assumed.
2063 * If @key cannot be found then %NULL is returned and @error is set
2064 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2065 * with @key cannot be interpreted or no suitable translation can
2066 * be found then the untranslated value is returned.
2068 * Return value: a newly allocated string or %NULL if the specified
2069 * key cannot be found.
2074 g_key_file_get_locale_string (GKeyFile *key_file,
2075 const gchar *group_name,
2077 const gchar *locale,
2080 gchar *candidate_key, *translated_value;
2081 GError *key_file_error;
2083 gboolean free_languages = FALSE;
2086 g_return_val_if_fail (key_file != NULL, NULL);
2087 g_return_val_if_fail (group_name != NULL, NULL);
2088 g_return_val_if_fail (key != NULL, NULL);
2090 candidate_key = NULL;
2091 translated_value = NULL;
2092 key_file_error = NULL;
2096 languages = g_get_locale_variants (locale);
2097 free_languages = TRUE;
2101 languages = (gchar **) g_get_language_names ();
2102 free_languages = FALSE;
2105 for (i = 0; languages[i]; i++)
2107 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2109 translated_value = g_key_file_get_string (key_file,
2111 candidate_key, NULL);
2112 g_free (candidate_key);
2114 if (translated_value)
2117 g_free (translated_value);
2118 translated_value = NULL;
2121 /* Fallback to untranslated key
2123 if (!translated_value)
2125 translated_value = g_key_file_get_string (key_file, group_name, key,
2128 if (!translated_value)
2129 g_propagate_error (error, key_file_error);
2133 g_strfreev (languages);
2135 return translated_value;
2139 * g_key_file_get_locale_string_list:
2140 * @key_file: a #GKeyFile
2141 * @group_name: a group name
2143 * @locale: (allow-none): a locale identifier or %NULL
2144 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2145 * @error: return location for a #GError or %NULL
2147 * Returns the values associated with @key under @group_name
2148 * translated in the given @locale if available. If @locale is
2149 * %NULL then the current locale is assumed.
2151 * If @key cannot be found then %NULL is returned and @error is set
2152 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2153 * with @key cannot be interpreted or no suitable translations
2154 * can be found then the untranslated values are returned. The
2155 * returned array is %NULL-terminated, so @length may optionally
2158 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2159 * or %NULL if the key isn't found. The string array should be freed
2160 * with g_strfreev().
2165 g_key_file_get_locale_string_list (GKeyFile *key_file,
2166 const gchar *group_name,
2168 const gchar *locale,
2172 GError *key_file_error;
2173 gchar **values, *value;
2174 char list_separator[2];
2177 g_return_val_if_fail (key_file != NULL, NULL);
2178 g_return_val_if_fail (group_name != NULL, NULL);
2179 g_return_val_if_fail (key != NULL, NULL);
2181 key_file_error = NULL;
2183 value = g_key_file_get_locale_string (key_file, group_name,
2188 g_propagate_error (error, key_file_error);
2197 len = strlen (value);
2198 if (value[len - 1] == key_file->list_separator)
2199 value[len - 1] = '\0';
2201 list_separator[0] = key_file->list_separator;
2202 list_separator[1] = '\0';
2203 values = g_strsplit (value, list_separator, 0);
2208 *length = g_strv_length (values);
2214 * g_key_file_set_locale_string_list:
2215 * @key_file: a #GKeyFile
2216 * @group_name: a group name
2218 * @locale: a locale identifier
2219 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2220 * @length: the length of @list
2222 * Associates a list of string values for @key and @locale under
2223 * @group_name. If the translation for @key cannot be found then
2229 g_key_file_set_locale_string_list (GKeyFile *key_file,
2230 const gchar *group_name,
2232 const gchar *locale,
2233 const gchar * const list[],
2236 GString *value_list;
2240 g_return_if_fail (key_file != NULL);
2241 g_return_if_fail (key != NULL);
2242 g_return_if_fail (locale != NULL);
2243 g_return_if_fail (length != 0);
2245 value_list = g_string_sized_new (length * 128);
2246 for (i = 0; i < length && list[i] != NULL; i++)
2250 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2251 g_string_append (value_list, value);
2252 g_string_append_c (value_list, key_file->list_separator);
2257 full_key = g_strdup_printf ("%s[%s]", key, locale);
2258 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2260 g_string_free (value_list, TRUE);
2264 * g_key_file_get_boolean:
2265 * @key_file: a #GKeyFile
2266 * @group_name: a group name
2268 * @error: return location for a #GError
2270 * Returns the value associated with @key under @group_name as a
2273 * If @key cannot be found then %FALSE is returned and @error is set
2274 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2275 * associated with @key cannot be interpreted as a boolean then %FALSE
2276 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2278 * Return value: the value associated with the key as a boolean,
2279 * or %FALSE if the key was not found or could not be parsed.
2284 g_key_file_get_boolean (GKeyFile *key_file,
2285 const gchar *group_name,
2289 GError *key_file_error = NULL;
2291 gboolean bool_value;
2293 g_return_val_if_fail (key_file != NULL, FALSE);
2294 g_return_val_if_fail (group_name != NULL, FALSE);
2295 g_return_val_if_fail (key != NULL, FALSE);
2297 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2301 g_propagate_error (error, key_file_error);
2305 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2311 if (g_error_matches (key_file_error,
2313 G_KEY_FILE_ERROR_INVALID_VALUE))
2315 g_set_error (error, G_KEY_FILE_ERROR,
2316 G_KEY_FILE_ERROR_INVALID_VALUE,
2317 _("Key file contains key '%s' "
2318 "which has a value that cannot be interpreted."),
2320 g_error_free (key_file_error);
2323 g_propagate_error (error, key_file_error);
2330 * g_key_file_set_boolean:
2331 * @key_file: a #GKeyFile
2332 * @group_name: a group name
2334 * @value: %TRUE or %FALSE
2336 * Associates a new boolean value with @key under @group_name.
2337 * If @key cannot be found then it is created.
2342 g_key_file_set_boolean (GKeyFile *key_file,
2343 const gchar *group_name,
2349 g_return_if_fail (key_file != NULL);
2351 result = g_key_file_parse_boolean_as_value (key_file, value);
2352 g_key_file_set_value (key_file, group_name, key, result);
2357 * g_key_file_get_boolean_list:
2358 * @key_file: a #GKeyFile
2359 * @group_name: a group name
2361 * @length: (out): the number of booleans returned
2362 * @error: return location for a #GError
2364 * Returns the values associated with @key under @group_name as
2367 * If @key cannot be found then %NULL is returned and @error is set to
2368 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2369 * with @key cannot be interpreted as booleans then %NULL is returned
2370 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2372 * Return value: (array length=length) (element-type gboolean) (transfer container):
2373 * the values associated with the key as a list of booleans, or %NULL if the
2374 * key was not found or could not be parsed. The returned list of booleans
2375 * should be freed with g_free() when no longer needed.
2380 g_key_file_get_boolean_list (GKeyFile *key_file,
2381 const gchar *group_name,
2386 GError *key_file_error;
2388 gboolean *bool_values;
2391 g_return_val_if_fail (key_file != NULL, NULL);
2392 g_return_val_if_fail (group_name != NULL, NULL);
2393 g_return_val_if_fail (key != NULL, NULL);
2398 key_file_error = NULL;
2400 values = g_key_file_get_string_list (key_file, group_name, key,
2401 &num_bools, &key_file_error);
2404 g_propagate_error (error, key_file_error);
2409 bool_values = g_new (gboolean, num_bools);
2411 for (i = 0; i < num_bools; i++)
2413 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2419 g_propagate_error (error, key_file_error);
2420 g_strfreev (values);
2421 g_free (bool_values);
2426 g_strfreev (values);
2429 *length = num_bools;
2435 * g_key_file_set_boolean_list:
2436 * @key_file: a #GKeyFile
2437 * @group_name: a group name
2439 * @list: (array length=length): an array of boolean values
2440 * @length: length of @list
2442 * Associates a list of boolean values with @key under @group_name.
2443 * If @key cannot be found then it is created.
2444 * If @group_name is %NULL, the start_group is used.
2449 g_key_file_set_boolean_list (GKeyFile *key_file,
2450 const gchar *group_name,
2455 GString *value_list;
2458 g_return_if_fail (key_file != NULL);
2459 g_return_if_fail (list != NULL);
2461 value_list = g_string_sized_new (length * 8);
2462 for (i = 0; i < length; i++)
2466 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2468 g_string_append (value_list, value);
2469 g_string_append_c (value_list, key_file->list_separator);
2474 g_key_file_set_value (key_file, group_name, key, value_list->str);
2475 g_string_free (value_list, TRUE);
2479 * g_key_file_get_integer:
2480 * @key_file: a #GKeyFile
2481 * @group_name: a group name
2483 * @error: return location for a #GError
2485 * Returns the value associated with @key under @group_name as an
2488 * If @key cannot be found then 0 is returned and @error is set to
2489 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2490 * with @key cannot be interpreted as an integer then 0 is returned
2491 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2493 * Return value: the value associated with the key as an integer, or
2494 * 0 if the key was not found or could not be parsed.
2499 g_key_file_get_integer (GKeyFile *key_file,
2500 const gchar *group_name,
2504 GError *key_file_error;
2508 g_return_val_if_fail (key_file != NULL, -1);
2509 g_return_val_if_fail (group_name != NULL, -1);
2510 g_return_val_if_fail (key != NULL, -1);
2512 key_file_error = NULL;
2514 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2518 g_propagate_error (error, key_file_error);
2522 int_value = g_key_file_parse_value_as_integer (key_file, value,
2528 if (g_error_matches (key_file_error,
2530 G_KEY_FILE_ERROR_INVALID_VALUE))
2532 g_set_error (error, G_KEY_FILE_ERROR,
2533 G_KEY_FILE_ERROR_INVALID_VALUE,
2534 _("Key file contains key '%s' in group '%s' "
2535 "which has a value that cannot be interpreted."),
2537 g_error_free (key_file_error);
2540 g_propagate_error (error, key_file_error);
2547 * g_key_file_set_integer:
2548 * @key_file: a #GKeyFile
2549 * @group_name: a group name
2551 * @value: an integer value
2553 * Associates a new integer value with @key under @group_name.
2554 * If @key cannot be found then it is created.
2559 g_key_file_set_integer (GKeyFile *key_file,
2560 const gchar *group_name,
2566 g_return_if_fail (key_file != NULL);
2568 result = g_key_file_parse_integer_as_value (key_file, value);
2569 g_key_file_set_value (key_file, group_name, key, result);
2574 * g_key_file_get_int64:
2575 * @key_file: a non-%NULL #GKeyFile
2576 * @group_name: a non-%NULL group name
2577 * @key: a non-%NULL key
2578 * @error: return location for a #GError
2580 * Returns the value associated with @key under @group_name as a signed
2581 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2582 * 64-bit results without truncation.
2584 * Returns: the value associated with the key as a signed 64-bit integer, or
2585 * 0 if the key was not found or could not be parsed.
2590 g_key_file_get_int64 (GKeyFile *key_file,
2591 const gchar *group_name,
2598 g_return_val_if_fail (key_file != NULL, -1);
2599 g_return_val_if_fail (group_name != NULL, -1);
2600 g_return_val_if_fail (key != NULL, -1);
2602 s = g_key_file_get_value (key_file, group_name, key, error);
2607 v = g_ascii_strtoll (s, &end, 10);
2609 if (*s == '\0' || *end != '\0')
2611 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2612 _("Key '%s' in group '%s' has value '%s' "
2613 "where %s was expected"),
2614 key, group_name, s, "int64");
2623 * g_key_file_set_int64:
2624 * @key_file: a #GKeyFile
2625 * @group_name: a group name
2627 * @value: an integer value
2629 * Associates a new integer value with @key under @group_name.
2630 * If @key cannot be found then it is created.
2635 g_key_file_set_int64 (GKeyFile *key_file,
2636 const gchar *group_name,
2642 g_return_if_fail (key_file != NULL);
2644 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2645 g_key_file_set_value (key_file, group_name, key, result);
2650 * g_key_file_get_uint64:
2651 * @key_file: a non-%NULL #GKeyFile
2652 * @group_name: a non-%NULL group name
2653 * @key: a non-%NULL key
2654 * @error: return location for a #GError
2656 * Returns the value associated with @key under @group_name as an unsigned
2657 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2658 * large positive results without truncation.
2660 * Returns: the value associated with the key as an unsigned 64-bit integer,
2661 * or 0 if the key was not found or could not be parsed.
2666 g_key_file_get_uint64 (GKeyFile *key_file,
2667 const gchar *group_name,
2674 g_return_val_if_fail (key_file != NULL, -1);
2675 g_return_val_if_fail (group_name != NULL, -1);
2676 g_return_val_if_fail (key != NULL, -1);
2678 s = g_key_file_get_value (key_file, group_name, key, error);
2683 v = g_ascii_strtoull (s, &end, 10);
2685 if (*s == '\0' || *end != '\0')
2687 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2688 _("Key '%s' in group '%s' has value '%s' "
2689 "where %s was expected"),
2690 key, group_name, s, "uint64");
2699 * g_key_file_set_uint64:
2700 * @key_file: a #GKeyFile
2701 * @group_name: a group name
2703 * @value: an integer value
2705 * Associates a new integer value with @key under @group_name.
2706 * If @key cannot be found then it is created.
2711 g_key_file_set_uint64 (GKeyFile *key_file,
2712 const gchar *group_name,
2718 g_return_if_fail (key_file != NULL);
2720 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2721 g_key_file_set_value (key_file, group_name, key, result);
2726 * g_key_file_get_integer_list:
2727 * @key_file: a #GKeyFile
2728 * @group_name: a group name
2730 * @length: (out): the number of integers returned
2731 * @error: return location for a #GError
2733 * Returns the values associated with @key under @group_name as
2736 * If @key cannot be found then %NULL is returned and @error is set to
2737 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2738 * with @key cannot be interpreted as integers then %NULL is returned
2739 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2741 * Return value: (array length=length) (element-type gint) (transfer container):
2742 * the values associated with the key as a list of integers, or %NULL if
2743 * the key was not found or could not be parsed. The returned list of
2744 * integers should be freed with g_free() when no longer needed.
2749 g_key_file_get_integer_list (GKeyFile *key_file,
2750 const gchar *group_name,
2755 GError *key_file_error = NULL;
2760 g_return_val_if_fail (key_file != NULL, NULL);
2761 g_return_val_if_fail (group_name != NULL, NULL);
2762 g_return_val_if_fail (key != NULL, NULL);
2767 values = g_key_file_get_string_list (key_file, group_name, key,
2768 &num_ints, &key_file_error);
2771 g_propagate_error (error, key_file_error);
2776 int_values = g_new (gint, num_ints);
2778 for (i = 0; i < num_ints; i++)
2780 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2786 g_propagate_error (error, key_file_error);
2787 g_strfreev (values);
2788 g_free (int_values);
2793 g_strfreev (values);
2802 * g_key_file_set_integer_list:
2803 * @key_file: a #GKeyFile
2804 * @group_name: a group name
2806 * @list: (array length=length): an array of integer values
2807 * @length: number of integer values in @list
2809 * Associates a list of integer values with @key under @group_name.
2810 * If @key cannot be found then it is created.
2815 g_key_file_set_integer_list (GKeyFile *key_file,
2816 const gchar *group_name,
2824 g_return_if_fail (key_file != NULL);
2825 g_return_if_fail (list != NULL);
2827 values = g_string_sized_new (length * 16);
2828 for (i = 0; i < length; i++)
2832 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2834 g_string_append (values, value);
2835 g_string_append_c (values, key_file->list_separator);
2840 g_key_file_set_value (key_file, group_name, key, values->str);
2841 g_string_free (values, TRUE);
2845 * g_key_file_get_double:
2846 * @key_file: a #GKeyFile
2847 * @group_name: a group name
2849 * @error: return location for a #GError
2851 * Returns the value associated with @key under @group_name as a
2852 * double. If @group_name is %NULL, the start_group is used.
2854 * If @key cannot be found then 0.0 is returned and @error is set to
2855 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2856 * with @key cannot be interpreted as a double then 0.0 is returned
2857 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2859 * Return value: the value associated with the key as a double, or
2860 * 0.0 if the key was not found or could not be parsed.
2865 g_key_file_get_double (GKeyFile *key_file,
2866 const gchar *group_name,
2870 GError *key_file_error;
2872 gdouble double_value;
2874 g_return_val_if_fail (key_file != NULL, -1);
2875 g_return_val_if_fail (group_name != NULL, -1);
2876 g_return_val_if_fail (key != NULL, -1);
2878 key_file_error = NULL;
2880 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2884 g_propagate_error (error, key_file_error);
2888 double_value = g_key_file_parse_value_as_double (key_file, value,
2894 if (g_error_matches (key_file_error,
2896 G_KEY_FILE_ERROR_INVALID_VALUE))
2898 g_set_error (error, G_KEY_FILE_ERROR,
2899 G_KEY_FILE_ERROR_INVALID_VALUE,
2900 _("Key file contains key '%s' in group '%s' "
2901 "which has a value that cannot be interpreted."),
2903 g_error_free (key_file_error);
2906 g_propagate_error (error, key_file_error);
2909 return double_value;
2913 * g_key_file_set_double:
2914 * @key_file: a #GKeyFile
2915 * @group_name: a group name
2917 * @value: an double value
2919 * Associates a new double value with @key under @group_name.
2920 * If @key cannot be found then it is created.
2925 g_key_file_set_double (GKeyFile *key_file,
2926 const gchar *group_name,
2930 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2932 g_return_if_fail (key_file != NULL);
2934 g_ascii_dtostr (result, sizeof (result), value);
2935 g_key_file_set_value (key_file, group_name, key, result);
2939 * g_key_file_get_double_list:
2940 * @key_file: a #GKeyFile
2941 * @group_name: a group name
2943 * @length: (out): the number of doubles returned
2944 * @error: return location for a #GError
2946 * Returns the values associated with @key under @group_name as
2949 * If @key cannot be found then %NULL is returned and @error is set to
2950 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2951 * with @key cannot be interpreted as doubles then %NULL is returned
2952 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2954 * Return value: (array length=length) (element-type gdouble) (transfer container):
2955 * the values associated with the key as a list of doubles, or %NULL if the
2956 * key was not found or could not be parsed. The returned list of doubles
2957 * should be freed with g_free() when no longer needed.
2962 g_key_file_get_double_list (GKeyFile *key_file,
2963 const gchar *group_name,
2968 GError *key_file_error = NULL;
2970 gdouble *double_values;
2971 gsize i, num_doubles;
2973 g_return_val_if_fail (key_file != NULL, NULL);
2974 g_return_val_if_fail (group_name != NULL, NULL);
2975 g_return_val_if_fail (key != NULL, NULL);
2980 values = g_key_file_get_string_list (key_file, group_name, key,
2981 &num_doubles, &key_file_error);
2984 g_propagate_error (error, key_file_error);
2989 double_values = g_new (gdouble, num_doubles);
2991 for (i = 0; i < num_doubles; i++)
2993 double_values[i] = g_key_file_parse_value_as_double (key_file,
2999 g_propagate_error (error, key_file_error);
3000 g_strfreev (values);
3001 g_free (double_values);
3006 g_strfreev (values);
3009 *length = num_doubles;
3011 return double_values;
3015 * g_key_file_set_double_list:
3016 * @key_file: a #GKeyFile
3017 * @group_name: a group name
3019 * @list: (array length=length): an array of double values
3020 * @length: number of double values in @list
3022 * Associates a list of double values with @key under
3023 * @group_name. If @key cannot be found then it is created.
3028 g_key_file_set_double_list (GKeyFile *key_file,
3029 const gchar *group_name,
3037 g_return_if_fail (key_file != NULL);
3038 g_return_if_fail (list != NULL);
3040 values = g_string_sized_new (length * 16);
3041 for (i = 0; i < length; i++)
3043 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3045 g_ascii_dtostr( result, sizeof (result), list[i] );
3047 g_string_append (values, result);
3048 g_string_append_c (values, key_file->list_separator);
3051 g_key_file_set_value (key_file, group_name, key, values->str);
3052 g_string_free (values, TRUE);
3056 g_key_file_set_key_comment (GKeyFile *key_file,
3057 const gchar *group_name,
3059 const gchar *comment,
3062 GKeyFileGroup *group;
3063 GKeyFileKeyValuePair *pair;
3064 GList *key_node, *comment_node, *tmp;
3066 group = g_key_file_lookup_group (key_file, group_name);
3069 g_set_error (error, G_KEY_FILE_ERROR,
3070 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3071 _("Key file does not have group '%s'"),
3072 group_name ? group_name : "(null)");
3077 /* First find the key the comments are supposed to be
3080 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3082 if (key_node == NULL)
3084 g_set_error (error, G_KEY_FILE_ERROR,
3085 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3086 _("Key file does not have key '%s' in group '%s'"),
3091 /* Then find all the comments already associated with the
3094 tmp = key_node->next;
3097 pair = (GKeyFileKeyValuePair *) tmp->data;
3099 if (pair->key != NULL)
3104 g_key_file_remove_key_value_pair_node (key_file, group,
3108 if (comment == NULL)
3111 /* Now we can add our new comment
3113 pair = g_slice_new (GKeyFileKeyValuePair);
3115 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3117 key_node = g_list_insert (key_node, pair, 1);
3123 g_key_file_set_group_comment (GKeyFile *key_file,
3124 const gchar *group_name,
3125 const gchar *comment,
3128 GKeyFileGroup *group;
3130 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3132 group = g_key_file_lookup_group (key_file, group_name);
3135 g_set_error (error, G_KEY_FILE_ERROR,
3136 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3137 _("Key file does not have group '%s'"),
3138 group_name ? group_name : "(null)");
3143 /* First remove any existing comment
3147 g_key_file_key_value_pair_free (group->comment);
3148 group->comment = NULL;
3151 if (comment == NULL)
3154 /* Now we can add our new comment
3156 group->comment = g_slice_new (GKeyFileKeyValuePair);
3157 group->comment->key = NULL;
3158 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3164 g_key_file_set_top_comment (GKeyFile *key_file,
3165 const gchar *comment,
3169 GKeyFileGroup *group;
3170 GKeyFileKeyValuePair *pair;
3172 /* The last group in the list should be the top (comments only)
3175 g_warn_if_fail (key_file->groups != NULL);
3176 group_node = g_list_last (key_file->groups);
3177 group = (GKeyFileGroup *) group_node->data;
3178 g_warn_if_fail (group->name == NULL);
3180 /* Note all keys must be comments at the top of
3181 * the file, so we can just free it all.
3183 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3184 group->key_value_pairs = NULL;
3186 if (comment == NULL)
3189 pair = g_slice_new (GKeyFileKeyValuePair);
3191 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3193 group->key_value_pairs =
3194 g_list_prepend (group->key_value_pairs, pair);
3200 * g_key_file_set_comment:
3201 * @key_file: a #GKeyFile
3202 * @group_name: (allow-none): a group name, or %NULL
3203 * @key: (allow-none): a key
3204 * @comment: a comment
3205 * @error: return location for a #GError
3207 * Places a comment above @key from @group_name.
3208 * If @key is %NULL then @comment will be written above @group_name.
3209 * If both @key and @group_name are %NULL, then @comment will be
3210 * written above the first group in the file.
3212 * Returns: %TRUE if the comment was written, %FALSE otherwise
3217 g_key_file_set_comment (GKeyFile *key_file,
3218 const gchar *group_name,
3220 const gchar *comment,
3223 g_return_val_if_fail (key_file != NULL, FALSE);
3225 if (group_name != NULL && key != NULL)
3227 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3230 else if (group_name != NULL)
3232 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3237 if (!g_key_file_set_top_comment (key_file, comment, error))
3245 g_key_file_get_key_comment (GKeyFile *key_file,
3246 const gchar *group_name,
3250 GKeyFileGroup *group;
3251 GKeyFileKeyValuePair *pair;
3252 GList *key_node, *tmp;
3256 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3258 group = g_key_file_lookup_group (key_file, group_name);
3261 g_set_error (error, G_KEY_FILE_ERROR,
3262 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3263 _("Key file does not have group '%s'"),
3264 group_name ? group_name : "(null)");
3269 /* First find the key the comments are supposed to be
3272 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3274 if (key_node == NULL)
3276 g_set_error (error, G_KEY_FILE_ERROR,
3277 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3278 _("Key file does not have key '%s' in group '%s'"),
3285 /* Then find all the comments already associated with the
3286 * key and concatentate them.
3288 tmp = key_node->next;
3289 if (!key_node->next)
3292 pair = (GKeyFileKeyValuePair *) tmp->data;
3293 if (pair->key != NULL)
3298 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3300 if (pair->key != NULL)
3306 while (tmp != key_node)
3308 pair = (GKeyFileKeyValuePair *) tmp->data;
3311 string = g_string_sized_new (512);
3313 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3314 g_string_append (string, comment);
3322 comment = string->str;
3323 g_string_free (string, FALSE);
3332 get_group_comment (GKeyFile *key_file,
3333 GKeyFileGroup *group,
3342 tmp = group->key_value_pairs;
3345 GKeyFileKeyValuePair *pair;
3347 pair = (GKeyFileKeyValuePair *) tmp->data;
3349 if (pair->key != NULL)
3355 if (tmp->next == NULL)
3363 GKeyFileKeyValuePair *pair;
3365 pair = (GKeyFileKeyValuePair *) tmp->data;
3368 string = g_string_sized_new (512);
3370 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3371 g_string_append (string, comment);
3378 return g_string_free (string, FALSE);
3384 g_key_file_get_group_comment (GKeyFile *key_file,
3385 const gchar *group_name,
3389 GKeyFileGroup *group;
3391 group = g_key_file_lookup_group (key_file, group_name);
3394 g_set_error (error, G_KEY_FILE_ERROR,
3395 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3396 _("Key file does not have group '%s'"),
3397 group_name ? group_name : "(null)");
3403 return g_strdup (group->comment->value);
3405 group_node = g_key_file_lookup_group_node (key_file, group_name);
3406 group_node = group_node->next;
3407 group = (GKeyFileGroup *)group_node->data;
3408 return get_group_comment (key_file, group, error);
3412 g_key_file_get_top_comment (GKeyFile *key_file,
3416 GKeyFileGroup *group;
3418 /* The last group in the list should be the top (comments only)
3421 g_warn_if_fail (key_file->groups != NULL);
3422 group_node = g_list_last (key_file->groups);
3423 group = (GKeyFileGroup *) group_node->data;
3424 g_warn_if_fail (group->name == NULL);
3426 return get_group_comment (key_file, group, error);
3430 * g_key_file_get_comment:
3431 * @key_file: a #GKeyFile
3432 * @group_name: (allow-none): a group name, or %NULL
3434 * @error: return location for a #GError
3436 * Retrieves a comment above @key from @group_name.
3437 * If @key is %NULL then @comment will be read from above
3438 * @group_name. If both @key and @group_name are %NULL, then
3439 * @comment will be read from above the first group in the file.
3441 * Returns: a comment that should be freed with g_free()
3446 g_key_file_get_comment (GKeyFile *key_file,
3447 const gchar *group_name,
3451 g_return_val_if_fail (key_file != NULL, NULL);
3453 if (group_name != NULL && key != NULL)
3454 return g_key_file_get_key_comment (key_file, group_name, key, error);
3455 else if (group_name != NULL)
3456 return g_key_file_get_group_comment (key_file, group_name, error);
3458 return g_key_file_get_top_comment (key_file, error);
3462 * g_key_file_remove_comment:
3463 * @key_file: a #GKeyFile
3464 * @group_name: (allow-none): a group name, or %NULL
3465 * @key: (allow-none): a key
3466 * @error: return location for a #GError
3468 * Removes a comment above @key from @group_name.
3469 * If @key is %NULL then @comment will be removed above @group_name.
3470 * If both @key and @group_name are %NULL, then @comment will
3471 * be removed above the first group in the file.
3473 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3479 g_key_file_remove_comment (GKeyFile *key_file,
3480 const gchar *group_name,
3484 g_return_val_if_fail (key_file != NULL, FALSE);
3486 if (group_name != NULL && key != NULL)
3487 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3488 else if (group_name != NULL)
3489 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3491 return g_key_file_set_top_comment (key_file, NULL, error);
3495 * g_key_file_has_group:
3496 * @key_file: a #GKeyFile
3497 * @group_name: a group name
3499 * Looks whether the key file has the group @group_name.
3501 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3506 g_key_file_has_group (GKeyFile *key_file,
3507 const gchar *group_name)
3509 g_return_val_if_fail (key_file != NULL, FALSE);
3510 g_return_val_if_fail (group_name != NULL, FALSE);
3512 return g_key_file_lookup_group (key_file, group_name) != NULL;
3515 /* This code remains from a historical attempt to add a new public API
3516 * which respects the GError rules.
3519 g_key_file_has_key_full (GKeyFile *key_file,
3520 const gchar *group_name,
3525 GKeyFileKeyValuePair *pair;
3526 GKeyFileGroup *group;
3528 g_return_val_if_fail (key_file != NULL, FALSE);
3529 g_return_val_if_fail (group_name != NULL, FALSE);
3530 g_return_val_if_fail (key != NULL, FALSE);
3532 group = g_key_file_lookup_group (key_file, group_name);
3536 g_set_error (error, G_KEY_FILE_ERROR,
3537 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3538 _("Key file does not have group '%s'"),
3539 group_name ? group_name : "(null)");
3544 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3547 *has_key = pair != NULL;
3552 * g_key_file_has_key: (skip)
3553 * @key_file: a #GKeyFile
3554 * @group_name: a group name
3556 * @error: return location for a #GError
3558 * Looks whether the key file has the key @key in the group
3561 * <note>This function does not follow the rules for #GError strictly;
3562 * the return value both carries meaning and signals an error. To use
3563 * this function, you must pass a #GError pointer in @error, and check
3564 * whether it is not %NULL to see if an error occurred.</note>
3566 * Language bindings should use g_key_file_get_value() to test whether
3567 * or not a key exists.
3569 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3575 g_key_file_has_key (GKeyFile *key_file,
3576 const gchar *group_name,
3580 GError *temp_error = NULL;
3583 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3589 g_propagate_error (error, temp_error);
3595 g_key_file_add_group (GKeyFile *key_file,
3596 const gchar *group_name)
3598 GKeyFileGroup *group;
3600 g_return_if_fail (key_file != NULL);
3601 g_return_if_fail (g_key_file_is_group_name (group_name));
3603 group = g_key_file_lookup_group (key_file, group_name);
3606 key_file->current_group = group;
3610 group = g_slice_new0 (GKeyFileGroup);
3611 group->name = g_strdup (group_name);
3612 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3613 key_file->groups = g_list_prepend (key_file->groups, group);
3614 key_file->current_group = group;
3616 if (key_file->start_group == NULL)
3617 key_file->start_group = group;
3619 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3623 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3628 g_free (pair->value);
3629 g_slice_free (GKeyFileKeyValuePair, pair);
3633 /* Be careful not to call this function on a node with data in the
3634 * lookup map without removing it from the lookup map, first.
3636 * Some current cases where this warning is not a concern are
3638 * - the node being removed is a comment node
3639 * - the entire lookup map is getting destroyed soon after
3643 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3644 GKeyFileGroup *group,
3648 GKeyFileKeyValuePair *pair;
3650 pair = (GKeyFileKeyValuePair *) pair_node->data;
3652 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3654 g_warn_if_fail (pair->value != NULL);
3656 g_key_file_key_value_pair_free (pair);
3658 g_list_free_1 (pair_node);
3662 g_key_file_remove_group_node (GKeyFile *key_file,
3665 GKeyFileGroup *group;
3668 group = (GKeyFileGroup *) group_node->data;
3671 g_hash_table_remove (key_file->group_hash, group->name);
3673 /* If the current group gets deleted make the current group the last
3676 if (key_file->current_group == group)
3678 /* groups should always contain at least the top comment group,
3679 * unless g_key_file_clear has been called
3681 if (key_file->groups)
3682 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3684 key_file->current_group = NULL;
3687 /* If the start group gets deleted make the start group the first
3690 if (key_file->start_group == group)
3692 tmp = g_list_last (key_file->groups);
3695 if (tmp != group_node &&
3696 ((GKeyFileGroup *) tmp->data)->name != NULL)
3703 key_file->start_group = (GKeyFileGroup *) tmp->data;
3705 key_file->start_group = NULL;
3708 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3710 tmp = group->key_value_pairs;
3717 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3720 g_warn_if_fail (group->key_value_pairs == NULL);
3724 g_key_file_key_value_pair_free (group->comment);
3725 group->comment = NULL;
3728 if (group->lookup_map)
3730 g_hash_table_destroy (group->lookup_map);
3731 group->lookup_map = NULL;
3734 g_free ((gchar *) group->name);
3735 g_slice_free (GKeyFileGroup, group);
3736 g_list_free_1 (group_node);
3740 * g_key_file_remove_group:
3741 * @key_file: a #GKeyFile
3742 * @group_name: a group name
3743 * @error: return location for a #GError or %NULL
3745 * Removes the specified group, @group_name,
3746 * from the key file.
3748 * Returns: %TRUE if the group was removed, %FALSE otherwise
3753 g_key_file_remove_group (GKeyFile *key_file,
3754 const gchar *group_name,
3759 g_return_val_if_fail (key_file != NULL, FALSE);
3760 g_return_val_if_fail (group_name != NULL, FALSE);
3762 group_node = g_key_file_lookup_group_node (key_file, group_name);
3766 g_set_error (error, G_KEY_FILE_ERROR,
3767 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3768 _("Key file does not have group '%s'"),
3773 g_key_file_remove_group_node (key_file, group_node);
3779 g_key_file_add_key_value_pair (GKeyFile *key_file,
3780 GKeyFileGroup *group,
3781 GKeyFileKeyValuePair *pair)
3783 g_hash_table_replace (group->lookup_map, pair->key, pair);
3784 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3788 g_key_file_add_key (GKeyFile *key_file,
3789 GKeyFileGroup *group,
3793 GKeyFileKeyValuePair *pair;
3795 pair = g_slice_new (GKeyFileKeyValuePair);
3796 pair->key = g_strdup (key);
3797 pair->value = g_strdup (value);
3799 g_key_file_add_key_value_pair (key_file, group, pair);
3803 * g_key_file_remove_key:
3804 * @key_file: a #GKeyFile
3805 * @group_name: a group name
3806 * @key: a key name to remove
3807 * @error: return location for a #GError or %NULL
3809 * Removes @key in @group_name from the key file.
3811 * Returns: %TRUE if the key was removed, %FALSE otherwise
3816 g_key_file_remove_key (GKeyFile *key_file,
3817 const gchar *group_name,
3821 GKeyFileGroup *group;
3822 GKeyFileKeyValuePair *pair;
3824 g_return_val_if_fail (key_file != NULL, FALSE);
3825 g_return_val_if_fail (group_name != NULL, FALSE);
3826 g_return_val_if_fail (key != NULL, FALSE);
3830 group = g_key_file_lookup_group (key_file, group_name);
3833 g_set_error (error, G_KEY_FILE_ERROR,
3834 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3835 _("Key file does not have group '%s'"),
3836 group_name ? group_name : "(null)");
3840 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3844 g_set_error (error, G_KEY_FILE_ERROR,
3845 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3846 _("Key file does not have key '%s' in group '%s'"),
3851 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3852 g_hash_table_remove (group->lookup_map, pair->key);
3853 g_key_file_key_value_pair_free (pair);
3859 g_key_file_lookup_group_node (GKeyFile *key_file,
3860 const gchar *group_name)
3862 GKeyFileGroup *group;
3865 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3867 group = (GKeyFileGroup *) tmp->data;
3869 if (group && group->name && strcmp (group->name, group_name) == 0)
3876 static GKeyFileGroup *
3877 g_key_file_lookup_group (GKeyFile *key_file,
3878 const gchar *group_name)
3880 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3884 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3885 GKeyFileGroup *group,
3890 for (key_node = group->key_value_pairs;
3892 key_node = key_node->next)
3894 GKeyFileKeyValuePair *pair;
3896 pair = (GKeyFileKeyValuePair *) key_node->data;
3898 if (pair->key && strcmp (pair->key, key) == 0)
3905 static GKeyFileKeyValuePair *
3906 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3907 GKeyFileGroup *group,
3910 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3913 /* Lines starting with # or consisting entirely of whitespace are merely
3914 * recorded, not parsed. This function assumes all leading whitespace
3915 * has been stripped.
3918 g_key_file_line_is_comment (const gchar *line)
3920 return (*line == '#' || *line == '\0' || *line == '\n');
3924 g_key_file_is_group_name (const gchar *name)
3931 p = q = (gchar *) name;
3932 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3933 q = g_utf8_find_next_char (q, NULL);
3935 if (*q != '\0' || q == p)
3942 g_key_file_is_key_name (const gchar *name)
3949 p = q = (gchar *) name;
3950 /* We accept a little more than the desktop entry spec says,
3951 * since gnome-vfs uses mime-types as keys in its cache.
3953 while (*q && *q != '=' && *q != '[' && *q != ']')
3954 q = g_utf8_find_next_char (q, NULL);
3956 /* No empty keys, please */
3960 /* We accept spaces in the middle of keys to not break
3961 * existing apps, but we don't tolerate initial or final
3962 * spaces, which would lead to silent corruption when
3963 * rereading the file.
3965 if (*p == ' ' || q[-1] == ' ')
3971 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3972 q = g_utf8_find_next_char (q, NULL);
3986 /* A group in a key file is made up of a starting '[' followed by one
3987 * or more letters making up the group name followed by ']'.
3990 g_key_file_line_is_group (const gchar *line)
4000 while (*p && *p != ']')
4001 p = g_utf8_find_next_char (p, NULL);
4006 /* silently accept whitespace after the ] */
4007 p = g_utf8_find_next_char (p, NULL);
4008 while (*p == ' ' || *p == '\t')
4009 p = g_utf8_find_next_char (p, NULL);
4018 g_key_file_line_is_key_value_pair (const gchar *line)
4022 p = (gchar *) g_utf8_strchr (line, -1, '=');
4027 /* Key must be non-empty
4036 g_key_file_parse_value_as_string (GKeyFile *key_file,
4041 gchar *string_value, *p, *q0, *q;
4043 string_value = g_new (gchar, strlen (value) + 1);
4045 p = (gchar *) value;
4046 q0 = q = string_value;
4076 g_set_error_literal (error, G_KEY_FILE_ERROR,
4077 G_KEY_FILE_ERROR_INVALID_VALUE,
4078 _("Key file contains escape character "
4083 if (pieces && *p == key_file->list_separator)
4084 *q = key_file->list_separator;
4098 g_set_error (error, G_KEY_FILE_ERROR,
4099 G_KEY_FILE_ERROR_INVALID_VALUE,
4100 _("Key file contains invalid escape "
4101 "sequence '%s'"), sequence);
4110 if (pieces && (*p == key_file->list_separator))
4112 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4128 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4129 *pieces = g_slist_reverse (*pieces);
4132 return string_value;
4136 g_key_file_parse_string_as_value (GKeyFile *key_file,
4137 const gchar *string,
4138 gboolean escape_separator)
4140 gchar *value, *p, *q;
4142 gboolean parsing_leading_space;
4144 length = strlen (string) + 1;
4146 /* Worst case would be that every character needs to be escaped.
4147 * In other words every character turns to two characters
4149 value = g_new (gchar, 2 * length);
4151 p = (gchar *) string;
4153 parsing_leading_space = TRUE;
4154 while (p < (string + length - 1))
4156 gchar escaped_character[3] = { '\\', 0, 0 };
4161 if (parsing_leading_space)
4163 escaped_character[1] = 's';
4164 strcpy (q, escaped_character);
4174 if (parsing_leading_space)
4176 escaped_character[1] = 't';
4177 strcpy (q, escaped_character);
4187 escaped_character[1] = 'n';
4188 strcpy (q, escaped_character);
4192 escaped_character[1] = 'r';
4193 strcpy (q, escaped_character);
4197 escaped_character[1] = '\\';
4198 strcpy (q, escaped_character);
4200 parsing_leading_space = FALSE;
4203 if (escape_separator && *p == key_file->list_separator)
4205 escaped_character[1] = key_file->list_separator;
4206 strcpy (q, escaped_character);
4208 parsing_leading_space = TRUE;
4214 parsing_leading_space = FALSE;
4226 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4235 long_value = strtol (value, &eof_int, 10);
4237 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4239 gchar *value_utf8 = _g_utf8_make_valid (value);
4240 g_set_error (error, G_KEY_FILE_ERROR,
4241 G_KEY_FILE_ERROR_INVALID_VALUE,
4242 _("Value '%s' cannot be interpreted "
4243 "as a number."), value_utf8);
4244 g_free (value_utf8);
4249 int_value = long_value;
4250 if (int_value != long_value || errno == ERANGE)
4252 gchar *value_utf8 = _g_utf8_make_valid (value);
4255 G_KEY_FILE_ERROR_INVALID_VALUE,
4256 _("Integer value '%s' out of range"),
4258 g_free (value_utf8);
4267 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4271 return g_strdup_printf ("%d", value);
4275 g_key_file_parse_value_as_double (GKeyFile *key_file,
4279 gchar *end_of_valid_d;
4280 gdouble double_value = 0;
4282 double_value = g_ascii_strtod (value, &end_of_valid_d);
4284 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4286 gchar *value_utf8 = _g_utf8_make_valid (value);
4287 g_set_error (error, G_KEY_FILE_ERROR,
4288 G_KEY_FILE_ERROR_INVALID_VALUE,
4289 _("Value '%s' cannot be interpreted "
4290 "as a float number."),
4292 g_free (value_utf8);
4295 return double_value;
4299 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4305 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4307 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4310 value_utf8 = _g_utf8_make_valid (value);
4311 g_set_error (error, G_KEY_FILE_ERROR,
4312 G_KEY_FILE_ERROR_INVALID_VALUE,
4313 _("Value '%s' cannot be interpreted "
4314 "as a boolean."), value_utf8);
4315 g_free (value_utf8);
4321 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4325 return g_strdup ("true");
4327 return g_strdup ("false");
4331 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4338 string = g_string_sized_new (512);
4340 lines = g_strsplit (value, "\n", 0);
4342 for (i = 0; lines[i] != NULL; i++)
4344 if (lines[i][0] != '#')
4345 g_string_append_printf (string, "%s\n", lines[i]);
4347 g_string_append_printf (string, "%s\n", lines[i] + 1);
4351 return g_string_free (string, FALSE);
4355 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4356 const gchar *comment)
4362 string = g_string_sized_new (512);
4364 lines = g_strsplit (comment, "\n", 0);
4366 for (i = 0; lines[i] != NULL; i++)
4367 g_string_append_printf (string, "#%s%s", lines[i],
4368 lines[i + 1] == NULL? "" : "\n");
4371 return g_string_free (string, FALSE);