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)");
1023 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1025 GKeyFileKeyValuePair *pair;
1027 pair = (GKeyFileKeyValuePair *) tmp->data;
1033 keys = g_new0 (gchar *, num_keys + 1);
1036 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1038 GKeyFileKeyValuePair *pair;
1040 pair = (GKeyFileKeyValuePair *) tmp->data;
1044 keys[i] = g_strdup (pair->key);
1049 keys[num_keys] = NULL;
1058 * g_key_file_get_start_group:
1059 * @key_file: a #GKeyFile
1061 * Returns the name of the start group of the file.
1063 * Return value: The start group of the key file.
1068 g_key_file_get_start_group (GKeyFile *key_file)
1070 g_return_val_if_fail (key_file != NULL, NULL);
1072 if (key_file->start_group)
1073 return g_strdup (key_file->start_group->name);
1079 * g_key_file_get_groups:
1080 * @key_file: a #GKeyFile
1081 * @length: return location for the number of returned groups, or %NULL
1083 * Returns all groups in the key file loaded with @key_file. The
1084 * array of returned groups will be %NULL-terminated, so @length may
1085 * optionally be %NULL.
1087 * Return value: a newly-allocated %NULL-terminated array of strings.
1088 * Use g_strfreev() to free it.
1092 g_key_file_get_groups (GKeyFile *key_file,
1097 gsize i, num_groups;
1099 g_return_val_if_fail (key_file != NULL, NULL);
1101 num_groups = g_list_length (key_file->groups);
1103 g_assert (num_groups > 0);
1105 /* Only need num_groups instead of num_groups + 1
1106 * because the first group of the file (last in the
1107 * list) is always the comment group at the top,
1110 groups = g_new0 (gchar *, num_groups);
1112 group_node = g_list_last (key_file->groups);
1114 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1117 for (group_node = group_node->prev;
1119 group_node = group_node->prev)
1121 GKeyFileGroup *group;
1123 group = (GKeyFileGroup *) group_node->data;
1125 g_assert (group->name != NULL);
1127 groups[i++] = g_strdup (group->name);
1138 * g_key_file_get_value:
1139 * @key_file: a #GKeyFile
1140 * @group_name: a group name
1142 * @error: return location for a #GError, or %NULL
1144 * Returns the value associated with @key under @group_name.
1146 * In the event the key cannot be found, %NULL is returned and
1147 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1148 * event that the @group_name cannot be found, %NULL is returned
1149 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1151 * Return value: a newly allocated string or %NULL if the specified
1152 * key cannot be found.
1157 g_key_file_get_value (GKeyFile *key_file,
1158 const gchar *group_name,
1162 GKeyFileGroup *group;
1163 GKeyFileKeyValuePair *pair;
1164 gchar *value = NULL;
1166 g_return_val_if_fail (key_file != NULL, NULL);
1167 g_return_val_if_fail (group_name != NULL, NULL);
1168 g_return_val_if_fail (key != NULL, NULL);
1170 group = g_key_file_lookup_group (key_file, group_name);
1174 g_set_error (error, G_KEY_FILE_ERROR,
1175 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1176 _("Key file does not have group '%s'"),
1177 group_name ? group_name : "(null)");
1181 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1184 value = g_strdup (pair->value);
1186 g_set_error (error, G_KEY_FILE_ERROR,
1187 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1188 _("Key file does not have key '%s'"), key);
1194 * g_key_file_set_value:
1195 * @key_file: a #GKeyFile
1196 * @group_name: a group name
1200 * Associates a new value with @key under @group_name. If @key
1201 * cannot be found then it is created. If @group_name cannot be
1202 * found then it is created.
1207 g_key_file_set_value (GKeyFile *key_file,
1208 const gchar *group_name,
1212 GKeyFileGroup *group;
1213 GKeyFileKeyValuePair *pair;
1215 g_return_if_fail (key_file != NULL);
1216 g_return_if_fail (group_name != NULL);
1217 g_return_if_fail (key != NULL);
1218 g_return_if_fail (value != NULL);
1220 group = g_key_file_lookup_group (key_file, group_name);
1224 g_key_file_add_group (key_file, group_name);
1225 group = (GKeyFileGroup *) key_file->groups->data;
1227 g_key_file_add_key (key_file, group, key, value);
1231 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1234 g_key_file_add_key (key_file, group, key, value);
1237 g_free (pair->value);
1238 pair->value = g_strdup (value);
1244 * g_key_file_get_string:
1245 * @key_file: a #GKeyFile
1246 * @group_name: a group name
1248 * @error: return location for a #GError, or %NULL
1250 * Returns the value associated with @key under @group_name.
1252 * In the event the key cannot be found, %NULL is returned and
1253 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1254 * event that the @group_name cannot be found, %NULL is returned
1255 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1257 * Return value: a newly allocated string or %NULL if the specified
1258 * key cannot be found.
1263 g_key_file_get_string (GKeyFile *key_file,
1264 const gchar *group_name,
1268 gchar *value, *string_value;
1269 GError *key_file_error;
1271 g_return_val_if_fail (key_file != NULL, NULL);
1272 g_return_val_if_fail (group_name != NULL, NULL);
1273 g_return_val_if_fail (key != NULL, NULL);
1275 key_file_error = NULL;
1277 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1281 g_propagate_error (error, key_file_error);
1285 if (!g_utf8_validate (value, -1, NULL))
1287 g_set_error (error, G_KEY_FILE_ERROR,
1288 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1289 _("Key file contains key '%s' with value '%s' "
1290 "which is not UTF-8"), key, value);
1295 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1301 if (g_error_matches (key_file_error,
1303 G_KEY_FILE_ERROR_INVALID_VALUE))
1305 g_set_error (error, G_KEY_FILE_ERROR,
1306 G_KEY_FILE_ERROR_INVALID_VALUE,
1307 _("Key file contains key '%s' "
1308 "which has value that cannot be interpreted."),
1310 g_error_free (key_file_error);
1313 g_propagate_error (error, key_file_error);
1316 return string_value;
1320 * g_key_file_set_string:
1321 * @key_file: a #GKeyFile
1322 * @group_name: a group name
1326 * Associates a new string value with @key under @group_name. If
1327 * @key cannot be found then it is created. If @group_name
1328 * cannot be found then it is created.
1333 g_key_file_set_string (GKeyFile *key_file,
1334 const gchar *group_name,
1336 const gchar *string)
1340 g_return_if_fail (key_file != NULL);
1341 g_return_if_fail (group_name != NULL);
1342 g_return_if_fail (key != NULL);
1343 g_return_if_fail (string != NULL);
1345 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1346 g_key_file_set_value (key_file, group_name, key, value);
1351 * g_key_file_get_string_list:
1352 * @key_file: a #GKeyFile
1353 * @group_name: a group name
1355 * @length: return location for the number of returned strings, or %NULL
1356 * @error: return location for a #GError, or %NULL
1358 * Returns the values associated with @key under @group_name.
1360 * In the event the key cannot be found, %NULL is returned and
1361 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1362 * event that the @group_name cannot be found, %NULL is returned
1363 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1365 * Return value: a %NULL-terminated string array or %NULL if the specified
1366 * key cannot be found. The array should be freed with g_strfreev().
1371 g_key_file_get_string_list (GKeyFile *key_file,
1372 const gchar *group_name,
1377 GError *key_file_error = NULL;
1378 gchar *value, *string_value, **values;
1380 GSList *p, *pieces = NULL;
1382 g_return_val_if_fail (key_file != NULL, NULL);
1383 g_return_val_if_fail (group_name != NULL, NULL);
1384 g_return_val_if_fail (key != NULL, NULL);
1386 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1390 g_propagate_error (error, key_file_error);
1394 if (!g_utf8_validate (value, -1, NULL))
1396 g_set_error (error, G_KEY_FILE_ERROR,
1397 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1398 _("Key file contains key '%s' with value '%s' "
1399 "which is not UTF-8"), key, value);
1404 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1406 g_free (string_value);
1410 if (g_error_matches (key_file_error,
1412 G_KEY_FILE_ERROR_INVALID_VALUE))
1414 g_set_error (error, G_KEY_FILE_ERROR,
1415 G_KEY_FILE_ERROR_INVALID_VALUE,
1416 _("Key file contains key '%s' "
1417 "which has value that cannot be interpreted."),
1419 g_error_free (key_file_error);
1422 g_propagate_error (error, key_file_error);
1425 len = g_slist_length (pieces);
1426 values = g_new0 (gchar *, len + 1);
1427 for (p = pieces, i = 0; p; p = p->next)
1428 values[i++] = p->data;
1431 g_slist_free (pieces);
1440 * g_key_file_set_string_list:
1441 * @key_file: a #GKeyFile
1442 * @group_name: a group name
1444 * @list: an array of locale string values
1445 * @length: number of locale string values in @list
1447 * Associates a list of string values for @key under @group_name.
1448 * If @key cannot be found then it is created. If @group_name
1449 * cannot be found then it is created.
1454 g_key_file_set_string_list (GKeyFile *key_file,
1455 const gchar *group_name,
1457 const gchar * const list[],
1460 GString *value_list;
1463 g_return_if_fail (key_file != NULL);
1464 g_return_if_fail (group_name != NULL);
1465 g_return_if_fail (key != NULL);
1466 g_return_if_fail (list != NULL);
1468 value_list = g_string_sized_new (length * 128);
1469 for (i = 0; list[i] != NULL && i < length; i++)
1473 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1474 g_string_append (value_list, value);
1475 g_string_append_c (value_list, key_file->list_separator);
1480 g_key_file_set_value (key_file, group_name, key, value_list->str);
1481 g_string_free (value_list, TRUE);
1485 * g_key_file_set_locale_string:
1486 * @key_file: a #GKeyFile
1487 * @group_name: a group name
1492 * Associates a string value for @key and @locale under
1493 * @group_name. If the translation for @key cannot be found
1494 * then it is created.
1499 g_key_file_set_locale_string (GKeyFile *key_file,
1500 const gchar *group_name,
1502 const gchar *locale,
1503 const gchar *string)
1505 gchar *full_key, *value;
1507 g_return_if_fail (key_file != NULL);
1508 g_return_if_fail (group_name != NULL);
1509 g_return_if_fail (key != NULL);
1510 g_return_if_fail (locale != NULL);
1511 g_return_if_fail (string != NULL);
1513 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1514 full_key = g_strdup_printf ("%s[%s]", key, locale);
1515 g_key_file_set_value (key_file, group_name, full_key, value);
1520 extern GSList *_g_compute_locale_variants (const gchar *locale);
1523 * g_key_file_get_locale_string:
1524 * @key_file: a #GKeyFile
1525 * @group_name: a group name
1527 * @locale: a locale or %NULL
1528 * @error: return location for a #GError, or %NULL
1530 * Returns the value associated with @key under @group_name
1531 * translated in the given @locale if available. If @locale is
1532 * %NULL then the current locale is assumed.
1534 * If @key cannot be found then %NULL is returned and @error is set to
1535 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1536 * with @key cannot be interpreted or no suitable translation can
1537 * be found then the untranslated value is returned.
1539 * Return value: a newly allocated string or %NULL if the specified
1540 * key cannot be found.
1545 g_key_file_get_locale_string (GKeyFile *key_file,
1546 const gchar *group_name,
1548 const gchar *locale,
1551 gchar *candidate_key, *translated_value;
1552 GError *key_file_error;
1554 gboolean free_languages = FALSE;
1557 g_return_val_if_fail (key_file != NULL, NULL);
1558 g_return_val_if_fail (group_name != NULL, NULL);
1559 g_return_val_if_fail (key != NULL, NULL);
1561 candidate_key = NULL;
1562 translated_value = NULL;
1563 key_file_error = NULL;
1569 list = _g_compute_locale_variants (locale);
1571 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1572 for (l = list, i = 0; l; l = l->next, i++)
1573 languages[i] = l->data;
1574 languages[i] = NULL;
1576 g_slist_free (list);
1577 free_languages = TRUE;
1581 languages = (gchar **) g_get_language_names ();
1582 free_languages = FALSE;
1585 for (i = 0; languages[i]; i++)
1587 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1589 translated_value = g_key_file_get_string (key_file,
1591 candidate_key, NULL);
1592 g_free (candidate_key);
1594 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1597 g_free (translated_value);
1598 translated_value = NULL;
1601 /* Fallback to untranslated key
1603 if (!translated_value)
1605 translated_value = g_key_file_get_string (key_file, group_name, key,
1608 if (!translated_value)
1609 g_propagate_error (error, key_file_error);
1613 g_strfreev (languages);
1615 return translated_value;
1619 * g_key_file_get_locale_string_list:
1620 * @key_file: a #GKeyFile
1621 * @group_name: a group name
1624 * @length: return location for the number of returned strings or %NULL
1625 * @error: return location for a #GError or %NULL
1627 * Returns the values associated with @key under @group_name
1628 * translated in the given @locale if available. If @locale is
1629 * %NULL then the current locale is assumed.
1631 * If @key cannot be found then %NULL is returned and @error is set to
1632 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1633 * with @key cannot be interpreted or no suitable translations
1634 * can be found then the untranslated values are returned.
1635 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1637 * Return value: a newly allocated %NULL-terminated string array
1638 * or %NULL if the key isn't found. The string array should be freed
1639 * with g_strfreev().
1644 g_key_file_get_locale_string_list (GKeyFile *key_file,
1645 const gchar *group_name,
1647 const gchar *locale,
1651 GError *key_file_error;
1652 gchar **values, *value;
1654 g_return_val_if_fail (key_file != NULL, NULL);
1655 g_return_val_if_fail (group_name != NULL, NULL);
1656 g_return_val_if_fail (key != NULL, NULL);
1658 key_file_error = NULL;
1660 value = g_key_file_get_locale_string (key_file, group_name,
1665 g_propagate_error (error, key_file_error);
1670 if (value[strlen (value) - 1] == ';')
1671 value[strlen (value) - 1] = '\0';
1673 values = g_strsplit (value, ";", 0);
1678 *length = g_strv_length (values);
1684 * g_key_file_set_locale_string_list:
1685 * @key_file: a #GKeyFile
1686 * @group_name: a group name
1689 * @list: a %NULL-terminated array of locale string values
1690 * @length: the length of @list
1692 * Associates a list of string values for @key and @locale under
1693 * @group_name. If the translation for @key cannot be found then
1699 g_key_file_set_locale_string_list (GKeyFile *key_file,
1700 const gchar *group_name,
1702 const gchar *locale,
1703 const gchar * const list[],
1706 GString *value_list;
1710 g_return_if_fail (key_file != NULL);
1711 g_return_if_fail (group_name != NULL);
1712 g_return_if_fail (key != NULL);
1713 g_return_if_fail (locale != NULL);
1714 g_return_if_fail (length != 0);
1716 value_list = g_string_sized_new (length * 128);
1717 for (i = 0; list[i] != NULL && i < length; i++)
1721 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1723 g_string_append (value_list, value);
1724 g_string_append_c (value_list, ';');
1729 full_key = g_strdup_printf ("%s[%s]", key, locale);
1730 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1732 g_string_free (value_list, TRUE);
1736 * g_key_file_get_boolean:
1737 * @key_file: a #GKeyFile
1738 * @group_name: a group name
1740 * @error: return location for a #GError
1742 * Returns the value associated with @key under @group_name as a
1745 * If @key cannot be found then the return value is undefined and
1746 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1747 * the value associated with @key cannot be interpreted as a boolean
1748 * then the return value is also undefined and @error is set to
1749 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1751 * Return value: the value associated with the key as a boolean
1755 g_key_file_get_boolean (GKeyFile *key_file,
1756 const gchar *group_name,
1760 GError *key_file_error = NULL;
1762 gboolean bool_value;
1764 g_return_val_if_fail (key_file != NULL, FALSE);
1765 g_return_val_if_fail (group_name != NULL, FALSE);
1766 g_return_val_if_fail (key != NULL, FALSE);
1768 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1772 g_propagate_error (error, key_file_error);
1776 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1782 if (g_error_matches (key_file_error,
1784 G_KEY_FILE_ERROR_INVALID_VALUE))
1786 g_set_error (error, G_KEY_FILE_ERROR,
1787 G_KEY_FILE_ERROR_INVALID_VALUE,
1788 _("Key file contains key '%s' "
1789 "which has value that cannot be interpreted."),
1791 g_error_free (key_file_error);
1794 g_propagate_error (error, key_file_error);
1801 * g_key_file_set_boolean:
1802 * @key_file: a #GKeyFile
1803 * @group_name: a group name
1805 * @value: %TRUE or %FALSE
1807 * Associates a new boolean value with @key under @group_name.
1808 * If @key cannot be found then it is created.
1813 g_key_file_set_boolean (GKeyFile *key_file,
1814 const gchar *group_name,
1820 g_return_if_fail (key_file != NULL);
1821 g_return_if_fail (group_name != NULL);
1822 g_return_if_fail (key != NULL);
1824 result = g_key_file_parse_boolean_as_value (key_file, value);
1825 g_key_file_set_value (key_file, group_name, key, result);
1830 * g_key_file_get_boolean_list:
1831 * @key_file: a #GKeyFile
1832 * @group_name: a group name
1834 * @length: the number of booleans returned
1835 * @error: return location for a #GError
1837 * Returns the values associated with @key under @group_name as
1838 * booleans. If @group_name is %NULL, the start_group is used.
1840 * If @key cannot be found then the return value is undefined and
1841 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1842 * the values associated with @key cannot be interpreted as booleans
1843 * then the return value is also undefined and @error is set to
1844 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1846 * Return value: the values associated with the key as a boolean
1851 g_key_file_get_boolean_list (GKeyFile *key_file,
1852 const gchar *group_name,
1857 GError *key_file_error;
1859 gboolean *bool_values;
1862 g_return_val_if_fail (key_file != NULL, NULL);
1863 g_return_val_if_fail (group_name != NULL, NULL);
1864 g_return_val_if_fail (key != NULL, NULL);
1866 key_file_error = NULL;
1868 values = g_key_file_get_string_list (key_file, group_name, key,
1869 &num_bools, &key_file_error);
1872 g_propagate_error (error, key_file_error);
1877 bool_values = g_new0 (gboolean, num_bools);
1879 for (i = 0; i < num_bools; i++)
1881 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1887 g_propagate_error (error, key_file_error);
1888 g_strfreev (values);
1889 g_free (bool_values);
1894 g_strfreev (values);
1897 *length = num_bools;
1903 * g_key_file_set_boolean_list:
1904 * @key_file: a #GKeyFile
1905 * @group_name: a group name
1907 * @list: an array of boolean values
1908 * @length: length of @list
1910 * Associates a list of boolean values with @key under
1911 * @group_name. If @key cannot be found then it is created.
1912 * If @group_name is %NULL, the start_group is used.
1917 g_key_file_set_boolean_list (GKeyFile *key_file,
1918 const gchar *group_name,
1923 GString *value_list;
1926 g_return_if_fail (key_file != NULL);
1927 g_return_if_fail (group_name != NULL);
1928 g_return_if_fail (key != NULL);
1929 g_return_if_fail (list != NULL);
1931 value_list = g_string_sized_new (length * 8);
1932 for (i = 0; i < length; i++)
1936 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1938 g_string_append (value_list, value);
1939 g_string_append_c (value_list, key_file->list_separator);
1944 g_key_file_set_value (key_file, group_name, key, value_list->str);
1945 g_string_free (value_list, TRUE);
1949 * g_key_file_get_integer:
1950 * @key_file: a #GKeyFile
1951 * @group_name: a group name
1953 * @error: return location for a #GError
1955 * Returns the value associated with @key under @group_name as an
1956 * integer. If @group_name is %NULL, the start_group is used.
1958 * If @key cannot be found then the return value is undefined and
1959 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1960 * the value associated with @key cannot be interpreted as an integer
1961 * then the return value is also undefined and @error is set to
1962 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1964 * Return value: the value associated with the key as an integer.
1969 g_key_file_get_integer (GKeyFile *key_file,
1970 const gchar *group_name,
1974 GError *key_file_error;
1978 g_return_val_if_fail (key_file != NULL, -1);
1979 g_return_val_if_fail (group_name != NULL, -1);
1980 g_return_val_if_fail (key != NULL, -1);
1982 key_file_error = NULL;
1984 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1988 g_propagate_error (error, key_file_error);
1992 int_value = g_key_file_parse_value_as_integer (key_file, value,
1998 if (g_error_matches (key_file_error,
2000 G_KEY_FILE_ERROR_INVALID_VALUE))
2002 g_set_error (error, G_KEY_FILE_ERROR,
2003 G_KEY_FILE_ERROR_INVALID_VALUE,
2004 _("Key file contains key '%s' in group '%s' "
2005 "which has value that cannot be interpreted."), key,
2007 g_error_free (key_file_error);
2010 g_propagate_error (error, key_file_error);
2017 * g_key_file_set_integer:
2018 * @key_file: a #GKeyFile
2019 * @group_name: a group name
2021 * @value: an integer value
2023 * Associates a new integer value with @key under @group_name.
2024 * If @key cannot be found then it is created.
2029 g_key_file_set_integer (GKeyFile *key_file,
2030 const gchar *group_name,
2036 g_return_if_fail (key_file != NULL);
2037 g_return_if_fail (group_name != NULL);
2038 g_return_if_fail (key != NULL);
2040 result = g_key_file_parse_integer_as_value (key_file, value);
2041 g_key_file_set_value (key_file, group_name, key, result);
2046 * g_key_file_get_integer_list:
2047 * @key_file: a #GKeyFile
2048 * @group_name: a group name
2050 * @length: the number of integers returned
2051 * @error: return location for a #GError
2053 * Returns the values associated with @key under @group_name as
2056 * If @key cannot be found then the return value is undefined and
2057 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2058 * the values associated with @key cannot be interpreted as integers
2059 * then the return value is also undefined and @error is set to
2060 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2062 * Return value: the values associated with the key as a integer
2067 g_key_file_get_integer_list (GKeyFile *key_file,
2068 const gchar *group_name,
2073 GError *key_file_error = NULL;
2078 g_return_val_if_fail (key_file != NULL, NULL);
2079 g_return_val_if_fail (group_name != NULL, NULL);
2080 g_return_val_if_fail (key != NULL, NULL);
2082 values = g_key_file_get_string_list (key_file, group_name, key,
2083 &num_ints, &key_file_error);
2086 g_propagate_error (error, key_file_error);
2091 int_values = g_new0 (gint, num_ints);
2093 for (i = 0; i < num_ints; i++)
2095 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2101 g_propagate_error (error, key_file_error);
2102 g_strfreev (values);
2103 g_free (int_values);
2108 g_strfreev (values);
2117 * g_key_file_set_integer_list:
2118 * @key_file: a #GKeyFile
2119 * @group_name: a group name
2121 * @list: an array of integer values
2122 * @length: number of integer values in @list
2124 * Associates a list of integer values with @key under
2125 * @group_name. If @key cannot be found then it is created.
2130 g_key_file_set_integer_list (GKeyFile *key_file,
2131 const gchar *group_name,
2139 g_return_if_fail (key_file != NULL);
2140 g_return_if_fail (group_name != NULL);
2141 g_return_if_fail (key != NULL);
2142 g_return_if_fail (list != NULL);
2144 values = g_string_sized_new (length * 16);
2145 for (i = 0; i < length; i++)
2149 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2151 g_string_append (values, value);
2152 g_string_append_c (values, ';');
2157 g_key_file_set_value (key_file, group_name, key, values->str);
2158 g_string_free (values, TRUE);
2162 g_key_file_set_key_comment (GKeyFile *key_file,
2163 const gchar *group_name,
2165 const gchar *comment,
2168 GKeyFileGroup *group;
2169 GKeyFileKeyValuePair *pair;
2170 GList *key_node, *comment_node, *tmp;
2172 group = g_key_file_lookup_group (key_file, group_name);
2175 g_set_error (error, G_KEY_FILE_ERROR,
2176 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2177 _("Key file does not have group '%s'"),
2178 group_name ? group_name : "(null)");
2183 /* First find the key the comments are supposed to be
2186 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2188 if (key_node == NULL)
2190 g_set_error (error, G_KEY_FILE_ERROR,
2191 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2192 _("Key file does not have key '%s' in group '%s'"),
2197 /* Then find all the comments already associated with the
2200 tmp = key_node->next;
2203 GKeyFileKeyValuePair *pair;
2205 pair = (GKeyFileKeyValuePair *) tmp->data;
2207 if (pair->key != NULL)
2212 g_key_file_remove_key_value_pair_node (key_file, group,
2216 if (comment == NULL)
2219 /* Now we can add our new comment
2221 pair = g_new0 (GKeyFileKeyValuePair, 1);
2224 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2226 key_node = g_list_insert (key_node, pair, 1);
2230 g_key_file_set_group_comment (GKeyFile *key_file,
2231 const gchar *group_name,
2232 const gchar *comment,
2235 GKeyFileGroup *group;
2237 group = g_key_file_lookup_group (key_file, group_name);
2240 g_set_error (error, G_KEY_FILE_ERROR,
2241 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2242 _("Key file does not have group '%s'"),
2243 group_name ? group_name : "(null)");
2248 /* First remove any existing comment
2252 g_key_file_key_value_pair_free (group->comment);
2253 group->comment = NULL;
2256 if (comment == NULL)
2259 /* Now we can add our new comment
2261 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2263 group->comment->key = NULL;
2264 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2268 g_key_file_set_top_comment (GKeyFile *key_file,
2269 const gchar *comment,
2273 GKeyFileGroup *group;
2274 GKeyFileKeyValuePair *pair;
2276 /* The last group in the list should be the top (comments only)
2279 g_assert (key_file->groups != NULL);
2280 group_node = g_list_last (key_file->groups);
2281 group = (GKeyFileGroup *) group_node->data;
2282 g_assert (group->name == NULL);
2284 /* Note all keys must be comments at the top of
2285 * the file, so we can just free it all.
2287 if (group->key_value_pairs != NULL)
2289 g_list_foreach (group->key_value_pairs,
2290 (GFunc) g_key_file_key_value_pair_free,
2292 g_list_free (group->key_value_pairs);
2293 group->key_value_pairs = NULL;
2296 if (comment == NULL)
2299 pair = g_new0 (GKeyFileKeyValuePair, 1);
2302 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2304 group->key_value_pairs =
2305 g_list_prepend (group->key_value_pairs, pair);
2309 * g_key_file_set_comment:
2310 * @key_file: a #GKeyFile
2311 * @group_name: a group name, or %NULL
2313 * @comment: a comment
2314 * @error: return location for a #GError
2316 * Places a comment above @key from @group_name.
2317 * @group_name. If @key is %NULL then @comment will
2318 * be written above @group_name. If both @key
2319 * and @group_name are NULL, then @comment will
2320 * be written above the first group in the file.
2325 g_key_file_set_comment (GKeyFile *key_file,
2326 const gchar *group_name,
2328 const gchar *comment,
2331 g_return_if_fail (key_file != NULL);
2333 if (group_name != NULL && key != NULL)
2334 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2335 else if (group_name != NULL)
2336 g_key_file_set_group_comment (key_file, group_name, comment, error);
2338 g_key_file_set_top_comment (key_file, comment, error);
2340 if (comment != NULL)
2341 key_file->approximate_size += strlen (comment);
2345 g_key_file_get_key_comment (GKeyFile *key_file,
2346 const gchar *group_name,
2350 GKeyFileGroup *group;
2351 GKeyFileKeyValuePair *pair;
2352 GList *key_node, *tmp;
2356 group = g_key_file_lookup_group (key_file, group_name);
2359 g_set_error (error, G_KEY_FILE_ERROR,
2360 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2361 _("Key file does not have group '%s'"),
2362 group_name ? group_name : "(null)");
2367 /* First find the key the comments are supposed to be
2370 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2372 if (key_node == NULL)
2374 g_set_error (error, G_KEY_FILE_ERROR,
2375 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2376 _("Key file does not have key '%s' in group '%s'"),
2383 /* Then find all the comments already associated with the
2384 * key and concatentate them.
2386 tmp = key_node->next;
2387 if (!key_node->next)
2390 pair = (GKeyFileKeyValuePair *) tmp->data;
2391 if (pair->key != NULL)
2396 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2398 if (pair->key != NULL)
2404 while (tmp != key_node)
2406 GKeyFileKeyValuePair *pair;
2408 pair = (GKeyFileKeyValuePair *) tmp->data;
2411 string = g_string_sized_new (512);
2413 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2414 g_string_append (string, comment);
2422 comment = string->str;
2423 g_string_free (string, FALSE);
2432 get_group_comment (GKeyFile *key_file,
2433 GKeyFileGroup *group,
2442 tmp = group->key_value_pairs;
2445 GKeyFileKeyValuePair *pair;
2447 pair = (GKeyFileKeyValuePair *) tmp->data;
2449 if (pair->key != NULL)
2455 if (tmp->next == NULL)
2463 GKeyFileKeyValuePair *pair;
2465 pair = (GKeyFileKeyValuePair *) tmp->data;
2468 string = g_string_sized_new (512);
2470 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2471 g_string_append (string, comment);
2478 return g_string_free (string, FALSE);
2484 g_key_file_get_group_comment (GKeyFile *key_file,
2485 const gchar *group_name,
2489 GKeyFileGroup *group;
2491 group_node = g_key_file_lookup_group_node (key_file, group_name);
2492 group = (GKeyFileGroup *)group_node->data;
2495 g_set_error (error, G_KEY_FILE_ERROR,
2496 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2497 _("Key file does not have group '%s'"),
2498 group_name ? group_name : "(null)");
2504 return g_strdup (group->comment->value);
2506 group_node = group_node->next;
2507 group = (GKeyFileGroup *)group_node->data;
2508 return get_group_comment (key_file, group, error);
2512 g_key_file_get_top_comment (GKeyFile *key_file,
2516 GKeyFileGroup *group;
2518 /* The last group in the list should be the top (comments only)
2521 g_assert (key_file->groups != NULL);
2522 group_node = g_list_last (key_file->groups);
2523 group = (GKeyFileGroup *) group_node->data;
2524 g_assert (group->name == NULL);
2526 return get_group_comment (key_file, group, error);
2530 * g_key_file_get_comment:
2531 * @key_file: a #GKeyFile
2532 * @group_name: a group name, or %NULL
2534 * @error: return location for a #GError
2536 * Retrieves a comment above @key from @group_name.
2537 * @group_name. If @key is %NULL then @comment will
2538 * be read from above @group_name. If both @key
2539 * and @group_name are NULL, then @comment will
2540 * be read from above the first group in the file.
2542 * Returns: a comment that should be freed with g_free()
2547 g_key_file_get_comment (GKeyFile *key_file,
2548 const gchar *group_name,
2552 g_return_val_if_fail (key_file != NULL, NULL);
2554 if (group_name != NULL && key != NULL)
2555 return g_key_file_get_key_comment (key_file, group_name, key, error);
2556 else if (group_name != NULL)
2557 return g_key_file_get_group_comment (key_file, group_name, error);
2559 return g_key_file_get_top_comment (key_file, error);
2563 * g_key_file_remove_comment:
2564 * @key_file: a #GKeyFile
2565 * @group_name: a group name, or %NULL
2567 * @error: return location for a #GError
2569 * Removes a comment above @key from @group_name.
2570 * @group_name. If @key is %NULL then @comment will
2571 * be written above @group_name. If both @key
2572 * and @group_name are NULL, then @comment will
2573 * be written above the first group in the file.
2579 g_key_file_remove_comment (GKeyFile *key_file,
2580 const gchar *group_name,
2584 g_return_if_fail (key_file != NULL);
2586 if (group_name != NULL && key != NULL)
2587 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2588 else if (group_name != NULL)
2589 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2591 g_key_file_set_top_comment (key_file, NULL, error);
2595 * g_key_file_has_group:
2596 * @key_file: a #GKeyFile
2597 * @group_name: a group name
2599 * Looks whether the key file has the group @group_name.
2601 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2606 g_key_file_has_group (GKeyFile *key_file,
2607 const gchar *group_name)
2609 g_return_val_if_fail (key_file != NULL, FALSE);
2610 g_return_val_if_fail (group_name != NULL, FALSE);
2612 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2616 * g_key_file_has_key:
2617 * @key_file: a #GKeyFile
2618 * @group_name: a group name
2620 * @error: return location for a #GError
2622 * Looks whether the key file has the key @key in the group
2625 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2631 g_key_file_has_key (GKeyFile *key_file,
2632 const gchar *group_name,
2636 GKeyFileKeyValuePair *pair;
2637 GKeyFileGroup *group;
2639 g_return_val_if_fail (key_file != NULL, FALSE);
2640 g_return_val_if_fail (group_name != NULL, FALSE);
2641 g_return_val_if_fail (key != NULL, FALSE);
2643 group = g_key_file_lookup_group (key_file, group_name);
2647 g_set_error (error, G_KEY_FILE_ERROR,
2648 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2649 _("Key file does not have group '%s'"),
2650 group_name ? group_name : "(null)");
2655 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2657 return pair != NULL;
2661 g_key_file_add_group (GKeyFile *key_file,
2662 const gchar *group_name)
2664 GKeyFileGroup *group;
2666 g_return_if_fail (key_file != NULL);
2667 g_return_if_fail (group_name != NULL);
2668 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2670 group = g_new0 (GKeyFileGroup, 1);
2671 group->name = g_strdup (group_name);
2672 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2673 key_file->groups = g_list_prepend (key_file->groups, group);
2674 key_file->approximate_size += strlen (group_name) + 3;
2675 key_file->current_group = group;
2677 if (key_file->start_group == NULL)
2678 key_file->start_group = group;
2682 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2687 g_free (pair->value);
2692 /* Be careful not to call this function on a node with data in the
2693 * lookup map without removing it from the lookup map, first.
2695 * Some current cases where this warning is not a concern are
2697 * - the node being removed is a comment node
2698 * - the entire lookup map is getting destroyed soon after
2702 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2703 GKeyFileGroup *group,
2707 GKeyFileKeyValuePair *pair;
2709 pair = (GKeyFileKeyValuePair *) pair_node->data;
2711 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2713 if (pair->key != NULL)
2714 key_file->approximate_size -= strlen (pair->key) + 1;
2716 g_assert (pair->value != NULL);
2717 key_file->approximate_size -= strlen (pair->value);
2719 g_key_file_key_value_pair_free (pair);
2721 g_list_free_1 (pair_node);
2725 g_key_file_remove_group_node (GKeyFile *key_file,
2728 GKeyFileGroup *group;
2731 group = (GKeyFileGroup *) group_node->data;
2733 /* If the current group gets deleted make the current group the last
2736 if (key_file->current_group == group)
2738 /* groups should always contain at least the top comment group,
2739 * unless g_key_file_clear has been called
2741 if (key_file->groups)
2742 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2744 key_file->current_group = NULL;
2747 /* If the start group gets deleted make the start group the first
2750 if (key_file->start_group == group)
2752 tmp = g_list_last (key_file->groups);
2755 if (tmp != group_node &&
2756 ((GKeyFileGroup *) tmp->data)->name != NULL)
2763 key_file->start_group = (GKeyFileGroup *) tmp->data;
2765 key_file->start_group = NULL;
2768 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2770 if (group->name != NULL)
2771 key_file->approximate_size -= strlen (group->name) + 3;
2773 tmp = group->key_value_pairs;
2780 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2783 g_assert (group->key_value_pairs == NULL);
2785 if (group->lookup_map)
2787 g_hash_table_destroy (group->lookup_map);
2788 group->lookup_map = NULL;
2791 g_free ((gchar *) group->name);
2793 g_list_free_1 (group_node);
2797 * g_key_file_remove_group:
2798 * @key_file: a #GKeyFile
2799 * @group_name: a group name
2800 * @error: return location for a #GError or %NULL
2802 * Removes the specified group, @group_name,
2803 * from the key file.
2808 g_key_file_remove_group (GKeyFile *key_file,
2809 const gchar *group_name,
2814 g_return_if_fail (key_file != NULL);
2815 g_return_if_fail (group_name != NULL);
2817 group_node = g_key_file_lookup_group_node (key_file, group_name);
2821 g_set_error (error, G_KEY_FILE_ERROR,
2822 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2823 _("Key file does not have group '%s'"),
2828 g_key_file_remove_group_node (key_file, group_node);
2832 g_key_file_add_key (GKeyFile *key_file,
2833 GKeyFileGroup *group,
2837 GKeyFileKeyValuePair *pair;
2839 pair = g_new0 (GKeyFileKeyValuePair, 1);
2841 pair->key = g_strdup (key);
2842 pair->value = g_strdup (value);
2844 g_hash_table_replace (group->lookup_map, pair->key, pair);
2845 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2846 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2850 * g_key_file_remove_key:
2851 * @key_file: a #GKeyFile
2852 * @group_name: a group name
2853 * @key: a key name to remove
2854 * @error: return location for a #GError or %NULL
2856 * Removes @key in @group_name from the key file.
2861 g_key_file_remove_key (GKeyFile *key_file,
2862 const gchar *group_name,
2866 GKeyFileGroup *group;
2867 GKeyFileKeyValuePair *pair;
2869 g_return_if_fail (key_file != NULL);
2870 g_return_if_fail (group_name != NULL);
2871 g_return_if_fail (key != NULL);
2875 group = g_key_file_lookup_group (key_file, group_name);
2878 g_set_error (error, G_KEY_FILE_ERROR,
2879 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2880 _("Key file does not have group '%s'"),
2881 group_name ? group_name : "(null)");
2885 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2889 g_set_error (error, G_KEY_FILE_ERROR,
2890 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2891 _("Key file does not have key '%s' in group '%s'"),
2896 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2898 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
2899 g_hash_table_remove (group->lookup_map, pair->key);
2900 g_key_file_key_value_pair_free (pair);
2904 g_key_file_lookup_group_node (GKeyFile *key_file,
2905 const gchar *group_name)
2907 GKeyFileGroup *group;
2911 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2913 group = (GKeyFileGroup *) tmp->data;
2915 if (group && group->name && strcmp (group->name, group_name) == 0)
2924 static GKeyFileGroup *
2925 g_key_file_lookup_group (GKeyFile *key_file,
2926 const gchar *group_name)
2930 group_node = g_key_file_lookup_group_node (key_file, group_name);
2932 if (group_node != NULL)
2933 return (GKeyFileGroup *) group_node->data;
2939 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2940 GKeyFileGroup *group,
2945 for (key_node = group->key_value_pairs;
2947 key_node = key_node->next)
2949 GKeyFileKeyValuePair *pair;
2951 pair = (GKeyFileKeyValuePair *) key_node->data;
2953 if (pair->key && strcmp (pair->key, key) == 0)
2960 static GKeyFileKeyValuePair *
2961 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2962 GKeyFileGroup *group,
2965 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2968 /* Lines starting with # or consisting entirely of whitespace are merely
2969 * recorded, not parsed. This function assumes all leading whitespace
2970 * has been stripped.
2973 g_key_file_line_is_comment (const gchar *line)
2975 return (*line == '#' || *line == '\0' || *line == '\n');
2978 /* A group in a key file is made up of a starting '[' followed by one
2979 * or more letters making up the group name followed by ']'.
2982 g_key_file_line_is_group (const gchar *line)
2990 p = g_utf8_next_char (p);
2992 /* Group name must be non-empty
2994 if (!*p || *p == ']')
2997 while (*p && *p != ']')
2998 p = g_utf8_next_char (p);
3007 g_key_file_line_is_key_value_pair (const gchar *line)
3011 p = (gchar *) g_utf8_strchr (line, -1, '=');
3016 /* Key must be non-empty
3025 g_key_file_parse_value_as_string (GKeyFile *key_file,
3030 gchar *string_value, *p, *q0, *q;
3032 string_value = g_new0 (gchar, strlen (value) + 1);
3034 p = (gchar *) value;
3035 q0 = q = string_value;
3065 g_set_error (error, G_KEY_FILE_ERROR,
3066 G_KEY_FILE_ERROR_INVALID_VALUE,
3067 _("Key file contains escape character "
3072 if (pieces && *p == key_file->list_separator)
3073 *q = key_file->list_separator;
3087 g_set_error (error, G_KEY_FILE_ERROR,
3088 G_KEY_FILE_ERROR_INVALID_VALUE,
3089 _("Key file contains invalid escape "
3090 "sequence '%s'"), sequence);
3099 if (pieces && (*p == key_file->list_separator))
3101 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3117 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3118 *pieces = g_slist_reverse (*pieces);
3121 return string_value;
3125 g_key_file_parse_string_as_value (GKeyFile *key_file,
3126 const gchar *string,
3127 gboolean escape_separator)
3129 gchar *value, *p, *q;
3131 gboolean parsing_leading_space;
3133 length = strlen (string) + 1;
3135 /* Worst case would be that every character needs to be escaped.
3136 * In other words every character turns to two characters
3138 value = g_new0 (gchar, 2 * length);
3140 p = (gchar *) string;
3142 parsing_leading_space = TRUE;
3143 while (p < (string + length - 1))
3145 gchar escaped_character[3] = { '\\', 0, 0 };
3150 if (parsing_leading_space)
3152 escaped_character[1] = 's';
3153 strcpy (q, escaped_character);
3163 if (parsing_leading_space)
3165 escaped_character[1] = 't';
3166 strcpy (q, escaped_character);
3176 escaped_character[1] = 'n';
3177 strcpy (q, escaped_character);
3181 escaped_character[1] = 'r';
3182 strcpy (q, escaped_character);
3186 escaped_character[1] = '\\';
3187 strcpy (q, escaped_character);
3189 parsing_leading_space = FALSE;
3192 if (escape_separator && *p == key_file->list_separator)
3194 escaped_character[1] = key_file->list_separator;
3195 strcpy (q, escaped_character);
3197 parsing_leading_space = TRUE;
3203 parsing_leading_space = FALSE;
3215 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3219 gchar *end_of_valid_int;
3224 long_value = strtol (value, &end_of_valid_int, 10);
3226 if (*value == '\0' || *end_of_valid_int != '\0')
3228 g_set_error (error, G_KEY_FILE_ERROR,
3229 G_KEY_FILE_ERROR_INVALID_VALUE,
3230 _("Value '%s' cannot be interpreted as a number."), value);
3234 int_value = long_value;
3235 if (int_value != long_value || errno == ERANGE)
3239 G_KEY_FILE_ERROR_INVALID_VALUE,
3240 _("Integer value '%s' out of range"), value);
3248 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3252 return g_strdup_printf ("%d", value);
3256 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3262 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3264 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3268 g_set_error (error, G_KEY_FILE_ERROR,
3269 G_KEY_FILE_ERROR_INVALID_VALUE,
3270 _("Value '%s' cannot be interpreted as a boolean."), value);
3276 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3280 return g_strdup ("true");
3282 return g_strdup ("false");
3286 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3290 gchar **lines, *comment;
3293 string = g_string_sized_new (512);
3295 lines = g_strsplit (value, "\n", 0);
3297 for (i = 0; lines[i] != NULL; i++)
3299 if (lines[i][0] != '#')
3300 g_string_append_printf (string, "%s\n", lines[i]);
3302 g_string_append_printf (string, "%s\n", lines[i] + 1);
3306 comment = string->str;
3308 g_string_free (string, FALSE);
3314 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3315 const gchar *comment)
3318 gchar **lines, *value;
3321 string = g_string_sized_new (512);
3323 lines = g_strsplit (comment, "\n", 0);
3325 for (i = 0; lines[i] != NULL; i++)
3326 g_string_append_printf (string, "#%s%s", lines[i],
3327 lines[i + 1] == NULL? "" : "\n");
3330 value = string->str;
3332 g_string_free (string, FALSE);
3337 #define __G_KEY_FILE_C__
3338 #include "galiasdef.c"