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"
55 #include "gstrfuncs.h"
58 typedef struct _GKeyFileGroup GKeyFileGroup;
64 gchar *start_group_name;
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 *entry,
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 *entry,
128 const gchar *group_name,
131 static void g_key_file_add_group (GKeyFile *entry,
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 *entry,
141 static gchar *g_key_file_parse_string_as_value (GKeyFile *entry,
143 gboolean escape_separator);
144 static gint g_key_file_parse_value_as_integer (GKeyFile *entry,
147 static gchar *g_key_file_parse_integer_as_value (GKeyFile *entry,
149 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *entry,
152 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *entry,
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 *entry,
162 static void g_key_file_parse_comment (GKeyFile *entry,
166 static void g_key_file_parse_group (GKeyFile *entry,
170 static gchar *key_get_locale (const gchar *key);
171 static void g_key_file_parse_data (GKeyFile *entry,
175 static void g_key_file_flush_parse_buffer (GKeyFile *entry,
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_name = 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 g_free (key_file->start_group_name);
212 tmp = key_file->groups;
217 g_key_file_remove_group_node (key_file, group_node);
220 g_assert (key_file->groups == NULL);
227 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
228 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
229 * read an existing key file.
231 * Return value: an empty #GKeyFile.
236 g_key_file_new (void)
240 key_file = g_new0 (GKeyFile, 1);
241 g_key_file_init (key_file);
247 * g_key_file_set_list_separator:
248 * @key_file: a #GKeyFile
249 * @separator: the separator
251 * Sets the character which is used to separate
252 * values in lists. Typically ';' or ',' are used
253 * as separators. The default list separator is ';'.
258 g_key_file_set_list_separator (GKeyFile *key_file,
261 key_file->list_separator = separator;
265 /* Iterates through all the directories in *dirs trying to
266 * open file. When it successfully locates and opens a file it
267 * returns the file descriptor to the open file. It also
268 * outputs the absolute path of the file in output_file and
269 * leaves the unchecked directories in *dirs.
272 find_file_in_data_dirs (const gchar *file,
277 gchar **data_dirs, *data_dir, *path;
290 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
292 gchar *candidate_file, *sub_dir;
294 candidate_file = (gchar *) file;
295 sub_dir = g_strdup ("");
296 while (candidate_file != NULL && fd < 0)
300 path = g_build_filename (data_dir, sub_dir,
301 candidate_file, NULL);
303 fd = open (path, O_RDONLY);
305 if (output_file != NULL)
306 *output_file = g_strdup (path);
310 if (fd < 0 && file_error == NULL)
311 file_error = g_error_new (G_KEY_FILE_ERROR,
312 G_KEY_FILE_ERROR_NOT_FOUND,
313 _("Valid key file could not be "
314 "found in data dirs"));
316 candidate_file = strchr (candidate_file, '-');
318 if (candidate_file == NULL)
324 sub_dir = g_strndup (file, candidate_file - file - 1);
326 for (p = sub_dir; *p != '\0'; p++)
329 *p = G_DIR_SEPARATOR;
341 g_error_free (file_error);
343 g_propagate_error (error, file_error);
346 if (output_file && fd < 0)
348 g_free (*output_file);
356 g_key_file_load_from_fd (GKeyFile *key_file,
361 GError *key_file_error = NULL;
363 struct stat stat_buf;
364 gchar read_buf[4096];
366 if (key_file->approximate_size > 0)
368 g_key_file_clear (key_file);
369 g_key_file_init (key_file);
371 key_file->flags = flags;
373 fstat (fd, &stat_buf);
374 if (stat_buf.st_size == 0)
376 g_set_error (error, G_KEY_FILE_ERROR,
377 G_KEY_FILE_ERROR_PARSE,
385 bytes_read = read (fd, read_buf, 4096);
387 if (bytes_read == 0) /* End of File */
395 g_set_error (error, G_FILE_ERROR,
396 g_file_error_from_errno (errno),
397 _("Failed to read from file: %s"),
402 g_key_file_parse_data (key_file,
403 read_buf, bytes_read,
406 while (!key_file_error);
410 g_propagate_error (error, key_file_error);
414 g_key_file_flush_parse_buffer (key_file, &key_file_error);
418 g_propagate_error (error, key_file_error);
426 * g_key_file_load_from_file:
427 * @key_file: an empty #GKeyFile struct
428 * @file: the path of a filename to load
429 * @flags: flags from #GKeyFileFlags
430 * @error: return location for a #GError, or %NULL
432 * Loads a key file into an empty #GKeyFile structure.
433 * If the file could not be loaded then %error is set to
434 * either a #GFileError or #GKeyFileError.
436 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
440 g_key_file_load_from_file (GKeyFile *key_file,
445 GError *key_file_error = NULL;
448 g_return_val_if_fail (key_file != NULL, FALSE);
449 g_return_val_if_fail (file != NULL, FALSE);
451 if (!g_file_test (file, G_FILE_TEST_IS_REGULAR))
453 g_set_error (error, G_FILE_ERROR,
455 _("Failed to open file '%s': Not a regular file"),
460 fd = open (file, O_RDONLY);
464 g_set_error (error, G_FILE_ERROR,
465 g_file_error_from_errno (errno),
466 _("Failed to open file '%s': %s"),
467 file, g_strerror (errno));
471 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
476 g_propagate_error (error, key_file_error);
484 * g_key_file_load_from_data:
485 * @key_file: an empty #GKeyFile struct
486 * @data: key file loaded in memory.
487 * @length: the length of @data in bytes
488 * @flags: flags from #GKeyFileFlags
489 * @error: return location for a #GError, or %NULL
491 * Loads a key file from memory into an empty #GKeyFile structure.
492 * If the object cannot be created then %error is set to a #GKeyFileError.
494 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
498 g_key_file_load_from_data (GKeyFile *key_file,
504 GError *key_file_error = NULL;
506 g_return_val_if_fail (data != NULL, FALSE);
507 g_return_val_if_fail (length != 0, FALSE);
509 if (key_file->approximate_size > 0)
511 g_key_file_clear (key_file);
512 g_key_file_init (key_file);
514 key_file->flags = flags;
516 g_key_file_parse_data (key_file, data, length, &key_file_error);
520 g_propagate_error (error, key_file_error);
524 g_key_file_flush_parse_buffer (key_file, &key_file_error);
528 g_propagate_error (error, key_file_error);
536 * g_key_file_load_from_data_dirs:
537 * @key_file: an empty #GKeyFile struct
538 * @file: a relative path to a filename to open and parse
539 * @full_path: return location for a string containing the full path
540 * of the file, or %NULL
541 * @flags: flags from #GKeyFileFlags
542 * @error: return location for a #GError, or %NULL
544 * This function looks for a key file named @file in the paths
545 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
546 * loads the file into @key_file and returns the file's full path in
547 * @full_path. If the file could not be loaded then an %error is
548 * set to either a #GFileError or #GKeyFileError.
550 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
554 g_key_file_load_from_data_dirs (GKeyFile *key_file,
560 GError *key_file_error = NULL;
561 gchar **all_data_dirs, **data_dirs;
562 const gchar * user_data_dir;
563 const gchar * const * system_data_dirs;
569 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
571 user_data_dir = g_get_user_data_dir ();
572 system_data_dirs = g_get_system_data_dirs ();
573 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 1);
576 all_data_dirs[i++] = g_strdup (user_data_dir);
579 while (system_data_dirs[j] != NULL)
580 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
583 data_dirs = all_data_dirs;
584 while (*data_dirs != NULL && !found_file)
586 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
592 g_propagate_error (error, key_file_error);
596 found_file = g_key_file_load_from_fd (key_file, fd, flags,
602 g_propagate_error (error, key_file_error);
603 g_free (output_path);
608 *full_path = output_path;
611 g_strfreev (all_data_dirs);
617 * @key_file: a #GKeyFile
624 g_key_file_free (GKeyFile *key_file)
626 g_return_if_fail (key_file != NULL);
628 g_key_file_clear (key_file);
632 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
633 * true for locales that match those in g_get_language_names().
636 g_key_file_locale_is_interesting (GKeyFile *key_file,
639 const gchar * const * current_locales;
642 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
645 current_locales = g_get_language_names ();
647 for (i = 0; current_locales[i] != NULL; i++)
649 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
657 g_key_file_parse_line (GKeyFile *key_file,
662 GError *parse_error = NULL;
665 g_return_if_fail (key_file != NULL);
666 g_return_if_fail (line != NULL);
668 line_start = (gchar *) line;
669 while (g_ascii_isspace (*line_start))
672 if (g_key_file_line_is_comment (line_start))
673 g_key_file_parse_comment (key_file, line, length, &parse_error);
674 else if (g_key_file_line_is_group (line_start))
675 g_key_file_parse_group (key_file, line_start,
676 length - (line_start - line),
678 else if (g_key_file_line_is_key_value_pair (line_start))
679 g_key_file_parse_key_value_pair (key_file, line_start,
680 length - (line_start - line),
684 g_set_error (error, G_KEY_FILE_ERROR,
685 G_KEY_FILE_ERROR_PARSE,
686 _("Key file contains line '%s' which is not "
687 "a key-value pair, group, or comment"), line);
692 g_propagate_error (error, parse_error);
696 g_key_file_parse_comment (GKeyFile *key_file,
701 GKeyFileKeyValuePair *pair;
703 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
706 g_assert (key_file->current_group != NULL);
708 pair = g_new0 (GKeyFileKeyValuePair, 1);
711 pair->value = g_strndup (line, length);
713 key_file->current_group->key_value_pairs =
714 g_list_prepend (key_file->current_group->key_value_pairs, pair);
718 g_key_file_parse_group (GKeyFile *key_file,
724 const gchar *group_name_start, *group_name_end;
726 /* advance past opening '['
728 group_name_start = line + 1;
729 group_name_end = line + length - 1;
731 while (*group_name_end != ']')
734 group_name = g_strndup (group_name_start,
735 group_name_end - group_name_start);
737 if (key_file->start_group_name == NULL)
738 key_file->start_group_name = g_strdup (group_name);
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_name != 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->name, 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);
849 for (i = 0; i < length; i++)
853 /* When a newline is encountered flush the parse buffer so that the
854 * line can be parsed. Note that completely blank lines won't show
855 * up in the parse buffer, so they get parsed directly.
857 if (key_file->parse_buffer->len > 0)
858 g_key_file_flush_parse_buffer (key_file, &parse_error);
860 g_key_file_parse_comment (key_file, "", 1, &parse_error);
864 g_propagate_error (error, parse_error);
869 g_string_append_c (key_file->parse_buffer, data[i]);
872 key_file->approximate_size += length;
876 g_key_file_flush_parse_buffer (GKeyFile *key_file,
879 GError *file_error = NULL;
881 g_return_if_fail (key_file != NULL);
885 if (key_file->parse_buffer->len > 0)
887 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
888 key_file->parse_buffer->len,
890 g_string_erase (key_file->parse_buffer, 0, -1);
894 g_propagate_error (error, file_error);
901 * g_key_file_to_data:
902 * @key_file: a #GKeyFile
903 * @length: return location for the length of the
904 * returned string, or %NULL
905 * @error: return location for a #GError, or %NULL
907 * This function outputs @key_file as a string.
909 * Return value: a newly allocated string holding
910 * the contents of the #GKeyFile
915 g_key_file_to_data (GKeyFile *key_file,
919 GString *data_string;
921 GList *group_node, *key_file_node;
923 g_return_val_if_fail (key_file != NULL, NULL);
925 data_string = g_string_sized_new (2 * key_file->approximate_size);
927 for (group_node = g_list_last (key_file->groups);
929 group_node = group_node->prev)
931 GKeyFileGroup *group;
933 group = (GKeyFileGroup *) group_node->data;
935 if (group->comment != NULL)
936 g_string_append_printf (data_string, "%s\n", group->comment->value);
937 if (group->name != NULL)
938 g_string_append_printf (data_string, "[%s]\n", group->name);
940 for (key_file_node = g_list_last (group->key_value_pairs);
941 key_file_node != NULL;
942 key_file_node = key_file_node->prev)
944 GKeyFileKeyValuePair *pair;
946 pair = (GKeyFileKeyValuePair *) key_file_node->data;
948 if (pair->key != NULL)
949 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
951 g_string_append_printf (data_string, "%s\n", pair->value);
956 *length = data_string->len;
958 data = data_string->str;
960 g_string_free (data_string, FALSE);
966 * g_key_file_get_keys:
967 * @key_file: a #GKeyFile
968 * @group_name: a group name, or %NULL
969 * @length: return location for the number of keys returned, or %NULL
970 * @error: return location for a #GError, or %NULL
972 * Returns all keys for the group name @group_name. If @group_name is
973 * %NULL, the start group is used. The array of returned keys will be
974 * %NULL-terminated, so @length may optionally be %NULL.
976 * Return value: a newly-allocated %NULL-terminated array of
977 * strings. Use g_strfreev() to free it.
982 g_key_file_get_keys (GKeyFile *key_file,
983 const gchar *group_name,
987 GKeyFileGroup *group;
992 g_return_val_if_fail (key_file != NULL, NULL);
993 g_return_val_if_fail (group_name != NULL, NULL);
995 group = g_key_file_lookup_group (key_file, group_name);
999 g_set_error (error, G_KEY_FILE_ERROR,
1000 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1001 _("Key file does not have group '%s'"),
1006 num_keys = g_list_length (group->key_value_pairs);
1008 keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1010 tmp = group->key_value_pairs;
1011 for (i = 0; i < num_keys; i++)
1013 GKeyFileKeyValuePair *pair;
1015 pair = (GKeyFileKeyValuePair *) tmp->data;
1016 keys[i] = g_strdup (pair->key);
1029 * g_key_file_get_start_group:
1030 * @key_file: a #GKeyFile
1032 * Returns the name of the start group of the file.
1034 * Return value: The start group of the key file.
1039 g_key_file_get_start_group (GKeyFile *key_file)
1041 g_return_val_if_fail (key_file != NULL, NULL);
1043 return g_strdup (key_file->start_group_name);
1047 * g_key_file_get_groups:
1048 * @key_file: a #GKeyFile
1049 * @length: return location for the number of returned groups, or %NULL
1051 * Returns all groups in the key file loaded with @key_file. The
1052 * array of returned groups will be %NULL-terminated, so @length may
1053 * optionally be %NULL.
1055 * Return value: a newly-allocated %NULL-terminated array of strings.
1056 * Use g_strfreev() to free it.
1060 g_key_file_get_groups (GKeyFile *key_file,
1065 gsize i, num_groups;
1067 g_return_val_if_fail (key_file != NULL, NULL);
1069 num_groups = g_list_length (key_file->groups);
1071 g_assert (num_groups > 0);
1073 /* Only need num_groups instead of num_groups + 1
1074 * because the first group of the file (last in the
1075 * list) is always the comment group at the top,
1078 groups = (gchar **) g_new0 (gchar **, num_groups);
1080 group_node = g_list_last (key_file->groups);
1082 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1085 for (group_node = group_node->prev;
1087 group_node = group_node->prev)
1089 GKeyFileGroup *group;
1091 group = (GKeyFileGroup *) group_node->data;
1093 g_assert (group->name != NULL);
1095 groups[i++] = g_strdup (group->name);
1106 * g_key_file_get_value:
1107 * @key_file: a #GKeyFile
1108 * @group_name: a group name
1110 * @error: return location for a #GError, or #NULL
1112 * Returns the value associated with @key under @group_name.
1113 * In the event the key cannot be found, %NULL is returned and
1114 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1115 * event that the @group_name cannot be found, %NULL is returned
1116 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1118 * Return value: a string or %NULL if the specified key cannot be
1124 g_key_file_get_value (GKeyFile *key_file,
1125 const gchar *group_name,
1129 GKeyFileGroup *group;
1130 GKeyFileKeyValuePair *pair;
1131 gchar *value = NULL;
1133 g_return_val_if_fail (key_file != NULL, NULL);
1134 g_return_val_if_fail (group_name != NULL, NULL);
1136 group = g_key_file_lookup_group (key_file, group_name);
1140 g_set_error (error, G_KEY_FILE_ERROR,
1141 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1142 _("Key file does not have group '%s'"),
1147 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1150 value = g_strdup (pair->value);
1152 g_set_error (error, G_KEY_FILE_ERROR,
1153 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1154 _("Key file does not have key '%s'"), key);
1160 * g_key_file_set_value:
1161 * @key_file: a #GKeyFile
1162 * @group_name: a group name
1166 * Associates a new value with @key under @group_name.
1167 * If @key cannot be found then it is created. If @group_name
1168 * cannot be found then it is created as well.
1173 g_key_file_set_value (GKeyFile *key_file,
1174 const gchar *group_name,
1178 GKeyFileGroup *group;
1179 GKeyFileKeyValuePair *pair;
1181 g_return_if_fail (key_file != NULL);
1182 g_return_if_fail (key != NULL);
1183 g_return_if_fail (value != NULL);
1185 if (!g_key_file_has_key (key_file, group_name, key, NULL))
1186 g_key_file_add_key (key_file, group_name, key, value);
1189 group = g_key_file_lookup_group (key_file, group_name);
1190 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1191 g_free (pair->value);
1192 pair->value = g_strdup (value);
1197 * g_key_file_get_string:
1198 * @key_file: a #GKeyFile
1199 * @group_name: a group name
1201 * @error: return location for a #GError, or #NULL
1203 * Returns the value associated with @key under @group_name.
1204 * In the event the key cannot be found, %NULL is returned and
1205 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1206 * event that the @group_name cannot be found, %NULL is returned
1207 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1209 * Return value: a string or %NULL if the specified key cannot be
1215 g_key_file_get_string (GKeyFile *key_file,
1216 const gchar *group_name,
1220 gchar *value, *string_value;
1221 GError *key_file_error;
1223 g_return_val_if_fail (key_file != NULL, NULL);
1224 g_return_val_if_fail (key != NULL, NULL);
1226 key_file_error = NULL;
1228 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1232 g_propagate_error (error, key_file_error);
1236 if (!g_utf8_validate (value, -1, NULL))
1238 g_set_error (error, G_KEY_FILE_ERROR,
1239 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1240 _("Key file contains key '%s' with value '%s' "
1241 "which is not UTF-8"), key, value);
1246 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1252 if (g_error_matches (key_file_error,
1254 G_KEY_FILE_ERROR_INVALID_VALUE))
1256 g_set_error (error, G_KEY_FILE_ERROR,
1257 G_KEY_FILE_ERROR_INVALID_VALUE,
1258 _("Key file contains key '%s' "
1259 "which has value that cannot be interpreted."),
1261 g_error_free (key_file_error);
1264 g_propagate_error (error, key_file_error);
1267 return string_value;
1271 * g_key_file_set_string:
1272 * @key_file: a #GKeyFile
1273 * @group_name: a group name
1277 * Associates a new string value with @key under @group_name.
1278 * If @key cannot be found then it is created. If @group_name
1279 * cannot be found then it is created as well.
1284 g_key_file_set_string (GKeyFile *key_file,
1285 const gchar *group_name,
1287 const gchar *string)
1291 g_return_if_fail (key_file != NULL);
1292 g_return_if_fail (key != NULL);
1294 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1295 g_key_file_set_value (key_file, group_name, key, value);
1300 * g_key_file_get_string_list:
1301 * @key_file: a #GKeyFile
1302 * @group_name: a group name
1304 * @length: return location for the number of returned strings, or %NULL
1305 * @error: return location for a #GError, or %NULL
1307 * Returns the values associated with @key under @group_name.
1308 * In the event the key cannot be found, %NULL is returned and
1309 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1310 * event that the @group_name cannot be found, %NULL is returned
1311 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1313 * Return value: a %NULL-terminated string array or %NULL if the specified
1314 * key cannot be found. The array should be freed with g_strfreev().
1319 g_key_file_get_string_list (GKeyFile *key_file,
1320 const gchar *group_name,
1325 GError *key_file_error = NULL;
1326 gchar *value, *string_value, **values;
1328 GSList *p, *pieces = NULL;
1330 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1334 g_propagate_error (error, key_file_error);
1338 if (!g_utf8_validate (value, -1, NULL))
1340 g_set_error (error, G_KEY_FILE_ERROR,
1341 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1342 _("Key file contains key '%s' with value '%s' "
1343 "which is not UTF-8"), key, value);
1348 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1350 g_free (string_value);
1354 if (g_error_matches (key_file_error,
1356 G_KEY_FILE_ERROR_INVALID_VALUE))
1358 g_set_error (error, G_KEY_FILE_ERROR,
1359 G_KEY_FILE_ERROR_INVALID_VALUE,
1360 _("Key file contains key '%s' "
1361 "which has value that cannot be interpreted."),
1363 g_error_free (key_file_error);
1366 g_propagate_error (error, key_file_error);
1369 len = g_slist_length (pieces);
1370 values = g_new0 (gchar *, len + 1);
1371 for (p = pieces, i = 0; p; p = p->next)
1372 values[i++] = p->data;
1375 g_slist_free (pieces);
1384 * g_key_file_set_string_list:
1385 * @key_file: a #GKeyFile
1386 * @group_name: a group name
1388 * @list: an array of locale string values
1389 * @length: number of locale string values in @list
1391 * Associates a list of string values for @key under @group_name.
1392 * If the @key cannot be found then it is created.
1397 g_key_file_set_string_list (GKeyFile *key_file,
1398 const gchar *group_name,
1400 const gchar * const list[],
1403 GString *value_list;
1406 g_return_if_fail (key_file != NULL);
1407 g_return_if_fail (key != NULL);
1409 value_list = g_string_sized_new (length * 128);
1410 for (i = 0; list[i] != NULL && i < length; i++)
1414 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1415 g_string_append (value_list, value);
1416 g_string_append_c (value_list, key_file->list_separator);
1421 g_key_file_set_value (key_file, group_name, key, value_list->str);
1422 g_string_free (value_list, TRUE);
1426 * g_key_file_set_locale_string:
1427 * @key_file: a #GKeyFile
1428 * @group_name: a group name
1433 * Associates a string value for @key and @locale under
1434 * @group_name. If the translation for @key cannot be found
1435 * then it is created.
1440 g_key_file_set_locale_string (GKeyFile *key_file,
1441 const gchar *group_name,
1443 const gchar *locale,
1444 const gchar *string)
1446 gchar *full_key, *value;
1448 g_return_if_fail (key_file != NULL);
1449 g_return_if_fail (key != NULL);
1451 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1452 full_key = g_strdup_printf ("%s[%s]", key, locale);
1453 g_key_file_set_value (key_file, group_name, full_key, value);
1458 extern GSList *_g_compute_locale_variants (const gchar *locale);
1461 * g_key_file_get_locale_string:
1462 * @key_file: a #GKeyFile
1463 * @group_name: a group name
1465 * @locale: a locale or %NULL
1466 * @error: return location for a #GError, or %NULL
1468 * Returns the value associated with @key under @group_name
1469 * translated in the given @locale if available. If @locale is
1470 * %NULL then the current locale is assumed. If @key cannot be
1471 * found then %NULL is returned and @error is set to
1472 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1473 * with @key cannot be interpreted or no suitable translation can
1474 * be found then the untranslated value is returned and @error is
1475 * set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1476 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1477 * event that the @group_name cannot be found, %NULL is returned
1478 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1480 * Return value: a string or %NULL if the specified key cannot be
1485 g_key_file_get_locale_string (GKeyFile *key_file,
1486 const gchar *group_name,
1488 const gchar *locale,
1491 gchar *candidate_key, *translated_value;
1492 GError *key_file_error;
1494 gboolean free_languages = FALSE;
1497 candidate_key = NULL;
1498 translated_value = NULL;
1499 key_file_error = NULL;
1501 if (!g_key_file_has_group (key_file, group_name))
1503 g_set_error (error, G_KEY_FILE_ERROR,
1504 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1505 _("Key file does not have group '%s'"),
1514 list = _g_compute_locale_variants (locale);
1516 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1517 for (l = list, i = 0; l; l = l->next, i++)
1518 languages[i] = l->data;
1519 languages[i] = NULL;
1521 g_slist_free (list);
1522 free_languages = TRUE;
1526 languages = (gchar **) g_get_language_names ();
1527 free_languages = FALSE;
1530 for (i = 0; languages[i]; i++)
1532 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1534 translated_value = g_key_file_get_string (key_file,
1536 candidate_key, NULL);
1537 g_free (candidate_key);
1539 if (translated_value)
1543 if (translated_value && !g_utf8_validate (translated_value, -1, NULL))
1545 g_set_error (error, G_KEY_FILE_ERROR,
1546 G_KEY_FILE_ERROR_INVALID_VALUE,
1547 _("Key file contains key '%s' "
1548 "which has value that cannot be interpreted."),
1550 g_free (translated_value);
1551 translated_value = NULL;
1554 /* Fallback to untranslated key
1556 if (!translated_value)
1558 translated_value = g_key_file_get_string (key_file, group_name, key,
1561 if (!translated_value)
1562 g_propagate_error (error, key_file_error);
1564 g_set_error (error, G_KEY_FILE_ERROR,
1565 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1566 _("Key file contains no translated value "
1567 "for key '%s' with locale '%s'."),
1572 g_strfreev (languages);
1574 return translated_value;
1578 * g_key_file_get_locale_string_list:
1579 * @key_file: a #GKeyFile
1580 * @group_name: a group name
1583 * @length: return location for the number of returned strings or %NULL
1584 * @error: return location for a #GError or %NULL
1586 * Returns the values associated with @key under @group_name
1587 * translated in the given @locale if available. If @locale is
1588 * %NULL then the current locale is assumed. If @key cannot be
1589 * found then %NULL is returned and @error is set to
1590 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1591 * with @key cannot be interpreted or no suitable translations
1592 * can be found then the untranslated values are returned and
1593 * @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1594 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1595 * event that the @group_name cannot be found, %NULL is returned
1596 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1597 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1599 * Return value: a newly allocated %NULL-terminated string array
1600 * or %NULL if the key isn't found. The string array should be freed
1601 * with g_strfreev().
1606 g_key_file_get_locale_string_list (GKeyFile *key_file,
1607 const gchar *group_name,
1609 const gchar *locale,
1613 GError *key_file_error;
1614 gchar **values, *value;
1616 key_file_error = NULL;
1618 value = g_key_file_get_locale_string (key_file, group_name,
1623 g_propagate_error (error, key_file_error);
1628 if (value[strlen (value) - 1] == ';')
1629 value[strlen (value) - 1] = '\0';
1631 values = g_strsplit (value, ";", 0);
1636 *length = g_strv_length (values);
1642 * g_key_file_set_locale_string_list:
1643 * @key_file: a #GKeyFile
1644 * @group_name: a group name
1647 * @list: a %NULL-terminated array of locale string values
1648 * @length: the length of @list
1650 * Associates a list of string values for @key and @locale under
1651 * @group_name. If the translation for @key cannot be found then
1657 g_key_file_set_locale_string_list (GKeyFile *key_file,
1658 const gchar *group_name,
1660 const gchar *locale,
1661 const gchar * const list[],
1664 GString *value_list;
1668 g_return_if_fail (key_file != NULL);
1669 g_return_if_fail (key != NULL);
1671 value_list = g_string_sized_new (length * 128);
1672 for (i = 0; list[i] != NULL && i < length; i++)
1676 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1678 g_string_append (value_list, value);
1679 g_string_append_c (value_list, ';');
1684 full_key = g_strdup_printf ("%s[%s]", key, locale);
1685 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1687 g_string_free (value_list, TRUE);
1691 * g_key_file_get_boolean:
1692 * @key_file: a #GKeyFile
1693 * @group_name: a group name
1695 * @error: return location for a #GError
1697 * Returns the value associated with @key under @group_name as a
1698 * boolean. If @key cannot be found then the return value is
1699 * undefined and @error is set to
1700 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1701 * associated with @key cannot be interpreted as a boolean then
1702 * the return value is also undefined and @error is set to
1703 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1705 * Return value: the value associated with the key as a boolean
1709 g_key_file_get_boolean (GKeyFile *key_file,
1710 const gchar *group_name,
1714 GError *key_file_error = NULL;
1716 gboolean bool_value;
1718 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1722 g_propagate_error (error, key_file_error);
1726 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1732 if (g_error_matches (key_file_error,
1734 G_KEY_FILE_ERROR_INVALID_VALUE))
1736 g_set_error (error, G_KEY_FILE_ERROR,
1737 G_KEY_FILE_ERROR_INVALID_VALUE,
1738 _("Key file contains key '%s' "
1739 "which has value that cannot be interpreted."),
1741 g_error_free (key_file_error);
1744 g_propagate_error (error, key_file_error);
1751 * g_key_file_set_boolean:
1752 * @key_file: a #GKeyFile
1753 * @group_name: a group name
1755 * @value: %TRUE or %FALSE
1757 * Associates a new boolean value with @key under @group_name.
1758 * If @key cannot be found then it is created.
1763 g_key_file_set_boolean (GKeyFile *key_file,
1764 const gchar *group_name,
1770 g_return_if_fail (key_file != NULL);
1771 g_return_if_fail (key != NULL);
1773 result = g_key_file_parse_boolean_as_value (key_file, value);
1774 g_key_file_set_value (key_file, group_name, key, result);
1779 * g_key_file_get_boolean_list:
1780 * @key_file: a #GKeyFile
1781 * @group_name: a group name
1783 * @length: the number of booleans returned
1784 * @error: return location for a #GError
1786 * Returns the values associated with @key under @group_name as
1787 * booleans. If @key cannot be found then the return value is
1788 * undefined and @error is set to
1789 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
1790 * associated with @key cannot be interpreted as booleans then
1791 * the return value is also undefined and @error is set to
1792 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1794 * Return value: the values associated with the key as a boolean
1797 g_key_file_get_boolean_list (GKeyFile *key_file,
1798 const gchar *group_name,
1803 GError *key_file_error;
1805 gboolean *bool_values;
1808 key_file_error = NULL;
1810 values = g_key_file_get_string_list (key_file, group_name, key,
1811 &num_bools, &key_file_error);
1814 g_propagate_error (error, key_file_error);
1819 bool_values = g_new0 (gboolean, num_bools);
1821 for (i = 0; i < num_bools; i++)
1823 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1829 g_propagate_error (error, key_file_error);
1830 g_strfreev (values);
1831 g_free (bool_values);
1836 g_strfreev (values);
1839 *length = num_bools;
1845 * g_key_file_set_boolean_list:
1846 * @key_file: a #GKeyFile
1847 * @group_name: a group name
1849 * @list: an array of boolean values
1850 * @length: length of @list
1852 * Associates a list of boolean values with @key under
1853 * @group_name. If @key cannot be found then it is created.
1858 g_key_file_set_boolean_list (GKeyFile *key_file,
1859 const gchar *group_name,
1864 GString *value_list;
1867 g_return_if_fail (key_file != NULL);
1868 g_return_if_fail (key != NULL);
1870 value_list = g_string_sized_new (length * 8);
1871 for (i = 0; i < length; i++)
1875 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1877 g_string_append (value_list, value);
1878 g_string_append_c (value_list, key_file->list_separator);
1883 g_key_file_set_value (key_file, group_name, key, value_list->str);
1884 g_string_free (value_list, TRUE);
1888 * g_key_file_get_integer:
1889 * @key_file: a #GKeyFile
1890 * @group_name: a group name
1892 * @error: return location for a #GError
1894 * Returns the value associated with @key under @group_name as an
1895 * integer. If @key cannot be found then the return value is
1896 * undefined and @error is set to
1897 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1898 * associated with @key cannot be interpreted as an integer then
1899 * the return value is also undefined and @error is set to
1900 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1902 * Return value: the value associated with the key as an integer.
1906 g_key_file_get_integer (GKeyFile *key_file,
1907 const gchar *group_name,
1911 GError *key_file_error;
1915 key_file_error = NULL;
1917 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1921 g_propagate_error (error, key_file_error);
1925 int_value = g_key_file_parse_value_as_integer (key_file, value,
1931 if (g_error_matches (key_file_error,
1933 G_KEY_FILE_ERROR_INVALID_VALUE))
1935 g_set_error (error, G_KEY_FILE_ERROR,
1936 G_KEY_FILE_ERROR_INVALID_VALUE,
1937 _("Key file contains key '%s' in group '%s' "
1938 "which has value that cannot be interpreted."), key,
1940 g_error_free (key_file_error);
1943 g_propagate_error (error, key_file_error);
1950 * g_key_file_set_integer:
1951 * @key_file: a #GKeyFile
1952 * @group_name: a group name
1954 * @value: an integer value
1956 * Associates a new integer value with @key under @group_name.
1957 * If @key cannot be found then it is created.
1962 g_key_file_set_integer (GKeyFile *key_file,
1963 const gchar *group_name,
1969 g_return_if_fail (key_file != NULL);
1970 g_return_if_fail (key != NULL);
1972 result = g_key_file_parse_integer_as_value (key_file, value);
1973 g_key_file_set_value (key_file, group_name, key, result);
1978 * g_key_file_get_integer_list:
1979 * @key_file: a #GKeyFile
1980 * @group_name: a group name
1982 * @length: the number of integers returned
1983 * @error: return location for a #GError
1985 * Returns the values associated with @key under @group_name as
1986 * integers. If @key cannot be found then the return value is
1987 * undefined and @error is set to
1988 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
1989 * associated with @key cannot be interpreted as integers then
1990 * the return value is also undefined and @error is set to
1991 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1993 * Return value: the values associated with the key as a integer
1997 g_key_file_get_integer_list (GKeyFile *key_file,
1998 const gchar *group_name,
2003 GError *key_file_error = NULL;
2008 values = g_key_file_get_string_list (key_file, group_name, key,
2009 &num_ints, &key_file_error);
2012 g_propagate_error (error, key_file_error);
2017 int_values = g_new0 (gint, num_ints);
2019 for (i = 0; i < num_ints; i++)
2021 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2027 g_propagate_error (error, key_file_error);
2028 g_strfreev (values);
2029 g_free (int_values);
2034 g_strfreev (values);
2043 * g_key_file_set_integer_list:
2044 * @key_file: a #GKeyFile
2045 * @group_name: a group name
2047 * @list: an array of integer values
2048 * @length: number of integer values in @list
2050 * Associates a list of integer values with @key under
2051 * @group_name. If @key cannot be found then it is created.
2056 g_key_file_set_integer_list (GKeyFile *key_file,
2057 const gchar *group_name,
2065 g_return_if_fail (key_file != NULL);
2066 g_return_if_fail (key != NULL);
2068 values = g_string_sized_new (length * 16);
2069 for (i = 0; i < length; i++)
2073 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2075 g_string_append (values, value);
2076 g_string_append_c (values, ';');
2081 g_key_file_set_value (key_file, group_name, key, values->str);
2082 g_string_free (values, TRUE);
2086 g_key_file_set_key_comment (GKeyFile *key_file,
2087 const gchar *group_name,
2089 const gchar *comment,
2092 GKeyFileGroup *group;
2093 GKeyFileKeyValuePair *pair;
2094 GList *key_node, *comment_node, *tmp;
2096 group = g_key_file_lookup_group (key_file, group_name);
2099 g_set_error (error, G_KEY_FILE_ERROR,
2100 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2101 _("Key file does not have group '%s'"),
2107 /* First find the key the comments are supposed to be
2110 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2112 if (key_node == NULL)
2114 g_set_error (error, G_KEY_FILE_ERROR,
2115 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2116 _("Key file does not have key '%s' in group '%s'"),
2121 /* Then find all the comments already associated with the
2124 tmp = key_node->next;
2127 GKeyFileKeyValuePair *pair;
2129 pair = (GKeyFileKeyValuePair *) tmp->data;
2131 if (pair->key != NULL)
2136 g_key_file_remove_key_value_pair_node (key_file, group,
2140 if (comment == NULL)
2143 /* Now we can add our new comment
2145 pair = g_new0 (GKeyFileKeyValuePair, 1);
2148 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2150 key_node = g_list_insert (key_node, pair, 1);
2154 g_key_file_set_group_comment (GKeyFile *key_file,
2155 const gchar *group_name,
2156 const gchar *comment,
2159 GKeyFileGroup *group;
2161 group = g_key_file_lookup_group (key_file, group_name);
2164 g_set_error (error, G_KEY_FILE_ERROR,
2165 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2166 _("Key file does not have group '%s'"),
2172 /* First remove any existing comment
2176 g_key_file_key_value_pair_free (group->comment);
2177 group->comment = NULL;
2180 if (comment == NULL)
2183 /* Now we can add our new comment
2185 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2187 group->comment->key = NULL;
2188 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2192 g_key_file_set_top_comment (GKeyFile *key_file,
2193 const gchar *comment,
2197 GKeyFileGroup *group;
2198 GKeyFileKeyValuePair *pair;
2200 /* The last group in the list should be the top (comments only)
2203 g_assert (key_file->groups != NULL);
2204 group_node = g_list_last (key_file->groups);
2205 group = (GKeyFileGroup *) group_node->data;
2206 g_assert (group->name == NULL);
2208 /* Note all keys must be comments at the top of
2209 * the file, so we can just free it all.
2211 if (group->key_value_pairs != NULL)
2213 g_list_foreach (group->key_value_pairs,
2214 (GFunc) g_key_file_key_value_pair_free,
2216 g_list_free (group->key_value_pairs);
2217 group->key_value_pairs = NULL;
2220 if (comment == NULL)
2223 pair = g_new0 (GKeyFileKeyValuePair, 1);
2226 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2228 group->key_value_pairs =
2229 g_list_prepend (group->key_value_pairs, pair);
2233 * g_key_file_set_comment:
2234 * @key_file: a #GKeyFile
2235 * @group_name: a group name
2237 * @comment: a comment
2238 * @error: return location for a #GError
2240 * Places a comment above @key from @group_name.
2241 * @group_name. If @key is %NULL then @comment will
2242 * be written above @group_name. If both @key
2243 * and @group_name are NULL, then @comment will
2244 * be written above the first group in the file.
2249 g_key_file_set_comment (GKeyFile *key_file,
2250 const gchar *group_name,
2252 const gchar *comment,
2255 g_return_if_fail (key_file != NULL);
2257 if (group_name != NULL && key != NULL)
2258 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2259 else if (group_name != NULL)
2260 g_key_file_set_group_comment (key_file, group_name, comment, error);
2262 g_key_file_set_top_comment (key_file, comment, error);
2264 if (comment != NULL)
2265 key_file->approximate_size += strlen (comment);
2269 g_key_file_get_key_comment (GKeyFile *key_file,
2270 const gchar *group_name,
2274 GKeyFileGroup *group;
2275 GList *key_node, *tmp;
2279 group = g_key_file_lookup_group (key_file, group_name);
2282 g_set_error (error, G_KEY_FILE_ERROR,
2283 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2284 _("Key file does not have group '%s'"),
2290 /* First find the key the comments are supposed to be
2293 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2295 if (key_node == NULL)
2297 g_set_error (error, G_KEY_FILE_ERROR,
2298 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2299 _("Key file does not have key '%s' in group '%s'"),
2306 /* Then find all the comments already associated with the
2307 * key and concatentate them.
2309 tmp = key_node->next;
2312 GKeyFileKeyValuePair *pair;
2314 pair = (GKeyFileKeyValuePair *) tmp->data;
2316 if (pair->key != NULL)
2320 string = g_string_sized_new (512);
2322 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2323 g_string_append (string, comment);
2331 comment = string->str;
2332 g_string_free (string, FALSE);
2341 g_key_file_get_group_comment (GKeyFile *key_file,
2342 const gchar *group_name,
2345 GKeyFileGroup *group;
2347 group = g_key_file_lookup_group (key_file, group_name);
2350 g_set_error (error, G_KEY_FILE_ERROR,
2351 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2352 _("Key file does not have group '%s'"),
2359 return g_strdup (group->comment->value);
2365 g_key_file_get_top_comment (GKeyFile *key_file,
2368 GList *group_node, *tmp;
2369 GKeyFileGroup *group;
2373 /* The last group in the list should be the top (comments only)
2376 g_assert (key_file->groups != NULL);
2377 group_node = g_list_last (key_file->groups);
2378 group = (GKeyFileGroup *) group_node->data;
2379 g_assert (group->name == NULL);
2383 /* Then find all the comments already associated with the
2384 * key and concatentate them.
2386 tmp = group->key_value_pairs;
2389 GKeyFileKeyValuePair *pair;
2391 pair = (GKeyFileKeyValuePair *) tmp->data;
2393 if (pair->key != NULL)
2397 string = g_string_sized_new (512);
2399 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2400 g_string_append (string, comment);
2408 comment = string->str;
2409 g_string_free (string, FALSE);
2418 * g_key_file_get_comment:
2419 * @key_file: a #GKeyFile
2420 * @group_name: a group name
2422 * @error: return location for a #GError
2424 * Retreives a comment above @key from @group_name.
2425 * @group_name. If @key is %NULL then @comment will
2426 * be read from above @group_name. If both @key
2427 * and @group_name are NULL, then @comment will
2428 * be read from above the first group in the file.
2431 * Returns: a comment that should be freed with g_free()
2434 g_key_file_get_comment (GKeyFile *key_file,
2435 const gchar *group_name,
2439 g_return_val_if_fail (key_file != NULL, NULL);
2441 if (group_name != NULL && key != NULL)
2442 return g_key_file_get_key_comment (key_file, group_name, key, error);
2443 else if (group_name != NULL)
2444 return g_key_file_get_group_comment (key_file, group_name, error);
2446 return g_key_file_get_top_comment (key_file, error);
2450 * g_key_file_remove_comment:
2451 * @key_file: a #GKeyFile
2452 * @group_name: a group name
2454 * @error: return location for a #GError
2456 * Removes a comment above @key from @group_name.
2457 * @group_name. If @key is %NULL then @comment will
2458 * be written above @group_name. If both @key
2459 * and @group_name are NULL, then @comment will
2460 * be written above the first group in the file.
2466 g_key_file_remove_comment (GKeyFile *key_file,
2467 const gchar *group_name,
2471 g_return_if_fail (key_file != NULL);
2473 if (group_name != NULL && key != NULL)
2474 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2475 else if (group_name != NULL)
2476 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2478 g_key_file_set_top_comment (key_file, NULL, error);
2482 * g_key_file_has_group:
2483 * @key_file: a #GKeyFile
2484 * @group_name: a group name
2486 * Looks whether the key file has the group @group_name.
2488 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2493 g_key_file_has_group (GKeyFile *key_file,
2494 const gchar *group_name)
2497 GKeyFileGroup *group;
2499 g_return_val_if_fail (key_file != NULL, FALSE);
2500 g_return_val_if_fail (group_name != NULL, FALSE);
2502 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2504 group = (GKeyFileGroup *) tmp->data;
2505 if (group && group->name && (strcmp (group->name, group_name) == 0))
2513 * g_key_file_has_key:
2514 * @key_file: a #GKeyFile
2515 * @group_name: a group name
2517 * @error: return location for a #GError
2519 * Looks whether the key file has the key @key in the group
2522 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2527 g_key_file_has_key (GKeyFile *key_file,
2528 const gchar *group_name,
2532 GKeyFileKeyValuePair *pair;
2533 GKeyFileGroup *group;
2535 g_return_val_if_fail (key_file != NULL, FALSE);
2536 g_return_val_if_fail (key != NULL, FALSE);
2537 g_return_val_if_fail (group_name != NULL, FALSE);
2539 group = g_key_file_lookup_group (key_file, group_name);
2543 g_set_error (error, G_KEY_FILE_ERROR,
2544 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2545 _("Key file does not have group '%s'"),
2551 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2553 return pair != NULL;
2557 g_key_file_add_group (GKeyFile *key_file,
2558 const gchar *group_name)
2560 GKeyFileGroup *group;
2562 g_return_if_fail (key_file != NULL);
2563 g_return_if_fail (group_name != NULL);
2564 g_return_if_fail (g_key_file_lookup_group (key_file, group_name) == NULL);
2566 group = g_new0 (GKeyFileGroup, 1);
2567 group->name = g_strdup (group_name);
2568 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2569 key_file->groups = g_list_prepend (key_file->groups, group);
2570 key_file->approximate_size += strlen (group_name) + 3;
2571 key_file->current_group = group;
2573 if (key_file->start_group_name == NULL)
2574 key_file->start_group_name = g_strdup (group_name);
2578 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2583 g_free (pair->value);
2588 /* Be careful not to call this function on a node with data in the
2589 * lookup map without removing it from the lookup map, first.
2591 * Some current cases where this warning is not a concern are
2593 * - the node being removed is a comment node
2594 * - the entire lookup map is getting destroyed soon after
2598 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2599 GKeyFileGroup *group,
2603 GKeyFileKeyValuePair *pair;
2605 pair = (GKeyFileKeyValuePair *) pair_node->data;
2607 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2609 if (pair->key != NULL)
2610 key_file->approximate_size -= strlen (pair->key) + 1;
2612 g_assert (pair->value != NULL);
2613 key_file->approximate_size -= strlen (pair->value);
2615 g_key_file_key_value_pair_free (pair);
2617 g_list_free_1 (pair_node);
2621 g_key_file_remove_group_node (GKeyFile *key_file,
2624 GKeyFileGroup *group;
2627 group = (GKeyFileGroup *) group_node->data;
2629 /* If the current group gets deleted make the current group the first
2632 if (key_file->current_group == group)
2634 /* groups should always contain at least the top comment group,
2635 * unless g_key_file_clear has been called
2637 if (key_file->groups)
2638 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2640 key_file->current_group = NULL;
2643 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2645 if (group->name != NULL)
2646 key_file->approximate_size -= strlen (group->name) + 3;
2648 tmp = group->key_value_pairs;
2655 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2658 g_assert (group->key_value_pairs == NULL);
2660 if (group->lookup_map)
2662 g_hash_table_destroy (group->lookup_map);
2663 group->lookup_map = NULL;
2666 g_free ((gchar *) group->name);
2668 g_list_free_1 (group_node);
2672 * g_key_file_remove_group:
2673 * @key_file: a #GKeyFile
2674 * @group_name: a group name
2675 * @error: return location for a #GError or %NULL
2677 * Removes the specified group, @group_name,
2678 * from the key file.
2683 g_key_file_remove_group (GKeyFile *key_file,
2684 const gchar *group_name,
2689 g_return_if_fail (key_file != NULL);
2690 g_return_if_fail (group_name != NULL);
2692 group_node = g_key_file_lookup_group_node (key_file, group_name);
2695 g_set_error (error, G_KEY_FILE_ERROR,
2696 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2697 _("Key file does not have group '%s'"),
2700 g_key_file_remove_group_node (key_file, group_node);
2704 g_key_file_add_key (GKeyFile *key_file,
2705 const gchar *group_name,
2709 GKeyFileGroup *group;
2710 GKeyFileKeyValuePair *pair;
2712 group = g_key_file_lookup_group (key_file, group_name);
2716 g_key_file_add_group (key_file, group_name);
2717 group = (GKeyFileGroup *) key_file->groups->data;
2720 pair = g_new0 (GKeyFileKeyValuePair, 1);
2722 pair->key = g_strdup (key);
2723 pair->value = g_strdup (value);
2725 g_hash_table_replace (group->lookup_map, pair->key, pair);
2726 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2727 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2731 * g_key_file_remove_key:
2732 * @key_file: a #GKeyFile
2733 * @group_name: a group name
2734 * @key: a key name to remove
2735 * @error: return location for a #GError or %NULL
2737 * Removes @key in @group_name from the key file.
2742 g_key_file_remove_key (GKeyFile *key_file,
2743 const gchar *group_name,
2747 GKeyFileGroup *group;
2748 GKeyFileKeyValuePair *pair;
2752 if (group_name == NULL)
2753 group = key_file->current_group;
2755 group = g_key_file_lookup_group (key_file, group_name);
2759 g_set_error (error, G_KEY_FILE_ERROR,
2760 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2761 _("Key file does not have group '%s'"),
2766 group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2767 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2771 g_set_error (error, G_KEY_FILE_ERROR,
2772 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2773 _("Key file does not have key '%s' in group '%s'"), key, group_name);
2777 g_hash_table_remove (group->lookup_map, pair->key);
2779 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2780 g_key_file_key_value_pair_free (pair);
2784 g_key_file_lookup_group_node (GKeyFile *key_file,
2785 const gchar *group_name)
2787 GKeyFileGroup *group;
2791 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2793 group = (GKeyFileGroup *) tmp->data;
2795 if (group && group->name && strcmp (group->name, group_name) == 0)
2804 static GKeyFileGroup *
2805 g_key_file_lookup_group (GKeyFile *key_file,
2806 const gchar *group_name)
2810 group_node = g_key_file_lookup_group_node (key_file, group_name);
2812 if (group_node != NULL)
2813 return (GKeyFileGroup *) group_node->data;
2819 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2820 GKeyFileGroup *group,
2825 for (key_node = group->key_value_pairs;
2827 key_node = key_node->next)
2829 GKeyFileKeyValuePair *pair;
2831 pair = (GKeyFileKeyValuePair *) key_node->data;
2833 if (pair->key && strcmp (pair->key, key) == 0)
2840 static GKeyFileKeyValuePair *
2841 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2842 GKeyFileGroup *group,
2845 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2848 /* Lines starting with # or consisting entirely of whitespace are merely
2849 * recorded, not parsed. This function assumes all leading whitespace
2850 * has been stripped.
2853 g_key_file_line_is_comment (const gchar *line)
2855 return (*line == '#' || *line == '\0' || *line == '\n');
2858 /* A group in a key file is made up of a starting '[' followed by one
2859 * or more letters making up the group name followed by ']'.
2862 g_key_file_line_is_group (const gchar *line)
2870 p = g_utf8_next_char (p);
2875 p = g_utf8_next_char (p);
2877 /* Group name must be non-empty
2882 while (*p && *p != ']')
2883 p = g_utf8_next_char (p);
2892 g_key_file_line_is_key_value_pair (const gchar *line)
2896 p = (gchar *) g_utf8_strchr (line, -1, '=');
2901 /* Key must be non-empty
2910 g_key_file_parse_value_as_string (GKeyFile *key_file,
2915 GError *parse_error = NULL;
2916 gchar *string_value, *p, *q0, *q;
2918 string_value = g_new0 (gchar, strlen (value) + 1);
2920 p = (gchar *) value;
2921 q0 = q = string_value;
2951 if (pieces && *p == key_file->list_separator)
2952 *q = key_file->list_separator;
2958 if (parse_error == NULL)
2966 g_set_error (error, G_KEY_FILE_ERROR,
2967 G_KEY_FILE_ERROR_INVALID_VALUE,
2968 _("Key file contains invalid escape "
2969 "sequence '%s'"), sequence);
2978 if (pieces && (*p == key_file->list_separator))
2980 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
2989 if (p[-1] == '\\' && error == NULL)
2990 g_set_error (error, G_KEY_FILE_ERROR,
2991 G_KEY_FILE_ERROR_INVALID_VALUE,
2992 _("Key file contains escape character at end of line"));
2998 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
2999 *pieces = g_slist_reverse (*pieces);
3002 return string_value;
3006 g_key_file_parse_string_as_value (GKeyFile *key_file,
3007 const gchar *string,
3008 gboolean escape_separator)
3010 gchar *value, *p, *q;
3013 length = strlen (string) + 1;
3015 /* Worst case would be that every character needs to be escaped.
3016 * In other words every character turns to two characters
3018 value = g_new0 (gchar, 2 * length);
3020 p = (gchar *) string;
3022 while (p < (string + length - 1))
3024 gchar escaped_character[3] = { '\\', 0, 0 };
3029 escaped_character[1] = 's';
3030 strcpy (q, escaped_character);
3034 escaped_character[1] = 'n';
3035 strcpy (q, escaped_character);
3039 escaped_character[1] = 't';
3040 strcpy (q, escaped_character);
3044 escaped_character[1] = 'r';
3045 strcpy (q, escaped_character);
3049 escaped_character[1] = '\\';
3050 strcpy (q, escaped_character);
3054 if (escape_separator && *p == key_file->list_separator)
3056 escaped_character[1] = key_file->list_separator;
3057 strcpy (q, escaped_character);
3075 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3079 gchar *end_of_valid_int;
3082 int_value = strtol (value, &end_of_valid_int, 0);
3084 if (*end_of_valid_int != '\0')
3085 g_set_error (error, G_KEY_FILE_ERROR,
3086 G_KEY_FILE_ERROR_INVALID_VALUE,
3087 _("Value '%s' cannot be interpreted as a number."), value);
3093 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3097 return g_strdup_printf ("%d", value);
3101 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3107 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3109 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3113 g_set_error (error, G_KEY_FILE_ERROR,
3114 G_KEY_FILE_ERROR_INVALID_VALUE,
3115 _("Value '%s' cannot be interpreted as a boolean."), value);
3121 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3125 return g_strdup ("true");
3127 return g_strdup ("false");
3131 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3135 gchar **lines, *comment;
3138 string = g_string_sized_new (512);
3140 lines = g_strsplit (value, "\n", 0);
3142 for (i = 0; lines[i] != NULL; i++)
3144 if (lines[i][0] != '#')
3145 g_string_append_printf (string, "%s\n", lines[i]);
3147 g_string_append_printf (string, "%s\n", lines[i] + 1);
3151 comment = string->str;
3153 g_string_free (string, FALSE);
3159 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3160 const gchar *comment)
3163 gchar **lines, *value;
3166 string = g_string_sized_new (512);
3168 lines = g_strsplit (comment, "\n", 0);
3170 for (i = 0; lines[i] != NULL; i++)
3171 g_string_append_printf (string, "#%s%s", lines[i],
3172 lines[i + 1] == NULL? "" : "\n");
3175 value = string->str;
3177 g_string_free (string, FALSE);