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>
47 #include "gfileutils.h"
53 #include "gmessages.h"
56 #include "gstrfuncs.h"
59 typedef struct _GKeyFileGroup GKeyFileGroup;
65 GKeyFileGroup *start_group;
66 GKeyFileGroup *current_group;
68 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
70 /* Used for sizing the output buffer during serialization
72 gsize approximate_size;
79 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
83 const gchar *name; /* NULL for above first group (which will be comments) */
85 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
87 GList *key_value_pairs;
89 /* Used in parallel with key_value_pairs for
90 * increased lookup performance
92 GHashTable *lookup_map;
95 struct _GKeyFileKeyValuePair
97 gchar *key; /* NULL for comments */
101 static gint find_file_in_data_dirs (const gchar *file,
105 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
109 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
110 const gchar *group_name);
111 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
112 const gchar *group_name);
114 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
115 GKeyFileGroup *group,
117 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
118 GKeyFileGroup *group,
121 static void g_key_file_remove_group_node (GKeyFile *key_file,
123 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
124 GKeyFileGroup *group,
127 static void g_key_file_add_key (GKeyFile *key_file,
128 GKeyFileGroup *group,
131 static void g_key_file_add_group (GKeyFile *key_file,
132 const gchar *group_name);
133 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
134 static gboolean g_key_file_line_is_comment (const gchar *line);
135 static gboolean g_key_file_line_is_group (const gchar *line);
136 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
137 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
141 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
143 gboolean escape_separator);
144 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
147 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
149 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
152 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
154 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
156 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
157 const gchar *comment);
158 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
162 static void g_key_file_parse_comment (GKeyFile *key_file,
166 static void g_key_file_parse_group (GKeyFile *key_file,
170 static gchar *key_get_locale (const gchar *key);
171 static void g_key_file_parse_data (GKeyFile *key_file,
175 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
180 g_key_file_error_quark (void)
182 static GQuark error_quark = 0;
184 if (error_quark == 0)
185 error_quark = g_quark_from_static_string ("g-key-file-error-quark");
191 g_key_file_init (GKeyFile *key_file)
193 key_file->current_group = g_new0 (GKeyFileGroup, 1);
194 key_file->groups = g_list_prepend (NULL, key_file->current_group);
195 key_file->start_group = NULL;
196 key_file->parse_buffer = g_string_sized_new (128);
197 key_file->approximate_size = 0;
198 key_file->list_separator = ';';
203 g_key_file_clear (GKeyFile *key_file)
205 GList *tmp, *group_node;
207 if (key_file->parse_buffer)
208 g_string_free (key_file->parse_buffer, TRUE);
210 tmp = key_file->groups;
215 g_key_file_remove_group_node (key_file, group_node);
218 g_assert (key_file->groups == NULL);
225 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
226 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
227 * read an existing key file.
229 * Return value: an empty #GKeyFile.
234 g_key_file_new (void)
238 key_file = g_new0 (GKeyFile, 1);
239 g_key_file_init (key_file);
245 * g_key_file_set_list_separator:
246 * @key_file: a #GKeyFile
247 * @separator: the separator
249 * Sets the character which is used to separate
250 * values in lists. Typically ';' or ',' are used
251 * as separators. The default list separator is ';'.
256 g_key_file_set_list_separator (GKeyFile *key_file,
259 key_file->list_separator = separator;
263 /* Iterates through all the directories in *dirs trying to
264 * open file. When it successfully locates and opens a file it
265 * returns the file descriptor to the open file. It also
266 * outputs the absolute path of the file in output_file and
267 * leaves the unchecked directories in *dirs.
270 find_file_in_data_dirs (const gchar *file,
275 gchar **data_dirs, *data_dir, *path;
286 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
288 gchar *candidate_file, *sub_dir;
290 candidate_file = (gchar *) file;
291 sub_dir = g_strdup ("");
292 while (candidate_file != NULL && fd < 0)
296 path = g_build_filename (data_dir, sub_dir,
297 candidate_file, NULL);
299 fd = g_open (path, O_RDONLY, 0);
307 candidate_file = strchr (candidate_file, '-');
309 if (candidate_file == NULL)
315 sub_dir = g_strndup (file, candidate_file - file - 1);
317 for (p = sub_dir; *p != '\0'; p++)
320 *p = G_DIR_SEPARATOR;
331 g_set_error (error, G_KEY_FILE_ERROR,
332 G_KEY_FILE_ERROR_NOT_FOUND,
333 _("Valid key file could not be "
334 "found in data dirs"));
337 if (output_file != NULL && fd > 0)
338 *output_file = g_strdup (path);
344 g_key_file_load_from_fd (GKeyFile *key_file,
349 GError *key_file_error = NULL;
351 struct stat stat_buf;
352 gchar read_buf[4096];
354 if (fstat (fd, &stat_buf) < 0)
356 g_set_error (error, G_FILE_ERROR,
357 g_file_error_from_errno (errno),
358 "%s", g_strerror (errno));
362 if ((stat_buf.st_mode & S_IFMT) != S_IFREG)
364 g_set_error (error, G_KEY_FILE_ERROR,
365 G_KEY_FILE_ERROR_PARSE,
366 _("Not a regular file"));
370 if (stat_buf.st_size == 0)
372 g_set_error (error, G_KEY_FILE_ERROR,
373 G_KEY_FILE_ERROR_PARSE,
378 if (key_file->approximate_size > 0)
380 g_key_file_clear (key_file);
381 g_key_file_init (key_file);
383 key_file->flags = flags;
388 bytes_read = read (fd, read_buf, 4096);
390 if (bytes_read == 0) /* End of File */
395 if (errno == EINTR || errno == EAGAIN)
398 g_set_error (error, G_FILE_ERROR,
399 g_file_error_from_errno (errno),
400 "%s", g_strerror (errno));
404 g_key_file_parse_data (key_file,
405 read_buf, bytes_read,
408 while (!key_file_error);
412 g_propagate_error (error, key_file_error);
416 g_key_file_flush_parse_buffer (key_file, &key_file_error);
420 g_propagate_error (error, key_file_error);
428 * g_key_file_load_from_file:
429 * @key_file: an empty #GKeyFile struct
430 * @file: the path of a filename to load, in the GLib file name encoding
431 * @flags: flags from #GKeyFileFlags
432 * @error: return location for a #GError, or %NULL
434 * Loads a key file into an empty #GKeyFile structure.
435 * If the file could not be loaded then %error is set to
436 * either a #GFileError or #GKeyFileError.
438 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
442 g_key_file_load_from_file (GKeyFile *key_file,
447 GError *key_file_error = NULL;
450 g_return_val_if_fail (key_file != NULL, FALSE);
451 g_return_val_if_fail (file != NULL, FALSE);
453 fd = g_open (file, O_RDONLY, 0);
457 g_set_error (error, G_FILE_ERROR,
458 g_file_error_from_errno (errno),
459 "%s", g_strerror (errno));
463 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
468 g_propagate_error (error, key_file_error);
476 * g_key_file_load_from_data:
477 * @key_file: an empty #GKeyFile struct
478 * @data: key file loaded in memory.
479 * @length: the length of @data in bytes
480 * @flags: flags from #GKeyFileFlags
481 * @error: return location for a #GError, or %NULL
483 * Loads a key file from memory into an empty #GKeyFile structure.
484 * If the object cannot be created then %error is set to a #GKeyFileError.
486 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
490 g_key_file_load_from_data (GKeyFile *key_file,
496 GError *key_file_error = NULL;
498 g_return_val_if_fail (key_file != NULL, FALSE);
499 g_return_val_if_fail (data != NULL, FALSE);
500 g_return_val_if_fail (length != 0, FALSE);
502 if (key_file->approximate_size > 0)
504 g_key_file_clear (key_file);
505 g_key_file_init (key_file);
507 key_file->flags = flags;
509 g_key_file_parse_data (key_file, data, length, &key_file_error);
513 g_propagate_error (error, key_file_error);
517 g_key_file_flush_parse_buffer (key_file, &key_file_error);
521 g_propagate_error (error, key_file_error);
529 * g_key_file_load_from_data_dirs:
530 * @key_file: an empty #GKeyFile struct
531 * @file: a relative path to a filename to open and parse
532 * @full_path: return location for a string containing the full path
533 * of the file, or %NULL
534 * @flags: flags from #GKeyFileFlags
535 * @error: return location for a #GError, or %NULL
537 * This function looks for a key file named @file in the paths
538 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
539 * loads the file into @key_file and returns the file's full path in
540 * @full_path. If the file could not be loaded then an %error is
541 * set to either a #GFileError or #GKeyFileError.
543 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
547 g_key_file_load_from_data_dirs (GKeyFile *key_file,
553 GError *key_file_error = NULL;
554 gchar **all_data_dirs, **data_dirs;
555 const gchar * user_data_dir;
556 const gchar * const * system_data_dirs;
562 g_return_val_if_fail (key_file != NULL, FALSE);
563 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
565 user_data_dir = g_get_user_data_dir ();
566 system_data_dirs = g_get_system_data_dirs ();
567 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
570 all_data_dirs[i++] = g_strdup (user_data_dir);
573 while (system_data_dirs[j] != NULL)
574 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
577 data_dirs = all_data_dirs;
578 while (*data_dirs != NULL && !found_file)
580 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
586 g_propagate_error (error, key_file_error);
590 found_file = g_key_file_load_from_fd (key_file, fd, flags,
596 g_propagate_error (error, key_file_error);
597 g_free (output_path);
602 *full_path = output_path;
605 g_strfreev (all_data_dirs);
611 * @key_file: a #GKeyFile
618 g_key_file_free (GKeyFile *key_file)
620 g_return_if_fail (key_file != NULL);
622 g_key_file_clear (key_file);
626 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
627 * true for locales that match those in g_get_language_names().
630 g_key_file_locale_is_interesting (GKeyFile *key_file,
633 const gchar * const * current_locales;
636 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
639 current_locales = g_get_language_names ();
641 for (i = 0; current_locales[i] != NULL; i++)
643 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
651 g_key_file_parse_line (GKeyFile *key_file,
656 GError *parse_error = NULL;
659 g_return_if_fail (key_file != NULL);
660 g_return_if_fail (line != NULL);
662 line_start = (gchar *) line;
663 while (g_ascii_isspace (*line_start))
666 if (g_key_file_line_is_comment (line_start))
667 g_key_file_parse_comment (key_file, line, length, &parse_error);
668 else if (g_key_file_line_is_group (line_start))
669 g_key_file_parse_group (key_file, line_start,
670 length - (line_start - line),
672 else if (g_key_file_line_is_key_value_pair (line_start))
673 g_key_file_parse_key_value_pair (key_file, line_start,
674 length - (line_start - line),
678 g_set_error (error, G_KEY_FILE_ERROR,
679 G_KEY_FILE_ERROR_PARSE,
680 _("Key file contains line '%s' which is not "
681 "a key-value pair, group, or comment"), line);
686 g_propagate_error (error, parse_error);
690 g_key_file_parse_comment (GKeyFile *key_file,
695 GKeyFileKeyValuePair *pair;
697 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
700 g_assert (key_file->current_group != NULL);
702 pair = g_new0 (GKeyFileKeyValuePair, 1);
705 pair->value = g_strndup (line, length);
707 key_file->current_group->key_value_pairs =
708 g_list_prepend (key_file->current_group->key_value_pairs, pair);
712 g_key_file_parse_group (GKeyFile *key_file,
718 const gchar *group_name_start, *group_name_end;
720 /* advance past opening '['
722 group_name_start = line + 1;
723 group_name_end = line + length - 1;
725 while (*group_name_end != ']')
728 group_name = g_strndup (group_name_start,
729 group_name_end - group_name_start);
731 g_key_file_add_group (key_file, group_name);
736 g_key_file_parse_key_value_pair (GKeyFile *key_file,
741 gchar *key, *value, *key_end, *value_start, *locale;
742 gsize key_len, value_len;
744 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
746 g_set_error (error, G_KEY_FILE_ERROR,
747 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
748 _("Key file does not start with a group"));
752 key_end = value_start = strchr (line, '=');
754 g_assert (key_end != NULL);
759 /* Pull the key name from the line (chomping trailing whitespace)
761 while (g_ascii_isspace (*key_end))
764 key_len = key_end - line + 2;
766 g_assert (key_len <= length);
768 key = g_strndup (line, key_len - 1);
770 /* Pull the value from the line (chugging leading whitespace)
772 while (g_ascii_isspace (*value_start))
775 value_len = line + length - value_start + 1;
777 value = g_strndup (value_start, value_len);
779 g_assert (key_file->start_group != NULL);
781 if (key_file->current_group
782 && key_file->current_group->name
783 && strcmp (key_file->start_group->name,
784 key_file->current_group->name) == 0
785 && strcmp (key, "Encoding") == 0)
787 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
789 g_set_error (error, G_KEY_FILE_ERROR,
790 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
791 _("Key file contains unsupported encoding '%s'"), value);
799 /* Is this key a translation? If so, is it one that we care about?
801 locale = key_get_locale (key);
803 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
804 g_key_file_add_key (key_file, key_file->current_group, key, value);
812 key_get_locale (const gchar *key)
816 locale = g_strrstr (key, "[");
818 if (locale && strlen (locale) <= 2)
822 locale = g_strndup (locale + 1, strlen (locale) - 2);
828 g_key_file_parse_data (GKeyFile *key_file,
836 g_return_if_fail (key_file != NULL);
837 g_return_if_fail (data != NULL);
841 for (i = 0; i < length; i++)
845 /* When a newline is encountered flush the parse buffer so that the
846 * line can be parsed. Note that completely blank lines won't show
847 * up in the parse buffer, so they get parsed directly.
849 if (key_file->parse_buffer->len > 0)
850 g_key_file_flush_parse_buffer (key_file, &parse_error);
852 g_key_file_parse_comment (key_file, "", 1, &parse_error);
856 g_propagate_error (error, parse_error);
861 g_string_append_c (key_file->parse_buffer, data[i]);
864 key_file->approximate_size += length;
868 g_key_file_flush_parse_buffer (GKeyFile *key_file,
871 GError *file_error = NULL;
873 g_return_if_fail (key_file != NULL);
877 if (key_file->parse_buffer->len > 0)
879 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
880 key_file->parse_buffer->len,
882 g_string_erase (key_file->parse_buffer, 0, -1);
886 g_propagate_error (error, file_error);
893 * g_key_file_to_data:
894 * @key_file: a #GKeyFile
895 * @length: return location for the length of the
896 * returned string, or %NULL
897 * @error: return location for a #GError, or %NULL
899 * This function outputs @key_file as a string.
901 * Return value: a newly allocated string holding
902 * the contents of the #GKeyFile
907 g_key_file_to_data (GKeyFile *key_file,
911 GString *data_string;
913 GList *group_node, *key_file_node;
915 g_return_val_if_fail (key_file != NULL, NULL);
917 data_string = g_string_sized_new (2 * key_file->approximate_size);
919 for (group_node = g_list_last (key_file->groups);
921 group_node = group_node->prev)
923 GKeyFileGroup *group;
925 group = (GKeyFileGroup *) group_node->data;
927 if (group->comment != NULL)
928 g_string_append_printf (data_string, "%s\n", group->comment->value);
929 if (group->name != NULL)
930 g_string_append_printf (data_string, "[%s]\n", group->name);
932 for (key_file_node = g_list_last (group->key_value_pairs);
933 key_file_node != NULL;
934 key_file_node = key_file_node->prev)
936 GKeyFileKeyValuePair *pair;
938 pair = (GKeyFileKeyValuePair *) key_file_node->data;
940 if (pair->key != NULL)
941 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
943 g_string_append_printf (data_string, "%s\n", pair->value);
948 *length = data_string->len;
950 data = data_string->str;
952 g_string_free (data_string, FALSE);
958 * g_key_file_get_keys:
959 * @key_file: a #GKeyFile
960 * @group_name: a group name
961 * @length: return location for the number of keys returned, or %NULL
962 * @error: return location for a #GError, or %NULL
964 * Returns all keys for the group name @group_name. The array of
965 * returned keys will be %NULL-terminated, so @length may
966 * optionally be %NULL. In the event that the @group_name cannot
967 * be found, %NULL is returned and @error is set to
968 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
970 * Return value: a newly-allocated %NULL-terminated array of
971 * strings. Use g_strfreev() to free it.
976 g_key_file_get_keys (GKeyFile *key_file,
977 const gchar *group_name,
981 GKeyFileGroup *group;
986 g_return_val_if_fail (key_file != NULL, NULL);
987 g_return_val_if_fail (group_name != NULL, NULL);
989 group = g_key_file_lookup_group (key_file, group_name);
993 g_set_error (error, G_KEY_FILE_ERROR,
994 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
995 _("Key file does not have group '%s'"),
996 group_name ? group_name : "(null)");
1000 num_keys = g_list_length (group->key_value_pairs);
1002 keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1004 tmp = group->key_value_pairs;
1005 for (i = 0; i < num_keys; i++)
1007 GKeyFileKeyValuePair *pair;
1009 pair = (GKeyFileKeyValuePair *) tmp->data;
1010 keys[i] = g_strdup (pair->key);
1023 * g_key_file_get_start_group:
1024 * @key_file: a #GKeyFile
1026 * Returns the name of the start group of the file.
1028 * Return value: The start group of the key file.
1033 g_key_file_get_start_group (GKeyFile *key_file)
1035 g_return_val_if_fail (key_file != NULL, NULL);
1037 if (key_file->start_group)
1038 return g_strdup (key_file->start_group->name);
1044 * g_key_file_get_groups:
1045 * @key_file: a #GKeyFile
1046 * @length: return location for the number of returned groups, or %NULL
1048 * Returns all groups in the key file loaded with @key_file. The
1049 * array of returned groups will be %NULL-terminated, so @length may
1050 * optionally be %NULL.
1052 * Return value: a newly-allocated %NULL-terminated array of strings.
1053 * Use g_strfreev() to free it.
1057 g_key_file_get_groups (GKeyFile *key_file,
1062 gsize i, num_groups;
1064 g_return_val_if_fail (key_file != NULL, NULL);
1066 num_groups = g_list_length (key_file->groups);
1068 g_assert (num_groups > 0);
1070 /* Only need num_groups instead of num_groups + 1
1071 * because the first group of the file (last in the
1072 * list) is always the comment group at the top,
1075 groups = (gchar **) g_new0 (gchar **, num_groups);
1077 group_node = g_list_last (key_file->groups);
1079 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1082 for (group_node = group_node->prev;
1084 group_node = group_node->prev)
1086 GKeyFileGroup *group;
1088 group = (GKeyFileGroup *) group_node->data;
1090 g_assert (group->name != NULL);
1092 groups[i++] = g_strdup (group->name);
1103 * g_key_file_get_value:
1104 * @key_file: a #GKeyFile
1105 * @group_name: a group name
1107 * @error: return location for a #GError, or %NULL
1109 * Returns the value associated with @key under @group_name.
1111 * In the event the key cannot be found, %NULL is returned and
1112 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1113 * event that the @group_name cannot be found, %NULL is returned
1114 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1116 * Return value: a string or %NULL if the specified key cannot be
1122 g_key_file_get_value (GKeyFile *key_file,
1123 const gchar *group_name,
1127 GKeyFileGroup *group;
1128 GKeyFileKeyValuePair *pair;
1129 gchar *value = NULL;
1131 g_return_val_if_fail (key_file != NULL, NULL);
1132 g_return_val_if_fail (group_name != NULL, NULL);
1133 g_return_val_if_fail (key != NULL, NULL);
1135 group = g_key_file_lookup_group (key_file, group_name);
1139 g_set_error (error, G_KEY_FILE_ERROR,
1140 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1141 _("Key file does not have group '%s'"),
1142 group_name ? group_name : "(null)");
1146 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1149 value = g_strdup (pair->value);
1151 g_set_error (error, G_KEY_FILE_ERROR,
1152 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1153 _("Key file does not have key '%s'"), key);
1159 * g_key_file_set_value:
1160 * @key_file: a #GKeyFile
1161 * @group_name: a group name
1165 * Associates a new value with @key under @group_name. If @key
1166 * cannot be found then it is created. If @group_name cannot be
1167 * found then it is created.
1172 g_key_file_set_value (GKeyFile *key_file,
1173 const gchar *group_name,
1177 GKeyFileGroup *group;
1178 GKeyFileKeyValuePair *pair;
1180 g_return_if_fail (key_file != NULL);
1181 g_return_if_fail (group_name != NULL);
1182 g_return_if_fail (key != NULL);
1183 g_return_if_fail (value != NULL);
1185 group = g_key_file_lookup_group (key_file, group_name);
1189 g_key_file_add_group (key_file, group_name);
1190 group = (GKeyFileGroup *) key_file->groups->data;
1192 g_key_file_add_key (key_file, group, key, value);
1196 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1199 g_key_file_add_key (key_file, group, key, value);
1202 g_free (pair->value);
1203 pair->value = g_strdup (value);
1209 * g_key_file_get_string:
1210 * @key_file: a #GKeyFile
1211 * @group_name: a group name
1213 * @error: return location for a #GError, or %NULL
1215 * Returns the value associated with @key under @group_name.
1217 * In the event the key cannot be found, %NULL is returned and
1218 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1219 * event that the @group_name cannot be found, %NULL is returned
1220 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1222 * Return value: a string or %NULL if the specified key cannot be
1228 g_key_file_get_string (GKeyFile *key_file,
1229 const gchar *group_name,
1233 gchar *value, *string_value;
1234 GError *key_file_error;
1236 g_return_val_if_fail (key_file != NULL, NULL);
1237 g_return_val_if_fail (group_name != NULL, NULL);
1238 g_return_val_if_fail (key != NULL, NULL);
1240 key_file_error = NULL;
1242 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1246 g_propagate_error (error, key_file_error);
1250 if (!g_utf8_validate (value, -1, NULL))
1252 g_set_error (error, G_KEY_FILE_ERROR,
1253 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1254 _("Key file contains key '%s' with value '%s' "
1255 "which is not UTF-8"), key, value);
1260 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1266 if (g_error_matches (key_file_error,
1268 G_KEY_FILE_ERROR_INVALID_VALUE))
1270 g_set_error (error, G_KEY_FILE_ERROR,
1271 G_KEY_FILE_ERROR_INVALID_VALUE,
1272 _("Key file contains key '%s' "
1273 "which has value that cannot be interpreted."),
1275 g_error_free (key_file_error);
1278 g_propagate_error (error, key_file_error);
1281 return string_value;
1285 * g_key_file_set_string:
1286 * @key_file: a #GKeyFile
1287 * @group_name: a group name
1291 * Associates a new string value with @key under @group_name. If
1292 * @key cannot be found then it is created. If @group_name
1293 * cannot be found then it is created.
1298 g_key_file_set_string (GKeyFile *key_file,
1299 const gchar *group_name,
1301 const gchar *string)
1305 g_return_if_fail (key_file != NULL);
1306 g_return_if_fail (group_name != NULL);
1307 g_return_if_fail (key != NULL);
1308 g_return_if_fail (string != NULL);
1310 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1311 g_key_file_set_value (key_file, group_name, key, value);
1316 * g_key_file_get_string_list:
1317 * @key_file: a #GKeyFile
1318 * @group_name: a group name
1320 * @length: return location for the number of returned strings, or %NULL
1321 * @error: return location for a #GError, or %NULL
1323 * Returns the values associated with @key under @group_name.
1325 * In the event the key cannot be found, %NULL is returned and
1326 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1327 * event that the @group_name cannot be found, %NULL is returned
1328 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1330 * Return value: a %NULL-terminated string array or %NULL if the specified
1331 * key cannot be found. The array should be freed with g_strfreev().
1336 g_key_file_get_string_list (GKeyFile *key_file,
1337 const gchar *group_name,
1342 GError *key_file_error = NULL;
1343 gchar *value, *string_value, **values;
1345 GSList *p, *pieces = NULL;
1347 g_return_val_if_fail (key_file != NULL, NULL);
1348 g_return_val_if_fail (group_name != NULL, NULL);
1349 g_return_val_if_fail (key != NULL, NULL);
1351 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1355 g_propagate_error (error, key_file_error);
1359 if (!g_utf8_validate (value, -1, NULL))
1361 g_set_error (error, G_KEY_FILE_ERROR,
1362 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1363 _("Key file contains key '%s' with value '%s' "
1364 "which is not UTF-8"), key, value);
1369 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1371 g_free (string_value);
1375 if (g_error_matches (key_file_error,
1377 G_KEY_FILE_ERROR_INVALID_VALUE))
1379 g_set_error (error, G_KEY_FILE_ERROR,
1380 G_KEY_FILE_ERROR_INVALID_VALUE,
1381 _("Key file contains key '%s' "
1382 "which has value that cannot be interpreted."),
1384 g_error_free (key_file_error);
1387 g_propagate_error (error, key_file_error);
1390 len = g_slist_length (pieces);
1391 values = g_new0 (gchar *, len + 1);
1392 for (p = pieces, i = 0; p; p = p->next)
1393 values[i++] = p->data;
1396 g_slist_free (pieces);
1405 * g_key_file_set_string_list:
1406 * @key_file: a #GKeyFile
1407 * @group_name: a group name
1409 * @list: an array of locale string values
1410 * @length: number of locale string values in @list
1412 * Associates a list of string values for @key under @group_name.
1413 * If @key cannot be found then it is created. If @group_name
1414 * cannot be found then it is created.
1419 g_key_file_set_string_list (GKeyFile *key_file,
1420 const gchar *group_name,
1422 const gchar * const list[],
1425 GString *value_list;
1428 g_return_if_fail (key_file != NULL);
1429 g_return_if_fail (group_name != NULL);
1430 g_return_if_fail (key != NULL);
1431 g_return_if_fail (list != NULL);
1433 value_list = g_string_sized_new (length * 128);
1434 for (i = 0; list[i] != NULL && i < length; i++)
1438 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1439 g_string_append (value_list, value);
1440 g_string_append_c (value_list, key_file->list_separator);
1445 g_key_file_set_value (key_file, group_name, key, value_list->str);
1446 g_string_free (value_list, TRUE);
1450 * g_key_file_set_locale_string:
1451 * @key_file: a #GKeyFile
1452 * @group_name: a group name
1457 * Associates a string value for @key and @locale under
1458 * @group_name. If the translation for @key cannot be found
1459 * then it is created.
1464 g_key_file_set_locale_string (GKeyFile *key_file,
1465 const gchar *group_name,
1467 const gchar *locale,
1468 const gchar *string)
1470 gchar *full_key, *value;
1472 g_return_if_fail (key_file != NULL);
1473 g_return_if_fail (group_name != NULL);
1474 g_return_if_fail (key != NULL);
1475 g_return_if_fail (locale != NULL);
1476 g_return_if_fail (string != NULL);
1478 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1479 full_key = g_strdup_printf ("%s[%s]", key, locale);
1480 g_key_file_set_value (key_file, group_name, full_key, value);
1485 extern GSList *_g_compute_locale_variants (const gchar *locale);
1488 * g_key_file_get_locale_string:
1489 * @key_file: a #GKeyFile
1490 * @group_name: a group name
1492 * @locale: a locale or %NULL
1493 * @error: return location for a #GError, or %NULL
1495 * Returns the value associated with @key under @group_name
1496 * translated in the given @locale if available. If @locale is
1497 * %NULL then the current locale is assumed.
1499 * If @key cannot be found then %NULL is returned and @error is set to
1500 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1501 * with @key cannot be interpreted or no suitable translation can
1502 * be found then the untranslated value is returned.
1504 * Return value: a string or %NULL if the specified key cannot be
1509 g_key_file_get_locale_string (GKeyFile *key_file,
1510 const gchar *group_name,
1512 const gchar *locale,
1515 gchar *candidate_key, *translated_value;
1516 GError *key_file_error;
1518 gboolean free_languages = FALSE;
1521 g_return_val_if_fail (key_file != NULL, NULL);
1522 g_return_val_if_fail (group_name != NULL, NULL);
1523 g_return_val_if_fail (key != NULL, NULL);
1525 candidate_key = NULL;
1526 translated_value = NULL;
1527 key_file_error = NULL;
1533 list = _g_compute_locale_variants (locale);
1535 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1536 for (l = list, i = 0; l; l = l->next, i++)
1537 languages[i] = l->data;
1538 languages[i] = NULL;
1540 g_slist_free (list);
1541 free_languages = TRUE;
1545 languages = (gchar **) g_get_language_names ();
1546 free_languages = FALSE;
1549 for (i = 0; languages[i]; i++)
1551 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1553 translated_value = g_key_file_get_string (key_file,
1555 candidate_key, NULL);
1556 g_free (candidate_key);
1558 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1561 g_free (translated_value);
1562 translated_value = NULL;
1565 /* Fallback to untranslated key
1567 if (!translated_value)
1569 translated_value = g_key_file_get_string (key_file, group_name, key,
1572 if (!translated_value)
1573 g_propagate_error (error, key_file_error);
1577 g_strfreev (languages);
1579 return translated_value;
1583 * g_key_file_get_locale_string_list:
1584 * @key_file: a #GKeyFile
1585 * @group_name: a group name
1588 * @length: return location for the number of returned strings or %NULL
1589 * @error: return location for a #GError or %NULL
1591 * Returns the values associated with @key under @group_name
1592 * translated in the given @locale if available. If @locale is
1593 * %NULL then the current locale is assumed. If @group_name is
1594 * %NULL, then the start group is used.
1596 * If @key cannot be found then %NULL is returned and @error is set to
1597 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1598 * with @key cannot be interpreted or no suitable translations
1599 * can be found then the untranslated values are returned.
1600 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1602 * Return value: a newly allocated %NULL-terminated string array
1603 * or %NULL if the key isn't found. The string array should be freed
1604 * with g_strfreev().
1609 g_key_file_get_locale_string_list (GKeyFile *key_file,
1610 const gchar *group_name,
1612 const gchar *locale,
1616 GError *key_file_error;
1617 gchar **values, *value;
1619 g_return_val_if_fail (key_file != NULL, NULL);
1620 g_return_val_if_fail (group_name != NULL, NULL);
1621 g_return_val_if_fail (key != NULL, NULL);
1623 key_file_error = NULL;
1625 value = g_key_file_get_locale_string (key_file, group_name,
1630 g_propagate_error (error, key_file_error);
1635 if (value[strlen (value) - 1] == ';')
1636 value[strlen (value) - 1] = '\0';
1638 values = g_strsplit (value, ";", 0);
1643 *length = g_strv_length (values);
1649 * g_key_file_set_locale_string_list:
1650 * @key_file: a #GKeyFile
1651 * @group_name: a group name
1654 * @list: a %NULL-terminated array of locale string values
1655 * @length: the length of @list
1657 * Associates a list of string values for @key and @locale under
1658 * @group_name. If the translation for @key cannot be found then
1659 * it is created. If @group_name is %NULL, the start group is
1665 g_key_file_set_locale_string_list (GKeyFile *key_file,
1666 const gchar *group_name,
1668 const gchar *locale,
1669 const gchar * const list[],
1672 GString *value_list;
1676 g_return_if_fail (key_file != NULL);
1677 g_return_if_fail (group_name != NULL);
1678 g_return_if_fail (key != NULL);
1679 g_return_if_fail (locale != NULL);
1680 g_return_if_fail (length != 0);
1682 value_list = g_string_sized_new (length * 128);
1683 for (i = 0; list[i] != NULL && i < length; i++)
1687 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1689 g_string_append (value_list, value);
1690 g_string_append_c (value_list, ';');
1695 full_key = g_strdup_printf ("%s[%s]", key, locale);
1696 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1698 g_string_free (value_list, TRUE);
1702 * g_key_file_get_boolean:
1703 * @key_file: a #GKeyFile
1704 * @group_name: a group name
1706 * @error: return location for a #GError
1708 * Returns the value associated with @key under @group_name as a
1709 * boolean. If @group_name is %NULL, the start group is used.
1711 * If @key cannot be found then the return value is undefined and
1712 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1713 * the value associated with @key cannot be interpreted as a boolean
1714 * then the return value is also undefined and @error is set to
1715 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1717 * Return value: the value associated with the key as a boolean
1721 g_key_file_get_boolean (GKeyFile *key_file,
1722 const gchar *group_name,
1726 GError *key_file_error = NULL;
1728 gboolean bool_value;
1730 g_return_val_if_fail (key_file != NULL, FALSE);
1731 g_return_val_if_fail (group_name != NULL, FALSE);
1732 g_return_val_if_fail (key != NULL, FALSE);
1734 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1738 g_propagate_error (error, key_file_error);
1742 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1748 if (g_error_matches (key_file_error,
1750 G_KEY_FILE_ERROR_INVALID_VALUE))
1752 g_set_error (error, G_KEY_FILE_ERROR,
1753 G_KEY_FILE_ERROR_INVALID_VALUE,
1754 _("Key file contains key '%s' "
1755 "which has value that cannot be interpreted."),
1757 g_error_free (key_file_error);
1760 g_propagate_error (error, key_file_error);
1767 * g_key_file_set_boolean:
1768 * @key_file: a #GKeyFile
1769 * @group_name: a group name
1771 * @value: %TRUE or %FALSE
1773 * Associates a new boolean value with @key under @group_name.
1774 * If @key cannot be found then it is created. If @group_name
1775 * is %NULL, the start group is used.
1780 g_key_file_set_boolean (GKeyFile *key_file,
1781 const gchar *group_name,
1787 g_return_if_fail (key_file != NULL);
1788 g_return_if_fail (group_name != NULL);
1789 g_return_if_fail (key != NULL);
1791 result = g_key_file_parse_boolean_as_value (key_file, value);
1792 g_key_file_set_value (key_file, group_name, key, result);
1797 * g_key_file_get_boolean_list:
1798 * @key_file: a #GKeyFile
1799 * @group_name: a group name
1801 * @length: the number of booleans returned
1802 * @error: return location for a #GError
1804 * Returns the values associated with @key under @group_name as
1805 * booleans. If @group_name is %NULL, the start_group is used.
1807 * If @key cannot be found then the return value is undefined and
1808 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1809 * the values associated with @key cannot be interpreted as booleans
1810 * then the return value is also undefined and @error is set to
1811 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1813 * Return value: the values associated with the key as a boolean
1818 g_key_file_get_boolean_list (GKeyFile *key_file,
1819 const gchar *group_name,
1824 GError *key_file_error;
1826 gboolean *bool_values;
1829 g_return_val_if_fail (key_file != NULL, NULL);
1830 g_return_val_if_fail (group_name != NULL, FALSE);
1831 g_return_val_if_fail (key != NULL, NULL);
1833 key_file_error = NULL;
1835 values = g_key_file_get_string_list (key_file, group_name, key,
1836 &num_bools, &key_file_error);
1839 g_propagate_error (error, key_file_error);
1844 bool_values = g_new0 (gboolean, num_bools);
1846 for (i = 0; i < num_bools; i++)
1848 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1854 g_propagate_error (error, key_file_error);
1855 g_strfreev (values);
1856 g_free (bool_values);
1861 g_strfreev (values);
1864 *length = num_bools;
1870 * g_key_file_set_boolean_list:
1871 * @key_file: a #GKeyFile
1872 * @group_name: a group name
1874 * @list: an array of boolean values
1875 * @length: length of @list
1877 * Associates a list of boolean values with @key under
1878 * @group_name. If @key cannot be found then it is created.
1879 * If @group_name is %NULL, the start_group is used.
1884 g_key_file_set_boolean_list (GKeyFile *key_file,
1885 const gchar *group_name,
1890 GString *value_list;
1893 g_return_if_fail (key_file != NULL);
1894 g_return_if_fail (group_name != NULL);
1895 g_return_if_fail (key != NULL);
1896 g_return_if_fail (list != NULL);
1898 value_list = g_string_sized_new (length * 8);
1899 for (i = 0; i < length; i++)
1903 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1905 g_string_append (value_list, value);
1906 g_string_append_c (value_list, key_file->list_separator);
1911 g_key_file_set_value (key_file, group_name, key, value_list->str);
1912 g_string_free (value_list, TRUE);
1916 * g_key_file_get_integer:
1917 * @key_file: a #GKeyFile
1918 * @group_name: a group name
1920 * @error: return location for a #GError
1922 * Returns the value associated with @key under @group_name as an
1923 * integer. If @group_name is %NULL, the start_group is used.
1925 * If @key cannot be found then the return value is undefined and
1926 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1927 * the value associated with @key cannot be interpreted as an integer
1928 * then the return value is also undefined and @error is set to
1929 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1931 * Return value: the value associated with the key as an integer.
1936 g_key_file_get_integer (GKeyFile *key_file,
1937 const gchar *group_name,
1941 GError *key_file_error;
1945 g_return_val_if_fail (key_file != NULL, -1);
1946 g_return_val_if_fail (group_name != NULL, -1);
1947 g_return_val_if_fail (key != NULL, -1);
1949 key_file_error = NULL;
1951 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1955 g_propagate_error (error, key_file_error);
1959 int_value = g_key_file_parse_value_as_integer (key_file, value,
1965 if (g_error_matches (key_file_error,
1967 G_KEY_FILE_ERROR_INVALID_VALUE))
1969 g_set_error (error, G_KEY_FILE_ERROR,
1970 G_KEY_FILE_ERROR_INVALID_VALUE,
1971 _("Key file contains key '%s' in group '%s' "
1972 "which has value that cannot be interpreted."), key,
1974 g_error_free (key_file_error);
1977 g_propagate_error (error, key_file_error);
1984 * g_key_file_set_integer:
1985 * @key_file: a #GKeyFile
1986 * @group_name: a group name
1988 * @value: an integer value
1990 * Associates a new integer value with @key under @group_name.
1991 * If @key cannot be found then it is created. If @group_name
1992 * is %NULL, the start group is used.
1997 g_key_file_set_integer (GKeyFile *key_file,
1998 const gchar *group_name,
2004 g_return_if_fail (key_file != NULL);
2005 g_return_if_fail (group_name != NULL);
2006 g_return_if_fail (key != NULL);
2008 result = g_key_file_parse_integer_as_value (key_file, value);
2009 g_key_file_set_value (key_file, group_name, key, result);
2014 * g_key_file_get_integer_list:
2015 * @key_file: a #GKeyFile
2016 * @group_name: a group name
2018 * @length: the number of integers returned
2019 * @error: return location for a #GError
2021 * Returns the values associated with @key under @group_name as
2022 * integers. If @group_name is %NULL, the start group is used.
2024 * If @key cannot be found then the return value is undefined and
2025 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2026 * the values associated with @key cannot be interpreted as integers
2027 * then the return value is also undefined and @error is set to
2028 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2030 * Return value: the values associated with the key as a integer
2035 g_key_file_get_integer_list (GKeyFile *key_file,
2036 const gchar *group_name,
2041 GError *key_file_error = NULL;
2046 g_return_val_if_fail (key_file != NULL, NULL);
2047 g_return_val_if_fail (group_name != NULL, NULL);
2048 g_return_val_if_fail (key != NULL, NULL);
2050 values = g_key_file_get_string_list (key_file, group_name, key,
2051 &num_ints, &key_file_error);
2054 g_propagate_error (error, key_file_error);
2059 int_values = g_new0 (gint, num_ints);
2061 for (i = 0; i < num_ints; i++)
2063 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2069 g_propagate_error (error, key_file_error);
2070 g_strfreev (values);
2071 g_free (int_values);
2076 g_strfreev (values);
2085 * g_key_file_set_integer_list:
2086 * @key_file: a #GKeyFile
2087 * @group_name: a group name
2089 * @list: an array of integer values
2090 * @length: number of integer values in @list
2092 * Associates a list of integer values with @key under
2093 * @group_name. If @key cannot be found then it is created.
2094 * If @group_name is %NULL the start group is used.
2099 g_key_file_set_integer_list (GKeyFile *key_file,
2100 const gchar *group_name,
2108 g_return_if_fail (key_file != NULL);
2109 g_return_if_fail (group_name != NULL);
2110 g_return_if_fail (key != NULL);
2111 g_return_if_fail (list != NULL);
2113 values = g_string_sized_new (length * 16);
2114 for (i = 0; i < length; i++)
2118 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2120 g_string_append (values, value);
2121 g_string_append_c (values, ';');
2126 g_key_file_set_value (key_file, group_name, key, values->str);
2127 g_string_free (values, TRUE);
2131 g_key_file_set_key_comment (GKeyFile *key_file,
2132 const gchar *group_name,
2134 const gchar *comment,
2137 GKeyFileGroup *group;
2138 GKeyFileKeyValuePair *pair;
2139 GList *key_node, *comment_node, *tmp;
2141 group = g_key_file_lookup_group (key_file, group_name);
2144 g_set_error (error, G_KEY_FILE_ERROR,
2145 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2146 _("Key file does not have group '%s'"),
2147 group_name ? group_name : "(null)");
2152 /* First find the key the comments are supposed to be
2155 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2157 if (key_node == NULL)
2159 g_set_error (error, G_KEY_FILE_ERROR,
2160 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2161 _("Key file does not have key '%s' in group '%s'"),
2166 /* Then find all the comments already associated with the
2169 tmp = key_node->next;
2172 GKeyFileKeyValuePair *pair;
2174 pair = (GKeyFileKeyValuePair *) tmp->data;
2176 if (pair->key != NULL)
2181 g_key_file_remove_key_value_pair_node (key_file, group,
2185 if (comment == NULL)
2188 /* Now we can add our new comment
2190 pair = g_new0 (GKeyFileKeyValuePair, 1);
2193 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2195 key_node = g_list_insert (key_node, pair, 1);
2199 g_key_file_set_group_comment (GKeyFile *key_file,
2200 const gchar *group_name,
2201 const gchar *comment,
2204 GKeyFileGroup *group;
2206 group = g_key_file_lookup_group (key_file, group_name);
2209 g_set_error (error, G_KEY_FILE_ERROR,
2210 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2211 _("Key file does not have group '%s'"),
2212 group_name ? group_name : "(null)");
2217 /* First remove any existing comment
2221 g_key_file_key_value_pair_free (group->comment);
2222 group->comment = NULL;
2225 if (comment == NULL)
2228 /* Now we can add our new comment
2230 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2232 group->comment->key = NULL;
2233 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2237 g_key_file_set_top_comment (GKeyFile *key_file,
2238 const gchar *comment,
2242 GKeyFileGroup *group;
2243 GKeyFileKeyValuePair *pair;
2245 /* The last group in the list should be the top (comments only)
2248 g_assert (key_file->groups != NULL);
2249 group_node = g_list_last (key_file->groups);
2250 group = (GKeyFileGroup *) group_node->data;
2251 g_assert (group->name == NULL);
2253 /* Note all keys must be comments at the top of
2254 * the file, so we can just free it all.
2256 if (group->key_value_pairs != NULL)
2258 g_list_foreach (group->key_value_pairs,
2259 (GFunc) g_key_file_key_value_pair_free,
2261 g_list_free (group->key_value_pairs);
2262 group->key_value_pairs = NULL;
2265 if (comment == NULL)
2268 pair = g_new0 (GKeyFileKeyValuePair, 1);
2271 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2273 group->key_value_pairs =
2274 g_list_prepend (group->key_value_pairs, pair);
2278 * g_key_file_set_comment:
2279 * @key_file: a #GKeyFile
2280 * @group_name: a group name, or %NULL
2282 * @comment: a comment
2283 * @error: return location for a #GError
2285 * Places a comment above @key from @group_name.
2286 * @group_name. If @key is %NULL then @comment will
2287 * be written above @group_name. If both @key
2288 * and @group_name are NULL, then @comment will
2289 * be written above the first group in the file.
2294 g_key_file_set_comment (GKeyFile *key_file,
2295 const gchar *group_name,
2297 const gchar *comment,
2300 g_return_if_fail (key_file != NULL);
2302 if (group_name != NULL && key != NULL)
2303 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2304 else if (group_name != NULL)
2305 g_key_file_set_group_comment (key_file, group_name, comment, error);
2307 g_key_file_set_top_comment (key_file, comment, error);
2309 if (comment != NULL)
2310 key_file->approximate_size += strlen (comment);
2314 g_key_file_get_key_comment (GKeyFile *key_file,
2315 const gchar *group_name,
2319 GKeyFileGroup *group;
2320 GList *key_node, *tmp;
2324 group = g_key_file_lookup_group (key_file, group_name);
2327 g_set_error (error, G_KEY_FILE_ERROR,
2328 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2329 _("Key file does not have group '%s'"),
2330 group_name ? group_name : "(null)");
2335 /* First find the key the comments are supposed to be
2338 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2340 if (key_node == NULL)
2342 g_set_error (error, G_KEY_FILE_ERROR,
2343 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2344 _("Key file does not have key '%s' in group '%s'"),
2351 /* Then find all the comments already associated with the
2352 * key and concatentate them.
2354 tmp = key_node->next;
2357 GKeyFileKeyValuePair *pair;
2359 pair = (GKeyFileKeyValuePair *) tmp->data;
2361 if (pair->key != NULL)
2365 string = g_string_sized_new (512);
2367 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2368 g_string_append (string, comment);
2376 comment = string->str;
2377 g_string_free (string, FALSE);
2386 g_key_file_get_group_comment (GKeyFile *key_file,
2387 const gchar *group_name,
2390 GKeyFileGroup *group;
2392 group = g_key_file_lookup_group (key_file, group_name);
2395 g_set_error (error, G_KEY_FILE_ERROR,
2396 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2397 _("Key file does not have group '%s'"),
2398 group_name ? group_name : "(null)");
2404 return g_strdup (group->comment->value);
2410 g_key_file_get_top_comment (GKeyFile *key_file,
2413 GList *group_node, *tmp;
2414 GKeyFileGroup *group;
2418 /* The last group in the list should be the top (comments only)
2421 g_assert (key_file->groups != NULL);
2422 group_node = g_list_last (key_file->groups);
2423 group = (GKeyFileGroup *) group_node->data;
2424 g_assert (group->name == NULL);
2428 /* Then find all the comments already associated with the
2429 * key and concatentate them.
2431 tmp = group->key_value_pairs;
2434 GKeyFileKeyValuePair *pair;
2436 pair = (GKeyFileKeyValuePair *) tmp->data;
2438 if (pair->key != NULL)
2442 string = g_string_sized_new (512);
2444 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2445 g_string_append (string, comment);
2453 comment = string->str;
2454 g_string_free (string, FALSE);
2463 * g_key_file_get_comment:
2464 * @key_file: a #GKeyFile
2465 * @group_name: a group name, or %NULL
2467 * @error: return location for a #GError
2469 * Retreives a comment above @key from @group_name.
2470 * @group_name. If @key is %NULL then @comment will
2471 * be read from above @group_name. If both @key
2472 * and @group_name are NULL, then @comment will
2473 * be read from above the first group in the file.
2476 * Returns: a comment that should be freed with g_free()
2479 g_key_file_get_comment (GKeyFile *key_file,
2480 const gchar *group_name,
2484 g_return_val_if_fail (key_file != NULL, NULL);
2486 if (group_name != NULL && key != NULL)
2487 return g_key_file_get_key_comment (key_file, group_name, key, error);
2488 else if (group_name != NULL)
2489 return g_key_file_get_group_comment (key_file, group_name, error);
2491 return g_key_file_get_top_comment (key_file, error);
2495 * g_key_file_remove_comment:
2496 * @key_file: a #GKeyFile
2497 * @group_name: a group name, or %NULL
2499 * @error: return location for a #GError
2501 * Removes a comment above @key from @group_name.
2502 * @group_name. If @key is %NULL then @comment will
2503 * be written above @group_name. If both @key
2504 * and @group_name are NULL, then @comment will
2505 * be written above the first group in the file.
2511 g_key_file_remove_comment (GKeyFile *key_file,
2512 const gchar *group_name,
2516 g_return_if_fail (key_file != NULL);
2518 if (group_name != NULL && key != NULL)
2519 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2520 else if (group_name != NULL)
2521 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2523 g_key_file_set_top_comment (key_file, NULL, error);
2527 * g_key_file_has_group:
2528 * @key_file: a #GKeyFile
2529 * @group_name: a group name
2531 * Looks whether the key file has the group @group_name.
2533 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2538 g_key_file_has_group (GKeyFile *key_file,
2539 const gchar *group_name)
2541 g_return_val_if_fail (key_file != NULL, FALSE);
2542 g_return_val_if_fail (group_name != NULL, FALSE);
2544 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2548 * g_key_file_has_key:
2549 * @key_file: a #GKeyFile
2550 * @group_name: a group name
2552 * @error: return location for a #GError
2554 * Looks whether the key file has the key @key in the group
2555 * @group_name. If @group_name is %NULL, the start group is
2558 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2564 g_key_file_has_key (GKeyFile *key_file,
2565 const gchar *group_name,
2569 GKeyFileKeyValuePair *pair;
2570 GKeyFileGroup *group;
2572 g_return_val_if_fail (key_file != NULL, FALSE);
2573 g_return_val_if_fail (group_name != NULL, FALSE);
2574 g_return_val_if_fail (key != NULL, FALSE);
2576 group = g_key_file_lookup_group (key_file, group_name);
2580 g_set_error (error, G_KEY_FILE_ERROR,
2581 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2582 _("Key file does not have group '%s'"),
2583 group_name ? group_name : "(null)");
2588 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2590 return pair != NULL;
2594 g_key_file_add_group (GKeyFile *key_file,
2595 const gchar *group_name)
2597 GKeyFileGroup *group;
2599 g_return_if_fail (key_file != NULL);
2600 g_return_if_fail (group_name != NULL);
2601 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2603 group = g_new0 (GKeyFileGroup, 1);
2604 group->name = g_strdup (group_name);
2605 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2606 key_file->groups = g_list_prepend (key_file->groups, group);
2607 key_file->approximate_size += strlen (group_name) + 3;
2608 key_file->current_group = group;
2610 if (key_file->start_group == NULL)
2611 key_file->start_group = group;
2615 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2620 g_free (pair->value);
2625 /* Be careful not to call this function on a node with data in the
2626 * lookup map without removing it from the lookup map, first.
2628 * Some current cases where this warning is not a concern are
2630 * - the node being removed is a comment node
2631 * - the entire lookup map is getting destroyed soon after
2635 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2636 GKeyFileGroup *group,
2640 GKeyFileKeyValuePair *pair;
2642 pair = (GKeyFileKeyValuePair *) pair_node->data;
2644 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2646 if (pair->key != NULL)
2647 key_file->approximate_size -= strlen (pair->key) + 1;
2649 g_assert (pair->value != NULL);
2650 key_file->approximate_size -= strlen (pair->value);
2652 g_key_file_key_value_pair_free (pair);
2654 g_list_free_1 (pair_node);
2658 g_key_file_remove_group_node (GKeyFile *key_file,
2661 GKeyFileGroup *group;
2664 group = (GKeyFileGroup *) group_node->data;
2666 /* If the current group gets deleted make the current group the last
2669 if (key_file->current_group == group)
2671 /* groups should always contain at least the top comment group,
2672 * unless g_key_file_clear has been called
2674 if (key_file->groups)
2675 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2677 key_file->current_group = NULL;
2680 /* If the start group gets deleted make the start group the first
2683 if (key_file->start_group == group)
2685 tmp = g_list_last (key_file->groups);
2688 if (tmp != group_node &&
2689 ((GKeyFileGroup *) tmp->data)->name != NULL)
2696 key_file->start_group = (GKeyFileGroup *) tmp->data;
2698 key_file->start_group = NULL;
2701 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2703 if (group->name != NULL)
2704 key_file->approximate_size -= strlen (group->name) + 3;
2706 tmp = group->key_value_pairs;
2713 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2716 g_assert (group->key_value_pairs == NULL);
2718 if (group->lookup_map)
2720 g_hash_table_destroy (group->lookup_map);
2721 group->lookup_map = NULL;
2724 g_free ((gchar *) group->name);
2726 g_list_free_1 (group_node);
2730 * g_key_file_remove_group:
2731 * @key_file: a #GKeyFile
2732 * @group_name: a group name
2733 * @error: return location for a #GError or %NULL
2735 * Removes the specified group, @group_name,
2736 * from the key file.
2741 g_key_file_remove_group (GKeyFile *key_file,
2742 const gchar *group_name,
2747 g_return_if_fail (key_file != NULL);
2748 g_return_if_fail (group_name != NULL);
2750 group_node = g_key_file_lookup_group_node (key_file, group_name);
2753 g_set_error (error, G_KEY_FILE_ERROR,
2754 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2755 _("Key file does not have group '%s'"),
2758 g_key_file_remove_group_node (key_file, group_node);
2762 g_key_file_add_key (GKeyFile *key_file,
2763 GKeyFileGroup *group,
2767 GKeyFileKeyValuePair *pair;
2769 pair = g_new0 (GKeyFileKeyValuePair, 1);
2771 pair->key = g_strdup (key);
2772 pair->value = g_strdup (value);
2774 g_hash_table_replace (group->lookup_map, pair->key, pair);
2775 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2776 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2780 * g_key_file_remove_key:
2781 * @key_file: a #GKeyFile
2782 * @group_name: a group name
2783 * @key: a key name to remove
2784 * @error: return location for a #GError or %NULL
2786 * Removes @key in @group_name from the key file.
2791 g_key_file_remove_key (GKeyFile *key_file,
2792 const gchar *group_name,
2796 GKeyFileGroup *group;
2797 GKeyFileKeyValuePair *pair;
2799 g_return_if_fail (key_file != NULL);
2800 g_return_if_fail (group_name != NULL);
2801 g_return_if_fail (key != NULL);
2805 group = g_key_file_lookup_group (key_file, group_name);
2808 g_set_error (error, G_KEY_FILE_ERROR,
2809 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2810 _("Key file does not have group '%s'"),
2811 group_name ? group_name : "(null)");
2815 group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2816 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2820 g_set_error (error, G_KEY_FILE_ERROR,
2821 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2822 _("Key file does not have key '%s' in group '%s'"),
2827 g_hash_table_remove (group->lookup_map, pair->key);
2829 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2830 g_key_file_key_value_pair_free (pair);
2834 g_key_file_lookup_group_node (GKeyFile *key_file,
2835 const gchar *group_name)
2837 GKeyFileGroup *group;
2841 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2843 group = (GKeyFileGroup *) tmp->data;
2845 if (group && group->name && strcmp (group->name, group_name) == 0)
2854 static GKeyFileGroup *
2855 g_key_file_lookup_group (GKeyFile *key_file,
2856 const gchar *group_name)
2860 group_node = g_key_file_lookup_group_node (key_file, group_name);
2862 if (group_node != NULL)
2863 return (GKeyFileGroup *) group_node->data;
2869 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2870 GKeyFileGroup *group,
2875 for (key_node = group->key_value_pairs;
2877 key_node = key_node->next)
2879 GKeyFileKeyValuePair *pair;
2881 pair = (GKeyFileKeyValuePair *) key_node->data;
2883 if (pair->key && strcmp (pair->key, key) == 0)
2890 static GKeyFileKeyValuePair *
2891 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2892 GKeyFileGroup *group,
2895 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2898 /* Lines starting with # or consisting entirely of whitespace are merely
2899 * recorded, not parsed. This function assumes all leading whitespace
2900 * has been stripped.
2903 g_key_file_line_is_comment (const gchar *line)
2905 return (*line == '#' || *line == '\0' || *line == '\n');
2908 /* A group in a key file is made up of a starting '[' followed by one
2909 * or more letters making up the group name followed by ']'.
2912 g_key_file_line_is_group (const gchar *line)
2920 p = g_utf8_next_char (p);
2925 p = g_utf8_next_char (p);
2927 /* Group name must be non-empty
2932 while (*p && *p != ']')
2933 p = g_utf8_next_char (p);
2942 g_key_file_line_is_key_value_pair (const gchar *line)
2946 p = (gchar *) g_utf8_strchr (line, -1, '=');
2951 /* Key must be non-empty
2960 g_key_file_parse_value_as_string (GKeyFile *key_file,
2965 GError *parse_error = NULL;
2966 gchar *string_value, *p, *q0, *q;
2968 string_value = g_new0 (gchar, strlen (value) + 1);
2970 p = (gchar *) value;
2971 q0 = q = string_value;
3001 if (pieces && *p == key_file->list_separator)
3002 *q = key_file->list_separator;
3008 if (parse_error == NULL)
3016 g_set_error (error, G_KEY_FILE_ERROR,
3017 G_KEY_FILE_ERROR_INVALID_VALUE,
3018 _("Key file contains invalid escape "
3019 "sequence '%s'"), sequence);
3028 if (pieces && (*p == key_file->list_separator))
3030 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3039 if (p[-1] == '\\' && error == NULL)
3040 g_set_error (error, G_KEY_FILE_ERROR,
3041 G_KEY_FILE_ERROR_INVALID_VALUE,
3042 _("Key file contains escape character at end of line"));
3048 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3049 *pieces = g_slist_reverse (*pieces);
3052 return string_value;
3056 g_key_file_parse_string_as_value (GKeyFile *key_file,
3057 const gchar *string,
3058 gboolean escape_separator)
3060 gchar *value, *p, *q;
3062 gboolean parsing_leading_space;
3064 length = strlen (string) + 1;
3066 /* Worst case would be that every character needs to be escaped.
3067 * In other words every character turns to two characters
3069 value = g_new0 (gchar, 2 * length);
3071 p = (gchar *) string;
3073 parsing_leading_space = TRUE;
3074 while (p < (string + length - 1))
3076 gchar escaped_character[3] = { '\\', 0, 0 };
3081 if (parsing_leading_space)
3083 escaped_character[1] = 's';
3084 strcpy (q, escaped_character);
3094 if (parsing_leading_space)
3096 escaped_character[1] = 't';
3097 strcpy (q, escaped_character);
3107 escaped_character[1] = 'n';
3108 strcpy (q, escaped_character);
3112 escaped_character[1] = 'r';
3113 strcpy (q, escaped_character);
3117 escaped_character[1] = '\\';
3118 strcpy (q, escaped_character);
3120 parsing_leading_space = FALSE;
3123 if (escape_separator && *p == key_file->list_separator)
3125 escaped_character[1] = key_file->list_separator;
3126 strcpy (q, escaped_character);
3128 parsing_leading_space = TRUE;
3134 parsing_leading_space = FALSE;
3146 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3150 gchar *end_of_valid_int;
3153 int_value = strtol (value, &end_of_valid_int, 0);
3155 if (*end_of_valid_int != '\0')
3156 g_set_error (error, G_KEY_FILE_ERROR,
3157 G_KEY_FILE_ERROR_INVALID_VALUE,
3158 _("Value '%s' cannot be interpreted as a number."), value);
3164 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3168 return g_strdup_printf ("%d", value);
3172 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3178 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3180 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3184 g_set_error (error, G_KEY_FILE_ERROR,
3185 G_KEY_FILE_ERROR_INVALID_VALUE,
3186 _("Value '%s' cannot be interpreted as a boolean."), value);
3192 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3196 return g_strdup ("true");
3198 return g_strdup ("false");
3202 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3206 gchar **lines, *comment;
3209 string = g_string_sized_new (512);
3211 lines = g_strsplit (value, "\n", 0);
3213 for (i = 0; lines[i] != NULL; i++)
3215 if (lines[i][0] != '#')
3216 g_string_append_printf (string, "%s\n", lines[i]);
3218 g_string_append_printf (string, "%s\n", lines[i] + 1);
3222 comment = string->str;
3224 g_string_free (string, FALSE);
3230 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3231 const gchar *comment)
3234 gchar **lines, *value;
3237 string = g_string_sized_new (512);
3239 lines = g_strsplit (comment, "\n", 0);
3241 for (i = 0; lines[i] != NULL; i++)
3242 g_string_append_printf (string, "#%s%s", lines[i],
3243 lines[i + 1] == NULL? "" : "\n");
3246 value = string->str;
3248 g_string_free (string, FALSE);