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. If @group_name is
1612 * %NULL, then the start group is used.
1614 * If @key cannot be found then %NULL is returned and @error is set to
1615 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1616 * with @key cannot be interpreted or no suitable translations
1617 * can be found then the untranslated values are returned.
1618 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1620 * Return value: a newly allocated %NULL-terminated string array
1621 * or %NULL if the key isn't found. The string array should be freed
1622 * with g_strfreev().
1627 g_key_file_get_locale_string_list (GKeyFile *key_file,
1628 const gchar *group_name,
1630 const gchar *locale,
1634 GError *key_file_error;
1635 gchar **values, *value;
1637 g_return_val_if_fail (key_file != NULL, NULL);
1638 g_return_val_if_fail (group_name != NULL, NULL);
1639 g_return_val_if_fail (key != NULL, NULL);
1641 key_file_error = NULL;
1643 value = g_key_file_get_locale_string (key_file, group_name,
1648 g_propagate_error (error, key_file_error);
1653 if (value[strlen (value) - 1] == ';')
1654 value[strlen (value) - 1] = '\0';
1656 values = g_strsplit (value, ";", 0);
1661 *length = g_strv_length (values);
1667 * g_key_file_set_locale_string_list:
1668 * @key_file: a #GKeyFile
1669 * @group_name: a group name
1672 * @list: a %NULL-terminated array of locale string values
1673 * @length: the length of @list
1675 * Associates a list of string values for @key and @locale under
1676 * @group_name. If the translation for @key cannot be found then
1677 * it is created. If @group_name is %NULL, the start group is
1683 g_key_file_set_locale_string_list (GKeyFile *key_file,
1684 const gchar *group_name,
1686 const gchar *locale,
1687 const gchar * const list[],
1690 GString *value_list;
1694 g_return_if_fail (key_file != NULL);
1695 g_return_if_fail (group_name != NULL);
1696 g_return_if_fail (key != NULL);
1697 g_return_if_fail (locale != NULL);
1698 g_return_if_fail (length != 0);
1700 value_list = g_string_sized_new (length * 128);
1701 for (i = 0; list[i] != NULL && i < length; i++)
1705 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1707 g_string_append (value_list, value);
1708 g_string_append_c (value_list, ';');
1713 full_key = g_strdup_printf ("%s[%s]", key, locale);
1714 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1716 g_string_free (value_list, TRUE);
1720 * g_key_file_get_boolean:
1721 * @key_file: a #GKeyFile
1722 * @group_name: a group name
1724 * @error: return location for a #GError
1726 * Returns the value associated with @key under @group_name as a
1727 * boolean. If @group_name is %NULL, the start group is used.
1729 * If @key cannot be found then the return value is undefined and
1730 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1731 * the value associated with @key cannot be interpreted as a boolean
1732 * then the return value is also undefined and @error is set to
1733 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1735 * Return value: the value associated with the key as a boolean
1739 g_key_file_get_boolean (GKeyFile *key_file,
1740 const gchar *group_name,
1744 GError *key_file_error = NULL;
1746 gboolean bool_value;
1748 g_return_val_if_fail (key_file != NULL, FALSE);
1749 g_return_val_if_fail (group_name != NULL, FALSE);
1750 g_return_val_if_fail (key != NULL, FALSE);
1752 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1756 g_propagate_error (error, key_file_error);
1760 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1766 if (g_error_matches (key_file_error,
1768 G_KEY_FILE_ERROR_INVALID_VALUE))
1770 g_set_error (error, G_KEY_FILE_ERROR,
1771 G_KEY_FILE_ERROR_INVALID_VALUE,
1772 _("Key file contains key '%s' "
1773 "which has value that cannot be interpreted."),
1775 g_error_free (key_file_error);
1778 g_propagate_error (error, key_file_error);
1785 * g_key_file_set_boolean:
1786 * @key_file: a #GKeyFile
1787 * @group_name: a group name
1789 * @value: %TRUE or %FALSE
1791 * Associates a new boolean value with @key under @group_name.
1792 * If @key cannot be found then it is created. If @group_name
1793 * is %NULL, the start group is used.
1798 g_key_file_set_boolean (GKeyFile *key_file,
1799 const gchar *group_name,
1805 g_return_if_fail (key_file != NULL);
1806 g_return_if_fail (group_name != NULL);
1807 g_return_if_fail (key != NULL);
1809 result = g_key_file_parse_boolean_as_value (key_file, value);
1810 g_key_file_set_value (key_file, group_name, key, result);
1815 * g_key_file_get_boolean_list:
1816 * @key_file: a #GKeyFile
1817 * @group_name: a group name
1819 * @length: the number of booleans returned
1820 * @error: return location for a #GError
1822 * Returns the values associated with @key under @group_name as
1823 * booleans. If @group_name is %NULL, the start_group is used.
1825 * If @key cannot be found then the return value is undefined and
1826 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1827 * the values associated with @key cannot be interpreted as booleans
1828 * then the return value is also undefined and @error is set to
1829 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1831 * Return value: the values associated with the key as a boolean
1836 g_key_file_get_boolean_list (GKeyFile *key_file,
1837 const gchar *group_name,
1842 GError *key_file_error;
1844 gboolean *bool_values;
1847 g_return_val_if_fail (key_file != NULL, NULL);
1848 g_return_val_if_fail (group_name != NULL, NULL);
1849 g_return_val_if_fail (key != NULL, NULL);
1851 key_file_error = NULL;
1853 values = g_key_file_get_string_list (key_file, group_name, key,
1854 &num_bools, &key_file_error);
1857 g_propagate_error (error, key_file_error);
1862 bool_values = g_new0 (gboolean, num_bools);
1864 for (i = 0; i < num_bools; i++)
1866 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1872 g_propagate_error (error, key_file_error);
1873 g_strfreev (values);
1874 g_free (bool_values);
1879 g_strfreev (values);
1882 *length = num_bools;
1888 * g_key_file_set_boolean_list:
1889 * @key_file: a #GKeyFile
1890 * @group_name: a group name
1892 * @list: an array of boolean values
1893 * @length: length of @list
1895 * Associates a list of boolean values with @key under
1896 * @group_name. If @key cannot be found then it is created.
1897 * If @group_name is %NULL, the start_group is used.
1902 g_key_file_set_boolean_list (GKeyFile *key_file,
1903 const gchar *group_name,
1908 GString *value_list;
1911 g_return_if_fail (key_file != NULL);
1912 g_return_if_fail (group_name != NULL);
1913 g_return_if_fail (key != NULL);
1914 g_return_if_fail (list != NULL);
1916 value_list = g_string_sized_new (length * 8);
1917 for (i = 0; i < length; i++)
1921 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1923 g_string_append (value_list, value);
1924 g_string_append_c (value_list, key_file->list_separator);
1929 g_key_file_set_value (key_file, group_name, key, value_list->str);
1930 g_string_free (value_list, TRUE);
1934 * g_key_file_get_integer:
1935 * @key_file: a #GKeyFile
1936 * @group_name: a group name
1938 * @error: return location for a #GError
1940 * Returns the value associated with @key under @group_name as an
1941 * integer. If @group_name is %NULL, the start_group is used.
1943 * If @key cannot be found then the return value is undefined and
1944 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1945 * the value associated with @key cannot be interpreted as an integer
1946 * then the return value is also undefined and @error is set to
1947 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1949 * Return value: the value associated with the key as an integer.
1954 g_key_file_get_integer (GKeyFile *key_file,
1955 const gchar *group_name,
1959 GError *key_file_error;
1963 g_return_val_if_fail (key_file != NULL, -1);
1964 g_return_val_if_fail (group_name != NULL, -1);
1965 g_return_val_if_fail (key != NULL, -1);
1967 key_file_error = NULL;
1969 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1973 g_propagate_error (error, key_file_error);
1977 int_value = g_key_file_parse_value_as_integer (key_file, value,
1983 if (g_error_matches (key_file_error,
1985 G_KEY_FILE_ERROR_INVALID_VALUE))
1987 g_set_error (error, G_KEY_FILE_ERROR,
1988 G_KEY_FILE_ERROR_INVALID_VALUE,
1989 _("Key file contains key '%s' in group '%s' "
1990 "which has value that cannot be interpreted."), key,
1992 g_error_free (key_file_error);
1995 g_propagate_error (error, key_file_error);
2002 * g_key_file_set_integer:
2003 * @key_file: a #GKeyFile
2004 * @group_name: a group name
2006 * @value: an integer value
2008 * Associates a new integer value with @key under @group_name.
2009 * If @key cannot be found then it is created. If @group_name
2010 * is %NULL, the start group is used.
2015 g_key_file_set_integer (GKeyFile *key_file,
2016 const gchar *group_name,
2022 g_return_if_fail (key_file != NULL);
2023 g_return_if_fail (group_name != NULL);
2024 g_return_if_fail (key != NULL);
2026 result = g_key_file_parse_integer_as_value (key_file, value);
2027 g_key_file_set_value (key_file, group_name, key, result);
2032 * g_key_file_get_integer_list:
2033 * @key_file: a #GKeyFile
2034 * @group_name: a group name
2036 * @length: the number of integers returned
2037 * @error: return location for a #GError
2039 * Returns the values associated with @key under @group_name as
2040 * integers. If @group_name is %NULL, the start group is used.
2042 * If @key cannot be found then the return value is undefined and
2043 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2044 * the values associated with @key cannot be interpreted as integers
2045 * then the return value is also undefined and @error is set to
2046 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2048 * Return value: the values associated with the key as a integer
2053 g_key_file_get_integer_list (GKeyFile *key_file,
2054 const gchar *group_name,
2059 GError *key_file_error = NULL;
2064 g_return_val_if_fail (key_file != NULL, NULL);
2065 g_return_val_if_fail (group_name != NULL, NULL);
2066 g_return_val_if_fail (key != NULL, NULL);
2068 values = g_key_file_get_string_list (key_file, group_name, key,
2069 &num_ints, &key_file_error);
2072 g_propagate_error (error, key_file_error);
2077 int_values = g_new0 (gint, num_ints);
2079 for (i = 0; i < num_ints; i++)
2081 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2087 g_propagate_error (error, key_file_error);
2088 g_strfreev (values);
2089 g_free (int_values);
2094 g_strfreev (values);
2103 * g_key_file_set_integer_list:
2104 * @key_file: a #GKeyFile
2105 * @group_name: a group name
2107 * @list: an array of integer values
2108 * @length: number of integer values in @list
2110 * Associates a list of integer values with @key under
2111 * @group_name. If @key cannot be found then it is created.
2112 * If @group_name is %NULL the start group is used.
2117 g_key_file_set_integer_list (GKeyFile *key_file,
2118 const gchar *group_name,
2126 g_return_if_fail (key_file != NULL);
2127 g_return_if_fail (group_name != NULL);
2128 g_return_if_fail (key != NULL);
2129 g_return_if_fail (list != NULL);
2131 values = g_string_sized_new (length * 16);
2132 for (i = 0; i < length; i++)
2136 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2138 g_string_append (values, value);
2139 g_string_append_c (values, ';');
2144 g_key_file_set_value (key_file, group_name, key, values->str);
2145 g_string_free (values, TRUE);
2149 g_key_file_set_key_comment (GKeyFile *key_file,
2150 const gchar *group_name,
2152 const gchar *comment,
2155 GKeyFileGroup *group;
2156 GKeyFileKeyValuePair *pair;
2157 GList *key_node, *comment_node, *tmp;
2159 group = g_key_file_lookup_group (key_file, group_name);
2162 g_set_error (error, G_KEY_FILE_ERROR,
2163 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2164 _("Key file does not have group '%s'"),
2165 group_name ? group_name : "(null)");
2170 /* First find the key the comments are supposed to be
2173 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2175 if (key_node == NULL)
2177 g_set_error (error, G_KEY_FILE_ERROR,
2178 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2179 _("Key file does not have key '%s' in group '%s'"),
2184 /* Then find all the comments already associated with the
2187 tmp = key_node->next;
2190 GKeyFileKeyValuePair *pair;
2192 pair = (GKeyFileKeyValuePair *) tmp->data;
2194 if (pair->key != NULL)
2199 g_key_file_remove_key_value_pair_node (key_file, group,
2203 if (comment == NULL)
2206 /* Now we can add our new comment
2208 pair = g_new0 (GKeyFileKeyValuePair, 1);
2211 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2213 key_node = g_list_insert (key_node, pair, 1);
2217 g_key_file_set_group_comment (GKeyFile *key_file,
2218 const gchar *group_name,
2219 const gchar *comment,
2222 GKeyFileGroup *group;
2224 group = g_key_file_lookup_group (key_file, group_name);
2227 g_set_error (error, G_KEY_FILE_ERROR,
2228 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2229 _("Key file does not have group '%s'"),
2230 group_name ? group_name : "(null)");
2235 /* First remove any existing comment
2239 g_key_file_key_value_pair_free (group->comment);
2240 group->comment = NULL;
2243 if (comment == NULL)
2246 /* Now we can add our new comment
2248 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2250 group->comment->key = NULL;
2251 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2255 g_key_file_set_top_comment (GKeyFile *key_file,
2256 const gchar *comment,
2260 GKeyFileGroup *group;
2261 GKeyFileKeyValuePair *pair;
2263 /* The last group in the list should be the top (comments only)
2266 g_assert (key_file->groups != NULL);
2267 group_node = g_list_last (key_file->groups);
2268 group = (GKeyFileGroup *) group_node->data;
2269 g_assert (group->name == NULL);
2271 /* Note all keys must be comments at the top of
2272 * the file, so we can just free it all.
2274 if (group->key_value_pairs != NULL)
2276 g_list_foreach (group->key_value_pairs,
2277 (GFunc) g_key_file_key_value_pair_free,
2279 g_list_free (group->key_value_pairs);
2280 group->key_value_pairs = NULL;
2283 if (comment == NULL)
2286 pair = g_new0 (GKeyFileKeyValuePair, 1);
2289 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2291 group->key_value_pairs =
2292 g_list_prepend (group->key_value_pairs, pair);
2296 * g_key_file_set_comment:
2297 * @key_file: a #GKeyFile
2298 * @group_name: a group name, or %NULL
2300 * @comment: a comment
2301 * @error: return location for a #GError
2303 * Places a comment above @key from @group_name.
2304 * @group_name. If @key is %NULL then @comment will
2305 * be written above @group_name. If both @key
2306 * and @group_name are NULL, then @comment will
2307 * be written above the first group in the file.
2312 g_key_file_set_comment (GKeyFile *key_file,
2313 const gchar *group_name,
2315 const gchar *comment,
2318 g_return_if_fail (key_file != NULL);
2320 if (group_name != NULL && key != NULL)
2321 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2322 else if (group_name != NULL)
2323 g_key_file_set_group_comment (key_file, group_name, comment, error);
2325 g_key_file_set_top_comment (key_file, comment, error);
2327 if (comment != NULL)
2328 key_file->approximate_size += strlen (comment);
2332 g_key_file_get_key_comment (GKeyFile *key_file,
2333 const gchar *group_name,
2337 GKeyFileGroup *group;
2338 GList *key_node, *tmp;
2342 group = g_key_file_lookup_group (key_file, group_name);
2345 g_set_error (error, G_KEY_FILE_ERROR,
2346 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2347 _("Key file does not have group '%s'"),
2348 group_name ? group_name : "(null)");
2353 /* First find the key the comments are supposed to be
2356 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2358 if (key_node == NULL)
2360 g_set_error (error, G_KEY_FILE_ERROR,
2361 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2362 _("Key file does not have key '%s' in group '%s'"),
2369 /* Then find all the comments already associated with the
2370 * key and concatentate them.
2372 tmp = key_node->next;
2375 GKeyFileKeyValuePair *pair;
2377 pair = (GKeyFileKeyValuePair *) tmp->data;
2379 if (pair->key != NULL)
2383 string = g_string_sized_new (512);
2385 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2386 g_string_append (string, comment);
2394 comment = string->str;
2395 g_string_free (string, FALSE);
2404 g_key_file_get_group_comment (GKeyFile *key_file,
2405 const gchar *group_name,
2408 GKeyFileGroup *group;
2410 group = g_key_file_lookup_group (key_file, group_name);
2413 g_set_error (error, G_KEY_FILE_ERROR,
2414 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2415 _("Key file does not have group '%s'"),
2416 group_name ? group_name : "(null)");
2422 return g_strdup (group->comment->value);
2428 g_key_file_get_top_comment (GKeyFile *key_file,
2431 GList *group_node, *tmp;
2432 GKeyFileGroup *group;
2436 /* The last group in the list should be the top (comments only)
2439 g_assert (key_file->groups != NULL);
2440 group_node = g_list_last (key_file->groups);
2441 group = (GKeyFileGroup *) group_node->data;
2442 g_assert (group->name == NULL);
2446 /* Then find all the comments already associated with the
2447 * key and concatentate them.
2449 tmp = group->key_value_pairs;
2452 GKeyFileKeyValuePair *pair;
2454 pair = (GKeyFileKeyValuePair *) tmp->data;
2456 if (pair->key != NULL)
2460 string = g_string_sized_new (512);
2462 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2463 g_string_append (string, comment);
2471 comment = string->str;
2472 g_string_free (string, FALSE);
2481 * g_key_file_get_comment:
2482 * @key_file: a #GKeyFile
2483 * @group_name: a group name, or %NULL
2485 * @error: return location for a #GError
2487 * Retreives a comment above @key from @group_name.
2488 * @group_name. If @key is %NULL then @comment will
2489 * be read from above @group_name. If both @key
2490 * and @group_name are NULL, then @comment will
2491 * be read from above the first group in the file.
2494 * Returns: a comment that should be freed with g_free()
2497 g_key_file_get_comment (GKeyFile *key_file,
2498 const gchar *group_name,
2502 g_return_val_if_fail (key_file != NULL, NULL);
2504 if (group_name != NULL && key != NULL)
2505 return g_key_file_get_key_comment (key_file, group_name, key, error);
2506 else if (group_name != NULL)
2507 return g_key_file_get_group_comment (key_file, group_name, error);
2509 return g_key_file_get_top_comment (key_file, error);
2513 * g_key_file_remove_comment:
2514 * @key_file: a #GKeyFile
2515 * @group_name: a group name, or %NULL
2517 * @error: return location for a #GError
2519 * Removes a comment above @key from @group_name.
2520 * @group_name. If @key is %NULL then @comment will
2521 * be written above @group_name. If both @key
2522 * and @group_name are NULL, then @comment will
2523 * be written above the first group in the file.
2529 g_key_file_remove_comment (GKeyFile *key_file,
2530 const gchar *group_name,
2534 g_return_if_fail (key_file != NULL);
2536 if (group_name != NULL && key != NULL)
2537 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2538 else if (group_name != NULL)
2539 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2541 g_key_file_set_top_comment (key_file, NULL, error);
2545 * g_key_file_has_group:
2546 * @key_file: a #GKeyFile
2547 * @group_name: a group name
2549 * Looks whether the key file has the group @group_name.
2551 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2556 g_key_file_has_group (GKeyFile *key_file,
2557 const gchar *group_name)
2559 g_return_val_if_fail (key_file != NULL, FALSE);
2560 g_return_val_if_fail (group_name != NULL, FALSE);
2562 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2566 * g_key_file_has_key:
2567 * @key_file: a #GKeyFile
2568 * @group_name: a group name
2570 * @error: return location for a #GError
2572 * Looks whether the key file has the key @key in the group
2573 * @group_name. If @group_name is %NULL, the start group is
2576 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2582 g_key_file_has_key (GKeyFile *key_file,
2583 const gchar *group_name,
2587 GKeyFileKeyValuePair *pair;
2588 GKeyFileGroup *group;
2590 g_return_val_if_fail (key_file != NULL, FALSE);
2591 g_return_val_if_fail (group_name != NULL, FALSE);
2592 g_return_val_if_fail (key != NULL, FALSE);
2594 group = g_key_file_lookup_group (key_file, group_name);
2598 g_set_error (error, G_KEY_FILE_ERROR,
2599 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2600 _("Key file does not have group '%s'"),
2601 group_name ? group_name : "(null)");
2606 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2608 return pair != NULL;
2612 g_key_file_add_group (GKeyFile *key_file,
2613 const gchar *group_name)
2615 GKeyFileGroup *group;
2617 g_return_if_fail (key_file != NULL);
2618 g_return_if_fail (group_name != NULL);
2619 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2621 group = g_new0 (GKeyFileGroup, 1);
2622 group->name = g_strdup (group_name);
2623 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2624 key_file->groups = g_list_prepend (key_file->groups, group);
2625 key_file->approximate_size += strlen (group_name) + 3;
2626 key_file->current_group = group;
2628 if (key_file->start_group == NULL)
2629 key_file->start_group = group;
2633 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2638 g_free (pair->value);
2643 /* Be careful not to call this function on a node with data in the
2644 * lookup map without removing it from the lookup map, first.
2646 * Some current cases where this warning is not a concern are
2648 * - the node being removed is a comment node
2649 * - the entire lookup map is getting destroyed soon after
2653 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2654 GKeyFileGroup *group,
2658 GKeyFileKeyValuePair *pair;
2660 pair = (GKeyFileKeyValuePair *) pair_node->data;
2662 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2664 if (pair->key != NULL)
2665 key_file->approximate_size -= strlen (pair->key) + 1;
2667 g_assert (pair->value != NULL);
2668 key_file->approximate_size -= strlen (pair->value);
2670 g_key_file_key_value_pair_free (pair);
2672 g_list_free_1 (pair_node);
2676 g_key_file_remove_group_node (GKeyFile *key_file,
2679 GKeyFileGroup *group;
2682 group = (GKeyFileGroup *) group_node->data;
2684 /* If the current group gets deleted make the current group the last
2687 if (key_file->current_group == group)
2689 /* groups should always contain at least the top comment group,
2690 * unless g_key_file_clear has been called
2692 if (key_file->groups)
2693 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2695 key_file->current_group = NULL;
2698 /* If the start group gets deleted make the start group the first
2701 if (key_file->start_group == group)
2703 tmp = g_list_last (key_file->groups);
2706 if (tmp != group_node &&
2707 ((GKeyFileGroup *) tmp->data)->name != NULL)
2714 key_file->start_group = (GKeyFileGroup *) tmp->data;
2716 key_file->start_group = NULL;
2719 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2721 if (group->name != NULL)
2722 key_file->approximate_size -= strlen (group->name) + 3;
2724 tmp = group->key_value_pairs;
2731 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2734 g_assert (group->key_value_pairs == NULL);
2736 if (group->lookup_map)
2738 g_hash_table_destroy (group->lookup_map);
2739 group->lookup_map = NULL;
2742 g_free ((gchar *) group->name);
2744 g_list_free_1 (group_node);
2748 * g_key_file_remove_group:
2749 * @key_file: a #GKeyFile
2750 * @group_name: a group name
2751 * @error: return location for a #GError or %NULL
2753 * Removes the specified group, @group_name,
2754 * from the key file.
2759 g_key_file_remove_group (GKeyFile *key_file,
2760 const gchar *group_name,
2765 g_return_if_fail (key_file != NULL);
2766 g_return_if_fail (group_name != NULL);
2768 group_node = g_key_file_lookup_group_node (key_file, group_name);
2772 g_set_error (error, G_KEY_FILE_ERROR,
2773 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2774 _("Key file does not have group '%s'"),
2779 g_key_file_remove_group_node (key_file, group_node);
2783 g_key_file_add_key (GKeyFile *key_file,
2784 GKeyFileGroup *group,
2788 GKeyFileKeyValuePair *pair;
2790 pair = g_new0 (GKeyFileKeyValuePair, 1);
2792 pair->key = g_strdup (key);
2793 pair->value = g_strdup (value);
2795 g_hash_table_replace (group->lookup_map, pair->key, pair);
2796 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2797 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2801 * g_key_file_remove_key:
2802 * @key_file: a #GKeyFile
2803 * @group_name: a group name
2804 * @key: a key name to remove
2805 * @error: return location for a #GError or %NULL
2807 * Removes @key in @group_name from the key file.
2812 g_key_file_remove_key (GKeyFile *key_file,
2813 const gchar *group_name,
2817 GKeyFileGroup *group;
2818 GKeyFileKeyValuePair *pair;
2820 g_return_if_fail (key_file != NULL);
2821 g_return_if_fail (group_name != NULL);
2822 g_return_if_fail (key != NULL);
2826 group = g_key_file_lookup_group (key_file, group_name);
2829 g_set_error (error, G_KEY_FILE_ERROR,
2830 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2831 _("Key file does not have group '%s'"),
2832 group_name ? group_name : "(null)");
2836 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2840 g_set_error (error, G_KEY_FILE_ERROR,
2841 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2842 _("Key file does not have key '%s' in group '%s'"),
2847 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2849 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
2850 g_hash_table_remove (group->lookup_map, pair->key);
2851 g_key_file_key_value_pair_free (pair);
2855 g_key_file_lookup_group_node (GKeyFile *key_file,
2856 const gchar *group_name)
2858 GKeyFileGroup *group;
2862 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2864 group = (GKeyFileGroup *) tmp->data;
2866 if (group && group->name && strcmp (group->name, group_name) == 0)
2875 static GKeyFileGroup *
2876 g_key_file_lookup_group (GKeyFile *key_file,
2877 const gchar *group_name)
2881 group_node = g_key_file_lookup_group_node (key_file, group_name);
2883 if (group_node != NULL)
2884 return (GKeyFileGroup *) group_node->data;
2890 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2891 GKeyFileGroup *group,
2896 for (key_node = group->key_value_pairs;
2898 key_node = key_node->next)
2900 GKeyFileKeyValuePair *pair;
2902 pair = (GKeyFileKeyValuePair *) key_node->data;
2904 if (pair->key && strcmp (pair->key, key) == 0)
2911 static GKeyFileKeyValuePair *
2912 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2913 GKeyFileGroup *group,
2916 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2919 /* Lines starting with # or consisting entirely of whitespace are merely
2920 * recorded, not parsed. This function assumes all leading whitespace
2921 * has been stripped.
2924 g_key_file_line_is_comment (const gchar *line)
2926 return (*line == '#' || *line == '\0' || *line == '\n');
2929 /* A group in a key file is made up of a starting '[' followed by one
2930 * or more letters making up the group name followed by ']'.
2933 g_key_file_line_is_group (const gchar *line)
2941 p = g_utf8_next_char (p);
2946 p = g_utf8_next_char (p);
2948 /* Group name must be non-empty
2953 while (*p && *p != ']')
2954 p = g_utf8_next_char (p);
2963 g_key_file_line_is_key_value_pair (const gchar *line)
2967 p = (gchar *) g_utf8_strchr (line, -1, '=');
2972 /* Key must be non-empty
2981 g_key_file_parse_value_as_string (GKeyFile *key_file,
2986 gchar *string_value, *p, *q0, *q;
2988 string_value = g_new0 (gchar, strlen (value) + 1);
2990 p = (gchar *) value;
2991 q0 = q = string_value;
3021 g_set_error (error, G_KEY_FILE_ERROR,
3022 G_KEY_FILE_ERROR_INVALID_VALUE,
3023 _("Key file contains escape character "
3028 if (pieces && *p == key_file->list_separator)
3029 *q = key_file->list_separator;
3043 g_set_error (error, G_KEY_FILE_ERROR,
3044 G_KEY_FILE_ERROR_INVALID_VALUE,
3045 _("Key file contains invalid escape "
3046 "sequence '%s'"), sequence);
3055 if (pieces && (*p == key_file->list_separator))
3057 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3073 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3074 *pieces = g_slist_reverse (*pieces);
3077 return string_value;
3081 g_key_file_parse_string_as_value (GKeyFile *key_file,
3082 const gchar *string,
3083 gboolean escape_separator)
3085 gchar *value, *p, *q;
3087 gboolean parsing_leading_space;
3089 length = strlen (string) + 1;
3091 /* Worst case would be that every character needs to be escaped.
3092 * In other words every character turns to two characters
3094 value = g_new0 (gchar, 2 * length);
3096 p = (gchar *) string;
3098 parsing_leading_space = TRUE;
3099 while (p < (string + length - 1))
3101 gchar escaped_character[3] = { '\\', 0, 0 };
3106 if (parsing_leading_space)
3108 escaped_character[1] = 's';
3109 strcpy (q, escaped_character);
3119 if (parsing_leading_space)
3121 escaped_character[1] = 't';
3122 strcpy (q, escaped_character);
3132 escaped_character[1] = 'n';
3133 strcpy (q, escaped_character);
3137 escaped_character[1] = 'r';
3138 strcpy (q, escaped_character);
3142 escaped_character[1] = '\\';
3143 strcpy (q, escaped_character);
3145 parsing_leading_space = FALSE;
3148 if (escape_separator && *p == key_file->list_separator)
3150 escaped_character[1] = key_file->list_separator;
3151 strcpy (q, escaped_character);
3153 parsing_leading_space = TRUE;
3159 parsing_leading_space = FALSE;
3171 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3175 gchar *end_of_valid_int;
3178 int_value = strtol (value, &end_of_valid_int, 10);
3180 if (*end_of_valid_int != '\0')
3181 g_set_error (error, G_KEY_FILE_ERROR,
3182 G_KEY_FILE_ERROR_INVALID_VALUE,
3183 _("Value '%s' cannot be interpreted as a number."), value);
3189 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3193 return g_strdup_printf ("%d", value);
3197 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3203 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3205 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3209 g_set_error (error, G_KEY_FILE_ERROR,
3210 G_KEY_FILE_ERROR_INVALID_VALUE,
3211 _("Value '%s' cannot be interpreted as a boolean."), value);
3217 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3221 return g_strdup ("true");
3223 return g_strdup ("false");
3227 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3231 gchar **lines, *comment;
3234 string = g_string_sized_new (512);
3236 lines = g_strsplit (value, "\n", 0);
3238 for (i = 0; lines[i] != NULL; i++)
3240 if (lines[i][0] != '#')
3241 g_string_append_printf (string, "%s\n", lines[i]);
3243 g_string_append_printf (string, "%s\n", lines[i] + 1);
3247 comment = string->str;
3249 g_string_free (string, FALSE);
3255 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3256 const gchar *comment)
3259 gchar **lines, *value;
3262 string = g_string_sized_new (512);
3264 lines = g_strsplit (comment, "\n", 0);
3266 for (i = 0; lines[i] != NULL; i++)
3267 g_string_append_printf (string, "#%s%s", lines[i],
3268 lines[i + 1] == NULL? "" : "\n");
3271 value = string->str;
3273 g_string_free (string, FALSE);
3278 #define __G_KEY_FILE_C__
3279 #include "galiasdef.c"