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: the values associated with the key as a list of
1990 * booleans, or %NULL if the key was not found or could not be parsed.
1995 g_key_file_get_boolean_list (GKeyFile *key_file,
1996 const gchar *group_name,
2001 GError *key_file_error;
2003 gboolean *bool_values;
2006 g_return_val_if_fail (key_file != NULL, NULL);
2007 g_return_val_if_fail (group_name != NULL, NULL);
2008 g_return_val_if_fail (key != NULL, NULL);
2013 key_file_error = NULL;
2015 values = g_key_file_get_string_list (key_file, group_name, key,
2016 &num_bools, &key_file_error);
2019 g_propagate_error (error, key_file_error);
2024 bool_values = g_new (gboolean, num_bools);
2026 for (i = 0; i < num_bools; i++)
2028 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2034 g_propagate_error (error, key_file_error);
2035 g_strfreev (values);
2036 g_free (bool_values);
2041 g_strfreev (values);
2044 *length = num_bools;
2050 * g_key_file_set_boolean_list:
2051 * @key_file: a #GKeyFile
2052 * @group_name: a group name
2054 * @list: an array of boolean values
2055 * @length: length of @list
2057 * Associates a list of boolean values with @key under @group_name.
2058 * If @key cannot be found then it is created.
2059 * If @group_name is %NULL, the start_group is used.
2064 g_key_file_set_boolean_list (GKeyFile *key_file,
2065 const gchar *group_name,
2070 GString *value_list;
2073 g_return_if_fail (key_file != NULL);
2074 g_return_if_fail (list != NULL);
2076 value_list = g_string_sized_new (length * 8);
2077 for (i = 0; i < length; i++)
2081 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2083 g_string_append (value_list, value);
2084 g_string_append_c (value_list, key_file->list_separator);
2089 g_key_file_set_value (key_file, group_name, key, value_list->str);
2090 g_string_free (value_list, TRUE);
2094 * g_key_file_get_integer:
2095 * @key_file: a #GKeyFile
2096 * @group_name: a group name
2098 * @error: return location for a #GError
2100 * Returns the value associated with @key under @group_name as an
2103 * If @key cannot be found then 0 is returned and @error is set to
2104 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2105 * with @key cannot be interpreted as an integer then 0 is returned
2106 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2108 * Return value: the value associated with the key as an integer, or
2109 * 0 if the key was not found or could not be parsed.
2114 g_key_file_get_integer (GKeyFile *key_file,
2115 const gchar *group_name,
2119 GError *key_file_error;
2123 g_return_val_if_fail (key_file != NULL, -1);
2124 g_return_val_if_fail (group_name != NULL, -1);
2125 g_return_val_if_fail (key != NULL, -1);
2127 key_file_error = NULL;
2129 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2133 g_propagate_error (error, key_file_error);
2137 int_value = g_key_file_parse_value_as_integer (key_file, value,
2143 if (g_error_matches (key_file_error,
2145 G_KEY_FILE_ERROR_INVALID_VALUE))
2147 g_set_error (error, G_KEY_FILE_ERROR,
2148 G_KEY_FILE_ERROR_INVALID_VALUE,
2149 _("Key file contains key '%s' in group '%s' "
2150 "which has value that cannot be interpreted."), key,
2152 g_error_free (key_file_error);
2155 g_propagate_error (error, key_file_error);
2162 * g_key_file_set_integer:
2163 * @key_file: a #GKeyFile
2164 * @group_name: a group name
2166 * @value: an integer value
2168 * Associates a new integer value with @key under @group_name.
2169 * If @key cannot be found then it is created.
2174 g_key_file_set_integer (GKeyFile *key_file,
2175 const gchar *group_name,
2181 g_return_if_fail (key_file != NULL);
2183 result = g_key_file_parse_integer_as_value (key_file, value);
2184 g_key_file_set_value (key_file, group_name, key, result);
2189 * g_key_file_get_int64:
2190 * @key_file: a non-%NULL #GKeyFile
2191 * @group_name: a non-%NULL group name
2192 * @key: a non-%NULL key
2193 * @error: return location for a #GError
2195 * Returns the value associated with @key under @group_name as a signed
2196 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2197 * 64-bit results without truncation.
2199 * Returns: the value associated with the key as a signed 64-bit integer, or
2200 * 0 if the key was not found or could not be parsed.
2205 g_key_file_get_int64 (GKeyFile *key_file,
2206 const gchar *group_name,
2213 g_return_val_if_fail (key_file != NULL, -1);
2214 g_return_val_if_fail (group_name != NULL, -1);
2215 g_return_val_if_fail (key != NULL, -1);
2217 s = g_key_file_get_value (key_file, group_name, key, error);
2222 v = g_ascii_strtoll (s, &end, 10);
2224 if (*s == '\0' || *end != '\0')
2226 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2227 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2228 key, group_name, s);
2237 * g_key_file_set_int64:
2238 * @key_file: a #GKeyFile
2239 * @group_name: a group name
2241 * @value: an integer value
2243 * Associates a new integer value with @key under @group_name.
2244 * If @key cannot be found then it is created.
2249 g_key_file_set_int64 (GKeyFile *key_file,
2250 const gchar *group_name,
2256 g_return_if_fail (key_file != NULL);
2258 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2259 g_key_file_set_value (key_file, group_name, key, result);
2264 * g_key_file_get_uint64:
2265 * @key_file: a non-%NULL #GKeyFile
2266 * @group_name: a non-%NULL group name
2267 * @key: a non-%NULL key
2268 * @error: return location for a #GError
2270 * Returns the value associated with @key under @group_name as an unsigned
2271 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2272 * large positive results without truncation.
2274 * Returns: the value associated with the key as an unsigned 64-bit integer,
2275 * or 0 if the key was not found or could not be parsed.
2280 g_key_file_get_uint64 (GKeyFile *key_file,
2281 const gchar *group_name,
2288 g_return_val_if_fail (key_file != NULL, -1);
2289 g_return_val_if_fail (group_name != NULL, -1);
2290 g_return_val_if_fail (key != NULL, -1);
2292 s = g_key_file_get_value (key_file, group_name, key, error);
2297 v = g_ascii_strtoull (s, &end, 10);
2299 if (*s == '\0' || *end != '\0')
2301 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2302 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2303 key, group_name, s);
2312 * g_key_file_set_uint64:
2313 * @key_file: a #GKeyFile
2314 * @group_name: a group name
2316 * @value: an integer value
2318 * Associates a new integer value with @key under @group_name.
2319 * If @key cannot be found then it is created.
2324 g_key_file_set_uint64 (GKeyFile *key_file,
2325 const gchar *group_name,
2331 g_return_if_fail (key_file != NULL);
2333 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2334 g_key_file_set_value (key_file, group_name, key, result);
2339 * g_key_file_get_integer_list:
2340 * @key_file: a #GKeyFile
2341 * @group_name: a group name
2343 * @length: the number of integers returned
2344 * @error: return location for a #GError
2346 * Returns the values associated with @key under @group_name as
2349 * If @key cannot be found then %NULL is returned and @error is set to
2350 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2351 * with @key cannot be interpreted as integers then %NULL is returned
2352 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2354 * Return value: the values associated with the key as a list of
2355 * integers, or %NULL if the key was not found or could not be parsed.
2360 g_key_file_get_integer_list (GKeyFile *key_file,
2361 const gchar *group_name,
2366 GError *key_file_error = NULL;
2371 g_return_val_if_fail (key_file != NULL, NULL);
2372 g_return_val_if_fail (group_name != NULL, NULL);
2373 g_return_val_if_fail (key != NULL, NULL);
2378 values = g_key_file_get_string_list (key_file, group_name, key,
2379 &num_ints, &key_file_error);
2382 g_propagate_error (error, key_file_error);
2387 int_values = g_new (gint, num_ints);
2389 for (i = 0; i < num_ints; i++)
2391 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2397 g_propagate_error (error, key_file_error);
2398 g_strfreev (values);
2399 g_free (int_values);
2404 g_strfreev (values);
2413 * g_key_file_set_integer_list:
2414 * @key_file: a #GKeyFile
2415 * @group_name: a group name
2417 * @list: an array of integer values
2418 * @length: number of integer values in @list
2420 * Associates a list of integer values with @key under @group_name.
2421 * If @key cannot be found then it is created.
2426 g_key_file_set_integer_list (GKeyFile *key_file,
2427 const gchar *group_name,
2435 g_return_if_fail (key_file != NULL);
2436 g_return_if_fail (list != NULL);
2438 values = g_string_sized_new (length * 16);
2439 for (i = 0; i < length; i++)
2443 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2445 g_string_append (values, value);
2446 g_string_append_c (values, key_file->list_separator);
2451 g_key_file_set_value (key_file, group_name, key, values->str);
2452 g_string_free (values, TRUE);
2456 * g_key_file_get_double:
2457 * @key_file: a #GKeyFile
2458 * @group_name: a group name
2460 * @error: return location for a #GError
2462 * Returns the value associated with @key under @group_name as a
2463 * double. If @group_name is %NULL, the start_group is used.
2465 * If @key cannot be found then 0.0 is returned and @error is set to
2466 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2467 * with @key cannot be interpreted as a double then 0.0 is returned
2468 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2470 * Return value: the value associated with the key as a double, or
2471 * 0.0 if the key was not found or could not be parsed.
2476 g_key_file_get_double (GKeyFile *key_file,
2477 const gchar *group_name,
2481 GError *key_file_error;
2483 gdouble double_value;
2485 g_return_val_if_fail (key_file != NULL, -1);
2486 g_return_val_if_fail (group_name != NULL, -1);
2487 g_return_val_if_fail (key != NULL, -1);
2489 key_file_error = NULL;
2491 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2495 g_propagate_error (error, key_file_error);
2499 double_value = g_key_file_parse_value_as_double (key_file, value,
2505 if (g_error_matches (key_file_error,
2507 G_KEY_FILE_ERROR_INVALID_VALUE))
2509 g_set_error (error, G_KEY_FILE_ERROR,
2510 G_KEY_FILE_ERROR_INVALID_VALUE,
2511 _("Key file contains key '%s' in group '%s' "
2512 "which has value that cannot be interpreted."), key,
2514 g_error_free (key_file_error);
2517 g_propagate_error (error, key_file_error);
2520 return double_value;
2524 * g_key_file_set_double:
2525 * @key_file: a #GKeyFile
2526 * @group_name: a group name
2528 * @value: an double value
2530 * Associates a new double value with @key under @group_name.
2531 * If @key cannot be found then it is created.
2536 g_key_file_set_double (GKeyFile *key_file,
2537 const gchar *group_name,
2541 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2543 g_return_if_fail (key_file != NULL);
2545 g_ascii_dtostr (result, sizeof (result), value);
2546 g_key_file_set_value (key_file, group_name, key, result);
2550 * g_key_file_get_double_list:
2551 * @key_file: a #GKeyFile
2552 * @group_name: a group name
2554 * @length: the number of doubles returned
2555 * @error: return location for a #GError
2557 * Returns the values associated with @key under @group_name as
2560 * If @key cannot be found then %NULL is returned and @error is set to
2561 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2562 * with @key cannot be interpreted as doubles then %NULL is returned
2563 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2565 * Return value: the values associated with the key as a list of
2566 * doubles, or %NULL if the key was not found or could not be parsed.
2571 g_key_file_get_double_list (GKeyFile *key_file,
2572 const gchar *group_name,
2577 GError *key_file_error = NULL;
2579 gdouble *double_values;
2580 gsize i, num_doubles;
2582 g_return_val_if_fail (key_file != NULL, NULL);
2583 g_return_val_if_fail (group_name != NULL, NULL);
2584 g_return_val_if_fail (key != NULL, NULL);
2589 values = g_key_file_get_string_list (key_file, group_name, key,
2590 &num_doubles, &key_file_error);
2593 g_propagate_error (error, key_file_error);
2598 double_values = g_new (gdouble, num_doubles);
2600 for (i = 0; i < num_doubles; i++)
2602 double_values[i] = g_key_file_parse_value_as_double (key_file,
2608 g_propagate_error (error, key_file_error);
2609 g_strfreev (values);
2610 g_free (double_values);
2615 g_strfreev (values);
2618 *length = num_doubles;
2620 return double_values;
2624 * g_key_file_set_double_list:
2625 * @key_file: a #GKeyFile
2626 * @group_name: a group name
2628 * @list: an array of double values
2629 * @length: number of double values in @list
2631 * Associates a list of double values with @key under
2632 * @group_name. If @key cannot be found then it is created.
2637 g_key_file_set_double_list (GKeyFile *key_file,
2638 const gchar *group_name,
2646 g_return_if_fail (key_file != NULL);
2647 g_return_if_fail (list != NULL);
2649 values = g_string_sized_new (length * 16);
2650 for (i = 0; i < length; i++)
2652 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2654 g_ascii_dtostr( result, sizeof (result), list[i] );
2656 g_string_append (values, result);
2657 g_string_append_c (values, key_file->list_separator);
2660 g_key_file_set_value (key_file, group_name, key, values->str);
2661 g_string_free (values, TRUE);
2665 g_key_file_set_key_comment (GKeyFile *key_file,
2666 const gchar *group_name,
2668 const gchar *comment,
2671 GKeyFileGroup *group;
2672 GKeyFileKeyValuePair *pair;
2673 GList *key_node, *comment_node, *tmp;
2675 group = g_key_file_lookup_group (key_file, group_name);
2678 g_set_error (error, G_KEY_FILE_ERROR,
2679 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2680 _("Key file does not have group '%s'"),
2681 group_name ? group_name : "(null)");
2686 /* First find the key the comments are supposed to be
2689 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2691 if (key_node == NULL)
2693 g_set_error (error, G_KEY_FILE_ERROR,
2694 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2695 _("Key file does not have key '%s' in group '%s'"),
2700 /* Then find all the comments already associated with the
2703 tmp = key_node->next;
2706 pair = (GKeyFileKeyValuePair *) tmp->data;
2708 if (pair->key != NULL)
2713 g_key_file_remove_key_value_pair_node (key_file, group,
2717 if (comment == NULL)
2720 /* Now we can add our new comment
2722 pair = g_slice_new (GKeyFileKeyValuePair);
2724 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2726 key_node = g_list_insert (key_node, pair, 1);
2732 g_key_file_set_group_comment (GKeyFile *key_file,
2733 const gchar *group_name,
2734 const gchar *comment,
2737 GKeyFileGroup *group;
2739 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2741 group = g_key_file_lookup_group (key_file, group_name);
2744 g_set_error (error, G_KEY_FILE_ERROR,
2745 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2746 _("Key file does not have group '%s'"),
2747 group_name ? group_name : "(null)");
2752 /* First remove any existing comment
2756 g_key_file_key_value_pair_free (group->comment);
2757 group->comment = NULL;
2760 if (comment == NULL)
2763 /* Now we can add our new comment
2765 group->comment = g_slice_new (GKeyFileKeyValuePair);
2766 group->comment->key = NULL;
2767 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2773 g_key_file_set_top_comment (GKeyFile *key_file,
2774 const gchar *comment,
2778 GKeyFileGroup *group;
2779 GKeyFileKeyValuePair *pair;
2781 /* The last group in the list should be the top (comments only)
2784 g_warn_if_fail (key_file->groups != NULL);
2785 group_node = g_list_last (key_file->groups);
2786 group = (GKeyFileGroup *) group_node->data;
2787 g_warn_if_fail (group->name == NULL);
2789 /* Note all keys must be comments at the top of
2790 * the file, so we can just free it all.
2792 if (group->key_value_pairs != NULL)
2794 g_list_foreach (group->key_value_pairs,
2795 (GFunc) g_key_file_key_value_pair_free,
2797 g_list_free (group->key_value_pairs);
2798 group->key_value_pairs = NULL;
2801 if (comment == NULL)
2804 pair = g_slice_new (GKeyFileKeyValuePair);
2806 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2808 group->key_value_pairs =
2809 g_list_prepend (group->key_value_pairs, pair);
2815 * g_key_file_set_comment:
2816 * @key_file: a #GKeyFile
2817 * @group_name: a group name, or %NULL
2819 * @comment: a comment
2820 * @error: return location for a #GError
2822 * Places a comment above @key from @group_name.
2823 * If @key is %NULL then @comment will be written above @group_name.
2824 * If both @key and @group_name are %NULL, then @comment will be
2825 * written above the first group in the file.
2827 * Returns: %TRUE if the comment was written, %FALSE otherwise
2832 g_key_file_set_comment (GKeyFile *key_file,
2833 const gchar *group_name,
2835 const gchar *comment,
2838 g_return_val_if_fail (key_file != NULL, FALSE);
2840 if (group_name != NULL && key != NULL)
2842 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2845 else if (group_name != NULL)
2847 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2852 if (!g_key_file_set_top_comment (key_file, comment, error))
2856 if (comment != NULL)
2857 key_file->approximate_size += strlen (comment);
2863 g_key_file_get_key_comment (GKeyFile *key_file,
2864 const gchar *group_name,
2868 GKeyFileGroup *group;
2869 GKeyFileKeyValuePair *pair;
2870 GList *key_node, *tmp;
2874 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2876 group = g_key_file_lookup_group (key_file, group_name);
2879 g_set_error (error, G_KEY_FILE_ERROR,
2880 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2881 _("Key file does not have group '%s'"),
2882 group_name ? group_name : "(null)");
2887 /* First find the key the comments are supposed to be
2890 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2892 if (key_node == NULL)
2894 g_set_error (error, G_KEY_FILE_ERROR,
2895 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2896 _("Key file does not have key '%s' in group '%s'"),
2903 /* Then find all the comments already associated with the
2904 * key and concatentate them.
2906 tmp = key_node->next;
2907 if (!key_node->next)
2910 pair = (GKeyFileKeyValuePair *) tmp->data;
2911 if (pair->key != NULL)
2916 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2918 if (pair->key != NULL)
2924 while (tmp != key_node)
2926 pair = (GKeyFileKeyValuePair *) tmp->data;
2929 string = g_string_sized_new (512);
2931 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2932 g_string_append (string, comment);
2940 comment = string->str;
2941 g_string_free (string, FALSE);
2950 get_group_comment (GKeyFile *key_file,
2951 GKeyFileGroup *group,
2960 tmp = group->key_value_pairs;
2963 GKeyFileKeyValuePair *pair;
2965 pair = (GKeyFileKeyValuePair *) tmp->data;
2967 if (pair->key != NULL)
2973 if (tmp->next == NULL)
2981 GKeyFileKeyValuePair *pair;
2983 pair = (GKeyFileKeyValuePair *) tmp->data;
2986 string = g_string_sized_new (512);
2988 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2989 g_string_append (string, comment);
2996 return g_string_free (string, FALSE);
3002 g_key_file_get_group_comment (GKeyFile *key_file,
3003 const gchar *group_name,
3007 GKeyFileGroup *group;
3009 group = g_key_file_lookup_group (key_file, group_name);
3012 g_set_error (error, G_KEY_FILE_ERROR,
3013 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3014 _("Key file does not have group '%s'"),
3015 group_name ? group_name : "(null)");
3021 return g_strdup (group->comment->value);
3023 group_node = g_key_file_lookup_group_node (key_file, group_name);
3024 group_node = group_node->next;
3025 group = (GKeyFileGroup *)group_node->data;
3026 return get_group_comment (key_file, group, error);
3030 g_key_file_get_top_comment (GKeyFile *key_file,
3034 GKeyFileGroup *group;
3036 /* The last group in the list should be the top (comments only)
3039 g_warn_if_fail (key_file->groups != NULL);
3040 group_node = g_list_last (key_file->groups);
3041 group = (GKeyFileGroup *) group_node->data;
3042 g_warn_if_fail (group->name == NULL);
3044 return get_group_comment (key_file, group, error);
3048 * g_key_file_get_comment:
3049 * @key_file: a #GKeyFile
3050 * @group_name: a group name, or %NULL
3052 * @error: return location for a #GError
3054 * Retrieves a comment above @key from @group_name.
3055 * If @key is %NULL then @comment will be read from above
3056 * @group_name. If both @key and @group_name are %NULL, then
3057 * @comment will be read from above the first group in the file.
3059 * Returns: a comment that should be freed with g_free()
3064 g_key_file_get_comment (GKeyFile *key_file,
3065 const gchar *group_name,
3069 g_return_val_if_fail (key_file != NULL, NULL);
3071 if (group_name != NULL && key != NULL)
3072 return g_key_file_get_key_comment (key_file, group_name, key, error);
3073 else if (group_name != NULL)
3074 return g_key_file_get_group_comment (key_file, group_name, error);
3076 return g_key_file_get_top_comment (key_file, error);
3080 * g_key_file_remove_comment:
3081 * @key_file: a #GKeyFile
3082 * @group_name: a group name, or %NULL
3084 * @error: return location for a #GError
3086 * Removes a comment above @key from @group_name.
3087 * If @key is %NULL then @comment will be removed above @group_name.
3088 * If both @key and @group_name are %NULL, then @comment will
3089 * be removed above the first group in the file.
3091 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3097 g_key_file_remove_comment (GKeyFile *key_file,
3098 const gchar *group_name,
3102 g_return_val_if_fail (key_file != NULL, FALSE);
3104 if (group_name != NULL && key != NULL)
3105 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3106 else if (group_name != NULL)
3107 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3109 return g_key_file_set_top_comment (key_file, NULL, error);
3113 * g_key_file_has_group:
3114 * @key_file: a #GKeyFile
3115 * @group_name: a group name
3117 * Looks whether the key file has the group @group_name.
3119 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3124 g_key_file_has_group (GKeyFile *key_file,
3125 const gchar *group_name)
3127 g_return_val_if_fail (key_file != NULL, FALSE);
3128 g_return_val_if_fail (group_name != NULL, FALSE);
3130 return g_key_file_lookup_group (key_file, group_name) != NULL;
3134 * g_key_file_has_key: (skip)
3135 * @key_file: a #GKeyFile
3136 * @group_name: a group name
3138 * @error: return location for a #GError
3140 * Looks whether the key file has the key @key in the group
3143 * <note>This function does not follow the rules for #GError strictly;
3144 * the return value both carries meaning and signals an error. To use
3145 * this function, you must pass a #GError pointer in @error, and check
3146 * whether it is not %NULL to see if an error occurred.</note>
3148 * See g_key_file_has_key_full() for a replacement function which does
3149 * follow the #GError rules.
3151 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3157 g_key_file_has_key (GKeyFile *key_file,
3158 const gchar *group_name,
3162 GError *temp_error = NULL;
3165 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3171 g_propagate_error (error, temp_error);
3177 * g_key_file_has_key_full:
3178 * @key_file: a #GKeyFile
3179 * @group_name: a group name
3181 * @has_key: (out) (allow-none): Return location for whether or not key exists
3182 * @error: return location for a #GError
3184 * Looks whether the key file has the key @key in the group
3187 * Return value: %TRUE if a group with the name @group_name
3188 * exists. Otherwise, @error is set and %FALSE is returned.
3192 * Rename to: g_key_file_has_key
3195 g_key_file_has_key_full (GKeyFile *key_file,
3196 const gchar *group_name,
3201 GKeyFileKeyValuePair *pair;
3202 GKeyFileGroup *group;
3204 g_return_val_if_fail (key_file != NULL, FALSE);
3205 g_return_val_if_fail (group_name != NULL, FALSE);
3206 g_return_val_if_fail (key != NULL, FALSE);
3208 group = g_key_file_lookup_group (key_file, group_name);
3212 g_set_error (error, G_KEY_FILE_ERROR,
3213 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3214 _("Key file does not have group '%s'"),
3215 group_name ? group_name : "(null)");
3220 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3223 *has_key = pair != NULL;
3228 g_key_file_add_group (GKeyFile *key_file,
3229 const gchar *group_name)
3231 GKeyFileGroup *group;
3233 g_return_if_fail (key_file != NULL);
3234 g_return_if_fail (g_key_file_is_group_name (group_name));
3236 group = g_key_file_lookup_group (key_file, group_name);
3239 key_file->current_group = group;
3243 group = g_slice_new0 (GKeyFileGroup);
3244 group->name = g_strdup (group_name);
3245 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3246 key_file->groups = g_list_prepend (key_file->groups, group);
3247 key_file->approximate_size += strlen (group_name) + 3;
3248 key_file->current_group = group;
3250 if (key_file->start_group == NULL)
3251 key_file->start_group = group;
3253 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3257 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3262 g_free (pair->value);
3263 g_slice_free (GKeyFileKeyValuePair, pair);
3267 /* Be careful not to call this function on a node with data in the
3268 * lookup map without removing it from the lookup map, first.
3270 * Some current cases where this warning is not a concern are
3272 * - the node being removed is a comment node
3273 * - the entire lookup map is getting destroyed soon after
3277 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3278 GKeyFileGroup *group,
3282 GKeyFileKeyValuePair *pair;
3284 pair = (GKeyFileKeyValuePair *) pair_node->data;
3286 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3288 if (pair->key != NULL)
3289 key_file->approximate_size -= strlen (pair->key) + 1;
3291 g_warn_if_fail (pair->value != NULL);
3292 key_file->approximate_size -= strlen (pair->value);
3294 g_key_file_key_value_pair_free (pair);
3296 g_list_free_1 (pair_node);
3300 g_key_file_remove_group_node (GKeyFile *key_file,
3303 GKeyFileGroup *group;
3306 group = (GKeyFileGroup *) group_node->data;
3309 g_hash_table_remove (key_file->group_hash, group->name);
3311 /* If the current group gets deleted make the current group the last
3314 if (key_file->current_group == group)
3316 /* groups should always contain at least the top comment group,
3317 * unless g_key_file_clear has been called
3319 if (key_file->groups)
3320 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3322 key_file->current_group = NULL;
3325 /* If the start group gets deleted make the start group the first
3328 if (key_file->start_group == group)
3330 tmp = g_list_last (key_file->groups);
3333 if (tmp != group_node &&
3334 ((GKeyFileGroup *) tmp->data)->name != NULL)
3341 key_file->start_group = (GKeyFileGroup *) tmp->data;
3343 key_file->start_group = NULL;
3346 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3348 if (group->name != NULL)
3349 key_file->approximate_size -= strlen (group->name) + 3;
3351 tmp = group->key_value_pairs;
3358 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3361 g_warn_if_fail (group->key_value_pairs == NULL);
3363 if (group->lookup_map)
3365 g_hash_table_destroy (group->lookup_map);
3366 group->lookup_map = NULL;
3369 g_free ((gchar *) group->name);
3370 g_slice_free (GKeyFileGroup, group);
3371 g_list_free_1 (group_node);
3375 * g_key_file_remove_group:
3376 * @key_file: a #GKeyFile
3377 * @group_name: a group name
3378 * @error: return location for a #GError or %NULL
3380 * Removes the specified group, @group_name,
3381 * from the key file.
3383 * Returns: %TRUE if the group was removed, %FALSE otherwise
3388 g_key_file_remove_group (GKeyFile *key_file,
3389 const gchar *group_name,
3394 g_return_val_if_fail (key_file != NULL, FALSE);
3395 g_return_val_if_fail (group_name != NULL, FALSE);
3397 group_node = g_key_file_lookup_group_node (key_file, group_name);
3401 g_set_error (error, G_KEY_FILE_ERROR,
3402 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3403 _("Key file does not have group '%s'"),
3408 g_key_file_remove_group_node (key_file, group_node);
3414 g_key_file_add_key_value_pair (GKeyFile *key_file,
3415 GKeyFileGroup *group,
3416 GKeyFileKeyValuePair *pair)
3418 g_hash_table_replace (group->lookup_map, pair->key, pair);
3419 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3420 group->has_trailing_blank_line = FALSE;
3421 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3425 g_key_file_add_key (GKeyFile *key_file,
3426 GKeyFileGroup *group,
3430 GKeyFileKeyValuePair *pair;
3432 pair = g_slice_new (GKeyFileKeyValuePair);
3433 pair->key = g_strdup (key);
3434 pair->value = g_strdup (value);
3436 g_key_file_add_key_value_pair (key_file, group, pair);
3440 * g_key_file_remove_key:
3441 * @key_file: a #GKeyFile
3442 * @group_name: a group name
3443 * @key: a key name to remove
3444 * @error: return location for a #GError or %NULL
3446 * Removes @key in @group_name from the key file.
3448 * Returns: %TRUE if the key was removed, %FALSE otherwise
3453 g_key_file_remove_key (GKeyFile *key_file,
3454 const gchar *group_name,
3458 GKeyFileGroup *group;
3459 GKeyFileKeyValuePair *pair;
3461 g_return_val_if_fail (key_file != NULL, FALSE);
3462 g_return_val_if_fail (group_name != NULL, FALSE);
3463 g_return_val_if_fail (key != NULL, FALSE);
3467 group = g_key_file_lookup_group (key_file, group_name);
3470 g_set_error (error, G_KEY_FILE_ERROR,
3471 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3472 _("Key file does not have group '%s'"),
3473 group_name ? group_name : "(null)");
3477 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3481 g_set_error (error, G_KEY_FILE_ERROR,
3482 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3483 _("Key file does not have key '%s' in group '%s'"),
3488 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3490 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3491 g_hash_table_remove (group->lookup_map, pair->key);
3492 g_key_file_key_value_pair_free (pair);
3498 g_key_file_lookup_group_node (GKeyFile *key_file,
3499 const gchar *group_name)
3501 GKeyFileGroup *group;
3504 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3506 group = (GKeyFileGroup *) tmp->data;
3508 if (group && group->name && strcmp (group->name, group_name) == 0)
3515 static GKeyFileGroup *
3516 g_key_file_lookup_group (GKeyFile *key_file,
3517 const gchar *group_name)
3519 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3523 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3524 GKeyFileGroup *group,
3529 for (key_node = group->key_value_pairs;
3531 key_node = key_node->next)
3533 GKeyFileKeyValuePair *pair;
3535 pair = (GKeyFileKeyValuePair *) key_node->data;
3537 if (pair->key && strcmp (pair->key, key) == 0)
3544 static GKeyFileKeyValuePair *
3545 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3546 GKeyFileGroup *group,
3549 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3552 /* Lines starting with # or consisting entirely of whitespace are merely
3553 * recorded, not parsed. This function assumes all leading whitespace
3554 * has been stripped.
3557 g_key_file_line_is_comment (const gchar *line)
3559 return (*line == '#' || *line == '\0' || *line == '\n');
3563 g_key_file_is_group_name (const gchar *name)
3570 p = q = (gchar *) name;
3571 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3572 q = g_utf8_find_next_char (q, NULL);
3574 if (*q != '\0' || q == p)
3581 g_key_file_is_key_name (const gchar *name)
3588 p = q = (gchar *) name;
3589 /* We accept a little more than the desktop entry spec says,
3590 * since gnome-vfs uses mime-types as keys in its cache.
3592 while (*q && *q != '=' && *q != '[' && *q != ']')
3593 q = g_utf8_find_next_char (q, NULL);
3595 /* No empty keys, please */
3599 /* We accept spaces in the middle of keys to not break
3600 * existing apps, but we don't tolerate initial or final
3601 * spaces, which would lead to silent corruption when
3602 * rereading the file.
3604 if (*p == ' ' || q[-1] == ' ')
3610 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3611 q = g_utf8_find_next_char (q, NULL);
3625 /* A group in a key file is made up of a starting '[' followed by one
3626 * or more letters making up the group name followed by ']'.
3629 g_key_file_line_is_group (const gchar *line)
3639 while (*p && *p != ']')
3640 p = g_utf8_find_next_char (p, NULL);
3645 /* silently accept whitespace after the ] */
3646 p = g_utf8_find_next_char (p, NULL);
3647 while (*p == ' ' || *p == '\t')
3648 p = g_utf8_find_next_char (p, NULL);
3657 g_key_file_line_is_key_value_pair (const gchar *line)
3661 p = (gchar *) g_utf8_strchr (line, -1, '=');
3666 /* Key must be non-empty
3675 g_key_file_parse_value_as_string (GKeyFile *key_file,
3680 gchar *string_value, *p, *q0, *q;
3682 string_value = g_new (gchar, strlen (value) + 1);
3684 p = (gchar *) value;
3685 q0 = q = string_value;
3715 g_set_error_literal (error, G_KEY_FILE_ERROR,
3716 G_KEY_FILE_ERROR_INVALID_VALUE,
3717 _("Key file contains escape character "
3722 if (pieces && *p == key_file->list_separator)
3723 *q = key_file->list_separator;
3737 g_set_error (error, G_KEY_FILE_ERROR,
3738 G_KEY_FILE_ERROR_INVALID_VALUE,
3739 _("Key file contains invalid escape "
3740 "sequence '%s'"), sequence);
3749 if (pieces && (*p == key_file->list_separator))
3751 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3767 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3768 *pieces = g_slist_reverse (*pieces);
3771 return string_value;
3775 g_key_file_parse_string_as_value (GKeyFile *key_file,
3776 const gchar *string,
3777 gboolean escape_separator)
3779 gchar *value, *p, *q;
3781 gboolean parsing_leading_space;
3783 length = strlen (string) + 1;
3785 /* Worst case would be that every character needs to be escaped.
3786 * In other words every character turns to two characters
3788 value = g_new (gchar, 2 * length);
3790 p = (gchar *) string;
3792 parsing_leading_space = TRUE;
3793 while (p < (string + length - 1))
3795 gchar escaped_character[3] = { '\\', 0, 0 };
3800 if (parsing_leading_space)
3802 escaped_character[1] = 's';
3803 strcpy (q, escaped_character);
3813 if (parsing_leading_space)
3815 escaped_character[1] = 't';
3816 strcpy (q, escaped_character);
3826 escaped_character[1] = 'n';
3827 strcpy (q, escaped_character);
3831 escaped_character[1] = 'r';
3832 strcpy (q, escaped_character);
3836 escaped_character[1] = '\\';
3837 strcpy (q, escaped_character);
3839 parsing_leading_space = FALSE;
3842 if (escape_separator && *p == key_file->list_separator)
3844 escaped_character[1] = key_file->list_separator;
3845 strcpy (q, escaped_character);
3847 parsing_leading_space = TRUE;
3853 parsing_leading_space = FALSE;
3865 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3869 gchar *end_of_valid_int;
3874 long_value = strtol (value, &end_of_valid_int, 10);
3876 if (*value == '\0' || *end_of_valid_int != '\0')
3878 gchar *value_utf8 = _g_utf8_make_valid (value);
3879 g_set_error (error, G_KEY_FILE_ERROR,
3880 G_KEY_FILE_ERROR_INVALID_VALUE,
3881 _("Value '%s' cannot be interpreted "
3882 "as a number."), value_utf8);
3883 g_free (value_utf8);
3888 int_value = long_value;
3889 if (int_value != long_value || errno == ERANGE)
3891 gchar *value_utf8 = _g_utf8_make_valid (value);
3894 G_KEY_FILE_ERROR_INVALID_VALUE,
3895 _("Integer value '%s' out of range"),
3897 g_free (value_utf8);
3906 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3910 return g_strdup_printf ("%d", value);
3914 g_key_file_parse_value_as_double (GKeyFile *key_file,
3918 gchar *end_of_valid_d;
3919 gdouble double_value = 0;
3921 double_value = g_ascii_strtod (value, &end_of_valid_d);
3923 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3925 gchar *value_utf8 = _g_utf8_make_valid (value);
3926 g_set_error (error, G_KEY_FILE_ERROR,
3927 G_KEY_FILE_ERROR_INVALID_VALUE,
3928 _("Value '%s' cannot be interpreted "
3929 "as a float number."),
3931 g_free (value_utf8);
3934 return double_value;
3938 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3944 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3946 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3949 value_utf8 = _g_utf8_make_valid (value);
3950 g_set_error (error, G_KEY_FILE_ERROR,
3951 G_KEY_FILE_ERROR_INVALID_VALUE,
3952 _("Value '%s' cannot be interpreted "
3953 "as a boolean."), value_utf8);
3954 g_free (value_utf8);
3960 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3964 return g_strdup ("true");
3966 return g_strdup ("false");
3970 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3977 string = g_string_sized_new (512);
3979 lines = g_strsplit (value, "\n", 0);
3981 for (i = 0; lines[i] != NULL; i++)
3983 if (lines[i][0] != '#')
3984 g_string_append_printf (string, "%s\n", lines[i]);
3986 g_string_append_printf (string, "%s\n", lines[i] + 1);
3990 return g_string_free (string, FALSE);
3994 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3995 const gchar *comment)
4001 string = g_string_sized_new (512);
4003 lines = g_strsplit (comment, "\n", 0);
4005 for (i = 0; lines[i] != NULL; i++)
4006 g_string_append_printf (string, "#%s%s", lines[i],
4007 lines[i + 1] == NULL? "" : "\n");
4010 return g_string_free (string, FALSE);