1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
5 * Written by Ray Strode <rstrode@redhat.com>
7 * GLib is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * GLib is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with GLib; see the file COPYING.LIB. If not,
19 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
32 #include <sys/types.h>
39 #include "gfileutils.h"
45 #include "gmessages.h"
47 #include "gstrfuncs.h"
50 typedef struct _GKeyFileGroup GKeyFileGroup;
56 gchar *start_group_name;
58 GKeyFileGroup *current_group;
60 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
62 /* Used for sizing the output buffer during serialization
64 gsize approximate_size;
73 const gchar *name; /* NULL for above first group (which will be comments) */
75 GList *key_value_pairs;
77 /* Used in parallel with key_value_pairs for
78 * increased lookup performance
80 GHashTable *lookup_map;
85 gchar *key; /* NULL for comments */
87 } GKeyFileKeyValuePair;
89 static gint find_file_in_data_dirs (const gchar *file,
93 static void g_key_file_load_from_fd (GKeyFile *key_file,
97 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
98 const gchar *group_name);
99 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
100 GKeyFileGroup *group,
102 static void g_key_file_remove_group_node (GKeyFile *entry,
104 static void g_key_file_add_key (GKeyFile *entry,
105 const gchar *group_name,
108 static void g_key_file_add_group (GKeyFile *entry,
109 const gchar *group_name);
110 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
111 static gboolean g_key_file_line_is_comment (const gchar *line);
112 static gboolean g_key_file_line_is_group (const gchar *line);
113 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
114 static gchar *g_key_file_parse_value_as_string (GKeyFile *entry,
118 static gchar *g_key_file_parse_string_as_value (GKeyFile *entry,
120 gboolean escape_separator);
121 static gint g_key_file_parse_value_as_integer (GKeyFile *entry,
124 static gchar *g_key_file_parse_integer_as_value (GKeyFile *entry,
126 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *entry,
129 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *entry,
131 static void g_key_file_parse_key_value_pair (GKeyFile *entry,
135 static void g_key_file_parse_comment (GKeyFile *entry,
139 static void g_key_file_parse_group (GKeyFile *entry,
143 static gchar *key_get_locale (const gchar *key);
144 static void g_key_file_parse_data (GKeyFile *entry,
148 static void g_key_file_flush_parse_buffer (GKeyFile *entry,
153 g_key_file_error_quark (void)
155 static GQuark error_quark = 0;
157 if (error_quark == 0)
158 error_quark = g_quark_from_static_string ("g-key-file-error-quark");
164 g_key_file_init (GKeyFile *key_file)
166 key_file->current_group = g_new0 (GKeyFileGroup, 1);
167 key_file->groups = g_list_prepend (NULL, key_file->current_group);
168 key_file->start_group_name = NULL;
169 key_file->parse_buffer = g_string_sized_new (128);
170 key_file->approximate_size = 0;
171 key_file->list_separator = ';';
176 g_key_file_clear (GKeyFile *key_file)
178 GList *tmp, *group_node;
180 if (key_file->parse_buffer)
181 g_string_free (key_file->parse_buffer, TRUE);
183 g_free (key_file->start_group_name);
185 tmp = key_file->groups;
190 g_key_file_remove_group_node (key_file, group_node);
193 g_assert (key_file->groups == NULL);
199 * @flags: flags from #GKeyFileFlags
201 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
202 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
203 * read an existing key file.
205 * Return value: an empty #GKeyFile.
210 g_key_file_new (void)
214 key_file = g_new0 (GKeyFile, 1);
215 g_key_file_init (key_file);
221 * g_key_file_set_list_separator:
222 * @key_file: a #GKeyFile
223 * @separator: the separator
225 * Sets the character which is used to separate
226 * values in lists. Typically ';' or ',' are used
227 * as separators. The default list separator is ';'.
232 g_key_file_set_list_separator (GKeyFile *key_file,
235 key_file->list_separator = separator;
239 /* Iterates through all the directories in *dirs trying to
240 * open file. When it successfully locates and opens a file it
241 * returns the file descriptor to the open file. It also
242 * outputs the absolute path of the file in output_file and
243 * leaves the unchecked directories in *dirs.
246 find_file_in_data_dirs (const gchar *file,
251 gchar **data_dirs, *data_dir, *path;
264 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
266 gchar *candidate_file, *sub_dir;
268 candidate_file = (gchar *) file;
269 sub_dir = g_strdup ("");
270 while (candidate_file != NULL && fd < 0)
274 path = g_build_filename (data_dir, sub_dir,
275 candidate_file, NULL);
277 fd = open (path, O_RDONLY);
279 if (output_file != NULL)
280 *output_file = g_strdup (path);
284 if (fd < 0 && file_error == NULL)
285 file_error = g_error_new (G_KEY_FILE_ERROR,
286 G_KEY_FILE_ERROR_NOT_FOUND,
287 _("Valid key file could not be "
288 "found in data dirs"));
290 candidate_file = strchr (candidate_file, '-');
292 if (candidate_file == NULL)
298 sub_dir = g_strndup (file, candidate_file - file - 1);
300 for (p = sub_dir; *p != '\0'; p++)
303 *p = G_DIR_SEPARATOR;
315 g_error_free (file_error);
317 g_propagate_error (error, file_error);
320 if (output_file && fd < 0)
322 g_free (*output_file);
330 g_key_file_load_from_fd (GKeyFile *key_file,
335 GError *key_file_error = NULL;
337 struct stat stat_buf;
338 gchar read_buf[4096];
340 if (key_file->approximate_size > 0)
342 g_key_file_clear (key_file);
343 g_key_file_init (key_file);
345 key_file->flags = flags;
347 fstat (fd, &stat_buf);
348 if (stat_buf.st_size == 0)
350 g_set_error (error, G_KEY_FILE_ERROR,
351 G_KEY_FILE_ERROR_PARSE,
356 key_file->start_group_name = NULL;
361 bytes_read = read (fd, read_buf, 4096);
363 if (bytes_read == 0) /* End of File */
371 g_set_error (error, G_FILE_ERROR,
372 g_file_error_from_errno (errno),
373 _("Failed to read from file: %s"),
379 g_key_file_parse_data (key_file,
380 read_buf, bytes_read,
383 while (!key_file_error);
389 g_propagate_error (error, key_file_error);
393 g_key_file_flush_parse_buffer (key_file, &key_file_error);
397 g_propagate_error (error, key_file_error);
403 * g_key_file_load_from_file:
404 * @key_file: an empty #GKeyFile struct
405 * @file: the path of a filename to load
406 * @flags: flags from #GKeyFileFlags
407 * @error: return location for a #GError, or %NULL
409 * Loads a key file into an empty #GKeyFile structure.
410 * If the file could not be loaded then %error is set to
411 * either a #GFileError or #GKeyFileError.
416 g_key_file_load_from_file (GKeyFile *key_file,
421 GError *key_file_error = NULL;
424 fd = open (file, O_RDONLY);
428 g_set_error (error, G_FILE_ERROR,
429 g_file_error_from_errno (errno),
430 _("Failed to open file '%s': %s"),
431 file, g_strerror (errno));
435 if (!g_file_test (file, G_FILE_TEST_IS_REGULAR))
437 g_set_error (error, G_FILE_ERROR,
439 _("Not a regular file: '%s'"),
444 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
447 g_propagate_error (error, key_file_error);
451 * g_key_file_load_from_data:
452 * @key_file: an empty #GKeyFile struct
453 * @data: key file loaded in memory.
454 * @length: the length of @data in bytes
455 * @flags: flags from #GKeyFileFlags
456 * @error: return location for a #GError, or %NULL
458 * Loads a key file from memory into an empty #GKeyFile structure.
459 * If the object cannot be created then %error is set to a #GKeyFileError.
464 g_key_file_load_from_data (GKeyFile *key_file,
470 GError *key_file_error = NULL;
472 g_return_if_fail (data != NULL);
473 g_return_if_fail (length != 0);
475 if (key_file->approximate_size > 0)
477 g_key_file_clear (key_file);
478 g_key_file_init (key_file);
480 key_file->flags = flags;
482 g_key_file_parse_data (key_file, data, length, &key_file_error);
486 g_propagate_error (error, key_file_error);
490 g_key_file_flush_parse_buffer (key_file, &key_file_error);
493 g_propagate_error (error, key_file_error);
497 * g_key_file_load_from_data_dirs:
498 * @key_file: an empty #GKeyFile struct
499 * @file: a relative path to a filename to open and parse
500 * @full_path: return location for a string containing the full path
501 * of the file, or %NULL
502 * @flags: flags from #GKeyFileFlags
503 * @error: return location for a #GError, or %NULL
505 * This function looks for a key file named @file in the paths
506 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
507 * loads the file into @key_file and returns the file's full path in
508 * @full_path. If the file could not be loaded then an %error is
509 * set to either a #GFileError or #GKeyFileError.
514 g_key_file_load_from_data_dirs (GKeyFile *key_file,
520 GError *key_file_error = NULL;
521 gchar **all_data_dirs, **data_dirs;
522 const gchar * user_data_dir;
523 const const gchar * const * system_data_dirs;
528 g_return_if_fail (!g_path_is_absolute (file));
530 user_data_dir = g_get_user_data_dir ();
531 system_data_dirs = g_get_system_data_dirs ();
532 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 1);
535 all_data_dirs[i++] = g_strdup (user_data_dir);
538 while (system_data_dirs[j] != NULL)
539 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
541 data_dirs = all_data_dirs;
542 while (*data_dirs != NULL)
544 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
550 g_propagate_error (error, key_file_error);
555 g_key_file_load_from_fd (key_file, fd, flags,
560 g_propagate_error (error, key_file_error);
561 g_free (output_path);
567 *full_path = output_path;
570 g_strfreev (all_data_dirs);
575 * @key_file: a #GKeyFile
582 g_key_file_free (GKeyFile *key_file)
584 g_return_if_fail (key_file != NULL);
586 g_key_file_clear (key_file);
590 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
591 * true for locales that match those in g_get_language_names().
594 g_key_file_locale_is_interesting (GKeyFile *key_file,
597 const const gchar * const * current_locales;
600 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
603 current_locales = g_get_language_names ();
605 for (i = 0; current_locales[i] != NULL; i++)
607 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
615 g_key_file_parse_line (GKeyFile *key_file,
620 GError *parse_error = NULL;
623 g_return_if_fail (key_file != NULL);
624 g_return_if_fail (line != NULL);
626 line_start = (gchar *) line;
627 while (g_ascii_isspace (*line_start))
630 if (g_key_file_line_is_comment (line_start))
631 g_key_file_parse_comment (key_file, line, length, &parse_error);
632 else if (g_key_file_line_is_group (line_start))
633 g_key_file_parse_group (key_file, line_start,
634 length - (line_start - line),
636 else if (g_key_file_line_is_key_value_pair (line_start))
637 g_key_file_parse_key_value_pair (key_file, line_start,
638 length - (line_start - line),
642 g_set_error (error, G_KEY_FILE_ERROR,
643 G_KEY_FILE_ERROR_PARSE,
644 _("Key file contains line '%s' which is not "
645 "a key-value pair, group, or comment"), line);
650 g_propagate_error (error, parse_error);
654 g_key_file_parse_comment (GKeyFile *key_file,
659 GKeyFileKeyValuePair *pair;
661 if (!key_file->flags & G_KEY_FILE_KEEP_COMMENTS)
664 g_assert (key_file->current_group != NULL);
666 pair = g_new0 (GKeyFileKeyValuePair, 1);
669 pair->value = g_strndup (line, length);
671 key_file->current_group->key_value_pairs =
672 g_list_prepend (key_file->current_group->key_value_pairs, pair);
676 g_key_file_parse_group (GKeyFile *key_file,
682 const gchar *group_name_start, *group_name_end;
684 /* advance past opening '['
686 group_name_start = line + 1;
687 group_name_end = line + length - 1;
689 while (*group_name_end != ']')
692 group_name = g_strndup (group_name_start,
693 group_name_end - group_name_start);
695 if (key_file->start_group_name == NULL)
696 key_file->start_group_name = g_strdup (group_name);
698 g_key_file_add_group (key_file, group_name);
703 g_key_file_parse_key_value_pair (GKeyFile *key_file,
708 gchar *key, *value, *key_end, *value_start, *locale;
709 gsize key_len, value_len;
711 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
713 g_set_error (error, G_KEY_FILE_ERROR,
714 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
715 _("Key file does not start with a group"));
719 key_end = value_start = strchr (line, '=');
721 g_assert (key_end != NULL);
726 /* Pull the key name from the line (chomping trailing whitespace)
728 while (g_ascii_isspace (*key_end))
731 key_len = key_end - line + 2;
733 g_assert (key_len <= length);
735 key = g_strndup (line, key_len - 1);
737 /* Pull the value from the line (chugging leading whitespace)
739 while (g_ascii_isspace (*value_start))
742 value_len = line + length - value_start + 1;
744 value = g_strndup (value_start, value_len);
746 if (!g_utf8_validate (value, -1, NULL))
748 g_set_error (error, G_KEY_FILE_ERROR,
749 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
750 _("Key file contains line '%s' "
751 "which is not UTF-8"), line);
758 if (key_file->current_group
759 && key_file->current_group->name
760 && strcmp (g_key_file_get_start_group (key_file),
761 key_file->current_group->name) == 0
762 && strcmp (key, "Encoding") == 0)
764 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
766 g_set_error (error, G_KEY_FILE_ERROR,
767 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
768 _("Key file contains unsupported encoding '%s'"), value);
776 /* Is this key a translation? If so, is it one that we care about?
778 locale = key_get_locale (key);
780 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
781 g_key_file_add_key (key_file, key_file->current_group->name, key, value);
789 key_get_locale (const gchar *key)
793 locale = g_strrstr (key, "[");
795 if (locale && strlen (locale) <= 2)
799 locale = g_strndup (locale + 1, strlen (locale) - 2);
805 g_key_file_parse_data (GKeyFile *key_file,
813 g_return_if_fail (key_file != NULL);
817 for (i = 0; i < length; i++)
821 /* When a newline is encountered flush the parse buffer so that the
822 * line can be parsed. Note that completely blank lines won't show
823 * up in the parse buffer, so they get parsed directly.
825 if (key_file->parse_buffer->len > 0)
826 g_key_file_flush_parse_buffer (key_file, &parse_error);
828 g_key_file_parse_comment (key_file, "", 1, &parse_error);
832 g_propagate_error (error, parse_error);
837 g_string_append_c (key_file->parse_buffer, data[i]);
840 key_file->approximate_size += length;
844 g_key_file_flush_parse_buffer (GKeyFile *key_file,
847 GError *file_error = NULL;
849 g_return_if_fail (key_file != NULL);
853 if (key_file->parse_buffer->len > 0)
855 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
856 key_file->parse_buffer->len,
858 g_string_erase (key_file->parse_buffer, 0, -1);
862 g_propagate_error (error, file_error);
869 * g_key_file_to_data:
870 * @key_file: a #GKeyFile
871 * @length: return location for the length of the
872 * returned string, or %NULL
873 * @error: return location for a #GError, or %NULL
875 * This function outputs @key_file as a string.
877 * Return value: a newly allocated string holding
878 * the contents of the #GKeyFile
883 g_key_file_to_data (GKeyFile *key_file,
887 GString *data_string;
889 GList *group_node, *key_file_node;
891 g_return_val_if_fail (key_file != NULL, NULL);
893 data_string = g_string_sized_new (2 * key_file->approximate_size);
895 for (group_node = g_list_last (key_file->groups);
897 group_node = group_node->prev)
899 GKeyFileGroup *group;
901 group = (GKeyFileGroup *) group_node->data;
903 if (group->name != NULL)
904 g_string_append_printf (data_string, "[%s]\n", group->name);
906 for (key_file_node = g_list_last (group->key_value_pairs);
907 key_file_node != NULL;
908 key_file_node = key_file_node->prev)
910 GKeyFileKeyValuePair *pair;
912 pair = (GKeyFileKeyValuePair *) key_file_node->data;
914 if (pair->key != NULL)
915 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
917 g_string_append_printf (data_string, "%s\n", pair->value);
922 *length = data_string->len;
924 data = data_string->str;
926 g_string_free (data_string, FALSE);
932 * g_key_file_get_keys:
933 * @key_file: a #GKeyFile
934 * @group_name: a group name, or %NULL
935 * @length: return location for the number of keys returned, or %NULL
936 * @error: return location for a #GError, or %NULL
938 * Returns all keys for the group name @group_name. If @group_name is
939 * %NULL, the start group is used. The array of returned keys will be
940 * %NULL-terminated, so @length may optionally be %NULL.
942 * Return value: a newly-allocated %NULL-terminated array of
943 * strings. Use g_strfreev() to free it.
948 g_key_file_get_keys (GKeyFile *key_file,
949 const gchar *group_name,
953 GKeyFileGroup *group;
958 g_return_val_if_fail (key_file != NULL, NULL);
960 if (group_name == NULL)
961 group_name = (const gchar *) key_file->start_group_name;
963 group = g_key_file_lookup_group (key_file, group_name);
967 g_set_error (error, G_KEY_FILE_ERROR,
968 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
969 _("Key file does not have group '%s'"),
974 num_keys = g_list_length (group->key_value_pairs);
976 keys = (gchar **) g_new (gchar **, num_keys + 1);
978 tmp = group->key_value_pairs;
979 for (i = 0; i < num_keys; i++)
981 GKeyFileKeyValuePair *pair;
983 pair = (GKeyFileKeyValuePair *) tmp->data;
984 keys[i] = g_strdup (pair->key);
997 * g_key_file_get_start_group:
998 * @key_file: a #GKeyFile
1000 * Returns the name of the start group of the file.
1002 * Return value: The start group of the key file.
1007 g_key_file_get_start_group (GKeyFile *key_file)
1009 g_return_val_if_fail (key_file != NULL, NULL);
1011 if (key_file->start_group_name)
1012 return g_strdup (key_file->start_group_name);
1018 * g_key_file_get_groups:
1019 * @key_file: a #GKeyFile
1020 * @length: return location for the number of returned groups, or %NULL
1022 * Returns all groups in the key file loaded with @key_file. The
1023 * array of returned groups will be %NULL-terminated, so @length may
1024 * optionally be %NULL.
1026 * Return value: a newly-allocated %NULL-terminated array of strings.
1027 * Use g_strfreev() to free it.
1031 g_key_file_get_groups (GKeyFile *key_file,
1036 gsize i, num_groups;
1038 g_return_val_if_fail (key_file != NULL, NULL);
1040 num_groups = g_list_length (key_file->groups);
1041 groups = (gchar **) g_new (gchar **, num_groups + 1);
1043 tmp = key_file->groups;
1044 for (i = 0; i < num_groups; i++)
1046 GKeyFileGroup *group;
1048 group = (GKeyFileGroup *) tmp->data;
1049 groups[i] = g_strdup (group->name);
1056 *length = num_groups;
1062 * g_key_file_get_value:
1063 * @key_file: a #GKeyFile
1064 * @group_name: a group name
1066 * @error: return location for a #GError, or #NULL
1068 * Returns the value associated with @key under @group_name.
1069 * In the event the key cannot be found, %NULL is returned and
1070 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1071 * event that the @group_name cannot be found, %NULL is returned
1072 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1074 * Return value: a string or %NULL if the specified key cannot be
1080 g_key_file_get_value (GKeyFile *key_file,
1081 const gchar *group_name,
1085 GKeyFileGroup *group;
1086 GKeyFileKeyValuePair *pair;
1087 gchar *value = NULL;
1089 if (group_name == NULL)
1090 group_name = (const gchar *) key_file->start_group_name;
1092 group = g_key_file_lookup_group (key_file, group_name);
1096 g_set_error (error, G_KEY_FILE_ERROR,
1097 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1098 _("Key file does not have group '%s'"),
1103 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1106 value = g_strdup (pair->value);
1108 g_set_error (error, G_KEY_FILE_ERROR,
1109 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1110 _("Key file does not have key '%s'"), key);
1116 * g_key_file_set_value:
1117 * @key_file: a #GKeyFile
1118 * @group_name: a group name
1122 * Associates a new value with @key under @group_name.
1123 * If @key cannot be found then it is created. If @group_name
1124 * cannot be found then it is created as well.
1129 g_key_file_set_value (GKeyFile *key_file,
1130 const gchar *group_name,
1134 GKeyFileGroup *group;
1135 GKeyFileKeyValuePair *pair;
1139 if (group_name == NULL)
1140 group_name = (const gchar *) key_file->start_group_name;
1142 if (!g_key_file_has_key (key_file, group_name, key, NULL))
1143 g_key_file_add_key (key_file, group_name, key, value);
1146 group = g_key_file_lookup_group (key_file, group_name);
1147 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1148 g_free (pair->value);
1149 pair->value = g_strdup (value);
1154 * g_key_file_get_string:
1155 * @key_file: a #GKeyFile
1156 * @group_name: a group name
1158 * @error: return location for a #GError, or #NULL
1160 * Returns the value associated with @key under @group_name.
1161 * In the event the key cannot be found, %NULL is returned and
1162 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1163 * event that the @group_name cannot be found, %NULL is returned
1164 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1166 * Return value: a string or %NULL if the specified key cannot be
1172 g_key_file_get_string (GKeyFile *key_file,
1173 const gchar *group_name,
1177 gchar *value, *string_value;
1178 GError *key_file_error;
1180 g_return_val_if_fail (key_file != NULL, NULL);
1181 g_return_val_if_fail (key != NULL, NULL);
1183 key_file_error = NULL;
1185 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1189 g_propagate_error (error, key_file_error);
1193 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1199 if (g_error_matches (key_file_error,
1201 G_KEY_FILE_ERROR_INVALID_VALUE))
1203 g_set_error (error, G_KEY_FILE_ERROR,
1204 G_KEY_FILE_ERROR_INVALID_VALUE,
1205 _("Key file contains key '%s' "
1206 "which has value that cannot be interpreted."),
1208 g_error_free (key_file_error);
1211 g_propagate_error (error, key_file_error);
1214 return string_value;
1218 * g_key_file_set_string:
1219 * @key_file: a #GKeyFile
1220 * @group_name: a group name
1224 * Associates a new string value with @key under @group_name.
1225 * If @key cannot be found then it is created. If @group_name
1226 * cannot be found then it is created as well.
1231 g_key_file_set_string (GKeyFile *key_file,
1232 const gchar *group_name,
1234 const gchar *string)
1238 g_return_if_fail (key_file != NULL);
1239 g_return_if_fail (key != NULL);
1241 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1242 g_key_file_set_value (key_file, group_name, key, value);
1247 * g_key_file_get_string_list:
1248 * @key_file: a #GKeyFile
1249 * @group_name: a group name
1251 * @length: return location for the number of returned strings, or %NULL
1252 * @error: return location for a #GError, or %NULL
1254 * Returns the values associated with @key under @group_name.
1255 * In the event the key cannot be found, %NULL is returned and
1256 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1257 * event that the @group_name cannot be found, %NULL is returned
1258 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1260 * Return value: a %NULL-terminated string array or %NULL if the specified
1261 * key cannot be found. The array should be freed with g_strfreev().
1266 g_key_file_get_string_list (GKeyFile *key_file,
1267 const gchar *group_name,
1272 GError *key_file_error = NULL;
1273 gchar *value, *string_value, **values;
1275 GSList *p, *pieces = 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 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1287 g_free (string_value);
1291 if (g_error_matches (key_file_error,
1293 G_KEY_FILE_ERROR_INVALID_VALUE))
1295 g_set_error (error, G_KEY_FILE_ERROR,
1296 G_KEY_FILE_ERROR_INVALID_VALUE,
1297 _("Key file contains key '%s' "
1298 "which has value that cannot be interpreted."),
1300 g_error_free (key_file_error);
1303 g_propagate_error (error, key_file_error);
1306 len = g_slist_length (pieces);
1307 values = g_new (gchar *, len + 1);
1308 for (p = pieces, i = 0; p; p = p->next)
1309 values[i++] = p->data;
1312 g_slist_free (pieces);
1321 * g_key_file_set_string_list:
1322 * @key_file: a #GKeyFile
1323 * @group_name: a group name
1325 * @list: an array of locale string values
1326 * @length: number of locale string values in @list
1328 * Associates a list of string values for @key under @group_name.
1329 * If the @key cannot be found then it is created.
1334 g_key_file_set_string_list (GKeyFile *key_file,
1335 const gchar *group_name,
1337 const gchar * const list[],
1340 GString *value_list;
1343 g_return_if_fail (key_file != NULL);
1344 g_return_if_fail (key != NULL);
1346 value_list = g_string_sized_new (length * 128);
1347 for (i = 0; list[i] != NULL && i < length; i++)
1351 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1352 g_string_append (value_list, value);
1353 g_string_append_c (value_list, key_file->list_separator);
1358 g_key_file_set_value (key_file, group_name, key, value_list->str);
1359 g_string_free (value_list, TRUE);
1363 * g_key_file_set_locale_string:
1364 * @key_file: a #GKeyFile
1365 * @group_name: a group name
1370 * Associates a string value for @key and @locale under
1371 * @group_name. If the translation for @key cannot be found
1372 * then it is created.
1377 g_key_file_set_locale_string (GKeyFile *key_file,
1378 const gchar *group_name,
1380 const gchar *locale,
1381 const gchar *string)
1383 gchar *full_key, *value;
1385 g_return_if_fail (key_file != NULL);
1386 g_return_if_fail (key != NULL);
1388 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1389 full_key = g_strdup_printf ("%s[%s]", key, locale);
1390 g_key_file_set_value (key_file, group_name, full_key, value);
1395 extern GSList *_g_compute_locale_variants (const gchar *locale);
1398 * g_key_file_get_locale_string:
1399 * @key_file: a #GKeyFile
1400 * @group_name: a group name
1402 * @locale: a locale or %NULL
1403 * @error: return location for a #GError, or %NULL
1405 * Returns the value associated with @key under @group_name
1406 * translated in the given @locale if available. If @locale is
1407 * %NULL then the current locale is assumed. If @key cannot be
1408 * found then %NULL is returned and @error is set to
1409 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1410 * with @key cannot be interpreted or no suitable translation can
1411 * be found then the untranslated value is returned and @error is
1412 * set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1413 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1414 * event that the @group_name cannot be found, %NULL is returned
1415 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1417 * Return value: a string or %NULL if the specified key cannot be
1422 g_key_file_get_locale_string (GKeyFile *key_file,
1423 const gchar *group_name,
1425 const gchar *locale,
1428 gchar *candidate_key, *translated_value;
1429 GError *key_file_error;
1431 gboolean free_languages = FALSE;
1434 candidate_key = NULL;
1435 translated_value = NULL;
1436 key_file_error = NULL;
1438 if (!g_key_file_has_group (key_file, group_name))
1440 g_set_error (error, G_KEY_FILE_ERROR,
1441 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1442 _("Key file does not have group '%s'"),
1451 list = _g_compute_locale_variants (locale);
1453 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1454 for (l = list, i = 0; l; l = l->next, i++)
1455 languages[i] = l->data;
1456 languages[i] = NULL;
1458 g_slist_free (list);
1459 free_languages = TRUE;
1463 languages = (gchar **) g_get_language_names ();
1464 free_languages = FALSE;
1467 for (i = 0; languages[i]; i++)
1469 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1471 translated_value = g_key_file_get_string (key_file,
1473 candidate_key, NULL);
1474 g_free (candidate_key);
1476 if (translated_value)
1480 if (translated_value && !g_utf8_validate (translated_value, -1, NULL))
1482 g_set_error (error, G_KEY_FILE_ERROR,
1483 G_KEY_FILE_ERROR_INVALID_VALUE,
1484 _("Key file contains key '%s' "
1485 "which has value that cannot be interpreted."),
1487 g_free (translated_value);
1488 translated_value = NULL;
1491 /* Fallback to untranslated key
1493 if (!translated_value)
1495 translated_value = g_key_file_get_string (key_file, group_name, key,
1498 if (!translated_value)
1499 g_propagate_error (error, key_file_error);
1501 g_set_error (error, G_KEY_FILE_ERROR,
1502 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1503 _("Key file contains no translated value "
1504 "for key '%s' with locale '%s'."),
1509 g_strfreev (languages);
1511 return translated_value;
1515 * g_key_file_get_locale_string_list:
1516 * @key_file: a #GKeyFile
1517 * @group_name: a group name
1520 * @length: return location for the number of returned strings or %NULL
1521 * @error: return location for a #GError or %NULL
1523 * Returns the values associated with @key under @group_name
1524 * translated in the given @locale if available. If @locale is
1525 * %NULL then the current locale is assumed. If @key cannot be
1526 * found then %NULL is returned and @error is set to
1527 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1528 * with @key cannot be interpreted or no suitable translations
1529 * can be found then the untranslated values are returned and
1530 * @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1531 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1532 * event that the @group_name cannot be found, %NULL is returned
1533 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1534 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1536 * Return value: a newly allocated %NULL-terminated string array
1537 * or %NULL if the key isn't found. The string array should be freed
1538 * with g_strfreev().
1543 g_key_file_get_locale_string_list (GKeyFile *key_file,
1544 const gchar *group_name,
1546 const gchar *locale,
1550 GError *key_file_error;
1551 gchar **values, *value;
1553 key_file_error = NULL;
1555 value = g_key_file_get_locale_string (key_file, group_name,
1560 g_propagate_error (error, key_file_error);
1565 if (value[strlen (value) - 1] == ';')
1566 value[strlen (value) - 1] = '\0';
1568 values = g_strsplit (value, ";", 0);
1573 *length = g_strv_length (values);
1579 * g_key_file_set_locale_string_list:
1580 * @key_file: a #GKeyFile
1581 * @group_name: a group name
1584 * @list: a %NULL-terminated array of locale string values
1585 * @length: the length of @list
1587 * Associates a list of string values for @key and @locale under
1588 * @group_name. If the translation for @key cannot be found then
1594 g_key_file_set_locale_string_list (GKeyFile *key_file,
1595 const gchar *group_name,
1597 const gchar *locale,
1598 const gchar * const list[],
1601 GString *value_list;
1605 g_return_if_fail (key_file != NULL);
1606 g_return_if_fail (key != NULL);
1608 value_list = g_string_sized_new (length * 128);
1609 for (i = 0; list[i] != NULL && i < length; i++)
1613 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1615 g_string_append (value_list, value);
1616 g_string_append_c (value_list, ';');
1621 full_key = g_strdup_printf ("%s[%s]", key, locale);
1622 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1624 g_string_free (value_list, TRUE);
1628 * g_key_file_get_boolean:
1629 * @key_file: a #GKeyFile
1630 * @group_name: a group name
1632 * @error: return location for a #GError
1634 * Returns the value associated with @key under @group_name as a
1635 * boolean. If @key cannot be found then the return value is
1636 * undefined and @error is set to
1637 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1638 * associated with @key cannot be interpreted as a boolean then
1639 * the return value is also undefined and @error is set to
1640 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1642 * Return value: the value associated with the key as a boolean
1646 g_key_file_get_boolean (GKeyFile *key_file,
1647 const gchar *group_name,
1651 GError *key_file_error = NULL;
1653 gboolean bool_value;
1655 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1659 g_propagate_error (error, key_file_error);
1663 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1669 if (g_error_matches (key_file_error,
1671 G_KEY_FILE_ERROR_INVALID_VALUE))
1673 g_set_error (error, G_KEY_FILE_ERROR,
1674 G_KEY_FILE_ERROR_INVALID_VALUE,
1675 _("Key file contains key '%s' "
1676 "which has value that cannot be interpreted."),
1678 g_error_free (key_file_error);
1681 g_propagate_error (error, key_file_error);
1688 * g_key_file_set_boolean:
1689 * @key_file: a #GKeyFile
1690 * @group_name: a group name
1692 * @value: %TRUE or %FALSE
1694 * Associates a new boolean value with @key under @group_name.
1695 * If @key cannot be found then it is created.
1700 g_key_file_set_boolean (GKeyFile *key_file,
1701 const gchar *group_name,
1707 g_return_if_fail (key_file != NULL);
1708 g_return_if_fail (key != NULL);
1710 result = g_key_file_parse_boolean_as_value (key_file, value);
1711 g_key_file_set_value (key_file, group_name, key, result);
1716 * g_key_file_get_boolean_list:
1717 * @key_file: a #GKeyFile
1718 * @group_name: a group name
1720 * @length: the number of booleans returned
1721 * @error: return location for a #GError
1723 * Returns the values associated with @key under @group_name as
1724 * booleans. If @key cannot be found then the return value is
1725 * undefined and @error is set to
1726 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
1727 * associated with @key cannot be interpreted as booleans then
1728 * the return value is also undefined and @error is set to
1729 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1731 * Return value: the values associated with the key as a boolean
1734 g_key_file_get_boolean_list (GKeyFile *key_file,
1735 const gchar *group_name,
1740 GError *key_file_error;
1742 gboolean *bool_values;
1745 key_file_error = NULL;
1747 values = g_key_file_get_string_list (key_file, group_name, key,
1748 &num_bools, &key_file_error);
1751 g_propagate_error (error, key_file_error);
1756 bool_values = g_new (gboolean, num_bools);
1758 for (i = 0; i < num_bools; i++)
1760 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1766 g_propagate_error (error, key_file_error);
1767 g_strfreev (values);
1768 g_free (bool_values);
1773 g_strfreev (values);
1776 *length = num_bools;
1782 * g_key_file_set_boolean_list:
1783 * @key_file: a #GKeyFile
1784 * @group_name: a group name
1786 * @list: an array of boolean values
1787 * @length: length of @list
1789 * Associates a list of boolean values with @key under
1790 * @group_name. If @key cannot be found then it is created.
1795 g_key_file_set_boolean_list (GKeyFile *key_file,
1796 const gchar *group_name,
1801 GString *value_list;
1804 g_return_if_fail (key_file != NULL);
1805 g_return_if_fail (key != NULL);
1807 value_list = g_string_sized_new (length * 8);
1808 for (i = 0; i < length; i++)
1812 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1814 g_string_append (value_list, value);
1815 g_string_append_c (value_list, key_file->list_separator);
1820 g_key_file_set_value (key_file, group_name, key, value_list->str);
1821 g_string_free (value_list, TRUE);
1825 * g_key_file_get_integer:
1826 * @key_file: a #GKeyFile
1827 * @group_name: a group name
1829 * @error: return location for a #GError
1831 * Returns the value associated with @key under @group_name as an
1832 * integer. If @key cannot be found then the return value is
1833 * undefined and @error is set to
1834 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1835 * associated with @key cannot be interpreted as an integer then
1836 * the return value is also undefined and @error is set to
1837 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1839 * Return value: the value associated with the key as an integer.
1843 g_key_file_get_integer (GKeyFile *key_file,
1844 const gchar *group_name,
1848 GError *key_file_error;
1852 key_file_error = NULL;
1854 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1858 g_propagate_error (error, key_file_error);
1862 int_value = g_key_file_parse_value_as_integer (key_file, value,
1868 if (g_error_matches (key_file_error,
1870 G_KEY_FILE_ERROR_INVALID_VALUE))
1872 g_set_error (error, G_KEY_FILE_ERROR,
1873 G_KEY_FILE_ERROR_INVALID_VALUE,
1874 _("Key file contains key '%s' in group '%s' "
1875 "which has value that cannot be interpreted."), key,
1877 g_error_free (key_file_error);
1880 g_propagate_error (error, key_file_error);
1887 * g_key_file_set_integer:
1888 * @key_file: a #GKeyFile
1889 * @group_name: a group name
1891 * @value: an integer value
1893 * Associates a new integer value with @key under @group_name.
1894 * If @key cannot be found then it is created.
1899 g_key_file_set_integer (GKeyFile *key_file,
1900 const gchar *group_name,
1906 g_return_if_fail (key_file != NULL);
1907 g_return_if_fail (key != NULL);
1909 result = g_key_file_parse_integer_as_value (key_file, value);
1910 g_key_file_set_value (key_file, group_name, key, result);
1915 * g_key_file_get_integer_list:
1916 * @key_file: a #GKeyFile
1917 * @group_name: a group name
1919 * @length: the number of integers returned
1920 * @error: return location for a #GError
1922 * Returns the values associated with @key under @group_name as
1923 * integers. If @key cannot be found then the return value is
1924 * undefined and @error is set to
1925 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
1926 * associated with @key cannot be interpreted as integers then
1927 * the return value is also undefined and @error is set to
1928 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1930 * Return value: the values associated with the key as a integer
1934 g_key_file_get_integer_list (GKeyFile *key_file,
1935 const gchar *group_name,
1940 GError *key_file_error = NULL;
1945 values = g_key_file_get_string_list (key_file, group_name, key,
1946 &num_ints, &key_file_error);
1949 g_propagate_error (error, key_file_error);
1954 int_values = g_new (gint, num_ints);
1956 for (i = 0; i < num_ints; i++)
1958 int_values[i] = g_key_file_parse_value_as_integer (key_file,
1964 g_propagate_error (error, key_file_error);
1965 g_strfreev (values);
1966 g_free (int_values);
1971 g_strfreev (values);
1980 * g_key_file_set_integer_list:
1981 * @key_file: a #GKeyFile
1982 * @group_name: a group name
1984 * @list: an array of integer values
1985 * @length: number of integer values in @list
1987 * Associates a list of integer values with @key under
1988 * @group_name. If @key cannot be found then it is created.
1993 g_key_file_set_integer_list (GKeyFile *key_file,
1994 const gchar *group_name,
2002 g_return_if_fail (key_file != NULL);
2003 g_return_if_fail (key != NULL);
2005 values = g_string_sized_new (length * 16);
2006 for (i = 0; i < length; i++)
2010 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2012 g_string_append (values, value);
2013 g_string_append_c (values, ';');
2018 g_key_file_set_value (key_file, group_name, key, values->str);
2019 g_string_free (values, TRUE);
2023 * g_key_file_has_group:
2024 * @key_file: a #GKeyFile
2025 * @group_name: a group name
2027 * Looks whether the key file has the group @group_name.
2029 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2034 g_key_file_has_group (GKeyFile *key_file,
2035 const gchar *group_name)
2038 GKeyFileGroup *group;
2040 g_return_val_if_fail (key_file != NULL, FALSE);
2041 g_return_val_if_fail (group_name != NULL, FALSE);
2043 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2045 group = (GKeyFileGroup *) tmp->data;
2046 if (group && group->name && (strcmp (group->name, group_name) == 0))
2054 * g_key_file_has_key:
2055 * @key_file: a #GKeyFile
2056 * @group_name: a group name
2058 * @error: return location for a #GError
2060 * Looks whether the key file has the key @key in the group
2063 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2068 g_key_file_has_key (GKeyFile *key_file,
2069 const gchar *group_name,
2073 GKeyFileKeyValuePair *pair;
2074 GKeyFileGroup *group;
2076 g_return_val_if_fail (key_file != NULL, FALSE);
2077 g_return_val_if_fail (key != NULL, FALSE);
2081 if (group_name == NULL)
2082 group_name = (const gchar *) key_file->start_group_name;
2084 group = g_key_file_lookup_group (key_file, group_name);
2088 g_set_error (error, G_KEY_FILE_ERROR,
2089 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2090 _("Key file does not have group '%s'"),
2096 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2098 return pair != NULL;
2102 g_key_file_add_group (GKeyFile *key_file,
2103 const gchar *group_name)
2105 GKeyFileGroup *group;
2107 g_return_if_fail (key_file != NULL);
2108 g_return_if_fail (group_name != NULL);
2109 g_return_if_fail (g_key_file_lookup_group (key_file, group_name) == NULL);
2111 group = g_new0 (GKeyFileGroup, 1);
2112 group->name = g_strdup (group_name);
2113 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2114 key_file->groups = g_list_prepend (key_file->groups, group);
2115 key_file->approximate_size += strlen (group_name) + 3;
2116 key_file->current_group = group;
2118 if (key_file->start_group_name == NULL)
2119 key_file->start_group_name = g_strdup (group_name);
2124 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2129 g_free (pair->value);
2135 g_key_file_remove_group_node (GKeyFile *key_file,
2138 GKeyFileGroup *group;
2140 group = (GKeyFileGroup *) group_node->data;
2142 /* If the current group gets deleted make the current group the first
2145 if (key_file->current_group == group)
2149 first_group = key_file->groups;
2152 key_file->current_group = (GKeyFileGroup *) first_group->data;
2154 key_file->current_group = NULL;
2157 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2159 if (group->name != NULL)
2160 key_file->approximate_size -= strlen (group->name) + 3;
2162 /* FIXME: approximate_size isn't getting updated for the
2163 * removed keys in group.
2165 g_list_foreach (group->key_value_pairs,
2166 (GFunc) g_key_file_key_value_pair_free, NULL);
2167 g_list_free (group->key_value_pairs);
2168 group->key_value_pairs = NULL;
2170 if (group->lookup_map)
2172 g_hash_table_destroy (group->lookup_map);
2173 group->lookup_map = NULL;
2176 g_free ((gchar *) group->name);
2178 g_list_free_1 (group_node);
2182 * g_key_file_remove_group:
2183 * @key_file: a #GKeyFile
2184 * @group_name: a group name
2185 * @error: return location for a #GError or %NULL
2187 * Removes the specified group, @group_name,
2188 * from the key file.
2193 g_key_file_remove_group (GKeyFile *key_file,
2194 const gchar *group_name,
2197 GKeyFileGroup *group;
2200 g_return_if_fail (key_file != NULL);
2201 g_return_if_fail (group_name != NULL);
2203 group = g_key_file_lookup_group (key_file, group_name);
2206 g_set_error (error, G_KEY_FILE_ERROR,
2207 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2208 _("Key file does not have group '%s'"),
2211 group_node = g_list_find (key_file->groups, group);
2212 g_key_file_remove_group_node (key_file, group_node);
2216 g_key_file_add_key (GKeyFile *key_file,
2217 const gchar *group_name,
2221 GKeyFileGroup *group;
2222 GKeyFileKeyValuePair *pair;
2224 group = g_key_file_lookup_group (key_file, group_name);
2228 g_key_file_add_group (key_file, group_name);
2229 group = (GKeyFileGroup *) key_file->groups->data;
2232 pair = g_new0 (GKeyFileKeyValuePair, 1);
2234 pair->key = g_strdup (key);
2235 pair->value = g_strdup (value);
2237 g_hash_table_replace (group->lookup_map, pair->key, pair);
2238 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2239 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2243 * g_key_file_remove_key:
2244 * @key_file: a #GKeyFile
2245 * @group_name: a group name
2246 * @key: a key name to remove
2247 * @error: return location for a #GError or %NULL
2249 * Removes @key in @group_name from the key file.
2254 g_key_file_remove_key (GKeyFile *key_file,
2255 const gchar *group_name,
2259 GKeyFileGroup *group;
2260 GKeyFileKeyValuePair *pair;
2264 if (group_name == NULL)
2265 group = key_file->current_group;
2267 group = g_key_file_lookup_group (key_file, group_name);
2271 g_set_error (error, G_KEY_FILE_ERROR,
2272 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2273 _("Key file does not have group '%s'"),
2278 group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2279 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2283 g_set_error (error, G_KEY_FILE_ERROR,
2284 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2285 _("Key file does not have key '%s'"), key);
2289 g_hash_table_remove (group->lookup_map, pair->key);
2291 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2292 g_key_file_key_value_pair_free (pair);
2295 static GKeyFileGroup *
2296 g_key_file_lookup_group (GKeyFile *key_file,
2297 const gchar *group_name)
2299 GKeyFileGroup *group;
2303 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2305 group = (GKeyFileGroup *) tmp->data;
2307 if (group && group->name && strcmp (group->name, group_name) == 0)
2316 static GKeyFileKeyValuePair *
2317 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2318 GKeyFileGroup *group,
2321 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2324 /* Lines starting with # or consisting entirely of whitespace are merely
2325 * recorded, not parsed. This function assumes all leading whitespace
2326 * has been stripped.
2329 g_key_file_line_is_comment (const gchar *line)
2331 return (*line == '#' || *line == '\0' || *line == '\n');
2334 /* A group in a key file is made up of a starting '[' followed by one
2335 * or more letters making up the group name followed by ']'.
2338 g_key_file_line_is_group (const gchar *line)
2346 p = g_utf8_next_char (p);
2351 p = g_utf8_next_char (p);
2353 /* Group name must be non-empty
2358 while (*p && *p != ']')
2359 p = g_utf8_next_char (p);
2368 g_key_file_line_is_key_value_pair (const gchar *line)
2372 p = (gchar *) g_utf8_strchr (line, -1, '=');
2377 /* Key must be non-empty
2386 g_key_file_parse_value_as_string (GKeyFile *key_file,
2391 GError *parse_error = NULL;
2392 gchar *string_value, *p, *q0, *q;
2394 string_value = g_new (gchar, strlen (value) + 1);
2396 p = (gchar *) value;
2397 q0 = q = string_value;
2427 if (pieces && *p == key_file->list_separator)
2428 *q = key_file->list_separator;
2434 if (parse_error == NULL)
2442 g_set_error (error, G_KEY_FILE_ERROR,
2443 G_KEY_FILE_ERROR_INVALID_VALUE,
2444 _("Key file contains invalid escape "
2445 "sequence '%s'"), sequence);
2454 if (pieces && (*p == key_file->list_separator))
2456 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
2465 if (p[-1] == '\\' && error == NULL)
2466 g_set_error (error, G_KEY_FILE_ERROR,
2467 G_KEY_FILE_ERROR_INVALID_VALUE,
2468 _("Key file contains escape character at end of line"));
2474 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
2475 *pieces = g_slist_reverse (*pieces);
2478 return string_value;
2482 g_key_file_parse_string_as_value (GKeyFile *key_file,
2483 const gchar *string,
2484 gboolean escape_separator)
2486 gchar *value, *p, *q;
2489 length = strlen (string) + 1;
2491 /* Worst case would be that every character needs to be escaped.
2492 * In other words every character turns to two characters
2494 value = g_new (gchar, 2 * length);
2496 p = (gchar *) string;
2498 while (p < (string + length - 1))
2500 gchar escaped_character[3] = { '\\', 0, 0 };
2505 escaped_character[1] = 's';
2506 strcpy (q, escaped_character);
2510 escaped_character[1] = 'n';
2511 strcpy (q, escaped_character);
2515 escaped_character[1] = 't';
2516 strcpy (q, escaped_character);
2520 escaped_character[1] = 'r';
2521 strcpy (q, escaped_character);
2525 escaped_character[1] = '\\';
2526 strcpy (q, escaped_character);
2530 if (escape_separator && *p == key_file->list_separator)
2532 escaped_character[1] = key_file->list_separator;
2533 strcpy (q, escaped_character);
2551 g_key_file_parse_value_as_integer (GKeyFile *key_file,
2555 gchar *end_of_valid_int;
2558 int_value = strtol (value, &end_of_valid_int, 0);
2560 if (*end_of_valid_int != '\0')
2561 g_set_error (error, G_KEY_FILE_ERROR,
2562 G_KEY_FILE_ERROR_INVALID_VALUE,
2563 _("Value '%s' cannot be interpreted as a number."), value);
2569 g_key_file_parse_integer_as_value (GKeyFile *key_file,
2573 return g_strdup_printf ("%d", value);
2577 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
2583 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
2585 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
2589 g_set_error (error, G_KEY_FILE_ERROR,
2590 G_KEY_FILE_ERROR_INVALID_VALUE,
2591 _("Value '%s' cannot be interpreted as a boolean."), value);
2597 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
2601 return g_strdup ("true");
2603 return g_strdup ("false");