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.
36 #include <sys/types.h>
44 #define fstat(a,b) _fstati64(a,b)
48 #define S_ISREG(mode) ((mode)&_S_IFREG)
51 #endif /* G_OS_WIN23 */
56 #include "gfileutils.h"
62 #include "gmessages.h"
65 #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);
1634 extern GSList *_g_compute_locale_variants (const gchar *locale);
1637 * g_key_file_get_locale_string:
1638 * @key_file: a #GKeyFile
1639 * @group_name: a group name
1641 * @locale: a locale identifier or %NULL
1642 * @error: return location for a #GError, or %NULL
1644 * Returns the value associated with @key under @group_name
1645 * translated in the given @locale if available. If @locale is
1646 * %NULL then the current locale is assumed.
1648 * If @key cannot be found then %NULL is returned and @error is set
1649 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1650 * with @key cannot be interpreted or no suitable translation can
1651 * be found then the untranslated value is returned.
1653 * Return value: a newly allocated string or %NULL if the specified
1654 * key cannot be found.
1659 g_key_file_get_locale_string (GKeyFile *key_file,
1660 const gchar *group_name,
1662 const gchar *locale,
1665 gchar *candidate_key, *translated_value;
1666 GError *key_file_error;
1668 gboolean free_languages = FALSE;
1671 g_return_val_if_fail (key_file != NULL, NULL);
1672 g_return_val_if_fail (group_name != NULL, NULL);
1673 g_return_val_if_fail (key != NULL, NULL);
1675 candidate_key = NULL;
1676 translated_value = NULL;
1677 key_file_error = NULL;
1683 list = _g_compute_locale_variants (locale);
1685 languages = g_new (gchar *, g_slist_length (list) + 1);
1686 for (l = list, i = 0; l; l = l->next, i++)
1687 languages[i] = l->data;
1688 languages[i] = NULL;
1690 g_slist_free (list);
1691 free_languages = TRUE;
1695 languages = (gchar **) g_get_language_names ();
1696 free_languages = FALSE;
1699 for (i = 0; languages[i]; i++)
1701 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1703 translated_value = g_key_file_get_string (key_file,
1705 candidate_key, NULL);
1706 g_free (candidate_key);
1708 if (translated_value)
1711 g_free (translated_value);
1712 translated_value = NULL;
1715 /* Fallback to untranslated key
1717 if (!translated_value)
1719 translated_value = g_key_file_get_string (key_file, group_name, key,
1722 if (!translated_value)
1723 g_propagate_error (error, key_file_error);
1727 g_strfreev (languages);
1729 return translated_value;
1733 * g_key_file_get_locale_string_list:
1734 * @key_file: a #GKeyFile
1735 * @group_name: a group name
1737 * @locale: a locale identifier or %NULL
1738 * @length: return location for the number of returned strings or %NULL
1739 * @error: return location for a #GError or %NULL
1741 * Returns the values associated with @key under @group_name
1742 * translated in the given @locale if available. If @locale is
1743 * %NULL then the current locale is assumed.
1745 * If @key cannot be found then %NULL is returned and @error is set
1746 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1747 * with @key cannot be interpreted or no suitable translations
1748 * can be found then the untranslated values are returned. The
1749 * returned array is %NULL-terminated, so @length may optionally
1752 * Return value: a newly allocated %NULL-terminated string array
1753 * or %NULL if the key isn't found. The string array should be freed
1754 * with g_strfreev().
1759 g_key_file_get_locale_string_list (GKeyFile *key_file,
1760 const gchar *group_name,
1762 const gchar *locale,
1766 GError *key_file_error;
1767 gchar **values, *value;
1768 char list_separator[2];
1771 g_return_val_if_fail (key_file != NULL, NULL);
1772 g_return_val_if_fail (group_name != NULL, NULL);
1773 g_return_val_if_fail (key != NULL, NULL);
1775 key_file_error = NULL;
1777 value = g_key_file_get_locale_string (key_file, group_name,
1782 g_propagate_error (error, key_file_error);
1791 len = strlen (value);
1792 if (value[len - 1] == key_file->list_separator)
1793 value[len - 1] = '\0';
1795 list_separator[0] = key_file->list_separator;
1796 list_separator[1] = '\0';
1797 values = g_strsplit (value, list_separator, 0);
1802 *length = g_strv_length (values);
1808 * g_key_file_set_locale_string_list:
1809 * @key_file: a #GKeyFile
1810 * @group_name: a group name
1812 * @locale: a locale identifier
1813 * @list: a %NULL-terminated array of locale string values
1814 * @length: the length of @list
1816 * Associates a list of string values for @key and @locale under
1817 * @group_name. If the translation for @key cannot be found then
1823 g_key_file_set_locale_string_list (GKeyFile *key_file,
1824 const gchar *group_name,
1826 const gchar *locale,
1827 const gchar * const list[],
1830 GString *value_list;
1834 g_return_if_fail (key_file != NULL);
1835 g_return_if_fail (key != NULL);
1836 g_return_if_fail (locale != NULL);
1837 g_return_if_fail (length != 0);
1839 value_list = g_string_sized_new (length * 128);
1840 for (i = 0; i < length && list[i] != NULL; i++)
1844 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1845 g_string_append (value_list, value);
1846 g_string_append_c (value_list, key_file->list_separator);
1851 full_key = g_strdup_printf ("%s[%s]", key, locale);
1852 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1854 g_string_free (value_list, TRUE);
1858 * g_key_file_get_boolean:
1859 * @key_file: a #GKeyFile
1860 * @group_name: a group name
1862 * @error: return location for a #GError
1864 * Returns the value associated with @key under @group_name as a
1867 * If @key cannot be found then %FALSE is returned and @error is set
1868 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1869 * associated with @key cannot be interpreted as a boolean then %FALSE
1870 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1872 * Return value: the value associated with the key as a boolean,
1873 * or %FALSE if the key was not found or could not be parsed.
1878 g_key_file_get_boolean (GKeyFile *key_file,
1879 const gchar *group_name,
1883 GError *key_file_error = NULL;
1885 gboolean bool_value;
1887 g_return_val_if_fail (key_file != NULL, FALSE);
1888 g_return_val_if_fail (group_name != NULL, FALSE);
1889 g_return_val_if_fail (key != NULL, FALSE);
1891 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1895 g_propagate_error (error, key_file_error);
1899 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1905 if (g_error_matches (key_file_error,
1907 G_KEY_FILE_ERROR_INVALID_VALUE))
1909 g_set_error (error, G_KEY_FILE_ERROR,
1910 G_KEY_FILE_ERROR_INVALID_VALUE,
1911 _("Key file contains key '%s' "
1912 "which has value that cannot be interpreted."),
1914 g_error_free (key_file_error);
1917 g_propagate_error (error, key_file_error);
1924 * g_key_file_set_boolean:
1925 * @key_file: a #GKeyFile
1926 * @group_name: a group name
1928 * @value: %TRUE or %FALSE
1930 * Associates a new boolean value with @key under @group_name.
1931 * If @key cannot be found then it is created.
1936 g_key_file_set_boolean (GKeyFile *key_file,
1937 const gchar *group_name,
1943 g_return_if_fail (key_file != NULL);
1945 result = g_key_file_parse_boolean_as_value (key_file, value);
1946 g_key_file_set_value (key_file, group_name, key, result);
1951 * g_key_file_get_boolean_list:
1952 * @key_file: a #GKeyFile
1953 * @group_name: a group name
1955 * @length: the number of booleans returned
1956 * @error: return location for a #GError
1958 * Returns the values associated with @key under @group_name as
1961 * If @key cannot be found then %NULL is returned and @error is set to
1962 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1963 * with @key cannot be interpreted as booleans then %NULL is returned
1964 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1966 * Return value: the values associated with the key as a list of
1967 * booleans, or %NULL if the key was not found or could not be parsed.
1972 g_key_file_get_boolean_list (GKeyFile *key_file,
1973 const gchar *group_name,
1978 GError *key_file_error;
1980 gboolean *bool_values;
1983 g_return_val_if_fail (key_file != NULL, NULL);
1984 g_return_val_if_fail (group_name != NULL, NULL);
1985 g_return_val_if_fail (key != NULL, NULL);
1990 key_file_error = NULL;
1992 values = g_key_file_get_string_list (key_file, group_name, key,
1993 &num_bools, &key_file_error);
1996 g_propagate_error (error, key_file_error);
2001 bool_values = g_new (gboolean, num_bools);
2003 for (i = 0; i < num_bools; i++)
2005 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2011 g_propagate_error (error, key_file_error);
2012 g_strfreev (values);
2013 g_free (bool_values);
2018 g_strfreev (values);
2021 *length = num_bools;
2027 * g_key_file_set_boolean_list:
2028 * @key_file: a #GKeyFile
2029 * @group_name: a group name
2031 * @list: an array of boolean values
2032 * @length: length of @list
2034 * Associates a list of boolean values with @key under @group_name.
2035 * If @key cannot be found then it is created.
2036 * If @group_name is %NULL, the start_group is used.
2041 g_key_file_set_boolean_list (GKeyFile *key_file,
2042 const gchar *group_name,
2047 GString *value_list;
2050 g_return_if_fail (key_file != NULL);
2051 g_return_if_fail (list != NULL);
2053 value_list = g_string_sized_new (length * 8);
2054 for (i = 0; i < length; i++)
2058 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2060 g_string_append (value_list, value);
2061 g_string_append_c (value_list, key_file->list_separator);
2066 g_key_file_set_value (key_file, group_name, key, value_list->str);
2067 g_string_free (value_list, TRUE);
2071 * g_key_file_get_integer:
2072 * @key_file: a #GKeyFile
2073 * @group_name: a group name
2075 * @error: return location for a #GError
2077 * Returns the value associated with @key under @group_name as an
2080 * If @key cannot be found then 0 is returned and @error is set to
2081 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2082 * with @key cannot be interpreted as an integer then 0 is returned
2083 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2085 * Return value: the value associated with the key as an integer, or
2086 * 0 if the key was not found or could not be parsed.
2091 g_key_file_get_integer (GKeyFile *key_file,
2092 const gchar *group_name,
2096 GError *key_file_error;
2100 g_return_val_if_fail (key_file != NULL, -1);
2101 g_return_val_if_fail (group_name != NULL, -1);
2102 g_return_val_if_fail (key != NULL, -1);
2104 key_file_error = NULL;
2106 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2110 g_propagate_error (error, key_file_error);
2114 int_value = g_key_file_parse_value_as_integer (key_file, value,
2120 if (g_error_matches (key_file_error,
2122 G_KEY_FILE_ERROR_INVALID_VALUE))
2124 g_set_error (error, G_KEY_FILE_ERROR,
2125 G_KEY_FILE_ERROR_INVALID_VALUE,
2126 _("Key file contains key '%s' in group '%s' "
2127 "which has value that cannot be interpreted."), key,
2129 g_error_free (key_file_error);
2132 g_propagate_error (error, key_file_error);
2139 * g_key_file_set_integer:
2140 * @key_file: a #GKeyFile
2141 * @group_name: a group name
2143 * @value: an integer value
2145 * Associates a new integer value with @key under @group_name.
2146 * If @key cannot be found then it is created.
2151 g_key_file_set_integer (GKeyFile *key_file,
2152 const gchar *group_name,
2158 g_return_if_fail (key_file != NULL);
2160 result = g_key_file_parse_integer_as_value (key_file, value);
2161 g_key_file_set_value (key_file, group_name, key, result);
2166 * g_key_file_get_int64:
2167 * @key_file: a non-%NULL #GKeyFile
2168 * @group_name: a non-%NULL group name
2169 * @key: a non-%NULL key
2170 * @error: return location for a #GError
2172 * Returns the value associated with @key under @group_name as a signed
2173 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2174 * 64-bit results without truncation.
2176 * Returns: the value associated with the key as a signed 64-bit integer, or
2177 * 0 if the key was not found or could not be parsed.
2182 g_key_file_get_int64 (GKeyFile *key_file,
2183 const gchar *group_name,
2190 g_return_val_if_fail (key_file != NULL, -1);
2191 g_return_val_if_fail (group_name != NULL, -1);
2192 g_return_val_if_fail (key != NULL, -1);
2194 s = g_key_file_get_value (key_file, group_name, key, error);
2199 v = g_ascii_strtoll (s, &end, 10);
2201 if (*s == '\0' || *end != '\0')
2203 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2204 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2205 key, group_name, s);
2214 * g_key_file_set_int64:
2215 * @key_file: a #GKeyFile
2216 * @group_name: a group name
2218 * @value: an integer value
2220 * Associates a new integer value with @key under @group_name.
2221 * If @key cannot be found then it is created.
2226 g_key_file_set_int64 (GKeyFile *key_file,
2227 const gchar *group_name,
2233 g_return_if_fail (key_file != NULL);
2235 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2236 g_key_file_set_value (key_file, group_name, key, result);
2241 * g_key_file_get_uint64:
2242 * @key_file: a non-%NULL #GKeyFile
2243 * @group_name: a non-%NULL group name
2244 * @key: a non-%NULL key
2245 * @error: return location for a #GError
2247 * Returns the value associated with @key under @group_name as an unsigned
2248 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2249 * large positive results without truncation.
2251 * Returns: the value associated with the key as an unsigned 64-bit integer,
2252 * or 0 if the key was not found or could not be parsed.
2257 g_key_file_get_uint64 (GKeyFile *key_file,
2258 const gchar *group_name,
2265 g_return_val_if_fail (key_file != NULL, -1);
2266 g_return_val_if_fail (group_name != NULL, -1);
2267 g_return_val_if_fail (key != NULL, -1);
2269 s = g_key_file_get_value (key_file, group_name, key, error);
2274 v = g_ascii_strtoull (s, &end, 10);
2276 if (*s == '\0' || *end != '\0')
2278 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2279 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2280 key, group_name, s);
2289 * g_key_file_set_uint64:
2290 * @key_file: a #GKeyFile
2291 * @group_name: a group name
2293 * @value: an integer value
2295 * Associates a new integer value with @key under @group_name.
2296 * If @key cannot be found then it is created.
2301 g_key_file_set_uint64 (GKeyFile *key_file,
2302 const gchar *group_name,
2308 g_return_if_fail (key_file != NULL);
2310 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2311 g_key_file_set_value (key_file, group_name, key, result);
2316 * g_key_file_get_integer_list:
2317 * @key_file: a #GKeyFile
2318 * @group_name: a group name
2320 * @length: the number of integers returned
2321 * @error: return location for a #GError
2323 * Returns the values associated with @key under @group_name as
2326 * If @key cannot be found then %NULL is returned and @error is set to
2327 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2328 * with @key cannot be interpreted as integers then %NULL is returned
2329 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2331 * Return value: the values associated with the key as a list of
2332 * integers, or %NULL if the key was not found or could not be parsed.
2337 g_key_file_get_integer_list (GKeyFile *key_file,
2338 const gchar *group_name,
2343 GError *key_file_error = NULL;
2348 g_return_val_if_fail (key_file != NULL, NULL);
2349 g_return_val_if_fail (group_name != NULL, NULL);
2350 g_return_val_if_fail (key != NULL, NULL);
2355 values = g_key_file_get_string_list (key_file, group_name, key,
2356 &num_ints, &key_file_error);
2359 g_propagate_error (error, key_file_error);
2364 int_values = g_new (gint, num_ints);
2366 for (i = 0; i < num_ints; i++)
2368 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2374 g_propagate_error (error, key_file_error);
2375 g_strfreev (values);
2376 g_free (int_values);
2381 g_strfreev (values);
2390 * g_key_file_set_integer_list:
2391 * @key_file: a #GKeyFile
2392 * @group_name: a group name
2394 * @list: an array of integer values
2395 * @length: number of integer values in @list
2397 * Associates a list of integer values with @key under @group_name.
2398 * If @key cannot be found then it is created.
2403 g_key_file_set_integer_list (GKeyFile *key_file,
2404 const gchar *group_name,
2412 g_return_if_fail (key_file != NULL);
2413 g_return_if_fail (list != NULL);
2415 values = g_string_sized_new (length * 16);
2416 for (i = 0; i < length; i++)
2420 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2422 g_string_append (values, value);
2423 g_string_append_c (values, key_file->list_separator);
2428 g_key_file_set_value (key_file, group_name, key, values->str);
2429 g_string_free (values, TRUE);
2433 * g_key_file_get_double:
2434 * @key_file: a #GKeyFile
2435 * @group_name: a group name
2437 * @error: return location for a #GError
2439 * Returns the value associated with @key under @group_name as a
2440 * double. If @group_name is %NULL, the start_group is used.
2442 * If @key cannot be found then 0.0 is returned and @error is set to
2443 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2444 * with @key cannot be interpreted as a double then 0.0 is returned
2445 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2447 * Return value: the value associated with the key as a double, or
2448 * 0.0 if the key was not found or could not be parsed.
2453 g_key_file_get_double (GKeyFile *key_file,
2454 const gchar *group_name,
2458 GError *key_file_error;
2460 gdouble double_value;
2462 g_return_val_if_fail (key_file != NULL, -1);
2463 g_return_val_if_fail (group_name != NULL, -1);
2464 g_return_val_if_fail (key != NULL, -1);
2466 key_file_error = NULL;
2468 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2472 g_propagate_error (error, key_file_error);
2476 double_value = g_key_file_parse_value_as_double (key_file, value,
2482 if (g_error_matches (key_file_error,
2484 G_KEY_FILE_ERROR_INVALID_VALUE))
2486 g_set_error (error, G_KEY_FILE_ERROR,
2487 G_KEY_FILE_ERROR_INVALID_VALUE,
2488 _("Key file contains key '%s' in group '%s' "
2489 "which has value that cannot be interpreted."), key,
2491 g_error_free (key_file_error);
2494 g_propagate_error (error, key_file_error);
2497 return double_value;
2501 * g_key_file_set_double:
2502 * @key_file: a #GKeyFile
2503 * @group_name: a group name
2505 * @value: an double value
2507 * Associates a new double value with @key under @group_name.
2508 * If @key cannot be found then it is created.
2513 g_key_file_set_double (GKeyFile *key_file,
2514 const gchar *group_name,
2518 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2520 g_return_if_fail (key_file != NULL);
2522 g_ascii_dtostr (result, sizeof (result), value);
2523 g_key_file_set_value (key_file, group_name, key, result);
2527 * g_key_file_get_double_list:
2528 * @key_file: a #GKeyFile
2529 * @group_name: a group name
2531 * @length: the number of doubles returned
2532 * @error: return location for a #GError
2534 * Returns the values associated with @key under @group_name as
2537 * If @key cannot be found then %NULL is returned and @error is set to
2538 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2539 * with @key cannot be interpreted as doubles then %NULL is returned
2540 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2542 * Return value: the values associated with the key as a list of
2543 * doubles, or %NULL if the key was not found or could not be parsed.
2548 g_key_file_get_double_list (GKeyFile *key_file,
2549 const gchar *group_name,
2554 GError *key_file_error = NULL;
2556 gdouble *double_values;
2557 gsize i, num_doubles;
2559 g_return_val_if_fail (key_file != NULL, NULL);
2560 g_return_val_if_fail (group_name != NULL, NULL);
2561 g_return_val_if_fail (key != NULL, NULL);
2566 values = g_key_file_get_string_list (key_file, group_name, key,
2567 &num_doubles, &key_file_error);
2570 g_propagate_error (error, key_file_error);
2575 double_values = g_new (gdouble, num_doubles);
2577 for (i = 0; i < num_doubles; i++)
2579 double_values[i] = g_key_file_parse_value_as_double (key_file,
2585 g_propagate_error (error, key_file_error);
2586 g_strfreev (values);
2587 g_free (double_values);
2592 g_strfreev (values);
2595 *length = num_doubles;
2597 return double_values;
2601 * g_key_file_set_double_list:
2602 * @key_file: a #GKeyFile
2603 * @group_name: a group name
2605 * @list: an array of double values
2606 * @length: number of double values in @list
2608 * Associates a list of double values with @key under
2609 * @group_name. If @key cannot be found then it is created.
2614 g_key_file_set_double_list (GKeyFile *key_file,
2615 const gchar *group_name,
2623 g_return_if_fail (key_file != NULL);
2624 g_return_if_fail (list != NULL);
2626 values = g_string_sized_new (length * 16);
2627 for (i = 0; i < length; i++)
2629 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2631 g_ascii_dtostr( result, sizeof (result), list[i] );
2633 g_string_append (values, result);
2634 g_string_append_c (values, key_file->list_separator);
2637 g_key_file_set_value (key_file, group_name, key, values->str);
2638 g_string_free (values, TRUE);
2642 g_key_file_set_key_comment (GKeyFile *key_file,
2643 const gchar *group_name,
2645 const gchar *comment,
2648 GKeyFileGroup *group;
2649 GKeyFileKeyValuePair *pair;
2650 GList *key_node, *comment_node, *tmp;
2652 group = g_key_file_lookup_group (key_file, group_name);
2655 g_set_error (error, G_KEY_FILE_ERROR,
2656 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2657 _("Key file does not have group '%s'"),
2658 group_name ? group_name : "(null)");
2663 /* First find the key the comments are supposed to be
2666 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2668 if (key_node == NULL)
2670 g_set_error (error, G_KEY_FILE_ERROR,
2671 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2672 _("Key file does not have key '%s' in group '%s'"),
2677 /* Then find all the comments already associated with the
2680 tmp = key_node->next;
2683 pair = (GKeyFileKeyValuePair *) tmp->data;
2685 if (pair->key != NULL)
2690 g_key_file_remove_key_value_pair_node (key_file, group,
2694 if (comment == NULL)
2697 /* Now we can add our new comment
2699 pair = g_slice_new (GKeyFileKeyValuePair);
2701 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2703 key_node = g_list_insert (key_node, pair, 1);
2709 g_key_file_set_group_comment (GKeyFile *key_file,
2710 const gchar *group_name,
2711 const gchar *comment,
2714 GKeyFileGroup *group;
2716 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2718 group = g_key_file_lookup_group (key_file, group_name);
2721 g_set_error (error, G_KEY_FILE_ERROR,
2722 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2723 _("Key file does not have group '%s'"),
2724 group_name ? group_name : "(null)");
2729 /* First remove any existing comment
2733 g_key_file_key_value_pair_free (group->comment);
2734 group->comment = NULL;
2737 if (comment == NULL)
2740 /* Now we can add our new comment
2742 group->comment = g_slice_new (GKeyFileKeyValuePair);
2743 group->comment->key = NULL;
2744 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2750 g_key_file_set_top_comment (GKeyFile *key_file,
2751 const gchar *comment,
2755 GKeyFileGroup *group;
2756 GKeyFileKeyValuePair *pair;
2758 /* The last group in the list should be the top (comments only)
2761 g_warn_if_fail (key_file->groups != NULL);
2762 group_node = g_list_last (key_file->groups);
2763 group = (GKeyFileGroup *) group_node->data;
2764 g_warn_if_fail (group->name == NULL);
2766 /* Note all keys must be comments at the top of
2767 * the file, so we can just free it all.
2769 if (group->key_value_pairs != NULL)
2771 g_list_foreach (group->key_value_pairs,
2772 (GFunc) g_key_file_key_value_pair_free,
2774 g_list_free (group->key_value_pairs);
2775 group->key_value_pairs = NULL;
2778 if (comment == NULL)
2781 pair = g_slice_new (GKeyFileKeyValuePair);
2783 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2785 group->key_value_pairs =
2786 g_list_prepend (group->key_value_pairs, pair);
2792 * g_key_file_set_comment:
2793 * @key_file: a #GKeyFile
2794 * @group_name: a group name, or %NULL
2796 * @comment: a comment
2797 * @error: return location for a #GError
2799 * Places a comment above @key from @group_name.
2800 * If @key is %NULL then @comment will be written above @group_name.
2801 * If both @key and @group_name are %NULL, then @comment will be
2802 * written above the first group in the file.
2804 * Returns: %TRUE if the comment was written, %FALSE otherwise
2809 g_key_file_set_comment (GKeyFile *key_file,
2810 const gchar *group_name,
2812 const gchar *comment,
2815 g_return_val_if_fail (key_file != NULL, FALSE);
2817 if (group_name != NULL && key != NULL)
2819 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2822 else if (group_name != NULL)
2824 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2829 if (!g_key_file_set_top_comment (key_file, comment, error))
2833 if (comment != NULL)
2834 key_file->approximate_size += strlen (comment);
2840 g_key_file_get_key_comment (GKeyFile *key_file,
2841 const gchar *group_name,
2845 GKeyFileGroup *group;
2846 GKeyFileKeyValuePair *pair;
2847 GList *key_node, *tmp;
2851 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2853 group = g_key_file_lookup_group (key_file, group_name);
2856 g_set_error (error, G_KEY_FILE_ERROR,
2857 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2858 _("Key file does not have group '%s'"),
2859 group_name ? group_name : "(null)");
2864 /* First find the key the comments are supposed to be
2867 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2869 if (key_node == NULL)
2871 g_set_error (error, G_KEY_FILE_ERROR,
2872 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2873 _("Key file does not have key '%s' in group '%s'"),
2880 /* Then find all the comments already associated with the
2881 * key and concatentate them.
2883 tmp = key_node->next;
2884 if (!key_node->next)
2887 pair = (GKeyFileKeyValuePair *) tmp->data;
2888 if (pair->key != NULL)
2893 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2895 if (pair->key != NULL)
2901 while (tmp != key_node)
2903 pair = (GKeyFileKeyValuePair *) tmp->data;
2906 string = g_string_sized_new (512);
2908 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2909 g_string_append (string, comment);
2917 comment = string->str;
2918 g_string_free (string, FALSE);
2927 get_group_comment (GKeyFile *key_file,
2928 GKeyFileGroup *group,
2937 tmp = group->key_value_pairs;
2940 GKeyFileKeyValuePair *pair;
2942 pair = (GKeyFileKeyValuePair *) tmp->data;
2944 if (pair->key != NULL)
2950 if (tmp->next == NULL)
2958 GKeyFileKeyValuePair *pair;
2960 pair = (GKeyFileKeyValuePair *) tmp->data;
2963 string = g_string_sized_new (512);
2965 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2966 g_string_append (string, comment);
2973 return g_string_free (string, FALSE);
2979 g_key_file_get_group_comment (GKeyFile *key_file,
2980 const gchar *group_name,
2984 GKeyFileGroup *group;
2986 group = g_key_file_lookup_group (key_file, group_name);
2989 g_set_error (error, G_KEY_FILE_ERROR,
2990 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2991 _("Key file does not have group '%s'"),
2992 group_name ? group_name : "(null)");
2998 return g_strdup (group->comment->value);
3000 group_node = g_key_file_lookup_group_node (key_file, group_name);
3001 group_node = group_node->next;
3002 group = (GKeyFileGroup *)group_node->data;
3003 return get_group_comment (key_file, group, error);
3007 g_key_file_get_top_comment (GKeyFile *key_file,
3011 GKeyFileGroup *group;
3013 /* The last group in the list should be the top (comments only)
3016 g_warn_if_fail (key_file->groups != NULL);
3017 group_node = g_list_last (key_file->groups);
3018 group = (GKeyFileGroup *) group_node->data;
3019 g_warn_if_fail (group->name == NULL);
3021 return get_group_comment (key_file, group, error);
3025 * g_key_file_get_comment:
3026 * @key_file: a #GKeyFile
3027 * @group_name: a group name, or %NULL
3029 * @error: return location for a #GError
3031 * Retrieves a comment above @key from @group_name.
3032 * If @key is %NULL then @comment will be read from above
3033 * @group_name. If both @key and @group_name are %NULL, then
3034 * @comment will be read from above the first group in the file.
3036 * Returns: a comment that should be freed with g_free()
3041 g_key_file_get_comment (GKeyFile *key_file,
3042 const gchar *group_name,
3046 g_return_val_if_fail (key_file != NULL, NULL);
3048 if (group_name != NULL && key != NULL)
3049 return g_key_file_get_key_comment (key_file, group_name, key, error);
3050 else if (group_name != NULL)
3051 return g_key_file_get_group_comment (key_file, group_name, error);
3053 return g_key_file_get_top_comment (key_file, error);
3057 * g_key_file_remove_comment:
3058 * @key_file: a #GKeyFile
3059 * @group_name: a group name, or %NULL
3061 * @error: return location for a #GError
3063 * Removes a comment above @key from @group_name.
3064 * If @key is %NULL then @comment will be removed above @group_name.
3065 * If both @key and @group_name are %NULL, then @comment will
3066 * be removed above the first group in the file.
3068 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3074 g_key_file_remove_comment (GKeyFile *key_file,
3075 const gchar *group_name,
3079 g_return_val_if_fail (key_file != NULL, FALSE);
3081 if (group_name != NULL && key != NULL)
3082 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3083 else if (group_name != NULL)
3084 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3086 return g_key_file_set_top_comment (key_file, NULL, error);
3090 * g_key_file_has_group:
3091 * @key_file: a #GKeyFile
3092 * @group_name: a group name
3094 * Looks whether the key file has the group @group_name.
3096 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3101 g_key_file_has_group (GKeyFile *key_file,
3102 const gchar *group_name)
3104 g_return_val_if_fail (key_file != NULL, FALSE);
3105 g_return_val_if_fail (group_name != NULL, FALSE);
3107 return g_key_file_lookup_group (key_file, group_name) != NULL;
3111 * g_key_file_has_key:
3112 * @key_file: a #GKeyFile
3113 * @group_name: a group name
3115 * @error: return location for a #GError
3117 * Looks whether the key file has the key @key in the group
3120 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3126 g_key_file_has_key (GKeyFile *key_file,
3127 const gchar *group_name,
3131 GKeyFileKeyValuePair *pair;
3132 GKeyFileGroup *group;
3134 g_return_val_if_fail (key_file != NULL, FALSE);
3135 g_return_val_if_fail (group_name != NULL, FALSE);
3136 g_return_val_if_fail (key != NULL, FALSE);
3138 group = g_key_file_lookup_group (key_file, group_name);
3142 g_set_error (error, G_KEY_FILE_ERROR,
3143 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3144 _("Key file does not have group '%s'"),
3145 group_name ? group_name : "(null)");
3150 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3152 return pair != NULL;
3156 g_key_file_add_group (GKeyFile *key_file,
3157 const gchar *group_name)
3159 GKeyFileGroup *group;
3161 g_return_if_fail (key_file != NULL);
3162 g_return_if_fail (g_key_file_is_group_name (group_name));
3164 group = g_key_file_lookup_group (key_file, group_name);
3167 key_file->current_group = group;
3171 group = g_slice_new0 (GKeyFileGroup);
3172 group->name = g_strdup (group_name);
3173 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3174 key_file->groups = g_list_prepend (key_file->groups, group);
3175 key_file->approximate_size += strlen (group_name) + 3;
3176 key_file->current_group = group;
3178 if (key_file->start_group == NULL)
3179 key_file->start_group = group;
3181 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3185 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3190 g_free (pair->value);
3191 g_slice_free (GKeyFileKeyValuePair, pair);
3195 /* Be careful not to call this function on a node with data in the
3196 * lookup map without removing it from the lookup map, first.
3198 * Some current cases where this warning is not a concern are
3200 * - the node being removed is a comment node
3201 * - the entire lookup map is getting destroyed soon after
3205 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3206 GKeyFileGroup *group,
3210 GKeyFileKeyValuePair *pair;
3212 pair = (GKeyFileKeyValuePair *) pair_node->data;
3214 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3216 if (pair->key != NULL)
3217 key_file->approximate_size -= strlen (pair->key) + 1;
3219 g_warn_if_fail (pair->value != NULL);
3220 key_file->approximate_size -= strlen (pair->value);
3222 g_key_file_key_value_pair_free (pair);
3224 g_list_free_1 (pair_node);
3228 g_key_file_remove_group_node (GKeyFile *key_file,
3231 GKeyFileGroup *group;
3234 group = (GKeyFileGroup *) group_node->data;
3237 g_hash_table_remove (key_file->group_hash, group->name);
3239 /* If the current group gets deleted make the current group the last
3242 if (key_file->current_group == group)
3244 /* groups should always contain at least the top comment group,
3245 * unless g_key_file_clear has been called
3247 if (key_file->groups)
3248 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3250 key_file->current_group = NULL;
3253 /* If the start group gets deleted make the start group the first
3256 if (key_file->start_group == group)
3258 tmp = g_list_last (key_file->groups);
3261 if (tmp != group_node &&
3262 ((GKeyFileGroup *) tmp->data)->name != NULL)
3269 key_file->start_group = (GKeyFileGroup *) tmp->data;
3271 key_file->start_group = NULL;
3274 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3276 if (group->name != NULL)
3277 key_file->approximate_size -= strlen (group->name) + 3;
3279 tmp = group->key_value_pairs;
3286 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3289 g_warn_if_fail (group->key_value_pairs == NULL);
3291 if (group->lookup_map)
3293 g_hash_table_destroy (group->lookup_map);
3294 group->lookup_map = NULL;
3297 g_free ((gchar *) group->name);
3298 g_slice_free (GKeyFileGroup, group);
3299 g_list_free_1 (group_node);
3303 * g_key_file_remove_group:
3304 * @key_file: a #GKeyFile
3305 * @group_name: a group name
3306 * @error: return location for a #GError or %NULL
3308 * Removes the specified group, @group_name,
3309 * from the key file.
3311 * Returns: %TRUE if the group was removed, %FALSE otherwise
3316 g_key_file_remove_group (GKeyFile *key_file,
3317 const gchar *group_name,
3322 g_return_val_if_fail (key_file != NULL, FALSE);
3323 g_return_val_if_fail (group_name != NULL, FALSE);
3325 group_node = g_key_file_lookup_group_node (key_file, group_name);
3329 g_set_error (error, G_KEY_FILE_ERROR,
3330 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3331 _("Key file does not have group '%s'"),
3336 g_key_file_remove_group_node (key_file, group_node);
3342 g_key_file_add_key (GKeyFile *key_file,
3343 GKeyFileGroup *group,
3347 GKeyFileKeyValuePair *pair;
3349 pair = g_slice_new (GKeyFileKeyValuePair);
3350 pair->key = g_strdup (key);
3351 pair->value = g_strdup (value);
3353 g_hash_table_replace (group->lookup_map, pair->key, pair);
3354 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3355 group->has_trailing_blank_line = FALSE;
3356 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3360 * g_key_file_remove_key:
3361 * @key_file: a #GKeyFile
3362 * @group_name: a group name
3363 * @key: a key name to remove
3364 * @error: return location for a #GError or %NULL
3366 * Removes @key in @group_name from the key file.
3368 * Returns: %TRUE if the key was removed, %FALSE otherwise
3373 g_key_file_remove_key (GKeyFile *key_file,
3374 const gchar *group_name,
3378 GKeyFileGroup *group;
3379 GKeyFileKeyValuePair *pair;
3381 g_return_val_if_fail (key_file != NULL, FALSE);
3382 g_return_val_if_fail (group_name != NULL, FALSE);
3383 g_return_val_if_fail (key != NULL, FALSE);
3387 group = g_key_file_lookup_group (key_file, group_name);
3390 g_set_error (error, G_KEY_FILE_ERROR,
3391 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3392 _("Key file does not have group '%s'"),
3393 group_name ? group_name : "(null)");
3397 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3401 g_set_error (error, G_KEY_FILE_ERROR,
3402 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3403 _("Key file does not have key '%s' in group '%s'"),
3408 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3410 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3411 g_hash_table_remove (group->lookup_map, pair->key);
3412 g_key_file_key_value_pair_free (pair);
3418 g_key_file_lookup_group_node (GKeyFile *key_file,
3419 const gchar *group_name)
3421 GKeyFileGroup *group;
3424 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3426 group = (GKeyFileGroup *) tmp->data;
3428 if (group && group->name && strcmp (group->name, group_name) == 0)
3435 static GKeyFileGroup *
3436 g_key_file_lookup_group (GKeyFile *key_file,
3437 const gchar *group_name)
3439 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3443 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3444 GKeyFileGroup *group,
3449 for (key_node = group->key_value_pairs;
3451 key_node = key_node->next)
3453 GKeyFileKeyValuePair *pair;
3455 pair = (GKeyFileKeyValuePair *) key_node->data;
3457 if (pair->key && strcmp (pair->key, key) == 0)
3464 static GKeyFileKeyValuePair *
3465 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3466 GKeyFileGroup *group,
3469 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3472 /* Lines starting with # or consisting entirely of whitespace are merely
3473 * recorded, not parsed. This function assumes all leading whitespace
3474 * has been stripped.
3477 g_key_file_line_is_comment (const gchar *line)
3479 return (*line == '#' || *line == '\0' || *line == '\n');
3483 g_key_file_is_group_name (const gchar *name)
3490 p = q = (gchar *) name;
3491 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3492 q = g_utf8_find_next_char (q, NULL);
3494 if (*q != '\0' || q == p)
3501 g_key_file_is_key_name (const gchar *name)
3508 p = q = (gchar *) name;
3509 /* We accept a little more than the desktop entry spec says,
3510 * since gnome-vfs uses mime-types as keys in its cache.
3512 while (*q && *q != '=' && *q != '[' && *q != ']')
3513 q = g_utf8_find_next_char (q, NULL);
3515 /* No empty keys, please */
3519 /* We accept spaces in the middle of keys to not break
3520 * existing apps, but we don't tolerate initial or final
3521 * spaces, which would lead to silent corruption when
3522 * rereading the file.
3524 if (*p == ' ' || q[-1] == ' ')
3530 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3531 q = g_utf8_find_next_char (q, NULL);
3545 /* A group in a key file is made up of a starting '[' followed by one
3546 * or more letters making up the group name followed by ']'.
3549 g_key_file_line_is_group (const gchar *line)
3559 while (*p && *p != ']')
3560 p = g_utf8_find_next_char (p, NULL);
3565 /* silently accept whitespace after the ] */
3566 p = g_utf8_find_next_char (p, NULL);
3567 while (*p == ' ' || *p == '\t')
3568 p = g_utf8_find_next_char (p, NULL);
3577 g_key_file_line_is_key_value_pair (const gchar *line)
3581 p = (gchar *) g_utf8_strchr (line, -1, '=');
3586 /* Key must be non-empty
3595 g_key_file_parse_value_as_string (GKeyFile *key_file,
3600 gchar *string_value, *p, *q0, *q;
3602 string_value = g_new (gchar, strlen (value) + 1);
3604 p = (gchar *) value;
3605 q0 = q = string_value;
3635 g_set_error_literal (error, G_KEY_FILE_ERROR,
3636 G_KEY_FILE_ERROR_INVALID_VALUE,
3637 _("Key file contains escape character "
3642 if (pieces && *p == key_file->list_separator)
3643 *q = key_file->list_separator;
3657 g_set_error (error, G_KEY_FILE_ERROR,
3658 G_KEY_FILE_ERROR_INVALID_VALUE,
3659 _("Key file contains invalid escape "
3660 "sequence '%s'"), sequence);
3669 if (pieces && (*p == key_file->list_separator))
3671 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3687 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3688 *pieces = g_slist_reverse (*pieces);
3691 return string_value;
3695 g_key_file_parse_string_as_value (GKeyFile *key_file,
3696 const gchar *string,
3697 gboolean escape_separator)
3699 gchar *value, *p, *q;
3701 gboolean parsing_leading_space;
3703 length = strlen (string) + 1;
3705 /* Worst case would be that every character needs to be escaped.
3706 * In other words every character turns to two characters
3708 value = g_new (gchar, 2 * length);
3710 p = (gchar *) string;
3712 parsing_leading_space = TRUE;
3713 while (p < (string + length - 1))
3715 gchar escaped_character[3] = { '\\', 0, 0 };
3720 if (parsing_leading_space)
3722 escaped_character[1] = 's';
3723 strcpy (q, escaped_character);
3733 if (parsing_leading_space)
3735 escaped_character[1] = 't';
3736 strcpy (q, escaped_character);
3746 escaped_character[1] = 'n';
3747 strcpy (q, escaped_character);
3751 escaped_character[1] = 'r';
3752 strcpy (q, escaped_character);
3756 escaped_character[1] = '\\';
3757 strcpy (q, escaped_character);
3759 parsing_leading_space = FALSE;
3762 if (escape_separator && *p == key_file->list_separator)
3764 escaped_character[1] = key_file->list_separator;
3765 strcpy (q, escaped_character);
3767 parsing_leading_space = TRUE;
3773 parsing_leading_space = FALSE;
3785 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3789 gchar *end_of_valid_int;
3794 long_value = strtol (value, &end_of_valid_int, 10);
3796 if (*value == '\0' || *end_of_valid_int != '\0')
3798 gchar *value_utf8 = _g_utf8_make_valid (value);
3799 g_set_error (error, G_KEY_FILE_ERROR,
3800 G_KEY_FILE_ERROR_INVALID_VALUE,
3801 _("Value '%s' cannot be interpreted "
3802 "as a number."), value_utf8);
3803 g_free (value_utf8);
3808 int_value = long_value;
3809 if (int_value != long_value || errno == ERANGE)
3811 gchar *value_utf8 = _g_utf8_make_valid (value);
3814 G_KEY_FILE_ERROR_INVALID_VALUE,
3815 _("Integer value '%s' out of range"),
3817 g_free (value_utf8);
3826 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3830 return g_strdup_printf ("%d", value);
3834 g_key_file_parse_value_as_double (GKeyFile *key_file,
3838 gchar *end_of_valid_d;
3839 gdouble double_value = 0;
3841 double_value = g_ascii_strtod (value, &end_of_valid_d);
3843 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3845 gchar *value_utf8 = _g_utf8_make_valid (value);
3846 g_set_error (error, G_KEY_FILE_ERROR,
3847 G_KEY_FILE_ERROR_INVALID_VALUE,
3848 _("Value '%s' cannot be interpreted "
3849 "as a float number."),
3851 g_free (value_utf8);
3854 return double_value;
3858 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3864 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3866 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3869 value_utf8 = _g_utf8_make_valid (value);
3870 g_set_error (error, G_KEY_FILE_ERROR,
3871 G_KEY_FILE_ERROR_INVALID_VALUE,
3872 _("Value '%s' cannot be interpreted "
3873 "as a boolean."), value_utf8);
3874 g_free (value_utf8);
3880 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3884 return g_strdup ("true");
3886 return g_strdup ("false");
3890 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3897 string = g_string_sized_new (512);
3899 lines = g_strsplit (value, "\n", 0);
3901 for (i = 0; lines[i] != NULL; i++)
3903 if (lines[i][0] != '#')
3904 g_string_append_printf (string, "%s\n", lines[i]);
3906 g_string_append_printf (string, "%s\n", lines[i] + 1);
3910 return g_string_free (string, FALSE);
3914 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3915 const gchar *comment)
3921 string = g_string_sized_new (512);
3923 lines = g_strsplit (comment, "\n", 0);
3925 for (i = 0; lines[i] != NULL; i++)
3926 g_string_append_printf (string, "#%s%s", lines[i],
3927 lines[i + 1] == NULL? "" : "\n");
3930 return g_string_free (string, FALSE);
3933 #define __G_KEY_FILE_C__
3934 #include "galiasdef.c"