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
1829 g_key_file_get_boolean_list (GKeyFile *key_file,
1830 const gchar *group_name,
1835 GError *key_file_error;
1837 gboolean *bool_values;
1840 g_return_val_if_fail (key_file != NULL, NULL);
1841 g_return_val_if_fail (group_name != NULL, NULL);
1842 g_return_val_if_fail (key != NULL, NULL);
1844 key_file_error = NULL;
1846 values = g_key_file_get_string_list (key_file, group_name, key,
1847 &num_bools, &key_file_error);
1850 g_propagate_error (error, key_file_error);
1855 bool_values = g_new0 (gboolean, num_bools);
1857 for (i = 0; i < num_bools; i++)
1859 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1865 g_propagate_error (error, key_file_error);
1866 g_strfreev (values);
1867 g_free (bool_values);
1872 g_strfreev (values);
1875 *length = num_bools;
1881 * g_key_file_set_boolean_list:
1882 * @key_file: a #GKeyFile
1883 * @group_name: a group name
1885 * @list: an array of boolean values
1886 * @length: length of @list
1888 * Associates a list of boolean values with @key under
1889 * @group_name. If @key cannot be found then it is created.
1894 g_key_file_set_boolean_list (GKeyFile *key_file,
1895 const gchar *group_name,
1900 GString *value_list;
1903 g_return_if_fail (key_file != NULL);
1904 g_return_if_fail (group_name != NULL);
1905 g_return_if_fail (key != NULL);
1906 g_return_if_fail (list != NULL);
1908 value_list = g_string_sized_new (length * 8);
1909 for (i = 0; i < length; i++)
1913 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1915 g_string_append (value_list, value);
1916 g_string_append_c (value_list, key_file->list_separator);
1921 g_key_file_set_value (key_file, group_name, key, value_list->str);
1922 g_string_free (value_list, TRUE);
1926 * g_key_file_get_integer:
1927 * @key_file: a #GKeyFile
1928 * @group_name: a group name
1930 * @error: return location for a #GError
1932 * Returns the value associated with @key under @group_name as an
1933 * integer. If @key cannot be found then the return value is
1934 * undefined and @error is set to
1935 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1936 * associated with @key cannot be interpreted as an integer then
1937 * the return value is also undefined and @error is set to
1938 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1940 * Return value: the value associated with the key as an integer.
1945 g_key_file_get_integer (GKeyFile *key_file,
1946 const gchar *group_name,
1950 GError *key_file_error;
1954 g_return_val_if_fail (key_file != NULL, -1);
1955 g_return_val_if_fail (group_name != NULL, -1);
1956 g_return_val_if_fail (key != NULL, -1);
1958 key_file_error = NULL;
1960 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1964 g_propagate_error (error, key_file_error);
1968 int_value = g_key_file_parse_value_as_integer (key_file, value,
1974 if (g_error_matches (key_file_error,
1976 G_KEY_FILE_ERROR_INVALID_VALUE))
1978 g_set_error (error, G_KEY_FILE_ERROR,
1979 G_KEY_FILE_ERROR_INVALID_VALUE,
1980 _("Key file contains key '%s' in group '%s' "
1981 "which has value that cannot be interpreted."), key,
1983 g_error_free (key_file_error);
1986 g_propagate_error (error, key_file_error);
1993 * g_key_file_set_integer:
1994 * @key_file: a #GKeyFile
1995 * @group_name: a group name
1997 * @value: an integer value
1999 * Associates a new integer value with @key under @group_name.
2000 * If @key cannot be found then it is created.
2005 g_key_file_set_integer (GKeyFile *key_file,
2006 const gchar *group_name,
2012 g_return_if_fail (key_file != NULL);
2013 g_return_if_fail (group_name != NULL);
2014 g_return_if_fail (key != NULL);
2016 result = g_key_file_parse_integer_as_value (key_file, value);
2017 g_key_file_set_value (key_file, group_name, key, result);
2022 * g_key_file_get_integer_list:
2023 * @key_file: a #GKeyFile
2024 * @group_name: a group name
2026 * @length: the number of integers returned
2027 * @error: return location for a #GError
2029 * Returns the values associated with @key under @group_name as
2030 * integers. If @key cannot be found then the return value is
2031 * undefined and @error is set to
2032 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
2033 * associated with @key cannot be interpreted as integers then
2034 * the return value is also undefined and @error is set to
2035 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2037 * Return value: the values associated with the key as a integer
2042 g_key_file_get_integer_list (GKeyFile *key_file,
2043 const gchar *group_name,
2048 GError *key_file_error = NULL;
2053 g_return_val_if_fail (key_file != NULL, NULL);
2054 g_return_val_if_fail (group_name != NULL, NULL);
2055 g_return_val_if_fail (key != NULL, NULL);
2057 values = g_key_file_get_string_list (key_file, group_name, key,
2058 &num_ints, &key_file_error);
2061 g_propagate_error (error, key_file_error);
2066 int_values = g_new0 (gint, num_ints);
2068 for (i = 0; i < num_ints; i++)
2070 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2076 g_propagate_error (error, key_file_error);
2077 g_strfreev (values);
2078 g_free (int_values);
2083 g_strfreev (values);
2092 * g_key_file_set_integer_list:
2093 * @key_file: a #GKeyFile
2094 * @group_name: a group name
2096 * @list: an array of integer values
2097 * @length: number of integer values in @list
2099 * Associates a list of integer values with @key under
2100 * @group_name. If @key cannot be found then it is created.
2105 g_key_file_set_integer_list (GKeyFile *key_file,
2106 const gchar *group_name,
2114 g_return_if_fail (key_file != NULL);
2115 g_return_if_fail (group_name != NULL);
2116 g_return_if_fail (key != NULL);
2117 g_return_if_fail (list != NULL);
2119 values = g_string_sized_new (length * 16);
2120 for (i = 0; i < length; i++)
2124 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2126 g_string_append (values, value);
2127 g_string_append_c (values, ';');
2132 g_key_file_set_value (key_file, group_name, key, values->str);
2133 g_string_free (values, TRUE);
2137 g_key_file_set_key_comment (GKeyFile *key_file,
2138 const gchar *group_name,
2140 const gchar *comment,
2143 GKeyFileGroup *group;
2144 GKeyFileKeyValuePair *pair;
2145 GList *key_node, *comment_node, *tmp;
2147 group = g_key_file_lookup_group (key_file, group_name);
2150 g_set_error (error, G_KEY_FILE_ERROR,
2151 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2152 _("Key file does not have group '%s'"),
2158 /* First find the key the comments are supposed to be
2161 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2163 if (key_node == NULL)
2165 g_set_error (error, G_KEY_FILE_ERROR,
2166 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2167 _("Key file does not have key '%s' in group '%s'"),
2172 /* Then find all the comments already associated with the
2175 tmp = key_node->next;
2178 GKeyFileKeyValuePair *pair;
2180 pair = (GKeyFileKeyValuePair *) tmp->data;
2182 if (pair->key != NULL)
2187 g_key_file_remove_key_value_pair_node (key_file, group,
2191 if (comment == NULL)
2194 /* Now we can add our new comment
2196 pair = g_new0 (GKeyFileKeyValuePair, 1);
2199 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2201 key_node = g_list_insert (key_node, pair, 1);
2205 g_key_file_set_group_comment (GKeyFile *key_file,
2206 const gchar *group_name,
2207 const gchar *comment,
2210 GKeyFileGroup *group;
2212 group = g_key_file_lookup_group (key_file, group_name);
2215 g_set_error (error, G_KEY_FILE_ERROR,
2216 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2217 _("Key file does not have group '%s'"),
2223 /* First remove any existing comment
2227 g_key_file_key_value_pair_free (group->comment);
2228 group->comment = NULL;
2231 if (comment == NULL)
2234 /* Now we can add our new comment
2236 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2238 group->comment->key = NULL;
2239 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2243 g_key_file_set_top_comment (GKeyFile *key_file,
2244 const gchar *comment,
2248 GKeyFileGroup *group;
2249 GKeyFileKeyValuePair *pair;
2251 /* The last group in the list should be the top (comments only)
2254 g_assert (key_file->groups != NULL);
2255 group_node = g_list_last (key_file->groups);
2256 group = (GKeyFileGroup *) group_node->data;
2257 g_assert (group->name == NULL);
2259 /* Note all keys must be comments at the top of
2260 * the file, so we can just free it all.
2262 if (group->key_value_pairs != NULL)
2264 g_list_foreach (group->key_value_pairs,
2265 (GFunc) g_key_file_key_value_pair_free,
2267 g_list_free (group->key_value_pairs);
2268 group->key_value_pairs = NULL;
2271 if (comment == NULL)
2274 pair = g_new0 (GKeyFileKeyValuePair, 1);
2277 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2279 group->key_value_pairs =
2280 g_list_prepend (group->key_value_pairs, pair);
2284 * g_key_file_set_comment:
2285 * @key_file: a #GKeyFile
2286 * @group_name: a group name
2288 * @comment: a comment
2289 * @error: return location for a #GError
2291 * Places a comment above @key from @group_name.
2292 * @group_name. If @key is %NULL then @comment will
2293 * be written above @group_name. If both @key
2294 * and @group_name are NULL, then @comment will
2295 * be written above the first group in the file.
2300 g_key_file_set_comment (GKeyFile *key_file,
2301 const gchar *group_name,
2303 const gchar *comment,
2306 g_return_if_fail (key_file != NULL);
2308 if (group_name != NULL && key != NULL)
2309 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2310 else if (group_name != NULL)
2311 g_key_file_set_group_comment (key_file, group_name, comment, error);
2313 g_key_file_set_top_comment (key_file, comment, error);
2315 if (comment != NULL)
2316 key_file->approximate_size += strlen (comment);
2320 g_key_file_get_key_comment (GKeyFile *key_file,
2321 const gchar *group_name,
2325 GKeyFileGroup *group;
2326 GList *key_node, *tmp;
2330 group = g_key_file_lookup_group (key_file, group_name);
2333 g_set_error (error, G_KEY_FILE_ERROR,
2334 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2335 _("Key file does not have group '%s'"),
2341 /* First find the key the comments are supposed to be
2344 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2346 if (key_node == NULL)
2348 g_set_error (error, G_KEY_FILE_ERROR,
2349 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2350 _("Key file does not have key '%s' in group '%s'"),
2357 /* Then find all the comments already associated with the
2358 * key and concatentate them.
2360 tmp = key_node->next;
2363 GKeyFileKeyValuePair *pair;
2365 pair = (GKeyFileKeyValuePair *) tmp->data;
2367 if (pair->key != NULL)
2371 string = g_string_sized_new (512);
2373 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2374 g_string_append (string, comment);
2382 comment = string->str;
2383 g_string_free (string, FALSE);
2392 g_key_file_get_group_comment (GKeyFile *key_file,
2393 const gchar *group_name,
2396 GKeyFileGroup *group;
2398 group = g_key_file_lookup_group (key_file, group_name);
2401 g_set_error (error, G_KEY_FILE_ERROR,
2402 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2403 _("Key file does not have group '%s'"),
2410 return g_strdup (group->comment->value);
2416 g_key_file_get_top_comment (GKeyFile *key_file,
2419 GList *group_node, *tmp;
2420 GKeyFileGroup *group;
2424 /* The last group in the list should be the top (comments only)
2427 g_assert (key_file->groups != NULL);
2428 group_node = g_list_last (key_file->groups);
2429 group = (GKeyFileGroup *) group_node->data;
2430 g_assert (group->name == NULL);
2434 /* Then find all the comments already associated with the
2435 * key and concatentate them.
2437 tmp = group->key_value_pairs;
2440 GKeyFileKeyValuePair *pair;
2442 pair = (GKeyFileKeyValuePair *) tmp->data;
2444 if (pair->key != NULL)
2448 string = g_string_sized_new (512);
2450 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2451 g_string_append (string, comment);
2459 comment = string->str;
2460 g_string_free (string, FALSE);
2469 * g_key_file_get_comment:
2470 * @key_file: a #GKeyFile
2471 * @group_name: a group name
2473 * @error: return location for a #GError
2475 * Retreives a comment above @key from @group_name.
2476 * @group_name. If @key is %NULL then @comment will
2477 * be read from above @group_name. If both @key
2478 * and @group_name are NULL, then @comment will
2479 * be read from above the first group in the file.
2482 * Returns: a comment that should be freed with g_free()
2485 g_key_file_get_comment (GKeyFile *key_file,
2486 const gchar *group_name,
2490 g_return_val_if_fail (key_file != NULL, NULL);
2492 if (group_name != NULL && key != NULL)
2493 return g_key_file_get_key_comment (key_file, group_name, key, error);
2494 else if (group_name != NULL)
2495 return g_key_file_get_group_comment (key_file, group_name, error);
2497 return g_key_file_get_top_comment (key_file, error);
2501 * g_key_file_remove_comment:
2502 * @key_file: a #GKeyFile
2503 * @group_name: a group name
2505 * @error: return location for a #GError
2507 * Removes a comment above @key from @group_name.
2508 * @group_name. If @key is %NULL then @comment will
2509 * be written above @group_name. If both @key
2510 * and @group_name are NULL, then @comment will
2511 * be written above the first group in the file.
2517 g_key_file_remove_comment (GKeyFile *key_file,
2518 const gchar *group_name,
2522 g_return_if_fail (key_file != NULL);
2524 if (group_name != NULL && key != NULL)
2525 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2526 else if (group_name != NULL)
2527 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2529 g_key_file_set_top_comment (key_file, NULL, error);
2533 * g_key_file_has_group:
2534 * @key_file: a #GKeyFile
2535 * @group_name: a group name
2537 * Looks whether the key file has the group @group_name.
2539 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2544 g_key_file_has_group (GKeyFile *key_file,
2545 const gchar *group_name)
2548 GKeyFileGroup *group;
2550 g_return_val_if_fail (key_file != NULL, FALSE);
2551 g_return_val_if_fail (group_name != NULL, FALSE);
2553 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2555 group = (GKeyFileGroup *) tmp->data;
2556 if (group && group->name && (strcmp (group->name, group_name) == 0))
2564 * g_key_file_has_key:
2565 * @key_file: a #GKeyFile
2566 * @group_name: a group name
2568 * @error: return location for a #GError
2570 * Looks whether the key file has the key @key in the group
2573 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2579 g_key_file_has_key (GKeyFile *key_file,
2580 const gchar *group_name,
2584 GKeyFileKeyValuePair *pair;
2585 GKeyFileGroup *group;
2587 g_return_val_if_fail (key_file != NULL, FALSE);
2588 g_return_val_if_fail (key != NULL, FALSE);
2589 g_return_val_if_fail (group_name != NULL, FALSE);
2591 group = g_key_file_lookup_group (key_file, group_name);
2595 g_set_error (error, G_KEY_FILE_ERROR,
2596 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2597 _("Key file does not have group '%s'"),
2603 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2605 return pair != NULL;
2609 g_key_file_add_group (GKeyFile *key_file,
2610 const gchar *group_name)
2612 GKeyFileGroup *group;
2614 g_return_if_fail (key_file != NULL);
2615 g_return_if_fail (group_name != NULL);
2616 g_return_if_fail (g_key_file_lookup_group (key_file, group_name) == NULL);
2618 group = g_new0 (GKeyFileGroup, 1);
2619 group->name = g_strdup (group_name);
2620 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2621 key_file->groups = g_list_prepend (key_file->groups, group);
2622 key_file->approximate_size += strlen (group_name) + 3;
2623 key_file->current_group = group;
2625 if (key_file->start_group_name == NULL)
2626 key_file->start_group_name = g_strdup (group_name);
2630 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2635 g_free (pair->value);
2640 /* Be careful not to call this function on a node with data in the
2641 * lookup map without removing it from the lookup map, first.
2643 * Some current cases where this warning is not a concern are
2645 * - the node being removed is a comment node
2646 * - the entire lookup map is getting destroyed soon after
2650 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2651 GKeyFileGroup *group,
2655 GKeyFileKeyValuePair *pair;
2657 pair = (GKeyFileKeyValuePair *) pair_node->data;
2659 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2661 if (pair->key != NULL)
2662 key_file->approximate_size -= strlen (pair->key) + 1;
2664 g_assert (pair->value != NULL);
2665 key_file->approximate_size -= strlen (pair->value);
2667 g_key_file_key_value_pair_free (pair);
2669 g_list_free_1 (pair_node);
2673 g_key_file_remove_group_node (GKeyFile *key_file,
2676 GKeyFileGroup *group;
2679 group = (GKeyFileGroup *) group_node->data;
2681 /* If the current group gets deleted make the current group the first
2684 if (key_file->current_group == group)
2686 /* groups should always contain at least the top comment group,
2687 * unless g_key_file_clear has been called
2689 if (key_file->groups)
2690 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2692 key_file->current_group = NULL;
2695 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2697 if (group->name != NULL)
2698 key_file->approximate_size -= strlen (group->name) + 3;
2700 tmp = group->key_value_pairs;
2707 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2710 g_assert (group->key_value_pairs == NULL);
2712 if (group->lookup_map)
2714 g_hash_table_destroy (group->lookup_map);
2715 group->lookup_map = NULL;
2718 g_free ((gchar *) group->name);
2720 g_list_free_1 (group_node);
2724 * g_key_file_remove_group:
2725 * @key_file: a #GKeyFile
2726 * @group_name: a group name
2727 * @error: return location for a #GError or %NULL
2729 * Removes the specified group, @group_name,
2730 * from the key file.
2735 g_key_file_remove_group (GKeyFile *key_file,
2736 const gchar *group_name,
2741 g_return_if_fail (key_file != NULL);
2742 g_return_if_fail (group_name != NULL);
2744 group_node = g_key_file_lookup_group_node (key_file, group_name);
2747 g_set_error (error, G_KEY_FILE_ERROR,
2748 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2749 _("Key file does not have group '%s'"),
2752 g_key_file_remove_group_node (key_file, group_node);
2756 g_key_file_add_key (GKeyFile *key_file,
2757 const gchar *group_name,
2761 GKeyFileGroup *group;
2762 GKeyFileKeyValuePair *pair;
2764 group = g_key_file_lookup_group (key_file, group_name);
2768 g_key_file_add_group (key_file, group_name);
2769 group = (GKeyFileGroup *) key_file->groups->data;
2772 pair = g_new0 (GKeyFileKeyValuePair, 1);
2774 pair->key = g_strdup (key);
2775 pair->value = g_strdup (value);
2777 g_hash_table_replace (group->lookup_map, pair->key, pair);
2778 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2779 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2783 * g_key_file_remove_key:
2784 * @key_file: a #GKeyFile
2785 * @group_name: a group name
2786 * @key: a key name to remove
2787 * @error: return location for a #GError or %NULL
2789 * Removes @key in @group_name from the key file.
2794 g_key_file_remove_key (GKeyFile *key_file,
2795 const gchar *group_name,
2799 GKeyFileGroup *group;
2800 GKeyFileKeyValuePair *pair;
2802 g_return_if_fail (key_file != NULL);
2803 g_return_if_fail (group_name != NULL);
2804 g_return_if_fail (key != NULL);
2808 if (group_name == NULL)
2809 group = key_file->current_group;
2811 group = g_key_file_lookup_group (key_file, group_name);
2815 g_set_error (error, G_KEY_FILE_ERROR,
2816 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2817 _("Key file does not have group '%s'"),
2822 group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2823 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2827 g_set_error (error, G_KEY_FILE_ERROR,
2828 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2829 _("Key file does not have key '%s' in group '%s'"), key, group_name);
2833 g_hash_table_remove (group->lookup_map, pair->key);
2835 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2836 g_key_file_key_value_pair_free (pair);
2840 g_key_file_lookup_group_node (GKeyFile *key_file,
2841 const gchar *group_name)
2843 GKeyFileGroup *group;
2847 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2849 group = (GKeyFileGroup *) tmp->data;
2851 if (group && group->name && strcmp (group->name, group_name) == 0)
2860 static GKeyFileGroup *
2861 g_key_file_lookup_group (GKeyFile *key_file,
2862 const gchar *group_name)
2866 group_node = g_key_file_lookup_group_node (key_file, group_name);
2868 if (group_node != NULL)
2869 return (GKeyFileGroup *) group_node->data;
2875 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2876 GKeyFileGroup *group,
2881 for (key_node = group->key_value_pairs;
2883 key_node = key_node->next)
2885 GKeyFileKeyValuePair *pair;
2887 pair = (GKeyFileKeyValuePair *) key_node->data;
2889 if (pair->key && strcmp (pair->key, key) == 0)
2896 static GKeyFileKeyValuePair *
2897 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2898 GKeyFileGroup *group,
2901 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2904 /* Lines starting with # or consisting entirely of whitespace are merely
2905 * recorded, not parsed. This function assumes all leading whitespace
2906 * has been stripped.
2909 g_key_file_line_is_comment (const gchar *line)
2911 return (*line == '#' || *line == '\0' || *line == '\n');
2914 /* A group in a key file is made up of a starting '[' followed by one
2915 * or more letters making up the group name followed by ']'.
2918 g_key_file_line_is_group (const gchar *line)
2926 p = g_utf8_next_char (p);
2931 p = g_utf8_next_char (p);
2933 /* Group name must be non-empty
2938 while (*p && *p != ']')
2939 p = g_utf8_next_char (p);
2948 g_key_file_line_is_key_value_pair (const gchar *line)
2952 p = (gchar *) g_utf8_strchr (line, -1, '=');
2957 /* Key must be non-empty
2966 g_key_file_parse_value_as_string (GKeyFile *key_file,
2971 GError *parse_error = NULL;
2972 gchar *string_value, *p, *q0, *q;
2974 string_value = g_new0 (gchar, strlen (value) + 1);
2976 p = (gchar *) value;
2977 q0 = q = string_value;
3007 if (pieces && *p == key_file->list_separator)
3008 *q = key_file->list_separator;
3014 if (parse_error == NULL)
3022 g_set_error (error, G_KEY_FILE_ERROR,
3023 G_KEY_FILE_ERROR_INVALID_VALUE,
3024 _("Key file contains invalid escape "
3025 "sequence '%s'"), sequence);
3034 if (pieces && (*p == key_file->list_separator))
3036 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3045 if (p[-1] == '\\' && error == NULL)
3046 g_set_error (error, G_KEY_FILE_ERROR,
3047 G_KEY_FILE_ERROR_INVALID_VALUE,
3048 _("Key file contains escape character at end of line"));
3054 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3055 *pieces = g_slist_reverse (*pieces);
3058 return string_value;
3062 g_key_file_parse_string_as_value (GKeyFile *key_file,
3063 const gchar *string,
3064 gboolean escape_separator)
3066 gchar *value, *p, *q;
3069 length = strlen (string) + 1;
3071 /* Worst case would be that every character needs to be escaped.
3072 * In other words every character turns to two characters
3074 value = g_new0 (gchar, 2 * length);
3076 p = (gchar *) string;
3078 while (p < (string + length - 1))
3080 gchar escaped_character[3] = { '\\', 0, 0 };
3085 escaped_character[1] = 's';
3086 strcpy (q, escaped_character);
3090 escaped_character[1] = 'n';
3091 strcpy (q, escaped_character);
3095 escaped_character[1] = 't';
3096 strcpy (q, escaped_character);
3100 escaped_character[1] = 'r';
3101 strcpy (q, escaped_character);
3105 escaped_character[1] = '\\';
3106 strcpy (q, escaped_character);
3110 if (escape_separator && *p == key_file->list_separator)
3112 escaped_character[1] = key_file->list_separator;
3113 strcpy (q, escaped_character);
3131 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3135 gchar *end_of_valid_int;
3138 int_value = strtol (value, &end_of_valid_int, 0);
3140 if (*end_of_valid_int != '\0')
3141 g_set_error (error, G_KEY_FILE_ERROR,
3142 G_KEY_FILE_ERROR_INVALID_VALUE,
3143 _("Value '%s' cannot be interpreted as a number."), value);
3149 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3153 return g_strdup_printf ("%d", value);
3157 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3163 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3165 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3169 g_set_error (error, G_KEY_FILE_ERROR,
3170 G_KEY_FILE_ERROR_INVALID_VALUE,
3171 _("Value '%s' cannot be interpreted as a boolean."), value);
3177 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3181 return g_strdup ("true");
3183 return g_strdup ("false");
3187 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3191 gchar **lines, *comment;
3194 string = g_string_sized_new (512);
3196 lines = g_strsplit (value, "\n", 0);
3198 for (i = 0; lines[i] != NULL; i++)
3200 if (lines[i][0] != '#')
3201 g_string_append_printf (string, "%s\n", lines[i]);
3203 g_string_append_printf (string, "%s\n", lines[i] + 1);
3207 comment = string->str;
3209 g_string_free (string, FALSE);
3215 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3216 const gchar *comment)
3219 gchar **lines, *value;
3222 string = g_string_sized_new (512);
3224 lines = g_strsplit (comment, "\n", 0);
3226 for (i = 0; lines[i] != NULL; i++)
3227 g_string_append_printf (string, "#%s%s", lines[i],
3228 lines[i + 1] == NULL? "" : "\n");
3231 value = string->str;
3233 g_string_free (string, FALSE);