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 string or %NULL if the specified key cannot be
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 string or %NULL if the specified key cannot be
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 string or %NULL if the specified key cannot be
1523 g_key_file_get_locale_string (GKeyFile *key_file,
1524 const gchar *group_name,
1526 const gchar *locale,
1529 gchar *candidate_key, *translated_value;
1530 GError *key_file_error;
1532 gboolean free_languages = FALSE;
1535 g_return_val_if_fail (key_file != NULL, NULL);
1536 g_return_val_if_fail (group_name != NULL, NULL);
1537 g_return_val_if_fail (key != NULL, NULL);
1539 candidate_key = NULL;
1540 translated_value = NULL;
1541 key_file_error = NULL;
1547 list = _g_compute_locale_variants (locale);
1549 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1550 for (l = list, i = 0; l; l = l->next, i++)
1551 languages[i] = l->data;
1552 languages[i] = NULL;
1554 g_slist_free (list);
1555 free_languages = TRUE;
1559 languages = (gchar **) g_get_language_names ();
1560 free_languages = FALSE;
1563 for (i = 0; languages[i]; i++)
1565 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1567 translated_value = g_key_file_get_string (key_file,
1569 candidate_key, NULL);
1570 g_free (candidate_key);
1572 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1575 g_free (translated_value);
1576 translated_value = NULL;
1579 /* Fallback to untranslated key
1581 if (!translated_value)
1583 translated_value = g_key_file_get_string (key_file, group_name, key,
1586 if (!translated_value)
1587 g_propagate_error (error, key_file_error);
1591 g_strfreev (languages);
1593 return translated_value;
1597 * g_key_file_get_locale_string_list:
1598 * @key_file: a #GKeyFile
1599 * @group_name: a group name
1602 * @length: return location for the number of returned strings or %NULL
1603 * @error: return location for a #GError or %NULL
1605 * Returns the values associated with @key under @group_name
1606 * translated in the given @locale if available. If @locale is
1607 * %NULL then the current locale is assumed. If @group_name is
1608 * %NULL, then the start group is used.
1610 * If @key cannot be found then %NULL is returned and @error is set to
1611 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1612 * with @key cannot be interpreted or no suitable translations
1613 * can be found then the untranslated values are returned.
1614 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1616 * Return value: a newly allocated %NULL-terminated string array
1617 * or %NULL if the key isn't found. The string array should be freed
1618 * with g_strfreev().
1623 g_key_file_get_locale_string_list (GKeyFile *key_file,
1624 const gchar *group_name,
1626 const gchar *locale,
1630 GError *key_file_error;
1631 gchar **values, *value;
1633 g_return_val_if_fail (key_file != NULL, NULL);
1634 g_return_val_if_fail (group_name != NULL, NULL);
1635 g_return_val_if_fail (key != NULL, NULL);
1637 key_file_error = NULL;
1639 value = g_key_file_get_locale_string (key_file, group_name,
1644 g_propagate_error (error, key_file_error);
1649 if (value[strlen (value) - 1] == ';')
1650 value[strlen (value) - 1] = '\0';
1652 values = g_strsplit (value, ";", 0);
1657 *length = g_strv_length (values);
1663 * g_key_file_set_locale_string_list:
1664 * @key_file: a #GKeyFile
1665 * @group_name: a group name
1668 * @list: a %NULL-terminated array of locale string values
1669 * @length: the length of @list
1671 * Associates a list of string values for @key and @locale under
1672 * @group_name. If the translation for @key cannot be found then
1673 * it is created. If @group_name is %NULL, the start group is
1679 g_key_file_set_locale_string_list (GKeyFile *key_file,
1680 const gchar *group_name,
1682 const gchar *locale,
1683 const gchar * const list[],
1686 GString *value_list;
1690 g_return_if_fail (key_file != NULL);
1691 g_return_if_fail (group_name != NULL);
1692 g_return_if_fail (key != NULL);
1693 g_return_if_fail (locale != NULL);
1694 g_return_if_fail (length != 0);
1696 value_list = g_string_sized_new (length * 128);
1697 for (i = 0; list[i] != NULL && i < length; i++)
1701 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1703 g_string_append (value_list, value);
1704 g_string_append_c (value_list, ';');
1709 full_key = g_strdup_printf ("%s[%s]", key, locale);
1710 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1712 g_string_free (value_list, TRUE);
1716 * g_key_file_get_boolean:
1717 * @key_file: a #GKeyFile
1718 * @group_name: a group name
1720 * @error: return location for a #GError
1722 * Returns the value associated with @key under @group_name as a
1723 * boolean. If @group_name is %NULL, the start group is used.
1725 * If @key cannot be found then the return value is undefined and
1726 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1727 * the value associated with @key cannot be interpreted as a boolean
1728 * then the return value is also undefined and @error is set to
1729 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1731 * Return value: the value associated with the key as a boolean
1735 g_key_file_get_boolean (GKeyFile *key_file,
1736 const gchar *group_name,
1740 GError *key_file_error = NULL;
1742 gboolean bool_value;
1744 g_return_val_if_fail (key_file != NULL, FALSE);
1745 g_return_val_if_fail (group_name != NULL, FALSE);
1746 g_return_val_if_fail (key != NULL, FALSE);
1748 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1752 g_propagate_error (error, key_file_error);
1756 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1762 if (g_error_matches (key_file_error,
1764 G_KEY_FILE_ERROR_INVALID_VALUE))
1766 g_set_error (error, G_KEY_FILE_ERROR,
1767 G_KEY_FILE_ERROR_INVALID_VALUE,
1768 _("Key file contains key '%s' "
1769 "which has value that cannot be interpreted."),
1771 g_error_free (key_file_error);
1774 g_propagate_error (error, key_file_error);
1781 * g_key_file_set_boolean:
1782 * @key_file: a #GKeyFile
1783 * @group_name: a group name
1785 * @value: %TRUE or %FALSE
1787 * Associates a new boolean value with @key under @group_name.
1788 * If @key cannot be found then it is created. If @group_name
1789 * is %NULL, the start group is used.
1794 g_key_file_set_boolean (GKeyFile *key_file,
1795 const gchar *group_name,
1801 g_return_if_fail (key_file != NULL);
1802 g_return_if_fail (group_name != NULL);
1803 g_return_if_fail (key != NULL);
1805 result = g_key_file_parse_boolean_as_value (key_file, value);
1806 g_key_file_set_value (key_file, group_name, key, result);
1811 * g_key_file_get_boolean_list:
1812 * @key_file: a #GKeyFile
1813 * @group_name: a group name
1815 * @length: the number of booleans returned
1816 * @error: return location for a #GError
1818 * Returns the values associated with @key under @group_name as
1819 * booleans. If @group_name is %NULL, the start_group is used.
1821 * If @key cannot be found then the return value is undefined and
1822 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1823 * the values associated with @key cannot be interpreted as booleans
1824 * then the return value is also undefined and @error is set to
1825 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1827 * Return value: the values associated with the key as a boolean
1832 g_key_file_get_boolean_list (GKeyFile *key_file,
1833 const gchar *group_name,
1838 GError *key_file_error;
1840 gboolean *bool_values;
1843 g_return_val_if_fail (key_file != NULL, NULL);
1844 g_return_val_if_fail (group_name != NULL, NULL);
1845 g_return_val_if_fail (key != NULL, NULL);
1847 key_file_error = NULL;
1849 values = g_key_file_get_string_list (key_file, group_name, key,
1850 &num_bools, &key_file_error);
1853 g_propagate_error (error, key_file_error);
1858 bool_values = g_new0 (gboolean, num_bools);
1860 for (i = 0; i < num_bools; i++)
1862 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1868 g_propagate_error (error, key_file_error);
1869 g_strfreev (values);
1870 g_free (bool_values);
1875 g_strfreev (values);
1878 *length = num_bools;
1884 * g_key_file_set_boolean_list:
1885 * @key_file: a #GKeyFile
1886 * @group_name: a group name
1888 * @list: an array of boolean values
1889 * @length: length of @list
1891 * Associates a list of boolean values with @key under
1892 * @group_name. If @key cannot be found then it is created.
1893 * If @group_name is %NULL, the start_group is used.
1898 g_key_file_set_boolean_list (GKeyFile *key_file,
1899 const gchar *group_name,
1904 GString *value_list;
1907 g_return_if_fail (key_file != NULL);
1908 g_return_if_fail (group_name != NULL);
1909 g_return_if_fail (key != NULL);
1910 g_return_if_fail (list != NULL);
1912 value_list = g_string_sized_new (length * 8);
1913 for (i = 0; i < length; i++)
1917 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1919 g_string_append (value_list, value);
1920 g_string_append_c (value_list, key_file->list_separator);
1925 g_key_file_set_value (key_file, group_name, key, value_list->str);
1926 g_string_free (value_list, TRUE);
1930 * g_key_file_get_integer:
1931 * @key_file: a #GKeyFile
1932 * @group_name: a group name
1934 * @error: return location for a #GError
1936 * Returns the value associated with @key under @group_name as an
1937 * integer. If @group_name is %NULL, the start_group is used.
1939 * If @key cannot be found then the return value is undefined and
1940 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1941 * the value associated with @key cannot be interpreted as an integer
1942 * then the return value is also undefined and @error is set to
1943 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1945 * Return value: the value associated with the key as an integer.
1950 g_key_file_get_integer (GKeyFile *key_file,
1951 const gchar *group_name,
1955 GError *key_file_error;
1959 g_return_val_if_fail (key_file != NULL, -1);
1960 g_return_val_if_fail (group_name != NULL, -1);
1961 g_return_val_if_fail (key != NULL, -1);
1963 key_file_error = NULL;
1965 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1969 g_propagate_error (error, key_file_error);
1973 int_value = g_key_file_parse_value_as_integer (key_file, value,
1979 if (g_error_matches (key_file_error,
1981 G_KEY_FILE_ERROR_INVALID_VALUE))
1983 g_set_error (error, G_KEY_FILE_ERROR,
1984 G_KEY_FILE_ERROR_INVALID_VALUE,
1985 _("Key file contains key '%s' in group '%s' "
1986 "which has value that cannot be interpreted."), key,
1988 g_error_free (key_file_error);
1991 g_propagate_error (error, key_file_error);
1998 * g_key_file_set_integer:
1999 * @key_file: a #GKeyFile
2000 * @group_name: a group name
2002 * @value: an integer value
2004 * Associates a new integer value with @key under @group_name.
2005 * If @key cannot be found then it is created. If @group_name
2006 * is %NULL, the start group is used.
2011 g_key_file_set_integer (GKeyFile *key_file,
2012 const gchar *group_name,
2018 g_return_if_fail (key_file != NULL);
2019 g_return_if_fail (group_name != NULL);
2020 g_return_if_fail (key != NULL);
2022 result = g_key_file_parse_integer_as_value (key_file, value);
2023 g_key_file_set_value (key_file, group_name, key, result);
2028 * g_key_file_get_integer_list:
2029 * @key_file: a #GKeyFile
2030 * @group_name: a group name
2032 * @length: the number of integers returned
2033 * @error: return location for a #GError
2035 * Returns the values associated with @key under @group_name as
2036 * integers. If @group_name is %NULL, the start group is used.
2038 * If @key cannot be found then the return value is undefined and
2039 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2040 * the values associated with @key cannot be interpreted as integers
2041 * then the return value is also undefined and @error is set to
2042 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2044 * Return value: the values associated with the key as a integer
2049 g_key_file_get_integer_list (GKeyFile *key_file,
2050 const gchar *group_name,
2055 GError *key_file_error = NULL;
2060 g_return_val_if_fail (key_file != NULL, NULL);
2061 g_return_val_if_fail (group_name != NULL, NULL);
2062 g_return_val_if_fail (key != NULL, NULL);
2064 values = g_key_file_get_string_list (key_file, group_name, key,
2065 &num_ints, &key_file_error);
2068 g_propagate_error (error, key_file_error);
2073 int_values = g_new0 (gint, num_ints);
2075 for (i = 0; i < num_ints; i++)
2077 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2083 g_propagate_error (error, key_file_error);
2084 g_strfreev (values);
2085 g_free (int_values);
2090 g_strfreev (values);
2099 * g_key_file_set_integer_list:
2100 * @key_file: a #GKeyFile
2101 * @group_name: a group name
2103 * @list: an array of integer values
2104 * @length: number of integer values in @list
2106 * Associates a list of integer values with @key under
2107 * @group_name. If @key cannot be found then it is created.
2108 * If @group_name is %NULL the start group is used.
2113 g_key_file_set_integer_list (GKeyFile *key_file,
2114 const gchar *group_name,
2122 g_return_if_fail (key_file != NULL);
2123 g_return_if_fail (group_name != NULL);
2124 g_return_if_fail (key != NULL);
2125 g_return_if_fail (list != NULL);
2127 values = g_string_sized_new (length * 16);
2128 for (i = 0; i < length; i++)
2132 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2134 g_string_append (values, value);
2135 g_string_append_c (values, ';');
2140 g_key_file_set_value (key_file, group_name, key, values->str);
2141 g_string_free (values, TRUE);
2145 g_key_file_set_key_comment (GKeyFile *key_file,
2146 const gchar *group_name,
2148 const gchar *comment,
2151 GKeyFileGroup *group;
2152 GKeyFileKeyValuePair *pair;
2153 GList *key_node, *comment_node, *tmp;
2155 group = g_key_file_lookup_group (key_file, group_name);
2158 g_set_error (error, G_KEY_FILE_ERROR,
2159 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2160 _("Key file does not have group '%s'"),
2161 group_name ? group_name : "(null)");
2166 /* First find the key the comments are supposed to be
2169 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2171 if (key_node == NULL)
2173 g_set_error (error, G_KEY_FILE_ERROR,
2174 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2175 _("Key file does not have key '%s' in group '%s'"),
2180 /* Then find all the comments already associated with the
2183 tmp = key_node->next;
2186 GKeyFileKeyValuePair *pair;
2188 pair = (GKeyFileKeyValuePair *) tmp->data;
2190 if (pair->key != NULL)
2195 g_key_file_remove_key_value_pair_node (key_file, group,
2199 if (comment == NULL)
2202 /* Now we can add our new comment
2204 pair = g_new0 (GKeyFileKeyValuePair, 1);
2207 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2209 key_node = g_list_insert (key_node, pair, 1);
2213 g_key_file_set_group_comment (GKeyFile *key_file,
2214 const gchar *group_name,
2215 const gchar *comment,
2218 GKeyFileGroup *group;
2220 group = g_key_file_lookup_group (key_file, group_name);
2223 g_set_error (error, G_KEY_FILE_ERROR,
2224 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2225 _("Key file does not have group '%s'"),
2226 group_name ? group_name : "(null)");
2231 /* First remove any existing comment
2235 g_key_file_key_value_pair_free (group->comment);
2236 group->comment = NULL;
2239 if (comment == NULL)
2242 /* Now we can add our new comment
2244 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2246 group->comment->key = NULL;
2247 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2251 g_key_file_set_top_comment (GKeyFile *key_file,
2252 const gchar *comment,
2256 GKeyFileGroup *group;
2257 GKeyFileKeyValuePair *pair;
2259 /* The last group in the list should be the top (comments only)
2262 g_assert (key_file->groups != NULL);
2263 group_node = g_list_last (key_file->groups);
2264 group = (GKeyFileGroup *) group_node->data;
2265 g_assert (group->name == NULL);
2267 /* Note all keys must be comments at the top of
2268 * the file, so we can just free it all.
2270 if (group->key_value_pairs != NULL)
2272 g_list_foreach (group->key_value_pairs,
2273 (GFunc) g_key_file_key_value_pair_free,
2275 g_list_free (group->key_value_pairs);
2276 group->key_value_pairs = NULL;
2279 if (comment == NULL)
2282 pair = g_new0 (GKeyFileKeyValuePair, 1);
2285 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2287 group->key_value_pairs =
2288 g_list_prepend (group->key_value_pairs, pair);
2292 * g_key_file_set_comment:
2293 * @key_file: a #GKeyFile
2294 * @group_name: a group name, or %NULL
2296 * @comment: a comment
2297 * @error: return location for a #GError
2299 * Places a comment above @key from @group_name.
2300 * @group_name. If @key is %NULL then @comment will
2301 * be written above @group_name. If both @key
2302 * and @group_name are NULL, then @comment will
2303 * be written above the first group in the file.
2308 g_key_file_set_comment (GKeyFile *key_file,
2309 const gchar *group_name,
2311 const gchar *comment,
2314 g_return_if_fail (key_file != NULL);
2316 if (group_name != NULL && key != NULL)
2317 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2318 else if (group_name != NULL)
2319 g_key_file_set_group_comment (key_file, group_name, comment, error);
2321 g_key_file_set_top_comment (key_file, comment, error);
2323 if (comment != NULL)
2324 key_file->approximate_size += strlen (comment);
2328 g_key_file_get_key_comment (GKeyFile *key_file,
2329 const gchar *group_name,
2333 GKeyFileGroup *group;
2334 GList *key_node, *tmp;
2338 group = g_key_file_lookup_group (key_file, group_name);
2341 g_set_error (error, G_KEY_FILE_ERROR,
2342 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2343 _("Key file does not have group '%s'"),
2344 group_name ? group_name : "(null)");
2349 /* First find the key the comments are supposed to be
2352 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2354 if (key_node == NULL)
2356 g_set_error (error, G_KEY_FILE_ERROR,
2357 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2358 _("Key file does not have key '%s' in group '%s'"),
2365 /* Then find all the comments already associated with the
2366 * key and concatentate them.
2368 tmp = key_node->next;
2371 GKeyFileKeyValuePair *pair;
2373 pair = (GKeyFileKeyValuePair *) tmp->data;
2375 if (pair->key != NULL)
2379 string = g_string_sized_new (512);
2381 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2382 g_string_append (string, comment);
2390 comment = string->str;
2391 g_string_free (string, FALSE);
2400 g_key_file_get_group_comment (GKeyFile *key_file,
2401 const gchar *group_name,
2404 GKeyFileGroup *group;
2406 group = g_key_file_lookup_group (key_file, group_name);
2409 g_set_error (error, G_KEY_FILE_ERROR,
2410 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2411 _("Key file does not have group '%s'"),
2412 group_name ? group_name : "(null)");
2418 return g_strdup (group->comment->value);
2424 g_key_file_get_top_comment (GKeyFile *key_file,
2427 GList *group_node, *tmp;
2428 GKeyFileGroup *group;
2432 /* The last group in the list should be the top (comments only)
2435 g_assert (key_file->groups != NULL);
2436 group_node = g_list_last (key_file->groups);
2437 group = (GKeyFileGroup *) group_node->data;
2438 g_assert (group->name == NULL);
2442 /* Then find all the comments already associated with the
2443 * key and concatentate them.
2445 tmp = group->key_value_pairs;
2448 GKeyFileKeyValuePair *pair;
2450 pair = (GKeyFileKeyValuePair *) tmp->data;
2452 if (pair->key != NULL)
2456 string = g_string_sized_new (512);
2458 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2459 g_string_append (string, comment);
2467 comment = string->str;
2468 g_string_free (string, FALSE);
2477 * g_key_file_get_comment:
2478 * @key_file: a #GKeyFile
2479 * @group_name: a group name, or %NULL
2481 * @error: return location for a #GError
2483 * Retreives a comment above @key from @group_name.
2484 * @group_name. If @key is %NULL then @comment will
2485 * be read from above @group_name. If both @key
2486 * and @group_name are NULL, then @comment will
2487 * be read from above the first group in the file.
2490 * Returns: a comment that should be freed with g_free()
2493 g_key_file_get_comment (GKeyFile *key_file,
2494 const gchar *group_name,
2498 g_return_val_if_fail (key_file != NULL, NULL);
2500 if (group_name != NULL && key != NULL)
2501 return g_key_file_get_key_comment (key_file, group_name, key, error);
2502 else if (group_name != NULL)
2503 return g_key_file_get_group_comment (key_file, group_name, error);
2505 return g_key_file_get_top_comment (key_file, error);
2509 * g_key_file_remove_comment:
2510 * @key_file: a #GKeyFile
2511 * @group_name: a group name, or %NULL
2513 * @error: return location for a #GError
2515 * Removes a comment above @key from @group_name.
2516 * @group_name. If @key is %NULL then @comment will
2517 * be written above @group_name. If both @key
2518 * and @group_name are NULL, then @comment will
2519 * be written above the first group in the file.
2525 g_key_file_remove_comment (GKeyFile *key_file,
2526 const gchar *group_name,
2530 g_return_if_fail (key_file != NULL);
2532 if (group_name != NULL && key != NULL)
2533 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2534 else if (group_name != NULL)
2535 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2537 g_key_file_set_top_comment (key_file, NULL, error);
2541 * g_key_file_has_group:
2542 * @key_file: a #GKeyFile
2543 * @group_name: a group name
2545 * Looks whether the key file has the group @group_name.
2547 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2552 g_key_file_has_group (GKeyFile *key_file,
2553 const gchar *group_name)
2555 g_return_val_if_fail (key_file != NULL, FALSE);
2556 g_return_val_if_fail (group_name != NULL, FALSE);
2558 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2562 * g_key_file_has_key:
2563 * @key_file: a #GKeyFile
2564 * @group_name: a group name
2566 * @error: return location for a #GError
2568 * Looks whether the key file has the key @key in the group
2569 * @group_name. If @group_name is %NULL, the start group is
2572 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2578 g_key_file_has_key (GKeyFile *key_file,
2579 const gchar *group_name,
2583 GKeyFileKeyValuePair *pair;
2584 GKeyFileGroup *group;
2586 g_return_val_if_fail (key_file != NULL, FALSE);
2587 g_return_val_if_fail (group_name != NULL, FALSE);
2588 g_return_val_if_fail (key != NULL, FALSE);
2590 group = g_key_file_lookup_group (key_file, group_name);
2594 g_set_error (error, G_KEY_FILE_ERROR,
2595 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2596 _("Key file does not have group '%s'"),
2597 group_name ? group_name : "(null)");
2602 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2604 return pair != NULL;
2608 g_key_file_add_group (GKeyFile *key_file,
2609 const gchar *group_name)
2611 GKeyFileGroup *group;
2613 g_return_if_fail (key_file != NULL);
2614 g_return_if_fail (group_name != NULL);
2615 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2617 group = g_new0 (GKeyFileGroup, 1);
2618 group->name = g_strdup (group_name);
2619 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2620 key_file->groups = g_list_prepend (key_file->groups, group);
2621 key_file->approximate_size += strlen (group_name) + 3;
2622 key_file->current_group = group;
2624 if (key_file->start_group == NULL)
2625 key_file->start_group = group;
2629 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2634 g_free (pair->value);
2639 /* Be careful not to call this function on a node with data in the
2640 * lookup map without removing it from the lookup map, first.
2642 * Some current cases where this warning is not a concern are
2644 * - the node being removed is a comment node
2645 * - the entire lookup map is getting destroyed soon after
2649 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2650 GKeyFileGroup *group,
2654 GKeyFileKeyValuePair *pair;
2656 pair = (GKeyFileKeyValuePair *) pair_node->data;
2658 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2660 if (pair->key != NULL)
2661 key_file->approximate_size -= strlen (pair->key) + 1;
2663 g_assert (pair->value != NULL);
2664 key_file->approximate_size -= strlen (pair->value);
2666 g_key_file_key_value_pair_free (pair);
2668 g_list_free_1 (pair_node);
2672 g_key_file_remove_group_node (GKeyFile *key_file,
2675 GKeyFileGroup *group;
2678 group = (GKeyFileGroup *) group_node->data;
2680 /* If the current group gets deleted make the current group the last
2683 if (key_file->current_group == group)
2685 /* groups should always contain at least the top comment group,
2686 * unless g_key_file_clear has been called
2688 if (key_file->groups)
2689 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2691 key_file->current_group = NULL;
2694 /* If the start group gets deleted make the start group the first
2697 if (key_file->start_group == group)
2699 tmp = g_list_last (key_file->groups);
2702 if (tmp != group_node &&
2703 ((GKeyFileGroup *) tmp->data)->name != NULL)
2710 key_file->start_group = (GKeyFileGroup *) tmp->data;
2712 key_file->start_group = NULL;
2715 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2717 if (group->name != NULL)
2718 key_file->approximate_size -= strlen (group->name) + 3;
2720 tmp = group->key_value_pairs;
2727 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2730 g_assert (group->key_value_pairs == NULL);
2732 if (group->lookup_map)
2734 g_hash_table_destroy (group->lookup_map);
2735 group->lookup_map = NULL;
2738 g_free ((gchar *) group->name);
2740 g_list_free_1 (group_node);
2744 * g_key_file_remove_group:
2745 * @key_file: a #GKeyFile
2746 * @group_name: a group name
2747 * @error: return location for a #GError or %NULL
2749 * Removes the specified group, @group_name,
2750 * from the key file.
2755 g_key_file_remove_group (GKeyFile *key_file,
2756 const gchar *group_name,
2761 g_return_if_fail (key_file != NULL);
2762 g_return_if_fail (group_name != NULL);
2764 group_node = g_key_file_lookup_group_node (key_file, group_name);
2767 g_set_error (error, G_KEY_FILE_ERROR,
2768 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2769 _("Key file does not have group '%s'"),
2772 g_key_file_remove_group_node (key_file, group_node);
2776 g_key_file_add_key (GKeyFile *key_file,
2777 GKeyFileGroup *group,
2781 GKeyFileKeyValuePair *pair;
2783 pair = g_new0 (GKeyFileKeyValuePair, 1);
2785 pair->key = g_strdup (key);
2786 pair->value = g_strdup (value);
2788 g_hash_table_replace (group->lookup_map, pair->key, pair);
2789 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2790 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2794 * g_key_file_remove_key:
2795 * @key_file: a #GKeyFile
2796 * @group_name: a group name
2797 * @key: a key name to remove
2798 * @error: return location for a #GError or %NULL
2800 * Removes @key in @group_name from the key file.
2805 g_key_file_remove_key (GKeyFile *key_file,
2806 const gchar *group_name,
2810 GKeyFileGroup *group;
2811 GKeyFileKeyValuePair *pair;
2813 g_return_if_fail (key_file != NULL);
2814 g_return_if_fail (group_name != NULL);
2815 g_return_if_fail (key != NULL);
2819 group = g_key_file_lookup_group (key_file, group_name);
2822 g_set_error (error, G_KEY_FILE_ERROR,
2823 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2824 _("Key file does not have group '%s'"),
2825 group_name ? group_name : "(null)");
2829 group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2830 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2834 g_set_error (error, G_KEY_FILE_ERROR,
2835 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2836 _("Key file does not have key '%s' in group '%s'"),
2841 g_hash_table_remove (group->lookup_map, pair->key);
2843 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2844 g_key_file_key_value_pair_free (pair);
2848 g_key_file_lookup_group_node (GKeyFile *key_file,
2849 const gchar *group_name)
2851 GKeyFileGroup *group;
2855 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2857 group = (GKeyFileGroup *) tmp->data;
2859 if (group && group->name && strcmp (group->name, group_name) == 0)
2868 static GKeyFileGroup *
2869 g_key_file_lookup_group (GKeyFile *key_file,
2870 const gchar *group_name)
2874 group_node = g_key_file_lookup_group_node (key_file, group_name);
2876 if (group_node != NULL)
2877 return (GKeyFileGroup *) group_node->data;
2883 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2884 GKeyFileGroup *group,
2889 for (key_node = group->key_value_pairs;
2891 key_node = key_node->next)
2893 GKeyFileKeyValuePair *pair;
2895 pair = (GKeyFileKeyValuePair *) key_node->data;
2897 if (pair->key && strcmp (pair->key, key) == 0)
2904 static GKeyFileKeyValuePair *
2905 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2906 GKeyFileGroup *group,
2909 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2912 /* Lines starting with # or consisting entirely of whitespace are merely
2913 * recorded, not parsed. This function assumes all leading whitespace
2914 * has been stripped.
2917 g_key_file_line_is_comment (const gchar *line)
2919 return (*line == '#' || *line == '\0' || *line == '\n');
2922 /* A group in a key file is made up of a starting '[' followed by one
2923 * or more letters making up the group name followed by ']'.
2926 g_key_file_line_is_group (const gchar *line)
2934 p = g_utf8_next_char (p);
2939 p = g_utf8_next_char (p);
2941 /* Group name must be non-empty
2946 while (*p && *p != ']')
2947 p = g_utf8_next_char (p);
2956 g_key_file_line_is_key_value_pair (const gchar *line)
2960 p = (gchar *) g_utf8_strchr (line, -1, '=');
2965 /* Key must be non-empty
2974 g_key_file_parse_value_as_string (GKeyFile *key_file,
2979 gchar *string_value, *p, *q0, *q;
2981 string_value = g_new0 (gchar, strlen (value) + 1);
2983 p = (gchar *) value;
2984 q0 = q = string_value;
3014 if (pieces && *p == key_file->list_separator)
3015 *q = key_file->list_separator;
3029 g_set_error (error, G_KEY_FILE_ERROR,
3030 G_KEY_FILE_ERROR_INVALID_VALUE,
3031 _("Key file contains invalid escape "
3032 "sequence '%s'"), sequence);
3041 if (pieces && (*p == key_file->list_separator))
3043 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3052 if (p > value && p[-1] == '\\' && q[-1] != '\\' && *error == NULL)
3053 g_set_error (error, G_KEY_FILE_ERROR,
3054 G_KEY_FILE_ERROR_INVALID_VALUE,
3055 _("Key file contains escape character at end of line"));
3061 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3062 *pieces = g_slist_reverse (*pieces);
3065 return string_value;
3069 g_key_file_parse_string_as_value (GKeyFile *key_file,
3070 const gchar *string,
3071 gboolean escape_separator)
3073 gchar *value, *p, *q;
3075 gboolean parsing_leading_space;
3077 length = strlen (string) + 1;
3079 /* Worst case would be that every character needs to be escaped.
3080 * In other words every character turns to two characters
3082 value = g_new0 (gchar, 2 * length);
3084 p = (gchar *) string;
3086 parsing_leading_space = TRUE;
3087 while (p < (string + length - 1))
3089 gchar escaped_character[3] = { '\\', 0, 0 };
3094 if (parsing_leading_space)
3096 escaped_character[1] = 's';
3097 strcpy (q, escaped_character);
3107 if (parsing_leading_space)
3109 escaped_character[1] = 't';
3110 strcpy (q, escaped_character);
3120 escaped_character[1] = 'n';
3121 strcpy (q, escaped_character);
3125 escaped_character[1] = 'r';
3126 strcpy (q, escaped_character);
3130 escaped_character[1] = '\\';
3131 strcpy (q, escaped_character);
3133 parsing_leading_space = FALSE;
3136 if (escape_separator && *p == key_file->list_separator)
3138 escaped_character[1] = key_file->list_separator;
3139 strcpy (q, escaped_character);
3141 parsing_leading_space = TRUE;
3147 parsing_leading_space = FALSE;
3159 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3163 gchar *end_of_valid_int;
3166 int_value = strtol (value, &end_of_valid_int, 0);
3168 if (*end_of_valid_int != '\0')
3169 g_set_error (error, G_KEY_FILE_ERROR,
3170 G_KEY_FILE_ERROR_INVALID_VALUE,
3171 _("Value '%s' cannot be interpreted as a number."), value);
3177 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3181 return g_strdup_printf ("%d", value);
3185 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3191 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3193 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3197 g_set_error (error, G_KEY_FILE_ERROR,
3198 G_KEY_FILE_ERROR_INVALID_VALUE,
3199 _("Value '%s' cannot be interpreted as a boolean."), value);
3205 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3209 return g_strdup ("true");
3211 return g_strdup ("false");
3215 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3219 gchar **lines, *comment;
3222 string = g_string_sized_new (512);
3224 lines = g_strsplit (value, "\n", 0);
3226 for (i = 0; lines[i] != NULL; i++)
3228 if (lines[i][0] != '#')
3229 g_string_append_printf (string, "%s\n", lines[i]);
3231 g_string_append_printf (string, "%s\n", lines[i] + 1);
3235 comment = string->str;
3237 g_string_free (string, FALSE);
3243 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3244 const gchar *comment)
3247 gchar **lines, *value;
3250 string = g_string_sized_new (512);
3252 lines = g_strsplit (comment, "\n", 0);
3254 for (i = 0; lines[i] != NULL; i++)
3255 g_string_append_printf (string, "#%s%s", lines[i],
3256 lines[i + 1] == NULL? "" : "\n");
3259 value = string->str;
3261 g_string_free (string, FALSE);