1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
5 * Written by Ray Strode <rstrode@redhat.com>
6 * Matthias Clasen <mclasen@redhat.com>
8 * GLib is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * GLib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with GLib; see the file COPYING.LIB. If not,
20 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
35 #include <sys/types.h>
44 #define S_ISREG(mode) ((mode)&_S_IFREG)
47 #endif /* G_OS_WIN23 */
52 #include "gfileutils.h"
58 #include "gmessages.h"
61 #include "gstrfuncs.h"
64 typedef struct _GKeyFileGroup GKeyFileGroup;
70 GKeyFileGroup *start_group;
71 GKeyFileGroup *current_group;
73 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
75 /* Used for sizing the output buffer during serialization
77 gsize approximate_size;
84 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
88 const gchar *name; /* NULL for above first group (which will be comments) */
90 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
92 GList *key_value_pairs;
94 /* Used in parallel with key_value_pairs for
95 * increased lookup performance
97 GHashTable *lookup_map;
100 struct _GKeyFileKeyValuePair
102 gchar *key; /* NULL for comments */
106 static gint find_file_in_data_dirs (const gchar *file,
110 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
114 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
115 const gchar *group_name);
116 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
117 const gchar *group_name);
119 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
120 GKeyFileGroup *group,
122 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
123 GKeyFileGroup *group,
126 static void g_key_file_remove_group_node (GKeyFile *key_file,
128 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
129 GKeyFileGroup *group,
132 static void g_key_file_add_key (GKeyFile *key_file,
133 GKeyFileGroup *group,
136 static void g_key_file_add_group (GKeyFile *key_file,
137 const gchar *group_name);
138 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
139 static gboolean g_key_file_line_is_comment (const gchar *line);
140 static gboolean g_key_file_line_is_group (const gchar *line);
141 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
142 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
146 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
148 gboolean escape_separator);
149 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
152 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
154 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
157 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
159 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
161 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
162 const gchar *comment);
163 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
167 static void g_key_file_parse_comment (GKeyFile *key_file,
171 static void g_key_file_parse_group (GKeyFile *key_file,
175 static gchar *key_get_locale (const gchar *key);
176 static void g_key_file_parse_data (GKeyFile *key_file,
180 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
185 g_key_file_error_quark (void)
187 static GQuark error_quark = 0;
189 if (error_quark == 0)
190 error_quark = g_quark_from_static_string ("g-key-file-error-quark");
196 g_key_file_init (GKeyFile *key_file)
198 key_file->current_group = g_new0 (GKeyFileGroup, 1);
199 key_file->groups = g_list_prepend (NULL, key_file->current_group);
200 key_file->start_group = NULL;
201 key_file->parse_buffer = g_string_sized_new (128);
202 key_file->approximate_size = 0;
203 key_file->list_separator = ';';
208 g_key_file_clear (GKeyFile *key_file)
210 GList *tmp, *group_node;
212 if (key_file->parse_buffer)
213 g_string_free (key_file->parse_buffer, TRUE);
215 tmp = key_file->groups;
220 g_key_file_remove_group_node (key_file, group_node);
223 g_assert (key_file->groups == NULL);
230 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
231 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
232 * read an existing key file.
234 * Return value: an empty #GKeyFile.
239 g_key_file_new (void)
243 key_file = g_new0 (GKeyFile, 1);
244 g_key_file_init (key_file);
250 * g_key_file_set_list_separator:
251 * @key_file: a #GKeyFile
252 * @separator: the separator
254 * Sets the character which is used to separate
255 * values in lists. Typically ';' or ',' are used
256 * as separators. The default list separator is ';'.
261 g_key_file_set_list_separator (GKeyFile *key_file,
264 key_file->list_separator = separator;
268 /* Iterates through all the directories in *dirs trying to
269 * open file. When it successfully locates and opens a file it
270 * returns the file descriptor to the open file. It also
271 * outputs the absolute path of the file in output_file and
272 * leaves the unchecked directories in *dirs.
275 find_file_in_data_dirs (const gchar *file,
280 gchar **data_dirs, *data_dir, *path;
291 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
293 gchar *candidate_file, *sub_dir;
295 candidate_file = (gchar *) file;
296 sub_dir = g_strdup ("");
297 while (candidate_file != NULL && fd < 0)
301 path = g_build_filename (data_dir, sub_dir,
302 candidate_file, NULL);
304 fd = g_open (path, O_RDONLY, 0);
312 candidate_file = strchr (candidate_file, '-');
314 if (candidate_file == NULL)
320 sub_dir = g_strndup (file, candidate_file - file - 1);
322 for (p = sub_dir; *p != '\0'; p++)
325 *p = G_DIR_SEPARATOR;
336 g_set_error (error, G_KEY_FILE_ERROR,
337 G_KEY_FILE_ERROR_NOT_FOUND,
338 _("Valid key file could not be "
339 "found in data dirs"));
342 if (output_file != NULL && fd > 0)
343 *output_file = g_strdup (path);
349 g_key_file_load_from_fd (GKeyFile *key_file,
354 GError *key_file_error = NULL;
356 struct stat stat_buf;
357 gchar read_buf[4096];
359 if (fstat (fd, &stat_buf) < 0)
361 g_set_error (error, G_FILE_ERROR,
362 g_file_error_from_errno (errno),
363 "%s", g_strerror (errno));
367 if (!S_ISREG (stat_buf.st_mode))
369 g_set_error (error, G_KEY_FILE_ERROR,
370 G_KEY_FILE_ERROR_PARSE,
371 _("Not a regular file"));
375 if (stat_buf.st_size == 0)
377 g_set_error (error, G_KEY_FILE_ERROR,
378 G_KEY_FILE_ERROR_PARSE,
383 if (key_file->approximate_size > 0)
385 g_key_file_clear (key_file);
386 g_key_file_init (key_file);
388 key_file->flags = flags;
393 bytes_read = read (fd, read_buf, 4096);
395 if (bytes_read == 0) /* End of File */
400 if (errno == EINTR || errno == EAGAIN)
403 g_set_error (error, G_FILE_ERROR,
404 g_file_error_from_errno (errno),
405 "%s", g_strerror (errno));
409 g_key_file_parse_data (key_file,
410 read_buf, bytes_read,
413 while (!key_file_error);
417 g_propagate_error (error, key_file_error);
421 g_key_file_flush_parse_buffer (key_file, &key_file_error);
425 g_propagate_error (error, key_file_error);
433 * g_key_file_load_from_file:
434 * @key_file: an empty #GKeyFile struct
435 * @file: the path of a filename to load, in the GLib file name encoding
436 * @flags: flags from #GKeyFileFlags
437 * @error: return location for a #GError, or %NULL
439 * Loads a key file into an empty #GKeyFile structure.
440 * If the file could not be loaded then %error is set to
441 * either a #GFileError or #GKeyFileError.
443 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
447 g_key_file_load_from_file (GKeyFile *key_file,
452 GError *key_file_error = NULL;
455 g_return_val_if_fail (key_file != NULL, FALSE);
456 g_return_val_if_fail (file != NULL, FALSE);
458 fd = g_open (file, O_RDONLY, 0);
462 g_set_error (error, G_FILE_ERROR,
463 g_file_error_from_errno (errno),
464 "%s", g_strerror (errno));
468 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
473 g_propagate_error (error, key_file_error);
481 * g_key_file_load_from_data:
482 * @key_file: an empty #GKeyFile struct
483 * @data: key file loaded in memory.
484 * @length: the length of @data in bytes
485 * @flags: flags from #GKeyFileFlags
486 * @error: return location for a #GError, or %NULL
488 * Loads a key file from memory into an empty #GKeyFile structure. If
489 * the object cannot be created then %error is set to a
492 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
496 g_key_file_load_from_data (GKeyFile *key_file,
502 GError *key_file_error = NULL;
504 g_return_val_if_fail (key_file != NULL, FALSE);
505 g_return_val_if_fail (data != NULL, FALSE);
506 g_return_val_if_fail (length != 0, FALSE);
508 if (length == (gsize)-1)
509 length = strlen (data);
511 if (key_file->approximate_size > 0)
513 g_key_file_clear (key_file);
514 g_key_file_init (key_file);
516 key_file->flags = flags;
518 g_key_file_parse_data (key_file, data, length, &key_file_error);
522 g_propagate_error (error, key_file_error);
526 g_key_file_flush_parse_buffer (key_file, &key_file_error);
530 g_propagate_error (error, key_file_error);
538 * g_key_file_load_from_data_dirs:
539 * @key_file: an empty #GKeyFile struct
540 * @file: a relative path to a filename to open and parse
541 * @full_path: return location for a string containing the full path
542 * of the file, or %NULL
543 * @flags: flags from #GKeyFileFlags
544 * @error: return location for a #GError, or %NULL
546 * This function looks for a key file named @file in the paths
547 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
548 * loads the file into @key_file and returns the file's full path in
549 * @full_path. If the file could not be loaded then an %error is
550 * set to either a #GFileError or #GKeyFileError.
552 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
556 g_key_file_load_from_data_dirs (GKeyFile *key_file,
562 GError *key_file_error = NULL;
563 gchar **all_data_dirs, **data_dirs;
564 const gchar * user_data_dir;
565 const gchar * const * system_data_dirs;
571 g_return_val_if_fail (key_file != NULL, FALSE);
572 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
574 user_data_dir = g_get_user_data_dir ();
575 system_data_dirs = g_get_system_data_dirs ();
576 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
579 all_data_dirs[i++] = g_strdup (user_data_dir);
582 while (system_data_dirs[j] != NULL)
583 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
586 data_dirs = all_data_dirs;
587 while (*data_dirs != NULL && !found_file)
589 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
595 g_propagate_error (error, key_file_error);
599 found_file = g_key_file_load_from_fd (key_file, fd, flags,
605 g_propagate_error (error, key_file_error);
606 g_free (output_path);
611 *full_path = output_path;
614 g_strfreev (all_data_dirs);
620 * @key_file: a #GKeyFile
627 g_key_file_free (GKeyFile *key_file)
629 g_return_if_fail (key_file != NULL);
631 g_key_file_clear (key_file);
635 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
636 * true for locales that match those in g_get_language_names().
639 g_key_file_locale_is_interesting (GKeyFile *key_file,
642 const gchar * const * current_locales;
645 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
648 current_locales = g_get_language_names ();
650 for (i = 0; current_locales[i] != NULL; i++)
652 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
660 g_key_file_parse_line (GKeyFile *key_file,
665 GError *parse_error = NULL;
668 g_return_if_fail (key_file != NULL);
669 g_return_if_fail (line != NULL);
671 line_start = (gchar *) line;
672 while (g_ascii_isspace (*line_start))
675 if (g_key_file_line_is_comment (line_start))
676 g_key_file_parse_comment (key_file, line, length, &parse_error);
677 else if (g_key_file_line_is_group (line_start))
678 g_key_file_parse_group (key_file, line_start,
679 length - (line_start - line),
681 else if (g_key_file_line_is_key_value_pair (line_start))
682 g_key_file_parse_key_value_pair (key_file, line_start,
683 length - (line_start - line),
687 g_set_error (error, G_KEY_FILE_ERROR,
688 G_KEY_FILE_ERROR_PARSE,
689 _("Key file contains line '%s' which is not "
690 "a key-value pair, group, or comment"), line);
695 g_propagate_error (error, parse_error);
699 g_key_file_parse_comment (GKeyFile *key_file,
704 GKeyFileKeyValuePair *pair;
706 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
709 g_assert (key_file->current_group != NULL);
711 pair = g_new0 (GKeyFileKeyValuePair, 1);
714 pair->value = g_strndup (line, length);
716 key_file->current_group->key_value_pairs =
717 g_list_prepend (key_file->current_group->key_value_pairs, pair);
721 g_key_file_parse_group (GKeyFile *key_file,
727 const gchar *group_name_start, *group_name_end;
729 /* advance past opening '['
731 group_name_start = line + 1;
732 group_name_end = line + length - 1;
734 while (*group_name_end != ']')
737 group_name = g_strndup (group_name_start,
738 group_name_end - group_name_start);
740 g_key_file_add_group (key_file, group_name);
745 g_key_file_parse_key_value_pair (GKeyFile *key_file,
750 gchar *key, *value, *key_end, *value_start, *locale;
751 gsize key_len, value_len;
753 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
755 g_set_error (error, G_KEY_FILE_ERROR,
756 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
757 _("Key file does not start with a group"));
761 key_end = value_start = strchr (line, '=');
763 g_assert (key_end != NULL);
768 /* Pull the key name from the line (chomping trailing whitespace)
770 while (g_ascii_isspace (*key_end))
773 key_len = key_end - line + 2;
775 g_assert (key_len <= length);
777 key = g_strndup (line, key_len - 1);
779 /* Pull the value from the line (chugging leading whitespace)
781 while (g_ascii_isspace (*value_start))
784 value_len = line + length - value_start + 1;
786 value = g_strndup (value_start, value_len);
788 g_assert (key_file->start_group != NULL);
790 if (key_file->current_group
791 && key_file->current_group->name
792 && strcmp (key_file->start_group->name,
793 key_file->current_group->name) == 0
794 && strcmp (key, "Encoding") == 0)
796 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
798 g_set_error (error, G_KEY_FILE_ERROR,
799 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
800 _("Key file contains unsupported encoding '%s'"), value);
808 /* Is this key a translation? If so, is it one that we care about?
810 locale = key_get_locale (key);
812 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
813 g_key_file_add_key (key_file, key_file->current_group, key, value);
821 key_get_locale (const gchar *key)
825 locale = g_strrstr (key, "[");
827 if (locale && strlen (locale) <= 2)
831 locale = g_strndup (locale + 1, strlen (locale) - 2);
837 g_key_file_parse_data (GKeyFile *key_file,
845 g_return_if_fail (key_file != NULL);
846 g_return_if_fail (data != NULL);
850 for (i = 0; i < length; i++)
854 if (i > 0 && data[i - 1] == '\r')
855 g_string_erase (key_file->parse_buffer,
856 key_file->parse_buffer->len - 1,
859 /* When a newline is encountered flush the parse buffer so that the
860 * line can be parsed. Note that completely blank lines won't show
861 * up in the parse buffer, so they get parsed directly.
863 if (key_file->parse_buffer->len > 0)
864 g_key_file_flush_parse_buffer (key_file, &parse_error);
866 g_key_file_parse_comment (key_file, "", 1, &parse_error);
870 g_propagate_error (error, parse_error);
875 g_string_append_c (key_file->parse_buffer, data[i]);
878 key_file->approximate_size += length;
882 g_key_file_flush_parse_buffer (GKeyFile *key_file,
885 GError *file_error = NULL;
887 g_return_if_fail (key_file != NULL);
891 if (key_file->parse_buffer->len > 0)
893 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
894 key_file->parse_buffer->len,
896 g_string_erase (key_file->parse_buffer, 0, -1);
900 g_propagate_error (error, file_error);
907 * g_key_file_to_data:
908 * @key_file: a #GKeyFile
909 * @length: return location for the length of the
910 * returned string, or %NULL
911 * @error: return location for a #GError, or %NULL
913 * This function outputs @key_file as a string.
915 * Return value: a newly allocated string holding
916 * the contents of the #GKeyFile
921 g_key_file_to_data (GKeyFile *key_file,
925 GString *data_string;
927 GList *group_node, *key_file_node;
929 g_return_val_if_fail (key_file != NULL, NULL);
931 data_string = g_string_sized_new (2 * key_file->approximate_size);
933 for (group_node = g_list_last (key_file->groups);
935 group_node = group_node->prev)
937 GKeyFileGroup *group;
939 group = (GKeyFileGroup *) group_node->data;
941 if (group->comment != NULL)
942 g_string_append_printf (data_string, "%s\n", group->comment->value);
943 if (group->name != NULL)
944 g_string_append_printf (data_string, "[%s]\n", group->name);
946 for (key_file_node = g_list_last (group->key_value_pairs);
947 key_file_node != NULL;
948 key_file_node = key_file_node->prev)
950 GKeyFileKeyValuePair *pair;
952 pair = (GKeyFileKeyValuePair *) key_file_node->data;
954 if (pair->key != NULL)
955 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
957 g_string_append_printf (data_string, "%s\n", pair->value);
962 *length = data_string->len;
964 data = data_string->str;
966 g_string_free (data_string, FALSE);
972 * g_key_file_get_keys:
973 * @key_file: a #GKeyFile
974 * @group_name: a group name
975 * @length: return location for the number of keys returned, or %NULL
976 * @error: return location for a #GError, or %NULL
978 * Returns all keys for the group name @group_name. The array of
979 * returned keys will be %NULL-terminated, so @length may
980 * optionally be %NULL. In the event that the @group_name cannot
981 * be found, %NULL is returned and @error is set to
982 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
984 * Return value: a newly-allocated %NULL-terminated array of
985 * strings. Use g_strfreev() to free it.
990 g_key_file_get_keys (GKeyFile *key_file,
991 const gchar *group_name,
995 GKeyFileGroup *group;
1000 g_return_val_if_fail (key_file != NULL, NULL);
1001 g_return_val_if_fail (group_name != NULL, NULL);
1003 group = g_key_file_lookup_group (key_file, group_name);
1007 g_set_error (error, G_KEY_FILE_ERROR,
1008 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1009 _("Key file does not have group '%s'"),
1010 group_name ? group_name : "(null)");
1014 num_keys = g_list_length (group->key_value_pairs);
1016 keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1018 tmp = group->key_value_pairs;
1019 for (i = 1; i <= num_keys; i++)
1021 GKeyFileKeyValuePair *pair;
1023 pair = (GKeyFileKeyValuePair *) tmp->data;
1024 keys[num_keys - i] = g_strdup (pair->key);
1028 keys[num_keys] = NULL;
1037 * g_key_file_get_start_group:
1038 * @key_file: a #GKeyFile
1040 * Returns the name of the start group of the file.
1042 * Return value: The start group of the key file.
1047 g_key_file_get_start_group (GKeyFile *key_file)
1049 g_return_val_if_fail (key_file != NULL, NULL);
1051 if (key_file->start_group)
1052 return g_strdup (key_file->start_group->name);
1058 * g_key_file_get_groups:
1059 * @key_file: a #GKeyFile
1060 * @length: return location for the number of returned groups, or %NULL
1062 * Returns all groups in the key file loaded with @key_file. The
1063 * array of returned groups will be %NULL-terminated, so @length may
1064 * optionally be %NULL.
1066 * Return value: a newly-allocated %NULL-terminated array of strings.
1067 * Use g_strfreev() to free it.
1071 g_key_file_get_groups (GKeyFile *key_file,
1076 gsize i, num_groups;
1078 g_return_val_if_fail (key_file != NULL, NULL);
1080 num_groups = g_list_length (key_file->groups);
1082 g_assert (num_groups > 0);
1084 /* Only need num_groups instead of num_groups + 1
1085 * because the first group of the file (last in the
1086 * list) is always the comment group at the top,
1089 groups = (gchar **) g_new0 (gchar **, num_groups);
1091 group_node = g_list_last (key_file->groups);
1093 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1096 for (group_node = group_node->prev;
1098 group_node = group_node->prev)
1100 GKeyFileGroup *group;
1102 group = (GKeyFileGroup *) group_node->data;
1104 g_assert (group->name != NULL);
1106 groups[i++] = g_strdup (group->name);
1117 * g_key_file_get_value:
1118 * @key_file: a #GKeyFile
1119 * @group_name: a group name
1121 * @error: return location for a #GError, or %NULL
1123 * Returns the value associated with @key under @group_name.
1125 * In the event the key cannot be found, %NULL is returned and
1126 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1127 * event that the @group_name cannot be found, %NULL is returned
1128 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1130 * Return value: a newly allocated string or %NULL if the specified
1131 * key cannot be found.
1136 g_key_file_get_value (GKeyFile *key_file,
1137 const gchar *group_name,
1141 GKeyFileGroup *group;
1142 GKeyFileKeyValuePair *pair;
1143 gchar *value = NULL;
1145 g_return_val_if_fail (key_file != NULL, NULL);
1146 g_return_val_if_fail (group_name != NULL, NULL);
1147 g_return_val_if_fail (key != NULL, NULL);
1149 group = g_key_file_lookup_group (key_file, group_name);
1153 g_set_error (error, G_KEY_FILE_ERROR,
1154 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1155 _("Key file does not have group '%s'"),
1156 group_name ? group_name : "(null)");
1160 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1163 value = g_strdup (pair->value);
1165 g_set_error (error, G_KEY_FILE_ERROR,
1166 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1167 _("Key file does not have key '%s'"), key);
1173 * g_key_file_set_value:
1174 * @key_file: a #GKeyFile
1175 * @group_name: a group name
1179 * Associates a new value with @key under @group_name. If @key
1180 * cannot be found then it is created. If @group_name cannot be
1181 * found then it is created.
1186 g_key_file_set_value (GKeyFile *key_file,
1187 const gchar *group_name,
1191 GKeyFileGroup *group;
1192 GKeyFileKeyValuePair *pair;
1194 g_return_if_fail (key_file != NULL);
1195 g_return_if_fail (group_name != NULL);
1196 g_return_if_fail (key != NULL);
1197 g_return_if_fail (value != NULL);
1199 group = g_key_file_lookup_group (key_file, group_name);
1203 g_key_file_add_group (key_file, group_name);
1204 group = (GKeyFileGroup *) key_file->groups->data;
1206 g_key_file_add_key (key_file, group, key, value);
1210 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1213 g_key_file_add_key (key_file, group, key, value);
1216 g_free (pair->value);
1217 pair->value = g_strdup (value);
1223 * g_key_file_get_string:
1224 * @key_file: a #GKeyFile
1225 * @group_name: a group name
1227 * @error: return location for a #GError, or %NULL
1229 * Returns the value associated with @key under @group_name.
1231 * In the event the key cannot be found, %NULL is returned and
1232 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1233 * event that the @group_name cannot be found, %NULL is returned
1234 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1236 * Return value: a newly allocated string or %NULL if the specified
1237 * key cannot be found.
1242 g_key_file_get_string (GKeyFile *key_file,
1243 const gchar *group_name,
1247 gchar *value, *string_value;
1248 GError *key_file_error;
1250 g_return_val_if_fail (key_file != NULL, NULL);
1251 g_return_val_if_fail (group_name != NULL, NULL);
1252 g_return_val_if_fail (key != NULL, NULL);
1254 key_file_error = NULL;
1256 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1260 g_propagate_error (error, key_file_error);
1264 if (!g_utf8_validate (value, -1, NULL))
1266 g_set_error (error, G_KEY_FILE_ERROR,
1267 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1268 _("Key file contains key '%s' with value '%s' "
1269 "which is not UTF-8"), key, value);
1274 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1280 if (g_error_matches (key_file_error,
1282 G_KEY_FILE_ERROR_INVALID_VALUE))
1284 g_set_error (error, G_KEY_FILE_ERROR,
1285 G_KEY_FILE_ERROR_INVALID_VALUE,
1286 _("Key file contains key '%s' "
1287 "which has value that cannot be interpreted."),
1289 g_error_free (key_file_error);
1292 g_propagate_error (error, key_file_error);
1295 return string_value;
1299 * g_key_file_set_string:
1300 * @key_file: a #GKeyFile
1301 * @group_name: a group name
1305 * Associates a new string value with @key under @group_name. If
1306 * @key cannot be found then it is created. If @group_name
1307 * cannot be found then it is created.
1312 g_key_file_set_string (GKeyFile *key_file,
1313 const gchar *group_name,
1315 const gchar *string)
1319 g_return_if_fail (key_file != NULL);
1320 g_return_if_fail (group_name != NULL);
1321 g_return_if_fail (key != NULL);
1322 g_return_if_fail (string != NULL);
1324 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1325 g_key_file_set_value (key_file, group_name, key, value);
1330 * g_key_file_get_string_list:
1331 * @key_file: a #GKeyFile
1332 * @group_name: a group name
1334 * @length: return location for the number of returned strings, or %NULL
1335 * @error: return location for a #GError, or %NULL
1337 * Returns the values associated with @key under @group_name.
1339 * In the event the key cannot be found, %NULL is returned and
1340 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1341 * event that the @group_name cannot be found, %NULL is returned
1342 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1344 * Return value: a %NULL-terminated string array or %NULL if the specified
1345 * key cannot be found. The array should be freed with g_strfreev().
1350 g_key_file_get_string_list (GKeyFile *key_file,
1351 const gchar *group_name,
1356 GError *key_file_error = NULL;
1357 gchar *value, *string_value, **values;
1359 GSList *p, *pieces = NULL;
1361 g_return_val_if_fail (key_file != NULL, NULL);
1362 g_return_val_if_fail (group_name != NULL, NULL);
1363 g_return_val_if_fail (key != NULL, NULL);
1365 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1369 g_propagate_error (error, key_file_error);
1373 if (!g_utf8_validate (value, -1, NULL))
1375 g_set_error (error, G_KEY_FILE_ERROR,
1376 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1377 _("Key file contains key '%s' with value '%s' "
1378 "which is not UTF-8"), key, value);
1383 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1385 g_free (string_value);
1389 if (g_error_matches (key_file_error,
1391 G_KEY_FILE_ERROR_INVALID_VALUE))
1393 g_set_error (error, G_KEY_FILE_ERROR,
1394 G_KEY_FILE_ERROR_INVALID_VALUE,
1395 _("Key file contains key '%s' "
1396 "which has value that cannot be interpreted."),
1398 g_error_free (key_file_error);
1401 g_propagate_error (error, key_file_error);
1404 len = g_slist_length (pieces);
1405 values = g_new0 (gchar *, len + 1);
1406 for (p = pieces, i = 0; p; p = p->next)
1407 values[i++] = p->data;
1410 g_slist_free (pieces);
1419 * g_key_file_set_string_list:
1420 * @key_file: a #GKeyFile
1421 * @group_name: a group name
1423 * @list: an array of locale string values
1424 * @length: number of locale string values in @list
1426 * Associates a list of string values for @key under @group_name.
1427 * If @key cannot be found then it is created. If @group_name
1428 * cannot be found then it is created.
1433 g_key_file_set_string_list (GKeyFile *key_file,
1434 const gchar *group_name,
1436 const gchar * const list[],
1439 GString *value_list;
1442 g_return_if_fail (key_file != NULL);
1443 g_return_if_fail (group_name != NULL);
1444 g_return_if_fail (key != NULL);
1445 g_return_if_fail (list != NULL);
1447 value_list = g_string_sized_new (length * 128);
1448 for (i = 0; list[i] != NULL && i < length; i++)
1452 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1453 g_string_append (value_list, value);
1454 g_string_append_c (value_list, key_file->list_separator);
1459 g_key_file_set_value (key_file, group_name, key, value_list->str);
1460 g_string_free (value_list, TRUE);
1464 * g_key_file_set_locale_string:
1465 * @key_file: a #GKeyFile
1466 * @group_name: a group name
1471 * Associates a string value for @key and @locale under
1472 * @group_name. If the translation for @key cannot be found
1473 * then it is created.
1478 g_key_file_set_locale_string (GKeyFile *key_file,
1479 const gchar *group_name,
1481 const gchar *locale,
1482 const gchar *string)
1484 gchar *full_key, *value;
1486 g_return_if_fail (key_file != NULL);
1487 g_return_if_fail (group_name != NULL);
1488 g_return_if_fail (key != NULL);
1489 g_return_if_fail (locale != NULL);
1490 g_return_if_fail (string != NULL);
1492 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1493 full_key = g_strdup_printf ("%s[%s]", key, locale);
1494 g_key_file_set_value (key_file, group_name, full_key, value);
1499 extern GSList *_g_compute_locale_variants (const gchar *locale);
1502 * g_key_file_get_locale_string:
1503 * @key_file: a #GKeyFile
1504 * @group_name: a group name
1506 * @locale: a locale or %NULL
1507 * @error: return location for a #GError, or %NULL
1509 * Returns the value associated with @key under @group_name
1510 * translated in the given @locale if available. If @locale is
1511 * %NULL then the current locale is assumed.
1513 * If @key cannot be found then %NULL is returned and @error is set to
1514 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1515 * with @key cannot be interpreted or no suitable translation can
1516 * be found then the untranslated value is returned.
1518 * Return value: a newly allocated string or %NULL if the specified
1519 * key cannot be found.
1524 g_key_file_get_locale_string (GKeyFile *key_file,
1525 const gchar *group_name,
1527 const gchar *locale,
1530 gchar *candidate_key, *translated_value;
1531 GError *key_file_error;
1533 gboolean free_languages = FALSE;
1536 g_return_val_if_fail (key_file != NULL, NULL);
1537 g_return_val_if_fail (group_name != NULL, NULL);
1538 g_return_val_if_fail (key != NULL, NULL);
1540 candidate_key = NULL;
1541 translated_value = NULL;
1542 key_file_error = NULL;
1548 list = _g_compute_locale_variants (locale);
1550 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1551 for (l = list, i = 0; l; l = l->next, i++)
1552 languages[i] = l->data;
1553 languages[i] = NULL;
1555 g_slist_free (list);
1556 free_languages = TRUE;
1560 languages = (gchar **) g_get_language_names ();
1561 free_languages = FALSE;
1564 for (i = 0; languages[i]; i++)
1566 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1568 translated_value = g_key_file_get_string (key_file,
1570 candidate_key, NULL);
1571 g_free (candidate_key);
1573 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1576 g_free (translated_value);
1577 translated_value = NULL;
1580 /* Fallback to untranslated key
1582 if (!translated_value)
1584 translated_value = g_key_file_get_string (key_file, group_name, key,
1587 if (!translated_value)
1588 g_propagate_error (error, key_file_error);
1592 g_strfreev (languages);
1594 return translated_value;
1598 * g_key_file_get_locale_string_list:
1599 * @key_file: a #GKeyFile
1600 * @group_name: a group name
1603 * @length: return location for the number of returned strings or %NULL
1604 * @error: return location for a #GError or %NULL
1606 * Returns the values associated with @key under @group_name
1607 * translated in the given @locale if available. If @locale is
1608 * %NULL then the current locale is assumed. If @group_name is
1609 * %NULL, then the start group is used.
1611 * If @key cannot be found then %NULL is returned and @error is set to
1612 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1613 * with @key cannot be interpreted or no suitable translations
1614 * can be found then the untranslated values are returned.
1615 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1617 * Return value: a newly allocated %NULL-terminated string array
1618 * or %NULL if the key isn't found. The string array should be freed
1619 * with g_strfreev().
1624 g_key_file_get_locale_string_list (GKeyFile *key_file,
1625 const gchar *group_name,
1627 const gchar *locale,
1631 GError *key_file_error;
1632 gchar **values, *value;
1634 g_return_val_if_fail (key_file != NULL, NULL);
1635 g_return_val_if_fail (group_name != NULL, NULL);
1636 g_return_val_if_fail (key != NULL, NULL);
1638 key_file_error = NULL;
1640 value = g_key_file_get_locale_string (key_file, group_name,
1645 g_propagate_error (error, key_file_error);
1650 if (value[strlen (value) - 1] == ';')
1651 value[strlen (value) - 1] = '\0';
1653 values = g_strsplit (value, ";", 0);
1658 *length = g_strv_length (values);
1664 * g_key_file_set_locale_string_list:
1665 * @key_file: a #GKeyFile
1666 * @group_name: a group name
1669 * @list: a %NULL-terminated array of locale string values
1670 * @length: the length of @list
1672 * Associates a list of string values for @key and @locale under
1673 * @group_name. If the translation for @key cannot be found then
1674 * it is created. If @group_name is %NULL, the start group is
1680 g_key_file_set_locale_string_list (GKeyFile *key_file,
1681 const gchar *group_name,
1683 const gchar *locale,
1684 const gchar * const list[],
1687 GString *value_list;
1691 g_return_if_fail (key_file != NULL);
1692 g_return_if_fail (group_name != NULL);
1693 g_return_if_fail (key != NULL);
1694 g_return_if_fail (locale != NULL);
1695 g_return_if_fail (length != 0);
1697 value_list = g_string_sized_new (length * 128);
1698 for (i = 0; list[i] != NULL && i < length; i++)
1702 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1704 g_string_append (value_list, value);
1705 g_string_append_c (value_list, ';');
1710 full_key = g_strdup_printf ("%s[%s]", key, locale);
1711 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1713 g_string_free (value_list, TRUE);
1717 * g_key_file_get_boolean:
1718 * @key_file: a #GKeyFile
1719 * @group_name: a group name
1721 * @error: return location for a #GError
1723 * Returns the value associated with @key under @group_name as a
1724 * boolean. If @group_name is %NULL, the start group is used.
1726 * If @key cannot be found then the return value is undefined and
1727 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1728 * the value associated with @key cannot be interpreted as a boolean
1729 * then the return value is also undefined and @error is set to
1730 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1732 * Return value: the value associated with the key as a boolean
1736 g_key_file_get_boolean (GKeyFile *key_file,
1737 const gchar *group_name,
1741 GError *key_file_error = NULL;
1743 gboolean bool_value;
1745 g_return_val_if_fail (key_file != NULL, FALSE);
1746 g_return_val_if_fail (group_name != NULL, FALSE);
1747 g_return_val_if_fail (key != NULL, FALSE);
1749 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1753 g_propagate_error (error, key_file_error);
1757 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1763 if (g_error_matches (key_file_error,
1765 G_KEY_FILE_ERROR_INVALID_VALUE))
1767 g_set_error (error, G_KEY_FILE_ERROR,
1768 G_KEY_FILE_ERROR_INVALID_VALUE,
1769 _("Key file contains key '%s' "
1770 "which has value that cannot be interpreted."),
1772 g_error_free (key_file_error);
1775 g_propagate_error (error, key_file_error);
1782 * g_key_file_set_boolean:
1783 * @key_file: a #GKeyFile
1784 * @group_name: a group name
1786 * @value: %TRUE or %FALSE
1788 * Associates a new boolean value with @key under @group_name.
1789 * If @key cannot be found then it is created. If @group_name
1790 * is %NULL, the start group is used.
1795 g_key_file_set_boolean (GKeyFile *key_file,
1796 const gchar *group_name,
1802 g_return_if_fail (key_file != NULL);
1803 g_return_if_fail (group_name != NULL);
1804 g_return_if_fail (key != NULL);
1806 result = g_key_file_parse_boolean_as_value (key_file, value);
1807 g_key_file_set_value (key_file, group_name, key, result);
1812 * g_key_file_get_boolean_list:
1813 * @key_file: a #GKeyFile
1814 * @group_name: a group name
1816 * @length: the number of booleans returned
1817 * @error: return location for a #GError
1819 * Returns the values associated with @key under @group_name as
1820 * booleans. If @group_name is %NULL, the start_group is used.
1822 * If @key cannot be found then the return value is undefined and
1823 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1824 * the values associated with @key cannot be interpreted as booleans
1825 * then the return value is also undefined and @error is set to
1826 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1828 * Return value: the values associated with the key as a boolean
1833 g_key_file_get_boolean_list (GKeyFile *key_file,
1834 const gchar *group_name,
1839 GError *key_file_error;
1841 gboolean *bool_values;
1844 g_return_val_if_fail (key_file != NULL, NULL);
1845 g_return_val_if_fail (group_name != NULL, NULL);
1846 g_return_val_if_fail (key != NULL, NULL);
1848 key_file_error = NULL;
1850 values = g_key_file_get_string_list (key_file, group_name, key,
1851 &num_bools, &key_file_error);
1854 g_propagate_error (error, key_file_error);
1859 bool_values = g_new0 (gboolean, num_bools);
1861 for (i = 0; i < num_bools; i++)
1863 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1869 g_propagate_error (error, key_file_error);
1870 g_strfreev (values);
1871 g_free (bool_values);
1876 g_strfreev (values);
1879 *length = num_bools;
1885 * g_key_file_set_boolean_list:
1886 * @key_file: a #GKeyFile
1887 * @group_name: a group name
1889 * @list: an array of boolean values
1890 * @length: length of @list
1892 * Associates a list of boolean values with @key under
1893 * @group_name. If @key cannot be found then it is created.
1894 * If @group_name is %NULL, the start_group is used.
1899 g_key_file_set_boolean_list (GKeyFile *key_file,
1900 const gchar *group_name,
1905 GString *value_list;
1908 g_return_if_fail (key_file != NULL);
1909 g_return_if_fail (group_name != NULL);
1910 g_return_if_fail (key != NULL);
1911 g_return_if_fail (list != NULL);
1913 value_list = g_string_sized_new (length * 8);
1914 for (i = 0; i < length; i++)
1918 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1920 g_string_append (value_list, value);
1921 g_string_append_c (value_list, key_file->list_separator);
1926 g_key_file_set_value (key_file, group_name, key, value_list->str);
1927 g_string_free (value_list, TRUE);
1931 * g_key_file_get_integer:
1932 * @key_file: a #GKeyFile
1933 * @group_name: a group name
1935 * @error: return location for a #GError
1937 * Returns the value associated with @key under @group_name as an
1938 * integer. If @group_name is %NULL, the start_group is used.
1940 * If @key cannot be found then the return value is undefined and
1941 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1942 * the value associated with @key cannot be interpreted as an integer
1943 * then the return value is also undefined and @error is set to
1944 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1946 * Return value: the value associated with the key as an integer.
1951 g_key_file_get_integer (GKeyFile *key_file,
1952 const gchar *group_name,
1956 GError *key_file_error;
1960 g_return_val_if_fail (key_file != NULL, -1);
1961 g_return_val_if_fail (group_name != NULL, -1);
1962 g_return_val_if_fail (key != NULL, -1);
1964 key_file_error = NULL;
1966 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1970 g_propagate_error (error, key_file_error);
1974 int_value = g_key_file_parse_value_as_integer (key_file, value,
1980 if (g_error_matches (key_file_error,
1982 G_KEY_FILE_ERROR_INVALID_VALUE))
1984 g_set_error (error, G_KEY_FILE_ERROR,
1985 G_KEY_FILE_ERROR_INVALID_VALUE,
1986 _("Key file contains key '%s' in group '%s' "
1987 "which has value that cannot be interpreted."), key,
1989 g_error_free (key_file_error);
1992 g_propagate_error (error, key_file_error);
1999 * g_key_file_set_integer:
2000 * @key_file: a #GKeyFile
2001 * @group_name: a group name
2003 * @value: an integer value
2005 * Associates a new integer value with @key under @group_name.
2006 * If @key cannot be found then it is created. If @group_name
2007 * is %NULL, the start group is used.
2012 g_key_file_set_integer (GKeyFile *key_file,
2013 const gchar *group_name,
2019 g_return_if_fail (key_file != NULL);
2020 g_return_if_fail (group_name != NULL);
2021 g_return_if_fail (key != NULL);
2023 result = g_key_file_parse_integer_as_value (key_file, value);
2024 g_key_file_set_value (key_file, group_name, key, result);
2029 * g_key_file_get_integer_list:
2030 * @key_file: a #GKeyFile
2031 * @group_name: a group name
2033 * @length: the number of integers returned
2034 * @error: return location for a #GError
2036 * Returns the values associated with @key under @group_name as
2037 * integers. If @group_name is %NULL, the start group is used.
2039 * If @key cannot be found then the return value is undefined and
2040 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2041 * the values associated with @key cannot be interpreted as integers
2042 * then the return value is also undefined and @error is set to
2043 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2045 * Return value: the values associated with the key as a integer
2050 g_key_file_get_integer_list (GKeyFile *key_file,
2051 const gchar *group_name,
2056 GError *key_file_error = NULL;
2061 g_return_val_if_fail (key_file != NULL, NULL);
2062 g_return_val_if_fail (group_name != NULL, NULL);
2063 g_return_val_if_fail (key != NULL, NULL);
2065 values = g_key_file_get_string_list (key_file, group_name, key,
2066 &num_ints, &key_file_error);
2069 g_propagate_error (error, key_file_error);
2074 int_values = g_new0 (gint, num_ints);
2076 for (i = 0; i < num_ints; i++)
2078 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2084 g_propagate_error (error, key_file_error);
2085 g_strfreev (values);
2086 g_free (int_values);
2091 g_strfreev (values);
2100 * g_key_file_set_integer_list:
2101 * @key_file: a #GKeyFile
2102 * @group_name: a group name
2104 * @list: an array of integer values
2105 * @length: number of integer values in @list
2107 * Associates a list of integer values with @key under
2108 * @group_name. If @key cannot be found then it is created.
2109 * If @group_name is %NULL the start group is used.
2114 g_key_file_set_integer_list (GKeyFile *key_file,
2115 const gchar *group_name,
2123 g_return_if_fail (key_file != NULL);
2124 g_return_if_fail (group_name != NULL);
2125 g_return_if_fail (key != NULL);
2126 g_return_if_fail (list != NULL);
2128 values = g_string_sized_new (length * 16);
2129 for (i = 0; i < length; i++)
2133 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2135 g_string_append (values, value);
2136 g_string_append_c (values, ';');
2141 g_key_file_set_value (key_file, group_name, key, values->str);
2142 g_string_free (values, TRUE);
2146 g_key_file_set_key_comment (GKeyFile *key_file,
2147 const gchar *group_name,
2149 const gchar *comment,
2152 GKeyFileGroup *group;
2153 GKeyFileKeyValuePair *pair;
2154 GList *key_node, *comment_node, *tmp;
2156 group = g_key_file_lookup_group (key_file, group_name);
2159 g_set_error (error, G_KEY_FILE_ERROR,
2160 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2161 _("Key file does not have group '%s'"),
2162 group_name ? group_name : "(null)");
2167 /* First find the key the comments are supposed to be
2170 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2172 if (key_node == NULL)
2174 g_set_error (error, G_KEY_FILE_ERROR,
2175 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2176 _("Key file does not have key '%s' in group '%s'"),
2181 /* Then find all the comments already associated with the
2184 tmp = key_node->next;
2187 GKeyFileKeyValuePair *pair;
2189 pair = (GKeyFileKeyValuePair *) tmp->data;
2191 if (pair->key != NULL)
2196 g_key_file_remove_key_value_pair_node (key_file, group,
2200 if (comment == NULL)
2203 /* Now we can add our new comment
2205 pair = g_new0 (GKeyFileKeyValuePair, 1);
2208 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2210 key_node = g_list_insert (key_node, pair, 1);
2214 g_key_file_set_group_comment (GKeyFile *key_file,
2215 const gchar *group_name,
2216 const gchar *comment,
2219 GKeyFileGroup *group;
2221 group = g_key_file_lookup_group (key_file, group_name);
2224 g_set_error (error, G_KEY_FILE_ERROR,
2225 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2226 _("Key file does not have group '%s'"),
2227 group_name ? group_name : "(null)");
2232 /* First remove any existing comment
2236 g_key_file_key_value_pair_free (group->comment);
2237 group->comment = NULL;
2240 if (comment == NULL)
2243 /* Now we can add our new comment
2245 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2247 group->comment->key = NULL;
2248 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2252 g_key_file_set_top_comment (GKeyFile *key_file,
2253 const gchar *comment,
2257 GKeyFileGroup *group;
2258 GKeyFileKeyValuePair *pair;
2260 /* The last group in the list should be the top (comments only)
2263 g_assert (key_file->groups != NULL);
2264 group_node = g_list_last (key_file->groups);
2265 group = (GKeyFileGroup *) group_node->data;
2266 g_assert (group->name == NULL);
2268 /* Note all keys must be comments at the top of
2269 * the file, so we can just free it all.
2271 if (group->key_value_pairs != NULL)
2273 g_list_foreach (group->key_value_pairs,
2274 (GFunc) g_key_file_key_value_pair_free,
2276 g_list_free (group->key_value_pairs);
2277 group->key_value_pairs = NULL;
2280 if (comment == NULL)
2283 pair = g_new0 (GKeyFileKeyValuePair, 1);
2286 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2288 group->key_value_pairs =
2289 g_list_prepend (group->key_value_pairs, pair);
2293 * g_key_file_set_comment:
2294 * @key_file: a #GKeyFile
2295 * @group_name: a group name, or %NULL
2297 * @comment: a comment
2298 * @error: return location for a #GError
2300 * Places a comment above @key from @group_name.
2301 * @group_name. If @key is %NULL then @comment will
2302 * be written above @group_name. If both @key
2303 * and @group_name are NULL, then @comment will
2304 * be written above the first group in the file.
2309 g_key_file_set_comment (GKeyFile *key_file,
2310 const gchar *group_name,
2312 const gchar *comment,
2315 g_return_if_fail (key_file != NULL);
2317 if (group_name != NULL && key != NULL)
2318 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2319 else if (group_name != NULL)
2320 g_key_file_set_group_comment (key_file, group_name, comment, error);
2322 g_key_file_set_top_comment (key_file, comment, error);
2324 if (comment != NULL)
2325 key_file->approximate_size += strlen (comment);
2329 g_key_file_get_key_comment (GKeyFile *key_file,
2330 const gchar *group_name,
2334 GKeyFileGroup *group;
2335 GList *key_node, *tmp;
2339 group = g_key_file_lookup_group (key_file, group_name);
2342 g_set_error (error, G_KEY_FILE_ERROR,
2343 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2344 _("Key file does not have group '%s'"),
2345 group_name ? group_name : "(null)");
2350 /* First find the key the comments are supposed to be
2353 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2355 if (key_node == NULL)
2357 g_set_error (error, G_KEY_FILE_ERROR,
2358 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2359 _("Key file does not have key '%s' in group '%s'"),
2366 /* Then find all the comments already associated with the
2367 * key and concatentate them.
2369 tmp = key_node->next;
2372 GKeyFileKeyValuePair *pair;
2374 pair = (GKeyFileKeyValuePair *) tmp->data;
2376 if (pair->key != NULL)
2380 string = g_string_sized_new (512);
2382 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2383 g_string_append (string, comment);
2391 comment = string->str;
2392 g_string_free (string, FALSE);
2401 g_key_file_get_group_comment (GKeyFile *key_file,
2402 const gchar *group_name,
2405 GKeyFileGroup *group;
2407 group = g_key_file_lookup_group (key_file, group_name);
2410 g_set_error (error, G_KEY_FILE_ERROR,
2411 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2412 _("Key file does not have group '%s'"),
2413 group_name ? group_name : "(null)");
2419 return g_strdup (group->comment->value);
2425 g_key_file_get_top_comment (GKeyFile *key_file,
2428 GList *group_node, *tmp;
2429 GKeyFileGroup *group;
2433 /* The last group in the list should be the top (comments only)
2436 g_assert (key_file->groups != NULL);
2437 group_node = g_list_last (key_file->groups);
2438 group = (GKeyFileGroup *) group_node->data;
2439 g_assert (group->name == NULL);
2443 /* Then find all the comments already associated with the
2444 * key and concatentate them.
2446 tmp = group->key_value_pairs;
2449 GKeyFileKeyValuePair *pair;
2451 pair = (GKeyFileKeyValuePair *) tmp->data;
2453 if (pair->key != NULL)
2457 string = g_string_sized_new (512);
2459 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2460 g_string_append (string, comment);
2468 comment = string->str;
2469 g_string_free (string, FALSE);
2478 * g_key_file_get_comment:
2479 * @key_file: a #GKeyFile
2480 * @group_name: a group name, or %NULL
2482 * @error: return location for a #GError
2484 * Retreives a comment above @key from @group_name.
2485 * @group_name. If @key is %NULL then @comment will
2486 * be read from above @group_name. If both @key
2487 * and @group_name are NULL, then @comment will
2488 * be read from above the first group in the file.
2491 * Returns: a comment that should be freed with g_free()
2494 g_key_file_get_comment (GKeyFile *key_file,
2495 const gchar *group_name,
2499 g_return_val_if_fail (key_file != NULL, NULL);
2501 if (group_name != NULL && key != NULL)
2502 return g_key_file_get_key_comment (key_file, group_name, key, error);
2503 else if (group_name != NULL)
2504 return g_key_file_get_group_comment (key_file, group_name, error);
2506 return g_key_file_get_top_comment (key_file, error);
2510 * g_key_file_remove_comment:
2511 * @key_file: a #GKeyFile
2512 * @group_name: a group name, or %NULL
2514 * @error: return location for a #GError
2516 * Removes a comment above @key from @group_name.
2517 * @group_name. If @key is %NULL then @comment will
2518 * be written above @group_name. If both @key
2519 * and @group_name are NULL, then @comment will
2520 * be written above the first group in the file.
2526 g_key_file_remove_comment (GKeyFile *key_file,
2527 const gchar *group_name,
2531 g_return_if_fail (key_file != NULL);
2533 if (group_name != NULL && key != NULL)
2534 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2535 else if (group_name != NULL)
2536 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2538 g_key_file_set_top_comment (key_file, NULL, error);
2542 * g_key_file_has_group:
2543 * @key_file: a #GKeyFile
2544 * @group_name: a group name
2546 * Looks whether the key file has the group @group_name.
2548 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2553 g_key_file_has_group (GKeyFile *key_file,
2554 const gchar *group_name)
2556 g_return_val_if_fail (key_file != NULL, FALSE);
2557 g_return_val_if_fail (group_name != NULL, FALSE);
2559 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2563 * g_key_file_has_key:
2564 * @key_file: a #GKeyFile
2565 * @group_name: a group name
2567 * @error: return location for a #GError
2569 * Looks whether the key file has the key @key in the group
2570 * @group_name. If @group_name is %NULL, the start group is
2573 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2579 g_key_file_has_key (GKeyFile *key_file,
2580 const gchar *group_name,
2584 GKeyFileKeyValuePair *pair;
2585 GKeyFileGroup *group;
2587 g_return_val_if_fail (key_file != NULL, FALSE);
2588 g_return_val_if_fail (group_name != NULL, FALSE);
2589 g_return_val_if_fail (key != NULL, FALSE);
2591 group = g_key_file_lookup_group (key_file, group_name);
2595 g_set_error (error, G_KEY_FILE_ERROR,
2596 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2597 _("Key file does not have group '%s'"),
2598 group_name ? group_name : "(null)");
2603 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2605 return pair != NULL;
2609 g_key_file_add_group (GKeyFile *key_file,
2610 const gchar *group_name)
2612 GKeyFileGroup *group;
2614 g_return_if_fail (key_file != NULL);
2615 g_return_if_fail (group_name != NULL);
2616 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2618 group = g_new0 (GKeyFileGroup, 1);
2619 group->name = g_strdup (group_name);
2620 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2621 key_file->groups = g_list_prepend (key_file->groups, group);
2622 key_file->approximate_size += strlen (group_name) + 3;
2623 key_file->current_group = group;
2625 if (key_file->start_group == NULL)
2626 key_file->start_group = group;
2630 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2635 g_free (pair->value);
2640 /* Be careful not to call this function on a node with data in the
2641 * lookup map without removing it from the lookup map, first.
2643 * Some current cases where this warning is not a concern are
2645 * - the node being removed is a comment node
2646 * - the entire lookup map is getting destroyed soon after
2650 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2651 GKeyFileGroup *group,
2655 GKeyFileKeyValuePair *pair;
2657 pair = (GKeyFileKeyValuePair *) pair_node->data;
2659 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2661 if (pair->key != NULL)
2662 key_file->approximate_size -= strlen (pair->key) + 1;
2664 g_assert (pair->value != NULL);
2665 key_file->approximate_size -= strlen (pair->value);
2667 g_key_file_key_value_pair_free (pair);
2669 g_list_free_1 (pair_node);
2673 g_key_file_remove_group_node (GKeyFile *key_file,
2676 GKeyFileGroup *group;
2679 group = (GKeyFileGroup *) group_node->data;
2681 /* If the current group gets deleted make the current group the last
2684 if (key_file->current_group == group)
2686 /* groups should always contain at least the top comment group,
2687 * unless g_key_file_clear has been called
2689 if (key_file->groups)
2690 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2692 key_file->current_group = NULL;
2695 /* If the start group gets deleted make the start group the first
2698 if (key_file->start_group == group)
2700 tmp = g_list_last (key_file->groups);
2703 if (tmp != group_node &&
2704 ((GKeyFileGroup *) tmp->data)->name != NULL)
2711 key_file->start_group = (GKeyFileGroup *) tmp->data;
2713 key_file->start_group = NULL;
2716 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2718 if (group->name != NULL)
2719 key_file->approximate_size -= strlen (group->name) + 3;
2721 tmp = group->key_value_pairs;
2728 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2731 g_assert (group->key_value_pairs == NULL);
2733 if (group->lookup_map)
2735 g_hash_table_destroy (group->lookup_map);
2736 group->lookup_map = NULL;
2739 g_free ((gchar *) group->name);
2741 g_list_free_1 (group_node);
2745 * g_key_file_remove_group:
2746 * @key_file: a #GKeyFile
2747 * @group_name: a group name
2748 * @error: return location for a #GError or %NULL
2750 * Removes the specified group, @group_name,
2751 * from the key file.
2756 g_key_file_remove_group (GKeyFile *key_file,
2757 const gchar *group_name,
2762 g_return_if_fail (key_file != NULL);
2763 g_return_if_fail (group_name != NULL);
2765 group_node = g_key_file_lookup_group_node (key_file, group_name);
2769 g_set_error (error, G_KEY_FILE_ERROR,
2770 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2771 _("Key file does not have group '%s'"),
2776 g_key_file_remove_group_node (key_file, group_node);
2780 g_key_file_add_key (GKeyFile *key_file,
2781 GKeyFileGroup *group,
2785 GKeyFileKeyValuePair *pair;
2787 pair = g_new0 (GKeyFileKeyValuePair, 1);
2789 pair->key = g_strdup (key);
2790 pair->value = g_strdup (value);
2792 g_hash_table_replace (group->lookup_map, pair->key, pair);
2793 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2794 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2798 * g_key_file_remove_key:
2799 * @key_file: a #GKeyFile
2800 * @group_name: a group name
2801 * @key: a key name to remove
2802 * @error: return location for a #GError or %NULL
2804 * Removes @key in @group_name from the key file.
2809 g_key_file_remove_key (GKeyFile *key_file,
2810 const gchar *group_name,
2814 GKeyFileGroup *group;
2815 GKeyFileKeyValuePair *pair;
2817 g_return_if_fail (key_file != NULL);
2818 g_return_if_fail (group_name != NULL);
2819 g_return_if_fail (key != NULL);
2823 group = g_key_file_lookup_group (key_file, group_name);
2826 g_set_error (error, G_KEY_FILE_ERROR,
2827 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2828 _("Key file does not have group '%s'"),
2829 group_name ? group_name : "(null)");
2833 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2837 g_set_error (error, G_KEY_FILE_ERROR,
2838 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2839 _("Key file does not have key '%s' in group '%s'"),
2844 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2846 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
2847 g_hash_table_remove (group->lookup_map, pair->key);
2848 g_key_file_key_value_pair_free (pair);
2852 g_key_file_lookup_group_node (GKeyFile *key_file,
2853 const gchar *group_name)
2855 GKeyFileGroup *group;
2859 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2861 group = (GKeyFileGroup *) tmp->data;
2863 if (group && group->name && strcmp (group->name, group_name) == 0)
2872 static GKeyFileGroup *
2873 g_key_file_lookup_group (GKeyFile *key_file,
2874 const gchar *group_name)
2878 group_node = g_key_file_lookup_group_node (key_file, group_name);
2880 if (group_node != NULL)
2881 return (GKeyFileGroup *) group_node->data;
2887 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2888 GKeyFileGroup *group,
2893 for (key_node = group->key_value_pairs;
2895 key_node = key_node->next)
2897 GKeyFileKeyValuePair *pair;
2899 pair = (GKeyFileKeyValuePair *) key_node->data;
2901 if (pair->key && strcmp (pair->key, key) == 0)
2908 static GKeyFileKeyValuePair *
2909 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2910 GKeyFileGroup *group,
2913 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2916 /* Lines starting with # or consisting entirely of whitespace are merely
2917 * recorded, not parsed. This function assumes all leading whitespace
2918 * has been stripped.
2921 g_key_file_line_is_comment (const gchar *line)
2923 return (*line == '#' || *line == '\0' || *line == '\n');
2926 /* A group in a key file is made up of a starting '[' followed by one
2927 * or more letters making up the group name followed by ']'.
2930 g_key_file_line_is_group (const gchar *line)
2938 p = g_utf8_next_char (p);
2943 p = g_utf8_next_char (p);
2945 /* Group name must be non-empty
2950 while (*p && *p != ']')
2951 p = g_utf8_next_char (p);
2960 g_key_file_line_is_key_value_pair (const gchar *line)
2964 p = (gchar *) g_utf8_strchr (line, -1, '=');
2969 /* Key must be non-empty
2978 g_key_file_parse_value_as_string (GKeyFile *key_file,
2983 gchar *string_value, *p, *q0, *q;
2985 string_value = g_new0 (gchar, strlen (value) + 1);
2987 p = (gchar *) value;
2988 q0 = q = string_value;
3018 g_set_error (error, G_KEY_FILE_ERROR,
3019 G_KEY_FILE_ERROR_INVALID_VALUE,
3020 _("Key file contains escape character "
3025 if (pieces && *p == key_file->list_separator)
3026 *q = key_file->list_separator;
3040 g_set_error (error, G_KEY_FILE_ERROR,
3041 G_KEY_FILE_ERROR_INVALID_VALUE,
3042 _("Key file contains invalid escape "
3043 "sequence '%s'"), sequence);
3052 if (pieces && (*p == key_file->list_separator))
3054 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3070 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3071 *pieces = g_slist_reverse (*pieces);
3074 return string_value;
3078 g_key_file_parse_string_as_value (GKeyFile *key_file,
3079 const gchar *string,
3080 gboolean escape_separator)
3082 gchar *value, *p, *q;
3084 gboolean parsing_leading_space;
3086 length = strlen (string) + 1;
3088 /* Worst case would be that every character needs to be escaped.
3089 * In other words every character turns to two characters
3091 value = g_new0 (gchar, 2 * length);
3093 p = (gchar *) string;
3095 parsing_leading_space = TRUE;
3096 while (p < (string + length - 1))
3098 gchar escaped_character[3] = { '\\', 0, 0 };
3103 if (parsing_leading_space)
3105 escaped_character[1] = 's';
3106 strcpy (q, escaped_character);
3116 if (parsing_leading_space)
3118 escaped_character[1] = 't';
3119 strcpy (q, escaped_character);
3129 escaped_character[1] = 'n';
3130 strcpy (q, escaped_character);
3134 escaped_character[1] = 'r';
3135 strcpy (q, escaped_character);
3139 escaped_character[1] = '\\';
3140 strcpy (q, escaped_character);
3142 parsing_leading_space = FALSE;
3145 if (escape_separator && *p == key_file->list_separator)
3147 escaped_character[1] = key_file->list_separator;
3148 strcpy (q, escaped_character);
3150 parsing_leading_space = TRUE;
3156 parsing_leading_space = FALSE;
3168 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3172 gchar *end_of_valid_int;
3175 int_value = strtol (value, &end_of_valid_int, 10);
3177 if (*end_of_valid_int != '\0')
3178 g_set_error (error, G_KEY_FILE_ERROR,
3179 G_KEY_FILE_ERROR_INVALID_VALUE,
3180 _("Value '%s' cannot be interpreted as a number."), value);
3186 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3190 return g_strdup_printf ("%d", value);
3194 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3200 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3202 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3206 g_set_error (error, G_KEY_FILE_ERROR,
3207 G_KEY_FILE_ERROR_INVALID_VALUE,
3208 _("Value '%s' cannot be interpreted as a boolean."), value);
3214 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3218 return g_strdup ("true");
3220 return g_strdup ("false");
3224 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3228 gchar **lines, *comment;
3231 string = g_string_sized_new (512);
3233 lines = g_strsplit (value, "\n", 0);
3235 for (i = 0; lines[i] != NULL; i++)
3237 if (lines[i][0] != '#')
3238 g_string_append_printf (string, "%s\n", lines[i]);
3240 g_string_append_printf (string, "%s\n", lines[i] + 1);
3244 comment = string->str;
3246 g_string_free (string, FALSE);
3252 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3253 const gchar *comment)
3256 gchar **lines, *value;
3259 string = g_string_sized_new (512);
3261 lines = g_strsplit (comment, "\n", 0);
3263 for (i = 0; lines[i] != NULL; i++)
3264 g_string_append_printf (string, "#%s%s", lines[i],
3265 lines[i + 1] == NULL? "" : "\n");
3268 value = string->str;
3270 g_string_free (string, FALSE);