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"
70 typedef struct _GKeyFileGroup GKeyFileGroup;
75 GHashTable *group_hash;
77 GKeyFileGroup *start_group;
78 GKeyFileGroup *current_group;
80 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
82 /* Used for sizing the output buffer during serialization
84 gsize approximate_size;
93 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
97 const gchar *name; /* NULL for above first group (which will be comments) */
99 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
100 gboolean has_trailing_blank_line;
102 GList *key_value_pairs;
104 /* Used in parallel with key_value_pairs for
105 * increased lookup performance
107 GHashTable *lookup_map;
110 struct _GKeyFileKeyValuePair
112 gchar *key; /* NULL for comments */
116 static gint find_file_in_data_dirs (const gchar *file,
117 const gchar **data_dirs,
120 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
124 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
125 const gchar *group_name);
126 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
127 const gchar *group_name);
129 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
130 GKeyFileGroup *group,
132 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
133 GKeyFileGroup *group,
136 static void g_key_file_remove_group_node (GKeyFile *key_file,
138 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
139 GKeyFileGroup *group,
142 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
143 GKeyFileGroup *group,
144 GKeyFileKeyValuePair *pair);
145 static void g_key_file_add_key (GKeyFile *key_file,
146 GKeyFileGroup *group,
149 static void g_key_file_add_group (GKeyFile *key_file,
150 const gchar *group_name);
151 static gboolean g_key_file_is_group_name (const gchar *name);
152 static gboolean g_key_file_is_key_name (const gchar *name);
153 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
154 static gboolean g_key_file_line_is_comment (const gchar *line);
155 static gboolean g_key_file_line_is_group (const gchar *line);
156 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
157 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
161 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
163 gboolean escape_separator);
164 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
167 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
169 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
172 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
175 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
177 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
179 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
180 const gchar *comment);
181 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
185 static void g_key_file_parse_comment (GKeyFile *key_file,
189 static void g_key_file_parse_group (GKeyFile *key_file,
193 static gchar *key_get_locale (const gchar *key);
194 static void g_key_file_parse_data (GKeyFile *key_file,
198 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
203 g_key_file_error_quark (void)
205 return g_quark_from_static_string ("g-key-file-error-quark");
209 g_key_file_init (GKeyFile *key_file)
211 key_file->current_group = g_slice_new0 (GKeyFileGroup);
212 key_file->groups = g_list_prepend (NULL, key_file->current_group);
213 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
214 key_file->start_group = NULL;
215 key_file->parse_buffer = g_string_sized_new (128);
216 key_file->approximate_size = 0;
217 key_file->list_separator = ';';
219 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
223 g_key_file_clear (GKeyFile *key_file)
225 GList *tmp, *group_node;
227 if (key_file->locales)
229 g_strfreev (key_file->locales);
230 key_file->locales = NULL;
233 if (key_file->parse_buffer)
235 g_string_free (key_file->parse_buffer, TRUE);
236 key_file->parse_buffer = NULL;
239 tmp = key_file->groups;
244 g_key_file_remove_group_node (key_file, group_node);
247 g_hash_table_destroy (key_file->group_hash);
248 key_file->group_hash = NULL;
250 g_warn_if_fail (key_file->groups == NULL);
257 * Creates a new empty #GKeyFile object. Use
258 * g_key_file_load_from_file(), g_key_file_load_from_data(),
259 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
260 * read an existing key file.
262 * Return value: an empty #GKeyFile.
267 g_key_file_new (void)
271 key_file = g_slice_new0 (GKeyFile);
272 g_key_file_init (key_file);
278 * g_key_file_set_list_separator:
279 * @key_file: a #GKeyFile
280 * @separator: the separator
282 * Sets the character which is used to separate
283 * values in lists. Typically ';' or ',' are used
284 * as separators. The default list separator is ';'.
289 g_key_file_set_list_separator (GKeyFile *key_file,
292 g_return_if_fail (key_file != NULL);
294 key_file->list_separator = separator;
298 /* Iterates through all the directories in *dirs trying to
299 * open file. When it successfully locates and opens a file it
300 * returns the file descriptor to the open file. It also
301 * outputs the absolute path of the file in output_file.
304 find_file_in_data_dirs (const gchar *file,
309 const gchar **data_dirs, *data_dir;
321 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
323 gchar *candidate_file, *sub_dir;
325 candidate_file = (gchar *) file;
326 sub_dir = g_strdup ("");
327 while (candidate_file != NULL && fd == -1)
331 path = g_build_filename (data_dir, sub_dir,
332 candidate_file, NULL);
334 fd = g_open (path, O_RDONLY, 0);
342 candidate_file = strchr (candidate_file, '-');
344 if (candidate_file == NULL)
350 sub_dir = g_strndup (file, candidate_file - file - 1);
352 for (p = sub_dir; *p != '\0'; p++)
355 *p = G_DIR_SEPARATOR;
364 g_set_error_literal (error, G_KEY_FILE_ERROR,
365 G_KEY_FILE_ERROR_NOT_FOUND,
366 _("Valid key file could not be "
367 "found in search dirs"));
370 if (output_file != NULL && fd > 0)
371 *output_file = g_strdup (path);
379 g_key_file_load_from_fd (GKeyFile *key_file,
384 GError *key_file_error = NULL;
386 struct stat stat_buf;
387 gchar read_buf[4096];
389 if (fstat (fd, &stat_buf) < 0)
391 g_set_error_literal (error, G_FILE_ERROR,
392 g_file_error_from_errno (errno),
397 if (!S_ISREG (stat_buf.st_mode))
399 g_set_error_literal (error, G_KEY_FILE_ERROR,
400 G_KEY_FILE_ERROR_PARSE,
401 _("Not a regular file"));
405 if (stat_buf.st_size == 0)
407 g_set_error_literal (error, G_KEY_FILE_ERROR,
408 G_KEY_FILE_ERROR_PARSE,
413 if (key_file->approximate_size > 0)
415 g_key_file_clear (key_file);
416 g_key_file_init (key_file);
418 key_file->flags = flags;
422 bytes_read = read (fd, read_buf, 4096);
424 if (bytes_read == 0) /* End of File */
429 if (errno == EINTR || errno == EAGAIN)
432 g_set_error_literal (error, G_FILE_ERROR,
433 g_file_error_from_errno (errno),
438 g_key_file_parse_data (key_file,
439 read_buf, bytes_read,
442 while (!key_file_error);
446 g_propagate_error (error, key_file_error);
450 g_key_file_flush_parse_buffer (key_file, &key_file_error);
454 g_propagate_error (error, key_file_error);
462 * g_key_file_load_from_file:
463 * @key_file: an empty #GKeyFile struct
464 * @file: the path of a filename to load, in the GLib filename encoding
465 * @flags: flags from #GKeyFileFlags
466 * @error: return location for a #GError, or %NULL
468 * Loads a key file into an empty #GKeyFile structure.
469 * If the file could not be loaded then %error is set to
470 * either a #GFileError or #GKeyFileError.
472 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
477 g_key_file_load_from_file (GKeyFile *key_file,
482 GError *key_file_error = NULL;
485 g_return_val_if_fail (key_file != NULL, FALSE);
486 g_return_val_if_fail (file != NULL, FALSE);
488 fd = g_open (file, O_RDONLY, 0);
492 g_set_error_literal (error, G_FILE_ERROR,
493 g_file_error_from_errno (errno),
498 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
503 g_propagate_error (error, key_file_error);
511 * g_key_file_load_from_data:
512 * @key_file: an empty #GKeyFile struct
513 * @data: key file loaded in memory
514 * @length: the length of @data in bytes
515 * @flags: flags from #GKeyFileFlags
516 * @error: return location for a #GError, or %NULL
518 * Loads a key file from memory into an empty #GKeyFile structure.
519 * If the object cannot be created then %error is set to a #GKeyFileError.
521 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
526 g_key_file_load_from_data (GKeyFile *key_file,
532 GError *key_file_error = NULL;
534 g_return_val_if_fail (key_file != NULL, FALSE);
535 g_return_val_if_fail (data != NULL, FALSE);
536 g_return_val_if_fail (length != 0, FALSE);
538 if (length == (gsize)-1)
539 length = strlen (data);
541 if (key_file->approximate_size > 0)
543 g_key_file_clear (key_file);
544 g_key_file_init (key_file);
546 key_file->flags = flags;
548 g_key_file_parse_data (key_file, data, length, &key_file_error);
552 g_propagate_error (error, key_file_error);
556 g_key_file_flush_parse_buffer (key_file, &key_file_error);
560 g_propagate_error (error, key_file_error);
568 * g_key_file_load_from_dirs:
569 * @key_file: an empty #GKeyFile struct
570 * @file: a relative path to a filename to open and parse
571 * @search_dirs: %NULL-terminated array of directories to search
572 * @full_path: return location for a string containing the full path
573 * of the file, or %NULL
574 * @flags: flags from #GKeyFileFlags
575 * @error: return location for a #GError, or %NULL
577 * This function looks for a key file named @file in the paths
578 * specified in @search_dirs, loads the file into @key_file and
579 * returns the file's full path in @full_path. If the file could not
580 * be loaded then an %error is set to either a #GFileError or
583 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
588 g_key_file_load_from_dirs (GKeyFile *key_file,
590 const gchar **search_dirs,
595 GError *key_file_error = NULL;
596 const gchar **data_dirs;
601 g_return_val_if_fail (key_file != NULL, FALSE);
602 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
603 g_return_val_if_fail (search_dirs != NULL, FALSE);
606 data_dirs = search_dirs;
608 while (*data_dirs != NULL && !found_file)
610 g_free (output_path);
612 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
618 g_propagate_error (error, key_file_error);
622 found_file = g_key_file_load_from_fd (key_file, fd, flags,
628 g_propagate_error (error, key_file_error);
633 if (found_file && full_path)
634 *full_path = output_path;
636 g_free (output_path);
642 * g_key_file_load_from_data_dirs:
643 * @key_file: an empty #GKeyFile struct
644 * @file: a relative path to a filename to open and parse
645 * @full_path: return location for a string containing the full path
646 * of the file, or %NULL
647 * @flags: flags from #GKeyFileFlags
648 * @error: return location for a #GError, or %NULL
650 * This function looks for a key file named @file in the paths
651 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
652 * loads the file into @key_file and returns the file's full path in
653 * @full_path. If the file could not be loaded then an %error is
654 * set to either a #GFileError or #GKeyFileError.
656 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
660 g_key_file_load_from_data_dirs (GKeyFile *key_file,
666 gchar **all_data_dirs;
667 const gchar * user_data_dir;
668 const gchar * const * system_data_dirs;
672 g_return_val_if_fail (key_file != NULL, FALSE);
673 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
675 user_data_dir = g_get_user_data_dir ();
676 system_data_dirs = g_get_system_data_dirs ();
677 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
680 all_data_dirs[i++] = g_strdup (user_data_dir);
683 while (system_data_dirs[j] != NULL)
684 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
685 all_data_dirs[i] = NULL;
687 found_file = g_key_file_load_from_dirs (key_file,
689 (const gchar **)all_data_dirs,
694 g_strfreev (all_data_dirs);
701 * @key_file: a #GKeyFile
708 g_key_file_free (GKeyFile *key_file)
710 g_return_if_fail (key_file != NULL);
712 g_key_file_clear (key_file);
713 g_slice_free (GKeyFile, key_file);
716 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
717 * true for locales that match those in g_get_language_names().
720 g_key_file_locale_is_interesting (GKeyFile *key_file,
725 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
728 for (i = 0; key_file->locales[i] != NULL; i++)
730 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
738 g_key_file_parse_line (GKeyFile *key_file,
743 GError *parse_error = NULL;
746 g_return_if_fail (key_file != NULL);
747 g_return_if_fail (line != NULL);
749 line_start = (gchar *) line;
750 while (g_ascii_isspace (*line_start))
753 if (g_key_file_line_is_comment (line_start))
754 g_key_file_parse_comment (key_file, line, length, &parse_error);
755 else if (g_key_file_line_is_group (line_start))
756 g_key_file_parse_group (key_file, line_start,
757 length - (line_start - line),
759 else if (g_key_file_line_is_key_value_pair (line_start))
760 g_key_file_parse_key_value_pair (key_file, line_start,
761 length - (line_start - line),
765 gchar *line_utf8 = _g_utf8_make_valid (line);
766 g_set_error (error, G_KEY_FILE_ERROR,
767 G_KEY_FILE_ERROR_PARSE,
768 _("Key file contains line '%s' which is not "
769 "a key-value pair, group, or comment"),
777 g_propagate_error (error, parse_error);
781 g_key_file_parse_comment (GKeyFile *key_file,
786 GKeyFileKeyValuePair *pair;
788 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
791 g_warn_if_fail (key_file->current_group != NULL);
793 pair = g_slice_new (GKeyFileKeyValuePair);
795 pair->value = g_strndup (line, length);
797 key_file->current_group->key_value_pairs =
798 g_list_prepend (key_file->current_group->key_value_pairs, pair);
800 if (length == 0 || line[0] != '#')
801 key_file->current_group->has_trailing_blank_line = TRUE;
805 g_key_file_parse_group (GKeyFile *key_file,
811 const gchar *group_name_start, *group_name_end;
813 /* advance past opening '['
815 group_name_start = line + 1;
816 group_name_end = line + length - 1;
818 while (*group_name_end != ']')
821 group_name = g_strndup (group_name_start,
822 group_name_end - group_name_start);
824 if (!g_key_file_is_group_name (group_name))
826 g_set_error (error, G_KEY_FILE_ERROR,
827 G_KEY_FILE_ERROR_PARSE,
828 _("Invalid group name: %s"), group_name);
833 g_key_file_add_group (key_file, group_name);
838 g_key_file_parse_key_value_pair (GKeyFile *key_file,
843 gchar *key, *value, *key_end, *value_start, *locale;
844 gsize key_len, value_len;
846 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
848 g_set_error_literal (error, G_KEY_FILE_ERROR,
849 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
850 _("Key file does not start with a group"));
854 key_end = value_start = strchr (line, '=');
856 g_warn_if_fail (key_end != NULL);
861 /* Pull the key name from the line (chomping trailing whitespace)
863 while (g_ascii_isspace (*key_end))
866 key_len = key_end - line + 2;
868 g_warn_if_fail (key_len <= length);
870 key = g_strndup (line, key_len - 1);
872 if (!g_key_file_is_key_name (key))
874 g_set_error (error, G_KEY_FILE_ERROR,
875 G_KEY_FILE_ERROR_PARSE,
876 _("Invalid key name: %s"), key);
881 /* Pull the value from the line (chugging leading whitespace)
883 while (g_ascii_isspace (*value_start))
886 value_len = line + length - value_start + 1;
888 value = g_strndup (value_start, value_len);
890 g_warn_if_fail (key_file->start_group != NULL);
892 if (key_file->current_group
893 && key_file->current_group->name
894 && strcmp (key_file->start_group->name,
895 key_file->current_group->name) == 0
896 && strcmp (key, "Encoding") == 0)
898 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
900 gchar *value_utf8 = _g_utf8_make_valid (value);
901 g_set_error (error, G_KEY_FILE_ERROR,
902 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
903 _("Key file contains unsupported "
904 "encoding '%s'"), value_utf8);
913 /* Is this key a translation? If so, is it one that we care about?
915 locale = key_get_locale (key);
917 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
919 GKeyFileKeyValuePair *pair;
921 pair = g_slice_new (GKeyFileKeyValuePair);
925 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
937 key_get_locale (const gchar *key)
941 locale = g_strrstr (key, "[");
943 if (locale && strlen (locale) <= 2)
947 locale = g_strndup (locale + 1, strlen (locale) - 2);
953 g_key_file_parse_data (GKeyFile *key_file,
961 g_return_if_fail (key_file != NULL);
962 g_return_if_fail (data != NULL);
971 if (key_file->parse_buffer->len > 0
972 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
974 g_string_erase (key_file->parse_buffer,
975 key_file->parse_buffer->len - 1,
978 /* When a newline is encountered flush the parse buffer so that the
979 * line can be parsed. Note that completely blank lines won't show
980 * up in the parse buffer, so they get parsed directly.
982 if (key_file->parse_buffer->len > 0)
983 g_key_file_flush_parse_buffer (key_file, &parse_error);
985 g_key_file_parse_comment (key_file, "", 1, &parse_error);
989 g_propagate_error (error, parse_error);
996 const gchar *start_of_line;
997 const gchar *end_of_line;
1000 start_of_line = data + i;
1001 end_of_line = memchr (start_of_line, '\n', length - i);
1003 if (end_of_line == NULL)
1004 end_of_line = data + length;
1006 line_length = end_of_line - start_of_line;
1008 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1013 key_file->approximate_size += length;
1017 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1020 GError *file_error = NULL;
1022 g_return_if_fail (key_file != NULL);
1026 if (key_file->parse_buffer->len > 0)
1028 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1029 key_file->parse_buffer->len,
1031 g_string_erase (key_file->parse_buffer, 0, -1);
1035 g_propagate_error (error, file_error);
1042 * g_key_file_to_data:
1043 * @key_file: a #GKeyFile
1044 * @length: return location for the length of the
1045 * returned string, or %NULL
1046 * @error: return location for a #GError, or %NULL
1048 * This function outputs @key_file as a string.
1050 * Note that this function never reports an error,
1051 * so it is safe to pass %NULL as @error.
1053 * Return value: a newly allocated string holding
1054 * the contents of the #GKeyFile
1059 g_key_file_to_data (GKeyFile *key_file,
1063 GString *data_string;
1064 GList *group_node, *key_file_node;
1065 gboolean has_blank_line = TRUE;
1067 g_return_val_if_fail (key_file != NULL, NULL);
1069 data_string = g_string_sized_new (2 * key_file->approximate_size);
1071 for (group_node = g_list_last (key_file->groups);
1073 group_node = group_node->prev)
1075 GKeyFileGroup *group;
1077 group = (GKeyFileGroup *) group_node->data;
1079 /* separate groups by at least an empty line */
1080 if (!has_blank_line)
1081 g_string_append_c (data_string, '\n');
1082 has_blank_line = group->has_trailing_blank_line;
1084 if (group->comment != NULL)
1085 g_string_append_printf (data_string, "%s\n", group->comment->value);
1087 if (group->name != NULL)
1088 g_string_append_printf (data_string, "[%s]\n", group->name);
1090 for (key_file_node = g_list_last (group->key_value_pairs);
1091 key_file_node != NULL;
1092 key_file_node = key_file_node->prev)
1094 GKeyFileKeyValuePair *pair;
1096 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1098 if (pair->key != NULL)
1099 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1101 g_string_append_printf (data_string, "%s\n", pair->value);
1106 *length = data_string->len;
1108 return g_string_free (data_string, FALSE);
1112 * g_key_file_get_keys:
1113 * @key_file: a #GKeyFile
1114 * @group_name: a group name
1115 * @length: return location for the number of keys returned, or %NULL
1116 * @error: return location for a #GError, or %NULL
1118 * Returns all keys for the group name @group_name. The array of
1119 * returned keys will be %NULL-terminated, so @length may
1120 * optionally be %NULL. In the event that the @group_name cannot
1121 * be found, %NULL is returned and @error is set to
1122 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1124 * Return value: a newly-allocated %NULL-terminated array of strings.
1125 * Use g_strfreev() to free it.
1130 g_key_file_get_keys (GKeyFile *key_file,
1131 const gchar *group_name,
1135 GKeyFileGroup *group;
1140 g_return_val_if_fail (key_file != NULL, NULL);
1141 g_return_val_if_fail (group_name != NULL, NULL);
1143 group = g_key_file_lookup_group (key_file, group_name);
1147 g_set_error (error, G_KEY_FILE_ERROR,
1148 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1149 _("Key file does not have group '%s'"),
1150 group_name ? group_name : "(null)");
1155 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1157 GKeyFileKeyValuePair *pair;
1159 pair = (GKeyFileKeyValuePair *) tmp->data;
1165 keys = g_new (gchar *, num_keys + 1);
1168 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1170 GKeyFileKeyValuePair *pair;
1172 pair = (GKeyFileKeyValuePair *) tmp->data;
1176 keys[i] = g_strdup (pair->key);
1181 keys[num_keys] = NULL;
1190 * g_key_file_get_start_group:
1191 * @key_file: a #GKeyFile
1193 * Returns the name of the start group of the file.
1195 * Return value: The start group of the key file.
1200 g_key_file_get_start_group (GKeyFile *key_file)
1202 g_return_val_if_fail (key_file != NULL, NULL);
1204 if (key_file->start_group)
1205 return g_strdup (key_file->start_group->name);
1211 * g_key_file_get_groups:
1212 * @key_file: a #GKeyFile
1213 * @length: return location for the number of returned groups, or %NULL
1215 * Returns all groups in the key file loaded with @key_file.
1216 * The array of returned groups will be %NULL-terminated, so
1217 * @length may optionally be %NULL.
1219 * Return value: a newly-allocated %NULL-terminated array of strings.
1220 * Use g_strfreev() to free it.
1224 g_key_file_get_groups (GKeyFile *key_file,
1229 gsize i, num_groups;
1231 g_return_val_if_fail (key_file != NULL, NULL);
1233 num_groups = g_list_length (key_file->groups);
1235 g_return_val_if_fail (num_groups > 0, NULL);
1237 group_node = g_list_last (key_file->groups);
1239 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1241 /* Only need num_groups instead of num_groups + 1
1242 * because the first group of the file (last in the
1243 * list) is always the comment group at the top,
1246 groups = g_new (gchar *, num_groups);
1250 for (group_node = group_node->prev;
1252 group_node = group_node->prev)
1254 GKeyFileGroup *group;
1256 group = (GKeyFileGroup *) group_node->data;
1258 g_warn_if_fail (group->name != NULL);
1260 groups[i++] = g_strdup (group->name);
1271 * g_key_file_get_value:
1272 * @key_file: a #GKeyFile
1273 * @group_name: a group name
1275 * @error: return location for a #GError, or %NULL
1277 * Returns the raw value associated with @key under @group_name.
1278 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1280 * In the event the key cannot be found, %NULL is returned and
1281 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1282 * event that the @group_name cannot be found, %NULL is returned
1283 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1286 * Return value: a newly allocated string or %NULL if the specified
1287 * key cannot be found.
1292 g_key_file_get_value (GKeyFile *key_file,
1293 const gchar *group_name,
1297 GKeyFileGroup *group;
1298 GKeyFileKeyValuePair *pair;
1299 gchar *value = NULL;
1301 g_return_val_if_fail (key_file != NULL, NULL);
1302 g_return_val_if_fail (group_name != NULL, NULL);
1303 g_return_val_if_fail (key != NULL, NULL);
1305 group = g_key_file_lookup_group (key_file, group_name);
1309 g_set_error (error, G_KEY_FILE_ERROR,
1310 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1311 _("Key file does not have group '%s'"),
1312 group_name ? group_name : "(null)");
1316 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1319 value = g_strdup (pair->value);
1321 g_set_error (error, G_KEY_FILE_ERROR,
1322 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1323 _("Key file does not have key '%s'"), key);
1329 * g_key_file_set_value:
1330 * @key_file: a #GKeyFile
1331 * @group_name: a group name
1335 * Associates a new value with @key under @group_name.
1337 * If @key cannot be found then it is created. If @group_name cannot
1338 * be found then it is created. To set an UTF-8 string which may contain
1339 * characters that need escaping (such as newlines or spaces), use
1340 * g_key_file_set_string().
1345 g_key_file_set_value (GKeyFile *key_file,
1346 const gchar *group_name,
1350 GKeyFileGroup *group;
1351 GKeyFileKeyValuePair *pair;
1353 g_return_if_fail (key_file != NULL);
1354 g_return_if_fail (g_key_file_is_group_name (group_name));
1355 g_return_if_fail (g_key_file_is_key_name (key));
1356 g_return_if_fail (value != NULL);
1358 group = g_key_file_lookup_group (key_file, group_name);
1362 g_key_file_add_group (key_file, group_name);
1363 group = (GKeyFileGroup *) key_file->groups->data;
1365 g_key_file_add_key (key_file, group, key, value);
1369 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1372 g_key_file_add_key (key_file, group, key, value);
1375 g_free (pair->value);
1376 pair->value = g_strdup (value);
1382 * g_key_file_get_string:
1383 * @key_file: a #GKeyFile
1384 * @group_name: a group name
1386 * @error: return location for a #GError, or %NULL
1388 * Returns the string value associated with @key under @group_name.
1389 * Unlike g_key_file_get_value(), this function handles escape sequences
1392 * In the event the key cannot be found, %NULL is returned and
1393 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1394 * event that the @group_name cannot be found, %NULL is returned
1395 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1397 * Return value: a newly allocated string or %NULL if the specified
1398 * key cannot be found.
1403 g_key_file_get_string (GKeyFile *key_file,
1404 const gchar *group_name,
1408 gchar *value, *string_value;
1409 GError *key_file_error;
1411 g_return_val_if_fail (key_file != NULL, NULL);
1412 g_return_val_if_fail (group_name != NULL, NULL);
1413 g_return_val_if_fail (key != NULL, NULL);
1415 key_file_error = NULL;
1417 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1421 g_propagate_error (error, key_file_error);
1425 if (!g_utf8_validate (value, -1, NULL))
1427 gchar *value_utf8 = _g_utf8_make_valid (value);
1428 g_set_error (error, G_KEY_FILE_ERROR,
1429 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1430 _("Key file contains key '%s' with value '%s' "
1431 "which is not UTF-8"), key, value_utf8);
1432 g_free (value_utf8);
1438 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1444 if (g_error_matches (key_file_error,
1446 G_KEY_FILE_ERROR_INVALID_VALUE))
1448 g_set_error (error, G_KEY_FILE_ERROR,
1449 G_KEY_FILE_ERROR_INVALID_VALUE,
1450 _("Key file contains key '%s' "
1451 "which has value that cannot be interpreted."),
1453 g_error_free (key_file_error);
1456 g_propagate_error (error, key_file_error);
1459 return string_value;
1463 * g_key_file_set_string:
1464 * @key_file: a #GKeyFile
1465 * @group_name: a group name
1469 * Associates a new string value with @key under @group_name.
1470 * If @key cannot be found then it is created.
1471 * If @group_name cannot be found then it is created.
1472 * Unlike g_key_file_set_value(), this function handles characters
1473 * that need escaping, such as newlines.
1478 g_key_file_set_string (GKeyFile *key_file,
1479 const gchar *group_name,
1481 const gchar *string)
1485 g_return_if_fail (key_file != NULL);
1486 g_return_if_fail (string != NULL);
1488 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1489 g_key_file_set_value (key_file, group_name, key, value);
1494 * g_key_file_get_string_list:
1495 * @key_file: a #GKeyFile
1496 * @group_name: a group name
1498 * @length: return location for the number of returned strings, or %NULL
1499 * @error: return location for a #GError, or %NULL
1501 * Returns the values associated with @key under @group_name.
1503 * In the event the key cannot be found, %NULL is returned and
1504 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1505 * event that the @group_name cannot be found, %NULL is returned
1506 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1508 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1509 * a %NULL-terminated string array or %NULL if the specified
1510 * key cannot be found. The array should be freed with g_strfreev().
1515 g_key_file_get_string_list (GKeyFile *key_file,
1516 const gchar *group_name,
1521 GError *key_file_error = NULL;
1522 gchar *value, *string_value, **values;
1524 GSList *p, *pieces = NULL;
1526 g_return_val_if_fail (key_file != NULL, NULL);
1527 g_return_val_if_fail (group_name != NULL, NULL);
1528 g_return_val_if_fail (key != NULL, NULL);
1533 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1537 g_propagate_error (error, key_file_error);
1541 if (!g_utf8_validate (value, -1, NULL))
1543 gchar *value_utf8 = _g_utf8_make_valid (value);
1544 g_set_error (error, G_KEY_FILE_ERROR,
1545 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1546 _("Key file contains key '%s' with value '%s' "
1547 "which is not UTF-8"), key, value_utf8);
1548 g_free (value_utf8);
1554 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1556 g_free (string_value);
1560 if (g_error_matches (key_file_error,
1562 G_KEY_FILE_ERROR_INVALID_VALUE))
1564 g_set_error (error, G_KEY_FILE_ERROR,
1565 G_KEY_FILE_ERROR_INVALID_VALUE,
1566 _("Key file contains key '%s' "
1567 "which has a value that cannot be interpreted."),
1569 g_error_free (key_file_error);
1572 g_propagate_error (error, key_file_error);
1577 len = g_slist_length (pieces);
1578 values = g_new (gchar *, len + 1);
1579 for (p = pieces, i = 0; p; p = p->next)
1580 values[i++] = p->data;
1583 g_slist_free (pieces);
1592 * g_key_file_set_string_list:
1593 * @key_file: a #GKeyFile
1594 * @group_name: a group name
1596 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1597 * @length: number of string values in @list
1599 * Associates a list of string values for @key under @group_name.
1600 * If @key cannot be found then it is created.
1601 * If @group_name cannot be found then it is created.
1606 g_key_file_set_string_list (GKeyFile *key_file,
1607 const gchar *group_name,
1609 const gchar * const list[],
1612 GString *value_list;
1615 g_return_if_fail (key_file != NULL);
1616 g_return_if_fail (list != NULL || length == 0);
1618 value_list = g_string_sized_new (length * 128);
1619 for (i = 0; i < length && list[i] != NULL; i++)
1623 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1624 g_string_append (value_list, value);
1625 g_string_append_c (value_list, key_file->list_separator);
1630 g_key_file_set_value (key_file, group_name, key, value_list->str);
1631 g_string_free (value_list, TRUE);
1635 * g_key_file_set_locale_string:
1636 * @key_file: a #GKeyFile
1637 * @group_name: a group name
1639 * @locale: a locale identifier
1642 * Associates a string value for @key and @locale under @group_name.
1643 * If the translation for @key cannot be found then it is created.
1648 g_key_file_set_locale_string (GKeyFile *key_file,
1649 const gchar *group_name,
1651 const gchar *locale,
1652 const gchar *string)
1654 gchar *full_key, *value;
1656 g_return_if_fail (key_file != NULL);
1657 g_return_if_fail (key != NULL);
1658 g_return_if_fail (locale != NULL);
1659 g_return_if_fail (string != NULL);
1661 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1662 full_key = g_strdup_printf ("%s[%s]", key, locale);
1663 g_key_file_set_value (key_file, group_name, full_key, value);
1669 * g_key_file_get_locale_string:
1670 * @key_file: a #GKeyFile
1671 * @group_name: a group name
1673 * @locale: (allow-none): a locale identifier or %NULL
1674 * @error: return location for a #GError, or %NULL
1676 * Returns the value associated with @key under @group_name
1677 * translated in the given @locale if available. If @locale is
1678 * %NULL then the current locale is assumed.
1680 * If @key cannot be found then %NULL is returned and @error is set
1681 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1682 * with @key cannot be interpreted or no suitable translation can
1683 * be found then the untranslated value is returned.
1685 * Return value: a newly allocated string or %NULL if the specified
1686 * key cannot be found.
1691 g_key_file_get_locale_string (GKeyFile *key_file,
1692 const gchar *group_name,
1694 const gchar *locale,
1697 gchar *candidate_key, *translated_value;
1698 GError *key_file_error;
1700 gboolean free_languages = FALSE;
1703 g_return_val_if_fail (key_file != NULL, NULL);
1704 g_return_val_if_fail (group_name != NULL, NULL);
1705 g_return_val_if_fail (key != NULL, NULL);
1707 candidate_key = NULL;
1708 translated_value = NULL;
1709 key_file_error = NULL;
1713 languages = g_get_locale_variants (locale);
1714 free_languages = TRUE;
1718 languages = (gchar **) g_get_language_names ();
1719 free_languages = FALSE;
1722 for (i = 0; languages[i]; i++)
1724 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1726 translated_value = g_key_file_get_string (key_file,
1728 candidate_key, NULL);
1729 g_free (candidate_key);
1731 if (translated_value)
1734 g_free (translated_value);
1735 translated_value = NULL;
1738 /* Fallback to untranslated key
1740 if (!translated_value)
1742 translated_value = g_key_file_get_string (key_file, group_name, key,
1745 if (!translated_value)
1746 g_propagate_error (error, key_file_error);
1750 g_strfreev (languages);
1752 return translated_value;
1756 * g_key_file_get_locale_string_list:
1757 * @key_file: a #GKeyFile
1758 * @group_name: a group name
1760 * @locale: (allow-none): a locale identifier or %NULL
1761 * @length: (out): return location for the number of returned strings or %NULL
1762 * @error: return location for a #GError or %NULL
1764 * Returns the values associated with @key under @group_name
1765 * translated in the given @locale if available. If @locale is
1766 * %NULL then the current locale is assumed.
1768 * If @key cannot be found then %NULL is returned and @error is set
1769 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1770 * with @key cannot be interpreted or no suitable translations
1771 * can be found then the untranslated values are returned. The
1772 * returned array is %NULL-terminated, so @length may optionally
1775 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
1776 * or %NULL if the key isn't found. The string array should be freed
1777 * with g_strfreev().
1782 g_key_file_get_locale_string_list (GKeyFile *key_file,
1783 const gchar *group_name,
1785 const gchar *locale,
1789 GError *key_file_error;
1790 gchar **values, *value;
1791 char list_separator[2];
1794 g_return_val_if_fail (key_file != NULL, NULL);
1795 g_return_val_if_fail (group_name != NULL, NULL);
1796 g_return_val_if_fail (key != NULL, NULL);
1798 key_file_error = NULL;
1800 value = g_key_file_get_locale_string (key_file, group_name,
1805 g_propagate_error (error, key_file_error);
1814 len = strlen (value);
1815 if (value[len - 1] == key_file->list_separator)
1816 value[len - 1] = '\0';
1818 list_separator[0] = key_file->list_separator;
1819 list_separator[1] = '\0';
1820 values = g_strsplit (value, list_separator, 0);
1825 *length = g_strv_length (values);
1831 * g_key_file_set_locale_string_list:
1832 * @key_file: a #GKeyFile
1833 * @group_name: a group name
1835 * @locale: a locale identifier
1836 * @list: a %NULL-terminated array of locale string values
1837 * @length: the length of @list
1839 * Associates a list of string values for @key and @locale under
1840 * @group_name. If the translation for @key cannot be found then
1846 g_key_file_set_locale_string_list (GKeyFile *key_file,
1847 const gchar *group_name,
1849 const gchar *locale,
1850 const gchar * const list[],
1853 GString *value_list;
1857 g_return_if_fail (key_file != NULL);
1858 g_return_if_fail (key != NULL);
1859 g_return_if_fail (locale != NULL);
1860 g_return_if_fail (length != 0);
1862 value_list = g_string_sized_new (length * 128);
1863 for (i = 0; i < length && list[i] != NULL; i++)
1867 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1868 g_string_append (value_list, value);
1869 g_string_append_c (value_list, key_file->list_separator);
1874 full_key = g_strdup_printf ("%s[%s]", key, locale);
1875 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1877 g_string_free (value_list, TRUE);
1881 * g_key_file_get_boolean:
1882 * @key_file: a #GKeyFile
1883 * @group_name: a group name
1885 * @error: return location for a #GError
1887 * Returns the value associated with @key under @group_name as a
1890 * If @key cannot be found then %FALSE is returned and @error is set
1891 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1892 * associated with @key cannot be interpreted as a boolean then %FALSE
1893 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1895 * Return value: the value associated with the key as a boolean,
1896 * or %FALSE if the key was not found or could not be parsed.
1901 g_key_file_get_boolean (GKeyFile *key_file,
1902 const gchar *group_name,
1906 GError *key_file_error = NULL;
1908 gboolean bool_value;
1910 g_return_val_if_fail (key_file != NULL, FALSE);
1911 g_return_val_if_fail (group_name != NULL, FALSE);
1912 g_return_val_if_fail (key != NULL, FALSE);
1914 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1918 g_propagate_error (error, key_file_error);
1922 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1928 if (g_error_matches (key_file_error,
1930 G_KEY_FILE_ERROR_INVALID_VALUE))
1932 g_set_error (error, G_KEY_FILE_ERROR,
1933 G_KEY_FILE_ERROR_INVALID_VALUE,
1934 _("Key file contains key '%s' "
1935 "which has value that cannot be interpreted."),
1937 g_error_free (key_file_error);
1940 g_propagate_error (error, key_file_error);
1947 * g_key_file_set_boolean:
1948 * @key_file: a #GKeyFile
1949 * @group_name: a group name
1951 * @value: %TRUE or %FALSE
1953 * Associates a new boolean value with @key under @group_name.
1954 * If @key cannot be found then it is created.
1959 g_key_file_set_boolean (GKeyFile *key_file,
1960 const gchar *group_name,
1966 g_return_if_fail (key_file != NULL);
1968 result = g_key_file_parse_boolean_as_value (key_file, value);
1969 g_key_file_set_value (key_file, group_name, key, result);
1974 * g_key_file_get_boolean_list:
1975 * @key_file: a #GKeyFile
1976 * @group_name: a group name
1978 * @length: the number of booleans returned
1979 * @error: return location for a #GError
1981 * Returns the values associated with @key under @group_name as
1984 * If @key cannot be found then %NULL is returned and @error is set to
1985 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1986 * with @key cannot be interpreted as booleans then %NULL is returned
1987 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1989 * Return value: (array length=length) (element-type gboolean) (transfer container):
1990 * the values associated with the key as a list of booleans, or %NULL if the
1991 * key was not found or could not be parsed. The returned list of booleans
1992 * should be freed with g_free() when no longer needed.
1997 g_key_file_get_boolean_list (GKeyFile *key_file,
1998 const gchar *group_name,
2003 GError *key_file_error;
2005 gboolean *bool_values;
2008 g_return_val_if_fail (key_file != NULL, NULL);
2009 g_return_val_if_fail (group_name != NULL, NULL);
2010 g_return_val_if_fail (key != NULL, NULL);
2015 key_file_error = NULL;
2017 values = g_key_file_get_string_list (key_file, group_name, key,
2018 &num_bools, &key_file_error);
2021 g_propagate_error (error, key_file_error);
2026 bool_values = g_new (gboolean, num_bools);
2028 for (i = 0; i < num_bools; i++)
2030 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2036 g_propagate_error (error, key_file_error);
2037 g_strfreev (values);
2038 g_free (bool_values);
2043 g_strfreev (values);
2046 *length = num_bools;
2052 * g_key_file_set_boolean_list:
2053 * @key_file: a #GKeyFile
2054 * @group_name: a group name
2056 * @list: an array of boolean values
2057 * @length: length of @list
2059 * Associates a list of boolean values with @key under @group_name.
2060 * If @key cannot be found then it is created.
2061 * If @group_name is %NULL, the start_group is used.
2066 g_key_file_set_boolean_list (GKeyFile *key_file,
2067 const gchar *group_name,
2072 GString *value_list;
2075 g_return_if_fail (key_file != NULL);
2076 g_return_if_fail (list != NULL);
2078 value_list = g_string_sized_new (length * 8);
2079 for (i = 0; i < length; i++)
2083 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2085 g_string_append (value_list, value);
2086 g_string_append_c (value_list, key_file->list_separator);
2091 g_key_file_set_value (key_file, group_name, key, value_list->str);
2092 g_string_free (value_list, TRUE);
2096 * g_key_file_get_integer:
2097 * @key_file: a #GKeyFile
2098 * @group_name: a group name
2100 * @error: return location for a #GError
2102 * Returns the value associated with @key under @group_name as an
2105 * If @key cannot be found then 0 is returned and @error is set to
2106 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2107 * with @key cannot be interpreted as an integer then 0 is returned
2108 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2110 * Return value: the value associated with the key as an integer, or
2111 * 0 if the key was not found or could not be parsed.
2116 g_key_file_get_integer (GKeyFile *key_file,
2117 const gchar *group_name,
2121 GError *key_file_error;
2125 g_return_val_if_fail (key_file != NULL, -1);
2126 g_return_val_if_fail (group_name != NULL, -1);
2127 g_return_val_if_fail (key != NULL, -1);
2129 key_file_error = NULL;
2131 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2135 g_propagate_error (error, key_file_error);
2139 int_value = g_key_file_parse_value_as_integer (key_file, value,
2145 if (g_error_matches (key_file_error,
2147 G_KEY_FILE_ERROR_INVALID_VALUE))
2149 g_set_error (error, G_KEY_FILE_ERROR,
2150 G_KEY_FILE_ERROR_INVALID_VALUE,
2151 _("Key file contains key '%s' in group '%s' "
2152 "which has value that cannot be interpreted."), key,
2154 g_error_free (key_file_error);
2157 g_propagate_error (error, key_file_error);
2164 * g_key_file_set_integer:
2165 * @key_file: a #GKeyFile
2166 * @group_name: a group name
2168 * @value: an integer value
2170 * Associates a new integer value with @key under @group_name.
2171 * If @key cannot be found then it is created.
2176 g_key_file_set_integer (GKeyFile *key_file,
2177 const gchar *group_name,
2183 g_return_if_fail (key_file != NULL);
2185 result = g_key_file_parse_integer_as_value (key_file, value);
2186 g_key_file_set_value (key_file, group_name, key, result);
2191 * g_key_file_get_int64:
2192 * @key_file: a non-%NULL #GKeyFile
2193 * @group_name: a non-%NULL group name
2194 * @key: a non-%NULL key
2195 * @error: return location for a #GError
2197 * Returns the value associated with @key under @group_name as a signed
2198 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2199 * 64-bit results without truncation.
2201 * Returns: the value associated with the key as a signed 64-bit integer, or
2202 * 0 if the key was not found or could not be parsed.
2207 g_key_file_get_int64 (GKeyFile *key_file,
2208 const gchar *group_name,
2215 g_return_val_if_fail (key_file != NULL, -1);
2216 g_return_val_if_fail (group_name != NULL, -1);
2217 g_return_val_if_fail (key != NULL, -1);
2219 s = g_key_file_get_value (key_file, group_name, key, error);
2224 v = g_ascii_strtoll (s, &end, 10);
2226 if (*s == '\0' || *end != '\0')
2228 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2229 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2230 key, group_name, s);
2239 * g_key_file_set_int64:
2240 * @key_file: a #GKeyFile
2241 * @group_name: a group name
2243 * @value: an integer value
2245 * Associates a new integer value with @key under @group_name.
2246 * If @key cannot be found then it is created.
2251 g_key_file_set_int64 (GKeyFile *key_file,
2252 const gchar *group_name,
2258 g_return_if_fail (key_file != NULL);
2260 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2261 g_key_file_set_value (key_file, group_name, key, result);
2266 * g_key_file_get_uint64:
2267 * @key_file: a non-%NULL #GKeyFile
2268 * @group_name: a non-%NULL group name
2269 * @key: a non-%NULL key
2270 * @error: return location for a #GError
2272 * Returns the value associated with @key under @group_name as an unsigned
2273 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2274 * large positive results without truncation.
2276 * Returns: the value associated with the key as an unsigned 64-bit integer,
2277 * or 0 if the key was not found or could not be parsed.
2282 g_key_file_get_uint64 (GKeyFile *key_file,
2283 const gchar *group_name,
2290 g_return_val_if_fail (key_file != NULL, -1);
2291 g_return_val_if_fail (group_name != NULL, -1);
2292 g_return_val_if_fail (key != NULL, -1);
2294 s = g_key_file_get_value (key_file, group_name, key, error);
2299 v = g_ascii_strtoull (s, &end, 10);
2301 if (*s == '\0' || *end != '\0')
2303 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2304 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2305 key, group_name, s);
2314 * g_key_file_set_uint64:
2315 * @key_file: a #GKeyFile
2316 * @group_name: a group name
2318 * @value: an integer value
2320 * Associates a new integer value with @key under @group_name.
2321 * If @key cannot be found then it is created.
2326 g_key_file_set_uint64 (GKeyFile *key_file,
2327 const gchar *group_name,
2333 g_return_if_fail (key_file != NULL);
2335 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2336 g_key_file_set_value (key_file, group_name, key, result);
2341 * g_key_file_get_integer_list:
2342 * @key_file: a #GKeyFile
2343 * @group_name: a group name
2345 * @length: the number of integers returned
2346 * @error: return location for a #GError
2348 * Returns the values associated with @key under @group_name as
2351 * If @key cannot be found then %NULL is returned and @error is set to
2352 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2353 * with @key cannot be interpreted as integers then %NULL is returned
2354 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2356 * Return value: (array length=length) (element-type gint) (transfer container):
2357 * the values associated with the key as a list of integers, or %NULL if
2358 * the key was not found or could not be parsed. The returned list of
2359 * integers should be freed with g_free() when no longer needed.
2364 g_key_file_get_integer_list (GKeyFile *key_file,
2365 const gchar *group_name,
2370 GError *key_file_error = NULL;
2375 g_return_val_if_fail (key_file != NULL, NULL);
2376 g_return_val_if_fail (group_name != NULL, NULL);
2377 g_return_val_if_fail (key != NULL, NULL);
2382 values = g_key_file_get_string_list (key_file, group_name, key,
2383 &num_ints, &key_file_error);
2386 g_propagate_error (error, key_file_error);
2391 int_values = g_new (gint, num_ints);
2393 for (i = 0; i < num_ints; i++)
2395 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2401 g_propagate_error (error, key_file_error);
2402 g_strfreev (values);
2403 g_free (int_values);
2408 g_strfreev (values);
2417 * g_key_file_set_integer_list:
2418 * @key_file: a #GKeyFile
2419 * @group_name: a group name
2421 * @list: an array of integer values
2422 * @length: number of integer values in @list
2424 * Associates a list of integer values with @key under @group_name.
2425 * If @key cannot be found then it is created.
2430 g_key_file_set_integer_list (GKeyFile *key_file,
2431 const gchar *group_name,
2439 g_return_if_fail (key_file != NULL);
2440 g_return_if_fail (list != NULL);
2442 values = g_string_sized_new (length * 16);
2443 for (i = 0; i < length; i++)
2447 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2449 g_string_append (values, value);
2450 g_string_append_c (values, key_file->list_separator);
2455 g_key_file_set_value (key_file, group_name, key, values->str);
2456 g_string_free (values, TRUE);
2460 * g_key_file_get_double:
2461 * @key_file: a #GKeyFile
2462 * @group_name: a group name
2464 * @error: return location for a #GError
2466 * Returns the value associated with @key under @group_name as a
2467 * double. If @group_name is %NULL, the start_group is used.
2469 * If @key cannot be found then 0.0 is returned and @error is set to
2470 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2471 * with @key cannot be interpreted as a double then 0.0 is returned
2472 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2474 * Return value: the value associated with the key as a double, or
2475 * 0.0 if the key was not found or could not be parsed.
2480 g_key_file_get_double (GKeyFile *key_file,
2481 const gchar *group_name,
2485 GError *key_file_error;
2487 gdouble double_value;
2489 g_return_val_if_fail (key_file != NULL, -1);
2490 g_return_val_if_fail (group_name != NULL, -1);
2491 g_return_val_if_fail (key != NULL, -1);
2493 key_file_error = NULL;
2495 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2499 g_propagate_error (error, key_file_error);
2503 double_value = g_key_file_parse_value_as_double (key_file, value,
2509 if (g_error_matches (key_file_error,
2511 G_KEY_FILE_ERROR_INVALID_VALUE))
2513 g_set_error (error, G_KEY_FILE_ERROR,
2514 G_KEY_FILE_ERROR_INVALID_VALUE,
2515 _("Key file contains key '%s' in group '%s' "
2516 "which has value that cannot be interpreted."), key,
2518 g_error_free (key_file_error);
2521 g_propagate_error (error, key_file_error);
2524 return double_value;
2528 * g_key_file_set_double:
2529 * @key_file: a #GKeyFile
2530 * @group_name: a group name
2532 * @value: an double value
2534 * Associates a new double value with @key under @group_name.
2535 * If @key cannot be found then it is created.
2540 g_key_file_set_double (GKeyFile *key_file,
2541 const gchar *group_name,
2545 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2547 g_return_if_fail (key_file != NULL);
2549 g_ascii_dtostr (result, sizeof (result), value);
2550 g_key_file_set_value (key_file, group_name, key, result);
2554 * g_key_file_get_double_list:
2555 * @key_file: a #GKeyFile
2556 * @group_name: a group name
2558 * @length: the number of doubles returned
2559 * @error: return location for a #GError
2561 * Returns the values associated with @key under @group_name as
2564 * If @key cannot be found then %NULL is returned and @error is set to
2565 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2566 * with @key cannot be interpreted as doubles then %NULL is returned
2567 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2569 * Return value: (array length=length) (element-type gdouble) (transfer container):
2570 * the values associated with the key as a list of doubles, or %NULL if the
2571 * key was not found or could not be parsed. The returned list of doubles
2572 * should be freed with g_free() when no longer needed.
2577 g_key_file_get_double_list (GKeyFile *key_file,
2578 const gchar *group_name,
2583 GError *key_file_error = NULL;
2585 gdouble *double_values;
2586 gsize i, num_doubles;
2588 g_return_val_if_fail (key_file != NULL, NULL);
2589 g_return_val_if_fail (group_name != NULL, NULL);
2590 g_return_val_if_fail (key != NULL, NULL);
2595 values = g_key_file_get_string_list (key_file, group_name, key,
2596 &num_doubles, &key_file_error);
2599 g_propagate_error (error, key_file_error);
2604 double_values = g_new (gdouble, num_doubles);
2606 for (i = 0; i < num_doubles; i++)
2608 double_values[i] = g_key_file_parse_value_as_double (key_file,
2614 g_propagate_error (error, key_file_error);
2615 g_strfreev (values);
2616 g_free (double_values);
2621 g_strfreev (values);
2624 *length = num_doubles;
2626 return double_values;
2630 * g_key_file_set_double_list:
2631 * @key_file: a #GKeyFile
2632 * @group_name: a group name
2634 * @list: an array of double values
2635 * @length: number of double values in @list
2637 * Associates a list of double values with @key under
2638 * @group_name. If @key cannot be found then it is created.
2643 g_key_file_set_double_list (GKeyFile *key_file,
2644 const gchar *group_name,
2652 g_return_if_fail (key_file != NULL);
2653 g_return_if_fail (list != NULL);
2655 values = g_string_sized_new (length * 16);
2656 for (i = 0; i < length; i++)
2658 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2660 g_ascii_dtostr( result, sizeof (result), list[i] );
2662 g_string_append (values, result);
2663 g_string_append_c (values, key_file->list_separator);
2666 g_key_file_set_value (key_file, group_name, key, values->str);
2667 g_string_free (values, TRUE);
2671 g_key_file_set_key_comment (GKeyFile *key_file,
2672 const gchar *group_name,
2674 const gchar *comment,
2677 GKeyFileGroup *group;
2678 GKeyFileKeyValuePair *pair;
2679 GList *key_node, *comment_node, *tmp;
2681 group = g_key_file_lookup_group (key_file, group_name);
2684 g_set_error (error, G_KEY_FILE_ERROR,
2685 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2686 _("Key file does not have group '%s'"),
2687 group_name ? group_name : "(null)");
2692 /* First find the key the comments are supposed to be
2695 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2697 if (key_node == NULL)
2699 g_set_error (error, G_KEY_FILE_ERROR,
2700 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2701 _("Key file does not have key '%s' in group '%s'"),
2706 /* Then find all the comments already associated with the
2709 tmp = key_node->next;
2712 pair = (GKeyFileKeyValuePair *) tmp->data;
2714 if (pair->key != NULL)
2719 g_key_file_remove_key_value_pair_node (key_file, group,
2723 if (comment == NULL)
2726 /* Now we can add our new comment
2728 pair = g_slice_new (GKeyFileKeyValuePair);
2730 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2732 key_node = g_list_insert (key_node, pair, 1);
2738 g_key_file_set_group_comment (GKeyFile *key_file,
2739 const gchar *group_name,
2740 const gchar *comment,
2743 GKeyFileGroup *group;
2745 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2747 group = g_key_file_lookup_group (key_file, group_name);
2750 g_set_error (error, G_KEY_FILE_ERROR,
2751 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2752 _("Key file does not have group '%s'"),
2753 group_name ? group_name : "(null)");
2758 /* First remove any existing comment
2762 g_key_file_key_value_pair_free (group->comment);
2763 group->comment = NULL;
2766 if (comment == NULL)
2769 /* Now we can add our new comment
2771 group->comment = g_slice_new (GKeyFileKeyValuePair);
2772 group->comment->key = NULL;
2773 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2779 g_key_file_set_top_comment (GKeyFile *key_file,
2780 const gchar *comment,
2784 GKeyFileGroup *group;
2785 GKeyFileKeyValuePair *pair;
2787 /* The last group in the list should be the top (comments only)
2790 g_warn_if_fail (key_file->groups != NULL);
2791 group_node = g_list_last (key_file->groups);
2792 group = (GKeyFileGroup *) group_node->data;
2793 g_warn_if_fail (group->name == NULL);
2795 /* Note all keys must be comments at the top of
2796 * the file, so we can just free it all.
2798 if (group->key_value_pairs != NULL)
2800 g_list_foreach (group->key_value_pairs,
2801 (GFunc) g_key_file_key_value_pair_free,
2803 g_list_free (group->key_value_pairs);
2804 group->key_value_pairs = NULL;
2807 if (comment == NULL)
2810 pair = g_slice_new (GKeyFileKeyValuePair);
2812 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2814 group->key_value_pairs =
2815 g_list_prepend (group->key_value_pairs, pair);
2821 * g_key_file_set_comment:
2822 * @key_file: a #GKeyFile
2823 * @group_name: a group name, or %NULL
2825 * @comment: a comment
2826 * @error: return location for a #GError
2828 * Places a comment above @key from @group_name.
2829 * If @key is %NULL then @comment will be written above @group_name.
2830 * If both @key and @group_name are %NULL, then @comment will be
2831 * written above the first group in the file.
2833 * Returns: %TRUE if the comment was written, %FALSE otherwise
2838 g_key_file_set_comment (GKeyFile *key_file,
2839 const gchar *group_name,
2841 const gchar *comment,
2844 g_return_val_if_fail (key_file != NULL, FALSE);
2846 if (group_name != NULL && key != NULL)
2848 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2851 else if (group_name != NULL)
2853 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2858 if (!g_key_file_set_top_comment (key_file, comment, error))
2862 if (comment != NULL)
2863 key_file->approximate_size += strlen (comment);
2869 g_key_file_get_key_comment (GKeyFile *key_file,
2870 const gchar *group_name,
2874 GKeyFileGroup *group;
2875 GKeyFileKeyValuePair *pair;
2876 GList *key_node, *tmp;
2880 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2882 group = g_key_file_lookup_group (key_file, group_name);
2885 g_set_error (error, G_KEY_FILE_ERROR,
2886 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2887 _("Key file does not have group '%s'"),
2888 group_name ? group_name : "(null)");
2893 /* First find the key the comments are supposed to be
2896 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2898 if (key_node == NULL)
2900 g_set_error (error, G_KEY_FILE_ERROR,
2901 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2902 _("Key file does not have key '%s' in group '%s'"),
2909 /* Then find all the comments already associated with the
2910 * key and concatentate them.
2912 tmp = key_node->next;
2913 if (!key_node->next)
2916 pair = (GKeyFileKeyValuePair *) tmp->data;
2917 if (pair->key != NULL)
2922 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2924 if (pair->key != NULL)
2930 while (tmp != key_node)
2932 pair = (GKeyFileKeyValuePair *) tmp->data;
2935 string = g_string_sized_new (512);
2937 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2938 g_string_append (string, comment);
2946 comment = string->str;
2947 g_string_free (string, FALSE);
2956 get_group_comment (GKeyFile *key_file,
2957 GKeyFileGroup *group,
2966 tmp = group->key_value_pairs;
2969 GKeyFileKeyValuePair *pair;
2971 pair = (GKeyFileKeyValuePair *) tmp->data;
2973 if (pair->key != NULL)
2979 if (tmp->next == NULL)
2987 GKeyFileKeyValuePair *pair;
2989 pair = (GKeyFileKeyValuePair *) tmp->data;
2992 string = g_string_sized_new (512);
2994 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2995 g_string_append (string, comment);
3002 return g_string_free (string, FALSE);
3008 g_key_file_get_group_comment (GKeyFile *key_file,
3009 const gchar *group_name,
3013 GKeyFileGroup *group;
3015 group = g_key_file_lookup_group (key_file, group_name);
3018 g_set_error (error, G_KEY_FILE_ERROR,
3019 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3020 _("Key file does not have group '%s'"),
3021 group_name ? group_name : "(null)");
3027 return g_strdup (group->comment->value);
3029 group_node = g_key_file_lookup_group_node (key_file, group_name);
3030 group_node = group_node->next;
3031 group = (GKeyFileGroup *)group_node->data;
3032 return get_group_comment (key_file, group, error);
3036 g_key_file_get_top_comment (GKeyFile *key_file,
3040 GKeyFileGroup *group;
3042 /* The last group in the list should be the top (comments only)
3045 g_warn_if_fail (key_file->groups != NULL);
3046 group_node = g_list_last (key_file->groups);
3047 group = (GKeyFileGroup *) group_node->data;
3048 g_warn_if_fail (group->name == NULL);
3050 return get_group_comment (key_file, group, error);
3054 * g_key_file_get_comment:
3055 * @key_file: a #GKeyFile
3056 * @group_name: a group name, or %NULL
3058 * @error: return location for a #GError
3060 * Retrieves a comment above @key from @group_name.
3061 * If @key is %NULL then @comment will be read from above
3062 * @group_name. If both @key and @group_name are %NULL, then
3063 * @comment will be read from above the first group in the file.
3065 * Returns: a comment that should be freed with g_free()
3070 g_key_file_get_comment (GKeyFile *key_file,
3071 const gchar *group_name,
3075 g_return_val_if_fail (key_file != NULL, NULL);
3077 if (group_name != NULL && key != NULL)
3078 return g_key_file_get_key_comment (key_file, group_name, key, error);
3079 else if (group_name != NULL)
3080 return g_key_file_get_group_comment (key_file, group_name, error);
3082 return g_key_file_get_top_comment (key_file, error);
3086 * g_key_file_remove_comment:
3087 * @key_file: a #GKeyFile
3088 * @group_name: a group name, or %NULL
3090 * @error: return location for a #GError
3092 * Removes a comment above @key from @group_name.
3093 * If @key is %NULL then @comment will be removed above @group_name.
3094 * If both @key and @group_name are %NULL, then @comment will
3095 * be removed above the first group in the file.
3097 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3103 g_key_file_remove_comment (GKeyFile *key_file,
3104 const gchar *group_name,
3108 g_return_val_if_fail (key_file != NULL, FALSE);
3110 if (group_name != NULL && key != NULL)
3111 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3112 else if (group_name != NULL)
3113 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3115 return g_key_file_set_top_comment (key_file, NULL, error);
3119 * g_key_file_has_group:
3120 * @key_file: a #GKeyFile
3121 * @group_name: a group name
3123 * Looks whether the key file has the group @group_name.
3125 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3130 g_key_file_has_group (GKeyFile *key_file,
3131 const gchar *group_name)
3133 g_return_val_if_fail (key_file != NULL, FALSE);
3134 g_return_val_if_fail (group_name != NULL, FALSE);
3136 return g_key_file_lookup_group (key_file, group_name) != NULL;
3139 /* This code remains from a historical attempt to add a new public API
3140 * which respects the GError rules.
3143 g_key_file_has_key_full (GKeyFile *key_file,
3144 const gchar *group_name,
3149 GKeyFileKeyValuePair *pair;
3150 GKeyFileGroup *group;
3152 g_return_val_if_fail (key_file != NULL, FALSE);
3153 g_return_val_if_fail (group_name != NULL, FALSE);
3154 g_return_val_if_fail (key != NULL, FALSE);
3156 group = g_key_file_lookup_group (key_file, group_name);
3160 g_set_error (error, G_KEY_FILE_ERROR,
3161 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3162 _("Key file does not have group '%s'"),
3163 group_name ? group_name : "(null)");
3168 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3171 *has_key = pair != NULL;
3176 * g_key_file_has_key: (skip)
3177 * @key_file: a #GKeyFile
3178 * @group_name: a group name
3180 * @error: return location for a #GError
3182 * Looks whether the key file has the key @key in the group
3185 * <note>This function does not follow the rules for #GError strictly;
3186 * the return value both carries meaning and signals an error. To use
3187 * this function, you must pass a #GError pointer in @error, and check
3188 * whether it is not %NULL to see if an error occurred.</note>
3190 * Language bindings should use g_key_file_get_value() to test whether
3191 * or not a key exists.
3193 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3199 g_key_file_has_key (GKeyFile *key_file,
3200 const gchar *group_name,
3204 GError *temp_error = NULL;
3207 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3213 g_propagate_error (error, temp_error);
3219 g_key_file_add_group (GKeyFile *key_file,
3220 const gchar *group_name)
3222 GKeyFileGroup *group;
3224 g_return_if_fail (key_file != NULL);
3225 g_return_if_fail (g_key_file_is_group_name (group_name));
3227 group = g_key_file_lookup_group (key_file, group_name);
3230 key_file->current_group = group;
3234 group = g_slice_new0 (GKeyFileGroup);
3235 group->name = g_strdup (group_name);
3236 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3237 key_file->groups = g_list_prepend (key_file->groups, group);
3238 key_file->approximate_size += strlen (group_name) + 3;
3239 key_file->current_group = group;
3241 if (key_file->start_group == NULL)
3242 key_file->start_group = group;
3244 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3248 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3253 g_free (pair->value);
3254 g_slice_free (GKeyFileKeyValuePair, pair);
3258 /* Be careful not to call this function on a node with data in the
3259 * lookup map without removing it from the lookup map, first.
3261 * Some current cases where this warning is not a concern are
3263 * - the node being removed is a comment node
3264 * - the entire lookup map is getting destroyed soon after
3268 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3269 GKeyFileGroup *group,
3273 GKeyFileKeyValuePair *pair;
3275 pair = (GKeyFileKeyValuePair *) pair_node->data;
3277 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3279 if (pair->key != NULL)
3280 key_file->approximate_size -= strlen (pair->key) + 1;
3282 g_warn_if_fail (pair->value != NULL);
3283 key_file->approximate_size -= strlen (pair->value);
3285 g_key_file_key_value_pair_free (pair);
3287 g_list_free_1 (pair_node);
3291 g_key_file_remove_group_node (GKeyFile *key_file,
3294 GKeyFileGroup *group;
3297 group = (GKeyFileGroup *) group_node->data;
3300 g_hash_table_remove (key_file->group_hash, group->name);
3302 /* If the current group gets deleted make the current group the last
3305 if (key_file->current_group == group)
3307 /* groups should always contain at least the top comment group,
3308 * unless g_key_file_clear has been called
3310 if (key_file->groups)
3311 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3313 key_file->current_group = NULL;
3316 /* If the start group gets deleted make the start group the first
3319 if (key_file->start_group == group)
3321 tmp = g_list_last (key_file->groups);
3324 if (tmp != group_node &&
3325 ((GKeyFileGroup *) tmp->data)->name != NULL)
3332 key_file->start_group = (GKeyFileGroup *) tmp->data;
3334 key_file->start_group = NULL;
3337 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3339 if (group->name != NULL)
3340 key_file->approximate_size -= strlen (group->name) + 3;
3342 tmp = group->key_value_pairs;
3349 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3352 g_warn_if_fail (group->key_value_pairs == NULL);
3354 if (group->lookup_map)
3356 g_hash_table_destroy (group->lookup_map);
3357 group->lookup_map = NULL;
3360 g_free ((gchar *) group->name);
3361 g_slice_free (GKeyFileGroup, group);
3362 g_list_free_1 (group_node);
3366 * g_key_file_remove_group:
3367 * @key_file: a #GKeyFile
3368 * @group_name: a group name
3369 * @error: return location for a #GError or %NULL
3371 * Removes the specified group, @group_name,
3372 * from the key file.
3374 * Returns: %TRUE if the group was removed, %FALSE otherwise
3379 g_key_file_remove_group (GKeyFile *key_file,
3380 const gchar *group_name,
3385 g_return_val_if_fail (key_file != NULL, FALSE);
3386 g_return_val_if_fail (group_name != NULL, FALSE);
3388 group_node = g_key_file_lookup_group_node (key_file, group_name);
3392 g_set_error (error, G_KEY_FILE_ERROR,
3393 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3394 _("Key file does not have group '%s'"),
3399 g_key_file_remove_group_node (key_file, group_node);
3405 g_key_file_add_key_value_pair (GKeyFile *key_file,
3406 GKeyFileGroup *group,
3407 GKeyFileKeyValuePair *pair)
3409 g_hash_table_replace (group->lookup_map, pair->key, pair);
3410 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3411 group->has_trailing_blank_line = FALSE;
3412 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3416 g_key_file_add_key (GKeyFile *key_file,
3417 GKeyFileGroup *group,
3421 GKeyFileKeyValuePair *pair;
3423 pair = g_slice_new (GKeyFileKeyValuePair);
3424 pair->key = g_strdup (key);
3425 pair->value = g_strdup (value);
3427 g_key_file_add_key_value_pair (key_file, group, pair);
3431 * g_key_file_remove_key:
3432 * @key_file: a #GKeyFile
3433 * @group_name: a group name
3434 * @key: a key name to remove
3435 * @error: return location for a #GError or %NULL
3437 * Removes @key in @group_name from the key file.
3439 * Returns: %TRUE if the key was removed, %FALSE otherwise
3444 g_key_file_remove_key (GKeyFile *key_file,
3445 const gchar *group_name,
3449 GKeyFileGroup *group;
3450 GKeyFileKeyValuePair *pair;
3452 g_return_val_if_fail (key_file != NULL, FALSE);
3453 g_return_val_if_fail (group_name != NULL, FALSE);
3454 g_return_val_if_fail (key != NULL, FALSE);
3458 group = g_key_file_lookup_group (key_file, group_name);
3461 g_set_error (error, G_KEY_FILE_ERROR,
3462 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3463 _("Key file does not have group '%s'"),
3464 group_name ? group_name : "(null)");
3468 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3472 g_set_error (error, G_KEY_FILE_ERROR,
3473 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3474 _("Key file does not have key '%s' in group '%s'"),
3479 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3481 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3482 g_hash_table_remove (group->lookup_map, pair->key);
3483 g_key_file_key_value_pair_free (pair);
3489 g_key_file_lookup_group_node (GKeyFile *key_file,
3490 const gchar *group_name)
3492 GKeyFileGroup *group;
3495 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3497 group = (GKeyFileGroup *) tmp->data;
3499 if (group && group->name && strcmp (group->name, group_name) == 0)
3506 static GKeyFileGroup *
3507 g_key_file_lookup_group (GKeyFile *key_file,
3508 const gchar *group_name)
3510 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3514 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3515 GKeyFileGroup *group,
3520 for (key_node = group->key_value_pairs;
3522 key_node = key_node->next)
3524 GKeyFileKeyValuePair *pair;
3526 pair = (GKeyFileKeyValuePair *) key_node->data;
3528 if (pair->key && strcmp (pair->key, key) == 0)
3535 static GKeyFileKeyValuePair *
3536 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3537 GKeyFileGroup *group,
3540 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3543 /* Lines starting with # or consisting entirely of whitespace are merely
3544 * recorded, not parsed. This function assumes all leading whitespace
3545 * has been stripped.
3548 g_key_file_line_is_comment (const gchar *line)
3550 return (*line == '#' || *line == '\0' || *line == '\n');
3554 g_key_file_is_group_name (const gchar *name)
3561 p = q = (gchar *) name;
3562 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3563 q = g_utf8_find_next_char (q, NULL);
3565 if (*q != '\0' || q == p)
3572 g_key_file_is_key_name (const gchar *name)
3579 p = q = (gchar *) name;
3580 /* We accept a little more than the desktop entry spec says,
3581 * since gnome-vfs uses mime-types as keys in its cache.
3583 while (*q && *q != '=' && *q != '[' && *q != ']')
3584 q = g_utf8_find_next_char (q, NULL);
3586 /* No empty keys, please */
3590 /* We accept spaces in the middle of keys to not break
3591 * existing apps, but we don't tolerate initial or final
3592 * spaces, which would lead to silent corruption when
3593 * rereading the file.
3595 if (*p == ' ' || q[-1] == ' ')
3601 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3602 q = g_utf8_find_next_char (q, NULL);
3616 /* A group in a key file is made up of a starting '[' followed by one
3617 * or more letters making up the group name followed by ']'.
3620 g_key_file_line_is_group (const gchar *line)
3630 while (*p && *p != ']')
3631 p = g_utf8_find_next_char (p, NULL);
3636 /* silently accept whitespace after the ] */
3637 p = g_utf8_find_next_char (p, NULL);
3638 while (*p == ' ' || *p == '\t')
3639 p = g_utf8_find_next_char (p, NULL);
3648 g_key_file_line_is_key_value_pair (const gchar *line)
3652 p = (gchar *) g_utf8_strchr (line, -1, '=');
3657 /* Key must be non-empty
3666 g_key_file_parse_value_as_string (GKeyFile *key_file,
3671 gchar *string_value, *p, *q0, *q;
3673 string_value = g_new (gchar, strlen (value) + 1);
3675 p = (gchar *) value;
3676 q0 = q = string_value;
3706 g_set_error_literal (error, G_KEY_FILE_ERROR,
3707 G_KEY_FILE_ERROR_INVALID_VALUE,
3708 _("Key file contains escape character "
3713 if (pieces && *p == key_file->list_separator)
3714 *q = key_file->list_separator;
3728 g_set_error (error, G_KEY_FILE_ERROR,
3729 G_KEY_FILE_ERROR_INVALID_VALUE,
3730 _("Key file contains invalid escape "
3731 "sequence '%s'"), sequence);
3740 if (pieces && (*p == key_file->list_separator))
3742 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3758 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3759 *pieces = g_slist_reverse (*pieces);
3762 return string_value;
3766 g_key_file_parse_string_as_value (GKeyFile *key_file,
3767 const gchar *string,
3768 gboolean escape_separator)
3770 gchar *value, *p, *q;
3772 gboolean parsing_leading_space;
3774 length = strlen (string) + 1;
3776 /* Worst case would be that every character needs to be escaped.
3777 * In other words every character turns to two characters
3779 value = g_new (gchar, 2 * length);
3781 p = (gchar *) string;
3783 parsing_leading_space = TRUE;
3784 while (p < (string + length - 1))
3786 gchar escaped_character[3] = { '\\', 0, 0 };
3791 if (parsing_leading_space)
3793 escaped_character[1] = 's';
3794 strcpy (q, escaped_character);
3804 if (parsing_leading_space)
3806 escaped_character[1] = 't';
3807 strcpy (q, escaped_character);
3817 escaped_character[1] = 'n';
3818 strcpy (q, escaped_character);
3822 escaped_character[1] = 'r';
3823 strcpy (q, escaped_character);
3827 escaped_character[1] = '\\';
3828 strcpy (q, escaped_character);
3830 parsing_leading_space = FALSE;
3833 if (escape_separator && *p == key_file->list_separator)
3835 escaped_character[1] = key_file->list_separator;
3836 strcpy (q, escaped_character);
3838 parsing_leading_space = TRUE;
3844 parsing_leading_space = FALSE;
3856 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3865 long_value = strtol (value, &eof_int, 10);
3867 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
3869 gchar *value_utf8 = _g_utf8_make_valid (value);
3870 g_set_error (error, G_KEY_FILE_ERROR,
3871 G_KEY_FILE_ERROR_INVALID_VALUE,
3872 _("Value '%s' cannot be interpreted "
3873 "as a number."), value_utf8);
3874 g_free (value_utf8);
3879 int_value = long_value;
3880 if (int_value != long_value || errno == ERANGE)
3882 gchar *value_utf8 = _g_utf8_make_valid (value);
3885 G_KEY_FILE_ERROR_INVALID_VALUE,
3886 _("Integer value '%s' out of range"),
3888 g_free (value_utf8);
3897 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3901 return g_strdup_printf ("%d", value);
3905 g_key_file_parse_value_as_double (GKeyFile *key_file,
3909 gchar *end_of_valid_d;
3910 gdouble double_value = 0;
3912 double_value = g_ascii_strtod (value, &end_of_valid_d);
3914 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3916 gchar *value_utf8 = _g_utf8_make_valid (value);
3917 g_set_error (error, G_KEY_FILE_ERROR,
3918 G_KEY_FILE_ERROR_INVALID_VALUE,
3919 _("Value '%s' cannot be interpreted "
3920 "as a float number."),
3922 g_free (value_utf8);
3925 return double_value;
3929 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3935 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3937 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3940 value_utf8 = _g_utf8_make_valid (value);
3941 g_set_error (error, G_KEY_FILE_ERROR,
3942 G_KEY_FILE_ERROR_INVALID_VALUE,
3943 _("Value '%s' cannot be interpreted "
3944 "as a boolean."), value_utf8);
3945 g_free (value_utf8);
3951 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3955 return g_strdup ("true");
3957 return g_strdup ("false");
3961 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3968 string = g_string_sized_new (512);
3970 lines = g_strsplit (value, "\n", 0);
3972 for (i = 0; lines[i] != NULL; i++)
3974 if (lines[i][0] != '#')
3975 g_string_append_printf (string, "%s\n", lines[i]);
3977 g_string_append_printf (string, "%s\n", lines[i] + 1);
3981 return g_string_free (string, FALSE);
3985 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3986 const gchar *comment)
3992 string = g_string_sized_new (512);
3994 lines = g_strsplit (comment, "\n", 0);
3996 for (i = 0; lines[i] != NULL; i++)
3997 g_string_append_printf (string, "#%s%s", lines[i],
3998 lines[i + 1] == NULL? "" : "\n");
4001 return g_string_free (string, FALSE);