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 "key files" for lack of a better name.
78 * Several freedesktop.org specifications use key files now, e.g the
79 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
81 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
83 * The syntax of key files is described in detail in the
84 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
85 * here is a quick summary: Key files
86 * consists of groups of key-value pairs, interspersed with comments.
89 * # this is just an example
90 * # there can be comments before the first group
94 * Name=Key File Example\tthis value shows\nescaping
96 * # localized strings are stored in multiple key-value pairs
99 * Welcome[fr_FR]=Bonjour
101 * Welcome[be@latin]=Hello
105 * Numbers=2;20;-200;0
107 * Booleans=true;false;true;true
110 * Lines beginning with a '#' and blank lines are considered comments.
112 * Groups are started by a header line containing the group name enclosed
113 * in '[' and ']', and ended implicitly by the start of the next group or
114 * the end of the file. Each key-value pair must be contained in a group.
116 * Key-value pairs generally have the form `key=value`, with the
117 * exception of localized strings, which have the form
118 * `key[locale]=value`, with a locale identifier of the
119 * form `lang_COUNTRY\@MODIFIER` where `COUNTRY` and `MODIFIER`
121 * Space before and after the '=' character are ignored. Newline, tab,
122 * carriage return and backslash characters in value are escaped as \n,
123 * \t, \r, and \\, respectively. To preserve leading spaces in values,
124 * these can also be escaped as \s.
126 * Key files can store strings (possibly with localized variants), integers,
127 * booleans and lists of these. Lists are separated by a separator character,
128 * typically ';' or ','. To use the list separator character in a value in
129 * a list, it has to be escaped by prefixing it with a backslash.
131 * This syntax is obviously inspired by the .ini files commonly met
132 * on Windows, but there are some important differences:
134 * - .ini files use the ';' character to begin comments,
135 * key files use the '#' character.
137 * - Key files do not allow for ungrouped keys meaning only
138 * comments can precede the first group.
140 * - Key files are always encoded in UTF-8.
142 * - Key and Group names are case-sensitive. For example, a group called
143 * [GROUP] is a different from [group].
145 * - .ini files don't have a strongly typed boolean entry type,
146 * they only have GetProfileInt(). In key files, only
147 * true and false (in lower case) are allowed.
149 * Note that in contrast to the
150 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
151 * groups in key files may contain the same
152 * key multiple times; the last entry wins. Key files may also contain
153 * multiple groups with the same name; they are merged together.
154 * Another difference is that keys and group names in key files are not
155 * restricted to ASCII characters.
161 * Error domain for key file parsing. Errors in this domain will
162 * be from the #GKeyFileError enumeration.
164 * See #GError for information on error domains.
169 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
170 * an unknown encoding
171 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
172 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
173 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
174 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
175 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
177 * Error codes returned by key file parsing.
182 * @G_KEY_FILE_NONE: No flags, default behaviour
183 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
184 * (possibly modified) contents of the key file back to a file;
185 * otherwise all comments will be lost when the key file is
187 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
188 * (possibly modified) contents of the key file back to a file;
189 * otherwise only the translations for the current language will be
192 * Flags which influence the parsing.
196 * G_KEY_FILE_DESKTOP_GROUP:
198 * The name of the main group of a desktop entry file, as defined in the
199 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
200 * Consult the specification for more
201 * details about the meanings of the keys below.
207 * G_KEY_FILE_DESKTOP_KEY_TYPE:
209 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
210 * giving the type of the desktop entry. Usually
211 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
212 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
213 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
219 * G_KEY_FILE_DESKTOP_KEY_VERSION:
221 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
222 * giving the version of the Desktop Entry Specification used for
223 * the desktop entry file.
229 * G_KEY_FILE_DESKTOP_KEY_NAME:
231 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
232 * string giving the specific name of the desktop entry.
238 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
240 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
241 * string giving the generic name of the desktop entry.
247 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
249 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
250 * stating whether the desktop entry should be shown in menus.
256 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
258 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
259 * string giving the tooltip for the desktop entry.
265 * G_KEY_FILE_DESKTOP_KEY_ICON:
267 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
268 * string giving the name of the icon to be displayed for the desktop
275 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
277 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
278 * stating whether the desktop entry has been deleted by the user.
284 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
286 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
287 * strings identifying the environments that should display the
294 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
296 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
297 * strings identifying the environments that should not display the
304 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
306 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
307 * giving the file name of a binary on disk used to determine if the
308 * program is actually installed. It is only valid for desktop entries
309 * with the `Application` type.
315 * G_KEY_FILE_DESKTOP_KEY_EXEC:
317 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
318 * giving the command line to execute. It is only valid for desktop
319 * entries with the `Application` type.
325 * G_KEY_FILE_DESKTOP_KEY_PATH:
327 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
328 * containing the working directory to run the program in. It is only
329 * valid for desktop entries with the `Application` type.
335 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
337 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
338 * stating whether the program should be run in a terminal window.
339 * It is only valid for desktop entries with the
340 * `Application` type.
346 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
348 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
349 * of strings giving the MIME types supported by this desktop entry.
355 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
357 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
358 * of strings giving the categories in which the desktop entry
359 * should be shown in a menu.
365 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
367 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
368 * stating whether the application supports the
369 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
375 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
377 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
378 * identifying the WM class or name hint of a window that the application
379 * will create, which can be used to emulate Startup Notification with
380 * older applications.
386 * G_KEY_FILE_DESKTOP_KEY_URL :
388 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
389 * giving the URL to access. It is only valid for desktop entries
390 * with the `Link` type.
396 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
398 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
399 * entries representing applications.
405 * G_KEY_FILE_DESKTOP_TYPE_LINK:
407 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
408 * entries representing links to documents.
414 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
416 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
417 * entries representing directories.
422 typedef struct _GKeyFileGroup GKeyFileGroup;
427 * The GKeyFile struct contains only private data
428 * and should not be accessed directly.
433 GHashTable *group_hash;
435 GKeyFileGroup *start_group;
436 GKeyFileGroup *current_group;
438 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
440 gchar list_separator;
446 volatile gint ref_count;
449 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
451 struct _GKeyFileGroup
453 const gchar *name; /* NULL for above first group (which will be comments) */
455 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
457 GList *key_value_pairs;
459 /* Used in parallel with key_value_pairs for
460 * increased lookup performance
462 GHashTable *lookup_map;
465 struct _GKeyFileKeyValuePair
467 gchar *key; /* NULL for comments */
471 static gint find_file_in_data_dirs (const gchar *file,
472 const gchar **data_dirs,
475 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
479 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
480 const gchar *group_name);
481 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
482 const gchar *group_name);
484 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
485 GKeyFileGroup *group,
487 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
488 GKeyFileGroup *group,
491 static void g_key_file_remove_group_node (GKeyFile *key_file,
493 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
494 GKeyFileGroup *group,
497 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
498 GKeyFileGroup *group,
499 GKeyFileKeyValuePair *pair);
500 static void g_key_file_add_key (GKeyFile *key_file,
501 GKeyFileGroup *group,
504 static void g_key_file_add_group (GKeyFile *key_file,
505 const gchar *group_name);
506 static gboolean g_key_file_is_group_name (const gchar *name);
507 static gboolean g_key_file_is_key_name (const gchar *name);
508 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
509 static gboolean g_key_file_line_is_comment (const gchar *line);
510 static gboolean g_key_file_line_is_group (const gchar *line);
511 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
512 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
516 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
518 gboolean escape_separator);
519 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
522 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
524 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
527 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
530 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
532 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
534 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
535 const gchar *comment);
536 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
540 static void g_key_file_parse_comment (GKeyFile *key_file,
544 static void g_key_file_parse_group (GKeyFile *key_file,
548 static gchar *key_get_locale (const gchar *key);
549 static void g_key_file_parse_data (GKeyFile *key_file,
553 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
556 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
559 g_key_file_init (GKeyFile *key_file)
561 key_file->current_group = g_slice_new0 (GKeyFileGroup);
562 key_file->groups = g_list_prepend (NULL, key_file->current_group);
563 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
564 key_file->start_group = NULL;
565 key_file->parse_buffer = g_string_sized_new (128);
566 key_file->list_separator = ';';
568 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
572 g_key_file_clear (GKeyFile *key_file)
574 GList *tmp, *group_node;
576 if (key_file->locales)
578 g_strfreev (key_file->locales);
579 key_file->locales = NULL;
582 if (key_file->parse_buffer)
584 g_string_free (key_file->parse_buffer, TRUE);
585 key_file->parse_buffer = NULL;
588 tmp = key_file->groups;
593 g_key_file_remove_group_node (key_file, group_node);
596 if (key_file->group_hash != NULL)
598 g_hash_table_destroy (key_file->group_hash);
599 key_file->group_hash = NULL;
602 g_warn_if_fail (key_file->groups == NULL);
609 * Creates a new empty #GKeyFile object. Use
610 * g_key_file_load_from_file(), g_key_file_load_from_data(),
611 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
612 * read an existing key file.
614 * Return value: (transfer full): an empty #GKeyFile.
619 g_key_file_new (void)
623 key_file = g_slice_new0 (GKeyFile);
624 key_file->ref_count = 1;
625 g_key_file_init (key_file);
631 * g_key_file_set_list_separator:
632 * @key_file: a #GKeyFile
633 * @separator: the separator
635 * Sets the character which is used to separate
636 * values in lists. Typically ';' or ',' are used
637 * as separators. The default list separator is ';'.
642 g_key_file_set_list_separator (GKeyFile *key_file,
645 g_return_if_fail (key_file != NULL);
647 key_file->list_separator = separator;
651 /* Iterates through all the directories in *dirs trying to
652 * open file. When it successfully locates and opens a file it
653 * returns the file descriptor to the open file. It also
654 * outputs the absolute path of the file in output_file.
657 find_file_in_data_dirs (const gchar *file,
662 const gchar **data_dirs, *data_dir;
674 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
676 gchar *candidate_file, *sub_dir;
678 candidate_file = (gchar *) file;
679 sub_dir = g_strdup ("");
680 while (candidate_file != NULL && fd == -1)
684 path = g_build_filename (data_dir, sub_dir,
685 candidate_file, NULL);
687 fd = g_open (path, O_RDONLY, 0);
695 candidate_file = strchr (candidate_file, '-');
697 if (candidate_file == NULL)
703 sub_dir = g_strndup (file, candidate_file - file - 1);
705 for (p = sub_dir; *p != '\0'; p++)
708 *p = G_DIR_SEPARATOR;
717 g_set_error_literal (error, G_KEY_FILE_ERROR,
718 G_KEY_FILE_ERROR_NOT_FOUND,
719 _("Valid key file could not be "
720 "found in search dirs"));
723 if (output_file != NULL && fd > 0)
724 *output_file = g_strdup (path);
732 g_key_file_load_from_fd (GKeyFile *key_file,
737 GError *key_file_error = NULL;
739 struct stat stat_buf;
740 gchar read_buf[4096];
741 gchar list_separator;
743 if (fstat (fd, &stat_buf) < 0)
745 g_set_error_literal (error, G_FILE_ERROR,
746 g_file_error_from_errno (errno),
751 if (!S_ISREG (stat_buf.st_mode))
753 g_set_error_literal (error, G_KEY_FILE_ERROR,
754 G_KEY_FILE_ERROR_PARSE,
755 _("Not a regular file"));
759 list_separator = key_file->list_separator;
760 g_key_file_clear (key_file);
761 g_key_file_init (key_file);
762 key_file->list_separator = list_separator;
763 key_file->flags = flags;
767 bytes_read = read (fd, read_buf, 4096);
769 if (bytes_read == 0) /* End of File */
774 if (errno == EINTR || errno == EAGAIN)
777 g_set_error_literal (error, G_FILE_ERROR,
778 g_file_error_from_errno (errno),
783 g_key_file_parse_data (key_file,
784 read_buf, bytes_read,
787 while (!key_file_error);
791 g_propagate_error (error, key_file_error);
795 g_key_file_flush_parse_buffer (key_file, &key_file_error);
799 g_propagate_error (error, key_file_error);
807 * g_key_file_load_from_file:
808 * @key_file: an empty #GKeyFile struct
809 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
810 * @flags: flags from #GKeyFileFlags
811 * @error: return location for a #GError, or %NULL
813 * Loads a key file into an empty #GKeyFile structure.
814 * If the file could not be loaded then @error is set to
815 * either a #GFileError or #GKeyFileError.
817 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
822 g_key_file_load_from_file (GKeyFile *key_file,
827 GError *key_file_error = NULL;
830 g_return_val_if_fail (key_file != NULL, FALSE);
831 g_return_val_if_fail (file != NULL, FALSE);
833 fd = g_open (file, O_RDONLY, 0);
837 g_set_error_literal (error, G_FILE_ERROR,
838 g_file_error_from_errno (errno),
843 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
848 g_propagate_error (error, key_file_error);
856 * g_key_file_load_from_data:
857 * @key_file: an empty #GKeyFile struct
858 * @data: key file loaded in memory
859 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
860 * @flags: flags from #GKeyFileFlags
861 * @error: return location for a #GError, or %NULL
863 * Loads a key file from memory into an empty #GKeyFile structure.
864 * If the object cannot be created then %error is set to a #GKeyFileError.
866 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
871 g_key_file_load_from_data (GKeyFile *key_file,
877 GError *key_file_error = NULL;
878 gchar list_separator;
880 g_return_val_if_fail (key_file != NULL, FALSE);
881 g_return_val_if_fail (data != NULL || length == 0, FALSE);
883 if (length == (gsize)-1)
884 length = strlen (data);
886 list_separator = key_file->list_separator;
887 g_key_file_clear (key_file);
888 g_key_file_init (key_file);
889 key_file->list_separator = list_separator;
890 key_file->flags = flags;
892 g_key_file_parse_data (key_file, data, length, &key_file_error);
896 g_propagate_error (error, key_file_error);
900 g_key_file_flush_parse_buffer (key_file, &key_file_error);
904 g_propagate_error (error, key_file_error);
912 * g_key_file_load_from_dirs:
913 * @key_file: an empty #GKeyFile struct
914 * @file: (type filename): a relative path to a filename to open and parse
915 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
916 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
917 * of the file, or %NULL
918 * @flags: flags from #GKeyFileFlags
919 * @error: return location for a #GError, or %NULL
921 * This function looks for a key file named @file in the paths
922 * specified in @search_dirs, loads the file into @key_file and
923 * returns the file's full path in @full_path. If the file could not
924 * be loaded then an %error is set to either a #GFileError or
927 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
932 g_key_file_load_from_dirs (GKeyFile *key_file,
934 const gchar **search_dirs,
939 GError *key_file_error = NULL;
940 const gchar **data_dirs;
945 g_return_val_if_fail (key_file != NULL, FALSE);
946 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
947 g_return_val_if_fail (search_dirs != NULL, FALSE);
950 data_dirs = search_dirs;
952 while (*data_dirs != NULL && !found_file)
954 g_free (output_path);
956 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
962 g_propagate_error (error, key_file_error);
966 found_file = g_key_file_load_from_fd (key_file, fd, flags,
972 g_propagate_error (error, key_file_error);
977 if (found_file && full_path)
978 *full_path = output_path;
980 g_free (output_path);
986 * g_key_file_load_from_data_dirs:
987 * @key_file: an empty #GKeyFile struct
988 * @file: (type filename): a relative path to a filename to open and parse
989 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
990 * of the file, or %NULL
991 * @flags: flags from #GKeyFileFlags
992 * @error: return location for a #GError, or %NULL
994 * This function looks for a key file named @file in the paths
995 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
996 * loads the file into @key_file and returns the file's full path in
997 * @full_path. If the file could not be loaded then an %error is
998 * set to either a #GFileError or #GKeyFileError.
1000 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1004 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1007 GKeyFileFlags flags,
1010 gchar **all_data_dirs;
1011 const gchar * user_data_dir;
1012 const gchar * const * system_data_dirs;
1014 gboolean found_file;
1016 g_return_val_if_fail (key_file != NULL, FALSE);
1017 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1019 user_data_dir = g_get_user_data_dir ();
1020 system_data_dirs = g_get_system_data_dirs ();
1021 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1024 all_data_dirs[i++] = g_strdup (user_data_dir);
1027 while (system_data_dirs[j] != NULL)
1028 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1029 all_data_dirs[i] = NULL;
1031 found_file = g_key_file_load_from_dirs (key_file,
1033 (const gchar **)all_data_dirs,
1038 g_strfreev (all_data_dirs);
1044 * g_key_file_ref: (skip)
1045 * @key_file: a #GKeyFile
1047 * Increases the reference count of @key_file.
1049 * Returns: the same @key_file.
1054 g_key_file_ref (GKeyFile *key_file)
1056 g_return_val_if_fail (key_file != NULL, NULL);
1058 g_atomic_int_inc (&key_file->ref_count);
1064 * g_key_file_free: (skip)
1065 * @key_file: a #GKeyFile
1067 * Clears all keys and groups from @key_file, and decreases the
1068 * reference count by 1. If the reference count reaches zero,
1069 * frees the key file and all its allocated memory.
1074 g_key_file_free (GKeyFile *key_file)
1076 g_return_if_fail (key_file != NULL);
1078 g_key_file_clear (key_file);
1079 g_key_file_unref (key_file);
1084 * @key_file: a #GKeyFile
1086 * Decreases the reference count of @key_file by 1. If the reference count
1087 * reaches zero, frees the key file and all its allocated memory.
1092 g_key_file_unref (GKeyFile *key_file)
1094 g_return_if_fail (key_file != NULL);
1096 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1098 g_key_file_clear (key_file);
1099 g_slice_free (GKeyFile, key_file);
1103 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1104 * true for locales that match those in g_get_language_names().
1107 g_key_file_locale_is_interesting (GKeyFile *key_file,
1108 const gchar *locale)
1112 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1115 for (i = 0; key_file->locales[i] != NULL; i++)
1117 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1125 g_key_file_parse_line (GKeyFile *key_file,
1130 GError *parse_error = NULL;
1133 g_return_if_fail (key_file != NULL);
1134 g_return_if_fail (line != NULL);
1136 line_start = (gchar *) line;
1137 while (g_ascii_isspace (*line_start))
1140 if (g_key_file_line_is_comment (line_start))
1141 g_key_file_parse_comment (key_file, line, length, &parse_error);
1142 else if (g_key_file_line_is_group (line_start))
1143 g_key_file_parse_group (key_file, line_start,
1144 length - (line_start - line),
1146 else if (g_key_file_line_is_key_value_pair (line_start))
1147 g_key_file_parse_key_value_pair (key_file, line_start,
1148 length - (line_start - line),
1152 gchar *line_utf8 = _g_utf8_make_valid (line);
1153 g_set_error (error, G_KEY_FILE_ERROR,
1154 G_KEY_FILE_ERROR_PARSE,
1155 _("Key file contains line '%s' which is not "
1156 "a key-value pair, group, or comment"),
1164 g_propagate_error (error, parse_error);
1168 g_key_file_parse_comment (GKeyFile *key_file,
1173 GKeyFileKeyValuePair *pair;
1175 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1178 g_warn_if_fail (key_file->current_group != NULL);
1180 pair = g_slice_new (GKeyFileKeyValuePair);
1182 pair->value = g_strndup (line, length);
1184 key_file->current_group->key_value_pairs =
1185 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1189 g_key_file_parse_group (GKeyFile *key_file,
1195 const gchar *group_name_start, *group_name_end;
1197 /* advance past opening '['
1199 group_name_start = line + 1;
1200 group_name_end = line + length - 1;
1202 while (*group_name_end != ']')
1205 group_name = g_strndup (group_name_start,
1206 group_name_end - group_name_start);
1208 if (!g_key_file_is_group_name (group_name))
1210 g_set_error (error, G_KEY_FILE_ERROR,
1211 G_KEY_FILE_ERROR_PARSE,
1212 _("Invalid group name: %s"), group_name);
1213 g_free (group_name);
1217 g_key_file_add_group (key_file, group_name);
1218 g_free (group_name);
1222 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1227 gchar *key, *value, *key_end, *value_start, *locale;
1228 gsize key_len, value_len;
1230 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1232 g_set_error_literal (error, G_KEY_FILE_ERROR,
1233 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1234 _("Key file does not start with a group"));
1238 key_end = value_start = strchr (line, '=');
1240 g_warn_if_fail (key_end != NULL);
1245 /* Pull the key name from the line (chomping trailing whitespace)
1247 while (g_ascii_isspace (*key_end))
1250 key_len = key_end - line + 2;
1252 g_warn_if_fail (key_len <= length);
1254 key = g_strndup (line, key_len - 1);
1256 if (!g_key_file_is_key_name (key))
1258 g_set_error (error, G_KEY_FILE_ERROR,
1259 G_KEY_FILE_ERROR_PARSE,
1260 _("Invalid key name: %s"), key);
1265 /* Pull the value from the line (chugging leading whitespace)
1267 while (g_ascii_isspace (*value_start))
1270 value_len = line + length - value_start + 1;
1272 value = g_strndup (value_start, value_len);
1274 g_warn_if_fail (key_file->start_group != NULL);
1276 if (key_file->current_group
1277 && key_file->current_group->name
1278 && strcmp (key_file->start_group->name,
1279 key_file->current_group->name) == 0
1280 && strcmp (key, "Encoding") == 0)
1282 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1284 gchar *value_utf8 = _g_utf8_make_valid (value);
1285 g_set_error (error, G_KEY_FILE_ERROR,
1286 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1287 _("Key file contains unsupported "
1288 "encoding '%s'"), value_utf8);
1289 g_free (value_utf8);
1297 /* Is this key a translation? If so, is it one that we care about?
1299 locale = key_get_locale (key);
1301 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1303 GKeyFileKeyValuePair *pair;
1305 pair = g_slice_new (GKeyFileKeyValuePair);
1307 pair->value = value;
1309 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1321 key_get_locale (const gchar *key)
1325 locale = g_strrstr (key, "[");
1327 if (locale && strlen (locale) <= 2)
1331 locale = g_strndup (locale + 1, strlen (locale) - 2);
1337 g_key_file_parse_data (GKeyFile *key_file,
1342 GError *parse_error;
1345 g_return_if_fail (key_file != NULL);
1346 g_return_if_fail (data != NULL || length == 0);
1353 if (data[i] == '\n')
1355 if (key_file->parse_buffer->len > 0
1356 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1358 g_string_erase (key_file->parse_buffer,
1359 key_file->parse_buffer->len - 1,
1362 /* When a newline is encountered flush the parse buffer so that the
1363 * line can be parsed. Note that completely blank lines won't show
1364 * up in the parse buffer, so they get parsed directly.
1366 if (key_file->parse_buffer->len > 0)
1367 g_key_file_flush_parse_buffer (key_file, &parse_error);
1369 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1373 g_propagate_error (error, parse_error);
1380 const gchar *start_of_line;
1381 const gchar *end_of_line;
1384 start_of_line = data + i;
1385 end_of_line = memchr (start_of_line, '\n', length - i);
1387 if (end_of_line == NULL)
1388 end_of_line = data + length;
1390 line_length = end_of_line - start_of_line;
1392 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1399 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1402 GError *file_error = NULL;
1404 g_return_if_fail (key_file != NULL);
1408 if (key_file->parse_buffer->len > 0)
1410 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1411 key_file->parse_buffer->len,
1413 g_string_erase (key_file->parse_buffer, 0, -1);
1417 g_propagate_error (error, file_error);
1424 * g_key_file_to_data:
1425 * @key_file: a #GKeyFile
1426 * @length: (out) (allow-none): return location for the length of the
1427 * returned string, or %NULL
1428 * @error: return location for a #GError, or %NULL
1430 * This function outputs @key_file as a string.
1432 * Note that this function never reports an error,
1433 * so it is safe to pass %NULL as @error.
1435 * Return value: a newly allocated string holding
1436 * the contents of the #GKeyFile
1441 g_key_file_to_data (GKeyFile *key_file,
1445 GString *data_string;
1446 GList *group_node, *key_file_node;
1448 g_return_val_if_fail (key_file != NULL, NULL);
1450 data_string = g_string_new (NULL);
1452 for (group_node = g_list_last (key_file->groups);
1454 group_node = group_node->prev)
1456 GKeyFileGroup *group;
1458 group = (GKeyFileGroup *) group_node->data;
1460 /* separate groups by at least an empty line */
1461 if (data_string->len >= 2 &&
1462 data_string->str[data_string->len - 2] != '\n')
1463 g_string_append_c (data_string, '\n');
1465 if (group->comment != NULL)
1466 g_string_append_printf (data_string, "%s\n", group->comment->value);
1468 if (group->name != NULL)
1469 g_string_append_printf (data_string, "[%s]\n", group->name);
1471 for (key_file_node = g_list_last (group->key_value_pairs);
1472 key_file_node != NULL;
1473 key_file_node = key_file_node->prev)
1475 GKeyFileKeyValuePair *pair;
1477 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1479 if (pair->key != NULL)
1480 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1482 g_string_append_printf (data_string, "%s\n", pair->value);
1487 *length = data_string->len;
1489 return g_string_free (data_string, FALSE);
1493 * g_key_file_get_keys:
1494 * @key_file: a #GKeyFile
1495 * @group_name: a group name
1496 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1497 * @error: return location for a #GError, or %NULL
1499 * Returns all keys for the group name @group_name. The array of
1500 * returned keys will be %NULL-terminated, so @length may
1501 * optionally be %NULL. In the event that the @group_name cannot
1502 * be found, %NULL is returned and @error is set to
1503 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1505 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1506 * Use g_strfreev() to free it.
1511 g_key_file_get_keys (GKeyFile *key_file,
1512 const gchar *group_name,
1516 GKeyFileGroup *group;
1521 g_return_val_if_fail (key_file != NULL, NULL);
1522 g_return_val_if_fail (group_name != NULL, NULL);
1524 group = g_key_file_lookup_group (key_file, group_name);
1528 g_set_error (error, G_KEY_FILE_ERROR,
1529 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1530 _("Key file does not have group '%s'"),
1531 group_name ? group_name : "(null)");
1536 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1538 GKeyFileKeyValuePair *pair;
1540 pair = (GKeyFileKeyValuePair *) tmp->data;
1546 keys = g_new (gchar *, num_keys + 1);
1549 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1551 GKeyFileKeyValuePair *pair;
1553 pair = (GKeyFileKeyValuePair *) tmp->data;
1557 keys[i] = g_strdup (pair->key);
1562 keys[num_keys] = NULL;
1571 * g_key_file_get_start_group:
1572 * @key_file: a #GKeyFile
1574 * Returns the name of the start group of the file.
1576 * Return value: The start group of the key file.
1581 g_key_file_get_start_group (GKeyFile *key_file)
1583 g_return_val_if_fail (key_file != NULL, NULL);
1585 if (key_file->start_group)
1586 return g_strdup (key_file->start_group->name);
1592 * g_key_file_get_groups:
1593 * @key_file: a #GKeyFile
1594 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1596 * Returns all groups in the key file loaded with @key_file.
1597 * The array of returned groups will be %NULL-terminated, so
1598 * @length may optionally be %NULL.
1600 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1601 * Use g_strfreev() to free it.
1605 g_key_file_get_groups (GKeyFile *key_file,
1610 gsize i, num_groups;
1612 g_return_val_if_fail (key_file != NULL, NULL);
1614 num_groups = g_list_length (key_file->groups);
1616 g_return_val_if_fail (num_groups > 0, NULL);
1618 group_node = g_list_last (key_file->groups);
1620 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1622 /* Only need num_groups instead of num_groups + 1
1623 * because the first group of the file (last in the
1624 * list) is always the comment group at the top,
1627 groups = g_new (gchar *, num_groups);
1631 for (group_node = group_node->prev;
1633 group_node = group_node->prev)
1635 GKeyFileGroup *group;
1637 group = (GKeyFileGroup *) group_node->data;
1639 g_warn_if_fail (group->name != NULL);
1641 groups[i++] = g_strdup (group->name);
1652 * g_key_file_get_value:
1653 * @key_file: a #GKeyFile
1654 * @group_name: a group name
1656 * @error: return location for a #GError, or %NULL
1658 * Returns the raw value associated with @key under @group_name.
1659 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1661 * In the event the key cannot be found, %NULL is returned and
1662 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1663 * event that the @group_name cannot be found, %NULL is returned
1664 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1667 * Return value: a newly allocated string or %NULL if the specified
1668 * key cannot be found.
1673 g_key_file_get_value (GKeyFile *key_file,
1674 const gchar *group_name,
1678 GKeyFileGroup *group;
1679 GKeyFileKeyValuePair *pair;
1680 gchar *value = NULL;
1682 g_return_val_if_fail (key_file != NULL, NULL);
1683 g_return_val_if_fail (group_name != NULL, NULL);
1684 g_return_val_if_fail (key != NULL, NULL);
1686 group = g_key_file_lookup_group (key_file, group_name);
1690 g_set_error (error, G_KEY_FILE_ERROR,
1691 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1692 _("Key file does not have group '%s'"),
1693 group_name ? group_name : "(null)");
1697 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1700 value = g_strdup (pair->value);
1702 g_set_error (error, G_KEY_FILE_ERROR,
1703 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1704 _("Key file does not have key '%s'"), key);
1710 * g_key_file_set_value:
1711 * @key_file: a #GKeyFile
1712 * @group_name: a group name
1716 * Associates a new value with @key under @group_name.
1718 * If @key cannot be found then it is created. If @group_name cannot
1719 * be found then it is created. To set an UTF-8 string which may contain
1720 * characters that need escaping (such as newlines or spaces), use
1721 * g_key_file_set_string().
1726 g_key_file_set_value (GKeyFile *key_file,
1727 const gchar *group_name,
1731 GKeyFileGroup *group;
1732 GKeyFileKeyValuePair *pair;
1734 g_return_if_fail (key_file != NULL);
1735 g_return_if_fail (g_key_file_is_group_name (group_name));
1736 g_return_if_fail (g_key_file_is_key_name (key));
1737 g_return_if_fail (value != NULL);
1739 group = g_key_file_lookup_group (key_file, group_name);
1743 g_key_file_add_group (key_file, group_name);
1744 group = (GKeyFileGroup *) key_file->groups->data;
1746 g_key_file_add_key (key_file, group, key, value);
1750 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1753 g_key_file_add_key (key_file, group, key, value);
1756 g_free (pair->value);
1757 pair->value = g_strdup (value);
1763 * g_key_file_get_string:
1764 * @key_file: a #GKeyFile
1765 * @group_name: a group name
1767 * @error: return location for a #GError, or %NULL
1769 * Returns the string value associated with @key under @group_name.
1770 * Unlike g_key_file_get_value(), this function handles escape sequences
1773 * In the event the key cannot be found, %NULL is returned and
1774 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1775 * event that the @group_name cannot be found, %NULL is returned
1776 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1778 * Return value: a newly allocated string or %NULL if the specified
1779 * key cannot be found.
1784 g_key_file_get_string (GKeyFile *key_file,
1785 const gchar *group_name,
1789 gchar *value, *string_value;
1790 GError *key_file_error;
1792 g_return_val_if_fail (key_file != NULL, NULL);
1793 g_return_val_if_fail (group_name != NULL, NULL);
1794 g_return_val_if_fail (key != NULL, NULL);
1796 key_file_error = NULL;
1798 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1802 g_propagate_error (error, key_file_error);
1806 if (!g_utf8_validate (value, -1, NULL))
1808 gchar *value_utf8 = _g_utf8_make_valid (value);
1809 g_set_error (error, G_KEY_FILE_ERROR,
1810 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1811 _("Key file contains key '%s' with value '%s' "
1812 "which is not UTF-8"), key, value_utf8);
1813 g_free (value_utf8);
1819 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1825 if (g_error_matches (key_file_error,
1827 G_KEY_FILE_ERROR_INVALID_VALUE))
1829 g_set_error (error, G_KEY_FILE_ERROR,
1830 G_KEY_FILE_ERROR_INVALID_VALUE,
1831 _("Key file contains key '%s' "
1832 "which has a value that cannot be interpreted."),
1834 g_error_free (key_file_error);
1837 g_propagate_error (error, key_file_error);
1840 return string_value;
1844 * g_key_file_set_string:
1845 * @key_file: a #GKeyFile
1846 * @group_name: a group name
1850 * Associates a new string value with @key under @group_name.
1851 * If @key cannot be found then it is created.
1852 * If @group_name cannot be found then it is created.
1853 * Unlike g_key_file_set_value(), this function handles characters
1854 * that need escaping, such as newlines.
1859 g_key_file_set_string (GKeyFile *key_file,
1860 const gchar *group_name,
1862 const gchar *string)
1866 g_return_if_fail (key_file != NULL);
1867 g_return_if_fail (string != NULL);
1869 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1870 g_key_file_set_value (key_file, group_name, key, value);
1875 * g_key_file_get_string_list:
1876 * @key_file: a #GKeyFile
1877 * @group_name: a group name
1879 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1880 * @error: return location for a #GError, or %NULL
1882 * Returns the values associated with @key under @group_name.
1884 * In the event the key cannot be found, %NULL is returned and
1885 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1886 * event that the @group_name cannot be found, %NULL is returned
1887 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1889 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1890 * a %NULL-terminated string array or %NULL if the specified
1891 * key cannot be found. The array should be freed with g_strfreev().
1896 g_key_file_get_string_list (GKeyFile *key_file,
1897 const gchar *group_name,
1902 GError *key_file_error = NULL;
1903 gchar *value, *string_value, **values;
1905 GSList *p, *pieces = NULL;
1907 g_return_val_if_fail (key_file != NULL, NULL);
1908 g_return_val_if_fail (group_name != NULL, NULL);
1909 g_return_val_if_fail (key != NULL, NULL);
1914 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1918 g_propagate_error (error, key_file_error);
1922 if (!g_utf8_validate (value, -1, NULL))
1924 gchar *value_utf8 = _g_utf8_make_valid (value);
1925 g_set_error (error, G_KEY_FILE_ERROR,
1926 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1927 _("Key file contains key '%s' with value '%s' "
1928 "which is not UTF-8"), key, value_utf8);
1929 g_free (value_utf8);
1935 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1937 g_free (string_value);
1941 if (g_error_matches (key_file_error,
1943 G_KEY_FILE_ERROR_INVALID_VALUE))
1945 g_set_error (error, G_KEY_FILE_ERROR,
1946 G_KEY_FILE_ERROR_INVALID_VALUE,
1947 _("Key file contains key '%s' "
1948 "which has a value that cannot be interpreted."),
1950 g_error_free (key_file_error);
1953 g_propagate_error (error, key_file_error);
1955 g_slist_free_full (pieces, g_free);
1959 len = g_slist_length (pieces);
1960 values = g_new (gchar *, len + 1);
1961 for (p = pieces, i = 0; p; p = p->next)
1962 values[i++] = p->data;
1965 g_slist_free (pieces);
1974 * g_key_file_set_string_list:
1975 * @key_file: a #GKeyFile
1976 * @group_name: a group name
1978 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1979 * @length: number of string values in @list
1981 * Associates a list of string values for @key under @group_name.
1982 * If @key cannot be found then it is created.
1983 * If @group_name cannot be found then it is created.
1988 g_key_file_set_string_list (GKeyFile *key_file,
1989 const gchar *group_name,
1991 const gchar * const list[],
1994 GString *value_list;
1997 g_return_if_fail (key_file != NULL);
1998 g_return_if_fail (list != NULL || length == 0);
2000 value_list = g_string_sized_new (length * 128);
2001 for (i = 0; i < length && list[i] != NULL; i++)
2005 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2006 g_string_append (value_list, value);
2007 g_string_append_c (value_list, key_file->list_separator);
2012 g_key_file_set_value (key_file, group_name, key, value_list->str);
2013 g_string_free (value_list, TRUE);
2017 * g_key_file_set_locale_string:
2018 * @key_file: a #GKeyFile
2019 * @group_name: a group name
2021 * @locale: a locale identifier
2024 * Associates a string value for @key and @locale under @group_name.
2025 * If the translation for @key cannot be found then it is created.
2030 g_key_file_set_locale_string (GKeyFile *key_file,
2031 const gchar *group_name,
2033 const gchar *locale,
2034 const gchar *string)
2036 gchar *full_key, *value;
2038 g_return_if_fail (key_file != NULL);
2039 g_return_if_fail (key != NULL);
2040 g_return_if_fail (locale != NULL);
2041 g_return_if_fail (string != NULL);
2043 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2044 full_key = g_strdup_printf ("%s[%s]", key, locale);
2045 g_key_file_set_value (key_file, group_name, full_key, value);
2051 * g_key_file_get_locale_string:
2052 * @key_file: a #GKeyFile
2053 * @group_name: a group name
2055 * @locale: (allow-none): a locale identifier or %NULL
2056 * @error: return location for a #GError, or %NULL
2058 * Returns the value associated with @key under @group_name
2059 * translated in the given @locale if available. If @locale is
2060 * %NULL then the current locale is assumed.
2062 * If @key cannot be found then %NULL is returned and @error is set
2063 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2064 * with @key cannot be interpreted or no suitable translation can
2065 * be found then the untranslated value is returned.
2067 * Return value: a newly allocated string or %NULL if the specified
2068 * key cannot be found.
2073 g_key_file_get_locale_string (GKeyFile *key_file,
2074 const gchar *group_name,
2076 const gchar *locale,
2079 gchar *candidate_key, *translated_value;
2080 GError *key_file_error;
2082 gboolean free_languages = FALSE;
2085 g_return_val_if_fail (key_file != NULL, NULL);
2086 g_return_val_if_fail (group_name != NULL, NULL);
2087 g_return_val_if_fail (key != NULL, NULL);
2089 candidate_key = NULL;
2090 translated_value = NULL;
2091 key_file_error = NULL;
2095 languages = g_get_locale_variants (locale);
2096 free_languages = TRUE;
2100 languages = (gchar **) g_get_language_names ();
2101 free_languages = FALSE;
2104 for (i = 0; languages[i]; i++)
2106 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2108 translated_value = g_key_file_get_string (key_file,
2110 candidate_key, NULL);
2111 g_free (candidate_key);
2113 if (translated_value)
2116 g_free (translated_value);
2117 translated_value = NULL;
2120 /* Fallback to untranslated key
2122 if (!translated_value)
2124 translated_value = g_key_file_get_string (key_file, group_name, key,
2127 if (!translated_value)
2128 g_propagate_error (error, key_file_error);
2132 g_strfreev (languages);
2134 return translated_value;
2138 * g_key_file_get_locale_string_list:
2139 * @key_file: a #GKeyFile
2140 * @group_name: a group name
2142 * @locale: (allow-none): a locale identifier or %NULL
2143 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2144 * @error: return location for a #GError or %NULL
2146 * Returns the values associated with @key under @group_name
2147 * translated in the given @locale if available. If @locale is
2148 * %NULL then the current locale is assumed.
2150 * If @key cannot be found then %NULL is returned and @error is set
2151 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2152 * with @key cannot be interpreted or no suitable translations
2153 * can be found then the untranslated values are returned. The
2154 * returned array is %NULL-terminated, so @length may optionally
2157 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2158 * or %NULL if the key isn't found. The string array should be freed
2159 * with g_strfreev().
2164 g_key_file_get_locale_string_list (GKeyFile *key_file,
2165 const gchar *group_name,
2167 const gchar *locale,
2171 GError *key_file_error;
2172 gchar **values, *value;
2173 char list_separator[2];
2176 g_return_val_if_fail (key_file != NULL, NULL);
2177 g_return_val_if_fail (group_name != NULL, NULL);
2178 g_return_val_if_fail (key != NULL, NULL);
2180 key_file_error = NULL;
2182 value = g_key_file_get_locale_string (key_file, group_name,
2187 g_propagate_error (error, key_file_error);
2196 len = strlen (value);
2197 if (value[len - 1] == key_file->list_separator)
2198 value[len - 1] = '\0';
2200 list_separator[0] = key_file->list_separator;
2201 list_separator[1] = '\0';
2202 values = g_strsplit (value, list_separator, 0);
2207 *length = g_strv_length (values);
2213 * g_key_file_set_locale_string_list:
2214 * @key_file: a #GKeyFile
2215 * @group_name: a group name
2217 * @locale: a locale identifier
2218 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2219 * @length: the length of @list
2221 * Associates a list of string values for @key and @locale under
2222 * @group_name. If the translation for @key cannot be found then
2228 g_key_file_set_locale_string_list (GKeyFile *key_file,
2229 const gchar *group_name,
2231 const gchar *locale,
2232 const gchar * const list[],
2235 GString *value_list;
2239 g_return_if_fail (key_file != NULL);
2240 g_return_if_fail (key != NULL);
2241 g_return_if_fail (locale != NULL);
2242 g_return_if_fail (length != 0);
2244 value_list = g_string_sized_new (length * 128);
2245 for (i = 0; i < length && list[i] != NULL; i++)
2249 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2250 g_string_append (value_list, value);
2251 g_string_append_c (value_list, key_file->list_separator);
2256 full_key = g_strdup_printf ("%s[%s]", key, locale);
2257 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2259 g_string_free (value_list, TRUE);
2263 * g_key_file_get_boolean:
2264 * @key_file: a #GKeyFile
2265 * @group_name: a group name
2267 * @error: return location for a #GError
2269 * Returns the value associated with @key under @group_name as a
2272 * If @key cannot be found then %FALSE is returned and @error is set
2273 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2274 * associated with @key cannot be interpreted as a boolean then %FALSE
2275 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2277 * Return value: the value associated with the key as a boolean,
2278 * or %FALSE if the key was not found or could not be parsed.
2283 g_key_file_get_boolean (GKeyFile *key_file,
2284 const gchar *group_name,
2288 GError *key_file_error = NULL;
2290 gboolean bool_value;
2292 g_return_val_if_fail (key_file != NULL, FALSE);
2293 g_return_val_if_fail (group_name != NULL, FALSE);
2294 g_return_val_if_fail (key != NULL, FALSE);
2296 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2300 g_propagate_error (error, key_file_error);
2304 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2310 if (g_error_matches (key_file_error,
2312 G_KEY_FILE_ERROR_INVALID_VALUE))
2314 g_set_error (error, G_KEY_FILE_ERROR,
2315 G_KEY_FILE_ERROR_INVALID_VALUE,
2316 _("Key file contains key '%s' "
2317 "which has a value that cannot be interpreted."),
2319 g_error_free (key_file_error);
2322 g_propagate_error (error, key_file_error);
2329 * g_key_file_set_boolean:
2330 * @key_file: a #GKeyFile
2331 * @group_name: a group name
2333 * @value: %TRUE or %FALSE
2335 * Associates a new boolean value with @key under @group_name.
2336 * If @key cannot be found then it is created.
2341 g_key_file_set_boolean (GKeyFile *key_file,
2342 const gchar *group_name,
2348 g_return_if_fail (key_file != NULL);
2350 result = g_key_file_parse_boolean_as_value (key_file, value);
2351 g_key_file_set_value (key_file, group_name, key, result);
2356 * g_key_file_get_boolean_list:
2357 * @key_file: a #GKeyFile
2358 * @group_name: a group name
2360 * @length: (out): the number of booleans returned
2361 * @error: return location for a #GError
2363 * Returns the values associated with @key under @group_name as
2366 * If @key cannot be found then %NULL is returned and @error is set to
2367 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2368 * with @key cannot be interpreted as booleans then %NULL is returned
2369 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2371 * Return value: (array length=length) (element-type gboolean) (transfer container):
2372 * the values associated with the key as a list of booleans, or %NULL if the
2373 * key was not found or could not be parsed. The returned list of booleans
2374 * should be freed with g_free() when no longer needed.
2379 g_key_file_get_boolean_list (GKeyFile *key_file,
2380 const gchar *group_name,
2385 GError *key_file_error;
2387 gboolean *bool_values;
2390 g_return_val_if_fail (key_file != NULL, NULL);
2391 g_return_val_if_fail (group_name != NULL, NULL);
2392 g_return_val_if_fail (key != NULL, NULL);
2397 key_file_error = NULL;
2399 values = g_key_file_get_string_list (key_file, group_name, key,
2400 &num_bools, &key_file_error);
2403 g_propagate_error (error, key_file_error);
2408 bool_values = g_new (gboolean, num_bools);
2410 for (i = 0; i < num_bools; i++)
2412 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2418 g_propagate_error (error, key_file_error);
2419 g_strfreev (values);
2420 g_free (bool_values);
2425 g_strfreev (values);
2428 *length = num_bools;
2434 * g_key_file_set_boolean_list:
2435 * @key_file: a #GKeyFile
2436 * @group_name: a group name
2438 * @list: (array length=length): an array of boolean values
2439 * @length: length of @list
2441 * Associates a list of boolean values with @key under @group_name.
2442 * If @key cannot be found then it is created.
2443 * If @group_name is %NULL, the start_group is used.
2448 g_key_file_set_boolean_list (GKeyFile *key_file,
2449 const gchar *group_name,
2454 GString *value_list;
2457 g_return_if_fail (key_file != NULL);
2458 g_return_if_fail (list != NULL);
2460 value_list = g_string_sized_new (length * 8);
2461 for (i = 0; i < length; i++)
2465 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2467 g_string_append (value_list, value);
2468 g_string_append_c (value_list, key_file->list_separator);
2473 g_key_file_set_value (key_file, group_name, key, value_list->str);
2474 g_string_free (value_list, TRUE);
2478 * g_key_file_get_integer:
2479 * @key_file: a #GKeyFile
2480 * @group_name: a group name
2482 * @error: return location for a #GError
2484 * Returns the value associated with @key under @group_name as an
2487 * If @key cannot be found then 0 is returned and @error is set to
2488 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2489 * with @key cannot be interpreted as an integer then 0 is returned
2490 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2492 * Return value: the value associated with the key as an integer, or
2493 * 0 if the key was not found or could not be parsed.
2498 g_key_file_get_integer (GKeyFile *key_file,
2499 const gchar *group_name,
2503 GError *key_file_error;
2507 g_return_val_if_fail (key_file != NULL, -1);
2508 g_return_val_if_fail (group_name != NULL, -1);
2509 g_return_val_if_fail (key != NULL, -1);
2511 key_file_error = NULL;
2513 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2517 g_propagate_error (error, key_file_error);
2521 int_value = g_key_file_parse_value_as_integer (key_file, value,
2527 if (g_error_matches (key_file_error,
2529 G_KEY_FILE_ERROR_INVALID_VALUE))
2531 g_set_error (error, G_KEY_FILE_ERROR,
2532 G_KEY_FILE_ERROR_INVALID_VALUE,
2533 _("Key file contains key '%s' in group '%s' "
2534 "which has a value that cannot be interpreted."),
2536 g_error_free (key_file_error);
2539 g_propagate_error (error, key_file_error);
2546 * g_key_file_set_integer:
2547 * @key_file: a #GKeyFile
2548 * @group_name: a group name
2550 * @value: an integer value
2552 * Associates a new integer value with @key under @group_name.
2553 * If @key cannot be found then it is created.
2558 g_key_file_set_integer (GKeyFile *key_file,
2559 const gchar *group_name,
2565 g_return_if_fail (key_file != NULL);
2567 result = g_key_file_parse_integer_as_value (key_file, value);
2568 g_key_file_set_value (key_file, group_name, key, result);
2573 * g_key_file_get_int64:
2574 * @key_file: a non-%NULL #GKeyFile
2575 * @group_name: a non-%NULL group name
2576 * @key: a non-%NULL key
2577 * @error: return location for a #GError
2579 * Returns the value associated with @key under @group_name as a signed
2580 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2581 * 64-bit results without truncation.
2583 * Returns: the value associated with the key as a signed 64-bit integer, or
2584 * 0 if the key was not found or could not be parsed.
2589 g_key_file_get_int64 (GKeyFile *key_file,
2590 const gchar *group_name,
2597 g_return_val_if_fail (key_file != NULL, -1);
2598 g_return_val_if_fail (group_name != NULL, -1);
2599 g_return_val_if_fail (key != NULL, -1);
2601 s = g_key_file_get_value (key_file, group_name, key, error);
2606 v = g_ascii_strtoll (s, &end, 10);
2608 if (*s == '\0' || *end != '\0')
2610 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2611 _("Key '%s' in group '%s' has value '%s' "
2612 "where %s was expected"),
2613 key, group_name, s, "int64");
2623 * g_key_file_set_int64:
2624 * @key_file: a #GKeyFile
2625 * @group_name: a group name
2627 * @value: an integer value
2629 * Associates a new integer value with @key under @group_name.
2630 * If @key cannot be found then it is created.
2635 g_key_file_set_int64 (GKeyFile *key_file,
2636 const gchar *group_name,
2642 g_return_if_fail (key_file != NULL);
2644 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2645 g_key_file_set_value (key_file, group_name, key, result);
2650 * g_key_file_get_uint64:
2651 * @key_file: a non-%NULL #GKeyFile
2652 * @group_name: a non-%NULL group name
2653 * @key: a non-%NULL key
2654 * @error: return location for a #GError
2656 * Returns the value associated with @key under @group_name as an unsigned
2657 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2658 * large positive results without truncation.
2660 * Returns: the value associated with the key as an unsigned 64-bit integer,
2661 * or 0 if the key was not found or could not be parsed.
2666 g_key_file_get_uint64 (GKeyFile *key_file,
2667 const gchar *group_name,
2674 g_return_val_if_fail (key_file != NULL, -1);
2675 g_return_val_if_fail (group_name != NULL, -1);
2676 g_return_val_if_fail (key != NULL, -1);
2678 s = g_key_file_get_value (key_file, group_name, key, error);
2683 v = g_ascii_strtoull (s, &end, 10);
2685 if (*s == '\0' || *end != '\0')
2687 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2688 _("Key '%s' in group '%s' has value '%s' "
2689 "where %s was expected"),
2690 key, group_name, s, "uint64");
2700 * g_key_file_set_uint64:
2701 * @key_file: a #GKeyFile
2702 * @group_name: a group name
2704 * @value: an integer value
2706 * Associates a new integer value with @key under @group_name.
2707 * If @key cannot be found then it is created.
2712 g_key_file_set_uint64 (GKeyFile *key_file,
2713 const gchar *group_name,
2719 g_return_if_fail (key_file != NULL);
2721 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2722 g_key_file_set_value (key_file, group_name, key, result);
2727 * g_key_file_get_integer_list:
2728 * @key_file: a #GKeyFile
2729 * @group_name: a group name
2731 * @length: (out): the number of integers returned
2732 * @error: return location for a #GError
2734 * Returns the values associated with @key under @group_name as
2737 * If @key cannot be found then %NULL is returned and @error is set to
2738 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2739 * with @key cannot be interpreted as integers then %NULL is returned
2740 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2742 * Return value: (array length=length) (element-type gint) (transfer container):
2743 * the values associated with the key as a list of integers, or %NULL if
2744 * the key was not found or could not be parsed. The returned list of
2745 * integers should be freed with g_free() when no longer needed.
2750 g_key_file_get_integer_list (GKeyFile *key_file,
2751 const gchar *group_name,
2756 GError *key_file_error = NULL;
2761 g_return_val_if_fail (key_file != NULL, NULL);
2762 g_return_val_if_fail (group_name != NULL, NULL);
2763 g_return_val_if_fail (key != NULL, NULL);
2768 values = g_key_file_get_string_list (key_file, group_name, key,
2769 &num_ints, &key_file_error);
2772 g_propagate_error (error, key_file_error);
2777 int_values = g_new (gint, num_ints);
2779 for (i = 0; i < num_ints; i++)
2781 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2787 g_propagate_error (error, key_file_error);
2788 g_strfreev (values);
2789 g_free (int_values);
2794 g_strfreev (values);
2803 * g_key_file_set_integer_list:
2804 * @key_file: a #GKeyFile
2805 * @group_name: a group name
2807 * @list: (array length=length): an array of integer values
2808 * @length: number of integer values in @list
2810 * Associates a list of integer values with @key under @group_name.
2811 * If @key cannot be found then it is created.
2816 g_key_file_set_integer_list (GKeyFile *key_file,
2817 const gchar *group_name,
2825 g_return_if_fail (key_file != NULL);
2826 g_return_if_fail (list != NULL);
2828 values = g_string_sized_new (length * 16);
2829 for (i = 0; i < length; i++)
2833 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2835 g_string_append (values, value);
2836 g_string_append_c (values, key_file->list_separator);
2841 g_key_file_set_value (key_file, group_name, key, values->str);
2842 g_string_free (values, TRUE);
2846 * g_key_file_get_double:
2847 * @key_file: a #GKeyFile
2848 * @group_name: a group name
2850 * @error: return location for a #GError
2852 * Returns the value associated with @key under @group_name as a
2853 * double. If @group_name is %NULL, the start_group is used.
2855 * If @key cannot be found then 0.0 is returned and @error is set to
2856 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2857 * with @key cannot be interpreted as a double then 0.0 is returned
2858 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2860 * Return value: the value associated with the key as a double, or
2861 * 0.0 if the key was not found or could not be parsed.
2866 g_key_file_get_double (GKeyFile *key_file,
2867 const gchar *group_name,
2871 GError *key_file_error;
2873 gdouble double_value;
2875 g_return_val_if_fail (key_file != NULL, -1);
2876 g_return_val_if_fail (group_name != NULL, -1);
2877 g_return_val_if_fail (key != NULL, -1);
2879 key_file_error = NULL;
2881 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2885 g_propagate_error (error, key_file_error);
2889 double_value = g_key_file_parse_value_as_double (key_file, value,
2895 if (g_error_matches (key_file_error,
2897 G_KEY_FILE_ERROR_INVALID_VALUE))
2899 g_set_error (error, G_KEY_FILE_ERROR,
2900 G_KEY_FILE_ERROR_INVALID_VALUE,
2901 _("Key file contains key '%s' in group '%s' "
2902 "which has a value that cannot be interpreted."),
2904 g_error_free (key_file_error);
2907 g_propagate_error (error, key_file_error);
2910 return double_value;
2914 * g_key_file_set_double:
2915 * @key_file: a #GKeyFile
2916 * @group_name: a group name
2918 * @value: an double value
2920 * Associates a new double value with @key under @group_name.
2921 * If @key cannot be found then it is created.
2926 g_key_file_set_double (GKeyFile *key_file,
2927 const gchar *group_name,
2931 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2933 g_return_if_fail (key_file != NULL);
2935 g_ascii_dtostr (result, sizeof (result), value);
2936 g_key_file_set_value (key_file, group_name, key, result);
2940 * g_key_file_get_double_list:
2941 * @key_file: a #GKeyFile
2942 * @group_name: a group name
2944 * @length: (out): the number of doubles returned
2945 * @error: return location for a #GError
2947 * Returns the values associated with @key under @group_name as
2950 * If @key cannot be found then %NULL is returned and @error is set to
2951 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2952 * with @key cannot be interpreted as doubles then %NULL is returned
2953 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2955 * Return value: (array length=length) (element-type gdouble) (transfer container):
2956 * the values associated with the key as a list of doubles, or %NULL if the
2957 * key was not found or could not be parsed. The returned list of doubles
2958 * should be freed with g_free() when no longer needed.
2963 g_key_file_get_double_list (GKeyFile *key_file,
2964 const gchar *group_name,
2969 GError *key_file_error = NULL;
2971 gdouble *double_values;
2972 gsize i, num_doubles;
2974 g_return_val_if_fail (key_file != NULL, NULL);
2975 g_return_val_if_fail (group_name != NULL, NULL);
2976 g_return_val_if_fail (key != NULL, NULL);
2981 values = g_key_file_get_string_list (key_file, group_name, key,
2982 &num_doubles, &key_file_error);
2985 g_propagate_error (error, key_file_error);
2990 double_values = g_new (gdouble, num_doubles);
2992 for (i = 0; i < num_doubles; i++)
2994 double_values[i] = g_key_file_parse_value_as_double (key_file,
3000 g_propagate_error (error, key_file_error);
3001 g_strfreev (values);
3002 g_free (double_values);
3007 g_strfreev (values);
3010 *length = num_doubles;
3012 return double_values;
3016 * g_key_file_set_double_list:
3017 * @key_file: a #GKeyFile
3018 * @group_name: a group name
3020 * @list: (array length=length): an array of double values
3021 * @length: number of double values in @list
3023 * Associates a list of double values with @key under
3024 * @group_name. If @key cannot be found then it is created.
3029 g_key_file_set_double_list (GKeyFile *key_file,
3030 const gchar *group_name,
3038 g_return_if_fail (key_file != NULL);
3039 g_return_if_fail (list != NULL);
3041 values = g_string_sized_new (length * 16);
3042 for (i = 0; i < length; i++)
3044 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3046 g_ascii_dtostr( result, sizeof (result), list[i] );
3048 g_string_append (values, result);
3049 g_string_append_c (values, key_file->list_separator);
3052 g_key_file_set_value (key_file, group_name, key, values->str);
3053 g_string_free (values, TRUE);
3057 g_key_file_set_key_comment (GKeyFile *key_file,
3058 const gchar *group_name,
3060 const gchar *comment,
3063 GKeyFileGroup *group;
3064 GKeyFileKeyValuePair *pair;
3065 GList *key_node, *comment_node, *tmp;
3067 group = g_key_file_lookup_group (key_file, group_name);
3070 g_set_error (error, G_KEY_FILE_ERROR,
3071 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3072 _("Key file does not have group '%s'"),
3073 group_name ? group_name : "(null)");
3078 /* First find the key the comments are supposed to be
3081 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3083 if (key_node == NULL)
3085 g_set_error (error, G_KEY_FILE_ERROR,
3086 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3087 _("Key file does not have key '%s' in group '%s'"),
3092 /* Then find all the comments already associated with the
3095 tmp = key_node->next;
3098 pair = (GKeyFileKeyValuePair *) tmp->data;
3100 if (pair->key != NULL)
3105 g_key_file_remove_key_value_pair_node (key_file, group,
3109 if (comment == NULL)
3112 /* Now we can add our new comment
3114 pair = g_slice_new (GKeyFileKeyValuePair);
3116 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3118 key_node = g_list_insert (key_node, pair, 1);
3124 g_key_file_set_group_comment (GKeyFile *key_file,
3125 const gchar *group_name,
3126 const gchar *comment,
3129 GKeyFileGroup *group;
3131 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3133 group = g_key_file_lookup_group (key_file, group_name);
3136 g_set_error (error, G_KEY_FILE_ERROR,
3137 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3138 _("Key file does not have group '%s'"),
3139 group_name ? group_name : "(null)");
3144 /* First remove any existing comment
3148 g_key_file_key_value_pair_free (group->comment);
3149 group->comment = NULL;
3152 if (comment == NULL)
3155 /* Now we can add our new comment
3157 group->comment = g_slice_new (GKeyFileKeyValuePair);
3158 group->comment->key = NULL;
3159 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3165 g_key_file_set_top_comment (GKeyFile *key_file,
3166 const gchar *comment,
3170 GKeyFileGroup *group;
3171 GKeyFileKeyValuePair *pair;
3173 /* The last group in the list should be the top (comments only)
3176 g_warn_if_fail (key_file->groups != NULL);
3177 group_node = g_list_last (key_file->groups);
3178 group = (GKeyFileGroup *) group_node->data;
3179 g_warn_if_fail (group->name == NULL);
3181 /* Note all keys must be comments at the top of
3182 * the file, so we can just free it all.
3184 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3185 group->key_value_pairs = NULL;
3187 if (comment == NULL)
3190 pair = g_slice_new (GKeyFileKeyValuePair);
3192 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3194 group->key_value_pairs =
3195 g_list_prepend (group->key_value_pairs, pair);
3201 * g_key_file_set_comment:
3202 * @key_file: a #GKeyFile
3203 * @group_name: (allow-none): a group name, or %NULL
3204 * @key: (allow-none): a key
3205 * @comment: a comment
3206 * @error: return location for a #GError
3208 * Places a comment above @key from @group_name.
3209 * If @key is %NULL then @comment will be written above @group_name.
3210 * If both @key and @group_name are %NULL, then @comment will be
3211 * written above the first group in the file.
3213 * Returns: %TRUE if the comment was written, %FALSE otherwise
3218 g_key_file_set_comment (GKeyFile *key_file,
3219 const gchar *group_name,
3221 const gchar *comment,
3224 g_return_val_if_fail (key_file != NULL, FALSE);
3226 if (group_name != NULL && key != NULL)
3228 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3231 else if (group_name != NULL)
3233 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3238 if (!g_key_file_set_top_comment (key_file, comment, error))
3246 g_key_file_get_key_comment (GKeyFile *key_file,
3247 const gchar *group_name,
3251 GKeyFileGroup *group;
3252 GKeyFileKeyValuePair *pair;
3253 GList *key_node, *tmp;
3257 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3259 group = g_key_file_lookup_group (key_file, group_name);
3262 g_set_error (error, G_KEY_FILE_ERROR,
3263 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3264 _("Key file does not have group '%s'"),
3265 group_name ? group_name : "(null)");
3270 /* First find the key the comments are supposed to be
3273 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3275 if (key_node == NULL)
3277 g_set_error (error, G_KEY_FILE_ERROR,
3278 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3279 _("Key file does not have key '%s' in group '%s'"),
3286 /* Then find all the comments already associated with the
3287 * key and concatentate them.
3289 tmp = key_node->next;
3290 if (!key_node->next)
3293 pair = (GKeyFileKeyValuePair *) tmp->data;
3294 if (pair->key != NULL)
3299 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3301 if (pair->key != NULL)
3307 while (tmp != key_node)
3309 pair = (GKeyFileKeyValuePair *) tmp->data;
3312 string = g_string_sized_new (512);
3314 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3315 g_string_append (string, comment);
3323 comment = string->str;
3324 g_string_free (string, FALSE);
3333 get_group_comment (GKeyFile *key_file,
3334 GKeyFileGroup *group,
3343 tmp = group->key_value_pairs;
3346 GKeyFileKeyValuePair *pair;
3348 pair = (GKeyFileKeyValuePair *) tmp->data;
3350 if (pair->key != NULL)
3356 if (tmp->next == NULL)
3364 GKeyFileKeyValuePair *pair;
3366 pair = (GKeyFileKeyValuePair *) tmp->data;
3369 string = g_string_sized_new (512);
3371 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3372 g_string_append (string, comment);
3379 return g_string_free (string, FALSE);
3385 g_key_file_get_group_comment (GKeyFile *key_file,
3386 const gchar *group_name,
3390 GKeyFileGroup *group;
3392 group = g_key_file_lookup_group (key_file, group_name);
3395 g_set_error (error, G_KEY_FILE_ERROR,
3396 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3397 _("Key file does not have group '%s'"),
3398 group_name ? group_name : "(null)");
3404 return g_strdup (group->comment->value);
3406 group_node = g_key_file_lookup_group_node (key_file, group_name);
3407 group_node = group_node->next;
3408 group = (GKeyFileGroup *)group_node->data;
3409 return get_group_comment (key_file, group, error);
3413 g_key_file_get_top_comment (GKeyFile *key_file,
3417 GKeyFileGroup *group;
3419 /* The last group in the list should be the top (comments only)
3422 g_warn_if_fail (key_file->groups != NULL);
3423 group_node = g_list_last (key_file->groups);
3424 group = (GKeyFileGroup *) group_node->data;
3425 g_warn_if_fail (group->name == NULL);
3427 return get_group_comment (key_file, group, error);
3431 * g_key_file_get_comment:
3432 * @key_file: a #GKeyFile
3433 * @group_name: (allow-none): a group name, or %NULL
3435 * @error: return location for a #GError
3437 * Retrieves a comment above @key from @group_name.
3438 * If @key is %NULL then @comment will be read from above
3439 * @group_name. If both @key and @group_name are %NULL, then
3440 * @comment will be read from above the first group in the file.
3442 * Returns: a comment that should be freed with g_free()
3447 g_key_file_get_comment (GKeyFile *key_file,
3448 const gchar *group_name,
3452 g_return_val_if_fail (key_file != NULL, NULL);
3454 if (group_name != NULL && key != NULL)
3455 return g_key_file_get_key_comment (key_file, group_name, key, error);
3456 else if (group_name != NULL)
3457 return g_key_file_get_group_comment (key_file, group_name, error);
3459 return g_key_file_get_top_comment (key_file, error);
3463 * g_key_file_remove_comment:
3464 * @key_file: a #GKeyFile
3465 * @group_name: (allow-none): a group name, or %NULL
3466 * @key: (allow-none): a key
3467 * @error: return location for a #GError
3469 * Removes a comment above @key from @group_name.
3470 * If @key is %NULL then @comment will be removed above @group_name.
3471 * If both @key and @group_name are %NULL, then @comment will
3472 * be removed above the first group in the file.
3474 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3480 g_key_file_remove_comment (GKeyFile *key_file,
3481 const gchar *group_name,
3485 g_return_val_if_fail (key_file != NULL, FALSE);
3487 if (group_name != NULL && key != NULL)
3488 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3489 else if (group_name != NULL)
3490 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3492 return g_key_file_set_top_comment (key_file, NULL, error);
3496 * g_key_file_has_group:
3497 * @key_file: a #GKeyFile
3498 * @group_name: a group name
3500 * Looks whether the key file has the group @group_name.
3502 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3507 g_key_file_has_group (GKeyFile *key_file,
3508 const gchar *group_name)
3510 g_return_val_if_fail (key_file != NULL, FALSE);
3511 g_return_val_if_fail (group_name != NULL, FALSE);
3513 return g_key_file_lookup_group (key_file, group_name) != NULL;
3516 /* This code remains from a historical attempt to add a new public API
3517 * which respects the GError rules.
3520 g_key_file_has_key_full (GKeyFile *key_file,
3521 const gchar *group_name,
3526 GKeyFileKeyValuePair *pair;
3527 GKeyFileGroup *group;
3529 g_return_val_if_fail (key_file != NULL, FALSE);
3530 g_return_val_if_fail (group_name != NULL, FALSE);
3531 g_return_val_if_fail (key != NULL, FALSE);
3533 group = g_key_file_lookup_group (key_file, group_name);
3537 g_set_error (error, G_KEY_FILE_ERROR,
3538 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3539 _("Key file does not have group '%s'"),
3540 group_name ? group_name : "(null)");
3545 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3548 *has_key = pair != NULL;
3553 * g_key_file_has_key: (skip)
3554 * @key_file: a #GKeyFile
3555 * @group_name: a group name
3557 * @error: return location for a #GError
3559 * Looks whether the key file has the key @key in the group
3562 * Note that this function does not follow the rules for #GError strictly;
3563 * the return value both carries meaning and signals an error. To use
3564 * this function, you must pass a #GError pointer in @error, and check
3565 * whether it is not %NULL to see if an error occurred.
3567 * Language bindings should use g_key_file_get_value() to test whether
3568 * or not a key exists.
3570 * Return value: %TRUE if @key is a part of @group_name, %FALSE otherwise
3575 g_key_file_has_key (GKeyFile *key_file,
3576 const gchar *group_name,
3580 GError *temp_error = NULL;
3583 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3589 g_propagate_error (error, temp_error);
3595 g_key_file_add_group (GKeyFile *key_file,
3596 const gchar *group_name)
3598 GKeyFileGroup *group;
3600 g_return_if_fail (key_file != NULL);
3601 g_return_if_fail (g_key_file_is_group_name (group_name));
3603 group = g_key_file_lookup_group (key_file, group_name);
3606 key_file->current_group = group;
3610 group = g_slice_new0 (GKeyFileGroup);
3611 group->name = g_strdup (group_name);
3612 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3613 key_file->groups = g_list_prepend (key_file->groups, group);
3614 key_file->current_group = group;
3616 if (key_file->start_group == NULL)
3617 key_file->start_group = group;
3619 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3623 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3628 g_free (pair->value);
3629 g_slice_free (GKeyFileKeyValuePair, pair);
3633 /* Be careful not to call this function on a node with data in the
3634 * lookup map without removing it from the lookup map, first.
3636 * Some current cases where this warning is not a concern are
3638 * - the node being removed is a comment node
3639 * - the entire lookup map is getting destroyed soon after
3643 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3644 GKeyFileGroup *group,
3648 GKeyFileKeyValuePair *pair;
3650 pair = (GKeyFileKeyValuePair *) pair_node->data;
3652 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3654 g_warn_if_fail (pair->value != NULL);
3656 g_key_file_key_value_pair_free (pair);
3658 g_list_free_1 (pair_node);
3662 g_key_file_remove_group_node (GKeyFile *key_file,
3665 GKeyFileGroup *group;
3668 group = (GKeyFileGroup *) group_node->data;
3671 g_hash_table_remove (key_file->group_hash, group->name);
3673 /* If the current group gets deleted make the current group the last
3676 if (key_file->current_group == group)
3678 /* groups should always contain at least the top comment group,
3679 * unless g_key_file_clear has been called
3681 if (key_file->groups)
3682 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3684 key_file->current_group = NULL;
3687 /* If the start group gets deleted make the start group the first
3690 if (key_file->start_group == group)
3692 tmp = g_list_last (key_file->groups);
3695 if (tmp != group_node &&
3696 ((GKeyFileGroup *) tmp->data)->name != NULL)
3703 key_file->start_group = (GKeyFileGroup *) tmp->data;
3705 key_file->start_group = NULL;
3708 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3710 tmp = group->key_value_pairs;
3717 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3720 g_warn_if_fail (group->key_value_pairs == NULL);
3724 g_key_file_key_value_pair_free (group->comment);
3725 group->comment = NULL;
3728 if (group->lookup_map)
3730 g_hash_table_destroy (group->lookup_map);
3731 group->lookup_map = NULL;
3734 g_free ((gchar *) group->name);
3735 g_slice_free (GKeyFileGroup, group);
3736 g_list_free_1 (group_node);
3740 * g_key_file_remove_group:
3741 * @key_file: a #GKeyFile
3742 * @group_name: a group name
3743 * @error: return location for a #GError or %NULL
3745 * Removes the specified group, @group_name,
3746 * from the key file.
3748 * Returns: %TRUE if the group was removed, %FALSE otherwise
3753 g_key_file_remove_group (GKeyFile *key_file,
3754 const gchar *group_name,
3759 g_return_val_if_fail (key_file != NULL, FALSE);
3760 g_return_val_if_fail (group_name != NULL, FALSE);
3762 group_node = g_key_file_lookup_group_node (key_file, group_name);
3766 g_set_error (error, G_KEY_FILE_ERROR,
3767 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3768 _("Key file does not have group '%s'"),
3773 g_key_file_remove_group_node (key_file, group_node);
3779 g_key_file_add_key_value_pair (GKeyFile *key_file,
3780 GKeyFileGroup *group,
3781 GKeyFileKeyValuePair *pair)
3783 g_hash_table_replace (group->lookup_map, pair->key, pair);
3784 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3788 g_key_file_add_key (GKeyFile *key_file,
3789 GKeyFileGroup *group,
3793 GKeyFileKeyValuePair *pair;
3795 pair = g_slice_new (GKeyFileKeyValuePair);
3796 pair->key = g_strdup (key);
3797 pair->value = g_strdup (value);
3799 g_key_file_add_key_value_pair (key_file, group, pair);
3803 * g_key_file_remove_key:
3804 * @key_file: a #GKeyFile
3805 * @group_name: a group name
3806 * @key: a key name to remove
3807 * @error: return location for a #GError or %NULL
3809 * Removes @key in @group_name from the key file.
3811 * Returns: %TRUE if the key was removed, %FALSE otherwise
3816 g_key_file_remove_key (GKeyFile *key_file,
3817 const gchar *group_name,
3821 GKeyFileGroup *group;
3822 GKeyFileKeyValuePair *pair;
3824 g_return_val_if_fail (key_file != NULL, FALSE);
3825 g_return_val_if_fail (group_name != NULL, FALSE);
3826 g_return_val_if_fail (key != NULL, FALSE);
3830 group = g_key_file_lookup_group (key_file, group_name);
3833 g_set_error (error, G_KEY_FILE_ERROR,
3834 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3835 _("Key file does not have group '%s'"),
3836 group_name ? group_name : "(null)");
3840 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3844 g_set_error (error, G_KEY_FILE_ERROR,
3845 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3846 _("Key file does not have key '%s' in group '%s'"),
3851 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3852 g_hash_table_remove (group->lookup_map, pair->key);
3853 g_key_file_key_value_pair_free (pair);
3859 g_key_file_lookup_group_node (GKeyFile *key_file,
3860 const gchar *group_name)
3862 GKeyFileGroup *group;
3865 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3867 group = (GKeyFileGroup *) tmp->data;
3869 if (group && group->name && strcmp (group->name, group_name) == 0)
3876 static GKeyFileGroup *
3877 g_key_file_lookup_group (GKeyFile *key_file,
3878 const gchar *group_name)
3880 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3884 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3885 GKeyFileGroup *group,
3890 for (key_node = group->key_value_pairs;
3892 key_node = key_node->next)
3894 GKeyFileKeyValuePair *pair;
3896 pair = (GKeyFileKeyValuePair *) key_node->data;
3898 if (pair->key && strcmp (pair->key, key) == 0)
3905 static GKeyFileKeyValuePair *
3906 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3907 GKeyFileGroup *group,
3910 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3913 /* Lines starting with # or consisting entirely of whitespace are merely
3914 * recorded, not parsed. This function assumes all leading whitespace
3915 * has been stripped.
3918 g_key_file_line_is_comment (const gchar *line)
3920 return (*line == '#' || *line == '\0' || *line == '\n');
3924 g_key_file_is_group_name (const gchar *name)
3931 p = q = (gchar *) name;
3932 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3933 q = g_utf8_find_next_char (q, NULL);
3935 if (*q != '\0' || q == p)
3942 g_key_file_is_key_name (const gchar *name)
3949 p = q = (gchar *) name;
3950 /* We accept a little more than the desktop entry spec says,
3951 * since gnome-vfs uses mime-types as keys in its cache.
3953 while (*q && *q != '=' && *q != '[' && *q != ']')
3954 q = g_utf8_find_next_char (q, NULL);
3956 /* No empty keys, please */
3960 /* We accept spaces in the middle of keys to not break
3961 * existing apps, but we don't tolerate initial or final
3962 * spaces, which would lead to silent corruption when
3963 * rereading the file.
3965 if (*p == ' ' || q[-1] == ' ')
3971 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3972 q = g_utf8_find_next_char (q, NULL);
3986 /* A group in a key file is made up of a starting '[' followed by one
3987 * or more letters making up the group name followed by ']'.
3990 g_key_file_line_is_group (const gchar *line)
4000 while (*p && *p != ']')
4001 p = g_utf8_find_next_char (p, NULL);
4006 /* silently accept whitespace after the ] */
4007 p = g_utf8_find_next_char (p, NULL);
4008 while (*p == ' ' || *p == '\t')
4009 p = g_utf8_find_next_char (p, NULL);
4018 g_key_file_line_is_key_value_pair (const gchar *line)
4022 p = (gchar *) g_utf8_strchr (line, -1, '=');
4027 /* Key must be non-empty
4036 g_key_file_parse_value_as_string (GKeyFile *key_file,
4041 gchar *string_value, *p, *q0, *q;
4043 string_value = g_new (gchar, strlen (value) + 1);
4045 p = (gchar *) value;
4046 q0 = q = string_value;
4076 g_set_error_literal (error, G_KEY_FILE_ERROR,
4077 G_KEY_FILE_ERROR_INVALID_VALUE,
4078 _("Key file contains escape character "
4083 if (pieces && *p == key_file->list_separator)
4084 *q = key_file->list_separator;
4098 g_set_error (error, G_KEY_FILE_ERROR,
4099 G_KEY_FILE_ERROR_INVALID_VALUE,
4100 _("Key file contains invalid escape "
4101 "sequence '%s'"), sequence);
4110 if (pieces && (*p == key_file->list_separator))
4112 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4128 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4129 *pieces = g_slist_reverse (*pieces);
4132 return string_value;
4136 g_key_file_parse_string_as_value (GKeyFile *key_file,
4137 const gchar *string,
4138 gboolean escape_separator)
4140 gchar *value, *p, *q;
4142 gboolean parsing_leading_space;
4144 length = strlen (string) + 1;
4146 /* Worst case would be that every character needs to be escaped.
4147 * In other words every character turns to two characters
4149 value = g_new (gchar, 2 * length);
4151 p = (gchar *) string;
4153 parsing_leading_space = TRUE;
4154 while (p < (string + length - 1))
4156 gchar escaped_character[3] = { '\\', 0, 0 };
4161 if (parsing_leading_space)
4163 escaped_character[1] = 's';
4164 strcpy (q, escaped_character);
4174 if (parsing_leading_space)
4176 escaped_character[1] = 't';
4177 strcpy (q, escaped_character);
4187 escaped_character[1] = 'n';
4188 strcpy (q, escaped_character);
4192 escaped_character[1] = 'r';
4193 strcpy (q, escaped_character);
4197 escaped_character[1] = '\\';
4198 strcpy (q, escaped_character);
4200 parsing_leading_space = FALSE;
4203 if (escape_separator && *p == key_file->list_separator)
4205 escaped_character[1] = key_file->list_separator;
4206 strcpy (q, escaped_character);
4208 parsing_leading_space = TRUE;
4214 parsing_leading_space = FALSE;
4226 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4235 long_value = strtol (value, &eof_int, 10);
4237 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4239 gchar *value_utf8 = _g_utf8_make_valid (value);
4240 g_set_error (error, G_KEY_FILE_ERROR,
4241 G_KEY_FILE_ERROR_INVALID_VALUE,
4242 _("Value '%s' cannot be interpreted "
4243 "as a number."), value_utf8);
4244 g_free (value_utf8);
4249 int_value = long_value;
4250 if (int_value != long_value || errno == ERANGE)
4252 gchar *value_utf8 = _g_utf8_make_valid (value);
4255 G_KEY_FILE_ERROR_INVALID_VALUE,
4256 _("Integer value '%s' out of range"),
4258 g_free (value_utf8);
4267 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4271 return g_strdup_printf ("%d", value);
4275 g_key_file_parse_value_as_double (GKeyFile *key_file,
4279 gchar *end_of_valid_d;
4280 gdouble double_value = 0;
4282 double_value = g_ascii_strtod (value, &end_of_valid_d);
4284 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4286 gchar *value_utf8 = _g_utf8_make_valid (value);
4287 g_set_error (error, G_KEY_FILE_ERROR,
4288 G_KEY_FILE_ERROR_INVALID_VALUE,
4289 _("Value '%s' cannot be interpreted "
4290 "as a float number."),
4292 g_free (value_utf8);
4295 return double_value;
4299 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4305 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4307 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4310 value_utf8 = _g_utf8_make_valid (value);
4311 g_set_error (error, G_KEY_FILE_ERROR,
4312 G_KEY_FILE_ERROR_INVALID_VALUE,
4313 _("Value '%s' cannot be interpreted "
4314 "as a boolean."), value_utf8);
4315 g_free (value_utf8);
4321 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4325 return g_strdup ("true");
4327 return g_strdup ("false");
4331 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4338 string = g_string_sized_new (512);
4340 lines = g_strsplit (value, "\n", 0);
4342 for (i = 0; lines[i] != NULL; i++)
4344 if (lines[i][0] != '#')
4345 g_string_append_printf (string, "%s\n", lines[i]);
4347 g_string_append_printf (string, "%s\n", lines[i] + 1);
4351 return g_string_free (string, FALSE);
4355 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4356 const gchar *comment)
4362 string = g_string_sized_new (512);
4364 lines = g_strsplit (comment, "\n", 0);
4366 for (i = 0; lines[i] != NULL; i++)
4367 g_string_append_printf (string, "#%s%s", lines[i],
4368 lines[i + 1] == NULL? "" : "\n");
4371 return g_string_free (string, FALSE);
4375 * g_key_file_save_to_file:
4376 * @key_file: a #GKeyFile
4377 * @filename: the name of the file to write to
4378 * @error: a pointer to a %NULL #GError, or %NULL
4380 * Writes the contents of @key_file to @filename using
4381 * g_file_set_contents().
4383 * This function can fail for any of the reasons that
4384 * g_file_set_contents() may fail.
4386 * Returns: %TRUE if successful, else %FALSE with @error set
4391 g_key_file_save_to_file (GKeyFile *key_file,
4392 const gchar *filename,
4399 g_return_val_if_fail (key_file != NULL, FALSE);
4400 g_return_val_if_fail (filename != NULL, FALSE);
4401 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4403 contents = g_key_file_to_data (key_file, &length, NULL);
4404 g_assert (contents != NULL);
4406 success = g_file_set_contents (filename, contents, length, error);