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 (GKeyFile *key_file,
143 GKeyFileGroup *group,
146 static void g_key_file_add_group (GKeyFile *key_file,
147 const gchar *group_name);
148 static gboolean g_key_file_is_group_name (const gchar *name);
149 static gboolean g_key_file_is_key_name (const gchar *name);
150 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
151 static gboolean g_key_file_line_is_comment (const gchar *line);
152 static gboolean g_key_file_line_is_group (const gchar *line);
153 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
154 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
158 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
160 gboolean escape_separator);
161 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
164 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
166 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
169 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
172 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
174 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
176 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
177 const gchar *comment);
178 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
182 static void g_key_file_parse_comment (GKeyFile *key_file,
186 static void g_key_file_parse_group (GKeyFile *key_file,
190 static gchar *key_get_locale (const gchar *key);
191 static void g_key_file_parse_data (GKeyFile *key_file,
195 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
200 g_key_file_error_quark (void)
202 return g_quark_from_static_string ("g-key-file-error-quark");
206 g_key_file_init (GKeyFile *key_file)
208 key_file->current_group = g_slice_new0 (GKeyFileGroup);
209 key_file->groups = g_list_prepend (NULL, key_file->current_group);
210 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
211 key_file->start_group = NULL;
212 key_file->parse_buffer = g_string_sized_new (128);
213 key_file->approximate_size = 0;
214 key_file->list_separator = ';';
216 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
220 g_key_file_clear (GKeyFile *key_file)
222 GList *tmp, *group_node;
224 if (key_file->locales)
226 g_strfreev (key_file->locales);
227 key_file->locales = NULL;
230 if (key_file->parse_buffer)
232 g_string_free (key_file->parse_buffer, TRUE);
233 key_file->parse_buffer = NULL;
236 tmp = key_file->groups;
241 g_key_file_remove_group_node (key_file, group_node);
244 g_hash_table_destroy (key_file->group_hash);
245 key_file->group_hash = NULL;
247 g_warn_if_fail (key_file->groups == NULL);
254 * Creates a new empty #GKeyFile object. Use
255 * g_key_file_load_from_file(), g_key_file_load_from_data(),
256 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
257 * read an existing key file.
259 * Return value: an empty #GKeyFile.
264 g_key_file_new (void)
268 key_file = g_slice_new0 (GKeyFile);
269 g_key_file_init (key_file);
275 * g_key_file_set_list_separator:
276 * @key_file: a #GKeyFile
277 * @separator: the separator
279 * Sets the character which is used to separate
280 * values in lists. Typically ';' or ',' are used
281 * as separators. The default list separator is ';'.
286 g_key_file_set_list_separator (GKeyFile *key_file,
289 g_return_if_fail (key_file != NULL);
291 key_file->list_separator = separator;
295 /* Iterates through all the directories in *dirs trying to
296 * open file. When it successfully locates and opens a file it
297 * returns the file descriptor to the open file. It also
298 * outputs the absolute path of the file in output_file.
301 find_file_in_data_dirs (const gchar *file,
306 const gchar **data_dirs, *data_dir;
318 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
320 gchar *candidate_file, *sub_dir;
322 candidate_file = (gchar *) file;
323 sub_dir = g_strdup ("");
324 while (candidate_file != NULL && fd < 0)
328 path = g_build_filename (data_dir, sub_dir,
329 candidate_file, NULL);
331 fd = g_open (path, O_RDONLY, 0);
339 candidate_file = strchr (candidate_file, '-');
341 if (candidate_file == NULL)
347 sub_dir = g_strndup (file, candidate_file - file - 1);
349 for (p = sub_dir; *p != '\0'; p++)
352 *p = G_DIR_SEPARATOR;
361 g_set_error_literal (error, G_KEY_FILE_ERROR,
362 G_KEY_FILE_ERROR_NOT_FOUND,
363 _("Valid key file could not be "
364 "found in search dirs"));
367 if (output_file != NULL && fd > 0)
368 *output_file = g_strdup (path);
376 g_key_file_load_from_fd (GKeyFile *key_file,
381 GError *key_file_error = NULL;
383 struct stat stat_buf;
384 gchar read_buf[4096];
386 if (fstat (fd, &stat_buf) < 0)
388 g_set_error_literal (error, G_FILE_ERROR,
389 g_file_error_from_errno (errno),
394 if (!S_ISREG (stat_buf.st_mode))
396 g_set_error_literal (error, G_KEY_FILE_ERROR,
397 G_KEY_FILE_ERROR_PARSE,
398 _("Not a regular file"));
402 if (stat_buf.st_size == 0)
404 g_set_error_literal (error, G_KEY_FILE_ERROR,
405 G_KEY_FILE_ERROR_PARSE,
410 if (key_file->approximate_size > 0)
412 g_key_file_clear (key_file);
413 g_key_file_init (key_file);
415 key_file->flags = flags;
419 bytes_read = read (fd, read_buf, 4096);
421 if (bytes_read == 0) /* End of File */
426 if (errno == EINTR || errno == EAGAIN)
429 g_set_error_literal (error, G_FILE_ERROR,
430 g_file_error_from_errno (errno),
435 g_key_file_parse_data (key_file,
436 read_buf, bytes_read,
439 while (!key_file_error);
443 g_propagate_error (error, key_file_error);
447 g_key_file_flush_parse_buffer (key_file, &key_file_error);
451 g_propagate_error (error, key_file_error);
459 * g_key_file_load_from_file:
460 * @key_file: an empty #GKeyFile struct
461 * @file: the path of a filename to load, in the GLib filename encoding
462 * @flags: flags from #GKeyFileFlags
463 * @error: return location for a #GError, or %NULL
465 * Loads a key file into an empty #GKeyFile structure.
466 * If the file could not be loaded then %error is set to
467 * either a #GFileError or #GKeyFileError.
469 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
474 g_key_file_load_from_file (GKeyFile *key_file,
479 GError *key_file_error = NULL;
482 g_return_val_if_fail (key_file != NULL, FALSE);
483 g_return_val_if_fail (file != NULL, FALSE);
485 fd = g_open (file, O_RDONLY, 0);
489 g_set_error_literal (error, G_FILE_ERROR,
490 g_file_error_from_errno (errno),
495 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
500 g_propagate_error (error, key_file_error);
508 * g_key_file_load_from_data:
509 * @key_file: an empty #GKeyFile struct
510 * @data: key file loaded in memory
511 * @length: the length of @data in bytes
512 * @flags: flags from #GKeyFileFlags
513 * @error: return location for a #GError, or %NULL
515 * Loads a key file from memory into an empty #GKeyFile structure.
516 * If the object cannot be created then %error is set to a #GKeyFileError.
518 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
523 g_key_file_load_from_data (GKeyFile *key_file,
529 GError *key_file_error = NULL;
531 g_return_val_if_fail (key_file != NULL, FALSE);
532 g_return_val_if_fail (data != NULL, FALSE);
533 g_return_val_if_fail (length != 0, FALSE);
535 if (length == (gsize)-1)
536 length = strlen (data);
538 if (key_file->approximate_size > 0)
540 g_key_file_clear (key_file);
541 g_key_file_init (key_file);
543 key_file->flags = flags;
545 g_key_file_parse_data (key_file, data, length, &key_file_error);
549 g_propagate_error (error, key_file_error);
553 g_key_file_flush_parse_buffer (key_file, &key_file_error);
557 g_propagate_error (error, key_file_error);
565 * g_key_file_load_from_dirs:
566 * @key_file: an empty #GKeyFile struct
567 * @file: a relative path to a filename to open and parse
568 * @search_dirs: %NULL-terminated array of directories to search
569 * @full_path: return location for a string containing the full path
570 * of the file, or %NULL
571 * @flags: flags from #GKeyFileFlags
572 * @error: return location for a #GError, or %NULL
574 * This function looks for a key file named @file in the paths
575 * specified in @search_dirs, loads the file into @key_file and
576 * returns the file's full path in @full_path. If the file could not
577 * be loaded then an %error is set to either a #GFileError or
580 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
585 g_key_file_load_from_dirs (GKeyFile *key_file,
587 const gchar **search_dirs,
592 GError *key_file_error = NULL;
593 const gchar **data_dirs;
598 g_return_val_if_fail (key_file != NULL, FALSE);
599 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
600 g_return_val_if_fail (search_dirs != NULL, FALSE);
603 data_dirs = search_dirs;
605 while (*data_dirs != NULL && !found_file)
607 g_free (output_path);
609 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
615 g_propagate_error (error, key_file_error);
619 found_file = g_key_file_load_from_fd (key_file, fd, flags,
625 g_propagate_error (error, key_file_error);
630 if (found_file && full_path)
631 *full_path = output_path;
633 g_free (output_path);
639 * g_key_file_load_from_data_dirs:
640 * @key_file: an empty #GKeyFile struct
641 * @file: a relative path to a filename to open and parse
642 * @full_path: return location for a string containing the full path
643 * of the file, or %NULL
644 * @flags: flags from #GKeyFileFlags
645 * @error: return location for a #GError, or %NULL
647 * This function looks for a key file named @file in the paths
648 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
649 * loads the file into @key_file and returns the file's full path in
650 * @full_path. If the file could not be loaded then an %error is
651 * set to either a #GFileError or #GKeyFileError.
653 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
657 g_key_file_load_from_data_dirs (GKeyFile *key_file,
663 gchar **all_data_dirs;
664 const gchar * user_data_dir;
665 const gchar * const * system_data_dirs;
669 g_return_val_if_fail (key_file != NULL, FALSE);
670 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
672 user_data_dir = g_get_user_data_dir ();
673 system_data_dirs = g_get_system_data_dirs ();
674 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
677 all_data_dirs[i++] = g_strdup (user_data_dir);
680 while (system_data_dirs[j] != NULL)
681 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
682 all_data_dirs[i] = NULL;
684 found_file = g_key_file_load_from_dirs (key_file,
686 (const gchar **)all_data_dirs,
691 g_strfreev (all_data_dirs);
698 * @key_file: a #GKeyFile
705 g_key_file_free (GKeyFile *key_file)
707 g_return_if_fail (key_file != NULL);
709 g_key_file_clear (key_file);
710 g_slice_free (GKeyFile, key_file);
713 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
714 * true for locales that match those in g_get_language_names().
717 g_key_file_locale_is_interesting (GKeyFile *key_file,
722 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
725 for (i = 0; key_file->locales[i] != NULL; i++)
727 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
735 g_key_file_parse_line (GKeyFile *key_file,
740 GError *parse_error = NULL;
743 g_return_if_fail (key_file != NULL);
744 g_return_if_fail (line != NULL);
746 line_start = (gchar *) line;
747 while (g_ascii_isspace (*line_start))
750 if (g_key_file_line_is_comment (line_start))
751 g_key_file_parse_comment (key_file, line, length, &parse_error);
752 else if (g_key_file_line_is_group (line_start))
753 g_key_file_parse_group (key_file, line_start,
754 length - (line_start - line),
756 else if (g_key_file_line_is_key_value_pair (line_start))
757 g_key_file_parse_key_value_pair (key_file, line_start,
758 length - (line_start - line),
762 gchar *line_utf8 = _g_utf8_make_valid (line);
763 g_set_error (error, G_KEY_FILE_ERROR,
764 G_KEY_FILE_ERROR_PARSE,
765 _("Key file contains line '%s' which is not "
766 "a key-value pair, group, or comment"),
774 g_propagate_error (error, parse_error);
778 g_key_file_parse_comment (GKeyFile *key_file,
783 GKeyFileKeyValuePair *pair;
785 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
788 g_warn_if_fail (key_file->current_group != NULL);
790 pair = g_slice_new (GKeyFileKeyValuePair);
792 pair->value = g_strndup (line, length);
794 key_file->current_group->key_value_pairs =
795 g_list_prepend (key_file->current_group->key_value_pairs, pair);
797 if (length == 0 || line[0] != '#')
798 key_file->current_group->has_trailing_blank_line = TRUE;
802 g_key_file_parse_group (GKeyFile *key_file,
808 const gchar *group_name_start, *group_name_end;
810 /* advance past opening '['
812 group_name_start = line + 1;
813 group_name_end = line + length - 1;
815 while (*group_name_end != ']')
818 group_name = g_strndup (group_name_start,
819 group_name_end - group_name_start);
821 if (!g_key_file_is_group_name (group_name))
823 g_set_error (error, G_KEY_FILE_ERROR,
824 G_KEY_FILE_ERROR_PARSE,
825 _("Invalid group name: %s"), group_name);
830 g_key_file_add_group (key_file, group_name);
835 g_key_file_parse_key_value_pair (GKeyFile *key_file,
840 gchar *key, *value, *key_end, *value_start, *locale;
841 gsize key_len, value_len;
843 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
845 g_set_error_literal (error, G_KEY_FILE_ERROR,
846 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
847 _("Key file does not start with a group"));
851 key_end = value_start = strchr (line, '=');
853 g_warn_if_fail (key_end != NULL);
858 /* Pull the key name from the line (chomping trailing whitespace)
860 while (g_ascii_isspace (*key_end))
863 key_len = key_end - line + 2;
865 g_warn_if_fail (key_len <= length);
867 key = g_strndup (line, key_len - 1);
869 if (!g_key_file_is_key_name (key))
871 g_set_error (error, G_KEY_FILE_ERROR,
872 G_KEY_FILE_ERROR_PARSE,
873 _("Invalid key name: %s"), key);
878 /* Pull the value from the line (chugging leading whitespace)
880 while (g_ascii_isspace (*value_start))
883 value_len = line + length - value_start + 1;
885 value = g_strndup (value_start, value_len);
887 g_warn_if_fail (key_file->start_group != NULL);
889 if (key_file->current_group
890 && key_file->current_group->name
891 && strcmp (key_file->start_group->name,
892 key_file->current_group->name) == 0
893 && strcmp (key, "Encoding") == 0)
895 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
897 gchar *value_utf8 = _g_utf8_make_valid (value);
898 g_set_error (error, G_KEY_FILE_ERROR,
899 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
900 _("Key file contains unsupported "
901 "encoding '%s'"), value_utf8);
910 /* Is this key a translation? If so, is it one that we care about?
912 locale = key_get_locale (key);
914 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
915 g_key_file_add_key (key_file, key_file->current_group, key, value);
923 key_get_locale (const gchar *key)
927 locale = g_strrstr (key, "[");
929 if (locale && strlen (locale) <= 2)
933 locale = g_strndup (locale + 1, strlen (locale) - 2);
939 g_key_file_parse_data (GKeyFile *key_file,
947 g_return_if_fail (key_file != NULL);
948 g_return_if_fail (data != NULL);
952 for (i = 0; i < length; i++)
956 if (key_file->parse_buffer->len > 0
957 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
959 g_string_erase (key_file->parse_buffer,
960 key_file->parse_buffer->len - 1,
963 /* When a newline is encountered flush the parse buffer so that the
964 * line can be parsed. Note that completely blank lines won't show
965 * up in the parse buffer, so they get parsed directly.
967 if (key_file->parse_buffer->len > 0)
968 g_key_file_flush_parse_buffer (key_file, &parse_error);
970 g_key_file_parse_comment (key_file, "", 1, &parse_error);
974 g_propagate_error (error, parse_error);
979 g_string_append_c (key_file->parse_buffer, data[i]);
982 key_file->approximate_size += length;
986 g_key_file_flush_parse_buffer (GKeyFile *key_file,
989 GError *file_error = NULL;
991 g_return_if_fail (key_file != NULL);
995 if (key_file->parse_buffer->len > 0)
997 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
998 key_file->parse_buffer->len,
1000 g_string_erase (key_file->parse_buffer, 0, -1);
1004 g_propagate_error (error, file_error);
1011 * g_key_file_to_data:
1012 * @key_file: a #GKeyFile
1013 * @length: return location for the length of the
1014 * returned string, or %NULL
1015 * @error: return location for a #GError, or %NULL
1017 * This function outputs @key_file as a string.
1019 * Note that this function never reports an error,
1020 * so it is safe to pass %NULL as @error.
1022 * Return value: a newly allocated string holding
1023 * the contents of the #GKeyFile
1028 g_key_file_to_data (GKeyFile *key_file,
1032 GString *data_string;
1033 GList *group_node, *key_file_node;
1034 gboolean has_blank_line = TRUE;
1036 g_return_val_if_fail (key_file != NULL, NULL);
1038 data_string = g_string_sized_new (2 * key_file->approximate_size);
1040 for (group_node = g_list_last (key_file->groups);
1042 group_node = group_node->prev)
1044 GKeyFileGroup *group;
1046 group = (GKeyFileGroup *) group_node->data;
1048 /* separate groups by at least an empty line */
1049 if (!has_blank_line)
1050 g_string_append_c (data_string, '\n');
1051 has_blank_line = group->has_trailing_blank_line;
1053 if (group->comment != NULL)
1054 g_string_append_printf (data_string, "%s\n", group->comment->value);
1056 if (group->name != NULL)
1057 g_string_append_printf (data_string, "[%s]\n", group->name);
1059 for (key_file_node = g_list_last (group->key_value_pairs);
1060 key_file_node != NULL;
1061 key_file_node = key_file_node->prev)
1063 GKeyFileKeyValuePair *pair;
1065 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1067 if (pair->key != NULL)
1068 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1070 g_string_append_printf (data_string, "%s\n", pair->value);
1075 *length = data_string->len;
1077 return g_string_free (data_string, FALSE);
1081 * g_key_file_get_keys:
1082 * @key_file: a #GKeyFile
1083 * @group_name: a group name
1084 * @length: return location for the number of keys returned, or %NULL
1085 * @error: return location for a #GError, or %NULL
1087 * Returns all keys for the group name @group_name. The array of
1088 * returned keys will be %NULL-terminated, so @length may
1089 * optionally be %NULL. In the event that the @group_name cannot
1090 * be found, %NULL is returned and @error is set to
1091 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1093 * Return value: a newly-allocated %NULL-terminated array of strings.
1094 * Use g_strfreev() to free it.
1099 g_key_file_get_keys (GKeyFile *key_file,
1100 const gchar *group_name,
1104 GKeyFileGroup *group;
1109 g_return_val_if_fail (key_file != NULL, NULL);
1110 g_return_val_if_fail (group_name != NULL, NULL);
1112 group = g_key_file_lookup_group (key_file, group_name);
1116 g_set_error (error, G_KEY_FILE_ERROR,
1117 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1118 _("Key file does not have group '%s'"),
1119 group_name ? group_name : "(null)");
1124 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1126 GKeyFileKeyValuePair *pair;
1128 pair = (GKeyFileKeyValuePair *) tmp->data;
1134 keys = g_new (gchar *, num_keys + 1);
1137 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1139 GKeyFileKeyValuePair *pair;
1141 pair = (GKeyFileKeyValuePair *) tmp->data;
1145 keys[i] = g_strdup (pair->key);
1150 keys[num_keys] = NULL;
1159 * g_key_file_get_start_group:
1160 * @key_file: a #GKeyFile
1162 * Returns the name of the start group of the file.
1164 * Return value: The start group of the key file.
1169 g_key_file_get_start_group (GKeyFile *key_file)
1171 g_return_val_if_fail (key_file != NULL, NULL);
1173 if (key_file->start_group)
1174 return g_strdup (key_file->start_group->name);
1180 * g_key_file_get_groups:
1181 * @key_file: a #GKeyFile
1182 * @length: return location for the number of returned groups, or %NULL
1184 * Returns all groups in the key file loaded with @key_file.
1185 * The array of returned groups will be %NULL-terminated, so
1186 * @length may optionally be %NULL.
1188 * Return value: a newly-allocated %NULL-terminated array of strings.
1189 * Use g_strfreev() to free it.
1193 g_key_file_get_groups (GKeyFile *key_file,
1198 gsize i, num_groups;
1200 g_return_val_if_fail (key_file != NULL, NULL);
1202 num_groups = g_list_length (key_file->groups);
1204 g_return_val_if_fail (num_groups > 0, NULL);
1206 group_node = g_list_last (key_file->groups);
1208 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1210 /* Only need num_groups instead of num_groups + 1
1211 * because the first group of the file (last in the
1212 * list) is always the comment group at the top,
1215 groups = g_new (gchar *, num_groups);
1219 for (group_node = group_node->prev;
1221 group_node = group_node->prev)
1223 GKeyFileGroup *group;
1225 group = (GKeyFileGroup *) group_node->data;
1227 g_warn_if_fail (group->name != NULL);
1229 groups[i++] = g_strdup (group->name);
1240 * g_key_file_get_value:
1241 * @key_file: a #GKeyFile
1242 * @group_name: a group name
1244 * @error: return location for a #GError, or %NULL
1246 * Returns the raw value associated with @key under @group_name.
1247 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1249 * In the event the key cannot be found, %NULL is returned and
1250 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1251 * event that the @group_name cannot be found, %NULL is returned
1252 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1255 * Return value: a newly allocated string or %NULL if the specified
1256 * key cannot be found.
1261 g_key_file_get_value (GKeyFile *key_file,
1262 const gchar *group_name,
1266 GKeyFileGroup *group;
1267 GKeyFileKeyValuePair *pair;
1268 gchar *value = NULL;
1270 g_return_val_if_fail (key_file != NULL, NULL);
1271 g_return_val_if_fail (group_name != NULL, NULL);
1272 g_return_val_if_fail (key != NULL, NULL);
1274 group = g_key_file_lookup_group (key_file, group_name);
1278 g_set_error (error, G_KEY_FILE_ERROR,
1279 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1280 _("Key file does not have group '%s'"),
1281 group_name ? group_name : "(null)");
1285 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1288 value = g_strdup (pair->value);
1290 g_set_error (error, G_KEY_FILE_ERROR,
1291 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1292 _("Key file does not have key '%s'"), key);
1298 * g_key_file_set_value:
1299 * @key_file: a #GKeyFile
1300 * @group_name: a group name
1304 * Associates a new value with @key under @group_name.
1306 * If @key cannot be found then it is created. If @group_name cannot
1307 * be found then it is created. To set an UTF-8 string which may contain
1308 * characters that need escaping (such as newlines or spaces), use
1309 * g_key_file_set_string().
1314 g_key_file_set_value (GKeyFile *key_file,
1315 const gchar *group_name,
1319 GKeyFileGroup *group;
1320 GKeyFileKeyValuePair *pair;
1322 g_return_if_fail (key_file != NULL);
1323 g_return_if_fail (g_key_file_is_group_name (group_name));
1324 g_return_if_fail (g_key_file_is_key_name (key));
1325 g_return_if_fail (value != NULL);
1327 group = g_key_file_lookup_group (key_file, group_name);
1331 g_key_file_add_group (key_file, group_name);
1332 group = (GKeyFileGroup *) key_file->groups->data;
1334 g_key_file_add_key (key_file, group, key, value);
1338 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1341 g_key_file_add_key (key_file, group, key, value);
1344 g_free (pair->value);
1345 pair->value = g_strdup (value);
1351 * g_key_file_get_string:
1352 * @key_file: a #GKeyFile
1353 * @group_name: a group name
1355 * @error: return location for a #GError, or %NULL
1357 * Returns the string value associated with @key under @group_name.
1358 * Unlike g_key_file_get_value(), this function handles escape sequences
1361 * In the event the key cannot be found, %NULL is returned and
1362 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1363 * event that the @group_name cannot be found, %NULL is returned
1364 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1366 * Return value: a newly allocated string or %NULL if the specified
1367 * key cannot be found.
1372 g_key_file_get_string (GKeyFile *key_file,
1373 const gchar *group_name,
1377 gchar *value, *string_value;
1378 GError *key_file_error;
1380 g_return_val_if_fail (key_file != NULL, NULL);
1381 g_return_val_if_fail (group_name != NULL, NULL);
1382 g_return_val_if_fail (key != NULL, NULL);
1384 key_file_error = NULL;
1386 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1390 g_propagate_error (error, key_file_error);
1394 if (!g_utf8_validate (value, -1, NULL))
1396 gchar *value_utf8 = _g_utf8_make_valid (value);
1397 g_set_error (error, G_KEY_FILE_ERROR,
1398 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1399 _("Key file contains key '%s' with value '%s' "
1400 "which is not UTF-8"), key, value_utf8);
1401 g_free (value_utf8);
1407 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1413 if (g_error_matches (key_file_error,
1415 G_KEY_FILE_ERROR_INVALID_VALUE))
1417 g_set_error (error, G_KEY_FILE_ERROR,
1418 G_KEY_FILE_ERROR_INVALID_VALUE,
1419 _("Key file contains key '%s' "
1420 "which has value that cannot be interpreted."),
1422 g_error_free (key_file_error);
1425 g_propagate_error (error, key_file_error);
1428 return string_value;
1432 * g_key_file_set_string:
1433 * @key_file: a #GKeyFile
1434 * @group_name: a group name
1438 * Associates a new string value with @key under @group_name.
1439 * If @key cannot be found then it is created.
1440 * If @group_name cannot be found then it is created.
1441 * Unlike g_key_file_set_value(), this function handles characters
1442 * that need escaping, such as newlines.
1447 g_key_file_set_string (GKeyFile *key_file,
1448 const gchar *group_name,
1450 const gchar *string)
1454 g_return_if_fail (key_file != NULL);
1455 g_return_if_fail (string != NULL);
1457 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1458 g_key_file_set_value (key_file, group_name, key, value);
1463 * g_key_file_get_string_list:
1464 * @key_file: a #GKeyFile
1465 * @group_name: a group name
1467 * @length: return location for the number of returned strings, or %NULL
1468 * @error: return location for a #GError, or %NULL
1470 * Returns the values associated with @key under @group_name.
1472 * In the event the key cannot be found, %NULL is returned and
1473 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1474 * event that the @group_name cannot be found, %NULL is returned
1475 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1477 * Return value: a %NULL-terminated string array or %NULL if the specified
1478 * key cannot be found. The array should be freed with g_strfreev().
1483 g_key_file_get_string_list (GKeyFile *key_file,
1484 const gchar *group_name,
1489 GError *key_file_error = NULL;
1490 gchar *value, *string_value, **values;
1492 GSList *p, *pieces = NULL;
1494 g_return_val_if_fail (key_file != NULL, NULL);
1495 g_return_val_if_fail (group_name != NULL, NULL);
1496 g_return_val_if_fail (key != NULL, NULL);
1501 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1505 g_propagate_error (error, key_file_error);
1509 if (!g_utf8_validate (value, -1, NULL))
1511 gchar *value_utf8 = _g_utf8_make_valid (value);
1512 g_set_error (error, G_KEY_FILE_ERROR,
1513 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1514 _("Key file contains key '%s' with value '%s' "
1515 "which is not UTF-8"), key, value_utf8);
1516 g_free (value_utf8);
1522 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1524 g_free (string_value);
1528 if (g_error_matches (key_file_error,
1530 G_KEY_FILE_ERROR_INVALID_VALUE))
1532 g_set_error (error, G_KEY_FILE_ERROR,
1533 G_KEY_FILE_ERROR_INVALID_VALUE,
1534 _("Key file contains key '%s' "
1535 "which has a value that cannot be interpreted."),
1537 g_error_free (key_file_error);
1540 g_propagate_error (error, key_file_error);
1545 len = g_slist_length (pieces);
1546 values = g_new (gchar *, len + 1);
1547 for (p = pieces, i = 0; p; p = p->next)
1548 values[i++] = p->data;
1551 g_slist_free (pieces);
1560 * g_key_file_set_string_list:
1561 * @key_file: a #GKeyFile
1562 * @group_name: a group name
1564 * @list: an array of string values
1565 * @length: number of string values in @list
1567 * Associates a list of string values for @key under @group_name.
1568 * If @key cannot be found then it is created.
1569 * If @group_name cannot be found then it is created.
1574 g_key_file_set_string_list (GKeyFile *key_file,
1575 const gchar *group_name,
1577 const gchar * const list[],
1580 GString *value_list;
1583 g_return_if_fail (key_file != NULL);
1584 g_return_if_fail (list != NULL || length == 0);
1586 value_list = g_string_sized_new (length * 128);
1587 for (i = 0; i < length && list[i] != NULL; i++)
1591 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1592 g_string_append (value_list, value);
1593 g_string_append_c (value_list, key_file->list_separator);
1598 g_key_file_set_value (key_file, group_name, key, value_list->str);
1599 g_string_free (value_list, TRUE);
1603 * g_key_file_set_locale_string:
1604 * @key_file: a #GKeyFile
1605 * @group_name: a group name
1607 * @locale: a locale identifier
1610 * Associates a string value for @key and @locale under @group_name.
1611 * If the translation for @key cannot be found then it is created.
1616 g_key_file_set_locale_string (GKeyFile *key_file,
1617 const gchar *group_name,
1619 const gchar *locale,
1620 const gchar *string)
1622 gchar *full_key, *value;
1624 g_return_if_fail (key_file != NULL);
1625 g_return_if_fail (key != NULL);
1626 g_return_if_fail (locale != NULL);
1627 g_return_if_fail (string != NULL);
1629 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1630 full_key = g_strdup_printf ("%s[%s]", key, locale);
1631 g_key_file_set_value (key_file, group_name, full_key, value);
1637 * g_key_file_get_locale_string:
1638 * @key_file: a #GKeyFile
1639 * @group_name: a group name
1641 * @locale: a locale identifier or %NULL
1642 * @error: return location for a #GError, or %NULL
1644 * Returns the value associated with @key under @group_name
1645 * translated in the given @locale if available. If @locale is
1646 * %NULL then the current locale is assumed.
1648 * If @key cannot be found then %NULL is returned and @error is set
1649 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1650 * with @key cannot be interpreted or no suitable translation can
1651 * be found then the untranslated value is returned.
1653 * Return value: a newly allocated string or %NULL if the specified
1654 * key cannot be found.
1659 g_key_file_get_locale_string (GKeyFile *key_file,
1660 const gchar *group_name,
1662 const gchar *locale,
1665 gchar *candidate_key, *translated_value;
1666 GError *key_file_error;
1668 gboolean free_languages = FALSE;
1671 g_return_val_if_fail (key_file != NULL, NULL);
1672 g_return_val_if_fail (group_name != NULL, NULL);
1673 g_return_val_if_fail (key != NULL, NULL);
1675 candidate_key = NULL;
1676 translated_value = NULL;
1677 key_file_error = NULL;
1681 languages = g_get_locale_variants (locale);
1682 free_languages = TRUE;
1686 languages = (gchar **) g_get_language_names ();
1687 free_languages = FALSE;
1690 for (i = 0; languages[i]; i++)
1692 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1694 translated_value = g_key_file_get_string (key_file,
1696 candidate_key, NULL);
1697 g_free (candidate_key);
1699 if (translated_value)
1702 g_free (translated_value);
1703 translated_value = NULL;
1706 /* Fallback to untranslated key
1708 if (!translated_value)
1710 translated_value = g_key_file_get_string (key_file, group_name, key,
1713 if (!translated_value)
1714 g_propagate_error (error, key_file_error);
1718 g_strfreev (languages);
1720 return translated_value;
1724 * g_key_file_get_locale_string_list:
1725 * @key_file: a #GKeyFile
1726 * @group_name: a group name
1728 * @locale: a locale identifier or %NULL
1729 * @length: return location for the number of returned strings or %NULL
1730 * @error: return location for a #GError or %NULL
1732 * Returns the values associated with @key under @group_name
1733 * translated in the given @locale if available. If @locale is
1734 * %NULL then the current locale is assumed.
1736 * If @key cannot be found then %NULL is returned and @error is set
1737 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1738 * with @key cannot be interpreted or no suitable translations
1739 * can be found then the untranslated values are returned. The
1740 * returned array is %NULL-terminated, so @length may optionally
1743 * Return value: a newly allocated %NULL-terminated string array
1744 * or %NULL if the key isn't found. The string array should be freed
1745 * with g_strfreev().
1750 g_key_file_get_locale_string_list (GKeyFile *key_file,
1751 const gchar *group_name,
1753 const gchar *locale,
1757 GError *key_file_error;
1758 gchar **values, *value;
1759 char list_separator[2];
1762 g_return_val_if_fail (key_file != NULL, NULL);
1763 g_return_val_if_fail (group_name != NULL, NULL);
1764 g_return_val_if_fail (key != NULL, NULL);
1766 key_file_error = NULL;
1768 value = g_key_file_get_locale_string (key_file, group_name,
1773 g_propagate_error (error, key_file_error);
1782 len = strlen (value);
1783 if (value[len - 1] == key_file->list_separator)
1784 value[len - 1] = '\0';
1786 list_separator[0] = key_file->list_separator;
1787 list_separator[1] = '\0';
1788 values = g_strsplit (value, list_separator, 0);
1793 *length = g_strv_length (values);
1799 * g_key_file_set_locale_string_list:
1800 * @key_file: a #GKeyFile
1801 * @group_name: a group name
1803 * @locale: a locale identifier
1804 * @list: a %NULL-terminated array of locale string values
1805 * @length: the length of @list
1807 * Associates a list of string values for @key and @locale under
1808 * @group_name. If the translation for @key cannot be found then
1814 g_key_file_set_locale_string_list (GKeyFile *key_file,
1815 const gchar *group_name,
1817 const gchar *locale,
1818 const gchar * const list[],
1821 GString *value_list;
1825 g_return_if_fail (key_file != NULL);
1826 g_return_if_fail (key != NULL);
1827 g_return_if_fail (locale != NULL);
1828 g_return_if_fail (length != 0);
1830 value_list = g_string_sized_new (length * 128);
1831 for (i = 0; i < length && list[i] != NULL; i++)
1835 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1836 g_string_append (value_list, value);
1837 g_string_append_c (value_list, key_file->list_separator);
1842 full_key = g_strdup_printf ("%s[%s]", key, locale);
1843 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1845 g_string_free (value_list, TRUE);
1849 * g_key_file_get_boolean:
1850 * @key_file: a #GKeyFile
1851 * @group_name: a group name
1853 * @error: return location for a #GError
1855 * Returns the value associated with @key under @group_name as a
1858 * If @key cannot be found then %FALSE is returned and @error is set
1859 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1860 * associated with @key cannot be interpreted as a boolean then %FALSE
1861 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1863 * Return value: the value associated with the key as a boolean,
1864 * or %FALSE if the key was not found or could not be parsed.
1869 g_key_file_get_boolean (GKeyFile *key_file,
1870 const gchar *group_name,
1874 GError *key_file_error = NULL;
1876 gboolean bool_value;
1878 g_return_val_if_fail (key_file != NULL, FALSE);
1879 g_return_val_if_fail (group_name != NULL, FALSE);
1880 g_return_val_if_fail (key != NULL, FALSE);
1882 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1886 g_propagate_error (error, key_file_error);
1890 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1896 if (g_error_matches (key_file_error,
1898 G_KEY_FILE_ERROR_INVALID_VALUE))
1900 g_set_error (error, G_KEY_FILE_ERROR,
1901 G_KEY_FILE_ERROR_INVALID_VALUE,
1902 _("Key file contains key '%s' "
1903 "which has value that cannot be interpreted."),
1905 g_error_free (key_file_error);
1908 g_propagate_error (error, key_file_error);
1915 * g_key_file_set_boolean:
1916 * @key_file: a #GKeyFile
1917 * @group_name: a group name
1919 * @value: %TRUE or %FALSE
1921 * Associates a new boolean value with @key under @group_name.
1922 * If @key cannot be found then it is created.
1927 g_key_file_set_boolean (GKeyFile *key_file,
1928 const gchar *group_name,
1934 g_return_if_fail (key_file != NULL);
1936 result = g_key_file_parse_boolean_as_value (key_file, value);
1937 g_key_file_set_value (key_file, group_name, key, result);
1942 * g_key_file_get_boolean_list:
1943 * @key_file: a #GKeyFile
1944 * @group_name: a group name
1946 * @length: the number of booleans returned
1947 * @error: return location for a #GError
1949 * Returns the values associated with @key under @group_name as
1952 * If @key cannot be found then %NULL is returned and @error is set to
1953 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1954 * with @key cannot be interpreted as booleans then %NULL is returned
1955 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1957 * Return value: the values associated with the key as a list of
1958 * booleans, or %NULL if the key was not found or could not be parsed.
1963 g_key_file_get_boolean_list (GKeyFile *key_file,
1964 const gchar *group_name,
1969 GError *key_file_error;
1971 gboolean *bool_values;
1974 g_return_val_if_fail (key_file != NULL, NULL);
1975 g_return_val_if_fail (group_name != NULL, NULL);
1976 g_return_val_if_fail (key != NULL, NULL);
1981 key_file_error = NULL;
1983 values = g_key_file_get_string_list (key_file, group_name, key,
1984 &num_bools, &key_file_error);
1987 g_propagate_error (error, key_file_error);
1992 bool_values = g_new (gboolean, num_bools);
1994 for (i = 0; i < num_bools; i++)
1996 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2002 g_propagate_error (error, key_file_error);
2003 g_strfreev (values);
2004 g_free (bool_values);
2009 g_strfreev (values);
2012 *length = num_bools;
2018 * g_key_file_set_boolean_list:
2019 * @key_file: a #GKeyFile
2020 * @group_name: a group name
2022 * @list: an array of boolean values
2023 * @length: length of @list
2025 * Associates a list of boolean values with @key under @group_name.
2026 * If @key cannot be found then it is created.
2027 * If @group_name is %NULL, the start_group is used.
2032 g_key_file_set_boolean_list (GKeyFile *key_file,
2033 const gchar *group_name,
2038 GString *value_list;
2041 g_return_if_fail (key_file != NULL);
2042 g_return_if_fail (list != NULL);
2044 value_list = g_string_sized_new (length * 8);
2045 for (i = 0; i < length; i++)
2049 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2051 g_string_append (value_list, value);
2052 g_string_append_c (value_list, key_file->list_separator);
2057 g_key_file_set_value (key_file, group_name, key, value_list->str);
2058 g_string_free (value_list, TRUE);
2062 * g_key_file_get_integer:
2063 * @key_file: a #GKeyFile
2064 * @group_name: a group name
2066 * @error: return location for a #GError
2068 * Returns the value associated with @key under @group_name as an
2071 * If @key cannot be found then 0 is returned and @error is set to
2072 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2073 * with @key cannot be interpreted as an integer then 0 is returned
2074 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2076 * Return value: the value associated with the key as an integer, or
2077 * 0 if the key was not found or could not be parsed.
2082 g_key_file_get_integer (GKeyFile *key_file,
2083 const gchar *group_name,
2087 GError *key_file_error;
2091 g_return_val_if_fail (key_file != NULL, -1);
2092 g_return_val_if_fail (group_name != NULL, -1);
2093 g_return_val_if_fail (key != NULL, -1);
2095 key_file_error = NULL;
2097 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2101 g_propagate_error (error, key_file_error);
2105 int_value = g_key_file_parse_value_as_integer (key_file, value,
2111 if (g_error_matches (key_file_error,
2113 G_KEY_FILE_ERROR_INVALID_VALUE))
2115 g_set_error (error, G_KEY_FILE_ERROR,
2116 G_KEY_FILE_ERROR_INVALID_VALUE,
2117 _("Key file contains key '%s' in group '%s' "
2118 "which has value that cannot be interpreted."), key,
2120 g_error_free (key_file_error);
2123 g_propagate_error (error, key_file_error);
2130 * g_key_file_set_integer:
2131 * @key_file: a #GKeyFile
2132 * @group_name: a group name
2134 * @value: an integer value
2136 * Associates a new integer value with @key under @group_name.
2137 * If @key cannot be found then it is created.
2142 g_key_file_set_integer (GKeyFile *key_file,
2143 const gchar *group_name,
2149 g_return_if_fail (key_file != NULL);
2151 result = g_key_file_parse_integer_as_value (key_file, value);
2152 g_key_file_set_value (key_file, group_name, key, result);
2157 * g_key_file_get_int64:
2158 * @key_file: a non-%NULL #GKeyFile
2159 * @group_name: a non-%NULL group name
2160 * @key: a non-%NULL key
2161 * @error: return location for a #GError
2163 * Returns the value associated with @key under @group_name as a signed
2164 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2165 * 64-bit results without truncation.
2167 * Returns: the value associated with the key as a signed 64-bit integer, or
2168 * 0 if the key was not found or could not be parsed.
2173 g_key_file_get_int64 (GKeyFile *key_file,
2174 const gchar *group_name,
2181 g_return_val_if_fail (key_file != NULL, -1);
2182 g_return_val_if_fail (group_name != NULL, -1);
2183 g_return_val_if_fail (key != NULL, -1);
2185 s = g_key_file_get_value (key_file, group_name, key, error);
2190 v = g_ascii_strtoll (s, &end, 10);
2192 if (*s == '\0' || *end != '\0')
2194 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2195 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2196 key, group_name, s);
2205 * g_key_file_set_int64:
2206 * @key_file: a #GKeyFile
2207 * @group_name: a group name
2209 * @value: an integer value
2211 * Associates a new integer value with @key under @group_name.
2212 * If @key cannot be found then it is created.
2217 g_key_file_set_int64 (GKeyFile *key_file,
2218 const gchar *group_name,
2224 g_return_if_fail (key_file != NULL);
2226 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2227 g_key_file_set_value (key_file, group_name, key, result);
2232 * g_key_file_get_uint64:
2233 * @key_file: a non-%NULL #GKeyFile
2234 * @group_name: a non-%NULL group name
2235 * @key: a non-%NULL key
2236 * @error: return location for a #GError
2238 * Returns the value associated with @key under @group_name as an unsigned
2239 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2240 * large positive results without truncation.
2242 * Returns: the value associated with the key as an unsigned 64-bit integer,
2243 * or 0 if the key was not found or could not be parsed.
2248 g_key_file_get_uint64 (GKeyFile *key_file,
2249 const gchar *group_name,
2256 g_return_val_if_fail (key_file != NULL, -1);
2257 g_return_val_if_fail (group_name != NULL, -1);
2258 g_return_val_if_fail (key != NULL, -1);
2260 s = g_key_file_get_value (key_file, group_name, key, error);
2265 v = g_ascii_strtoull (s, &end, 10);
2267 if (*s == '\0' || *end != '\0')
2269 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2270 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2271 key, group_name, s);
2280 * g_key_file_set_uint64:
2281 * @key_file: a #GKeyFile
2282 * @group_name: a group name
2284 * @value: an integer value
2286 * Associates a new integer value with @key under @group_name.
2287 * If @key cannot be found then it is created.
2292 g_key_file_set_uint64 (GKeyFile *key_file,
2293 const gchar *group_name,
2299 g_return_if_fail (key_file != NULL);
2301 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2302 g_key_file_set_value (key_file, group_name, key, result);
2307 * g_key_file_get_integer_list:
2308 * @key_file: a #GKeyFile
2309 * @group_name: a group name
2311 * @length: the number of integers returned
2312 * @error: return location for a #GError
2314 * Returns the values associated with @key under @group_name as
2317 * If @key cannot be found then %NULL is returned and @error is set to
2318 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2319 * with @key cannot be interpreted as integers then %NULL is returned
2320 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2322 * Return value: the values associated with the key as a list of
2323 * integers, or %NULL if the key was not found or could not be parsed.
2328 g_key_file_get_integer_list (GKeyFile *key_file,
2329 const gchar *group_name,
2334 GError *key_file_error = NULL;
2339 g_return_val_if_fail (key_file != NULL, NULL);
2340 g_return_val_if_fail (group_name != NULL, NULL);
2341 g_return_val_if_fail (key != NULL, NULL);
2346 values = g_key_file_get_string_list (key_file, group_name, key,
2347 &num_ints, &key_file_error);
2350 g_propagate_error (error, key_file_error);
2355 int_values = g_new (gint, num_ints);
2357 for (i = 0; i < num_ints; i++)
2359 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2365 g_propagate_error (error, key_file_error);
2366 g_strfreev (values);
2367 g_free (int_values);
2372 g_strfreev (values);
2381 * g_key_file_set_integer_list:
2382 * @key_file: a #GKeyFile
2383 * @group_name: a group name
2385 * @list: an array of integer values
2386 * @length: number of integer values in @list
2388 * Associates a list of integer values with @key under @group_name.
2389 * If @key cannot be found then it is created.
2394 g_key_file_set_integer_list (GKeyFile *key_file,
2395 const gchar *group_name,
2403 g_return_if_fail (key_file != NULL);
2404 g_return_if_fail (list != NULL);
2406 values = g_string_sized_new (length * 16);
2407 for (i = 0; i < length; i++)
2411 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2413 g_string_append (values, value);
2414 g_string_append_c (values, key_file->list_separator);
2419 g_key_file_set_value (key_file, group_name, key, values->str);
2420 g_string_free (values, TRUE);
2424 * g_key_file_get_double:
2425 * @key_file: a #GKeyFile
2426 * @group_name: a group name
2428 * @error: return location for a #GError
2430 * Returns the value associated with @key under @group_name as a
2431 * double. If @group_name is %NULL, the start_group is used.
2433 * If @key cannot be found then 0.0 is returned and @error is set to
2434 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2435 * with @key cannot be interpreted as a double then 0.0 is returned
2436 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2438 * Return value: the value associated with the key as a double, or
2439 * 0.0 if the key was not found or could not be parsed.
2444 g_key_file_get_double (GKeyFile *key_file,
2445 const gchar *group_name,
2449 GError *key_file_error;
2451 gdouble double_value;
2453 g_return_val_if_fail (key_file != NULL, -1);
2454 g_return_val_if_fail (group_name != NULL, -1);
2455 g_return_val_if_fail (key != NULL, -1);
2457 key_file_error = NULL;
2459 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2463 g_propagate_error (error, key_file_error);
2467 double_value = g_key_file_parse_value_as_double (key_file, value,
2473 if (g_error_matches (key_file_error,
2475 G_KEY_FILE_ERROR_INVALID_VALUE))
2477 g_set_error (error, G_KEY_FILE_ERROR,
2478 G_KEY_FILE_ERROR_INVALID_VALUE,
2479 _("Key file contains key '%s' in group '%s' "
2480 "which has value that cannot be interpreted."), key,
2482 g_error_free (key_file_error);
2485 g_propagate_error (error, key_file_error);
2488 return double_value;
2492 * g_key_file_set_double:
2493 * @key_file: a #GKeyFile
2494 * @group_name: a group name
2496 * @value: an double value
2498 * Associates a new double value with @key under @group_name.
2499 * If @key cannot be found then it is created.
2504 g_key_file_set_double (GKeyFile *key_file,
2505 const gchar *group_name,
2509 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2511 g_return_if_fail (key_file != NULL);
2513 g_ascii_dtostr (result, sizeof (result), value);
2514 g_key_file_set_value (key_file, group_name, key, result);
2518 * g_key_file_get_double_list:
2519 * @key_file: a #GKeyFile
2520 * @group_name: a group name
2522 * @length: the number of doubles returned
2523 * @error: return location for a #GError
2525 * Returns the values associated with @key under @group_name as
2528 * If @key cannot be found then %NULL is returned and @error is set to
2529 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2530 * with @key cannot be interpreted as doubles then %NULL is returned
2531 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2533 * Return value: the values associated with the key as a list of
2534 * doubles, or %NULL if the key was not found or could not be parsed.
2539 g_key_file_get_double_list (GKeyFile *key_file,
2540 const gchar *group_name,
2545 GError *key_file_error = NULL;
2547 gdouble *double_values;
2548 gsize i, num_doubles;
2550 g_return_val_if_fail (key_file != NULL, NULL);
2551 g_return_val_if_fail (group_name != NULL, NULL);
2552 g_return_val_if_fail (key != NULL, NULL);
2557 values = g_key_file_get_string_list (key_file, group_name, key,
2558 &num_doubles, &key_file_error);
2561 g_propagate_error (error, key_file_error);
2566 double_values = g_new (gdouble, num_doubles);
2568 for (i = 0; i < num_doubles; i++)
2570 double_values[i] = g_key_file_parse_value_as_double (key_file,
2576 g_propagate_error (error, key_file_error);
2577 g_strfreev (values);
2578 g_free (double_values);
2583 g_strfreev (values);
2586 *length = num_doubles;
2588 return double_values;
2592 * g_key_file_set_double_list:
2593 * @key_file: a #GKeyFile
2594 * @group_name: a group name
2596 * @list: an array of double values
2597 * @length: number of double values in @list
2599 * Associates a list of double values with @key under
2600 * @group_name. If @key cannot be found then it is created.
2605 g_key_file_set_double_list (GKeyFile *key_file,
2606 const gchar *group_name,
2614 g_return_if_fail (key_file != NULL);
2615 g_return_if_fail (list != NULL);
2617 values = g_string_sized_new (length * 16);
2618 for (i = 0; i < length; i++)
2620 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2622 g_ascii_dtostr( result, sizeof (result), list[i] );
2624 g_string_append (values, result);
2625 g_string_append_c (values, key_file->list_separator);
2628 g_key_file_set_value (key_file, group_name, key, values->str);
2629 g_string_free (values, TRUE);
2633 g_key_file_set_key_comment (GKeyFile *key_file,
2634 const gchar *group_name,
2636 const gchar *comment,
2639 GKeyFileGroup *group;
2640 GKeyFileKeyValuePair *pair;
2641 GList *key_node, *comment_node, *tmp;
2643 group = g_key_file_lookup_group (key_file, group_name);
2646 g_set_error (error, G_KEY_FILE_ERROR,
2647 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2648 _("Key file does not have group '%s'"),
2649 group_name ? group_name : "(null)");
2654 /* First find the key the comments are supposed to be
2657 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2659 if (key_node == NULL)
2661 g_set_error (error, G_KEY_FILE_ERROR,
2662 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2663 _("Key file does not have key '%s' in group '%s'"),
2668 /* Then find all the comments already associated with the
2671 tmp = key_node->next;
2674 pair = (GKeyFileKeyValuePair *) tmp->data;
2676 if (pair->key != NULL)
2681 g_key_file_remove_key_value_pair_node (key_file, group,
2685 if (comment == NULL)
2688 /* Now we can add our new comment
2690 pair = g_slice_new (GKeyFileKeyValuePair);
2692 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2694 key_node = g_list_insert (key_node, pair, 1);
2700 g_key_file_set_group_comment (GKeyFile *key_file,
2701 const gchar *group_name,
2702 const gchar *comment,
2705 GKeyFileGroup *group;
2707 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2709 group = g_key_file_lookup_group (key_file, group_name);
2712 g_set_error (error, G_KEY_FILE_ERROR,
2713 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2714 _("Key file does not have group '%s'"),
2715 group_name ? group_name : "(null)");
2720 /* First remove any existing comment
2724 g_key_file_key_value_pair_free (group->comment);
2725 group->comment = NULL;
2728 if (comment == NULL)
2731 /* Now we can add our new comment
2733 group->comment = g_slice_new (GKeyFileKeyValuePair);
2734 group->comment->key = NULL;
2735 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2741 g_key_file_set_top_comment (GKeyFile *key_file,
2742 const gchar *comment,
2746 GKeyFileGroup *group;
2747 GKeyFileKeyValuePair *pair;
2749 /* The last group in the list should be the top (comments only)
2752 g_warn_if_fail (key_file->groups != NULL);
2753 group_node = g_list_last (key_file->groups);
2754 group = (GKeyFileGroup *) group_node->data;
2755 g_warn_if_fail (group->name == NULL);
2757 /* Note all keys must be comments at the top of
2758 * the file, so we can just free it all.
2760 if (group->key_value_pairs != NULL)
2762 g_list_foreach (group->key_value_pairs,
2763 (GFunc) g_key_file_key_value_pair_free,
2765 g_list_free (group->key_value_pairs);
2766 group->key_value_pairs = NULL;
2769 if (comment == NULL)
2772 pair = g_slice_new (GKeyFileKeyValuePair);
2774 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2776 group->key_value_pairs =
2777 g_list_prepend (group->key_value_pairs, pair);
2783 * g_key_file_set_comment:
2784 * @key_file: a #GKeyFile
2785 * @group_name: a group name, or %NULL
2787 * @comment: a comment
2788 * @error: return location for a #GError
2790 * Places a comment above @key from @group_name.
2791 * If @key is %NULL then @comment will be written above @group_name.
2792 * If both @key and @group_name are %NULL, then @comment will be
2793 * written above the first group in the file.
2795 * Returns: %TRUE if the comment was written, %FALSE otherwise
2800 g_key_file_set_comment (GKeyFile *key_file,
2801 const gchar *group_name,
2803 const gchar *comment,
2806 g_return_val_if_fail (key_file != NULL, FALSE);
2808 if (group_name != NULL && key != NULL)
2810 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2813 else if (group_name != NULL)
2815 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2820 if (!g_key_file_set_top_comment (key_file, comment, error))
2824 if (comment != NULL)
2825 key_file->approximate_size += strlen (comment);
2831 g_key_file_get_key_comment (GKeyFile *key_file,
2832 const gchar *group_name,
2836 GKeyFileGroup *group;
2837 GKeyFileKeyValuePair *pair;
2838 GList *key_node, *tmp;
2842 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2844 group = g_key_file_lookup_group (key_file, group_name);
2847 g_set_error (error, G_KEY_FILE_ERROR,
2848 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2849 _("Key file does not have group '%s'"),
2850 group_name ? group_name : "(null)");
2855 /* First find the key the comments are supposed to be
2858 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2860 if (key_node == NULL)
2862 g_set_error (error, G_KEY_FILE_ERROR,
2863 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2864 _("Key file does not have key '%s' in group '%s'"),
2871 /* Then find all the comments already associated with the
2872 * key and concatentate them.
2874 tmp = key_node->next;
2875 if (!key_node->next)
2878 pair = (GKeyFileKeyValuePair *) tmp->data;
2879 if (pair->key != NULL)
2884 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2886 if (pair->key != NULL)
2892 while (tmp != key_node)
2894 pair = (GKeyFileKeyValuePair *) tmp->data;
2897 string = g_string_sized_new (512);
2899 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2900 g_string_append (string, comment);
2908 comment = string->str;
2909 g_string_free (string, FALSE);
2918 get_group_comment (GKeyFile *key_file,
2919 GKeyFileGroup *group,
2928 tmp = group->key_value_pairs;
2931 GKeyFileKeyValuePair *pair;
2933 pair = (GKeyFileKeyValuePair *) tmp->data;
2935 if (pair->key != NULL)
2941 if (tmp->next == NULL)
2949 GKeyFileKeyValuePair *pair;
2951 pair = (GKeyFileKeyValuePair *) tmp->data;
2954 string = g_string_sized_new (512);
2956 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2957 g_string_append (string, comment);
2964 return g_string_free (string, FALSE);
2970 g_key_file_get_group_comment (GKeyFile *key_file,
2971 const gchar *group_name,
2975 GKeyFileGroup *group;
2977 group = g_key_file_lookup_group (key_file, group_name);
2980 g_set_error (error, G_KEY_FILE_ERROR,
2981 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2982 _("Key file does not have group '%s'"),
2983 group_name ? group_name : "(null)");
2989 return g_strdup (group->comment->value);
2991 group_node = g_key_file_lookup_group_node (key_file, group_name);
2992 group_node = group_node->next;
2993 group = (GKeyFileGroup *)group_node->data;
2994 return get_group_comment (key_file, group, error);
2998 g_key_file_get_top_comment (GKeyFile *key_file,
3002 GKeyFileGroup *group;
3004 /* The last group in the list should be the top (comments only)
3007 g_warn_if_fail (key_file->groups != NULL);
3008 group_node = g_list_last (key_file->groups);
3009 group = (GKeyFileGroup *) group_node->data;
3010 g_warn_if_fail (group->name == NULL);
3012 return get_group_comment (key_file, group, error);
3016 * g_key_file_get_comment:
3017 * @key_file: a #GKeyFile
3018 * @group_name: a group name, or %NULL
3020 * @error: return location for a #GError
3022 * Retrieves a comment above @key from @group_name.
3023 * If @key is %NULL then @comment will be read from above
3024 * @group_name. If both @key and @group_name are %NULL, then
3025 * @comment will be read from above the first group in the file.
3027 * Returns: a comment that should be freed with g_free()
3032 g_key_file_get_comment (GKeyFile *key_file,
3033 const gchar *group_name,
3037 g_return_val_if_fail (key_file != NULL, NULL);
3039 if (group_name != NULL && key != NULL)
3040 return g_key_file_get_key_comment (key_file, group_name, key, error);
3041 else if (group_name != NULL)
3042 return g_key_file_get_group_comment (key_file, group_name, error);
3044 return g_key_file_get_top_comment (key_file, error);
3048 * g_key_file_remove_comment:
3049 * @key_file: a #GKeyFile
3050 * @group_name: a group name, or %NULL
3052 * @error: return location for a #GError
3054 * Removes a comment above @key from @group_name.
3055 * If @key is %NULL then @comment will be removed above @group_name.
3056 * If both @key and @group_name are %NULL, then @comment will
3057 * be removed above the first group in the file.
3059 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3065 g_key_file_remove_comment (GKeyFile *key_file,
3066 const gchar *group_name,
3070 g_return_val_if_fail (key_file != NULL, FALSE);
3072 if (group_name != NULL && key != NULL)
3073 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3074 else if (group_name != NULL)
3075 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3077 return g_key_file_set_top_comment (key_file, NULL, error);
3081 * g_key_file_has_group:
3082 * @key_file: a #GKeyFile
3083 * @group_name: a group name
3085 * Looks whether the key file has the group @group_name.
3087 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3092 g_key_file_has_group (GKeyFile *key_file,
3093 const gchar *group_name)
3095 g_return_val_if_fail (key_file != NULL, FALSE);
3096 g_return_val_if_fail (group_name != NULL, FALSE);
3098 return g_key_file_lookup_group (key_file, group_name) != NULL;
3102 * g_key_file_has_key:
3103 * @key_file: a #GKeyFile
3104 * @group_name: a group name
3106 * @error: return location for a #GError
3108 * Looks whether the key file has the key @key in the group
3111 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3117 g_key_file_has_key (GKeyFile *key_file,
3118 const gchar *group_name,
3122 GKeyFileKeyValuePair *pair;
3123 GKeyFileGroup *group;
3125 g_return_val_if_fail (key_file != NULL, FALSE);
3126 g_return_val_if_fail (group_name != NULL, FALSE);
3127 g_return_val_if_fail (key != NULL, FALSE);
3129 group = g_key_file_lookup_group (key_file, group_name);
3133 g_set_error (error, G_KEY_FILE_ERROR,
3134 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3135 _("Key file does not have group '%s'"),
3136 group_name ? group_name : "(null)");
3141 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3143 return pair != NULL;
3147 g_key_file_add_group (GKeyFile *key_file,
3148 const gchar *group_name)
3150 GKeyFileGroup *group;
3152 g_return_if_fail (key_file != NULL);
3153 g_return_if_fail (g_key_file_is_group_name (group_name));
3155 group = g_key_file_lookup_group (key_file, group_name);
3158 key_file->current_group = group;
3162 group = g_slice_new0 (GKeyFileGroup);
3163 group->name = g_strdup (group_name);
3164 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3165 key_file->groups = g_list_prepend (key_file->groups, group);
3166 key_file->approximate_size += strlen (group_name) + 3;
3167 key_file->current_group = group;
3169 if (key_file->start_group == NULL)
3170 key_file->start_group = group;
3172 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3176 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3181 g_free (pair->value);
3182 g_slice_free (GKeyFileKeyValuePair, pair);
3186 /* Be careful not to call this function on a node with data in the
3187 * lookup map without removing it from the lookup map, first.
3189 * Some current cases where this warning is not a concern are
3191 * - the node being removed is a comment node
3192 * - the entire lookup map is getting destroyed soon after
3196 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3197 GKeyFileGroup *group,
3201 GKeyFileKeyValuePair *pair;
3203 pair = (GKeyFileKeyValuePair *) pair_node->data;
3205 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3207 if (pair->key != NULL)
3208 key_file->approximate_size -= strlen (pair->key) + 1;
3210 g_warn_if_fail (pair->value != NULL);
3211 key_file->approximate_size -= strlen (pair->value);
3213 g_key_file_key_value_pair_free (pair);
3215 g_list_free_1 (pair_node);
3219 g_key_file_remove_group_node (GKeyFile *key_file,
3222 GKeyFileGroup *group;
3225 group = (GKeyFileGroup *) group_node->data;
3228 g_hash_table_remove (key_file->group_hash, group->name);
3230 /* If the current group gets deleted make the current group the last
3233 if (key_file->current_group == group)
3235 /* groups should always contain at least the top comment group,
3236 * unless g_key_file_clear has been called
3238 if (key_file->groups)
3239 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3241 key_file->current_group = NULL;
3244 /* If the start group gets deleted make the start group the first
3247 if (key_file->start_group == group)
3249 tmp = g_list_last (key_file->groups);
3252 if (tmp != group_node &&
3253 ((GKeyFileGroup *) tmp->data)->name != NULL)
3260 key_file->start_group = (GKeyFileGroup *) tmp->data;
3262 key_file->start_group = NULL;
3265 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3267 if (group->name != NULL)
3268 key_file->approximate_size -= strlen (group->name) + 3;
3270 tmp = group->key_value_pairs;
3277 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3280 g_warn_if_fail (group->key_value_pairs == NULL);
3282 if (group->lookup_map)
3284 g_hash_table_destroy (group->lookup_map);
3285 group->lookup_map = NULL;
3288 g_free ((gchar *) group->name);
3289 g_slice_free (GKeyFileGroup, group);
3290 g_list_free_1 (group_node);
3294 * g_key_file_remove_group:
3295 * @key_file: a #GKeyFile
3296 * @group_name: a group name
3297 * @error: return location for a #GError or %NULL
3299 * Removes the specified group, @group_name,
3300 * from the key file.
3302 * Returns: %TRUE if the group was removed, %FALSE otherwise
3307 g_key_file_remove_group (GKeyFile *key_file,
3308 const gchar *group_name,
3313 g_return_val_if_fail (key_file != NULL, FALSE);
3314 g_return_val_if_fail (group_name != NULL, FALSE);
3316 group_node = g_key_file_lookup_group_node (key_file, group_name);
3320 g_set_error (error, G_KEY_FILE_ERROR,
3321 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3322 _("Key file does not have group '%s'"),
3327 g_key_file_remove_group_node (key_file, group_node);
3333 g_key_file_add_key (GKeyFile *key_file,
3334 GKeyFileGroup *group,
3338 GKeyFileKeyValuePair *pair;
3340 pair = g_slice_new (GKeyFileKeyValuePair);
3341 pair->key = g_strdup (key);
3342 pair->value = g_strdup (value);
3344 g_hash_table_replace (group->lookup_map, pair->key, pair);
3345 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3346 group->has_trailing_blank_line = FALSE;
3347 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3351 * g_key_file_remove_key:
3352 * @key_file: a #GKeyFile
3353 * @group_name: a group name
3354 * @key: a key name to remove
3355 * @error: return location for a #GError or %NULL
3357 * Removes @key in @group_name from the key file.
3359 * Returns: %TRUE if the key was removed, %FALSE otherwise
3364 g_key_file_remove_key (GKeyFile *key_file,
3365 const gchar *group_name,
3369 GKeyFileGroup *group;
3370 GKeyFileKeyValuePair *pair;
3372 g_return_val_if_fail (key_file != NULL, FALSE);
3373 g_return_val_if_fail (group_name != NULL, FALSE);
3374 g_return_val_if_fail (key != NULL, FALSE);
3378 group = g_key_file_lookup_group (key_file, group_name);
3381 g_set_error (error, G_KEY_FILE_ERROR,
3382 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3383 _("Key file does not have group '%s'"),
3384 group_name ? group_name : "(null)");
3388 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3392 g_set_error (error, G_KEY_FILE_ERROR,
3393 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3394 _("Key file does not have key '%s' in group '%s'"),
3399 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3401 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3402 g_hash_table_remove (group->lookup_map, pair->key);
3403 g_key_file_key_value_pair_free (pair);
3409 g_key_file_lookup_group_node (GKeyFile *key_file,
3410 const gchar *group_name)
3412 GKeyFileGroup *group;
3415 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3417 group = (GKeyFileGroup *) tmp->data;
3419 if (group && group->name && strcmp (group->name, group_name) == 0)
3426 static GKeyFileGroup *
3427 g_key_file_lookup_group (GKeyFile *key_file,
3428 const gchar *group_name)
3430 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3434 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3435 GKeyFileGroup *group,
3440 for (key_node = group->key_value_pairs;
3442 key_node = key_node->next)
3444 GKeyFileKeyValuePair *pair;
3446 pair = (GKeyFileKeyValuePair *) key_node->data;
3448 if (pair->key && strcmp (pair->key, key) == 0)
3455 static GKeyFileKeyValuePair *
3456 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3457 GKeyFileGroup *group,
3460 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3463 /* Lines starting with # or consisting entirely of whitespace are merely
3464 * recorded, not parsed. This function assumes all leading whitespace
3465 * has been stripped.
3468 g_key_file_line_is_comment (const gchar *line)
3470 return (*line == '#' || *line == '\0' || *line == '\n');
3474 g_key_file_is_group_name (const gchar *name)
3481 p = q = (gchar *) name;
3482 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3483 q = g_utf8_find_next_char (q, NULL);
3485 if (*q != '\0' || q == p)
3492 g_key_file_is_key_name (const gchar *name)
3499 p = q = (gchar *) name;
3500 /* We accept a little more than the desktop entry spec says,
3501 * since gnome-vfs uses mime-types as keys in its cache.
3503 while (*q && *q != '=' && *q != '[' && *q != ']')
3504 q = g_utf8_find_next_char (q, NULL);
3506 /* No empty keys, please */
3510 /* We accept spaces in the middle of keys to not break
3511 * existing apps, but we don't tolerate initial or final
3512 * spaces, which would lead to silent corruption when
3513 * rereading the file.
3515 if (*p == ' ' || q[-1] == ' ')
3521 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3522 q = g_utf8_find_next_char (q, NULL);
3536 /* A group in a key file is made up of a starting '[' followed by one
3537 * or more letters making up the group name followed by ']'.
3540 g_key_file_line_is_group (const gchar *line)
3550 while (*p && *p != ']')
3551 p = g_utf8_find_next_char (p, NULL);
3556 /* silently accept whitespace after the ] */
3557 p = g_utf8_find_next_char (p, NULL);
3558 while (*p == ' ' || *p == '\t')
3559 p = g_utf8_find_next_char (p, NULL);
3568 g_key_file_line_is_key_value_pair (const gchar *line)
3572 p = (gchar *) g_utf8_strchr (line, -1, '=');
3577 /* Key must be non-empty
3586 g_key_file_parse_value_as_string (GKeyFile *key_file,
3591 gchar *string_value, *p, *q0, *q;
3593 string_value = g_new (gchar, strlen (value) + 1);
3595 p = (gchar *) value;
3596 q0 = q = string_value;
3626 g_set_error_literal (error, G_KEY_FILE_ERROR,
3627 G_KEY_FILE_ERROR_INVALID_VALUE,
3628 _("Key file contains escape character "
3633 if (pieces && *p == key_file->list_separator)
3634 *q = key_file->list_separator;
3648 g_set_error (error, G_KEY_FILE_ERROR,
3649 G_KEY_FILE_ERROR_INVALID_VALUE,
3650 _("Key file contains invalid escape "
3651 "sequence '%s'"), sequence);
3660 if (pieces && (*p == key_file->list_separator))
3662 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3678 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3679 *pieces = g_slist_reverse (*pieces);
3682 return string_value;
3686 g_key_file_parse_string_as_value (GKeyFile *key_file,
3687 const gchar *string,
3688 gboolean escape_separator)
3690 gchar *value, *p, *q;
3692 gboolean parsing_leading_space;
3694 length = strlen (string) + 1;
3696 /* Worst case would be that every character needs to be escaped.
3697 * In other words every character turns to two characters
3699 value = g_new (gchar, 2 * length);
3701 p = (gchar *) string;
3703 parsing_leading_space = TRUE;
3704 while (p < (string + length - 1))
3706 gchar escaped_character[3] = { '\\', 0, 0 };
3711 if (parsing_leading_space)
3713 escaped_character[1] = 's';
3714 strcpy (q, escaped_character);
3724 if (parsing_leading_space)
3726 escaped_character[1] = 't';
3727 strcpy (q, escaped_character);
3737 escaped_character[1] = 'n';
3738 strcpy (q, escaped_character);
3742 escaped_character[1] = 'r';
3743 strcpy (q, escaped_character);
3747 escaped_character[1] = '\\';
3748 strcpy (q, escaped_character);
3750 parsing_leading_space = FALSE;
3753 if (escape_separator && *p == key_file->list_separator)
3755 escaped_character[1] = key_file->list_separator;
3756 strcpy (q, escaped_character);
3758 parsing_leading_space = TRUE;
3764 parsing_leading_space = FALSE;
3776 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3780 gchar *end_of_valid_int;
3785 long_value = strtol (value, &end_of_valid_int, 10);
3787 if (*value == '\0' || *end_of_valid_int != '\0')
3789 gchar *value_utf8 = _g_utf8_make_valid (value);
3790 g_set_error (error, G_KEY_FILE_ERROR,
3791 G_KEY_FILE_ERROR_INVALID_VALUE,
3792 _("Value '%s' cannot be interpreted "
3793 "as a number."), value_utf8);
3794 g_free (value_utf8);
3799 int_value = long_value;
3800 if (int_value != long_value || errno == ERANGE)
3802 gchar *value_utf8 = _g_utf8_make_valid (value);
3805 G_KEY_FILE_ERROR_INVALID_VALUE,
3806 _("Integer value '%s' out of range"),
3808 g_free (value_utf8);
3817 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3821 return g_strdup_printf ("%d", value);
3825 g_key_file_parse_value_as_double (GKeyFile *key_file,
3829 gchar *end_of_valid_d;
3830 gdouble double_value = 0;
3832 double_value = g_ascii_strtod (value, &end_of_valid_d);
3834 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3836 gchar *value_utf8 = _g_utf8_make_valid (value);
3837 g_set_error (error, G_KEY_FILE_ERROR,
3838 G_KEY_FILE_ERROR_INVALID_VALUE,
3839 _("Value '%s' cannot be interpreted "
3840 "as a float number."),
3842 g_free (value_utf8);
3845 return double_value;
3849 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3855 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3857 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3860 value_utf8 = _g_utf8_make_valid (value);
3861 g_set_error (error, G_KEY_FILE_ERROR,
3862 G_KEY_FILE_ERROR_INVALID_VALUE,
3863 _("Value '%s' cannot be interpreted "
3864 "as a boolean."), value_utf8);
3865 g_free (value_utf8);
3871 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3875 return g_strdup ("true");
3877 return g_strdup ("false");
3881 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3888 string = g_string_sized_new (512);
3890 lines = g_strsplit (value, "\n", 0);
3892 for (i = 0; lines[i] != NULL; i++)
3894 if (lines[i][0] != '#')
3895 g_string_append_printf (string, "%s\n", lines[i]);
3897 g_string_append_printf (string, "%s\n", lines[i] + 1);
3901 return g_string_free (string, FALSE);
3905 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3906 const gchar *comment)
3912 string = g_string_sized_new (512);
3914 lines = g_strsplit (comment, "\n", 0);
3916 for (i = 0; lines[i] != NULL; i++)
3917 g_string_append_printf (string, "#%s%s", lines[i],
3918 lines[i + 1] == NULL? "" : "\n");
3921 return g_string_free (string, FALSE);