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 return g_quark_from_static_string ("g-key-file-error-quark");
192 g_key_file_init (GKeyFile *key_file)
194 key_file->current_group = g_new0 (GKeyFileGroup, 1);
195 key_file->groups = g_list_prepend (NULL, key_file->current_group);
196 key_file->start_group = NULL;
197 key_file->parse_buffer = g_string_sized_new (128);
198 key_file->approximate_size = 0;
199 key_file->list_separator = ';';
204 g_key_file_clear (GKeyFile *key_file)
206 GList *tmp, *group_node;
208 if (key_file->parse_buffer)
209 g_string_free (key_file->parse_buffer, TRUE);
211 tmp = key_file->groups;
216 g_key_file_remove_group_node (key_file, group_node);
219 g_assert (key_file->groups == NULL);
226 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
227 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
228 * read an existing key file.
230 * Return value: an empty #GKeyFile.
235 g_key_file_new (void)
239 key_file = g_new0 (GKeyFile, 1);
240 g_key_file_init (key_file);
246 * g_key_file_set_list_separator:
247 * @key_file: a #GKeyFile
248 * @separator: the separator
250 * Sets the character which is used to separate
251 * values in lists. Typically ';' or ',' are used
252 * as separators. The default list separator is ';'.
257 g_key_file_set_list_separator (GKeyFile *key_file,
260 key_file->list_separator = separator;
264 /* Iterates through all the directories in *dirs trying to
265 * open file. When it successfully locates and opens a file it
266 * returns the file descriptor to the open file. It also
267 * outputs the absolute path of the file in output_file and
268 * leaves the unchecked directories in *dirs.
271 find_file_in_data_dirs (const gchar *file,
276 gchar **data_dirs, *data_dir, *path;
287 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
289 gchar *candidate_file, *sub_dir;
291 candidate_file = (gchar *) file;
292 sub_dir = g_strdup ("");
293 while (candidate_file != NULL && fd < 0)
297 path = g_build_filename (data_dir, sub_dir,
298 candidate_file, NULL);
300 fd = g_open (path, O_RDONLY, 0);
308 candidate_file = strchr (candidate_file, '-');
310 if (candidate_file == NULL)
316 sub_dir = g_strndup (file, candidate_file - file - 1);
318 for (p = sub_dir; *p != '\0'; p++)
321 *p = G_DIR_SEPARATOR;
332 g_set_error (error, G_KEY_FILE_ERROR,
333 G_KEY_FILE_ERROR_NOT_FOUND,
334 _("Valid key file could not be "
335 "found in data dirs"));
338 if (output_file != NULL && fd > 0)
339 *output_file = g_strdup (path);
347 g_key_file_load_from_fd (GKeyFile *key_file,
352 GError *key_file_error = NULL;
354 struct stat stat_buf;
355 gchar read_buf[4096];
357 if (fstat (fd, &stat_buf) < 0)
359 g_set_error (error, G_FILE_ERROR,
360 g_file_error_from_errno (errno),
361 "%s", g_strerror (errno));
365 if (!S_ISREG (stat_buf.st_mode))
367 g_set_error (error, G_KEY_FILE_ERROR,
368 G_KEY_FILE_ERROR_PARSE,
369 _("Not a regular file"));
373 if (stat_buf.st_size == 0)
375 g_set_error (error, G_KEY_FILE_ERROR,
376 G_KEY_FILE_ERROR_PARSE,
381 if (key_file->approximate_size > 0)
383 g_key_file_clear (key_file);
384 g_key_file_init (key_file);
386 key_file->flags = flags;
391 bytes_read = read (fd, read_buf, 4096);
393 if (bytes_read == 0) /* End of File */
398 if (errno == EINTR || errno == EAGAIN)
401 g_set_error (error, G_FILE_ERROR,
402 g_file_error_from_errno (errno),
403 "%s", g_strerror (errno));
407 g_key_file_parse_data (key_file,
408 read_buf, bytes_read,
411 while (!key_file_error);
415 g_propagate_error (error, key_file_error);
419 g_key_file_flush_parse_buffer (key_file, &key_file_error);
423 g_propagate_error (error, key_file_error);
431 * g_key_file_load_from_file:
432 * @key_file: an empty #GKeyFile struct
433 * @file: the path of a filename to load, in the GLib file name encoding
434 * @flags: flags from #GKeyFileFlags
435 * @error: return location for a #GError, or %NULL
437 * Loads a key file into an empty #GKeyFile structure.
438 * If the file could not be loaded then %error is set to
439 * either a #GFileError or #GKeyFileError.
441 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
445 g_key_file_load_from_file (GKeyFile *key_file,
450 GError *key_file_error = NULL;
453 g_return_val_if_fail (key_file != NULL, FALSE);
454 g_return_val_if_fail (file != NULL, FALSE);
456 fd = g_open (file, O_RDONLY, 0);
460 g_set_error (error, G_FILE_ERROR,
461 g_file_error_from_errno (errno),
462 "%s", g_strerror (errno));
466 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
471 g_propagate_error (error, key_file_error);
479 * g_key_file_load_from_data:
480 * @key_file: an empty #GKeyFile struct
481 * @data: key file loaded in memory.
482 * @length: the length of @data in bytes
483 * @flags: flags from #GKeyFileFlags
484 * @error: return location for a #GError, or %NULL
486 * Loads a key file from memory into an empty #GKeyFile structure. If
487 * the object cannot be created then %error is set to a
490 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
494 g_key_file_load_from_data (GKeyFile *key_file,
500 GError *key_file_error = NULL;
502 g_return_val_if_fail (key_file != NULL, FALSE);
503 g_return_val_if_fail (data != NULL, FALSE);
504 g_return_val_if_fail (length != 0, FALSE);
506 if (length == (gsize)-1)
507 length = strlen (data);
509 if (key_file->approximate_size > 0)
511 g_key_file_clear (key_file);
512 g_key_file_init (key_file);
514 key_file->flags = flags;
516 g_key_file_parse_data (key_file, data, length, &key_file_error);
520 g_propagate_error (error, key_file_error);
524 g_key_file_flush_parse_buffer (key_file, &key_file_error);
528 g_propagate_error (error, key_file_error);
536 * g_key_file_load_from_data_dirs:
537 * @key_file: an empty #GKeyFile struct
538 * @file: a relative path to a filename to open and parse
539 * @full_path: return location for a string containing the full path
540 * of the file, or %NULL
541 * @flags: flags from #GKeyFileFlags
542 * @error: return location for a #GError, or %NULL
544 * This function looks for a key file named @file in the paths
545 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
546 * loads the file into @key_file and returns the file's full path in
547 * @full_path. If the file could not be loaded then an %error is
548 * set to either a #GFileError or #GKeyFileError.
550 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
554 g_key_file_load_from_data_dirs (GKeyFile *key_file,
560 GError *key_file_error = NULL;
561 gchar **all_data_dirs, **data_dirs;
562 const gchar * user_data_dir;
563 const gchar * const * system_data_dirs;
569 g_return_val_if_fail (key_file != NULL, FALSE);
570 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
572 user_data_dir = g_get_user_data_dir ();
573 system_data_dirs = g_get_system_data_dirs ();
574 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
577 all_data_dirs[i++] = g_strdup (user_data_dir);
580 while (system_data_dirs[j] != NULL)
581 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
584 data_dirs = all_data_dirs;
586 while (*data_dirs != NULL && !found_file)
588 g_free (output_path);
590 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
596 g_propagate_error (error, key_file_error);
600 found_file = g_key_file_load_from_fd (key_file, fd, flags,
606 g_propagate_error (error, key_file_error);
611 if (found_file && full_path)
612 *full_path = output_path;
614 g_free (output_path);
616 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)");
1018 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1020 GKeyFileKeyValuePair *pair;
1022 pair = (GKeyFileKeyValuePair *) tmp->data;
1028 keys = g_new0 (gchar *, num_keys + 1);
1031 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1033 GKeyFileKeyValuePair *pair;
1035 pair = (GKeyFileKeyValuePair *) tmp->data;
1039 keys[i] = g_strdup (pair->key);
1044 keys[num_keys] = NULL;
1053 * g_key_file_get_start_group:
1054 * @key_file: a #GKeyFile
1056 * Returns the name of the start group of the file.
1058 * Return value: The start group of the key file.
1063 g_key_file_get_start_group (GKeyFile *key_file)
1065 g_return_val_if_fail (key_file != NULL, NULL);
1067 if (key_file->start_group)
1068 return g_strdup (key_file->start_group->name);
1074 * g_key_file_get_groups:
1075 * @key_file: a #GKeyFile
1076 * @length: return location for the number of returned groups, or %NULL
1078 * Returns all groups in the key file loaded with @key_file. The
1079 * array of returned groups will be %NULL-terminated, so @length may
1080 * optionally be %NULL.
1082 * Return value: a newly-allocated %NULL-terminated array of strings.
1083 * Use g_strfreev() to free it.
1087 g_key_file_get_groups (GKeyFile *key_file,
1092 gsize i, num_groups;
1094 g_return_val_if_fail (key_file != NULL, NULL);
1096 num_groups = g_list_length (key_file->groups);
1098 g_assert (num_groups > 0);
1100 /* Only need num_groups instead of num_groups + 1
1101 * because the first group of the file (last in the
1102 * list) is always the comment group at the top,
1105 groups = g_new0 (gchar *, num_groups);
1107 group_node = g_list_last (key_file->groups);
1109 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1112 for (group_node = group_node->prev;
1114 group_node = group_node->prev)
1116 GKeyFileGroup *group;
1118 group = (GKeyFileGroup *) group_node->data;
1120 g_assert (group->name != NULL);
1122 groups[i++] = g_strdup (group->name);
1133 * g_key_file_get_value:
1134 * @key_file: a #GKeyFile
1135 * @group_name: a group name
1137 * @error: return location for a #GError, or %NULL
1139 * Returns the value associated with @key under @group_name.
1141 * In the event the key cannot be found, %NULL is returned and
1142 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1143 * event that the @group_name cannot be found, %NULL is returned
1144 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1146 * Return value: a newly allocated string or %NULL if the specified
1147 * key cannot be found.
1152 g_key_file_get_value (GKeyFile *key_file,
1153 const gchar *group_name,
1157 GKeyFileGroup *group;
1158 GKeyFileKeyValuePair *pair;
1159 gchar *value = NULL;
1161 g_return_val_if_fail (key_file != NULL, NULL);
1162 g_return_val_if_fail (group_name != NULL, NULL);
1163 g_return_val_if_fail (key != NULL, NULL);
1165 group = g_key_file_lookup_group (key_file, group_name);
1169 g_set_error (error, G_KEY_FILE_ERROR,
1170 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1171 _("Key file does not have group '%s'"),
1172 group_name ? group_name : "(null)");
1176 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1179 value = g_strdup (pair->value);
1181 g_set_error (error, G_KEY_FILE_ERROR,
1182 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1183 _("Key file does not have key '%s'"), key);
1189 * g_key_file_set_value:
1190 * @key_file: a #GKeyFile
1191 * @group_name: a group name
1195 * Associates a new value with @key under @group_name. If @key
1196 * cannot be found then it is created. If @group_name cannot be
1197 * found then it is created.
1202 g_key_file_set_value (GKeyFile *key_file,
1203 const gchar *group_name,
1207 GKeyFileGroup *group;
1208 GKeyFileKeyValuePair *pair;
1210 g_return_if_fail (key_file != NULL);
1211 g_return_if_fail (group_name != NULL);
1212 g_return_if_fail (key != NULL);
1213 g_return_if_fail (value != NULL);
1215 group = g_key_file_lookup_group (key_file, group_name);
1219 g_key_file_add_group (key_file, group_name);
1220 group = (GKeyFileGroup *) key_file->groups->data;
1222 g_key_file_add_key (key_file, group, key, value);
1226 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1229 g_key_file_add_key (key_file, group, key, value);
1232 g_free (pair->value);
1233 pair->value = g_strdup (value);
1239 * g_key_file_get_string:
1240 * @key_file: a #GKeyFile
1241 * @group_name: a group name
1243 * @error: return location for a #GError, or %NULL
1245 * Returns the value associated with @key under @group_name.
1247 * In the event the key cannot be found, %NULL is returned and
1248 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1249 * event that the @group_name cannot be found, %NULL is returned
1250 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1252 * Return value: a newly allocated string or %NULL if the specified
1253 * key cannot be found.
1258 g_key_file_get_string (GKeyFile *key_file,
1259 const gchar *group_name,
1263 gchar *value, *string_value;
1264 GError *key_file_error;
1266 g_return_val_if_fail (key_file != NULL, NULL);
1267 g_return_val_if_fail (group_name != NULL, NULL);
1268 g_return_val_if_fail (key != NULL, NULL);
1270 key_file_error = NULL;
1272 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1276 g_propagate_error (error, key_file_error);
1280 if (!g_utf8_validate (value, -1, NULL))
1282 g_set_error (error, G_KEY_FILE_ERROR,
1283 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1284 _("Key file contains key '%s' with value '%s' "
1285 "which is not UTF-8"), key, value);
1290 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1296 if (g_error_matches (key_file_error,
1298 G_KEY_FILE_ERROR_INVALID_VALUE))
1300 g_set_error (error, G_KEY_FILE_ERROR,
1301 G_KEY_FILE_ERROR_INVALID_VALUE,
1302 _("Key file contains key '%s' "
1303 "which has value that cannot be interpreted."),
1305 g_error_free (key_file_error);
1308 g_propagate_error (error, key_file_error);
1311 return string_value;
1315 * g_key_file_set_string:
1316 * @key_file: a #GKeyFile
1317 * @group_name: a group name
1321 * Associates a new string value with @key under @group_name. If
1322 * @key cannot be found then it is created. If @group_name
1323 * cannot be found then it is created.
1328 g_key_file_set_string (GKeyFile *key_file,
1329 const gchar *group_name,
1331 const gchar *string)
1335 g_return_if_fail (key_file != NULL);
1336 g_return_if_fail (group_name != NULL);
1337 g_return_if_fail (key != NULL);
1338 g_return_if_fail (string != NULL);
1340 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1341 g_key_file_set_value (key_file, group_name, key, value);
1346 * g_key_file_get_string_list:
1347 * @key_file: a #GKeyFile
1348 * @group_name: a group name
1350 * @length: return location for the number of returned strings, or %NULL
1351 * @error: return location for a #GError, or %NULL
1353 * Returns the values associated with @key under @group_name.
1355 * In the event the key cannot be found, %NULL is returned and
1356 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1357 * event that the @group_name cannot be found, %NULL is returned
1358 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1360 * Return value: a %NULL-terminated string array or %NULL if the specified
1361 * key cannot be found. The array should be freed with g_strfreev().
1366 g_key_file_get_string_list (GKeyFile *key_file,
1367 const gchar *group_name,
1372 GError *key_file_error = NULL;
1373 gchar *value, *string_value, **values;
1375 GSList *p, *pieces = NULL;
1377 g_return_val_if_fail (key_file != NULL, NULL);
1378 g_return_val_if_fail (group_name != NULL, NULL);
1379 g_return_val_if_fail (key != NULL, NULL);
1381 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1385 g_propagate_error (error, key_file_error);
1389 if (!g_utf8_validate (value, -1, NULL))
1391 g_set_error (error, G_KEY_FILE_ERROR,
1392 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1393 _("Key file contains key '%s' with value '%s' "
1394 "which is not UTF-8"), key, value);
1399 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1401 g_free (string_value);
1405 if (g_error_matches (key_file_error,
1407 G_KEY_FILE_ERROR_INVALID_VALUE))
1409 g_set_error (error, G_KEY_FILE_ERROR,
1410 G_KEY_FILE_ERROR_INVALID_VALUE,
1411 _("Key file contains key '%s' "
1412 "which has value that cannot be interpreted."),
1414 g_error_free (key_file_error);
1417 g_propagate_error (error, key_file_error);
1420 len = g_slist_length (pieces);
1421 values = g_new0 (gchar *, len + 1);
1422 for (p = pieces, i = 0; p; p = p->next)
1423 values[i++] = p->data;
1426 g_slist_free (pieces);
1435 * g_key_file_set_string_list:
1436 * @key_file: a #GKeyFile
1437 * @group_name: a group name
1439 * @list: an array of locale string values
1440 * @length: number of locale string values in @list
1442 * Associates a list of string values for @key under @group_name.
1443 * If @key cannot be found then it is created. If @group_name
1444 * cannot be found then it is created.
1449 g_key_file_set_string_list (GKeyFile *key_file,
1450 const gchar *group_name,
1452 const gchar * const list[],
1455 GString *value_list;
1458 g_return_if_fail (key_file != NULL);
1459 g_return_if_fail (group_name != NULL);
1460 g_return_if_fail (key != NULL);
1461 g_return_if_fail (list != NULL);
1463 value_list = g_string_sized_new (length * 128);
1464 for (i = 0; list[i] != NULL && i < length; i++)
1468 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1469 g_string_append (value_list, value);
1470 g_string_append_c (value_list, key_file->list_separator);
1475 g_key_file_set_value (key_file, group_name, key, value_list->str);
1476 g_string_free (value_list, TRUE);
1480 * g_key_file_set_locale_string:
1481 * @key_file: a #GKeyFile
1482 * @group_name: a group name
1487 * Associates a string value for @key and @locale under
1488 * @group_name. If the translation for @key cannot be found
1489 * then it is created.
1494 g_key_file_set_locale_string (GKeyFile *key_file,
1495 const gchar *group_name,
1497 const gchar *locale,
1498 const gchar *string)
1500 gchar *full_key, *value;
1502 g_return_if_fail (key_file != NULL);
1503 g_return_if_fail (group_name != NULL);
1504 g_return_if_fail (key != NULL);
1505 g_return_if_fail (locale != NULL);
1506 g_return_if_fail (string != NULL);
1508 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1509 full_key = g_strdup_printf ("%s[%s]", key, locale);
1510 g_key_file_set_value (key_file, group_name, full_key, value);
1515 extern GSList *_g_compute_locale_variants (const gchar *locale);
1518 * g_key_file_get_locale_string:
1519 * @key_file: a #GKeyFile
1520 * @group_name: a group name
1522 * @locale: a locale or %NULL
1523 * @error: return location for a #GError, or %NULL
1525 * Returns the value associated with @key under @group_name
1526 * translated in the given @locale if available. If @locale is
1527 * %NULL then the current locale is assumed.
1529 * If @key cannot be found then %NULL is returned and @error is set to
1530 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1531 * with @key cannot be interpreted or no suitable translation can
1532 * be found then the untranslated value is returned.
1534 * Return value: a newly allocated string or %NULL if the specified
1535 * key cannot be found.
1540 g_key_file_get_locale_string (GKeyFile *key_file,
1541 const gchar *group_name,
1543 const gchar *locale,
1546 gchar *candidate_key, *translated_value;
1547 GError *key_file_error;
1549 gboolean free_languages = FALSE;
1552 g_return_val_if_fail (key_file != NULL, NULL);
1553 g_return_val_if_fail (group_name != NULL, NULL);
1554 g_return_val_if_fail (key != NULL, NULL);
1556 candidate_key = NULL;
1557 translated_value = NULL;
1558 key_file_error = NULL;
1564 list = _g_compute_locale_variants (locale);
1566 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1567 for (l = list, i = 0; l; l = l->next, i++)
1568 languages[i] = l->data;
1569 languages[i] = NULL;
1571 g_slist_free (list);
1572 free_languages = TRUE;
1576 languages = (gchar **) g_get_language_names ();
1577 free_languages = FALSE;
1580 for (i = 0; languages[i]; i++)
1582 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1584 translated_value = g_key_file_get_string (key_file,
1586 candidate_key, NULL);
1587 g_free (candidate_key);
1589 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1592 g_free (translated_value);
1593 translated_value = NULL;
1596 /* Fallback to untranslated key
1598 if (!translated_value)
1600 translated_value = g_key_file_get_string (key_file, group_name, key,
1603 if (!translated_value)
1604 g_propagate_error (error, key_file_error);
1608 g_strfreev (languages);
1610 return translated_value;
1614 * g_key_file_get_locale_string_list:
1615 * @key_file: a #GKeyFile
1616 * @group_name: a group name
1619 * @length: return location for the number of returned strings or %NULL
1620 * @error: return location for a #GError or %NULL
1622 * Returns the values associated with @key under @group_name
1623 * translated in the given @locale if available. If @locale is
1624 * %NULL then the current locale is assumed.
1626 * If @key cannot be found then %NULL is returned and @error is set to
1627 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1628 * with @key cannot be interpreted or no suitable translations
1629 * can be found then the untranslated values are returned.
1630 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1632 * Return value: a newly allocated %NULL-terminated string array
1633 * or %NULL if the key isn't found. The string array should be freed
1634 * with g_strfreev().
1639 g_key_file_get_locale_string_list (GKeyFile *key_file,
1640 const gchar *group_name,
1642 const gchar *locale,
1646 GError *key_file_error;
1647 gchar **values, *value;
1649 g_return_val_if_fail (key_file != NULL, NULL);
1650 g_return_val_if_fail (group_name != NULL, NULL);
1651 g_return_val_if_fail (key != NULL, NULL);
1653 key_file_error = NULL;
1655 value = g_key_file_get_locale_string (key_file, group_name,
1660 g_propagate_error (error, key_file_error);
1665 if (value[strlen (value) - 1] == ';')
1666 value[strlen (value) - 1] = '\0';
1668 values = g_strsplit (value, ";", 0);
1673 *length = g_strv_length (values);
1679 * g_key_file_set_locale_string_list:
1680 * @key_file: a #GKeyFile
1681 * @group_name: a group name
1684 * @list: a %NULL-terminated array of locale string values
1685 * @length: the length of @list
1687 * Associates a list of string values for @key and @locale under
1688 * @group_name. If the translation for @key cannot be found then
1694 g_key_file_set_locale_string_list (GKeyFile *key_file,
1695 const gchar *group_name,
1697 const gchar *locale,
1698 const gchar * const list[],
1701 GString *value_list;
1705 g_return_if_fail (key_file != NULL);
1706 g_return_if_fail (group_name != NULL);
1707 g_return_if_fail (key != NULL);
1708 g_return_if_fail (locale != NULL);
1709 g_return_if_fail (length != 0);
1711 value_list = g_string_sized_new (length * 128);
1712 for (i = 0; list[i] != NULL && i < length; i++)
1716 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1718 g_string_append (value_list, value);
1719 g_string_append_c (value_list, ';');
1724 full_key = g_strdup_printf ("%s[%s]", key, locale);
1725 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1727 g_string_free (value_list, TRUE);
1731 * g_key_file_get_boolean:
1732 * @key_file: a #GKeyFile
1733 * @group_name: a group name
1735 * @error: return location for a #GError
1737 * Returns the value associated with @key under @group_name as a
1740 * If @key cannot be found then the return value is undefined and
1741 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1742 * the value associated with @key cannot be interpreted as a boolean
1743 * then the return value is also undefined and @error is set to
1744 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1746 * Return value: the value associated with the key as a boolean
1750 g_key_file_get_boolean (GKeyFile *key_file,
1751 const gchar *group_name,
1755 GError *key_file_error = NULL;
1757 gboolean bool_value;
1759 g_return_val_if_fail (key_file != NULL, FALSE);
1760 g_return_val_if_fail (group_name != NULL, FALSE);
1761 g_return_val_if_fail (key != NULL, FALSE);
1763 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1767 g_propagate_error (error, key_file_error);
1771 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1777 if (g_error_matches (key_file_error,
1779 G_KEY_FILE_ERROR_INVALID_VALUE))
1781 g_set_error (error, G_KEY_FILE_ERROR,
1782 G_KEY_FILE_ERROR_INVALID_VALUE,
1783 _("Key file contains key '%s' "
1784 "which has value that cannot be interpreted."),
1786 g_error_free (key_file_error);
1789 g_propagate_error (error, key_file_error);
1796 * g_key_file_set_boolean:
1797 * @key_file: a #GKeyFile
1798 * @group_name: a group name
1800 * @value: %TRUE or %FALSE
1802 * Associates a new boolean value with @key under @group_name.
1803 * If @key cannot be found then it is created.
1808 g_key_file_set_boolean (GKeyFile *key_file,
1809 const gchar *group_name,
1815 g_return_if_fail (key_file != NULL);
1816 g_return_if_fail (group_name != NULL);
1817 g_return_if_fail (key != NULL);
1819 result = g_key_file_parse_boolean_as_value (key_file, value);
1820 g_key_file_set_value (key_file, group_name, key, result);
1825 * g_key_file_get_boolean_list:
1826 * @key_file: a #GKeyFile
1827 * @group_name: a group name
1829 * @length: the number of booleans returned
1830 * @error: return location for a #GError
1832 * Returns the values associated with @key under @group_name as
1833 * booleans. If @group_name is %NULL, the start_group is used.
1835 * If @key cannot be found then the return value is undefined and
1836 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1837 * the values associated with @key cannot be interpreted as booleans
1838 * then the return value is also undefined and @error is set to
1839 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1841 * Return value: the values associated with the key as a boolean
1846 g_key_file_get_boolean_list (GKeyFile *key_file,
1847 const gchar *group_name,
1852 GError *key_file_error;
1854 gboolean *bool_values;
1857 g_return_val_if_fail (key_file != NULL, NULL);
1858 g_return_val_if_fail (group_name != NULL, NULL);
1859 g_return_val_if_fail (key != NULL, NULL);
1861 key_file_error = NULL;
1863 values = g_key_file_get_string_list (key_file, group_name, key,
1864 &num_bools, &key_file_error);
1867 g_propagate_error (error, key_file_error);
1872 bool_values = g_new0 (gboolean, num_bools);
1874 for (i = 0; i < num_bools; i++)
1876 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1882 g_propagate_error (error, key_file_error);
1883 g_strfreev (values);
1884 g_free (bool_values);
1889 g_strfreev (values);
1892 *length = num_bools;
1898 * g_key_file_set_boolean_list:
1899 * @key_file: a #GKeyFile
1900 * @group_name: a group name
1902 * @list: an array of boolean values
1903 * @length: length of @list
1905 * Associates a list of boolean values with @key under
1906 * @group_name. If @key cannot be found then it is created.
1907 * If @group_name is %NULL, the start_group is used.
1912 g_key_file_set_boolean_list (GKeyFile *key_file,
1913 const gchar *group_name,
1918 GString *value_list;
1921 g_return_if_fail (key_file != NULL);
1922 g_return_if_fail (group_name != NULL);
1923 g_return_if_fail (key != NULL);
1924 g_return_if_fail (list != NULL);
1926 value_list = g_string_sized_new (length * 8);
1927 for (i = 0; i < length; i++)
1931 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1933 g_string_append (value_list, value);
1934 g_string_append_c (value_list, key_file->list_separator);
1939 g_key_file_set_value (key_file, group_name, key, value_list->str);
1940 g_string_free (value_list, TRUE);
1944 * g_key_file_get_integer:
1945 * @key_file: a #GKeyFile
1946 * @group_name: a group name
1948 * @error: return location for a #GError
1950 * Returns the value associated with @key under @group_name as an
1951 * integer. If @group_name is %NULL, the start_group is used.
1953 * If @key cannot be found then the return value is undefined and
1954 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1955 * the value associated with @key cannot be interpreted as an integer
1956 * then the return value is also undefined and @error is set to
1957 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1959 * Return value: the value associated with the key as an integer.
1964 g_key_file_get_integer (GKeyFile *key_file,
1965 const gchar *group_name,
1969 GError *key_file_error;
1973 g_return_val_if_fail (key_file != NULL, -1);
1974 g_return_val_if_fail (group_name != NULL, -1);
1975 g_return_val_if_fail (key != NULL, -1);
1977 key_file_error = NULL;
1979 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1983 g_propagate_error (error, key_file_error);
1987 int_value = g_key_file_parse_value_as_integer (key_file, value,
1993 if (g_error_matches (key_file_error,
1995 G_KEY_FILE_ERROR_INVALID_VALUE))
1997 g_set_error (error, G_KEY_FILE_ERROR,
1998 G_KEY_FILE_ERROR_INVALID_VALUE,
1999 _("Key file contains key '%s' in group '%s' "
2000 "which has value that cannot be interpreted."), key,
2002 g_error_free (key_file_error);
2005 g_propagate_error (error, key_file_error);
2012 * g_key_file_set_integer:
2013 * @key_file: a #GKeyFile
2014 * @group_name: a group name
2016 * @value: an integer value
2018 * Associates a new integer value with @key under @group_name.
2019 * If @key cannot be found then it is created.
2024 g_key_file_set_integer (GKeyFile *key_file,
2025 const gchar *group_name,
2031 g_return_if_fail (key_file != NULL);
2032 g_return_if_fail (group_name != NULL);
2033 g_return_if_fail (key != NULL);
2035 result = g_key_file_parse_integer_as_value (key_file, value);
2036 g_key_file_set_value (key_file, group_name, key, result);
2041 * g_key_file_get_integer_list:
2042 * @key_file: a #GKeyFile
2043 * @group_name: a group name
2045 * @length: the number of integers returned
2046 * @error: return location for a #GError
2048 * Returns the values associated with @key under @group_name as
2051 * If @key cannot be found then the return value is undefined and
2052 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2053 * the values associated with @key cannot be interpreted as integers
2054 * then the return value is also undefined and @error is set to
2055 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2057 * Return value: the values associated with the key as a integer
2062 g_key_file_get_integer_list (GKeyFile *key_file,
2063 const gchar *group_name,
2068 GError *key_file_error = NULL;
2073 g_return_val_if_fail (key_file != NULL, NULL);
2074 g_return_val_if_fail (group_name != NULL, NULL);
2075 g_return_val_if_fail (key != NULL, NULL);
2077 values = g_key_file_get_string_list (key_file, group_name, key,
2078 &num_ints, &key_file_error);
2081 g_propagate_error (error, key_file_error);
2086 int_values = g_new0 (gint, num_ints);
2088 for (i = 0; i < num_ints; i++)
2090 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2096 g_propagate_error (error, key_file_error);
2097 g_strfreev (values);
2098 g_free (int_values);
2103 g_strfreev (values);
2112 * g_key_file_set_integer_list:
2113 * @key_file: a #GKeyFile
2114 * @group_name: a group name
2116 * @list: an array of integer values
2117 * @length: number of integer values in @list
2119 * Associates a list of integer values with @key under
2120 * @group_name. If @key cannot be found then it is created.
2125 g_key_file_set_integer_list (GKeyFile *key_file,
2126 const gchar *group_name,
2134 g_return_if_fail (key_file != NULL);
2135 g_return_if_fail (group_name != NULL);
2136 g_return_if_fail (key != NULL);
2137 g_return_if_fail (list != NULL);
2139 values = g_string_sized_new (length * 16);
2140 for (i = 0; i < length; i++)
2144 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2146 g_string_append (values, value);
2147 g_string_append_c (values, ';');
2152 g_key_file_set_value (key_file, group_name, key, values->str);
2153 g_string_free (values, TRUE);
2157 g_key_file_set_key_comment (GKeyFile *key_file,
2158 const gchar *group_name,
2160 const gchar *comment,
2163 GKeyFileGroup *group;
2164 GKeyFileKeyValuePair *pair;
2165 GList *key_node, *comment_node, *tmp;
2167 group = g_key_file_lookup_group (key_file, group_name);
2170 g_set_error (error, G_KEY_FILE_ERROR,
2171 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2172 _("Key file does not have group '%s'"),
2173 group_name ? group_name : "(null)");
2178 /* First find the key the comments are supposed to be
2181 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2183 if (key_node == NULL)
2185 g_set_error (error, G_KEY_FILE_ERROR,
2186 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2187 _("Key file does not have key '%s' in group '%s'"),
2192 /* Then find all the comments already associated with the
2195 tmp = key_node->next;
2198 GKeyFileKeyValuePair *pair;
2200 pair = (GKeyFileKeyValuePair *) tmp->data;
2202 if (pair->key != NULL)
2207 g_key_file_remove_key_value_pair_node (key_file, group,
2211 if (comment == NULL)
2214 /* Now we can add our new comment
2216 pair = g_new0 (GKeyFileKeyValuePair, 1);
2219 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2221 key_node = g_list_insert (key_node, pair, 1);
2225 g_key_file_set_group_comment (GKeyFile *key_file,
2226 const gchar *group_name,
2227 const gchar *comment,
2230 GKeyFileGroup *group;
2232 group = g_key_file_lookup_group (key_file, group_name);
2235 g_set_error (error, G_KEY_FILE_ERROR,
2236 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2237 _("Key file does not have group '%s'"),
2238 group_name ? group_name : "(null)");
2243 /* First remove any existing comment
2247 g_key_file_key_value_pair_free (group->comment);
2248 group->comment = NULL;
2251 if (comment == NULL)
2254 /* Now we can add our new comment
2256 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2258 group->comment->key = NULL;
2259 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2263 g_key_file_set_top_comment (GKeyFile *key_file,
2264 const gchar *comment,
2268 GKeyFileGroup *group;
2269 GKeyFileKeyValuePair *pair;
2271 /* The last group in the list should be the top (comments only)
2274 g_assert (key_file->groups != NULL);
2275 group_node = g_list_last (key_file->groups);
2276 group = (GKeyFileGroup *) group_node->data;
2277 g_assert (group->name == NULL);
2279 /* Note all keys must be comments at the top of
2280 * the file, so we can just free it all.
2282 if (group->key_value_pairs != NULL)
2284 g_list_foreach (group->key_value_pairs,
2285 (GFunc) g_key_file_key_value_pair_free,
2287 g_list_free (group->key_value_pairs);
2288 group->key_value_pairs = NULL;
2291 if (comment == NULL)
2294 pair = g_new0 (GKeyFileKeyValuePair, 1);
2297 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2299 group->key_value_pairs =
2300 g_list_prepend (group->key_value_pairs, pair);
2304 * g_key_file_set_comment:
2305 * @key_file: a #GKeyFile
2306 * @group_name: a group name, or %NULL
2308 * @comment: a comment
2309 * @error: return location for a #GError
2311 * Places a comment above @key from @group_name.
2312 * @group_name. If @key is %NULL then @comment will
2313 * be written above @group_name. If both @key
2314 * and @group_name are NULL, then @comment will
2315 * be written above the first group in the file.
2320 g_key_file_set_comment (GKeyFile *key_file,
2321 const gchar *group_name,
2323 const gchar *comment,
2326 g_return_if_fail (key_file != NULL);
2328 if (group_name != NULL && key != NULL)
2329 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2330 else if (group_name != NULL)
2331 g_key_file_set_group_comment (key_file, group_name, comment, error);
2333 g_key_file_set_top_comment (key_file, comment, error);
2335 if (comment != NULL)
2336 key_file->approximate_size += strlen (comment);
2340 g_key_file_get_key_comment (GKeyFile *key_file,
2341 const gchar *group_name,
2345 GKeyFileGroup *group;
2346 GKeyFileKeyValuePair *pair;
2347 GList *key_node, *tmp;
2351 group = g_key_file_lookup_group (key_file, group_name);
2354 g_set_error (error, G_KEY_FILE_ERROR,
2355 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2356 _("Key file does not have group '%s'"),
2357 group_name ? group_name : "(null)");
2362 /* First find the key the comments are supposed to be
2365 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2367 if (key_node == NULL)
2369 g_set_error (error, G_KEY_FILE_ERROR,
2370 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2371 _("Key file does not have key '%s' in group '%s'"),
2378 /* Then find all the comments already associated with the
2379 * key and concatentate them.
2381 tmp = key_node->next;
2382 if (!key_node->next)
2385 pair = (GKeyFileKeyValuePair *) tmp->data;
2386 if (pair->key != NULL)
2391 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2393 if (pair->key != NULL)
2399 while (tmp != key_node)
2401 GKeyFileKeyValuePair *pair;
2403 pair = (GKeyFileKeyValuePair *) tmp->data;
2406 string = g_string_sized_new (512);
2408 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2409 g_string_append (string, comment);
2417 comment = string->str;
2418 g_string_free (string, FALSE);
2427 get_group_comment (GKeyFile *key_file,
2428 GKeyFileGroup *group,
2437 tmp = group->key_value_pairs;
2440 GKeyFileKeyValuePair *pair;
2442 pair = (GKeyFileKeyValuePair *) tmp->data;
2444 if (pair->key != NULL)
2450 if (tmp->next == NULL)
2458 GKeyFileKeyValuePair *pair;
2460 pair = (GKeyFileKeyValuePair *) tmp->data;
2463 string = g_string_sized_new (512);
2465 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2466 g_string_append (string, comment);
2473 return g_string_free (string, FALSE);
2479 g_key_file_get_group_comment (GKeyFile *key_file,
2480 const gchar *group_name,
2484 GKeyFileGroup *group;
2486 group_node = g_key_file_lookup_group_node (key_file, group_name);
2487 group = (GKeyFileGroup *)group_node->data;
2490 g_set_error (error, G_KEY_FILE_ERROR,
2491 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2492 _("Key file does not have group '%s'"),
2493 group_name ? group_name : "(null)");
2499 return g_strdup (group->comment->value);
2501 group_node = group_node->next;
2502 group = (GKeyFileGroup *)group_node->data;
2503 return get_group_comment (key_file, group, error);
2507 g_key_file_get_top_comment (GKeyFile *key_file,
2511 GKeyFileGroup *group;
2513 /* The last group in the list should be the top (comments only)
2516 g_assert (key_file->groups != NULL);
2517 group_node = g_list_last (key_file->groups);
2518 group = (GKeyFileGroup *) group_node->data;
2519 g_assert (group->name == NULL);
2521 return get_group_comment (key_file, group, error);
2525 * g_key_file_get_comment:
2526 * @key_file: a #GKeyFile
2527 * @group_name: a group name, or %NULL
2529 * @error: return location for a #GError
2531 * Retrieves a comment above @key from @group_name.
2532 * @group_name. If @key is %NULL then @comment will
2533 * be read from above @group_name. If both @key
2534 * and @group_name are NULL, then @comment will
2535 * be read from above the first group in the file.
2537 * Returns: a comment that should be freed with g_free()
2542 g_key_file_get_comment (GKeyFile *key_file,
2543 const gchar *group_name,
2547 g_return_val_if_fail (key_file != NULL, NULL);
2549 if (group_name != NULL && key != NULL)
2550 return g_key_file_get_key_comment (key_file, group_name, key, error);
2551 else if (group_name != NULL)
2552 return g_key_file_get_group_comment (key_file, group_name, error);
2554 return g_key_file_get_top_comment (key_file, error);
2558 * g_key_file_remove_comment:
2559 * @key_file: a #GKeyFile
2560 * @group_name: a group name, or %NULL
2562 * @error: return location for a #GError
2564 * Removes a comment above @key from @group_name.
2565 * @group_name. If @key is %NULL then @comment will
2566 * be written above @group_name. If both @key
2567 * and @group_name are NULL, then @comment will
2568 * be written above the first group in the file.
2574 g_key_file_remove_comment (GKeyFile *key_file,
2575 const gchar *group_name,
2579 g_return_if_fail (key_file != NULL);
2581 if (group_name != NULL && key != NULL)
2582 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2583 else if (group_name != NULL)
2584 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2586 g_key_file_set_top_comment (key_file, NULL, error);
2590 * g_key_file_has_group:
2591 * @key_file: a #GKeyFile
2592 * @group_name: a group name
2594 * Looks whether the key file has the group @group_name.
2596 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2601 g_key_file_has_group (GKeyFile *key_file,
2602 const gchar *group_name)
2604 g_return_val_if_fail (key_file != NULL, FALSE);
2605 g_return_val_if_fail (group_name != NULL, FALSE);
2607 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2611 * g_key_file_has_key:
2612 * @key_file: a #GKeyFile
2613 * @group_name: a group name
2615 * @error: return location for a #GError
2617 * Looks whether the key file has the key @key in the group
2620 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2626 g_key_file_has_key (GKeyFile *key_file,
2627 const gchar *group_name,
2631 GKeyFileKeyValuePair *pair;
2632 GKeyFileGroup *group;
2634 g_return_val_if_fail (key_file != NULL, FALSE);
2635 g_return_val_if_fail (group_name != NULL, FALSE);
2636 g_return_val_if_fail (key != NULL, FALSE);
2638 group = g_key_file_lookup_group (key_file, group_name);
2642 g_set_error (error, G_KEY_FILE_ERROR,
2643 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2644 _("Key file does not have group '%s'"),
2645 group_name ? group_name : "(null)");
2650 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2652 return pair != NULL;
2656 g_key_file_add_group (GKeyFile *key_file,
2657 const gchar *group_name)
2659 GKeyFileGroup *group;
2661 g_return_if_fail (key_file != NULL);
2662 g_return_if_fail (group_name != NULL);
2664 if (g_key_file_lookup_group_node (key_file, group_name) != NULL)
2667 group = g_new0 (GKeyFileGroup, 1);
2668 group->name = g_strdup (group_name);
2669 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2670 key_file->groups = g_list_prepend (key_file->groups, group);
2671 key_file->approximate_size += strlen (group_name) + 3;
2672 key_file->current_group = group;
2674 if (key_file->start_group == NULL)
2675 key_file->start_group = group;
2679 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2684 g_free (pair->value);
2689 /* Be careful not to call this function on a node with data in the
2690 * lookup map without removing it from the lookup map, first.
2692 * Some current cases where this warning is not a concern are
2694 * - the node being removed is a comment node
2695 * - the entire lookup map is getting destroyed soon after
2699 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2700 GKeyFileGroup *group,
2704 GKeyFileKeyValuePair *pair;
2706 pair = (GKeyFileKeyValuePair *) pair_node->data;
2708 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2710 if (pair->key != NULL)
2711 key_file->approximate_size -= strlen (pair->key) + 1;
2713 g_assert (pair->value != NULL);
2714 key_file->approximate_size -= strlen (pair->value);
2716 g_key_file_key_value_pair_free (pair);
2718 g_list_free_1 (pair_node);
2722 g_key_file_remove_group_node (GKeyFile *key_file,
2725 GKeyFileGroup *group;
2728 group = (GKeyFileGroup *) group_node->data;
2730 /* If the current group gets deleted make the current group the last
2733 if (key_file->current_group == group)
2735 /* groups should always contain at least the top comment group,
2736 * unless g_key_file_clear has been called
2738 if (key_file->groups)
2739 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2741 key_file->current_group = NULL;
2744 /* If the start group gets deleted make the start group the first
2747 if (key_file->start_group == group)
2749 tmp = g_list_last (key_file->groups);
2752 if (tmp != group_node &&
2753 ((GKeyFileGroup *) tmp->data)->name != NULL)
2760 key_file->start_group = (GKeyFileGroup *) tmp->data;
2762 key_file->start_group = NULL;
2765 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2767 if (group->name != NULL)
2768 key_file->approximate_size -= strlen (group->name) + 3;
2770 tmp = group->key_value_pairs;
2777 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2780 g_assert (group->key_value_pairs == NULL);
2782 if (group->lookup_map)
2784 g_hash_table_destroy (group->lookup_map);
2785 group->lookup_map = NULL;
2788 g_free ((gchar *) group->name);
2790 g_list_free_1 (group_node);
2794 * g_key_file_remove_group:
2795 * @key_file: a #GKeyFile
2796 * @group_name: a group name
2797 * @error: return location for a #GError or %NULL
2799 * Removes the specified group, @group_name,
2800 * from the key file.
2805 g_key_file_remove_group (GKeyFile *key_file,
2806 const gchar *group_name,
2811 g_return_if_fail (key_file != NULL);
2812 g_return_if_fail (group_name != NULL);
2814 group_node = g_key_file_lookup_group_node (key_file, group_name);
2818 g_set_error (error, G_KEY_FILE_ERROR,
2819 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2820 _("Key file does not have group '%s'"),
2825 g_key_file_remove_group_node (key_file, group_node);
2829 g_key_file_add_key (GKeyFile *key_file,
2830 GKeyFileGroup *group,
2834 GKeyFileKeyValuePair *pair;
2836 pair = g_new0 (GKeyFileKeyValuePair, 1);
2838 pair->key = g_strdup (key);
2839 pair->value = g_strdup (value);
2841 g_hash_table_replace (group->lookup_map, pair->key, pair);
2842 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2843 key_file->approximate_size += strlen (key) + strlen (value) + 2;
2847 * g_key_file_remove_key:
2848 * @key_file: a #GKeyFile
2849 * @group_name: a group name
2850 * @key: a key name to remove
2851 * @error: return location for a #GError or %NULL
2853 * Removes @key in @group_name from the key file.
2858 g_key_file_remove_key (GKeyFile *key_file,
2859 const gchar *group_name,
2863 GKeyFileGroup *group;
2864 GKeyFileKeyValuePair *pair;
2866 g_return_if_fail (key_file != NULL);
2867 g_return_if_fail (group_name != NULL);
2868 g_return_if_fail (key != NULL);
2872 group = g_key_file_lookup_group (key_file, group_name);
2875 g_set_error (error, G_KEY_FILE_ERROR,
2876 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2877 _("Key file does not have group '%s'"),
2878 group_name ? group_name : "(null)");
2882 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2886 g_set_error (error, G_KEY_FILE_ERROR,
2887 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2888 _("Key file does not have key '%s' in group '%s'"),
2893 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2895 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
2896 g_hash_table_remove (group->lookup_map, pair->key);
2897 g_key_file_key_value_pair_free (pair);
2901 g_key_file_lookup_group_node (GKeyFile *key_file,
2902 const gchar *group_name)
2904 GKeyFileGroup *group;
2908 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2910 group = (GKeyFileGroup *) tmp->data;
2912 if (group && group->name && strcmp (group->name, group_name) == 0)
2921 static GKeyFileGroup *
2922 g_key_file_lookup_group (GKeyFile *key_file,
2923 const gchar *group_name)
2927 group_node = g_key_file_lookup_group_node (key_file, group_name);
2929 if (group_node != NULL)
2930 return (GKeyFileGroup *) group_node->data;
2936 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
2937 GKeyFileGroup *group,
2942 for (key_node = group->key_value_pairs;
2944 key_node = key_node->next)
2946 GKeyFileKeyValuePair *pair;
2948 pair = (GKeyFileKeyValuePair *) key_node->data;
2950 if (pair->key && strcmp (pair->key, key) == 0)
2957 static GKeyFileKeyValuePair *
2958 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
2959 GKeyFileGroup *group,
2962 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2965 /* Lines starting with # or consisting entirely of whitespace are merely
2966 * recorded, not parsed. This function assumes all leading whitespace
2967 * has been stripped.
2970 g_key_file_line_is_comment (const gchar *line)
2972 return (*line == '#' || *line == '\0' || *line == '\n');
2975 /* A group in a key file is made up of a starting '[' followed by one
2976 * or more letters making up the group name followed by ']'.
2979 g_key_file_line_is_group (const gchar *line)
2987 p = g_utf8_next_char (p);
2989 /* Group name must be non-empty
2991 if (!*p || *p == ']')
2994 while (*p && *p != ']')
2995 p = g_utf8_next_char (p);
3004 g_key_file_line_is_key_value_pair (const gchar *line)
3008 p = (gchar *) g_utf8_strchr (line, -1, '=');
3013 /* Key must be non-empty
3022 g_key_file_parse_value_as_string (GKeyFile *key_file,
3027 gchar *string_value, *p, *q0, *q;
3029 string_value = g_new0 (gchar, strlen (value) + 1);
3031 p = (gchar *) value;
3032 q0 = q = string_value;
3062 g_set_error (error, G_KEY_FILE_ERROR,
3063 G_KEY_FILE_ERROR_INVALID_VALUE,
3064 _("Key file contains escape character "
3069 if (pieces && *p == key_file->list_separator)
3070 *q = key_file->list_separator;
3084 g_set_error (error, G_KEY_FILE_ERROR,
3085 G_KEY_FILE_ERROR_INVALID_VALUE,
3086 _("Key file contains invalid escape "
3087 "sequence '%s'"), sequence);
3096 if (pieces && (*p == key_file->list_separator))
3098 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3114 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3115 *pieces = g_slist_reverse (*pieces);
3118 return string_value;
3122 g_key_file_parse_string_as_value (GKeyFile *key_file,
3123 const gchar *string,
3124 gboolean escape_separator)
3126 gchar *value, *p, *q;
3128 gboolean parsing_leading_space;
3130 length = strlen (string) + 1;
3132 /* Worst case would be that every character needs to be escaped.
3133 * In other words every character turns to two characters
3135 value = g_new0 (gchar, 2 * length);
3137 p = (gchar *) string;
3139 parsing_leading_space = TRUE;
3140 while (p < (string + length - 1))
3142 gchar escaped_character[3] = { '\\', 0, 0 };
3147 if (parsing_leading_space)
3149 escaped_character[1] = 's';
3150 strcpy (q, escaped_character);
3160 if (parsing_leading_space)
3162 escaped_character[1] = 't';
3163 strcpy (q, escaped_character);
3173 escaped_character[1] = 'n';
3174 strcpy (q, escaped_character);
3178 escaped_character[1] = 'r';
3179 strcpy (q, escaped_character);
3183 escaped_character[1] = '\\';
3184 strcpy (q, escaped_character);
3186 parsing_leading_space = FALSE;
3189 if (escape_separator && *p == key_file->list_separator)
3191 escaped_character[1] = key_file->list_separator;
3192 strcpy (q, escaped_character);
3194 parsing_leading_space = TRUE;
3200 parsing_leading_space = FALSE;
3212 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3216 gchar *end_of_valid_int;
3221 long_value = strtol (value, &end_of_valid_int, 10);
3223 if (*value == '\0' || *end_of_valid_int != '\0')
3225 g_set_error (error, G_KEY_FILE_ERROR,
3226 G_KEY_FILE_ERROR_INVALID_VALUE,
3227 _("Value '%s' cannot be interpreted as a number."), value);
3231 int_value = long_value;
3232 if (int_value != long_value || errno == ERANGE)
3236 G_KEY_FILE_ERROR_INVALID_VALUE,
3237 _("Integer value '%s' out of range"), value);
3245 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3249 return g_strdup_printf ("%d", value);
3253 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3259 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3261 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3265 g_set_error (error, G_KEY_FILE_ERROR,
3266 G_KEY_FILE_ERROR_INVALID_VALUE,
3267 _("Value '%s' cannot be interpreted as a boolean."), value);
3273 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3277 return g_strdup ("true");
3279 return g_strdup ("false");
3283 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3287 gchar **lines, *comment;
3290 string = g_string_sized_new (512);
3292 lines = g_strsplit (value, "\n", 0);
3294 for (i = 0; lines[i] != NULL; i++)
3296 if (lines[i][0] != '#')
3297 g_string_append_printf (string, "%s\n", lines[i]);
3299 g_string_append_printf (string, "%s\n", lines[i] + 1);
3303 comment = string->str;
3305 g_string_free (string, FALSE);
3311 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3312 const gchar *comment)
3315 gchar **lines, *value;
3318 string = g_string_sized_new (512);
3320 lines = g_strsplit (comment, "\n", 0);
3322 for (i = 0; lines[i] != NULL; i++)
3323 g_string_append_printf (string, "#%s%s", lines[i],
3324 lines[i + 1] == NULL? "" : "\n");
3327 value = string->str;
3329 g_string_free (string, FALSE);
3334 #define __G_KEY_FILE_C__
3335 #include "galiasdef.c"