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 <literal>key=value</literal>,
117 * with the exception of localized strings, which have the form
118 * <literal>key[locale]=value</literal>, with a locale identifier of the
119 * form <literal>lang_COUNTRY\@MODIFIER</literal>
120 * where <literal>COUNTRY</literal> and <literal>MODIFIER</literal>
122 * Space before and after the '=' character are ignored. Newline, tab,
123 * carriage return and backslash characters in value are escaped as \n,
124 * \t, \r, and \\, respectively. To preserve leading spaces in values,
125 * these can also be escaped as \s.
127 * Key files can store strings (possibly with localized variants), integers,
128 * booleans and lists of these. Lists are separated by a separator character,
129 * typically ';' or ','. To use the list separator character in a value in
130 * a list, it has to be escaped by prefixing it with a backslash.
132 * This syntax is obviously inspired by the .ini files commonly met
133 * on Windows, but there are some important differences:
135 * - .ini files use the ';' character to begin comments,
136 * key files use the '#' character.
138 * - Key files do not allow for ungrouped keys meaning only
139 * comments can precede the first group.
141 * - Key files are always encoded in UTF-8.
143 * - Key and Group names are case-sensitive. For example, a group called
144 * [GROUP] is a different from [group].
146 * - .ini files don't have a strongly typed boolean entry type,
147 * they only have GetProfileInt(). In key files, only
148 * true and false (in lower case) are allowed.
150 * Note that in contrast to the
151 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
152 * groups in key files may contain the same
153 * key multiple times; the last entry wins. Key files may also contain
154 * multiple groups with the same name; they are merged together.
155 * Another difference is that keys and group names in key files are not
156 * restricted to ASCII characters.
162 * Error domain for key file parsing. Errors in this domain will
163 * be from the #GKeyFileError enumeration.
165 * See #GError for information on error domains.
170 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
171 * an unknown encoding
172 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
173 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
174 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
175 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
176 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
178 * Error codes returned by key file parsing.
183 * @G_KEY_FILE_NONE: No flags, default behaviour
184 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
185 * (possibly modified) contents of the key file back to a file;
186 * otherwise all comments will be lost when the key file is
188 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
189 * (possibly modified) contents of the key file back to a file;
190 * otherwise only the translations for the current language will be
193 * Flags which influence the parsing.
197 * G_KEY_FILE_DESKTOP_GROUP:
199 * The name of the main group of a desktop entry file, as defined in the
200 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
201 * Consult the specification for more
202 * details about the meanings of the keys below.
208 * G_KEY_FILE_DESKTOP_KEY_TYPE:
210 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
211 * giving the type of the desktop entry. Usually
212 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
213 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
214 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
220 * G_KEY_FILE_DESKTOP_KEY_VERSION:
222 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
223 * giving the version of the Desktop Entry Specification used for
224 * the desktop entry file.
230 * G_KEY_FILE_DESKTOP_KEY_NAME:
232 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
233 * string giving the specific name of the desktop entry.
239 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
241 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
242 * string giving the generic name of the desktop entry.
248 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
250 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
251 * stating whether the desktop entry should be shown in menus.
257 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
259 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
260 * string giving the tooltip for the desktop entry.
266 * G_KEY_FILE_DESKTOP_KEY_ICON:
268 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
269 * string giving the name of the icon to be displayed for the desktop
276 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
278 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
279 * stating whether the desktop entry has been deleted by the user.
285 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
287 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
288 * strings identifying the environments that should display the
295 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
297 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
298 * strings identifying the environments that should not display the
305 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
307 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
308 * giving the file name of a binary on disk used to determine if the
309 * program is actually installed. It is only valid for desktop entries
310 * with the <literal>Application</literal> type.
316 * G_KEY_FILE_DESKTOP_KEY_EXEC:
318 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
319 * giving the command line to execute. It is only valid for desktop
320 * entries with the <literal>Application</literal> type.
326 * G_KEY_FILE_DESKTOP_KEY_PATH:
328 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
329 * containing the working directory to run the program in. It is only
330 * valid for desktop entries with the <literal>Application</literal> type.
336 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
338 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
339 * stating whether the program should be run in a terminal window.
340 * It is only valid for desktop entries with the
341 * <literal>Application</literal> type.
347 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
349 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
350 * of strings giving the MIME types supported by this desktop entry.
356 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
358 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
359 * of strings giving the categories in which the desktop entry
360 * should be shown in a menu.
366 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
368 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
369 * stating whether the application supports the
370 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
376 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
378 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
379 * identifying the WM class or name hint of a window that the application
380 * will create, which can be used to emulate Startup Notification with
381 * older applications.
387 * G_KEY_FILE_DESKTOP_KEY_URL :
389 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
390 * giving the URL to access. It is only valid for desktop entries
391 * with the <literal>Link</literal> type.
397 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
399 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
400 * entries representing applications.
406 * G_KEY_FILE_DESKTOP_TYPE_LINK:
408 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
409 * entries representing links to documents.
415 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
417 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
418 * entries representing directories.
423 typedef struct _GKeyFileGroup GKeyFileGroup;
428 * The GKeyFile struct contains only private data
429 * and should not be accessed directly.
434 GHashTable *group_hash;
436 GKeyFileGroup *start_group;
437 GKeyFileGroup *current_group;
439 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
441 gchar list_separator;
447 volatile gint ref_count;
450 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
452 struct _GKeyFileGroup
454 const gchar *name; /* NULL for above first group (which will be comments) */
456 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
458 GList *key_value_pairs;
460 /* Used in parallel with key_value_pairs for
461 * increased lookup performance
463 GHashTable *lookup_map;
466 struct _GKeyFileKeyValuePair
468 gchar *key; /* NULL for comments */
472 static gint find_file_in_data_dirs (const gchar *file,
473 const gchar **data_dirs,
476 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
480 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
481 const gchar *group_name);
482 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
483 const gchar *group_name);
485 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
486 GKeyFileGroup *group,
488 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
489 GKeyFileGroup *group,
492 static void g_key_file_remove_group_node (GKeyFile *key_file,
494 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
495 GKeyFileGroup *group,
498 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
499 GKeyFileGroup *group,
500 GKeyFileKeyValuePair *pair);
501 static void g_key_file_add_key (GKeyFile *key_file,
502 GKeyFileGroup *group,
505 static void g_key_file_add_group (GKeyFile *key_file,
506 const gchar *group_name);
507 static gboolean g_key_file_is_group_name (const gchar *name);
508 static gboolean g_key_file_is_key_name (const gchar *name);
509 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
510 static gboolean g_key_file_line_is_comment (const gchar *line);
511 static gboolean g_key_file_line_is_group (const gchar *line);
512 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
513 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
517 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
519 gboolean escape_separator);
520 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
523 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
525 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
528 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
531 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
533 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
535 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
536 const gchar *comment);
537 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
541 static void g_key_file_parse_comment (GKeyFile *key_file,
545 static void g_key_file_parse_group (GKeyFile *key_file,
549 static gchar *key_get_locale (const gchar *key);
550 static void g_key_file_parse_data (GKeyFile *key_file,
554 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
557 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
560 g_key_file_init (GKeyFile *key_file)
562 key_file->current_group = g_slice_new0 (GKeyFileGroup);
563 key_file->groups = g_list_prepend (NULL, key_file->current_group);
564 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
565 key_file->start_group = NULL;
566 key_file->parse_buffer = g_string_sized_new (128);
567 key_file->list_separator = ';';
569 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
573 g_key_file_clear (GKeyFile *key_file)
575 GList *tmp, *group_node;
577 if (key_file->locales)
579 g_strfreev (key_file->locales);
580 key_file->locales = NULL;
583 if (key_file->parse_buffer)
585 g_string_free (key_file->parse_buffer, TRUE);
586 key_file->parse_buffer = NULL;
589 tmp = key_file->groups;
594 g_key_file_remove_group_node (key_file, group_node);
597 if (key_file->group_hash != NULL)
599 g_hash_table_destroy (key_file->group_hash);
600 key_file->group_hash = NULL;
603 g_warn_if_fail (key_file->groups == NULL);
610 * Creates a new empty #GKeyFile object. Use
611 * g_key_file_load_from_file(), g_key_file_load_from_data(),
612 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
613 * read an existing key file.
615 * Return value: (transfer full): an empty #GKeyFile.
620 g_key_file_new (void)
624 key_file = g_slice_new0 (GKeyFile);
625 key_file->ref_count = 1;
626 g_key_file_init (key_file);
632 * g_key_file_set_list_separator:
633 * @key_file: a #GKeyFile
634 * @separator: the separator
636 * Sets the character which is used to separate
637 * values in lists. Typically ';' or ',' are used
638 * as separators. The default list separator is ';'.
643 g_key_file_set_list_separator (GKeyFile *key_file,
646 g_return_if_fail (key_file != NULL);
648 key_file->list_separator = separator;
652 /* Iterates through all the directories in *dirs trying to
653 * open file. When it successfully locates and opens a file it
654 * returns the file descriptor to the open file. It also
655 * outputs the absolute path of the file in output_file.
658 find_file_in_data_dirs (const gchar *file,
663 const gchar **data_dirs, *data_dir;
675 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
677 gchar *candidate_file, *sub_dir;
679 candidate_file = (gchar *) file;
680 sub_dir = g_strdup ("");
681 while (candidate_file != NULL && fd == -1)
685 path = g_build_filename (data_dir, sub_dir,
686 candidate_file, NULL);
688 fd = g_open (path, O_RDONLY, 0);
696 candidate_file = strchr (candidate_file, '-');
698 if (candidate_file == NULL)
704 sub_dir = g_strndup (file, candidate_file - file - 1);
706 for (p = sub_dir; *p != '\0'; p++)
709 *p = G_DIR_SEPARATOR;
718 g_set_error_literal (error, G_KEY_FILE_ERROR,
719 G_KEY_FILE_ERROR_NOT_FOUND,
720 _("Valid key file could not be "
721 "found in search dirs"));
724 if (output_file != NULL && fd > 0)
725 *output_file = g_strdup (path);
733 g_key_file_load_from_fd (GKeyFile *key_file,
738 GError *key_file_error = NULL;
740 struct stat stat_buf;
741 gchar read_buf[4096];
742 gchar list_separator;
744 if (fstat (fd, &stat_buf) < 0)
746 g_set_error_literal (error, G_FILE_ERROR,
747 g_file_error_from_errno (errno),
752 if (!S_ISREG (stat_buf.st_mode))
754 g_set_error_literal (error, G_KEY_FILE_ERROR,
755 G_KEY_FILE_ERROR_PARSE,
756 _("Not a regular file"));
760 list_separator = key_file->list_separator;
761 g_key_file_clear (key_file);
762 g_key_file_init (key_file);
763 key_file->list_separator = list_separator;
764 key_file->flags = flags;
768 bytes_read = read (fd, read_buf, 4096);
770 if (bytes_read == 0) /* End of File */
775 if (errno == EINTR || errno == EAGAIN)
778 g_set_error_literal (error, G_FILE_ERROR,
779 g_file_error_from_errno (errno),
784 g_key_file_parse_data (key_file,
785 read_buf, bytes_read,
788 while (!key_file_error);
792 g_propagate_error (error, key_file_error);
796 g_key_file_flush_parse_buffer (key_file, &key_file_error);
800 g_propagate_error (error, key_file_error);
808 * g_key_file_load_from_file:
809 * @key_file: an empty #GKeyFile struct
810 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
811 * @flags: flags from #GKeyFileFlags
812 * @error: return location for a #GError, or %NULL
814 * Loads a key file into an empty #GKeyFile structure.
815 * If the file could not be loaded then @error is set to
816 * either a #GFileError or #GKeyFileError.
818 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
823 g_key_file_load_from_file (GKeyFile *key_file,
828 GError *key_file_error = NULL;
831 g_return_val_if_fail (key_file != NULL, FALSE);
832 g_return_val_if_fail (file != NULL, FALSE);
834 fd = g_open (file, O_RDONLY, 0);
838 g_set_error_literal (error, G_FILE_ERROR,
839 g_file_error_from_errno (errno),
844 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
849 g_propagate_error (error, key_file_error);
857 * g_key_file_load_from_data:
858 * @key_file: an empty #GKeyFile struct
859 * @data: key file loaded in memory
860 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
861 * @flags: flags from #GKeyFileFlags
862 * @error: return location for a #GError, or %NULL
864 * Loads a key file from memory into an empty #GKeyFile structure.
865 * If the object cannot be created then %error is set to a #GKeyFileError.
867 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
872 g_key_file_load_from_data (GKeyFile *key_file,
878 GError *key_file_error = NULL;
879 gchar list_separator;
881 g_return_val_if_fail (key_file != NULL, FALSE);
882 g_return_val_if_fail (data != NULL || length == 0, FALSE);
884 if (length == (gsize)-1)
885 length = strlen (data);
887 list_separator = key_file->list_separator;
888 g_key_file_clear (key_file);
889 g_key_file_init (key_file);
890 key_file->list_separator = list_separator;
891 key_file->flags = flags;
893 g_key_file_parse_data (key_file, data, length, &key_file_error);
897 g_propagate_error (error, key_file_error);
901 g_key_file_flush_parse_buffer (key_file, &key_file_error);
905 g_propagate_error (error, key_file_error);
913 * g_key_file_load_from_dirs:
914 * @key_file: an empty #GKeyFile struct
915 * @file: (type filename): a relative path to a filename to open and parse
916 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
917 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
918 * of the file, or %NULL
919 * @flags: flags from #GKeyFileFlags
920 * @error: return location for a #GError, or %NULL
922 * This function looks for a key file named @file in the paths
923 * specified in @search_dirs, loads the file into @key_file and
924 * returns the file's full path in @full_path. If the file could not
925 * be loaded then an %error is set to either a #GFileError or
928 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
933 g_key_file_load_from_dirs (GKeyFile *key_file,
935 const gchar **search_dirs,
940 GError *key_file_error = NULL;
941 const gchar **data_dirs;
946 g_return_val_if_fail (key_file != NULL, FALSE);
947 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
948 g_return_val_if_fail (search_dirs != NULL, FALSE);
951 data_dirs = search_dirs;
953 while (*data_dirs != NULL && !found_file)
955 g_free (output_path);
957 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
963 g_propagate_error (error, key_file_error);
967 found_file = g_key_file_load_from_fd (key_file, fd, flags,
973 g_propagate_error (error, key_file_error);
978 if (found_file && full_path)
979 *full_path = output_path;
981 g_free (output_path);
987 * g_key_file_load_from_data_dirs:
988 * @key_file: an empty #GKeyFile struct
989 * @file: (type filename): a relative path to a filename to open and parse
990 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
991 * of the file, or %NULL
992 * @flags: flags from #GKeyFileFlags
993 * @error: return location for a #GError, or %NULL
995 * This function looks for a key file named @file in the paths
996 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
997 * loads the file into @key_file and returns the file's full path in
998 * @full_path. If the file could not be loaded then an %error is
999 * set to either a #GFileError or #GKeyFileError.
1001 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1005 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1008 GKeyFileFlags flags,
1011 gchar **all_data_dirs;
1012 const gchar * user_data_dir;
1013 const gchar * const * system_data_dirs;
1015 gboolean found_file;
1017 g_return_val_if_fail (key_file != NULL, FALSE);
1018 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1020 user_data_dir = g_get_user_data_dir ();
1021 system_data_dirs = g_get_system_data_dirs ();
1022 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1025 all_data_dirs[i++] = g_strdup (user_data_dir);
1028 while (system_data_dirs[j] != NULL)
1029 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1030 all_data_dirs[i] = NULL;
1032 found_file = g_key_file_load_from_dirs (key_file,
1034 (const gchar **)all_data_dirs,
1039 g_strfreev (all_data_dirs);
1045 * g_key_file_ref: (skip)
1046 * @key_file: a #GKeyFile
1048 * Increases the reference count of @key_file.
1050 * Returns: the same @key_file.
1055 g_key_file_ref (GKeyFile *key_file)
1057 g_return_val_if_fail (key_file != NULL, NULL);
1059 g_atomic_int_inc (&key_file->ref_count);
1065 * g_key_file_free: (skip)
1066 * @key_file: a #GKeyFile
1068 * Clears all keys and groups from @key_file, and decreases the
1069 * reference count by 1. If the reference count reaches zero,
1070 * frees the key file and all its allocated memory.
1075 g_key_file_free (GKeyFile *key_file)
1077 g_return_if_fail (key_file != NULL);
1079 g_key_file_clear (key_file);
1080 g_key_file_unref (key_file);
1085 * @key_file: a #GKeyFile
1087 * Decreases the reference count of @key_file by 1. If the reference count
1088 * reaches zero, frees the key file and all its allocated memory.
1093 g_key_file_unref (GKeyFile *key_file)
1095 g_return_if_fail (key_file != NULL);
1097 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1099 g_key_file_clear (key_file);
1100 g_slice_free (GKeyFile, key_file);
1104 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1105 * true for locales that match those in g_get_language_names().
1108 g_key_file_locale_is_interesting (GKeyFile *key_file,
1109 const gchar *locale)
1113 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1116 for (i = 0; key_file->locales[i] != NULL; i++)
1118 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1126 g_key_file_parse_line (GKeyFile *key_file,
1131 GError *parse_error = NULL;
1134 g_return_if_fail (key_file != NULL);
1135 g_return_if_fail (line != NULL);
1137 line_start = (gchar *) line;
1138 while (g_ascii_isspace (*line_start))
1141 if (g_key_file_line_is_comment (line_start))
1142 g_key_file_parse_comment (key_file, line, length, &parse_error);
1143 else if (g_key_file_line_is_group (line_start))
1144 g_key_file_parse_group (key_file, line_start,
1145 length - (line_start - line),
1147 else if (g_key_file_line_is_key_value_pair (line_start))
1148 g_key_file_parse_key_value_pair (key_file, line_start,
1149 length - (line_start - line),
1153 gchar *line_utf8 = _g_utf8_make_valid (line);
1154 g_set_error (error, G_KEY_FILE_ERROR,
1155 G_KEY_FILE_ERROR_PARSE,
1156 _("Key file contains line '%s' which is not "
1157 "a key-value pair, group, or comment"),
1165 g_propagate_error (error, parse_error);
1169 g_key_file_parse_comment (GKeyFile *key_file,
1174 GKeyFileKeyValuePair *pair;
1176 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1179 g_warn_if_fail (key_file->current_group != NULL);
1181 pair = g_slice_new (GKeyFileKeyValuePair);
1183 pair->value = g_strndup (line, length);
1185 key_file->current_group->key_value_pairs =
1186 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1190 g_key_file_parse_group (GKeyFile *key_file,
1196 const gchar *group_name_start, *group_name_end;
1198 /* advance past opening '['
1200 group_name_start = line + 1;
1201 group_name_end = line + length - 1;
1203 while (*group_name_end != ']')
1206 group_name = g_strndup (group_name_start,
1207 group_name_end - group_name_start);
1209 if (!g_key_file_is_group_name (group_name))
1211 g_set_error (error, G_KEY_FILE_ERROR,
1212 G_KEY_FILE_ERROR_PARSE,
1213 _("Invalid group name: %s"), group_name);
1214 g_free (group_name);
1218 g_key_file_add_group (key_file, group_name);
1219 g_free (group_name);
1223 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1228 gchar *key, *value, *key_end, *value_start, *locale;
1229 gsize key_len, value_len;
1231 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1233 g_set_error_literal (error, G_KEY_FILE_ERROR,
1234 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1235 _("Key file does not start with a group"));
1239 key_end = value_start = strchr (line, '=');
1241 g_warn_if_fail (key_end != NULL);
1246 /* Pull the key name from the line (chomping trailing whitespace)
1248 while (g_ascii_isspace (*key_end))
1251 key_len = key_end - line + 2;
1253 g_warn_if_fail (key_len <= length);
1255 key = g_strndup (line, key_len - 1);
1257 if (!g_key_file_is_key_name (key))
1259 g_set_error (error, G_KEY_FILE_ERROR,
1260 G_KEY_FILE_ERROR_PARSE,
1261 _("Invalid key name: %s"), key);
1266 /* Pull the value from the line (chugging leading whitespace)
1268 while (g_ascii_isspace (*value_start))
1271 value_len = line + length - value_start + 1;
1273 value = g_strndup (value_start, value_len);
1275 g_warn_if_fail (key_file->start_group != NULL);
1277 if (key_file->current_group
1278 && key_file->current_group->name
1279 && strcmp (key_file->start_group->name,
1280 key_file->current_group->name) == 0
1281 && strcmp (key, "Encoding") == 0)
1283 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1285 gchar *value_utf8 = _g_utf8_make_valid (value);
1286 g_set_error (error, G_KEY_FILE_ERROR,
1287 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1288 _("Key file contains unsupported "
1289 "encoding '%s'"), value_utf8);
1290 g_free (value_utf8);
1298 /* Is this key a translation? If so, is it one that we care about?
1300 locale = key_get_locale (key);
1302 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1304 GKeyFileKeyValuePair *pair;
1306 pair = g_slice_new (GKeyFileKeyValuePair);
1308 pair->value = value;
1310 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1322 key_get_locale (const gchar *key)
1326 locale = g_strrstr (key, "[");
1328 if (locale && strlen (locale) <= 2)
1332 locale = g_strndup (locale + 1, strlen (locale) - 2);
1338 g_key_file_parse_data (GKeyFile *key_file,
1343 GError *parse_error;
1346 g_return_if_fail (key_file != NULL);
1347 g_return_if_fail (data != NULL || length == 0);
1354 if (data[i] == '\n')
1356 if (key_file->parse_buffer->len > 0
1357 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1359 g_string_erase (key_file->parse_buffer,
1360 key_file->parse_buffer->len - 1,
1363 /* When a newline is encountered flush the parse buffer so that the
1364 * line can be parsed. Note that completely blank lines won't show
1365 * up in the parse buffer, so they get parsed directly.
1367 if (key_file->parse_buffer->len > 0)
1368 g_key_file_flush_parse_buffer (key_file, &parse_error);
1370 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1374 g_propagate_error (error, parse_error);
1381 const gchar *start_of_line;
1382 const gchar *end_of_line;
1385 start_of_line = data + i;
1386 end_of_line = memchr (start_of_line, '\n', length - i);
1388 if (end_of_line == NULL)
1389 end_of_line = data + length;
1391 line_length = end_of_line - start_of_line;
1393 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1400 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1403 GError *file_error = NULL;
1405 g_return_if_fail (key_file != NULL);
1409 if (key_file->parse_buffer->len > 0)
1411 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1412 key_file->parse_buffer->len,
1414 g_string_erase (key_file->parse_buffer, 0, -1);
1418 g_propagate_error (error, file_error);
1425 * g_key_file_to_data:
1426 * @key_file: a #GKeyFile
1427 * @length: (out) (allow-none): return location for the length of the
1428 * returned string, or %NULL
1429 * @error: return location for a #GError, or %NULL
1431 * This function outputs @key_file as a string.
1433 * Note that this function never reports an error,
1434 * so it is safe to pass %NULL as @error.
1436 * Return value: a newly allocated string holding
1437 * the contents of the #GKeyFile
1442 g_key_file_to_data (GKeyFile *key_file,
1446 GString *data_string;
1447 GList *group_node, *key_file_node;
1449 g_return_val_if_fail (key_file != NULL, NULL);
1451 data_string = g_string_new (NULL);
1453 for (group_node = g_list_last (key_file->groups);
1455 group_node = group_node->prev)
1457 GKeyFileGroup *group;
1459 group = (GKeyFileGroup *) group_node->data;
1461 /* separate groups by at least an empty line */
1462 if (data_string->len >= 2 &&
1463 data_string->str[data_string->len - 2] != '\n')
1464 g_string_append_c (data_string, '\n');
1466 if (group->comment != NULL)
1467 g_string_append_printf (data_string, "%s\n", group->comment->value);
1469 if (group->name != NULL)
1470 g_string_append_printf (data_string, "[%s]\n", group->name);
1472 for (key_file_node = g_list_last (group->key_value_pairs);
1473 key_file_node != NULL;
1474 key_file_node = key_file_node->prev)
1476 GKeyFileKeyValuePair *pair;
1478 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1480 if (pair->key != NULL)
1481 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1483 g_string_append_printf (data_string, "%s\n", pair->value);
1488 *length = data_string->len;
1490 return g_string_free (data_string, FALSE);
1494 * g_key_file_get_keys:
1495 * @key_file: a #GKeyFile
1496 * @group_name: a group name
1497 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1498 * @error: return location for a #GError, or %NULL
1500 * Returns all keys for the group name @group_name. The array of
1501 * returned keys will be %NULL-terminated, so @length may
1502 * optionally be %NULL. In the event that the @group_name cannot
1503 * be found, %NULL is returned and @error is set to
1504 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1506 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1507 * Use g_strfreev() to free it.
1512 g_key_file_get_keys (GKeyFile *key_file,
1513 const gchar *group_name,
1517 GKeyFileGroup *group;
1522 g_return_val_if_fail (key_file != NULL, NULL);
1523 g_return_val_if_fail (group_name != NULL, NULL);
1525 group = g_key_file_lookup_group (key_file, group_name);
1529 g_set_error (error, G_KEY_FILE_ERROR,
1530 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1531 _("Key file does not have group '%s'"),
1532 group_name ? group_name : "(null)");
1537 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1539 GKeyFileKeyValuePair *pair;
1541 pair = (GKeyFileKeyValuePair *) tmp->data;
1547 keys = g_new (gchar *, num_keys + 1);
1550 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1552 GKeyFileKeyValuePair *pair;
1554 pair = (GKeyFileKeyValuePair *) tmp->data;
1558 keys[i] = g_strdup (pair->key);
1563 keys[num_keys] = NULL;
1572 * g_key_file_get_start_group:
1573 * @key_file: a #GKeyFile
1575 * Returns the name of the start group of the file.
1577 * Return value: The start group of the key file.
1582 g_key_file_get_start_group (GKeyFile *key_file)
1584 g_return_val_if_fail (key_file != NULL, NULL);
1586 if (key_file->start_group)
1587 return g_strdup (key_file->start_group->name);
1593 * g_key_file_get_groups:
1594 * @key_file: a #GKeyFile
1595 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1597 * Returns all groups in the key file loaded with @key_file.
1598 * The array of returned groups will be %NULL-terminated, so
1599 * @length may optionally be %NULL.
1601 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1602 * Use g_strfreev() to free it.
1606 g_key_file_get_groups (GKeyFile *key_file,
1611 gsize i, num_groups;
1613 g_return_val_if_fail (key_file != NULL, NULL);
1615 num_groups = g_list_length (key_file->groups);
1617 g_return_val_if_fail (num_groups > 0, NULL);
1619 group_node = g_list_last (key_file->groups);
1621 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1623 /* Only need num_groups instead of num_groups + 1
1624 * because the first group of the file (last in the
1625 * list) is always the comment group at the top,
1628 groups = g_new (gchar *, num_groups);
1632 for (group_node = group_node->prev;
1634 group_node = group_node->prev)
1636 GKeyFileGroup *group;
1638 group = (GKeyFileGroup *) group_node->data;
1640 g_warn_if_fail (group->name != NULL);
1642 groups[i++] = g_strdup (group->name);
1653 * g_key_file_get_value:
1654 * @key_file: a #GKeyFile
1655 * @group_name: a group name
1657 * @error: return location for a #GError, or %NULL
1659 * Returns the raw value associated with @key under @group_name.
1660 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1662 * In the event the key cannot be found, %NULL is returned and
1663 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1664 * event that the @group_name cannot be found, %NULL is returned
1665 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1668 * Return value: a newly allocated string or %NULL if the specified
1669 * key cannot be found.
1674 g_key_file_get_value (GKeyFile *key_file,
1675 const gchar *group_name,
1679 GKeyFileGroup *group;
1680 GKeyFileKeyValuePair *pair;
1681 gchar *value = NULL;
1683 g_return_val_if_fail (key_file != NULL, NULL);
1684 g_return_val_if_fail (group_name != NULL, NULL);
1685 g_return_val_if_fail (key != NULL, NULL);
1687 group = g_key_file_lookup_group (key_file, group_name);
1691 g_set_error (error, G_KEY_FILE_ERROR,
1692 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1693 _("Key file does not have group '%s'"),
1694 group_name ? group_name : "(null)");
1698 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1701 value = g_strdup (pair->value);
1703 g_set_error (error, G_KEY_FILE_ERROR,
1704 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1705 _("Key file does not have key '%s'"), key);
1711 * g_key_file_set_value:
1712 * @key_file: a #GKeyFile
1713 * @group_name: a group name
1717 * Associates a new value with @key under @group_name.
1719 * If @key cannot be found then it is created. If @group_name cannot
1720 * be found then it is created. To set an UTF-8 string which may contain
1721 * characters that need escaping (such as newlines or spaces), use
1722 * g_key_file_set_string().
1727 g_key_file_set_value (GKeyFile *key_file,
1728 const gchar *group_name,
1732 GKeyFileGroup *group;
1733 GKeyFileKeyValuePair *pair;
1735 g_return_if_fail (key_file != NULL);
1736 g_return_if_fail (g_key_file_is_group_name (group_name));
1737 g_return_if_fail (g_key_file_is_key_name (key));
1738 g_return_if_fail (value != NULL);
1740 group = g_key_file_lookup_group (key_file, group_name);
1744 g_key_file_add_group (key_file, group_name);
1745 group = (GKeyFileGroup *) key_file->groups->data;
1747 g_key_file_add_key (key_file, group, key, value);
1751 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1754 g_key_file_add_key (key_file, group, key, value);
1757 g_free (pair->value);
1758 pair->value = g_strdup (value);
1764 * g_key_file_get_string:
1765 * @key_file: a #GKeyFile
1766 * @group_name: a group name
1768 * @error: return location for a #GError, or %NULL
1770 * Returns the string value associated with @key under @group_name.
1771 * Unlike g_key_file_get_value(), this function handles escape sequences
1774 * In the event the key cannot be found, %NULL is returned and
1775 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1776 * event that the @group_name cannot be found, %NULL is returned
1777 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1779 * Return value: a newly allocated string or %NULL if the specified
1780 * key cannot be found.
1785 g_key_file_get_string (GKeyFile *key_file,
1786 const gchar *group_name,
1790 gchar *value, *string_value;
1791 GError *key_file_error;
1793 g_return_val_if_fail (key_file != NULL, NULL);
1794 g_return_val_if_fail (group_name != NULL, NULL);
1795 g_return_val_if_fail (key != NULL, NULL);
1797 key_file_error = NULL;
1799 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1803 g_propagate_error (error, key_file_error);
1807 if (!g_utf8_validate (value, -1, NULL))
1809 gchar *value_utf8 = _g_utf8_make_valid (value);
1810 g_set_error (error, G_KEY_FILE_ERROR,
1811 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1812 _("Key file contains key '%s' with value '%s' "
1813 "which is not UTF-8"), key, value_utf8);
1814 g_free (value_utf8);
1820 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1826 if (g_error_matches (key_file_error,
1828 G_KEY_FILE_ERROR_INVALID_VALUE))
1830 g_set_error (error, G_KEY_FILE_ERROR,
1831 G_KEY_FILE_ERROR_INVALID_VALUE,
1832 _("Key file contains key '%s' "
1833 "which has a value that cannot be interpreted."),
1835 g_error_free (key_file_error);
1838 g_propagate_error (error, key_file_error);
1841 return string_value;
1845 * g_key_file_set_string:
1846 * @key_file: a #GKeyFile
1847 * @group_name: a group name
1851 * Associates a new string value with @key under @group_name.
1852 * If @key cannot be found then it is created.
1853 * If @group_name cannot be found then it is created.
1854 * Unlike g_key_file_set_value(), this function handles characters
1855 * that need escaping, such as newlines.
1860 g_key_file_set_string (GKeyFile *key_file,
1861 const gchar *group_name,
1863 const gchar *string)
1867 g_return_if_fail (key_file != NULL);
1868 g_return_if_fail (string != NULL);
1870 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1871 g_key_file_set_value (key_file, group_name, key, value);
1876 * g_key_file_get_string_list:
1877 * @key_file: a #GKeyFile
1878 * @group_name: a group name
1880 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1881 * @error: return location for a #GError, or %NULL
1883 * Returns the values associated with @key under @group_name.
1885 * In the event the key cannot be found, %NULL is returned and
1886 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1887 * event that the @group_name cannot be found, %NULL is returned
1888 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1890 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1891 * a %NULL-terminated string array or %NULL if the specified
1892 * key cannot be found. The array should be freed with g_strfreev().
1897 g_key_file_get_string_list (GKeyFile *key_file,
1898 const gchar *group_name,
1903 GError *key_file_error = NULL;
1904 gchar *value, *string_value, **values;
1906 GSList *p, *pieces = NULL;
1908 g_return_val_if_fail (key_file != NULL, NULL);
1909 g_return_val_if_fail (group_name != NULL, NULL);
1910 g_return_val_if_fail (key != NULL, NULL);
1915 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1919 g_propagate_error (error, key_file_error);
1923 if (!g_utf8_validate (value, -1, NULL))
1925 gchar *value_utf8 = _g_utf8_make_valid (value);
1926 g_set_error (error, G_KEY_FILE_ERROR,
1927 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1928 _("Key file contains key '%s' with value '%s' "
1929 "which is not UTF-8"), key, value_utf8);
1930 g_free (value_utf8);
1936 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1938 g_free (string_value);
1942 if (g_error_matches (key_file_error,
1944 G_KEY_FILE_ERROR_INVALID_VALUE))
1946 g_set_error (error, G_KEY_FILE_ERROR,
1947 G_KEY_FILE_ERROR_INVALID_VALUE,
1948 _("Key file contains key '%s' "
1949 "which has a value that cannot be interpreted."),
1951 g_error_free (key_file_error);
1954 g_propagate_error (error, key_file_error);
1956 g_slist_free_full (pieces, g_free);
1960 len = g_slist_length (pieces);
1961 values = g_new (gchar *, len + 1);
1962 for (p = pieces, i = 0; p; p = p->next)
1963 values[i++] = p->data;
1966 g_slist_free (pieces);
1975 * g_key_file_set_string_list:
1976 * @key_file: a #GKeyFile
1977 * @group_name: a group name
1979 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1980 * @length: number of string values in @list
1982 * Associates a list of string values for @key under @group_name.
1983 * If @key cannot be found then it is created.
1984 * If @group_name cannot be found then it is created.
1989 g_key_file_set_string_list (GKeyFile *key_file,
1990 const gchar *group_name,
1992 const gchar * const list[],
1995 GString *value_list;
1998 g_return_if_fail (key_file != NULL);
1999 g_return_if_fail (list != NULL || length == 0);
2001 value_list = g_string_sized_new (length * 128);
2002 for (i = 0; i < length && list[i] != NULL; i++)
2006 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2007 g_string_append (value_list, value);
2008 g_string_append_c (value_list, key_file->list_separator);
2013 g_key_file_set_value (key_file, group_name, key, value_list->str);
2014 g_string_free (value_list, TRUE);
2018 * g_key_file_set_locale_string:
2019 * @key_file: a #GKeyFile
2020 * @group_name: a group name
2022 * @locale: a locale identifier
2025 * Associates a string value for @key and @locale under @group_name.
2026 * If the translation for @key cannot be found then it is created.
2031 g_key_file_set_locale_string (GKeyFile *key_file,
2032 const gchar *group_name,
2034 const gchar *locale,
2035 const gchar *string)
2037 gchar *full_key, *value;
2039 g_return_if_fail (key_file != NULL);
2040 g_return_if_fail (key != NULL);
2041 g_return_if_fail (locale != NULL);
2042 g_return_if_fail (string != NULL);
2044 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2045 full_key = g_strdup_printf ("%s[%s]", key, locale);
2046 g_key_file_set_value (key_file, group_name, full_key, value);
2052 * g_key_file_get_locale_string:
2053 * @key_file: a #GKeyFile
2054 * @group_name: a group name
2056 * @locale: (allow-none): a locale identifier or %NULL
2057 * @error: return location for a #GError, or %NULL
2059 * Returns the value associated with @key under @group_name
2060 * translated in the given @locale if available. If @locale is
2061 * %NULL then the current locale is assumed.
2063 * If @key cannot be found then %NULL is returned and @error is set
2064 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2065 * with @key cannot be interpreted or no suitable translation can
2066 * be found then the untranslated value is returned.
2068 * Return value: a newly allocated string or %NULL if the specified
2069 * key cannot be found.
2074 g_key_file_get_locale_string (GKeyFile *key_file,
2075 const gchar *group_name,
2077 const gchar *locale,
2080 gchar *candidate_key, *translated_value;
2081 GError *key_file_error;
2083 gboolean free_languages = FALSE;
2086 g_return_val_if_fail (key_file != NULL, NULL);
2087 g_return_val_if_fail (group_name != NULL, NULL);
2088 g_return_val_if_fail (key != NULL, NULL);
2090 candidate_key = NULL;
2091 translated_value = NULL;
2092 key_file_error = NULL;
2096 languages = g_get_locale_variants (locale);
2097 free_languages = TRUE;
2101 languages = (gchar **) g_get_language_names ();
2102 free_languages = FALSE;
2105 for (i = 0; languages[i]; i++)
2107 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2109 translated_value = g_key_file_get_string (key_file,
2111 candidate_key, NULL);
2112 g_free (candidate_key);
2114 if (translated_value)
2117 g_free (translated_value);
2118 translated_value = NULL;
2121 /* Fallback to untranslated key
2123 if (!translated_value)
2125 translated_value = g_key_file_get_string (key_file, group_name, key,
2128 if (!translated_value)
2129 g_propagate_error (error, key_file_error);
2133 g_strfreev (languages);
2135 return translated_value;
2139 * g_key_file_get_locale_string_list:
2140 * @key_file: a #GKeyFile
2141 * @group_name: a group name
2143 * @locale: (allow-none): a locale identifier or %NULL
2144 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2145 * @error: return location for a #GError or %NULL
2147 * Returns the values associated with @key under @group_name
2148 * translated in the given @locale if available. If @locale is
2149 * %NULL then the current locale is assumed.
2151 * If @key cannot be found then %NULL is returned and @error is set
2152 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2153 * with @key cannot be interpreted or no suitable translations
2154 * can be found then the untranslated values are returned. The
2155 * returned array is %NULL-terminated, so @length may optionally
2158 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2159 * or %NULL if the key isn't found. The string array should be freed
2160 * with g_strfreev().
2165 g_key_file_get_locale_string_list (GKeyFile *key_file,
2166 const gchar *group_name,
2168 const gchar *locale,
2172 GError *key_file_error;
2173 gchar **values, *value;
2174 char list_separator[2];
2177 g_return_val_if_fail (key_file != NULL, NULL);
2178 g_return_val_if_fail (group_name != NULL, NULL);
2179 g_return_val_if_fail (key != NULL, NULL);
2181 key_file_error = NULL;
2183 value = g_key_file_get_locale_string (key_file, group_name,
2188 g_propagate_error (error, key_file_error);
2197 len = strlen (value);
2198 if (value[len - 1] == key_file->list_separator)
2199 value[len - 1] = '\0';
2201 list_separator[0] = key_file->list_separator;
2202 list_separator[1] = '\0';
2203 values = g_strsplit (value, list_separator, 0);
2208 *length = g_strv_length (values);
2214 * g_key_file_set_locale_string_list:
2215 * @key_file: a #GKeyFile
2216 * @group_name: a group name
2218 * @locale: a locale identifier
2219 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2220 * @length: the length of @list
2222 * Associates a list of string values for @key and @locale under
2223 * @group_name. If the translation for @key cannot be found then
2229 g_key_file_set_locale_string_list (GKeyFile *key_file,
2230 const gchar *group_name,
2232 const gchar *locale,
2233 const gchar * const list[],
2236 GString *value_list;
2240 g_return_if_fail (key_file != NULL);
2241 g_return_if_fail (key != NULL);
2242 g_return_if_fail (locale != NULL);
2243 g_return_if_fail (length != 0);
2245 value_list = g_string_sized_new (length * 128);
2246 for (i = 0; i < length && list[i] != NULL; i++)
2250 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2251 g_string_append (value_list, value);
2252 g_string_append_c (value_list, key_file->list_separator);
2257 full_key = g_strdup_printf ("%s[%s]", key, locale);
2258 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2260 g_string_free (value_list, TRUE);
2264 * g_key_file_get_boolean:
2265 * @key_file: a #GKeyFile
2266 * @group_name: a group name
2268 * @error: return location for a #GError
2270 * Returns the value associated with @key under @group_name as a
2273 * If @key cannot be found then %FALSE is returned and @error is set
2274 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2275 * associated with @key cannot be interpreted as a boolean then %FALSE
2276 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2278 * Return value: the value associated with the key as a boolean,
2279 * or %FALSE if the key was not found or could not be parsed.
2284 g_key_file_get_boolean (GKeyFile *key_file,
2285 const gchar *group_name,
2289 GError *key_file_error = NULL;
2291 gboolean bool_value;
2293 g_return_val_if_fail (key_file != NULL, FALSE);
2294 g_return_val_if_fail (group_name != NULL, FALSE);
2295 g_return_val_if_fail (key != NULL, FALSE);
2297 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2301 g_propagate_error (error, key_file_error);
2305 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2311 if (g_error_matches (key_file_error,
2313 G_KEY_FILE_ERROR_INVALID_VALUE))
2315 g_set_error (error, G_KEY_FILE_ERROR,
2316 G_KEY_FILE_ERROR_INVALID_VALUE,
2317 _("Key file contains key '%s' "
2318 "which has a value that cannot be interpreted."),
2320 g_error_free (key_file_error);
2323 g_propagate_error (error, key_file_error);
2330 * g_key_file_set_boolean:
2331 * @key_file: a #GKeyFile
2332 * @group_name: a group name
2334 * @value: %TRUE or %FALSE
2336 * Associates a new boolean value with @key under @group_name.
2337 * If @key cannot be found then it is created.
2342 g_key_file_set_boolean (GKeyFile *key_file,
2343 const gchar *group_name,
2349 g_return_if_fail (key_file != NULL);
2351 result = g_key_file_parse_boolean_as_value (key_file, value);
2352 g_key_file_set_value (key_file, group_name, key, result);
2357 * g_key_file_get_boolean_list:
2358 * @key_file: a #GKeyFile
2359 * @group_name: a group name
2361 * @length: (out): the number of booleans returned
2362 * @error: return location for a #GError
2364 * Returns the values associated with @key under @group_name as
2367 * If @key cannot be found then %NULL is returned and @error is set to
2368 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2369 * with @key cannot be interpreted as booleans then %NULL is returned
2370 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2372 * Return value: (array length=length) (element-type gboolean) (transfer container):
2373 * the values associated with the key as a list of booleans, or %NULL if the
2374 * key was not found or could not be parsed. The returned list of booleans
2375 * should be freed with g_free() when no longer needed.
2380 g_key_file_get_boolean_list (GKeyFile *key_file,
2381 const gchar *group_name,
2386 GError *key_file_error;
2388 gboolean *bool_values;
2391 g_return_val_if_fail (key_file != NULL, NULL);
2392 g_return_val_if_fail (group_name != NULL, NULL);
2393 g_return_val_if_fail (key != NULL, NULL);
2398 key_file_error = NULL;
2400 values = g_key_file_get_string_list (key_file, group_name, key,
2401 &num_bools, &key_file_error);
2404 g_propagate_error (error, key_file_error);
2409 bool_values = g_new (gboolean, num_bools);
2411 for (i = 0; i < num_bools; i++)
2413 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2419 g_propagate_error (error, key_file_error);
2420 g_strfreev (values);
2421 g_free (bool_values);
2426 g_strfreev (values);
2429 *length = num_bools;
2435 * g_key_file_set_boolean_list:
2436 * @key_file: a #GKeyFile
2437 * @group_name: a group name
2439 * @list: (array length=length): an array of boolean values
2440 * @length: length of @list
2442 * Associates a list of boolean values with @key under @group_name.
2443 * If @key cannot be found then it is created.
2444 * If @group_name is %NULL, the start_group is used.
2449 g_key_file_set_boolean_list (GKeyFile *key_file,
2450 const gchar *group_name,
2455 GString *value_list;
2458 g_return_if_fail (key_file != NULL);
2459 g_return_if_fail (list != NULL);
2461 value_list = g_string_sized_new (length * 8);
2462 for (i = 0; i < length; i++)
2466 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2468 g_string_append (value_list, value);
2469 g_string_append_c (value_list, key_file->list_separator);
2474 g_key_file_set_value (key_file, group_name, key, value_list->str);
2475 g_string_free (value_list, TRUE);
2479 * g_key_file_get_integer:
2480 * @key_file: a #GKeyFile
2481 * @group_name: a group name
2483 * @error: return location for a #GError
2485 * Returns the value associated with @key under @group_name as an
2488 * If @key cannot be found then 0 is returned and @error is set to
2489 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2490 * with @key cannot be interpreted as an integer then 0 is returned
2491 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2493 * Return value: the value associated with the key as an integer, or
2494 * 0 if the key was not found or could not be parsed.
2499 g_key_file_get_integer (GKeyFile *key_file,
2500 const gchar *group_name,
2504 GError *key_file_error;
2508 g_return_val_if_fail (key_file != NULL, -1);
2509 g_return_val_if_fail (group_name != NULL, -1);
2510 g_return_val_if_fail (key != NULL, -1);
2512 key_file_error = NULL;
2514 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2518 g_propagate_error (error, key_file_error);
2522 int_value = g_key_file_parse_value_as_integer (key_file, value,
2528 if (g_error_matches (key_file_error,
2530 G_KEY_FILE_ERROR_INVALID_VALUE))
2532 g_set_error (error, G_KEY_FILE_ERROR,
2533 G_KEY_FILE_ERROR_INVALID_VALUE,
2534 _("Key file contains key '%s' in group '%s' "
2535 "which has a value that cannot be interpreted."),
2537 g_error_free (key_file_error);
2540 g_propagate_error (error, key_file_error);
2547 * g_key_file_set_integer:
2548 * @key_file: a #GKeyFile
2549 * @group_name: a group name
2551 * @value: an integer value
2553 * Associates a new integer value with @key under @group_name.
2554 * If @key cannot be found then it is created.
2559 g_key_file_set_integer (GKeyFile *key_file,
2560 const gchar *group_name,
2566 g_return_if_fail (key_file != NULL);
2568 result = g_key_file_parse_integer_as_value (key_file, value);
2569 g_key_file_set_value (key_file, group_name, key, result);
2574 * g_key_file_get_int64:
2575 * @key_file: a non-%NULL #GKeyFile
2576 * @group_name: a non-%NULL group name
2577 * @key: a non-%NULL key
2578 * @error: return location for a #GError
2580 * Returns the value associated with @key under @group_name as a signed
2581 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2582 * 64-bit results without truncation.
2584 * Returns: the value associated with the key as a signed 64-bit integer, or
2585 * 0 if the key was not found or could not be parsed.
2590 g_key_file_get_int64 (GKeyFile *key_file,
2591 const gchar *group_name,
2598 g_return_val_if_fail (key_file != NULL, -1);
2599 g_return_val_if_fail (group_name != NULL, -1);
2600 g_return_val_if_fail (key != NULL, -1);
2602 s = g_key_file_get_value (key_file, group_name, key, error);
2607 v = g_ascii_strtoll (s, &end, 10);
2609 if (*s == '\0' || *end != '\0')
2611 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2612 _("Key '%s' in group '%s' has value '%s' "
2613 "where %s was expected"),
2614 key, group_name, s, "int64");
2624 * g_key_file_set_int64:
2625 * @key_file: a #GKeyFile
2626 * @group_name: a group name
2628 * @value: an integer value
2630 * Associates a new integer value with @key under @group_name.
2631 * If @key cannot be found then it is created.
2636 g_key_file_set_int64 (GKeyFile *key_file,
2637 const gchar *group_name,
2643 g_return_if_fail (key_file != NULL);
2645 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2646 g_key_file_set_value (key_file, group_name, key, result);
2651 * g_key_file_get_uint64:
2652 * @key_file: a non-%NULL #GKeyFile
2653 * @group_name: a non-%NULL group name
2654 * @key: a non-%NULL key
2655 * @error: return location for a #GError
2657 * Returns the value associated with @key under @group_name as an unsigned
2658 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2659 * large positive results without truncation.
2661 * Returns: the value associated with the key as an unsigned 64-bit integer,
2662 * or 0 if the key was not found or could not be parsed.
2667 g_key_file_get_uint64 (GKeyFile *key_file,
2668 const gchar *group_name,
2675 g_return_val_if_fail (key_file != NULL, -1);
2676 g_return_val_if_fail (group_name != NULL, -1);
2677 g_return_val_if_fail (key != NULL, -1);
2679 s = g_key_file_get_value (key_file, group_name, key, error);
2684 v = g_ascii_strtoull (s, &end, 10);
2686 if (*s == '\0' || *end != '\0')
2688 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2689 _("Key '%s' in group '%s' has value '%s' "
2690 "where %s was expected"),
2691 key, group_name, s, "uint64");
2701 * g_key_file_set_uint64:
2702 * @key_file: a #GKeyFile
2703 * @group_name: a group name
2705 * @value: an integer value
2707 * Associates a new integer value with @key under @group_name.
2708 * If @key cannot be found then it is created.
2713 g_key_file_set_uint64 (GKeyFile *key_file,
2714 const gchar *group_name,
2720 g_return_if_fail (key_file != NULL);
2722 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2723 g_key_file_set_value (key_file, group_name, key, result);
2728 * g_key_file_get_integer_list:
2729 * @key_file: a #GKeyFile
2730 * @group_name: a group name
2732 * @length: (out): the number of integers returned
2733 * @error: return location for a #GError
2735 * Returns the values associated with @key under @group_name as
2738 * If @key cannot be found then %NULL is returned and @error is set to
2739 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2740 * with @key cannot be interpreted as integers then %NULL is returned
2741 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2743 * Return value: (array length=length) (element-type gint) (transfer container):
2744 * the values associated with the key as a list of integers, or %NULL if
2745 * the key was not found or could not be parsed. The returned list of
2746 * integers should be freed with g_free() when no longer needed.
2751 g_key_file_get_integer_list (GKeyFile *key_file,
2752 const gchar *group_name,
2757 GError *key_file_error = NULL;
2762 g_return_val_if_fail (key_file != NULL, NULL);
2763 g_return_val_if_fail (group_name != NULL, NULL);
2764 g_return_val_if_fail (key != NULL, NULL);
2769 values = g_key_file_get_string_list (key_file, group_name, key,
2770 &num_ints, &key_file_error);
2773 g_propagate_error (error, key_file_error);
2778 int_values = g_new (gint, num_ints);
2780 for (i = 0; i < num_ints; i++)
2782 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2788 g_propagate_error (error, key_file_error);
2789 g_strfreev (values);
2790 g_free (int_values);
2795 g_strfreev (values);
2804 * g_key_file_set_integer_list:
2805 * @key_file: a #GKeyFile
2806 * @group_name: a group name
2808 * @list: (array length=length): an array of integer values
2809 * @length: number of integer values in @list
2811 * Associates a list of integer values with @key under @group_name.
2812 * If @key cannot be found then it is created.
2817 g_key_file_set_integer_list (GKeyFile *key_file,
2818 const gchar *group_name,
2826 g_return_if_fail (key_file != NULL);
2827 g_return_if_fail (list != NULL);
2829 values = g_string_sized_new (length * 16);
2830 for (i = 0; i < length; i++)
2834 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2836 g_string_append (values, value);
2837 g_string_append_c (values, key_file->list_separator);
2842 g_key_file_set_value (key_file, group_name, key, values->str);
2843 g_string_free (values, TRUE);
2847 * g_key_file_get_double:
2848 * @key_file: a #GKeyFile
2849 * @group_name: a group name
2851 * @error: return location for a #GError
2853 * Returns the value associated with @key under @group_name as a
2854 * double. If @group_name is %NULL, the start_group is used.
2856 * If @key cannot be found then 0.0 is returned and @error is set to
2857 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2858 * with @key cannot be interpreted as a double then 0.0 is returned
2859 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2861 * Return value: the value associated with the key as a double, or
2862 * 0.0 if the key was not found or could not be parsed.
2867 g_key_file_get_double (GKeyFile *key_file,
2868 const gchar *group_name,
2872 GError *key_file_error;
2874 gdouble double_value;
2876 g_return_val_if_fail (key_file != NULL, -1);
2877 g_return_val_if_fail (group_name != NULL, -1);
2878 g_return_val_if_fail (key != NULL, -1);
2880 key_file_error = NULL;
2882 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2886 g_propagate_error (error, key_file_error);
2890 double_value = g_key_file_parse_value_as_double (key_file, value,
2896 if (g_error_matches (key_file_error,
2898 G_KEY_FILE_ERROR_INVALID_VALUE))
2900 g_set_error (error, G_KEY_FILE_ERROR,
2901 G_KEY_FILE_ERROR_INVALID_VALUE,
2902 _("Key file contains key '%s' in group '%s' "
2903 "which has a value that cannot be interpreted."),
2905 g_error_free (key_file_error);
2908 g_propagate_error (error, key_file_error);
2911 return double_value;
2915 * g_key_file_set_double:
2916 * @key_file: a #GKeyFile
2917 * @group_name: a group name
2919 * @value: an double value
2921 * Associates a new double value with @key under @group_name.
2922 * If @key cannot be found then it is created.
2927 g_key_file_set_double (GKeyFile *key_file,
2928 const gchar *group_name,
2932 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2934 g_return_if_fail (key_file != NULL);
2936 g_ascii_dtostr (result, sizeof (result), value);
2937 g_key_file_set_value (key_file, group_name, key, result);
2941 * g_key_file_get_double_list:
2942 * @key_file: a #GKeyFile
2943 * @group_name: a group name
2945 * @length: (out): the number of doubles returned
2946 * @error: return location for a #GError
2948 * Returns the values associated with @key under @group_name as
2951 * If @key cannot be found then %NULL is returned and @error is set to
2952 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2953 * with @key cannot be interpreted as doubles then %NULL is returned
2954 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2956 * Return value: (array length=length) (element-type gdouble) (transfer container):
2957 * the values associated with the key as a list of doubles, or %NULL if the
2958 * key was not found or could not be parsed. The returned list of doubles
2959 * should be freed with g_free() when no longer needed.
2964 g_key_file_get_double_list (GKeyFile *key_file,
2965 const gchar *group_name,
2970 GError *key_file_error = NULL;
2972 gdouble *double_values;
2973 gsize i, num_doubles;
2975 g_return_val_if_fail (key_file != NULL, NULL);
2976 g_return_val_if_fail (group_name != NULL, NULL);
2977 g_return_val_if_fail (key != NULL, NULL);
2982 values = g_key_file_get_string_list (key_file, group_name, key,
2983 &num_doubles, &key_file_error);
2986 g_propagate_error (error, key_file_error);
2991 double_values = g_new (gdouble, num_doubles);
2993 for (i = 0; i < num_doubles; i++)
2995 double_values[i] = g_key_file_parse_value_as_double (key_file,
3001 g_propagate_error (error, key_file_error);
3002 g_strfreev (values);
3003 g_free (double_values);
3008 g_strfreev (values);
3011 *length = num_doubles;
3013 return double_values;
3017 * g_key_file_set_double_list:
3018 * @key_file: a #GKeyFile
3019 * @group_name: a group name
3021 * @list: (array length=length): an array of double values
3022 * @length: number of double values in @list
3024 * Associates a list of double values with @key under
3025 * @group_name. If @key cannot be found then it is created.
3030 g_key_file_set_double_list (GKeyFile *key_file,
3031 const gchar *group_name,
3039 g_return_if_fail (key_file != NULL);
3040 g_return_if_fail (list != NULL);
3042 values = g_string_sized_new (length * 16);
3043 for (i = 0; i < length; i++)
3045 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3047 g_ascii_dtostr( result, sizeof (result), list[i] );
3049 g_string_append (values, result);
3050 g_string_append_c (values, key_file->list_separator);
3053 g_key_file_set_value (key_file, group_name, key, values->str);
3054 g_string_free (values, TRUE);
3058 g_key_file_set_key_comment (GKeyFile *key_file,
3059 const gchar *group_name,
3061 const gchar *comment,
3064 GKeyFileGroup *group;
3065 GKeyFileKeyValuePair *pair;
3066 GList *key_node, *comment_node, *tmp;
3068 group = g_key_file_lookup_group (key_file, group_name);
3071 g_set_error (error, G_KEY_FILE_ERROR,
3072 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3073 _("Key file does not have group '%s'"),
3074 group_name ? group_name : "(null)");
3079 /* First find the key the comments are supposed to be
3082 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3084 if (key_node == NULL)
3086 g_set_error (error, G_KEY_FILE_ERROR,
3087 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3088 _("Key file does not have key '%s' in group '%s'"),
3093 /* Then find all the comments already associated with the
3096 tmp = key_node->next;
3099 pair = (GKeyFileKeyValuePair *) tmp->data;
3101 if (pair->key != NULL)
3106 g_key_file_remove_key_value_pair_node (key_file, group,
3110 if (comment == NULL)
3113 /* Now we can add our new comment
3115 pair = g_slice_new (GKeyFileKeyValuePair);
3117 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3119 key_node = g_list_insert (key_node, pair, 1);
3125 g_key_file_set_group_comment (GKeyFile *key_file,
3126 const gchar *group_name,
3127 const gchar *comment,
3130 GKeyFileGroup *group;
3132 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3134 group = g_key_file_lookup_group (key_file, group_name);
3137 g_set_error (error, G_KEY_FILE_ERROR,
3138 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3139 _("Key file does not have group '%s'"),
3140 group_name ? group_name : "(null)");
3145 /* First remove any existing comment
3149 g_key_file_key_value_pair_free (group->comment);
3150 group->comment = NULL;
3153 if (comment == NULL)
3156 /* Now we can add our new comment
3158 group->comment = g_slice_new (GKeyFileKeyValuePair);
3159 group->comment->key = NULL;
3160 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3166 g_key_file_set_top_comment (GKeyFile *key_file,
3167 const gchar *comment,
3171 GKeyFileGroup *group;
3172 GKeyFileKeyValuePair *pair;
3174 /* The last group in the list should be the top (comments only)
3177 g_warn_if_fail (key_file->groups != NULL);
3178 group_node = g_list_last (key_file->groups);
3179 group = (GKeyFileGroup *) group_node->data;
3180 g_warn_if_fail (group->name == NULL);
3182 /* Note all keys must be comments at the top of
3183 * the file, so we can just free it all.
3185 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3186 group->key_value_pairs = NULL;
3188 if (comment == NULL)
3191 pair = g_slice_new (GKeyFileKeyValuePair);
3193 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3195 group->key_value_pairs =
3196 g_list_prepend (group->key_value_pairs, pair);
3202 * g_key_file_set_comment:
3203 * @key_file: a #GKeyFile
3204 * @group_name: (allow-none): a group name, or %NULL
3205 * @key: (allow-none): a key
3206 * @comment: a comment
3207 * @error: return location for a #GError
3209 * Places a comment above @key from @group_name.
3210 * If @key is %NULL then @comment will be written above @group_name.
3211 * If both @key and @group_name are %NULL, then @comment will be
3212 * written above the first group in the file.
3214 * Returns: %TRUE if the comment was written, %FALSE otherwise
3219 g_key_file_set_comment (GKeyFile *key_file,
3220 const gchar *group_name,
3222 const gchar *comment,
3225 g_return_val_if_fail (key_file != NULL, FALSE);
3227 if (group_name != NULL && key != NULL)
3229 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3232 else if (group_name != NULL)
3234 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3239 if (!g_key_file_set_top_comment (key_file, comment, error))
3247 g_key_file_get_key_comment (GKeyFile *key_file,
3248 const gchar *group_name,
3252 GKeyFileGroup *group;
3253 GKeyFileKeyValuePair *pair;
3254 GList *key_node, *tmp;
3258 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3260 group = g_key_file_lookup_group (key_file, group_name);
3263 g_set_error (error, G_KEY_FILE_ERROR,
3264 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3265 _("Key file does not have group '%s'"),
3266 group_name ? group_name : "(null)");
3271 /* First find the key the comments are supposed to be
3274 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3276 if (key_node == NULL)
3278 g_set_error (error, G_KEY_FILE_ERROR,
3279 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3280 _("Key file does not have key '%s' in group '%s'"),
3287 /* Then find all the comments already associated with the
3288 * key and concatentate them.
3290 tmp = key_node->next;
3291 if (!key_node->next)
3294 pair = (GKeyFileKeyValuePair *) tmp->data;
3295 if (pair->key != NULL)
3300 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3302 if (pair->key != NULL)
3308 while (tmp != key_node)
3310 pair = (GKeyFileKeyValuePair *) tmp->data;
3313 string = g_string_sized_new (512);
3315 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3316 g_string_append (string, comment);
3324 comment = string->str;
3325 g_string_free (string, FALSE);
3334 get_group_comment (GKeyFile *key_file,
3335 GKeyFileGroup *group,
3344 tmp = group->key_value_pairs;
3347 GKeyFileKeyValuePair *pair;
3349 pair = (GKeyFileKeyValuePair *) tmp->data;
3351 if (pair->key != NULL)
3357 if (tmp->next == NULL)
3365 GKeyFileKeyValuePair *pair;
3367 pair = (GKeyFileKeyValuePair *) tmp->data;
3370 string = g_string_sized_new (512);
3372 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3373 g_string_append (string, comment);
3380 return g_string_free (string, FALSE);
3386 g_key_file_get_group_comment (GKeyFile *key_file,
3387 const gchar *group_name,
3391 GKeyFileGroup *group;
3393 group = g_key_file_lookup_group (key_file, group_name);
3396 g_set_error (error, G_KEY_FILE_ERROR,
3397 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3398 _("Key file does not have group '%s'"),
3399 group_name ? group_name : "(null)");
3405 return g_strdup (group->comment->value);
3407 group_node = g_key_file_lookup_group_node (key_file, group_name);
3408 group_node = group_node->next;
3409 group = (GKeyFileGroup *)group_node->data;
3410 return get_group_comment (key_file, group, error);
3414 g_key_file_get_top_comment (GKeyFile *key_file,
3418 GKeyFileGroup *group;
3420 /* The last group in the list should be the top (comments only)
3423 g_warn_if_fail (key_file->groups != NULL);
3424 group_node = g_list_last (key_file->groups);
3425 group = (GKeyFileGroup *) group_node->data;
3426 g_warn_if_fail (group->name == NULL);
3428 return get_group_comment (key_file, group, error);
3432 * g_key_file_get_comment:
3433 * @key_file: a #GKeyFile
3434 * @group_name: (allow-none): a group name, or %NULL
3436 * @error: return location for a #GError
3438 * Retrieves a comment above @key from @group_name.
3439 * If @key is %NULL then @comment will be read from above
3440 * @group_name. If both @key and @group_name are %NULL, then
3441 * @comment will be read from above the first group in the file.
3443 * Returns: a comment that should be freed with g_free()
3448 g_key_file_get_comment (GKeyFile *key_file,
3449 const gchar *group_name,
3453 g_return_val_if_fail (key_file != NULL, NULL);
3455 if (group_name != NULL && key != NULL)
3456 return g_key_file_get_key_comment (key_file, group_name, key, error);
3457 else if (group_name != NULL)
3458 return g_key_file_get_group_comment (key_file, group_name, error);
3460 return g_key_file_get_top_comment (key_file, error);
3464 * g_key_file_remove_comment:
3465 * @key_file: a #GKeyFile
3466 * @group_name: (allow-none): a group name, or %NULL
3467 * @key: (allow-none): a key
3468 * @error: return location for a #GError
3470 * Removes a comment above @key from @group_name.
3471 * If @key is %NULL then @comment will be removed above @group_name.
3472 * If both @key and @group_name are %NULL, then @comment will
3473 * be removed above the first group in the file.
3475 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3481 g_key_file_remove_comment (GKeyFile *key_file,
3482 const gchar *group_name,
3486 g_return_val_if_fail (key_file != NULL, FALSE);
3488 if (group_name != NULL && key != NULL)
3489 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3490 else if (group_name != NULL)
3491 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3493 return g_key_file_set_top_comment (key_file, NULL, error);
3497 * g_key_file_has_group:
3498 * @key_file: a #GKeyFile
3499 * @group_name: a group name
3501 * Looks whether the key file has the group @group_name.
3503 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3508 g_key_file_has_group (GKeyFile *key_file,
3509 const gchar *group_name)
3511 g_return_val_if_fail (key_file != NULL, FALSE);
3512 g_return_val_if_fail (group_name != NULL, FALSE);
3514 return g_key_file_lookup_group (key_file, group_name) != NULL;
3517 /* This code remains from a historical attempt to add a new public API
3518 * which respects the GError rules.
3521 g_key_file_has_key_full (GKeyFile *key_file,
3522 const gchar *group_name,
3527 GKeyFileKeyValuePair *pair;
3528 GKeyFileGroup *group;
3530 g_return_val_if_fail (key_file != NULL, FALSE);
3531 g_return_val_if_fail (group_name != NULL, FALSE);
3532 g_return_val_if_fail (key != NULL, FALSE);
3534 group = g_key_file_lookup_group (key_file, group_name);
3538 g_set_error (error, G_KEY_FILE_ERROR,
3539 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3540 _("Key file does not have group '%s'"),
3541 group_name ? group_name : "(null)");
3546 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3549 *has_key = pair != NULL;
3554 * g_key_file_has_key: (skip)
3555 * @key_file: a #GKeyFile
3556 * @group_name: a group name
3558 * @error: return location for a #GError
3560 * Looks whether the key file has the key @key in the group
3563 * Note that this function does not follow the rules for #GError strictly;
3564 * the return value both carries meaning and signals an error. To use
3565 * this function, you must pass a #GError pointer in @error, and check
3566 * whether it is not %NULL to see if an error occurred.
3568 * Language bindings should use g_key_file_get_value() to test whether
3569 * or not a key exists.
3571 * Return value: %TRUE if @key is a part of @group_name, %FALSE otherwise
3576 g_key_file_has_key (GKeyFile *key_file,
3577 const gchar *group_name,
3581 GError *temp_error = NULL;
3584 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3590 g_propagate_error (error, temp_error);
3596 g_key_file_add_group (GKeyFile *key_file,
3597 const gchar *group_name)
3599 GKeyFileGroup *group;
3601 g_return_if_fail (key_file != NULL);
3602 g_return_if_fail (g_key_file_is_group_name (group_name));
3604 group = g_key_file_lookup_group (key_file, group_name);
3607 key_file->current_group = group;
3611 group = g_slice_new0 (GKeyFileGroup);
3612 group->name = g_strdup (group_name);
3613 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3614 key_file->groups = g_list_prepend (key_file->groups, group);
3615 key_file->current_group = group;
3617 if (key_file->start_group == NULL)
3618 key_file->start_group = group;
3620 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3624 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3629 g_free (pair->value);
3630 g_slice_free (GKeyFileKeyValuePair, pair);
3634 /* Be careful not to call this function on a node with data in the
3635 * lookup map without removing it from the lookup map, first.
3637 * Some current cases where this warning is not a concern are
3639 * - the node being removed is a comment node
3640 * - the entire lookup map is getting destroyed soon after
3644 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3645 GKeyFileGroup *group,
3649 GKeyFileKeyValuePair *pair;
3651 pair = (GKeyFileKeyValuePair *) pair_node->data;
3653 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3655 g_warn_if_fail (pair->value != NULL);
3657 g_key_file_key_value_pair_free (pair);
3659 g_list_free_1 (pair_node);
3663 g_key_file_remove_group_node (GKeyFile *key_file,
3666 GKeyFileGroup *group;
3669 group = (GKeyFileGroup *) group_node->data;
3672 g_hash_table_remove (key_file->group_hash, group->name);
3674 /* If the current group gets deleted make the current group the last
3677 if (key_file->current_group == group)
3679 /* groups should always contain at least the top comment group,
3680 * unless g_key_file_clear has been called
3682 if (key_file->groups)
3683 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3685 key_file->current_group = NULL;
3688 /* If the start group gets deleted make the start group the first
3691 if (key_file->start_group == group)
3693 tmp = g_list_last (key_file->groups);
3696 if (tmp != group_node &&
3697 ((GKeyFileGroup *) tmp->data)->name != NULL)
3704 key_file->start_group = (GKeyFileGroup *) tmp->data;
3706 key_file->start_group = NULL;
3709 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3711 tmp = group->key_value_pairs;
3718 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3721 g_warn_if_fail (group->key_value_pairs == NULL);
3725 g_key_file_key_value_pair_free (group->comment);
3726 group->comment = NULL;
3729 if (group->lookup_map)
3731 g_hash_table_destroy (group->lookup_map);
3732 group->lookup_map = NULL;
3735 g_free ((gchar *) group->name);
3736 g_slice_free (GKeyFileGroup, group);
3737 g_list_free_1 (group_node);
3741 * g_key_file_remove_group:
3742 * @key_file: a #GKeyFile
3743 * @group_name: a group name
3744 * @error: return location for a #GError or %NULL
3746 * Removes the specified group, @group_name,
3747 * from the key file.
3749 * Returns: %TRUE if the group was removed, %FALSE otherwise
3754 g_key_file_remove_group (GKeyFile *key_file,
3755 const gchar *group_name,
3760 g_return_val_if_fail (key_file != NULL, FALSE);
3761 g_return_val_if_fail (group_name != NULL, FALSE);
3763 group_node = g_key_file_lookup_group_node (key_file, group_name);
3767 g_set_error (error, G_KEY_FILE_ERROR,
3768 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3769 _("Key file does not have group '%s'"),
3774 g_key_file_remove_group_node (key_file, group_node);
3780 g_key_file_add_key_value_pair (GKeyFile *key_file,
3781 GKeyFileGroup *group,
3782 GKeyFileKeyValuePair *pair)
3784 g_hash_table_replace (group->lookup_map, pair->key, pair);
3785 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3789 g_key_file_add_key (GKeyFile *key_file,
3790 GKeyFileGroup *group,
3794 GKeyFileKeyValuePair *pair;
3796 pair = g_slice_new (GKeyFileKeyValuePair);
3797 pair->key = g_strdup (key);
3798 pair->value = g_strdup (value);
3800 g_key_file_add_key_value_pair (key_file, group, pair);
3804 * g_key_file_remove_key:
3805 * @key_file: a #GKeyFile
3806 * @group_name: a group name
3807 * @key: a key name to remove
3808 * @error: return location for a #GError or %NULL
3810 * Removes @key in @group_name from the key file.
3812 * Returns: %TRUE if the key was removed, %FALSE otherwise
3817 g_key_file_remove_key (GKeyFile *key_file,
3818 const gchar *group_name,
3822 GKeyFileGroup *group;
3823 GKeyFileKeyValuePair *pair;
3825 g_return_val_if_fail (key_file != NULL, FALSE);
3826 g_return_val_if_fail (group_name != NULL, FALSE);
3827 g_return_val_if_fail (key != NULL, FALSE);
3831 group = g_key_file_lookup_group (key_file, group_name);
3834 g_set_error (error, G_KEY_FILE_ERROR,
3835 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3836 _("Key file does not have group '%s'"),
3837 group_name ? group_name : "(null)");
3841 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3845 g_set_error (error, G_KEY_FILE_ERROR,
3846 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3847 _("Key file does not have key '%s' in group '%s'"),
3852 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3853 g_hash_table_remove (group->lookup_map, pair->key);
3854 g_key_file_key_value_pair_free (pair);
3860 g_key_file_lookup_group_node (GKeyFile *key_file,
3861 const gchar *group_name)
3863 GKeyFileGroup *group;
3866 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3868 group = (GKeyFileGroup *) tmp->data;
3870 if (group && group->name && strcmp (group->name, group_name) == 0)
3877 static GKeyFileGroup *
3878 g_key_file_lookup_group (GKeyFile *key_file,
3879 const gchar *group_name)
3881 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3885 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3886 GKeyFileGroup *group,
3891 for (key_node = group->key_value_pairs;
3893 key_node = key_node->next)
3895 GKeyFileKeyValuePair *pair;
3897 pair = (GKeyFileKeyValuePair *) key_node->data;
3899 if (pair->key && strcmp (pair->key, key) == 0)
3906 static GKeyFileKeyValuePair *
3907 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3908 GKeyFileGroup *group,
3911 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3914 /* Lines starting with # or consisting entirely of whitespace are merely
3915 * recorded, not parsed. This function assumes all leading whitespace
3916 * has been stripped.
3919 g_key_file_line_is_comment (const gchar *line)
3921 return (*line == '#' || *line == '\0' || *line == '\n');
3925 g_key_file_is_group_name (const gchar *name)
3932 p = q = (gchar *) name;
3933 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3934 q = g_utf8_find_next_char (q, NULL);
3936 if (*q != '\0' || q == p)
3943 g_key_file_is_key_name (const gchar *name)
3950 p = q = (gchar *) name;
3951 /* We accept a little more than the desktop entry spec says,
3952 * since gnome-vfs uses mime-types as keys in its cache.
3954 while (*q && *q != '=' && *q != '[' && *q != ']')
3955 q = g_utf8_find_next_char (q, NULL);
3957 /* No empty keys, please */
3961 /* We accept spaces in the middle of keys to not break
3962 * existing apps, but we don't tolerate initial or final
3963 * spaces, which would lead to silent corruption when
3964 * rereading the file.
3966 if (*p == ' ' || q[-1] == ' ')
3972 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3973 q = g_utf8_find_next_char (q, NULL);
3987 /* A group in a key file is made up of a starting '[' followed by one
3988 * or more letters making up the group name followed by ']'.
3991 g_key_file_line_is_group (const gchar *line)
4001 while (*p && *p != ']')
4002 p = g_utf8_find_next_char (p, NULL);
4007 /* silently accept whitespace after the ] */
4008 p = g_utf8_find_next_char (p, NULL);
4009 while (*p == ' ' || *p == '\t')
4010 p = g_utf8_find_next_char (p, NULL);
4019 g_key_file_line_is_key_value_pair (const gchar *line)
4023 p = (gchar *) g_utf8_strchr (line, -1, '=');
4028 /* Key must be non-empty
4037 g_key_file_parse_value_as_string (GKeyFile *key_file,
4042 gchar *string_value, *p, *q0, *q;
4044 string_value = g_new (gchar, strlen (value) + 1);
4046 p = (gchar *) value;
4047 q0 = q = string_value;
4077 g_set_error_literal (error, G_KEY_FILE_ERROR,
4078 G_KEY_FILE_ERROR_INVALID_VALUE,
4079 _("Key file contains escape character "
4084 if (pieces && *p == key_file->list_separator)
4085 *q = key_file->list_separator;
4099 g_set_error (error, G_KEY_FILE_ERROR,
4100 G_KEY_FILE_ERROR_INVALID_VALUE,
4101 _("Key file contains invalid escape "
4102 "sequence '%s'"), sequence);
4111 if (pieces && (*p == key_file->list_separator))
4113 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4129 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4130 *pieces = g_slist_reverse (*pieces);
4133 return string_value;
4137 g_key_file_parse_string_as_value (GKeyFile *key_file,
4138 const gchar *string,
4139 gboolean escape_separator)
4141 gchar *value, *p, *q;
4143 gboolean parsing_leading_space;
4145 length = strlen (string) + 1;
4147 /* Worst case would be that every character needs to be escaped.
4148 * In other words every character turns to two characters
4150 value = g_new (gchar, 2 * length);
4152 p = (gchar *) string;
4154 parsing_leading_space = TRUE;
4155 while (p < (string + length - 1))
4157 gchar escaped_character[3] = { '\\', 0, 0 };
4162 if (parsing_leading_space)
4164 escaped_character[1] = 's';
4165 strcpy (q, escaped_character);
4175 if (parsing_leading_space)
4177 escaped_character[1] = 't';
4178 strcpy (q, escaped_character);
4188 escaped_character[1] = 'n';
4189 strcpy (q, escaped_character);
4193 escaped_character[1] = 'r';
4194 strcpy (q, escaped_character);
4198 escaped_character[1] = '\\';
4199 strcpy (q, escaped_character);
4201 parsing_leading_space = FALSE;
4204 if (escape_separator && *p == key_file->list_separator)
4206 escaped_character[1] = key_file->list_separator;
4207 strcpy (q, escaped_character);
4209 parsing_leading_space = TRUE;
4215 parsing_leading_space = FALSE;
4227 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4236 long_value = strtol (value, &eof_int, 10);
4238 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4240 gchar *value_utf8 = _g_utf8_make_valid (value);
4241 g_set_error (error, G_KEY_FILE_ERROR,
4242 G_KEY_FILE_ERROR_INVALID_VALUE,
4243 _("Value '%s' cannot be interpreted "
4244 "as a number."), value_utf8);
4245 g_free (value_utf8);
4250 int_value = long_value;
4251 if (int_value != long_value || errno == ERANGE)
4253 gchar *value_utf8 = _g_utf8_make_valid (value);
4256 G_KEY_FILE_ERROR_INVALID_VALUE,
4257 _("Integer value '%s' out of range"),
4259 g_free (value_utf8);
4268 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4272 return g_strdup_printf ("%d", value);
4276 g_key_file_parse_value_as_double (GKeyFile *key_file,
4280 gchar *end_of_valid_d;
4281 gdouble double_value = 0;
4283 double_value = g_ascii_strtod (value, &end_of_valid_d);
4285 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4287 gchar *value_utf8 = _g_utf8_make_valid (value);
4288 g_set_error (error, G_KEY_FILE_ERROR,
4289 G_KEY_FILE_ERROR_INVALID_VALUE,
4290 _("Value '%s' cannot be interpreted "
4291 "as a float number."),
4293 g_free (value_utf8);
4296 return double_value;
4300 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4306 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4308 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4311 value_utf8 = _g_utf8_make_valid (value);
4312 g_set_error (error, G_KEY_FILE_ERROR,
4313 G_KEY_FILE_ERROR_INVALID_VALUE,
4314 _("Value '%s' cannot be interpreted "
4315 "as a boolean."), value_utf8);
4316 g_free (value_utf8);
4322 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4326 return g_strdup ("true");
4328 return g_strdup ("false");
4332 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4339 string = g_string_sized_new (512);
4341 lines = g_strsplit (value, "\n", 0);
4343 for (i = 0; lines[i] != NULL; i++)
4345 if (lines[i][0] != '#')
4346 g_string_append_printf (string, "%s\n", lines[i]);
4348 g_string_append_printf (string, "%s\n", lines[i] + 1);
4352 return g_string_free (string, FALSE);
4356 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4357 const gchar *comment)
4363 string = g_string_sized_new (512);
4365 lines = g_strsplit (comment, "\n", 0);
4367 for (i = 0; lines[i] != NULL; i++)
4368 g_string_append_printf (string, "#%s%s", lines[i],
4369 lines[i + 1] == NULL? "" : "\n");
4372 return g_string_free (string, FALSE);
4376 * g_key_file_save_to_file:
4377 * @key_file: a #GKeyFile
4378 * @filename: the name of the file to write to
4379 * @error: a pointer to a %NULL #GError, or %NULL
4381 * Writes the contents of @key_file to @filename using
4382 * g_file_set_contents().
4384 * This function can fail for any of the reasons that
4385 * g_file_set_contents() may fail.
4387 * Returns: %TRUE if successful, else %FALSE with @error set
4392 g_key_file_save_to_file (GKeyFile *key_file,
4393 const gchar *filename,
4400 g_return_val_if_fail (key_file != NULL, FALSE);
4401 g_return_val_if_fail (filename != NULL, FALSE);
4402 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4404 contents = g_key_file_to_data (key_file, &length, NULL);
4405 g_assert (contents != NULL);
4407 success = g_file_set_contents (filename, contents, length, error);