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;
590 while (*data_dirs != NULL && !found_file)
592 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
598 g_propagate_error (error, key_file_error);
602 found_file = g_key_file_load_from_fd (key_file, fd, flags,
608 g_propagate_error (error, key_file_error);
609 g_free (output_path);
614 *full_path = output_path;
617 g_strfreev (all_data_dirs);
623 * @key_file: a #GKeyFile
630 g_key_file_free (GKeyFile *key_file)
632 g_return_if_fail (key_file != NULL);
634 g_key_file_clear (key_file);
638 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
639 * true for locales that match those in g_get_language_names().
642 g_key_file_locale_is_interesting (GKeyFile *key_file,
645 const gchar * const * current_locales;
648 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
651 current_locales = g_get_language_names ();
653 for (i = 0; current_locales[i] != NULL; i++)
655 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
663 g_key_file_parse_line (GKeyFile *key_file,
668 GError *parse_error = NULL;
671 g_return_if_fail (key_file != NULL);
672 g_return_if_fail (line != NULL);
674 line_start = (gchar *) line;
675 while (g_ascii_isspace (*line_start))
678 if (g_key_file_line_is_comment (line_start))
679 g_key_file_parse_comment (key_file, line, length, &parse_error);
680 else if (g_key_file_line_is_group (line_start))
681 g_key_file_parse_group (key_file, line_start,
682 length - (line_start - line),
684 else if (g_key_file_line_is_key_value_pair (line_start))
685 g_key_file_parse_key_value_pair (key_file, line_start,
686 length - (line_start - line),
690 g_set_error (error, G_KEY_FILE_ERROR,
691 G_KEY_FILE_ERROR_PARSE,
692 _("Key file contains line '%s' which is not "
693 "a key-value pair, group, or comment"), line);
698 g_propagate_error (error, parse_error);
702 g_key_file_parse_comment (GKeyFile *key_file,
707 GKeyFileKeyValuePair *pair;
709 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
712 g_assert (key_file->current_group != NULL);
714 pair = g_new0 (GKeyFileKeyValuePair, 1);
717 pair->value = g_strndup (line, length);
719 key_file->current_group->key_value_pairs =
720 g_list_prepend (key_file->current_group->key_value_pairs, pair);
724 g_key_file_parse_group (GKeyFile *key_file,
730 const gchar *group_name_start, *group_name_end;
732 /* advance past opening '['
734 group_name_start = line + 1;
735 group_name_end = line + length - 1;
737 while (*group_name_end != ']')
740 group_name = g_strndup (group_name_start,
741 group_name_end - group_name_start);
743 g_key_file_add_group (key_file, group_name);
748 g_key_file_parse_key_value_pair (GKeyFile *key_file,
753 gchar *key, *value, *key_end, *value_start, *locale;
754 gsize key_len, value_len;
756 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
758 g_set_error (error, G_KEY_FILE_ERROR,
759 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
760 _("Key file does not start with a group"));
764 key_end = value_start = strchr (line, '=');
766 g_assert (key_end != NULL);
771 /* Pull the key name from the line (chomping trailing whitespace)
773 while (g_ascii_isspace (*key_end))
776 key_len = key_end - line + 2;
778 g_assert (key_len <= length);
780 key = g_strndup (line, key_len - 1);
782 /* Pull the value from the line (chugging leading whitespace)
784 while (g_ascii_isspace (*value_start))
787 value_len = line + length - value_start + 1;
789 value = g_strndup (value_start, value_len);
791 g_assert (key_file->start_group != NULL);
793 if (key_file->current_group
794 && key_file->current_group->name
795 && strcmp (key_file->start_group->name,
796 key_file->current_group->name) == 0
797 && strcmp (key, "Encoding") == 0)
799 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
801 g_set_error (error, G_KEY_FILE_ERROR,
802 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
803 _("Key file contains unsupported encoding '%s'"), value);
811 /* Is this key a translation? If so, is it one that we care about?
813 locale = key_get_locale (key);
815 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
816 g_key_file_add_key (key_file, key_file->current_group, key, value);
824 key_get_locale (const gchar *key)
828 locale = g_strrstr (key, "[");
830 if (locale && strlen (locale) <= 2)
834 locale = g_strndup (locale + 1, strlen (locale) - 2);
840 g_key_file_parse_data (GKeyFile *key_file,
848 g_return_if_fail (key_file != NULL);
849 g_return_if_fail (data != NULL);
853 for (i = 0; i < length; i++)
857 if (i > 0 && data[i - 1] == '\r')
858 g_string_erase (key_file->parse_buffer,
859 key_file->parse_buffer->len - 1,
862 /* When a newline is encountered flush the parse buffer so that the
863 * line can be parsed. Note that completely blank lines won't show
864 * up in the parse buffer, so they get parsed directly.
866 if (key_file->parse_buffer->len > 0)
867 g_key_file_flush_parse_buffer (key_file, &parse_error);
869 g_key_file_parse_comment (key_file, "", 1, &parse_error);
873 g_propagate_error (error, parse_error);
878 g_string_append_c (key_file->parse_buffer, data[i]);
881 key_file->approximate_size += length;
885 g_key_file_flush_parse_buffer (GKeyFile *key_file,
888 GError *file_error = NULL;
890 g_return_if_fail (key_file != NULL);
894 if (key_file->parse_buffer->len > 0)
896 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
897 key_file->parse_buffer->len,
899 g_string_erase (key_file->parse_buffer, 0, -1);
903 g_propagate_error (error, file_error);
910 * g_key_file_to_data:
911 * @key_file: a #GKeyFile
912 * @length: return location for the length of the
913 * returned string, or %NULL
914 * @error: return location for a #GError, or %NULL
916 * This function outputs @key_file as a string.
918 * Return value: a newly allocated string holding
919 * the contents of the #GKeyFile
924 g_key_file_to_data (GKeyFile *key_file,
928 GString *data_string;
930 GList *group_node, *key_file_node;
932 g_return_val_if_fail (key_file != NULL, NULL);
934 data_string = g_string_sized_new (2 * key_file->approximate_size);
936 for (group_node = g_list_last (key_file->groups);
938 group_node = group_node->prev)
940 GKeyFileGroup *group;
942 group = (GKeyFileGroup *) group_node->data;
944 if (group->comment != NULL)
945 g_string_append_printf (data_string, "%s\n", group->comment->value);
946 if (group->name != NULL)
947 g_string_append_printf (data_string, "[%s]\n", group->name);
949 for (key_file_node = g_list_last (group->key_value_pairs);
950 key_file_node != NULL;
951 key_file_node = key_file_node->prev)
953 GKeyFileKeyValuePair *pair;
955 pair = (GKeyFileKeyValuePair *) key_file_node->data;
957 if (pair->key != NULL)
958 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
960 g_string_append_printf (data_string, "%s\n", pair->value);
965 *length = data_string->len;
967 data = data_string->str;
969 g_string_free (data_string, FALSE);
975 * g_key_file_get_keys:
976 * @key_file: a #GKeyFile
977 * @group_name: a group name
978 * @length: return location for the number of keys returned, or %NULL
979 * @error: return location for a #GError, or %NULL
981 * Returns all keys for the group name @group_name. The array of
982 * returned keys will be %NULL-terminated, so @length may
983 * optionally be %NULL. In the event that the @group_name cannot
984 * be found, %NULL is returned and @error is set to
985 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
987 * Return value: a newly-allocated %NULL-terminated array of
988 * strings. Use g_strfreev() to free it.
993 g_key_file_get_keys (GKeyFile *key_file,
994 const gchar *group_name,
998 GKeyFileGroup *group;
1003 g_return_val_if_fail (key_file != NULL, NULL);
1004 g_return_val_if_fail (group_name != NULL, NULL);
1006 group = g_key_file_lookup_group (key_file, group_name);
1010 g_set_error (error, G_KEY_FILE_ERROR,
1011 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1012 _("Key file does not have group '%s'"),
1013 group_name ? group_name : "(null)");
1017 num_keys = g_list_length (group->key_value_pairs);
1019 keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1021 tmp = group->key_value_pairs;
1022 for (i = 1; i <= num_keys; i++)
1024 GKeyFileKeyValuePair *pair;
1026 pair = (GKeyFileKeyValuePair *) tmp->data;
1027 keys[num_keys - i] = g_strdup (pair->key);
1031 keys[num_keys] = NULL;
1040 * g_key_file_get_start_group:
1041 * @key_file: a #GKeyFile
1043 * Returns the name of the start group of the file.
1045 * Return value: The start group of the key file.
1050 g_key_file_get_start_group (GKeyFile *key_file)
1052 g_return_val_if_fail (key_file != NULL, NULL);
1054 if (key_file->start_group)
1055 return g_strdup (key_file->start_group->name);
1061 * g_key_file_get_groups:
1062 * @key_file: a #GKeyFile
1063 * @length: return location for the number of returned groups, or %NULL
1065 * Returns all groups in the key file loaded with @key_file. The
1066 * array of returned groups will be %NULL-terminated, so @length may
1067 * optionally be %NULL.
1069 * Return value: a newly-allocated %NULL-terminated array of strings.
1070 * Use g_strfreev() to free it.
1074 g_key_file_get_groups (GKeyFile *key_file,
1079 gsize i, num_groups;
1081 g_return_val_if_fail (key_file != NULL, NULL);
1083 num_groups = g_list_length (key_file->groups);
1085 g_assert (num_groups > 0);
1087 /* Only need num_groups instead of num_groups + 1
1088 * because the first group of the file (last in the
1089 * list) is always the comment group at the top,
1092 groups = (gchar **) g_new0 (gchar **, num_groups);
1094 group_node = g_list_last (key_file->groups);
1096 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1099 for (group_node = group_node->prev;
1101 group_node = group_node->prev)
1103 GKeyFileGroup *group;
1105 group = (GKeyFileGroup *) group_node->data;
1107 g_assert (group->name != NULL);
1109 groups[i++] = g_strdup (group->name);
1120 * g_key_file_get_value:
1121 * @key_file: a #GKeyFile
1122 * @group_name: a group name
1124 * @error: return location for a #GError, or %NULL
1126 * Returns the value associated with @key under @group_name.
1128 * In the event the key cannot be found, %NULL is returned and
1129 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1130 * event that the @group_name cannot be found, %NULL is returned
1131 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1133 * Return value: a newly allocated string or %NULL if the specified
1134 * key cannot be found.
1139 g_key_file_get_value (GKeyFile *key_file,
1140 const gchar *group_name,
1144 GKeyFileGroup *group;
1145 GKeyFileKeyValuePair *pair;
1146 gchar *value = NULL;
1148 g_return_val_if_fail (key_file != NULL, NULL);
1149 g_return_val_if_fail (group_name != NULL, NULL);
1150 g_return_val_if_fail (key != NULL, NULL);
1152 group = g_key_file_lookup_group (key_file, group_name);
1156 g_set_error (error, G_KEY_FILE_ERROR,
1157 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1158 _("Key file does not have group '%s'"),
1159 group_name ? group_name : "(null)");
1163 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1166 value = g_strdup (pair->value);
1168 g_set_error (error, G_KEY_FILE_ERROR,
1169 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1170 _("Key file does not have key '%s'"), key);
1176 * g_key_file_set_value:
1177 * @key_file: a #GKeyFile
1178 * @group_name: a group name
1182 * Associates a new value with @key under @group_name. If @key
1183 * cannot be found then it is created. If @group_name cannot be
1184 * found then it is created.
1189 g_key_file_set_value (GKeyFile *key_file,
1190 const gchar *group_name,
1194 GKeyFileGroup *group;
1195 GKeyFileKeyValuePair *pair;
1197 g_return_if_fail (key_file != NULL);
1198 g_return_if_fail (group_name != NULL);
1199 g_return_if_fail (key != NULL);
1200 g_return_if_fail (value != NULL);
1202 group = g_key_file_lookup_group (key_file, group_name);
1206 g_key_file_add_group (key_file, group_name);
1207 group = (GKeyFileGroup *) key_file->groups->data;
1209 g_key_file_add_key (key_file, group, key, value);
1213 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1216 g_key_file_add_key (key_file, group, key, value);
1219 g_free (pair->value);
1220 pair->value = g_strdup (value);
1226 * g_key_file_get_string:
1227 * @key_file: a #GKeyFile
1228 * @group_name: a group name
1230 * @error: return location for a #GError, or %NULL
1232 * Returns the value associated with @key under @group_name.
1234 * In the event the key cannot be found, %NULL is returned and
1235 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1236 * event that the @group_name cannot be found, %NULL is returned
1237 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1239 * Return value: a newly allocated string or %NULL if the specified
1240 * key cannot be found.
1245 g_key_file_get_string (GKeyFile *key_file,
1246 const gchar *group_name,
1250 gchar *value, *string_value;
1251 GError *key_file_error;
1253 g_return_val_if_fail (key_file != NULL, NULL);
1254 g_return_val_if_fail (group_name != NULL, NULL);
1255 g_return_val_if_fail (key != NULL, NULL);
1257 key_file_error = NULL;
1259 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1263 g_propagate_error (error, key_file_error);
1267 if (!g_utf8_validate (value, -1, NULL))
1269 g_set_error (error, G_KEY_FILE_ERROR,
1270 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1271 _("Key file contains key '%s' with value '%s' "
1272 "which is not UTF-8"), key, value);
1277 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1283 if (g_error_matches (key_file_error,
1285 G_KEY_FILE_ERROR_INVALID_VALUE))
1287 g_set_error (error, G_KEY_FILE_ERROR,
1288 G_KEY_FILE_ERROR_INVALID_VALUE,
1289 _("Key file contains key '%s' "
1290 "which has value that cannot be interpreted."),
1292 g_error_free (key_file_error);
1295 g_propagate_error (error, key_file_error);
1298 return string_value;
1302 * g_key_file_set_string:
1303 * @key_file: a #GKeyFile
1304 * @group_name: a group name
1308 * Associates a new string value with @key under @group_name. If
1309 * @key cannot be found then it is created. If @group_name
1310 * cannot be found then it is created.
1315 g_key_file_set_string (GKeyFile *key_file,
1316 const gchar *group_name,
1318 const gchar *string)
1322 g_return_if_fail (key_file != NULL);
1323 g_return_if_fail (group_name != NULL);
1324 g_return_if_fail (key != NULL);
1325 g_return_if_fail (string != NULL);
1327 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1328 g_key_file_set_value (key_file, group_name, key, value);
1333 * g_key_file_get_string_list:
1334 * @key_file: a #GKeyFile
1335 * @group_name: a group name
1337 * @length: return location for the number of returned strings, or %NULL
1338 * @error: return location for a #GError, or %NULL
1340 * Returns the values associated with @key under @group_name.
1342 * In the event the key cannot be found, %NULL is returned and
1343 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1344 * event that the @group_name cannot be found, %NULL is returned
1345 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1347 * Return value: a %NULL-terminated string array or %NULL if the specified
1348 * key cannot be found. The array should be freed with g_strfreev().
1353 g_key_file_get_string_list (GKeyFile *key_file,
1354 const gchar *group_name,
1359 GError *key_file_error = NULL;
1360 gchar *value, *string_value, **values;
1362 GSList *p, *pieces = NULL;
1364 g_return_val_if_fail (key_file != NULL, NULL);
1365 g_return_val_if_fail (group_name != NULL, NULL);
1366 g_return_val_if_fail (key != NULL, NULL);
1368 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1372 g_propagate_error (error, key_file_error);
1376 if (!g_utf8_validate (value, -1, NULL))
1378 g_set_error (error, G_KEY_FILE_ERROR,
1379 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1380 _("Key file contains key '%s' with value '%s' "
1381 "which is not UTF-8"), key, value);
1386 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1388 g_free (string_value);
1392 if (g_error_matches (key_file_error,
1394 G_KEY_FILE_ERROR_INVALID_VALUE))
1396 g_set_error (error, G_KEY_FILE_ERROR,
1397 G_KEY_FILE_ERROR_INVALID_VALUE,
1398 _("Key file contains key '%s' "
1399 "which has value that cannot be interpreted."),
1401 g_error_free (key_file_error);
1404 g_propagate_error (error, key_file_error);
1407 len = g_slist_length (pieces);
1408 values = g_new0 (gchar *, len + 1);
1409 for (p = pieces, i = 0; p; p = p->next)
1410 values[i++] = p->data;
1413 g_slist_free (pieces);
1422 * g_key_file_set_string_list:
1423 * @key_file: a #GKeyFile
1424 * @group_name: a group name
1426 * @list: an array of locale string values
1427 * @length: number of locale string values in @list
1429 * Associates a list of string values for @key under @group_name.
1430 * If @key cannot be found then it is created. If @group_name
1431 * cannot be found then it is created.
1436 g_key_file_set_string_list (GKeyFile *key_file,
1437 const gchar *group_name,
1439 const gchar * const list[],
1442 GString *value_list;
1445 g_return_if_fail (key_file != NULL);
1446 g_return_if_fail (group_name != NULL);
1447 g_return_if_fail (key != NULL);
1448 g_return_if_fail (list != NULL);
1450 value_list = g_string_sized_new (length * 128);
1451 for (i = 0; list[i] != NULL && i < length; i++)
1455 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1456 g_string_append (value_list, value);
1457 g_string_append_c (value_list, key_file->list_separator);
1462 g_key_file_set_value (key_file, group_name, key, value_list->str);
1463 g_string_free (value_list, TRUE);
1467 * g_key_file_set_locale_string:
1468 * @key_file: a #GKeyFile
1469 * @group_name: a group name
1474 * Associates a string value for @key and @locale under
1475 * @group_name. If the translation for @key cannot be found
1476 * then it is created.
1481 g_key_file_set_locale_string (GKeyFile *key_file,
1482 const gchar *group_name,
1484 const gchar *locale,
1485 const gchar *string)
1487 gchar *full_key, *value;
1489 g_return_if_fail (key_file != NULL);
1490 g_return_if_fail (group_name != NULL);
1491 g_return_if_fail (key != NULL);
1492 g_return_if_fail (locale != NULL);
1493 g_return_if_fail (string != NULL);
1495 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1496 full_key = g_strdup_printf ("%s[%s]", key, locale);
1497 g_key_file_set_value (key_file, group_name, full_key, value);
1502 extern GSList *_g_compute_locale_variants (const gchar *locale);
1505 * g_key_file_get_locale_string:
1506 * @key_file: a #GKeyFile
1507 * @group_name: a group name
1509 * @locale: a locale or %NULL
1510 * @error: return location for a #GError, or %NULL
1512 * Returns the value associated with @key under @group_name
1513 * translated in the given @locale if available. If @locale is
1514 * %NULL then the current locale is assumed.
1516 * If @key cannot be found then %NULL is returned and @error is set to
1517 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1518 * with @key cannot be interpreted or no suitable translation can
1519 * be found then the untranslated value is returned.
1521 * Return value: a newly allocated string or %NULL if the specified
1522 * key cannot be found.
1527 g_key_file_get_locale_string (GKeyFile *key_file,
1528 const gchar *group_name,
1530 const gchar *locale,
1533 gchar *candidate_key, *translated_value;
1534 GError *key_file_error;
1536 gboolean free_languages = FALSE;
1539 g_return_val_if_fail (key_file != NULL, NULL);
1540 g_return_val_if_fail (group_name != NULL, NULL);
1541 g_return_val_if_fail (key != NULL, NULL);
1543 candidate_key = NULL;
1544 translated_value = NULL;
1545 key_file_error = NULL;
1551 list = _g_compute_locale_variants (locale);
1553 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1554 for (l = list, i = 0; l; l = l->next, i++)
1555 languages[i] = l->data;
1556 languages[i] = NULL;
1558 g_slist_free (list);
1559 free_languages = TRUE;
1563 languages = (gchar **) g_get_language_names ();
1564 free_languages = FALSE;
1567 for (i = 0; languages[i]; i++)
1569 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1571 translated_value = g_key_file_get_string (key_file,
1573 candidate_key, NULL);
1574 g_free (candidate_key);
1576 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1579 g_free (translated_value);
1580 translated_value = NULL;
1583 /* Fallback to untranslated key
1585 if (!translated_value)
1587 translated_value = g_key_file_get_string (key_file, group_name, key,
1590 if (!translated_value)
1591 g_propagate_error (error, key_file_error);
1595 g_strfreev (languages);
1597 return translated_value;
1601 * g_key_file_get_locale_string_list:
1602 * @key_file: a #GKeyFile
1603 * @group_name: a group name
1606 * @length: return location for the number of returned strings or %NULL
1607 * @error: return location for a #GError or %NULL
1609 * Returns the values associated with @key under @group_name
1610 * translated in the given @locale if available. If @locale is
1611 * %NULL then the current locale is assumed.
1613 * If @key cannot be found then %NULL is returned and @error is set to
1614 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1615 * with @key cannot be interpreted or no suitable translations
1616 * can be found then the untranslated values are returned.
1617 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1619 * Return value: a newly allocated %NULL-terminated string array
1620 * or %NULL if the key isn't found. The string array should be freed
1621 * with g_strfreev().
1626 g_key_file_get_locale_string_list (GKeyFile *key_file,
1627 const gchar *group_name,
1629 const gchar *locale,
1633 GError *key_file_error;
1634 gchar **values, *value;
1636 g_return_val_if_fail (key_file != NULL, NULL);
1637 g_return_val_if_fail (group_name != NULL, NULL);
1638 g_return_val_if_fail (key != NULL, NULL);
1640 key_file_error = NULL;
1642 value = g_key_file_get_locale_string (key_file, group_name,
1647 g_propagate_error (error, key_file_error);
1652 if (value[strlen (value) - 1] == ';')
1653 value[strlen (value) - 1] = '\0';
1655 values = g_strsplit (value, ";", 0);
1660 *length = g_strv_length (values);
1666 * g_key_file_set_locale_string_list:
1667 * @key_file: a #GKeyFile
1668 * @group_name: a group name
1671 * @list: a %NULL-terminated array of locale string values
1672 * @length: the length of @list
1674 * Associates a list of string values for @key and @locale under
1675 * @group_name. If the translation for @key cannot be found then
1681 g_key_file_set_locale_string_list (GKeyFile *key_file,
1682 const gchar *group_name,
1684 const gchar *locale,
1685 const gchar * const list[],
1688 GString *value_list;
1692 g_return_if_fail (key_file != NULL);
1693 g_return_if_fail (group_name != NULL);
1694 g_return_if_fail (key != NULL);
1695 g_return_if_fail (locale != NULL);
1696 g_return_if_fail (length != 0);
1698 value_list = g_string_sized_new (length * 128);
1699 for (i = 0; list[i] != NULL && i < length; i++)
1703 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1705 g_string_append (value_list, value);
1706 g_string_append_c (value_list, ';');
1711 full_key = g_strdup_printf ("%s[%s]", key, locale);
1712 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1714 g_string_free (value_list, TRUE);
1718 * g_key_file_get_boolean:
1719 * @key_file: a #GKeyFile
1720 * @group_name: a group name
1722 * @error: return location for a #GError
1724 * Returns the value associated with @key under @group_name as a
1727 * If @key cannot be found then the return value is undefined and
1728 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1729 * the value associated with @key cannot be interpreted as a boolean
1730 * then the return value is also undefined and @error is set to
1731 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1733 * Return value: the value associated with the key as a boolean
1737 g_key_file_get_boolean (GKeyFile *key_file,
1738 const gchar *group_name,
1742 GError *key_file_error = NULL;
1744 gboolean bool_value;
1746 g_return_val_if_fail (key_file != NULL, FALSE);
1747 g_return_val_if_fail (group_name != NULL, FALSE);
1748 g_return_val_if_fail (key != NULL, FALSE);
1750 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1754 g_propagate_error (error, key_file_error);
1758 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1764 if (g_error_matches (key_file_error,
1766 G_KEY_FILE_ERROR_INVALID_VALUE))
1768 g_set_error (error, G_KEY_FILE_ERROR,
1769 G_KEY_FILE_ERROR_INVALID_VALUE,
1770 _("Key file contains key '%s' "
1771 "which has value that cannot be interpreted."),
1773 g_error_free (key_file_error);
1776 g_propagate_error (error, key_file_error);
1783 * g_key_file_set_boolean:
1784 * @key_file: a #GKeyFile
1785 * @group_name: a group name
1787 * @value: %TRUE or %FALSE
1789 * Associates a new boolean value with @key under @group_name.
1790 * If @key cannot be found then it is created.
1795 g_key_file_set_boolean (GKeyFile *key_file,
1796 const gchar *group_name,
1802 g_return_if_fail (key_file != NULL);
1803 g_return_if_fail (group_name != NULL);
1804 g_return_if_fail (key != NULL);
1806 result = g_key_file_parse_boolean_as_value (key_file, value);
1807 g_key_file_set_value (key_file, group_name, key, result);
1812 * g_key_file_get_boolean_list:
1813 * @key_file: a #GKeyFile
1814 * @group_name: a group name
1816 * @length: the number of booleans returned
1817 * @error: return location for a #GError
1819 * Returns the values associated with @key under @group_name as
1820 * booleans. If @group_name is %NULL, the start_group is used.
1822 * If @key cannot be found then the return value is undefined and
1823 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1824 * the values associated with @key cannot be interpreted as booleans
1825 * then the return value is also undefined and @error is set to
1826 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1828 * Return value: the values associated with the key as a boolean
1833 g_key_file_get_boolean_list (GKeyFile *key_file,
1834 const gchar *group_name,
1839 GError *key_file_error;
1841 gboolean *bool_values;
1844 g_return_val_if_fail (key_file != NULL, NULL);
1845 g_return_val_if_fail (group_name != NULL, NULL);
1846 g_return_val_if_fail (key != NULL, NULL);
1848 key_file_error = NULL;
1850 values = g_key_file_get_string_list (key_file, group_name, key,
1851 &num_bools, &key_file_error);
1854 g_propagate_error (error, key_file_error);
1859 bool_values = g_new0 (gboolean, num_bools);
1861 for (i = 0; i < num_bools; i++)
1863 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1869 g_propagate_error (error, key_file_error);
1870 g_strfreev (values);
1871 g_free (bool_values);
1876 g_strfreev (values);
1879 *length = num_bools;
1885 * g_key_file_set_boolean_list:
1886 * @key_file: a #GKeyFile
1887 * @group_name: a group name
1889 * @list: an array of boolean values
1890 * @length: length of @list
1892 * Associates a list of boolean values with @key under
1893 * @group_name. If @key cannot be found then it is created.
1894 * If @group_name is %NULL, the start_group is used.
1899 g_key_file_set_boolean_list (GKeyFile *key_file,
1900 const gchar *group_name,
1905 GString *value_list;
1908 g_return_if_fail (key_file != NULL);
1909 g_return_if_fail (group_name != NULL);
1910 g_return_if_fail (key != NULL);
1911 g_return_if_fail (list != NULL);
1913 value_list = g_string_sized_new (length * 8);
1914 for (i = 0; i < length; i++)
1918 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1920 g_string_append (value_list, value);
1921 g_string_append_c (value_list, key_file->list_separator);
1926 g_key_file_set_value (key_file, group_name, key, value_list->str);
1927 g_string_free (value_list, TRUE);
1931 * g_key_file_get_integer:
1932 * @key_file: a #GKeyFile
1933 * @group_name: a group name
1935 * @error: return location for a #GError
1937 * Returns the value associated with @key under @group_name as an
1938 * integer. If @group_name is %NULL, the start_group is used.
1940 * If @key cannot be found then the return value is undefined and
1941 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1942 * the value associated with @key cannot be interpreted as an integer
1943 * then the return value is also undefined and @error is set to
1944 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1946 * Return value: the value associated with the key as an integer.
1951 g_key_file_get_integer (GKeyFile *key_file,
1952 const gchar *group_name,
1956 GError *key_file_error;
1960 g_return_val_if_fail (key_file != NULL, -1);
1961 g_return_val_if_fail (group_name != NULL, -1);
1962 g_return_val_if_fail (key != NULL, -1);
1964 key_file_error = NULL;
1966 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1970 g_propagate_error (error, key_file_error);
1974 int_value = g_key_file_parse_value_as_integer (key_file, value,
1980 if (g_error_matches (key_file_error,
1982 G_KEY_FILE_ERROR_INVALID_VALUE))
1984 g_set_error (error, G_KEY_FILE_ERROR,
1985 G_KEY_FILE_ERROR_INVALID_VALUE,
1986 _("Key file contains key '%s' in group '%s' "
1987 "which has value that cannot be interpreted."), key,
1989 g_error_free (key_file_error);
1992 g_propagate_error (error, key_file_error);
1999 * g_key_file_set_integer:
2000 * @key_file: a #GKeyFile
2001 * @group_name: a group name
2003 * @value: an integer value
2005 * Associates a new integer value with @key under @group_name.
2006 * If @key cannot be found then it is created.
2011 g_key_file_set_integer (GKeyFile *key_file,
2012 const gchar *group_name,
2018 g_return_if_fail (key_file != NULL);
2019 g_return_if_fail (group_name != NULL);
2020 g_return_if_fail (key != NULL);
2022 result = g_key_file_parse_integer_as_value (key_file, value);
2023 g_key_file_set_value (key_file, group_name, key, result);
2028 * g_key_file_get_integer_list:
2029 * @key_file: a #GKeyFile
2030 * @group_name: a group name
2032 * @length: the number of integers returned
2033 * @error: return location for a #GError
2035 * Returns the values associated with @key under @group_name as
2038 * If @key cannot be found then the return value is undefined and
2039 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2040 * the values associated with @key cannot be interpreted as integers
2041 * then the return value is also undefined and @error is set to
2042 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2044 * Return value: the values associated with the key as a integer
2049 g_key_file_get_integer_list (GKeyFile *key_file,
2050 const gchar *group_name,
2055 GError *key_file_error = NULL;
2060 g_return_val_if_fail (key_file != NULL, NULL);
2061 g_return_val_if_fail (group_name != NULL, NULL);
2062 g_return_val_if_fail (key != NULL, NULL);
2064 values = g_key_file_get_string_list (key_file, group_name, key,
2065 &num_ints, &key_file_error);
2068 g_propagate_error (error, key_file_error);
2073 int_values = g_new0 (gint, num_ints);
2075 for (i = 0; i < num_ints; i++)
2077 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2083 g_propagate_error (error, key_file_error);
2084 g_strfreev (values);
2085 g_free (int_values);
2090 g_strfreev (values);
2099 * g_key_file_set_integer_list:
2100 * @key_file: a #GKeyFile
2101 * @group_name: a group name
2103 * @list: an array of integer values
2104 * @length: number of integer values in @list
2106 * Associates a list of integer values with @key under
2107 * @group_name. If @key cannot be found then it is created.
2112 g_key_file_set_integer_list (GKeyFile *key_file,
2113 const gchar *group_name,
2121 g_return_if_fail (key_file != NULL);
2122 g_return_if_fail (group_name != NULL);
2123 g_return_if_fail (key != NULL);
2124 g_return_if_fail (list != NULL);
2126 values = g_string_sized_new (length * 16);
2127 for (i = 0; i < length; i++)
2131 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2133 g_string_append (values, value);
2134 g_string_append_c (values, ';');
2139 g_key_file_set_value (key_file, group_name, key, values->str);
2140 g_string_free (values, TRUE);
2144 g_key_file_set_key_comment (GKeyFile *key_file,
2145 const gchar *group_name,
2147 const gchar *comment,
2150 GKeyFileGroup *group;
2151 GKeyFileKeyValuePair *pair;
2152 GList *key_node, *comment_node, *tmp;
2154 group = g_key_file_lookup_group (key_file, group_name);
2157 g_set_error (error, G_KEY_FILE_ERROR,
2158 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2159 _("Key file does not have group '%s'"),
2160 group_name ? group_name : "(null)");
2165 /* First find the key the comments are supposed to be
2168 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2170 if (key_node == NULL)
2172 g_set_error (error, G_KEY_FILE_ERROR,
2173 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2174 _("Key file does not have key '%s' in group '%s'"),
2179 /* Then find all the comments already associated with the
2182 tmp = key_node->next;
2185 GKeyFileKeyValuePair *pair;
2187 pair = (GKeyFileKeyValuePair *) tmp->data;
2189 if (pair->key != NULL)
2194 g_key_file_remove_key_value_pair_node (key_file, group,
2198 if (comment == NULL)
2201 /* Now we can add our new comment
2203 pair = g_new0 (GKeyFileKeyValuePair, 1);
2206 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2208 key_node = g_list_insert (key_node, pair, 1);
2212 g_key_file_set_group_comment (GKeyFile *key_file,
2213 const gchar *group_name,
2214 const gchar *comment,
2217 GKeyFileGroup *group;
2219 group = g_key_file_lookup_group (key_file, group_name);
2222 g_set_error (error, G_KEY_FILE_ERROR,
2223 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2224 _("Key file does not have group '%s'"),
2225 group_name ? group_name : "(null)");
2230 /* First remove any existing comment
2234 g_key_file_key_value_pair_free (group->comment);
2235 group->comment = NULL;
2238 if (comment == NULL)
2241 /* Now we can add our new comment
2243 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2245 group->comment->key = NULL;
2246 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2250 g_key_file_set_top_comment (GKeyFile *key_file,
2251 const gchar *comment,
2255 GKeyFileGroup *group;
2256 GKeyFileKeyValuePair *pair;
2258 /* The last group in the list should be the top (comments only)
2261 g_assert (key_file->groups != NULL);
2262 group_node = g_list_last (key_file->groups);
2263 group = (GKeyFileGroup *) group_node->data;
2264 g_assert (group->name == NULL);
2266 /* Note all keys must be comments at the top of
2267 * the file, so we can just free it all.
2269 if (group->key_value_pairs != NULL)
2271 g_list_foreach (group->key_value_pairs,
2272 (GFunc) g_key_file_key_value_pair_free,
2274 g_list_free (group->key_value_pairs);
2275 group->key_value_pairs = NULL;
2278 if (comment == NULL)
2281 pair = g_new0 (GKeyFileKeyValuePair, 1);
2284 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2286 group->key_value_pairs =
2287 g_list_prepend (group->key_value_pairs, pair);
2291 * g_key_file_set_comment:
2292 * @key_file: a #GKeyFile
2293 * @group_name: a group name, or %NULL
2295 * @comment: a comment
2296 * @error: return location for a #GError
2298 * Places a comment above @key from @group_name.
2299 * @group_name. If @key is %NULL then @comment will
2300 * be written above @group_name. If both @key
2301 * and @group_name are NULL, then @comment will
2302 * be written above the first group in the file.
2307 g_key_file_set_comment (GKeyFile *key_file,
2308 const gchar *group_name,
2310 const gchar *comment,
2313 g_return_if_fail (key_file != NULL);
2315 if (group_name != NULL && key != NULL)
2316 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2317 else if (group_name != NULL)
2318 g_key_file_set_group_comment (key_file, group_name, comment, error);
2320 g_key_file_set_top_comment (key_file, comment, error);
2322 if (comment != NULL)
2323 key_file->approximate_size += strlen (comment);
2327 g_key_file_get_key_comment (GKeyFile *key_file,
2328 const gchar *group_name,
2332 GKeyFileGroup *group;
2333 GList *key_node, *tmp;
2337 group = g_key_file_lookup_group (key_file, group_name);
2340 g_set_error (error, G_KEY_FILE_ERROR,
2341 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2342 _("Key file does not have group '%s'"),
2343 group_name ? group_name : "(null)");
2348 /* First find the key the comments are supposed to be
2351 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2353 if (key_node == NULL)
2355 g_set_error (error, G_KEY_FILE_ERROR,
2356 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2357 _("Key file does not have key '%s' in group '%s'"),
2364 /* Then find all the comments already associated with the
2365 * key and concatentate them.
2367 tmp = key_node->next;
2370 GKeyFileKeyValuePair *pair;
2372 pair = (GKeyFileKeyValuePair *) tmp->data;
2374 if (pair->key != NULL)
2378 string = g_string_sized_new (512);
2380 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2381 g_string_append (string, comment);
2389 comment = string->str;
2390 g_string_free (string, FALSE);
2399 g_key_file_get_group_comment (GKeyFile *key_file,
2400 const gchar *group_name,
2403 GKeyFileGroup *group;
2405 group = g_key_file_lookup_group (key_file, group_name);
2408 g_set_error (error, G_KEY_FILE_ERROR,
2409 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2410 _("Key file does not have group '%s'"),
2411 group_name ? group_name : "(null)");
2417 return g_strdup (group->comment->value);
2423 g_key_file_get_top_comment (GKeyFile *key_file,
2426 GList *group_node, *tmp;
2427 GKeyFileGroup *group;
2431 /* The last group in the list should be the top (comments only)
2434 g_assert (key_file->groups != NULL);
2435 group_node = g_list_last (key_file->groups);
2436 group = (GKeyFileGroup *) group_node->data;
2437 g_assert (group->name == NULL);
2441 /* Then find all the comments already associated with the
2442 * key and concatentate them.
2444 tmp = group->key_value_pairs;
2447 GKeyFileKeyValuePair *pair;
2449 pair = (GKeyFileKeyValuePair *) tmp->data;
2451 if (pair->key != NULL)
2455 string = g_string_sized_new (512);
2457 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2458 g_string_append (string, comment);
2466 comment = string->str;
2467 g_string_free (string, FALSE);
2476 * g_key_file_get_comment:
2477 * @key_file: a #GKeyFile
2478 * @group_name: a group name, or %NULL
2480 * @error: return location for a #GError
2482 * Retreives a comment above @key from @group_name.
2483 * @group_name. If @key is %NULL then @comment will
2484 * be read from above @group_name. If both @key
2485 * and @group_name are NULL, then @comment will
2486 * be read from above the first group in the file.
2489 * Returns: a comment that should be freed with g_free()
2492 g_key_file_get_comment (GKeyFile *key_file,
2493 const gchar *group_name,
2497 g_return_val_if_fail (key_file != NULL, NULL);
2499 if (group_name != NULL && key != NULL)
2500 return g_key_file_get_key_comment (key_file, group_name, key, error);
2501 else if (group_name != NULL)
2502 return g_key_file_get_group_comment (key_file, group_name, error);
2504 return g_key_file_get_top_comment (key_file, error);
2508 * g_key_file_remove_comment:
2509 * @key_file: a #GKeyFile
2510 * @group_name: a group name, or %NULL
2512 * @error: return location for a #GError
2514 * Removes a comment above @key from @group_name.
2515 * @group_name. If @key is %NULL then @comment will
2516 * be written above @group_name. If both @key
2517 * and @group_name are NULL, then @comment will
2518 * be written above the first group in the file.
2524 g_key_file_remove_comment (GKeyFile *key_file,
2525 const gchar *group_name,
2529 g_return_if_fail (key_file != NULL);
2531 if (group_name != NULL && key != NULL)
2532 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2533 else if (group_name != NULL)
2534 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2536 g_key_file_set_top_comment (key_file, NULL, error);
2540 * g_key_file_has_group:
2541 * @key_file: a #GKeyFile
2542 * @group_name: a group name
2544 * Looks whether the key file has the group @group_name.
2546 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2551 g_key_file_has_group (GKeyFile *key_file,
2552 const gchar *group_name)
2554 g_return_val_if_fail (key_file != NULL, FALSE);
2555 g_return_val_if_fail (group_name != NULL, FALSE);
2557 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2561 * g_key_file_has_key:
2562 * @key_file: a #GKeyFile
2563 * @group_name: a group name
2565 * @error: return location for a #GError
2567 * Looks whether the key file has the key @key in the group
2570 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2576 g_key_file_has_key (GKeyFile *key_file,
2577 const gchar *group_name,
2581 GKeyFileKeyValuePair *pair;
2582 GKeyFileGroup *group;
2584 g_return_val_if_fail (key_file != NULL, FALSE);
2585 g_return_val_if_fail (group_name != NULL, FALSE);
2586 g_return_val_if_fail (key != NULL, FALSE);
2588 group = g_key_file_lookup_group (key_file, group_name);
2592 g_set_error (error, G_KEY_FILE_ERROR,
2593 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2594 _("Key file does not have group '%s'"),
2595 group_name ? group_name : "(null)");
2600 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2602 return pair != NULL;
2606 g_key_file_add_group (GKeyFile *key_file,
2607 const gchar *group_name)
2609 GKeyFileGroup *group;
2611 g_return_if_fail (key_file != NULL);
2612 g_return_if_fail (group_name != NULL);
2613 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2615 group = g_new0 (GKeyFileGroup, 1);
2616 group->name = g_strdup (group_name);
2617 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2618 key_file->groups = g_list_prepend (key_file->groups, group);
2619 key_file->approximate_size += strlen (group_name) + 3;
2620 key_file->current_group = group;
2622 if (key_file->start_group == NULL)
2623 key_file->start_group = group;
2627 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2632 g_free (pair->value);
2637 /* Be careful not to call this function on a node with data in the
2638 * lookup map without removing it from the lookup map, first.
2640 * Some current cases where this warning is not a concern are
2642 * - the node being removed is a comment node
2643 * - the entire lookup map is getting destroyed soon after
2647 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2648 GKeyFileGroup *group,
2652 GKeyFileKeyValuePair *pair;
2654 pair = (GKeyFileKeyValuePair *) pair_node->data;
2656 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2658 if (pair->key != NULL)
2659 key_file->approximate_size -= strlen (pair->key) + 1;
2661 g_assert (pair->value != NULL);
2662 key_file->approximate_size -= strlen (pair->value);
2664 g_key_file_key_value_pair_free (pair);
2666 g_list_free_1 (pair_node);
2670 g_key_file_remove_group_node (GKeyFile *key_file,
2673 GKeyFileGroup *group;
2676 group = (GKeyFileGroup *) group_node->data;
2678 /* If the current group gets deleted make the current group the last
2681 if (key_file->current_group == group)
2683 /* groups should always contain at least the top comment group,
2684 * unless g_key_file_clear has been called
2686 if (key_file->groups)
2687 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2689 key_file->current_group = NULL;
2692 /* If the start group gets deleted make the start group the first
2695 if (key_file->start_group == group)
2697 tmp = g_list_last (key_file->groups);
2700 if (tmp != group_node &&
2701 ((GKeyFileGroup *) tmp->data)->name != NULL)
2708 key_file->start_group = (GKeyFileGroup *) tmp->data;
2710 key_file->start_group = NULL;
2713 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2715 if (group->name != NULL)
2716 key_file->approximate_size -= strlen (group->name) + 3;
2718 tmp = group->key_value_pairs;
2725 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2728 g_assert (group->key_value_pairs == NULL);
2730 if (group->lookup_map)
2732 g_hash_table_destroy (group->lookup_map);
2733 group->lookup_map = NULL;
2736 g_free ((gchar *) group->name);
2738 g_list_free_1 (group_node);
2742 * g_key_file_remove_group:
2743 * @key_file: a #GKeyFile
2744 * @group_name: a group name
2745 * @error: return location for a #GError or %NULL
2747 * Removes the specified group, @group_name,
2748 * from the key file.
2753 g_key_file_remove_group (GKeyFile *key_file,
2754 const gchar *group_name,
2759 g_return_if_fail (key_file != NULL);
2760 g_return_if_fail (group_name != NULL);
2762 group_node = g_key_file_lookup_group_node (key_file, group_name);
2766 g_set_error (error, G_KEY_FILE_ERROR,
2767 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2768 _("Key file does not have group '%s'"),
2773 g_key_file_remove_group_node (key_file, group_node);
2777 g_key_file_add_key (GKeyFile *key_file,
2778 GKeyFileGroup *group,
2782 GKeyFileKeyValuePair *pair;
2784 pair = g_new0 (GKeyFileKeyValuePair, 1);
2786 pair->key = g_strdup (key);
2787 pair->value = g_strdup (value);
2789 g_hash_table_replace (group->lookup_map, pair->key, pair);
2790 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2791 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2795 * g_key_file_remove_key:
2796 * @key_file: a #GKeyFile
2797 * @group_name: a group name
2798 * @key: a key name to remove
2799 * @error: return location for a #GError or %NULL
2801 * Removes @key in @group_name from the key file.
2806 g_key_file_remove_key (GKeyFile *key_file,
2807 const gchar *group_name,
2811 GKeyFileGroup *group;
2812 GKeyFileKeyValuePair *pair;
2814 g_return_if_fail (key_file != NULL);
2815 g_return_if_fail (group_name != NULL);
2816 g_return_if_fail (key != NULL);
2820 group = g_key_file_lookup_group (key_file, group_name);
2823 g_set_error (error, G_KEY_FILE_ERROR,
2824 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2825 _("Key file does not have group '%s'"),
2826 group_name ? group_name : "(null)");
2830 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2834 g_set_error (error, G_KEY_FILE_ERROR,
2835 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2836 _("Key file does not have key '%s' in group '%s'"),
2841 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2843 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
2844 g_hash_table_remove (group->lookup_map, pair->key);
2845 g_key_file_key_value_pair_free (pair);
2849 g_key_file_lookup_group_node (GKeyFile *key_file,
2850 const gchar *group_name)
2852 GKeyFileGroup *group;
2856 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2858 group = (GKeyFileGroup *) tmp->data;
2860 if (group && group->name && strcmp (group->name, group_name) == 0)
2869 static GKeyFileGroup *
2870 g_key_file_lookup_group (GKeyFile *key_file,
2871 const gchar *group_name)
2875 group_node = g_key_file_lookup_group_node (key_file, group_name);
2877 if (group_node != NULL)
2878 return (GKeyFileGroup *) group_node->data;
2884 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2885 GKeyFileGroup *group,
2890 for (key_node = group->key_value_pairs;
2892 key_node = key_node->next)
2894 GKeyFileKeyValuePair *pair;
2896 pair = (GKeyFileKeyValuePair *) key_node->data;
2898 if (pair->key && strcmp (pair->key, key) == 0)
2905 static GKeyFileKeyValuePair *
2906 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2907 GKeyFileGroup *group,
2910 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2913 /* Lines starting with # or consisting entirely of whitespace are merely
2914 * recorded, not parsed. This function assumes all leading whitespace
2915 * has been stripped.
2918 g_key_file_line_is_comment (const gchar *line)
2920 return (*line == '#' || *line == '\0' || *line == '\n');
2923 /* A group in a key file is made up of a starting '[' followed by one
2924 * or more letters making up the group name followed by ']'.
2927 g_key_file_line_is_group (const gchar *line)
2935 p = g_utf8_next_char (p);
2940 p = g_utf8_next_char (p);
2942 /* Group name must be non-empty
2947 while (*p && *p != ']')
2948 p = g_utf8_next_char (p);
2957 g_key_file_line_is_key_value_pair (const gchar *line)
2961 p = (gchar *) g_utf8_strchr (line, -1, '=');
2966 /* Key must be non-empty
2975 g_key_file_parse_value_as_string (GKeyFile *key_file,
2980 gchar *string_value, *p, *q0, *q;
2982 string_value = g_new0 (gchar, strlen (value) + 1);
2984 p = (gchar *) value;
2985 q0 = q = string_value;
3015 g_set_error (error, G_KEY_FILE_ERROR,
3016 G_KEY_FILE_ERROR_INVALID_VALUE,
3017 _("Key file contains escape character "
3022 if (pieces && *p == key_file->list_separator)
3023 *q = key_file->list_separator;
3037 g_set_error (error, G_KEY_FILE_ERROR,
3038 G_KEY_FILE_ERROR_INVALID_VALUE,
3039 _("Key file contains invalid escape "
3040 "sequence '%s'"), sequence);
3049 if (pieces && (*p == key_file->list_separator))
3051 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3067 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3068 *pieces = g_slist_reverse (*pieces);
3071 return string_value;
3075 g_key_file_parse_string_as_value (GKeyFile *key_file,
3076 const gchar *string,
3077 gboolean escape_separator)
3079 gchar *value, *p, *q;
3081 gboolean parsing_leading_space;
3083 length = strlen (string) + 1;
3085 /* Worst case would be that every character needs to be escaped.
3086 * In other words every character turns to two characters
3088 value = g_new0 (gchar, 2 * length);
3090 p = (gchar *) string;
3092 parsing_leading_space = TRUE;
3093 while (p < (string + length - 1))
3095 gchar escaped_character[3] = { '\\', 0, 0 };
3100 if (parsing_leading_space)
3102 escaped_character[1] = 's';
3103 strcpy (q, escaped_character);
3113 if (parsing_leading_space)
3115 escaped_character[1] = 't';
3116 strcpy (q, escaped_character);
3126 escaped_character[1] = 'n';
3127 strcpy (q, escaped_character);
3131 escaped_character[1] = 'r';
3132 strcpy (q, escaped_character);
3136 escaped_character[1] = '\\';
3137 strcpy (q, escaped_character);
3139 parsing_leading_space = FALSE;
3142 if (escape_separator && *p == key_file->list_separator)
3144 escaped_character[1] = key_file->list_separator;
3145 strcpy (q, escaped_character);
3147 parsing_leading_space = TRUE;
3153 parsing_leading_space = FALSE;
3165 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3169 gchar *end_of_valid_int;
3172 int_value = strtol (value, &end_of_valid_int, 10);
3174 if (*end_of_valid_int != '\0')
3175 g_set_error (error, G_KEY_FILE_ERROR,
3176 G_KEY_FILE_ERROR_INVALID_VALUE,
3177 _("Value '%s' cannot be interpreted as a number."), value);
3183 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3187 return g_strdup_printf ("%d", value);
3191 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3197 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3199 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3203 g_set_error (error, G_KEY_FILE_ERROR,
3204 G_KEY_FILE_ERROR_INVALID_VALUE,
3205 _("Value '%s' cannot be interpreted as a boolean."), value);
3211 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3215 return g_strdup ("true");
3217 return g_strdup ("false");
3221 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3225 gchar **lines, *comment;
3228 string = g_string_sized_new (512);
3230 lines = g_strsplit (value, "\n", 0);
3232 for (i = 0; lines[i] != NULL; i++)
3234 if (lines[i][0] != '#')
3235 g_string_append_printf (string, "%s\n", lines[i]);
3237 g_string_append_printf (string, "%s\n", lines[i] + 1);
3241 comment = string->str;
3243 g_string_free (string, FALSE);
3249 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3250 const gchar *comment)
3253 gchar **lines, *value;
3256 string = g_string_sized_new (512);
3258 lines = g_strsplit (comment, "\n", 0);
3260 for (i = 0; lines[i] != NULL; i++)
3261 g_string_append_printf (string, "#%s%s", lines[i],
3262 lines[i + 1] == NULL? "" : "\n");
3265 value = string->str;
3267 g_string_free (string, FALSE);
3272 #define __G_KEY_FILE_C__
3273 #include "galiasdef.c"