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.
34 #include <sys/types.h>
43 #define S_ISREG(mode) ((mode)&_S_IFREG)
46 #endif /* G_OS_WIN23 */
51 #include "gfileutils.h"
57 #include "gmessages.h"
60 #include "gstrfuncs.h"
65 typedef struct _GKeyFileGroup GKeyFileGroup;
71 GKeyFileGroup *start_group;
72 GKeyFileGroup *current_group;
74 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
76 /* Used for sizing the output buffer during serialization
78 gsize approximate_size;
85 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
89 const gchar *name; /* NULL for above first group (which will be comments) */
91 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
93 GList *key_value_pairs;
95 /* Used in parallel with key_value_pairs for
96 * increased lookup performance
98 GHashTable *lookup_map;
101 struct _GKeyFileKeyValuePair
103 gchar *key; /* NULL for comments */
107 static gint find_file_in_data_dirs (const gchar *file,
111 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
115 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
116 const gchar *group_name);
117 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
118 const gchar *group_name);
120 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
121 GKeyFileGroup *group,
123 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
124 GKeyFileGroup *group,
127 static void g_key_file_remove_group_node (GKeyFile *key_file,
129 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
130 GKeyFileGroup *group,
133 static void g_key_file_add_key (GKeyFile *key_file,
134 GKeyFileGroup *group,
137 static void g_key_file_add_group (GKeyFile *key_file,
138 const gchar *group_name);
139 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
140 static gboolean g_key_file_line_is_comment (const gchar *line);
141 static gboolean g_key_file_line_is_group (const gchar *line);
142 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
143 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
147 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
149 gboolean escape_separator);
150 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
153 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
155 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
158 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
160 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
162 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
163 const gchar *comment);
164 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
168 static void g_key_file_parse_comment (GKeyFile *key_file,
172 static void g_key_file_parse_group (GKeyFile *key_file,
176 static gchar *key_get_locale (const gchar *key);
177 static void g_key_file_parse_data (GKeyFile *key_file,
181 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
186 g_key_file_error_quark (void)
188 static GQuark error_quark = 0;
190 if (error_quark == 0)
191 error_quark = g_quark_from_static_string ("g-key-file-error-quark");
197 g_key_file_init (GKeyFile *key_file)
199 key_file->current_group = g_new0 (GKeyFileGroup, 1);
200 key_file->groups = g_list_prepend (NULL, key_file->current_group);
201 key_file->start_group = NULL;
202 key_file->parse_buffer = g_string_sized_new (128);
203 key_file->approximate_size = 0;
204 key_file->list_separator = ';';
209 g_key_file_clear (GKeyFile *key_file)
211 GList *tmp, *group_node;
213 if (key_file->parse_buffer)
214 g_string_free (key_file->parse_buffer, TRUE);
216 tmp = key_file->groups;
221 g_key_file_remove_group_node (key_file, group_node);
224 g_assert (key_file->groups == NULL);
231 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
232 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
233 * read an existing key file.
235 * Return value: an empty #GKeyFile.
240 g_key_file_new (void)
244 key_file = g_new0 (GKeyFile, 1);
245 g_key_file_init (key_file);
251 * g_key_file_set_list_separator:
252 * @key_file: a #GKeyFile
253 * @separator: the separator
255 * Sets the character which is used to separate
256 * values in lists. Typically ';' or ',' are used
257 * as separators. The default list separator is ';'.
262 g_key_file_set_list_separator (GKeyFile *key_file,
265 key_file->list_separator = separator;
269 /* Iterates through all the directories in *dirs trying to
270 * open file. When it successfully locates and opens a file it
271 * returns the file descriptor to the open file. It also
272 * outputs the absolute path of the file in output_file and
273 * leaves the unchecked directories in *dirs.
276 find_file_in_data_dirs (const gchar *file,
281 gchar **data_dirs, *data_dir, *path;
292 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
294 gchar *candidate_file, *sub_dir;
296 candidate_file = (gchar *) file;
297 sub_dir = g_strdup ("");
298 while (candidate_file != NULL && fd < 0)
302 path = g_build_filename (data_dir, sub_dir,
303 candidate_file, NULL);
305 fd = g_open (path, O_RDONLY, 0);
313 candidate_file = strchr (candidate_file, '-');
315 if (candidate_file == NULL)
321 sub_dir = g_strndup (file, candidate_file - file - 1);
323 for (p = sub_dir; *p != '\0'; p++)
326 *p = G_DIR_SEPARATOR;
337 g_set_error (error, G_KEY_FILE_ERROR,
338 G_KEY_FILE_ERROR_NOT_FOUND,
339 _("Valid key file could not be "
340 "found in data dirs"));
343 if (output_file != NULL && fd > 0)
344 *output_file = g_strdup (path);
352 g_key_file_load_from_fd (GKeyFile *key_file,
357 GError *key_file_error = NULL;
359 struct stat stat_buf;
360 gchar read_buf[4096];
362 if (fstat (fd, &stat_buf) < 0)
364 g_set_error (error, G_FILE_ERROR,
365 g_file_error_from_errno (errno),
366 "%s", g_strerror (errno));
370 if (!S_ISREG (stat_buf.st_mode))
372 g_set_error (error, G_KEY_FILE_ERROR,
373 G_KEY_FILE_ERROR_PARSE,
374 _("Not a regular file"));
378 if (stat_buf.st_size == 0)
380 g_set_error (error, G_KEY_FILE_ERROR,
381 G_KEY_FILE_ERROR_PARSE,
386 if (key_file->approximate_size > 0)
388 g_key_file_clear (key_file);
389 g_key_file_init (key_file);
391 key_file->flags = flags;
396 bytes_read = read (fd, read_buf, 4096);
398 if (bytes_read == 0) /* End of File */
403 if (errno == EINTR || errno == EAGAIN)
406 g_set_error (error, G_FILE_ERROR,
407 g_file_error_from_errno (errno),
408 "%s", g_strerror (errno));
412 g_key_file_parse_data (key_file,
413 read_buf, bytes_read,
416 while (!key_file_error);
420 g_propagate_error (error, key_file_error);
424 g_key_file_flush_parse_buffer (key_file, &key_file_error);
428 g_propagate_error (error, key_file_error);
436 * g_key_file_load_from_file:
437 * @key_file: an empty #GKeyFile struct
438 * @file: the path of a filename to load, in the GLib file name encoding
439 * @flags: flags from #GKeyFileFlags
440 * @error: return location for a #GError, or %NULL
442 * Loads a key file into an empty #GKeyFile structure.
443 * If the file could not be loaded then %error is set to
444 * either a #GFileError or #GKeyFileError.
446 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
450 g_key_file_load_from_file (GKeyFile *key_file,
455 GError *key_file_error = NULL;
458 g_return_val_if_fail (key_file != NULL, FALSE);
459 g_return_val_if_fail (file != NULL, FALSE);
461 fd = g_open (file, O_RDONLY, 0);
465 g_set_error (error, G_FILE_ERROR,
466 g_file_error_from_errno (errno),
467 "%s", g_strerror (errno));
471 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
476 g_propagate_error (error, key_file_error);
484 * g_key_file_load_from_data:
485 * @key_file: an empty #GKeyFile struct
486 * @data: key file loaded in memory.
487 * @length: the length of @data in bytes
488 * @flags: flags from #GKeyFileFlags
489 * @error: return location for a #GError, or %NULL
491 * Loads a key file from memory into an empty #GKeyFile structure. If
492 * the object cannot be created then %error is set to a
495 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
499 g_key_file_load_from_data (GKeyFile *key_file,
505 GError *key_file_error = NULL;
507 g_return_val_if_fail (key_file != NULL, FALSE);
508 g_return_val_if_fail (data != NULL, FALSE);
509 g_return_val_if_fail (length != 0, FALSE);
511 if (length == (gsize)-1)
512 length = strlen (data);
514 if (key_file->approximate_size > 0)
516 g_key_file_clear (key_file);
517 g_key_file_init (key_file);
519 key_file->flags = flags;
521 g_key_file_parse_data (key_file, data, length, &key_file_error);
525 g_propagate_error (error, key_file_error);
529 g_key_file_flush_parse_buffer (key_file, &key_file_error);
533 g_propagate_error (error, key_file_error);
541 * g_key_file_load_from_data_dirs:
542 * @key_file: an empty #GKeyFile struct
543 * @file: a relative path to a filename to open and parse
544 * @full_path: return location for a string containing the full path
545 * of the file, or %NULL
546 * @flags: flags from #GKeyFileFlags
547 * @error: return location for a #GError, or %NULL
549 * This function looks for a key file named @file in the paths
550 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
551 * loads the file into @key_file and returns the file's full path in
552 * @full_path. If the file could not be loaded then an %error is
553 * set to either a #GFileError or #GKeyFileError.
555 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
559 g_key_file_load_from_data_dirs (GKeyFile *key_file,
565 GError *key_file_error = NULL;
566 gchar **all_data_dirs, **data_dirs;
567 const gchar * user_data_dir;
568 const gchar * const * system_data_dirs;
574 g_return_val_if_fail (key_file != NULL, FALSE);
575 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
577 user_data_dir = g_get_user_data_dir ();
578 system_data_dirs = g_get_system_data_dirs ();
579 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
582 all_data_dirs[i++] = g_strdup (user_data_dir);
585 while (system_data_dirs[j] != NULL)
586 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
589 data_dirs = all_data_dirs;
591 while (*data_dirs != NULL && !found_file)
593 g_free (output_path);
595 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
601 g_propagate_error (error, key_file_error);
605 found_file = g_key_file_load_from_fd (key_file, fd, flags,
611 g_propagate_error (error, key_file_error);
616 if (found_file && full_path)
617 *full_path = output_path;
619 g_free (output_path);
621 g_strfreev (all_data_dirs);
628 * @key_file: a #GKeyFile
635 g_key_file_free (GKeyFile *key_file)
637 g_return_if_fail (key_file != NULL);
639 g_key_file_clear (key_file);
643 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
644 * true for locales that match those in g_get_language_names().
647 g_key_file_locale_is_interesting (GKeyFile *key_file,
650 const gchar * const * current_locales;
653 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
656 current_locales = g_get_language_names ();
658 for (i = 0; current_locales[i] != NULL; i++)
660 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
668 g_key_file_parse_line (GKeyFile *key_file,
673 GError *parse_error = NULL;
676 g_return_if_fail (key_file != NULL);
677 g_return_if_fail (line != NULL);
679 line_start = (gchar *) line;
680 while (g_ascii_isspace (*line_start))
683 if (g_key_file_line_is_comment (line_start))
684 g_key_file_parse_comment (key_file, line, length, &parse_error);
685 else if (g_key_file_line_is_group (line_start))
686 g_key_file_parse_group (key_file, line_start,
687 length - (line_start - line),
689 else if (g_key_file_line_is_key_value_pair (line_start))
690 g_key_file_parse_key_value_pair (key_file, line_start,
691 length - (line_start - line),
695 g_set_error (error, G_KEY_FILE_ERROR,
696 G_KEY_FILE_ERROR_PARSE,
697 _("Key file contains line '%s' which is not "
698 "a key-value pair, group, or comment"), line);
703 g_propagate_error (error, parse_error);
707 g_key_file_parse_comment (GKeyFile *key_file,
712 GKeyFileKeyValuePair *pair;
714 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
717 g_assert (key_file->current_group != NULL);
719 pair = g_new0 (GKeyFileKeyValuePair, 1);
722 pair->value = g_strndup (line, length);
724 key_file->current_group->key_value_pairs =
725 g_list_prepend (key_file->current_group->key_value_pairs, pair);
729 g_key_file_parse_group (GKeyFile *key_file,
735 const gchar *group_name_start, *group_name_end;
737 /* advance past opening '['
739 group_name_start = line + 1;
740 group_name_end = line + length - 1;
742 while (*group_name_end != ']')
745 group_name = g_strndup (group_name_start,
746 group_name_end - group_name_start);
748 g_key_file_add_group (key_file, group_name);
753 g_key_file_parse_key_value_pair (GKeyFile *key_file,
758 gchar *key, *value, *key_end, *value_start, *locale;
759 gsize key_len, value_len;
761 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
763 g_set_error (error, G_KEY_FILE_ERROR,
764 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
765 _("Key file does not start with a group"));
769 key_end = value_start = strchr (line, '=');
771 g_assert (key_end != NULL);
776 /* Pull the key name from the line (chomping trailing whitespace)
778 while (g_ascii_isspace (*key_end))
781 key_len = key_end - line + 2;
783 g_assert (key_len <= length);
785 key = g_strndup (line, key_len - 1);
787 /* Pull the value from the line (chugging leading whitespace)
789 while (g_ascii_isspace (*value_start))
792 value_len = line + length - value_start + 1;
794 value = g_strndup (value_start, value_len);
796 g_assert (key_file->start_group != NULL);
798 if (key_file->current_group
799 && key_file->current_group->name
800 && strcmp (key_file->start_group->name,
801 key_file->current_group->name) == 0
802 && strcmp (key, "Encoding") == 0)
804 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
806 g_set_error (error, G_KEY_FILE_ERROR,
807 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
808 _("Key file contains unsupported encoding '%s'"), value);
816 /* Is this key a translation? If so, is it one that we care about?
818 locale = key_get_locale (key);
820 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
821 g_key_file_add_key (key_file, key_file->current_group, key, value);
829 key_get_locale (const gchar *key)
833 locale = g_strrstr (key, "[");
835 if (locale && strlen (locale) <= 2)
839 locale = g_strndup (locale + 1, strlen (locale) - 2);
845 g_key_file_parse_data (GKeyFile *key_file,
853 g_return_if_fail (key_file != NULL);
854 g_return_if_fail (data != NULL);
858 for (i = 0; i < length; i++)
862 if (i > 0 && data[i - 1] == '\r')
863 g_string_erase (key_file->parse_buffer,
864 key_file->parse_buffer->len - 1,
867 /* When a newline is encountered flush the parse buffer so that the
868 * line can be parsed. Note that completely blank lines won't show
869 * up in the parse buffer, so they get parsed directly.
871 if (key_file->parse_buffer->len > 0)
872 g_key_file_flush_parse_buffer (key_file, &parse_error);
874 g_key_file_parse_comment (key_file, "", 1, &parse_error);
878 g_propagate_error (error, parse_error);
883 g_string_append_c (key_file->parse_buffer, data[i]);
886 key_file->approximate_size += length;
890 g_key_file_flush_parse_buffer (GKeyFile *key_file,
893 GError *file_error = NULL;
895 g_return_if_fail (key_file != NULL);
899 if (key_file->parse_buffer->len > 0)
901 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
902 key_file->parse_buffer->len,
904 g_string_erase (key_file->parse_buffer, 0, -1);
908 g_propagate_error (error, file_error);
915 * g_key_file_to_data:
916 * @key_file: a #GKeyFile
917 * @length: return location for the length of the
918 * returned string, or %NULL
919 * @error: return location for a #GError, or %NULL
921 * This function outputs @key_file as a string.
923 * Return value: a newly allocated string holding
924 * the contents of the #GKeyFile
929 g_key_file_to_data (GKeyFile *key_file,
933 GString *data_string;
935 GList *group_node, *key_file_node;
937 g_return_val_if_fail (key_file != NULL, NULL);
939 data_string = g_string_sized_new (2 * key_file->approximate_size);
941 for (group_node = g_list_last (key_file->groups);
943 group_node = group_node->prev)
945 GKeyFileGroup *group;
947 group = (GKeyFileGroup *) group_node->data;
949 if (group->comment != NULL)
950 g_string_append_printf (data_string, "%s\n", group->comment->value);
951 if (group->name != NULL)
952 g_string_append_printf (data_string, "[%s]\n", group->name);
954 for (key_file_node = g_list_last (group->key_value_pairs);
955 key_file_node != NULL;
956 key_file_node = key_file_node->prev)
958 GKeyFileKeyValuePair *pair;
960 pair = (GKeyFileKeyValuePair *) key_file_node->data;
962 if (pair->key != NULL)
963 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
965 g_string_append_printf (data_string, "%s\n", pair->value);
970 *length = data_string->len;
972 data = data_string->str;
974 g_string_free (data_string, FALSE);
980 * g_key_file_get_keys:
981 * @key_file: a #GKeyFile
982 * @group_name: a group name
983 * @length: return location for the number of keys returned, or %NULL
984 * @error: return location for a #GError, or %NULL
986 * Returns all keys for the group name @group_name. The array of
987 * returned keys will be %NULL-terminated, so @length may
988 * optionally be %NULL. In the event that the @group_name cannot
989 * be found, %NULL is returned and @error is set to
990 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
992 * Return value: a newly-allocated %NULL-terminated array of
993 * strings. Use g_strfreev() to free it.
998 g_key_file_get_keys (GKeyFile *key_file,
999 const gchar *group_name,
1003 GKeyFileGroup *group;
1008 g_return_val_if_fail (key_file != NULL, NULL);
1009 g_return_val_if_fail (group_name != NULL, NULL);
1011 group = g_key_file_lookup_group (key_file, group_name);
1015 g_set_error (error, G_KEY_FILE_ERROR,
1016 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1017 _("Key file does not have group '%s'"),
1018 group_name ? group_name : "(null)");
1022 num_keys = g_list_length (group->key_value_pairs);
1024 keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1026 tmp = group->key_value_pairs;
1027 for (i = 1; i <= num_keys; i++)
1029 GKeyFileKeyValuePair *pair;
1031 pair = (GKeyFileKeyValuePair *) tmp->data;
1032 keys[num_keys - i] = g_strdup (pair->key);
1036 keys[num_keys] = NULL;
1045 * g_key_file_get_start_group:
1046 * @key_file: a #GKeyFile
1048 * Returns the name of the start group of the file.
1050 * Return value: The start group of the key file.
1055 g_key_file_get_start_group (GKeyFile *key_file)
1057 g_return_val_if_fail (key_file != NULL, NULL);
1059 if (key_file->start_group)
1060 return g_strdup (key_file->start_group->name);
1066 * g_key_file_get_groups:
1067 * @key_file: a #GKeyFile
1068 * @length: return location for the number of returned groups, or %NULL
1070 * Returns all groups in the key file loaded with @key_file. The
1071 * array of returned groups will be %NULL-terminated, so @length may
1072 * optionally be %NULL.
1074 * Return value: a newly-allocated %NULL-terminated array of strings.
1075 * Use g_strfreev() to free it.
1079 g_key_file_get_groups (GKeyFile *key_file,
1084 gsize i, num_groups;
1086 g_return_val_if_fail (key_file != NULL, NULL);
1088 num_groups = g_list_length (key_file->groups);
1090 g_assert (num_groups > 0);
1092 /* Only need num_groups instead of num_groups + 1
1093 * because the first group of the file (last in the
1094 * list) is always the comment group at the top,
1097 groups = (gchar **) g_new0 (gchar **, num_groups);
1099 group_node = g_list_last (key_file->groups);
1101 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1104 for (group_node = group_node->prev;
1106 group_node = group_node->prev)
1108 GKeyFileGroup *group;
1110 group = (GKeyFileGroup *) group_node->data;
1112 g_assert (group->name != NULL);
1114 groups[i++] = g_strdup (group->name);
1125 * g_key_file_get_value:
1126 * @key_file: a #GKeyFile
1127 * @group_name: a group name
1129 * @error: return location for a #GError, or %NULL
1131 * Returns the value associated with @key under @group_name.
1133 * In the event the key cannot be found, %NULL is returned and
1134 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1135 * event that the @group_name cannot be found, %NULL is returned
1136 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1138 * Return value: a newly allocated string or %NULL if the specified
1139 * key cannot be found.
1144 g_key_file_get_value (GKeyFile *key_file,
1145 const gchar *group_name,
1149 GKeyFileGroup *group;
1150 GKeyFileKeyValuePair *pair;
1151 gchar *value = NULL;
1153 g_return_val_if_fail (key_file != NULL, NULL);
1154 g_return_val_if_fail (group_name != NULL, NULL);
1155 g_return_val_if_fail (key != NULL, NULL);
1157 group = g_key_file_lookup_group (key_file, group_name);
1161 g_set_error (error, G_KEY_FILE_ERROR,
1162 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1163 _("Key file does not have group '%s'"),
1164 group_name ? group_name : "(null)");
1168 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1171 value = g_strdup (pair->value);
1173 g_set_error (error, G_KEY_FILE_ERROR,
1174 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1175 _("Key file does not have key '%s'"), key);
1181 * g_key_file_set_value:
1182 * @key_file: a #GKeyFile
1183 * @group_name: a group name
1187 * Associates a new value with @key under @group_name. If @key
1188 * cannot be found then it is created. If @group_name cannot be
1189 * found then it is created.
1194 g_key_file_set_value (GKeyFile *key_file,
1195 const gchar *group_name,
1199 GKeyFileGroup *group;
1200 GKeyFileKeyValuePair *pair;
1202 g_return_if_fail (key_file != NULL);
1203 g_return_if_fail (group_name != NULL);
1204 g_return_if_fail (key != NULL);
1205 g_return_if_fail (value != NULL);
1207 group = g_key_file_lookup_group (key_file, group_name);
1211 g_key_file_add_group (key_file, group_name);
1212 group = (GKeyFileGroup *) key_file->groups->data;
1214 g_key_file_add_key (key_file, group, key, value);
1218 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1221 g_key_file_add_key (key_file, group, key, value);
1224 g_free (pair->value);
1225 pair->value = g_strdup (value);
1231 * g_key_file_get_string:
1232 * @key_file: a #GKeyFile
1233 * @group_name: a group name
1235 * @error: return location for a #GError, or %NULL
1237 * Returns the value associated with @key under @group_name.
1239 * In the event the key cannot be found, %NULL is returned and
1240 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1241 * event that the @group_name cannot be found, %NULL is returned
1242 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1244 * Return value: a newly allocated string or %NULL if the specified
1245 * key cannot be found.
1250 g_key_file_get_string (GKeyFile *key_file,
1251 const gchar *group_name,
1255 gchar *value, *string_value;
1256 GError *key_file_error;
1258 g_return_val_if_fail (key_file != NULL, NULL);
1259 g_return_val_if_fail (group_name != NULL, NULL);
1260 g_return_val_if_fail (key != NULL, NULL);
1262 key_file_error = NULL;
1264 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1268 g_propagate_error (error, key_file_error);
1272 if (!g_utf8_validate (value, -1, NULL))
1274 g_set_error (error, G_KEY_FILE_ERROR,
1275 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1276 _("Key file contains key '%s' with value '%s' "
1277 "which is not UTF-8"), key, value);
1282 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1288 if (g_error_matches (key_file_error,
1290 G_KEY_FILE_ERROR_INVALID_VALUE))
1292 g_set_error (error, G_KEY_FILE_ERROR,
1293 G_KEY_FILE_ERROR_INVALID_VALUE,
1294 _("Key file contains key '%s' "
1295 "which has value that cannot be interpreted."),
1297 g_error_free (key_file_error);
1300 g_propagate_error (error, key_file_error);
1303 return string_value;
1307 * g_key_file_set_string:
1308 * @key_file: a #GKeyFile
1309 * @group_name: a group name
1313 * Associates a new string value with @key under @group_name. If
1314 * @key cannot be found then it is created. If @group_name
1315 * cannot be found then it is created.
1320 g_key_file_set_string (GKeyFile *key_file,
1321 const gchar *group_name,
1323 const gchar *string)
1327 g_return_if_fail (key_file != NULL);
1328 g_return_if_fail (group_name != NULL);
1329 g_return_if_fail (key != NULL);
1330 g_return_if_fail (string != NULL);
1332 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1333 g_key_file_set_value (key_file, group_name, key, value);
1338 * g_key_file_get_string_list:
1339 * @key_file: a #GKeyFile
1340 * @group_name: a group name
1342 * @length: return location for the number of returned strings, or %NULL
1343 * @error: return location for a #GError, or %NULL
1345 * Returns the values associated with @key under @group_name.
1347 * In the event the key cannot be found, %NULL is returned and
1348 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1349 * event that the @group_name cannot be found, %NULL is returned
1350 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1352 * Return value: a %NULL-terminated string array or %NULL if the specified
1353 * key cannot be found. The array should be freed with g_strfreev().
1358 g_key_file_get_string_list (GKeyFile *key_file,
1359 const gchar *group_name,
1364 GError *key_file_error = NULL;
1365 gchar *value, *string_value, **values;
1367 GSList *p, *pieces = NULL;
1369 g_return_val_if_fail (key_file != NULL, NULL);
1370 g_return_val_if_fail (group_name != NULL, NULL);
1371 g_return_val_if_fail (key != NULL, NULL);
1373 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1377 g_propagate_error (error, key_file_error);
1381 if (!g_utf8_validate (value, -1, NULL))
1383 g_set_error (error, G_KEY_FILE_ERROR,
1384 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1385 _("Key file contains key '%s' with value '%s' "
1386 "which is not UTF-8"), key, value);
1391 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1393 g_free (string_value);
1397 if (g_error_matches (key_file_error,
1399 G_KEY_FILE_ERROR_INVALID_VALUE))
1401 g_set_error (error, G_KEY_FILE_ERROR,
1402 G_KEY_FILE_ERROR_INVALID_VALUE,
1403 _("Key file contains key '%s' "
1404 "which has value that cannot be interpreted."),
1406 g_error_free (key_file_error);
1409 g_propagate_error (error, key_file_error);
1412 len = g_slist_length (pieces);
1413 values = g_new0 (gchar *, len + 1);
1414 for (p = pieces, i = 0; p; p = p->next)
1415 values[i++] = p->data;
1418 g_slist_free (pieces);
1427 * g_key_file_set_string_list:
1428 * @key_file: a #GKeyFile
1429 * @group_name: a group name
1431 * @list: an array of locale string values
1432 * @length: number of locale string values in @list
1434 * Associates a list of string values for @key under @group_name.
1435 * If @key cannot be found then it is created. If @group_name
1436 * cannot be found then it is created.
1441 g_key_file_set_string_list (GKeyFile *key_file,
1442 const gchar *group_name,
1444 const gchar * const list[],
1447 GString *value_list;
1450 g_return_if_fail (key_file != NULL);
1451 g_return_if_fail (group_name != NULL);
1452 g_return_if_fail (key != NULL);
1453 g_return_if_fail (list != NULL);
1455 value_list = g_string_sized_new (length * 128);
1456 for (i = 0; list[i] != NULL && i < length; i++)
1460 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1461 g_string_append (value_list, value);
1462 g_string_append_c (value_list, key_file->list_separator);
1467 g_key_file_set_value (key_file, group_name, key, value_list->str);
1468 g_string_free (value_list, TRUE);
1472 * g_key_file_set_locale_string:
1473 * @key_file: a #GKeyFile
1474 * @group_name: a group name
1479 * Associates a string value for @key and @locale under
1480 * @group_name. If the translation for @key cannot be found
1481 * then it is created.
1486 g_key_file_set_locale_string (GKeyFile *key_file,
1487 const gchar *group_name,
1489 const gchar *locale,
1490 const gchar *string)
1492 gchar *full_key, *value;
1494 g_return_if_fail (key_file != NULL);
1495 g_return_if_fail (group_name != NULL);
1496 g_return_if_fail (key != NULL);
1497 g_return_if_fail (locale != NULL);
1498 g_return_if_fail (string != NULL);
1500 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1501 full_key = g_strdup_printf ("%s[%s]", key, locale);
1502 g_key_file_set_value (key_file, group_name, full_key, value);
1507 extern GSList *_g_compute_locale_variants (const gchar *locale);
1510 * g_key_file_get_locale_string:
1511 * @key_file: a #GKeyFile
1512 * @group_name: a group name
1514 * @locale: a locale or %NULL
1515 * @error: return location for a #GError, or %NULL
1517 * Returns the value associated with @key under @group_name
1518 * translated in the given @locale if available. If @locale is
1519 * %NULL then the current locale is assumed.
1521 * If @key cannot be found then %NULL is returned and @error is set to
1522 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1523 * with @key cannot be interpreted or no suitable translation can
1524 * be found then the untranslated value is returned.
1526 * Return value: a newly allocated string or %NULL if the specified
1527 * key cannot be found.
1532 g_key_file_get_locale_string (GKeyFile *key_file,
1533 const gchar *group_name,
1535 const gchar *locale,
1538 gchar *candidate_key, *translated_value;
1539 GError *key_file_error;
1541 gboolean free_languages = FALSE;
1544 g_return_val_if_fail (key_file != NULL, NULL);
1545 g_return_val_if_fail (group_name != NULL, NULL);
1546 g_return_val_if_fail (key != NULL, NULL);
1548 candidate_key = NULL;
1549 translated_value = NULL;
1550 key_file_error = NULL;
1556 list = _g_compute_locale_variants (locale);
1558 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1559 for (l = list, i = 0; l; l = l->next, i++)
1560 languages[i] = l->data;
1561 languages[i] = NULL;
1563 g_slist_free (list);
1564 free_languages = TRUE;
1568 languages = (gchar **) g_get_language_names ();
1569 free_languages = FALSE;
1572 for (i = 0; languages[i]; i++)
1574 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1576 translated_value = g_key_file_get_string (key_file,
1578 candidate_key, NULL);
1579 g_free (candidate_key);
1581 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1584 g_free (translated_value);
1585 translated_value = NULL;
1588 /* Fallback to untranslated key
1590 if (!translated_value)
1592 translated_value = g_key_file_get_string (key_file, group_name, key,
1595 if (!translated_value)
1596 g_propagate_error (error, key_file_error);
1600 g_strfreev (languages);
1602 return translated_value;
1606 * g_key_file_get_locale_string_list:
1607 * @key_file: a #GKeyFile
1608 * @group_name: a group name
1611 * @length: return location for the number of returned strings or %NULL
1612 * @error: return location for a #GError or %NULL
1614 * Returns the values associated with @key under @group_name
1615 * translated in the given @locale if available. If @locale is
1616 * %NULL then the current locale is assumed.
1618 * If @key cannot be found then %NULL is returned and @error is set to
1619 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1620 * with @key cannot be interpreted or no suitable translations
1621 * can be found then the untranslated values are returned.
1622 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1624 * Return value: a newly allocated %NULL-terminated string array
1625 * or %NULL if the key isn't found. The string array should be freed
1626 * with g_strfreev().
1631 g_key_file_get_locale_string_list (GKeyFile *key_file,
1632 const gchar *group_name,
1634 const gchar *locale,
1638 GError *key_file_error;
1639 gchar **values, *value;
1641 g_return_val_if_fail (key_file != NULL, NULL);
1642 g_return_val_if_fail (group_name != NULL, NULL);
1643 g_return_val_if_fail (key != NULL, NULL);
1645 key_file_error = NULL;
1647 value = g_key_file_get_locale_string (key_file, group_name,
1652 g_propagate_error (error, key_file_error);
1657 if (value[strlen (value) - 1] == ';')
1658 value[strlen (value) - 1] = '\0';
1660 values = g_strsplit (value, ";", 0);
1665 *length = g_strv_length (values);
1671 * g_key_file_set_locale_string_list:
1672 * @key_file: a #GKeyFile
1673 * @group_name: a group name
1676 * @list: a %NULL-terminated array of locale string values
1677 * @length: the length of @list
1679 * Associates a list of string values for @key and @locale under
1680 * @group_name. If the translation for @key cannot be found then
1686 g_key_file_set_locale_string_list (GKeyFile *key_file,
1687 const gchar *group_name,
1689 const gchar *locale,
1690 const gchar * const list[],
1693 GString *value_list;
1697 g_return_if_fail (key_file != NULL);
1698 g_return_if_fail (group_name != NULL);
1699 g_return_if_fail (key != NULL);
1700 g_return_if_fail (locale != NULL);
1701 g_return_if_fail (length != 0);
1703 value_list = g_string_sized_new (length * 128);
1704 for (i = 0; list[i] != NULL && i < length; i++)
1708 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1710 g_string_append (value_list, value);
1711 g_string_append_c (value_list, ';');
1716 full_key = g_strdup_printf ("%s[%s]", key, locale);
1717 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1719 g_string_free (value_list, TRUE);
1723 * g_key_file_get_boolean:
1724 * @key_file: a #GKeyFile
1725 * @group_name: a group name
1727 * @error: return location for a #GError
1729 * Returns the value associated with @key under @group_name as a
1732 * If @key cannot be found then the return value is undefined and
1733 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1734 * the value associated with @key cannot be interpreted as a boolean
1735 * then the return value is also undefined and @error is set to
1736 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1738 * Return value: the value associated with the key as a boolean
1742 g_key_file_get_boolean (GKeyFile *key_file,
1743 const gchar *group_name,
1747 GError *key_file_error = NULL;
1749 gboolean bool_value;
1751 g_return_val_if_fail (key_file != NULL, FALSE);
1752 g_return_val_if_fail (group_name != NULL, FALSE);
1753 g_return_val_if_fail (key != NULL, FALSE);
1755 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1759 g_propagate_error (error, key_file_error);
1763 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1769 if (g_error_matches (key_file_error,
1771 G_KEY_FILE_ERROR_INVALID_VALUE))
1773 g_set_error (error, G_KEY_FILE_ERROR,
1774 G_KEY_FILE_ERROR_INVALID_VALUE,
1775 _("Key file contains key '%s' "
1776 "which has value that cannot be interpreted."),
1778 g_error_free (key_file_error);
1781 g_propagate_error (error, key_file_error);
1788 * g_key_file_set_boolean:
1789 * @key_file: a #GKeyFile
1790 * @group_name: a group name
1792 * @value: %TRUE or %FALSE
1794 * Associates a new boolean value with @key under @group_name.
1795 * If @key cannot be found then it is created.
1800 g_key_file_set_boolean (GKeyFile *key_file,
1801 const gchar *group_name,
1807 g_return_if_fail (key_file != NULL);
1808 g_return_if_fail (group_name != NULL);
1809 g_return_if_fail (key != NULL);
1811 result = g_key_file_parse_boolean_as_value (key_file, value);
1812 g_key_file_set_value (key_file, group_name, key, result);
1817 * g_key_file_get_boolean_list:
1818 * @key_file: a #GKeyFile
1819 * @group_name: a group name
1821 * @length: the number of booleans returned
1822 * @error: return location for a #GError
1824 * Returns the values associated with @key under @group_name as
1825 * booleans. If @group_name is %NULL, the start_group is used.
1827 * If @key cannot be found then the return value is undefined and
1828 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1829 * the values associated with @key cannot be interpreted as booleans
1830 * then the return value is also undefined and @error is set to
1831 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1833 * Return value: the values associated with the key as a boolean
1838 g_key_file_get_boolean_list (GKeyFile *key_file,
1839 const gchar *group_name,
1844 GError *key_file_error;
1846 gboolean *bool_values;
1849 g_return_val_if_fail (key_file != NULL, NULL);
1850 g_return_val_if_fail (group_name != NULL, NULL);
1851 g_return_val_if_fail (key != NULL, NULL);
1853 key_file_error = NULL;
1855 values = g_key_file_get_string_list (key_file, group_name, key,
1856 &num_bools, &key_file_error);
1859 g_propagate_error (error, key_file_error);
1864 bool_values = g_new0 (gboolean, num_bools);
1866 for (i = 0; i < num_bools; i++)
1868 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1874 g_propagate_error (error, key_file_error);
1875 g_strfreev (values);
1876 g_free (bool_values);
1881 g_strfreev (values);
1884 *length = num_bools;
1890 * g_key_file_set_boolean_list:
1891 * @key_file: a #GKeyFile
1892 * @group_name: a group name
1894 * @list: an array of boolean values
1895 * @length: length of @list
1897 * Associates a list of boolean values with @key under
1898 * @group_name. If @key cannot be found then it is created.
1899 * If @group_name is %NULL, the start_group is used.
1904 g_key_file_set_boolean_list (GKeyFile *key_file,
1905 const gchar *group_name,
1910 GString *value_list;
1913 g_return_if_fail (key_file != NULL);
1914 g_return_if_fail (group_name != NULL);
1915 g_return_if_fail (key != NULL);
1916 g_return_if_fail (list != NULL);
1918 value_list = g_string_sized_new (length * 8);
1919 for (i = 0; i < length; i++)
1923 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1925 g_string_append (value_list, value);
1926 g_string_append_c (value_list, key_file->list_separator);
1931 g_key_file_set_value (key_file, group_name, key, value_list->str);
1932 g_string_free (value_list, TRUE);
1936 * g_key_file_get_integer:
1937 * @key_file: a #GKeyFile
1938 * @group_name: a group name
1940 * @error: return location for a #GError
1942 * Returns the value associated with @key under @group_name as an
1943 * integer. If @group_name is %NULL, the start_group is used.
1945 * If @key cannot be found then the return value is undefined and
1946 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1947 * the value associated with @key cannot be interpreted as an integer
1948 * then the return value is also undefined and @error is set to
1949 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1951 * Return value: the value associated with the key as an integer.
1956 g_key_file_get_integer (GKeyFile *key_file,
1957 const gchar *group_name,
1961 GError *key_file_error;
1965 g_return_val_if_fail (key_file != NULL, -1);
1966 g_return_val_if_fail (group_name != NULL, -1);
1967 g_return_val_if_fail (key != NULL, -1);
1969 key_file_error = NULL;
1971 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1975 g_propagate_error (error, key_file_error);
1979 int_value = g_key_file_parse_value_as_integer (key_file, value,
1985 if (g_error_matches (key_file_error,
1987 G_KEY_FILE_ERROR_INVALID_VALUE))
1989 g_set_error (error, G_KEY_FILE_ERROR,
1990 G_KEY_FILE_ERROR_INVALID_VALUE,
1991 _("Key file contains key '%s' in group '%s' "
1992 "which has value that cannot be interpreted."), key,
1994 g_error_free (key_file_error);
1997 g_propagate_error (error, key_file_error);
2004 * g_key_file_set_integer:
2005 * @key_file: a #GKeyFile
2006 * @group_name: a group name
2008 * @value: an integer value
2010 * Associates a new integer value with @key under @group_name.
2011 * If @key cannot be found then it is created.
2016 g_key_file_set_integer (GKeyFile *key_file,
2017 const gchar *group_name,
2023 g_return_if_fail (key_file != NULL);
2024 g_return_if_fail (group_name != NULL);
2025 g_return_if_fail (key != NULL);
2027 result = g_key_file_parse_integer_as_value (key_file, value);
2028 g_key_file_set_value (key_file, group_name, key, result);
2033 * g_key_file_get_integer_list:
2034 * @key_file: a #GKeyFile
2035 * @group_name: a group name
2037 * @length: the number of integers returned
2038 * @error: return location for a #GError
2040 * Returns the values associated with @key under @group_name as
2043 * If @key cannot be found then the return value is undefined and
2044 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2045 * the values associated with @key cannot be interpreted as integers
2046 * then the return value is also undefined and @error is set to
2047 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2049 * Return value: the values associated with the key as a integer
2054 g_key_file_get_integer_list (GKeyFile *key_file,
2055 const gchar *group_name,
2060 GError *key_file_error = NULL;
2065 g_return_val_if_fail (key_file != NULL, NULL);
2066 g_return_val_if_fail (group_name != NULL, NULL);
2067 g_return_val_if_fail (key != NULL, NULL);
2069 values = g_key_file_get_string_list (key_file, group_name, key,
2070 &num_ints, &key_file_error);
2073 g_propagate_error (error, key_file_error);
2078 int_values = g_new0 (gint, num_ints);
2080 for (i = 0; i < num_ints; i++)
2082 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2088 g_propagate_error (error, key_file_error);
2089 g_strfreev (values);
2090 g_free (int_values);
2095 g_strfreev (values);
2104 * g_key_file_set_integer_list:
2105 * @key_file: a #GKeyFile
2106 * @group_name: a group name
2108 * @list: an array of integer values
2109 * @length: number of integer values in @list
2111 * Associates a list of integer values with @key under
2112 * @group_name. If @key cannot be found then it is created.
2117 g_key_file_set_integer_list (GKeyFile *key_file,
2118 const gchar *group_name,
2126 g_return_if_fail (key_file != NULL);
2127 g_return_if_fail (group_name != NULL);
2128 g_return_if_fail (key != NULL);
2129 g_return_if_fail (list != NULL);
2131 values = g_string_sized_new (length * 16);
2132 for (i = 0; i < length; i++)
2136 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2138 g_string_append (values, value);
2139 g_string_append_c (values, ';');
2144 g_key_file_set_value (key_file, group_name, key, values->str);
2145 g_string_free (values, TRUE);
2149 g_key_file_set_key_comment (GKeyFile *key_file,
2150 const gchar *group_name,
2152 const gchar *comment,
2155 GKeyFileGroup *group;
2156 GKeyFileKeyValuePair *pair;
2157 GList *key_node, *comment_node, *tmp;
2159 group = g_key_file_lookup_group (key_file, group_name);
2162 g_set_error (error, G_KEY_FILE_ERROR,
2163 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2164 _("Key file does not have group '%s'"),
2165 group_name ? group_name : "(null)");
2170 /* First find the key the comments are supposed to be
2173 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2175 if (key_node == NULL)
2177 g_set_error (error, G_KEY_FILE_ERROR,
2178 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2179 _("Key file does not have key '%s' in group '%s'"),
2184 /* Then find all the comments already associated with the
2187 tmp = key_node->next;
2190 GKeyFileKeyValuePair *pair;
2192 pair = (GKeyFileKeyValuePair *) tmp->data;
2194 if (pair->key != NULL)
2199 g_key_file_remove_key_value_pair_node (key_file, group,
2203 if (comment == NULL)
2206 /* Now we can add our new comment
2208 pair = g_new0 (GKeyFileKeyValuePair, 1);
2211 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2213 key_node = g_list_insert (key_node, pair, 1);
2217 g_key_file_set_group_comment (GKeyFile *key_file,
2218 const gchar *group_name,
2219 const gchar *comment,
2222 GKeyFileGroup *group;
2224 group = g_key_file_lookup_group (key_file, group_name);
2227 g_set_error (error, G_KEY_FILE_ERROR,
2228 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2229 _("Key file does not have group '%s'"),
2230 group_name ? group_name : "(null)");
2235 /* First remove any existing comment
2239 g_key_file_key_value_pair_free (group->comment);
2240 group->comment = NULL;
2243 if (comment == NULL)
2246 /* Now we can add our new comment
2248 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2250 group->comment->key = NULL;
2251 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2255 g_key_file_set_top_comment (GKeyFile *key_file,
2256 const gchar *comment,
2260 GKeyFileGroup *group;
2261 GKeyFileKeyValuePair *pair;
2263 /* The last group in the list should be the top (comments only)
2266 g_assert (key_file->groups != NULL);
2267 group_node = g_list_last (key_file->groups);
2268 group = (GKeyFileGroup *) group_node->data;
2269 g_assert (group->name == NULL);
2271 /* Note all keys must be comments at the top of
2272 * the file, so we can just free it all.
2274 if (group->key_value_pairs != NULL)
2276 g_list_foreach (group->key_value_pairs,
2277 (GFunc) g_key_file_key_value_pair_free,
2279 g_list_free (group->key_value_pairs);
2280 group->key_value_pairs = NULL;
2283 if (comment == NULL)
2286 pair = g_new0 (GKeyFileKeyValuePair, 1);
2289 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2291 group->key_value_pairs =
2292 g_list_prepend (group->key_value_pairs, pair);
2296 * g_key_file_set_comment:
2297 * @key_file: a #GKeyFile
2298 * @group_name: a group name, or %NULL
2300 * @comment: a comment
2301 * @error: return location for a #GError
2303 * Places a comment above @key from @group_name.
2304 * @group_name. If @key is %NULL then @comment will
2305 * be written above @group_name. If both @key
2306 * and @group_name are NULL, then @comment will
2307 * be written above the first group in the file.
2312 g_key_file_set_comment (GKeyFile *key_file,
2313 const gchar *group_name,
2315 const gchar *comment,
2318 g_return_if_fail (key_file != NULL);
2320 if (group_name != NULL && key != NULL)
2321 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2322 else if (group_name != NULL)
2323 g_key_file_set_group_comment (key_file, group_name, comment, error);
2325 g_key_file_set_top_comment (key_file, comment, error);
2327 if (comment != NULL)
2328 key_file->approximate_size += strlen (comment);
2332 g_key_file_get_key_comment (GKeyFile *key_file,
2333 const gchar *group_name,
2337 GKeyFileGroup *group;
2338 GList *key_node, *tmp;
2342 group = g_key_file_lookup_group (key_file, group_name);
2345 g_set_error (error, G_KEY_FILE_ERROR,
2346 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2347 _("Key file does not have group '%s'"),
2348 group_name ? group_name : "(null)");
2353 /* First find the key the comments are supposed to be
2356 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2358 if (key_node == NULL)
2360 g_set_error (error, G_KEY_FILE_ERROR,
2361 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2362 _("Key file does not have key '%s' in group '%s'"),
2369 /* Then find all the comments already associated with the
2370 * key and concatentate them.
2372 tmp = key_node->next;
2375 GKeyFileKeyValuePair *pair;
2377 pair = (GKeyFileKeyValuePair *) tmp->data;
2379 if (pair->key != NULL)
2383 string = g_string_sized_new (512);
2385 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2386 g_string_append (string, comment);
2394 comment = string->str;
2395 g_string_free (string, FALSE);
2404 g_key_file_get_group_comment (GKeyFile *key_file,
2405 const gchar *group_name,
2408 GKeyFileGroup *group;
2410 group = g_key_file_lookup_group (key_file, group_name);
2413 g_set_error (error, G_KEY_FILE_ERROR,
2414 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2415 _("Key file does not have group '%s'"),
2416 group_name ? group_name : "(null)");
2422 return g_strdup (group->comment->value);
2428 g_key_file_get_top_comment (GKeyFile *key_file,
2431 GList *group_node, *tmp;
2432 GKeyFileGroup *group;
2436 /* The last group in the list should be the top (comments only)
2439 g_assert (key_file->groups != NULL);
2440 group_node = g_list_last (key_file->groups);
2441 group = (GKeyFileGroup *) group_node->data;
2442 g_assert (group->name == NULL);
2446 /* Then find all the comments already associated with the
2447 * key and concatentate them.
2449 tmp = group->key_value_pairs;
2452 GKeyFileKeyValuePair *pair;
2454 pair = (GKeyFileKeyValuePair *) tmp->data;
2456 if (pair->key != NULL)
2460 string = g_string_sized_new (512);
2462 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2463 g_string_append (string, comment);
2471 comment = string->str;
2472 g_string_free (string, FALSE);
2481 * g_key_file_get_comment:
2482 * @key_file: a #GKeyFile
2483 * @group_name: a group name, or %NULL
2485 * @error: return location for a #GError
2487 * Retreives a comment above @key from @group_name.
2488 * @group_name. If @key is %NULL then @comment will
2489 * be read from above @group_name. If both @key
2490 * and @group_name are NULL, then @comment will
2491 * be read from above the first group in the file.
2494 * Returns: a comment that should be freed with g_free()
2497 g_key_file_get_comment (GKeyFile *key_file,
2498 const gchar *group_name,
2502 g_return_val_if_fail (key_file != NULL, NULL);
2504 if (group_name != NULL && key != NULL)
2505 return g_key_file_get_key_comment (key_file, group_name, key, error);
2506 else if (group_name != NULL)
2507 return g_key_file_get_group_comment (key_file, group_name, error);
2509 return g_key_file_get_top_comment (key_file, error);
2513 * g_key_file_remove_comment:
2514 * @key_file: a #GKeyFile
2515 * @group_name: a group name, or %NULL
2517 * @error: return location for a #GError
2519 * Removes a comment above @key from @group_name.
2520 * @group_name. If @key is %NULL then @comment will
2521 * be written above @group_name. If both @key
2522 * and @group_name are NULL, then @comment will
2523 * be written above the first group in the file.
2529 g_key_file_remove_comment (GKeyFile *key_file,
2530 const gchar *group_name,
2534 g_return_if_fail (key_file != NULL);
2536 if (group_name != NULL && key != NULL)
2537 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2538 else if (group_name != NULL)
2539 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2541 g_key_file_set_top_comment (key_file, NULL, error);
2545 * g_key_file_has_group:
2546 * @key_file: a #GKeyFile
2547 * @group_name: a group name
2549 * Looks whether the key file has the group @group_name.
2551 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2556 g_key_file_has_group (GKeyFile *key_file,
2557 const gchar *group_name)
2559 g_return_val_if_fail (key_file != NULL, FALSE);
2560 g_return_val_if_fail (group_name != NULL, FALSE);
2562 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2566 * g_key_file_has_key:
2567 * @key_file: a #GKeyFile
2568 * @group_name: a group name
2570 * @error: return location for a #GError
2572 * Looks whether the key file has the key @key in the group
2575 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2581 g_key_file_has_key (GKeyFile *key_file,
2582 const gchar *group_name,
2586 GKeyFileKeyValuePair *pair;
2587 GKeyFileGroup *group;
2589 g_return_val_if_fail (key_file != NULL, FALSE);
2590 g_return_val_if_fail (group_name != NULL, FALSE);
2591 g_return_val_if_fail (key != NULL, FALSE);
2593 group = g_key_file_lookup_group (key_file, group_name);
2597 g_set_error (error, G_KEY_FILE_ERROR,
2598 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2599 _("Key file does not have group '%s'"),
2600 group_name ? group_name : "(null)");
2605 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2607 return pair != NULL;
2611 g_key_file_add_group (GKeyFile *key_file,
2612 const gchar *group_name)
2614 GKeyFileGroup *group;
2616 g_return_if_fail (key_file != NULL);
2617 g_return_if_fail (group_name != NULL);
2618 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2620 group = g_new0 (GKeyFileGroup, 1);
2621 group->name = g_strdup (group_name);
2622 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2623 key_file->groups = g_list_prepend (key_file->groups, group);
2624 key_file->approximate_size += strlen (group_name) + 3;
2625 key_file->current_group = group;
2627 if (key_file->start_group == NULL)
2628 key_file->start_group = group;
2632 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2637 g_free (pair->value);
2642 /* Be careful not to call this function on a node with data in the
2643 * lookup map without removing it from the lookup map, first.
2645 * Some current cases where this warning is not a concern are
2647 * - the node being removed is a comment node
2648 * - the entire lookup map is getting destroyed soon after
2652 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2653 GKeyFileGroup *group,
2657 GKeyFileKeyValuePair *pair;
2659 pair = (GKeyFileKeyValuePair *) pair_node->data;
2661 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2663 if (pair->key != NULL)
2664 key_file->approximate_size -= strlen (pair->key) + 1;
2666 g_assert (pair->value != NULL);
2667 key_file->approximate_size -= strlen (pair->value);
2669 g_key_file_key_value_pair_free (pair);
2671 g_list_free_1 (pair_node);
2675 g_key_file_remove_group_node (GKeyFile *key_file,
2678 GKeyFileGroup *group;
2681 group = (GKeyFileGroup *) group_node->data;
2683 /* If the current group gets deleted make the current group the last
2686 if (key_file->current_group == group)
2688 /* groups should always contain at least the top comment group,
2689 * unless g_key_file_clear has been called
2691 if (key_file->groups)
2692 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2694 key_file->current_group = NULL;
2697 /* If the start group gets deleted make the start group the first
2700 if (key_file->start_group == group)
2702 tmp = g_list_last (key_file->groups);
2705 if (tmp != group_node &&
2706 ((GKeyFileGroup *) tmp->data)->name != NULL)
2713 key_file->start_group = (GKeyFileGroup *) tmp->data;
2715 key_file->start_group = NULL;
2718 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2720 if (group->name != NULL)
2721 key_file->approximate_size -= strlen (group->name) + 3;
2723 tmp = group->key_value_pairs;
2730 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2733 g_assert (group->key_value_pairs == NULL);
2735 if (group->lookup_map)
2737 g_hash_table_destroy (group->lookup_map);
2738 group->lookup_map = NULL;
2741 g_free ((gchar *) group->name);
2743 g_list_free_1 (group_node);
2747 * g_key_file_remove_group:
2748 * @key_file: a #GKeyFile
2749 * @group_name: a group name
2750 * @error: return location for a #GError or %NULL
2752 * Removes the specified group, @group_name,
2753 * from the key file.
2758 g_key_file_remove_group (GKeyFile *key_file,
2759 const gchar *group_name,
2764 g_return_if_fail (key_file != NULL);
2765 g_return_if_fail (group_name != NULL);
2767 group_node = g_key_file_lookup_group_node (key_file, group_name);
2771 g_set_error (error, G_KEY_FILE_ERROR,
2772 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2773 _("Key file does not have group '%s'"),
2778 g_key_file_remove_group_node (key_file, group_node);
2782 g_key_file_add_key (GKeyFile *key_file,
2783 GKeyFileGroup *group,
2787 GKeyFileKeyValuePair *pair;
2789 pair = g_new0 (GKeyFileKeyValuePair, 1);
2791 pair->key = g_strdup (key);
2792 pair->value = g_strdup (value);
2794 g_hash_table_replace (group->lookup_map, pair->key, pair);
2795 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2796 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2800 * g_key_file_remove_key:
2801 * @key_file: a #GKeyFile
2802 * @group_name: a group name
2803 * @key: a key name to remove
2804 * @error: return location for a #GError or %NULL
2806 * Removes @key in @group_name from the key file.
2811 g_key_file_remove_key (GKeyFile *key_file,
2812 const gchar *group_name,
2816 GKeyFileGroup *group;
2817 GKeyFileKeyValuePair *pair;
2819 g_return_if_fail (key_file != NULL);
2820 g_return_if_fail (group_name != NULL);
2821 g_return_if_fail (key != NULL);
2825 group = g_key_file_lookup_group (key_file, group_name);
2828 g_set_error (error, G_KEY_FILE_ERROR,
2829 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2830 _("Key file does not have group '%s'"),
2831 group_name ? group_name : "(null)");
2835 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2839 g_set_error (error, G_KEY_FILE_ERROR,
2840 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2841 _("Key file does not have key '%s' in group '%s'"),
2846 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2848 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
2849 g_hash_table_remove (group->lookup_map, pair->key);
2850 g_key_file_key_value_pair_free (pair);
2854 g_key_file_lookup_group_node (GKeyFile *key_file,
2855 const gchar *group_name)
2857 GKeyFileGroup *group;
2861 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2863 group = (GKeyFileGroup *) tmp->data;
2865 if (group && group->name && strcmp (group->name, group_name) == 0)
2874 static GKeyFileGroup *
2875 g_key_file_lookup_group (GKeyFile *key_file,
2876 const gchar *group_name)
2880 group_node = g_key_file_lookup_group_node (key_file, group_name);
2882 if (group_node != NULL)
2883 return (GKeyFileGroup *) group_node->data;
2889 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2890 GKeyFileGroup *group,
2895 for (key_node = group->key_value_pairs;
2897 key_node = key_node->next)
2899 GKeyFileKeyValuePair *pair;
2901 pair = (GKeyFileKeyValuePair *) key_node->data;
2903 if (pair->key && strcmp (pair->key, key) == 0)
2910 static GKeyFileKeyValuePair *
2911 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2912 GKeyFileGroup *group,
2915 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2918 /* Lines starting with # or consisting entirely of whitespace are merely
2919 * recorded, not parsed. This function assumes all leading whitespace
2920 * has been stripped.
2923 g_key_file_line_is_comment (const gchar *line)
2925 return (*line == '#' || *line == '\0' || *line == '\n');
2928 /* A group in a key file is made up of a starting '[' followed by one
2929 * or more letters making up the group name followed by ']'.
2932 g_key_file_line_is_group (const gchar *line)
2940 p = g_utf8_next_char (p);
2945 p = g_utf8_next_char (p);
2947 /* Group name must be non-empty
2952 while (*p && *p != ']')
2953 p = g_utf8_next_char (p);
2962 g_key_file_line_is_key_value_pair (const gchar *line)
2966 p = (gchar *) g_utf8_strchr (line, -1, '=');
2971 /* Key must be non-empty
2980 g_key_file_parse_value_as_string (GKeyFile *key_file,
2985 gchar *string_value, *p, *q0, *q;
2987 string_value = g_new0 (gchar, strlen (value) + 1);
2989 p = (gchar *) value;
2990 q0 = q = string_value;
3020 g_set_error (error, G_KEY_FILE_ERROR,
3021 G_KEY_FILE_ERROR_INVALID_VALUE,
3022 _("Key file contains escape character "
3027 if (pieces && *p == key_file->list_separator)
3028 *q = key_file->list_separator;
3042 g_set_error (error, G_KEY_FILE_ERROR,
3043 G_KEY_FILE_ERROR_INVALID_VALUE,
3044 _("Key file contains invalid escape "
3045 "sequence '%s'"), sequence);
3054 if (pieces && (*p == key_file->list_separator))
3056 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3072 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3073 *pieces = g_slist_reverse (*pieces);
3076 return string_value;
3080 g_key_file_parse_string_as_value (GKeyFile *key_file,
3081 const gchar *string,
3082 gboolean escape_separator)
3084 gchar *value, *p, *q;
3086 gboolean parsing_leading_space;
3088 length = strlen (string) + 1;
3090 /* Worst case would be that every character needs to be escaped.
3091 * In other words every character turns to two characters
3093 value = g_new0 (gchar, 2 * length);
3095 p = (gchar *) string;
3097 parsing_leading_space = TRUE;
3098 while (p < (string + length - 1))
3100 gchar escaped_character[3] = { '\\', 0, 0 };
3105 if (parsing_leading_space)
3107 escaped_character[1] = 's';
3108 strcpy (q, escaped_character);
3118 if (parsing_leading_space)
3120 escaped_character[1] = 't';
3121 strcpy (q, escaped_character);
3131 escaped_character[1] = 'n';
3132 strcpy (q, escaped_character);
3136 escaped_character[1] = 'r';
3137 strcpy (q, escaped_character);
3141 escaped_character[1] = '\\';
3142 strcpy (q, escaped_character);
3144 parsing_leading_space = FALSE;
3147 if (escape_separator && *p == key_file->list_separator)
3149 escaped_character[1] = key_file->list_separator;
3150 strcpy (q, escaped_character);
3152 parsing_leading_space = TRUE;
3158 parsing_leading_space = FALSE;
3170 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3174 gchar *end_of_valid_int;
3179 long_value = strtol (value, &end_of_valid_int, 10);
3181 if (*value == '\0' || *end_of_valid_int != '\0')
3183 g_set_error (error, G_KEY_FILE_ERROR,
3184 G_KEY_FILE_ERROR_INVALID_VALUE,
3185 _("Value '%s' cannot be interpreted as a number."), value);
3189 int_value = long_value;
3190 if (int_value != long_value || errno == ERANGE)
3194 G_KEY_FILE_ERROR_INVALID_VALUE,
3195 _("Integer value '%s' out of range"), value);
3203 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3207 return g_strdup_printf ("%d", value);
3211 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3217 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3219 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3223 g_set_error (error, G_KEY_FILE_ERROR,
3224 G_KEY_FILE_ERROR_INVALID_VALUE,
3225 _("Value '%s' cannot be interpreted as a boolean."), value);
3231 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3235 return g_strdup ("true");
3237 return g_strdup ("false");
3241 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3245 gchar **lines, *comment;
3248 string = g_string_sized_new (512);
3250 lines = g_strsplit (value, "\n", 0);
3252 for (i = 0; lines[i] != NULL; i++)
3254 if (lines[i][0] != '#')
3255 g_string_append_printf (string, "%s\n", lines[i]);
3257 g_string_append_printf (string, "%s\n", lines[i] + 1);
3261 comment = string->str;
3263 g_string_free (string, FALSE);
3269 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3270 const gchar *comment)
3273 gchar **lines, *value;
3276 string = g_string_sized_new (512);
3278 lines = g_strsplit (comment, "\n", 0);
3280 for (i = 0; lines[i] != NULL; i++)
3281 g_string_append_printf (string, "#%s%s", lines[i],
3282 lines[i + 1] == NULL? "" : "\n");
3285 value = string->str;
3287 g_string_free (string, FALSE);
3292 #define __G_KEY_FILE_C__
3293 #include "galiasdef.c"