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.
35 #include <sys/types.h>
44 #define S_ISREG(mode) ((mode)&_S_IFREG)
47 #endif /* G_OS_WIN23 */
52 #include "gfileutils.h"
58 #include "gmessages.h"
61 #include "gstrfuncs.h"
64 typedef struct _GKeyFileGroup GKeyFileGroup;
70 GKeyFileGroup *start_group;
71 GKeyFileGroup *current_group;
73 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
75 /* Used for sizing the output buffer during serialization
77 gsize approximate_size;
84 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
88 const gchar *name; /* NULL for above first group (which will be comments) */
90 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
92 GList *key_value_pairs;
94 /* Used in parallel with key_value_pairs for
95 * increased lookup performance
97 GHashTable *lookup_map;
100 struct _GKeyFileKeyValuePair
102 gchar *key; /* NULL for comments */
106 static gint find_file_in_data_dirs (const gchar *file,
110 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
114 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
115 const gchar *group_name);
116 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
117 const gchar *group_name);
119 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
120 GKeyFileGroup *group,
122 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
123 GKeyFileGroup *group,
126 static void g_key_file_remove_group_node (GKeyFile *key_file,
128 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
129 GKeyFileGroup *group,
132 static void g_key_file_add_key (GKeyFile *key_file,
133 GKeyFileGroup *group,
136 static void g_key_file_add_group (GKeyFile *key_file,
137 const gchar *group_name);
138 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
139 static gboolean g_key_file_line_is_comment (const gchar *line);
140 static gboolean g_key_file_line_is_group (const gchar *line);
141 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
142 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
146 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
148 gboolean escape_separator);
149 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
152 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
154 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
157 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
159 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
161 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
162 const gchar *comment);
163 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
167 static void g_key_file_parse_comment (GKeyFile *key_file,
171 static void g_key_file_parse_group (GKeyFile *key_file,
175 static gchar *key_get_locale (const gchar *key);
176 static void g_key_file_parse_data (GKeyFile *key_file,
180 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
185 g_key_file_error_quark (void)
187 static GQuark error_quark = 0;
189 if (error_quark == 0)
190 error_quark = g_quark_from_static_string ("g-key-file-error-quark");
196 g_key_file_init (GKeyFile *key_file)
198 key_file->current_group = g_new0 (GKeyFileGroup, 1);
199 key_file->groups = g_list_prepend (NULL, key_file->current_group);
200 key_file->start_group = NULL;
201 key_file->parse_buffer = g_string_sized_new (128);
202 key_file->approximate_size = 0;
203 key_file->list_separator = ';';
208 g_key_file_clear (GKeyFile *key_file)
210 GList *tmp, *group_node;
212 if (key_file->parse_buffer)
213 g_string_free (key_file->parse_buffer, TRUE);
215 tmp = key_file->groups;
220 g_key_file_remove_group_node (key_file, group_node);
223 g_assert (key_file->groups == NULL);
230 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
231 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
232 * read an existing key file.
234 * Return value: an empty #GKeyFile.
239 g_key_file_new (void)
243 key_file = g_new0 (GKeyFile, 1);
244 g_key_file_init (key_file);
250 * g_key_file_set_list_separator:
251 * @key_file: a #GKeyFile
252 * @separator: the separator
254 * Sets the character which is used to separate
255 * values in lists. Typically ';' or ',' are used
256 * as separators. The default list separator is ';'.
261 g_key_file_set_list_separator (GKeyFile *key_file,
264 key_file->list_separator = separator;
268 /* Iterates through all the directories in *dirs trying to
269 * open file. When it successfully locates and opens a file it
270 * returns the file descriptor to the open file. It also
271 * outputs the absolute path of the file in output_file and
272 * leaves the unchecked directories in *dirs.
275 find_file_in_data_dirs (const gchar *file,
280 gchar **data_dirs, *data_dir, *path;
291 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
293 gchar *candidate_file, *sub_dir;
295 candidate_file = (gchar *) file;
296 sub_dir = g_strdup ("");
297 while (candidate_file != NULL && fd < 0)
301 path = g_build_filename (data_dir, sub_dir,
302 candidate_file, NULL);
304 fd = g_open (path, O_RDONLY, 0);
312 candidate_file = strchr (candidate_file, '-');
314 if (candidate_file == NULL)
320 sub_dir = g_strndup (file, candidate_file - file - 1);
322 for (p = sub_dir; *p != '\0'; p++)
325 *p = G_DIR_SEPARATOR;
336 g_set_error (error, G_KEY_FILE_ERROR,
337 G_KEY_FILE_ERROR_NOT_FOUND,
338 _("Valid key file could not be "
339 "found in data dirs"));
342 if (output_file != NULL && fd > 0)
343 *output_file = g_strdup (path);
349 g_key_file_load_from_fd (GKeyFile *key_file,
354 GError *key_file_error = NULL;
356 struct stat stat_buf;
357 gchar read_buf[4096];
359 if (fstat (fd, &stat_buf) < 0)
361 g_set_error (error, G_FILE_ERROR,
362 g_file_error_from_errno (errno),
363 "%s", g_strerror (errno));
367 if (!S_ISREG (stat_buf.st_mode))
369 g_set_error (error, G_KEY_FILE_ERROR,
370 G_KEY_FILE_ERROR_PARSE,
371 _("Not a regular file"));
375 if (stat_buf.st_size == 0)
377 g_set_error (error, G_KEY_FILE_ERROR,
378 G_KEY_FILE_ERROR_PARSE,
383 if (key_file->approximate_size > 0)
385 g_key_file_clear (key_file);
386 g_key_file_init (key_file);
388 key_file->flags = flags;
393 bytes_read = read (fd, read_buf, 4096);
395 if (bytes_read == 0) /* End of File */
400 if (errno == EINTR || errno == EAGAIN)
403 g_set_error (error, G_FILE_ERROR,
404 g_file_error_from_errno (errno),
405 "%s", g_strerror (errno));
409 g_key_file_parse_data (key_file,
410 read_buf, bytes_read,
413 while (!key_file_error);
417 g_propagate_error (error, key_file_error);
421 g_key_file_flush_parse_buffer (key_file, &key_file_error);
425 g_propagate_error (error, key_file_error);
433 * g_key_file_load_from_file:
434 * @key_file: an empty #GKeyFile struct
435 * @file: the path of a filename to load, in the GLib file name encoding
436 * @flags: flags from #GKeyFileFlags
437 * @error: return location for a #GError, or %NULL
439 * Loads a key file into an empty #GKeyFile structure.
440 * If the file could not be loaded then %error is set to
441 * either a #GFileError or #GKeyFileError.
443 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
447 g_key_file_load_from_file (GKeyFile *key_file,
452 GError *key_file_error = NULL;
455 g_return_val_if_fail (key_file != NULL, FALSE);
456 g_return_val_if_fail (file != NULL, FALSE);
458 fd = g_open (file, O_RDONLY, 0);
462 g_set_error (error, G_FILE_ERROR,
463 g_file_error_from_errno (errno),
464 "%s", g_strerror (errno));
468 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
473 g_propagate_error (error, key_file_error);
481 * g_key_file_load_from_data:
482 * @key_file: an empty #GKeyFile struct
483 * @data: key file loaded in memory.
484 * @length: the length of @data in bytes
485 * @flags: flags from #GKeyFileFlags
486 * @error: return location for a #GError, or %NULL
488 * Loads a key file from memory into an empty #GKeyFile structure.
489 * If the object cannot be created then %error is set to a #GKeyFileError.
491 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
495 g_key_file_load_from_data (GKeyFile *key_file,
501 GError *key_file_error = NULL;
503 g_return_val_if_fail (key_file != NULL, FALSE);
504 g_return_val_if_fail (data != NULL, FALSE);
505 g_return_val_if_fail (length != 0, FALSE);
507 if (key_file->approximate_size > 0)
509 g_key_file_clear (key_file);
510 g_key_file_init (key_file);
512 key_file->flags = flags;
514 g_key_file_parse_data (key_file, data, length, &key_file_error);
518 g_propagate_error (error, key_file_error);
522 g_key_file_flush_parse_buffer (key_file, &key_file_error);
526 g_propagate_error (error, key_file_error);
534 * g_key_file_load_from_data_dirs:
535 * @key_file: an empty #GKeyFile struct
536 * @file: a relative path to a filename to open and parse
537 * @full_path: return location for a string containing the full path
538 * of the file, or %NULL
539 * @flags: flags from #GKeyFileFlags
540 * @error: return location for a #GError, or %NULL
542 * This function looks for a key file named @file in the paths
543 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
544 * loads the file into @key_file and returns the file's full path in
545 * @full_path. If the file could not be loaded then an %error is
546 * set to either a #GFileError or #GKeyFileError.
548 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
552 g_key_file_load_from_data_dirs (GKeyFile *key_file,
558 GError *key_file_error = NULL;
559 gchar **all_data_dirs, **data_dirs;
560 const gchar * user_data_dir;
561 const gchar * const * system_data_dirs;
567 g_return_val_if_fail (key_file != NULL, FALSE);
568 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
570 user_data_dir = g_get_user_data_dir ();
571 system_data_dirs = g_get_system_data_dirs ();
572 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
575 all_data_dirs[i++] = g_strdup (user_data_dir);
578 while (system_data_dirs[j] != NULL)
579 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
582 data_dirs = all_data_dirs;
583 while (*data_dirs != NULL && !found_file)
585 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
591 g_propagate_error (error, key_file_error);
595 found_file = g_key_file_load_from_fd (key_file, fd, flags,
601 g_propagate_error (error, key_file_error);
602 g_free (output_path);
607 *full_path = output_path;
610 g_strfreev (all_data_dirs);
616 * @key_file: a #GKeyFile
623 g_key_file_free (GKeyFile *key_file)
625 g_return_if_fail (key_file != NULL);
627 g_key_file_clear (key_file);
631 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
632 * true for locales that match those in g_get_language_names().
635 g_key_file_locale_is_interesting (GKeyFile *key_file,
638 const gchar * const * current_locales;
641 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
644 current_locales = g_get_language_names ();
646 for (i = 0; current_locales[i] != NULL; i++)
648 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
656 g_key_file_parse_line (GKeyFile *key_file,
661 GError *parse_error = NULL;
664 g_return_if_fail (key_file != NULL);
665 g_return_if_fail (line != NULL);
667 line_start = (gchar *) line;
668 while (g_ascii_isspace (*line_start))
671 if (g_key_file_line_is_comment (line_start))
672 g_key_file_parse_comment (key_file, line, length, &parse_error);
673 else if (g_key_file_line_is_group (line_start))
674 g_key_file_parse_group (key_file, line_start,
675 length - (line_start - line),
677 else if (g_key_file_line_is_key_value_pair (line_start))
678 g_key_file_parse_key_value_pair (key_file, line_start,
679 length - (line_start - line),
683 g_set_error (error, G_KEY_FILE_ERROR,
684 G_KEY_FILE_ERROR_PARSE,
685 _("Key file contains line '%s' which is not "
686 "a key-value pair, group, or comment"), line);
691 g_propagate_error (error, parse_error);
695 g_key_file_parse_comment (GKeyFile *key_file,
700 GKeyFileKeyValuePair *pair;
702 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
705 g_assert (key_file->current_group != NULL);
707 pair = g_new0 (GKeyFileKeyValuePair, 1);
710 pair->value = g_strndup (line, length);
712 key_file->current_group->key_value_pairs =
713 g_list_prepend (key_file->current_group->key_value_pairs, pair);
717 g_key_file_parse_group (GKeyFile *key_file,
723 const gchar *group_name_start, *group_name_end;
725 /* advance past opening '['
727 group_name_start = line + 1;
728 group_name_end = line + length - 1;
730 while (*group_name_end != ']')
733 group_name = g_strndup (group_name_start,
734 group_name_end - group_name_start);
736 g_key_file_add_group (key_file, group_name);
741 g_key_file_parse_key_value_pair (GKeyFile *key_file,
746 gchar *key, *value, *key_end, *value_start, *locale;
747 gsize key_len, value_len;
749 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
751 g_set_error (error, G_KEY_FILE_ERROR,
752 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
753 _("Key file does not start with a group"));
757 key_end = value_start = strchr (line, '=');
759 g_assert (key_end != NULL);
764 /* Pull the key name from the line (chomping trailing whitespace)
766 while (g_ascii_isspace (*key_end))
769 key_len = key_end - line + 2;
771 g_assert (key_len <= length);
773 key = g_strndup (line, key_len - 1);
775 /* Pull the value from the line (chugging leading whitespace)
777 while (g_ascii_isspace (*value_start))
780 value_len = line + length - value_start + 1;
782 value = g_strndup (value_start, value_len);
784 g_assert (key_file->start_group != NULL);
786 if (key_file->current_group
787 && key_file->current_group->name
788 && strcmp (key_file->start_group->name,
789 key_file->current_group->name) == 0
790 && strcmp (key, "Encoding") == 0)
792 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
794 g_set_error (error, G_KEY_FILE_ERROR,
795 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
796 _("Key file contains unsupported encoding '%s'"), value);
804 /* Is this key a translation? If so, is it one that we care about?
806 locale = key_get_locale (key);
808 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
809 g_key_file_add_key (key_file, key_file->current_group, key, value);
817 key_get_locale (const gchar *key)
821 locale = g_strrstr (key, "[");
823 if (locale && strlen (locale) <= 2)
827 locale = g_strndup (locale + 1, strlen (locale) - 2);
833 g_key_file_parse_data (GKeyFile *key_file,
841 g_return_if_fail (key_file != NULL);
842 g_return_if_fail (data != NULL);
846 for (i = 0; i < length; i++)
850 /* When a newline is encountered flush the parse buffer so that the
851 * line can be parsed. Note that completely blank lines won't show
852 * up in the parse buffer, so they get parsed directly.
854 if (key_file->parse_buffer->len > 0)
855 g_key_file_flush_parse_buffer (key_file, &parse_error);
857 g_key_file_parse_comment (key_file, "", 1, &parse_error);
861 g_propagate_error (error, parse_error);
866 g_string_append_c (key_file->parse_buffer, data[i]);
869 key_file->approximate_size += length;
873 g_key_file_flush_parse_buffer (GKeyFile *key_file,
876 GError *file_error = NULL;
878 g_return_if_fail (key_file != NULL);
882 if (key_file->parse_buffer->len > 0)
884 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
885 key_file->parse_buffer->len,
887 g_string_erase (key_file->parse_buffer, 0, -1);
891 g_propagate_error (error, file_error);
898 * g_key_file_to_data:
899 * @key_file: a #GKeyFile
900 * @length: return location for the length of the
901 * returned string, or %NULL
902 * @error: return location for a #GError, or %NULL
904 * This function outputs @key_file as a string.
906 * Return value: a newly allocated string holding
907 * the contents of the #GKeyFile
912 g_key_file_to_data (GKeyFile *key_file,
916 GString *data_string;
918 GList *group_node, *key_file_node;
920 g_return_val_if_fail (key_file != NULL, NULL);
922 data_string = g_string_sized_new (2 * key_file->approximate_size);
924 for (group_node = g_list_last (key_file->groups);
926 group_node = group_node->prev)
928 GKeyFileGroup *group;
930 group = (GKeyFileGroup *) group_node->data;
932 if (group->comment != NULL)
933 g_string_append_printf (data_string, "%s\n", group->comment->value);
934 if (group->name != NULL)
935 g_string_append_printf (data_string, "[%s]\n", group->name);
937 for (key_file_node = g_list_last (group->key_value_pairs);
938 key_file_node != NULL;
939 key_file_node = key_file_node->prev)
941 GKeyFileKeyValuePair *pair;
943 pair = (GKeyFileKeyValuePair *) key_file_node->data;
945 if (pair->key != NULL)
946 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
948 g_string_append_printf (data_string, "%s\n", pair->value);
953 *length = data_string->len;
955 data = data_string->str;
957 g_string_free (data_string, FALSE);
963 * g_key_file_get_keys:
964 * @key_file: a #GKeyFile
965 * @group_name: a group name
966 * @length: return location for the number of keys returned, or %NULL
967 * @error: return location for a #GError, or %NULL
969 * Returns all keys for the group name @group_name. The array of
970 * returned keys will be %NULL-terminated, so @length may
971 * optionally be %NULL. In the event that the @group_name cannot
972 * be found, %NULL is returned and @error is set to
973 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
975 * Return value: a newly-allocated %NULL-terminated array of
976 * strings. Use g_strfreev() to free it.
981 g_key_file_get_keys (GKeyFile *key_file,
982 const gchar *group_name,
986 GKeyFileGroup *group;
991 g_return_val_if_fail (key_file != NULL, NULL);
992 g_return_val_if_fail (group_name != NULL, NULL);
994 group = g_key_file_lookup_group (key_file, group_name);
998 g_set_error (error, G_KEY_FILE_ERROR,
999 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1000 _("Key file does not have group '%s'"),
1001 group_name ? group_name : "(null)");
1005 num_keys = g_list_length (group->key_value_pairs);
1007 keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1009 tmp = group->key_value_pairs;
1010 for (i = 0; i < num_keys; i++)
1012 GKeyFileKeyValuePair *pair;
1014 pair = (GKeyFileKeyValuePair *) tmp->data;
1015 keys[i] = g_strdup (pair->key);
1028 * g_key_file_get_start_group:
1029 * @key_file: a #GKeyFile
1031 * Returns the name of the start group of the file.
1033 * Return value: The start group of the key file.
1038 g_key_file_get_start_group (GKeyFile *key_file)
1040 g_return_val_if_fail (key_file != NULL, NULL);
1042 if (key_file->start_group)
1043 return g_strdup (key_file->start_group->name);
1049 * g_key_file_get_groups:
1050 * @key_file: a #GKeyFile
1051 * @length: return location for the number of returned groups, or %NULL
1053 * Returns all groups in the key file loaded with @key_file. The
1054 * array of returned groups will be %NULL-terminated, so @length may
1055 * optionally be %NULL.
1057 * Return value: a newly-allocated %NULL-terminated array of strings.
1058 * Use g_strfreev() to free it.
1062 g_key_file_get_groups (GKeyFile *key_file,
1067 gsize i, num_groups;
1069 g_return_val_if_fail (key_file != NULL, NULL);
1071 num_groups = g_list_length (key_file->groups);
1073 g_assert (num_groups > 0);
1075 /* Only need num_groups instead of num_groups + 1
1076 * because the first group of the file (last in the
1077 * list) is always the comment group at the top,
1080 groups = (gchar **) g_new0 (gchar **, num_groups);
1082 group_node = g_list_last (key_file->groups);
1084 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1087 for (group_node = group_node->prev;
1089 group_node = group_node->prev)
1091 GKeyFileGroup *group;
1093 group = (GKeyFileGroup *) group_node->data;
1095 g_assert (group->name != NULL);
1097 groups[i++] = g_strdup (group->name);
1108 * g_key_file_get_value:
1109 * @key_file: a #GKeyFile
1110 * @group_name: a group name
1112 * @error: return location for a #GError, or %NULL
1114 * Returns the value associated with @key under @group_name.
1116 * In the event the key cannot be found, %NULL is returned and
1117 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1118 * event that the @group_name cannot be found, %NULL is returned
1119 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1121 * Return value: a string or %NULL if the specified key cannot be
1127 g_key_file_get_value (GKeyFile *key_file,
1128 const gchar *group_name,
1132 GKeyFileGroup *group;
1133 GKeyFileKeyValuePair *pair;
1134 gchar *value = NULL;
1136 g_return_val_if_fail (key_file != NULL, NULL);
1137 g_return_val_if_fail (group_name != NULL, NULL);
1138 g_return_val_if_fail (key != NULL, NULL);
1140 group = g_key_file_lookup_group (key_file, group_name);
1144 g_set_error (error, G_KEY_FILE_ERROR,
1145 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1146 _("Key file does not have group '%s'"),
1147 group_name ? group_name : "(null)");
1151 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1154 value = g_strdup (pair->value);
1156 g_set_error (error, G_KEY_FILE_ERROR,
1157 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1158 _("Key file does not have key '%s'"), key);
1164 * g_key_file_set_value:
1165 * @key_file: a #GKeyFile
1166 * @group_name: a group name
1170 * Associates a new value with @key under @group_name. If @key
1171 * cannot be found then it is created. If @group_name cannot be
1172 * found then it is created.
1177 g_key_file_set_value (GKeyFile *key_file,
1178 const gchar *group_name,
1182 GKeyFileGroup *group;
1183 GKeyFileKeyValuePair *pair;
1185 g_return_if_fail (key_file != NULL);
1186 g_return_if_fail (group_name != NULL);
1187 g_return_if_fail (key != NULL);
1188 g_return_if_fail (value != NULL);
1190 group = g_key_file_lookup_group (key_file, group_name);
1194 g_key_file_add_group (key_file, group_name);
1195 group = (GKeyFileGroup *) key_file->groups->data;
1197 g_key_file_add_key (key_file, group, key, value);
1201 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1204 g_key_file_add_key (key_file, group, key, value);
1207 g_free (pair->value);
1208 pair->value = g_strdup (value);
1214 * g_key_file_get_string:
1215 * @key_file: a #GKeyFile
1216 * @group_name: a group name
1218 * @error: return location for a #GError, or %NULL
1220 * Returns the value associated with @key under @group_name.
1222 * In the event the key cannot be found, %NULL is returned and
1223 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1224 * event that the @group_name cannot be found, %NULL is returned
1225 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1227 * Return value: a string or %NULL if the specified key cannot be
1233 g_key_file_get_string (GKeyFile *key_file,
1234 const gchar *group_name,
1238 gchar *value, *string_value;
1239 GError *key_file_error;
1241 g_return_val_if_fail (key_file != NULL, NULL);
1242 g_return_val_if_fail (group_name != NULL, NULL);
1243 g_return_val_if_fail (key != NULL, NULL);
1245 key_file_error = NULL;
1247 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1251 g_propagate_error (error, key_file_error);
1255 if (!g_utf8_validate (value, -1, NULL))
1257 g_set_error (error, G_KEY_FILE_ERROR,
1258 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1259 _("Key file contains key '%s' with value '%s' "
1260 "which is not UTF-8"), key, value);
1265 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1271 if (g_error_matches (key_file_error,
1273 G_KEY_FILE_ERROR_INVALID_VALUE))
1275 g_set_error (error, G_KEY_FILE_ERROR,
1276 G_KEY_FILE_ERROR_INVALID_VALUE,
1277 _("Key file contains key '%s' "
1278 "which has value that cannot be interpreted."),
1280 g_error_free (key_file_error);
1283 g_propagate_error (error, key_file_error);
1286 return string_value;
1290 * g_key_file_set_string:
1291 * @key_file: a #GKeyFile
1292 * @group_name: a group name
1296 * Associates a new string value with @key under @group_name. If
1297 * @key cannot be found then it is created. If @group_name
1298 * cannot be found then it is created.
1303 g_key_file_set_string (GKeyFile *key_file,
1304 const gchar *group_name,
1306 const gchar *string)
1310 g_return_if_fail (key_file != NULL);
1311 g_return_if_fail (group_name != NULL);
1312 g_return_if_fail (key != NULL);
1313 g_return_if_fail (string != NULL);
1315 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1316 g_key_file_set_value (key_file, group_name, key, value);
1321 * g_key_file_get_string_list:
1322 * @key_file: a #GKeyFile
1323 * @group_name: a group name
1325 * @length: return location for the number of returned strings, or %NULL
1326 * @error: return location for a #GError, or %NULL
1328 * Returns the values associated with @key under @group_name.
1330 * In the event the key cannot be found, %NULL is returned and
1331 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1332 * event that the @group_name cannot be found, %NULL is returned
1333 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1335 * Return value: a %NULL-terminated string array or %NULL if the specified
1336 * key cannot be found. The array should be freed with g_strfreev().
1341 g_key_file_get_string_list (GKeyFile *key_file,
1342 const gchar *group_name,
1347 GError *key_file_error = NULL;
1348 gchar *value, *string_value, **values;
1350 GSList *p, *pieces = NULL;
1352 g_return_val_if_fail (key_file != NULL, NULL);
1353 g_return_val_if_fail (group_name != NULL, NULL);
1354 g_return_val_if_fail (key != NULL, NULL);
1356 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1360 g_propagate_error (error, key_file_error);
1364 if (!g_utf8_validate (value, -1, NULL))
1366 g_set_error (error, G_KEY_FILE_ERROR,
1367 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1368 _("Key file contains key '%s' with value '%s' "
1369 "which is not UTF-8"), key, value);
1374 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1376 g_free (string_value);
1380 if (g_error_matches (key_file_error,
1382 G_KEY_FILE_ERROR_INVALID_VALUE))
1384 g_set_error (error, G_KEY_FILE_ERROR,
1385 G_KEY_FILE_ERROR_INVALID_VALUE,
1386 _("Key file contains key '%s' "
1387 "which has value that cannot be interpreted."),
1389 g_error_free (key_file_error);
1392 g_propagate_error (error, key_file_error);
1395 len = g_slist_length (pieces);
1396 values = g_new0 (gchar *, len + 1);
1397 for (p = pieces, i = 0; p; p = p->next)
1398 values[i++] = p->data;
1401 g_slist_free (pieces);
1410 * g_key_file_set_string_list:
1411 * @key_file: a #GKeyFile
1412 * @group_name: a group name
1414 * @list: an array of locale string values
1415 * @length: number of locale string values in @list
1417 * Associates a list of string values for @key under @group_name.
1418 * If @key cannot be found then it is created. If @group_name
1419 * cannot be found then it is created.
1424 g_key_file_set_string_list (GKeyFile *key_file,
1425 const gchar *group_name,
1427 const gchar * const list[],
1430 GString *value_list;
1433 g_return_if_fail (key_file != NULL);
1434 g_return_if_fail (group_name != NULL);
1435 g_return_if_fail (key != NULL);
1436 g_return_if_fail (list != NULL);
1438 value_list = g_string_sized_new (length * 128);
1439 for (i = 0; list[i] != NULL && i < length; i++)
1443 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1444 g_string_append (value_list, value);
1445 g_string_append_c (value_list, key_file->list_separator);
1450 g_key_file_set_value (key_file, group_name, key, value_list->str);
1451 g_string_free (value_list, TRUE);
1455 * g_key_file_set_locale_string:
1456 * @key_file: a #GKeyFile
1457 * @group_name: a group name
1462 * Associates a string value for @key and @locale under
1463 * @group_name. If the translation for @key cannot be found
1464 * then it is created.
1469 g_key_file_set_locale_string (GKeyFile *key_file,
1470 const gchar *group_name,
1472 const gchar *locale,
1473 const gchar *string)
1475 gchar *full_key, *value;
1477 g_return_if_fail (key_file != NULL);
1478 g_return_if_fail (group_name != NULL);
1479 g_return_if_fail (key != NULL);
1480 g_return_if_fail (locale != NULL);
1481 g_return_if_fail (string != NULL);
1483 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1484 full_key = g_strdup_printf ("%s[%s]", key, locale);
1485 g_key_file_set_value (key_file, group_name, full_key, value);
1490 extern GSList *_g_compute_locale_variants (const gchar *locale);
1493 * g_key_file_get_locale_string:
1494 * @key_file: a #GKeyFile
1495 * @group_name: a group name
1497 * @locale: a locale or %NULL
1498 * @error: return location for a #GError, or %NULL
1500 * Returns the value associated with @key under @group_name
1501 * translated in the given @locale if available. If @locale is
1502 * %NULL then the current locale is assumed.
1504 * If @key cannot be found then %NULL is returned and @error is set to
1505 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1506 * with @key cannot be interpreted or no suitable translation can
1507 * be found then the untranslated value is returned.
1509 * Return value: a string or %NULL if the specified key cannot be
1514 g_key_file_get_locale_string (GKeyFile *key_file,
1515 const gchar *group_name,
1517 const gchar *locale,
1520 gchar *candidate_key, *translated_value;
1521 GError *key_file_error;
1523 gboolean free_languages = FALSE;
1526 g_return_val_if_fail (key_file != NULL, NULL);
1527 g_return_val_if_fail (group_name != NULL, NULL);
1528 g_return_val_if_fail (key != NULL, NULL);
1530 candidate_key = NULL;
1531 translated_value = NULL;
1532 key_file_error = NULL;
1538 list = _g_compute_locale_variants (locale);
1540 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1541 for (l = list, i = 0; l; l = l->next, i++)
1542 languages[i] = l->data;
1543 languages[i] = NULL;
1545 g_slist_free (list);
1546 free_languages = TRUE;
1550 languages = (gchar **) g_get_language_names ();
1551 free_languages = FALSE;
1554 for (i = 0; languages[i]; i++)
1556 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1558 translated_value = g_key_file_get_string (key_file,
1560 candidate_key, NULL);
1561 g_free (candidate_key);
1563 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1566 g_free (translated_value);
1567 translated_value = NULL;
1570 /* Fallback to untranslated key
1572 if (!translated_value)
1574 translated_value = g_key_file_get_string (key_file, group_name, key,
1577 if (!translated_value)
1578 g_propagate_error (error, key_file_error);
1582 g_strfreev (languages);
1584 return translated_value;
1588 * g_key_file_get_locale_string_list:
1589 * @key_file: a #GKeyFile
1590 * @group_name: a group name
1593 * @length: return location for the number of returned strings or %NULL
1594 * @error: return location for a #GError or %NULL
1596 * Returns the values associated with @key under @group_name
1597 * translated in the given @locale if available. If @locale is
1598 * %NULL then the current locale is assumed. If @group_name is
1599 * %NULL, then the start group is used.
1601 * If @key cannot be found then %NULL is returned and @error is set to
1602 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1603 * with @key cannot be interpreted or no suitable translations
1604 * can be found then the untranslated values are returned.
1605 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1607 * Return value: a newly allocated %NULL-terminated string array
1608 * or %NULL if the key isn't found. The string array should be freed
1609 * with g_strfreev().
1614 g_key_file_get_locale_string_list (GKeyFile *key_file,
1615 const gchar *group_name,
1617 const gchar *locale,
1621 GError *key_file_error;
1622 gchar **values, *value;
1624 g_return_val_if_fail (key_file != NULL, NULL);
1625 g_return_val_if_fail (group_name != NULL, NULL);
1626 g_return_val_if_fail (key != NULL, NULL);
1628 key_file_error = NULL;
1630 value = g_key_file_get_locale_string (key_file, group_name,
1635 g_propagate_error (error, key_file_error);
1640 if (value[strlen (value) - 1] == ';')
1641 value[strlen (value) - 1] = '\0';
1643 values = g_strsplit (value, ";", 0);
1648 *length = g_strv_length (values);
1654 * g_key_file_set_locale_string_list:
1655 * @key_file: a #GKeyFile
1656 * @group_name: a group name
1659 * @list: a %NULL-terminated array of locale string values
1660 * @length: the length of @list
1662 * Associates a list of string values for @key and @locale under
1663 * @group_name. If the translation for @key cannot be found then
1664 * it is created. If @group_name is %NULL, the start group is
1670 g_key_file_set_locale_string_list (GKeyFile *key_file,
1671 const gchar *group_name,
1673 const gchar *locale,
1674 const gchar * const list[],
1677 GString *value_list;
1681 g_return_if_fail (key_file != NULL);
1682 g_return_if_fail (group_name != NULL);
1683 g_return_if_fail (key != NULL);
1684 g_return_if_fail (locale != NULL);
1685 g_return_if_fail (length != 0);
1687 value_list = g_string_sized_new (length * 128);
1688 for (i = 0; list[i] != NULL && i < length; i++)
1692 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1694 g_string_append (value_list, value);
1695 g_string_append_c (value_list, ';');
1700 full_key = g_strdup_printf ("%s[%s]", key, locale);
1701 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1703 g_string_free (value_list, TRUE);
1707 * g_key_file_get_boolean:
1708 * @key_file: a #GKeyFile
1709 * @group_name: a group name
1711 * @error: return location for a #GError
1713 * Returns the value associated with @key under @group_name as a
1714 * boolean. If @group_name is %NULL, the start group is used.
1716 * If @key cannot be found then the return value is undefined and
1717 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1718 * the value associated with @key cannot be interpreted as a boolean
1719 * then the return value is also undefined and @error is set to
1720 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1722 * Return value: the value associated with the key as a boolean
1726 g_key_file_get_boolean (GKeyFile *key_file,
1727 const gchar *group_name,
1731 GError *key_file_error = NULL;
1733 gboolean bool_value;
1735 g_return_val_if_fail (key_file != NULL, FALSE);
1736 g_return_val_if_fail (group_name != NULL, FALSE);
1737 g_return_val_if_fail (key != NULL, FALSE);
1739 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1743 g_propagate_error (error, key_file_error);
1747 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1753 if (g_error_matches (key_file_error,
1755 G_KEY_FILE_ERROR_INVALID_VALUE))
1757 g_set_error (error, G_KEY_FILE_ERROR,
1758 G_KEY_FILE_ERROR_INVALID_VALUE,
1759 _("Key file contains key '%s' "
1760 "which has value that cannot be interpreted."),
1762 g_error_free (key_file_error);
1765 g_propagate_error (error, key_file_error);
1772 * g_key_file_set_boolean:
1773 * @key_file: a #GKeyFile
1774 * @group_name: a group name
1776 * @value: %TRUE or %FALSE
1778 * Associates a new boolean value with @key under @group_name.
1779 * If @key cannot be found then it is created. If @group_name
1780 * is %NULL, the start group is used.
1785 g_key_file_set_boolean (GKeyFile *key_file,
1786 const gchar *group_name,
1792 g_return_if_fail (key_file != NULL);
1793 g_return_if_fail (group_name != NULL);
1794 g_return_if_fail (key != NULL);
1796 result = g_key_file_parse_boolean_as_value (key_file, value);
1797 g_key_file_set_value (key_file, group_name, key, result);
1802 * g_key_file_get_boolean_list:
1803 * @key_file: a #GKeyFile
1804 * @group_name: a group name
1806 * @length: the number of booleans returned
1807 * @error: return location for a #GError
1809 * Returns the values associated with @key under @group_name as
1810 * booleans. If @group_name is %NULL, the start_group is used.
1812 * If @key cannot be found then the return value is undefined and
1813 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1814 * the values associated with @key cannot be interpreted as booleans
1815 * then the return value is also undefined and @error is set to
1816 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1818 * Return value: the values associated with the key as a boolean
1823 g_key_file_get_boolean_list (GKeyFile *key_file,
1824 const gchar *group_name,
1829 GError *key_file_error;
1831 gboolean *bool_values;
1834 g_return_val_if_fail (key_file != NULL, NULL);
1835 g_return_val_if_fail (group_name != NULL, NULL);
1836 g_return_val_if_fail (key != NULL, NULL);
1838 key_file_error = NULL;
1840 values = g_key_file_get_string_list (key_file, group_name, key,
1841 &num_bools, &key_file_error);
1844 g_propagate_error (error, key_file_error);
1849 bool_values = g_new0 (gboolean, num_bools);
1851 for (i = 0; i < num_bools; i++)
1853 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1859 g_propagate_error (error, key_file_error);
1860 g_strfreev (values);
1861 g_free (bool_values);
1866 g_strfreev (values);
1869 *length = num_bools;
1875 * g_key_file_set_boolean_list:
1876 * @key_file: a #GKeyFile
1877 * @group_name: a group name
1879 * @list: an array of boolean values
1880 * @length: length of @list
1882 * Associates a list of boolean values with @key under
1883 * @group_name. If @key cannot be found then it is created.
1884 * If @group_name is %NULL, the start_group is used.
1889 g_key_file_set_boolean_list (GKeyFile *key_file,
1890 const gchar *group_name,
1895 GString *value_list;
1898 g_return_if_fail (key_file != NULL);
1899 g_return_if_fail (group_name != NULL);
1900 g_return_if_fail (key != NULL);
1901 g_return_if_fail (list != NULL);
1903 value_list = g_string_sized_new (length * 8);
1904 for (i = 0; i < length; i++)
1908 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1910 g_string_append (value_list, value);
1911 g_string_append_c (value_list, key_file->list_separator);
1916 g_key_file_set_value (key_file, group_name, key, value_list->str);
1917 g_string_free (value_list, TRUE);
1921 * g_key_file_get_integer:
1922 * @key_file: a #GKeyFile
1923 * @group_name: a group name
1925 * @error: return location for a #GError
1927 * Returns the value associated with @key under @group_name as an
1928 * integer. If @group_name is %NULL, the start_group is used.
1930 * If @key cannot be found then the return value is undefined and
1931 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1932 * the value associated with @key cannot be interpreted as an integer
1933 * then the return value is also undefined and @error is set to
1934 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1936 * Return value: the value associated with the key as an integer.
1941 g_key_file_get_integer (GKeyFile *key_file,
1942 const gchar *group_name,
1946 GError *key_file_error;
1950 g_return_val_if_fail (key_file != NULL, -1);
1951 g_return_val_if_fail (group_name != NULL, -1);
1952 g_return_val_if_fail (key != NULL, -1);
1954 key_file_error = NULL;
1956 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1960 g_propagate_error (error, key_file_error);
1964 int_value = g_key_file_parse_value_as_integer (key_file, value,
1970 if (g_error_matches (key_file_error,
1972 G_KEY_FILE_ERROR_INVALID_VALUE))
1974 g_set_error (error, G_KEY_FILE_ERROR,
1975 G_KEY_FILE_ERROR_INVALID_VALUE,
1976 _("Key file contains key '%s' in group '%s' "
1977 "which has value that cannot be interpreted."), key,
1979 g_error_free (key_file_error);
1982 g_propagate_error (error, key_file_error);
1989 * g_key_file_set_integer:
1990 * @key_file: a #GKeyFile
1991 * @group_name: a group name
1993 * @value: an integer value
1995 * Associates a new integer value with @key under @group_name.
1996 * If @key cannot be found then it is created. If @group_name
1997 * is %NULL, the start group is used.
2002 g_key_file_set_integer (GKeyFile *key_file,
2003 const gchar *group_name,
2009 g_return_if_fail (key_file != NULL);
2010 g_return_if_fail (group_name != NULL);
2011 g_return_if_fail (key != NULL);
2013 result = g_key_file_parse_integer_as_value (key_file, value);
2014 g_key_file_set_value (key_file, group_name, key, result);
2019 * g_key_file_get_integer_list:
2020 * @key_file: a #GKeyFile
2021 * @group_name: a group name
2023 * @length: the number of integers returned
2024 * @error: return location for a #GError
2026 * Returns the values associated with @key under @group_name as
2027 * integers. If @group_name is %NULL, the start group is used.
2029 * If @key cannot be found then the return value is undefined and
2030 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2031 * the values associated with @key cannot be interpreted as integers
2032 * then the return value is also undefined and @error is set to
2033 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2035 * Return value: the values associated with the key as a integer
2040 g_key_file_get_integer_list (GKeyFile *key_file,
2041 const gchar *group_name,
2046 GError *key_file_error = NULL;
2051 g_return_val_if_fail (key_file != NULL, NULL);
2052 g_return_val_if_fail (group_name != NULL, NULL);
2053 g_return_val_if_fail (key != NULL, NULL);
2055 values = g_key_file_get_string_list (key_file, group_name, key,
2056 &num_ints, &key_file_error);
2059 g_propagate_error (error, key_file_error);
2064 int_values = g_new0 (gint, num_ints);
2066 for (i = 0; i < num_ints; i++)
2068 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2074 g_propagate_error (error, key_file_error);
2075 g_strfreev (values);
2076 g_free (int_values);
2081 g_strfreev (values);
2090 * g_key_file_set_integer_list:
2091 * @key_file: a #GKeyFile
2092 * @group_name: a group name
2094 * @list: an array of integer values
2095 * @length: number of integer values in @list
2097 * Associates a list of integer values with @key under
2098 * @group_name. If @key cannot be found then it is created.
2099 * If @group_name is %NULL the start group is used.
2104 g_key_file_set_integer_list (GKeyFile *key_file,
2105 const gchar *group_name,
2113 g_return_if_fail (key_file != NULL);
2114 g_return_if_fail (group_name != NULL);
2115 g_return_if_fail (key != NULL);
2116 g_return_if_fail (list != NULL);
2118 values = g_string_sized_new (length * 16);
2119 for (i = 0; i < length; i++)
2123 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2125 g_string_append (values, value);
2126 g_string_append_c (values, ';');
2131 g_key_file_set_value (key_file, group_name, key, values->str);
2132 g_string_free (values, TRUE);
2136 g_key_file_set_key_comment (GKeyFile *key_file,
2137 const gchar *group_name,
2139 const gchar *comment,
2142 GKeyFileGroup *group;
2143 GKeyFileKeyValuePair *pair;
2144 GList *key_node, *comment_node, *tmp;
2146 group = g_key_file_lookup_group (key_file, group_name);
2149 g_set_error (error, G_KEY_FILE_ERROR,
2150 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2151 _("Key file does not have group '%s'"),
2152 group_name ? group_name : "(null)");
2157 /* First find the key the comments are supposed to be
2160 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2162 if (key_node == NULL)
2164 g_set_error (error, G_KEY_FILE_ERROR,
2165 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2166 _("Key file does not have key '%s' in group '%s'"),
2171 /* Then find all the comments already associated with the
2174 tmp = key_node->next;
2177 GKeyFileKeyValuePair *pair;
2179 pair = (GKeyFileKeyValuePair *) tmp->data;
2181 if (pair->key != NULL)
2186 g_key_file_remove_key_value_pair_node (key_file, group,
2190 if (comment == NULL)
2193 /* Now we can add our new comment
2195 pair = g_new0 (GKeyFileKeyValuePair, 1);
2198 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2200 key_node = g_list_insert (key_node, pair, 1);
2204 g_key_file_set_group_comment (GKeyFile *key_file,
2205 const gchar *group_name,
2206 const gchar *comment,
2209 GKeyFileGroup *group;
2211 group = g_key_file_lookup_group (key_file, group_name);
2214 g_set_error (error, G_KEY_FILE_ERROR,
2215 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2216 _("Key file does not have group '%s'"),
2217 group_name ? group_name : "(null)");
2222 /* First remove any existing comment
2226 g_key_file_key_value_pair_free (group->comment);
2227 group->comment = NULL;
2230 if (comment == NULL)
2233 /* Now we can add our new comment
2235 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2237 group->comment->key = NULL;
2238 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2242 g_key_file_set_top_comment (GKeyFile *key_file,
2243 const gchar *comment,
2247 GKeyFileGroup *group;
2248 GKeyFileKeyValuePair *pair;
2250 /* The last group in the list should be the top (comments only)
2253 g_assert (key_file->groups != NULL);
2254 group_node = g_list_last (key_file->groups);
2255 group = (GKeyFileGroup *) group_node->data;
2256 g_assert (group->name == NULL);
2258 /* Note all keys must be comments at the top of
2259 * the file, so we can just free it all.
2261 if (group->key_value_pairs != NULL)
2263 g_list_foreach (group->key_value_pairs,
2264 (GFunc) g_key_file_key_value_pair_free,
2266 g_list_free (group->key_value_pairs);
2267 group->key_value_pairs = NULL;
2270 if (comment == NULL)
2273 pair = g_new0 (GKeyFileKeyValuePair, 1);
2276 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2278 group->key_value_pairs =
2279 g_list_prepend (group->key_value_pairs, pair);
2283 * g_key_file_set_comment:
2284 * @key_file: a #GKeyFile
2285 * @group_name: a group name, or %NULL
2287 * @comment: a comment
2288 * @error: return location for a #GError
2290 * Places a comment above @key from @group_name.
2291 * @group_name. If @key is %NULL then @comment will
2292 * be written above @group_name. If both @key
2293 * and @group_name are NULL, then @comment will
2294 * be written above the first group in the file.
2299 g_key_file_set_comment (GKeyFile *key_file,
2300 const gchar *group_name,
2302 const gchar *comment,
2305 g_return_if_fail (key_file != NULL);
2307 if (group_name != NULL && key != NULL)
2308 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2309 else if (group_name != NULL)
2310 g_key_file_set_group_comment (key_file, group_name, comment, error);
2312 g_key_file_set_top_comment (key_file, comment, error);
2314 if (comment != NULL)
2315 key_file->approximate_size += strlen (comment);
2319 g_key_file_get_key_comment (GKeyFile *key_file,
2320 const gchar *group_name,
2324 GKeyFileGroup *group;
2325 GList *key_node, *tmp;
2329 group = g_key_file_lookup_group (key_file, group_name);
2332 g_set_error (error, G_KEY_FILE_ERROR,
2333 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2334 _("Key file does not have group '%s'"),
2335 group_name ? group_name : "(null)");
2340 /* First find the key the comments are supposed to be
2343 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2345 if (key_node == NULL)
2347 g_set_error (error, G_KEY_FILE_ERROR,
2348 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2349 _("Key file does not have key '%s' in group '%s'"),
2356 /* Then find all the comments already associated with the
2357 * key and concatentate them.
2359 tmp = key_node->next;
2362 GKeyFileKeyValuePair *pair;
2364 pair = (GKeyFileKeyValuePair *) tmp->data;
2366 if (pair->key != NULL)
2370 string = g_string_sized_new (512);
2372 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2373 g_string_append (string, comment);
2381 comment = string->str;
2382 g_string_free (string, FALSE);
2391 g_key_file_get_group_comment (GKeyFile *key_file,
2392 const gchar *group_name,
2395 GKeyFileGroup *group;
2397 group = g_key_file_lookup_group (key_file, group_name);
2400 g_set_error (error, G_KEY_FILE_ERROR,
2401 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2402 _("Key file does not have group '%s'"),
2403 group_name ? group_name : "(null)");
2409 return g_strdup (group->comment->value);
2415 g_key_file_get_top_comment (GKeyFile *key_file,
2418 GList *group_node, *tmp;
2419 GKeyFileGroup *group;
2423 /* The last group in the list should be the top (comments only)
2426 g_assert (key_file->groups != NULL);
2427 group_node = g_list_last (key_file->groups);
2428 group = (GKeyFileGroup *) group_node->data;
2429 g_assert (group->name == NULL);
2433 /* Then find all the comments already associated with the
2434 * key and concatentate them.
2436 tmp = group->key_value_pairs;
2439 GKeyFileKeyValuePair *pair;
2441 pair = (GKeyFileKeyValuePair *) tmp->data;
2443 if (pair->key != NULL)
2447 string = g_string_sized_new (512);
2449 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2450 g_string_append (string, comment);
2458 comment = string->str;
2459 g_string_free (string, FALSE);
2468 * g_key_file_get_comment:
2469 * @key_file: a #GKeyFile
2470 * @group_name: a group name, or %NULL
2472 * @error: return location for a #GError
2474 * Retreives a comment above @key from @group_name.
2475 * @group_name. If @key is %NULL then @comment will
2476 * be read from above @group_name. If both @key
2477 * and @group_name are NULL, then @comment will
2478 * be read from above the first group in the file.
2481 * Returns: a comment that should be freed with g_free()
2484 g_key_file_get_comment (GKeyFile *key_file,
2485 const gchar *group_name,
2489 g_return_val_if_fail (key_file != NULL, NULL);
2491 if (group_name != NULL && key != NULL)
2492 return g_key_file_get_key_comment (key_file, group_name, key, error);
2493 else if (group_name != NULL)
2494 return g_key_file_get_group_comment (key_file, group_name, error);
2496 return g_key_file_get_top_comment (key_file, error);
2500 * g_key_file_remove_comment:
2501 * @key_file: a #GKeyFile
2502 * @group_name: a group name, or %NULL
2504 * @error: return location for a #GError
2506 * Removes a comment above @key from @group_name.
2507 * @group_name. If @key is %NULL then @comment will
2508 * be written above @group_name. If both @key
2509 * and @group_name are NULL, then @comment will
2510 * be written above the first group in the file.
2516 g_key_file_remove_comment (GKeyFile *key_file,
2517 const gchar *group_name,
2521 g_return_if_fail (key_file != NULL);
2523 if (group_name != NULL && key != NULL)
2524 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2525 else if (group_name != NULL)
2526 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2528 g_key_file_set_top_comment (key_file, NULL, error);
2532 * g_key_file_has_group:
2533 * @key_file: a #GKeyFile
2534 * @group_name: a group name
2536 * Looks whether the key file has the group @group_name.
2538 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2543 g_key_file_has_group (GKeyFile *key_file,
2544 const gchar *group_name)
2546 g_return_val_if_fail (key_file != NULL, FALSE);
2547 g_return_val_if_fail (group_name != NULL, FALSE);
2549 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2553 * g_key_file_has_key:
2554 * @key_file: a #GKeyFile
2555 * @group_name: a group name
2557 * @error: return location for a #GError
2559 * Looks whether the key file has the key @key in the group
2560 * @group_name. If @group_name is %NULL, the start group is
2563 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2569 g_key_file_has_key (GKeyFile *key_file,
2570 const gchar *group_name,
2574 GKeyFileKeyValuePair *pair;
2575 GKeyFileGroup *group;
2577 g_return_val_if_fail (key_file != NULL, FALSE);
2578 g_return_val_if_fail (group_name != NULL, FALSE);
2579 g_return_val_if_fail (key != NULL, FALSE);
2581 group = g_key_file_lookup_group (key_file, group_name);
2585 g_set_error (error, G_KEY_FILE_ERROR,
2586 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2587 _("Key file does not have group '%s'"),
2588 group_name ? group_name : "(null)");
2593 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2595 return pair != NULL;
2599 g_key_file_add_group (GKeyFile *key_file,
2600 const gchar *group_name)
2602 GKeyFileGroup *group;
2604 g_return_if_fail (key_file != NULL);
2605 g_return_if_fail (group_name != NULL);
2606 g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2608 group = g_new0 (GKeyFileGroup, 1);
2609 group->name = g_strdup (group_name);
2610 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2611 key_file->groups = g_list_prepend (key_file->groups, group);
2612 key_file->approximate_size += strlen (group_name) + 3;
2613 key_file->current_group = group;
2615 if (key_file->start_group == NULL)
2616 key_file->start_group = group;
2620 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2625 g_free (pair->value);
2630 /* Be careful not to call this function on a node with data in the
2631 * lookup map without removing it from the lookup map, first.
2633 * Some current cases where this warning is not a concern are
2635 * - the node being removed is a comment node
2636 * - the entire lookup map is getting destroyed soon after
2640 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2641 GKeyFileGroup *group,
2645 GKeyFileKeyValuePair *pair;
2647 pair = (GKeyFileKeyValuePair *) pair_node->data;
2649 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2651 if (pair->key != NULL)
2652 key_file->approximate_size -= strlen (pair->key) + 1;
2654 g_assert (pair->value != NULL);
2655 key_file->approximate_size -= strlen (pair->value);
2657 g_key_file_key_value_pair_free (pair);
2659 g_list_free_1 (pair_node);
2663 g_key_file_remove_group_node (GKeyFile *key_file,
2666 GKeyFileGroup *group;
2669 group = (GKeyFileGroup *) group_node->data;
2671 /* If the current group gets deleted make the current group the last
2674 if (key_file->current_group == group)
2676 /* groups should always contain at least the top comment group,
2677 * unless g_key_file_clear has been called
2679 if (key_file->groups)
2680 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2682 key_file->current_group = NULL;
2685 /* If the start group gets deleted make the start group the first
2688 if (key_file->start_group == group)
2690 tmp = g_list_last (key_file->groups);
2693 if (tmp != group_node &&
2694 ((GKeyFileGroup *) tmp->data)->name != NULL)
2701 key_file->start_group = (GKeyFileGroup *) tmp->data;
2703 key_file->start_group = NULL;
2706 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2708 if (group->name != NULL)
2709 key_file->approximate_size -= strlen (group->name) + 3;
2711 tmp = group->key_value_pairs;
2718 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2721 g_assert (group->key_value_pairs == NULL);
2723 if (group->lookup_map)
2725 g_hash_table_destroy (group->lookup_map);
2726 group->lookup_map = NULL;
2729 g_free ((gchar *) group->name);
2731 g_list_free_1 (group_node);
2735 * g_key_file_remove_group:
2736 * @key_file: a #GKeyFile
2737 * @group_name: a group name
2738 * @error: return location for a #GError or %NULL
2740 * Removes the specified group, @group_name,
2741 * from the key file.
2746 g_key_file_remove_group (GKeyFile *key_file,
2747 const gchar *group_name,
2752 g_return_if_fail (key_file != NULL);
2753 g_return_if_fail (group_name != NULL);
2755 group_node = g_key_file_lookup_group_node (key_file, group_name);
2758 g_set_error (error, G_KEY_FILE_ERROR,
2759 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2760 _("Key file does not have group '%s'"),
2763 g_key_file_remove_group_node (key_file, group_node);
2767 g_key_file_add_key (GKeyFile *key_file,
2768 GKeyFileGroup *group,
2772 GKeyFileKeyValuePair *pair;
2774 pair = g_new0 (GKeyFileKeyValuePair, 1);
2776 pair->key = g_strdup (key);
2777 pair->value = g_strdup (value);
2779 g_hash_table_replace (group->lookup_map, pair->key, pair);
2780 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2781 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2785 * g_key_file_remove_key:
2786 * @key_file: a #GKeyFile
2787 * @group_name: a group name
2788 * @key: a key name to remove
2789 * @error: return location for a #GError or %NULL
2791 * Removes @key in @group_name from the key file.
2796 g_key_file_remove_key (GKeyFile *key_file,
2797 const gchar *group_name,
2801 GKeyFileGroup *group;
2802 GKeyFileKeyValuePair *pair;
2804 g_return_if_fail (key_file != NULL);
2805 g_return_if_fail (group_name != NULL);
2806 g_return_if_fail (key != NULL);
2810 group = g_key_file_lookup_group (key_file, group_name);
2813 g_set_error (error, G_KEY_FILE_ERROR,
2814 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2815 _("Key file does not have group '%s'"),
2816 group_name ? group_name : "(null)");
2820 group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2821 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2825 g_set_error (error, G_KEY_FILE_ERROR,
2826 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2827 _("Key file does not have key '%s' in group '%s'"),
2832 g_hash_table_remove (group->lookup_map, pair->key);
2834 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2835 g_key_file_key_value_pair_free (pair);
2839 g_key_file_lookup_group_node (GKeyFile *key_file,
2840 const gchar *group_name)
2842 GKeyFileGroup *group;
2846 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2848 group = (GKeyFileGroup *) tmp->data;
2850 if (group && group->name && strcmp (group->name, group_name) == 0)
2859 static GKeyFileGroup *
2860 g_key_file_lookup_group (GKeyFile *key_file,
2861 const gchar *group_name)
2865 group_node = g_key_file_lookup_group_node (key_file, group_name);
2867 if (group_node != NULL)
2868 return (GKeyFileGroup *) group_node->data;
2874 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2875 GKeyFileGroup *group,
2880 for (key_node = group->key_value_pairs;
2882 key_node = key_node->next)
2884 GKeyFileKeyValuePair *pair;
2886 pair = (GKeyFileKeyValuePair *) key_node->data;
2888 if (pair->key && strcmp (pair->key, key) == 0)
2895 static GKeyFileKeyValuePair *
2896 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2897 GKeyFileGroup *group,
2900 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2903 /* Lines starting with # or consisting entirely of whitespace are merely
2904 * recorded, not parsed. This function assumes all leading whitespace
2905 * has been stripped.
2908 g_key_file_line_is_comment (const gchar *line)
2910 return (*line == '#' || *line == '\0' || *line == '\n');
2913 /* A group in a key file is made up of a starting '[' followed by one
2914 * or more letters making up the group name followed by ']'.
2917 g_key_file_line_is_group (const gchar *line)
2925 p = g_utf8_next_char (p);
2930 p = g_utf8_next_char (p);
2932 /* Group name must be non-empty
2937 while (*p && *p != ']')
2938 p = g_utf8_next_char (p);
2947 g_key_file_line_is_key_value_pair (const gchar *line)
2951 p = (gchar *) g_utf8_strchr (line, -1, '=');
2956 /* Key must be non-empty
2965 g_key_file_parse_value_as_string (GKeyFile *key_file,
2970 GError *parse_error = NULL;
2971 gchar *string_value, *p, *q0, *q;
2973 string_value = g_new0 (gchar, strlen (value) + 1);
2975 p = (gchar *) value;
2976 q0 = q = string_value;
3006 if (pieces && *p == key_file->list_separator)
3007 *q = key_file->list_separator;
3013 if (parse_error == NULL)
3021 g_set_error (error, G_KEY_FILE_ERROR,
3022 G_KEY_FILE_ERROR_INVALID_VALUE,
3023 _("Key file contains invalid escape "
3024 "sequence '%s'"), sequence);
3033 if (pieces && (*p == key_file->list_separator))
3035 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3044 if (p > value && p[-1] == '\\' && error == NULL)
3045 g_set_error (error, G_KEY_FILE_ERROR,
3046 G_KEY_FILE_ERROR_INVALID_VALUE,
3047 _("Key file contains escape character at end of line"));
3053 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3054 *pieces = g_slist_reverse (*pieces);
3057 return string_value;
3061 g_key_file_parse_string_as_value (GKeyFile *key_file,
3062 const gchar *string,
3063 gboolean escape_separator)
3065 gchar *value, *p, *q;
3067 gboolean parsing_leading_space;
3069 length = strlen (string) + 1;
3071 /* Worst case would be that every character needs to be escaped.
3072 * In other words every character turns to two characters
3074 value = g_new0 (gchar, 2 * length);
3076 p = (gchar *) string;
3078 parsing_leading_space = TRUE;
3079 while (p < (string + length - 1))
3081 gchar escaped_character[3] = { '\\', 0, 0 };
3086 if (parsing_leading_space)
3088 escaped_character[1] = 's';
3089 strcpy (q, escaped_character);
3099 if (parsing_leading_space)
3101 escaped_character[1] = 't';
3102 strcpy (q, escaped_character);
3112 escaped_character[1] = 'n';
3113 strcpy (q, escaped_character);
3117 escaped_character[1] = 'r';
3118 strcpy (q, escaped_character);
3122 escaped_character[1] = '\\';
3123 strcpy (q, escaped_character);
3125 parsing_leading_space = FALSE;
3128 if (escape_separator && *p == key_file->list_separator)
3130 escaped_character[1] = key_file->list_separator;
3131 strcpy (q, escaped_character);
3133 parsing_leading_space = TRUE;
3139 parsing_leading_space = FALSE;
3151 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3155 gchar *end_of_valid_int;
3158 int_value = strtol (value, &end_of_valid_int, 0);
3160 if (*end_of_valid_int != '\0')
3161 g_set_error (error, G_KEY_FILE_ERROR,
3162 G_KEY_FILE_ERROR_INVALID_VALUE,
3163 _("Value '%s' cannot be interpreted as a number."), value);
3169 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3173 return g_strdup_printf ("%d", value);
3177 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3183 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3185 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3189 g_set_error (error, G_KEY_FILE_ERROR,
3190 G_KEY_FILE_ERROR_INVALID_VALUE,
3191 _("Value '%s' cannot be interpreted as a boolean."), value);
3197 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3201 return g_strdup ("true");
3203 return g_strdup ("false");
3207 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3211 gchar **lines, *comment;
3214 string = g_string_sized_new (512);
3216 lines = g_strsplit (value, "\n", 0);
3218 for (i = 0; lines[i] != NULL; i++)
3220 if (lines[i][0] != '#')
3221 g_string_append_printf (string, "%s\n", lines[i]);
3223 g_string_append_printf (string, "%s\n", lines[i] + 1);
3227 comment = string->str;
3229 g_string_free (string, FALSE);
3235 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3236 const gchar *comment)
3239 gchar **lines, *value;
3242 string = g_string_sized_new (512);
3244 lines = g_strsplit (comment, "\n", 0);
3246 for (i = 0; lines[i] != NULL; i++)
3247 g_string_append_printf (string, "#%s%s", lines[i],
3248 lines[i + 1] == NULL? "" : "\n");
3251 value = string->str;
3253 g_string_free (string, FALSE);