1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
4 * Copyright 2009-2010 Collabora Ltd.
5 * Copyright 2009 Nokia Corporation
7 * Written by Ray Strode <rstrode@redhat.com>
8 * Matthias Clasen <mclasen@redhat.com>
10 * GLib is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * GLib is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GLib; see the file COPYING.LIB. If not,
22 * see <http://www.gnu.org/licenses/>.
36 #include <sys/types.h>
45 #define fstat(a,b) _fstati64(a,b)
50 #define S_ISREG(mode) ((mode)&_S_IFREG)
53 #endif /* G_OS_WIN23 */
58 #include "gfileutils.h"
64 #include "gmessages.h"
67 #include "gstrfuncs.h"
73 * @title: Key-value file parser
74 * @short_description: parses .ini-like config files
76 * #GKeyFile lets you parse, edit or create files containing groups of
77 * key-value pairs, which we call <firstterm>key files</firstterm> for
78 * lack of a better name. Several freedesktop.org specifications use
79 * key files now, e.g the
80 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
81 * Entry Specification</ulink> and the
82 * <ulink url="http://freedesktop.org/Standards/icon-theme-spec">Icon
83 * Theme Specification</ulink>.
85 * The syntax of key files is described in detail in the
86 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
87 * Entry Specification</ulink>, here is a quick summary: Key files
88 * consists of groups of key-value pairs, interspersed with comments.
91 * # this is just an example
92 * # there can be comments before the first group
96 * Name=Key File Example\tthis value shows\nescaping
98 * # localized strings are stored in multiple key-value pairs
101 * Welcome[fr_FR]=Bonjour
103 * Welcome[be@latin]=Hello
107 * Numbers=2;20;-200;0
109 * Booleans=true;false;true;true
112 * Lines beginning with a '#' and blank lines are considered comments.
114 * Groups are started by a header line containing the group name enclosed
115 * in '[' and ']', and ended implicitly by the start of the next group or
116 * the end of the file. Each key-value pair must be contained in a group.
118 * Key-value pairs generally have the form <literal>key=value</literal>,
119 * with the exception of localized strings, which have the form
120 * <literal>key[locale]=value</literal>, with a locale identifier of the
121 * form <literal>lang_COUNTRY\@MODIFIER</literal>
122 * where <literal>COUNTRY</literal> and <literal>MODIFIER</literal>
124 * Space before and after the '=' character are ignored. Newline, tab,
125 * carriage return and backslash characters in value are escaped as \n,
126 * \t, \r, and \\, respectively. To preserve leading spaces in values,
127 * these can also be escaped as \s.
129 * Key files can store strings (possibly with localized variants), integers,
130 * booleans and lists of these. Lists are separated by a separator character,
131 * typically ';' or ','. To use the list separator character in a value in
132 * a list, it has to be escaped by prefixing it with a backslash.
134 * This syntax is obviously inspired by the .ini files commonly met
135 * on Windows, but there are some important differences:
137 * - .ini files use the ';' character to begin comments,
138 * key files use the '#' character.
140 * - Key files do not allow for ungrouped keys meaning only
141 * comments can precede the first group.
143 * - Key files are always encoded in UTF-8.
145 * - Key and Group names are case-sensitive. For example, a group called
146 * [GROUP] is a different from [group].
148 * - .ini files don't have a strongly typed boolean entry type,
149 * they only have GetProfileInt(). In key files, only
150 * true and false (in lower case) are allowed.
152 * Note that in contrast to the
153 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
154 * Entry Specification</ulink>, groups in key files may contain the same
155 * key multiple times; the last entry wins. Key files may also contain
156 * multiple groups with the same name; they are merged together.
157 * Another difference is that keys and group names in key files are not
158 * restricted to ASCII characters.
164 * Error domain for key file parsing. Errors in this domain will
165 * be from the #GKeyFileError enumeration.
167 * See #GError for information on error domains.
172 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
173 * an unknown encoding
174 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
175 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
176 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
177 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
178 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
180 * Error codes returned by key file parsing.
185 * @G_KEY_FILE_NONE: No flags, default behaviour
186 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
187 * (possibly modified) contents of the key file back to a file;
188 * otherwise all comments will be lost when the key file is
190 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
191 * (possibly modified) contents of the key file back to a file;
192 * otherwise only the translations for the current language will be
195 * Flags which influence the parsing.
199 * G_KEY_FILE_DESKTOP_GROUP:
201 * The name of the main group of a desktop entry file, as defined in the
202 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
203 * Entry Specification</ulink>. Consult the specification for more
204 * details about the meanings of the keys below.
210 * G_KEY_FILE_DESKTOP_KEY_TYPE:
212 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
213 * giving the type of the desktop entry. Usually
214 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
215 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
216 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
222 * G_KEY_FILE_DESKTOP_KEY_VERSION:
224 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
225 * giving the version of the Desktop Entry Specification used for
226 * the desktop entry file.
232 * G_KEY_FILE_DESKTOP_KEY_NAME:
234 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
235 * string giving the specific name of the desktop entry.
241 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
243 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
244 * string giving the generic name of the desktop entry.
250 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
252 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
253 * stating whether the desktop entry should be shown in menus.
259 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
261 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
262 * string giving the tooltip for the desktop entry.
268 * G_KEY_FILE_DESKTOP_KEY_ICON:
270 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
271 * string giving the name of the icon to be displayed for the desktop
278 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
280 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
281 * stating whether the desktop entry has been deleted by the user.
287 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
289 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
290 * strings identifying the environments that should display the
297 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
299 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
300 * strings identifying the environments that should not display the
307 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
309 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
310 * giving the file name of a binary on disk used to determine if the
311 * program is actually installed. It is only valid for desktop entries
312 * with the <literal>Application</literal> type.
318 * G_KEY_FILE_DESKTOP_KEY_EXEC:
320 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
321 * giving the command line to execute. It is only valid for desktop
322 * entries with the <literal>Application</literal> type.
328 * G_KEY_FILE_DESKTOP_KEY_PATH:
330 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
331 * containing the working directory to run the program in. It is only
332 * valid for desktop entries with the <literal>Application</literal> type.
338 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
340 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
341 * stating whether the program should be run in a terminal window.
342 * It is only valid for desktop entries with the
343 * <literal>Application</literal> type.
349 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
351 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
352 * of strings giving the MIME types supported by this desktop entry.
358 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
360 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
361 * of strings giving the categories in which the desktop entry
362 * should be shown in a menu.
368 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
370 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
371 * stating whether the application supports the <ulink
372 * url="http://www.freedesktop.org/Standards/startup-notification-spec">Startup
373 * Notification Protocol Specification</ulink>.
379 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
381 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
382 * identifying the WM class or name hint of a window that the application
383 * will create, which can be used to emulate Startup Notification with
384 * older applications.
390 * G_KEY_FILE_DESKTOP_KEY_URL :
392 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
393 * giving the URL to access. It is only valid for desktop entries
394 * with the <literal>Link</literal> type.
400 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
402 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
403 * entries representing applications.
409 * G_KEY_FILE_DESKTOP_TYPE_LINK:
411 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
412 * entries representing links to documents.
418 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
420 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
421 * entries representing directories.
426 typedef struct _GKeyFileGroup GKeyFileGroup;
431 * The GKeyFile struct contains only private data
432 * and should not be accessed directly.
437 GHashTable *group_hash;
439 GKeyFileGroup *start_group;
440 GKeyFileGroup *current_group;
442 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
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 */
461 GList *key_value_pairs;
463 /* Used in parallel with key_value_pairs for
464 * increased lookup performance
466 GHashTable *lookup_map;
469 struct _GKeyFileKeyValuePair
471 gchar *key; /* NULL for comments */
475 static gint find_file_in_data_dirs (const gchar *file,
476 const gchar **data_dirs,
479 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
483 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
484 const gchar *group_name);
485 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
486 const gchar *group_name);
488 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
489 GKeyFileGroup *group,
491 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
492 GKeyFileGroup *group,
495 static void g_key_file_remove_group_node (GKeyFile *key_file,
497 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
498 GKeyFileGroup *group,
501 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
502 GKeyFileGroup *group,
503 GKeyFileKeyValuePair *pair);
504 static void g_key_file_add_key (GKeyFile *key_file,
505 GKeyFileGroup *group,
508 static void g_key_file_add_group (GKeyFile *key_file,
509 const gchar *group_name);
510 static gboolean g_key_file_is_group_name (const gchar *name);
511 static gboolean g_key_file_is_key_name (const gchar *name);
512 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
513 static gboolean g_key_file_line_is_comment (const gchar *line);
514 static gboolean g_key_file_line_is_group (const gchar *line);
515 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
516 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
520 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
522 gboolean escape_separator);
523 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
526 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
528 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
531 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
534 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
536 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
538 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
539 const gchar *comment);
540 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
544 static void g_key_file_parse_comment (GKeyFile *key_file,
548 static void g_key_file_parse_group (GKeyFile *key_file,
552 static gchar *key_get_locale (const gchar *key);
553 static void g_key_file_parse_data (GKeyFile *key_file,
557 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
560 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
563 g_key_file_init (GKeyFile *key_file)
565 key_file->current_group = g_slice_new0 (GKeyFileGroup);
566 key_file->groups = g_list_prepend (NULL, key_file->current_group);
567 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
568 key_file->start_group = NULL;
569 key_file->parse_buffer = g_string_sized_new (128);
570 key_file->list_separator = ';';
572 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
576 g_key_file_clear (GKeyFile *key_file)
578 GList *tmp, *group_node;
580 if (key_file->locales)
582 g_strfreev (key_file->locales);
583 key_file->locales = NULL;
586 if (key_file->parse_buffer)
588 g_string_free (key_file->parse_buffer, TRUE);
589 key_file->parse_buffer = NULL;
592 tmp = key_file->groups;
597 g_key_file_remove_group_node (key_file, group_node);
600 if (key_file->group_hash != NULL)
602 g_hash_table_destroy (key_file->group_hash);
603 key_file->group_hash = NULL;
606 g_warn_if_fail (key_file->groups == NULL);
613 * Creates a new empty #GKeyFile object. Use
614 * g_key_file_load_from_file(), g_key_file_load_from_data(),
615 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
616 * read an existing key file.
618 * Return value: (transfer full): an empty #GKeyFile.
623 g_key_file_new (void)
627 key_file = g_slice_new0 (GKeyFile);
628 key_file->ref_count = 1;
629 g_key_file_init (key_file);
635 * g_key_file_set_list_separator:
636 * @key_file: a #GKeyFile
637 * @separator: the separator
639 * Sets the character which is used to separate
640 * values in lists. Typically ';' or ',' are used
641 * as separators. The default list separator is ';'.
646 g_key_file_set_list_separator (GKeyFile *key_file,
649 g_return_if_fail (key_file != NULL);
651 key_file->list_separator = separator;
655 /* Iterates through all the directories in *dirs trying to
656 * open file. When it successfully locates and opens a file it
657 * returns the file descriptor to the open file. It also
658 * outputs the absolute path of the file in output_file.
661 find_file_in_data_dirs (const gchar *file,
666 const gchar **data_dirs, *data_dir;
678 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
680 gchar *candidate_file, *sub_dir;
682 candidate_file = (gchar *) file;
683 sub_dir = g_strdup ("");
684 while (candidate_file != NULL && fd == -1)
688 path = g_build_filename (data_dir, sub_dir,
689 candidate_file, NULL);
691 fd = g_open (path, O_RDONLY, 0);
699 candidate_file = strchr (candidate_file, '-');
701 if (candidate_file == NULL)
707 sub_dir = g_strndup (file, candidate_file - file - 1);
709 for (p = sub_dir; *p != '\0'; p++)
712 *p = G_DIR_SEPARATOR;
721 g_set_error_literal (error, G_KEY_FILE_ERROR,
722 G_KEY_FILE_ERROR_NOT_FOUND,
723 _("Valid key file could not be "
724 "found in search dirs"));
727 if (output_file != NULL && fd > 0)
728 *output_file = g_strdup (path);
736 g_key_file_load_from_fd (GKeyFile *key_file,
741 GError *key_file_error = NULL;
743 struct stat stat_buf;
744 gchar read_buf[4096];
745 gchar list_separator;
747 if (fstat (fd, &stat_buf) < 0)
749 g_set_error_literal (error, G_FILE_ERROR,
750 g_file_error_from_errno (errno),
755 if (!S_ISREG (stat_buf.st_mode))
757 g_set_error_literal (error, G_KEY_FILE_ERROR,
758 G_KEY_FILE_ERROR_PARSE,
759 _("Not a regular file"));
763 list_separator = key_file->list_separator;
764 g_key_file_clear (key_file);
765 g_key_file_init (key_file);
766 key_file->list_separator = list_separator;
767 key_file->flags = flags;
771 bytes_read = read (fd, read_buf, 4096);
773 if (bytes_read == 0) /* End of File */
778 if (errno == EINTR || errno == EAGAIN)
781 g_set_error_literal (error, G_FILE_ERROR,
782 g_file_error_from_errno (errno),
787 g_key_file_parse_data (key_file,
788 read_buf, bytes_read,
791 while (!key_file_error);
795 g_propagate_error (error, key_file_error);
799 g_key_file_flush_parse_buffer (key_file, &key_file_error);
803 g_propagate_error (error, key_file_error);
811 * g_key_file_load_from_file:
812 * @key_file: an empty #GKeyFile struct
813 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
814 * @flags: flags from #GKeyFileFlags
815 * @error: return location for a #GError, or %NULL
817 * Loads a key file into an empty #GKeyFile structure.
818 * If the file could not be loaded then @error is set to
819 * either a #GFileError or #GKeyFileError.
821 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
826 g_key_file_load_from_file (GKeyFile *key_file,
831 GError *key_file_error = NULL;
834 g_return_val_if_fail (key_file != NULL, FALSE);
835 g_return_val_if_fail (file != NULL, FALSE);
837 fd = g_open (file, O_RDONLY, 0);
841 g_set_error_literal (error, G_FILE_ERROR,
842 g_file_error_from_errno (errno),
847 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
852 g_propagate_error (error, key_file_error);
860 * g_key_file_load_from_data:
861 * @key_file: an empty #GKeyFile struct
862 * @data: key file loaded in memory
863 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
864 * @flags: flags from #GKeyFileFlags
865 * @error: return location for a #GError, or %NULL
867 * Loads a key file from memory into an empty #GKeyFile structure.
868 * If the object cannot be created then %error is set to a #GKeyFileError.
870 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
875 g_key_file_load_from_data (GKeyFile *key_file,
881 GError *key_file_error = NULL;
882 gchar list_separator;
884 g_return_val_if_fail (key_file != NULL, FALSE);
885 g_return_val_if_fail (data != NULL || length == 0, FALSE);
887 if (length == (gsize)-1)
888 length = strlen (data);
890 list_separator = key_file->list_separator;
891 g_key_file_clear (key_file);
892 g_key_file_init (key_file);
893 key_file->list_separator = list_separator;
894 key_file->flags = flags;
896 g_key_file_parse_data (key_file, data, length, &key_file_error);
900 g_propagate_error (error, key_file_error);
904 g_key_file_flush_parse_buffer (key_file, &key_file_error);
908 g_propagate_error (error, key_file_error);
916 * g_key_file_load_from_dirs:
917 * @key_file: an empty #GKeyFile struct
918 * @file: (type filename): a relative path to a filename to open and parse
919 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
920 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
921 * of the file, or %NULL
922 * @flags: flags from #GKeyFileFlags
923 * @error: return location for a #GError, or %NULL
925 * This function looks for a key file named @file in the paths
926 * specified in @search_dirs, loads the file into @key_file and
927 * returns the file's full path in @full_path. If the file could not
928 * be loaded then an %error is set to either a #GFileError or
931 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
936 g_key_file_load_from_dirs (GKeyFile *key_file,
938 const gchar **search_dirs,
943 GError *key_file_error = NULL;
944 const gchar **data_dirs;
949 g_return_val_if_fail (key_file != NULL, FALSE);
950 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
951 g_return_val_if_fail (search_dirs != NULL, FALSE);
954 data_dirs = search_dirs;
956 while (*data_dirs != NULL && !found_file)
958 g_free (output_path);
960 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
966 g_propagate_error (error, key_file_error);
970 found_file = g_key_file_load_from_fd (key_file, fd, flags,
976 g_propagate_error (error, key_file_error);
981 if (found_file && full_path)
982 *full_path = output_path;
984 g_free (output_path);
990 * g_key_file_load_from_data_dirs:
991 * @key_file: an empty #GKeyFile struct
992 * @file: (type filename): a relative path to a filename to open and parse
993 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
994 * of the file, or %NULL
995 * @flags: flags from #GKeyFileFlags
996 * @error: return location for a #GError, or %NULL
998 * This function looks for a key file named @file in the paths
999 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1000 * loads the file into @key_file and returns the file's full path in
1001 * @full_path. If the file could not be loaded then an %error is
1002 * set to either a #GFileError or #GKeyFileError.
1004 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1008 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1011 GKeyFileFlags flags,
1014 gchar **all_data_dirs;
1015 const gchar * user_data_dir;
1016 const gchar * const * system_data_dirs;
1018 gboolean found_file;
1020 g_return_val_if_fail (key_file != NULL, FALSE);
1021 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1023 user_data_dir = g_get_user_data_dir ();
1024 system_data_dirs = g_get_system_data_dirs ();
1025 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1028 all_data_dirs[i++] = g_strdup (user_data_dir);
1031 while (system_data_dirs[j] != NULL)
1032 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1033 all_data_dirs[i] = NULL;
1035 found_file = g_key_file_load_from_dirs (key_file,
1037 (const gchar **)all_data_dirs,
1042 g_strfreev (all_data_dirs);
1048 * g_key_file_ref: (skip)
1049 * @key_file: a #GKeyFile
1051 * Increases the reference count of @key_file.
1053 * Returns: the same @key_file.
1058 g_key_file_ref (GKeyFile *key_file)
1060 g_return_val_if_fail (key_file != NULL, NULL);
1062 g_atomic_int_inc (&key_file->ref_count);
1068 * g_key_file_free: (skip)
1069 * @key_file: a #GKeyFile
1071 * Clears all keys and groups from @key_file, and decreases the
1072 * reference count by 1. If the reference count reaches zero,
1073 * frees the key file and all its allocated memory.
1078 g_key_file_free (GKeyFile *key_file)
1080 g_return_if_fail (key_file != NULL);
1082 g_key_file_clear (key_file);
1083 g_key_file_unref (key_file);
1088 * @key_file: a #GKeyFile
1090 * Decreases the reference count of @key_file by 1. If the reference count
1091 * reaches zero, frees the key file and all its allocated memory.
1096 g_key_file_unref (GKeyFile *key_file)
1098 g_return_if_fail (key_file != NULL);
1100 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1102 g_key_file_clear (key_file);
1103 g_slice_free (GKeyFile, key_file);
1107 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1108 * true for locales that match those in g_get_language_names().
1111 g_key_file_locale_is_interesting (GKeyFile *key_file,
1112 const gchar *locale)
1116 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1119 for (i = 0; key_file->locales[i] != NULL; i++)
1121 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1129 g_key_file_parse_line (GKeyFile *key_file,
1134 GError *parse_error = NULL;
1137 g_return_if_fail (key_file != NULL);
1138 g_return_if_fail (line != NULL);
1140 line_start = (gchar *) line;
1141 while (g_ascii_isspace (*line_start))
1144 if (g_key_file_line_is_comment (line_start))
1145 g_key_file_parse_comment (key_file, line, length, &parse_error);
1146 else if (g_key_file_line_is_group (line_start))
1147 g_key_file_parse_group (key_file, line_start,
1148 length - (line_start - line),
1150 else if (g_key_file_line_is_key_value_pair (line_start))
1151 g_key_file_parse_key_value_pair (key_file, line_start,
1152 length - (line_start - line),
1156 gchar *line_utf8 = _g_utf8_make_valid (line);
1157 g_set_error (error, G_KEY_FILE_ERROR,
1158 G_KEY_FILE_ERROR_PARSE,
1159 _("Key file contains line '%s' which is not "
1160 "a key-value pair, group, or comment"),
1168 g_propagate_error (error, parse_error);
1172 g_key_file_parse_comment (GKeyFile *key_file,
1177 GKeyFileKeyValuePair *pair;
1179 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1182 g_warn_if_fail (key_file->current_group != NULL);
1184 pair = g_slice_new (GKeyFileKeyValuePair);
1186 pair->value = g_strndup (line, length);
1188 key_file->current_group->key_value_pairs =
1189 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1193 g_key_file_parse_group (GKeyFile *key_file,
1199 const gchar *group_name_start, *group_name_end;
1201 /* advance past opening '['
1203 group_name_start = line + 1;
1204 group_name_end = line + length - 1;
1206 while (*group_name_end != ']')
1209 group_name = g_strndup (group_name_start,
1210 group_name_end - group_name_start);
1212 if (!g_key_file_is_group_name (group_name))
1214 g_set_error (error, G_KEY_FILE_ERROR,
1215 G_KEY_FILE_ERROR_PARSE,
1216 _("Invalid group name: %s"), group_name);
1217 g_free (group_name);
1221 g_key_file_add_group (key_file, group_name);
1222 g_free (group_name);
1226 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1231 gchar *key, *value, *key_end, *value_start, *locale;
1232 gsize key_len, value_len;
1234 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1236 g_set_error_literal (error, G_KEY_FILE_ERROR,
1237 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1238 _("Key file does not start with a group"));
1242 key_end = value_start = strchr (line, '=');
1244 g_warn_if_fail (key_end != NULL);
1249 /* Pull the key name from the line (chomping trailing whitespace)
1251 while (g_ascii_isspace (*key_end))
1254 key_len = key_end - line + 2;
1256 g_warn_if_fail (key_len <= length);
1258 key = g_strndup (line, key_len - 1);
1260 if (!g_key_file_is_key_name (key))
1262 g_set_error (error, G_KEY_FILE_ERROR,
1263 G_KEY_FILE_ERROR_PARSE,
1264 _("Invalid key name: %s"), key);
1269 /* Pull the value from the line (chugging leading whitespace)
1271 while (g_ascii_isspace (*value_start))
1274 value_len = line + length - value_start + 1;
1276 value = g_strndup (value_start, value_len);
1278 g_warn_if_fail (key_file->start_group != NULL);
1280 if (key_file->current_group
1281 && key_file->current_group->name
1282 && strcmp (key_file->start_group->name,
1283 key_file->current_group->name) == 0
1284 && strcmp (key, "Encoding") == 0)
1286 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1288 gchar *value_utf8 = _g_utf8_make_valid (value);
1289 g_set_error (error, G_KEY_FILE_ERROR,
1290 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1291 _("Key file contains unsupported "
1292 "encoding '%s'"), value_utf8);
1293 g_free (value_utf8);
1301 /* Is this key a translation? If so, is it one that we care about?
1303 locale = key_get_locale (key);
1305 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1307 GKeyFileKeyValuePair *pair;
1309 pair = g_slice_new (GKeyFileKeyValuePair);
1311 pair->value = value;
1313 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1325 key_get_locale (const gchar *key)
1329 locale = g_strrstr (key, "[");
1331 if (locale && strlen (locale) <= 2)
1335 locale = g_strndup (locale + 1, strlen (locale) - 2);
1341 g_key_file_parse_data (GKeyFile *key_file,
1346 GError *parse_error;
1349 g_return_if_fail (key_file != NULL);
1350 g_return_if_fail (data != NULL || length == 0);
1357 if (data[i] == '\n')
1359 if (key_file->parse_buffer->len > 0
1360 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1362 g_string_erase (key_file->parse_buffer,
1363 key_file->parse_buffer->len - 1,
1366 /* When a newline is encountered flush the parse buffer so that the
1367 * line can be parsed. Note that completely blank lines won't show
1368 * up in the parse buffer, so they get parsed directly.
1370 if (key_file->parse_buffer->len > 0)
1371 g_key_file_flush_parse_buffer (key_file, &parse_error);
1373 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1377 g_propagate_error (error, parse_error);
1384 const gchar *start_of_line;
1385 const gchar *end_of_line;
1388 start_of_line = data + i;
1389 end_of_line = memchr (start_of_line, '\n', length - i);
1391 if (end_of_line == NULL)
1392 end_of_line = data + length;
1394 line_length = end_of_line - start_of_line;
1396 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1403 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1406 GError *file_error = NULL;
1408 g_return_if_fail (key_file != NULL);
1412 if (key_file->parse_buffer->len > 0)
1414 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1415 key_file->parse_buffer->len,
1417 g_string_erase (key_file->parse_buffer, 0, -1);
1421 g_propagate_error (error, file_error);
1428 * g_key_file_to_data:
1429 * @key_file: a #GKeyFile
1430 * @length: (out) (allow-none): return location for the length of the
1431 * returned string, or %NULL
1432 * @error: return location for a #GError, or %NULL
1434 * This function outputs @key_file as a string.
1436 * Note that this function never reports an error,
1437 * so it is safe to pass %NULL as @error.
1439 * Return value: a newly allocated string holding
1440 * the contents of the #GKeyFile
1445 g_key_file_to_data (GKeyFile *key_file,
1449 GString *data_string;
1450 GList *group_node, *key_file_node;
1452 g_return_val_if_fail (key_file != NULL, NULL);
1454 data_string = g_string_new (NULL);
1456 for (group_node = g_list_last (key_file->groups);
1458 group_node = group_node->prev)
1460 GKeyFileGroup *group;
1462 group = (GKeyFileGroup *) group_node->data;
1464 /* separate groups by at least an empty line */
1465 if (data_string->len >= 2 &&
1466 data_string->str[data_string->len - 2] != '\n')
1467 g_string_append_c (data_string, '\n');
1469 if (group->comment != NULL)
1470 g_string_append_printf (data_string, "%s\n", group->comment->value);
1472 if (group->name != NULL)
1473 g_string_append_printf (data_string, "[%s]\n", group->name);
1475 for (key_file_node = g_list_last (group->key_value_pairs);
1476 key_file_node != NULL;
1477 key_file_node = key_file_node->prev)
1479 GKeyFileKeyValuePair *pair;
1481 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1483 if (pair->key != NULL)
1484 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1486 g_string_append_printf (data_string, "%s\n", pair->value);
1491 *length = data_string->len;
1493 return g_string_free (data_string, FALSE);
1497 * g_key_file_get_keys:
1498 * @key_file: a #GKeyFile
1499 * @group_name: a group name
1500 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1501 * @error: return location for a #GError, or %NULL
1503 * Returns all keys for the group name @group_name. The array of
1504 * returned keys will be %NULL-terminated, so @length may
1505 * optionally be %NULL. In the event that the @group_name cannot
1506 * be found, %NULL is returned and @error is set to
1507 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1509 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1510 * Use g_strfreev() to free it.
1515 g_key_file_get_keys (GKeyFile *key_file,
1516 const gchar *group_name,
1520 GKeyFileGroup *group;
1525 g_return_val_if_fail (key_file != NULL, NULL);
1526 g_return_val_if_fail (group_name != NULL, NULL);
1528 group = g_key_file_lookup_group (key_file, group_name);
1532 g_set_error (error, G_KEY_FILE_ERROR,
1533 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1534 _("Key file does not have group '%s'"),
1535 group_name ? group_name : "(null)");
1540 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1542 GKeyFileKeyValuePair *pair;
1544 pair = (GKeyFileKeyValuePair *) tmp->data;
1550 keys = g_new (gchar *, num_keys + 1);
1553 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1555 GKeyFileKeyValuePair *pair;
1557 pair = (GKeyFileKeyValuePair *) tmp->data;
1561 keys[i] = g_strdup (pair->key);
1566 keys[num_keys] = NULL;
1575 * g_key_file_get_start_group:
1576 * @key_file: a #GKeyFile
1578 * Returns the name of the start group of the file.
1580 * Return value: The start group of the key file.
1585 g_key_file_get_start_group (GKeyFile *key_file)
1587 g_return_val_if_fail (key_file != NULL, NULL);
1589 if (key_file->start_group)
1590 return g_strdup (key_file->start_group->name);
1596 * g_key_file_get_groups:
1597 * @key_file: a #GKeyFile
1598 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1600 * Returns all groups in the key file loaded with @key_file.
1601 * The array of returned groups will be %NULL-terminated, so
1602 * @length may optionally be %NULL.
1604 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1605 * Use g_strfreev() to free it.
1609 g_key_file_get_groups (GKeyFile *key_file,
1614 gsize i, num_groups;
1616 g_return_val_if_fail (key_file != NULL, NULL);
1618 num_groups = g_list_length (key_file->groups);
1620 g_return_val_if_fail (num_groups > 0, NULL);
1622 group_node = g_list_last (key_file->groups);
1624 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1626 /* Only need num_groups instead of num_groups + 1
1627 * because the first group of the file (last in the
1628 * list) is always the comment group at the top,
1631 groups = g_new (gchar *, num_groups);
1635 for (group_node = group_node->prev;
1637 group_node = group_node->prev)
1639 GKeyFileGroup *group;
1641 group = (GKeyFileGroup *) group_node->data;
1643 g_warn_if_fail (group->name != NULL);
1645 groups[i++] = g_strdup (group->name);
1656 * g_key_file_get_value:
1657 * @key_file: a #GKeyFile
1658 * @group_name: a group name
1660 * @error: return location for a #GError, or %NULL
1662 * Returns the raw value associated with @key under @group_name.
1663 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1665 * In the event the key cannot be found, %NULL is returned and
1666 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1667 * event that the @group_name cannot be found, %NULL is returned
1668 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1671 * Return value: a newly allocated string or %NULL if the specified
1672 * key cannot be found.
1677 g_key_file_get_value (GKeyFile *key_file,
1678 const gchar *group_name,
1682 GKeyFileGroup *group;
1683 GKeyFileKeyValuePair *pair;
1684 gchar *value = NULL;
1686 g_return_val_if_fail (key_file != NULL, NULL);
1687 g_return_val_if_fail (group_name != NULL, NULL);
1688 g_return_val_if_fail (key != NULL, NULL);
1690 group = g_key_file_lookup_group (key_file, group_name);
1694 g_set_error (error, G_KEY_FILE_ERROR,
1695 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1696 _("Key file does not have group '%s'"),
1697 group_name ? group_name : "(null)");
1701 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1704 value = g_strdup (pair->value);
1706 g_set_error (error, G_KEY_FILE_ERROR,
1707 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1708 _("Key file does not have key '%s'"), key);
1714 * g_key_file_set_value:
1715 * @key_file: a #GKeyFile
1716 * @group_name: a group name
1720 * Associates a new value with @key under @group_name.
1722 * If @key cannot be found then it is created. If @group_name cannot
1723 * be found then it is created. To set an UTF-8 string which may contain
1724 * characters that need escaping (such as newlines or spaces), use
1725 * g_key_file_set_string().
1730 g_key_file_set_value (GKeyFile *key_file,
1731 const gchar *group_name,
1735 GKeyFileGroup *group;
1736 GKeyFileKeyValuePair *pair;
1738 g_return_if_fail (key_file != NULL);
1739 g_return_if_fail (g_key_file_is_group_name (group_name));
1740 g_return_if_fail (g_key_file_is_key_name (key));
1741 g_return_if_fail (value != NULL);
1743 group = g_key_file_lookup_group (key_file, group_name);
1747 g_key_file_add_group (key_file, group_name);
1748 group = (GKeyFileGroup *) key_file->groups->data;
1750 g_key_file_add_key (key_file, group, key, value);
1754 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1757 g_key_file_add_key (key_file, group, key, value);
1760 g_free (pair->value);
1761 pair->value = g_strdup (value);
1767 * g_key_file_get_string:
1768 * @key_file: a #GKeyFile
1769 * @group_name: a group name
1771 * @error: return location for a #GError, or %NULL
1773 * Returns the string value associated with @key under @group_name.
1774 * Unlike g_key_file_get_value(), this function handles escape sequences
1777 * In the event the key cannot be found, %NULL is returned and
1778 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1779 * event that the @group_name cannot be found, %NULL is returned
1780 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1782 * Return value: a newly allocated string or %NULL if the specified
1783 * key cannot be found.
1788 g_key_file_get_string (GKeyFile *key_file,
1789 const gchar *group_name,
1793 gchar *value, *string_value;
1794 GError *key_file_error;
1796 g_return_val_if_fail (key_file != NULL, NULL);
1797 g_return_val_if_fail (group_name != NULL, NULL);
1798 g_return_val_if_fail (key != NULL, NULL);
1800 key_file_error = NULL;
1802 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1806 g_propagate_error (error, key_file_error);
1810 if (!g_utf8_validate (value, -1, NULL))
1812 gchar *value_utf8 = _g_utf8_make_valid (value);
1813 g_set_error (error, G_KEY_FILE_ERROR,
1814 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1815 _("Key file contains key '%s' with value '%s' "
1816 "which is not UTF-8"), key, value_utf8);
1817 g_free (value_utf8);
1823 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1829 if (g_error_matches (key_file_error,
1831 G_KEY_FILE_ERROR_INVALID_VALUE))
1833 g_set_error (error, G_KEY_FILE_ERROR,
1834 G_KEY_FILE_ERROR_INVALID_VALUE,
1835 _("Key file contains key '%s' "
1836 "which has a value that cannot be interpreted."),
1838 g_error_free (key_file_error);
1841 g_propagate_error (error, key_file_error);
1844 return string_value;
1848 * g_key_file_set_string:
1849 * @key_file: a #GKeyFile
1850 * @group_name: a group name
1854 * Associates a new string value with @key under @group_name.
1855 * If @key cannot be found then it is created.
1856 * If @group_name cannot be found then it is created.
1857 * Unlike g_key_file_set_value(), this function handles characters
1858 * that need escaping, such as newlines.
1863 g_key_file_set_string (GKeyFile *key_file,
1864 const gchar *group_name,
1866 const gchar *string)
1870 g_return_if_fail (key_file != NULL);
1871 g_return_if_fail (string != NULL);
1873 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1874 g_key_file_set_value (key_file, group_name, key, value);
1879 * g_key_file_get_string_list:
1880 * @key_file: a #GKeyFile
1881 * @group_name: a group name
1883 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1884 * @error: return location for a #GError, or %NULL
1886 * Returns the values associated with @key under @group_name.
1888 * In the event the key cannot be found, %NULL is returned and
1889 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1890 * event that the @group_name cannot be found, %NULL is returned
1891 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1893 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1894 * a %NULL-terminated string array or %NULL if the specified
1895 * key cannot be found. The array should be freed with g_strfreev().
1900 g_key_file_get_string_list (GKeyFile *key_file,
1901 const gchar *group_name,
1906 GError *key_file_error = NULL;
1907 gchar *value, *string_value, **values;
1909 GSList *p, *pieces = NULL;
1911 g_return_val_if_fail (key_file != NULL, NULL);
1912 g_return_val_if_fail (group_name != NULL, NULL);
1913 g_return_val_if_fail (key != NULL, NULL);
1918 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1922 g_propagate_error (error, key_file_error);
1926 if (!g_utf8_validate (value, -1, NULL))
1928 gchar *value_utf8 = _g_utf8_make_valid (value);
1929 g_set_error (error, G_KEY_FILE_ERROR,
1930 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1931 _("Key file contains key '%s' with value '%s' "
1932 "which is not UTF-8"), key, value_utf8);
1933 g_free (value_utf8);
1939 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1941 g_free (string_value);
1945 if (g_error_matches (key_file_error,
1947 G_KEY_FILE_ERROR_INVALID_VALUE))
1949 g_set_error (error, G_KEY_FILE_ERROR,
1950 G_KEY_FILE_ERROR_INVALID_VALUE,
1951 _("Key file contains key '%s' "
1952 "which has a value that cannot be interpreted."),
1954 g_error_free (key_file_error);
1957 g_propagate_error (error, key_file_error);
1959 g_slist_free_full (pieces, g_free);
1963 len = g_slist_length (pieces);
1964 values = g_new (gchar *, len + 1);
1965 for (p = pieces, i = 0; p; p = p->next)
1966 values[i++] = p->data;
1969 g_slist_free (pieces);
1978 * g_key_file_set_string_list:
1979 * @key_file: a #GKeyFile
1980 * @group_name: a group name
1982 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1983 * @length: number of string values in @list
1985 * Associates a list of string values for @key under @group_name.
1986 * If @key cannot be found then it is created.
1987 * If @group_name cannot be found then it is created.
1992 g_key_file_set_string_list (GKeyFile *key_file,
1993 const gchar *group_name,
1995 const gchar * const list[],
1998 GString *value_list;
2001 g_return_if_fail (key_file != NULL);
2002 g_return_if_fail (list != NULL || length == 0);
2004 value_list = g_string_sized_new (length * 128);
2005 for (i = 0; i < length && list[i] != NULL; i++)
2009 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2010 g_string_append (value_list, value);
2011 g_string_append_c (value_list, key_file->list_separator);
2016 g_key_file_set_value (key_file, group_name, key, value_list->str);
2017 g_string_free (value_list, TRUE);
2021 * g_key_file_set_locale_string:
2022 * @key_file: a #GKeyFile
2023 * @group_name: a group name
2025 * @locale: a locale identifier
2028 * Associates a string value for @key and @locale under @group_name.
2029 * If the translation for @key cannot be found then it is created.
2034 g_key_file_set_locale_string (GKeyFile *key_file,
2035 const gchar *group_name,
2037 const gchar *locale,
2038 const gchar *string)
2040 gchar *full_key, *value;
2042 g_return_if_fail (key_file != NULL);
2043 g_return_if_fail (key != NULL);
2044 g_return_if_fail (locale != NULL);
2045 g_return_if_fail (string != NULL);
2047 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2048 full_key = g_strdup_printf ("%s[%s]", key, locale);
2049 g_key_file_set_value (key_file, group_name, full_key, value);
2055 * g_key_file_get_locale_string:
2056 * @key_file: a #GKeyFile
2057 * @group_name: a group name
2059 * @locale: (allow-none): a locale identifier or %NULL
2060 * @error: return location for a #GError, or %NULL
2062 * Returns the value associated with @key under @group_name
2063 * translated in the given @locale if available. If @locale is
2064 * %NULL then the current locale is assumed.
2066 * If @key cannot be found then %NULL is returned and @error is set
2067 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2068 * with @key cannot be interpreted or no suitable translation can
2069 * be found then the untranslated value is returned.
2071 * Return value: a newly allocated string or %NULL if the specified
2072 * key cannot be found.
2077 g_key_file_get_locale_string (GKeyFile *key_file,
2078 const gchar *group_name,
2080 const gchar *locale,
2083 gchar *candidate_key, *translated_value;
2084 GError *key_file_error;
2086 gboolean free_languages = FALSE;
2089 g_return_val_if_fail (key_file != NULL, NULL);
2090 g_return_val_if_fail (group_name != NULL, NULL);
2091 g_return_val_if_fail (key != NULL, NULL);
2093 candidate_key = NULL;
2094 translated_value = NULL;
2095 key_file_error = NULL;
2099 languages = g_get_locale_variants (locale);
2100 free_languages = TRUE;
2104 languages = (gchar **) g_get_language_names ();
2105 free_languages = FALSE;
2108 for (i = 0; languages[i]; i++)
2110 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2112 translated_value = g_key_file_get_string (key_file,
2114 candidate_key, NULL);
2115 g_free (candidate_key);
2117 if (translated_value)
2120 g_free (translated_value);
2121 translated_value = NULL;
2124 /* Fallback to untranslated key
2126 if (!translated_value)
2128 translated_value = g_key_file_get_string (key_file, group_name, key,
2131 if (!translated_value)
2132 g_propagate_error (error, key_file_error);
2136 g_strfreev (languages);
2138 return translated_value;
2142 * g_key_file_get_locale_string_list:
2143 * @key_file: a #GKeyFile
2144 * @group_name: a group name
2146 * @locale: (allow-none): a locale identifier or %NULL
2147 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2148 * @error: return location for a #GError or %NULL
2150 * Returns the values associated with @key under @group_name
2151 * translated in the given @locale if available. If @locale is
2152 * %NULL then the current locale is assumed.
2154 * If @key cannot be found then %NULL is returned and @error is set
2155 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2156 * with @key cannot be interpreted or no suitable translations
2157 * can be found then the untranslated values are returned. The
2158 * returned array is %NULL-terminated, so @length may optionally
2161 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2162 * or %NULL if the key isn't found. The string array should be freed
2163 * with g_strfreev().
2168 g_key_file_get_locale_string_list (GKeyFile *key_file,
2169 const gchar *group_name,
2171 const gchar *locale,
2175 GError *key_file_error;
2176 gchar **values, *value;
2177 char list_separator[2];
2180 g_return_val_if_fail (key_file != NULL, NULL);
2181 g_return_val_if_fail (group_name != NULL, NULL);
2182 g_return_val_if_fail (key != NULL, NULL);
2184 key_file_error = NULL;
2186 value = g_key_file_get_locale_string (key_file, group_name,
2191 g_propagate_error (error, key_file_error);
2200 len = strlen (value);
2201 if (value[len - 1] == key_file->list_separator)
2202 value[len - 1] = '\0';
2204 list_separator[0] = key_file->list_separator;
2205 list_separator[1] = '\0';
2206 values = g_strsplit (value, list_separator, 0);
2211 *length = g_strv_length (values);
2217 * g_key_file_set_locale_string_list:
2218 * @key_file: a #GKeyFile
2219 * @group_name: a group name
2221 * @locale: a locale identifier
2222 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2223 * @length: the length of @list
2225 * Associates a list of string values for @key and @locale under
2226 * @group_name. If the translation for @key cannot be found then
2232 g_key_file_set_locale_string_list (GKeyFile *key_file,
2233 const gchar *group_name,
2235 const gchar *locale,
2236 const gchar * const list[],
2239 GString *value_list;
2243 g_return_if_fail (key_file != NULL);
2244 g_return_if_fail (key != NULL);
2245 g_return_if_fail (locale != NULL);
2246 g_return_if_fail (length != 0);
2248 value_list = g_string_sized_new (length * 128);
2249 for (i = 0; i < length && list[i] != NULL; i++)
2253 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2254 g_string_append (value_list, value);
2255 g_string_append_c (value_list, key_file->list_separator);
2260 full_key = g_strdup_printf ("%s[%s]", key, locale);
2261 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2263 g_string_free (value_list, TRUE);
2267 * g_key_file_get_boolean:
2268 * @key_file: a #GKeyFile
2269 * @group_name: a group name
2271 * @error: return location for a #GError
2273 * Returns the value associated with @key under @group_name as a
2276 * If @key cannot be found then %FALSE is returned and @error is set
2277 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2278 * associated with @key cannot be interpreted as a boolean then %FALSE
2279 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2281 * Return value: the value associated with the key as a boolean,
2282 * or %FALSE if the key was not found or could not be parsed.
2287 g_key_file_get_boolean (GKeyFile *key_file,
2288 const gchar *group_name,
2292 GError *key_file_error = NULL;
2294 gboolean bool_value;
2296 g_return_val_if_fail (key_file != NULL, FALSE);
2297 g_return_val_if_fail (group_name != NULL, FALSE);
2298 g_return_val_if_fail (key != NULL, FALSE);
2300 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2304 g_propagate_error (error, key_file_error);
2308 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2314 if (g_error_matches (key_file_error,
2316 G_KEY_FILE_ERROR_INVALID_VALUE))
2318 g_set_error (error, G_KEY_FILE_ERROR,
2319 G_KEY_FILE_ERROR_INVALID_VALUE,
2320 _("Key file contains key '%s' "
2321 "which has a value that cannot be interpreted."),
2323 g_error_free (key_file_error);
2326 g_propagate_error (error, key_file_error);
2333 * g_key_file_set_boolean:
2334 * @key_file: a #GKeyFile
2335 * @group_name: a group name
2337 * @value: %TRUE or %FALSE
2339 * Associates a new boolean value with @key under @group_name.
2340 * If @key cannot be found then it is created.
2345 g_key_file_set_boolean (GKeyFile *key_file,
2346 const gchar *group_name,
2352 g_return_if_fail (key_file != NULL);
2354 result = g_key_file_parse_boolean_as_value (key_file, value);
2355 g_key_file_set_value (key_file, group_name, key, result);
2360 * g_key_file_get_boolean_list:
2361 * @key_file: a #GKeyFile
2362 * @group_name: a group name
2364 * @length: (out): the number of booleans returned
2365 * @error: return location for a #GError
2367 * Returns the values associated with @key under @group_name as
2370 * If @key cannot be found then %NULL is returned and @error is set to
2371 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2372 * with @key cannot be interpreted as booleans then %NULL is returned
2373 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2375 * Return value: (array length=length) (element-type gboolean) (transfer container):
2376 * the values associated with the key as a list of booleans, or %NULL if the
2377 * key was not found or could not be parsed. The returned list of booleans
2378 * should be freed with g_free() when no longer needed.
2383 g_key_file_get_boolean_list (GKeyFile *key_file,
2384 const gchar *group_name,
2389 GError *key_file_error;
2391 gboolean *bool_values;
2394 g_return_val_if_fail (key_file != NULL, NULL);
2395 g_return_val_if_fail (group_name != NULL, NULL);
2396 g_return_val_if_fail (key != NULL, NULL);
2401 key_file_error = NULL;
2403 values = g_key_file_get_string_list (key_file, group_name, key,
2404 &num_bools, &key_file_error);
2407 g_propagate_error (error, key_file_error);
2412 bool_values = g_new (gboolean, num_bools);
2414 for (i = 0; i < num_bools; i++)
2416 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2422 g_propagate_error (error, key_file_error);
2423 g_strfreev (values);
2424 g_free (bool_values);
2429 g_strfreev (values);
2432 *length = num_bools;
2438 * g_key_file_set_boolean_list:
2439 * @key_file: a #GKeyFile
2440 * @group_name: a group name
2442 * @list: (array length=length): an array of boolean values
2443 * @length: length of @list
2445 * Associates a list of boolean values with @key under @group_name.
2446 * If @key cannot be found then it is created.
2447 * If @group_name is %NULL, the start_group is used.
2452 g_key_file_set_boolean_list (GKeyFile *key_file,
2453 const gchar *group_name,
2458 GString *value_list;
2461 g_return_if_fail (key_file != NULL);
2462 g_return_if_fail (list != NULL);
2464 value_list = g_string_sized_new (length * 8);
2465 for (i = 0; i < length; i++)
2469 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2471 g_string_append (value_list, value);
2472 g_string_append_c (value_list, key_file->list_separator);
2477 g_key_file_set_value (key_file, group_name, key, value_list->str);
2478 g_string_free (value_list, TRUE);
2482 * g_key_file_get_integer:
2483 * @key_file: a #GKeyFile
2484 * @group_name: a group name
2486 * @error: return location for a #GError
2488 * Returns the value associated with @key under @group_name as an
2491 * If @key cannot be found then 0 is returned and @error is set to
2492 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2493 * with @key cannot be interpreted as an integer then 0 is returned
2494 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2496 * Return value: the value associated with the key as an integer, or
2497 * 0 if the key was not found or could not be parsed.
2502 g_key_file_get_integer (GKeyFile *key_file,
2503 const gchar *group_name,
2507 GError *key_file_error;
2511 g_return_val_if_fail (key_file != NULL, -1);
2512 g_return_val_if_fail (group_name != NULL, -1);
2513 g_return_val_if_fail (key != NULL, -1);
2515 key_file_error = NULL;
2517 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2521 g_propagate_error (error, key_file_error);
2525 int_value = g_key_file_parse_value_as_integer (key_file, value,
2531 if (g_error_matches (key_file_error,
2533 G_KEY_FILE_ERROR_INVALID_VALUE))
2535 g_set_error (error, G_KEY_FILE_ERROR,
2536 G_KEY_FILE_ERROR_INVALID_VALUE,
2537 _("Key file contains key '%s' in group '%s' "
2538 "which has a value that cannot be interpreted."),
2540 g_error_free (key_file_error);
2543 g_propagate_error (error, key_file_error);
2550 * g_key_file_set_integer:
2551 * @key_file: a #GKeyFile
2552 * @group_name: a group name
2554 * @value: an integer value
2556 * Associates a new integer value with @key under @group_name.
2557 * If @key cannot be found then it is created.
2562 g_key_file_set_integer (GKeyFile *key_file,
2563 const gchar *group_name,
2569 g_return_if_fail (key_file != NULL);
2571 result = g_key_file_parse_integer_as_value (key_file, value);
2572 g_key_file_set_value (key_file, group_name, key, result);
2577 * g_key_file_get_int64:
2578 * @key_file: a non-%NULL #GKeyFile
2579 * @group_name: a non-%NULL group name
2580 * @key: a non-%NULL key
2581 * @error: return location for a #GError
2583 * Returns the value associated with @key under @group_name as a signed
2584 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2585 * 64-bit results without truncation.
2587 * Returns: the value associated with the key as a signed 64-bit integer, or
2588 * 0 if the key was not found or could not be parsed.
2593 g_key_file_get_int64 (GKeyFile *key_file,
2594 const gchar *group_name,
2601 g_return_val_if_fail (key_file != NULL, -1);
2602 g_return_val_if_fail (group_name != NULL, -1);
2603 g_return_val_if_fail (key != NULL, -1);
2605 s = g_key_file_get_value (key_file, group_name, key, error);
2610 v = g_ascii_strtoll (s, &end, 10);
2612 if (*s == '\0' || *end != '\0')
2614 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2615 _("Key '%s' in group '%s' has value '%s' "
2616 "where %s was expected"),
2617 key, group_name, s, "int64");
2627 * g_key_file_set_int64:
2628 * @key_file: a #GKeyFile
2629 * @group_name: a group name
2631 * @value: an integer value
2633 * Associates a new integer value with @key under @group_name.
2634 * If @key cannot be found then it is created.
2639 g_key_file_set_int64 (GKeyFile *key_file,
2640 const gchar *group_name,
2646 g_return_if_fail (key_file != NULL);
2648 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2649 g_key_file_set_value (key_file, group_name, key, result);
2654 * g_key_file_get_uint64:
2655 * @key_file: a non-%NULL #GKeyFile
2656 * @group_name: a non-%NULL group name
2657 * @key: a non-%NULL key
2658 * @error: return location for a #GError
2660 * Returns the value associated with @key under @group_name as an unsigned
2661 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2662 * large positive results without truncation.
2664 * Returns: the value associated with the key as an unsigned 64-bit integer,
2665 * or 0 if the key was not found or could not be parsed.
2670 g_key_file_get_uint64 (GKeyFile *key_file,
2671 const gchar *group_name,
2678 g_return_val_if_fail (key_file != NULL, -1);
2679 g_return_val_if_fail (group_name != NULL, -1);
2680 g_return_val_if_fail (key != NULL, -1);
2682 s = g_key_file_get_value (key_file, group_name, key, error);
2687 v = g_ascii_strtoull (s, &end, 10);
2689 if (*s == '\0' || *end != '\0')
2691 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2692 _("Key '%s' in group '%s' has value '%s' "
2693 "where %s was expected"),
2694 key, group_name, s, "uint64");
2704 * g_key_file_set_uint64:
2705 * @key_file: a #GKeyFile
2706 * @group_name: a group name
2708 * @value: an integer value
2710 * Associates a new integer value with @key under @group_name.
2711 * If @key cannot be found then it is created.
2716 g_key_file_set_uint64 (GKeyFile *key_file,
2717 const gchar *group_name,
2723 g_return_if_fail (key_file != NULL);
2725 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2726 g_key_file_set_value (key_file, group_name, key, result);
2731 * g_key_file_get_integer_list:
2732 * @key_file: a #GKeyFile
2733 * @group_name: a group name
2735 * @length: (out): the number of integers returned
2736 * @error: return location for a #GError
2738 * Returns the values associated with @key under @group_name as
2741 * If @key cannot be found then %NULL is returned and @error is set to
2742 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2743 * with @key cannot be interpreted as integers then %NULL is returned
2744 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2746 * Return value: (array length=length) (element-type gint) (transfer container):
2747 * the values associated with the key as a list of integers, or %NULL if
2748 * the key was not found or could not be parsed. The returned list of
2749 * integers should be freed with g_free() when no longer needed.
2754 g_key_file_get_integer_list (GKeyFile *key_file,
2755 const gchar *group_name,
2760 GError *key_file_error = NULL;
2765 g_return_val_if_fail (key_file != NULL, NULL);
2766 g_return_val_if_fail (group_name != NULL, NULL);
2767 g_return_val_if_fail (key != NULL, NULL);
2772 values = g_key_file_get_string_list (key_file, group_name, key,
2773 &num_ints, &key_file_error);
2776 g_propagate_error (error, key_file_error);
2781 int_values = g_new (gint, num_ints);
2783 for (i = 0; i < num_ints; i++)
2785 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2791 g_propagate_error (error, key_file_error);
2792 g_strfreev (values);
2793 g_free (int_values);
2798 g_strfreev (values);
2807 * g_key_file_set_integer_list:
2808 * @key_file: a #GKeyFile
2809 * @group_name: a group name
2811 * @list: (array length=length): an array of integer values
2812 * @length: number of integer values in @list
2814 * Associates a list of integer values with @key under @group_name.
2815 * If @key cannot be found then it is created.
2820 g_key_file_set_integer_list (GKeyFile *key_file,
2821 const gchar *group_name,
2829 g_return_if_fail (key_file != NULL);
2830 g_return_if_fail (list != NULL);
2832 values = g_string_sized_new (length * 16);
2833 for (i = 0; i < length; i++)
2837 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2839 g_string_append (values, value);
2840 g_string_append_c (values, key_file->list_separator);
2845 g_key_file_set_value (key_file, group_name, key, values->str);
2846 g_string_free (values, TRUE);
2850 * g_key_file_get_double:
2851 * @key_file: a #GKeyFile
2852 * @group_name: a group name
2854 * @error: return location for a #GError
2856 * Returns the value associated with @key under @group_name as a
2857 * double. If @group_name is %NULL, the start_group is used.
2859 * If @key cannot be found then 0.0 is returned and @error is set to
2860 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2861 * with @key cannot be interpreted as a double then 0.0 is returned
2862 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2864 * Return value: the value associated with the key as a double, or
2865 * 0.0 if the key was not found or could not be parsed.
2870 g_key_file_get_double (GKeyFile *key_file,
2871 const gchar *group_name,
2875 GError *key_file_error;
2877 gdouble double_value;
2879 g_return_val_if_fail (key_file != NULL, -1);
2880 g_return_val_if_fail (group_name != NULL, -1);
2881 g_return_val_if_fail (key != NULL, -1);
2883 key_file_error = NULL;
2885 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2889 g_propagate_error (error, key_file_error);
2893 double_value = g_key_file_parse_value_as_double (key_file, value,
2899 if (g_error_matches (key_file_error,
2901 G_KEY_FILE_ERROR_INVALID_VALUE))
2903 g_set_error (error, G_KEY_FILE_ERROR,
2904 G_KEY_FILE_ERROR_INVALID_VALUE,
2905 _("Key file contains key '%s' in group '%s' "
2906 "which has a value that cannot be interpreted."),
2908 g_error_free (key_file_error);
2911 g_propagate_error (error, key_file_error);
2914 return double_value;
2918 * g_key_file_set_double:
2919 * @key_file: a #GKeyFile
2920 * @group_name: a group name
2922 * @value: an double value
2924 * Associates a new double value with @key under @group_name.
2925 * If @key cannot be found then it is created.
2930 g_key_file_set_double (GKeyFile *key_file,
2931 const gchar *group_name,
2935 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2937 g_return_if_fail (key_file != NULL);
2939 g_ascii_dtostr (result, sizeof (result), value);
2940 g_key_file_set_value (key_file, group_name, key, result);
2944 * g_key_file_get_double_list:
2945 * @key_file: a #GKeyFile
2946 * @group_name: a group name
2948 * @length: (out): the number of doubles returned
2949 * @error: return location for a #GError
2951 * Returns the values associated with @key under @group_name as
2954 * If @key cannot be found then %NULL is returned and @error is set to
2955 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2956 * with @key cannot be interpreted as doubles then %NULL is returned
2957 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2959 * Return value: (array length=length) (element-type gdouble) (transfer container):
2960 * the values associated with the key as a list of doubles, or %NULL if the
2961 * key was not found or could not be parsed. The returned list of doubles
2962 * should be freed with g_free() when no longer needed.
2967 g_key_file_get_double_list (GKeyFile *key_file,
2968 const gchar *group_name,
2973 GError *key_file_error = NULL;
2975 gdouble *double_values;
2976 gsize i, num_doubles;
2978 g_return_val_if_fail (key_file != NULL, NULL);
2979 g_return_val_if_fail (group_name != NULL, NULL);
2980 g_return_val_if_fail (key != NULL, NULL);
2985 values = g_key_file_get_string_list (key_file, group_name, key,
2986 &num_doubles, &key_file_error);
2989 g_propagate_error (error, key_file_error);
2994 double_values = g_new (gdouble, num_doubles);
2996 for (i = 0; i < num_doubles; i++)
2998 double_values[i] = g_key_file_parse_value_as_double (key_file,
3004 g_propagate_error (error, key_file_error);
3005 g_strfreev (values);
3006 g_free (double_values);
3011 g_strfreev (values);
3014 *length = num_doubles;
3016 return double_values;
3020 * g_key_file_set_double_list:
3021 * @key_file: a #GKeyFile
3022 * @group_name: a group name
3024 * @list: (array length=length): an array of double values
3025 * @length: number of double values in @list
3027 * Associates a list of double values with @key under
3028 * @group_name. If @key cannot be found then it is created.
3033 g_key_file_set_double_list (GKeyFile *key_file,
3034 const gchar *group_name,
3042 g_return_if_fail (key_file != NULL);
3043 g_return_if_fail (list != NULL);
3045 values = g_string_sized_new (length * 16);
3046 for (i = 0; i < length; i++)
3048 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3050 g_ascii_dtostr( result, sizeof (result), list[i] );
3052 g_string_append (values, result);
3053 g_string_append_c (values, key_file->list_separator);
3056 g_key_file_set_value (key_file, group_name, key, values->str);
3057 g_string_free (values, TRUE);
3061 g_key_file_set_key_comment (GKeyFile *key_file,
3062 const gchar *group_name,
3064 const gchar *comment,
3067 GKeyFileGroup *group;
3068 GKeyFileKeyValuePair *pair;
3069 GList *key_node, *comment_node, *tmp;
3071 group = g_key_file_lookup_group (key_file, group_name);
3074 g_set_error (error, G_KEY_FILE_ERROR,
3075 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3076 _("Key file does not have group '%s'"),
3077 group_name ? group_name : "(null)");
3082 /* First find the key the comments are supposed to be
3085 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3087 if (key_node == NULL)
3089 g_set_error (error, G_KEY_FILE_ERROR,
3090 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3091 _("Key file does not have key '%s' in group '%s'"),
3096 /* Then find all the comments already associated with the
3099 tmp = key_node->next;
3102 pair = (GKeyFileKeyValuePair *) tmp->data;
3104 if (pair->key != NULL)
3109 g_key_file_remove_key_value_pair_node (key_file, group,
3113 if (comment == NULL)
3116 /* Now we can add our new comment
3118 pair = g_slice_new (GKeyFileKeyValuePair);
3120 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3122 key_node = g_list_insert (key_node, pair, 1);
3128 g_key_file_set_group_comment (GKeyFile *key_file,
3129 const gchar *group_name,
3130 const gchar *comment,
3133 GKeyFileGroup *group;
3135 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3137 group = g_key_file_lookup_group (key_file, group_name);
3140 g_set_error (error, G_KEY_FILE_ERROR,
3141 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3142 _("Key file does not have group '%s'"),
3143 group_name ? group_name : "(null)");
3148 /* First remove any existing comment
3152 g_key_file_key_value_pair_free (group->comment);
3153 group->comment = NULL;
3156 if (comment == NULL)
3159 /* Now we can add our new comment
3161 group->comment = g_slice_new (GKeyFileKeyValuePair);
3162 group->comment->key = NULL;
3163 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3169 g_key_file_set_top_comment (GKeyFile *key_file,
3170 const gchar *comment,
3174 GKeyFileGroup *group;
3175 GKeyFileKeyValuePair *pair;
3177 /* The last group in the list should be the top (comments only)
3180 g_warn_if_fail (key_file->groups != NULL);
3181 group_node = g_list_last (key_file->groups);
3182 group = (GKeyFileGroup *) group_node->data;
3183 g_warn_if_fail (group->name == NULL);
3185 /* Note all keys must be comments at the top of
3186 * the file, so we can just free it all.
3188 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3189 group->key_value_pairs = NULL;
3191 if (comment == NULL)
3194 pair = g_slice_new (GKeyFileKeyValuePair);
3196 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3198 group->key_value_pairs =
3199 g_list_prepend (group->key_value_pairs, pair);
3205 * g_key_file_set_comment:
3206 * @key_file: a #GKeyFile
3207 * @group_name: (allow-none): a group name, or %NULL
3208 * @key: (allow-none): a key
3209 * @comment: a comment
3210 * @error: return location for a #GError
3212 * Places a comment above @key from @group_name.
3213 * If @key is %NULL then @comment will be written above @group_name.
3214 * If both @key and @group_name are %NULL, then @comment will be
3215 * written above the first group in the file.
3217 * Returns: %TRUE if the comment was written, %FALSE otherwise
3222 g_key_file_set_comment (GKeyFile *key_file,
3223 const gchar *group_name,
3225 const gchar *comment,
3228 g_return_val_if_fail (key_file != NULL, FALSE);
3230 if (group_name != NULL && key != NULL)
3232 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3235 else if (group_name != NULL)
3237 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3242 if (!g_key_file_set_top_comment (key_file, comment, error))
3250 g_key_file_get_key_comment (GKeyFile *key_file,
3251 const gchar *group_name,
3255 GKeyFileGroup *group;
3256 GKeyFileKeyValuePair *pair;
3257 GList *key_node, *tmp;
3261 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3263 group = g_key_file_lookup_group (key_file, group_name);
3266 g_set_error (error, G_KEY_FILE_ERROR,
3267 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3268 _("Key file does not have group '%s'"),
3269 group_name ? group_name : "(null)");
3274 /* First find the key the comments are supposed to be
3277 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3279 if (key_node == NULL)
3281 g_set_error (error, G_KEY_FILE_ERROR,
3282 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3283 _("Key file does not have key '%s' in group '%s'"),
3290 /* Then find all the comments already associated with the
3291 * key and concatentate them.
3293 tmp = key_node->next;
3294 if (!key_node->next)
3297 pair = (GKeyFileKeyValuePair *) tmp->data;
3298 if (pair->key != NULL)
3303 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3305 if (pair->key != NULL)
3311 while (tmp != key_node)
3313 pair = (GKeyFileKeyValuePair *) tmp->data;
3316 string = g_string_sized_new (512);
3318 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3319 g_string_append (string, comment);
3327 comment = string->str;
3328 g_string_free (string, FALSE);
3337 get_group_comment (GKeyFile *key_file,
3338 GKeyFileGroup *group,
3347 tmp = group->key_value_pairs;
3350 GKeyFileKeyValuePair *pair;
3352 pair = (GKeyFileKeyValuePair *) tmp->data;
3354 if (pair->key != NULL)
3360 if (tmp->next == NULL)
3368 GKeyFileKeyValuePair *pair;
3370 pair = (GKeyFileKeyValuePair *) tmp->data;
3373 string = g_string_sized_new (512);
3375 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3376 g_string_append (string, comment);
3383 return g_string_free (string, FALSE);
3389 g_key_file_get_group_comment (GKeyFile *key_file,
3390 const gchar *group_name,
3394 GKeyFileGroup *group;
3396 group = g_key_file_lookup_group (key_file, group_name);
3399 g_set_error (error, G_KEY_FILE_ERROR,
3400 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3401 _("Key file does not have group '%s'"),
3402 group_name ? group_name : "(null)");
3408 return g_strdup (group->comment->value);
3410 group_node = g_key_file_lookup_group_node (key_file, group_name);
3411 group_node = group_node->next;
3412 group = (GKeyFileGroup *)group_node->data;
3413 return get_group_comment (key_file, group, error);
3417 g_key_file_get_top_comment (GKeyFile *key_file,
3421 GKeyFileGroup *group;
3423 /* The last group in the list should be the top (comments only)
3426 g_warn_if_fail (key_file->groups != NULL);
3427 group_node = g_list_last (key_file->groups);
3428 group = (GKeyFileGroup *) group_node->data;
3429 g_warn_if_fail (group->name == NULL);
3431 return get_group_comment (key_file, group, error);
3435 * g_key_file_get_comment:
3436 * @key_file: a #GKeyFile
3437 * @group_name: (allow-none): a group name, or %NULL
3439 * @error: return location for a #GError
3441 * Retrieves a comment above @key from @group_name.
3442 * If @key is %NULL then @comment will be read from above
3443 * @group_name. If both @key and @group_name are %NULL, then
3444 * @comment will be read from above the first group in the file.
3446 * Returns: a comment that should be freed with g_free()
3451 g_key_file_get_comment (GKeyFile *key_file,
3452 const gchar *group_name,
3456 g_return_val_if_fail (key_file != NULL, NULL);
3458 if (group_name != NULL && key != NULL)
3459 return g_key_file_get_key_comment (key_file, group_name, key, error);
3460 else if (group_name != NULL)
3461 return g_key_file_get_group_comment (key_file, group_name, error);
3463 return g_key_file_get_top_comment (key_file, error);
3467 * g_key_file_remove_comment:
3468 * @key_file: a #GKeyFile
3469 * @group_name: (allow-none): a group name, or %NULL
3470 * @key: (allow-none): a key
3471 * @error: return location for a #GError
3473 * Removes a comment above @key from @group_name.
3474 * If @key is %NULL then @comment will be removed above @group_name.
3475 * If both @key and @group_name are %NULL, then @comment will
3476 * be removed above the first group in the file.
3478 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3484 g_key_file_remove_comment (GKeyFile *key_file,
3485 const gchar *group_name,
3489 g_return_val_if_fail (key_file != NULL, FALSE);
3491 if (group_name != NULL && key != NULL)
3492 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3493 else if (group_name != NULL)
3494 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3496 return g_key_file_set_top_comment (key_file, NULL, error);
3500 * g_key_file_has_group:
3501 * @key_file: a #GKeyFile
3502 * @group_name: a group name
3504 * Looks whether the key file has the group @group_name.
3506 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3511 g_key_file_has_group (GKeyFile *key_file,
3512 const gchar *group_name)
3514 g_return_val_if_fail (key_file != NULL, FALSE);
3515 g_return_val_if_fail (group_name != NULL, FALSE);
3517 return g_key_file_lookup_group (key_file, group_name) != NULL;
3520 /* This code remains from a historical attempt to add a new public API
3521 * which respects the GError rules.
3524 g_key_file_has_key_full (GKeyFile *key_file,
3525 const gchar *group_name,
3530 GKeyFileKeyValuePair *pair;
3531 GKeyFileGroup *group;
3533 g_return_val_if_fail (key_file != NULL, FALSE);
3534 g_return_val_if_fail (group_name != NULL, FALSE);
3535 g_return_val_if_fail (key != NULL, FALSE);
3537 group = g_key_file_lookup_group (key_file, group_name);
3541 g_set_error (error, G_KEY_FILE_ERROR,
3542 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3543 _("Key file does not have group '%s'"),
3544 group_name ? group_name : "(null)");
3549 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3552 *has_key = pair != NULL;
3557 * g_key_file_has_key: (skip)
3558 * @key_file: a #GKeyFile
3559 * @group_name: a group name
3561 * @error: return location for a #GError
3563 * Looks whether the key file has the key @key in the group
3566 * Note that this function does not follow the rules for #GError strictly;
3567 * the return value both carries meaning and signals an error. To use
3568 * this function, you must pass a #GError pointer in @error, and check
3569 * whether it is not %NULL to see if an error occurred.
3571 * Language bindings should use g_key_file_get_value() to test whether
3572 * or not a key exists.
3574 * Return value: %TRUE if @key is a part of @group_name, %FALSE otherwise
3579 g_key_file_has_key (GKeyFile *key_file,
3580 const gchar *group_name,
3584 GError *temp_error = NULL;
3587 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3593 g_propagate_error (error, temp_error);
3599 g_key_file_add_group (GKeyFile *key_file,
3600 const gchar *group_name)
3602 GKeyFileGroup *group;
3604 g_return_if_fail (key_file != NULL);
3605 g_return_if_fail (g_key_file_is_group_name (group_name));
3607 group = g_key_file_lookup_group (key_file, group_name);
3610 key_file->current_group = group;
3614 group = g_slice_new0 (GKeyFileGroup);
3615 group->name = g_strdup (group_name);
3616 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3617 key_file->groups = g_list_prepend (key_file->groups, group);
3618 key_file->current_group = group;
3620 if (key_file->start_group == NULL)
3621 key_file->start_group = group;
3623 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3627 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3632 g_free (pair->value);
3633 g_slice_free (GKeyFileKeyValuePair, pair);
3637 /* Be careful not to call this function on a node with data in the
3638 * lookup map without removing it from the lookup map, first.
3640 * Some current cases where this warning is not a concern are
3642 * - the node being removed is a comment node
3643 * - the entire lookup map is getting destroyed soon after
3647 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3648 GKeyFileGroup *group,
3652 GKeyFileKeyValuePair *pair;
3654 pair = (GKeyFileKeyValuePair *) pair_node->data;
3656 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3658 g_warn_if_fail (pair->value != NULL);
3660 g_key_file_key_value_pair_free (pair);
3662 g_list_free_1 (pair_node);
3666 g_key_file_remove_group_node (GKeyFile *key_file,
3669 GKeyFileGroup *group;
3672 group = (GKeyFileGroup *) group_node->data;
3675 g_hash_table_remove (key_file->group_hash, group->name);
3677 /* If the current group gets deleted make the current group the last
3680 if (key_file->current_group == group)
3682 /* groups should always contain at least the top comment group,
3683 * unless g_key_file_clear has been called
3685 if (key_file->groups)
3686 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3688 key_file->current_group = NULL;
3691 /* If the start group gets deleted make the start group the first
3694 if (key_file->start_group == group)
3696 tmp = g_list_last (key_file->groups);
3699 if (tmp != group_node &&
3700 ((GKeyFileGroup *) tmp->data)->name != NULL)
3707 key_file->start_group = (GKeyFileGroup *) tmp->data;
3709 key_file->start_group = NULL;
3712 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3714 tmp = group->key_value_pairs;
3721 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3724 g_warn_if_fail (group->key_value_pairs == NULL);
3728 g_key_file_key_value_pair_free (group->comment);
3729 group->comment = NULL;
3732 if (group->lookup_map)
3734 g_hash_table_destroy (group->lookup_map);
3735 group->lookup_map = NULL;
3738 g_free ((gchar *) group->name);
3739 g_slice_free (GKeyFileGroup, group);
3740 g_list_free_1 (group_node);
3744 * g_key_file_remove_group:
3745 * @key_file: a #GKeyFile
3746 * @group_name: a group name
3747 * @error: return location for a #GError or %NULL
3749 * Removes the specified group, @group_name,
3750 * from the key file.
3752 * Returns: %TRUE if the group was removed, %FALSE otherwise
3757 g_key_file_remove_group (GKeyFile *key_file,
3758 const gchar *group_name,
3763 g_return_val_if_fail (key_file != NULL, FALSE);
3764 g_return_val_if_fail (group_name != NULL, FALSE);
3766 group_node = g_key_file_lookup_group_node (key_file, group_name);
3770 g_set_error (error, G_KEY_FILE_ERROR,
3771 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3772 _("Key file does not have group '%s'"),
3777 g_key_file_remove_group_node (key_file, group_node);
3783 g_key_file_add_key_value_pair (GKeyFile *key_file,
3784 GKeyFileGroup *group,
3785 GKeyFileKeyValuePair *pair)
3787 g_hash_table_replace (group->lookup_map, pair->key, pair);
3788 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3792 g_key_file_add_key (GKeyFile *key_file,
3793 GKeyFileGroup *group,
3797 GKeyFileKeyValuePair *pair;
3799 pair = g_slice_new (GKeyFileKeyValuePair);
3800 pair->key = g_strdup (key);
3801 pair->value = g_strdup (value);
3803 g_key_file_add_key_value_pair (key_file, group, pair);
3807 * g_key_file_remove_key:
3808 * @key_file: a #GKeyFile
3809 * @group_name: a group name
3810 * @key: a key name to remove
3811 * @error: return location for a #GError or %NULL
3813 * Removes @key in @group_name from the key file.
3815 * Returns: %TRUE if the key was removed, %FALSE otherwise
3820 g_key_file_remove_key (GKeyFile *key_file,
3821 const gchar *group_name,
3825 GKeyFileGroup *group;
3826 GKeyFileKeyValuePair *pair;
3828 g_return_val_if_fail (key_file != NULL, FALSE);
3829 g_return_val_if_fail (group_name != NULL, FALSE);
3830 g_return_val_if_fail (key != NULL, FALSE);
3834 group = g_key_file_lookup_group (key_file, group_name);
3837 g_set_error (error, G_KEY_FILE_ERROR,
3838 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3839 _("Key file does not have group '%s'"),
3840 group_name ? group_name : "(null)");
3844 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3848 g_set_error (error, G_KEY_FILE_ERROR,
3849 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3850 _("Key file does not have key '%s' in group '%s'"),
3855 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3856 g_hash_table_remove (group->lookup_map, pair->key);
3857 g_key_file_key_value_pair_free (pair);
3863 g_key_file_lookup_group_node (GKeyFile *key_file,
3864 const gchar *group_name)
3866 GKeyFileGroup *group;
3869 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3871 group = (GKeyFileGroup *) tmp->data;
3873 if (group && group->name && strcmp (group->name, group_name) == 0)
3880 static GKeyFileGroup *
3881 g_key_file_lookup_group (GKeyFile *key_file,
3882 const gchar *group_name)
3884 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3888 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3889 GKeyFileGroup *group,
3894 for (key_node = group->key_value_pairs;
3896 key_node = key_node->next)
3898 GKeyFileKeyValuePair *pair;
3900 pair = (GKeyFileKeyValuePair *) key_node->data;
3902 if (pair->key && strcmp (pair->key, key) == 0)
3909 static GKeyFileKeyValuePair *
3910 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3911 GKeyFileGroup *group,
3914 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3917 /* Lines starting with # or consisting entirely of whitespace are merely
3918 * recorded, not parsed. This function assumes all leading whitespace
3919 * has been stripped.
3922 g_key_file_line_is_comment (const gchar *line)
3924 return (*line == '#' || *line == '\0' || *line == '\n');
3928 g_key_file_is_group_name (const gchar *name)
3935 p = q = (gchar *) name;
3936 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3937 q = g_utf8_find_next_char (q, NULL);
3939 if (*q != '\0' || q == p)
3946 g_key_file_is_key_name (const gchar *name)
3953 p = q = (gchar *) name;
3954 /* We accept a little more than the desktop entry spec says,
3955 * since gnome-vfs uses mime-types as keys in its cache.
3957 while (*q && *q != '=' && *q != '[' && *q != ']')
3958 q = g_utf8_find_next_char (q, NULL);
3960 /* No empty keys, please */
3964 /* We accept spaces in the middle of keys to not break
3965 * existing apps, but we don't tolerate initial or final
3966 * spaces, which would lead to silent corruption when
3967 * rereading the file.
3969 if (*p == ' ' || q[-1] == ' ')
3975 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3976 q = g_utf8_find_next_char (q, NULL);
3990 /* A group in a key file is made up of a starting '[' followed by one
3991 * or more letters making up the group name followed by ']'.
3994 g_key_file_line_is_group (const gchar *line)
4004 while (*p && *p != ']')
4005 p = g_utf8_find_next_char (p, NULL);
4010 /* silently accept whitespace after the ] */
4011 p = g_utf8_find_next_char (p, NULL);
4012 while (*p == ' ' || *p == '\t')
4013 p = g_utf8_find_next_char (p, NULL);
4022 g_key_file_line_is_key_value_pair (const gchar *line)
4026 p = (gchar *) g_utf8_strchr (line, -1, '=');
4031 /* Key must be non-empty
4040 g_key_file_parse_value_as_string (GKeyFile *key_file,
4045 gchar *string_value, *p, *q0, *q;
4047 string_value = g_new (gchar, strlen (value) + 1);
4049 p = (gchar *) value;
4050 q0 = q = string_value;
4080 g_set_error_literal (error, G_KEY_FILE_ERROR,
4081 G_KEY_FILE_ERROR_INVALID_VALUE,
4082 _("Key file contains escape character "
4087 if (pieces && *p == key_file->list_separator)
4088 *q = key_file->list_separator;
4102 g_set_error (error, G_KEY_FILE_ERROR,
4103 G_KEY_FILE_ERROR_INVALID_VALUE,
4104 _("Key file contains invalid escape "
4105 "sequence '%s'"), sequence);
4114 if (pieces && (*p == key_file->list_separator))
4116 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4132 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4133 *pieces = g_slist_reverse (*pieces);
4136 return string_value;
4140 g_key_file_parse_string_as_value (GKeyFile *key_file,
4141 const gchar *string,
4142 gboolean escape_separator)
4144 gchar *value, *p, *q;
4146 gboolean parsing_leading_space;
4148 length = strlen (string) + 1;
4150 /* Worst case would be that every character needs to be escaped.
4151 * In other words every character turns to two characters
4153 value = g_new (gchar, 2 * length);
4155 p = (gchar *) string;
4157 parsing_leading_space = TRUE;
4158 while (p < (string + length - 1))
4160 gchar escaped_character[3] = { '\\', 0, 0 };
4165 if (parsing_leading_space)
4167 escaped_character[1] = 's';
4168 strcpy (q, escaped_character);
4178 if (parsing_leading_space)
4180 escaped_character[1] = 't';
4181 strcpy (q, escaped_character);
4191 escaped_character[1] = 'n';
4192 strcpy (q, escaped_character);
4196 escaped_character[1] = 'r';
4197 strcpy (q, escaped_character);
4201 escaped_character[1] = '\\';
4202 strcpy (q, escaped_character);
4204 parsing_leading_space = FALSE;
4207 if (escape_separator && *p == key_file->list_separator)
4209 escaped_character[1] = key_file->list_separator;
4210 strcpy (q, escaped_character);
4212 parsing_leading_space = TRUE;
4218 parsing_leading_space = FALSE;
4230 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4239 long_value = strtol (value, &eof_int, 10);
4241 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4243 gchar *value_utf8 = _g_utf8_make_valid (value);
4244 g_set_error (error, G_KEY_FILE_ERROR,
4245 G_KEY_FILE_ERROR_INVALID_VALUE,
4246 _("Value '%s' cannot be interpreted "
4247 "as a number."), value_utf8);
4248 g_free (value_utf8);
4253 int_value = long_value;
4254 if (int_value != long_value || errno == ERANGE)
4256 gchar *value_utf8 = _g_utf8_make_valid (value);
4259 G_KEY_FILE_ERROR_INVALID_VALUE,
4260 _("Integer value '%s' out of range"),
4262 g_free (value_utf8);
4271 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4275 return g_strdup_printf ("%d", value);
4279 g_key_file_parse_value_as_double (GKeyFile *key_file,
4283 gchar *end_of_valid_d;
4284 gdouble double_value = 0;
4286 double_value = g_ascii_strtod (value, &end_of_valid_d);
4288 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4290 gchar *value_utf8 = _g_utf8_make_valid (value);
4291 g_set_error (error, G_KEY_FILE_ERROR,
4292 G_KEY_FILE_ERROR_INVALID_VALUE,
4293 _("Value '%s' cannot be interpreted "
4294 "as a float number."),
4296 g_free (value_utf8);
4299 return double_value;
4303 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4309 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4311 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4314 value_utf8 = _g_utf8_make_valid (value);
4315 g_set_error (error, G_KEY_FILE_ERROR,
4316 G_KEY_FILE_ERROR_INVALID_VALUE,
4317 _("Value '%s' cannot be interpreted "
4318 "as a boolean."), value_utf8);
4319 g_free (value_utf8);
4325 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4329 return g_strdup ("true");
4331 return g_strdup ("false");
4335 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4342 string = g_string_sized_new (512);
4344 lines = g_strsplit (value, "\n", 0);
4346 for (i = 0; lines[i] != NULL; i++)
4348 if (lines[i][0] != '#')
4349 g_string_append_printf (string, "%s\n", lines[i]);
4351 g_string_append_printf (string, "%s\n", lines[i] + 1);
4355 return g_string_free (string, FALSE);
4359 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4360 const gchar *comment)
4366 string = g_string_sized_new (512);
4368 lines = g_strsplit (comment, "\n", 0);
4370 for (i = 0; lines[i] != NULL; i++)
4371 g_string_append_printf (string, "#%s%s", lines[i],
4372 lines[i + 1] == NULL? "" : "\n");
4375 return g_string_free (string, FALSE);
4379 * g_key_file_save_to_file:
4380 * @key_file: a #GKeyFile
4381 * @filename: the name of the file to write to
4382 * @error: a pointer to a %NULL #GError, or %NULL
4384 * Writes the contents of @key_file to @filename using
4385 * g_file_set_contents().
4387 * This function can fail for any of the reasons that
4388 * g_file_set_contents() may fail.
4390 * Returns: %TRUE if successful, else %FALSE with @error set
4395 g_key_file_save_to_file (GKeyFile *key_file,
4396 const gchar *filename,
4403 g_return_val_if_fail (key_file != NULL, FALSE);
4404 g_return_val_if_fail (filename != NULL, FALSE);
4405 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4407 contents = g_key_file_to_data (key_file, &length, NULL);
4408 g_assert (contents != NULL);
4410 success = g_file_set_contents (filename, contents, length, error);