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 gchar *start_group_name;
67 GKeyFileGroup *current_group;
69 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
71 /* Used for sizing the output buffer during serialization
73 gsize approximate_size;
80 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
84 const gchar *name; /* NULL for above first group (which will be comments) */
86 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
88 GList *key_value_pairs;
90 /* Used in parallel with key_value_pairs for
91 * increased lookup performance
93 GHashTable *lookup_map;
96 struct _GKeyFileKeyValuePair
98 gchar *key; /* NULL for comments */
102 static gint find_file_in_data_dirs (const gchar *file,
106 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
110 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
111 const gchar *group_name);
112 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
113 const gchar *group_name);
115 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
116 GKeyFileGroup *group,
118 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
119 GKeyFileGroup *group,
122 static void g_key_file_remove_group_node (GKeyFile *entry,
124 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
125 GKeyFileGroup *group,
128 static void g_key_file_add_key (GKeyFile *entry,
129 const gchar *group_name,
132 static void g_key_file_add_group (GKeyFile *entry,
133 const gchar *group_name);
134 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
135 static gboolean g_key_file_line_is_comment (const gchar *line);
136 static gboolean g_key_file_line_is_group (const gchar *line);
137 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
138 static gchar *g_key_file_parse_value_as_string (GKeyFile *entry,
142 static gchar *g_key_file_parse_string_as_value (GKeyFile *entry,
144 gboolean escape_separator);
145 static gint g_key_file_parse_value_as_integer (GKeyFile *entry,
148 static gchar *g_key_file_parse_integer_as_value (GKeyFile *entry,
150 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *entry,
153 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *entry,
155 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
157 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
158 const gchar *comment);
159 static void g_key_file_parse_key_value_pair (GKeyFile *entry,
163 static void g_key_file_parse_comment (GKeyFile *entry,
167 static void g_key_file_parse_group (GKeyFile *entry,
171 static gchar *key_get_locale (const gchar *key);
172 static void g_key_file_parse_data (GKeyFile *entry,
176 static void g_key_file_flush_parse_buffer (GKeyFile *entry,
181 g_key_file_error_quark (void)
183 static GQuark error_quark = 0;
185 if (error_quark == 0)
186 error_quark = g_quark_from_static_string ("g-key-file-error-quark");
192 g_key_file_init (GKeyFile *key_file)
194 key_file->current_group = g_new0 (GKeyFileGroup, 1);
195 key_file->groups = g_list_prepend (NULL, key_file->current_group);
196 key_file->start_group_name = NULL;
197 key_file->parse_buffer = g_string_sized_new (128);
198 key_file->approximate_size = 0;
199 key_file->list_separator = ';';
204 g_key_file_clear (GKeyFile *key_file)
206 GList *tmp, *group_node;
208 if (key_file->parse_buffer)
209 g_string_free (key_file->parse_buffer, TRUE);
211 g_free (key_file->start_group_name);
213 tmp = key_file->groups;
218 g_key_file_remove_group_node (key_file, group_node);
221 g_assert (key_file->groups == NULL);
228 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
229 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
230 * read an existing key file.
232 * Return value: an empty #GKeyFile.
237 g_key_file_new (void)
241 key_file = g_new0 (GKeyFile, 1);
242 g_key_file_init (key_file);
248 * g_key_file_set_list_separator:
249 * @key_file: a #GKeyFile
250 * @separator: the separator
252 * Sets the character which is used to separate
253 * values in lists. Typically ';' or ',' are used
254 * as separators. The default list separator is ';'.
259 g_key_file_set_list_separator (GKeyFile *key_file,
262 key_file->list_separator = separator;
266 /* Iterates through all the directories in *dirs trying to
267 * open file. When it successfully locates and opens a file it
268 * returns the file descriptor to the open file. It also
269 * outputs the absolute path of the file in output_file and
270 * leaves the unchecked directories in *dirs.
273 find_file_in_data_dirs (const gchar *file,
278 gchar **data_dirs, *data_dir, *path;
291 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
293 gchar *candidate_file, *sub_dir;
295 candidate_file = (gchar *) file;
296 sub_dir = g_strdup ("");
297 while (candidate_file != NULL && fd < 0)
301 path = g_build_filename (data_dir, sub_dir,
302 candidate_file, NULL);
304 fd = g_open (path, O_RDONLY, 0);
306 if (output_file != NULL)
307 *output_file = g_strdup (path);
311 if (fd < 0 && file_error == NULL)
312 file_error = g_error_new (G_KEY_FILE_ERROR,
313 G_KEY_FILE_ERROR_NOT_FOUND,
314 _("Valid key file could not be "
315 "found in data dirs"));
317 candidate_file = strchr (candidate_file, '-');
319 if (candidate_file == NULL)
325 sub_dir = g_strndup (file, candidate_file - file - 1);
327 for (p = sub_dir; *p != '\0'; p++)
330 *p = G_DIR_SEPARATOR;
342 g_error_free (file_error);
344 g_propagate_error (error, file_error);
347 if (output_file && fd < 0)
349 g_free (*output_file);
357 g_key_file_load_from_fd (GKeyFile *key_file,
362 GError *key_file_error = NULL;
364 struct stat stat_buf;
365 gchar read_buf[4096];
367 fstat (fd, &stat_buf);
368 if (!S_ISREG (stat_buf.st_mode))
370 g_set_error (error, G_KEY_FILE_ERROR,
371 G_KEY_FILE_ERROR_PARSE,
372 _("Not a regular file"));
376 if (stat_buf.st_size == 0)
378 g_set_error (error, G_KEY_FILE_ERROR,
379 G_KEY_FILE_ERROR_PARSE,
384 if (key_file->approximate_size > 0)
386 g_key_file_clear (key_file);
387 g_key_file_init (key_file);
389 key_file->flags = flags;
394 bytes_read = read (fd, read_buf, 4096);
396 if (bytes_read == 0) /* End of File */
404 g_set_error (error, G_FILE_ERROR,
405 g_file_error_from_errno (errno),
406 "%s", g_strerror (errno));
410 g_key_file_parse_data (key_file,
411 read_buf, bytes_read,
414 while (!key_file_error);
418 g_propagate_error (error, key_file_error);
422 g_key_file_flush_parse_buffer (key_file, &key_file_error);
426 g_propagate_error (error, key_file_error);
434 * g_key_file_load_from_file:
435 * @key_file: an empty #GKeyFile struct
436 * @file: the path of a filename to load, in the GLib file name encoding
437 * @flags: flags from #GKeyFileFlags
438 * @error: return location for a #GError, or %NULL
440 * Loads a key file into an empty #GKeyFile structure.
441 * If the file could not be loaded then %error is set to
442 * either a #GFileError or #GKeyFileError.
444 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
448 g_key_file_load_from_file (GKeyFile *key_file,
453 GError *key_file_error = NULL;
456 g_return_val_if_fail (key_file != NULL, FALSE);
457 g_return_val_if_fail (file != NULL, FALSE);
459 fd = g_open (file, O_RDONLY, 0);
463 g_set_error (error, G_FILE_ERROR,
464 g_file_error_from_errno (errno),
465 "%s", g_strerror (errno));
469 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
474 g_propagate_error (error, key_file_error);
482 * g_key_file_load_from_data:
483 * @key_file: an empty #GKeyFile struct
484 * @data: key file loaded in memory.
485 * @length: the length of @data in bytes
486 * @flags: flags from #GKeyFileFlags
487 * @error: return location for a #GError, or %NULL
489 * Loads a key file from memory into an empty #GKeyFile structure.
490 * If the object cannot be created then %error is set to a #GKeyFileError.
492 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
496 g_key_file_load_from_data (GKeyFile *key_file,
502 GError *key_file_error = NULL;
504 g_return_val_if_fail (key_file != NULL, FALSE);
505 g_return_val_if_fail (data != NULL, FALSE);
506 g_return_val_if_fail (length != 0, FALSE);
508 if (key_file->approximate_size > 0)
510 g_key_file_clear (key_file);
511 g_key_file_init (key_file);
513 key_file->flags = flags;
515 g_key_file_parse_data (key_file, data, length, &key_file_error);
519 g_propagate_error (error, key_file_error);
523 g_key_file_flush_parse_buffer (key_file, &key_file_error);
527 g_propagate_error (error, key_file_error);
535 * g_key_file_load_from_data_dirs:
536 * @key_file: an empty #GKeyFile struct
537 * @file: a relative path to a filename to open and parse
538 * @full_path: return location for a string containing the full path
539 * of the file, or %NULL
540 * @flags: flags from #GKeyFileFlags
541 * @error: return location for a #GError, or %NULL
543 * This function looks for a key file named @file in the paths
544 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
545 * loads the file into @key_file and returns the file's full path in
546 * @full_path. If the file could not be loaded then an %error is
547 * set to either a #GFileError or #GKeyFileError.
549 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
553 g_key_file_load_from_data_dirs (GKeyFile *key_file,
559 GError *key_file_error = NULL;
560 gchar **all_data_dirs, **data_dirs;
561 const gchar * user_data_dir;
562 const gchar * const * system_data_dirs;
568 g_return_val_if_fail (key_file != NULL, FALSE);
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);
846 g_return_if_fail (data != NULL);
850 for (i = 0; i < length; i++)
854 /* When a newline is encountered flush the parse buffer so that the
855 * line can be parsed. Note that completely blank lines won't show
856 * up in the parse buffer, so they get parsed directly.
858 if (key_file->parse_buffer->len > 0)
859 g_key_file_flush_parse_buffer (key_file, &parse_error);
861 g_key_file_parse_comment (key_file, "", 1, &parse_error);
865 g_propagate_error (error, parse_error);
870 g_string_append_c (key_file->parse_buffer, data[i]);
873 key_file->approximate_size += length;
877 g_key_file_flush_parse_buffer (GKeyFile *key_file,
880 GError *file_error = NULL;
882 g_return_if_fail (key_file != NULL);
886 if (key_file->parse_buffer->len > 0)
888 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
889 key_file->parse_buffer->len,
891 g_string_erase (key_file->parse_buffer, 0, -1);
895 g_propagate_error (error, file_error);
902 * g_key_file_to_data:
903 * @key_file: a #GKeyFile
904 * @length: return location for the length of the
905 * returned string, or %NULL
906 * @error: return location for a #GError, or %NULL
908 * This function outputs @key_file as a string.
910 * Return value: a newly allocated string holding
911 * the contents of the #GKeyFile
916 g_key_file_to_data (GKeyFile *key_file,
920 GString *data_string;
922 GList *group_node, *key_file_node;
924 g_return_val_if_fail (key_file != NULL, NULL);
926 data_string = g_string_sized_new (2 * key_file->approximate_size);
928 for (group_node = g_list_last (key_file->groups);
930 group_node = group_node->prev)
932 GKeyFileGroup *group;
934 group = (GKeyFileGroup *) group_node->data;
936 if (group->comment != NULL)
937 g_string_append_printf (data_string, "%s\n", group->comment->value);
938 if (group->name != NULL)
939 g_string_append_printf (data_string, "[%s]\n", group->name);
941 for (key_file_node = g_list_last (group->key_value_pairs);
942 key_file_node != NULL;
943 key_file_node = key_file_node->prev)
945 GKeyFileKeyValuePair *pair;
947 pair = (GKeyFileKeyValuePair *) key_file_node->data;
949 if (pair->key != NULL)
950 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
952 g_string_append_printf (data_string, "%s\n", pair->value);
957 *length = data_string->len;
959 data = data_string->str;
961 g_string_free (data_string, FALSE);
967 * g_key_file_get_keys:
968 * @key_file: a #GKeyFile
969 * @group_name: a group name, or %NULL
970 * @length: return location for the number of keys returned, or %NULL
971 * @error: return location for a #GError, or %NULL
973 * Returns all keys for the group name @group_name. If @group_name is
974 * %NULL, the start group is used. The array of returned keys will be
975 * %NULL-terminated, so @length may optionally be %NULL.
977 * Return value: a newly-allocated %NULL-terminated array of
978 * strings. Use g_strfreev() to free it.
983 g_key_file_get_keys (GKeyFile *key_file,
984 const gchar *group_name,
988 GKeyFileGroup *group;
993 g_return_val_if_fail (key_file != NULL, NULL);
994 g_return_val_if_fail (group_name != NULL, NULL);
996 group = g_key_file_lookup_group (key_file, group_name);
1000 g_set_error (error, G_KEY_FILE_ERROR,
1001 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1002 _("Key file does not have group '%s'"),
1007 num_keys = g_list_length (group->key_value_pairs);
1009 keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1011 tmp = group->key_value_pairs;
1012 for (i = 0; i < num_keys; i++)
1014 GKeyFileKeyValuePair *pair;
1016 pair = (GKeyFileKeyValuePair *) tmp->data;
1017 keys[i] = g_strdup (pair->key);
1030 * g_key_file_get_start_group:
1031 * @key_file: a #GKeyFile
1033 * Returns the name of the start group of the file.
1035 * Return value: The start group of the key file.
1040 g_key_file_get_start_group (GKeyFile *key_file)
1042 g_return_val_if_fail (key_file != NULL, NULL);
1044 return g_strdup (key_file->start_group_name);
1048 * g_key_file_get_groups:
1049 * @key_file: a #GKeyFile
1050 * @length: return location for the number of returned groups, or %NULL
1052 * Returns all groups in the key file loaded with @key_file. The
1053 * array of returned groups will be %NULL-terminated, so @length may
1054 * optionally be %NULL.
1056 * Return value: a newly-allocated %NULL-terminated array of strings.
1057 * Use g_strfreev() to free it.
1061 g_key_file_get_groups (GKeyFile *key_file,
1066 gsize i, num_groups;
1068 g_return_val_if_fail (key_file != NULL, NULL);
1070 num_groups = g_list_length (key_file->groups);
1072 g_assert (num_groups > 0);
1074 /* Only need num_groups instead of num_groups + 1
1075 * because the first group of the file (last in the
1076 * list) is always the comment group at the top,
1079 groups = (gchar **) g_new0 (gchar **, num_groups);
1081 group_node = g_list_last (key_file->groups);
1083 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1086 for (group_node = group_node->prev;
1088 group_node = group_node->prev)
1090 GKeyFileGroup *group;
1092 group = (GKeyFileGroup *) group_node->data;
1094 g_assert (group->name != NULL);
1096 groups[i++] = g_strdup (group->name);
1107 * g_key_file_get_value:
1108 * @key_file: a #GKeyFile
1109 * @group_name: a group name
1111 * @error: return location for a #GError, or #NULL
1113 * Returns the value associated with @key under @group_name.
1114 * In the event the key cannot be found, %NULL is returned and
1115 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1116 * event that the @group_name cannot be found, %NULL is returned
1117 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1119 * Return value: a string or %NULL if the specified key cannot be
1125 g_key_file_get_value (GKeyFile *key_file,
1126 const gchar *group_name,
1130 GKeyFileGroup *group;
1131 GKeyFileKeyValuePair *pair;
1132 gchar *value = NULL;
1134 g_return_val_if_fail (key_file != NULL, NULL);
1135 g_return_val_if_fail (group_name != NULL, NULL);
1136 g_return_val_if_fail (key != NULL, NULL);
1138 group = g_key_file_lookup_group (key_file, group_name);
1142 g_set_error (error, G_KEY_FILE_ERROR,
1143 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1144 _("Key file does not have group '%s'"),
1149 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1152 value = g_strdup (pair->value);
1154 g_set_error (error, G_KEY_FILE_ERROR,
1155 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1156 _("Key file does not have key '%s'"), key);
1162 * g_key_file_set_value:
1163 * @key_file: a #GKeyFile
1164 * @group_name: a group name
1168 * Associates a new value with @key under @group_name.
1169 * If @key cannot be found then it is created. If @group_name
1170 * cannot be found then it is created as well.
1175 g_key_file_set_value (GKeyFile *key_file,
1176 const gchar *group_name,
1180 GKeyFileGroup *group;
1181 GKeyFileKeyValuePair *pair;
1183 g_return_if_fail (key_file != NULL);
1184 g_return_if_fail (key != NULL);
1185 g_return_if_fail (value != NULL);
1187 if (!g_key_file_has_key (key_file, group_name, key, NULL))
1188 g_key_file_add_key (key_file, group_name, key, value);
1191 group = g_key_file_lookup_group (key_file, group_name);
1192 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1193 g_free (pair->value);
1194 pair->value = g_strdup (value);
1199 * g_key_file_get_string:
1200 * @key_file: a #GKeyFile
1201 * @group_name: a group name
1203 * @error: return location for a #GError, or #NULL
1205 * Returns the value associated with @key under @group_name.
1206 * In the event the key cannot be found, %NULL is returned and
1207 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1208 * event that the @group_name cannot be found, %NULL is returned
1209 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1211 * Return value: a string or %NULL if the specified key cannot be
1217 g_key_file_get_string (GKeyFile *key_file,
1218 const gchar *group_name,
1222 gchar *value, *string_value;
1223 GError *key_file_error;
1225 g_return_val_if_fail (key_file != NULL, NULL);
1226 g_return_val_if_fail (group_name != NULL, NULL);
1227 g_return_val_if_fail (key != NULL, NULL);
1229 key_file_error = NULL;
1231 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1235 g_propagate_error (error, key_file_error);
1239 if (!g_utf8_validate (value, -1, NULL))
1241 g_set_error (error, G_KEY_FILE_ERROR,
1242 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1243 _("Key file contains key '%s' with value '%s' "
1244 "which is not UTF-8"), key, value);
1249 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1255 if (g_error_matches (key_file_error,
1257 G_KEY_FILE_ERROR_INVALID_VALUE))
1259 g_set_error (error, G_KEY_FILE_ERROR,
1260 G_KEY_FILE_ERROR_INVALID_VALUE,
1261 _("Key file contains key '%s' "
1262 "which has value that cannot be interpreted."),
1264 g_error_free (key_file_error);
1267 g_propagate_error (error, key_file_error);
1270 return string_value;
1274 * g_key_file_set_string:
1275 * @key_file: a #GKeyFile
1276 * @group_name: a group name
1280 * Associates a new string value with @key under @group_name.
1281 * If @key cannot be found then it is created. If @group_name
1282 * cannot be found then it is created as well.
1287 g_key_file_set_string (GKeyFile *key_file,
1288 const gchar *group_name,
1290 const gchar *string)
1294 g_return_if_fail (key_file != NULL);
1295 g_return_if_fail (group_name != NULL);
1296 g_return_if_fail (key != NULL);
1297 g_return_if_fail (string != NULL);
1299 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1300 g_key_file_set_value (key_file, group_name, key, value);
1305 * g_key_file_get_string_list:
1306 * @key_file: a #GKeyFile
1307 * @group_name: a group name
1309 * @length: return location for the number of returned strings, or %NULL
1310 * @error: return location for a #GError, or %NULL
1312 * Returns the values associated with @key under @group_name.
1313 * In the event the key cannot be found, %NULL is returned and
1314 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1315 * event that the @group_name cannot be found, %NULL is returned
1316 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1318 * Return value: a %NULL-terminated string array or %NULL if the specified
1319 * key cannot be found. The array should be freed with g_strfreev().
1324 g_key_file_get_string_list (GKeyFile *key_file,
1325 const gchar *group_name,
1330 GError *key_file_error = NULL;
1331 gchar *value, *string_value, **values;
1333 GSList *p, *pieces = NULL;
1335 g_return_val_if_fail (key_file != NULL, NULL);
1336 g_return_val_if_fail (group_name != NULL, NULL);
1337 g_return_val_if_fail (key != NULL, NULL);
1339 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1343 g_propagate_error (error, key_file_error);
1347 if (!g_utf8_validate (value, -1, NULL))
1349 g_set_error (error, G_KEY_FILE_ERROR,
1350 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1351 _("Key file contains key '%s' with value '%s' "
1352 "which is not UTF-8"), key, value);
1357 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1359 g_free (string_value);
1363 if (g_error_matches (key_file_error,
1365 G_KEY_FILE_ERROR_INVALID_VALUE))
1367 g_set_error (error, G_KEY_FILE_ERROR,
1368 G_KEY_FILE_ERROR_INVALID_VALUE,
1369 _("Key file contains key '%s' "
1370 "which has value that cannot be interpreted."),
1372 g_error_free (key_file_error);
1375 g_propagate_error (error, key_file_error);
1378 len = g_slist_length (pieces);
1379 values = g_new0 (gchar *, len + 1);
1380 for (p = pieces, i = 0; p; p = p->next)
1381 values[i++] = p->data;
1384 g_slist_free (pieces);
1393 * g_key_file_set_string_list:
1394 * @key_file: a #GKeyFile
1395 * @group_name: a group name
1397 * @list: an array of locale string values
1398 * @length: number of locale string values in @list
1400 * Associates a list of string values for @key under @group_name.
1401 * If the @key cannot be found then it is created.
1406 g_key_file_set_string_list (GKeyFile *key_file,
1407 const gchar *group_name,
1409 const gchar * const list[],
1412 GString *value_list;
1415 g_return_if_fail (key_file != NULL);
1416 g_return_if_fail (group_name != NULL);
1417 g_return_if_fail (key != NULL);
1418 g_return_if_fail (list != NULL);
1420 value_list = g_string_sized_new (length * 128);
1421 for (i = 0; list[i] != NULL && i < length; i++)
1425 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1426 g_string_append (value_list, value);
1427 g_string_append_c (value_list, key_file->list_separator);
1432 g_key_file_set_value (key_file, group_name, key, value_list->str);
1433 g_string_free (value_list, TRUE);
1437 * g_key_file_set_locale_string:
1438 * @key_file: a #GKeyFile
1439 * @group_name: a group name
1444 * Associates a string value for @key and @locale under
1445 * @group_name. If the translation for @key cannot be found
1446 * then it is created.
1451 g_key_file_set_locale_string (GKeyFile *key_file,
1452 const gchar *group_name,
1454 const gchar *locale,
1455 const gchar *string)
1457 gchar *full_key, *value;
1459 g_return_if_fail (key_file != NULL);
1460 g_return_if_fail (group_name != NULL);
1461 g_return_if_fail (key != NULL);
1462 g_return_if_fail (locale != NULL);
1463 g_return_if_fail (string != NULL);
1465 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1466 full_key = g_strdup_printf ("%s[%s]", key, locale);
1467 g_key_file_set_value (key_file, group_name, full_key, value);
1472 extern GSList *_g_compute_locale_variants (const gchar *locale);
1475 * g_key_file_get_locale_string:
1476 * @key_file: a #GKeyFile
1477 * @group_name: a group name
1479 * @locale: a locale or %NULL
1480 * @error: return location for a #GError, or %NULL
1482 * Returns the value associated with @key under @group_name
1483 * translated in the given @locale if available. If @locale is
1484 * %NULL then the current locale is assumed. If @key cannot be
1485 * found then %NULL is returned and @error is set to
1486 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1487 * with @key cannot be interpreted or no suitable translation can
1488 * be found then the untranslated value is returned and @error is
1489 * set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1490 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1491 * event that the @group_name cannot be found, %NULL is returned
1492 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1494 * Return value: a string or %NULL if the specified key cannot be
1499 g_key_file_get_locale_string (GKeyFile *key_file,
1500 const gchar *group_name,
1502 const gchar *locale,
1505 gchar *candidate_key, *translated_value;
1506 GError *key_file_error;
1508 gboolean free_languages = FALSE;
1511 g_return_val_if_fail (key_file != NULL, NULL);
1512 g_return_val_if_fail (group_name != NULL, NULL);
1513 g_return_val_if_fail (key != NULL, NULL);
1515 candidate_key = NULL;
1516 translated_value = NULL;
1517 key_file_error = NULL;
1519 if (!g_key_file_has_group (key_file, group_name))
1521 g_set_error (error, G_KEY_FILE_ERROR,
1522 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1523 _("Key file does not have group '%s'"),
1532 list = _g_compute_locale_variants (locale);
1534 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1535 for (l = list, i = 0; l; l = l->next, i++)
1536 languages[i] = l->data;
1537 languages[i] = NULL;
1539 g_slist_free (list);
1540 free_languages = TRUE;
1544 languages = (gchar **) g_get_language_names ();
1545 free_languages = FALSE;
1548 for (i = 0; languages[i]; i++)
1550 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1552 translated_value = g_key_file_get_string (key_file,
1554 candidate_key, NULL);
1555 g_free (candidate_key);
1557 if (translated_value)
1561 if (translated_value && !g_utf8_validate (translated_value, -1, NULL))
1563 g_set_error (error, G_KEY_FILE_ERROR,
1564 G_KEY_FILE_ERROR_INVALID_VALUE,
1565 _("Key file contains key '%s' "
1566 "which has value that cannot be interpreted."),
1568 g_free (translated_value);
1569 translated_value = NULL;
1572 /* Fallback to untranslated key
1574 if (!translated_value)
1576 translated_value = g_key_file_get_string (key_file, group_name, key,
1579 if (!translated_value)
1580 g_propagate_error (error, key_file_error);
1582 g_set_error (error, G_KEY_FILE_ERROR,
1583 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1584 _("Key file contains no translated value "
1585 "for key '%s' with locale '%s'."),
1590 g_strfreev (languages);
1592 return translated_value;
1596 * g_key_file_get_locale_string_list:
1597 * @key_file: a #GKeyFile
1598 * @group_name: a group name
1601 * @length: return location for the number of returned strings or %NULL
1602 * @error: return location for a #GError or %NULL
1604 * Returns the values associated with @key under @group_name
1605 * translated in the given @locale if available. If @locale is
1606 * %NULL then the current locale is assumed. If @key cannot be
1607 * found then %NULL is returned and @error is set to
1608 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1609 * with @key cannot be interpreted or no suitable translations
1610 * can be found then the untranslated values are returned and
1611 * @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1612 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1613 * event that the @group_name cannot be found, %NULL is returned
1614 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1615 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1617 * Return value: a newly allocated %NULL-terminated string array
1618 * or %NULL if the key isn't found. The string array should be freed
1619 * with g_strfreev().
1624 g_key_file_get_locale_string_list (GKeyFile *key_file,
1625 const gchar *group_name,
1627 const gchar *locale,
1631 GError *key_file_error;
1632 gchar **values, *value;
1634 g_return_val_if_fail (key_file != NULL, NULL);
1635 g_return_val_if_fail (group_name != NULL, NULL);
1636 g_return_val_if_fail (key != NULL, NULL);
1638 key_file_error = NULL;
1640 value = g_key_file_get_locale_string (key_file, group_name,
1645 g_propagate_error (error, key_file_error);
1650 if (value[strlen (value) - 1] == ';')
1651 value[strlen (value) - 1] = '\0';
1653 values = g_strsplit (value, ";", 0);
1658 *length = g_strv_length (values);
1664 * g_key_file_set_locale_string_list:
1665 * @key_file: a #GKeyFile
1666 * @group_name: a group name
1669 * @list: a %NULL-terminated array of locale string values
1670 * @length: the length of @list
1672 * Associates a list of string values for @key and @locale under
1673 * @group_name. If the translation for @key cannot be found then
1679 g_key_file_set_locale_string_list (GKeyFile *key_file,
1680 const gchar *group_name,
1682 const gchar *locale,
1683 const gchar * const list[],
1686 GString *value_list;
1690 g_return_if_fail (key_file != NULL);
1691 g_return_if_fail (group_name != NULL);
1692 g_return_if_fail (key != NULL);
1693 g_return_if_fail (locale != NULL);
1694 g_return_if_fail (length != 0);
1696 value_list = g_string_sized_new (length * 128);
1697 for (i = 0; list[i] != NULL && i < length; i++)
1701 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1703 g_string_append (value_list, value);
1704 g_string_append_c (value_list, ';');
1709 full_key = g_strdup_printf ("%s[%s]", key, locale);
1710 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1712 g_string_free (value_list, TRUE);
1716 * g_key_file_get_boolean:
1717 * @key_file: a #GKeyFile
1718 * @group_name: a group name
1720 * @error: return location for a #GError
1722 * Returns the value associated with @key under @group_name as a
1723 * boolean. If @key cannot be found then the return value is
1724 * undefined and @error is set to
1725 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1726 * associated with @key cannot be interpreted as a boolean then
1727 * the return value is also undefined and @error is set to
1728 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1730 * Return value: the value associated with the key as a boolean
1734 g_key_file_get_boolean (GKeyFile *key_file,
1735 const gchar *group_name,
1739 GError *key_file_error = NULL;
1741 gboolean bool_value;
1743 g_return_val_if_fail (key_file != NULL, FALSE);
1744 g_return_val_if_fail (group_name != NULL, FALSE);
1745 g_return_val_if_fail (key != NULL, FALSE);
1747 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1751 g_propagate_error (error, key_file_error);
1755 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1761 if (g_error_matches (key_file_error,
1763 G_KEY_FILE_ERROR_INVALID_VALUE))
1765 g_set_error (error, G_KEY_FILE_ERROR,
1766 G_KEY_FILE_ERROR_INVALID_VALUE,
1767 _("Key file contains key '%s' "
1768 "which has value that cannot be interpreted."),
1770 g_error_free (key_file_error);
1773 g_propagate_error (error, key_file_error);
1780 * g_key_file_set_boolean:
1781 * @key_file: a #GKeyFile
1782 * @group_name: a group name
1784 * @value: %TRUE or %FALSE
1786 * Associates a new boolean value with @key under @group_name.
1787 * If @key cannot be found then it is created.
1792 g_key_file_set_boolean (GKeyFile *key_file,
1793 const gchar *group_name,
1799 g_return_if_fail (key_file != NULL);
1800 g_return_if_fail (group_name != NULL);
1801 g_return_if_fail (key != NULL);
1803 result = g_key_file_parse_boolean_as_value (key_file, value);
1804 g_key_file_set_value (key_file, group_name, key, result);
1809 * g_key_file_get_boolean_list:
1810 * @key_file: a #GKeyFile
1811 * @group_name: a group name
1813 * @length: the number of booleans returned
1814 * @error: return location for a #GError
1816 * Returns the values associated with @key under @group_name as
1817 * booleans. If @key cannot be found then the return value is
1818 * undefined and @error is set to
1819 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
1820 * associated with @key cannot be interpreted as booleans then
1821 * the return value is also undefined and @error is set to
1822 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1824 * Return value: the values associated with the key as a boolean
1827 g_key_file_get_boolean_list (GKeyFile *key_file,
1828 const gchar *group_name,
1833 GError *key_file_error;
1835 gboolean *bool_values;
1838 g_return_val_if_fail (key_file != NULL, NULL);
1839 g_return_val_if_fail (group_name != NULL, NULL);
1840 g_return_val_if_fail (key != NULL, NULL);
1842 key_file_error = NULL;
1844 values = g_key_file_get_string_list (key_file, group_name, key,
1845 &num_bools, &key_file_error);
1848 g_propagate_error (error, key_file_error);
1853 bool_values = g_new0 (gboolean, num_bools);
1855 for (i = 0; i < num_bools; i++)
1857 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1863 g_propagate_error (error, key_file_error);
1864 g_strfreev (values);
1865 g_free (bool_values);
1870 g_strfreev (values);
1873 *length = num_bools;
1879 * g_key_file_set_boolean_list:
1880 * @key_file: a #GKeyFile
1881 * @group_name: a group name
1883 * @list: an array of boolean values
1884 * @length: length of @list
1886 * Associates a list of boolean values with @key under
1887 * @group_name. If @key cannot be found then it is created.
1892 g_key_file_set_boolean_list (GKeyFile *key_file,
1893 const gchar *group_name,
1898 GString *value_list;
1901 g_return_if_fail (key_file != NULL);
1902 g_return_if_fail (group_name != NULL);
1903 g_return_if_fail (key != NULL);
1904 g_return_if_fail (list != NULL);
1906 value_list = g_string_sized_new (length * 8);
1907 for (i = 0; i < length; i++)
1911 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1913 g_string_append (value_list, value);
1914 g_string_append_c (value_list, key_file->list_separator);
1919 g_key_file_set_value (key_file, group_name, key, value_list->str);
1920 g_string_free (value_list, TRUE);
1924 * g_key_file_get_integer:
1925 * @key_file: a #GKeyFile
1926 * @group_name: a group name
1928 * @error: return location for a #GError
1930 * Returns the value associated with @key under @group_name as an
1931 * integer. If @key cannot be found then the return value is
1932 * undefined and @error is set to
1933 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1934 * associated with @key cannot be interpreted as an integer then
1935 * the return value is also undefined and @error is set to
1936 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1938 * Return value: the value associated with the key as an integer.
1942 g_key_file_get_integer (GKeyFile *key_file,
1943 const gchar *group_name,
1947 GError *key_file_error;
1951 g_return_val_if_fail (key_file != NULL, -1);
1952 g_return_val_if_fail (group_name != NULL, -1);
1953 g_return_val_if_fail (key != NULL, -1);
1955 key_file_error = NULL;
1957 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1961 g_propagate_error (error, key_file_error);
1965 int_value = g_key_file_parse_value_as_integer (key_file, value,
1971 if (g_error_matches (key_file_error,
1973 G_KEY_FILE_ERROR_INVALID_VALUE))
1975 g_set_error (error, G_KEY_FILE_ERROR,
1976 G_KEY_FILE_ERROR_INVALID_VALUE,
1977 _("Key file contains key '%s' in group '%s' "
1978 "which has value that cannot be interpreted."), key,
1980 g_error_free (key_file_error);
1983 g_propagate_error (error, key_file_error);
1990 * g_key_file_set_integer:
1991 * @key_file: a #GKeyFile
1992 * @group_name: a group name
1994 * @value: an integer value
1996 * Associates a new integer value with @key under @group_name.
1997 * If @key cannot be found then it is created.
2002 g_key_file_set_integer (GKeyFile *key_file,
2003 const gchar *group_name,
2009 g_return_if_fail (key_file != NULL);
2010 g_return_if_fail (group_name != NULL);
2011 g_return_if_fail (key != NULL);
2013 result = g_key_file_parse_integer_as_value (key_file, value);
2014 g_key_file_set_value (key_file, group_name, key, result);
2019 * g_key_file_get_integer_list:
2020 * @key_file: a #GKeyFile
2021 * @group_name: a group name
2023 * @length: the number of integers returned
2024 * @error: return location for a #GError
2026 * Returns the values associated with @key under @group_name as
2027 * integers. If @key cannot be found then the return value is
2028 * undefined and @error is set to
2029 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
2030 * associated with @key cannot be interpreted as integers then
2031 * the return value is also undefined and @error is set to
2032 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2034 * Return value: the values associated with the key as a integer
2038 g_key_file_get_integer_list (GKeyFile *key_file,
2039 const gchar *group_name,
2044 GError *key_file_error = NULL;
2049 g_return_val_if_fail (key_file != NULL, NULL);
2050 g_return_val_if_fail (group_name != NULL, NULL);
2051 g_return_val_if_fail (key != NULL, NULL);
2053 values = g_key_file_get_string_list (key_file, group_name, key,
2054 &num_ints, &key_file_error);
2057 g_propagate_error (error, key_file_error);
2062 int_values = g_new0 (gint, num_ints);
2064 for (i = 0; i < num_ints; i++)
2066 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2072 g_propagate_error (error, key_file_error);
2073 g_strfreev (values);
2074 g_free (int_values);
2079 g_strfreev (values);
2088 * g_key_file_set_integer_list:
2089 * @key_file: a #GKeyFile
2090 * @group_name: a group name
2092 * @list: an array of integer values
2093 * @length: number of integer values in @list
2095 * Associates a list of integer values with @key under
2096 * @group_name. If @key cannot be found then it is created.
2101 g_key_file_set_integer_list (GKeyFile *key_file,
2102 const gchar *group_name,
2110 g_return_if_fail (key_file != NULL);
2111 g_return_if_fail (group_name != NULL);
2112 g_return_if_fail (key != NULL);
2113 g_return_if_fail (list != NULL);
2115 values = g_string_sized_new (length * 16);
2116 for (i = 0; i < length; i++)
2120 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2122 g_string_append (values, value);
2123 g_string_append_c (values, ';');
2128 g_key_file_set_value (key_file, group_name, key, values->str);
2129 g_string_free (values, TRUE);
2133 g_key_file_set_key_comment (GKeyFile *key_file,
2134 const gchar *group_name,
2136 const gchar *comment,
2139 GKeyFileGroup *group;
2140 GKeyFileKeyValuePair *pair;
2141 GList *key_node, *comment_node, *tmp;
2143 group = g_key_file_lookup_group (key_file, group_name);
2146 g_set_error (error, G_KEY_FILE_ERROR,
2147 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2148 _("Key file does not have group '%s'"),
2154 /* First find the key the comments are supposed to be
2157 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2159 if (key_node == NULL)
2161 g_set_error (error, G_KEY_FILE_ERROR,
2162 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2163 _("Key file does not have key '%s' in group '%s'"),
2168 /* Then find all the comments already associated with the
2171 tmp = key_node->next;
2174 GKeyFileKeyValuePair *pair;
2176 pair = (GKeyFileKeyValuePair *) tmp->data;
2178 if (pair->key != NULL)
2183 g_key_file_remove_key_value_pair_node (key_file, group,
2187 if (comment == NULL)
2190 /* Now we can add our new comment
2192 pair = g_new0 (GKeyFileKeyValuePair, 1);
2195 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2197 key_node = g_list_insert (key_node, pair, 1);
2201 g_key_file_set_group_comment (GKeyFile *key_file,
2202 const gchar *group_name,
2203 const gchar *comment,
2206 GKeyFileGroup *group;
2208 group = g_key_file_lookup_group (key_file, group_name);
2211 g_set_error (error, G_KEY_FILE_ERROR,
2212 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2213 _("Key file does not have group '%s'"),
2219 /* First remove any existing comment
2223 g_key_file_key_value_pair_free (group->comment);
2224 group->comment = NULL;
2227 if (comment == NULL)
2230 /* Now we can add our new comment
2232 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2234 group->comment->key = NULL;
2235 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2239 g_key_file_set_top_comment (GKeyFile *key_file,
2240 const gchar *comment,
2244 GKeyFileGroup *group;
2245 GKeyFileKeyValuePair *pair;
2247 /* The last group in the list should be the top (comments only)
2250 g_assert (key_file->groups != NULL);
2251 group_node = g_list_last (key_file->groups);
2252 group = (GKeyFileGroup *) group_node->data;
2253 g_assert (group->name == NULL);
2255 /* Note all keys must be comments at the top of
2256 * the file, so we can just free it all.
2258 if (group->key_value_pairs != NULL)
2260 g_list_foreach (group->key_value_pairs,
2261 (GFunc) g_key_file_key_value_pair_free,
2263 g_list_free (group->key_value_pairs);
2264 group->key_value_pairs = NULL;
2267 if (comment == NULL)
2270 pair = g_new0 (GKeyFileKeyValuePair, 1);
2273 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2275 group->key_value_pairs =
2276 g_list_prepend (group->key_value_pairs, pair);
2280 * g_key_file_set_comment:
2281 * @key_file: a #GKeyFile
2282 * @group_name: a group name
2284 * @comment: a comment
2285 * @error: return location for a #GError
2287 * Places a comment above @key from @group_name.
2288 * @group_name. If @key is %NULL then @comment will
2289 * be written above @group_name. If both @key
2290 * and @group_name are NULL, then @comment will
2291 * be written above the first group in the file.
2296 g_key_file_set_comment (GKeyFile *key_file,
2297 const gchar *group_name,
2299 const gchar *comment,
2302 g_return_if_fail (key_file != NULL);
2304 if (group_name != NULL && key != NULL)
2305 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2306 else if (group_name != NULL)
2307 g_key_file_set_group_comment (key_file, group_name, comment, error);
2309 g_key_file_set_top_comment (key_file, comment, error);
2311 if (comment != NULL)
2312 key_file->approximate_size += strlen (comment);
2316 g_key_file_get_key_comment (GKeyFile *key_file,
2317 const gchar *group_name,
2321 GKeyFileGroup *group;
2322 GList *key_node, *tmp;
2326 group = g_key_file_lookup_group (key_file, group_name);
2329 g_set_error (error, G_KEY_FILE_ERROR,
2330 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2331 _("Key file does not have group '%s'"),
2337 /* First find the key the comments are supposed to be
2340 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2342 if (key_node == NULL)
2344 g_set_error (error, G_KEY_FILE_ERROR,
2345 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2346 _("Key file does not have key '%s' in group '%s'"),
2353 /* Then find all the comments already associated with the
2354 * key and concatentate them.
2356 tmp = key_node->next;
2359 GKeyFileKeyValuePair *pair;
2361 pair = (GKeyFileKeyValuePair *) tmp->data;
2363 if (pair->key != NULL)
2367 string = g_string_sized_new (512);
2369 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2370 g_string_append (string, comment);
2378 comment = string->str;
2379 g_string_free (string, FALSE);
2388 g_key_file_get_group_comment (GKeyFile *key_file,
2389 const gchar *group_name,
2392 GKeyFileGroup *group;
2394 group = g_key_file_lookup_group (key_file, group_name);
2397 g_set_error (error, G_KEY_FILE_ERROR,
2398 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2399 _("Key file does not have group '%s'"),
2406 return g_strdup (group->comment->value);
2412 g_key_file_get_top_comment (GKeyFile *key_file,
2415 GList *group_node, *tmp;
2416 GKeyFileGroup *group;
2420 /* The last group in the list should be the top (comments only)
2423 g_assert (key_file->groups != NULL);
2424 group_node = g_list_last (key_file->groups);
2425 group = (GKeyFileGroup *) group_node->data;
2426 g_assert (group->name == NULL);
2430 /* Then find all the comments already associated with the
2431 * key and concatentate them.
2433 tmp = group->key_value_pairs;
2436 GKeyFileKeyValuePair *pair;
2438 pair = (GKeyFileKeyValuePair *) tmp->data;
2440 if (pair->key != NULL)
2444 string = g_string_sized_new (512);
2446 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2447 g_string_append (string, comment);
2455 comment = string->str;
2456 g_string_free (string, FALSE);
2465 * g_key_file_get_comment:
2466 * @key_file: a #GKeyFile
2467 * @group_name: a group name
2469 * @error: return location for a #GError
2471 * Retreives a comment above @key from @group_name.
2472 * @group_name. If @key is %NULL then @comment will
2473 * be read from above @group_name. If both @key
2474 * and @group_name are NULL, then @comment will
2475 * be read from above the first group in the file.
2478 * Returns: a comment that should be freed with g_free()
2481 g_key_file_get_comment (GKeyFile *key_file,
2482 const gchar *group_name,
2486 g_return_val_if_fail (key_file != NULL, NULL);
2488 if (group_name != NULL && key != NULL)
2489 return g_key_file_get_key_comment (key_file, group_name, key, error);
2490 else if (group_name != NULL)
2491 return g_key_file_get_group_comment (key_file, group_name, error);
2493 return g_key_file_get_top_comment (key_file, error);
2497 * g_key_file_remove_comment:
2498 * @key_file: a #GKeyFile
2499 * @group_name: a group name
2501 * @error: return location for a #GError
2503 * Removes a comment above @key from @group_name.
2504 * @group_name. If @key is %NULL then @comment will
2505 * be written above @group_name. If both @key
2506 * and @group_name are NULL, then @comment will
2507 * be written above the first group in the file.
2513 g_key_file_remove_comment (GKeyFile *key_file,
2514 const gchar *group_name,
2518 g_return_if_fail (key_file != NULL);
2520 if (group_name != NULL && key != NULL)
2521 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2522 else if (group_name != NULL)
2523 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2525 g_key_file_set_top_comment (key_file, NULL, error);
2529 * g_key_file_has_group:
2530 * @key_file: a #GKeyFile
2531 * @group_name: a group name
2533 * Looks whether the key file has the group @group_name.
2535 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2540 g_key_file_has_group (GKeyFile *key_file,
2541 const gchar *group_name)
2544 GKeyFileGroup *group;
2546 g_return_val_if_fail (key_file != NULL, FALSE);
2547 g_return_val_if_fail (group_name != NULL, FALSE);
2549 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2551 group = (GKeyFileGroup *) tmp->data;
2552 if (group && group->name && (strcmp (group->name, group_name) == 0))
2560 * g_key_file_has_key:
2561 * @key_file: a #GKeyFile
2562 * @group_name: a group name
2564 * @error: return location for a #GError
2566 * Looks whether the key file has the key @key in the group
2569 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2574 g_key_file_has_key (GKeyFile *key_file,
2575 const gchar *group_name,
2579 GKeyFileKeyValuePair *pair;
2580 GKeyFileGroup *group;
2582 g_return_val_if_fail (key_file != NULL, FALSE);
2583 g_return_val_if_fail (key != NULL, FALSE);
2584 g_return_val_if_fail (group_name != NULL, FALSE);
2586 group = g_key_file_lookup_group (key_file, group_name);
2590 g_set_error (error, G_KEY_FILE_ERROR,
2591 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2592 _("Key file does not have group '%s'"),
2598 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2600 return pair != NULL;
2604 g_key_file_add_group (GKeyFile *key_file,
2605 const gchar *group_name)
2607 GKeyFileGroup *group;
2609 g_return_if_fail (key_file != NULL);
2610 g_return_if_fail (group_name != NULL);
2611 g_return_if_fail (g_key_file_lookup_group (key_file, group_name) == NULL);
2613 group = g_new0 (GKeyFileGroup, 1);
2614 group->name = g_strdup (group_name);
2615 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2616 key_file->groups = g_list_prepend (key_file->groups, group);
2617 key_file->approximate_size += strlen (group_name) + 3;
2618 key_file->current_group = group;
2620 if (key_file->start_group_name == NULL)
2621 key_file->start_group_name = g_strdup (group_name);
2625 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2630 g_free (pair->value);
2635 /* Be careful not to call this function on a node with data in the
2636 * lookup map without removing it from the lookup map, first.
2638 * Some current cases where this warning is not a concern are
2640 * - the node being removed is a comment node
2641 * - the entire lookup map is getting destroyed soon after
2645 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2646 GKeyFileGroup *group,
2650 GKeyFileKeyValuePair *pair;
2652 pair = (GKeyFileKeyValuePair *) pair_node->data;
2654 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2656 if (pair->key != NULL)
2657 key_file->approximate_size -= strlen (pair->key) + 1;
2659 g_assert (pair->value != NULL);
2660 key_file->approximate_size -= strlen (pair->value);
2662 g_key_file_key_value_pair_free (pair);
2664 g_list_free_1 (pair_node);
2668 g_key_file_remove_group_node (GKeyFile *key_file,
2671 GKeyFileGroup *group;
2674 group = (GKeyFileGroup *) group_node->data;
2676 /* If the current group gets deleted make the current group the first
2679 if (key_file->current_group == group)
2681 /* groups should always contain at least the top comment group,
2682 * unless g_key_file_clear has been called
2684 if (key_file->groups)
2685 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2687 key_file->current_group = NULL;
2690 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2692 if (group->name != NULL)
2693 key_file->approximate_size -= strlen (group->name) + 3;
2695 tmp = group->key_value_pairs;
2702 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2705 g_assert (group->key_value_pairs == NULL);
2707 if (group->lookup_map)
2709 g_hash_table_destroy (group->lookup_map);
2710 group->lookup_map = NULL;
2713 g_free ((gchar *) group->name);
2715 g_list_free_1 (group_node);
2719 * g_key_file_remove_group:
2720 * @key_file: a #GKeyFile
2721 * @group_name: a group name
2722 * @error: return location for a #GError or %NULL
2724 * Removes the specified group, @group_name,
2725 * from the key file.
2730 g_key_file_remove_group (GKeyFile *key_file,
2731 const gchar *group_name,
2736 g_return_if_fail (key_file != NULL);
2737 g_return_if_fail (group_name != NULL);
2739 group_node = g_key_file_lookup_group_node (key_file, group_name);
2742 g_set_error (error, G_KEY_FILE_ERROR,
2743 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2744 _("Key file does not have group '%s'"),
2747 g_key_file_remove_group_node (key_file, group_node);
2751 g_key_file_add_key (GKeyFile *key_file,
2752 const gchar *group_name,
2756 GKeyFileGroup *group;
2757 GKeyFileKeyValuePair *pair;
2759 group = g_key_file_lookup_group (key_file, group_name);
2763 g_key_file_add_group (key_file, group_name);
2764 group = (GKeyFileGroup *) key_file->groups->data;
2767 pair = g_new0 (GKeyFileKeyValuePair, 1);
2769 pair->key = g_strdup (key);
2770 pair->value = g_strdup (value);
2772 g_hash_table_replace (group->lookup_map, pair->key, pair);
2773 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2774 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2778 * g_key_file_remove_key:
2779 * @key_file: a #GKeyFile
2780 * @group_name: a group name
2781 * @key: a key name to remove
2782 * @error: return location for a #GError or %NULL
2784 * Removes @key in @group_name from the key file.
2789 g_key_file_remove_key (GKeyFile *key_file,
2790 const gchar *group_name,
2794 GKeyFileGroup *group;
2795 GKeyFileKeyValuePair *pair;
2797 g_return_if_fail (key_file != NULL);
2798 g_return_if_fail (group_name != NULL);
2799 g_return_if_fail (key != NULL);
2803 if (group_name == NULL)
2804 group = key_file->current_group;
2806 group = g_key_file_lookup_group (key_file, group_name);
2810 g_set_error (error, G_KEY_FILE_ERROR,
2811 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2812 _("Key file does not have group '%s'"),
2817 group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2818 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2822 g_set_error (error, G_KEY_FILE_ERROR,
2823 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2824 _("Key file does not have key '%s' in group '%s'"), key, group_name);
2828 g_hash_table_remove (group->lookup_map, pair->key);
2830 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2831 g_key_file_key_value_pair_free (pair);
2835 g_key_file_lookup_group_node (GKeyFile *key_file,
2836 const gchar *group_name)
2838 GKeyFileGroup *group;
2842 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2844 group = (GKeyFileGroup *) tmp->data;
2846 if (group && group->name && strcmp (group->name, group_name) == 0)
2855 static GKeyFileGroup *
2856 g_key_file_lookup_group (GKeyFile *key_file,
2857 const gchar *group_name)
2861 group_node = g_key_file_lookup_group_node (key_file, group_name);
2863 if (group_node != NULL)
2864 return (GKeyFileGroup *) group_node->data;
2870 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2871 GKeyFileGroup *group,
2876 for (key_node = group->key_value_pairs;
2878 key_node = key_node->next)
2880 GKeyFileKeyValuePair *pair;
2882 pair = (GKeyFileKeyValuePair *) key_node->data;
2884 if (pair->key && strcmp (pair->key, key) == 0)
2891 static GKeyFileKeyValuePair *
2892 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2893 GKeyFileGroup *group,
2896 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2899 /* Lines starting with # or consisting entirely of whitespace are merely
2900 * recorded, not parsed. This function assumes all leading whitespace
2901 * has been stripped.
2904 g_key_file_line_is_comment (const gchar *line)
2906 return (*line == '#' || *line == '\0' || *line == '\n');
2909 /* A group in a key file is made up of a starting '[' followed by one
2910 * or more letters making up the group name followed by ']'.
2913 g_key_file_line_is_group (const gchar *line)
2921 p = g_utf8_next_char (p);
2926 p = g_utf8_next_char (p);
2928 /* Group name must be non-empty
2933 while (*p && *p != ']')
2934 p = g_utf8_next_char (p);
2943 g_key_file_line_is_key_value_pair (const gchar *line)
2947 p = (gchar *) g_utf8_strchr (line, -1, '=');
2952 /* Key must be non-empty
2961 g_key_file_parse_value_as_string (GKeyFile *key_file,
2966 GError *parse_error = NULL;
2967 gchar *string_value, *p, *q0, *q;
2969 string_value = g_new0 (gchar, strlen (value) + 1);
2971 p = (gchar *) value;
2972 q0 = q = string_value;
3002 if (pieces && *p == key_file->list_separator)
3003 *q = key_file->list_separator;
3009 if (parse_error == NULL)
3017 g_set_error (error, G_KEY_FILE_ERROR,
3018 G_KEY_FILE_ERROR_INVALID_VALUE,
3019 _("Key file contains invalid escape "
3020 "sequence '%s'"), sequence);
3029 if (pieces && (*p == key_file->list_separator))
3031 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3040 if (p[-1] == '\\' && error == NULL)
3041 g_set_error (error, G_KEY_FILE_ERROR,
3042 G_KEY_FILE_ERROR_INVALID_VALUE,
3043 _("Key file contains escape character at end of line"));
3049 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3050 *pieces = g_slist_reverse (*pieces);
3053 return string_value;
3057 g_key_file_parse_string_as_value (GKeyFile *key_file,
3058 const gchar *string,
3059 gboolean escape_separator)
3061 gchar *value, *p, *q;
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 while (p < (string + length - 1))
3075 gchar escaped_character[3] = { '\\', 0, 0 };
3080 escaped_character[1] = 's';
3081 strcpy (q, escaped_character);
3085 escaped_character[1] = 'n';
3086 strcpy (q, escaped_character);
3090 escaped_character[1] = 't';
3091 strcpy (q, escaped_character);
3095 escaped_character[1] = 'r';
3096 strcpy (q, escaped_character);
3100 escaped_character[1] = '\\';
3101 strcpy (q, escaped_character);
3105 if (escape_separator && *p == key_file->list_separator)
3107 escaped_character[1] = key_file->list_separator;
3108 strcpy (q, escaped_character);
3126 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3130 gchar *end_of_valid_int;
3133 int_value = strtol (value, &end_of_valid_int, 0);
3135 if (*end_of_valid_int != '\0')
3136 g_set_error (error, G_KEY_FILE_ERROR,
3137 G_KEY_FILE_ERROR_INVALID_VALUE,
3138 _("Value '%s' cannot be interpreted as a number."), value);
3144 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3148 return g_strdup_printf ("%d", value);
3152 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3158 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3160 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3164 g_set_error (error, G_KEY_FILE_ERROR,
3165 G_KEY_FILE_ERROR_INVALID_VALUE,
3166 _("Value '%s' cannot be interpreted as a boolean."), value);
3172 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3176 return g_strdup ("true");
3178 return g_strdup ("false");
3182 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3186 gchar **lines, *comment;
3189 string = g_string_sized_new (512);
3191 lines = g_strsplit (value, "\n", 0);
3193 for (i = 0; lines[i] != NULL; i++)
3195 if (lines[i][0] != '#')
3196 g_string_append_printf (string, "%s\n", lines[i]);
3198 g_string_append_printf (string, "%s\n", lines[i] + 1);
3202 comment = string->str;
3204 g_string_free (string, FALSE);
3210 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3211 const gchar *comment)
3214 gchar **lines, *value;
3217 string = g_string_sized_new (512);
3219 lines = g_strsplit (comment, "\n", 0);
3221 for (i = 0; lines[i] != NULL; i++)
3222 g_string_append_printf (string, "#%s%s", lines[i],
3223 lines[i + 1] == NULL? "" : "\n");
3226 value = string->str;
3228 g_string_free (string, FALSE);