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>
46 #define fstat(a,b) _fstati64(a,b)
51 #define S_ISREG(mode) ((mode)&_S_IFREG)
54 #endif /* G_OS_WIN23 */
59 #include "gfileutils.h"
65 #include "gmessages.h"
68 #include "gstrfuncs.h"
74 * @title: Key-value file parser
75 * @short_description: parses .ini-like config files
77 * #GKeyFile lets you parse, edit or create files containing groups of
78 * key-value pairs, which we call <firstterm>key files</firstterm> for
79 * lack of a better name. Several freedesktop.org specifications use
80 * key files now, e.g the
81 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
82 * Entry Specification</ulink> and the
83 * <ulink url="http://freedesktop.org/Standards/icon-theme-spec">Icon
84 * Theme Specification</ulink>.
86 * The syntax of key files is described in detail in the
87 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
88 * Entry Specification</ulink>, here is a quick summary: Key files
89 * consists of groups of key-value pairs, interspersed with comments.
92 * # this is just an example
93 * # there can be comments before the first group
97 * Name=Key File Example\tthis value shows\nescaping
99 * # localized strings are stored in multiple key-value pairs
102 * Welcome[fr_FR]=Bonjour
104 * Welcome[be@latin]=Hello
108 * Numbers=2;20;-200;0
110 * Booleans=true;false;true;true
113 * Lines beginning with a '#' and blank lines are considered comments.
115 * Groups are started by a header line containing the group name enclosed
116 * in '[' and ']', and ended implicitly by the start of the next group or
117 * the end of the file. Each key-value pair must be contained in a group.
119 * Key-value pairs generally have the form <literal>key=value</literal>,
120 * with the exception of localized strings, which have the form
121 * <literal>key[locale]=value</literal>, with a locale identifier of the
122 * form <literal>lang_COUNTRY@MODIFIER</literal> where
123 * <literal>COUNTRY</literal> and <literal>MODIFIER</literal> are optional.
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 * <listitem>.ini files use the ';' character to begin comments,
138 * key files use the '#' character.</listitem>
139 * <listitem>Key files do not allow for ungrouped keys meaning only
140 * comments can precede the first group.</listitem>
141 * <listitem>Key files are always encoded in UTF-8.</listitem>
142 * <listitem>Key and Group names are case-sensitive. For example, a
143 * group called <literal>[GROUP]</literal> is a different from
144 * <literal>[group]</literal>.</listitem>
145 * <listitem>.ini files don't have a strongly typed boolean entry type,
146 * they only have GetProfileInt(). In key files, only
147 * <literal>true</literal> and <literal>false</literal> (in lower case)
148 * are allowed.</listitem>
151 * Note that in contrast to the
152 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
153 * Entry Specification</ulink>, groups in key files may contain the same
154 * key multiple times; the last entry wins. Key files may also contain
155 * multiple groups with the same name; they are merged together.
156 * Another difference is that keys and group names in key files are not
157 * restricted to ASCII characters.
163 * Error domain for key file parsing. Errors in this domain will
164 * be from the #GKeyFileError enumeration.
166 * See #GError for information on error domains.
171 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
172 * an unknown encoding
173 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
174 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
175 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
176 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
177 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
179 * Error codes returned by key file parsing.
184 * @G_KEY_FILE_NONE: No flags, default behaviour
185 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
186 * (possibly modified) contents of the key file back to a file;
187 * otherwise all comments will be lost when the key file is
189 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
190 * (possibly modified) contents of the key file back to a file;
191 * otherwise only the translations for the current language will be
194 * Flags which influence the parsing.
198 * G_KEY_FILE_DESKTOP_GROUP:
200 * The name of the main group of a desktop entry file, as defined in the
201 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
202 * Entry Specification</ulink>. Consult the specification for more
203 * details about the meanings of the keys below.
209 * G_KEY_FILE_DESKTOP_KEY_TYPE:
211 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
212 * giving the type of the desktop entry. Usually
213 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
214 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
215 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
221 * G_KEY_FILE_DESKTOP_KEY_VERSION:
223 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
224 * giving the version of the Desktop Entry Specification used for
225 * the desktop entry file.
231 * G_KEY_FILE_DESKTOP_KEY_NAME:
233 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
234 * string giving the specific name of the desktop entry.
240 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
242 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
243 * string giving the generic name of the desktop entry.
249 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
251 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
252 * stating whether the desktop entry should be shown in menus.
258 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
260 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
261 * string giving the tooltip for the desktop entry.
267 * G_KEY_FILE_DESKTOP_KEY_ICON:
269 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
270 * string giving the name of the icon to be displayed for the desktop
277 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
279 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
280 * stating whether the desktop entry has been deleted by the user.
286 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
288 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
289 * strings identifying the environments that should display the
296 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
298 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
299 * strings identifying the environments that should not display the
306 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
308 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
309 * giving the file name of a binary on disk used to determine if the
310 * program is actually installed. It is only valid for desktop entries
311 * with the <literal>Application</literal> type.
317 * G_KEY_FILE_DESKTOP_KEY_EXEC:
319 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
320 * giving the command line to execute. It is only valid for desktop
321 * entries with the <literal>Application</literal> type.
327 * G_KEY_FILE_DESKTOP_KEY_PATH:
329 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
330 * containing the working directory to run the program in. It is only
331 * valid for desktop entries with the <literal>Application</literal> type.
337 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
339 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
340 * stating whether the program should be run in a terminal window.
341 * It is only valid for desktop entries with the
342 * <literal>Application</literal> type.
348 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
350 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
351 * of strings giving the MIME types supported by this desktop entry.
357 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
359 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
360 * of strings giving the categories in which the desktop entry
361 * should be shown in a menu.
367 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
369 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
370 * stating whether the application supports the <ulink
371 * url="http://www.freedesktop.org/Standards/startup-notification-spec">Startup
372 * Notification Protocol Specification</ulink>.
378 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
380 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
381 * identifying the WM class or name hint of a window that the application
382 * will create, which can be used to emulate Startup Notification with
383 * older applications.
389 * G_KEY_FILE_DESKTOP_KEY_URL :
391 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
392 * giving the URL to access. It is only valid for desktop entries
393 * with the <literal>Link</literal> type.
399 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
401 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
402 * entries representing applications.
408 * G_KEY_FILE_DESKTOP_TYPE_LINK:
410 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
411 * entries representing links to documents.
417 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
419 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
420 * entries representing directories.
425 typedef struct _GKeyFileGroup GKeyFileGroup;
430 * The GKeyFile struct contains only private data
431 * and should not be accessed directly.
436 GHashTable *group_hash;
438 GKeyFileGroup *start_group;
439 GKeyFileGroup *current_group;
441 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
443 gchar list_separator;
449 volatile gint ref_count;
452 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
454 struct _GKeyFileGroup
456 const gchar *name; /* NULL for above first group (which will be comments) */
458 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
460 GList *key_value_pairs;
462 /* Used in parallel with key_value_pairs for
463 * increased lookup performance
465 GHashTable *lookup_map;
468 struct _GKeyFileKeyValuePair
470 gchar *key; /* NULL for comments */
474 static gint find_file_in_data_dirs (const gchar *file,
475 const gchar **data_dirs,
478 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
482 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
483 const gchar *group_name);
484 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
485 const gchar *group_name);
487 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
488 GKeyFileGroup *group,
490 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
491 GKeyFileGroup *group,
494 static void g_key_file_remove_group_node (GKeyFile *key_file,
496 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
497 GKeyFileGroup *group,
500 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
501 GKeyFileGroup *group,
502 GKeyFileKeyValuePair *pair);
503 static void g_key_file_add_key (GKeyFile *key_file,
504 GKeyFileGroup *group,
507 static void g_key_file_add_group (GKeyFile *key_file,
508 const gchar *group_name);
509 static gboolean g_key_file_is_group_name (const gchar *name);
510 static gboolean g_key_file_is_key_name (const gchar *name);
511 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
512 static gboolean g_key_file_line_is_comment (const gchar *line);
513 static gboolean g_key_file_line_is_group (const gchar *line);
514 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
515 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
519 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
521 gboolean escape_separator);
522 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
525 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
527 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
530 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
533 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
535 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
537 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
538 const gchar *comment);
539 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
543 static void g_key_file_parse_comment (GKeyFile *key_file,
547 static void g_key_file_parse_group (GKeyFile *key_file,
551 static gchar *key_get_locale (const gchar *key);
552 static void g_key_file_parse_data (GKeyFile *key_file,
556 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
559 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
562 g_key_file_init (GKeyFile *key_file)
564 key_file->current_group = g_slice_new0 (GKeyFileGroup);
565 key_file->groups = g_list_prepend (NULL, key_file->current_group);
566 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
567 key_file->start_group = NULL;
568 key_file->parse_buffer = g_string_sized_new (128);
569 key_file->list_separator = ';';
571 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
575 g_key_file_clear (GKeyFile *key_file)
577 GList *tmp, *group_node;
579 if (key_file->locales)
581 g_strfreev (key_file->locales);
582 key_file->locales = NULL;
585 if (key_file->parse_buffer)
587 g_string_free (key_file->parse_buffer, TRUE);
588 key_file->parse_buffer = NULL;
591 tmp = key_file->groups;
596 g_key_file_remove_group_node (key_file, group_node);
599 if (key_file->group_hash != NULL)
601 g_hash_table_destroy (key_file->group_hash);
602 key_file->group_hash = NULL;
605 g_warn_if_fail (key_file->groups == NULL);
612 * Creates a new empty #GKeyFile object. Use
613 * g_key_file_load_from_file(), g_key_file_load_from_data(),
614 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
615 * read an existing key file.
617 * Return value: (transfer full): an empty #GKeyFile.
622 g_key_file_new (void)
626 key_file = g_slice_new0 (GKeyFile);
627 key_file->ref_count = 1;
628 g_key_file_init (key_file);
634 * g_key_file_set_list_separator:
635 * @key_file: a #GKeyFile
636 * @separator: the separator
638 * Sets the character which is used to separate
639 * values in lists. Typically ';' or ',' are used
640 * as separators. The default list separator is ';'.
645 g_key_file_set_list_separator (GKeyFile *key_file,
648 g_return_if_fail (key_file != NULL);
650 key_file->list_separator = separator;
654 /* Iterates through all the directories in *dirs trying to
655 * open file. When it successfully locates and opens a file it
656 * returns the file descriptor to the open file. It also
657 * outputs the absolute path of the file in output_file.
660 find_file_in_data_dirs (const gchar *file,
665 const gchar **data_dirs, *data_dir;
677 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
679 gchar *candidate_file, *sub_dir;
681 candidate_file = (gchar *) file;
682 sub_dir = g_strdup ("");
683 while (candidate_file != NULL && fd == -1)
687 path = g_build_filename (data_dir, sub_dir,
688 candidate_file, NULL);
690 fd = g_open (path, O_RDONLY, 0);
698 candidate_file = strchr (candidate_file, '-');
700 if (candidate_file == NULL)
706 sub_dir = g_strndup (file, candidate_file - file - 1);
708 for (p = sub_dir; *p != '\0'; p++)
711 *p = G_DIR_SEPARATOR;
720 g_set_error_literal (error, G_KEY_FILE_ERROR,
721 G_KEY_FILE_ERROR_NOT_FOUND,
722 _("Valid key file could not be "
723 "found in search dirs"));
726 if (output_file != NULL && fd > 0)
727 *output_file = g_strdup (path);
735 g_key_file_load_from_fd (GKeyFile *key_file,
740 GError *key_file_error = NULL;
742 struct stat stat_buf;
743 gchar read_buf[4096];
744 gchar list_separator;
746 if (fstat (fd, &stat_buf) < 0)
748 g_set_error_literal (error, G_FILE_ERROR,
749 g_file_error_from_errno (errno),
754 if (!S_ISREG (stat_buf.st_mode))
756 g_set_error_literal (error, G_KEY_FILE_ERROR,
757 G_KEY_FILE_ERROR_PARSE,
758 _("Not a regular file"));
762 list_separator = key_file->list_separator;
763 g_key_file_clear (key_file);
764 g_key_file_init (key_file);
765 key_file->list_separator = list_separator;
766 key_file->flags = flags;
770 bytes_read = read (fd, read_buf, 4096);
772 if (bytes_read == 0) /* End of File */
777 if (errno == EINTR || errno == EAGAIN)
780 g_set_error_literal (error, G_FILE_ERROR,
781 g_file_error_from_errno (errno),
786 g_key_file_parse_data (key_file,
787 read_buf, bytes_read,
790 while (!key_file_error);
794 g_propagate_error (error, key_file_error);
798 g_key_file_flush_parse_buffer (key_file, &key_file_error);
802 g_propagate_error (error, key_file_error);
810 * g_key_file_load_from_file:
811 * @key_file: an empty #GKeyFile struct
812 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
813 * @flags: flags from #GKeyFileFlags
814 * @error: return location for a #GError, or %NULL
816 * Loads a key file into an empty #GKeyFile structure.
817 * If the file could not be loaded then @error is set to
818 * either a #GFileError or #GKeyFileError.
820 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
825 g_key_file_load_from_file (GKeyFile *key_file,
830 GError *key_file_error = NULL;
833 g_return_val_if_fail (key_file != NULL, FALSE);
834 g_return_val_if_fail (file != NULL, FALSE);
836 fd = g_open (file, O_RDONLY, 0);
840 g_set_error_literal (error, G_FILE_ERROR,
841 g_file_error_from_errno (errno),
846 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
851 g_propagate_error (error, key_file_error);
859 * g_key_file_load_from_data:
860 * @key_file: an empty #GKeyFile struct
861 * @data: key file loaded in memory
862 * @length: the length of @data in bytes (or -1 if data is nul-terminated)
863 * @flags: flags from #GKeyFileFlags
864 * @error: return location for a #GError, or %NULL
866 * Loads a key file from memory into an empty #GKeyFile structure.
867 * If the object cannot be created then %error is set to a #GKeyFileError.
869 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
874 g_key_file_load_from_data (GKeyFile *key_file,
880 GError *key_file_error = NULL;
881 gchar list_separator;
883 g_return_val_if_fail (key_file != NULL, FALSE);
884 g_return_val_if_fail (data != NULL || length == 0, FALSE);
886 if (length == (gsize)-1)
887 length = strlen (data);
889 list_separator = key_file->list_separator;
890 g_key_file_clear (key_file);
891 g_key_file_init (key_file);
892 key_file->list_separator = list_separator;
893 key_file->flags = flags;
895 g_key_file_parse_data (key_file, data, length, &key_file_error);
899 g_propagate_error (error, key_file_error);
903 g_key_file_flush_parse_buffer (key_file, &key_file_error);
907 g_propagate_error (error, key_file_error);
915 * g_key_file_load_from_dirs:
916 * @key_file: an empty #GKeyFile struct
917 * @file: (type filename): a relative path to a filename to open and parse
918 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
919 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
920 * of the file, or %NULL
921 * @flags: flags from #GKeyFileFlags
922 * @error: return location for a #GError, or %NULL
924 * This function looks for a key file named @file in the paths
925 * specified in @search_dirs, loads the file into @key_file and
926 * returns the file's full path in @full_path. If the file could not
927 * be loaded then an %error is set to either a #GFileError or
930 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
935 g_key_file_load_from_dirs (GKeyFile *key_file,
937 const gchar **search_dirs,
942 GError *key_file_error = NULL;
943 const gchar **data_dirs;
948 g_return_val_if_fail (key_file != NULL, FALSE);
949 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
950 g_return_val_if_fail (search_dirs != NULL, FALSE);
953 data_dirs = search_dirs;
955 while (*data_dirs != NULL && !found_file)
957 g_free (output_path);
959 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
965 g_propagate_error (error, key_file_error);
969 found_file = g_key_file_load_from_fd (key_file, fd, flags,
975 g_propagate_error (error, key_file_error);
980 if (found_file && full_path)
981 *full_path = output_path;
983 g_free (output_path);
989 * g_key_file_load_from_data_dirs:
990 * @key_file: an empty #GKeyFile struct
991 * @file: (type filename): a relative path to a filename to open and parse
992 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
993 * of the file, or %NULL
994 * @flags: flags from #GKeyFileFlags
995 * @error: return location for a #GError, or %NULL
997 * This function looks for a key file named @file in the paths
998 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
999 * loads the file into @key_file and returns the file's full path in
1000 * @full_path. If the file could not be loaded then an %error is
1001 * set to either a #GFileError or #GKeyFileError.
1003 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1007 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1010 GKeyFileFlags flags,
1013 gchar **all_data_dirs;
1014 const gchar * user_data_dir;
1015 const gchar * const * system_data_dirs;
1017 gboolean found_file;
1019 g_return_val_if_fail (key_file != NULL, FALSE);
1020 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1022 user_data_dir = g_get_user_data_dir ();
1023 system_data_dirs = g_get_system_data_dirs ();
1024 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1027 all_data_dirs[i++] = g_strdup (user_data_dir);
1030 while (system_data_dirs[j] != NULL)
1031 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1032 all_data_dirs[i] = NULL;
1034 found_file = g_key_file_load_from_dirs (key_file,
1036 (const gchar **)all_data_dirs,
1041 g_strfreev (all_data_dirs);
1047 * g_key_file_ref: (skip)
1048 * @key_file: a #GKeyFile
1050 * Increases the reference count of @key_file.
1052 * Returns: the same @key_file.
1057 g_key_file_ref (GKeyFile *key_file)
1059 g_return_val_if_fail (key_file != NULL, NULL);
1061 g_atomic_int_inc (&key_file->ref_count);
1067 * g_key_file_free: (skip)
1068 * @key_file: a #GKeyFile
1070 * Clears all keys and groups from @key_file, and decreases the
1071 * reference count by 1. If the reference count reaches zero,
1072 * frees the key file and all its allocated memory.
1077 g_key_file_free (GKeyFile *key_file)
1079 g_return_if_fail (key_file != NULL);
1081 g_key_file_clear (key_file);
1082 g_key_file_unref (key_file);
1087 * @key_file: a #GKeyFile
1089 * Decreases the reference count of @key_file by 1. If the reference count
1090 * reaches zero, frees the key file and all its allocated memory.
1095 g_key_file_unref (GKeyFile *key_file)
1097 g_return_if_fail (key_file != NULL);
1099 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1101 g_key_file_clear (key_file);
1102 g_slice_free (GKeyFile, key_file);
1106 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1107 * true for locales that match those in g_get_language_names().
1110 g_key_file_locale_is_interesting (GKeyFile *key_file,
1111 const gchar *locale)
1115 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1118 for (i = 0; key_file->locales[i] != NULL; i++)
1120 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1128 g_key_file_parse_line (GKeyFile *key_file,
1133 GError *parse_error = NULL;
1136 g_return_if_fail (key_file != NULL);
1137 g_return_if_fail (line != NULL);
1139 line_start = (gchar *) line;
1140 while (g_ascii_isspace (*line_start))
1143 if (g_key_file_line_is_comment (line_start))
1144 g_key_file_parse_comment (key_file, line, length, &parse_error);
1145 else if (g_key_file_line_is_group (line_start))
1146 g_key_file_parse_group (key_file, line_start,
1147 length - (line_start - line),
1149 else if (g_key_file_line_is_key_value_pair (line_start))
1150 g_key_file_parse_key_value_pair (key_file, line_start,
1151 length - (line_start - line),
1155 gchar *line_utf8 = _g_utf8_make_valid (line);
1156 g_set_error (error, G_KEY_FILE_ERROR,
1157 G_KEY_FILE_ERROR_PARSE,
1158 _("Key file contains line '%s' which is not "
1159 "a key-value pair, group, or comment"),
1167 g_propagate_error (error, parse_error);
1171 g_key_file_parse_comment (GKeyFile *key_file,
1176 GKeyFileKeyValuePair *pair;
1178 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1181 g_warn_if_fail (key_file->current_group != NULL);
1183 pair = g_slice_new (GKeyFileKeyValuePair);
1185 pair->value = g_strndup (line, length);
1187 key_file->current_group->key_value_pairs =
1188 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1192 g_key_file_parse_group (GKeyFile *key_file,
1198 const gchar *group_name_start, *group_name_end;
1200 /* advance past opening '['
1202 group_name_start = line + 1;
1203 group_name_end = line + length - 1;
1205 while (*group_name_end != ']')
1208 group_name = g_strndup (group_name_start,
1209 group_name_end - group_name_start);
1211 if (!g_key_file_is_group_name (group_name))
1213 g_set_error (error, G_KEY_FILE_ERROR,
1214 G_KEY_FILE_ERROR_PARSE,
1215 _("Invalid group name: %s"), group_name);
1216 g_free (group_name);
1220 g_key_file_add_group (key_file, group_name);
1221 g_free (group_name);
1225 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1230 gchar *key, *value, *key_end, *value_start, *locale;
1231 gsize key_len, value_len;
1233 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1235 g_set_error_literal (error, G_KEY_FILE_ERROR,
1236 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1237 _("Key file does not start with a group"));
1241 key_end = value_start = strchr (line, '=');
1243 g_warn_if_fail (key_end != NULL);
1248 /* Pull the key name from the line (chomping trailing whitespace)
1250 while (g_ascii_isspace (*key_end))
1253 key_len = key_end - line + 2;
1255 g_warn_if_fail (key_len <= length);
1257 key = g_strndup (line, key_len - 1);
1259 if (!g_key_file_is_key_name (key))
1261 g_set_error (error, G_KEY_FILE_ERROR,
1262 G_KEY_FILE_ERROR_PARSE,
1263 _("Invalid key name: %s"), key);
1268 /* Pull the value from the line (chugging leading whitespace)
1270 while (g_ascii_isspace (*value_start))
1273 value_len = line + length - value_start + 1;
1275 value = g_strndup (value_start, value_len);
1277 g_warn_if_fail (key_file->start_group != NULL);
1279 if (key_file->current_group
1280 && key_file->current_group->name
1281 && strcmp (key_file->start_group->name,
1282 key_file->current_group->name) == 0
1283 && strcmp (key, "Encoding") == 0)
1285 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1287 gchar *value_utf8 = _g_utf8_make_valid (value);
1288 g_set_error (error, G_KEY_FILE_ERROR,
1289 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1290 _("Key file contains unsupported "
1291 "encoding '%s'"), value_utf8);
1292 g_free (value_utf8);
1300 /* Is this key a translation? If so, is it one that we care about?
1302 locale = key_get_locale (key);
1304 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1306 GKeyFileKeyValuePair *pair;
1308 pair = g_slice_new (GKeyFileKeyValuePair);
1310 pair->value = value;
1312 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1324 key_get_locale (const gchar *key)
1328 locale = g_strrstr (key, "[");
1330 if (locale && strlen (locale) <= 2)
1334 locale = g_strndup (locale + 1, strlen (locale) - 2);
1340 g_key_file_parse_data (GKeyFile *key_file,
1345 GError *parse_error;
1348 g_return_if_fail (key_file != NULL);
1349 g_return_if_fail (data != NULL || length == 0);
1356 if (data[i] == '\n')
1358 if (key_file->parse_buffer->len > 0
1359 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1361 g_string_erase (key_file->parse_buffer,
1362 key_file->parse_buffer->len - 1,
1365 /* When a newline is encountered flush the parse buffer so that the
1366 * line can be parsed. Note that completely blank lines won't show
1367 * up in the parse buffer, so they get parsed directly.
1369 if (key_file->parse_buffer->len > 0)
1370 g_key_file_flush_parse_buffer (key_file, &parse_error);
1372 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1376 g_propagate_error (error, parse_error);
1383 const gchar *start_of_line;
1384 const gchar *end_of_line;
1387 start_of_line = data + i;
1388 end_of_line = memchr (start_of_line, '\n', length - i);
1390 if (end_of_line == NULL)
1391 end_of_line = data + length;
1393 line_length = end_of_line - start_of_line;
1395 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1402 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1405 GError *file_error = NULL;
1407 g_return_if_fail (key_file != NULL);
1411 if (key_file->parse_buffer->len > 0)
1413 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1414 key_file->parse_buffer->len,
1416 g_string_erase (key_file->parse_buffer, 0, -1);
1420 g_propagate_error (error, file_error);
1427 * g_key_file_to_data:
1428 * @key_file: a #GKeyFile
1429 * @length: (out) (allow-none): return location for the length of the
1430 * returned string, or %NULL
1431 * @error: return location for a #GError, or %NULL
1433 * This function outputs @key_file as a string.
1435 * Note that this function never reports an error,
1436 * so it is safe to pass %NULL as @error.
1438 * Return value: a newly allocated string holding
1439 * the contents of the #GKeyFile
1444 g_key_file_to_data (GKeyFile *key_file,
1448 GString *data_string;
1449 GList *group_node, *key_file_node;
1451 g_return_val_if_fail (key_file != NULL, NULL);
1453 data_string = g_string_new (NULL);
1455 for (group_node = g_list_last (key_file->groups);
1457 group_node = group_node->prev)
1459 GKeyFileGroup *group;
1461 group = (GKeyFileGroup *) group_node->data;
1463 /* separate groups by at least an empty line */
1464 if (data_string->len >= 2 &&
1465 data_string->str[data_string->len - 2] != '\n')
1466 g_string_append_c (data_string, '\n');
1468 if (group->comment != NULL)
1469 g_string_append_printf (data_string, "%s\n", group->comment->value);
1471 if (group->name != NULL)
1472 g_string_append_printf (data_string, "[%s]\n", group->name);
1474 for (key_file_node = g_list_last (group->key_value_pairs);
1475 key_file_node != NULL;
1476 key_file_node = key_file_node->prev)
1478 GKeyFileKeyValuePair *pair;
1480 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1482 if (pair->key != NULL)
1483 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1485 g_string_append_printf (data_string, "%s\n", pair->value);
1490 *length = data_string->len;
1492 return g_string_free (data_string, FALSE);
1496 * g_key_file_get_keys:
1497 * @key_file: a #GKeyFile
1498 * @group_name: a group name
1499 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1500 * @error: return location for a #GError, or %NULL
1502 * Returns all keys for the group name @group_name. The array of
1503 * returned keys will be %NULL-terminated, so @length may
1504 * optionally be %NULL. In the event that the @group_name cannot
1505 * be found, %NULL is returned and @error is set to
1506 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1508 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1509 * Use g_strfreev() to free it.
1514 g_key_file_get_keys (GKeyFile *key_file,
1515 const gchar *group_name,
1519 GKeyFileGroup *group;
1524 g_return_val_if_fail (key_file != NULL, NULL);
1525 g_return_val_if_fail (group_name != NULL, NULL);
1527 group = g_key_file_lookup_group (key_file, group_name);
1531 g_set_error (error, G_KEY_FILE_ERROR,
1532 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1533 _("Key file does not have group '%s'"),
1534 group_name ? group_name : "(null)");
1539 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1541 GKeyFileKeyValuePair *pair;
1543 pair = (GKeyFileKeyValuePair *) tmp->data;
1549 keys = g_new (gchar *, num_keys + 1);
1552 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1554 GKeyFileKeyValuePair *pair;
1556 pair = (GKeyFileKeyValuePair *) tmp->data;
1560 keys[i] = g_strdup (pair->key);
1565 keys[num_keys] = NULL;
1574 * g_key_file_get_start_group:
1575 * @key_file: a #GKeyFile
1577 * Returns the name of the start group of the file.
1579 * Return value: The start group of the key file.
1584 g_key_file_get_start_group (GKeyFile *key_file)
1586 g_return_val_if_fail (key_file != NULL, NULL);
1588 if (key_file->start_group)
1589 return g_strdup (key_file->start_group->name);
1595 * g_key_file_get_groups:
1596 * @key_file: a #GKeyFile
1597 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1599 * Returns all groups in the key file loaded with @key_file.
1600 * The array of returned groups will be %NULL-terminated, so
1601 * @length may optionally be %NULL.
1603 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1604 * Use g_strfreev() to free it.
1608 g_key_file_get_groups (GKeyFile *key_file,
1613 gsize i, num_groups;
1615 g_return_val_if_fail (key_file != NULL, NULL);
1617 num_groups = g_list_length (key_file->groups);
1619 g_return_val_if_fail (num_groups > 0, NULL);
1621 group_node = g_list_last (key_file->groups);
1623 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1625 /* Only need num_groups instead of num_groups + 1
1626 * because the first group of the file (last in the
1627 * list) is always the comment group at the top,
1630 groups = g_new (gchar *, num_groups);
1634 for (group_node = group_node->prev;
1636 group_node = group_node->prev)
1638 GKeyFileGroup *group;
1640 group = (GKeyFileGroup *) group_node->data;
1642 g_warn_if_fail (group->name != NULL);
1644 groups[i++] = g_strdup (group->name);
1655 * g_key_file_get_value:
1656 * @key_file: a #GKeyFile
1657 * @group_name: a group name
1659 * @error: return location for a #GError, or %NULL
1661 * Returns the raw value associated with @key under @group_name.
1662 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1664 * In the event the key cannot be found, %NULL is returned and
1665 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1666 * event that the @group_name cannot be found, %NULL is returned
1667 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1670 * Return value: a newly allocated string or %NULL if the specified
1671 * key cannot be found.
1676 g_key_file_get_value (GKeyFile *key_file,
1677 const gchar *group_name,
1681 GKeyFileGroup *group;
1682 GKeyFileKeyValuePair *pair;
1683 gchar *value = NULL;
1685 g_return_val_if_fail (key_file != NULL, NULL);
1686 g_return_val_if_fail (group_name != NULL, NULL);
1687 g_return_val_if_fail (key != NULL, NULL);
1689 group = g_key_file_lookup_group (key_file, group_name);
1693 g_set_error (error, G_KEY_FILE_ERROR,
1694 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1695 _("Key file does not have group '%s'"),
1696 group_name ? group_name : "(null)");
1700 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1703 value = g_strdup (pair->value);
1705 g_set_error (error, G_KEY_FILE_ERROR,
1706 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1707 _("Key file does not have key '%s'"), key);
1713 * g_key_file_set_value:
1714 * @key_file: a #GKeyFile
1715 * @group_name: a group name
1719 * Associates a new value with @key under @group_name.
1721 * If @key cannot be found then it is created. If @group_name cannot
1722 * be found then it is created. To set an UTF-8 string which may contain
1723 * characters that need escaping (such as newlines or spaces), use
1724 * g_key_file_set_string().
1729 g_key_file_set_value (GKeyFile *key_file,
1730 const gchar *group_name,
1734 GKeyFileGroup *group;
1735 GKeyFileKeyValuePair *pair;
1737 g_return_if_fail (key_file != NULL);
1738 g_return_if_fail (g_key_file_is_group_name (group_name));
1739 g_return_if_fail (g_key_file_is_key_name (key));
1740 g_return_if_fail (value != NULL);
1742 group = g_key_file_lookup_group (key_file, group_name);
1746 g_key_file_add_group (key_file, group_name);
1747 group = (GKeyFileGroup *) key_file->groups->data;
1749 g_key_file_add_key (key_file, group, key, value);
1753 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1756 g_key_file_add_key (key_file, group, key, value);
1759 g_free (pair->value);
1760 pair->value = g_strdup (value);
1766 * g_key_file_get_string:
1767 * @key_file: a #GKeyFile
1768 * @group_name: a group name
1770 * @error: return location for a #GError, or %NULL
1772 * Returns the string value associated with @key under @group_name.
1773 * Unlike g_key_file_get_value(), this function handles escape sequences
1776 * In the event the key cannot be found, %NULL is returned and
1777 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1778 * event that the @group_name cannot be found, %NULL is returned
1779 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1781 * Return value: a newly allocated string or %NULL if the specified
1782 * key cannot be found.
1787 g_key_file_get_string (GKeyFile *key_file,
1788 const gchar *group_name,
1792 gchar *value, *string_value;
1793 GError *key_file_error;
1795 g_return_val_if_fail (key_file != NULL, NULL);
1796 g_return_val_if_fail (group_name != NULL, NULL);
1797 g_return_val_if_fail (key != NULL, NULL);
1799 key_file_error = NULL;
1801 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1805 g_propagate_error (error, key_file_error);
1809 if (!g_utf8_validate (value, -1, NULL))
1811 gchar *value_utf8 = _g_utf8_make_valid (value);
1812 g_set_error (error, G_KEY_FILE_ERROR,
1813 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1814 _("Key file contains key '%s' with value '%s' "
1815 "which is not UTF-8"), key, value_utf8);
1816 g_free (value_utf8);
1822 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1828 if (g_error_matches (key_file_error,
1830 G_KEY_FILE_ERROR_INVALID_VALUE))
1832 g_set_error (error, G_KEY_FILE_ERROR,
1833 G_KEY_FILE_ERROR_INVALID_VALUE,
1834 _("Key file contains key '%s' "
1835 "which has a value that cannot be interpreted."),
1837 g_error_free (key_file_error);
1840 g_propagate_error (error, key_file_error);
1843 return string_value;
1847 * g_key_file_set_string:
1848 * @key_file: a #GKeyFile
1849 * @group_name: a group name
1853 * Associates a new string value with @key under @group_name.
1854 * If @key cannot be found then it is created.
1855 * If @group_name cannot be found then it is created.
1856 * Unlike g_key_file_set_value(), this function handles characters
1857 * that need escaping, such as newlines.
1862 g_key_file_set_string (GKeyFile *key_file,
1863 const gchar *group_name,
1865 const gchar *string)
1869 g_return_if_fail (key_file != NULL);
1870 g_return_if_fail (string != NULL);
1872 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1873 g_key_file_set_value (key_file, group_name, key, value);
1878 * g_key_file_get_string_list:
1879 * @key_file: a #GKeyFile
1880 * @group_name: a group name
1882 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1883 * @error: return location for a #GError, or %NULL
1885 * Returns the values associated with @key under @group_name.
1887 * In the event the key cannot be found, %NULL is returned and
1888 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1889 * event that the @group_name cannot be found, %NULL is returned
1890 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1892 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1893 * a %NULL-terminated string array or %NULL if the specified
1894 * key cannot be found. The array should be freed with g_strfreev().
1899 g_key_file_get_string_list (GKeyFile *key_file,
1900 const gchar *group_name,
1905 GError *key_file_error = NULL;
1906 gchar *value, *string_value, **values;
1908 GSList *p, *pieces = NULL;
1910 g_return_val_if_fail (key_file != NULL, NULL);
1911 g_return_val_if_fail (group_name != NULL, NULL);
1912 g_return_val_if_fail (key != NULL, NULL);
1917 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1921 g_propagate_error (error, key_file_error);
1925 if (!g_utf8_validate (value, -1, NULL))
1927 gchar *value_utf8 = _g_utf8_make_valid (value);
1928 g_set_error (error, G_KEY_FILE_ERROR,
1929 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1930 _("Key file contains key '%s' with value '%s' "
1931 "which is not UTF-8"), key, value_utf8);
1932 g_free (value_utf8);
1938 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1940 g_free (string_value);
1944 if (g_error_matches (key_file_error,
1946 G_KEY_FILE_ERROR_INVALID_VALUE))
1948 g_set_error (error, G_KEY_FILE_ERROR,
1949 G_KEY_FILE_ERROR_INVALID_VALUE,
1950 _("Key file contains key '%s' "
1951 "which has a value that cannot be interpreted."),
1953 g_error_free (key_file_error);
1956 g_propagate_error (error, key_file_error);
1958 g_slist_free_full (pieces, g_free);
1962 len = g_slist_length (pieces);
1963 values = g_new (gchar *, len + 1);
1964 for (p = pieces, i = 0; p; p = p->next)
1965 values[i++] = p->data;
1968 g_slist_free (pieces);
1977 * g_key_file_set_string_list:
1978 * @key_file: a #GKeyFile
1979 * @group_name: a group name
1981 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1982 * @length: number of string values in @list
1984 * Associates a list of string values for @key under @group_name.
1985 * If @key cannot be found then it is created.
1986 * If @group_name cannot be found then it is created.
1991 g_key_file_set_string_list (GKeyFile *key_file,
1992 const gchar *group_name,
1994 const gchar * const list[],
1997 GString *value_list;
2000 g_return_if_fail (key_file != NULL);
2001 g_return_if_fail (list != NULL || length == 0);
2003 value_list = g_string_sized_new (length * 128);
2004 for (i = 0; i < length && list[i] != NULL; i++)
2008 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2009 g_string_append (value_list, value);
2010 g_string_append_c (value_list, key_file->list_separator);
2015 g_key_file_set_value (key_file, group_name, key, value_list->str);
2016 g_string_free (value_list, TRUE);
2020 * g_key_file_set_locale_string:
2021 * @key_file: a #GKeyFile
2022 * @group_name: a group name
2024 * @locale: a locale identifier
2027 * Associates a string value for @key and @locale under @group_name.
2028 * If the translation for @key cannot be found then it is created.
2033 g_key_file_set_locale_string (GKeyFile *key_file,
2034 const gchar *group_name,
2036 const gchar *locale,
2037 const gchar *string)
2039 gchar *full_key, *value;
2041 g_return_if_fail (key_file != NULL);
2042 g_return_if_fail (key != NULL);
2043 g_return_if_fail (locale != NULL);
2044 g_return_if_fail (string != NULL);
2046 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2047 full_key = g_strdup_printf ("%s[%s]", key, locale);
2048 g_key_file_set_value (key_file, group_name, full_key, value);
2054 * g_key_file_get_locale_string:
2055 * @key_file: a #GKeyFile
2056 * @group_name: a group name
2058 * @locale: (allow-none): a locale identifier or %NULL
2059 * @error: return location for a #GError, or %NULL
2061 * Returns the value associated with @key under @group_name
2062 * translated in the given @locale if available. If @locale is
2063 * %NULL then the current locale is assumed.
2065 * If @key cannot be found then %NULL is returned and @error is set
2066 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2067 * with @key cannot be interpreted or no suitable translation can
2068 * be found then the untranslated value is returned.
2070 * Return value: a newly allocated string or %NULL if the specified
2071 * key cannot be found.
2076 g_key_file_get_locale_string (GKeyFile *key_file,
2077 const gchar *group_name,
2079 const gchar *locale,
2082 gchar *candidate_key, *translated_value;
2083 GError *key_file_error;
2085 gboolean free_languages = FALSE;
2088 g_return_val_if_fail (key_file != NULL, NULL);
2089 g_return_val_if_fail (group_name != NULL, NULL);
2090 g_return_val_if_fail (key != NULL, NULL);
2092 candidate_key = NULL;
2093 translated_value = NULL;
2094 key_file_error = NULL;
2098 languages = g_get_locale_variants (locale);
2099 free_languages = TRUE;
2103 languages = (gchar **) g_get_language_names ();
2104 free_languages = FALSE;
2107 for (i = 0; languages[i]; i++)
2109 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2111 translated_value = g_key_file_get_string (key_file,
2113 candidate_key, NULL);
2114 g_free (candidate_key);
2116 if (translated_value)
2119 g_free (translated_value);
2120 translated_value = NULL;
2123 /* Fallback to untranslated key
2125 if (!translated_value)
2127 translated_value = g_key_file_get_string (key_file, group_name, key,
2130 if (!translated_value)
2131 g_propagate_error (error, key_file_error);
2135 g_strfreev (languages);
2137 return translated_value;
2141 * g_key_file_get_locale_string_list:
2142 * @key_file: a #GKeyFile
2143 * @group_name: a group name
2145 * @locale: (allow-none): a locale identifier or %NULL
2146 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2147 * @error: return location for a #GError or %NULL
2149 * Returns the values associated with @key under @group_name
2150 * translated in the given @locale if available. If @locale is
2151 * %NULL then the current locale is assumed.
2153 * If @key cannot be found then %NULL is returned and @error is set
2154 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2155 * with @key cannot be interpreted or no suitable translations
2156 * can be found then the untranslated values are returned. The
2157 * returned array is %NULL-terminated, so @length may optionally
2160 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2161 * or %NULL if the key isn't found. The string array should be freed
2162 * with g_strfreev().
2167 g_key_file_get_locale_string_list (GKeyFile *key_file,
2168 const gchar *group_name,
2170 const gchar *locale,
2174 GError *key_file_error;
2175 gchar **values, *value;
2176 char list_separator[2];
2179 g_return_val_if_fail (key_file != NULL, NULL);
2180 g_return_val_if_fail (group_name != NULL, NULL);
2181 g_return_val_if_fail (key != NULL, NULL);
2183 key_file_error = NULL;
2185 value = g_key_file_get_locale_string (key_file, group_name,
2190 g_propagate_error (error, key_file_error);
2199 len = strlen (value);
2200 if (value[len - 1] == key_file->list_separator)
2201 value[len - 1] = '\0';
2203 list_separator[0] = key_file->list_separator;
2204 list_separator[1] = '\0';
2205 values = g_strsplit (value, list_separator, 0);
2210 *length = g_strv_length (values);
2216 * g_key_file_set_locale_string_list:
2217 * @key_file: a #GKeyFile
2218 * @group_name: a group name
2220 * @locale: a locale identifier
2221 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2222 * @length: the length of @list
2224 * Associates a list of string values for @key and @locale under
2225 * @group_name. If the translation for @key cannot be found then
2231 g_key_file_set_locale_string_list (GKeyFile *key_file,
2232 const gchar *group_name,
2234 const gchar *locale,
2235 const gchar * const list[],
2238 GString *value_list;
2242 g_return_if_fail (key_file != NULL);
2243 g_return_if_fail (key != NULL);
2244 g_return_if_fail (locale != NULL);
2245 g_return_if_fail (length != 0);
2247 value_list = g_string_sized_new (length * 128);
2248 for (i = 0; i < length && list[i] != NULL; i++)
2252 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2253 g_string_append (value_list, value);
2254 g_string_append_c (value_list, key_file->list_separator);
2259 full_key = g_strdup_printf ("%s[%s]", key, locale);
2260 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2262 g_string_free (value_list, TRUE);
2266 * g_key_file_get_boolean:
2267 * @key_file: a #GKeyFile
2268 * @group_name: a group name
2270 * @error: return location for a #GError
2272 * Returns the value associated with @key under @group_name as a
2275 * If @key cannot be found then %FALSE is returned and @error is set
2276 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2277 * associated with @key cannot be interpreted as a boolean then %FALSE
2278 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2280 * Return value: the value associated with the key as a boolean,
2281 * or %FALSE if the key was not found or could not be parsed.
2286 g_key_file_get_boolean (GKeyFile *key_file,
2287 const gchar *group_name,
2291 GError *key_file_error = NULL;
2293 gboolean bool_value;
2295 g_return_val_if_fail (key_file != NULL, FALSE);
2296 g_return_val_if_fail (group_name != NULL, FALSE);
2297 g_return_val_if_fail (key != NULL, FALSE);
2299 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2303 g_propagate_error (error, key_file_error);
2307 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2313 if (g_error_matches (key_file_error,
2315 G_KEY_FILE_ERROR_INVALID_VALUE))
2317 g_set_error (error, G_KEY_FILE_ERROR,
2318 G_KEY_FILE_ERROR_INVALID_VALUE,
2319 _("Key file contains key '%s' "
2320 "which has a value that cannot be interpreted."),
2322 g_error_free (key_file_error);
2325 g_propagate_error (error, key_file_error);
2332 * g_key_file_set_boolean:
2333 * @key_file: a #GKeyFile
2334 * @group_name: a group name
2336 * @value: %TRUE or %FALSE
2338 * Associates a new boolean value with @key under @group_name.
2339 * If @key cannot be found then it is created.
2344 g_key_file_set_boolean (GKeyFile *key_file,
2345 const gchar *group_name,
2351 g_return_if_fail (key_file != NULL);
2353 result = g_key_file_parse_boolean_as_value (key_file, value);
2354 g_key_file_set_value (key_file, group_name, key, result);
2359 * g_key_file_get_boolean_list:
2360 * @key_file: a #GKeyFile
2361 * @group_name: a group name
2363 * @length: (out): the number of booleans returned
2364 * @error: return location for a #GError
2366 * Returns the values associated with @key under @group_name as
2369 * If @key cannot be found then %NULL is returned and @error is set to
2370 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2371 * with @key cannot be interpreted as booleans then %NULL is returned
2372 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2374 * Return value: (array length=length) (element-type gboolean) (transfer container):
2375 * the values associated with the key as a list of booleans, or %NULL if the
2376 * key was not found or could not be parsed. The returned list of booleans
2377 * should be freed with g_free() when no longer needed.
2382 g_key_file_get_boolean_list (GKeyFile *key_file,
2383 const gchar *group_name,
2388 GError *key_file_error;
2390 gboolean *bool_values;
2393 g_return_val_if_fail (key_file != NULL, NULL);
2394 g_return_val_if_fail (group_name != NULL, NULL);
2395 g_return_val_if_fail (key != NULL, NULL);
2400 key_file_error = NULL;
2402 values = g_key_file_get_string_list (key_file, group_name, key,
2403 &num_bools, &key_file_error);
2406 g_propagate_error (error, key_file_error);
2411 bool_values = g_new (gboolean, num_bools);
2413 for (i = 0; i < num_bools; i++)
2415 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2421 g_propagate_error (error, key_file_error);
2422 g_strfreev (values);
2423 g_free (bool_values);
2428 g_strfreev (values);
2431 *length = num_bools;
2437 * g_key_file_set_boolean_list:
2438 * @key_file: a #GKeyFile
2439 * @group_name: a group name
2441 * @list: (array length=length): an array of boolean values
2442 * @length: length of @list
2444 * Associates a list of boolean values with @key under @group_name.
2445 * If @key cannot be found then it is created.
2446 * If @group_name is %NULL, the start_group is used.
2451 g_key_file_set_boolean_list (GKeyFile *key_file,
2452 const gchar *group_name,
2457 GString *value_list;
2460 g_return_if_fail (key_file != NULL);
2461 g_return_if_fail (list != NULL);
2463 value_list = g_string_sized_new (length * 8);
2464 for (i = 0; i < length; i++)
2468 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2470 g_string_append (value_list, value);
2471 g_string_append_c (value_list, key_file->list_separator);
2476 g_key_file_set_value (key_file, group_name, key, value_list->str);
2477 g_string_free (value_list, TRUE);
2481 * g_key_file_get_integer:
2482 * @key_file: a #GKeyFile
2483 * @group_name: a group name
2485 * @error: return location for a #GError
2487 * Returns the value associated with @key under @group_name as an
2490 * If @key cannot be found then 0 is returned and @error is set to
2491 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2492 * with @key cannot be interpreted as an integer then 0 is returned
2493 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2495 * Return value: the value associated with the key as an integer, or
2496 * 0 if the key was not found or could not be parsed.
2501 g_key_file_get_integer (GKeyFile *key_file,
2502 const gchar *group_name,
2506 GError *key_file_error;
2510 g_return_val_if_fail (key_file != NULL, -1);
2511 g_return_val_if_fail (group_name != NULL, -1);
2512 g_return_val_if_fail (key != NULL, -1);
2514 key_file_error = NULL;
2516 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2520 g_propagate_error (error, key_file_error);
2524 int_value = g_key_file_parse_value_as_integer (key_file, value,
2530 if (g_error_matches (key_file_error,
2532 G_KEY_FILE_ERROR_INVALID_VALUE))
2534 g_set_error (error, G_KEY_FILE_ERROR,
2535 G_KEY_FILE_ERROR_INVALID_VALUE,
2536 _("Key file contains key '%s' in group '%s' "
2537 "which has a value that cannot be interpreted."),
2539 g_error_free (key_file_error);
2542 g_propagate_error (error, key_file_error);
2549 * g_key_file_set_integer:
2550 * @key_file: a #GKeyFile
2551 * @group_name: a group name
2553 * @value: an integer value
2555 * Associates a new integer value with @key under @group_name.
2556 * If @key cannot be found then it is created.
2561 g_key_file_set_integer (GKeyFile *key_file,
2562 const gchar *group_name,
2568 g_return_if_fail (key_file != NULL);
2570 result = g_key_file_parse_integer_as_value (key_file, value);
2571 g_key_file_set_value (key_file, group_name, key, result);
2576 * g_key_file_get_int64:
2577 * @key_file: a non-%NULL #GKeyFile
2578 * @group_name: a non-%NULL group name
2579 * @key: a non-%NULL key
2580 * @error: return location for a #GError
2582 * Returns the value associated with @key under @group_name as a signed
2583 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2584 * 64-bit results without truncation.
2586 * Returns: the value associated with the key as a signed 64-bit integer, or
2587 * 0 if the key was not found or could not be parsed.
2592 g_key_file_get_int64 (GKeyFile *key_file,
2593 const gchar *group_name,
2600 g_return_val_if_fail (key_file != NULL, -1);
2601 g_return_val_if_fail (group_name != NULL, -1);
2602 g_return_val_if_fail (key != NULL, -1);
2604 s = g_key_file_get_value (key_file, group_name, key, error);
2609 v = g_ascii_strtoll (s, &end, 10);
2611 if (*s == '\0' || *end != '\0')
2613 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2614 _("Key '%s' in group '%s' has value '%s' "
2615 "where %s was expected"),
2616 key, group_name, s, "int64");
2626 * g_key_file_set_int64:
2627 * @key_file: a #GKeyFile
2628 * @group_name: a group name
2630 * @value: an integer value
2632 * Associates a new integer value with @key under @group_name.
2633 * If @key cannot be found then it is created.
2638 g_key_file_set_int64 (GKeyFile *key_file,
2639 const gchar *group_name,
2645 g_return_if_fail (key_file != NULL);
2647 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2648 g_key_file_set_value (key_file, group_name, key, result);
2653 * g_key_file_get_uint64:
2654 * @key_file: a non-%NULL #GKeyFile
2655 * @group_name: a non-%NULL group name
2656 * @key: a non-%NULL key
2657 * @error: return location for a #GError
2659 * Returns the value associated with @key under @group_name as an unsigned
2660 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2661 * large positive results without truncation.
2663 * Returns: the value associated with the key as an unsigned 64-bit integer,
2664 * or 0 if the key was not found or could not be parsed.
2669 g_key_file_get_uint64 (GKeyFile *key_file,
2670 const gchar *group_name,
2677 g_return_val_if_fail (key_file != NULL, -1);
2678 g_return_val_if_fail (group_name != NULL, -1);
2679 g_return_val_if_fail (key != NULL, -1);
2681 s = g_key_file_get_value (key_file, group_name, key, error);
2686 v = g_ascii_strtoull (s, &end, 10);
2688 if (*s == '\0' || *end != '\0')
2690 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2691 _("Key '%s' in group '%s' has value '%s' "
2692 "where %s was expected"),
2693 key, group_name, s, "uint64");
2703 * g_key_file_set_uint64:
2704 * @key_file: a #GKeyFile
2705 * @group_name: a group name
2707 * @value: an integer value
2709 * Associates a new integer value with @key under @group_name.
2710 * If @key cannot be found then it is created.
2715 g_key_file_set_uint64 (GKeyFile *key_file,
2716 const gchar *group_name,
2722 g_return_if_fail (key_file != NULL);
2724 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2725 g_key_file_set_value (key_file, group_name, key, result);
2730 * g_key_file_get_integer_list:
2731 * @key_file: a #GKeyFile
2732 * @group_name: a group name
2734 * @length: (out): the number of integers returned
2735 * @error: return location for a #GError
2737 * Returns the values associated with @key under @group_name as
2740 * If @key cannot be found then %NULL is returned and @error is set to
2741 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2742 * with @key cannot be interpreted as integers then %NULL is returned
2743 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2745 * Return value: (array length=length) (element-type gint) (transfer container):
2746 * the values associated with the key as a list of integers, or %NULL if
2747 * the key was not found or could not be parsed. The returned list of
2748 * integers should be freed with g_free() when no longer needed.
2753 g_key_file_get_integer_list (GKeyFile *key_file,
2754 const gchar *group_name,
2759 GError *key_file_error = NULL;
2764 g_return_val_if_fail (key_file != NULL, NULL);
2765 g_return_val_if_fail (group_name != NULL, NULL);
2766 g_return_val_if_fail (key != NULL, NULL);
2771 values = g_key_file_get_string_list (key_file, group_name, key,
2772 &num_ints, &key_file_error);
2775 g_propagate_error (error, key_file_error);
2780 int_values = g_new (gint, num_ints);
2782 for (i = 0; i < num_ints; i++)
2784 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2790 g_propagate_error (error, key_file_error);
2791 g_strfreev (values);
2792 g_free (int_values);
2797 g_strfreev (values);
2806 * g_key_file_set_integer_list:
2807 * @key_file: a #GKeyFile
2808 * @group_name: a group name
2810 * @list: (array length=length): an array of integer values
2811 * @length: number of integer values in @list
2813 * Associates a list of integer values with @key under @group_name.
2814 * If @key cannot be found then it is created.
2819 g_key_file_set_integer_list (GKeyFile *key_file,
2820 const gchar *group_name,
2828 g_return_if_fail (key_file != NULL);
2829 g_return_if_fail (list != NULL);
2831 values = g_string_sized_new (length * 16);
2832 for (i = 0; i < length; i++)
2836 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2838 g_string_append (values, value);
2839 g_string_append_c (values, key_file->list_separator);
2844 g_key_file_set_value (key_file, group_name, key, values->str);
2845 g_string_free (values, TRUE);
2849 * g_key_file_get_double:
2850 * @key_file: a #GKeyFile
2851 * @group_name: a group name
2853 * @error: return location for a #GError
2855 * Returns the value associated with @key under @group_name as a
2856 * double. If @group_name is %NULL, the start_group is used.
2858 * If @key cannot be found then 0.0 is returned and @error is set to
2859 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2860 * with @key cannot be interpreted as a double then 0.0 is returned
2861 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2863 * Return value: the value associated with the key as a double, or
2864 * 0.0 if the key was not found or could not be parsed.
2869 g_key_file_get_double (GKeyFile *key_file,
2870 const gchar *group_name,
2874 GError *key_file_error;
2876 gdouble double_value;
2878 g_return_val_if_fail (key_file != NULL, -1);
2879 g_return_val_if_fail (group_name != NULL, -1);
2880 g_return_val_if_fail (key != NULL, -1);
2882 key_file_error = NULL;
2884 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2888 g_propagate_error (error, key_file_error);
2892 double_value = g_key_file_parse_value_as_double (key_file, value,
2898 if (g_error_matches (key_file_error,
2900 G_KEY_FILE_ERROR_INVALID_VALUE))
2902 g_set_error (error, G_KEY_FILE_ERROR,
2903 G_KEY_FILE_ERROR_INVALID_VALUE,
2904 _("Key file contains key '%s' in group '%s' "
2905 "which has a value that cannot be interpreted."),
2907 g_error_free (key_file_error);
2910 g_propagate_error (error, key_file_error);
2913 return double_value;
2917 * g_key_file_set_double:
2918 * @key_file: a #GKeyFile
2919 * @group_name: a group name
2921 * @value: an double value
2923 * Associates a new double value with @key under @group_name.
2924 * If @key cannot be found then it is created.
2929 g_key_file_set_double (GKeyFile *key_file,
2930 const gchar *group_name,
2934 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2936 g_return_if_fail (key_file != NULL);
2938 g_ascii_dtostr (result, sizeof (result), value);
2939 g_key_file_set_value (key_file, group_name, key, result);
2943 * g_key_file_get_double_list:
2944 * @key_file: a #GKeyFile
2945 * @group_name: a group name
2947 * @length: (out): the number of doubles returned
2948 * @error: return location for a #GError
2950 * Returns the values associated with @key under @group_name as
2953 * If @key cannot be found then %NULL is returned and @error is set to
2954 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2955 * with @key cannot be interpreted as doubles then %NULL is returned
2956 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2958 * Return value: (array length=length) (element-type gdouble) (transfer container):
2959 * the values associated with the key as a list of doubles, or %NULL if the
2960 * key was not found or could not be parsed. The returned list of doubles
2961 * should be freed with g_free() when no longer needed.
2966 g_key_file_get_double_list (GKeyFile *key_file,
2967 const gchar *group_name,
2972 GError *key_file_error = NULL;
2974 gdouble *double_values;
2975 gsize i, num_doubles;
2977 g_return_val_if_fail (key_file != NULL, NULL);
2978 g_return_val_if_fail (group_name != NULL, NULL);
2979 g_return_val_if_fail (key != NULL, NULL);
2984 values = g_key_file_get_string_list (key_file, group_name, key,
2985 &num_doubles, &key_file_error);
2988 g_propagate_error (error, key_file_error);
2993 double_values = g_new (gdouble, num_doubles);
2995 for (i = 0; i < num_doubles; i++)
2997 double_values[i] = g_key_file_parse_value_as_double (key_file,
3003 g_propagate_error (error, key_file_error);
3004 g_strfreev (values);
3005 g_free (double_values);
3010 g_strfreev (values);
3013 *length = num_doubles;
3015 return double_values;
3019 * g_key_file_set_double_list:
3020 * @key_file: a #GKeyFile
3021 * @group_name: a group name
3023 * @list: (array length=length): an array of double values
3024 * @length: number of double values in @list
3026 * Associates a list of double values with @key under
3027 * @group_name. If @key cannot be found then it is created.
3032 g_key_file_set_double_list (GKeyFile *key_file,
3033 const gchar *group_name,
3041 g_return_if_fail (key_file != NULL);
3042 g_return_if_fail (list != NULL);
3044 values = g_string_sized_new (length * 16);
3045 for (i = 0; i < length; i++)
3047 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3049 g_ascii_dtostr( result, sizeof (result), list[i] );
3051 g_string_append (values, result);
3052 g_string_append_c (values, key_file->list_separator);
3055 g_key_file_set_value (key_file, group_name, key, values->str);
3056 g_string_free (values, TRUE);
3060 g_key_file_set_key_comment (GKeyFile *key_file,
3061 const gchar *group_name,
3063 const gchar *comment,
3066 GKeyFileGroup *group;
3067 GKeyFileKeyValuePair *pair;
3068 GList *key_node, *comment_node, *tmp;
3070 group = g_key_file_lookup_group (key_file, group_name);
3073 g_set_error (error, G_KEY_FILE_ERROR,
3074 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3075 _("Key file does not have group '%s'"),
3076 group_name ? group_name : "(null)");
3081 /* First find the key the comments are supposed to be
3084 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3086 if (key_node == NULL)
3088 g_set_error (error, G_KEY_FILE_ERROR,
3089 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3090 _("Key file does not have key '%s' in group '%s'"),
3095 /* Then find all the comments already associated with the
3098 tmp = key_node->next;
3101 pair = (GKeyFileKeyValuePair *) tmp->data;
3103 if (pair->key != NULL)
3108 g_key_file_remove_key_value_pair_node (key_file, group,
3112 if (comment == NULL)
3115 /* Now we can add our new comment
3117 pair = g_slice_new (GKeyFileKeyValuePair);
3119 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3121 key_node = g_list_insert (key_node, pair, 1);
3127 g_key_file_set_group_comment (GKeyFile *key_file,
3128 const gchar *group_name,
3129 const gchar *comment,
3132 GKeyFileGroup *group;
3134 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3136 group = g_key_file_lookup_group (key_file, group_name);
3139 g_set_error (error, G_KEY_FILE_ERROR,
3140 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3141 _("Key file does not have group '%s'"),
3142 group_name ? group_name : "(null)");
3147 /* First remove any existing comment
3151 g_key_file_key_value_pair_free (group->comment);
3152 group->comment = NULL;
3155 if (comment == NULL)
3158 /* Now we can add our new comment
3160 group->comment = g_slice_new (GKeyFileKeyValuePair);
3161 group->comment->key = NULL;
3162 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3168 g_key_file_set_top_comment (GKeyFile *key_file,
3169 const gchar *comment,
3173 GKeyFileGroup *group;
3174 GKeyFileKeyValuePair *pair;
3176 /* The last group in the list should be the top (comments only)
3179 g_warn_if_fail (key_file->groups != NULL);
3180 group_node = g_list_last (key_file->groups);
3181 group = (GKeyFileGroup *) group_node->data;
3182 g_warn_if_fail (group->name == NULL);
3184 /* Note all keys must be comments at the top of
3185 * the file, so we can just free it all.
3187 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3188 group->key_value_pairs = NULL;
3190 if (comment == NULL)
3193 pair = g_slice_new (GKeyFileKeyValuePair);
3195 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3197 group->key_value_pairs =
3198 g_list_prepend (group->key_value_pairs, pair);
3204 * g_key_file_set_comment:
3205 * @key_file: a #GKeyFile
3206 * @group_name: (allow-none): a group name, or %NULL
3207 * @key: (allow-none): a key
3208 * @comment: a comment
3209 * @error: return location for a #GError
3211 * Places a comment above @key from @group_name.
3212 * If @key is %NULL then @comment will be written above @group_name.
3213 * If both @key and @group_name are %NULL, then @comment will be
3214 * written above the first group in the file.
3216 * Returns: %TRUE if the comment was written, %FALSE otherwise
3221 g_key_file_set_comment (GKeyFile *key_file,
3222 const gchar *group_name,
3224 const gchar *comment,
3227 g_return_val_if_fail (key_file != NULL, FALSE);
3229 if (group_name != NULL && key != NULL)
3231 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3234 else if (group_name != NULL)
3236 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3241 if (!g_key_file_set_top_comment (key_file, comment, error))
3249 g_key_file_get_key_comment (GKeyFile *key_file,
3250 const gchar *group_name,
3254 GKeyFileGroup *group;
3255 GKeyFileKeyValuePair *pair;
3256 GList *key_node, *tmp;
3260 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3262 group = g_key_file_lookup_group (key_file, group_name);
3265 g_set_error (error, G_KEY_FILE_ERROR,
3266 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3267 _("Key file does not have group '%s'"),
3268 group_name ? group_name : "(null)");
3273 /* First find the key the comments are supposed to be
3276 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3278 if (key_node == NULL)
3280 g_set_error (error, G_KEY_FILE_ERROR,
3281 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3282 _("Key file does not have key '%s' in group '%s'"),
3289 /* Then find all the comments already associated with the
3290 * key and concatentate them.
3292 tmp = key_node->next;
3293 if (!key_node->next)
3296 pair = (GKeyFileKeyValuePair *) tmp->data;
3297 if (pair->key != NULL)
3302 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3304 if (pair->key != NULL)
3310 while (tmp != key_node)
3312 pair = (GKeyFileKeyValuePair *) tmp->data;
3315 string = g_string_sized_new (512);
3317 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3318 g_string_append (string, comment);
3326 comment = string->str;
3327 g_string_free (string, FALSE);
3336 get_group_comment (GKeyFile *key_file,
3337 GKeyFileGroup *group,
3346 tmp = group->key_value_pairs;
3349 GKeyFileKeyValuePair *pair;
3351 pair = (GKeyFileKeyValuePair *) tmp->data;
3353 if (pair->key != NULL)
3359 if (tmp->next == NULL)
3367 GKeyFileKeyValuePair *pair;
3369 pair = (GKeyFileKeyValuePair *) tmp->data;
3372 string = g_string_sized_new (512);
3374 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3375 g_string_append (string, comment);
3382 return g_string_free (string, FALSE);
3388 g_key_file_get_group_comment (GKeyFile *key_file,
3389 const gchar *group_name,
3393 GKeyFileGroup *group;
3395 group = g_key_file_lookup_group (key_file, group_name);
3398 g_set_error (error, G_KEY_FILE_ERROR,
3399 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3400 _("Key file does not have group '%s'"),
3401 group_name ? group_name : "(null)");
3407 return g_strdup (group->comment->value);
3409 group_node = g_key_file_lookup_group_node (key_file, group_name);
3410 group_node = group_node->next;
3411 group = (GKeyFileGroup *)group_node->data;
3412 return get_group_comment (key_file, group, error);
3416 g_key_file_get_top_comment (GKeyFile *key_file,
3420 GKeyFileGroup *group;
3422 /* The last group in the list should be the top (comments only)
3425 g_warn_if_fail (key_file->groups != NULL);
3426 group_node = g_list_last (key_file->groups);
3427 group = (GKeyFileGroup *) group_node->data;
3428 g_warn_if_fail (group->name == NULL);
3430 return get_group_comment (key_file, group, error);
3434 * g_key_file_get_comment:
3435 * @key_file: a #GKeyFile
3436 * @group_name: (allow-none): a group name, or %NULL
3438 * @error: return location for a #GError
3440 * Retrieves a comment above @key from @group_name.
3441 * If @key is %NULL then @comment will be read from above
3442 * @group_name. If both @key and @group_name are %NULL, then
3443 * @comment will be read from above the first group in the file.
3445 * Returns: a comment that should be freed with g_free()
3450 g_key_file_get_comment (GKeyFile *key_file,
3451 const gchar *group_name,
3455 g_return_val_if_fail (key_file != NULL, NULL);
3457 if (group_name != NULL && key != NULL)
3458 return g_key_file_get_key_comment (key_file, group_name, key, error);
3459 else if (group_name != NULL)
3460 return g_key_file_get_group_comment (key_file, group_name, error);
3462 return g_key_file_get_top_comment (key_file, error);
3466 * g_key_file_remove_comment:
3467 * @key_file: a #GKeyFile
3468 * @group_name: (allow-none): a group name, or %NULL
3469 * @key: (allow-none): a key
3470 * @error: return location for a #GError
3472 * Removes a comment above @key from @group_name.
3473 * If @key is %NULL then @comment will be removed above @group_name.
3474 * If both @key and @group_name are %NULL, then @comment will
3475 * be removed above the first group in the file.
3477 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3483 g_key_file_remove_comment (GKeyFile *key_file,
3484 const gchar *group_name,
3488 g_return_val_if_fail (key_file != NULL, FALSE);
3490 if (group_name != NULL && key != NULL)
3491 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3492 else if (group_name != NULL)
3493 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3495 return g_key_file_set_top_comment (key_file, NULL, error);
3499 * g_key_file_has_group:
3500 * @key_file: a #GKeyFile
3501 * @group_name: a group name
3503 * Looks whether the key file has the group @group_name.
3505 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3510 g_key_file_has_group (GKeyFile *key_file,
3511 const gchar *group_name)
3513 g_return_val_if_fail (key_file != NULL, FALSE);
3514 g_return_val_if_fail (group_name != NULL, FALSE);
3516 return g_key_file_lookup_group (key_file, group_name) != NULL;
3519 /* This code remains from a historical attempt to add a new public API
3520 * which respects the GError rules.
3523 g_key_file_has_key_full (GKeyFile *key_file,
3524 const gchar *group_name,
3529 GKeyFileKeyValuePair *pair;
3530 GKeyFileGroup *group;
3532 g_return_val_if_fail (key_file != NULL, FALSE);
3533 g_return_val_if_fail (group_name != NULL, FALSE);
3534 g_return_val_if_fail (key != NULL, FALSE);
3536 group = g_key_file_lookup_group (key_file, group_name);
3540 g_set_error (error, G_KEY_FILE_ERROR,
3541 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3542 _("Key file does not have group '%s'"),
3543 group_name ? group_name : "(null)");
3548 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3551 *has_key = pair != NULL;
3556 * g_key_file_has_key: (skip)
3557 * @key_file: a #GKeyFile
3558 * @group_name: a group name
3560 * @error: return location for a #GError
3562 * Looks whether the key file has the key @key in the group
3565 * <note>This function does not follow the rules for #GError strictly;
3566 * the return value both carries meaning and signals an error. To use
3567 * this function, you must pass a #GError pointer in @error, and check
3568 * whether it is not %NULL to see if an error occurred.</note>
3570 * Language bindings should use g_key_file_get_value() to test whether
3571 * or not a key exists.
3573 * Return value: %TRUE if @key is a part of @group_name, %FALSE
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);