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);
966 for (i = 0; i < length; i++)
970 if (key_file->parse_buffer->len > 0
971 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
973 g_string_erase (key_file->parse_buffer,
974 key_file->parse_buffer->len - 1,
977 /* When a newline is encountered flush the parse buffer so that the
978 * line can be parsed. Note that completely blank lines won't show
979 * up in the parse buffer, so they get parsed directly.
981 if (key_file->parse_buffer->len > 0)
982 g_key_file_flush_parse_buffer (key_file, &parse_error);
984 g_key_file_parse_comment (key_file, "", 1, &parse_error);
988 g_propagate_error (error, parse_error);
993 g_string_append_c (key_file->parse_buffer, data[i]);
996 key_file->approximate_size += length;
1000 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1003 GError *file_error = NULL;
1005 g_return_if_fail (key_file != NULL);
1009 if (key_file->parse_buffer->len > 0)
1011 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1012 key_file->parse_buffer->len,
1014 g_string_erase (key_file->parse_buffer, 0, -1);
1018 g_propagate_error (error, file_error);
1025 * g_key_file_to_data:
1026 * @key_file: a #GKeyFile
1027 * @length: return location for the length of the
1028 * returned string, or %NULL
1029 * @error: return location for a #GError, or %NULL
1031 * This function outputs @key_file as a string.
1033 * Note that this function never reports an error,
1034 * so it is safe to pass %NULL as @error.
1036 * Return value: a newly allocated string holding
1037 * the contents of the #GKeyFile
1042 g_key_file_to_data (GKeyFile *key_file,
1046 GString *data_string;
1047 GList *group_node, *key_file_node;
1048 gboolean has_blank_line = TRUE;
1050 g_return_val_if_fail (key_file != NULL, NULL);
1052 data_string = g_string_sized_new (2 * key_file->approximate_size);
1054 for (group_node = g_list_last (key_file->groups);
1056 group_node = group_node->prev)
1058 GKeyFileGroup *group;
1060 group = (GKeyFileGroup *) group_node->data;
1062 /* separate groups by at least an empty line */
1063 if (!has_blank_line)
1064 g_string_append_c (data_string, '\n');
1065 has_blank_line = group->has_trailing_blank_line;
1067 if (group->comment != NULL)
1068 g_string_append_printf (data_string, "%s\n", group->comment->value);
1070 if (group->name != NULL)
1071 g_string_append_printf (data_string, "[%s]\n", group->name);
1073 for (key_file_node = g_list_last (group->key_value_pairs);
1074 key_file_node != NULL;
1075 key_file_node = key_file_node->prev)
1077 GKeyFileKeyValuePair *pair;
1079 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1081 if (pair->key != NULL)
1082 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1084 g_string_append_printf (data_string, "%s\n", pair->value);
1089 *length = data_string->len;
1091 return g_string_free (data_string, FALSE);
1095 * g_key_file_get_keys:
1096 * @key_file: a #GKeyFile
1097 * @group_name: a group name
1098 * @length: return location for the number of keys returned, or %NULL
1099 * @error: return location for a #GError, or %NULL
1101 * Returns all keys for the group name @group_name. The array of
1102 * returned keys will be %NULL-terminated, so @length may
1103 * optionally be %NULL. In the event that the @group_name cannot
1104 * be found, %NULL is returned and @error is set to
1105 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1107 * Return value: a newly-allocated %NULL-terminated array of strings.
1108 * Use g_strfreev() to free it.
1113 g_key_file_get_keys (GKeyFile *key_file,
1114 const gchar *group_name,
1118 GKeyFileGroup *group;
1123 g_return_val_if_fail (key_file != NULL, NULL);
1124 g_return_val_if_fail (group_name != NULL, NULL);
1126 group = g_key_file_lookup_group (key_file, group_name);
1130 g_set_error (error, G_KEY_FILE_ERROR,
1131 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1132 _("Key file does not have group '%s'"),
1133 group_name ? group_name : "(null)");
1138 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1140 GKeyFileKeyValuePair *pair;
1142 pair = (GKeyFileKeyValuePair *) tmp->data;
1148 keys = g_new (gchar *, num_keys + 1);
1151 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1153 GKeyFileKeyValuePair *pair;
1155 pair = (GKeyFileKeyValuePair *) tmp->data;
1159 keys[i] = g_strdup (pair->key);
1164 keys[num_keys] = NULL;
1173 * g_key_file_get_start_group:
1174 * @key_file: a #GKeyFile
1176 * Returns the name of the start group of the file.
1178 * Return value: The start group of the key file.
1183 g_key_file_get_start_group (GKeyFile *key_file)
1185 g_return_val_if_fail (key_file != NULL, NULL);
1187 if (key_file->start_group)
1188 return g_strdup (key_file->start_group->name);
1194 * g_key_file_get_groups:
1195 * @key_file: a #GKeyFile
1196 * @length: return location for the number of returned groups, or %NULL
1198 * Returns all groups in the key file loaded with @key_file.
1199 * The array of returned groups will be %NULL-terminated, so
1200 * @length may optionally be %NULL.
1202 * Return value: a newly-allocated %NULL-terminated array of strings.
1203 * Use g_strfreev() to free it.
1207 g_key_file_get_groups (GKeyFile *key_file,
1212 gsize i, num_groups;
1214 g_return_val_if_fail (key_file != NULL, NULL);
1216 num_groups = g_list_length (key_file->groups);
1218 g_return_val_if_fail (num_groups > 0, NULL);
1220 group_node = g_list_last (key_file->groups);
1222 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1224 /* Only need num_groups instead of num_groups + 1
1225 * because the first group of the file (last in the
1226 * list) is always the comment group at the top,
1229 groups = g_new (gchar *, num_groups);
1233 for (group_node = group_node->prev;
1235 group_node = group_node->prev)
1237 GKeyFileGroup *group;
1239 group = (GKeyFileGroup *) group_node->data;
1241 g_warn_if_fail (group->name != NULL);
1243 groups[i++] = g_strdup (group->name);
1254 * g_key_file_get_value:
1255 * @key_file: a #GKeyFile
1256 * @group_name: a group name
1258 * @error: return location for a #GError, or %NULL
1260 * Returns the raw value associated with @key under @group_name.
1261 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1263 * In the event the key cannot be found, %NULL is returned and
1264 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1265 * event that the @group_name cannot be found, %NULL is returned
1266 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1269 * Return value: a newly allocated string or %NULL if the specified
1270 * key cannot be found.
1275 g_key_file_get_value (GKeyFile *key_file,
1276 const gchar *group_name,
1280 GKeyFileGroup *group;
1281 GKeyFileKeyValuePair *pair;
1282 gchar *value = NULL;
1284 g_return_val_if_fail (key_file != NULL, NULL);
1285 g_return_val_if_fail (group_name != NULL, NULL);
1286 g_return_val_if_fail (key != NULL, NULL);
1288 group = g_key_file_lookup_group (key_file, group_name);
1292 g_set_error (error, G_KEY_FILE_ERROR,
1293 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1294 _("Key file does not have group '%s'"),
1295 group_name ? group_name : "(null)");
1299 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1302 value = g_strdup (pair->value);
1304 g_set_error (error, G_KEY_FILE_ERROR,
1305 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1306 _("Key file does not have key '%s'"), key);
1312 * g_key_file_set_value:
1313 * @key_file: a #GKeyFile
1314 * @group_name: a group name
1318 * Associates a new value with @key under @group_name.
1320 * If @key cannot be found then it is created. If @group_name cannot
1321 * be found then it is created. To set an UTF-8 string which may contain
1322 * characters that need escaping (such as newlines or spaces), use
1323 * g_key_file_set_string().
1328 g_key_file_set_value (GKeyFile *key_file,
1329 const gchar *group_name,
1333 GKeyFileGroup *group;
1334 GKeyFileKeyValuePair *pair;
1336 g_return_if_fail (key_file != NULL);
1337 g_return_if_fail (g_key_file_is_group_name (group_name));
1338 g_return_if_fail (g_key_file_is_key_name (key));
1339 g_return_if_fail (value != NULL);
1341 group = g_key_file_lookup_group (key_file, group_name);
1345 g_key_file_add_group (key_file, group_name);
1346 group = (GKeyFileGroup *) key_file->groups->data;
1348 g_key_file_add_key (key_file, group, key, value);
1352 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1355 g_key_file_add_key (key_file, group, key, value);
1358 g_free (pair->value);
1359 pair->value = g_strdup (value);
1365 * g_key_file_get_string:
1366 * @key_file: a #GKeyFile
1367 * @group_name: a group name
1369 * @error: return location for a #GError, or %NULL
1371 * Returns the string value associated with @key under @group_name.
1372 * Unlike g_key_file_get_value(), this function handles escape sequences
1375 * In the event the key cannot be found, %NULL is returned and
1376 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1377 * event that the @group_name cannot be found, %NULL is returned
1378 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1380 * Return value: a newly allocated string or %NULL if the specified
1381 * key cannot be found.
1386 g_key_file_get_string (GKeyFile *key_file,
1387 const gchar *group_name,
1391 gchar *value, *string_value;
1392 GError *key_file_error;
1394 g_return_val_if_fail (key_file != NULL, NULL);
1395 g_return_val_if_fail (group_name != NULL, NULL);
1396 g_return_val_if_fail (key != NULL, NULL);
1398 key_file_error = NULL;
1400 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1404 g_propagate_error (error, key_file_error);
1408 if (!g_utf8_validate (value, -1, NULL))
1410 gchar *value_utf8 = _g_utf8_make_valid (value);
1411 g_set_error (error, G_KEY_FILE_ERROR,
1412 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1413 _("Key file contains key '%s' with value '%s' "
1414 "which is not UTF-8"), key, value_utf8);
1415 g_free (value_utf8);
1421 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1427 if (g_error_matches (key_file_error,
1429 G_KEY_FILE_ERROR_INVALID_VALUE))
1431 g_set_error (error, G_KEY_FILE_ERROR,
1432 G_KEY_FILE_ERROR_INVALID_VALUE,
1433 _("Key file contains key '%s' "
1434 "which has value that cannot be interpreted."),
1436 g_error_free (key_file_error);
1439 g_propagate_error (error, key_file_error);
1442 return string_value;
1446 * g_key_file_set_string:
1447 * @key_file: a #GKeyFile
1448 * @group_name: a group name
1452 * Associates a new string value with @key under @group_name.
1453 * If @key cannot be found then it is created.
1454 * If @group_name cannot be found then it is created.
1455 * Unlike g_key_file_set_value(), this function handles characters
1456 * that need escaping, such as newlines.
1461 g_key_file_set_string (GKeyFile *key_file,
1462 const gchar *group_name,
1464 const gchar *string)
1468 g_return_if_fail (key_file != NULL);
1469 g_return_if_fail (string != NULL);
1471 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1472 g_key_file_set_value (key_file, group_name, key, value);
1477 * g_key_file_get_string_list:
1478 * @key_file: a #GKeyFile
1479 * @group_name: a group name
1481 * @length: return location for the number of returned strings, or %NULL
1482 * @error: return location for a #GError, or %NULL
1484 * Returns the values associated with @key under @group_name.
1486 * In the event the key cannot be found, %NULL is returned and
1487 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1488 * event that the @group_name cannot be found, %NULL is returned
1489 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1491 * Return value: a %NULL-terminated string array or %NULL if the specified
1492 * key cannot be found. The array should be freed with g_strfreev().
1497 g_key_file_get_string_list (GKeyFile *key_file,
1498 const gchar *group_name,
1503 GError *key_file_error = NULL;
1504 gchar *value, *string_value, **values;
1506 GSList *p, *pieces = NULL;
1508 g_return_val_if_fail (key_file != NULL, NULL);
1509 g_return_val_if_fail (group_name != NULL, NULL);
1510 g_return_val_if_fail (key != NULL, NULL);
1515 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1519 g_propagate_error (error, key_file_error);
1523 if (!g_utf8_validate (value, -1, NULL))
1525 gchar *value_utf8 = _g_utf8_make_valid (value);
1526 g_set_error (error, G_KEY_FILE_ERROR,
1527 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1528 _("Key file contains key '%s' with value '%s' "
1529 "which is not UTF-8"), key, value_utf8);
1530 g_free (value_utf8);
1536 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1538 g_free (string_value);
1542 if (g_error_matches (key_file_error,
1544 G_KEY_FILE_ERROR_INVALID_VALUE))
1546 g_set_error (error, G_KEY_FILE_ERROR,
1547 G_KEY_FILE_ERROR_INVALID_VALUE,
1548 _("Key file contains key '%s' "
1549 "which has a value that cannot be interpreted."),
1551 g_error_free (key_file_error);
1554 g_propagate_error (error, key_file_error);
1559 len = g_slist_length (pieces);
1560 values = g_new (gchar *, len + 1);
1561 for (p = pieces, i = 0; p; p = p->next)
1562 values[i++] = p->data;
1565 g_slist_free (pieces);
1574 * g_key_file_set_string_list:
1575 * @key_file: a #GKeyFile
1576 * @group_name: a group name
1578 * @list: an array of string values
1579 * @length: number of string values in @list
1581 * Associates a list of string values for @key under @group_name.
1582 * If @key cannot be found then it is created.
1583 * If @group_name cannot be found then it is created.
1588 g_key_file_set_string_list (GKeyFile *key_file,
1589 const gchar *group_name,
1591 const gchar * const list[],
1594 GString *value_list;
1597 g_return_if_fail (key_file != NULL);
1598 g_return_if_fail (list != NULL || length == 0);
1600 value_list = g_string_sized_new (length * 128);
1601 for (i = 0; i < length && list[i] != NULL; i++)
1605 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1606 g_string_append (value_list, value);
1607 g_string_append_c (value_list, key_file->list_separator);
1612 g_key_file_set_value (key_file, group_name, key, value_list->str);
1613 g_string_free (value_list, TRUE);
1617 * g_key_file_set_locale_string:
1618 * @key_file: a #GKeyFile
1619 * @group_name: a group name
1621 * @locale: a locale identifier
1624 * Associates a string value for @key and @locale under @group_name.
1625 * If the translation for @key cannot be found then it is created.
1630 g_key_file_set_locale_string (GKeyFile *key_file,
1631 const gchar *group_name,
1633 const gchar *locale,
1634 const gchar *string)
1636 gchar *full_key, *value;
1638 g_return_if_fail (key_file != NULL);
1639 g_return_if_fail (key != NULL);
1640 g_return_if_fail (locale != NULL);
1641 g_return_if_fail (string != NULL);
1643 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1644 full_key = g_strdup_printf ("%s[%s]", key, locale);
1645 g_key_file_set_value (key_file, group_name, full_key, value);
1651 * g_key_file_get_locale_string:
1652 * @key_file: a #GKeyFile
1653 * @group_name: a group name
1655 * @locale: a locale identifier or %NULL
1656 * @error: return location for a #GError, or %NULL
1658 * Returns the value associated with @key under @group_name
1659 * translated in the given @locale if available. If @locale is
1660 * %NULL then the current locale is assumed.
1662 * If @key cannot be found then %NULL is returned and @error is set
1663 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1664 * with @key cannot be interpreted or no suitable translation can
1665 * be found then the untranslated value is returned.
1667 * Return value: a newly allocated string or %NULL if the specified
1668 * key cannot be found.
1673 g_key_file_get_locale_string (GKeyFile *key_file,
1674 const gchar *group_name,
1676 const gchar *locale,
1679 gchar *candidate_key, *translated_value;
1680 GError *key_file_error;
1682 gboolean free_languages = FALSE;
1685 g_return_val_if_fail (key_file != NULL, NULL);
1686 g_return_val_if_fail (group_name != NULL, NULL);
1687 g_return_val_if_fail (key != NULL, NULL);
1689 candidate_key = NULL;
1690 translated_value = NULL;
1691 key_file_error = NULL;
1695 languages = g_get_locale_variants (locale);
1696 free_languages = TRUE;
1700 languages = (gchar **) g_get_language_names ();
1701 free_languages = FALSE;
1704 for (i = 0; languages[i]; i++)
1706 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1708 translated_value = g_key_file_get_string (key_file,
1710 candidate_key, NULL);
1711 g_free (candidate_key);
1713 if (translated_value)
1716 g_free (translated_value);
1717 translated_value = NULL;
1720 /* Fallback to untranslated key
1722 if (!translated_value)
1724 translated_value = g_key_file_get_string (key_file, group_name, key,
1727 if (!translated_value)
1728 g_propagate_error (error, key_file_error);
1732 g_strfreev (languages);
1734 return translated_value;
1738 * g_key_file_get_locale_string_list:
1739 * @key_file: a #GKeyFile
1740 * @group_name: a group name
1742 * @locale: a locale identifier or %NULL
1743 * @length: return location for the number of returned strings or %NULL
1744 * @error: return location for a #GError or %NULL
1746 * Returns the values associated with @key under @group_name
1747 * translated in the given @locale if available. If @locale is
1748 * %NULL then the current locale is assumed.
1750 * If @key cannot be found then %NULL is returned and @error is set
1751 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1752 * with @key cannot be interpreted or no suitable translations
1753 * can be found then the untranslated values are returned. The
1754 * returned array is %NULL-terminated, so @length may optionally
1757 * Return value: a newly allocated %NULL-terminated string array
1758 * or %NULL if the key isn't found. The string array should be freed
1759 * with g_strfreev().
1764 g_key_file_get_locale_string_list (GKeyFile *key_file,
1765 const gchar *group_name,
1767 const gchar *locale,
1771 GError *key_file_error;
1772 gchar **values, *value;
1773 char list_separator[2];
1776 g_return_val_if_fail (key_file != NULL, NULL);
1777 g_return_val_if_fail (group_name != NULL, NULL);
1778 g_return_val_if_fail (key != NULL, NULL);
1780 key_file_error = NULL;
1782 value = g_key_file_get_locale_string (key_file, group_name,
1787 g_propagate_error (error, key_file_error);
1796 len = strlen (value);
1797 if (value[len - 1] == key_file->list_separator)
1798 value[len - 1] = '\0';
1800 list_separator[0] = key_file->list_separator;
1801 list_separator[1] = '\0';
1802 values = g_strsplit (value, list_separator, 0);
1807 *length = g_strv_length (values);
1813 * g_key_file_set_locale_string_list:
1814 * @key_file: a #GKeyFile
1815 * @group_name: a group name
1817 * @locale: a locale identifier
1818 * @list: a %NULL-terminated array of locale string values
1819 * @length: the length of @list
1821 * Associates a list of string values for @key and @locale under
1822 * @group_name. If the translation for @key cannot be found then
1828 g_key_file_set_locale_string_list (GKeyFile *key_file,
1829 const gchar *group_name,
1831 const gchar *locale,
1832 const gchar * const list[],
1835 GString *value_list;
1839 g_return_if_fail (key_file != NULL);
1840 g_return_if_fail (key != NULL);
1841 g_return_if_fail (locale != NULL);
1842 g_return_if_fail (length != 0);
1844 value_list = g_string_sized_new (length * 128);
1845 for (i = 0; i < length && list[i] != NULL; i++)
1849 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1850 g_string_append (value_list, value);
1851 g_string_append_c (value_list, key_file->list_separator);
1856 full_key = g_strdup_printf ("%s[%s]", key, locale);
1857 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1859 g_string_free (value_list, TRUE);
1863 * g_key_file_get_boolean:
1864 * @key_file: a #GKeyFile
1865 * @group_name: a group name
1867 * @error: return location for a #GError
1869 * Returns the value associated with @key under @group_name as a
1872 * If @key cannot be found then %FALSE is returned and @error is set
1873 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1874 * associated with @key cannot be interpreted as a boolean then %FALSE
1875 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1877 * Return value: the value associated with the key as a boolean,
1878 * or %FALSE if the key was not found or could not be parsed.
1883 g_key_file_get_boolean (GKeyFile *key_file,
1884 const gchar *group_name,
1888 GError *key_file_error = NULL;
1890 gboolean bool_value;
1892 g_return_val_if_fail (key_file != NULL, FALSE);
1893 g_return_val_if_fail (group_name != NULL, FALSE);
1894 g_return_val_if_fail (key != NULL, FALSE);
1896 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1900 g_propagate_error (error, key_file_error);
1904 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1910 if (g_error_matches (key_file_error,
1912 G_KEY_FILE_ERROR_INVALID_VALUE))
1914 g_set_error (error, G_KEY_FILE_ERROR,
1915 G_KEY_FILE_ERROR_INVALID_VALUE,
1916 _("Key file contains key '%s' "
1917 "which has value that cannot be interpreted."),
1919 g_error_free (key_file_error);
1922 g_propagate_error (error, key_file_error);
1929 * g_key_file_set_boolean:
1930 * @key_file: a #GKeyFile
1931 * @group_name: a group name
1933 * @value: %TRUE or %FALSE
1935 * Associates a new boolean value with @key under @group_name.
1936 * If @key cannot be found then it is created.
1941 g_key_file_set_boolean (GKeyFile *key_file,
1942 const gchar *group_name,
1948 g_return_if_fail (key_file != NULL);
1950 result = g_key_file_parse_boolean_as_value (key_file, value);
1951 g_key_file_set_value (key_file, group_name, key, result);
1956 * g_key_file_get_boolean_list:
1957 * @key_file: a #GKeyFile
1958 * @group_name: a group name
1960 * @length: the number of booleans returned
1961 * @error: return location for a #GError
1963 * Returns the values associated with @key under @group_name as
1966 * If @key cannot be found then %NULL is returned and @error is set to
1967 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1968 * with @key cannot be interpreted as booleans then %NULL is returned
1969 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1971 * Return value: the values associated with the key as a list of
1972 * booleans, or %NULL if the key was not found or could not be parsed.
1977 g_key_file_get_boolean_list (GKeyFile *key_file,
1978 const gchar *group_name,
1983 GError *key_file_error;
1985 gboolean *bool_values;
1988 g_return_val_if_fail (key_file != NULL, NULL);
1989 g_return_val_if_fail (group_name != NULL, NULL);
1990 g_return_val_if_fail (key != NULL, NULL);
1995 key_file_error = NULL;
1997 values = g_key_file_get_string_list (key_file, group_name, key,
1998 &num_bools, &key_file_error);
2001 g_propagate_error (error, key_file_error);
2006 bool_values = g_new (gboolean, num_bools);
2008 for (i = 0; i < num_bools; i++)
2010 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2016 g_propagate_error (error, key_file_error);
2017 g_strfreev (values);
2018 g_free (bool_values);
2023 g_strfreev (values);
2026 *length = num_bools;
2032 * g_key_file_set_boolean_list:
2033 * @key_file: a #GKeyFile
2034 * @group_name: a group name
2036 * @list: an array of boolean values
2037 * @length: length of @list
2039 * Associates a list of boolean values with @key under @group_name.
2040 * If @key cannot be found then it is created.
2041 * If @group_name is %NULL, the start_group is used.
2046 g_key_file_set_boolean_list (GKeyFile *key_file,
2047 const gchar *group_name,
2052 GString *value_list;
2055 g_return_if_fail (key_file != NULL);
2056 g_return_if_fail (list != NULL);
2058 value_list = g_string_sized_new (length * 8);
2059 for (i = 0; i < length; i++)
2063 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2065 g_string_append (value_list, value);
2066 g_string_append_c (value_list, key_file->list_separator);
2071 g_key_file_set_value (key_file, group_name, key, value_list->str);
2072 g_string_free (value_list, TRUE);
2076 * g_key_file_get_integer:
2077 * @key_file: a #GKeyFile
2078 * @group_name: a group name
2080 * @error: return location for a #GError
2082 * Returns the value associated with @key under @group_name as an
2085 * If @key cannot be found then 0 is returned and @error is set to
2086 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2087 * with @key cannot be interpreted as an integer then 0 is returned
2088 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2090 * Return value: the value associated with the key as an integer, or
2091 * 0 if the key was not found or could not be parsed.
2096 g_key_file_get_integer (GKeyFile *key_file,
2097 const gchar *group_name,
2101 GError *key_file_error;
2105 g_return_val_if_fail (key_file != NULL, -1);
2106 g_return_val_if_fail (group_name != NULL, -1);
2107 g_return_val_if_fail (key != NULL, -1);
2109 key_file_error = NULL;
2111 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2115 g_propagate_error (error, key_file_error);
2119 int_value = g_key_file_parse_value_as_integer (key_file, value,
2125 if (g_error_matches (key_file_error,
2127 G_KEY_FILE_ERROR_INVALID_VALUE))
2129 g_set_error (error, G_KEY_FILE_ERROR,
2130 G_KEY_FILE_ERROR_INVALID_VALUE,
2131 _("Key file contains key '%s' in group '%s' "
2132 "which has value that cannot be interpreted."), key,
2134 g_error_free (key_file_error);
2137 g_propagate_error (error, key_file_error);
2144 * g_key_file_set_integer:
2145 * @key_file: a #GKeyFile
2146 * @group_name: a group name
2148 * @value: an integer value
2150 * Associates a new integer value with @key under @group_name.
2151 * If @key cannot be found then it is created.
2156 g_key_file_set_integer (GKeyFile *key_file,
2157 const gchar *group_name,
2163 g_return_if_fail (key_file != NULL);
2165 result = g_key_file_parse_integer_as_value (key_file, value);
2166 g_key_file_set_value (key_file, group_name, key, result);
2171 * g_key_file_get_int64:
2172 * @key_file: a non-%NULL #GKeyFile
2173 * @group_name: a non-%NULL group name
2174 * @key: a non-%NULL key
2175 * @error: return location for a #GError
2177 * Returns the value associated with @key under @group_name as a signed
2178 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2179 * 64-bit results without truncation.
2181 * Returns: the value associated with the key as a signed 64-bit integer, or
2182 * 0 if the key was not found or could not be parsed.
2187 g_key_file_get_int64 (GKeyFile *key_file,
2188 const gchar *group_name,
2195 g_return_val_if_fail (key_file != NULL, -1);
2196 g_return_val_if_fail (group_name != NULL, -1);
2197 g_return_val_if_fail (key != NULL, -1);
2199 s = g_key_file_get_value (key_file, group_name, key, error);
2204 v = g_ascii_strtoll (s, &end, 10);
2206 if (*s == '\0' || *end != '\0')
2208 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2209 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2210 key, group_name, s);
2219 * g_key_file_set_int64:
2220 * @key_file: a #GKeyFile
2221 * @group_name: a group name
2223 * @value: an integer value
2225 * Associates a new integer value with @key under @group_name.
2226 * If @key cannot be found then it is created.
2231 g_key_file_set_int64 (GKeyFile *key_file,
2232 const gchar *group_name,
2238 g_return_if_fail (key_file != NULL);
2240 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2241 g_key_file_set_value (key_file, group_name, key, result);
2246 * g_key_file_get_uint64:
2247 * @key_file: a non-%NULL #GKeyFile
2248 * @group_name: a non-%NULL group name
2249 * @key: a non-%NULL key
2250 * @error: return location for a #GError
2252 * Returns the value associated with @key under @group_name as an unsigned
2253 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2254 * large positive results without truncation.
2256 * Returns: the value associated with the key as an unsigned 64-bit integer,
2257 * or 0 if the key was not found or could not be parsed.
2262 g_key_file_get_uint64 (GKeyFile *key_file,
2263 const gchar *group_name,
2270 g_return_val_if_fail (key_file != NULL, -1);
2271 g_return_val_if_fail (group_name != NULL, -1);
2272 g_return_val_if_fail (key != NULL, -1);
2274 s = g_key_file_get_value (key_file, group_name, key, error);
2279 v = g_ascii_strtoull (s, &end, 10);
2281 if (*s == '\0' || *end != '\0')
2283 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2284 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2285 key, group_name, s);
2294 * g_key_file_set_uint64:
2295 * @key_file: a #GKeyFile
2296 * @group_name: a group name
2298 * @value: an integer value
2300 * Associates a new integer value with @key under @group_name.
2301 * If @key cannot be found then it is created.
2306 g_key_file_set_uint64 (GKeyFile *key_file,
2307 const gchar *group_name,
2313 g_return_if_fail (key_file != NULL);
2315 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2316 g_key_file_set_value (key_file, group_name, key, result);
2321 * g_key_file_get_integer_list:
2322 * @key_file: a #GKeyFile
2323 * @group_name: a group name
2325 * @length: the number of integers returned
2326 * @error: return location for a #GError
2328 * Returns the values associated with @key under @group_name as
2331 * If @key cannot be found then %NULL is returned and @error is set to
2332 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2333 * with @key cannot be interpreted as integers then %NULL is returned
2334 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2336 * Return value: the values associated with the key as a list of
2337 * integers, or %NULL if the key was not found or could not be parsed.
2342 g_key_file_get_integer_list (GKeyFile *key_file,
2343 const gchar *group_name,
2348 GError *key_file_error = NULL;
2353 g_return_val_if_fail (key_file != NULL, NULL);
2354 g_return_val_if_fail (group_name != NULL, NULL);
2355 g_return_val_if_fail (key != NULL, NULL);
2360 values = g_key_file_get_string_list (key_file, group_name, key,
2361 &num_ints, &key_file_error);
2364 g_propagate_error (error, key_file_error);
2369 int_values = g_new (gint, num_ints);
2371 for (i = 0; i < num_ints; i++)
2373 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2379 g_propagate_error (error, key_file_error);
2380 g_strfreev (values);
2381 g_free (int_values);
2386 g_strfreev (values);
2395 * g_key_file_set_integer_list:
2396 * @key_file: a #GKeyFile
2397 * @group_name: a group name
2399 * @list: an array of integer values
2400 * @length: number of integer values in @list
2402 * Associates a list of integer values with @key under @group_name.
2403 * If @key cannot be found then it is created.
2408 g_key_file_set_integer_list (GKeyFile *key_file,
2409 const gchar *group_name,
2417 g_return_if_fail (key_file != NULL);
2418 g_return_if_fail (list != NULL);
2420 values = g_string_sized_new (length * 16);
2421 for (i = 0; i < length; i++)
2425 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2427 g_string_append (values, value);
2428 g_string_append_c (values, key_file->list_separator);
2433 g_key_file_set_value (key_file, group_name, key, values->str);
2434 g_string_free (values, TRUE);
2438 * g_key_file_get_double:
2439 * @key_file: a #GKeyFile
2440 * @group_name: a group name
2442 * @error: return location for a #GError
2444 * Returns the value associated with @key under @group_name as a
2445 * double. If @group_name is %NULL, the start_group is used.
2447 * If @key cannot be found then 0.0 is returned and @error is set to
2448 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2449 * with @key cannot be interpreted as a double then 0.0 is returned
2450 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2452 * Return value: the value associated with the key as a double, or
2453 * 0.0 if the key was not found or could not be parsed.
2458 g_key_file_get_double (GKeyFile *key_file,
2459 const gchar *group_name,
2463 GError *key_file_error;
2465 gdouble double_value;
2467 g_return_val_if_fail (key_file != NULL, -1);
2468 g_return_val_if_fail (group_name != NULL, -1);
2469 g_return_val_if_fail (key != NULL, -1);
2471 key_file_error = NULL;
2473 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2477 g_propagate_error (error, key_file_error);
2481 double_value = g_key_file_parse_value_as_double (key_file, value,
2487 if (g_error_matches (key_file_error,
2489 G_KEY_FILE_ERROR_INVALID_VALUE))
2491 g_set_error (error, G_KEY_FILE_ERROR,
2492 G_KEY_FILE_ERROR_INVALID_VALUE,
2493 _("Key file contains key '%s' in group '%s' "
2494 "which has value that cannot be interpreted."), key,
2496 g_error_free (key_file_error);
2499 g_propagate_error (error, key_file_error);
2502 return double_value;
2506 * g_key_file_set_double:
2507 * @key_file: a #GKeyFile
2508 * @group_name: a group name
2510 * @value: an double value
2512 * Associates a new double value with @key under @group_name.
2513 * If @key cannot be found then it is created.
2518 g_key_file_set_double (GKeyFile *key_file,
2519 const gchar *group_name,
2523 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2525 g_return_if_fail (key_file != NULL);
2527 g_ascii_dtostr (result, sizeof (result), value);
2528 g_key_file_set_value (key_file, group_name, key, result);
2532 * g_key_file_get_double_list:
2533 * @key_file: a #GKeyFile
2534 * @group_name: a group name
2536 * @length: the number of doubles returned
2537 * @error: return location for a #GError
2539 * Returns the values associated with @key under @group_name as
2542 * If @key cannot be found then %NULL is returned and @error is set to
2543 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2544 * with @key cannot be interpreted as doubles then %NULL is returned
2545 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2547 * Return value: the values associated with the key as a list of
2548 * doubles, or %NULL if the key was not found or could not be parsed.
2553 g_key_file_get_double_list (GKeyFile *key_file,
2554 const gchar *group_name,
2559 GError *key_file_error = NULL;
2561 gdouble *double_values;
2562 gsize i, num_doubles;
2564 g_return_val_if_fail (key_file != NULL, NULL);
2565 g_return_val_if_fail (group_name != NULL, NULL);
2566 g_return_val_if_fail (key != NULL, NULL);
2571 values = g_key_file_get_string_list (key_file, group_name, key,
2572 &num_doubles, &key_file_error);
2575 g_propagate_error (error, key_file_error);
2580 double_values = g_new (gdouble, num_doubles);
2582 for (i = 0; i < num_doubles; i++)
2584 double_values[i] = g_key_file_parse_value_as_double (key_file,
2590 g_propagate_error (error, key_file_error);
2591 g_strfreev (values);
2592 g_free (double_values);
2597 g_strfreev (values);
2600 *length = num_doubles;
2602 return double_values;
2606 * g_key_file_set_double_list:
2607 * @key_file: a #GKeyFile
2608 * @group_name: a group name
2610 * @list: an array of double values
2611 * @length: number of double values in @list
2613 * Associates a list of double values with @key under
2614 * @group_name. If @key cannot be found then it is created.
2619 g_key_file_set_double_list (GKeyFile *key_file,
2620 const gchar *group_name,
2628 g_return_if_fail (key_file != NULL);
2629 g_return_if_fail (list != NULL);
2631 values = g_string_sized_new (length * 16);
2632 for (i = 0; i < length; i++)
2634 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2636 g_ascii_dtostr( result, sizeof (result), list[i] );
2638 g_string_append (values, result);
2639 g_string_append_c (values, key_file->list_separator);
2642 g_key_file_set_value (key_file, group_name, key, values->str);
2643 g_string_free (values, TRUE);
2647 g_key_file_set_key_comment (GKeyFile *key_file,
2648 const gchar *group_name,
2650 const gchar *comment,
2653 GKeyFileGroup *group;
2654 GKeyFileKeyValuePair *pair;
2655 GList *key_node, *comment_node, *tmp;
2657 group = g_key_file_lookup_group (key_file, group_name);
2660 g_set_error (error, G_KEY_FILE_ERROR,
2661 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2662 _("Key file does not have group '%s'"),
2663 group_name ? group_name : "(null)");
2668 /* First find the key the comments are supposed to be
2671 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2673 if (key_node == NULL)
2675 g_set_error (error, G_KEY_FILE_ERROR,
2676 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2677 _("Key file does not have key '%s' in group '%s'"),
2682 /* Then find all the comments already associated with the
2685 tmp = key_node->next;
2688 pair = (GKeyFileKeyValuePair *) tmp->data;
2690 if (pair->key != NULL)
2695 g_key_file_remove_key_value_pair_node (key_file, group,
2699 if (comment == NULL)
2702 /* Now we can add our new comment
2704 pair = g_slice_new (GKeyFileKeyValuePair);
2706 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2708 key_node = g_list_insert (key_node, pair, 1);
2714 g_key_file_set_group_comment (GKeyFile *key_file,
2715 const gchar *group_name,
2716 const gchar *comment,
2719 GKeyFileGroup *group;
2721 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2723 group = g_key_file_lookup_group (key_file, group_name);
2726 g_set_error (error, G_KEY_FILE_ERROR,
2727 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2728 _("Key file does not have group '%s'"),
2729 group_name ? group_name : "(null)");
2734 /* First remove any existing comment
2738 g_key_file_key_value_pair_free (group->comment);
2739 group->comment = NULL;
2742 if (comment == NULL)
2745 /* Now we can add our new comment
2747 group->comment = g_slice_new (GKeyFileKeyValuePair);
2748 group->comment->key = NULL;
2749 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2755 g_key_file_set_top_comment (GKeyFile *key_file,
2756 const gchar *comment,
2760 GKeyFileGroup *group;
2761 GKeyFileKeyValuePair *pair;
2763 /* The last group in the list should be the top (comments only)
2766 g_warn_if_fail (key_file->groups != NULL);
2767 group_node = g_list_last (key_file->groups);
2768 group = (GKeyFileGroup *) group_node->data;
2769 g_warn_if_fail (group->name == NULL);
2771 /* Note all keys must be comments at the top of
2772 * the file, so we can just free it all.
2774 if (group->key_value_pairs != NULL)
2776 g_list_foreach (group->key_value_pairs,
2777 (GFunc) g_key_file_key_value_pair_free,
2779 g_list_free (group->key_value_pairs);
2780 group->key_value_pairs = NULL;
2783 if (comment == NULL)
2786 pair = g_slice_new (GKeyFileKeyValuePair);
2788 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2790 group->key_value_pairs =
2791 g_list_prepend (group->key_value_pairs, pair);
2797 * g_key_file_set_comment:
2798 * @key_file: a #GKeyFile
2799 * @group_name: a group name, or %NULL
2801 * @comment: a comment
2802 * @error: return location for a #GError
2804 * Places a comment above @key from @group_name.
2805 * If @key is %NULL then @comment will be written above @group_name.
2806 * If both @key and @group_name are %NULL, then @comment will be
2807 * written above the first group in the file.
2809 * Returns: %TRUE if the comment was written, %FALSE otherwise
2814 g_key_file_set_comment (GKeyFile *key_file,
2815 const gchar *group_name,
2817 const gchar *comment,
2820 g_return_val_if_fail (key_file != NULL, FALSE);
2822 if (group_name != NULL && key != NULL)
2824 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2827 else if (group_name != NULL)
2829 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2834 if (!g_key_file_set_top_comment (key_file, comment, error))
2838 if (comment != NULL)
2839 key_file->approximate_size += strlen (comment);
2845 g_key_file_get_key_comment (GKeyFile *key_file,
2846 const gchar *group_name,
2850 GKeyFileGroup *group;
2851 GKeyFileKeyValuePair *pair;
2852 GList *key_node, *tmp;
2856 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2858 group = g_key_file_lookup_group (key_file, group_name);
2861 g_set_error (error, G_KEY_FILE_ERROR,
2862 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2863 _("Key file does not have group '%s'"),
2864 group_name ? group_name : "(null)");
2869 /* First find the key the comments are supposed to be
2872 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2874 if (key_node == NULL)
2876 g_set_error (error, G_KEY_FILE_ERROR,
2877 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2878 _("Key file does not have key '%s' in group '%s'"),
2885 /* Then find all the comments already associated with the
2886 * key and concatentate them.
2888 tmp = key_node->next;
2889 if (!key_node->next)
2892 pair = (GKeyFileKeyValuePair *) tmp->data;
2893 if (pair->key != NULL)
2898 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2900 if (pair->key != NULL)
2906 while (tmp != key_node)
2908 pair = (GKeyFileKeyValuePair *) tmp->data;
2911 string = g_string_sized_new (512);
2913 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2914 g_string_append (string, comment);
2922 comment = string->str;
2923 g_string_free (string, FALSE);
2932 get_group_comment (GKeyFile *key_file,
2933 GKeyFileGroup *group,
2942 tmp = group->key_value_pairs;
2945 GKeyFileKeyValuePair *pair;
2947 pair = (GKeyFileKeyValuePair *) tmp->data;
2949 if (pair->key != NULL)
2955 if (tmp->next == NULL)
2963 GKeyFileKeyValuePair *pair;
2965 pair = (GKeyFileKeyValuePair *) tmp->data;
2968 string = g_string_sized_new (512);
2970 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2971 g_string_append (string, comment);
2978 return g_string_free (string, FALSE);
2984 g_key_file_get_group_comment (GKeyFile *key_file,
2985 const gchar *group_name,
2989 GKeyFileGroup *group;
2991 group = g_key_file_lookup_group (key_file, group_name);
2994 g_set_error (error, G_KEY_FILE_ERROR,
2995 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2996 _("Key file does not have group '%s'"),
2997 group_name ? group_name : "(null)");
3003 return g_strdup (group->comment->value);
3005 group_node = g_key_file_lookup_group_node (key_file, group_name);
3006 group_node = group_node->next;
3007 group = (GKeyFileGroup *)group_node->data;
3008 return get_group_comment (key_file, group, error);
3012 g_key_file_get_top_comment (GKeyFile *key_file,
3016 GKeyFileGroup *group;
3018 /* The last group in the list should be the top (comments only)
3021 g_warn_if_fail (key_file->groups != NULL);
3022 group_node = g_list_last (key_file->groups);
3023 group = (GKeyFileGroup *) group_node->data;
3024 g_warn_if_fail (group->name == NULL);
3026 return get_group_comment (key_file, group, error);
3030 * g_key_file_get_comment:
3031 * @key_file: a #GKeyFile
3032 * @group_name: a group name, or %NULL
3034 * @error: return location for a #GError
3036 * Retrieves a comment above @key from @group_name.
3037 * If @key is %NULL then @comment will be read from above
3038 * @group_name. If both @key and @group_name are %NULL, then
3039 * @comment will be read from above the first group in the file.
3041 * Returns: a comment that should be freed with g_free()
3046 g_key_file_get_comment (GKeyFile *key_file,
3047 const gchar *group_name,
3051 g_return_val_if_fail (key_file != NULL, NULL);
3053 if (group_name != NULL && key != NULL)
3054 return g_key_file_get_key_comment (key_file, group_name, key, error);
3055 else if (group_name != NULL)
3056 return g_key_file_get_group_comment (key_file, group_name, error);
3058 return g_key_file_get_top_comment (key_file, error);
3062 * g_key_file_remove_comment:
3063 * @key_file: a #GKeyFile
3064 * @group_name: a group name, or %NULL
3066 * @error: return location for a #GError
3068 * Removes a comment above @key from @group_name.
3069 * If @key is %NULL then @comment will be removed above @group_name.
3070 * If both @key and @group_name are %NULL, then @comment will
3071 * be removed above the first group in the file.
3073 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3079 g_key_file_remove_comment (GKeyFile *key_file,
3080 const gchar *group_name,
3084 g_return_val_if_fail (key_file != NULL, FALSE);
3086 if (group_name != NULL && key != NULL)
3087 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3088 else if (group_name != NULL)
3089 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3091 return g_key_file_set_top_comment (key_file, NULL, error);
3095 * g_key_file_has_group:
3096 * @key_file: a #GKeyFile
3097 * @group_name: a group name
3099 * Looks whether the key file has the group @group_name.
3101 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3106 g_key_file_has_group (GKeyFile *key_file,
3107 const gchar *group_name)
3109 g_return_val_if_fail (key_file != NULL, FALSE);
3110 g_return_val_if_fail (group_name != NULL, FALSE);
3112 return g_key_file_lookup_group (key_file, group_name) != NULL;
3116 * g_key_file_has_key:
3117 * @key_file: a #GKeyFile
3118 * @group_name: a group name
3120 * @error: return location for a #GError
3122 * Looks whether the key file has the key @key in the group
3125 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3131 g_key_file_has_key (GKeyFile *key_file,
3132 const gchar *group_name,
3136 GKeyFileKeyValuePair *pair;
3137 GKeyFileGroup *group;
3139 g_return_val_if_fail (key_file != NULL, FALSE);
3140 g_return_val_if_fail (group_name != NULL, FALSE);
3141 g_return_val_if_fail (key != NULL, FALSE);
3143 group = g_key_file_lookup_group (key_file, group_name);
3147 g_set_error (error, G_KEY_FILE_ERROR,
3148 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3149 _("Key file does not have group '%s'"),
3150 group_name ? group_name : "(null)");
3155 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3157 return pair != NULL;
3161 g_key_file_add_group (GKeyFile *key_file,
3162 const gchar *group_name)
3164 GKeyFileGroup *group;
3166 g_return_if_fail (key_file != NULL);
3167 g_return_if_fail (g_key_file_is_group_name (group_name));
3169 group = g_key_file_lookup_group (key_file, group_name);
3172 key_file->current_group = group;
3176 group = g_slice_new0 (GKeyFileGroup);
3177 group->name = g_strdup (group_name);
3178 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3179 key_file->groups = g_list_prepend (key_file->groups, group);
3180 key_file->approximate_size += strlen (group_name) + 3;
3181 key_file->current_group = group;
3183 if (key_file->start_group == NULL)
3184 key_file->start_group = group;
3186 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3190 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3195 g_free (pair->value);
3196 g_slice_free (GKeyFileKeyValuePair, pair);
3200 /* Be careful not to call this function on a node with data in the
3201 * lookup map without removing it from the lookup map, first.
3203 * Some current cases where this warning is not a concern are
3205 * - the node being removed is a comment node
3206 * - the entire lookup map is getting destroyed soon after
3210 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3211 GKeyFileGroup *group,
3215 GKeyFileKeyValuePair *pair;
3217 pair = (GKeyFileKeyValuePair *) pair_node->data;
3219 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3221 if (pair->key != NULL)
3222 key_file->approximate_size -= strlen (pair->key) + 1;
3224 g_warn_if_fail (pair->value != NULL);
3225 key_file->approximate_size -= strlen (pair->value);
3227 g_key_file_key_value_pair_free (pair);
3229 g_list_free_1 (pair_node);
3233 g_key_file_remove_group_node (GKeyFile *key_file,
3236 GKeyFileGroup *group;
3239 group = (GKeyFileGroup *) group_node->data;
3242 g_hash_table_remove (key_file->group_hash, group->name);
3244 /* If the current group gets deleted make the current group the last
3247 if (key_file->current_group == group)
3249 /* groups should always contain at least the top comment group,
3250 * unless g_key_file_clear has been called
3252 if (key_file->groups)
3253 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3255 key_file->current_group = NULL;
3258 /* If the start group gets deleted make the start group the first
3261 if (key_file->start_group == group)
3263 tmp = g_list_last (key_file->groups);
3266 if (tmp != group_node &&
3267 ((GKeyFileGroup *) tmp->data)->name != NULL)
3274 key_file->start_group = (GKeyFileGroup *) tmp->data;
3276 key_file->start_group = NULL;
3279 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3281 if (group->name != NULL)
3282 key_file->approximate_size -= strlen (group->name) + 3;
3284 tmp = group->key_value_pairs;
3291 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3294 g_warn_if_fail (group->key_value_pairs == NULL);
3296 if (group->lookup_map)
3298 g_hash_table_destroy (group->lookup_map);
3299 group->lookup_map = NULL;
3302 g_free ((gchar *) group->name);
3303 g_slice_free (GKeyFileGroup, group);
3304 g_list_free_1 (group_node);
3308 * g_key_file_remove_group:
3309 * @key_file: a #GKeyFile
3310 * @group_name: a group name
3311 * @error: return location for a #GError or %NULL
3313 * Removes the specified group, @group_name,
3314 * from the key file.
3316 * Returns: %TRUE if the group was removed, %FALSE otherwise
3321 g_key_file_remove_group (GKeyFile *key_file,
3322 const gchar *group_name,
3327 g_return_val_if_fail (key_file != NULL, FALSE);
3328 g_return_val_if_fail (group_name != NULL, FALSE);
3330 group_node = g_key_file_lookup_group_node (key_file, group_name);
3334 g_set_error (error, G_KEY_FILE_ERROR,
3335 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3336 _("Key file does not have group '%s'"),
3341 g_key_file_remove_group_node (key_file, group_node);
3347 g_key_file_add_key_value_pair (GKeyFile *key_file,
3348 GKeyFileGroup *group,
3349 GKeyFileKeyValuePair *pair)
3351 g_hash_table_replace (group->lookup_map, pair->key, pair);
3352 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3353 group->has_trailing_blank_line = FALSE;
3354 key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2;
3358 g_key_file_add_key (GKeyFile *key_file,
3359 GKeyFileGroup *group,
3363 GKeyFileKeyValuePair *pair;
3365 pair = g_slice_new (GKeyFileKeyValuePair);
3366 pair->key = g_strdup (key);
3367 pair->value = g_strdup (value);
3369 g_key_file_add_key_value_pair (key_file, group, pair);
3373 * g_key_file_remove_key:
3374 * @key_file: a #GKeyFile
3375 * @group_name: a group name
3376 * @key: a key name to remove
3377 * @error: return location for a #GError or %NULL
3379 * Removes @key in @group_name from the key file.
3381 * Returns: %TRUE if the key was removed, %FALSE otherwise
3386 g_key_file_remove_key (GKeyFile *key_file,
3387 const gchar *group_name,
3391 GKeyFileGroup *group;
3392 GKeyFileKeyValuePair *pair;
3394 g_return_val_if_fail (key_file != NULL, FALSE);
3395 g_return_val_if_fail (group_name != NULL, FALSE);
3396 g_return_val_if_fail (key != NULL, FALSE);
3400 group = g_key_file_lookup_group (key_file, group_name);
3403 g_set_error (error, G_KEY_FILE_ERROR,
3404 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3405 _("Key file does not have group '%s'"),
3406 group_name ? group_name : "(null)");
3410 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3414 g_set_error (error, G_KEY_FILE_ERROR,
3415 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3416 _("Key file does not have key '%s' in group '%s'"),
3421 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3423 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3424 g_hash_table_remove (group->lookup_map, pair->key);
3425 g_key_file_key_value_pair_free (pair);
3431 g_key_file_lookup_group_node (GKeyFile *key_file,
3432 const gchar *group_name)
3434 GKeyFileGroup *group;
3437 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3439 group = (GKeyFileGroup *) tmp->data;
3441 if (group && group->name && strcmp (group->name, group_name) == 0)
3448 static GKeyFileGroup *
3449 g_key_file_lookup_group (GKeyFile *key_file,
3450 const gchar *group_name)
3452 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3456 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3457 GKeyFileGroup *group,
3462 for (key_node = group->key_value_pairs;
3464 key_node = key_node->next)
3466 GKeyFileKeyValuePair *pair;
3468 pair = (GKeyFileKeyValuePair *) key_node->data;
3470 if (pair->key && strcmp (pair->key, key) == 0)
3477 static GKeyFileKeyValuePair *
3478 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3479 GKeyFileGroup *group,
3482 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3485 /* Lines starting with # or consisting entirely of whitespace are merely
3486 * recorded, not parsed. This function assumes all leading whitespace
3487 * has been stripped.
3490 g_key_file_line_is_comment (const gchar *line)
3492 return (*line == '#' || *line == '\0' || *line == '\n');
3496 g_key_file_is_group_name (const gchar *name)
3503 p = q = (gchar *) name;
3504 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3505 q = g_utf8_find_next_char (q, NULL);
3507 if (*q != '\0' || q == p)
3514 g_key_file_is_key_name (const gchar *name)
3521 p = q = (gchar *) name;
3522 /* We accept a little more than the desktop entry spec says,
3523 * since gnome-vfs uses mime-types as keys in its cache.
3525 while (*q && *q != '=' && *q != '[' && *q != ']')
3526 q = g_utf8_find_next_char (q, NULL);
3528 /* No empty keys, please */
3532 /* We accept spaces in the middle of keys to not break
3533 * existing apps, but we don't tolerate initial or final
3534 * spaces, which would lead to silent corruption when
3535 * rereading the file.
3537 if (*p == ' ' || q[-1] == ' ')
3543 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3544 q = g_utf8_find_next_char (q, NULL);
3558 /* A group in a key file is made up of a starting '[' followed by one
3559 * or more letters making up the group name followed by ']'.
3562 g_key_file_line_is_group (const gchar *line)
3572 while (*p && *p != ']')
3573 p = g_utf8_find_next_char (p, NULL);
3578 /* silently accept whitespace after the ] */
3579 p = g_utf8_find_next_char (p, NULL);
3580 while (*p == ' ' || *p == '\t')
3581 p = g_utf8_find_next_char (p, NULL);
3590 g_key_file_line_is_key_value_pair (const gchar *line)
3594 p = (gchar *) g_utf8_strchr (line, -1, '=');
3599 /* Key must be non-empty
3608 g_key_file_parse_value_as_string (GKeyFile *key_file,
3613 gchar *string_value, *p, *q0, *q;
3615 string_value = g_new (gchar, strlen (value) + 1);
3617 p = (gchar *) value;
3618 q0 = q = string_value;
3648 g_set_error_literal (error, G_KEY_FILE_ERROR,
3649 G_KEY_FILE_ERROR_INVALID_VALUE,
3650 _("Key file contains escape character "
3655 if (pieces && *p == key_file->list_separator)
3656 *q = key_file->list_separator;
3670 g_set_error (error, G_KEY_FILE_ERROR,
3671 G_KEY_FILE_ERROR_INVALID_VALUE,
3672 _("Key file contains invalid escape "
3673 "sequence '%s'"), sequence);
3682 if (pieces && (*p == key_file->list_separator))
3684 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3700 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3701 *pieces = g_slist_reverse (*pieces);
3704 return string_value;
3708 g_key_file_parse_string_as_value (GKeyFile *key_file,
3709 const gchar *string,
3710 gboolean escape_separator)
3712 gchar *value, *p, *q;
3714 gboolean parsing_leading_space;
3716 length = strlen (string) + 1;
3718 /* Worst case would be that every character needs to be escaped.
3719 * In other words every character turns to two characters
3721 value = g_new (gchar, 2 * length);
3723 p = (gchar *) string;
3725 parsing_leading_space = TRUE;
3726 while (p < (string + length - 1))
3728 gchar escaped_character[3] = { '\\', 0, 0 };
3733 if (parsing_leading_space)
3735 escaped_character[1] = 's';
3736 strcpy (q, escaped_character);
3746 if (parsing_leading_space)
3748 escaped_character[1] = 't';
3749 strcpy (q, escaped_character);
3759 escaped_character[1] = 'n';
3760 strcpy (q, escaped_character);
3764 escaped_character[1] = 'r';
3765 strcpy (q, escaped_character);
3769 escaped_character[1] = '\\';
3770 strcpy (q, escaped_character);
3772 parsing_leading_space = FALSE;
3775 if (escape_separator && *p == key_file->list_separator)
3777 escaped_character[1] = key_file->list_separator;
3778 strcpy (q, escaped_character);
3780 parsing_leading_space = TRUE;
3786 parsing_leading_space = FALSE;
3798 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3802 gchar *end_of_valid_int;
3807 long_value = strtol (value, &end_of_valid_int, 10);
3809 if (*value == '\0' || *end_of_valid_int != '\0')
3811 gchar *value_utf8 = _g_utf8_make_valid (value);
3812 g_set_error (error, G_KEY_FILE_ERROR,
3813 G_KEY_FILE_ERROR_INVALID_VALUE,
3814 _("Value '%s' cannot be interpreted "
3815 "as a number."), value_utf8);
3816 g_free (value_utf8);
3821 int_value = long_value;
3822 if (int_value != long_value || errno == ERANGE)
3824 gchar *value_utf8 = _g_utf8_make_valid (value);
3827 G_KEY_FILE_ERROR_INVALID_VALUE,
3828 _("Integer value '%s' out of range"),
3830 g_free (value_utf8);
3839 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3843 return g_strdup_printf ("%d", value);
3847 g_key_file_parse_value_as_double (GKeyFile *key_file,
3851 gchar *end_of_valid_d;
3852 gdouble double_value = 0;
3854 double_value = g_ascii_strtod (value, &end_of_valid_d);
3856 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3858 gchar *value_utf8 = _g_utf8_make_valid (value);
3859 g_set_error (error, G_KEY_FILE_ERROR,
3860 G_KEY_FILE_ERROR_INVALID_VALUE,
3861 _("Value '%s' cannot be interpreted "
3862 "as a float number."),
3864 g_free (value_utf8);
3867 return double_value;
3871 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3877 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3879 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3882 value_utf8 = _g_utf8_make_valid (value);
3883 g_set_error (error, G_KEY_FILE_ERROR,
3884 G_KEY_FILE_ERROR_INVALID_VALUE,
3885 _("Value '%s' cannot be interpreted "
3886 "as a boolean."), value_utf8);
3887 g_free (value_utf8);
3893 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3897 return g_strdup ("true");
3899 return g_strdup ("false");
3903 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3910 string = g_string_sized_new (512);
3912 lines = g_strsplit (value, "\n", 0);
3914 for (i = 0; lines[i] != NULL; i++)
3916 if (lines[i][0] != '#')
3917 g_string_append_printf (string, "%s\n", lines[i]);
3919 g_string_append_printf (string, "%s\n", lines[i] + 1);
3923 return g_string_free (string, FALSE);
3927 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3928 const gchar *comment)
3934 string = g_string_sized_new (512);
3936 lines = g_strsplit (comment, "\n", 0);
3938 for (i = 0; lines[i] != NULL; i++)
3939 g_string_append_printf (string, "#%s%s", lines[i],
3940 lines[i + 1] == NULL? "" : "\n");
3943 return g_string_free (string, FALSE);