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 < 0)
323 gchar *candidate_file, *sub_dir;
325 candidate_file = (gchar *) file;
326 sub_dir = g_strdup ("");
327 while (candidate_file != NULL && fd < 0)
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: a %NULL-terminated string array or %NULL if the specified
1509 * key cannot be found. The array should be freed with g_strfreev().
1514 g_key_file_get_string_list (GKeyFile *key_file,
1515 const gchar *group_name,
1520 GError *key_file_error = NULL;
1521 gchar *value, *string_value, **values;
1523 GSList *p, *pieces = NULL;
1525 g_return_val_if_fail (key_file != NULL, NULL);
1526 g_return_val_if_fail (group_name != NULL, NULL);
1527 g_return_val_if_fail (key != NULL, NULL);
1532 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1536 g_propagate_error (error, key_file_error);
1540 if (!g_utf8_validate (value, -1, NULL))
1542 gchar *value_utf8 = _g_utf8_make_valid (value);
1543 g_set_error (error, G_KEY_FILE_ERROR,
1544 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1545 _("Key file contains key '%s' with value '%s' "
1546 "which is not UTF-8"), key, value_utf8);
1547 g_free (value_utf8);
1553 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1555 g_free (string_value);
1559 if (g_error_matches (key_file_error,
1561 G_KEY_FILE_ERROR_INVALID_VALUE))
1563 g_set_error (error, G_KEY_FILE_ERROR,
1564 G_KEY_FILE_ERROR_INVALID_VALUE,
1565 _("Key file contains key '%s' "
1566 "which has a value that cannot be interpreted."),
1568 g_error_free (key_file_error);
1571 g_propagate_error (error, key_file_error);
1576 len = g_slist_length (pieces);
1577 values = g_new (gchar *, len + 1);
1578 for (p = pieces, i = 0; p; p = p->next)
1579 values[i++] = p->data;
1582 g_slist_free (pieces);
1591 * g_key_file_set_string_list:
1592 * @key_file: a #GKeyFile
1593 * @group_name: a group name
1595 * @list: an array of string values
1596 * @length: number of string values in @list
1598 * Associates a list of string values for @key under @group_name.
1599 * If @key cannot be found then it is created.
1600 * If @group_name cannot be found then it is created.
1605 g_key_file_set_string_list (GKeyFile *key_file,
1606 const gchar *group_name,
1608 const gchar * const list[],
1611 GString *value_list;
1614 g_return_if_fail (key_file != NULL);
1615 g_return_if_fail (list != NULL || length == 0);
1617 value_list = g_string_sized_new (length * 128);
1618 for (i = 0; i < length && list[i] != NULL; i++)
1622 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1623 g_string_append (value_list, value);
1624 g_string_append_c (value_list, key_file->list_separator);
1629 g_key_file_set_value (key_file, group_name, key, value_list->str);
1630 g_string_free (value_list, TRUE);
1634 * g_key_file_set_locale_string:
1635 * @key_file: a #GKeyFile
1636 * @group_name: a group name
1638 * @locale: a locale identifier
1641 * Associates a string value for @key and @locale under @group_name.
1642 * If the translation for @key cannot be found then it is created.
1647 g_key_file_set_locale_string (GKeyFile *key_file,
1648 const gchar *group_name,
1650 const gchar *locale,
1651 const gchar *string)
1653 gchar *full_key, *value;
1655 g_return_if_fail (key_file != NULL);
1656 g_return_if_fail (key != NULL);
1657 g_return_if_fail (locale != NULL);
1658 g_return_if_fail (string != NULL);
1660 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1661 full_key = g_strdup_printf ("%s[%s]", key, locale);
1662 g_key_file_set_value (key_file, group_name, full_key, value);
1668 * g_key_file_get_locale_string:
1669 * @key_file: a #GKeyFile
1670 * @group_name: a group name
1672 * @locale: a locale identifier or %NULL
1673 * @error: return location for a #GError, or %NULL
1675 * Returns the value associated with @key under @group_name
1676 * translated in the given @locale if available. If @locale is
1677 * %NULL then the current locale is assumed.
1679 * If @key cannot be found then %NULL is returned and @error is set
1680 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1681 * with @key cannot be interpreted or no suitable translation can
1682 * be found then the untranslated value is returned.
1684 * Return value: a newly allocated string or %NULL if the specified
1685 * key cannot be found.
1690 g_key_file_get_locale_string (GKeyFile *key_file,
1691 const gchar *group_name,
1693 const gchar *locale,
1696 gchar *candidate_key, *translated_value;
1697 GError *key_file_error;
1699 gboolean free_languages = FALSE;
1702 g_return_val_if_fail (key_file != NULL, NULL);
1703 g_return_val_if_fail (group_name != NULL, NULL);
1704 g_return_val_if_fail (key != NULL, NULL);
1706 candidate_key = NULL;
1707 translated_value = NULL;
1708 key_file_error = NULL;
1712 languages = g_get_locale_variants (locale);
1713 free_languages = TRUE;
1717 languages = (gchar **) g_get_language_names ();
1718 free_languages = FALSE;
1721 for (i = 0; languages[i]; i++)
1723 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1725 translated_value = g_key_file_get_string (key_file,
1727 candidate_key, NULL);
1728 g_free (candidate_key);
1730 if (translated_value)
1733 g_free (translated_value);
1734 translated_value = NULL;
1737 /* Fallback to untranslated key
1739 if (!translated_value)
1741 translated_value = g_key_file_get_string (key_file, group_name, key,
1744 if (!translated_value)
1745 g_propagate_error (error, key_file_error);
1749 g_strfreev (languages);
1751 return translated_value;
1755 * g_key_file_get_locale_string_list:
1756 * @key_file: a #GKeyFile
1757 * @group_name: a group name
1759 * @locale: a locale identifier or %NULL
1760 * @length: return location for the number of returned strings or %NULL
1761 * @error: return location for a #GError or %NULL
1763 * Returns the values associated with @key under @group_name
1764 * translated in the given @locale if available. If @locale is
1765 * %NULL then the current locale is assumed.
1767 * If @key cannot be found then %NULL is returned and @error is set
1768 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1769 * with @key cannot be interpreted or no suitable translations
1770 * can be found then the untranslated values are returned. The
1771 * returned array is %NULL-terminated, so @length may optionally
1774 * Return value: a newly allocated %NULL-terminated string array
1775 * or %NULL if the key isn't found. The string array should be freed
1776 * with g_strfreev().
1781 g_key_file_get_locale_string_list (GKeyFile *key_file,
1782 const gchar *group_name,
1784 const gchar *locale,
1788 GError *key_file_error;
1789 gchar **values, *value;
1790 char list_separator[2];
1793 g_return_val_if_fail (key_file != NULL, NULL);
1794 g_return_val_if_fail (group_name != NULL, NULL);
1795 g_return_val_if_fail (key != NULL, NULL);
1797 key_file_error = NULL;
1799 value = g_key_file_get_locale_string (key_file, group_name,
1804 g_propagate_error (error, key_file_error);
1813 len = strlen (value);
1814 if (value[len - 1] == key_file->list_separator)
1815 value[len - 1] = '\0';
1817 list_separator[0] = key_file->list_separator;
1818 list_separator[1] = '\0';
1819 values = g_strsplit (value, list_separator, 0);
1824 *length = g_strv_length (values);
1830 * g_key_file_set_locale_string_list:
1831 * @key_file: a #GKeyFile
1832 * @group_name: a group name
1834 * @locale: a locale identifier
1835 * @list: a %NULL-terminated array of locale string values
1836 * @length: the length of @list
1838 * Associates a list of string values for @key and @locale under
1839 * @group_name. If the translation for @key cannot be found then
1845 g_key_file_set_locale_string_list (GKeyFile *key_file,
1846 const gchar *group_name,
1848 const gchar *locale,
1849 const gchar * const list[],
1852 GString *value_list;
1856 g_return_if_fail (key_file != NULL);
1857 g_return_if_fail (key != NULL);
1858 g_return_if_fail (locale != NULL);
1859 g_return_if_fail (length != 0);
1861 value_list = g_string_sized_new (length * 128);
1862 for (i = 0; i < length && list[i] != NULL; i++)
1866 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1867 g_string_append (value_list, value);
1868 g_string_append_c (value_list, key_file->list_separator);
1873 full_key = g_strdup_printf ("%s[%s]", key, locale);
1874 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1876 g_string_free (value_list, TRUE);
1880 * g_key_file_get_boolean:
1881 * @key_file: a #GKeyFile
1882 * @group_name: a group name
1884 * @error: return location for a #GError
1886 * Returns the value associated with @key under @group_name as a
1889 * If @key cannot be found then %FALSE is returned and @error is set
1890 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1891 * associated with @key cannot be interpreted as a boolean then %FALSE
1892 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1894 * Return value: the value associated with the key as a boolean,
1895 * or %FALSE if the key was not found or could not be parsed.
1900 g_key_file_get_boolean (GKeyFile *key_file,
1901 const gchar *group_name,
1905 GError *key_file_error = NULL;
1907 gboolean bool_value;
1909 g_return_val_if_fail (key_file != NULL, FALSE);
1910 g_return_val_if_fail (group_name != NULL, FALSE);
1911 g_return_val_if_fail (key != NULL, FALSE);
1913 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1917 g_propagate_error (error, key_file_error);
1921 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1927 if (g_error_matches (key_file_error,
1929 G_KEY_FILE_ERROR_INVALID_VALUE))
1931 g_set_error (error, G_KEY_FILE_ERROR,
1932 G_KEY_FILE_ERROR_INVALID_VALUE,
1933 _("Key file contains key '%s' "
1934 "which has value that cannot be interpreted."),
1936 g_error_free (key_file_error);
1939 g_propagate_error (error, key_file_error);
1946 * g_key_file_set_boolean:
1947 * @key_file: a #GKeyFile
1948 * @group_name: a group name
1950 * @value: %TRUE or %FALSE
1952 * Associates a new boolean value with @key under @group_name.
1953 * If @key cannot be found then it is created.
1958 g_key_file_set_boolean (GKeyFile *key_file,
1959 const gchar *group_name,
1965 g_return_if_fail (key_file != NULL);
1967 result = g_key_file_parse_boolean_as_value (key_file, value);
1968 g_key_file_set_value (key_file, group_name, key, result);
1973 * g_key_file_get_boolean_list:
1974 * @key_file: a #GKeyFile
1975 * @group_name: a group name
1977 * @length: the number of booleans returned
1978 * @error: return location for a #GError
1980 * Returns the values associated with @key under @group_name as
1983 * If @key cannot be found then %NULL is returned and @error is set to
1984 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1985 * with @key cannot be interpreted as booleans then %NULL is returned
1986 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1988 * Return value: the values associated with the key as a list of
1989 * booleans, or %NULL if the key was not found or could not be parsed.
1994 g_key_file_get_boolean_list (GKeyFile *key_file,
1995 const gchar *group_name,
2000 GError *key_file_error;
2002 gboolean *bool_values;
2005 g_return_val_if_fail (key_file != NULL, NULL);
2006 g_return_val_if_fail (group_name != NULL, NULL);
2007 g_return_val_if_fail (key != NULL, NULL);
2012 key_file_error = NULL;
2014 values = g_key_file_get_string_list (key_file, group_name, key,
2015 &num_bools, &key_file_error);
2018 g_propagate_error (error, key_file_error);
2023 bool_values = g_new (gboolean, num_bools);
2025 for (i = 0; i < num_bools; i++)
2027 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2033 g_propagate_error (error, key_file_error);
2034 g_strfreev (values);
2035 g_free (bool_values);
2040 g_strfreev (values);
2043 *length = num_bools;
2049 * g_key_file_set_boolean_list:
2050 * @key_file: a #GKeyFile
2051 * @group_name: a group name
2053 * @list: an array of boolean values
2054 * @length: length of @list
2056 * Associates a list of boolean values with @key under @group_name.
2057 * If @key cannot be found then it is created.
2058 * If @group_name is %NULL, the start_group is used.
2063 g_key_file_set_boolean_list (GKeyFile *key_file,
2064 const gchar *group_name,
2069 GString *value_list;
2072 g_return_if_fail (key_file != NULL);
2073 g_return_if_fail (list != NULL);
2075 value_list = g_string_sized_new (length * 8);
2076 for (i = 0; i < length; i++)
2080 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2082 g_string_append (value_list, value);
2083 g_string_append_c (value_list, key_file->list_separator);
2088 g_key_file_set_value (key_file, group_name, key, value_list->str);
2089 g_string_free (value_list, TRUE);
2093 * g_key_file_get_integer:
2094 * @key_file: a #GKeyFile
2095 * @group_name: a group name
2097 * @error: return location for a #GError
2099 * Returns the value associated with @key under @group_name as an
2102 * If @key cannot be found then 0 is returned and @error is set to
2103 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2104 * with @key cannot be interpreted as an integer then 0 is returned
2105 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2107 * Return value: the value associated with the key as an integer, or
2108 * 0 if the key was not found or could not be parsed.
2113 g_key_file_get_integer (GKeyFile *key_file,
2114 const gchar *group_name,
2118 GError *key_file_error;
2122 g_return_val_if_fail (key_file != NULL, -1);
2123 g_return_val_if_fail (group_name != NULL, -1);
2124 g_return_val_if_fail (key != NULL, -1);
2126 key_file_error = NULL;
2128 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2132 g_propagate_error (error, key_file_error);
2136 int_value = g_key_file_parse_value_as_integer (key_file, value,
2142 if (g_error_matches (key_file_error,
2144 G_KEY_FILE_ERROR_INVALID_VALUE))
2146 g_set_error (error, G_KEY_FILE_ERROR,
2147 G_KEY_FILE_ERROR_INVALID_VALUE,
2148 _("Key file contains key '%s' in group '%s' "
2149 "which has value that cannot be interpreted."), key,
2151 g_error_free (key_file_error);
2154 g_propagate_error (error, key_file_error);
2161 * g_key_file_set_integer:
2162 * @key_file: a #GKeyFile
2163 * @group_name: a group name
2165 * @value: an integer value
2167 * Associates a new integer value with @key under @group_name.
2168 * If @key cannot be found then it is created.
2173 g_key_file_set_integer (GKeyFile *key_file,
2174 const gchar *group_name,
2180 g_return_if_fail (key_file != NULL);
2182 result = g_key_file_parse_integer_as_value (key_file, value);
2183 g_key_file_set_value (key_file, group_name, key, result);
2188 * g_key_file_get_int64:
2189 * @key_file: a non-%NULL #GKeyFile
2190 * @group_name: a non-%NULL group name
2191 * @key: a non-%NULL key
2192 * @error: return location for a #GError
2194 * Returns the value associated with @key under @group_name as a signed
2195 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2196 * 64-bit results without truncation.
2198 * Returns: the value associated with the key as a signed 64-bit integer, or
2199 * 0 if the key was not found or could not be parsed.
2204 g_key_file_get_int64 (GKeyFile *key_file,
2205 const gchar *group_name,
2212 g_return_val_if_fail (key_file != NULL, -1);
2213 g_return_val_if_fail (group_name != NULL, -1);
2214 g_return_val_if_fail (key != NULL, -1);
2216 s = g_key_file_get_value (key_file, group_name, key, error);
2221 v = g_ascii_strtoll (s, &end, 10);
2223 if (*s == '\0' || *end != '\0')
2225 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2226 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2227 key, group_name, s);
2236 * g_key_file_set_int64:
2237 * @key_file: a #GKeyFile
2238 * @group_name: a group name
2240 * @value: an integer value
2242 * Associates a new integer value with @key under @group_name.
2243 * If @key cannot be found then it is created.
2248 g_key_file_set_int64 (GKeyFile *key_file,
2249 const gchar *group_name,
2255 g_return_if_fail (key_file != NULL);
2257 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2258 g_key_file_set_value (key_file, group_name, key, result);
2263 * g_key_file_get_uint64:
2264 * @key_file: a non-%NULL #GKeyFile
2265 * @group_name: a non-%NULL group name
2266 * @key: a non-%NULL key
2267 * @error: return location for a #GError
2269 * Returns the value associated with @key under @group_name as an unsigned
2270 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2271 * large positive results without truncation.
2273 * Returns: the value associated with the key as an unsigned 64-bit integer,
2274 * or 0 if the key was not found or could not be parsed.
2279 g_key_file_get_uint64 (GKeyFile *key_file,
2280 const gchar *group_name,
2287 g_return_val_if_fail (key_file != NULL, -1);
2288 g_return_val_if_fail (group_name != NULL, -1);
2289 g_return_val_if_fail (key != NULL, -1);
2291 s = g_key_file_get_value (key_file, group_name, key, error);
2296 v = g_ascii_strtoull (s, &end, 10);
2298 if (*s == '\0' || *end != '\0')
2300 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2301 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2302 key, group_name, s);
2311 * g_key_file_set_uint64:
2312 * @key_file: a #GKeyFile
2313 * @group_name: a group name
2315 * @value: an integer value
2317 * Associates a new integer value with @key under @group_name.
2318 * If @key cannot be found then it is created.
2323 g_key_file_set_uint64 (GKeyFile *key_file,
2324 const gchar *group_name,
2330 g_return_if_fail (key_file != NULL);
2332 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2333 g_key_file_set_value (key_file, group_name, key, result);
2338 * g_key_file_get_integer_list:
2339 * @key_file: a #GKeyFile
2340 * @group_name: a group name
2342 * @length: the number of integers returned
2343 * @error: return location for a #GError
2345 * Returns the values associated with @key under @group_name as
2348 * If @key cannot be found then %NULL is returned and @error is set to
2349 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2350 * with @key cannot be interpreted as integers then %NULL is returned
2351 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2353 * Return value: the values associated with the key as a list of
2354 * integers, or %NULL if the key was not found or could not be parsed.
2359 g_key_file_get_integer_list (GKeyFile *key_file,
2360 const gchar *group_name,
2365 GError *key_file_error = NULL;
2370 g_return_val_if_fail (key_file != NULL, NULL);
2371 g_return_val_if_fail (group_name != NULL, NULL);
2372 g_return_val_if_fail (key != NULL, NULL);
2377 values = g_key_file_get_string_list (key_file, group_name, key,
2378 &num_ints, &key_file_error);
2381 g_propagate_error (error, key_file_error);
2386 int_values = g_new (gint, num_ints);
2388 for (i = 0; i < num_ints; i++)
2390 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2396 g_propagate_error (error, key_file_error);
2397 g_strfreev (values);
2398 g_free (int_values);
2403 g_strfreev (values);
2412 * g_key_file_set_integer_list:
2413 * @key_file: a #GKeyFile
2414 * @group_name: a group name
2416 * @list: an array of integer values
2417 * @length: number of integer values in @list
2419 * Associates a list of integer values with @key under @group_name.
2420 * If @key cannot be found then it is created.
2425 g_key_file_set_integer_list (GKeyFile *key_file,
2426 const gchar *group_name,
2434 g_return_if_fail (key_file != NULL);
2435 g_return_if_fail (list != NULL);
2437 values = g_string_sized_new (length * 16);
2438 for (i = 0; i < length; i++)
2442 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2444 g_string_append (values, value);
2445 g_string_append_c (values, key_file->list_separator);
2450 g_key_file_set_value (key_file, group_name, key, values->str);
2451 g_string_free (values, TRUE);
2455 * g_key_file_get_double:
2456 * @key_file: a #GKeyFile
2457 * @group_name: a group name
2459 * @error: return location for a #GError
2461 * Returns the value associated with @key under @group_name as a
2462 * double. If @group_name is %NULL, the start_group is used.
2464 * If @key cannot be found then 0.0 is returned and @error is set to
2465 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2466 * with @key cannot be interpreted as a double then 0.0 is returned
2467 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2469 * Return value: the value associated with the key as a double, or
2470 * 0.0 if the key was not found or could not be parsed.
2475 g_key_file_get_double (GKeyFile *key_file,
2476 const gchar *group_name,
2480 GError *key_file_error;
2482 gdouble double_value;
2484 g_return_val_if_fail (key_file != NULL, -1);
2485 g_return_val_if_fail (group_name != NULL, -1);
2486 g_return_val_if_fail (key != NULL, -1);
2488 key_file_error = NULL;
2490 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2494 g_propagate_error (error, key_file_error);
2498 double_value = g_key_file_parse_value_as_double (key_file, value,
2504 if (g_error_matches (key_file_error,
2506 G_KEY_FILE_ERROR_INVALID_VALUE))
2508 g_set_error (error, G_KEY_FILE_ERROR,
2509 G_KEY_FILE_ERROR_INVALID_VALUE,
2510 _("Key file contains key '%s' in group '%s' "
2511 "which has value that cannot be interpreted."), key,
2513 g_error_free (key_file_error);
2516 g_propagate_error (error, key_file_error);
2519 return double_value;
2523 * g_key_file_set_double:
2524 * @key_file: a #GKeyFile
2525 * @group_name: a group name
2527 * @value: an double value
2529 * Associates a new double value with @key under @group_name.
2530 * If @key cannot be found then it is created.
2535 g_key_file_set_double (GKeyFile *key_file,
2536 const gchar *group_name,
2540 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2542 g_return_if_fail (key_file != NULL);
2544 g_ascii_dtostr (result, sizeof (result), value);
2545 g_key_file_set_value (key_file, group_name, key, result);
2549 * g_key_file_get_double_list:
2550 * @key_file: a #GKeyFile
2551 * @group_name: a group name
2553 * @length: the number of doubles returned
2554 * @error: return location for a #GError
2556 * Returns the values associated with @key under @group_name as
2559 * If @key cannot be found then %NULL is returned and @error is set to
2560 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2561 * with @key cannot be interpreted as doubles then %NULL is returned
2562 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2564 * Return value: the values associated with the key as a list of
2565 * doubles, or %NULL if the key was not found or could not be parsed.
2570 g_key_file_get_double_list (GKeyFile *key_file,
2571 const gchar *group_name,
2576 GError *key_file_error = NULL;
2578 gdouble *double_values;
2579 gsize i, num_doubles;
2581 g_return_val_if_fail (key_file != NULL, NULL);
2582 g_return_val_if_fail (group_name != NULL, NULL);
2583 g_return_val_if_fail (key != NULL, NULL);
2588 values = g_key_file_get_string_list (key_file, group_name, key,
2589 &num_doubles, &key_file_error);
2592 g_propagate_error (error, key_file_error);
2597 double_values = g_new (gdouble, num_doubles);
2599 for (i = 0; i < num_doubles; i++)
2601 double_values[i] = g_key_file_parse_value_as_double (key_file,
2607 g_propagate_error (error, key_file_error);
2608 g_strfreev (values);
2609 g_free (double_values);
2614 g_strfreev (values);
2617 *length = num_doubles;
2619 return double_values;
2623 * g_key_file_set_double_list:
2624 * @key_file: a #GKeyFile
2625 * @group_name: a group name
2627 * @list: an array of double values
2628 * @length: number of double values in @list
2630 * Associates a list of double values with @key under
2631 * @group_name. If @key cannot be found then it is created.
2636 g_key_file_set_double_list (GKeyFile *key_file,
2637 const gchar *group_name,
2645 g_return_if_fail (key_file != NULL);
2646 g_return_if_fail (list != NULL);
2648 values = g_string_sized_new (length * 16);
2649 for (i = 0; i < length; i++)
2651 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2653 g_ascii_dtostr( result, sizeof (result), list[i] );
2655 g_string_append (values, result);
2656 g_string_append_c (values, key_file->list_separator);
2659 g_key_file_set_value (key_file, group_name, key, values->str);
2660 g_string_free (values, TRUE);
2664 g_key_file_set_key_comment (GKeyFile *key_file,
2665 const gchar *group_name,
2667 const gchar *comment,
2670 GKeyFileGroup *group;
2671 GKeyFileKeyValuePair *pair;
2672 GList *key_node, *comment_node, *tmp;
2674 group = g_key_file_lookup_group (key_file, group_name);
2677 g_set_error (error, G_KEY_FILE_ERROR,
2678 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2679 _("Key file does not have group '%s'"),
2680 group_name ? group_name : "(null)");
2685 /* First find the key the comments are supposed to be
2688 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2690 if (key_node == NULL)
2692 g_set_error (error, G_KEY_FILE_ERROR,
2693 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2694 _("Key file does not have key '%s' in group '%s'"),
2699 /* Then find all the comments already associated with the
2702 tmp = key_node->next;
2705 pair = (GKeyFileKeyValuePair *) tmp->data;
2707 if (pair->key != NULL)
2712 g_key_file_remove_key_value_pair_node (key_file, group,
2716 if (comment == NULL)
2719 /* Now we can add our new comment
2721 pair = g_slice_new (GKeyFileKeyValuePair);
2723 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2725 key_node = g_list_insert (key_node, pair, 1);
2731 g_key_file_set_group_comment (GKeyFile *key_file,
2732 const gchar *group_name,
2733 const gchar *comment,
2736 GKeyFileGroup *group;
2738 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2740 group = g_key_file_lookup_group (key_file, group_name);
2743 g_set_error (error, G_KEY_FILE_ERROR,
2744 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2745 _("Key file does not have group '%s'"),
2746 group_name ? group_name : "(null)");
2751 /* First remove any existing comment
2755 g_key_file_key_value_pair_free (group->comment);
2756 group->comment = NULL;
2759 if (comment == NULL)
2762 /* Now we can add our new comment
2764 group->comment = g_slice_new (GKeyFileKeyValuePair);
2765 group->comment->key = NULL;
2766 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2772 g_key_file_set_top_comment (GKeyFile *key_file,
2773 const gchar *comment,
2777 GKeyFileGroup *group;
2778 GKeyFileKeyValuePair *pair;
2780 /* The last group in the list should be the top (comments only)
2783 g_warn_if_fail (key_file->groups != NULL);
2784 group_node = g_list_last (key_file->groups);
2785 group = (GKeyFileGroup *) group_node->data;
2786 g_warn_if_fail (group->name == NULL);
2788 /* Note all keys must be comments at the top of
2789 * the file, so we can just free it all.
2791 if (group->key_value_pairs != NULL)
2793 g_list_foreach (group->key_value_pairs,
2794 (GFunc) g_key_file_key_value_pair_free,
2796 g_list_free (group->key_value_pairs);
2797 group->key_value_pairs = NULL;
2800 if (comment == NULL)
2803 pair = g_slice_new (GKeyFileKeyValuePair);
2805 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2807 group->key_value_pairs =
2808 g_list_prepend (group->key_value_pairs, pair);
2814 * g_key_file_set_comment:
2815 * @key_file: a #GKeyFile
2816 * @group_name: a group name, or %NULL
2818 * @comment: a comment
2819 * @error: return location for a #GError
2821 * Places a comment above @key from @group_name.
2822 * If @key is %NULL then @comment will be written above @group_name.
2823 * If both @key and @group_name are %NULL, then @comment will be
2824 * written above the first group in the file.
2826 * Returns: %TRUE if the comment was written, %FALSE otherwise
2831 g_key_file_set_comment (GKeyFile *key_file,
2832 const gchar *group_name,
2834 const gchar *comment,
2837 g_return_val_if_fail (key_file != NULL, FALSE);
2839 if (group_name != NULL && key != NULL)
2841 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2844 else if (group_name != NULL)
2846 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2851 if (!g_key_file_set_top_comment (key_file, comment, error))
2855 if (comment != NULL)
2856 key_file->approximate_size += strlen (comment);
2862 g_key_file_get_key_comment (GKeyFile *key_file,
2863 const gchar *group_name,
2867 GKeyFileGroup *group;
2868 GKeyFileKeyValuePair *pair;
2869 GList *key_node, *tmp;
2873 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2875 group = g_key_file_lookup_group (key_file, group_name);
2878 g_set_error (error, G_KEY_FILE_ERROR,
2879 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2880 _("Key file does not have group '%s'"),
2881 group_name ? group_name : "(null)");
2886 /* First find the key the comments are supposed to be
2889 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2891 if (key_node == NULL)
2893 g_set_error (error, G_KEY_FILE_ERROR,
2894 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2895 _("Key file does not have key '%s' in group '%s'"),
2902 /* Then find all the comments already associated with the
2903 * key and concatentate them.
2905 tmp = key_node->next;
2906 if (!key_node->next)
2909 pair = (GKeyFileKeyValuePair *) tmp->data;
2910 if (pair->key != NULL)
2915 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2917 if (pair->key != NULL)
2923 while (tmp != key_node)
2925 pair = (GKeyFileKeyValuePair *) tmp->data;
2928 string = g_string_sized_new (512);
2930 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2931 g_string_append (string, comment);
2939 comment = string->str;
2940 g_string_free (string, FALSE);
2949 get_group_comment (GKeyFile *key_file,
2950 GKeyFileGroup *group,
2959 tmp = group->key_value_pairs;
2962 GKeyFileKeyValuePair *pair;
2964 pair = (GKeyFileKeyValuePair *) tmp->data;
2966 if (pair->key != NULL)
2972 if (tmp->next == NULL)
2980 GKeyFileKeyValuePair *pair;
2982 pair = (GKeyFileKeyValuePair *) tmp->data;
2985 string = g_string_sized_new (512);
2987 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2988 g_string_append (string, comment);
2995 return g_string_free (string, FALSE);
3001 g_key_file_get_group_comment (GKeyFile *key_file,
3002 const gchar *group_name,
3006 GKeyFileGroup *group;
3008 group = g_key_file_lookup_group (key_file, group_name);
3011 g_set_error (error, G_KEY_FILE_ERROR,
3012 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3013 _("Key file does not have group '%s'"),
3014 group_name ? group_name : "(null)");
3020 return g_strdup (group->comment->value);
3022 group_node = g_key_file_lookup_group_node (key_file, group_name);
3023 group_node = group_node->next;
3024 group = (GKeyFileGroup *)group_node->data;
3025 return get_group_comment (key_file, group, error);
3029 g_key_file_get_top_comment (GKeyFile *key_file,
3033 GKeyFileGroup *group;
3035 /* The last group in the list should be the top (comments only)
3038 g_warn_if_fail (key_file->groups != NULL);
3039 group_node = g_list_last (key_file->groups);
3040 group = (GKeyFileGroup *) group_node->data;
3041 g_warn_if_fail (group->name == NULL);
3043 return get_group_comment (key_file, group, error);
3047 * g_key_file_get_comment:
3048 * @key_file: a #GKeyFile
3049 * @group_name: a group name, or %NULL
3051 * @error: return location for a #GError
3053 * Retrieves a comment above @key from @group_name.
3054 * If @key is %NULL then @comment will be read from above
3055 * @group_name. If both @key and @group_name are %NULL, then
3056 * @comment will be read from above the first group in the file.
3058 * Returns: a comment that should be freed with g_free()
3063 g_key_file_get_comment (GKeyFile *key_file,
3064 const gchar *group_name,
3068 g_return_val_if_fail (key_file != NULL, NULL);
3070 if (group_name != NULL && key != NULL)
3071 return g_key_file_get_key_comment (key_file, group_name, key, error);
3072 else if (group_name != NULL)
3073 return g_key_file_get_group_comment (key_file, group_name, error);
3075 return g_key_file_get_top_comment (key_file, error);
3079 * g_key_file_remove_comment:
3080 * @key_file: a #GKeyFile
3081 * @group_name: a group name, or %NULL
3083 * @error: return location for a #GError
3085 * Removes a comment above @key from @group_name.
3086 * If @key is %NULL then @comment will be removed above @group_name.
3087 * If both @key and @group_name are %NULL, then @comment will
3088 * be removed above the first group in the file.
3090 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3096 g_key_file_remove_comment (GKeyFile *key_file,
3097 const gchar *group_name,
3101 g_return_val_if_fail (key_file != NULL, FALSE);
3103 if (group_name != NULL && key != NULL)
3104 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3105 else if (group_name != NULL)
3106 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3108 return g_key_file_set_top_comment (key_file, NULL, error);
3112 * g_key_file_has_group:
3113 * @key_file: a #GKeyFile
3114 * @group_name: a group name
3116 * Looks whether the key file has the group @group_name.
3118 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3123 g_key_file_has_group (GKeyFile *key_file,
3124 const gchar *group_name)
3126 g_return_val_if_fail (key_file != NULL, FALSE);
3127 g_return_val_if_fail (group_name != NULL, FALSE);
3129 return g_key_file_lookup_group (key_file, group_name) != NULL;
3133 * g_key_file_has_key: (skip)
3134 * @key_file: a #GKeyFile
3135 * @group_name: a group name
3137 * @error: return location for a #GError
3139 * Looks whether the key file has the key @key in the group
3142 * <note>This function does not follow the rules for #GError strictly;
3143 * the return value both carries meaning and signals an error. To use
3144 * this function, you must pass a #GError pointer in @error, and check
3145 * whether it is not %NULL to see if an error occurred.</note>
3147 * See g_key_file_has_key_full() for a replacement function which does
3148 * follow the #GError rules.
3150 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3156 g_key_file_has_key (GKeyFile *key_file,
3157 const gchar *group_name,
3161 GError *temp_error = NULL;
3164 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3170 g_propagate_error (error, temp_error);
3176 * g_key_file_has_key_full:
3177 * @key_file: a #GKeyFile
3178 * @group_name: a group name
3180 * @has_key: (out) (allow-none): Return location for whether or not key exists
3181 * @error: return location for a #GError
3183 * Looks whether the key file has the key @key in the group
3186 * Return value: %TRUE if a group with the name @group_name
3187 * exists. Otherwise, @error is set and %FALSE is returned.
3190 * Rename to: g_key_file_has_key
3193 g_key_file_has_key_full (GKeyFile *key_file,
3194 const gchar *group_name,
3199 GKeyFileKeyValuePair *pair;
3200 GKeyFileGroup *group;
3202 g_return_val_if_fail (key_file != NULL, FALSE);
3203 g_return_val_if_fail (group_name != NULL, FALSE);
3204 g_return_val_if_fail (key != NULL, FALSE);
3206 group = g_key_file_lookup_group (key_file, group_name);
3210 g_set_error (error, G_KEY_FILE_ERROR,
3211 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3212 _("Key file does not have group '%s'"),
3213 group_name ? group_name : "(null)");
3218 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3221 *has_key = pair != NULL;
3226 g_key_file_add_group (GKeyFile *key_file,
3227 const gchar *group_name)
3229 GKeyFileGroup *group;
3231 g_return_if_fail (key_file != NULL);
3232 g_return_if_fail (g_key_file_is_group_name (group_name));
3234 group = g_key_file_lookup_group (key_file, group_name);
3237 key_file->current_group = group;
3241 group = g_slice_new0 (GKeyFileGroup);
3242 group->name = g_strdup (group_name);
3243 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3244 key_file->groups = g_list_prepend (key_file->groups, group);
3245 key_file->approximate_size += strlen (group_name) + 3;
3246 key_file->current_group = group;
3248 if (key_file->start_group == NULL)
3249 key_file->start_group = group;
3251 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3255 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3260 g_free (pair->value);
3261 g_slice_free (GKeyFileKeyValuePair, pair);
3265 /* Be careful not to call this function on a node with data in the
3266 * lookup map without removing it from the lookup map, first.
3268 * Some current cases where this warning is not a concern are
3270 * - the node being removed is a comment node
3271 * - the entire lookup map is getting destroyed soon after
3275 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3276 GKeyFileGroup *group,
3280 GKeyFileKeyValuePair *pair;
3282 pair = (GKeyFileKeyValuePair *) pair_node->data;
3284 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3286 if (pair->key != NULL)
3287 key_file->approximate_size -= strlen (pair->key) + 1;
3289 g_warn_if_fail (pair->value != NULL);
3290 key_file->approximate_size -= strlen (pair->value);
3292 g_key_file_key_value_pair_free (pair);
3294 g_list_free_1 (pair_node);
3298 g_key_file_remove_group_node (GKeyFile *key_file,
3301 GKeyFileGroup *group;
3304 group = (GKeyFileGroup *) group_node->data;
3307 g_hash_table_remove (key_file->group_hash, group->name);
3309 /* If the current group gets deleted make the current group the last
3312 if (key_file->current_group == group)
3314 /* groups should always contain at least the top comment group,
3315 * unless g_key_file_clear has been called
3317 if (key_file->groups)
3318 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3320 key_file->current_group = NULL;
3323 /* If the start group gets deleted make the start group the first
3326 if (key_file->start_group == group)
3328 tmp = g_list_last (key_file->groups);
3331 if (tmp != group_node &&
3332 ((GKeyFileGroup *) tmp->data)->name != NULL)
3339 key_file->start_group = (GKeyFileGroup *) tmp->data;
3341 key_file->start_group = NULL;
3344 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3346 if (group->name != NULL)
3347 key_file->approximate_size -= strlen (group->name) + 3;
3349 tmp = group->key_value_pairs;
3356 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3359 g_warn_if_fail (group->key_value_pairs == NULL);
3361 if (group->lookup_map)
3363 g_hash_table_destroy (group->lookup_map);
3364 group->lookup_map = NULL;
3367 g_free ((gchar *) group->name);
3368 g_slice_free (GKeyFileGroup, group);
3369 g_list_free_1 (group_node);
3373 * g_key_file_remove_group:
3374 * @key_file: a #GKeyFile
3375 * @group_name: a group name
3376 * @error: return location for a #GError or %NULL
3378 * Removes the specified group, @group_name,
3379 * from the key file.
3381 * Returns: %TRUE if the group was removed, %FALSE otherwise
3386 g_key_file_remove_group (GKeyFile *key_file,
3387 const gchar *group_name,
3392 g_return_val_if_fail (key_file != NULL, FALSE);
3393 g_return_val_if_fail (group_name != NULL, FALSE);
3395 group_node = g_key_file_lookup_group_node (key_file, group_name);
3399 g_set_error (error, G_KEY_FILE_ERROR,
3400 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3401 _("Key file does not have group '%s'"),
3406 g_key_file_remove_group_node (key_file, group_node);
3412 g_key_file_add_key_value_pair (GKeyFile *key_file,
3413 GKeyFileGroup *group,
3414 GKeyFileKeyValuePair *pair)
3416 g_hash_table_replace (group->lookup_map, pair->key, pair);
3417 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3418 group->has_trailing_blank_line = FALSE;
3419 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3423 g_key_file_add_key (GKeyFile *key_file,
3424 GKeyFileGroup *group,
3428 GKeyFileKeyValuePair *pair;
3430 pair = g_slice_new (GKeyFileKeyValuePair);
3431 pair->key = g_strdup (key);
3432 pair->value = g_strdup (value);
3434 g_key_file_add_key_value_pair (key_file, group, pair);
3438 * g_key_file_remove_key:
3439 * @key_file: a #GKeyFile
3440 * @group_name: a group name
3441 * @key: a key name to remove
3442 * @error: return location for a #GError or %NULL
3444 * Removes @key in @group_name from the key file.
3446 * Returns: %TRUE if the key was removed, %FALSE otherwise
3451 g_key_file_remove_key (GKeyFile *key_file,
3452 const gchar *group_name,
3456 GKeyFileGroup *group;
3457 GKeyFileKeyValuePair *pair;
3459 g_return_val_if_fail (key_file != NULL, FALSE);
3460 g_return_val_if_fail (group_name != NULL, FALSE);
3461 g_return_val_if_fail (key != NULL, FALSE);
3465 group = g_key_file_lookup_group (key_file, group_name);
3468 g_set_error (error, G_KEY_FILE_ERROR,
3469 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3470 _("Key file does not have group '%s'"),
3471 group_name ? group_name : "(null)");
3475 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3479 g_set_error (error, G_KEY_FILE_ERROR,
3480 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3481 _("Key file does not have key '%s' in group '%s'"),
3486 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3488 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3489 g_hash_table_remove (group->lookup_map, pair->key);
3490 g_key_file_key_value_pair_free (pair);
3496 g_key_file_lookup_group_node (GKeyFile *key_file,
3497 const gchar *group_name)
3499 GKeyFileGroup *group;
3502 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3504 group = (GKeyFileGroup *) tmp->data;
3506 if (group && group->name && strcmp (group->name, group_name) == 0)
3513 static GKeyFileGroup *
3514 g_key_file_lookup_group (GKeyFile *key_file,
3515 const gchar *group_name)
3517 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3521 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3522 GKeyFileGroup *group,
3527 for (key_node = group->key_value_pairs;
3529 key_node = key_node->next)
3531 GKeyFileKeyValuePair *pair;
3533 pair = (GKeyFileKeyValuePair *) key_node->data;
3535 if (pair->key && strcmp (pair->key, key) == 0)
3542 static GKeyFileKeyValuePair *
3543 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3544 GKeyFileGroup *group,
3547 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3550 /* Lines starting with # or consisting entirely of whitespace are merely
3551 * recorded, not parsed. This function assumes all leading whitespace
3552 * has been stripped.
3555 g_key_file_line_is_comment (const gchar *line)
3557 return (*line == '#' || *line == '\0' || *line == '\n');
3561 g_key_file_is_group_name (const gchar *name)
3568 p = q = (gchar *) name;
3569 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3570 q = g_utf8_find_next_char (q, NULL);
3572 if (*q != '\0' || q == p)
3579 g_key_file_is_key_name (const gchar *name)
3586 p = q = (gchar *) name;
3587 /* We accept a little more than the desktop entry spec says,
3588 * since gnome-vfs uses mime-types as keys in its cache.
3590 while (*q && *q != '=' && *q != '[' && *q != ']')
3591 q = g_utf8_find_next_char (q, NULL);
3593 /* No empty keys, please */
3597 /* We accept spaces in the middle of keys to not break
3598 * existing apps, but we don't tolerate initial or final
3599 * spaces, which would lead to silent corruption when
3600 * rereading the file.
3602 if (*p == ' ' || q[-1] == ' ')
3608 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3609 q = g_utf8_find_next_char (q, NULL);
3623 /* A group in a key file is made up of a starting '[' followed by one
3624 * or more letters making up the group name followed by ']'.
3627 g_key_file_line_is_group (const gchar *line)
3637 while (*p && *p != ']')
3638 p = g_utf8_find_next_char (p, NULL);
3643 /* silently accept whitespace after the ] */
3644 p = g_utf8_find_next_char (p, NULL);
3645 while (*p == ' ' || *p == '\t')
3646 p = g_utf8_find_next_char (p, NULL);
3655 g_key_file_line_is_key_value_pair (const gchar *line)
3659 p = (gchar *) g_utf8_strchr (line, -1, '=');
3664 /* Key must be non-empty
3673 g_key_file_parse_value_as_string (GKeyFile *key_file,
3678 gchar *string_value, *p, *q0, *q;
3680 string_value = g_new (gchar, strlen (value) + 1);
3682 p = (gchar *) value;
3683 q0 = q = string_value;
3713 g_set_error_literal (error, G_KEY_FILE_ERROR,
3714 G_KEY_FILE_ERROR_INVALID_VALUE,
3715 _("Key file contains escape character "
3720 if (pieces && *p == key_file->list_separator)
3721 *q = key_file->list_separator;
3735 g_set_error (error, G_KEY_FILE_ERROR,
3736 G_KEY_FILE_ERROR_INVALID_VALUE,
3737 _("Key file contains invalid escape "
3738 "sequence '%s'"), sequence);
3747 if (pieces && (*p == key_file->list_separator))
3749 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3765 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3766 *pieces = g_slist_reverse (*pieces);
3769 return string_value;
3773 g_key_file_parse_string_as_value (GKeyFile *key_file,
3774 const gchar *string,
3775 gboolean escape_separator)
3777 gchar *value, *p, *q;
3779 gboolean parsing_leading_space;
3781 length = strlen (string) + 1;
3783 /* Worst case would be that every character needs to be escaped.
3784 * In other words every character turns to two characters
3786 value = g_new (gchar, 2 * length);
3788 p = (gchar *) string;
3790 parsing_leading_space = TRUE;
3791 while (p < (string + length - 1))
3793 gchar escaped_character[3] = { '\\', 0, 0 };
3798 if (parsing_leading_space)
3800 escaped_character[1] = 's';
3801 strcpy (q, escaped_character);
3811 if (parsing_leading_space)
3813 escaped_character[1] = 't';
3814 strcpy (q, escaped_character);
3824 escaped_character[1] = 'n';
3825 strcpy (q, escaped_character);
3829 escaped_character[1] = 'r';
3830 strcpy (q, escaped_character);
3834 escaped_character[1] = '\\';
3835 strcpy (q, escaped_character);
3837 parsing_leading_space = FALSE;
3840 if (escape_separator && *p == key_file->list_separator)
3842 escaped_character[1] = key_file->list_separator;
3843 strcpy (q, escaped_character);
3845 parsing_leading_space = TRUE;
3851 parsing_leading_space = FALSE;
3863 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3867 gchar *end_of_valid_int;
3872 long_value = strtol (value, &end_of_valid_int, 10);
3874 if (*value == '\0' || *end_of_valid_int != '\0')
3876 gchar *value_utf8 = _g_utf8_make_valid (value);
3877 g_set_error (error, G_KEY_FILE_ERROR,
3878 G_KEY_FILE_ERROR_INVALID_VALUE,
3879 _("Value '%s' cannot be interpreted "
3880 "as a number."), value_utf8);
3881 g_free (value_utf8);
3886 int_value = long_value;
3887 if (int_value != long_value || errno == ERANGE)
3889 gchar *value_utf8 = _g_utf8_make_valid (value);
3892 G_KEY_FILE_ERROR_INVALID_VALUE,
3893 _("Integer value '%s' out of range"),
3895 g_free (value_utf8);
3904 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3908 return g_strdup_printf ("%d", value);
3912 g_key_file_parse_value_as_double (GKeyFile *key_file,
3916 gchar *end_of_valid_d;
3917 gdouble double_value = 0;
3919 double_value = g_ascii_strtod (value, &end_of_valid_d);
3921 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3923 gchar *value_utf8 = _g_utf8_make_valid (value);
3924 g_set_error (error, G_KEY_FILE_ERROR,
3925 G_KEY_FILE_ERROR_INVALID_VALUE,
3926 _("Value '%s' cannot be interpreted "
3927 "as a float number."),
3929 g_free (value_utf8);
3932 return double_value;
3936 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3942 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3944 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3947 value_utf8 = _g_utf8_make_valid (value);
3948 g_set_error (error, G_KEY_FILE_ERROR,
3949 G_KEY_FILE_ERROR_INVALID_VALUE,
3950 _("Value '%s' cannot be interpreted "
3951 "as a boolean."), value_utf8);
3952 g_free (value_utf8);
3958 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3962 return g_strdup ("true");
3964 return g_strdup ("false");
3968 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3975 string = g_string_sized_new (512);
3977 lines = g_strsplit (value, "\n", 0);
3979 for (i = 0; lines[i] != NULL; i++)
3981 if (lines[i][0] != '#')
3982 g_string_append_printf (string, "%s\n", lines[i]);
3984 g_string_append_printf (string, "%s\n", lines[i] + 1);
3988 return g_string_free (string, FALSE);
3992 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3993 const gchar *comment)
3999 string = g_string_sized_new (512);
4001 lines = g_strsplit (comment, "\n", 0);
4003 for (i = 0; lines[i] != NULL; i++)
4004 g_string_append_printf (string, "#%s%s", lines[i],
4005 lines[i + 1] == NULL? "" : "\n");
4008 return g_string_free (string, FALSE);