1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
4 * Copyright 2009-2010 Collabora Ltd.
5 * Copyright 2009 Nokia Corporation
7 * Written by Ray Strode <rstrode@redhat.com>
8 * Matthias Clasen <mclasen@redhat.com>
10 * GLib is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * GLib is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GLib; see the file COPYING.LIB. If not,
22 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
37 #include <sys/types.h>
45 #define fstat(a,b) _fstati64(a,b)
49 #define S_ISREG(mode) ((mode)&_S_IFREG)
52 #endif /* G_OS_WIN23 */
57 #include "gfileutils.h"
63 #include "gmessages.h"
66 #include "gstrfuncs.h"
70 typedef struct _GKeyFileGroup GKeyFileGroup;
75 GHashTable *group_hash;
77 GKeyFileGroup *start_group;
78 GKeyFileGroup *current_group;
80 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
82 /* Used for sizing the output buffer during serialization
84 gsize approximate_size;
93 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
97 const gchar *name; /* NULL for above first group (which will be comments) */
99 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
100 gboolean has_trailing_blank_line;
102 GList *key_value_pairs;
104 /* Used in parallel with key_value_pairs for
105 * increased lookup performance
107 GHashTable *lookup_map;
110 struct _GKeyFileKeyValuePair
112 gchar *key; /* NULL for comments */
116 static gint find_file_in_data_dirs (const gchar *file,
117 const gchar **data_dirs,
120 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
124 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
125 const gchar *group_name);
126 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
127 const gchar *group_name);
129 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
130 GKeyFileGroup *group,
132 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
133 GKeyFileGroup *group,
136 static void g_key_file_remove_group_node (GKeyFile *key_file,
138 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
139 GKeyFileGroup *group,
142 static void g_key_file_add_key (GKeyFile *key_file,
143 GKeyFileGroup *group,
146 static void g_key_file_add_group (GKeyFile *key_file,
147 const gchar *group_name);
148 static gboolean g_key_file_is_group_name (const gchar *name);
149 static gboolean g_key_file_is_key_name (const gchar *name);
150 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
151 static gboolean g_key_file_line_is_comment (const gchar *line);
152 static gboolean g_key_file_line_is_group (const gchar *line);
153 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
154 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
158 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
160 gboolean escape_separator);
161 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
164 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
166 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
169 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
172 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
174 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
176 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
177 const gchar *comment);
178 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
182 static void g_key_file_parse_comment (GKeyFile *key_file,
186 static void g_key_file_parse_group (GKeyFile *key_file,
190 static gchar *key_get_locale (const gchar *key);
191 static void g_key_file_parse_data (GKeyFile *key_file,
195 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
200 g_key_file_error_quark (void)
202 return g_quark_from_static_string ("g-key-file-error-quark");
206 g_key_file_init (GKeyFile *key_file)
208 key_file->current_group = g_slice_new0 (GKeyFileGroup);
209 key_file->groups = g_list_prepend (NULL, key_file->current_group);
210 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
211 key_file->start_group = NULL;
212 key_file->parse_buffer = g_string_sized_new (128);
213 key_file->approximate_size = 0;
214 key_file->list_separator = ';';
216 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
220 g_key_file_clear (GKeyFile *key_file)
222 GList *tmp, *group_node;
224 if (key_file->locales)
226 g_strfreev (key_file->locales);
227 key_file->locales = NULL;
230 if (key_file->parse_buffer)
232 g_string_free (key_file->parse_buffer, TRUE);
233 key_file->parse_buffer = NULL;
236 tmp = key_file->groups;
241 g_key_file_remove_group_node (key_file, group_node);
244 g_hash_table_destroy (key_file->group_hash);
245 key_file->group_hash = NULL;
247 g_warn_if_fail (key_file->groups == NULL);
254 * Creates a new empty #GKeyFile object. Use
255 * g_key_file_load_from_file(), g_key_file_load_from_data(),
256 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
257 * read an existing key file.
259 * Return value: an empty #GKeyFile.
264 g_key_file_new (void)
268 key_file = g_slice_new0 (GKeyFile);
269 g_key_file_init (key_file);
275 * g_key_file_set_list_separator:
276 * @key_file: a #GKeyFile
277 * @separator: the separator
279 * Sets the character which is used to separate
280 * values in lists. Typically ';' or ',' are used
281 * as separators. The default list separator is ';'.
286 g_key_file_set_list_separator (GKeyFile *key_file,
289 g_return_if_fail (key_file != NULL);
291 key_file->list_separator = separator;
295 /* Iterates through all the directories in *dirs trying to
296 * open file. When it successfully locates and opens a file it
297 * returns the file descriptor to the open file. It also
298 * outputs the absolute path of the file in output_file.
301 find_file_in_data_dirs (const gchar *file,
306 const gchar **data_dirs, *data_dir;
318 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
320 gchar *candidate_file, *sub_dir;
322 candidate_file = (gchar *) file;
323 sub_dir = g_strdup ("");
324 while (candidate_file != NULL && fd < 0)
328 path = g_build_filename (data_dir, sub_dir,
329 candidate_file, NULL);
331 fd = g_open (path, O_RDONLY, 0);
339 candidate_file = strchr (candidate_file, '-');
341 if (candidate_file == NULL)
347 sub_dir = g_strndup (file, candidate_file - file - 1);
349 for (p = sub_dir; *p != '\0'; p++)
352 *p = G_DIR_SEPARATOR;
361 g_set_error_literal (error, G_KEY_FILE_ERROR,
362 G_KEY_FILE_ERROR_NOT_FOUND,
363 _("Valid key file could not be "
364 "found in search dirs"));
367 if (output_file != NULL && fd > 0)
368 *output_file = g_strdup (path);
376 g_key_file_load_from_fd (GKeyFile *key_file,
381 GError *key_file_error = NULL;
383 struct stat stat_buf;
384 gchar read_buf[4096];
386 if (fstat (fd, &stat_buf) < 0)
388 g_set_error_literal (error, G_FILE_ERROR,
389 g_file_error_from_errno (errno),
394 if (!S_ISREG (stat_buf.st_mode))
396 g_set_error_literal (error, G_KEY_FILE_ERROR,
397 G_KEY_FILE_ERROR_PARSE,
398 _("Not a regular file"));
402 if (stat_buf.st_size == 0)
404 g_set_error_literal (error, G_KEY_FILE_ERROR,
405 G_KEY_FILE_ERROR_PARSE,
410 if (key_file->approximate_size > 0)
412 g_key_file_clear (key_file);
413 g_key_file_init (key_file);
415 key_file->flags = flags;
419 bytes_read = read (fd, read_buf, 4096);
421 if (bytes_read == 0) /* End of File */
426 if (errno == EINTR || errno == EAGAIN)
429 g_set_error_literal (error, G_FILE_ERROR,
430 g_file_error_from_errno (errno),
435 g_key_file_parse_data (key_file,
436 read_buf, bytes_read,
439 while (!key_file_error);
443 g_propagate_error (error, key_file_error);
447 g_key_file_flush_parse_buffer (key_file, &key_file_error);
451 g_propagate_error (error, key_file_error);
459 * g_key_file_load_from_file:
460 * @key_file: an empty #GKeyFile struct
461 * @file: the path of a filename to load, in the GLib filename encoding
462 * @flags: flags from #GKeyFileFlags
463 * @error: return location for a #GError, or %NULL
465 * Loads a key file into an empty #GKeyFile structure.
466 * If the file could not be loaded then %error is set to
467 * either a #GFileError or #GKeyFileError.
469 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
474 g_key_file_load_from_file (GKeyFile *key_file,
479 GError *key_file_error = NULL;
482 g_return_val_if_fail (key_file != NULL, FALSE);
483 g_return_val_if_fail (file != NULL, FALSE);
485 fd = g_open (file, O_RDONLY, 0);
489 g_set_error_literal (error, G_FILE_ERROR,
490 g_file_error_from_errno (errno),
495 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
500 g_propagate_error (error, key_file_error);
508 * g_key_file_load_from_data:
509 * @key_file: an empty #GKeyFile struct
510 * @data: key file loaded in memory
511 * @length: the length of @data in bytes
512 * @flags: flags from #GKeyFileFlags
513 * @error: return location for a #GError, or %NULL
515 * Loads a key file from memory into an empty #GKeyFile structure.
516 * If the object cannot be created then %error is set to a #GKeyFileError.
518 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
523 g_key_file_load_from_data (GKeyFile *key_file,
529 GError *key_file_error = NULL;
531 g_return_val_if_fail (key_file != NULL, FALSE);
532 g_return_val_if_fail (data != NULL, FALSE);
533 g_return_val_if_fail (length != 0, FALSE);
535 if (length == (gsize)-1)
536 length = strlen (data);
538 if (key_file->approximate_size > 0)
540 g_key_file_clear (key_file);
541 g_key_file_init (key_file);
543 key_file->flags = flags;
545 g_key_file_parse_data (key_file, data, length, &key_file_error);
549 g_propagate_error (error, key_file_error);
553 g_key_file_flush_parse_buffer (key_file, &key_file_error);
557 g_propagate_error (error, key_file_error);
565 * g_key_file_load_from_dirs:
566 * @key_file: an empty #GKeyFile struct
567 * @file: a relative path to a filename to open and parse
568 * @search_dirs: %NULL-terminated array of directories to search
569 * @full_path: return location for a string containing the full path
570 * of the file, or %NULL
571 * @flags: flags from #GKeyFileFlags
572 * @error: return location for a #GError, or %NULL
574 * This function looks for a key file named @file in the paths
575 * specified in @search_dirs, loads the file into @key_file and
576 * returns the file's full path in @full_path. If the file could not
577 * be loaded then an %error is set to either a #GFileError or
580 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
585 g_key_file_load_from_dirs (GKeyFile *key_file,
587 const gchar **search_dirs,
592 GError *key_file_error = NULL;
593 const gchar **data_dirs;
598 g_return_val_if_fail (key_file != NULL, FALSE);
599 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
600 g_return_val_if_fail (search_dirs != NULL, FALSE);
603 data_dirs = search_dirs;
605 while (*data_dirs != NULL && !found_file)
607 g_free (output_path);
609 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
615 g_propagate_error (error, key_file_error);
619 found_file = g_key_file_load_from_fd (key_file, fd, flags,
625 g_propagate_error (error, key_file_error);
630 if (found_file && full_path)
631 *full_path = output_path;
633 g_free (output_path);
639 * g_key_file_load_from_data_dirs:
640 * @key_file: an empty #GKeyFile struct
641 * @file: a relative path to a filename to open and parse
642 * @full_path: return location for a string containing the full path
643 * of the file, or %NULL
644 * @flags: flags from #GKeyFileFlags
645 * @error: return location for a #GError, or %NULL
647 * This function looks for a key file named @file in the paths
648 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
649 * loads the file into @key_file and returns the file's full path in
650 * @full_path. If the file could not be loaded then an %error is
651 * set to either a #GFileError or #GKeyFileError.
653 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
657 g_key_file_load_from_data_dirs (GKeyFile *key_file,
663 gchar **all_data_dirs;
664 const gchar * user_data_dir;
665 const gchar * const * system_data_dirs;
669 g_return_val_if_fail (key_file != NULL, FALSE);
670 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
672 user_data_dir = g_get_user_data_dir ();
673 system_data_dirs = g_get_system_data_dirs ();
674 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
677 all_data_dirs[i++] = g_strdup (user_data_dir);
680 while (system_data_dirs[j] != NULL)
681 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
682 all_data_dirs[i] = NULL;
684 found_file = g_key_file_load_from_dirs (key_file,
686 (const gchar **)all_data_dirs,
691 g_strfreev (all_data_dirs);
698 * @key_file: a #GKeyFile
705 g_key_file_free (GKeyFile *key_file)
707 g_return_if_fail (key_file != NULL);
709 g_key_file_clear (key_file);
710 g_slice_free (GKeyFile, key_file);
713 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
714 * true for locales that match those in g_get_language_names().
717 g_key_file_locale_is_interesting (GKeyFile *key_file,
722 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
725 for (i = 0; key_file->locales[i] != NULL; i++)
727 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
735 g_key_file_parse_line (GKeyFile *key_file,
740 GError *parse_error = NULL;
743 g_return_if_fail (key_file != NULL);
744 g_return_if_fail (line != NULL);
746 line_start = (gchar *) line;
747 while (g_ascii_isspace (*line_start))
750 if (g_key_file_line_is_comment (line_start))
751 g_key_file_parse_comment (key_file, line, length, &parse_error);
752 else if (g_key_file_line_is_group (line_start))
753 g_key_file_parse_group (key_file, line_start,
754 length - (line_start - line),
756 else if (g_key_file_line_is_key_value_pair (line_start))
757 g_key_file_parse_key_value_pair (key_file, line_start,
758 length - (line_start - line),
762 gchar *line_utf8 = _g_utf8_make_valid (line);
763 g_set_error (error, G_KEY_FILE_ERROR,
764 G_KEY_FILE_ERROR_PARSE,
765 _("Key file contains line '%s' which is not "
766 "a key-value pair, group, or comment"),
774 g_propagate_error (error, parse_error);
778 g_key_file_parse_comment (GKeyFile *key_file,
783 GKeyFileKeyValuePair *pair;
785 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
788 g_warn_if_fail (key_file->current_group != NULL);
790 pair = g_slice_new (GKeyFileKeyValuePair);
792 pair->value = g_strndup (line, length);
794 key_file->current_group->key_value_pairs =
795 g_list_prepend (key_file->current_group->key_value_pairs, pair);
797 if (length == 0 || line[0] != '#')
798 key_file->current_group->has_trailing_blank_line = TRUE;
802 g_key_file_parse_group (GKeyFile *key_file,
808 const gchar *group_name_start, *group_name_end;
810 /* advance past opening '['
812 group_name_start = line + 1;
813 group_name_end = line + length - 1;
815 while (*group_name_end != ']')
818 group_name = g_strndup (group_name_start,
819 group_name_end - group_name_start);
821 if (!g_key_file_is_group_name (group_name))
823 g_set_error (error, G_KEY_FILE_ERROR,
824 G_KEY_FILE_ERROR_PARSE,
825 _("Invalid group name: %s"), group_name);
830 g_key_file_add_group (key_file, group_name);
835 g_key_file_parse_key_value_pair (GKeyFile *key_file,
840 gchar *key, *value, *key_end, *value_start, *locale;
841 gsize key_len, value_len;
843 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
845 g_set_error_literal (error, G_KEY_FILE_ERROR,
846 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
847 _("Key file does not start with a group"));
851 key_end = value_start = strchr (line, '=');
853 g_warn_if_fail (key_end != NULL);
858 /* Pull the key name from the line (chomping trailing whitespace)
860 while (g_ascii_isspace (*key_end))
863 key_len = key_end - line + 2;
865 g_warn_if_fail (key_len <= length);
867 key = g_strndup (line, key_len - 1);
869 if (!g_key_file_is_key_name (key))
871 g_set_error (error, G_KEY_FILE_ERROR,
872 G_KEY_FILE_ERROR_PARSE,
873 _("Invalid key name: %s"), key);
878 /* Pull the value from the line (chugging leading whitespace)
880 while (g_ascii_isspace (*value_start))
883 value_len = line + length - value_start + 1;
885 value = g_strndup (value_start, value_len);
887 g_warn_if_fail (key_file->start_group != NULL);
889 if (key_file->current_group
890 && key_file->current_group->name
891 && strcmp (key_file->start_group->name,
892 key_file->current_group->name) == 0
893 && strcmp (key, "Encoding") == 0)
895 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
897 gchar *value_utf8 = _g_utf8_make_valid (value);
898 g_set_error (error, G_KEY_FILE_ERROR,
899 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
900 _("Key file contains unsupported "
901 "encoding '%s'"), value_utf8);
910 /* Is this key a translation? If so, is it one that we care about?
912 locale = key_get_locale (key);
914 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
915 g_key_file_add_key (key_file, key_file->current_group, key, value);
923 key_get_locale (const gchar *key)
927 locale = g_strrstr (key, "[");
929 if (locale && strlen (locale) <= 2)
933 locale = g_strndup (locale + 1, strlen (locale) - 2);
939 g_key_file_parse_data (GKeyFile *key_file,
947 g_return_if_fail (key_file != NULL);
948 g_return_if_fail (data != NULL);
952 for (i = 0; i < length; i++)
956 if (i > 0 && data[i - 1] == '\r')
957 g_string_erase (key_file->parse_buffer,
958 key_file->parse_buffer->len - 1,
961 /* When a newline is encountered flush the parse buffer so that the
962 * line can be parsed. Note that completely blank lines won't show
963 * up in the parse buffer, so they get parsed directly.
965 if (key_file->parse_buffer->len > 0)
966 g_key_file_flush_parse_buffer (key_file, &parse_error);
968 g_key_file_parse_comment (key_file, "", 1, &parse_error);
972 g_propagate_error (error, parse_error);
977 g_string_append_c (key_file->parse_buffer, data[i]);
980 key_file->approximate_size += length;
984 g_key_file_flush_parse_buffer (GKeyFile *key_file,
987 GError *file_error = NULL;
989 g_return_if_fail (key_file != NULL);
993 if (key_file->parse_buffer->len > 0)
995 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
996 key_file->parse_buffer->len,
998 g_string_erase (key_file->parse_buffer, 0, -1);
1002 g_propagate_error (error, file_error);
1009 * g_key_file_to_data:
1010 * @key_file: a #GKeyFile
1011 * @length: return location for the length of the
1012 * returned string, or %NULL
1013 * @error: return location for a #GError, or %NULL
1015 * This function outputs @key_file as a string.
1017 * Note that this function never reports an error,
1018 * so it is safe to pass %NULL as @error.
1020 * Return value: a newly allocated string holding
1021 * the contents of the #GKeyFile
1026 g_key_file_to_data (GKeyFile *key_file,
1030 GString *data_string;
1031 GList *group_node, *key_file_node;
1032 gboolean has_blank_line = TRUE;
1034 g_return_val_if_fail (key_file != NULL, NULL);
1036 data_string = g_string_sized_new (2 * key_file->approximate_size);
1038 for (group_node = g_list_last (key_file->groups);
1040 group_node = group_node->prev)
1042 GKeyFileGroup *group;
1044 group = (GKeyFileGroup *) group_node->data;
1046 /* separate groups by at least an empty line */
1047 if (!has_blank_line)
1048 g_string_append_c (data_string, '\n');
1049 has_blank_line = group->has_trailing_blank_line;
1051 if (group->comment != NULL)
1052 g_string_append_printf (data_string, "%s\n", group->comment->value);
1054 if (group->name != NULL)
1055 g_string_append_printf (data_string, "[%s]\n", group->name);
1057 for (key_file_node = g_list_last (group->key_value_pairs);
1058 key_file_node != NULL;
1059 key_file_node = key_file_node->prev)
1061 GKeyFileKeyValuePair *pair;
1063 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1065 if (pair->key != NULL)
1066 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1068 g_string_append_printf (data_string, "%s\n", pair->value);
1073 *length = data_string->len;
1075 return g_string_free (data_string, FALSE);
1079 * g_key_file_get_keys:
1080 * @key_file: a #GKeyFile
1081 * @group_name: a group name
1082 * @length: return location for the number of keys returned, or %NULL
1083 * @error: return location for a #GError, or %NULL
1085 * Returns all keys for the group name @group_name. The array of
1086 * returned keys will be %NULL-terminated, so @length may
1087 * optionally be %NULL. In the event that the @group_name cannot
1088 * be found, %NULL is returned and @error is set to
1089 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1091 * Return value: a newly-allocated %NULL-terminated array of strings.
1092 * Use g_strfreev() to free it.
1097 g_key_file_get_keys (GKeyFile *key_file,
1098 const gchar *group_name,
1102 GKeyFileGroup *group;
1107 g_return_val_if_fail (key_file != NULL, NULL);
1108 g_return_val_if_fail (group_name != NULL, NULL);
1110 group = g_key_file_lookup_group (key_file, group_name);
1114 g_set_error (error, G_KEY_FILE_ERROR,
1115 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1116 _("Key file does not have group '%s'"),
1117 group_name ? group_name : "(null)");
1122 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1124 GKeyFileKeyValuePair *pair;
1126 pair = (GKeyFileKeyValuePair *) tmp->data;
1132 keys = g_new (gchar *, num_keys + 1);
1135 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1137 GKeyFileKeyValuePair *pair;
1139 pair = (GKeyFileKeyValuePair *) tmp->data;
1143 keys[i] = g_strdup (pair->key);
1148 keys[num_keys] = NULL;
1157 * g_key_file_get_start_group:
1158 * @key_file: a #GKeyFile
1160 * Returns the name of the start group of the file.
1162 * Return value: The start group of the key file.
1167 g_key_file_get_start_group (GKeyFile *key_file)
1169 g_return_val_if_fail (key_file != NULL, NULL);
1171 if (key_file->start_group)
1172 return g_strdup (key_file->start_group->name);
1178 * g_key_file_get_groups:
1179 * @key_file: a #GKeyFile
1180 * @length: return location for the number of returned groups, or %NULL
1182 * Returns all groups in the key file loaded with @key_file.
1183 * The array of returned groups will be %NULL-terminated, so
1184 * @length may optionally be %NULL.
1186 * Return value: a newly-allocated %NULL-terminated array of strings.
1187 * Use g_strfreev() to free it.
1191 g_key_file_get_groups (GKeyFile *key_file,
1196 gsize i, num_groups;
1198 g_return_val_if_fail (key_file != NULL, NULL);
1200 num_groups = g_list_length (key_file->groups);
1202 g_return_val_if_fail (num_groups > 0, NULL);
1204 group_node = g_list_last (key_file->groups);
1206 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1208 /* Only need num_groups instead of num_groups + 1
1209 * because the first group of the file (last in the
1210 * list) is always the comment group at the top,
1213 groups = g_new (gchar *, num_groups);
1217 for (group_node = group_node->prev;
1219 group_node = group_node->prev)
1221 GKeyFileGroup *group;
1223 group = (GKeyFileGroup *) group_node->data;
1225 g_warn_if_fail (group->name != NULL);
1227 groups[i++] = g_strdup (group->name);
1238 * g_key_file_get_value:
1239 * @key_file: a #GKeyFile
1240 * @group_name: a group name
1242 * @error: return location for a #GError, or %NULL
1244 * Returns the raw value associated with @key under @group_name.
1245 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1247 * In the event the key cannot be found, %NULL is returned and
1248 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1249 * event that the @group_name cannot be found, %NULL is returned
1250 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1253 * Return value: a newly allocated string or %NULL if the specified
1254 * key cannot be found.
1259 g_key_file_get_value (GKeyFile *key_file,
1260 const gchar *group_name,
1264 GKeyFileGroup *group;
1265 GKeyFileKeyValuePair *pair;
1266 gchar *value = NULL;
1268 g_return_val_if_fail (key_file != NULL, NULL);
1269 g_return_val_if_fail (group_name != NULL, NULL);
1270 g_return_val_if_fail (key != NULL, NULL);
1272 group = g_key_file_lookup_group (key_file, group_name);
1276 g_set_error (error, G_KEY_FILE_ERROR,
1277 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1278 _("Key file does not have group '%s'"),
1279 group_name ? group_name : "(null)");
1283 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1286 value = g_strdup (pair->value);
1288 g_set_error (error, G_KEY_FILE_ERROR,
1289 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1290 _("Key file does not have key '%s'"), key);
1296 * g_key_file_set_value:
1297 * @key_file: a #GKeyFile
1298 * @group_name: a group name
1302 * Associates a new value with @key under @group_name.
1304 * If @key cannot be found then it is created. If @group_name cannot
1305 * be found then it is created. To set an UTF-8 string which may contain
1306 * characters that need escaping (such as newlines or spaces), use
1307 * g_key_file_set_string().
1312 g_key_file_set_value (GKeyFile *key_file,
1313 const gchar *group_name,
1317 GKeyFileGroup *group;
1318 GKeyFileKeyValuePair *pair;
1320 g_return_if_fail (key_file != NULL);
1321 g_return_if_fail (g_key_file_is_group_name (group_name));
1322 g_return_if_fail (g_key_file_is_key_name (key));
1323 g_return_if_fail (value != NULL);
1325 group = g_key_file_lookup_group (key_file, group_name);
1329 g_key_file_add_group (key_file, group_name);
1330 group = (GKeyFileGroup *) key_file->groups->data;
1332 g_key_file_add_key (key_file, group, key, value);
1336 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1339 g_key_file_add_key (key_file, group, key, value);
1342 g_free (pair->value);
1343 pair->value = g_strdup (value);
1349 * g_key_file_get_string:
1350 * @key_file: a #GKeyFile
1351 * @group_name: a group name
1353 * @error: return location for a #GError, or %NULL
1355 * Returns the string value associated with @key under @group_name.
1356 * Unlike g_key_file_get_value(), this function handles escape sequences
1359 * In the event the key cannot be found, %NULL is returned and
1360 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1361 * event that the @group_name cannot be found, %NULL is returned
1362 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1364 * Return value: a newly allocated string or %NULL if the specified
1365 * key cannot be found.
1370 g_key_file_get_string (GKeyFile *key_file,
1371 const gchar *group_name,
1375 gchar *value, *string_value;
1376 GError *key_file_error;
1378 g_return_val_if_fail (key_file != NULL, NULL);
1379 g_return_val_if_fail (group_name != NULL, NULL);
1380 g_return_val_if_fail (key != NULL, NULL);
1382 key_file_error = NULL;
1384 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1388 g_propagate_error (error, key_file_error);
1392 if (!g_utf8_validate (value, -1, NULL))
1394 gchar *value_utf8 = _g_utf8_make_valid (value);
1395 g_set_error (error, G_KEY_FILE_ERROR,
1396 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1397 _("Key file contains key '%s' with value '%s' "
1398 "which is not UTF-8"), key, value_utf8);
1399 g_free (value_utf8);
1405 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1411 if (g_error_matches (key_file_error,
1413 G_KEY_FILE_ERROR_INVALID_VALUE))
1415 g_set_error (error, G_KEY_FILE_ERROR,
1416 G_KEY_FILE_ERROR_INVALID_VALUE,
1417 _("Key file contains key '%s' "
1418 "which has value that cannot be interpreted."),
1420 g_error_free (key_file_error);
1423 g_propagate_error (error, key_file_error);
1426 return string_value;
1430 * g_key_file_set_string:
1431 * @key_file: a #GKeyFile
1432 * @group_name: a group name
1436 * Associates a new string value with @key under @group_name.
1437 * If @key cannot be found then it is created.
1438 * If @group_name cannot be found then it is created.
1439 * Unlike g_key_file_set_value(), this function handles characters
1440 * that need escaping, such as newlines.
1445 g_key_file_set_string (GKeyFile *key_file,
1446 const gchar *group_name,
1448 const gchar *string)
1452 g_return_if_fail (key_file != NULL);
1453 g_return_if_fail (string != NULL);
1455 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1456 g_key_file_set_value (key_file, group_name, key, value);
1461 * g_key_file_get_string_list:
1462 * @key_file: a #GKeyFile
1463 * @group_name: a group name
1465 * @length: return location for the number of returned strings, or %NULL
1466 * @error: return location for a #GError, or %NULL
1468 * Returns the values associated with @key under @group_name.
1470 * In the event the key cannot be found, %NULL is returned and
1471 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1472 * event that the @group_name cannot be found, %NULL is returned
1473 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1475 * Return value: a %NULL-terminated string array or %NULL if the specified
1476 * key cannot be found. The array should be freed with g_strfreev().
1481 g_key_file_get_string_list (GKeyFile *key_file,
1482 const gchar *group_name,
1487 GError *key_file_error = NULL;
1488 gchar *value, *string_value, **values;
1490 GSList *p, *pieces = NULL;
1492 g_return_val_if_fail (key_file != NULL, NULL);
1493 g_return_val_if_fail (group_name != NULL, NULL);
1494 g_return_val_if_fail (key != NULL, NULL);
1499 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1503 g_propagate_error (error, key_file_error);
1507 if (!g_utf8_validate (value, -1, NULL))
1509 gchar *value_utf8 = _g_utf8_make_valid (value);
1510 g_set_error (error, G_KEY_FILE_ERROR,
1511 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1512 _("Key file contains key '%s' with value '%s' "
1513 "which is not UTF-8"), key, value_utf8);
1514 g_free (value_utf8);
1520 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1522 g_free (string_value);
1526 if (g_error_matches (key_file_error,
1528 G_KEY_FILE_ERROR_INVALID_VALUE))
1530 g_set_error (error, G_KEY_FILE_ERROR,
1531 G_KEY_FILE_ERROR_INVALID_VALUE,
1532 _("Key file contains key '%s' "
1533 "which has a value that cannot be interpreted."),
1535 g_error_free (key_file_error);
1538 g_propagate_error (error, key_file_error);
1543 len = g_slist_length (pieces);
1544 values = g_new (gchar *, len + 1);
1545 for (p = pieces, i = 0; p; p = p->next)
1546 values[i++] = p->data;
1549 g_slist_free (pieces);
1558 * g_key_file_set_string_list:
1559 * @key_file: a #GKeyFile
1560 * @group_name: a group name
1562 * @list: an array of string values
1563 * @length: number of string values in @list
1565 * Associates a list of string values for @key under @group_name.
1566 * If @key cannot be found then it is created.
1567 * If @group_name cannot be found then it is created.
1572 g_key_file_set_string_list (GKeyFile *key_file,
1573 const gchar *group_name,
1575 const gchar * const list[],
1578 GString *value_list;
1581 g_return_if_fail (key_file != NULL);
1582 g_return_if_fail (list != NULL || length == 0);
1584 value_list = g_string_sized_new (length * 128);
1585 for (i = 0; i < length && list[i] != NULL; i++)
1589 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1590 g_string_append (value_list, value);
1591 g_string_append_c (value_list, key_file->list_separator);
1596 g_key_file_set_value (key_file, group_name, key, value_list->str);
1597 g_string_free (value_list, TRUE);
1601 * g_key_file_set_locale_string:
1602 * @key_file: a #GKeyFile
1603 * @group_name: a group name
1605 * @locale: a locale identifier
1608 * Associates a string value for @key and @locale under @group_name.
1609 * If the translation for @key cannot be found then it is created.
1614 g_key_file_set_locale_string (GKeyFile *key_file,
1615 const gchar *group_name,
1617 const gchar *locale,
1618 const gchar *string)
1620 gchar *full_key, *value;
1622 g_return_if_fail (key_file != NULL);
1623 g_return_if_fail (key != NULL);
1624 g_return_if_fail (locale != NULL);
1625 g_return_if_fail (string != NULL);
1627 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1628 full_key = g_strdup_printf ("%s[%s]", key, locale);
1629 g_key_file_set_value (key_file, group_name, full_key, value);
1635 * g_key_file_get_locale_string:
1636 * @key_file: a #GKeyFile
1637 * @group_name: a group name
1639 * @locale: a locale identifier or %NULL
1640 * @error: return location for a #GError, or %NULL
1642 * Returns the value associated with @key under @group_name
1643 * translated in the given @locale if available. If @locale is
1644 * %NULL then the current locale is assumed.
1646 * If @key cannot be found then %NULL is returned and @error is set
1647 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1648 * with @key cannot be interpreted or no suitable translation can
1649 * be found then the untranslated value is returned.
1651 * Return value: a newly allocated string or %NULL if the specified
1652 * key cannot be found.
1657 g_key_file_get_locale_string (GKeyFile *key_file,
1658 const gchar *group_name,
1660 const gchar *locale,
1663 gchar *candidate_key, *translated_value;
1664 GError *key_file_error;
1666 gboolean free_languages = FALSE;
1669 g_return_val_if_fail (key_file != NULL, NULL);
1670 g_return_val_if_fail (group_name != NULL, NULL);
1671 g_return_val_if_fail (key != NULL, NULL);
1673 candidate_key = NULL;
1674 translated_value = NULL;
1675 key_file_error = NULL;
1679 languages = g_get_locale_variants (locale);
1680 free_languages = TRUE;
1684 languages = (gchar **) g_get_language_names ();
1685 free_languages = FALSE;
1688 for (i = 0; languages[i]; i++)
1690 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1692 translated_value = g_key_file_get_string (key_file,
1694 candidate_key, NULL);
1695 g_free (candidate_key);
1697 if (translated_value)
1700 g_free (translated_value);
1701 translated_value = NULL;
1704 /* Fallback to untranslated key
1706 if (!translated_value)
1708 translated_value = g_key_file_get_string (key_file, group_name, key,
1711 if (!translated_value)
1712 g_propagate_error (error, key_file_error);
1716 g_strfreev (languages);
1718 return translated_value;
1722 * g_key_file_get_locale_string_list:
1723 * @key_file: a #GKeyFile
1724 * @group_name: a group name
1726 * @locale: a locale identifier or %NULL
1727 * @length: return location for the number of returned strings or %NULL
1728 * @error: return location for a #GError or %NULL
1730 * Returns the values associated with @key under @group_name
1731 * translated in the given @locale if available. If @locale is
1732 * %NULL then the current locale is assumed.
1734 * If @key cannot be found then %NULL is returned and @error is set
1735 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1736 * with @key cannot be interpreted or no suitable translations
1737 * can be found then the untranslated values are returned. The
1738 * returned array is %NULL-terminated, so @length may optionally
1741 * Return value: a newly allocated %NULL-terminated string array
1742 * or %NULL if the key isn't found. The string array should be freed
1743 * with g_strfreev().
1748 g_key_file_get_locale_string_list (GKeyFile *key_file,
1749 const gchar *group_name,
1751 const gchar *locale,
1755 GError *key_file_error;
1756 gchar **values, *value;
1757 char list_separator[2];
1760 g_return_val_if_fail (key_file != NULL, NULL);
1761 g_return_val_if_fail (group_name != NULL, NULL);
1762 g_return_val_if_fail (key != NULL, NULL);
1764 key_file_error = NULL;
1766 value = g_key_file_get_locale_string (key_file, group_name,
1771 g_propagate_error (error, key_file_error);
1780 len = strlen (value);
1781 if (value[len - 1] == key_file->list_separator)
1782 value[len - 1] = '\0';
1784 list_separator[0] = key_file->list_separator;
1785 list_separator[1] = '\0';
1786 values = g_strsplit (value, list_separator, 0);
1791 *length = g_strv_length (values);
1797 * g_key_file_set_locale_string_list:
1798 * @key_file: a #GKeyFile
1799 * @group_name: a group name
1801 * @locale: a locale identifier
1802 * @list: a %NULL-terminated array of locale string values
1803 * @length: the length of @list
1805 * Associates a list of string values for @key and @locale under
1806 * @group_name. If the translation for @key cannot be found then
1812 g_key_file_set_locale_string_list (GKeyFile *key_file,
1813 const gchar *group_name,
1815 const gchar *locale,
1816 const gchar * const list[],
1819 GString *value_list;
1823 g_return_if_fail (key_file != NULL);
1824 g_return_if_fail (key != NULL);
1825 g_return_if_fail (locale != NULL);
1826 g_return_if_fail (length != 0);
1828 value_list = g_string_sized_new (length * 128);
1829 for (i = 0; i < length && list[i] != NULL; i++)
1833 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1834 g_string_append (value_list, value);
1835 g_string_append_c (value_list, key_file->list_separator);
1840 full_key = g_strdup_printf ("%s[%s]", key, locale);
1841 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1843 g_string_free (value_list, TRUE);
1847 * g_key_file_get_boolean:
1848 * @key_file: a #GKeyFile
1849 * @group_name: a group name
1851 * @error: return location for a #GError
1853 * Returns the value associated with @key under @group_name as a
1856 * If @key cannot be found then %FALSE is returned and @error is set
1857 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1858 * associated with @key cannot be interpreted as a boolean then %FALSE
1859 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1861 * Return value: the value associated with the key as a boolean,
1862 * or %FALSE if the key was not found or could not be parsed.
1867 g_key_file_get_boolean (GKeyFile *key_file,
1868 const gchar *group_name,
1872 GError *key_file_error = NULL;
1874 gboolean bool_value;
1876 g_return_val_if_fail (key_file != NULL, FALSE);
1877 g_return_val_if_fail (group_name != NULL, FALSE);
1878 g_return_val_if_fail (key != NULL, FALSE);
1880 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1884 g_propagate_error (error, key_file_error);
1888 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1894 if (g_error_matches (key_file_error,
1896 G_KEY_FILE_ERROR_INVALID_VALUE))
1898 g_set_error (error, G_KEY_FILE_ERROR,
1899 G_KEY_FILE_ERROR_INVALID_VALUE,
1900 _("Key file contains key '%s' "
1901 "which has value that cannot be interpreted."),
1903 g_error_free (key_file_error);
1906 g_propagate_error (error, key_file_error);
1913 * g_key_file_set_boolean:
1914 * @key_file: a #GKeyFile
1915 * @group_name: a group name
1917 * @value: %TRUE or %FALSE
1919 * Associates a new boolean value with @key under @group_name.
1920 * If @key cannot be found then it is created.
1925 g_key_file_set_boolean (GKeyFile *key_file,
1926 const gchar *group_name,
1932 g_return_if_fail (key_file != NULL);
1934 result = g_key_file_parse_boolean_as_value (key_file, value);
1935 g_key_file_set_value (key_file, group_name, key, result);
1940 * g_key_file_get_boolean_list:
1941 * @key_file: a #GKeyFile
1942 * @group_name: a group name
1944 * @length: the number of booleans returned
1945 * @error: return location for a #GError
1947 * Returns the values associated with @key under @group_name as
1950 * If @key cannot be found then %NULL is returned and @error is set to
1951 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1952 * with @key cannot be interpreted as booleans then %NULL is returned
1953 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1955 * Return value: the values associated with the key as a list of
1956 * booleans, or %NULL if the key was not found or could not be parsed.
1961 g_key_file_get_boolean_list (GKeyFile *key_file,
1962 const gchar *group_name,
1967 GError *key_file_error;
1969 gboolean *bool_values;
1972 g_return_val_if_fail (key_file != NULL, NULL);
1973 g_return_val_if_fail (group_name != NULL, NULL);
1974 g_return_val_if_fail (key != NULL, NULL);
1979 key_file_error = NULL;
1981 values = g_key_file_get_string_list (key_file, group_name, key,
1982 &num_bools, &key_file_error);
1985 g_propagate_error (error, key_file_error);
1990 bool_values = g_new (gboolean, num_bools);
1992 for (i = 0; i < num_bools; i++)
1994 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2000 g_propagate_error (error, key_file_error);
2001 g_strfreev (values);
2002 g_free (bool_values);
2007 g_strfreev (values);
2010 *length = num_bools;
2016 * g_key_file_set_boolean_list:
2017 * @key_file: a #GKeyFile
2018 * @group_name: a group name
2020 * @list: an array of boolean values
2021 * @length: length of @list
2023 * Associates a list of boolean values with @key under @group_name.
2024 * If @key cannot be found then it is created.
2025 * If @group_name is %NULL, the start_group is used.
2030 g_key_file_set_boolean_list (GKeyFile *key_file,
2031 const gchar *group_name,
2036 GString *value_list;
2039 g_return_if_fail (key_file != NULL);
2040 g_return_if_fail (list != NULL);
2042 value_list = g_string_sized_new (length * 8);
2043 for (i = 0; i < length; i++)
2047 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2049 g_string_append (value_list, value);
2050 g_string_append_c (value_list, key_file->list_separator);
2055 g_key_file_set_value (key_file, group_name, key, value_list->str);
2056 g_string_free (value_list, TRUE);
2060 * g_key_file_get_integer:
2061 * @key_file: a #GKeyFile
2062 * @group_name: a group name
2064 * @error: return location for a #GError
2066 * Returns the value associated with @key under @group_name as an
2069 * If @key cannot be found then 0 is returned and @error is set to
2070 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2071 * with @key cannot be interpreted as an integer then 0 is returned
2072 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2074 * Return value: the value associated with the key as an integer, or
2075 * 0 if the key was not found or could not be parsed.
2080 g_key_file_get_integer (GKeyFile *key_file,
2081 const gchar *group_name,
2085 GError *key_file_error;
2089 g_return_val_if_fail (key_file != NULL, -1);
2090 g_return_val_if_fail (group_name != NULL, -1);
2091 g_return_val_if_fail (key != NULL, -1);
2093 key_file_error = NULL;
2095 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2099 g_propagate_error (error, key_file_error);
2103 int_value = g_key_file_parse_value_as_integer (key_file, value,
2109 if (g_error_matches (key_file_error,
2111 G_KEY_FILE_ERROR_INVALID_VALUE))
2113 g_set_error (error, G_KEY_FILE_ERROR,
2114 G_KEY_FILE_ERROR_INVALID_VALUE,
2115 _("Key file contains key '%s' in group '%s' "
2116 "which has value that cannot be interpreted."), key,
2118 g_error_free (key_file_error);
2121 g_propagate_error (error, key_file_error);
2128 * g_key_file_set_integer:
2129 * @key_file: a #GKeyFile
2130 * @group_name: a group name
2132 * @value: an integer value
2134 * Associates a new integer value with @key under @group_name.
2135 * If @key cannot be found then it is created.
2140 g_key_file_set_integer (GKeyFile *key_file,
2141 const gchar *group_name,
2147 g_return_if_fail (key_file != NULL);
2149 result = g_key_file_parse_integer_as_value (key_file, value);
2150 g_key_file_set_value (key_file, group_name, key, result);
2155 * g_key_file_get_int64:
2156 * @key_file: a non-%NULL #GKeyFile
2157 * @group_name: a non-%NULL group name
2158 * @key: a non-%NULL key
2159 * @error: return location for a #GError
2161 * Returns the value associated with @key under @group_name as a signed
2162 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2163 * 64-bit results without truncation.
2165 * Returns: the value associated with the key as a signed 64-bit integer, or
2166 * 0 if the key was not found or could not be parsed.
2171 g_key_file_get_int64 (GKeyFile *key_file,
2172 const gchar *group_name,
2179 g_return_val_if_fail (key_file != NULL, -1);
2180 g_return_val_if_fail (group_name != NULL, -1);
2181 g_return_val_if_fail (key != NULL, -1);
2183 s = g_key_file_get_value (key_file, group_name, key, error);
2188 v = g_ascii_strtoll (s, &end, 10);
2190 if (*s == '\0' || *end != '\0')
2192 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2193 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2194 key, group_name, s);
2203 * g_key_file_set_int64:
2204 * @key_file: a #GKeyFile
2205 * @group_name: a group name
2207 * @value: an integer value
2209 * Associates a new integer value with @key under @group_name.
2210 * If @key cannot be found then it is created.
2215 g_key_file_set_int64 (GKeyFile *key_file,
2216 const gchar *group_name,
2222 g_return_if_fail (key_file != NULL);
2224 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2225 g_key_file_set_value (key_file, group_name, key, result);
2230 * g_key_file_get_uint64:
2231 * @key_file: a non-%NULL #GKeyFile
2232 * @group_name: a non-%NULL group name
2233 * @key: a non-%NULL key
2234 * @error: return location for a #GError
2236 * Returns the value associated with @key under @group_name as an unsigned
2237 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2238 * large positive results without truncation.
2240 * Returns: the value associated with the key as an unsigned 64-bit integer,
2241 * or 0 if the key was not found or could not be parsed.
2246 g_key_file_get_uint64 (GKeyFile *key_file,
2247 const gchar *group_name,
2254 g_return_val_if_fail (key_file != NULL, -1);
2255 g_return_val_if_fail (group_name != NULL, -1);
2256 g_return_val_if_fail (key != NULL, -1);
2258 s = g_key_file_get_value (key_file, group_name, key, error);
2263 v = g_ascii_strtoull (s, &end, 10);
2265 if (*s == '\0' || *end != '\0')
2267 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2268 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2269 key, group_name, s);
2278 * g_key_file_set_uint64:
2279 * @key_file: a #GKeyFile
2280 * @group_name: a group name
2282 * @value: an integer value
2284 * Associates a new integer value with @key under @group_name.
2285 * If @key cannot be found then it is created.
2290 g_key_file_set_uint64 (GKeyFile *key_file,
2291 const gchar *group_name,
2297 g_return_if_fail (key_file != NULL);
2299 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2300 g_key_file_set_value (key_file, group_name, key, result);
2305 * g_key_file_get_integer_list:
2306 * @key_file: a #GKeyFile
2307 * @group_name: a group name
2309 * @length: the number of integers returned
2310 * @error: return location for a #GError
2312 * Returns the values associated with @key under @group_name as
2315 * If @key cannot be found then %NULL is returned and @error is set to
2316 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2317 * with @key cannot be interpreted as integers then %NULL is returned
2318 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2320 * Return value: the values associated with the key as a list of
2321 * integers, or %NULL if the key was not found or could not be parsed.
2326 g_key_file_get_integer_list (GKeyFile *key_file,
2327 const gchar *group_name,
2332 GError *key_file_error = NULL;
2337 g_return_val_if_fail (key_file != NULL, NULL);
2338 g_return_val_if_fail (group_name != NULL, NULL);
2339 g_return_val_if_fail (key != NULL, NULL);
2344 values = g_key_file_get_string_list (key_file, group_name, key,
2345 &num_ints, &key_file_error);
2348 g_propagate_error (error, key_file_error);
2353 int_values = g_new (gint, num_ints);
2355 for (i = 0; i < num_ints; i++)
2357 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2363 g_propagate_error (error, key_file_error);
2364 g_strfreev (values);
2365 g_free (int_values);
2370 g_strfreev (values);
2379 * g_key_file_set_integer_list:
2380 * @key_file: a #GKeyFile
2381 * @group_name: a group name
2383 * @list: an array of integer values
2384 * @length: number of integer values in @list
2386 * Associates a list of integer values with @key under @group_name.
2387 * If @key cannot be found then it is created.
2392 g_key_file_set_integer_list (GKeyFile *key_file,
2393 const gchar *group_name,
2401 g_return_if_fail (key_file != NULL);
2402 g_return_if_fail (list != NULL);
2404 values = g_string_sized_new (length * 16);
2405 for (i = 0; i < length; i++)
2409 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2411 g_string_append (values, value);
2412 g_string_append_c (values, key_file->list_separator);
2417 g_key_file_set_value (key_file, group_name, key, values->str);
2418 g_string_free (values, TRUE);
2422 * g_key_file_get_double:
2423 * @key_file: a #GKeyFile
2424 * @group_name: a group name
2426 * @error: return location for a #GError
2428 * Returns the value associated with @key under @group_name as a
2429 * double. If @group_name is %NULL, the start_group is used.
2431 * If @key cannot be found then 0.0 is returned and @error is set to
2432 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2433 * with @key cannot be interpreted as a double then 0.0 is returned
2434 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2436 * Return value: the value associated with the key as a double, or
2437 * 0.0 if the key was not found or could not be parsed.
2442 g_key_file_get_double (GKeyFile *key_file,
2443 const gchar *group_name,
2447 GError *key_file_error;
2449 gdouble double_value;
2451 g_return_val_if_fail (key_file != NULL, -1);
2452 g_return_val_if_fail (group_name != NULL, -1);
2453 g_return_val_if_fail (key != NULL, -1);
2455 key_file_error = NULL;
2457 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2461 g_propagate_error (error, key_file_error);
2465 double_value = g_key_file_parse_value_as_double (key_file, value,
2471 if (g_error_matches (key_file_error,
2473 G_KEY_FILE_ERROR_INVALID_VALUE))
2475 g_set_error (error, G_KEY_FILE_ERROR,
2476 G_KEY_FILE_ERROR_INVALID_VALUE,
2477 _("Key file contains key '%s' in group '%s' "
2478 "which has value that cannot be interpreted."), key,
2480 g_error_free (key_file_error);
2483 g_propagate_error (error, key_file_error);
2486 return double_value;
2490 * g_key_file_set_double:
2491 * @key_file: a #GKeyFile
2492 * @group_name: a group name
2494 * @value: an double value
2496 * Associates a new double value with @key under @group_name.
2497 * If @key cannot be found then it is created.
2502 g_key_file_set_double (GKeyFile *key_file,
2503 const gchar *group_name,
2507 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2509 g_return_if_fail (key_file != NULL);
2511 g_ascii_dtostr (result, sizeof (result), value);
2512 g_key_file_set_value (key_file, group_name, key, result);
2516 * g_key_file_get_double_list:
2517 * @key_file: a #GKeyFile
2518 * @group_name: a group name
2520 * @length: the number of doubles returned
2521 * @error: return location for a #GError
2523 * Returns the values associated with @key under @group_name as
2526 * If @key cannot be found then %NULL is returned and @error is set to
2527 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2528 * with @key cannot be interpreted as doubles then %NULL is returned
2529 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2531 * Return value: the values associated with the key as a list of
2532 * doubles, or %NULL if the key was not found or could not be parsed.
2537 g_key_file_get_double_list (GKeyFile *key_file,
2538 const gchar *group_name,
2543 GError *key_file_error = NULL;
2545 gdouble *double_values;
2546 gsize i, num_doubles;
2548 g_return_val_if_fail (key_file != NULL, NULL);
2549 g_return_val_if_fail (group_name != NULL, NULL);
2550 g_return_val_if_fail (key != NULL, NULL);
2555 values = g_key_file_get_string_list (key_file, group_name, key,
2556 &num_doubles, &key_file_error);
2559 g_propagate_error (error, key_file_error);
2564 double_values = g_new (gdouble, num_doubles);
2566 for (i = 0; i < num_doubles; i++)
2568 double_values[i] = g_key_file_parse_value_as_double (key_file,
2574 g_propagate_error (error, key_file_error);
2575 g_strfreev (values);
2576 g_free (double_values);
2581 g_strfreev (values);
2584 *length = num_doubles;
2586 return double_values;
2590 * g_key_file_set_double_list:
2591 * @key_file: a #GKeyFile
2592 * @group_name: a group name
2594 * @list: an array of double values
2595 * @length: number of double values in @list
2597 * Associates a list of double values with @key under
2598 * @group_name. If @key cannot be found then it is created.
2603 g_key_file_set_double_list (GKeyFile *key_file,
2604 const gchar *group_name,
2612 g_return_if_fail (key_file != NULL);
2613 g_return_if_fail (list != NULL);
2615 values = g_string_sized_new (length * 16);
2616 for (i = 0; i < length; i++)
2618 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2620 g_ascii_dtostr( result, sizeof (result), list[i] );
2622 g_string_append (values, result);
2623 g_string_append_c (values, key_file->list_separator);
2626 g_key_file_set_value (key_file, group_name, key, values->str);
2627 g_string_free (values, TRUE);
2631 g_key_file_set_key_comment (GKeyFile *key_file,
2632 const gchar *group_name,
2634 const gchar *comment,
2637 GKeyFileGroup *group;
2638 GKeyFileKeyValuePair *pair;
2639 GList *key_node, *comment_node, *tmp;
2641 group = g_key_file_lookup_group (key_file, group_name);
2644 g_set_error (error, G_KEY_FILE_ERROR,
2645 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2646 _("Key file does not have group '%s'"),
2647 group_name ? group_name : "(null)");
2652 /* First find the key the comments are supposed to be
2655 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2657 if (key_node == NULL)
2659 g_set_error (error, G_KEY_FILE_ERROR,
2660 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2661 _("Key file does not have key '%s' in group '%s'"),
2666 /* Then find all the comments already associated with the
2669 tmp = key_node->next;
2672 pair = (GKeyFileKeyValuePair *) tmp->data;
2674 if (pair->key != NULL)
2679 g_key_file_remove_key_value_pair_node (key_file, group,
2683 if (comment == NULL)
2686 /* Now we can add our new comment
2688 pair = g_slice_new (GKeyFileKeyValuePair);
2690 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2692 key_node = g_list_insert (key_node, pair, 1);
2698 g_key_file_set_group_comment (GKeyFile *key_file,
2699 const gchar *group_name,
2700 const gchar *comment,
2703 GKeyFileGroup *group;
2705 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2707 group = g_key_file_lookup_group (key_file, group_name);
2710 g_set_error (error, G_KEY_FILE_ERROR,
2711 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2712 _("Key file does not have group '%s'"),
2713 group_name ? group_name : "(null)");
2718 /* First remove any existing comment
2722 g_key_file_key_value_pair_free (group->comment);
2723 group->comment = NULL;
2726 if (comment == NULL)
2729 /* Now we can add our new comment
2731 group->comment = g_slice_new (GKeyFileKeyValuePair);
2732 group->comment->key = NULL;
2733 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2739 g_key_file_set_top_comment (GKeyFile *key_file,
2740 const gchar *comment,
2744 GKeyFileGroup *group;
2745 GKeyFileKeyValuePair *pair;
2747 /* The last group in the list should be the top (comments only)
2750 g_warn_if_fail (key_file->groups != NULL);
2751 group_node = g_list_last (key_file->groups);
2752 group = (GKeyFileGroup *) group_node->data;
2753 g_warn_if_fail (group->name == NULL);
2755 /* Note all keys must be comments at the top of
2756 * the file, so we can just free it all.
2758 if (group->key_value_pairs != NULL)
2760 g_list_foreach (group->key_value_pairs,
2761 (GFunc) g_key_file_key_value_pair_free,
2763 g_list_free (group->key_value_pairs);
2764 group->key_value_pairs = NULL;
2767 if (comment == NULL)
2770 pair = g_slice_new (GKeyFileKeyValuePair);
2772 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2774 group->key_value_pairs =
2775 g_list_prepend (group->key_value_pairs, pair);
2781 * g_key_file_set_comment:
2782 * @key_file: a #GKeyFile
2783 * @group_name: a group name, or %NULL
2785 * @comment: a comment
2786 * @error: return location for a #GError
2788 * Places a comment above @key from @group_name.
2789 * If @key is %NULL then @comment will be written above @group_name.
2790 * If both @key and @group_name are %NULL, then @comment will be
2791 * written above the first group in the file.
2793 * Returns: %TRUE if the comment was written, %FALSE otherwise
2798 g_key_file_set_comment (GKeyFile *key_file,
2799 const gchar *group_name,
2801 const gchar *comment,
2804 g_return_val_if_fail (key_file != NULL, FALSE);
2806 if (group_name != NULL && key != NULL)
2808 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2811 else if (group_name != NULL)
2813 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2818 if (!g_key_file_set_top_comment (key_file, comment, error))
2822 if (comment != NULL)
2823 key_file->approximate_size += strlen (comment);
2829 g_key_file_get_key_comment (GKeyFile *key_file,
2830 const gchar *group_name,
2834 GKeyFileGroup *group;
2835 GKeyFileKeyValuePair *pair;
2836 GList *key_node, *tmp;
2840 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2842 group = g_key_file_lookup_group (key_file, group_name);
2845 g_set_error (error, G_KEY_FILE_ERROR,
2846 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2847 _("Key file does not have group '%s'"),
2848 group_name ? group_name : "(null)");
2853 /* First find the key the comments are supposed to be
2856 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2858 if (key_node == NULL)
2860 g_set_error (error, G_KEY_FILE_ERROR,
2861 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2862 _("Key file does not have key '%s' in group '%s'"),
2869 /* Then find all the comments already associated with the
2870 * key and concatentate them.
2872 tmp = key_node->next;
2873 if (!key_node->next)
2876 pair = (GKeyFileKeyValuePair *) tmp->data;
2877 if (pair->key != NULL)
2882 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2884 if (pair->key != NULL)
2890 while (tmp != key_node)
2892 pair = (GKeyFileKeyValuePair *) tmp->data;
2895 string = g_string_sized_new (512);
2897 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2898 g_string_append (string, comment);
2906 comment = string->str;
2907 g_string_free (string, FALSE);
2916 get_group_comment (GKeyFile *key_file,
2917 GKeyFileGroup *group,
2926 tmp = group->key_value_pairs;
2929 GKeyFileKeyValuePair *pair;
2931 pair = (GKeyFileKeyValuePair *) tmp->data;
2933 if (pair->key != NULL)
2939 if (tmp->next == NULL)
2947 GKeyFileKeyValuePair *pair;
2949 pair = (GKeyFileKeyValuePair *) tmp->data;
2952 string = g_string_sized_new (512);
2954 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2955 g_string_append (string, comment);
2962 return g_string_free (string, FALSE);
2968 g_key_file_get_group_comment (GKeyFile *key_file,
2969 const gchar *group_name,
2973 GKeyFileGroup *group;
2975 group = g_key_file_lookup_group (key_file, group_name);
2978 g_set_error (error, G_KEY_FILE_ERROR,
2979 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2980 _("Key file does not have group '%s'"),
2981 group_name ? group_name : "(null)");
2987 return g_strdup (group->comment->value);
2989 group_node = g_key_file_lookup_group_node (key_file, group_name);
2990 group_node = group_node->next;
2991 group = (GKeyFileGroup *)group_node->data;
2992 return get_group_comment (key_file, group, error);
2996 g_key_file_get_top_comment (GKeyFile *key_file,
3000 GKeyFileGroup *group;
3002 /* The last group in the list should be the top (comments only)
3005 g_warn_if_fail (key_file->groups != NULL);
3006 group_node = g_list_last (key_file->groups);
3007 group = (GKeyFileGroup *) group_node->data;
3008 g_warn_if_fail (group->name == NULL);
3010 return get_group_comment (key_file, group, error);
3014 * g_key_file_get_comment:
3015 * @key_file: a #GKeyFile
3016 * @group_name: a group name, or %NULL
3018 * @error: return location for a #GError
3020 * Retrieves a comment above @key from @group_name.
3021 * If @key is %NULL then @comment will be read from above
3022 * @group_name. If both @key and @group_name are %NULL, then
3023 * @comment will be read from above the first group in the file.
3025 * Returns: a comment that should be freed with g_free()
3030 g_key_file_get_comment (GKeyFile *key_file,
3031 const gchar *group_name,
3035 g_return_val_if_fail (key_file != NULL, NULL);
3037 if (group_name != NULL && key != NULL)
3038 return g_key_file_get_key_comment (key_file, group_name, key, error);
3039 else if (group_name != NULL)
3040 return g_key_file_get_group_comment (key_file, group_name, error);
3042 return g_key_file_get_top_comment (key_file, error);
3046 * g_key_file_remove_comment:
3047 * @key_file: a #GKeyFile
3048 * @group_name: a group name, or %NULL
3050 * @error: return location for a #GError
3052 * Removes a comment above @key from @group_name.
3053 * If @key is %NULL then @comment will be removed above @group_name.
3054 * If both @key and @group_name are %NULL, then @comment will
3055 * be removed above the first group in the file.
3057 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3063 g_key_file_remove_comment (GKeyFile *key_file,
3064 const gchar *group_name,
3068 g_return_val_if_fail (key_file != NULL, FALSE);
3070 if (group_name != NULL && key != NULL)
3071 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3072 else if (group_name != NULL)
3073 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3075 return g_key_file_set_top_comment (key_file, NULL, error);
3079 * g_key_file_has_group:
3080 * @key_file: a #GKeyFile
3081 * @group_name: a group name
3083 * Looks whether the key file has the group @group_name.
3085 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3090 g_key_file_has_group (GKeyFile *key_file,
3091 const gchar *group_name)
3093 g_return_val_if_fail (key_file != NULL, FALSE);
3094 g_return_val_if_fail (group_name != NULL, FALSE);
3096 return g_key_file_lookup_group (key_file, group_name) != NULL;
3100 * g_key_file_has_key:
3101 * @key_file: a #GKeyFile
3102 * @group_name: a group name
3104 * @error: return location for a #GError
3106 * Looks whether the key file has the key @key in the group
3109 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3115 g_key_file_has_key (GKeyFile *key_file,
3116 const gchar *group_name,
3120 GKeyFileKeyValuePair *pair;
3121 GKeyFileGroup *group;
3123 g_return_val_if_fail (key_file != NULL, FALSE);
3124 g_return_val_if_fail (group_name != NULL, FALSE);
3125 g_return_val_if_fail (key != NULL, FALSE);
3127 group = g_key_file_lookup_group (key_file, group_name);
3131 g_set_error (error, G_KEY_FILE_ERROR,
3132 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3133 _("Key file does not have group '%s'"),
3134 group_name ? group_name : "(null)");
3139 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3141 return pair != NULL;
3145 g_key_file_add_group (GKeyFile *key_file,
3146 const gchar *group_name)
3148 GKeyFileGroup *group;
3150 g_return_if_fail (key_file != NULL);
3151 g_return_if_fail (g_key_file_is_group_name (group_name));
3153 group = g_key_file_lookup_group (key_file, group_name);
3156 key_file->current_group = group;
3160 group = g_slice_new0 (GKeyFileGroup);
3161 group->name = g_strdup (group_name);
3162 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3163 key_file->groups = g_list_prepend (key_file->groups, group);
3164 key_file->approximate_size += strlen (group_name) + 3;
3165 key_file->current_group = group;
3167 if (key_file->start_group == NULL)
3168 key_file->start_group = group;
3170 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3174 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3179 g_free (pair->value);
3180 g_slice_free (GKeyFileKeyValuePair, pair);
3184 /* Be careful not to call this function on a node with data in the
3185 * lookup map without removing it from the lookup map, first.
3187 * Some current cases where this warning is not a concern are
3189 * - the node being removed is a comment node
3190 * - the entire lookup map is getting destroyed soon after
3194 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3195 GKeyFileGroup *group,
3199 GKeyFileKeyValuePair *pair;
3201 pair = (GKeyFileKeyValuePair *) pair_node->data;
3203 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3205 if (pair->key != NULL)
3206 key_file->approximate_size -= strlen (pair->key) + 1;
3208 g_warn_if_fail (pair->value != NULL);
3209 key_file->approximate_size -= strlen (pair->value);
3211 g_key_file_key_value_pair_free (pair);
3213 g_list_free_1 (pair_node);
3217 g_key_file_remove_group_node (GKeyFile *key_file,
3220 GKeyFileGroup *group;
3223 group = (GKeyFileGroup *) group_node->data;
3226 g_hash_table_remove (key_file->group_hash, group->name);
3228 /* If the current group gets deleted make the current group the last
3231 if (key_file->current_group == group)
3233 /* groups should always contain at least the top comment group,
3234 * unless g_key_file_clear has been called
3236 if (key_file->groups)
3237 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3239 key_file->current_group = NULL;
3242 /* If the start group gets deleted make the start group the first
3245 if (key_file->start_group == group)
3247 tmp = g_list_last (key_file->groups);
3250 if (tmp != group_node &&
3251 ((GKeyFileGroup *) tmp->data)->name != NULL)
3258 key_file->start_group = (GKeyFileGroup *) tmp->data;
3260 key_file->start_group = NULL;
3263 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3265 if (group->name != NULL)
3266 key_file->approximate_size -= strlen (group->name) + 3;
3268 tmp = group->key_value_pairs;
3275 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3278 g_warn_if_fail (group->key_value_pairs == NULL);
3280 if (group->lookup_map)
3282 g_hash_table_destroy (group->lookup_map);
3283 group->lookup_map = NULL;
3286 g_free ((gchar *) group->name);
3287 g_slice_free (GKeyFileGroup, group);
3288 g_list_free_1 (group_node);
3292 * g_key_file_remove_group:
3293 * @key_file: a #GKeyFile
3294 * @group_name: a group name
3295 * @error: return location for a #GError or %NULL
3297 * Removes the specified group, @group_name,
3298 * from the key file.
3300 * Returns: %TRUE if the group was removed, %FALSE otherwise
3305 g_key_file_remove_group (GKeyFile *key_file,
3306 const gchar *group_name,
3311 g_return_val_if_fail (key_file != NULL, FALSE);
3312 g_return_val_if_fail (group_name != NULL, FALSE);
3314 group_node = g_key_file_lookup_group_node (key_file, group_name);
3318 g_set_error (error, G_KEY_FILE_ERROR,
3319 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3320 _("Key file does not have group '%s'"),
3325 g_key_file_remove_group_node (key_file, group_node);
3331 g_key_file_add_key (GKeyFile *key_file,
3332 GKeyFileGroup *group,
3336 GKeyFileKeyValuePair *pair;
3338 pair = g_slice_new (GKeyFileKeyValuePair);
3339 pair->key = g_strdup (key);
3340 pair->value = g_strdup (value);
3342 g_hash_table_replace (group->lookup_map, pair->key, pair);
3343 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3344 group->has_trailing_blank_line = FALSE;
3345 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3349 * g_key_file_remove_key:
3350 * @key_file: a #GKeyFile
3351 * @group_name: a group name
3352 * @key: a key name to remove
3353 * @error: return location for a #GError or %NULL
3355 * Removes @key in @group_name from the key file.
3357 * Returns: %TRUE if the key was removed, %FALSE otherwise
3362 g_key_file_remove_key (GKeyFile *key_file,
3363 const gchar *group_name,
3367 GKeyFileGroup *group;
3368 GKeyFileKeyValuePair *pair;
3370 g_return_val_if_fail (key_file != NULL, FALSE);
3371 g_return_val_if_fail (group_name != NULL, FALSE);
3372 g_return_val_if_fail (key != NULL, FALSE);
3376 group = g_key_file_lookup_group (key_file, group_name);
3379 g_set_error (error, G_KEY_FILE_ERROR,
3380 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3381 _("Key file does not have group '%s'"),
3382 group_name ? group_name : "(null)");
3386 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3390 g_set_error (error, G_KEY_FILE_ERROR,
3391 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3392 _("Key file does not have key '%s' in group '%s'"),
3397 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3399 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3400 g_hash_table_remove (group->lookup_map, pair->key);
3401 g_key_file_key_value_pair_free (pair);
3407 g_key_file_lookup_group_node (GKeyFile *key_file,
3408 const gchar *group_name)
3410 GKeyFileGroup *group;
3413 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3415 group = (GKeyFileGroup *) tmp->data;
3417 if (group && group->name && strcmp (group->name, group_name) == 0)
3424 static GKeyFileGroup *
3425 g_key_file_lookup_group (GKeyFile *key_file,
3426 const gchar *group_name)
3428 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3432 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3433 GKeyFileGroup *group,
3438 for (key_node = group->key_value_pairs;
3440 key_node = key_node->next)
3442 GKeyFileKeyValuePair *pair;
3444 pair = (GKeyFileKeyValuePair *) key_node->data;
3446 if (pair->key && strcmp (pair->key, key) == 0)
3453 static GKeyFileKeyValuePair *
3454 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3455 GKeyFileGroup *group,
3458 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3461 /* Lines starting with # or consisting entirely of whitespace are merely
3462 * recorded, not parsed. This function assumes all leading whitespace
3463 * has been stripped.
3466 g_key_file_line_is_comment (const gchar *line)
3468 return (*line == '#' || *line == '\0' || *line == '\n');
3472 g_key_file_is_group_name (const gchar *name)
3479 p = q = (gchar *) name;
3480 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3481 q = g_utf8_find_next_char (q, NULL);
3483 if (*q != '\0' || q == p)
3490 g_key_file_is_key_name (const gchar *name)
3497 p = q = (gchar *) name;
3498 /* We accept a little more than the desktop entry spec says,
3499 * since gnome-vfs uses mime-types as keys in its cache.
3501 while (*q && *q != '=' && *q != '[' && *q != ']')
3502 q = g_utf8_find_next_char (q, NULL);
3504 /* No empty keys, please */
3508 /* We accept spaces in the middle of keys to not break
3509 * existing apps, but we don't tolerate initial or final
3510 * spaces, which would lead to silent corruption when
3511 * rereading the file.
3513 if (*p == ' ' || q[-1] == ' ')
3519 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3520 q = g_utf8_find_next_char (q, NULL);
3534 /* A group in a key file is made up of a starting '[' followed by one
3535 * or more letters making up the group name followed by ']'.
3538 g_key_file_line_is_group (const gchar *line)
3548 while (*p && *p != ']')
3549 p = g_utf8_find_next_char (p, NULL);
3554 /* silently accept whitespace after the ] */
3555 p = g_utf8_find_next_char (p, NULL);
3556 while (*p == ' ' || *p == '\t')
3557 p = g_utf8_find_next_char (p, NULL);
3566 g_key_file_line_is_key_value_pair (const gchar *line)
3570 p = (gchar *) g_utf8_strchr (line, -1, '=');
3575 /* Key must be non-empty
3584 g_key_file_parse_value_as_string (GKeyFile *key_file,
3589 gchar *string_value, *p, *q0, *q;
3591 string_value = g_new (gchar, strlen (value) + 1);
3593 p = (gchar *) value;
3594 q0 = q = string_value;
3624 g_set_error_literal (error, G_KEY_FILE_ERROR,
3625 G_KEY_FILE_ERROR_INVALID_VALUE,
3626 _("Key file contains escape character "
3631 if (pieces && *p == key_file->list_separator)
3632 *q = key_file->list_separator;
3646 g_set_error (error, G_KEY_FILE_ERROR,
3647 G_KEY_FILE_ERROR_INVALID_VALUE,
3648 _("Key file contains invalid escape "
3649 "sequence '%s'"), sequence);
3658 if (pieces && (*p == key_file->list_separator))
3660 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3676 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3677 *pieces = g_slist_reverse (*pieces);
3680 return string_value;
3684 g_key_file_parse_string_as_value (GKeyFile *key_file,
3685 const gchar *string,
3686 gboolean escape_separator)
3688 gchar *value, *p, *q;
3690 gboolean parsing_leading_space;
3692 length = strlen (string) + 1;
3694 /* Worst case would be that every character needs to be escaped.
3695 * In other words every character turns to two characters
3697 value = g_new (gchar, 2 * length);
3699 p = (gchar *) string;
3701 parsing_leading_space = TRUE;
3702 while (p < (string + length - 1))
3704 gchar escaped_character[3] = { '\\', 0, 0 };
3709 if (parsing_leading_space)
3711 escaped_character[1] = 's';
3712 strcpy (q, escaped_character);
3722 if (parsing_leading_space)
3724 escaped_character[1] = 't';
3725 strcpy (q, escaped_character);
3735 escaped_character[1] = 'n';
3736 strcpy (q, escaped_character);
3740 escaped_character[1] = 'r';
3741 strcpy (q, escaped_character);
3745 escaped_character[1] = '\\';
3746 strcpy (q, escaped_character);
3748 parsing_leading_space = FALSE;
3751 if (escape_separator && *p == key_file->list_separator)
3753 escaped_character[1] = key_file->list_separator;
3754 strcpy (q, escaped_character);
3756 parsing_leading_space = TRUE;
3762 parsing_leading_space = FALSE;
3774 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3778 gchar *end_of_valid_int;
3783 long_value = strtol (value, &end_of_valid_int, 10);
3785 if (*value == '\0' || *end_of_valid_int != '\0')
3787 gchar *value_utf8 = _g_utf8_make_valid (value);
3788 g_set_error (error, G_KEY_FILE_ERROR,
3789 G_KEY_FILE_ERROR_INVALID_VALUE,
3790 _("Value '%s' cannot be interpreted "
3791 "as a number."), value_utf8);
3792 g_free (value_utf8);
3797 int_value = long_value;
3798 if (int_value != long_value || errno == ERANGE)
3800 gchar *value_utf8 = _g_utf8_make_valid (value);
3803 G_KEY_FILE_ERROR_INVALID_VALUE,
3804 _("Integer value '%s' out of range"),
3806 g_free (value_utf8);
3815 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3819 return g_strdup_printf ("%d", value);
3823 g_key_file_parse_value_as_double (GKeyFile *key_file,
3827 gchar *end_of_valid_d;
3828 gdouble double_value = 0;
3830 double_value = g_ascii_strtod (value, &end_of_valid_d);
3832 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3834 gchar *value_utf8 = _g_utf8_make_valid (value);
3835 g_set_error (error, G_KEY_FILE_ERROR,
3836 G_KEY_FILE_ERROR_INVALID_VALUE,
3837 _("Value '%s' cannot be interpreted "
3838 "as a float number."),
3840 g_free (value_utf8);
3843 return double_value;
3847 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3853 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3855 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3858 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 boolean."), value_utf8);
3863 g_free (value_utf8);
3869 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3873 return g_strdup ("true");
3875 return g_strdup ("false");
3879 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3886 string = g_string_sized_new (512);
3888 lines = g_strsplit (value, "\n", 0);
3890 for (i = 0; lines[i] != NULL; i++)
3892 if (lines[i][0] != '#')
3893 g_string_append_printf (string, "%s\n", lines[i]);
3895 g_string_append_printf (string, "%s\n", lines[i] + 1);
3899 return g_string_free (string, FALSE);
3903 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3904 const gchar *comment)
3910 string = g_string_sized_new (512);
3912 lines = g_strsplit (comment, "\n", 0);
3914 for (i = 0; lines[i] != NULL; i++)
3915 g_string_append_printf (string, "#%s%s", lines[i],
3916 lines[i + 1] == NULL? "" : "\n");
3919 return g_string_free (string, FALSE);