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;
451 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
453 struct _GKeyFileGroup
455 const gchar *name; /* NULL for above first group (which will be comments) */
457 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
458 gboolean has_trailing_blank_line;
460 GList *key_value_pairs;
462 /* Used in parallel with key_value_pairs for
463 * increased lookup performance
465 GHashTable *lookup_map;
468 struct _GKeyFileKeyValuePair
470 gchar *key; /* NULL for comments */
474 static gint find_file_in_data_dirs (const gchar *file,
475 const gchar **data_dirs,
478 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
482 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
483 const gchar *group_name);
484 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
485 const gchar *group_name);
487 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
488 GKeyFileGroup *group,
490 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
491 GKeyFileGroup *group,
494 static void g_key_file_remove_group_node (GKeyFile *key_file,
496 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
497 GKeyFileGroup *group,
500 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
501 GKeyFileGroup *group,
502 GKeyFileKeyValuePair *pair);
503 static void g_key_file_add_key (GKeyFile *key_file,
504 GKeyFileGroup *group,
507 static void g_key_file_add_group (GKeyFile *key_file,
508 const gchar *group_name);
509 static gboolean g_key_file_is_group_name (const gchar *name);
510 static gboolean g_key_file_is_key_name (const gchar *name);
511 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
512 static gboolean g_key_file_line_is_comment (const gchar *line);
513 static gboolean g_key_file_line_is_group (const gchar *line);
514 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
515 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
519 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
521 gboolean escape_separator);
522 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
525 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
527 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
530 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
533 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
535 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
537 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
538 const gchar *comment);
539 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
543 static void g_key_file_parse_comment (GKeyFile *key_file,
547 static void g_key_file_parse_group (GKeyFile *key_file,
551 static gchar *key_get_locale (const gchar *key);
552 static void g_key_file_parse_data (GKeyFile *key_file,
556 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
561 g_key_file_error_quark (void)
563 return g_quark_from_static_string ("g-key-file-error-quark");
567 g_key_file_init (GKeyFile *key_file)
569 key_file->current_group = g_slice_new0 (GKeyFileGroup);
570 key_file->groups = g_list_prepend (NULL, key_file->current_group);
571 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
572 key_file->start_group = NULL;
573 key_file->parse_buffer = g_string_sized_new (128);
574 key_file->approximate_size = 0;
575 key_file->list_separator = ';';
577 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
581 g_key_file_clear (GKeyFile *key_file)
583 GList *tmp, *group_node;
585 if (key_file->locales)
587 g_strfreev (key_file->locales);
588 key_file->locales = NULL;
591 if (key_file->parse_buffer)
593 g_string_free (key_file->parse_buffer, TRUE);
594 key_file->parse_buffer = NULL;
597 tmp = key_file->groups;
602 g_key_file_remove_group_node (key_file, group_node);
605 g_hash_table_destroy (key_file->group_hash);
606 key_file->group_hash = NULL;
608 g_warn_if_fail (key_file->groups == NULL);
615 * Creates a new empty #GKeyFile object. Use
616 * g_key_file_load_from_file(), g_key_file_load_from_data(),
617 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
618 * read an existing key file.
620 * Return value: an empty #GKeyFile.
625 g_key_file_new (void)
629 key_file = g_slice_new0 (GKeyFile);
630 g_key_file_init (key_file);
636 * g_key_file_set_list_separator:
637 * @key_file: a #GKeyFile
638 * @separator: the separator
640 * Sets the character which is used to separate
641 * values in lists. Typically ';' or ',' are used
642 * as separators. The default list separator is ';'.
647 g_key_file_set_list_separator (GKeyFile *key_file,
650 g_return_if_fail (key_file != NULL);
652 key_file->list_separator = separator;
656 /* Iterates through all the directories in *dirs trying to
657 * open file. When it successfully locates and opens a file it
658 * returns the file descriptor to the open file. It also
659 * outputs the absolute path of the file in output_file.
662 find_file_in_data_dirs (const gchar *file,
667 const gchar **data_dirs, *data_dir;
679 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
681 gchar *candidate_file, *sub_dir;
683 candidate_file = (gchar *) file;
684 sub_dir = g_strdup ("");
685 while (candidate_file != NULL && fd == -1)
689 path = g_build_filename (data_dir, sub_dir,
690 candidate_file, NULL);
692 fd = g_open (path, O_RDONLY, 0);
700 candidate_file = strchr (candidate_file, '-');
702 if (candidate_file == NULL)
708 sub_dir = g_strndup (file, candidate_file - file - 1);
710 for (p = sub_dir; *p != '\0'; p++)
713 *p = G_DIR_SEPARATOR;
722 g_set_error_literal (error, G_KEY_FILE_ERROR,
723 G_KEY_FILE_ERROR_NOT_FOUND,
724 _("Valid key file could not be "
725 "found in search dirs"));
728 if (output_file != NULL && fd > 0)
729 *output_file = g_strdup (path);
737 g_key_file_load_from_fd (GKeyFile *key_file,
742 GError *key_file_error = NULL;
744 struct stat stat_buf;
745 gchar read_buf[4096];
747 if (fstat (fd, &stat_buf) < 0)
749 g_set_error_literal (error, G_FILE_ERROR,
750 g_file_error_from_errno (errno),
755 if (!S_ISREG (stat_buf.st_mode))
757 g_set_error_literal (error, G_KEY_FILE_ERROR,
758 G_KEY_FILE_ERROR_PARSE,
759 _("Not a regular file"));
763 if (stat_buf.st_size == 0)
765 g_set_error_literal (error, G_KEY_FILE_ERROR,
766 G_KEY_FILE_ERROR_PARSE,
771 if (key_file->approximate_size > 0)
773 g_key_file_clear (key_file);
774 g_key_file_init (key_file);
776 key_file->flags = flags;
780 bytes_read = read (fd, read_buf, 4096);
782 if (bytes_read == 0) /* End of File */
787 if (errno == EINTR || errno == EAGAIN)
790 g_set_error_literal (error, G_FILE_ERROR,
791 g_file_error_from_errno (errno),
796 g_key_file_parse_data (key_file,
797 read_buf, bytes_read,
800 while (!key_file_error);
804 g_propagate_error (error, key_file_error);
808 g_key_file_flush_parse_buffer (key_file, &key_file_error);
812 g_propagate_error (error, key_file_error);
820 * g_key_file_load_from_file:
821 * @key_file: an empty #GKeyFile struct
822 * @file: the path of a filename to load, in the GLib filename encoding
823 * @flags: flags from #GKeyFileFlags
824 * @error: return location for a #GError, or %NULL
826 * Loads a key file into an empty #GKeyFile structure.
827 * If the file could not be loaded then %error is set to
828 * either a #GFileError or #GKeyFileError.
830 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
835 g_key_file_load_from_file (GKeyFile *key_file,
840 GError *key_file_error = NULL;
843 g_return_val_if_fail (key_file != NULL, FALSE);
844 g_return_val_if_fail (file != NULL, FALSE);
846 fd = g_open (file, O_RDONLY, 0);
850 g_set_error_literal (error, G_FILE_ERROR,
851 g_file_error_from_errno (errno),
856 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
861 g_propagate_error (error, key_file_error);
869 * g_key_file_load_from_data:
870 * @key_file: an empty #GKeyFile struct
871 * @data: key file loaded in memory
872 * @length: the length of @data in bytes
873 * @flags: flags from #GKeyFileFlags
874 * @error: return location for a #GError, or %NULL
876 * Loads a key file from memory into an empty #GKeyFile structure.
877 * If the object cannot be created then %error is set to a #GKeyFileError.
879 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
884 g_key_file_load_from_data (GKeyFile *key_file,
890 GError *key_file_error = NULL;
892 g_return_val_if_fail (key_file != NULL, FALSE);
893 g_return_val_if_fail (data != NULL, FALSE);
894 g_return_val_if_fail (length != 0, FALSE);
896 if (length == (gsize)-1)
897 length = strlen (data);
899 if (key_file->approximate_size > 0)
901 g_key_file_clear (key_file);
902 g_key_file_init (key_file);
904 key_file->flags = flags;
906 g_key_file_parse_data (key_file, data, length, &key_file_error);
910 g_propagate_error (error, key_file_error);
914 g_key_file_flush_parse_buffer (key_file, &key_file_error);
918 g_propagate_error (error, key_file_error);
926 * g_key_file_load_from_dirs:
927 * @key_file: an empty #GKeyFile struct
928 * @file: a relative path to a filename to open and parse
929 * @search_dirs: %NULL-terminated array of directories to search
930 * @full_path: return location for a string containing the full path
931 * of the file, or %NULL
932 * @flags: flags from #GKeyFileFlags
933 * @error: return location for a #GError, or %NULL
935 * This function looks for a key file named @file in the paths
936 * specified in @search_dirs, loads the file into @key_file and
937 * returns the file's full path in @full_path. If the file could not
938 * be loaded then an %error is set to either a #GFileError or
941 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
946 g_key_file_load_from_dirs (GKeyFile *key_file,
948 const gchar **search_dirs,
953 GError *key_file_error = NULL;
954 const gchar **data_dirs;
959 g_return_val_if_fail (key_file != NULL, FALSE);
960 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
961 g_return_val_if_fail (search_dirs != NULL, FALSE);
964 data_dirs = search_dirs;
966 while (*data_dirs != NULL && !found_file)
968 g_free (output_path);
970 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
976 g_propagate_error (error, key_file_error);
980 found_file = g_key_file_load_from_fd (key_file, fd, flags,
986 g_propagate_error (error, key_file_error);
991 if (found_file && full_path)
992 *full_path = output_path;
994 g_free (output_path);
1000 * g_key_file_load_from_data_dirs:
1001 * @key_file: an empty #GKeyFile struct
1002 * @file: a relative path to a filename to open and parse
1003 * @full_path: return location for a string containing the full path
1004 * of the file, or %NULL
1005 * @flags: flags from #GKeyFileFlags
1006 * @error: return location for a #GError, or %NULL
1008 * This function looks for a key file named @file in the paths
1009 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1010 * loads the file into @key_file and returns the file's full path in
1011 * @full_path. If the file could not be loaded then an %error is
1012 * set to either a #GFileError or #GKeyFileError.
1014 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1018 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1021 GKeyFileFlags flags,
1024 gchar **all_data_dirs;
1025 const gchar * user_data_dir;
1026 const gchar * const * system_data_dirs;
1028 gboolean found_file;
1030 g_return_val_if_fail (key_file != NULL, FALSE);
1031 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1033 user_data_dir = g_get_user_data_dir ();
1034 system_data_dirs = g_get_system_data_dirs ();
1035 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1038 all_data_dirs[i++] = g_strdup (user_data_dir);
1041 while (system_data_dirs[j] != NULL)
1042 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1043 all_data_dirs[i] = NULL;
1045 found_file = g_key_file_load_from_dirs (key_file,
1047 (const gchar **)all_data_dirs,
1052 g_strfreev (all_data_dirs);
1059 * @key_file: a #GKeyFile
1061 * Frees a #GKeyFile.
1066 g_key_file_free (GKeyFile *key_file)
1068 g_return_if_fail (key_file != NULL);
1070 g_key_file_clear (key_file);
1071 g_slice_free (GKeyFile, key_file);
1074 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1075 * true for locales that match those in g_get_language_names().
1078 g_key_file_locale_is_interesting (GKeyFile *key_file,
1079 const gchar *locale)
1083 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1086 for (i = 0; key_file->locales[i] != NULL; i++)
1088 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1096 g_key_file_parse_line (GKeyFile *key_file,
1101 GError *parse_error = NULL;
1104 g_return_if_fail (key_file != NULL);
1105 g_return_if_fail (line != NULL);
1107 line_start = (gchar *) line;
1108 while (g_ascii_isspace (*line_start))
1111 if (g_key_file_line_is_comment (line_start))
1112 g_key_file_parse_comment (key_file, line, length, &parse_error);
1113 else if (g_key_file_line_is_group (line_start))
1114 g_key_file_parse_group (key_file, line_start,
1115 length - (line_start - line),
1117 else if (g_key_file_line_is_key_value_pair (line_start))
1118 g_key_file_parse_key_value_pair (key_file, line_start,
1119 length - (line_start - line),
1123 gchar *line_utf8 = _g_utf8_make_valid (line);
1124 g_set_error (error, G_KEY_FILE_ERROR,
1125 G_KEY_FILE_ERROR_PARSE,
1126 _("Key file contains line '%s' which is not "
1127 "a key-value pair, group, or comment"),
1135 g_propagate_error (error, parse_error);
1139 g_key_file_parse_comment (GKeyFile *key_file,
1144 GKeyFileKeyValuePair *pair;
1146 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1149 g_warn_if_fail (key_file->current_group != NULL);
1151 pair = g_slice_new (GKeyFileKeyValuePair);
1153 pair->value = g_strndup (line, length);
1155 key_file->current_group->key_value_pairs =
1156 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1158 if (length == 0 || line[0] != '#')
1159 key_file->current_group->has_trailing_blank_line = TRUE;
1163 g_key_file_parse_group (GKeyFile *key_file,
1169 const gchar *group_name_start, *group_name_end;
1171 /* advance past opening '['
1173 group_name_start = line + 1;
1174 group_name_end = line + length - 1;
1176 while (*group_name_end != ']')
1179 group_name = g_strndup (group_name_start,
1180 group_name_end - group_name_start);
1182 if (!g_key_file_is_group_name (group_name))
1184 g_set_error (error, G_KEY_FILE_ERROR,
1185 G_KEY_FILE_ERROR_PARSE,
1186 _("Invalid group name: %s"), group_name);
1187 g_free (group_name);
1191 g_key_file_add_group (key_file, group_name);
1192 g_free (group_name);
1196 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1201 gchar *key, *value, *key_end, *value_start, *locale;
1202 gsize key_len, value_len;
1204 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1206 g_set_error_literal (error, G_KEY_FILE_ERROR,
1207 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1208 _("Key file does not start with a group"));
1212 key_end = value_start = strchr (line, '=');
1214 g_warn_if_fail (key_end != NULL);
1219 /* Pull the key name from the line (chomping trailing whitespace)
1221 while (g_ascii_isspace (*key_end))
1224 key_len = key_end - line + 2;
1226 g_warn_if_fail (key_len <= length);
1228 key = g_strndup (line, key_len - 1);
1230 if (!g_key_file_is_key_name (key))
1232 g_set_error (error, G_KEY_FILE_ERROR,
1233 G_KEY_FILE_ERROR_PARSE,
1234 _("Invalid key name: %s"), key);
1239 /* Pull the value from the line (chugging leading whitespace)
1241 while (g_ascii_isspace (*value_start))
1244 value_len = line + length - value_start + 1;
1246 value = g_strndup (value_start, value_len);
1248 g_warn_if_fail (key_file->start_group != NULL);
1250 if (key_file->current_group
1251 && key_file->current_group->name
1252 && strcmp (key_file->start_group->name,
1253 key_file->current_group->name) == 0
1254 && strcmp (key, "Encoding") == 0)
1256 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1258 gchar *value_utf8 = _g_utf8_make_valid (value);
1259 g_set_error (error, G_KEY_FILE_ERROR,
1260 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1261 _("Key file contains unsupported "
1262 "encoding '%s'"), value_utf8);
1263 g_free (value_utf8);
1271 /* Is this key a translation? If so, is it one that we care about?
1273 locale = key_get_locale (key);
1275 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1277 GKeyFileKeyValuePair *pair;
1279 pair = g_slice_new (GKeyFileKeyValuePair);
1281 pair->value = value;
1283 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1295 key_get_locale (const gchar *key)
1299 locale = g_strrstr (key, "[");
1301 if (locale && strlen (locale) <= 2)
1305 locale = g_strndup (locale + 1, strlen (locale) - 2);
1311 g_key_file_parse_data (GKeyFile *key_file,
1316 GError *parse_error;
1319 g_return_if_fail (key_file != NULL);
1320 g_return_if_fail (data != NULL);
1327 if (data[i] == '\n')
1329 if (key_file->parse_buffer->len > 0
1330 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1332 g_string_erase (key_file->parse_buffer,
1333 key_file->parse_buffer->len - 1,
1336 /* When a newline is encountered flush the parse buffer so that the
1337 * line can be parsed. Note that completely blank lines won't show
1338 * up in the parse buffer, so they get parsed directly.
1340 if (key_file->parse_buffer->len > 0)
1341 g_key_file_flush_parse_buffer (key_file, &parse_error);
1343 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1347 g_propagate_error (error, parse_error);
1354 const gchar *start_of_line;
1355 const gchar *end_of_line;
1358 start_of_line = data + i;
1359 end_of_line = memchr (start_of_line, '\n', length - i);
1361 if (end_of_line == NULL)
1362 end_of_line = data + length;
1364 line_length = end_of_line - start_of_line;
1366 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1371 key_file->approximate_size += length;
1375 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1378 GError *file_error = NULL;
1380 g_return_if_fail (key_file != NULL);
1384 if (key_file->parse_buffer->len > 0)
1386 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1387 key_file->parse_buffer->len,
1389 g_string_erase (key_file->parse_buffer, 0, -1);
1393 g_propagate_error (error, file_error);
1400 * g_key_file_to_data:
1401 * @key_file: a #GKeyFile
1402 * @length: return location for the length of the
1403 * returned string, or %NULL
1404 * @error: return location for a #GError, or %NULL
1406 * This function outputs @key_file as a string.
1408 * Note that this function never reports an error,
1409 * so it is safe to pass %NULL as @error.
1411 * Return value: a newly allocated string holding
1412 * the contents of the #GKeyFile
1417 g_key_file_to_data (GKeyFile *key_file,
1421 GString *data_string;
1422 GList *group_node, *key_file_node;
1423 gboolean has_blank_line = TRUE;
1425 g_return_val_if_fail (key_file != NULL, NULL);
1427 data_string = g_string_sized_new (2 * key_file->approximate_size);
1429 for (group_node = g_list_last (key_file->groups);
1431 group_node = group_node->prev)
1433 GKeyFileGroup *group;
1435 group = (GKeyFileGroup *) group_node->data;
1437 /* separate groups by at least an empty line */
1438 if (!has_blank_line)
1439 g_string_append_c (data_string, '\n');
1440 has_blank_line = group->has_trailing_blank_line;
1442 if (group->comment != NULL)
1443 g_string_append_printf (data_string, "%s\n", group->comment->value);
1445 if (group->name != NULL)
1446 g_string_append_printf (data_string, "[%s]\n", group->name);
1448 for (key_file_node = g_list_last (group->key_value_pairs);
1449 key_file_node != NULL;
1450 key_file_node = key_file_node->prev)
1452 GKeyFileKeyValuePair *pair;
1454 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1456 if (pair->key != NULL)
1457 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1459 g_string_append_printf (data_string, "%s\n", pair->value);
1464 *length = data_string->len;
1466 return g_string_free (data_string, FALSE);
1470 * g_key_file_get_keys:
1471 * @key_file: a #GKeyFile
1472 * @group_name: a group name
1473 * @length: return location for the number of keys returned, or %NULL
1474 * @error: return location for a #GError, or %NULL
1476 * Returns all keys for the group name @group_name. The array of
1477 * returned keys will be %NULL-terminated, so @length may
1478 * optionally be %NULL. In the event that the @group_name cannot
1479 * be found, %NULL is returned and @error is set to
1480 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1482 * Return value: a newly-allocated %NULL-terminated array of strings.
1483 * Use g_strfreev() to free it.
1488 g_key_file_get_keys (GKeyFile *key_file,
1489 const gchar *group_name,
1493 GKeyFileGroup *group;
1498 g_return_val_if_fail (key_file != NULL, NULL);
1499 g_return_val_if_fail (group_name != NULL, NULL);
1501 group = g_key_file_lookup_group (key_file, group_name);
1505 g_set_error (error, G_KEY_FILE_ERROR,
1506 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1507 _("Key file does not have group '%s'"),
1508 group_name ? group_name : "(null)");
1513 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1515 GKeyFileKeyValuePair *pair;
1517 pair = (GKeyFileKeyValuePair *) tmp->data;
1523 keys = g_new (gchar *, num_keys + 1);
1526 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1528 GKeyFileKeyValuePair *pair;
1530 pair = (GKeyFileKeyValuePair *) tmp->data;
1534 keys[i] = g_strdup (pair->key);
1539 keys[num_keys] = NULL;
1548 * g_key_file_get_start_group:
1549 * @key_file: a #GKeyFile
1551 * Returns the name of the start group of the file.
1553 * Return value: The start group of the key file.
1558 g_key_file_get_start_group (GKeyFile *key_file)
1560 g_return_val_if_fail (key_file != NULL, NULL);
1562 if (key_file->start_group)
1563 return g_strdup (key_file->start_group->name);
1569 * g_key_file_get_groups:
1570 * @key_file: a #GKeyFile
1571 * @length: return location for the number of returned groups, or %NULL
1573 * Returns all groups in the key file loaded with @key_file.
1574 * The array of returned groups will be %NULL-terminated, so
1575 * @length may optionally be %NULL.
1577 * Return value: a newly-allocated %NULL-terminated array of strings.
1578 * Use g_strfreev() to free it.
1582 g_key_file_get_groups (GKeyFile *key_file,
1587 gsize i, num_groups;
1589 g_return_val_if_fail (key_file != NULL, NULL);
1591 num_groups = g_list_length (key_file->groups);
1593 g_return_val_if_fail (num_groups > 0, NULL);
1595 group_node = g_list_last (key_file->groups);
1597 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1599 /* Only need num_groups instead of num_groups + 1
1600 * because the first group of the file (last in the
1601 * list) is always the comment group at the top,
1604 groups = g_new (gchar *, num_groups);
1608 for (group_node = group_node->prev;
1610 group_node = group_node->prev)
1612 GKeyFileGroup *group;
1614 group = (GKeyFileGroup *) group_node->data;
1616 g_warn_if_fail (group->name != NULL);
1618 groups[i++] = g_strdup (group->name);
1629 * g_key_file_get_value:
1630 * @key_file: a #GKeyFile
1631 * @group_name: a group name
1633 * @error: return location for a #GError, or %NULL
1635 * Returns the raw value associated with @key under @group_name.
1636 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1638 * In the event the key cannot be found, %NULL is returned and
1639 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1640 * event that the @group_name cannot be found, %NULL is returned
1641 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1644 * Return value: a newly allocated string or %NULL if the specified
1645 * key cannot be found.
1650 g_key_file_get_value (GKeyFile *key_file,
1651 const gchar *group_name,
1655 GKeyFileGroup *group;
1656 GKeyFileKeyValuePair *pair;
1657 gchar *value = NULL;
1659 g_return_val_if_fail (key_file != NULL, NULL);
1660 g_return_val_if_fail (group_name != NULL, NULL);
1661 g_return_val_if_fail (key != NULL, NULL);
1663 group = g_key_file_lookup_group (key_file, group_name);
1667 g_set_error (error, G_KEY_FILE_ERROR,
1668 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1669 _("Key file does not have group '%s'"),
1670 group_name ? group_name : "(null)");
1674 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1677 value = g_strdup (pair->value);
1679 g_set_error (error, G_KEY_FILE_ERROR,
1680 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1681 _("Key file does not have key '%s'"), key);
1687 * g_key_file_set_value:
1688 * @key_file: a #GKeyFile
1689 * @group_name: a group name
1693 * Associates a new value with @key under @group_name.
1695 * If @key cannot be found then it is created. If @group_name cannot
1696 * be found then it is created. To set an UTF-8 string which may contain
1697 * characters that need escaping (such as newlines or spaces), use
1698 * g_key_file_set_string().
1703 g_key_file_set_value (GKeyFile *key_file,
1704 const gchar *group_name,
1708 GKeyFileGroup *group;
1709 GKeyFileKeyValuePair *pair;
1711 g_return_if_fail (key_file != NULL);
1712 g_return_if_fail (g_key_file_is_group_name (group_name));
1713 g_return_if_fail (g_key_file_is_key_name (key));
1714 g_return_if_fail (value != NULL);
1716 group = g_key_file_lookup_group (key_file, group_name);
1720 g_key_file_add_group (key_file, group_name);
1721 group = (GKeyFileGroup *) key_file->groups->data;
1723 g_key_file_add_key (key_file, group, key, value);
1727 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1730 g_key_file_add_key (key_file, group, key, value);
1733 g_free (pair->value);
1734 pair->value = g_strdup (value);
1740 * g_key_file_get_string:
1741 * @key_file: a #GKeyFile
1742 * @group_name: a group name
1744 * @error: return location for a #GError, or %NULL
1746 * Returns the string value associated with @key under @group_name.
1747 * Unlike g_key_file_get_value(), this function handles escape sequences
1750 * In the event the key cannot be found, %NULL is returned and
1751 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1752 * event that the @group_name cannot be found, %NULL is returned
1753 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1755 * Return value: a newly allocated string or %NULL if the specified
1756 * key cannot be found.
1761 g_key_file_get_string (GKeyFile *key_file,
1762 const gchar *group_name,
1766 gchar *value, *string_value;
1767 GError *key_file_error;
1769 g_return_val_if_fail (key_file != NULL, NULL);
1770 g_return_val_if_fail (group_name != NULL, NULL);
1771 g_return_val_if_fail (key != NULL, NULL);
1773 key_file_error = NULL;
1775 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1779 g_propagate_error (error, key_file_error);
1783 if (!g_utf8_validate (value, -1, NULL))
1785 gchar *value_utf8 = _g_utf8_make_valid (value);
1786 g_set_error (error, G_KEY_FILE_ERROR,
1787 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1788 _("Key file contains key '%s' with value '%s' "
1789 "which is not UTF-8"), key, value_utf8);
1790 g_free (value_utf8);
1796 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1802 if (g_error_matches (key_file_error,
1804 G_KEY_FILE_ERROR_INVALID_VALUE))
1806 g_set_error (error, G_KEY_FILE_ERROR,
1807 G_KEY_FILE_ERROR_INVALID_VALUE,
1808 _("Key file contains key '%s' "
1809 "which has a value that cannot be interpreted."),
1811 g_error_free (key_file_error);
1814 g_propagate_error (error, key_file_error);
1817 return string_value;
1821 * g_key_file_set_string:
1822 * @key_file: a #GKeyFile
1823 * @group_name: a group name
1827 * Associates a new string value with @key under @group_name.
1828 * If @key cannot be found then it is created.
1829 * If @group_name cannot be found then it is created.
1830 * Unlike g_key_file_set_value(), this function handles characters
1831 * that need escaping, such as newlines.
1836 g_key_file_set_string (GKeyFile *key_file,
1837 const gchar *group_name,
1839 const gchar *string)
1843 g_return_if_fail (key_file != NULL);
1844 g_return_if_fail (string != NULL);
1846 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1847 g_key_file_set_value (key_file, group_name, key, value);
1852 * g_key_file_get_string_list:
1853 * @key_file: a #GKeyFile
1854 * @group_name: a group name
1856 * @length: return location for the number of returned strings, or %NULL
1857 * @error: return location for a #GError, or %NULL
1859 * Returns the values associated with @key under @group_name.
1861 * In the event the key cannot be found, %NULL is returned and
1862 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1863 * event that the @group_name cannot be found, %NULL is returned
1864 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1866 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1867 * a %NULL-terminated string array or %NULL if the specified
1868 * key cannot be found. The array should be freed with g_strfreev().
1873 g_key_file_get_string_list (GKeyFile *key_file,
1874 const gchar *group_name,
1879 GError *key_file_error = NULL;
1880 gchar *value, *string_value, **values;
1882 GSList *p, *pieces = NULL;
1884 g_return_val_if_fail (key_file != NULL, NULL);
1885 g_return_val_if_fail (group_name != NULL, NULL);
1886 g_return_val_if_fail (key != NULL, NULL);
1891 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1895 g_propagate_error (error, key_file_error);
1899 if (!g_utf8_validate (value, -1, NULL))
1901 gchar *value_utf8 = _g_utf8_make_valid (value);
1902 g_set_error (error, G_KEY_FILE_ERROR,
1903 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1904 _("Key file contains key '%s' with value '%s' "
1905 "which is not UTF-8"), key, value_utf8);
1906 g_free (value_utf8);
1912 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1914 g_free (string_value);
1918 if (g_error_matches (key_file_error,
1920 G_KEY_FILE_ERROR_INVALID_VALUE))
1922 g_set_error (error, G_KEY_FILE_ERROR,
1923 G_KEY_FILE_ERROR_INVALID_VALUE,
1924 _("Key file contains key '%s' "
1925 "which has a value that cannot be interpreted."),
1927 g_error_free (key_file_error);
1930 g_propagate_error (error, key_file_error);
1935 len = g_slist_length (pieces);
1936 values = g_new (gchar *, len + 1);
1937 for (p = pieces, i = 0; p; p = p->next)
1938 values[i++] = p->data;
1941 g_slist_free (pieces);
1950 * g_key_file_set_string_list:
1951 * @key_file: a #GKeyFile
1952 * @group_name: a group name
1954 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1955 * @length: number of string values in @list
1957 * Associates a list of string values for @key under @group_name.
1958 * If @key cannot be found then it is created.
1959 * If @group_name cannot be found then it is created.
1964 g_key_file_set_string_list (GKeyFile *key_file,
1965 const gchar *group_name,
1967 const gchar * const list[],
1970 GString *value_list;
1973 g_return_if_fail (key_file != NULL);
1974 g_return_if_fail (list != NULL || length == 0);
1976 value_list = g_string_sized_new (length * 128);
1977 for (i = 0; i < length && list[i] != NULL; i++)
1981 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1982 g_string_append (value_list, value);
1983 g_string_append_c (value_list, key_file->list_separator);
1988 g_key_file_set_value (key_file, group_name, key, value_list->str);
1989 g_string_free (value_list, TRUE);
1993 * g_key_file_set_locale_string:
1994 * @key_file: a #GKeyFile
1995 * @group_name: a group name
1997 * @locale: a locale identifier
2000 * Associates a string value for @key and @locale under @group_name.
2001 * If the translation for @key cannot be found then it is created.
2006 g_key_file_set_locale_string (GKeyFile *key_file,
2007 const gchar *group_name,
2009 const gchar *locale,
2010 const gchar *string)
2012 gchar *full_key, *value;
2014 g_return_if_fail (key_file != NULL);
2015 g_return_if_fail (key != NULL);
2016 g_return_if_fail (locale != NULL);
2017 g_return_if_fail (string != NULL);
2019 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2020 full_key = g_strdup_printf ("%s[%s]", key, locale);
2021 g_key_file_set_value (key_file, group_name, full_key, value);
2027 * g_key_file_get_locale_string:
2028 * @key_file: a #GKeyFile
2029 * @group_name: a group name
2031 * @locale: (allow-none): a locale identifier or %NULL
2032 * @error: return location for a #GError, or %NULL
2034 * Returns the value associated with @key under @group_name
2035 * translated in the given @locale if available. If @locale is
2036 * %NULL then the current locale is assumed.
2038 * If @key cannot be found then %NULL is returned and @error is set
2039 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2040 * with @key cannot be interpreted or no suitable translation can
2041 * be found then the untranslated value is returned.
2043 * Return value: a newly allocated string or %NULL if the specified
2044 * key cannot be found.
2049 g_key_file_get_locale_string (GKeyFile *key_file,
2050 const gchar *group_name,
2052 const gchar *locale,
2055 gchar *candidate_key, *translated_value;
2056 GError *key_file_error;
2058 gboolean free_languages = FALSE;
2061 g_return_val_if_fail (key_file != NULL, NULL);
2062 g_return_val_if_fail (group_name != NULL, NULL);
2063 g_return_val_if_fail (key != NULL, NULL);
2065 candidate_key = NULL;
2066 translated_value = NULL;
2067 key_file_error = NULL;
2071 languages = g_get_locale_variants (locale);
2072 free_languages = TRUE;
2076 languages = (gchar **) g_get_language_names ();
2077 free_languages = FALSE;
2080 for (i = 0; languages[i]; i++)
2082 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2084 translated_value = g_key_file_get_string (key_file,
2086 candidate_key, NULL);
2087 g_free (candidate_key);
2089 if (translated_value)
2092 g_free (translated_value);
2093 translated_value = NULL;
2096 /* Fallback to untranslated key
2098 if (!translated_value)
2100 translated_value = g_key_file_get_string (key_file, group_name, key,
2103 if (!translated_value)
2104 g_propagate_error (error, key_file_error);
2108 g_strfreev (languages);
2110 return translated_value;
2114 * g_key_file_get_locale_string_list:
2115 * @key_file: a #GKeyFile
2116 * @group_name: a group name
2118 * @locale: (allow-none): a locale identifier or %NULL
2119 * @length: (out): return location for the number of returned strings or %NULL
2120 * @error: return location for a #GError or %NULL
2122 * Returns the values associated with @key under @group_name
2123 * translated in the given @locale if available. If @locale is
2124 * %NULL then the current locale is assumed.
2126 * If @key cannot be found then %NULL is returned and @error is set
2127 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2128 * with @key cannot be interpreted or no suitable translations
2129 * can be found then the untranslated values are returned. The
2130 * returned array is %NULL-terminated, so @length may optionally
2133 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2134 * or %NULL if the key isn't found. The string array should be freed
2135 * with g_strfreev().
2140 g_key_file_get_locale_string_list (GKeyFile *key_file,
2141 const gchar *group_name,
2143 const gchar *locale,
2147 GError *key_file_error;
2148 gchar **values, *value;
2149 char list_separator[2];
2152 g_return_val_if_fail (key_file != NULL, NULL);
2153 g_return_val_if_fail (group_name != NULL, NULL);
2154 g_return_val_if_fail (key != NULL, NULL);
2156 key_file_error = NULL;
2158 value = g_key_file_get_locale_string (key_file, group_name,
2163 g_propagate_error (error, key_file_error);
2172 len = strlen (value);
2173 if (value[len - 1] == key_file->list_separator)
2174 value[len - 1] = '\0';
2176 list_separator[0] = key_file->list_separator;
2177 list_separator[1] = '\0';
2178 values = g_strsplit (value, list_separator, 0);
2183 *length = g_strv_length (values);
2189 * g_key_file_set_locale_string_list:
2190 * @key_file: a #GKeyFile
2191 * @group_name: a group name
2193 * @locale: a locale identifier
2194 * @list: a %NULL-terminated array of locale string values
2195 * @length: the length of @list
2197 * Associates a list of string values for @key and @locale under
2198 * @group_name. If the translation for @key cannot be found then
2204 g_key_file_set_locale_string_list (GKeyFile *key_file,
2205 const gchar *group_name,
2207 const gchar *locale,
2208 const gchar * const list[],
2211 GString *value_list;
2215 g_return_if_fail (key_file != NULL);
2216 g_return_if_fail (key != NULL);
2217 g_return_if_fail (locale != NULL);
2218 g_return_if_fail (length != 0);
2220 value_list = g_string_sized_new (length * 128);
2221 for (i = 0; i < length && list[i] != NULL; i++)
2225 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2226 g_string_append (value_list, value);
2227 g_string_append_c (value_list, key_file->list_separator);
2232 full_key = g_strdup_printf ("%s[%s]", key, locale);
2233 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2235 g_string_free (value_list, TRUE);
2239 * g_key_file_get_boolean:
2240 * @key_file: a #GKeyFile
2241 * @group_name: a group name
2243 * @error: return location for a #GError
2245 * Returns the value associated with @key under @group_name as a
2248 * If @key cannot be found then %FALSE is returned and @error is set
2249 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2250 * associated with @key cannot be interpreted as a boolean then %FALSE
2251 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2253 * Return value: the value associated with the key as a boolean,
2254 * or %FALSE if the key was not found or could not be parsed.
2259 g_key_file_get_boolean (GKeyFile *key_file,
2260 const gchar *group_name,
2264 GError *key_file_error = NULL;
2266 gboolean bool_value;
2268 g_return_val_if_fail (key_file != NULL, FALSE);
2269 g_return_val_if_fail (group_name != NULL, FALSE);
2270 g_return_val_if_fail (key != NULL, FALSE);
2272 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2276 g_propagate_error (error, key_file_error);
2280 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2286 if (g_error_matches (key_file_error,
2288 G_KEY_FILE_ERROR_INVALID_VALUE))
2290 g_set_error (error, G_KEY_FILE_ERROR,
2291 G_KEY_FILE_ERROR_INVALID_VALUE,
2292 _("Key file contains key '%s' "
2293 "which has a value that cannot be interpreted."),
2295 g_error_free (key_file_error);
2298 g_propagate_error (error, key_file_error);
2305 * g_key_file_set_boolean:
2306 * @key_file: a #GKeyFile
2307 * @group_name: a group name
2309 * @value: %TRUE or %FALSE
2311 * Associates a new boolean value with @key under @group_name.
2312 * If @key cannot be found then it is created.
2317 g_key_file_set_boolean (GKeyFile *key_file,
2318 const gchar *group_name,
2324 g_return_if_fail (key_file != NULL);
2326 result = g_key_file_parse_boolean_as_value (key_file, value);
2327 g_key_file_set_value (key_file, group_name, key, result);
2332 * g_key_file_get_boolean_list:
2333 * @key_file: a #GKeyFile
2334 * @group_name: a group name
2336 * @length: the number of booleans returned
2337 * @error: return location for a #GError
2339 * Returns the values associated with @key under @group_name as
2342 * If @key cannot be found then %NULL is returned and @error is set to
2343 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2344 * with @key cannot be interpreted as booleans then %NULL is returned
2345 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2347 * Return value: (array length=length) (element-type gboolean) (transfer container):
2348 * the values associated with the key as a list of booleans, or %NULL if the
2349 * key was not found or could not be parsed. The returned list of booleans
2350 * should be freed with g_free() when no longer needed.
2355 g_key_file_get_boolean_list (GKeyFile *key_file,
2356 const gchar *group_name,
2361 GError *key_file_error;
2363 gboolean *bool_values;
2366 g_return_val_if_fail (key_file != NULL, NULL);
2367 g_return_val_if_fail (group_name != NULL, NULL);
2368 g_return_val_if_fail (key != NULL, NULL);
2373 key_file_error = NULL;
2375 values = g_key_file_get_string_list (key_file, group_name, key,
2376 &num_bools, &key_file_error);
2379 g_propagate_error (error, key_file_error);
2384 bool_values = g_new (gboolean, num_bools);
2386 for (i = 0; i < num_bools; i++)
2388 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2394 g_propagate_error (error, key_file_error);
2395 g_strfreev (values);
2396 g_free (bool_values);
2401 g_strfreev (values);
2404 *length = num_bools;
2410 * g_key_file_set_boolean_list:
2411 * @key_file: a #GKeyFile
2412 * @group_name: a group name
2414 * @list: an array of boolean values
2415 * @length: length of @list
2417 * Associates a list of boolean values with @key under @group_name.
2418 * If @key cannot be found then it is created.
2419 * If @group_name is %NULL, the start_group is used.
2424 g_key_file_set_boolean_list (GKeyFile *key_file,
2425 const gchar *group_name,
2430 GString *value_list;
2433 g_return_if_fail (key_file != NULL);
2434 g_return_if_fail (list != NULL);
2436 value_list = g_string_sized_new (length * 8);
2437 for (i = 0; i < length; i++)
2441 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2443 g_string_append (value_list, value);
2444 g_string_append_c (value_list, key_file->list_separator);
2449 g_key_file_set_value (key_file, group_name, key, value_list->str);
2450 g_string_free (value_list, TRUE);
2454 * g_key_file_get_integer:
2455 * @key_file: a #GKeyFile
2456 * @group_name: a group name
2458 * @error: return location for a #GError
2460 * Returns the value associated with @key under @group_name as an
2463 * If @key cannot be found then 0 is returned and @error is set to
2464 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2465 * with @key cannot be interpreted as an integer then 0 is returned
2466 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2468 * Return value: the value associated with the key as an integer, or
2469 * 0 if the key was not found or could not be parsed.
2474 g_key_file_get_integer (GKeyFile *key_file,
2475 const gchar *group_name,
2479 GError *key_file_error;
2483 g_return_val_if_fail (key_file != NULL, -1);
2484 g_return_val_if_fail (group_name != NULL, -1);
2485 g_return_val_if_fail (key != NULL, -1);
2487 key_file_error = NULL;
2489 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2493 g_propagate_error (error, key_file_error);
2497 int_value = g_key_file_parse_value_as_integer (key_file, value,
2503 if (g_error_matches (key_file_error,
2505 G_KEY_FILE_ERROR_INVALID_VALUE))
2507 g_set_error (error, G_KEY_FILE_ERROR,
2508 G_KEY_FILE_ERROR_INVALID_VALUE,
2509 _("Key file contains key '%s' in group '%s' "
2510 "which has a value that cannot be interpreted."),
2512 g_error_free (key_file_error);
2515 g_propagate_error (error, key_file_error);
2522 * g_key_file_set_integer:
2523 * @key_file: a #GKeyFile
2524 * @group_name: a group name
2526 * @value: an integer value
2528 * Associates a new integer value with @key under @group_name.
2529 * If @key cannot be found then it is created.
2534 g_key_file_set_integer (GKeyFile *key_file,
2535 const gchar *group_name,
2541 g_return_if_fail (key_file != NULL);
2543 result = g_key_file_parse_integer_as_value (key_file, value);
2544 g_key_file_set_value (key_file, group_name, key, result);
2549 * g_key_file_get_int64:
2550 * @key_file: a non-%NULL #GKeyFile
2551 * @group_name: a non-%NULL group name
2552 * @key: a non-%NULL key
2553 * @error: return location for a #GError
2555 * Returns the value associated with @key under @group_name as a signed
2556 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2557 * 64-bit results without truncation.
2559 * Returns: the value associated with the key as a signed 64-bit integer, or
2560 * 0 if the key was not found or could not be parsed.
2565 g_key_file_get_int64 (GKeyFile *key_file,
2566 const gchar *group_name,
2573 g_return_val_if_fail (key_file != NULL, -1);
2574 g_return_val_if_fail (group_name != NULL, -1);
2575 g_return_val_if_fail (key != NULL, -1);
2577 s = g_key_file_get_value (key_file, group_name, key, error);
2582 v = g_ascii_strtoll (s, &end, 10);
2584 if (*s == '\0' || *end != '\0')
2586 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2587 _("Key '%s' in group '%s' has value '%s' "
2588 "where %s was expected"),
2589 key, group_name, s, "int64");
2598 * g_key_file_set_int64:
2599 * @key_file: a #GKeyFile
2600 * @group_name: a group name
2602 * @value: an integer value
2604 * Associates a new integer value with @key under @group_name.
2605 * If @key cannot be found then it is created.
2610 g_key_file_set_int64 (GKeyFile *key_file,
2611 const gchar *group_name,
2617 g_return_if_fail (key_file != NULL);
2619 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2620 g_key_file_set_value (key_file, group_name, key, result);
2625 * g_key_file_get_uint64:
2626 * @key_file: a non-%NULL #GKeyFile
2627 * @group_name: a non-%NULL group name
2628 * @key: a non-%NULL key
2629 * @error: return location for a #GError
2631 * Returns the value associated with @key under @group_name as an unsigned
2632 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2633 * large positive results without truncation.
2635 * Returns: the value associated with the key as an unsigned 64-bit integer,
2636 * or 0 if the key was not found or could not be parsed.
2641 g_key_file_get_uint64 (GKeyFile *key_file,
2642 const gchar *group_name,
2649 g_return_val_if_fail (key_file != NULL, -1);
2650 g_return_val_if_fail (group_name != NULL, -1);
2651 g_return_val_if_fail (key != NULL, -1);
2653 s = g_key_file_get_value (key_file, group_name, key, error);
2658 v = g_ascii_strtoull (s, &end, 10);
2660 if (*s == '\0' || *end != '\0')
2662 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2663 _("Key '%s' in group '%s' has value '%s' "
2664 "where %s was expected"),
2665 key, group_name, s, "uint64");
2674 * g_key_file_set_uint64:
2675 * @key_file: a #GKeyFile
2676 * @group_name: a group name
2678 * @value: an integer value
2680 * Associates a new integer value with @key under @group_name.
2681 * If @key cannot be found then it is created.
2686 g_key_file_set_uint64 (GKeyFile *key_file,
2687 const gchar *group_name,
2693 g_return_if_fail (key_file != NULL);
2695 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2696 g_key_file_set_value (key_file, group_name, key, result);
2701 * g_key_file_get_integer_list:
2702 * @key_file: a #GKeyFile
2703 * @group_name: a group name
2705 * @length: the number of integers returned
2706 * @error: return location for a #GError
2708 * Returns the values associated with @key under @group_name as
2711 * If @key cannot be found then %NULL is returned and @error is set to
2712 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2713 * with @key cannot be interpreted as integers then %NULL is returned
2714 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2716 * Return value: (array length=length) (element-type gint) (transfer container):
2717 * the values associated with the key as a list of integers, or %NULL if
2718 * the key was not found or could not be parsed. The returned list of
2719 * integers should be freed with g_free() when no longer needed.
2724 g_key_file_get_integer_list (GKeyFile *key_file,
2725 const gchar *group_name,
2730 GError *key_file_error = NULL;
2735 g_return_val_if_fail (key_file != NULL, NULL);
2736 g_return_val_if_fail (group_name != NULL, NULL);
2737 g_return_val_if_fail (key != NULL, NULL);
2742 values = g_key_file_get_string_list (key_file, group_name, key,
2743 &num_ints, &key_file_error);
2746 g_propagate_error (error, key_file_error);
2751 int_values = g_new (gint, num_ints);
2753 for (i = 0; i < num_ints; i++)
2755 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2761 g_propagate_error (error, key_file_error);
2762 g_strfreev (values);
2763 g_free (int_values);
2768 g_strfreev (values);
2777 * g_key_file_set_integer_list:
2778 * @key_file: a #GKeyFile
2779 * @group_name: a group name
2781 * @list: an array of integer values
2782 * @length: number of integer values in @list
2784 * Associates a list of integer values with @key under @group_name.
2785 * If @key cannot be found then it is created.
2790 g_key_file_set_integer_list (GKeyFile *key_file,
2791 const gchar *group_name,
2799 g_return_if_fail (key_file != NULL);
2800 g_return_if_fail (list != NULL);
2802 values = g_string_sized_new (length * 16);
2803 for (i = 0; i < length; i++)
2807 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2809 g_string_append (values, value);
2810 g_string_append_c (values, key_file->list_separator);
2815 g_key_file_set_value (key_file, group_name, key, values->str);
2816 g_string_free (values, TRUE);
2820 * g_key_file_get_double:
2821 * @key_file: a #GKeyFile
2822 * @group_name: a group name
2824 * @error: return location for a #GError
2826 * Returns the value associated with @key under @group_name as a
2827 * double. If @group_name is %NULL, the start_group is used.
2829 * If @key cannot be found then 0.0 is returned and @error is set to
2830 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2831 * with @key cannot be interpreted as a double then 0.0 is returned
2832 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2834 * Return value: the value associated with the key as a double, or
2835 * 0.0 if the key was not found or could not be parsed.
2840 g_key_file_get_double (GKeyFile *key_file,
2841 const gchar *group_name,
2845 GError *key_file_error;
2847 gdouble double_value;
2849 g_return_val_if_fail (key_file != NULL, -1);
2850 g_return_val_if_fail (group_name != NULL, -1);
2851 g_return_val_if_fail (key != NULL, -1);
2853 key_file_error = NULL;
2855 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2859 g_propagate_error (error, key_file_error);
2863 double_value = g_key_file_parse_value_as_double (key_file, value,
2869 if (g_error_matches (key_file_error,
2871 G_KEY_FILE_ERROR_INVALID_VALUE))
2873 g_set_error (error, G_KEY_FILE_ERROR,
2874 G_KEY_FILE_ERROR_INVALID_VALUE,
2875 _("Key file contains key '%s' in group '%s' "
2876 "which has a value that cannot be interpreted."),
2878 g_error_free (key_file_error);
2881 g_propagate_error (error, key_file_error);
2884 return double_value;
2888 * g_key_file_set_double:
2889 * @key_file: a #GKeyFile
2890 * @group_name: a group name
2892 * @value: an double value
2894 * Associates a new double value with @key under @group_name.
2895 * If @key cannot be found then it is created.
2900 g_key_file_set_double (GKeyFile *key_file,
2901 const gchar *group_name,
2905 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2907 g_return_if_fail (key_file != NULL);
2909 g_ascii_dtostr (result, sizeof (result), value);
2910 g_key_file_set_value (key_file, group_name, key, result);
2914 * g_key_file_get_double_list:
2915 * @key_file: a #GKeyFile
2916 * @group_name: a group name
2918 * @length: the number of doubles returned
2919 * @error: return location for a #GError
2921 * Returns the values associated with @key under @group_name as
2924 * If @key cannot be found then %NULL is returned and @error is set to
2925 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2926 * with @key cannot be interpreted as doubles then %NULL is returned
2927 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2929 * Return value: (array length=length) (element-type gdouble) (transfer container):
2930 * the values associated with the key as a list of doubles, or %NULL if the
2931 * key was not found or could not be parsed. The returned list of doubles
2932 * should be freed with g_free() when no longer needed.
2937 g_key_file_get_double_list (GKeyFile *key_file,
2938 const gchar *group_name,
2943 GError *key_file_error = NULL;
2945 gdouble *double_values;
2946 gsize i, num_doubles;
2948 g_return_val_if_fail (key_file != NULL, NULL);
2949 g_return_val_if_fail (group_name != NULL, NULL);
2950 g_return_val_if_fail (key != NULL, NULL);
2955 values = g_key_file_get_string_list (key_file, group_name, key,
2956 &num_doubles, &key_file_error);
2959 g_propagate_error (error, key_file_error);
2964 double_values = g_new (gdouble, num_doubles);
2966 for (i = 0; i < num_doubles; i++)
2968 double_values[i] = g_key_file_parse_value_as_double (key_file,
2974 g_propagate_error (error, key_file_error);
2975 g_strfreev (values);
2976 g_free (double_values);
2981 g_strfreev (values);
2984 *length = num_doubles;
2986 return double_values;
2990 * g_key_file_set_double_list:
2991 * @key_file: a #GKeyFile
2992 * @group_name: a group name
2994 * @list: an array of double values
2995 * @length: number of double values in @list
2997 * Associates a list of double values with @key under
2998 * @group_name. If @key cannot be found then it is created.
3003 g_key_file_set_double_list (GKeyFile *key_file,
3004 const gchar *group_name,
3012 g_return_if_fail (key_file != NULL);
3013 g_return_if_fail (list != NULL);
3015 values = g_string_sized_new (length * 16);
3016 for (i = 0; i < length; i++)
3018 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3020 g_ascii_dtostr( result, sizeof (result), list[i] );
3022 g_string_append (values, result);
3023 g_string_append_c (values, key_file->list_separator);
3026 g_key_file_set_value (key_file, group_name, key, values->str);
3027 g_string_free (values, TRUE);
3031 g_key_file_set_key_comment (GKeyFile *key_file,
3032 const gchar *group_name,
3034 const gchar *comment,
3037 GKeyFileGroup *group;
3038 GKeyFileKeyValuePair *pair;
3039 GList *key_node, *comment_node, *tmp;
3041 group = g_key_file_lookup_group (key_file, group_name);
3044 g_set_error (error, G_KEY_FILE_ERROR,
3045 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3046 _("Key file does not have group '%s'"),
3047 group_name ? group_name : "(null)");
3052 /* First find the key the comments are supposed to be
3055 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3057 if (key_node == NULL)
3059 g_set_error (error, G_KEY_FILE_ERROR,
3060 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3061 _("Key file does not have key '%s' in group '%s'"),
3066 /* Then find all the comments already associated with the
3069 tmp = key_node->next;
3072 pair = (GKeyFileKeyValuePair *) tmp->data;
3074 if (pair->key != NULL)
3079 g_key_file_remove_key_value_pair_node (key_file, group,
3083 if (comment == NULL)
3086 /* Now we can add our new comment
3088 pair = g_slice_new (GKeyFileKeyValuePair);
3090 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3092 key_node = g_list_insert (key_node, pair, 1);
3098 g_key_file_set_group_comment (GKeyFile *key_file,
3099 const gchar *group_name,
3100 const gchar *comment,
3103 GKeyFileGroup *group;
3105 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3107 group = g_key_file_lookup_group (key_file, group_name);
3110 g_set_error (error, G_KEY_FILE_ERROR,
3111 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3112 _("Key file does not have group '%s'"),
3113 group_name ? group_name : "(null)");
3118 /* First remove any existing comment
3122 g_key_file_key_value_pair_free (group->comment);
3123 group->comment = NULL;
3126 if (comment == NULL)
3129 /* Now we can add our new comment
3131 group->comment = g_slice_new (GKeyFileKeyValuePair);
3132 group->comment->key = NULL;
3133 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3139 g_key_file_set_top_comment (GKeyFile *key_file,
3140 const gchar *comment,
3144 GKeyFileGroup *group;
3145 GKeyFileKeyValuePair *pair;
3147 /* The last group in the list should be the top (comments only)
3150 g_warn_if_fail (key_file->groups != NULL);
3151 group_node = g_list_last (key_file->groups);
3152 group = (GKeyFileGroup *) group_node->data;
3153 g_warn_if_fail (group->name == NULL);
3155 /* Note all keys must be comments at the top of
3156 * the file, so we can just free it all.
3158 if (group->key_value_pairs != NULL)
3160 g_list_foreach (group->key_value_pairs,
3161 (GFunc) g_key_file_key_value_pair_free,
3163 g_list_free (group->key_value_pairs);
3164 group->key_value_pairs = NULL;
3167 if (comment == NULL)
3170 pair = g_slice_new (GKeyFileKeyValuePair);
3172 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3174 group->key_value_pairs =
3175 g_list_prepend (group->key_value_pairs, pair);
3181 * g_key_file_set_comment:
3182 * @key_file: a #GKeyFile
3183 * @group_name: a group name, or %NULL
3185 * @comment: a comment
3186 * @error: return location for a #GError
3188 * Places a comment above @key from @group_name.
3189 * If @key is %NULL then @comment will be written above @group_name.
3190 * If both @key and @group_name are %NULL, then @comment will be
3191 * written above the first group in the file.
3193 * Returns: %TRUE if the comment was written, %FALSE otherwise
3198 g_key_file_set_comment (GKeyFile *key_file,
3199 const gchar *group_name,
3201 const gchar *comment,
3204 g_return_val_if_fail (key_file != NULL, FALSE);
3206 if (group_name != NULL && key != NULL)
3208 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3211 else if (group_name != NULL)
3213 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3218 if (!g_key_file_set_top_comment (key_file, comment, error))
3222 if (comment != NULL)
3223 key_file->approximate_size += strlen (comment);
3229 g_key_file_get_key_comment (GKeyFile *key_file,
3230 const gchar *group_name,
3234 GKeyFileGroup *group;
3235 GKeyFileKeyValuePair *pair;
3236 GList *key_node, *tmp;
3240 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3242 group = g_key_file_lookup_group (key_file, group_name);
3245 g_set_error (error, G_KEY_FILE_ERROR,
3246 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3247 _("Key file does not have group '%s'"),
3248 group_name ? group_name : "(null)");
3253 /* First find the key the comments are supposed to be
3256 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3258 if (key_node == NULL)
3260 g_set_error (error, G_KEY_FILE_ERROR,
3261 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3262 _("Key file does not have key '%s' in group '%s'"),
3269 /* Then find all the comments already associated with the
3270 * key and concatentate them.
3272 tmp = key_node->next;
3273 if (!key_node->next)
3276 pair = (GKeyFileKeyValuePair *) tmp->data;
3277 if (pair->key != NULL)
3282 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3284 if (pair->key != NULL)
3290 while (tmp != key_node)
3292 pair = (GKeyFileKeyValuePair *) tmp->data;
3295 string = g_string_sized_new (512);
3297 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3298 g_string_append (string, comment);
3306 comment = string->str;
3307 g_string_free (string, FALSE);
3316 get_group_comment (GKeyFile *key_file,
3317 GKeyFileGroup *group,
3326 tmp = group->key_value_pairs;
3329 GKeyFileKeyValuePair *pair;
3331 pair = (GKeyFileKeyValuePair *) tmp->data;
3333 if (pair->key != NULL)
3339 if (tmp->next == NULL)
3347 GKeyFileKeyValuePair *pair;
3349 pair = (GKeyFileKeyValuePair *) tmp->data;
3352 string = g_string_sized_new (512);
3354 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3355 g_string_append (string, comment);
3362 return g_string_free (string, FALSE);
3368 g_key_file_get_group_comment (GKeyFile *key_file,
3369 const gchar *group_name,
3373 GKeyFileGroup *group;
3375 group = g_key_file_lookup_group (key_file, group_name);
3378 g_set_error (error, G_KEY_FILE_ERROR,
3379 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3380 _("Key file does not have group '%s'"),
3381 group_name ? group_name : "(null)");
3387 return g_strdup (group->comment->value);
3389 group_node = g_key_file_lookup_group_node (key_file, group_name);
3390 group_node = group_node->next;
3391 group = (GKeyFileGroup *)group_node->data;
3392 return get_group_comment (key_file, group, error);
3396 g_key_file_get_top_comment (GKeyFile *key_file,
3400 GKeyFileGroup *group;
3402 /* The last group in the list should be the top (comments only)
3405 g_warn_if_fail (key_file->groups != NULL);
3406 group_node = g_list_last (key_file->groups);
3407 group = (GKeyFileGroup *) group_node->data;
3408 g_warn_if_fail (group->name == NULL);
3410 return get_group_comment (key_file, group, error);
3414 * g_key_file_get_comment:
3415 * @key_file: a #GKeyFile
3416 * @group_name: a group name, or %NULL
3418 * @error: return location for a #GError
3420 * Retrieves a comment above @key from @group_name.
3421 * If @key is %NULL then @comment will be read from above
3422 * @group_name. If both @key and @group_name are %NULL, then
3423 * @comment will be read from above the first group in the file.
3425 * Returns: a comment that should be freed with g_free()
3430 g_key_file_get_comment (GKeyFile *key_file,
3431 const gchar *group_name,
3435 g_return_val_if_fail (key_file != NULL, NULL);
3437 if (group_name != NULL && key != NULL)
3438 return g_key_file_get_key_comment (key_file, group_name, key, error);
3439 else if (group_name != NULL)
3440 return g_key_file_get_group_comment (key_file, group_name, error);
3442 return g_key_file_get_top_comment (key_file, error);
3446 * g_key_file_remove_comment:
3447 * @key_file: a #GKeyFile
3448 * @group_name: a group name, or %NULL
3450 * @error: return location for a #GError
3452 * Removes a comment above @key from @group_name.
3453 * If @key is %NULL then @comment will be removed above @group_name.
3454 * If both @key and @group_name are %NULL, then @comment will
3455 * be removed above the first group in the file.
3457 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3463 g_key_file_remove_comment (GKeyFile *key_file,
3464 const gchar *group_name,
3468 g_return_val_if_fail (key_file != NULL, FALSE);
3470 if (group_name != NULL && key != NULL)
3471 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3472 else if (group_name != NULL)
3473 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3475 return g_key_file_set_top_comment (key_file, NULL, error);
3479 * g_key_file_has_group:
3480 * @key_file: a #GKeyFile
3481 * @group_name: a group name
3483 * Looks whether the key file has the group @group_name.
3485 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3490 g_key_file_has_group (GKeyFile *key_file,
3491 const gchar *group_name)
3493 g_return_val_if_fail (key_file != NULL, FALSE);
3494 g_return_val_if_fail (group_name != NULL, FALSE);
3496 return g_key_file_lookup_group (key_file, group_name) != NULL;
3499 /* This code remains from a historical attempt to add a new public API
3500 * which respects the GError rules.
3503 g_key_file_has_key_full (GKeyFile *key_file,
3504 const gchar *group_name,
3509 GKeyFileKeyValuePair *pair;
3510 GKeyFileGroup *group;
3512 g_return_val_if_fail (key_file != NULL, FALSE);
3513 g_return_val_if_fail (group_name != NULL, FALSE);
3514 g_return_val_if_fail (key != NULL, FALSE);
3516 group = g_key_file_lookup_group (key_file, group_name);
3520 g_set_error (error, G_KEY_FILE_ERROR,
3521 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3522 _("Key file does not have group '%s'"),
3523 group_name ? group_name : "(null)");
3528 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3531 *has_key = pair != NULL;
3536 * g_key_file_has_key: (skip)
3537 * @key_file: a #GKeyFile
3538 * @group_name: a group name
3540 * @error: return location for a #GError
3542 * Looks whether the key file has the key @key in the group
3545 * <note>This function does not follow the rules for #GError strictly;
3546 * the return value both carries meaning and signals an error. To use
3547 * this function, you must pass a #GError pointer in @error, and check
3548 * whether it is not %NULL to see if an error occurred.</note>
3550 * Language bindings should use g_key_file_get_value() to test whether
3551 * or not a key exists.
3553 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3559 g_key_file_has_key (GKeyFile *key_file,
3560 const gchar *group_name,
3564 GError *temp_error = NULL;
3567 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3573 g_propagate_error (error, temp_error);
3579 g_key_file_add_group (GKeyFile *key_file,
3580 const gchar *group_name)
3582 GKeyFileGroup *group;
3584 g_return_if_fail (key_file != NULL);
3585 g_return_if_fail (g_key_file_is_group_name (group_name));
3587 group = g_key_file_lookup_group (key_file, group_name);
3590 key_file->current_group = group;
3594 group = g_slice_new0 (GKeyFileGroup);
3595 group->name = g_strdup (group_name);
3596 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3597 key_file->groups = g_list_prepend (key_file->groups, group);
3598 key_file->approximate_size += strlen (group_name) + 3;
3599 key_file->current_group = group;
3601 if (key_file->start_group == NULL)
3602 key_file->start_group = group;
3604 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3608 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3613 g_free (pair->value);
3614 g_slice_free (GKeyFileKeyValuePair, pair);
3618 /* Be careful not to call this function on a node with data in the
3619 * lookup map without removing it from the lookup map, first.
3621 * Some current cases where this warning is not a concern are
3623 * - the node being removed is a comment node
3624 * - the entire lookup map is getting destroyed soon after
3628 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3629 GKeyFileGroup *group,
3633 GKeyFileKeyValuePair *pair;
3635 pair = (GKeyFileKeyValuePair *) pair_node->data;
3637 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3639 if (pair->key != NULL)
3640 key_file->approximate_size -= strlen (pair->key) + 1;
3642 g_warn_if_fail (pair->value != NULL);
3643 key_file->approximate_size -= strlen (pair->value);
3645 g_key_file_key_value_pair_free (pair);
3647 g_list_free_1 (pair_node);
3651 g_key_file_remove_group_node (GKeyFile *key_file,
3654 GKeyFileGroup *group;
3657 group = (GKeyFileGroup *) group_node->data;
3660 g_hash_table_remove (key_file->group_hash, group->name);
3662 /* If the current group gets deleted make the current group the last
3665 if (key_file->current_group == group)
3667 /* groups should always contain at least the top comment group,
3668 * unless g_key_file_clear has been called
3670 if (key_file->groups)
3671 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3673 key_file->current_group = NULL;
3676 /* If the start group gets deleted make the start group the first
3679 if (key_file->start_group == group)
3681 tmp = g_list_last (key_file->groups);
3684 if (tmp != group_node &&
3685 ((GKeyFileGroup *) tmp->data)->name != NULL)
3692 key_file->start_group = (GKeyFileGroup *) tmp->data;
3694 key_file->start_group = NULL;
3697 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3699 if (group->name != NULL)
3700 key_file->approximate_size -= strlen (group->name) + 3;
3702 tmp = group->key_value_pairs;
3709 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3712 g_warn_if_fail (group->key_value_pairs == NULL);
3714 if (group->lookup_map)
3716 g_hash_table_destroy (group->lookup_map);
3717 group->lookup_map = NULL;
3720 g_free ((gchar *) group->name);
3721 g_slice_free (GKeyFileGroup, group);
3722 g_list_free_1 (group_node);
3726 * g_key_file_remove_group:
3727 * @key_file: a #GKeyFile
3728 * @group_name: a group name
3729 * @error: return location for a #GError or %NULL
3731 * Removes the specified group, @group_name,
3732 * from the key file.
3734 * Returns: %TRUE if the group was removed, %FALSE otherwise
3739 g_key_file_remove_group (GKeyFile *key_file,
3740 const gchar *group_name,
3745 g_return_val_if_fail (key_file != NULL, FALSE);
3746 g_return_val_if_fail (group_name != NULL, FALSE);
3748 group_node = g_key_file_lookup_group_node (key_file, group_name);
3752 g_set_error (error, G_KEY_FILE_ERROR,
3753 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3754 _("Key file does not have group '%s'"),
3759 g_key_file_remove_group_node (key_file, group_node);
3765 g_key_file_add_key_value_pair (GKeyFile *key_file,
3766 GKeyFileGroup *group,
3767 GKeyFileKeyValuePair *pair)
3769 g_hash_table_replace (group->lookup_map, pair->key, pair);
3770 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3771 group->has_trailing_blank_line = FALSE;
3772 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3776 g_key_file_add_key (GKeyFile *key_file,
3777 GKeyFileGroup *group,
3781 GKeyFileKeyValuePair *pair;
3783 pair = g_slice_new (GKeyFileKeyValuePair);
3784 pair->key = g_strdup (key);
3785 pair->value = g_strdup (value);
3787 g_key_file_add_key_value_pair (key_file, group, pair);
3791 * g_key_file_remove_key:
3792 * @key_file: a #GKeyFile
3793 * @group_name: a group name
3794 * @key: a key name to remove
3795 * @error: return location for a #GError or %NULL
3797 * Removes @key in @group_name from the key file.
3799 * Returns: %TRUE if the key was removed, %FALSE otherwise
3804 g_key_file_remove_key (GKeyFile *key_file,
3805 const gchar *group_name,
3809 GKeyFileGroup *group;
3810 GKeyFileKeyValuePair *pair;
3812 g_return_val_if_fail (key_file != NULL, FALSE);
3813 g_return_val_if_fail (group_name != NULL, FALSE);
3814 g_return_val_if_fail (key != NULL, FALSE);
3818 group = g_key_file_lookup_group (key_file, group_name);
3821 g_set_error (error, G_KEY_FILE_ERROR,
3822 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3823 _("Key file does not have group '%s'"),
3824 group_name ? group_name : "(null)");
3828 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3832 g_set_error (error, G_KEY_FILE_ERROR,
3833 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3834 _("Key file does not have key '%s' in group '%s'"),
3839 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3841 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3842 g_hash_table_remove (group->lookup_map, pair->key);
3843 g_key_file_key_value_pair_free (pair);
3849 g_key_file_lookup_group_node (GKeyFile *key_file,
3850 const gchar *group_name)
3852 GKeyFileGroup *group;
3855 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3857 group = (GKeyFileGroup *) tmp->data;
3859 if (group && group->name && strcmp (group->name, group_name) == 0)
3866 static GKeyFileGroup *
3867 g_key_file_lookup_group (GKeyFile *key_file,
3868 const gchar *group_name)
3870 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3874 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3875 GKeyFileGroup *group,
3880 for (key_node = group->key_value_pairs;
3882 key_node = key_node->next)
3884 GKeyFileKeyValuePair *pair;
3886 pair = (GKeyFileKeyValuePair *) key_node->data;
3888 if (pair->key && strcmp (pair->key, key) == 0)
3895 static GKeyFileKeyValuePair *
3896 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3897 GKeyFileGroup *group,
3900 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3903 /* Lines starting with # or consisting entirely of whitespace are merely
3904 * recorded, not parsed. This function assumes all leading whitespace
3905 * has been stripped.
3908 g_key_file_line_is_comment (const gchar *line)
3910 return (*line == '#' || *line == '\0' || *line == '\n');
3914 g_key_file_is_group_name (const gchar *name)
3921 p = q = (gchar *) name;
3922 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3923 q = g_utf8_find_next_char (q, NULL);
3925 if (*q != '\0' || q == p)
3932 g_key_file_is_key_name (const gchar *name)
3939 p = q = (gchar *) name;
3940 /* We accept a little more than the desktop entry spec says,
3941 * since gnome-vfs uses mime-types as keys in its cache.
3943 while (*q && *q != '=' && *q != '[' && *q != ']')
3944 q = g_utf8_find_next_char (q, NULL);
3946 /* No empty keys, please */
3950 /* We accept spaces in the middle of keys to not break
3951 * existing apps, but we don't tolerate initial or final
3952 * spaces, which would lead to silent corruption when
3953 * rereading the file.
3955 if (*p == ' ' || q[-1] == ' ')
3961 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3962 q = g_utf8_find_next_char (q, NULL);
3976 /* A group in a key file is made up of a starting '[' followed by one
3977 * or more letters making up the group name followed by ']'.
3980 g_key_file_line_is_group (const gchar *line)
3990 while (*p && *p != ']')
3991 p = g_utf8_find_next_char (p, NULL);
3996 /* silently accept whitespace after the ] */
3997 p = g_utf8_find_next_char (p, NULL);
3998 while (*p == ' ' || *p == '\t')
3999 p = g_utf8_find_next_char (p, NULL);
4008 g_key_file_line_is_key_value_pair (const gchar *line)
4012 p = (gchar *) g_utf8_strchr (line, -1, '=');
4017 /* Key must be non-empty
4026 g_key_file_parse_value_as_string (GKeyFile *key_file,
4031 gchar *string_value, *p, *q0, *q;
4033 string_value = g_new (gchar, strlen (value) + 1);
4035 p = (gchar *) value;
4036 q0 = q = string_value;
4066 g_set_error_literal (error, G_KEY_FILE_ERROR,
4067 G_KEY_FILE_ERROR_INVALID_VALUE,
4068 _("Key file contains escape character "
4073 if (pieces && *p == key_file->list_separator)
4074 *q = key_file->list_separator;
4088 g_set_error (error, G_KEY_FILE_ERROR,
4089 G_KEY_FILE_ERROR_INVALID_VALUE,
4090 _("Key file contains invalid escape "
4091 "sequence '%s'"), sequence);
4100 if (pieces && (*p == key_file->list_separator))
4102 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4118 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4119 *pieces = g_slist_reverse (*pieces);
4122 return string_value;
4126 g_key_file_parse_string_as_value (GKeyFile *key_file,
4127 const gchar *string,
4128 gboolean escape_separator)
4130 gchar *value, *p, *q;
4132 gboolean parsing_leading_space;
4134 length = strlen (string) + 1;
4136 /* Worst case would be that every character needs to be escaped.
4137 * In other words every character turns to two characters
4139 value = g_new (gchar, 2 * length);
4141 p = (gchar *) string;
4143 parsing_leading_space = TRUE;
4144 while (p < (string + length - 1))
4146 gchar escaped_character[3] = { '\\', 0, 0 };
4151 if (parsing_leading_space)
4153 escaped_character[1] = 's';
4154 strcpy (q, escaped_character);
4164 if (parsing_leading_space)
4166 escaped_character[1] = 't';
4167 strcpy (q, escaped_character);
4177 escaped_character[1] = 'n';
4178 strcpy (q, escaped_character);
4182 escaped_character[1] = 'r';
4183 strcpy (q, escaped_character);
4187 escaped_character[1] = '\\';
4188 strcpy (q, escaped_character);
4190 parsing_leading_space = FALSE;
4193 if (escape_separator && *p == key_file->list_separator)
4195 escaped_character[1] = key_file->list_separator;
4196 strcpy (q, escaped_character);
4198 parsing_leading_space = TRUE;
4204 parsing_leading_space = FALSE;
4216 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4225 long_value = strtol (value, &eof_int, 10);
4227 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4229 gchar *value_utf8 = _g_utf8_make_valid (value);
4230 g_set_error (error, G_KEY_FILE_ERROR,
4231 G_KEY_FILE_ERROR_INVALID_VALUE,
4232 _("Value '%s' cannot be interpreted "
4233 "as a number."), value_utf8);
4234 g_free (value_utf8);
4239 int_value = long_value;
4240 if (int_value != long_value || errno == ERANGE)
4242 gchar *value_utf8 = _g_utf8_make_valid (value);
4245 G_KEY_FILE_ERROR_INVALID_VALUE,
4246 _("Integer value '%s' out of range"),
4248 g_free (value_utf8);
4257 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4261 return g_strdup_printf ("%d", value);
4265 g_key_file_parse_value_as_double (GKeyFile *key_file,
4269 gchar *end_of_valid_d;
4270 gdouble double_value = 0;
4272 double_value = g_ascii_strtod (value, &end_of_valid_d);
4274 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
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 float number."),
4282 g_free (value_utf8);
4285 return double_value;
4289 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4295 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4297 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4300 value_utf8 = _g_utf8_make_valid (value);
4301 g_set_error (error, G_KEY_FILE_ERROR,
4302 G_KEY_FILE_ERROR_INVALID_VALUE,
4303 _("Value '%s' cannot be interpreted "
4304 "as a boolean."), value_utf8);
4305 g_free (value_utf8);
4311 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4315 return g_strdup ("true");
4317 return g_strdup ("false");
4321 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4328 string = g_string_sized_new (512);
4330 lines = g_strsplit (value, "\n", 0);
4332 for (i = 0; lines[i] != NULL; i++)
4334 if (lines[i][0] != '#')
4335 g_string_append_printf (string, "%s\n", lines[i]);
4337 g_string_append_printf (string, "%s\n", lines[i] + 1);
4341 return g_string_free (string, FALSE);
4345 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4346 const gchar *comment)
4352 string = g_string_sized_new (512);
4354 lines = g_strsplit (comment, "\n", 0);
4356 for (i = 0; lines[i] != NULL; i++)
4357 g_string_append_printf (string, "#%s%s", lines[i],
4358 lines[i + 1] == NULL? "" : "\n");
4361 return g_string_free (string, FALSE);