1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
4 * Copyright 2009-2010 Collabora Ltd.
5 * Copyright 2009 Nokia Corporation
7 * Written by Ray Strode <rstrode@redhat.com>
8 * Matthias Clasen <mclasen@redhat.com>
10 * GLib is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * GLib is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GLib; see the file COPYING.LIB. If not,
22 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
37 #include <sys/types.h>
45 #define fstat(a,b) _fstati64(a,b)
49 #define S_ISREG(mode) ((mode)&_S_IFREG)
52 #endif /* G_OS_WIN23 */
57 #include "gfileutils.h"
63 #include "gmessages.h"
66 #include "gstrfuncs.h"
72 * @title: Key-value file parser
73 * @short_description: parses .ini-like config files
75 * #GKeyFile lets you parse, edit or create files containing groups of
76 * key-value pairs, which we call <firstterm>key files</firstterm> for
77 * lack of a better name. Several freedesktop.org specifications use
78 * key files now, e.g the
79 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
80 * Entry Specification</ulink> and the
81 * <ulink url="http://freedesktop.org/Standards/icon-theme-spec">Icon
82 * Theme Specification</ulink>.
84 * The syntax of key files is described in detail in the
85 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
86 * Entry Specification</ulink>, here is a quick summary: Key files
87 * consists of groups of key-value pairs, interspersed with comments.
90 * # this is just an example
91 * # there can be comments before the first group
95 * Name=Key File Example\tthis value shows\nescaping
97 * # localized strings are stored in multiple key-value pairs
100 * Welcome[fr_FR]=Bonjour
102 * Welcome[be@latin]=Hello
106 * Numbers=2;20;-200;0
108 * Booleans=true;false;true;true
111 * Lines beginning with a '#' and blank lines are considered comments.
113 * Groups are started by a header line containing the group name enclosed
114 * in '[' and ']', and ended implicitly by the start of the next group or
115 * the end of the file. Each key-value pair must be contained in a group.
117 * Key-value pairs generally have the form <literal>key=value</literal>,
118 * with the exception of localized strings, which have the form
119 * <literal>key[locale]=value</literal>, with a locale identifier of the
120 * form <literal>lang_COUNTRY@MODIFIER</literal> where
121 * <literal>COUNTRY</literal> and <literal>MODIFIER</literal> are optional.
122 * Space before and after the '=' character are ignored. Newline, tab,
123 * carriage return and backslash characters in value are escaped as \n,
124 * \t, \r, and \\, respectively. To preserve leading spaces in values,
125 * these can also be escaped as \s.
127 * Key files can store strings (possibly with localized variants), integers,
128 * booleans and lists of these. Lists are separated by a separator character,
129 * typically ';' or ','. To use the list separator character in a value in
130 * a list, it has to be escaped by prefixing it with a backslash.
132 * This syntax is obviously inspired by the .ini files commonly met
133 * on Windows, but there are some important differences:
135 * <listitem>.ini files use the ';' character to begin comments,
136 * key files use the '#' character.</listitem>
137 * <listitem>Key files do not allow for ungrouped keys meaning only
138 * comments can precede the first group.</listitem>
139 * <listitem>Key files are always encoded in UTF-8.</listitem>
140 * <listitem>Key and Group names are case-sensitive. For example, a
141 * group called <literal>[GROUP]</literal> is a different from
142 * <literal>[group]</literal>.</listitem>
143 * <listitem>.ini files don't have a strongly typed boolean entry type,
144 * they only have GetProfileInt(). In key files, only
145 * <literal>true</literal> and <literal>false</literal> (in lower case)
146 * are allowed.</listitem>
149 * Note that in contrast to the
150 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
151 * Entry Specification</ulink>, groups in key files may contain the same
152 * key multiple times; the last entry wins. Key files may also contain
153 * multiple groups with the same name; they are merged together.
154 * Another difference is that keys and group names in key files are not
155 * restricted to ASCII characters.
161 * Error domain for key file parsing. Errors in this domain will
162 * be from the #GKeyFileError enumeration.
164 * See #GError for information on error domains.
169 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
170 * an unknown encoding
171 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
172 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
173 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
174 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
175 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
177 * Error codes returned by key file parsing.
182 * @G_KEY_FILE_NONE: No flags, default behaviour
183 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
184 * (possibly modified) contents of the key file back to a file;
185 * otherwise all comments will be lost when the key file is
187 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
188 * (possibly modified) contents of the key file back to a file;
189 * otherwise only the translations for the current language will be
192 * Flags which influence the parsing.
196 * G_KEY_FILE_DESKTOP_GROUP:
198 * The name of the main group of a desktop entry file, as defined in the
199 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
200 * Entry Specification</ulink>. Consult the specification for more
201 * details about the meanings of the keys below.
207 * G_KEY_FILE_DESKTOP_KEY_TYPE:
209 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
210 * giving the type of the desktop entry. Usually
211 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
212 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
213 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
219 * G_KEY_FILE_DESKTOP_KEY_VERSION:
221 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
222 * giving the version of the Desktop Entry Specification used for
223 * the desktop entry file.
229 * G_KEY_FILE_DESKTOP_KEY_NAME:
231 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
232 * string giving the specific name of the desktop entry.
238 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
240 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
241 * string giving the generic name of the desktop entry.
247 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
249 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
250 * stating whether the desktop entry should be shown in menus.
256 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
258 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
259 * string giving the tooltip for the desktop entry.
265 * G_KEY_FILE_DESKTOP_KEY_ICON:
267 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
268 * string giving the name of the icon to be displayed for the desktop
275 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
277 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
278 * stating whether the desktop entry has been deleted by the user.
284 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
286 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
287 * strings identifying the environments that should display the
294 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
296 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
297 * strings identifying the environments that should not display the
304 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
306 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
307 * giving the file name of a binary on disk used to determine if the
308 * program is actually installed. It is only valid for desktop entries
309 * with the <literal>Application</literal> type.
315 * G_KEY_FILE_DESKTOP_KEY_EXEC:
317 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
318 * giving the command line to execute. It is only valid for desktop
319 * entries with the <literal>Application</literal> type.
325 * G_KEY_FILE_DESKTOP_KEY_PATH:
327 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
328 * containing the working directory to run the program in. It is only
329 * valid for desktop entries with the <literal>Application</literal> type.
335 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
337 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
338 * stating whether the program should be run in a terminal window.
339 * It is only valid for desktop entries with the
340 * <literal>Application</literal> type.
346 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
348 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
349 * of strings giving the MIME types supported by this desktop entry.
355 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
357 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
358 * of strings giving the categories in which the desktop entry
359 * should be shown in a menu.
365 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
367 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
368 * stating whether the application supports the <ulink
369 * url="http://www.freedesktop.org/Standards/startup-notification-spec">Startup
370 * Notification Protocol Specification</ulink>.
376 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
378 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
379 * identifying the WM class or name hint of a window that the application
380 * will create, which can be used to emulate Startup Notification with
381 * older applications.
387 * G_KEY_FILE_DESKTOP_KEY_URL :
389 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
390 * giving the URL to access. It is only valid for desktop entries
391 * with the <literal>Link</literal> type.
397 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
399 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
400 * entries representing applications.
406 * G_KEY_FILE_DESKTOP_TYPE_LINK:
408 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
409 * entries representing links to documents.
415 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
417 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
418 * entries representing directories.
423 typedef struct _GKeyFileGroup GKeyFileGroup;
428 * The GKeyFile struct contains only private data
429 * and should not be accessed directly.
434 GHashTable *group_hash;
436 GKeyFileGroup *start_group;
437 GKeyFileGroup *current_group;
439 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
441 /* Used for sizing the output buffer during serialization */
442 gsize approximate_size;
444 gchar list_separator;
450 volatile gint ref_count;
453 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
455 struct _GKeyFileGroup
457 const gchar *name; /* NULL for above first group (which will be comments) */
459 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
460 gboolean has_trailing_blank_line;
462 GList *key_value_pairs;
464 /* Used in parallel with key_value_pairs for
465 * increased lookup performance
467 GHashTable *lookup_map;
470 struct _GKeyFileKeyValuePair
472 gchar *key; /* NULL for comments */
476 static gint find_file_in_data_dirs (const gchar *file,
477 const gchar **data_dirs,
480 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
484 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
485 const gchar *group_name);
486 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
487 const gchar *group_name);
489 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
490 GKeyFileGroup *group,
492 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
493 GKeyFileGroup *group,
496 static void g_key_file_remove_group_node (GKeyFile *key_file,
498 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
499 GKeyFileGroup *group,
502 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
503 GKeyFileGroup *group,
504 GKeyFileKeyValuePair *pair);
505 static void g_key_file_add_key (GKeyFile *key_file,
506 GKeyFileGroup *group,
509 static void g_key_file_add_group (GKeyFile *key_file,
510 const gchar *group_name);
511 static gboolean g_key_file_is_group_name (const gchar *name);
512 static gboolean g_key_file_is_key_name (const gchar *name);
513 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
514 static gboolean g_key_file_line_is_comment (const gchar *line);
515 static gboolean g_key_file_line_is_group (const gchar *line);
516 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
517 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
521 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
523 gboolean escape_separator);
524 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
527 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
529 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
532 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
535 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
537 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
539 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
540 const gchar *comment);
541 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
545 static void g_key_file_parse_comment (GKeyFile *key_file,
549 static void g_key_file_parse_group (GKeyFile *key_file,
553 static gchar *key_get_locale (const gchar *key);
554 static void g_key_file_parse_data (GKeyFile *key_file,
558 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
563 g_key_file_error_quark (void)
565 return g_quark_from_static_string ("g-key-file-error-quark");
569 g_key_file_init (GKeyFile *key_file)
571 key_file->current_group = g_slice_new0 (GKeyFileGroup);
572 key_file->groups = g_list_prepend (NULL, key_file->current_group);
573 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
574 key_file->start_group = NULL;
575 key_file->parse_buffer = g_string_sized_new (128);
576 key_file->approximate_size = 0;
577 key_file->list_separator = ';';
579 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
583 g_key_file_clear (GKeyFile *key_file)
585 GList *tmp, *group_node;
587 if (key_file->locales)
589 g_strfreev (key_file->locales);
590 key_file->locales = NULL;
593 if (key_file->parse_buffer)
595 g_string_free (key_file->parse_buffer, TRUE);
596 key_file->parse_buffer = NULL;
599 tmp = key_file->groups;
604 g_key_file_remove_group_node (key_file, group_node);
607 if (key_file->group_hash != NULL)
609 g_hash_table_destroy (key_file->group_hash);
610 key_file->group_hash = NULL;
613 g_warn_if_fail (key_file->groups == NULL);
620 * Creates a new empty #GKeyFile object. Use
621 * g_key_file_load_from_file(), g_key_file_load_from_data(),
622 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
623 * read an existing key file.
625 * Return value: (transfer full): an empty #GKeyFile.
630 g_key_file_new (void)
634 key_file = g_slice_new0 (GKeyFile);
635 key_file->ref_count = 1;
636 g_key_file_init (key_file);
642 * g_key_file_set_list_separator:
643 * @key_file: a #GKeyFile
644 * @separator: the separator
646 * Sets the character which is used to separate
647 * values in lists. Typically ';' or ',' are used
648 * as separators. The default list separator is ';'.
653 g_key_file_set_list_separator (GKeyFile *key_file,
656 g_return_if_fail (key_file != NULL);
658 key_file->list_separator = separator;
662 /* Iterates through all the directories in *dirs trying to
663 * open file. When it successfully locates and opens a file it
664 * returns the file descriptor to the open file. It also
665 * outputs the absolute path of the file in output_file.
668 find_file_in_data_dirs (const gchar *file,
673 const gchar **data_dirs, *data_dir;
685 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
687 gchar *candidate_file, *sub_dir;
689 candidate_file = (gchar *) file;
690 sub_dir = g_strdup ("");
691 while (candidate_file != NULL && fd == -1)
695 path = g_build_filename (data_dir, sub_dir,
696 candidate_file, NULL);
698 fd = g_open (path, O_RDONLY, 0);
706 candidate_file = strchr (candidate_file, '-');
708 if (candidate_file == NULL)
714 sub_dir = g_strndup (file, candidate_file - file - 1);
716 for (p = sub_dir; *p != '\0'; p++)
719 *p = G_DIR_SEPARATOR;
728 g_set_error_literal (error, G_KEY_FILE_ERROR,
729 G_KEY_FILE_ERROR_NOT_FOUND,
730 _("Valid key file could not be "
731 "found in search dirs"));
734 if (output_file != NULL && fd > 0)
735 *output_file = g_strdup (path);
743 g_key_file_load_from_fd (GKeyFile *key_file,
748 GError *key_file_error = NULL;
750 struct stat stat_buf;
751 gchar read_buf[4096];
753 if (fstat (fd, &stat_buf) < 0)
755 g_set_error_literal (error, G_FILE_ERROR,
756 g_file_error_from_errno (errno),
761 if (!S_ISREG (stat_buf.st_mode))
763 g_set_error_literal (error, G_KEY_FILE_ERROR,
764 G_KEY_FILE_ERROR_PARSE,
765 _("Not a regular file"));
769 if (key_file->approximate_size > 0)
771 g_key_file_clear (key_file);
772 g_key_file_init (key_file);
774 key_file->flags = flags;
778 bytes_read = read (fd, read_buf, 4096);
780 if (bytes_read == 0) /* End of File */
785 if (errno == EINTR || errno == EAGAIN)
788 g_set_error_literal (error, G_FILE_ERROR,
789 g_file_error_from_errno (errno),
794 g_key_file_parse_data (key_file,
795 read_buf, bytes_read,
798 while (!key_file_error);
802 g_propagate_error (error, key_file_error);
806 g_key_file_flush_parse_buffer (key_file, &key_file_error);
810 g_propagate_error (error, key_file_error);
818 * g_key_file_load_from_file:
819 * @key_file: an empty #GKeyFile struct
820 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
821 * @flags: flags from #GKeyFileFlags
822 * @error: return location for a #GError, or %NULL
824 * Loads a key file into an empty #GKeyFile structure.
825 * If the file could not be loaded then %error is set to
826 * either a #GFileError or #GKeyFileError.
828 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
833 g_key_file_load_from_file (GKeyFile *key_file,
838 GError *key_file_error = NULL;
841 g_return_val_if_fail (key_file != NULL, FALSE);
842 g_return_val_if_fail (file != NULL, FALSE);
844 fd = g_open (file, O_RDONLY, 0);
848 g_set_error_literal (error, G_FILE_ERROR,
849 g_file_error_from_errno (errno),
854 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
859 g_propagate_error (error, key_file_error);
867 * g_key_file_load_from_data:
868 * @key_file: an empty #GKeyFile struct
869 * @data: (array length=length): key file loaded in memory
870 * @length: the length of @data in bytes
871 * @flags: flags from #GKeyFileFlags
872 * @error: return location for a #GError, or %NULL
874 * Loads a key file from memory into an empty #GKeyFile structure.
875 * If the object cannot be created then %error is set to a #GKeyFileError.
877 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
882 g_key_file_load_from_data (GKeyFile *key_file,
888 GError *key_file_error = NULL;
890 g_return_val_if_fail (key_file != NULL, FALSE);
891 g_return_val_if_fail (data != NULL, FALSE);
892 g_return_val_if_fail (length != 0, FALSE);
894 if (length == (gsize)-1)
895 length = strlen (data);
897 if (key_file->approximate_size > 0)
899 g_key_file_clear (key_file);
900 g_key_file_init (key_file);
902 key_file->flags = flags;
904 g_key_file_parse_data (key_file, data, length, &key_file_error);
908 g_propagate_error (error, key_file_error);
912 g_key_file_flush_parse_buffer (key_file, &key_file_error);
916 g_propagate_error (error, key_file_error);
924 * g_key_file_load_from_dirs:
925 * @key_file: an empty #GKeyFile struct
926 * @file: (type filename): a relative path to a filename to open and parse
927 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
928 * @full_path: (out) (type filename): return location for a string containing the full path
929 * of the file, or %NULL
930 * @flags: flags from #GKeyFileFlags
931 * @error: return location for a #GError, or %NULL
933 * This function looks for a key file named @file in the paths
934 * specified in @search_dirs, loads the file into @key_file and
935 * returns the file's full path in @full_path. If the file could not
936 * be loaded then an %error is set to either a #GFileError or
939 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
944 g_key_file_load_from_dirs (GKeyFile *key_file,
946 const gchar **search_dirs,
951 GError *key_file_error = NULL;
952 const gchar **data_dirs;
957 g_return_val_if_fail (key_file != NULL, FALSE);
958 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
959 g_return_val_if_fail (search_dirs != NULL, FALSE);
962 data_dirs = search_dirs;
964 while (*data_dirs != NULL && !found_file)
966 g_free (output_path);
968 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
974 g_propagate_error (error, key_file_error);
978 found_file = g_key_file_load_from_fd (key_file, fd, flags,
984 g_propagate_error (error, key_file_error);
989 if (found_file && full_path)
990 *full_path = output_path;
992 g_free (output_path);
998 * g_key_file_load_from_data_dirs:
999 * @key_file: an empty #GKeyFile struct
1000 * @file: (type filename): a relative path to a filename to open and parse
1001 * @full_path: (out) (type filename): return location for a string containing the full path
1002 * of the file, or %NULL
1003 * @flags: flags from #GKeyFileFlags
1004 * @error: return location for a #GError, or %NULL
1006 * This function looks for a key file named @file in the paths
1007 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1008 * loads the file into @key_file and returns the file's full path in
1009 * @full_path. If the file could not be loaded then an %error is
1010 * set to either a #GFileError or #GKeyFileError.
1012 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1016 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1019 GKeyFileFlags flags,
1022 gchar **all_data_dirs;
1023 const gchar * user_data_dir;
1024 const gchar * const * system_data_dirs;
1026 gboolean found_file;
1028 g_return_val_if_fail (key_file != NULL, FALSE);
1029 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1031 user_data_dir = g_get_user_data_dir ();
1032 system_data_dirs = g_get_system_data_dirs ();
1033 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1036 all_data_dirs[i++] = g_strdup (user_data_dir);
1039 while (system_data_dirs[j] != NULL)
1040 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1041 all_data_dirs[i] = NULL;
1043 found_file = g_key_file_load_from_dirs (key_file,
1045 (const gchar **)all_data_dirs,
1050 g_strfreev (all_data_dirs);
1056 * g_key_file_ref: (skip)
1057 * @key_file: a #GKeyFile
1059 * Increases the reference count of @key_file.
1061 * Returns: the same @key_file.
1066 g_key_file_ref (GKeyFile *key_file)
1068 g_return_val_if_fail (key_file != NULL, NULL);
1070 g_atomic_int_inc (&key_file->ref_count);
1076 * g_key_file_free: (skip)
1077 * @key_file: a #GKeyFile
1079 * Clears all keys and groups from @key_file, and decreases the
1080 * reference count by 1. If the reference count reaches zero,
1081 * frees the key file and all its allocated memory.
1086 g_key_file_free (GKeyFile *key_file)
1088 g_return_if_fail (key_file != NULL);
1090 g_key_file_clear (key_file);
1091 g_key_file_unref (key_file);
1096 * @key_file: a #GKeyFile
1098 * Decreases the reference count of @key_file by 1. If the reference count
1099 * reaches zero, frees the key file and all its allocated memory.
1104 g_key_file_unref (GKeyFile *key_file)
1106 g_return_if_fail (key_file != NULL);
1108 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1110 g_key_file_clear (key_file);
1111 g_slice_free (GKeyFile, key_file);
1115 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1116 * true for locales that match those in g_get_language_names().
1119 g_key_file_locale_is_interesting (GKeyFile *key_file,
1120 const gchar *locale)
1124 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1127 for (i = 0; key_file->locales[i] != NULL; i++)
1129 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1137 g_key_file_parse_line (GKeyFile *key_file,
1142 GError *parse_error = NULL;
1145 g_return_if_fail (key_file != NULL);
1146 g_return_if_fail (line != NULL);
1148 line_start = (gchar *) line;
1149 while (g_ascii_isspace (*line_start))
1152 if (g_key_file_line_is_comment (line_start))
1153 g_key_file_parse_comment (key_file, line, length, &parse_error);
1154 else if (g_key_file_line_is_group (line_start))
1155 g_key_file_parse_group (key_file, line_start,
1156 length - (line_start - line),
1158 else if (g_key_file_line_is_key_value_pair (line_start))
1159 g_key_file_parse_key_value_pair (key_file, line_start,
1160 length - (line_start - line),
1164 gchar *line_utf8 = _g_utf8_make_valid (line);
1165 g_set_error (error, G_KEY_FILE_ERROR,
1166 G_KEY_FILE_ERROR_PARSE,
1167 _("Key file contains line '%s' which is not "
1168 "a key-value pair, group, or comment"),
1176 g_propagate_error (error, parse_error);
1180 g_key_file_parse_comment (GKeyFile *key_file,
1185 GKeyFileKeyValuePair *pair;
1187 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1190 g_warn_if_fail (key_file->current_group != NULL);
1192 pair = g_slice_new (GKeyFileKeyValuePair);
1194 pair->value = g_strndup (line, length);
1196 key_file->current_group->key_value_pairs =
1197 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1199 if (length == 0 || line[0] != '#')
1200 key_file->current_group->has_trailing_blank_line = TRUE;
1204 g_key_file_parse_group (GKeyFile *key_file,
1210 const gchar *group_name_start, *group_name_end;
1212 /* advance past opening '['
1214 group_name_start = line + 1;
1215 group_name_end = line + length - 1;
1217 while (*group_name_end != ']')
1220 group_name = g_strndup (group_name_start,
1221 group_name_end - group_name_start);
1223 if (!g_key_file_is_group_name (group_name))
1225 g_set_error (error, G_KEY_FILE_ERROR,
1226 G_KEY_FILE_ERROR_PARSE,
1227 _("Invalid group name: %s"), group_name);
1228 g_free (group_name);
1232 g_key_file_add_group (key_file, group_name);
1233 g_free (group_name);
1237 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1242 gchar *key, *value, *key_end, *value_start, *locale;
1243 gsize key_len, value_len;
1245 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1247 g_set_error_literal (error, G_KEY_FILE_ERROR,
1248 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1249 _("Key file does not start with a group"));
1253 key_end = value_start = strchr (line, '=');
1255 g_warn_if_fail (key_end != NULL);
1260 /* Pull the key name from the line (chomping trailing whitespace)
1262 while (g_ascii_isspace (*key_end))
1265 key_len = key_end - line + 2;
1267 g_warn_if_fail (key_len <= length);
1269 key = g_strndup (line, key_len - 1);
1271 if (!g_key_file_is_key_name (key))
1273 g_set_error (error, G_KEY_FILE_ERROR,
1274 G_KEY_FILE_ERROR_PARSE,
1275 _("Invalid key name: %s"), key);
1280 /* Pull the value from the line (chugging leading whitespace)
1282 while (g_ascii_isspace (*value_start))
1285 value_len = line + length - value_start + 1;
1287 value = g_strndup (value_start, value_len);
1289 g_warn_if_fail (key_file->start_group != NULL);
1291 if (key_file->current_group
1292 && key_file->current_group->name
1293 && strcmp (key_file->start_group->name,
1294 key_file->current_group->name) == 0
1295 && strcmp (key, "Encoding") == 0)
1297 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1299 gchar *value_utf8 = _g_utf8_make_valid (value);
1300 g_set_error (error, G_KEY_FILE_ERROR,
1301 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1302 _("Key file contains unsupported "
1303 "encoding '%s'"), value_utf8);
1304 g_free (value_utf8);
1312 /* Is this key a translation? If so, is it one that we care about?
1314 locale = key_get_locale (key);
1316 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1318 GKeyFileKeyValuePair *pair;
1320 pair = g_slice_new (GKeyFileKeyValuePair);
1322 pair->value = value;
1324 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1336 key_get_locale (const gchar *key)
1340 locale = g_strrstr (key, "[");
1342 if (locale && strlen (locale) <= 2)
1346 locale = g_strndup (locale + 1, strlen (locale) - 2);
1352 g_key_file_parse_data (GKeyFile *key_file,
1357 GError *parse_error;
1360 g_return_if_fail (key_file != NULL);
1361 g_return_if_fail (data != NULL);
1368 if (data[i] == '\n')
1370 if (key_file->parse_buffer->len > 0
1371 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1373 g_string_erase (key_file->parse_buffer,
1374 key_file->parse_buffer->len - 1,
1377 /* When a newline is encountered flush the parse buffer so that the
1378 * line can be parsed. Note that completely blank lines won't show
1379 * up in the parse buffer, so they get parsed directly.
1381 if (key_file->parse_buffer->len > 0)
1382 g_key_file_flush_parse_buffer (key_file, &parse_error);
1384 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1388 g_propagate_error (error, parse_error);
1395 const gchar *start_of_line;
1396 const gchar *end_of_line;
1399 start_of_line = data + i;
1400 end_of_line = memchr (start_of_line, '\n', length - i);
1402 if (end_of_line == NULL)
1403 end_of_line = data + length;
1405 line_length = end_of_line - start_of_line;
1407 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1412 key_file->approximate_size += length;
1416 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1419 GError *file_error = NULL;
1421 g_return_if_fail (key_file != NULL);
1425 if (key_file->parse_buffer->len > 0)
1427 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1428 key_file->parse_buffer->len,
1430 g_string_erase (key_file->parse_buffer, 0, -1);
1434 g_propagate_error (error, file_error);
1441 * g_key_file_to_data:
1442 * @key_file: a #GKeyFile
1443 * @length: (out) (allow-none): return location for the length of the
1444 * returned string, or %NULL
1445 * @error: return location for a #GError, or %NULL
1447 * This function outputs @key_file as a string.
1449 * Note that this function never reports an error,
1450 * so it is safe to pass %NULL as @error.
1452 * Return value: a newly allocated string holding
1453 * the contents of the #GKeyFile
1458 g_key_file_to_data (GKeyFile *key_file,
1462 GString *data_string;
1463 GList *group_node, *key_file_node;
1464 gboolean has_blank_line = TRUE;
1466 g_return_val_if_fail (key_file != NULL, NULL);
1468 data_string = g_string_sized_new (2 * key_file->approximate_size);
1470 for (group_node = g_list_last (key_file->groups);
1472 group_node = group_node->prev)
1474 GKeyFileGroup *group;
1476 group = (GKeyFileGroup *) group_node->data;
1478 /* separate groups by at least an empty line */
1479 if (!has_blank_line)
1480 g_string_append_c (data_string, '\n');
1481 has_blank_line = group->has_trailing_blank_line;
1483 if (group->comment != NULL)
1484 g_string_append_printf (data_string, "%s\n", group->comment->value);
1486 if (group->name != NULL)
1487 g_string_append_printf (data_string, "[%s]\n", group->name);
1489 for (key_file_node = g_list_last (group->key_value_pairs);
1490 key_file_node != NULL;
1491 key_file_node = key_file_node->prev)
1493 GKeyFileKeyValuePair *pair;
1495 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1497 if (pair->key != NULL)
1498 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1500 g_string_append_printf (data_string, "%s\n", pair->value);
1505 *length = data_string->len;
1507 return g_string_free (data_string, FALSE);
1511 * g_key_file_get_keys:
1512 * @key_file: a #GKeyFile
1513 * @group_name: a group name
1514 * @length: (out): return location for the number of keys returned, or %NULL
1515 * @error: return location for a #GError, or %NULL
1517 * Returns all keys for the group name @group_name. The array of
1518 * returned keys will be %NULL-terminated, so @length may
1519 * optionally be %NULL. In the event that the @group_name cannot
1520 * be found, %NULL is returned and @error is set to
1521 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1523 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1524 * Use g_strfreev() to free it.
1529 g_key_file_get_keys (GKeyFile *key_file,
1530 const gchar *group_name,
1534 GKeyFileGroup *group;
1539 g_return_val_if_fail (key_file != NULL, NULL);
1540 g_return_val_if_fail (group_name != NULL, NULL);
1542 group = g_key_file_lookup_group (key_file, group_name);
1546 g_set_error (error, G_KEY_FILE_ERROR,
1547 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1548 _("Key file does not have group '%s'"),
1549 group_name ? group_name : "(null)");
1554 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1556 GKeyFileKeyValuePair *pair;
1558 pair = (GKeyFileKeyValuePair *) tmp->data;
1564 keys = g_new (gchar *, num_keys + 1);
1567 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1569 GKeyFileKeyValuePair *pair;
1571 pair = (GKeyFileKeyValuePair *) tmp->data;
1575 keys[i] = g_strdup (pair->key);
1580 keys[num_keys] = NULL;
1589 * g_key_file_get_start_group:
1590 * @key_file: a #GKeyFile
1592 * Returns the name of the start group of the file.
1594 * Return value: The start group of the key file.
1599 g_key_file_get_start_group (GKeyFile *key_file)
1601 g_return_val_if_fail (key_file != NULL, NULL);
1603 if (key_file->start_group)
1604 return g_strdup (key_file->start_group->name);
1610 * g_key_file_get_groups:
1611 * @key_file: a #GKeyFile
1612 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1614 * Returns all groups in the key file loaded with @key_file.
1615 * The array of returned groups will be %NULL-terminated, so
1616 * @length may optionally be %NULL.
1618 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1619 * Use g_strfreev() to free it.
1623 g_key_file_get_groups (GKeyFile *key_file,
1628 gsize i, num_groups;
1630 g_return_val_if_fail (key_file != NULL, NULL);
1632 num_groups = g_list_length (key_file->groups);
1634 g_return_val_if_fail (num_groups > 0, NULL);
1636 group_node = g_list_last (key_file->groups);
1638 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1640 /* Only need num_groups instead of num_groups + 1
1641 * because the first group of the file (last in the
1642 * list) is always the comment group at the top,
1645 groups = g_new (gchar *, num_groups);
1649 for (group_node = group_node->prev;
1651 group_node = group_node->prev)
1653 GKeyFileGroup *group;
1655 group = (GKeyFileGroup *) group_node->data;
1657 g_warn_if_fail (group->name != NULL);
1659 groups[i++] = g_strdup (group->name);
1670 * g_key_file_get_value:
1671 * @key_file: a #GKeyFile
1672 * @group_name: a group name
1674 * @error: return location for a #GError, or %NULL
1676 * Returns the raw value associated with @key under @group_name.
1677 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1679 * In the event the key cannot be found, %NULL is returned and
1680 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1681 * event that the @group_name cannot be found, %NULL is returned
1682 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1685 * Return value: a newly allocated string or %NULL if the specified
1686 * key cannot be found.
1691 g_key_file_get_value (GKeyFile *key_file,
1692 const gchar *group_name,
1696 GKeyFileGroup *group;
1697 GKeyFileKeyValuePair *pair;
1698 gchar *value = NULL;
1700 g_return_val_if_fail (key_file != NULL, NULL);
1701 g_return_val_if_fail (group_name != NULL, NULL);
1702 g_return_val_if_fail (key != NULL, NULL);
1704 group = g_key_file_lookup_group (key_file, group_name);
1708 g_set_error (error, G_KEY_FILE_ERROR,
1709 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1710 _("Key file does not have group '%s'"),
1711 group_name ? group_name : "(null)");
1715 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1718 value = g_strdup (pair->value);
1720 g_set_error (error, G_KEY_FILE_ERROR,
1721 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1722 _("Key file does not have key '%s'"), key);
1728 * g_key_file_set_value:
1729 * @key_file: a #GKeyFile
1730 * @group_name: a group name
1734 * Associates a new value with @key under @group_name.
1736 * If @key cannot be found then it is created. If @group_name cannot
1737 * be found then it is created. To set an UTF-8 string which may contain
1738 * characters that need escaping (such as newlines or spaces), use
1739 * g_key_file_set_string().
1744 g_key_file_set_value (GKeyFile *key_file,
1745 const gchar *group_name,
1749 GKeyFileGroup *group;
1750 GKeyFileKeyValuePair *pair;
1752 g_return_if_fail (key_file != NULL);
1753 g_return_if_fail (g_key_file_is_group_name (group_name));
1754 g_return_if_fail (g_key_file_is_key_name (key));
1755 g_return_if_fail (value != NULL);
1757 group = g_key_file_lookup_group (key_file, group_name);
1761 g_key_file_add_group (key_file, group_name);
1762 group = (GKeyFileGroup *) key_file->groups->data;
1764 g_key_file_add_key (key_file, group, key, value);
1768 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1771 g_key_file_add_key (key_file, group, key, value);
1774 g_free (pair->value);
1775 pair->value = g_strdup (value);
1781 * g_key_file_get_string:
1782 * @key_file: a #GKeyFile
1783 * @group_name: a group name
1785 * @error: return location for a #GError, or %NULL
1787 * Returns the string value associated with @key under @group_name.
1788 * Unlike g_key_file_get_value(), this function handles escape sequences
1791 * In the event the key cannot be found, %NULL is returned and
1792 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1793 * event that the @group_name cannot be found, %NULL is returned
1794 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1796 * Return value: a newly allocated string or %NULL if the specified
1797 * key cannot be found.
1802 g_key_file_get_string (GKeyFile *key_file,
1803 const gchar *group_name,
1807 gchar *value, *string_value;
1808 GError *key_file_error;
1810 g_return_val_if_fail (key_file != NULL, NULL);
1811 g_return_val_if_fail (group_name != NULL, NULL);
1812 g_return_val_if_fail (key != NULL, NULL);
1814 key_file_error = NULL;
1816 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1820 g_propagate_error (error, key_file_error);
1824 if (!g_utf8_validate (value, -1, NULL))
1826 gchar *value_utf8 = _g_utf8_make_valid (value);
1827 g_set_error (error, G_KEY_FILE_ERROR,
1828 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1829 _("Key file contains key '%s' with value '%s' "
1830 "which is not UTF-8"), key, value_utf8);
1831 g_free (value_utf8);
1837 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1843 if (g_error_matches (key_file_error,
1845 G_KEY_FILE_ERROR_INVALID_VALUE))
1847 g_set_error (error, G_KEY_FILE_ERROR,
1848 G_KEY_FILE_ERROR_INVALID_VALUE,
1849 _("Key file contains key '%s' "
1850 "which has a value that cannot be interpreted."),
1852 g_error_free (key_file_error);
1855 g_propagate_error (error, key_file_error);
1858 return string_value;
1862 * g_key_file_set_string:
1863 * @key_file: a #GKeyFile
1864 * @group_name: a group name
1868 * Associates a new string value with @key under @group_name.
1869 * If @key cannot be found then it is created.
1870 * If @group_name cannot be found then it is created.
1871 * Unlike g_key_file_set_value(), this function handles characters
1872 * that need escaping, such as newlines.
1877 g_key_file_set_string (GKeyFile *key_file,
1878 const gchar *group_name,
1880 const gchar *string)
1884 g_return_if_fail (key_file != NULL);
1885 g_return_if_fail (string != NULL);
1887 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1888 g_key_file_set_value (key_file, group_name, key, value);
1893 * g_key_file_get_string_list:
1894 * @key_file: a #GKeyFile
1895 * @group_name: a group name
1897 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1898 * @error: return location for a #GError, or %NULL
1900 * Returns the values associated with @key under @group_name.
1902 * In the event the key cannot be found, %NULL is returned and
1903 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1904 * event that the @group_name cannot be found, %NULL is returned
1905 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1907 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1908 * a %NULL-terminated string array or %NULL if the specified
1909 * key cannot be found. The array should be freed with g_strfreev().
1914 g_key_file_get_string_list (GKeyFile *key_file,
1915 const gchar *group_name,
1920 GError *key_file_error = NULL;
1921 gchar *value, *string_value, **values;
1923 GSList *p, *pieces = NULL;
1925 g_return_val_if_fail (key_file != NULL, NULL);
1926 g_return_val_if_fail (group_name != NULL, NULL);
1927 g_return_val_if_fail (key != NULL, NULL);
1932 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1936 g_propagate_error (error, key_file_error);
1940 if (!g_utf8_validate (value, -1, NULL))
1942 gchar *value_utf8 = _g_utf8_make_valid (value);
1943 g_set_error (error, G_KEY_FILE_ERROR,
1944 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1945 _("Key file contains key '%s' with value '%s' "
1946 "which is not UTF-8"), key, value_utf8);
1947 g_free (value_utf8);
1953 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1955 g_free (string_value);
1959 if (g_error_matches (key_file_error,
1961 G_KEY_FILE_ERROR_INVALID_VALUE))
1963 g_set_error (error, G_KEY_FILE_ERROR,
1964 G_KEY_FILE_ERROR_INVALID_VALUE,
1965 _("Key file contains key '%s' "
1966 "which has a value that cannot be interpreted."),
1968 g_error_free (key_file_error);
1971 g_propagate_error (error, key_file_error);
1976 len = g_slist_length (pieces);
1977 values = g_new (gchar *, len + 1);
1978 for (p = pieces, i = 0; p; p = p->next)
1979 values[i++] = p->data;
1982 g_slist_free (pieces);
1991 * g_key_file_set_string_list:
1992 * @key_file: a #GKeyFile
1993 * @group_name: a group name
1995 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1996 * @length: number of string values in @list
1998 * Associates a list of string values for @key under @group_name.
1999 * If @key cannot be found then it is created.
2000 * If @group_name cannot be found then it is created.
2005 g_key_file_set_string_list (GKeyFile *key_file,
2006 const gchar *group_name,
2008 const gchar * const list[],
2011 GString *value_list;
2014 g_return_if_fail (key_file != NULL);
2015 g_return_if_fail (list != NULL || length == 0);
2017 value_list = g_string_sized_new (length * 128);
2018 for (i = 0; i < length && list[i] != NULL; i++)
2022 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2023 g_string_append (value_list, value);
2024 g_string_append_c (value_list, key_file->list_separator);
2029 g_key_file_set_value (key_file, group_name, key, value_list->str);
2030 g_string_free (value_list, TRUE);
2034 * g_key_file_set_locale_string:
2035 * @key_file: a #GKeyFile
2036 * @group_name: a group name
2038 * @locale: a locale identifier
2041 * Associates a string value for @key and @locale under @group_name.
2042 * If the translation for @key cannot be found then it is created.
2047 g_key_file_set_locale_string (GKeyFile *key_file,
2048 const gchar *group_name,
2050 const gchar *locale,
2051 const gchar *string)
2053 gchar *full_key, *value;
2055 g_return_if_fail (key_file != NULL);
2056 g_return_if_fail (key != NULL);
2057 g_return_if_fail (locale != NULL);
2058 g_return_if_fail (string != NULL);
2060 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2061 full_key = g_strdup_printf ("%s[%s]", key, locale);
2062 g_key_file_set_value (key_file, group_name, full_key, value);
2068 * g_key_file_get_locale_string:
2069 * @key_file: a #GKeyFile
2070 * @group_name: a group name
2072 * @locale: (allow-none): a locale identifier or %NULL
2073 * @error: return location for a #GError, or %NULL
2075 * Returns the value associated with @key under @group_name
2076 * translated in the given @locale if available. If @locale is
2077 * %NULL then the current locale is assumed.
2079 * If @key cannot be found then %NULL is returned and @error is set
2080 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2081 * with @key cannot be interpreted or no suitable translation can
2082 * be found then the untranslated value is returned.
2084 * Return value: a newly allocated string or %NULL if the specified
2085 * key cannot be found.
2090 g_key_file_get_locale_string (GKeyFile *key_file,
2091 const gchar *group_name,
2093 const gchar *locale,
2096 gchar *candidate_key, *translated_value;
2097 GError *key_file_error;
2099 gboolean free_languages = FALSE;
2102 g_return_val_if_fail (key_file != NULL, NULL);
2103 g_return_val_if_fail (group_name != NULL, NULL);
2104 g_return_val_if_fail (key != NULL, NULL);
2106 candidate_key = NULL;
2107 translated_value = NULL;
2108 key_file_error = NULL;
2112 languages = g_get_locale_variants (locale);
2113 free_languages = TRUE;
2117 languages = (gchar **) g_get_language_names ();
2118 free_languages = FALSE;
2121 for (i = 0; languages[i]; i++)
2123 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2125 translated_value = g_key_file_get_string (key_file,
2127 candidate_key, NULL);
2128 g_free (candidate_key);
2130 if (translated_value)
2133 g_free (translated_value);
2134 translated_value = NULL;
2137 /* Fallback to untranslated key
2139 if (!translated_value)
2141 translated_value = g_key_file_get_string (key_file, group_name, key,
2144 if (!translated_value)
2145 g_propagate_error (error, key_file_error);
2149 g_strfreev (languages);
2151 return translated_value;
2155 * g_key_file_get_locale_string_list:
2156 * @key_file: a #GKeyFile
2157 * @group_name: a group name
2159 * @locale: (allow-none): a locale identifier or %NULL
2160 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2161 * @error: return location for a #GError or %NULL
2163 * Returns the values associated with @key under @group_name
2164 * translated in the given @locale if available. If @locale is
2165 * %NULL then the current locale is assumed.
2167 * If @key cannot be found then %NULL is returned and @error is set
2168 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2169 * with @key cannot be interpreted or no suitable translations
2170 * can be found then the untranslated values are returned. The
2171 * returned array is %NULL-terminated, so @length may optionally
2174 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2175 * or %NULL if the key isn't found. The string array should be freed
2176 * with g_strfreev().
2181 g_key_file_get_locale_string_list (GKeyFile *key_file,
2182 const gchar *group_name,
2184 const gchar *locale,
2188 GError *key_file_error;
2189 gchar **values, *value;
2190 char list_separator[2];
2193 g_return_val_if_fail (key_file != NULL, NULL);
2194 g_return_val_if_fail (group_name != NULL, NULL);
2195 g_return_val_if_fail (key != NULL, NULL);
2197 key_file_error = NULL;
2199 value = g_key_file_get_locale_string (key_file, group_name,
2204 g_propagate_error (error, key_file_error);
2213 len = strlen (value);
2214 if (value[len - 1] == key_file->list_separator)
2215 value[len - 1] = '\0';
2217 list_separator[0] = key_file->list_separator;
2218 list_separator[1] = '\0';
2219 values = g_strsplit (value, list_separator, 0);
2224 *length = g_strv_length (values);
2230 * g_key_file_set_locale_string_list:
2231 * @key_file: a #GKeyFile
2232 * @group_name: a group name
2234 * @locale: a locale identifier
2235 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2236 * @length: the length of @list
2238 * Associates a list of string values for @key and @locale under
2239 * @group_name. If the translation for @key cannot be found then
2245 g_key_file_set_locale_string_list (GKeyFile *key_file,
2246 const gchar *group_name,
2248 const gchar *locale,
2249 const gchar * const list[],
2252 GString *value_list;
2256 g_return_if_fail (key_file != NULL);
2257 g_return_if_fail (key != NULL);
2258 g_return_if_fail (locale != NULL);
2259 g_return_if_fail (length != 0);
2261 value_list = g_string_sized_new (length * 128);
2262 for (i = 0; i < length && list[i] != NULL; i++)
2266 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2267 g_string_append (value_list, value);
2268 g_string_append_c (value_list, key_file->list_separator);
2273 full_key = g_strdup_printf ("%s[%s]", key, locale);
2274 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2276 g_string_free (value_list, TRUE);
2280 * g_key_file_get_boolean:
2281 * @key_file: a #GKeyFile
2282 * @group_name: a group name
2284 * @error: return location for a #GError
2286 * Returns the value associated with @key under @group_name as a
2289 * If @key cannot be found then %FALSE is returned and @error is set
2290 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2291 * associated with @key cannot be interpreted as a boolean then %FALSE
2292 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2294 * Return value: the value associated with the key as a boolean,
2295 * or %FALSE if the key was not found or could not be parsed.
2300 g_key_file_get_boolean (GKeyFile *key_file,
2301 const gchar *group_name,
2305 GError *key_file_error = NULL;
2307 gboolean bool_value;
2309 g_return_val_if_fail (key_file != NULL, FALSE);
2310 g_return_val_if_fail (group_name != NULL, FALSE);
2311 g_return_val_if_fail (key != NULL, FALSE);
2313 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2317 g_propagate_error (error, key_file_error);
2321 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2327 if (g_error_matches (key_file_error,
2329 G_KEY_FILE_ERROR_INVALID_VALUE))
2331 g_set_error (error, G_KEY_FILE_ERROR,
2332 G_KEY_FILE_ERROR_INVALID_VALUE,
2333 _("Key file contains key '%s' "
2334 "which has a value that cannot be interpreted."),
2336 g_error_free (key_file_error);
2339 g_propagate_error (error, key_file_error);
2346 * g_key_file_set_boolean:
2347 * @key_file: a #GKeyFile
2348 * @group_name: a group name
2350 * @value: %TRUE or %FALSE
2352 * Associates a new boolean value with @key under @group_name.
2353 * If @key cannot be found then it is created.
2358 g_key_file_set_boolean (GKeyFile *key_file,
2359 const gchar *group_name,
2365 g_return_if_fail (key_file != NULL);
2367 result = g_key_file_parse_boolean_as_value (key_file, value);
2368 g_key_file_set_value (key_file, group_name, key, result);
2373 * g_key_file_get_boolean_list:
2374 * @key_file: a #GKeyFile
2375 * @group_name: a group name
2377 * @length: (out): the number of booleans returned
2378 * @error: return location for a #GError
2380 * Returns the values associated with @key under @group_name as
2383 * If @key cannot be found then %NULL is returned and @error is set to
2384 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2385 * with @key cannot be interpreted as booleans then %NULL is returned
2386 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2388 * Return value: (array length=length) (element-type gboolean) (transfer container):
2389 * the values associated with the key as a list of booleans, or %NULL if the
2390 * key was not found or could not be parsed. The returned list of booleans
2391 * should be freed with g_free() when no longer needed.
2396 g_key_file_get_boolean_list (GKeyFile *key_file,
2397 const gchar *group_name,
2402 GError *key_file_error;
2404 gboolean *bool_values;
2407 g_return_val_if_fail (key_file != NULL, NULL);
2408 g_return_val_if_fail (group_name != NULL, NULL);
2409 g_return_val_if_fail (key != NULL, NULL);
2414 key_file_error = NULL;
2416 values = g_key_file_get_string_list (key_file, group_name, key,
2417 &num_bools, &key_file_error);
2420 g_propagate_error (error, key_file_error);
2425 bool_values = g_new (gboolean, num_bools);
2427 for (i = 0; i < num_bools; i++)
2429 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2435 g_propagate_error (error, key_file_error);
2436 g_strfreev (values);
2437 g_free (bool_values);
2442 g_strfreev (values);
2445 *length = num_bools;
2451 * g_key_file_set_boolean_list:
2452 * @key_file: a #GKeyFile
2453 * @group_name: a group name
2455 * @list: (array length=length): an array of boolean values
2456 * @length: length of @list
2458 * Associates a list of boolean values with @key under @group_name.
2459 * If @key cannot be found then it is created.
2460 * If @group_name is %NULL, the start_group is used.
2465 g_key_file_set_boolean_list (GKeyFile *key_file,
2466 const gchar *group_name,
2471 GString *value_list;
2474 g_return_if_fail (key_file != NULL);
2475 g_return_if_fail (list != NULL);
2477 value_list = g_string_sized_new (length * 8);
2478 for (i = 0; i < length; i++)
2482 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2484 g_string_append (value_list, value);
2485 g_string_append_c (value_list, key_file->list_separator);
2490 g_key_file_set_value (key_file, group_name, key, value_list->str);
2491 g_string_free (value_list, TRUE);
2495 * g_key_file_get_integer:
2496 * @key_file: a #GKeyFile
2497 * @group_name: a group name
2499 * @error: return location for a #GError
2501 * Returns the value associated with @key under @group_name as an
2504 * If @key cannot be found then 0 is returned and @error is set to
2505 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2506 * with @key cannot be interpreted as an integer then 0 is returned
2507 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2509 * Return value: the value associated with the key as an integer, or
2510 * 0 if the key was not found or could not be parsed.
2515 g_key_file_get_integer (GKeyFile *key_file,
2516 const gchar *group_name,
2520 GError *key_file_error;
2524 g_return_val_if_fail (key_file != NULL, -1);
2525 g_return_val_if_fail (group_name != NULL, -1);
2526 g_return_val_if_fail (key != NULL, -1);
2528 key_file_error = NULL;
2530 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2534 g_propagate_error (error, key_file_error);
2538 int_value = g_key_file_parse_value_as_integer (key_file, value,
2544 if (g_error_matches (key_file_error,
2546 G_KEY_FILE_ERROR_INVALID_VALUE))
2548 g_set_error (error, G_KEY_FILE_ERROR,
2549 G_KEY_FILE_ERROR_INVALID_VALUE,
2550 _("Key file contains key '%s' in group '%s' "
2551 "which has a value that cannot be interpreted."),
2553 g_error_free (key_file_error);
2556 g_propagate_error (error, key_file_error);
2563 * g_key_file_set_integer:
2564 * @key_file: a #GKeyFile
2565 * @group_name: a group name
2567 * @value: an integer value
2569 * Associates a new integer value with @key under @group_name.
2570 * If @key cannot be found then it is created.
2575 g_key_file_set_integer (GKeyFile *key_file,
2576 const gchar *group_name,
2582 g_return_if_fail (key_file != NULL);
2584 result = g_key_file_parse_integer_as_value (key_file, value);
2585 g_key_file_set_value (key_file, group_name, key, result);
2590 * g_key_file_get_int64:
2591 * @key_file: a non-%NULL #GKeyFile
2592 * @group_name: a non-%NULL group name
2593 * @key: a non-%NULL key
2594 * @error: return location for a #GError
2596 * Returns the value associated with @key under @group_name as a signed
2597 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2598 * 64-bit results without truncation.
2600 * Returns: the value associated with the key as a signed 64-bit integer, or
2601 * 0 if the key was not found or could not be parsed.
2606 g_key_file_get_int64 (GKeyFile *key_file,
2607 const gchar *group_name,
2614 g_return_val_if_fail (key_file != NULL, -1);
2615 g_return_val_if_fail (group_name != NULL, -1);
2616 g_return_val_if_fail (key != NULL, -1);
2618 s = g_key_file_get_value (key_file, group_name, key, error);
2623 v = g_ascii_strtoll (s, &end, 10);
2625 if (*s == '\0' || *end != '\0')
2627 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2628 _("Key '%s' in group '%s' has value '%s' "
2629 "where %s was expected"),
2630 key, group_name, s, "int64");
2639 * g_key_file_set_int64:
2640 * @key_file: a #GKeyFile
2641 * @group_name: a group name
2643 * @value: an integer value
2645 * Associates a new integer value with @key under @group_name.
2646 * If @key cannot be found then it is created.
2651 g_key_file_set_int64 (GKeyFile *key_file,
2652 const gchar *group_name,
2658 g_return_if_fail (key_file != NULL);
2660 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2661 g_key_file_set_value (key_file, group_name, key, result);
2666 * g_key_file_get_uint64:
2667 * @key_file: a non-%NULL #GKeyFile
2668 * @group_name: a non-%NULL group name
2669 * @key: a non-%NULL key
2670 * @error: return location for a #GError
2672 * Returns the value associated with @key under @group_name as an unsigned
2673 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2674 * large positive results without truncation.
2676 * Returns: the value associated with the key as an unsigned 64-bit integer,
2677 * or 0 if the key was not found or could not be parsed.
2682 g_key_file_get_uint64 (GKeyFile *key_file,
2683 const gchar *group_name,
2690 g_return_val_if_fail (key_file != NULL, -1);
2691 g_return_val_if_fail (group_name != NULL, -1);
2692 g_return_val_if_fail (key != NULL, -1);
2694 s = g_key_file_get_value (key_file, group_name, key, error);
2699 v = g_ascii_strtoull (s, &end, 10);
2701 if (*s == '\0' || *end != '\0')
2703 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2704 _("Key '%s' in group '%s' has value '%s' "
2705 "where %s was expected"),
2706 key, group_name, s, "uint64");
2715 * g_key_file_set_uint64:
2716 * @key_file: a #GKeyFile
2717 * @group_name: a group name
2719 * @value: an integer value
2721 * Associates a new integer value with @key under @group_name.
2722 * If @key cannot be found then it is created.
2727 g_key_file_set_uint64 (GKeyFile *key_file,
2728 const gchar *group_name,
2734 g_return_if_fail (key_file != NULL);
2736 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2737 g_key_file_set_value (key_file, group_name, key, result);
2742 * g_key_file_get_integer_list:
2743 * @key_file: a #GKeyFile
2744 * @group_name: a group name
2746 * @length: (out): the number of integers returned
2747 * @error: return location for a #GError
2749 * Returns the values associated with @key under @group_name as
2752 * If @key cannot be found then %NULL is returned and @error is set to
2753 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2754 * with @key cannot be interpreted as integers then %NULL is returned
2755 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2757 * Return value: (array length=length) (element-type gint) (transfer container):
2758 * the values associated with the key as a list of integers, or %NULL if
2759 * the key was not found or could not be parsed. The returned list of
2760 * integers should be freed with g_free() when no longer needed.
2765 g_key_file_get_integer_list (GKeyFile *key_file,
2766 const gchar *group_name,
2771 GError *key_file_error = NULL;
2776 g_return_val_if_fail (key_file != NULL, NULL);
2777 g_return_val_if_fail (group_name != NULL, NULL);
2778 g_return_val_if_fail (key != NULL, NULL);
2783 values = g_key_file_get_string_list (key_file, group_name, key,
2784 &num_ints, &key_file_error);
2787 g_propagate_error (error, key_file_error);
2792 int_values = g_new (gint, num_ints);
2794 for (i = 0; i < num_ints; i++)
2796 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2802 g_propagate_error (error, key_file_error);
2803 g_strfreev (values);
2804 g_free (int_values);
2809 g_strfreev (values);
2818 * g_key_file_set_integer_list:
2819 * @key_file: a #GKeyFile
2820 * @group_name: a group name
2822 * @list: (array length=length): an array of integer values
2823 * @length: number of integer values in @list
2825 * Associates a list of integer values with @key under @group_name.
2826 * If @key cannot be found then it is created.
2831 g_key_file_set_integer_list (GKeyFile *key_file,
2832 const gchar *group_name,
2840 g_return_if_fail (key_file != NULL);
2841 g_return_if_fail (list != NULL);
2843 values = g_string_sized_new (length * 16);
2844 for (i = 0; i < length; i++)
2848 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2850 g_string_append (values, value);
2851 g_string_append_c (values, key_file->list_separator);
2856 g_key_file_set_value (key_file, group_name, key, values->str);
2857 g_string_free (values, TRUE);
2861 * g_key_file_get_double:
2862 * @key_file: a #GKeyFile
2863 * @group_name: a group name
2865 * @error: return location for a #GError
2867 * Returns the value associated with @key under @group_name as a
2868 * double. If @group_name is %NULL, the start_group is used.
2870 * If @key cannot be found then 0.0 is returned and @error is set to
2871 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2872 * with @key cannot be interpreted as a double then 0.0 is returned
2873 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2875 * Return value: the value associated with the key as a double, or
2876 * 0.0 if the key was not found or could not be parsed.
2881 g_key_file_get_double (GKeyFile *key_file,
2882 const gchar *group_name,
2886 GError *key_file_error;
2888 gdouble double_value;
2890 g_return_val_if_fail (key_file != NULL, -1);
2891 g_return_val_if_fail (group_name != NULL, -1);
2892 g_return_val_if_fail (key != NULL, -1);
2894 key_file_error = NULL;
2896 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2900 g_propagate_error (error, key_file_error);
2904 double_value = g_key_file_parse_value_as_double (key_file, value,
2910 if (g_error_matches (key_file_error,
2912 G_KEY_FILE_ERROR_INVALID_VALUE))
2914 g_set_error (error, G_KEY_FILE_ERROR,
2915 G_KEY_FILE_ERROR_INVALID_VALUE,
2916 _("Key file contains key '%s' in group '%s' "
2917 "which has a value that cannot be interpreted."),
2919 g_error_free (key_file_error);
2922 g_propagate_error (error, key_file_error);
2925 return double_value;
2929 * g_key_file_set_double:
2930 * @key_file: a #GKeyFile
2931 * @group_name: a group name
2933 * @value: an double value
2935 * Associates a new double value with @key under @group_name.
2936 * If @key cannot be found then it is created.
2941 g_key_file_set_double (GKeyFile *key_file,
2942 const gchar *group_name,
2946 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2948 g_return_if_fail (key_file != NULL);
2950 g_ascii_dtostr (result, sizeof (result), value);
2951 g_key_file_set_value (key_file, group_name, key, result);
2955 * g_key_file_get_double_list:
2956 * @key_file: a #GKeyFile
2957 * @group_name: a group name
2959 * @length: (out): the number of doubles returned
2960 * @error: return location for a #GError
2962 * Returns the values associated with @key under @group_name as
2965 * If @key cannot be found then %NULL is returned and @error is set to
2966 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2967 * with @key cannot be interpreted as doubles then %NULL is returned
2968 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2970 * Return value: (array length=length) (element-type gdouble) (transfer container):
2971 * the values associated with the key as a list of doubles, or %NULL if the
2972 * key was not found or could not be parsed. The returned list of doubles
2973 * should be freed with g_free() when no longer needed.
2978 g_key_file_get_double_list (GKeyFile *key_file,
2979 const gchar *group_name,
2984 GError *key_file_error = NULL;
2986 gdouble *double_values;
2987 gsize i, num_doubles;
2989 g_return_val_if_fail (key_file != NULL, NULL);
2990 g_return_val_if_fail (group_name != NULL, NULL);
2991 g_return_val_if_fail (key != NULL, NULL);
2996 values = g_key_file_get_string_list (key_file, group_name, key,
2997 &num_doubles, &key_file_error);
3000 g_propagate_error (error, key_file_error);
3005 double_values = g_new (gdouble, num_doubles);
3007 for (i = 0; i < num_doubles; i++)
3009 double_values[i] = g_key_file_parse_value_as_double (key_file,
3015 g_propagate_error (error, key_file_error);
3016 g_strfreev (values);
3017 g_free (double_values);
3022 g_strfreev (values);
3025 *length = num_doubles;
3027 return double_values;
3031 * g_key_file_set_double_list:
3032 * @key_file: a #GKeyFile
3033 * @group_name: a group name
3035 * @list: (array length=length): an array of double values
3036 * @length: number of double values in @list
3038 * Associates a list of double values with @key under
3039 * @group_name. If @key cannot be found then it is created.
3044 g_key_file_set_double_list (GKeyFile *key_file,
3045 const gchar *group_name,
3053 g_return_if_fail (key_file != NULL);
3054 g_return_if_fail (list != NULL);
3056 values = g_string_sized_new (length * 16);
3057 for (i = 0; i < length; i++)
3059 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3061 g_ascii_dtostr( result, sizeof (result), list[i] );
3063 g_string_append (values, result);
3064 g_string_append_c (values, key_file->list_separator);
3067 g_key_file_set_value (key_file, group_name, key, values->str);
3068 g_string_free (values, TRUE);
3072 g_key_file_set_key_comment (GKeyFile *key_file,
3073 const gchar *group_name,
3075 const gchar *comment,
3078 GKeyFileGroup *group;
3079 GKeyFileKeyValuePair *pair;
3080 GList *key_node, *comment_node, *tmp;
3082 group = g_key_file_lookup_group (key_file, group_name);
3085 g_set_error (error, G_KEY_FILE_ERROR,
3086 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3087 _("Key file does not have group '%s'"),
3088 group_name ? group_name : "(null)");
3093 /* First find the key the comments are supposed to be
3096 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3098 if (key_node == NULL)
3100 g_set_error (error, G_KEY_FILE_ERROR,
3101 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3102 _("Key file does not have key '%s' in group '%s'"),
3107 /* Then find all the comments already associated with the
3110 tmp = key_node->next;
3113 pair = (GKeyFileKeyValuePair *) tmp->data;
3115 if (pair->key != NULL)
3120 g_key_file_remove_key_value_pair_node (key_file, group,
3124 if (comment == NULL)
3127 /* Now we can add our new comment
3129 pair = g_slice_new (GKeyFileKeyValuePair);
3131 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3133 key_node = g_list_insert (key_node, pair, 1);
3139 g_key_file_set_group_comment (GKeyFile *key_file,
3140 const gchar *group_name,
3141 const gchar *comment,
3144 GKeyFileGroup *group;
3146 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3148 group = g_key_file_lookup_group (key_file, group_name);
3151 g_set_error (error, G_KEY_FILE_ERROR,
3152 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3153 _("Key file does not have group '%s'"),
3154 group_name ? group_name : "(null)");
3159 /* First remove any existing comment
3163 g_key_file_key_value_pair_free (group->comment);
3164 group->comment = NULL;
3167 if (comment == NULL)
3170 /* Now we can add our new comment
3172 group->comment = g_slice_new (GKeyFileKeyValuePair);
3173 group->comment->key = NULL;
3174 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3180 g_key_file_set_top_comment (GKeyFile *key_file,
3181 const gchar *comment,
3185 GKeyFileGroup *group;
3186 GKeyFileKeyValuePair *pair;
3188 /* The last group in the list should be the top (comments only)
3191 g_warn_if_fail (key_file->groups != NULL);
3192 group_node = g_list_last (key_file->groups);
3193 group = (GKeyFileGroup *) group_node->data;
3194 g_warn_if_fail (group->name == NULL);
3196 /* Note all keys must be comments at the top of
3197 * the file, so we can just free it all.
3199 if (group->key_value_pairs != NULL)
3201 g_list_foreach (group->key_value_pairs,
3202 (GFunc) g_key_file_key_value_pair_free,
3204 g_list_free (group->key_value_pairs);
3205 group->key_value_pairs = NULL;
3208 if (comment == NULL)
3211 pair = g_slice_new (GKeyFileKeyValuePair);
3213 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3215 group->key_value_pairs =
3216 g_list_prepend (group->key_value_pairs, pair);
3222 * g_key_file_set_comment:
3223 * @key_file: a #GKeyFile
3224 * @group_name: (allow-none): a group name, or %NULL
3225 * @key: (allow-none): a key
3226 * @comment: a comment
3227 * @error: return location for a #GError
3229 * Places a comment above @key from @group_name.
3230 * If @key is %NULL then @comment will be written above @group_name.
3231 * If both @key and @group_name are %NULL, then @comment will be
3232 * written above the first group in the file.
3234 * Returns: %TRUE if the comment was written, %FALSE otherwise
3239 g_key_file_set_comment (GKeyFile *key_file,
3240 const gchar *group_name,
3242 const gchar *comment,
3245 g_return_val_if_fail (key_file != NULL, FALSE);
3247 if (group_name != NULL && key != NULL)
3249 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3252 else if (group_name != NULL)
3254 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3259 if (!g_key_file_set_top_comment (key_file, comment, error))
3263 if (comment != NULL)
3264 key_file->approximate_size += strlen (comment);
3270 g_key_file_get_key_comment (GKeyFile *key_file,
3271 const gchar *group_name,
3275 GKeyFileGroup *group;
3276 GKeyFileKeyValuePair *pair;
3277 GList *key_node, *tmp;
3281 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3283 group = g_key_file_lookup_group (key_file, group_name);
3286 g_set_error (error, G_KEY_FILE_ERROR,
3287 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3288 _("Key file does not have group '%s'"),
3289 group_name ? group_name : "(null)");
3294 /* First find the key the comments are supposed to be
3297 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3299 if (key_node == NULL)
3301 g_set_error (error, G_KEY_FILE_ERROR,
3302 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3303 _("Key file does not have key '%s' in group '%s'"),
3310 /* Then find all the comments already associated with the
3311 * key and concatentate them.
3313 tmp = key_node->next;
3314 if (!key_node->next)
3317 pair = (GKeyFileKeyValuePair *) tmp->data;
3318 if (pair->key != NULL)
3323 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3325 if (pair->key != NULL)
3331 while (tmp != key_node)
3333 pair = (GKeyFileKeyValuePair *) tmp->data;
3336 string = g_string_sized_new (512);
3338 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3339 g_string_append (string, comment);
3347 comment = string->str;
3348 g_string_free (string, FALSE);
3357 get_group_comment (GKeyFile *key_file,
3358 GKeyFileGroup *group,
3367 tmp = group->key_value_pairs;
3370 GKeyFileKeyValuePair *pair;
3372 pair = (GKeyFileKeyValuePair *) tmp->data;
3374 if (pair->key != NULL)
3380 if (tmp->next == NULL)
3388 GKeyFileKeyValuePair *pair;
3390 pair = (GKeyFileKeyValuePair *) tmp->data;
3393 string = g_string_sized_new (512);
3395 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3396 g_string_append (string, comment);
3403 return g_string_free (string, FALSE);
3409 g_key_file_get_group_comment (GKeyFile *key_file,
3410 const gchar *group_name,
3414 GKeyFileGroup *group;
3416 group = g_key_file_lookup_group (key_file, group_name);
3419 g_set_error (error, G_KEY_FILE_ERROR,
3420 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3421 _("Key file does not have group '%s'"),
3422 group_name ? group_name : "(null)");
3428 return g_strdup (group->comment->value);
3430 group_node = g_key_file_lookup_group_node (key_file, group_name);
3431 group_node = group_node->next;
3432 group = (GKeyFileGroup *)group_node->data;
3433 return get_group_comment (key_file, group, error);
3437 g_key_file_get_top_comment (GKeyFile *key_file,
3441 GKeyFileGroup *group;
3443 /* The last group in the list should be the top (comments only)
3446 g_warn_if_fail (key_file->groups != NULL);
3447 group_node = g_list_last (key_file->groups);
3448 group = (GKeyFileGroup *) group_node->data;
3449 g_warn_if_fail (group->name == NULL);
3451 return get_group_comment (key_file, group, error);
3455 * g_key_file_get_comment:
3456 * @key_file: a #GKeyFile
3457 * @group_name: a group name, or %NULL
3459 * @error: return location for a #GError
3461 * Retrieves a comment above @key from @group_name.
3462 * If @key is %NULL then @comment will be read from above
3463 * @group_name. If both @key and @group_name are %NULL, then
3464 * @comment will be read from above the first group in the file.
3466 * Returns: a comment that should be freed with g_free()
3471 g_key_file_get_comment (GKeyFile *key_file,
3472 const gchar *group_name,
3476 g_return_val_if_fail (key_file != NULL, NULL);
3478 if (group_name != NULL && key != NULL)
3479 return g_key_file_get_key_comment (key_file, group_name, key, error);
3480 else if (group_name != NULL)
3481 return g_key_file_get_group_comment (key_file, group_name, error);
3483 return g_key_file_get_top_comment (key_file, error);
3487 * g_key_file_remove_comment:
3488 * @key_file: a #GKeyFile
3489 * @group_name: (allow-none): a group name, or %NULL
3490 * @key: (allow-none): a key
3491 * @error: return location for a #GError
3493 * Removes a comment above @key from @group_name.
3494 * If @key is %NULL then @comment will be removed above @group_name.
3495 * If both @key and @group_name are %NULL, then @comment will
3496 * be removed above the first group in the file.
3498 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3504 g_key_file_remove_comment (GKeyFile *key_file,
3505 const gchar *group_name,
3509 g_return_val_if_fail (key_file != NULL, FALSE);
3511 if (group_name != NULL && key != NULL)
3512 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3513 else if (group_name != NULL)
3514 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3516 return g_key_file_set_top_comment (key_file, NULL, error);
3520 * g_key_file_has_group:
3521 * @key_file: a #GKeyFile
3522 * @group_name: a group name
3524 * Looks whether the key file has the group @group_name.
3526 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3531 g_key_file_has_group (GKeyFile *key_file,
3532 const gchar *group_name)
3534 g_return_val_if_fail (key_file != NULL, FALSE);
3535 g_return_val_if_fail (group_name != NULL, FALSE);
3537 return g_key_file_lookup_group (key_file, group_name) != NULL;
3540 /* This code remains from a historical attempt to add a new public API
3541 * which respects the GError rules.
3544 g_key_file_has_key_full (GKeyFile *key_file,
3545 const gchar *group_name,
3550 GKeyFileKeyValuePair *pair;
3551 GKeyFileGroup *group;
3553 g_return_val_if_fail (key_file != NULL, FALSE);
3554 g_return_val_if_fail (group_name != NULL, FALSE);
3555 g_return_val_if_fail (key != NULL, FALSE);
3557 group = g_key_file_lookup_group (key_file, group_name);
3561 g_set_error (error, G_KEY_FILE_ERROR,
3562 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3563 _("Key file does not have group '%s'"),
3564 group_name ? group_name : "(null)");
3569 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3572 *has_key = pair != NULL;
3577 * g_key_file_has_key: (skip)
3578 * @key_file: a #GKeyFile
3579 * @group_name: a group name
3581 * @error: return location for a #GError
3583 * Looks whether the key file has the key @key in the group
3586 * <note>This function does not follow the rules for #GError strictly;
3587 * the return value both carries meaning and signals an error. To use
3588 * this function, you must pass a #GError pointer in @error, and check
3589 * whether it is not %NULL to see if an error occurred.</note>
3591 * Language bindings should use g_key_file_get_value() to test whether
3592 * or not a key exists.
3594 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3600 g_key_file_has_key (GKeyFile *key_file,
3601 const gchar *group_name,
3605 GError *temp_error = NULL;
3608 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3614 g_propagate_error (error, temp_error);
3620 g_key_file_add_group (GKeyFile *key_file,
3621 const gchar *group_name)
3623 GKeyFileGroup *group;
3625 g_return_if_fail (key_file != NULL);
3626 g_return_if_fail (g_key_file_is_group_name (group_name));
3628 group = g_key_file_lookup_group (key_file, group_name);
3631 key_file->current_group = group;
3635 group = g_slice_new0 (GKeyFileGroup);
3636 group->name = g_strdup (group_name);
3637 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3638 key_file->groups = g_list_prepend (key_file->groups, group);
3639 key_file->approximate_size += strlen (group_name) + 3;
3640 key_file->current_group = group;
3642 if (key_file->start_group == NULL)
3643 key_file->start_group = group;
3645 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3649 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3654 g_free (pair->value);
3655 g_slice_free (GKeyFileKeyValuePair, pair);
3659 /* Be careful not to call this function on a node with data in the
3660 * lookup map without removing it from the lookup map, first.
3662 * Some current cases where this warning is not a concern are
3664 * - the node being removed is a comment node
3665 * - the entire lookup map is getting destroyed soon after
3669 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3670 GKeyFileGroup *group,
3674 GKeyFileKeyValuePair *pair;
3676 pair = (GKeyFileKeyValuePair *) pair_node->data;
3678 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3680 if (pair->key != NULL)
3681 key_file->approximate_size -= strlen (pair->key) + 1;
3683 g_warn_if_fail (pair->value != NULL);
3684 key_file->approximate_size -= strlen (pair->value);
3686 g_key_file_key_value_pair_free (pair);
3688 g_list_free_1 (pair_node);
3692 g_key_file_remove_group_node (GKeyFile *key_file,
3695 GKeyFileGroup *group;
3698 group = (GKeyFileGroup *) group_node->data;
3701 g_hash_table_remove (key_file->group_hash, group->name);
3703 /* If the current group gets deleted make the current group the last
3706 if (key_file->current_group == group)
3708 /* groups should always contain at least the top comment group,
3709 * unless g_key_file_clear has been called
3711 if (key_file->groups)
3712 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3714 key_file->current_group = NULL;
3717 /* If the start group gets deleted make the start group the first
3720 if (key_file->start_group == group)
3722 tmp = g_list_last (key_file->groups);
3725 if (tmp != group_node &&
3726 ((GKeyFileGroup *) tmp->data)->name != NULL)
3733 key_file->start_group = (GKeyFileGroup *) tmp->data;
3735 key_file->start_group = NULL;
3738 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3740 if (group->name != NULL)
3741 key_file->approximate_size -= strlen (group->name) + 3;
3743 tmp = group->key_value_pairs;
3750 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3753 g_warn_if_fail (group->key_value_pairs == NULL);
3757 g_key_file_key_value_pair_free (group->comment);
3758 group->comment = NULL;
3761 if (group->lookup_map)
3763 g_hash_table_destroy (group->lookup_map);
3764 group->lookup_map = NULL;
3767 g_free ((gchar *) group->name);
3768 g_slice_free (GKeyFileGroup, group);
3769 g_list_free_1 (group_node);
3773 * g_key_file_remove_group:
3774 * @key_file: a #GKeyFile
3775 * @group_name: a group name
3776 * @error: return location for a #GError or %NULL
3778 * Removes the specified group, @group_name,
3779 * from the key file.
3781 * Returns: %TRUE if the group was removed, %FALSE otherwise
3786 g_key_file_remove_group (GKeyFile *key_file,
3787 const gchar *group_name,
3792 g_return_val_if_fail (key_file != NULL, FALSE);
3793 g_return_val_if_fail (group_name != NULL, FALSE);
3795 group_node = g_key_file_lookup_group_node (key_file, group_name);
3799 g_set_error (error, G_KEY_FILE_ERROR,
3800 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3801 _("Key file does not have group '%s'"),
3806 g_key_file_remove_group_node (key_file, group_node);
3812 g_key_file_add_key_value_pair (GKeyFile *key_file,
3813 GKeyFileGroup *group,
3814 GKeyFileKeyValuePair *pair)
3816 g_hash_table_replace (group->lookup_map, pair->key, pair);
3817 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3818 group->has_trailing_blank_line = FALSE;
3819 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3823 g_key_file_add_key (GKeyFile *key_file,
3824 GKeyFileGroup *group,
3828 GKeyFileKeyValuePair *pair;
3830 pair = g_slice_new (GKeyFileKeyValuePair);
3831 pair->key = g_strdup (key);
3832 pair->value = g_strdup (value);
3834 g_key_file_add_key_value_pair (key_file, group, pair);
3838 * g_key_file_remove_key:
3839 * @key_file: a #GKeyFile
3840 * @group_name: a group name
3841 * @key: a key name to remove
3842 * @error: return location for a #GError or %NULL
3844 * Removes @key in @group_name from the key file.
3846 * Returns: %TRUE if the key was removed, %FALSE otherwise
3851 g_key_file_remove_key (GKeyFile *key_file,
3852 const gchar *group_name,
3856 GKeyFileGroup *group;
3857 GKeyFileKeyValuePair *pair;
3859 g_return_val_if_fail (key_file != NULL, FALSE);
3860 g_return_val_if_fail (group_name != NULL, FALSE);
3861 g_return_val_if_fail (key != NULL, FALSE);
3865 group = g_key_file_lookup_group (key_file, group_name);
3868 g_set_error (error, G_KEY_FILE_ERROR,
3869 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3870 _("Key file does not have group '%s'"),
3871 group_name ? group_name : "(null)");
3875 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3879 g_set_error (error, G_KEY_FILE_ERROR,
3880 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3881 _("Key file does not have key '%s' in group '%s'"),
3886 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3888 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3889 g_hash_table_remove (group->lookup_map, pair->key);
3890 g_key_file_key_value_pair_free (pair);
3896 g_key_file_lookup_group_node (GKeyFile *key_file,
3897 const gchar *group_name)
3899 GKeyFileGroup *group;
3902 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3904 group = (GKeyFileGroup *) tmp->data;
3906 if (group && group->name && strcmp (group->name, group_name) == 0)
3913 static GKeyFileGroup *
3914 g_key_file_lookup_group (GKeyFile *key_file,
3915 const gchar *group_name)
3917 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3921 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3922 GKeyFileGroup *group,
3927 for (key_node = group->key_value_pairs;
3929 key_node = key_node->next)
3931 GKeyFileKeyValuePair *pair;
3933 pair = (GKeyFileKeyValuePair *) key_node->data;
3935 if (pair->key && strcmp (pair->key, key) == 0)
3942 static GKeyFileKeyValuePair *
3943 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3944 GKeyFileGroup *group,
3947 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3950 /* Lines starting with # or consisting entirely of whitespace are merely
3951 * recorded, not parsed. This function assumes all leading whitespace
3952 * has been stripped.
3955 g_key_file_line_is_comment (const gchar *line)
3957 return (*line == '#' || *line == '\0' || *line == '\n');
3961 g_key_file_is_group_name (const gchar *name)
3968 p = q = (gchar *) name;
3969 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3970 q = g_utf8_find_next_char (q, NULL);
3972 if (*q != '\0' || q == p)
3979 g_key_file_is_key_name (const gchar *name)
3986 p = q = (gchar *) name;
3987 /* We accept a little more than the desktop entry spec says,
3988 * since gnome-vfs uses mime-types as keys in its cache.
3990 while (*q && *q != '=' && *q != '[' && *q != ']')
3991 q = g_utf8_find_next_char (q, NULL);
3993 /* No empty keys, please */
3997 /* We accept spaces in the middle of keys to not break
3998 * existing apps, but we don't tolerate initial or final
3999 * spaces, which would lead to silent corruption when
4000 * rereading the file.
4002 if (*p == ' ' || q[-1] == ' ')
4008 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4009 q = g_utf8_find_next_char (q, NULL);
4023 /* A group in a key file is made up of a starting '[' followed by one
4024 * or more letters making up the group name followed by ']'.
4027 g_key_file_line_is_group (const gchar *line)
4037 while (*p && *p != ']')
4038 p = g_utf8_find_next_char (p, NULL);
4043 /* silently accept whitespace after the ] */
4044 p = g_utf8_find_next_char (p, NULL);
4045 while (*p == ' ' || *p == '\t')
4046 p = g_utf8_find_next_char (p, NULL);
4055 g_key_file_line_is_key_value_pair (const gchar *line)
4059 p = (gchar *) g_utf8_strchr (line, -1, '=');
4064 /* Key must be non-empty
4073 g_key_file_parse_value_as_string (GKeyFile *key_file,
4078 gchar *string_value, *p, *q0, *q;
4080 string_value = g_new (gchar, strlen (value) + 1);
4082 p = (gchar *) value;
4083 q0 = q = string_value;
4113 g_set_error_literal (error, G_KEY_FILE_ERROR,
4114 G_KEY_FILE_ERROR_INVALID_VALUE,
4115 _("Key file contains escape character "
4120 if (pieces && *p == key_file->list_separator)
4121 *q = key_file->list_separator;
4135 g_set_error (error, G_KEY_FILE_ERROR,
4136 G_KEY_FILE_ERROR_INVALID_VALUE,
4137 _("Key file contains invalid escape "
4138 "sequence '%s'"), sequence);
4147 if (pieces && (*p == key_file->list_separator))
4149 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4165 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4166 *pieces = g_slist_reverse (*pieces);
4169 return string_value;
4173 g_key_file_parse_string_as_value (GKeyFile *key_file,
4174 const gchar *string,
4175 gboolean escape_separator)
4177 gchar *value, *p, *q;
4179 gboolean parsing_leading_space;
4181 length = strlen (string) + 1;
4183 /* Worst case would be that every character needs to be escaped.
4184 * In other words every character turns to two characters
4186 value = g_new (gchar, 2 * length);
4188 p = (gchar *) string;
4190 parsing_leading_space = TRUE;
4191 while (p < (string + length - 1))
4193 gchar escaped_character[3] = { '\\', 0, 0 };
4198 if (parsing_leading_space)
4200 escaped_character[1] = 's';
4201 strcpy (q, escaped_character);
4211 if (parsing_leading_space)
4213 escaped_character[1] = 't';
4214 strcpy (q, escaped_character);
4224 escaped_character[1] = 'n';
4225 strcpy (q, escaped_character);
4229 escaped_character[1] = 'r';
4230 strcpy (q, escaped_character);
4234 escaped_character[1] = '\\';
4235 strcpy (q, escaped_character);
4237 parsing_leading_space = FALSE;
4240 if (escape_separator && *p == key_file->list_separator)
4242 escaped_character[1] = key_file->list_separator;
4243 strcpy (q, escaped_character);
4245 parsing_leading_space = TRUE;
4251 parsing_leading_space = FALSE;
4263 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4272 long_value = strtol (value, &eof_int, 10);
4274 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4276 gchar *value_utf8 = _g_utf8_make_valid (value);
4277 g_set_error (error, G_KEY_FILE_ERROR,
4278 G_KEY_FILE_ERROR_INVALID_VALUE,
4279 _("Value '%s' cannot be interpreted "
4280 "as a number."), value_utf8);
4281 g_free (value_utf8);
4286 int_value = long_value;
4287 if (int_value != long_value || errno == ERANGE)
4289 gchar *value_utf8 = _g_utf8_make_valid (value);
4292 G_KEY_FILE_ERROR_INVALID_VALUE,
4293 _("Integer value '%s' out of range"),
4295 g_free (value_utf8);
4304 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4308 return g_strdup_printf ("%d", value);
4312 g_key_file_parse_value_as_double (GKeyFile *key_file,
4316 gchar *end_of_valid_d;
4317 gdouble double_value = 0;
4319 double_value = g_ascii_strtod (value, &end_of_valid_d);
4321 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4323 gchar *value_utf8 = _g_utf8_make_valid (value);
4324 g_set_error (error, G_KEY_FILE_ERROR,
4325 G_KEY_FILE_ERROR_INVALID_VALUE,
4326 _("Value '%s' cannot be interpreted "
4327 "as a float number."),
4329 g_free (value_utf8);
4332 return double_value;
4336 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4342 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4344 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4347 value_utf8 = _g_utf8_make_valid (value);
4348 g_set_error (error, G_KEY_FILE_ERROR,
4349 G_KEY_FILE_ERROR_INVALID_VALUE,
4350 _("Value '%s' cannot be interpreted "
4351 "as a boolean."), value_utf8);
4352 g_free (value_utf8);
4358 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4362 return g_strdup ("true");
4364 return g_strdup ("false");
4368 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4375 string = g_string_sized_new (512);
4377 lines = g_strsplit (value, "\n", 0);
4379 for (i = 0; lines[i] != NULL; i++)
4381 if (lines[i][0] != '#')
4382 g_string_append_printf (string, "%s\n", lines[i]);
4384 g_string_append_printf (string, "%s\n", lines[i] + 1);
4388 return g_string_free (string, FALSE);
4392 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4393 const gchar *comment)
4399 string = g_string_sized_new (512);
4401 lines = g_strsplit (comment, "\n", 0);
4403 for (i = 0; lines[i] != NULL; i++)
4404 g_string_append_printf (string, "#%s%s", lines[i],
4405 lines[i + 1] == NULL? "" : "\n");
4408 return g_string_free (string, FALSE);