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 */
92 gboolean has_trailing_blank_line;
94 GList *key_value_pairs;
96 /* Used in parallel with key_value_pairs for
97 * increased lookup performance
99 GHashTable *lookup_map;
102 struct _GKeyFileKeyValuePair
104 gchar *key; /* NULL for comments */
108 static gint find_file_in_data_dirs (const gchar *file,
112 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
116 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
117 const gchar *group_name);
118 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
119 const gchar *group_name);
121 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
122 GKeyFileGroup *group,
124 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
125 GKeyFileGroup *group,
128 static void g_key_file_remove_group_node (GKeyFile *key_file,
130 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
131 GKeyFileGroup *group,
134 static void g_key_file_add_key (GKeyFile *key_file,
135 GKeyFileGroup *group,
138 static void g_key_file_add_group (GKeyFile *key_file,
139 const gchar *group_name);
140 static gboolean g_key_file_is_group_name (const gchar *name);
141 static gboolean g_key_file_is_key_name (const gchar *name);
142 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
143 static gboolean g_key_file_line_is_comment (const gchar *line);
144 static gboolean g_key_file_line_is_group (const gchar *line);
145 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
146 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
150 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
152 gboolean escape_separator);
153 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
156 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
158 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
161 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
164 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
166 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
168 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
169 const gchar *comment);
170 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
174 static void g_key_file_parse_comment (GKeyFile *key_file,
178 static void g_key_file_parse_group (GKeyFile *key_file,
182 static gchar *key_get_locale (const gchar *key);
183 static void g_key_file_parse_data (GKeyFile *key_file,
187 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
192 g_key_file_error_quark (void)
194 return g_quark_from_static_string ("g-key-file-error-quark");
198 g_key_file_init (GKeyFile *key_file)
200 key_file->current_group = g_slice_new0 (GKeyFileGroup);
201 key_file->groups = g_list_prepend (NULL, key_file->current_group);
202 key_file->start_group = NULL;
203 key_file->parse_buffer = g_string_sized_new (128);
204 key_file->approximate_size = 0;
205 key_file->list_separator = ';';
210 g_key_file_clear (GKeyFile *key_file)
212 GList *tmp, *group_node;
214 if (key_file->parse_buffer)
215 g_string_free (key_file->parse_buffer, TRUE);
217 tmp = key_file->groups;
222 g_key_file_remove_group_node (key_file, group_node);
225 g_assert (key_file->groups == NULL);
232 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
233 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
234 * read an existing key file.
236 * Return value: an empty #GKeyFile.
241 g_key_file_new (void)
245 key_file = g_slice_new0 (GKeyFile);
246 g_key_file_init (key_file);
252 * g_key_file_set_list_separator:
253 * @key_file: a #GKeyFile
254 * @separator: the separator
256 * Sets the character which is used to separate
257 * values in lists. Typically ';' or ',' are used
258 * as separators. The default list separator is ';'.
263 g_key_file_set_list_separator (GKeyFile *key_file,
266 g_return_if_fail (key_file != NULL);
268 key_file->list_separator = separator;
272 /* Iterates through all the directories in *dirs trying to
273 * open file. When it successfully locates and opens a file it
274 * returns the file descriptor to the open file. It also
275 * outputs the absolute path of the file in output_file and
276 * leaves the unchecked directories in *dirs.
279 find_file_in_data_dirs (const gchar *file,
284 gchar **data_dirs, *data_dir, *path;
295 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
297 gchar *candidate_file, *sub_dir;
299 candidate_file = (gchar *) file;
300 sub_dir = g_strdup ("");
301 while (candidate_file != NULL && fd < 0)
305 path = g_build_filename (data_dir, sub_dir,
306 candidate_file, NULL);
308 fd = g_open (path, O_RDONLY, 0);
316 candidate_file = strchr (candidate_file, '-');
318 if (candidate_file == NULL)
324 sub_dir = g_strndup (file, candidate_file - file - 1);
326 for (p = sub_dir; *p != '\0'; p++)
329 *p = G_DIR_SEPARATOR;
340 g_set_error (error, G_KEY_FILE_ERROR,
341 G_KEY_FILE_ERROR_NOT_FOUND,
342 _("Valid key file could not be "
343 "found in data dirs"));
346 if (output_file != NULL && fd > 0)
347 *output_file = g_strdup (path);
355 g_key_file_load_from_fd (GKeyFile *key_file,
360 GError *key_file_error = NULL;
362 struct stat stat_buf;
363 gchar read_buf[4096];
365 if (fstat (fd, &stat_buf) < 0)
367 g_set_error (error, G_FILE_ERROR,
368 g_file_error_from_errno (errno),
369 "%s", g_strerror (errno));
373 if (!S_ISREG (stat_buf.st_mode))
375 g_set_error (error, G_KEY_FILE_ERROR,
376 G_KEY_FILE_ERROR_PARSE,
377 _("Not a regular file"));
381 if (stat_buf.st_size == 0)
383 g_set_error (error, G_KEY_FILE_ERROR,
384 G_KEY_FILE_ERROR_PARSE,
389 if (key_file->approximate_size > 0)
391 g_key_file_clear (key_file);
392 g_key_file_init (key_file);
394 key_file->flags = flags;
399 bytes_read = read (fd, read_buf, 4096);
401 if (bytes_read == 0) /* End of File */
406 if (errno == EINTR || errno == EAGAIN)
409 g_set_error (error, G_FILE_ERROR,
410 g_file_error_from_errno (errno),
411 "%s", g_strerror (errno));
415 g_key_file_parse_data (key_file,
416 read_buf, bytes_read,
419 while (!key_file_error);
423 g_propagate_error (error, key_file_error);
427 g_key_file_flush_parse_buffer (key_file, &key_file_error);
431 g_propagate_error (error, key_file_error);
439 * g_key_file_load_from_file:
440 * @key_file: an empty #GKeyFile struct
441 * @file: the path of a filename to load, in the GLib file name encoding
442 * @flags: flags from #GKeyFileFlags
443 * @error: return location for a #GError, or %NULL
445 * Loads a key file into an empty #GKeyFile structure.
446 * If the file could not be loaded then %error is set to
447 * either a #GFileError or #GKeyFileError.
449 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
453 g_key_file_load_from_file (GKeyFile *key_file,
458 GError *key_file_error = NULL;
461 g_return_val_if_fail (key_file != NULL, FALSE);
462 g_return_val_if_fail (file != NULL, FALSE);
464 fd = g_open (file, O_RDONLY, 0);
468 g_set_error (error, G_FILE_ERROR,
469 g_file_error_from_errno (errno),
470 "%s", g_strerror (errno));
474 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
479 g_propagate_error (error, key_file_error);
487 * g_key_file_load_from_data:
488 * @key_file: an empty #GKeyFile struct
489 * @data: key file loaded in memory.
490 * @length: the length of @data in bytes
491 * @flags: flags from #GKeyFileFlags
492 * @error: return location for a #GError, or %NULL
494 * Loads a key file from memory into an empty #GKeyFile structure. If
495 * the object cannot be created then %error is set to a
498 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
502 g_key_file_load_from_data (GKeyFile *key_file,
508 GError *key_file_error = NULL;
510 g_return_val_if_fail (key_file != NULL, FALSE);
511 g_return_val_if_fail (data != NULL, FALSE);
512 g_return_val_if_fail (length != 0, FALSE);
514 if (length == (gsize)-1)
515 length = strlen (data);
517 if (key_file->approximate_size > 0)
519 g_key_file_clear (key_file);
520 g_key_file_init (key_file);
522 key_file->flags = flags;
524 g_key_file_parse_data (key_file, data, length, &key_file_error);
528 g_propagate_error (error, key_file_error);
532 g_key_file_flush_parse_buffer (key_file, &key_file_error);
536 g_propagate_error (error, key_file_error);
544 * g_key_file_load_from_data_dirs:
545 * @key_file: an empty #GKeyFile struct
546 * @file: a relative path to a filename to open and parse
547 * @full_path: return location for a string containing the full path
548 * of the file, or %NULL
549 * @flags: flags from #GKeyFileFlags
550 * @error: return location for a #GError, or %NULL
552 * This function looks for a key file named @file in the paths
553 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
554 * loads the file into @key_file and returns the file's full path in
555 * @full_path. If the file could not be loaded then an %error is
556 * set to either a #GFileError or #GKeyFileError.
558 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
562 g_key_file_load_from_data_dirs (GKeyFile *key_file,
568 GError *key_file_error = NULL;
569 gchar **all_data_dirs, **data_dirs;
570 const gchar * user_data_dir;
571 const gchar * const * system_data_dirs;
577 g_return_val_if_fail (key_file != NULL, FALSE);
578 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
580 user_data_dir = g_get_user_data_dir ();
581 system_data_dirs = g_get_system_data_dirs ();
582 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
585 all_data_dirs[i++] = g_strdup (user_data_dir);
588 while (system_data_dirs[j] != NULL)
589 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
590 all_data_dirs[i] = NULL;
593 data_dirs = all_data_dirs;
595 while (*data_dirs != NULL && !found_file)
597 g_free (output_path);
599 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
605 g_propagate_error (error, key_file_error);
609 found_file = g_key_file_load_from_fd (key_file, fd, flags,
615 g_propagate_error (error, key_file_error);
620 if (found_file && full_path)
621 *full_path = output_path;
623 g_free (output_path);
625 g_strfreev (all_data_dirs);
632 * @key_file: a #GKeyFile
639 g_key_file_free (GKeyFile *key_file)
641 g_return_if_fail (key_file != NULL);
643 g_key_file_clear (key_file);
644 g_slice_free (GKeyFile, key_file);
647 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
648 * true for locales that match those in g_get_language_names().
651 g_key_file_locale_is_interesting (GKeyFile *key_file,
654 const gchar * const * current_locales;
657 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
660 current_locales = g_get_language_names ();
662 for (i = 0; current_locales[i] != NULL; i++)
664 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
672 g_key_file_parse_line (GKeyFile *key_file,
677 GError *parse_error = NULL;
680 g_return_if_fail (key_file != NULL);
681 g_return_if_fail (line != NULL);
683 line_start = (gchar *) line;
684 while (g_ascii_isspace (*line_start))
687 if (g_key_file_line_is_comment (line_start))
688 g_key_file_parse_comment (key_file, line, length, &parse_error);
689 else if (g_key_file_line_is_group (line_start))
690 g_key_file_parse_group (key_file, line_start,
691 length - (line_start - line),
693 else if (g_key_file_line_is_key_value_pair (line_start))
694 g_key_file_parse_key_value_pair (key_file, line_start,
695 length - (line_start - line),
699 gchar *line_utf8 = _g_utf8_make_valid (line);
700 g_set_error (error, G_KEY_FILE_ERROR,
701 G_KEY_FILE_ERROR_PARSE,
702 _("Key file contains line '%s' which is not "
703 "a key-value pair, group, or comment"),
711 g_propagate_error (error, parse_error);
715 g_key_file_parse_comment (GKeyFile *key_file,
720 GKeyFileKeyValuePair *pair;
722 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
725 g_assert (key_file->current_group != NULL);
727 pair = g_slice_new (GKeyFileKeyValuePair);
729 pair->value = g_strndup (line, length);
731 key_file->current_group->key_value_pairs =
732 g_list_prepend (key_file->current_group->key_value_pairs, pair);
734 if (length == 0 || line[0] != '#')
735 key_file->current_group->has_trailing_blank_line = TRUE;
739 g_key_file_parse_group (GKeyFile *key_file,
745 const gchar *group_name_start, *group_name_end;
747 /* advance past opening '['
749 group_name_start = line + 1;
750 group_name_end = line + length - 1;
752 while (*group_name_end != ']')
755 group_name = g_strndup (group_name_start,
756 group_name_end - group_name_start);
758 if (!g_key_file_is_group_name (group_name))
760 g_set_error (error, G_KEY_FILE_ERROR,
761 G_KEY_FILE_ERROR_PARSE,
762 _("Invalid group name: %s"), group_name);
767 g_key_file_add_group (key_file, group_name);
772 g_key_file_parse_key_value_pair (GKeyFile *key_file,
777 gchar *key, *value, *key_end, *value_start, *locale;
778 gsize key_len, value_len;
780 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
782 g_set_error (error, G_KEY_FILE_ERROR,
783 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
784 _("Key file does not start with a group"));
788 key_end = value_start = strchr (line, '=');
790 g_assert (key_end != NULL);
795 /* Pull the key name from the line (chomping trailing whitespace)
797 while (g_ascii_isspace (*key_end))
800 key_len = key_end - line + 2;
802 g_assert (key_len <= length);
804 key = g_strndup (line, key_len - 1);
806 if (!g_key_file_is_key_name (key))
808 g_set_error (error, G_KEY_FILE_ERROR,
809 G_KEY_FILE_ERROR_PARSE,
810 _("Invalid key name: %s"), key);
815 /* Pull the value from the line (chugging leading whitespace)
817 while (g_ascii_isspace (*value_start))
820 value_len = line + length - value_start + 1;
822 value = g_strndup (value_start, value_len);
824 g_assert (key_file->start_group != NULL);
826 if (key_file->current_group
827 && key_file->current_group->name
828 && strcmp (key_file->start_group->name,
829 key_file->current_group->name) == 0
830 && strcmp (key, "Encoding") == 0)
832 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
834 gchar *value_utf8 = _g_utf8_make_valid (value);
835 g_set_error (error, G_KEY_FILE_ERROR,
836 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
837 _("Key file contains unsupported "
838 "encoding '%s'"), value_utf8);
847 /* Is this key a translation? If so, is it one that we care about?
849 locale = key_get_locale (key);
851 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
852 g_key_file_add_key (key_file, key_file->current_group, key, value);
860 key_get_locale (const gchar *key)
864 locale = g_strrstr (key, "[");
866 if (locale && strlen (locale) <= 2)
870 locale = g_strndup (locale + 1, strlen (locale) - 2);
876 g_key_file_parse_data (GKeyFile *key_file,
884 g_return_if_fail (key_file != NULL);
885 g_return_if_fail (data != NULL);
889 for (i = 0; i < length; i++)
893 if (i > 0 && data[i - 1] == '\r')
894 g_string_erase (key_file->parse_buffer,
895 key_file->parse_buffer->len - 1,
898 /* When a newline is encountered flush the parse buffer so that the
899 * line can be parsed. Note that completely blank lines won't show
900 * up in the parse buffer, so they get parsed directly.
902 if (key_file->parse_buffer->len > 0)
903 g_key_file_flush_parse_buffer (key_file, &parse_error);
905 g_key_file_parse_comment (key_file, "", 1, &parse_error);
909 g_propagate_error (error, parse_error);
914 g_string_append_c (key_file->parse_buffer, data[i]);
917 key_file->approximate_size += length;
921 g_key_file_flush_parse_buffer (GKeyFile *key_file,
924 GError *file_error = NULL;
926 g_return_if_fail (key_file != NULL);
930 if (key_file->parse_buffer->len > 0)
932 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
933 key_file->parse_buffer->len,
935 g_string_erase (key_file->parse_buffer, 0, -1);
939 g_propagate_error (error, file_error);
946 * g_key_file_to_data:
947 * @key_file: a #GKeyFile
948 * @length: return location for the length of the
949 * returned string, or %NULL
950 * @error: return location for a #GError, or %NULL
952 * This function outputs @key_file as a string.
954 * Return value: a newly allocated string holding
955 * the contents of the #GKeyFile
960 g_key_file_to_data (GKeyFile *key_file,
964 GString *data_string;
965 GList *group_node, *key_file_node;
966 gboolean has_blank_line = TRUE;
968 g_return_val_if_fail (key_file != NULL, NULL);
970 data_string = g_string_sized_new (2 * key_file->approximate_size);
972 for (group_node = g_list_last (key_file->groups);
974 group_node = group_node->prev)
976 GKeyFileGroup *group;
978 group = (GKeyFileGroup *) group_node->data;
980 /* separate groups by at least an empty line */
982 g_string_append_c (data_string, '\n');
983 has_blank_line = group->has_trailing_blank_line;
985 if (group->comment != NULL)
986 g_string_append_printf (data_string, "%s\n", group->comment->value);
988 if (group->name != NULL)
989 g_string_append_printf (data_string, "[%s]\n", group->name);
991 for (key_file_node = g_list_last (group->key_value_pairs);
992 key_file_node != NULL;
993 key_file_node = key_file_node->prev)
995 GKeyFileKeyValuePair *pair;
997 pair = (GKeyFileKeyValuePair *) key_file_node->data;
999 if (pair->key != NULL)
1000 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1002 g_string_append_printf (data_string, "%s\n", pair->value);
1007 *length = data_string->len;
1009 return g_string_free (data_string, FALSE);
1013 * g_key_file_get_keys:
1014 * @key_file: a #GKeyFile
1015 * @group_name: a group name
1016 * @length: return location for the number of keys returned, or %NULL
1017 * @error: return location for a #GError, or %NULL
1019 * Returns all keys for the group name @group_name. The array of
1020 * returned keys will be %NULL-terminated, so @length may
1021 * optionally be %NULL. In the event that the @group_name cannot
1022 * be found, %NULL is returned and @error is set to
1023 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1025 * Return value: a newly-allocated %NULL-terminated array of
1026 * strings. Use g_strfreev() to free it.
1031 g_key_file_get_keys (GKeyFile *key_file,
1032 const gchar *group_name,
1036 GKeyFileGroup *group;
1041 g_return_val_if_fail (key_file != NULL, NULL);
1042 g_return_val_if_fail (group_name != NULL, NULL);
1044 group = g_key_file_lookup_group (key_file, group_name);
1048 g_set_error (error, G_KEY_FILE_ERROR,
1049 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1050 _("Key file does not have group '%s'"),
1051 group_name ? group_name : "(null)");
1056 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1058 GKeyFileKeyValuePair *pair;
1060 pair = (GKeyFileKeyValuePair *) tmp->data;
1066 keys = g_new (gchar *, num_keys + 1);
1069 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1071 GKeyFileKeyValuePair *pair;
1073 pair = (GKeyFileKeyValuePair *) tmp->data;
1077 keys[i] = g_strdup (pair->key);
1082 keys[num_keys] = NULL;
1091 * g_key_file_get_start_group:
1092 * @key_file: a #GKeyFile
1094 * Returns the name of the start group of the file.
1096 * Return value: The start group of the key file.
1101 g_key_file_get_start_group (GKeyFile *key_file)
1103 g_return_val_if_fail (key_file != NULL, NULL);
1105 if (key_file->start_group)
1106 return g_strdup (key_file->start_group->name);
1112 * g_key_file_get_groups:
1113 * @key_file: a #GKeyFile
1114 * @length: return location for the number of returned groups, or %NULL
1116 * Returns all groups in the key file loaded with @key_file. The
1117 * array of returned groups will be %NULL-terminated, so @length may
1118 * optionally be %NULL.
1120 * Return value: a newly-allocated %NULL-terminated array of strings.
1121 * Use g_strfreev() to free it.
1125 g_key_file_get_groups (GKeyFile *key_file,
1130 gsize i, num_groups;
1132 g_return_val_if_fail (key_file != NULL, NULL);
1134 num_groups = g_list_length (key_file->groups);
1136 g_assert (num_groups > 0);
1138 /* Only need num_groups instead of num_groups + 1
1139 * because the first group of the file (last in the
1140 * list) is always the comment group at the top,
1143 groups = g_new (gchar *, num_groups);
1145 group_node = g_list_last (key_file->groups);
1147 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1150 for (group_node = group_node->prev;
1152 group_node = group_node->prev)
1154 GKeyFileGroup *group;
1156 group = (GKeyFileGroup *) group_node->data;
1158 g_assert (group->name != NULL);
1160 groups[i++] = g_strdup (group->name);
1171 * g_key_file_get_value:
1172 * @key_file: a #GKeyFile
1173 * @group_name: a group name
1175 * @error: return location for a #GError, or %NULL
1177 * Returns the value associated with @key under @group_name.
1179 * In the event the key cannot be found, %NULL is returned and
1180 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1181 * event that the @group_name cannot be found, %NULL is returned
1182 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1184 * Return value: a newly allocated string or %NULL if the specified
1185 * key cannot be found.
1190 g_key_file_get_value (GKeyFile *key_file,
1191 const gchar *group_name,
1195 GKeyFileGroup *group;
1196 GKeyFileKeyValuePair *pair;
1197 gchar *value = NULL;
1199 g_return_val_if_fail (key_file != NULL, NULL);
1200 g_return_val_if_fail (group_name != NULL, NULL);
1201 g_return_val_if_fail (key != NULL, NULL);
1203 group = g_key_file_lookup_group (key_file, group_name);
1207 g_set_error (error, G_KEY_FILE_ERROR,
1208 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1209 _("Key file does not have group '%s'"),
1210 group_name ? group_name : "(null)");
1214 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1217 value = g_strdup (pair->value);
1219 g_set_error (error, G_KEY_FILE_ERROR,
1220 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1221 _("Key file does not have key '%s'"), key);
1227 * g_key_file_set_value:
1228 * @key_file: a #GKeyFile
1229 * @group_name: a group name
1233 * Associates a new value with @key under @group_name. If @key
1234 * cannot be found then it is created. If @group_name cannot be
1235 * found then it is created.
1240 g_key_file_set_value (GKeyFile *key_file,
1241 const gchar *group_name,
1245 GKeyFileGroup *group;
1246 GKeyFileKeyValuePair *pair;
1248 g_return_if_fail (key_file != NULL);
1249 g_return_if_fail (g_key_file_is_group_name (group_name));
1250 g_return_if_fail (g_key_file_is_key_name (key));
1251 g_return_if_fail (value != NULL);
1253 group = g_key_file_lookup_group (key_file, group_name);
1257 g_key_file_add_group (key_file, group_name);
1258 group = (GKeyFileGroup *) key_file->groups->data;
1260 g_key_file_add_key (key_file, group, key, value);
1264 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1267 g_key_file_add_key (key_file, group, key, value);
1270 g_free (pair->value);
1271 pair->value = g_strdup (value);
1277 * g_key_file_get_string:
1278 * @key_file: a #GKeyFile
1279 * @group_name: a group name
1281 * @error: return location for a #GError, or %NULL
1283 * Returns the value associated with @key under @group_name.
1285 * In the event the key cannot be found, %NULL is returned and
1286 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1287 * event that the @group_name cannot be found, %NULL is returned
1288 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1290 * Return value: a newly allocated string or %NULL if the specified
1291 * key cannot be found.
1296 g_key_file_get_string (GKeyFile *key_file,
1297 const gchar *group_name,
1301 gchar *value, *string_value;
1302 GError *key_file_error;
1304 g_return_val_if_fail (key_file != NULL, NULL);
1305 g_return_val_if_fail (group_name != NULL, NULL);
1306 g_return_val_if_fail (key != NULL, NULL);
1308 key_file_error = NULL;
1310 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1314 g_propagate_error (error, key_file_error);
1318 if (!g_utf8_validate (value, -1, NULL))
1320 gchar *value_utf8 = _g_utf8_make_valid (value);
1321 g_set_error (error, G_KEY_FILE_ERROR,
1322 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1323 _("Key file contains key '%s' with value '%s' "
1324 "which is not UTF-8"), key, value_utf8);
1325 g_free (value_utf8);
1331 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1337 if (g_error_matches (key_file_error,
1339 G_KEY_FILE_ERROR_INVALID_VALUE))
1341 g_set_error (error, G_KEY_FILE_ERROR,
1342 G_KEY_FILE_ERROR_INVALID_VALUE,
1343 _("Key file contains key '%s' "
1344 "which has value that cannot be interpreted."),
1346 g_error_free (key_file_error);
1349 g_propagate_error (error, key_file_error);
1352 return string_value;
1356 * g_key_file_set_string:
1357 * @key_file: a #GKeyFile
1358 * @group_name: a group name
1362 * Associates a new string value with @key under @group_name. If
1363 * @key cannot be found then it is created. If @group_name
1364 * cannot be found then it is created.
1369 g_key_file_set_string (GKeyFile *key_file,
1370 const gchar *group_name,
1372 const gchar *string)
1376 g_return_if_fail (key_file != NULL);
1377 g_return_if_fail (string != NULL);
1379 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1380 g_key_file_set_value (key_file, group_name, key, value);
1385 * g_key_file_get_string_list:
1386 * @key_file: a #GKeyFile
1387 * @group_name: a group name
1389 * @length: return location for the number of returned strings, or %NULL
1390 * @error: return location for a #GError, or %NULL
1392 * Returns the values associated with @key under @group_name.
1394 * In the event the key cannot be found, %NULL is returned and
1395 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1396 * event that the @group_name cannot be found, %NULL is returned
1397 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1399 * Return value: a %NULL-terminated string array or %NULL if the specified
1400 * key cannot be found. The array should be freed with g_strfreev().
1405 g_key_file_get_string_list (GKeyFile *key_file,
1406 const gchar *group_name,
1411 GError *key_file_error = NULL;
1412 gchar *value, *string_value, **values;
1414 GSList *p, *pieces = NULL;
1416 g_return_val_if_fail (key_file != NULL, NULL);
1417 g_return_val_if_fail (group_name != NULL, NULL);
1418 g_return_val_if_fail (key != NULL, NULL);
1420 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1424 g_propagate_error (error, key_file_error);
1428 if (!g_utf8_validate (value, -1, NULL))
1430 gchar *value_utf8 = _g_utf8_make_valid (value);
1431 g_set_error (error, G_KEY_FILE_ERROR,
1432 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1433 _("Key file contains key '%s' with value '%s' "
1434 "which is not UTF-8"), key, value_utf8);
1435 g_free (value_utf8);
1441 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1443 g_free (string_value);
1447 if (g_error_matches (key_file_error,
1449 G_KEY_FILE_ERROR_INVALID_VALUE))
1451 g_set_error (error, G_KEY_FILE_ERROR,
1452 G_KEY_FILE_ERROR_INVALID_VALUE,
1453 _("Key file contains key '%s' "
1454 "which has value that cannot be interpreted."),
1456 g_error_free (key_file_error);
1459 g_propagate_error (error, key_file_error);
1462 len = g_slist_length (pieces);
1463 values = g_new (gchar *, len + 1);
1464 for (p = pieces, i = 0; p; p = p->next)
1465 values[i++] = p->data;
1468 g_slist_free (pieces);
1477 * g_key_file_set_string_list:
1478 * @key_file: a #GKeyFile
1479 * @group_name: a group name
1481 * @list: an array of locale string values
1482 * @length: number of locale string values in @list
1484 * Associates a list of string values for @key under @group_name.
1485 * If @key cannot be found then it is created. If @group_name
1486 * cannot be found then it is created.
1491 g_key_file_set_string_list (GKeyFile *key_file,
1492 const gchar *group_name,
1494 const gchar * const list[],
1497 GString *value_list;
1500 g_return_if_fail (key_file != NULL);
1501 g_return_if_fail (list != NULL);
1503 value_list = g_string_sized_new (length * 128);
1504 for (i = 0; i < length && list[i] != NULL; i++)
1508 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1509 g_string_append (value_list, value);
1510 g_string_append_c (value_list, key_file->list_separator);
1515 g_key_file_set_value (key_file, group_name, key, value_list->str);
1516 g_string_free (value_list, TRUE);
1520 * g_key_file_set_locale_string:
1521 * @key_file: a #GKeyFile
1522 * @group_name: a group name
1527 * Associates a string value for @key and @locale under
1528 * @group_name. If the translation for @key cannot be found
1529 * then it is created.
1534 g_key_file_set_locale_string (GKeyFile *key_file,
1535 const gchar *group_name,
1537 const gchar *locale,
1538 const gchar *string)
1540 gchar *full_key, *value;
1542 g_return_if_fail (key_file != NULL);
1543 g_return_if_fail (key != NULL);
1544 g_return_if_fail (locale != NULL);
1545 g_return_if_fail (string != NULL);
1547 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1548 full_key = g_strdup_printf ("%s[%s]", key, locale);
1549 g_key_file_set_value (key_file, group_name, full_key, value);
1554 extern GSList *_g_compute_locale_variants (const gchar *locale);
1557 * g_key_file_get_locale_string:
1558 * @key_file: a #GKeyFile
1559 * @group_name: a group name
1561 * @locale: a locale or %NULL
1562 * @error: return location for a #GError, or %NULL
1564 * Returns the value associated with @key under @group_name
1565 * translated in the given @locale if available. If @locale is
1566 * %NULL then the current locale is assumed.
1568 * If @key cannot be found then %NULL is returned and @error is set to
1569 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1570 * with @key cannot be interpreted or no suitable translation can
1571 * be found then the untranslated value is returned.
1573 * Return value: a newly allocated string or %NULL if the specified
1574 * key cannot be found.
1579 g_key_file_get_locale_string (GKeyFile *key_file,
1580 const gchar *group_name,
1582 const gchar *locale,
1585 gchar *candidate_key, *translated_value;
1586 GError *key_file_error;
1588 gboolean free_languages = FALSE;
1591 g_return_val_if_fail (key_file != NULL, NULL);
1592 g_return_val_if_fail (group_name != NULL, NULL);
1593 g_return_val_if_fail (key != NULL, NULL);
1595 candidate_key = NULL;
1596 translated_value = NULL;
1597 key_file_error = NULL;
1603 list = _g_compute_locale_variants (locale);
1605 languages = g_new (gchar *, g_slist_length (list) + 1);
1606 for (l = list, i = 0; l; l = l->next, i++)
1607 languages[i] = l->data;
1608 languages[i] = NULL;
1610 g_slist_free (list);
1611 free_languages = TRUE;
1615 languages = (gchar **) g_get_language_names ();
1616 free_languages = FALSE;
1619 for (i = 0; languages[i]; i++)
1621 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1623 translated_value = g_key_file_get_string (key_file,
1625 candidate_key, NULL);
1626 g_free (candidate_key);
1628 if (translated_value)
1631 g_free (translated_value);
1632 translated_value = NULL;
1635 /* Fallback to untranslated key
1637 if (!translated_value)
1639 translated_value = g_key_file_get_string (key_file, group_name, key,
1642 if (!translated_value)
1643 g_propagate_error (error, key_file_error);
1647 g_strfreev (languages);
1649 return translated_value;
1653 * g_key_file_get_locale_string_list:
1654 * @key_file: a #GKeyFile
1655 * @group_name: a group name
1658 * @length: return location for the number of returned strings or %NULL
1659 * @error: return location for a #GError or %NULL
1661 * Returns the values associated with @key under @group_name
1662 * translated in the given @locale if available. If @locale is
1663 * %NULL then the current locale is assumed.
1665 * If @key cannot be found then %NULL is returned and @error is set to
1666 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1667 * with @key cannot be interpreted or no suitable translations
1668 * can be found then the untranslated values are returned.
1669 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1671 * Return value: a newly allocated %NULL-terminated string array
1672 * or %NULL if the key isn't found. The string array should be freed
1673 * with g_strfreev().
1678 g_key_file_get_locale_string_list (GKeyFile *key_file,
1679 const gchar *group_name,
1681 const gchar *locale,
1685 GError *key_file_error;
1686 gchar **values, *value;
1688 g_return_val_if_fail (key_file != NULL, NULL);
1689 g_return_val_if_fail (group_name != NULL, NULL);
1690 g_return_val_if_fail (key != NULL, NULL);
1692 key_file_error = NULL;
1694 value = g_key_file_get_locale_string (key_file, group_name,
1699 g_propagate_error (error, key_file_error);
1704 if (value[strlen (value) - 1] == ';')
1705 value[strlen (value) - 1] = '\0';
1707 values = g_strsplit (value, ";", 0);
1712 *length = g_strv_length (values);
1718 * g_key_file_set_locale_string_list:
1719 * @key_file: a #GKeyFile
1720 * @group_name: a group name
1723 * @list: a %NULL-terminated array of locale string values
1724 * @length: the length of @list
1726 * Associates a list of string values for @key and @locale under
1727 * @group_name. If the translation for @key cannot be found then
1733 g_key_file_set_locale_string_list (GKeyFile *key_file,
1734 const gchar *group_name,
1736 const gchar *locale,
1737 const gchar * const list[],
1740 GString *value_list;
1744 g_return_if_fail (key_file != NULL);
1745 g_return_if_fail (key != NULL);
1746 g_return_if_fail (locale != NULL);
1747 g_return_if_fail (length != 0);
1749 value_list = g_string_sized_new (length * 128);
1750 for (i = 0; i < length && list[i] != NULL; i++)
1754 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1756 g_string_append (value_list, value);
1757 g_string_append_c (value_list, ';');
1762 full_key = g_strdup_printf ("%s[%s]", key, locale);
1763 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1765 g_string_free (value_list, TRUE);
1769 * g_key_file_get_boolean:
1770 * @key_file: a #GKeyFile
1771 * @group_name: a group name
1773 * @error: return location for a #GError
1775 * Returns the value associated with @key under @group_name as a
1778 * If @key cannot be found then the return value is undefined and
1779 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1780 * the value associated with @key cannot be interpreted as a boolean
1781 * then the return value is also undefined and @error is set to
1782 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1784 * Return value: the value associated with the key as a boolean
1788 g_key_file_get_boolean (GKeyFile *key_file,
1789 const gchar *group_name,
1793 GError *key_file_error = NULL;
1795 gboolean bool_value;
1797 g_return_val_if_fail (key_file != NULL, FALSE);
1798 g_return_val_if_fail (group_name != NULL, FALSE);
1799 g_return_val_if_fail (key != NULL, FALSE);
1801 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1805 g_propagate_error (error, key_file_error);
1809 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1815 if (g_error_matches (key_file_error,
1817 G_KEY_FILE_ERROR_INVALID_VALUE))
1819 g_set_error (error, G_KEY_FILE_ERROR,
1820 G_KEY_FILE_ERROR_INVALID_VALUE,
1821 _("Key file contains key '%s' "
1822 "which has value that cannot be interpreted."),
1824 g_error_free (key_file_error);
1827 g_propagate_error (error, key_file_error);
1834 * g_key_file_set_boolean:
1835 * @key_file: a #GKeyFile
1836 * @group_name: a group name
1838 * @value: %TRUE or %FALSE
1840 * Associates a new boolean value with @key under @group_name.
1841 * If @key cannot be found then it is created.
1846 g_key_file_set_boolean (GKeyFile *key_file,
1847 const gchar *group_name,
1853 g_return_if_fail (key_file != NULL);
1855 result = g_key_file_parse_boolean_as_value (key_file, value);
1856 g_key_file_set_value (key_file, group_name, key, result);
1861 * g_key_file_get_boolean_list:
1862 * @key_file: a #GKeyFile
1863 * @group_name: a group name
1865 * @length: the number of booleans returned
1866 * @error: return location for a #GError
1868 * Returns the values associated with @key under @group_name as
1869 * booleans. If @group_name is %NULL, the start_group is used.
1871 * If @key cannot be found then the return value is undefined and
1872 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1873 * the values associated with @key cannot be interpreted as booleans
1874 * then the return value is also undefined and @error is set to
1875 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1877 * Return value: the values associated with the key as a boolean
1882 g_key_file_get_boolean_list (GKeyFile *key_file,
1883 const gchar *group_name,
1888 GError *key_file_error;
1890 gboolean *bool_values;
1893 g_return_val_if_fail (key_file != NULL, NULL);
1894 g_return_val_if_fail (group_name != NULL, NULL);
1895 g_return_val_if_fail (key != NULL, NULL);
1897 key_file_error = NULL;
1899 values = g_key_file_get_string_list (key_file, group_name, key,
1900 &num_bools, &key_file_error);
1903 g_propagate_error (error, key_file_error);
1908 bool_values = g_new (gboolean, num_bools);
1910 for (i = 0; i < num_bools; i++)
1912 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1918 g_propagate_error (error, key_file_error);
1919 g_strfreev (values);
1920 g_free (bool_values);
1925 g_strfreev (values);
1928 *length = num_bools;
1934 * g_key_file_set_boolean_list:
1935 * @key_file: a #GKeyFile
1936 * @group_name: a group name
1938 * @list: an array of boolean values
1939 * @length: length of @list
1941 * Associates a list of boolean values with @key under
1942 * @group_name. If @key cannot be found then it is created.
1943 * If @group_name is %NULL, the start_group is used.
1948 g_key_file_set_boolean_list (GKeyFile *key_file,
1949 const gchar *group_name,
1954 GString *value_list;
1957 g_return_if_fail (key_file != NULL);
1958 g_return_if_fail (list != NULL);
1960 value_list = g_string_sized_new (length * 8);
1961 for (i = 0; i < length; i++)
1965 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1967 g_string_append (value_list, value);
1968 g_string_append_c (value_list, key_file->list_separator);
1973 g_key_file_set_value (key_file, group_name, key, value_list->str);
1974 g_string_free (value_list, TRUE);
1978 * g_key_file_get_integer:
1979 * @key_file: a #GKeyFile
1980 * @group_name: a group name
1982 * @error: return location for a #GError
1984 * Returns the value associated with @key under @group_name as an
1985 * integer. If @group_name is %NULL, the start_group is used.
1987 * If @key cannot be found then the return value is undefined and
1988 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1989 * the value associated with @key cannot be interpreted as an integer
1990 * then the return value is also undefined and @error is set to
1991 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1993 * Return value: the value associated with the key as an integer.
1998 g_key_file_get_integer (GKeyFile *key_file,
1999 const gchar *group_name,
2003 GError *key_file_error;
2007 g_return_val_if_fail (key_file != NULL, -1);
2008 g_return_val_if_fail (group_name != NULL, -1);
2009 g_return_val_if_fail (key != NULL, -1);
2011 key_file_error = NULL;
2013 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2017 g_propagate_error (error, key_file_error);
2021 int_value = g_key_file_parse_value_as_integer (key_file, value,
2027 if (g_error_matches (key_file_error,
2029 G_KEY_FILE_ERROR_INVALID_VALUE))
2031 g_set_error (error, G_KEY_FILE_ERROR,
2032 G_KEY_FILE_ERROR_INVALID_VALUE,
2033 _("Key file contains key '%s' in group '%s' "
2034 "which has value that cannot be interpreted."), key,
2036 g_error_free (key_file_error);
2039 g_propagate_error (error, key_file_error);
2046 * g_key_file_set_integer:
2047 * @key_file: a #GKeyFile
2048 * @group_name: a group name
2050 * @value: an integer value
2052 * Associates a new integer value with @key under @group_name.
2053 * If @key cannot be found then it is created.
2058 g_key_file_set_integer (GKeyFile *key_file,
2059 const gchar *group_name,
2065 g_return_if_fail (key_file != NULL);
2067 result = g_key_file_parse_integer_as_value (key_file, value);
2068 g_key_file_set_value (key_file, group_name, key, result);
2073 * g_key_file_get_integer_list:
2074 * @key_file: a #GKeyFile
2075 * @group_name: a group name
2077 * @length: the number of integers returned
2078 * @error: return location for a #GError
2080 * Returns the values associated with @key under @group_name as
2083 * If @key cannot be found then the return value is undefined and
2084 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2085 * the values associated with @key cannot be interpreted as integers
2086 * then the return value is also undefined and @error is set to
2087 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2089 * Return value: the values associated with the key as a integer
2094 g_key_file_get_integer_list (GKeyFile *key_file,
2095 const gchar *group_name,
2100 GError *key_file_error = NULL;
2105 g_return_val_if_fail (key_file != NULL, NULL);
2106 g_return_val_if_fail (group_name != NULL, NULL);
2107 g_return_val_if_fail (key != NULL, NULL);
2109 values = g_key_file_get_string_list (key_file, group_name, key,
2110 &num_ints, &key_file_error);
2113 g_propagate_error (error, key_file_error);
2118 int_values = g_new (gint, num_ints);
2120 for (i = 0; i < num_ints; i++)
2122 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2128 g_propagate_error (error, key_file_error);
2129 g_strfreev (values);
2130 g_free (int_values);
2135 g_strfreev (values);
2144 * g_key_file_set_integer_list:
2145 * @key_file: a #GKeyFile
2146 * @group_name: a group name
2148 * @list: an array of integer values
2149 * @length: number of integer values in @list
2151 * Associates a list of integer values with @key under
2152 * @group_name. If @key cannot be found then it is created.
2157 g_key_file_set_integer_list (GKeyFile *key_file,
2158 const gchar *group_name,
2166 g_return_if_fail (key_file != NULL);
2167 g_return_if_fail (list != NULL);
2169 values = g_string_sized_new (length * 16);
2170 for (i = 0; i < length; i++)
2174 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2176 g_string_append (values, value);
2177 g_string_append_c (values, ';');
2182 g_key_file_set_value (key_file, group_name, key, values->str);
2183 g_string_free (values, TRUE);
2187 * g_key_file_get_double:
2188 * @key_file: a #GKeyFile
2189 * @group_name: a group name
2191 * @error: return location for a #GError
2193 * Returns the value associated with @key under @group_name as a
2194 * double. If @group_name is %NULL, the start_group is used.
2196 * If @key cannot be found then the return value is undefined and
2197 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2198 * the value associated with @key cannot be interpreted as a double
2199 * then the return value is also undefined and @error is set to
2200 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2202 * Return value: the value associated with the key as a double.
2207 g_key_file_get_double (GKeyFile *key_file,
2208 const gchar *group_name,
2212 GError *key_file_error;
2214 gdouble double_value;
2216 g_return_val_if_fail (key_file != NULL, -1);
2217 g_return_val_if_fail (group_name != NULL, -1);
2218 g_return_val_if_fail (key != NULL, -1);
2220 key_file_error = NULL;
2222 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2226 g_propagate_error (error, key_file_error);
2230 double_value = g_key_file_parse_value_as_double (key_file, value,
2236 if (g_error_matches (key_file_error,
2238 G_KEY_FILE_ERROR_INVALID_VALUE))
2240 g_set_error (error, G_KEY_FILE_ERROR,
2241 G_KEY_FILE_ERROR_INVALID_VALUE,
2242 _("Key file contains key '%s' in group '%s' "
2243 "which has value that cannot be interpreted."), key,
2245 g_error_free (key_file_error);
2248 g_propagate_error (error, key_file_error);
2251 return double_value;
2255 * g_key_file_set_double:
2256 * @key_file: a #GKeyFile
2257 * @group_name: a group name
2259 * @value: an double value
2261 * Associates a new double value with @key under @group_name.
2262 * If @key cannot be found then it is created. If @group_name
2263 * is %NULL, the start group is used.
2268 g_key_file_set_double (GKeyFile *key_file,
2269 const gchar *group_name,
2273 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2275 g_return_if_fail (key_file != NULL);
2277 g_ascii_dtostr (result, sizeof (result), value);
2278 g_key_file_set_value (key_file, group_name, key, result);
2282 * g_key_file_get_double_list:
2283 * @key_file: a #GKeyFile
2284 * @group_name: a group name
2286 * @length: the number of doubles returned
2287 * @error: return location for a #GError
2289 * Returns the values associated with @key under @group_name as
2290 * doubles. If @group_name is %NULL, the start group is used.
2292 * If @key cannot be found then the return value is undefined and
2293 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2294 * the values associated with @key cannot be interpreted as doubles
2295 * then the return value is also undefined and @error is set to
2296 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2298 * Return value: the values associated with the key as a double
2303 g_key_file_get_double_list (GKeyFile *key_file,
2304 const gchar *group_name,
2309 GError *key_file_error = NULL;
2311 gdouble *double_values;
2312 gsize i, num_doubles;
2314 g_return_val_if_fail (key_file != NULL, NULL);
2315 g_return_val_if_fail (group_name != NULL, NULL);
2316 g_return_val_if_fail (key != NULL, NULL);
2318 values = g_key_file_get_string_list (key_file, group_name, key,
2319 &num_doubles, &key_file_error);
2322 g_propagate_error (error, key_file_error);
2327 double_values = g_new (gdouble, num_doubles);
2329 for (i = 0; i < num_doubles; i++)
2331 double_values[i] = g_key_file_parse_value_as_double (key_file,
2337 g_propagate_error (error, key_file_error);
2338 g_strfreev (values);
2339 g_free (double_values);
2344 g_strfreev (values);
2347 *length = num_doubles;
2349 return double_values;
2353 * g_key_file_set_double_list:
2354 * @key_file: a #GKeyFile
2355 * @group_name: a group name
2357 * @list: an array of double values
2358 * @length: number of double values in @list
2360 * Associates a list of double values with @key under
2361 * @group_name. If @key cannot be found then it is created.
2362 * If @group_name is %NULL the start group is used.
2367 g_key_file_set_double_list (GKeyFile *key_file,
2368 const gchar *group_name,
2376 g_return_if_fail (key_file != NULL);
2377 g_return_if_fail (list != NULL);
2379 values = g_string_sized_new (length * 16);
2380 for (i = 0; i < length; i++)
2382 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2384 g_ascii_dtostr( result, sizeof (result), list[i] );
2386 g_string_append (values, result);
2387 g_string_append_c (values, ';');
2390 g_key_file_set_value (key_file, group_name, key, values->str);
2391 g_string_free (values, TRUE);
2395 g_key_file_set_key_comment (GKeyFile *key_file,
2396 const gchar *group_name,
2398 const gchar *comment,
2401 GKeyFileGroup *group;
2402 GKeyFileKeyValuePair *pair;
2403 GList *key_node, *comment_node, *tmp;
2405 group = g_key_file_lookup_group (key_file, group_name);
2408 g_set_error (error, G_KEY_FILE_ERROR,
2409 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2410 _("Key file does not have group '%s'"),
2411 group_name ? group_name : "(null)");
2416 /* First find the key the comments are supposed to be
2419 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2421 if (key_node == NULL)
2423 g_set_error (error, G_KEY_FILE_ERROR,
2424 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2425 _("Key file does not have key '%s' in group '%s'"),
2430 /* Then find all the comments already associated with the
2433 tmp = key_node->next;
2436 GKeyFileKeyValuePair *pair;
2438 pair = (GKeyFileKeyValuePair *) tmp->data;
2440 if (pair->key != NULL)
2445 g_key_file_remove_key_value_pair_node (key_file, group,
2449 if (comment == NULL)
2452 /* Now we can add our new comment
2454 pair = g_slice_new (GKeyFileKeyValuePair);
2456 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2458 key_node = g_list_insert (key_node, pair, 1);
2462 g_key_file_set_group_comment (GKeyFile *key_file,
2463 const gchar *group_name,
2464 const gchar *comment,
2467 GKeyFileGroup *group;
2469 g_return_if_fail (g_key_file_is_group_name (group_name));
2471 group = g_key_file_lookup_group (key_file, group_name);
2474 g_set_error (error, G_KEY_FILE_ERROR,
2475 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2476 _("Key file does not have group '%s'"),
2477 group_name ? group_name : "(null)");
2482 /* First remove any existing comment
2486 g_key_file_key_value_pair_free (group->comment);
2487 group->comment = NULL;
2490 if (comment == NULL)
2493 /* Now we can add our new comment
2495 group->comment = g_slice_new (GKeyFileKeyValuePair);
2496 group->comment->key = NULL;
2497 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2501 g_key_file_set_top_comment (GKeyFile *key_file,
2502 const gchar *comment,
2506 GKeyFileGroup *group;
2507 GKeyFileKeyValuePair *pair;
2509 /* The last group in the list should be the top (comments only)
2512 g_assert (key_file->groups != NULL);
2513 group_node = g_list_last (key_file->groups);
2514 group = (GKeyFileGroup *) group_node->data;
2515 g_assert (group->name == NULL);
2517 /* Note all keys must be comments at the top of
2518 * the file, so we can just free it all.
2520 if (group->key_value_pairs != NULL)
2522 g_list_foreach (group->key_value_pairs,
2523 (GFunc) g_key_file_key_value_pair_free,
2525 g_list_free (group->key_value_pairs);
2526 group->key_value_pairs = NULL;
2529 if (comment == NULL)
2532 pair = g_slice_new (GKeyFileKeyValuePair);
2534 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2536 group->key_value_pairs =
2537 g_list_prepend (group->key_value_pairs, pair);
2541 * g_key_file_set_comment:
2542 * @key_file: a #GKeyFile
2543 * @group_name: a group name, or %NULL
2545 * @comment: a comment
2546 * @error: return location for a #GError
2548 * Places a comment above @key from @group_name.
2549 * @group_name. If @key is %NULL then @comment will
2550 * be written above @group_name. If both @key
2551 * and @group_name are NULL, then @comment will
2552 * be written above the first group in the file.
2557 g_key_file_set_comment (GKeyFile *key_file,
2558 const gchar *group_name,
2560 const gchar *comment,
2563 g_return_if_fail (key_file != NULL);
2565 if (group_name != NULL && key != NULL)
2566 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2567 else if (group_name != NULL)
2568 g_key_file_set_group_comment (key_file, group_name, comment, error);
2570 g_key_file_set_top_comment (key_file, comment, error);
2572 if (comment != NULL)
2573 key_file->approximate_size += strlen (comment);
2577 g_key_file_get_key_comment (GKeyFile *key_file,
2578 const gchar *group_name,
2582 GKeyFileGroup *group;
2583 GKeyFileKeyValuePair *pair;
2584 GList *key_node, *tmp;
2588 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2590 group = g_key_file_lookup_group (key_file, group_name);
2593 g_set_error (error, G_KEY_FILE_ERROR,
2594 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2595 _("Key file does not have group '%s'"),
2596 group_name ? group_name : "(null)");
2601 /* First find the key the comments are supposed to be
2604 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2606 if (key_node == NULL)
2608 g_set_error (error, G_KEY_FILE_ERROR,
2609 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2610 _("Key file does not have key '%s' in group '%s'"),
2617 /* Then find all the comments already associated with the
2618 * key and concatentate them.
2620 tmp = key_node->next;
2621 if (!key_node->next)
2624 pair = (GKeyFileKeyValuePair *) tmp->data;
2625 if (pair->key != NULL)
2630 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2632 if (pair->key != NULL)
2638 while (tmp != key_node)
2640 GKeyFileKeyValuePair *pair;
2642 pair = (GKeyFileKeyValuePair *) tmp->data;
2645 string = g_string_sized_new (512);
2647 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2648 g_string_append (string, comment);
2656 comment = string->str;
2657 g_string_free (string, FALSE);
2666 get_group_comment (GKeyFile *key_file,
2667 GKeyFileGroup *group,
2676 tmp = group->key_value_pairs;
2679 GKeyFileKeyValuePair *pair;
2681 pair = (GKeyFileKeyValuePair *) tmp->data;
2683 if (pair->key != NULL)
2689 if (tmp->next == NULL)
2697 GKeyFileKeyValuePair *pair;
2699 pair = (GKeyFileKeyValuePair *) tmp->data;
2702 string = g_string_sized_new (512);
2704 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2705 g_string_append (string, comment);
2712 return g_string_free (string, FALSE);
2718 g_key_file_get_group_comment (GKeyFile *key_file,
2719 const gchar *group_name,
2723 GKeyFileGroup *group;
2725 group_node = g_key_file_lookup_group_node (key_file, group_name);
2728 g_set_error (error, G_KEY_FILE_ERROR,
2729 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2730 _("Key file does not have group '%s'"),
2731 group_name ? group_name : "(null)");
2736 group = (GKeyFileGroup *)group_node->data;
2738 return g_strdup (group->comment->value);
2740 group_node = group_node->next;
2741 group = (GKeyFileGroup *)group_node->data;
2742 return get_group_comment (key_file, group, error);
2746 g_key_file_get_top_comment (GKeyFile *key_file,
2750 GKeyFileGroup *group;
2752 /* The last group in the list should be the top (comments only)
2755 g_assert (key_file->groups != NULL);
2756 group_node = g_list_last (key_file->groups);
2757 group = (GKeyFileGroup *) group_node->data;
2758 g_assert (group->name == NULL);
2760 return get_group_comment (key_file, group, error);
2764 * g_key_file_get_comment:
2765 * @key_file: a #GKeyFile
2766 * @group_name: a group name, or %NULL
2768 * @error: return location for a #GError
2770 * Retrieves a comment above @key from @group_name.
2771 * @group_name. If @key is %NULL then @comment will
2772 * be read from above @group_name. If both @key
2773 * and @group_name are NULL, then @comment will
2774 * be read from above the first group in the file.
2776 * Returns: a comment that should be freed with g_free()
2781 g_key_file_get_comment (GKeyFile *key_file,
2782 const gchar *group_name,
2786 g_return_val_if_fail (key_file != NULL, NULL);
2788 if (group_name != NULL && key != NULL)
2789 return g_key_file_get_key_comment (key_file, group_name, key, error);
2790 else if (group_name != NULL)
2791 return g_key_file_get_group_comment (key_file, group_name, error);
2793 return g_key_file_get_top_comment (key_file, error);
2797 * g_key_file_remove_comment:
2798 * @key_file: a #GKeyFile
2799 * @group_name: a group name, or %NULL
2801 * @error: return location for a #GError
2803 * Removes a comment above @key from @group_name.
2804 * @group_name. If @key is %NULL then @comment will
2805 * be written above @group_name. If both @key
2806 * and @group_name are NULL, then @comment will
2807 * be written above the first group in the file.
2813 g_key_file_remove_comment (GKeyFile *key_file,
2814 const gchar *group_name,
2818 g_return_if_fail (key_file != NULL);
2820 if (group_name != NULL && key != NULL)
2821 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2822 else if (group_name != NULL)
2823 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2825 g_key_file_set_top_comment (key_file, NULL, error);
2829 * g_key_file_has_group:
2830 * @key_file: a #GKeyFile
2831 * @group_name: a group name
2833 * Looks whether the key file has the group @group_name.
2835 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2840 g_key_file_has_group (GKeyFile *key_file,
2841 const gchar *group_name)
2843 g_return_val_if_fail (key_file != NULL, FALSE);
2844 g_return_val_if_fail (group_name != NULL, FALSE);
2846 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2850 * g_key_file_has_key:
2851 * @key_file: a #GKeyFile
2852 * @group_name: a group name
2854 * @error: return location for a #GError
2856 * Looks whether the key file has the key @key in the group
2859 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2865 g_key_file_has_key (GKeyFile *key_file,
2866 const gchar *group_name,
2870 GKeyFileKeyValuePair *pair;
2871 GKeyFileGroup *group;
2873 g_return_val_if_fail (key_file != NULL, FALSE);
2874 g_return_val_if_fail (group_name != NULL, FALSE);
2875 g_return_val_if_fail (key != NULL, FALSE);
2877 group = g_key_file_lookup_group (key_file, group_name);
2881 g_set_error (error, G_KEY_FILE_ERROR,
2882 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2883 _("Key file does not have group '%s'"),
2884 group_name ? group_name : "(null)");
2889 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2891 return pair != NULL;
2895 g_key_file_add_group (GKeyFile *key_file,
2896 const gchar *group_name)
2898 GKeyFileGroup *group;
2900 g_return_if_fail (key_file != NULL);
2901 g_return_if_fail (g_key_file_is_group_name (group_name));
2903 group = g_key_file_lookup_group (key_file, group_name);
2906 key_file->current_group = group;
2910 group = g_slice_new0 (GKeyFileGroup);
2911 group->name = g_strdup (group_name);
2912 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2913 key_file->groups = g_list_prepend (key_file->groups, group);
2914 key_file->approximate_size += strlen (group_name) + 3;
2915 key_file->current_group = group;
2917 if (key_file->start_group == NULL)
2918 key_file->start_group = group;
2922 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2927 g_free (pair->value);
2928 g_slice_free (GKeyFileKeyValuePair, pair);
2932 /* Be careful not to call this function on a node with data in the
2933 * lookup map without removing it from the lookup map, first.
2935 * Some current cases where this warning is not a concern are
2937 * - the node being removed is a comment node
2938 * - the entire lookup map is getting destroyed soon after
2942 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2943 GKeyFileGroup *group,
2947 GKeyFileKeyValuePair *pair;
2949 pair = (GKeyFileKeyValuePair *) pair_node->data;
2951 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2953 if (pair->key != NULL)
2954 key_file->approximate_size -= strlen (pair->key) + 1;
2956 g_assert (pair->value != NULL);
2957 key_file->approximate_size -= strlen (pair->value);
2959 g_key_file_key_value_pair_free (pair);
2961 g_list_free_1 (pair_node);
2965 g_key_file_remove_group_node (GKeyFile *key_file,
2968 GKeyFileGroup *group;
2971 group = (GKeyFileGroup *) group_node->data;
2973 /* If the current group gets deleted make the current group the last
2976 if (key_file->current_group == group)
2978 /* groups should always contain at least the top comment group,
2979 * unless g_key_file_clear has been called
2981 if (key_file->groups)
2982 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2984 key_file->current_group = NULL;
2987 /* If the start group gets deleted make the start group the first
2990 if (key_file->start_group == group)
2992 tmp = g_list_last (key_file->groups);
2995 if (tmp != group_node &&
2996 ((GKeyFileGroup *) tmp->data)->name != NULL)
3003 key_file->start_group = (GKeyFileGroup *) tmp->data;
3005 key_file->start_group = NULL;
3008 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3010 if (group->name != NULL)
3011 key_file->approximate_size -= strlen (group->name) + 3;
3013 tmp = group->key_value_pairs;
3020 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3023 g_assert (group->key_value_pairs == NULL);
3025 if (group->lookup_map)
3027 g_hash_table_destroy (group->lookup_map);
3028 group->lookup_map = NULL;
3031 g_free ((gchar *) group->name);
3032 g_slice_free (GKeyFileGroup, group);
3033 g_list_free_1 (group_node);
3037 * g_key_file_remove_group:
3038 * @key_file: a #GKeyFile
3039 * @group_name: a group name
3040 * @error: return location for a #GError or %NULL
3042 * Removes the specified group, @group_name,
3043 * from the key file.
3048 g_key_file_remove_group (GKeyFile *key_file,
3049 const gchar *group_name,
3054 g_return_if_fail (key_file != NULL);
3055 g_return_if_fail (group_name != NULL);
3057 group_node = g_key_file_lookup_group_node (key_file, group_name);
3061 g_set_error (error, G_KEY_FILE_ERROR,
3062 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3063 _("Key file does not have group '%s'"),
3068 g_key_file_remove_group_node (key_file, group_node);
3072 g_key_file_add_key (GKeyFile *key_file,
3073 GKeyFileGroup *group,
3077 GKeyFileKeyValuePair *pair;
3079 pair = g_slice_new (GKeyFileKeyValuePair);
3080 pair->key = g_strdup (key);
3081 pair->value = g_strdup (value);
3083 g_hash_table_replace (group->lookup_map, pair->key, pair);
3084 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3085 group->has_trailing_blank_line = FALSE;
3086 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3090 * g_key_file_remove_key:
3091 * @key_file: a #GKeyFile
3092 * @group_name: a group name
3093 * @key: a key name to remove
3094 * @error: return location for a #GError or %NULL
3096 * Removes @key in @group_name from the key file.
3101 g_key_file_remove_key (GKeyFile *key_file,
3102 const gchar *group_name,
3106 GKeyFileGroup *group;
3107 GKeyFileKeyValuePair *pair;
3109 g_return_if_fail (key_file != NULL);
3110 g_return_if_fail (group_name != NULL);
3111 g_return_if_fail (key != NULL);
3115 group = g_key_file_lookup_group (key_file, group_name);
3118 g_set_error (error, G_KEY_FILE_ERROR,
3119 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3120 _("Key file does not have group '%s'"),
3121 group_name ? group_name : "(null)");
3125 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3129 g_set_error (error, G_KEY_FILE_ERROR,
3130 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3131 _("Key file does not have key '%s' in group '%s'"),
3136 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3138 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3139 g_hash_table_remove (group->lookup_map, pair->key);
3140 g_key_file_key_value_pair_free (pair);
3144 g_key_file_lookup_group_node (GKeyFile *key_file,
3145 const gchar *group_name)
3147 GKeyFileGroup *group;
3150 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3152 group = (GKeyFileGroup *) tmp->data;
3154 if (group && group->name && strcmp (group->name, group_name) == 0)
3161 static GKeyFileGroup *
3162 g_key_file_lookup_group (GKeyFile *key_file,
3163 const gchar *group_name)
3167 group_node = g_key_file_lookup_group_node (key_file, group_name);
3169 if (group_node != NULL)
3170 return (GKeyFileGroup *) group_node->data;
3176 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3177 GKeyFileGroup *group,
3182 for (key_node = group->key_value_pairs;
3184 key_node = key_node->next)
3186 GKeyFileKeyValuePair *pair;
3188 pair = (GKeyFileKeyValuePair *) key_node->data;
3190 if (pair->key && strcmp (pair->key, key) == 0)
3197 static GKeyFileKeyValuePair *
3198 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3199 GKeyFileGroup *group,
3202 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3205 /* Lines starting with # or consisting entirely of whitespace are merely
3206 * recorded, not parsed. This function assumes all leading whitespace
3207 * has been stripped.
3210 g_key_file_line_is_comment (const gchar *line)
3212 return (*line == '#' || *line == '\0' || *line == '\n');
3216 g_key_file_is_group_name (const gchar *name)
3223 p = q = (gchar *) name;
3224 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3225 q = g_utf8_next_char (q);
3227 if (*q != '\0' || q == p)
3234 g_key_file_is_key_name (const gchar *name)
3241 p = q = (gchar *) name;
3242 /* We accept a little more than the desktop entry spec says,
3243 * since gnome-vfs uses mime-types as keys in its cache.
3245 while (*q && *q != '=' && *q != '[' && *q != ']')
3246 q = g_utf8_next_char (q);
3248 /* No empty keys, please */
3252 /* We accept spaces in the middle of keys to not break
3253 * existing apps, but we don't tolerate initial of final
3254 * spaces, which would lead to silent corruption when
3255 * rereading the file.
3257 if (*p == ' ' || q[-1] == ' ')
3263 while (*q && (g_unichar_isalnum (g_utf8_get_char (q)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3264 q = g_utf8_next_char (q);
3278 /* A group in a key file is made up of a starting '[' followed by one
3279 * or more letters making up the group name followed by ']'.
3282 g_key_file_line_is_group (const gchar *line)
3292 while (*p && *p != ']')
3293 p = g_utf8_next_char (p);
3298 /* silently accept whitespace after the ] */
3299 p = g_utf8_next_char (p);
3300 while (*p == ' ' || *p == '\t')
3301 p = g_utf8_next_char (p);
3310 g_key_file_line_is_key_value_pair (const gchar *line)
3314 p = (gchar *) g_utf8_strchr (line, -1, '=');
3319 /* Key must be non-empty
3328 g_key_file_parse_value_as_string (GKeyFile *key_file,
3333 gchar *string_value, *p, *q0, *q;
3335 string_value = g_new (gchar, strlen (value) + 1);
3337 p = (gchar *) value;
3338 q0 = q = string_value;
3368 g_set_error (error, G_KEY_FILE_ERROR,
3369 G_KEY_FILE_ERROR_INVALID_VALUE,
3370 _("Key file contains escape character "
3375 if (pieces && *p == key_file->list_separator)
3376 *q = key_file->list_separator;
3390 g_set_error (error, G_KEY_FILE_ERROR,
3391 G_KEY_FILE_ERROR_INVALID_VALUE,
3392 _("Key file contains invalid escape "
3393 "sequence '%s'"), sequence);
3402 if (pieces && (*p == key_file->list_separator))
3404 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3420 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3421 *pieces = g_slist_reverse (*pieces);
3424 return string_value;
3428 g_key_file_parse_string_as_value (GKeyFile *key_file,
3429 const gchar *string,
3430 gboolean escape_separator)
3432 gchar *value, *p, *q;
3434 gboolean parsing_leading_space;
3436 length = strlen (string) + 1;
3438 /* Worst case would be that every character needs to be escaped.
3439 * In other words every character turns to two characters
3441 value = g_new (gchar, 2 * length);
3443 p = (gchar *) string;
3445 parsing_leading_space = TRUE;
3446 while (p < (string + length - 1))
3448 gchar escaped_character[3] = { '\\', 0, 0 };
3453 if (parsing_leading_space)
3455 escaped_character[1] = 's';
3456 strcpy (q, escaped_character);
3466 if (parsing_leading_space)
3468 escaped_character[1] = 't';
3469 strcpy (q, escaped_character);
3479 escaped_character[1] = 'n';
3480 strcpy (q, escaped_character);
3484 escaped_character[1] = 'r';
3485 strcpy (q, escaped_character);
3489 escaped_character[1] = '\\';
3490 strcpy (q, escaped_character);
3492 parsing_leading_space = FALSE;
3495 if (escape_separator && *p == key_file->list_separator)
3497 escaped_character[1] = key_file->list_separator;
3498 strcpy (q, escaped_character);
3500 parsing_leading_space = TRUE;
3506 parsing_leading_space = FALSE;
3518 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3522 gchar *end_of_valid_int;
3527 long_value = strtol (value, &end_of_valid_int, 10);
3529 if (*value == '\0' || *end_of_valid_int != '\0')
3531 gchar *value_utf8 = _g_utf8_make_valid (value);
3532 g_set_error (error, G_KEY_FILE_ERROR,
3533 G_KEY_FILE_ERROR_INVALID_VALUE,
3534 _("Value '%s' cannot be interpreted "
3535 "as a number."), value_utf8);
3536 g_free (value_utf8);
3541 int_value = long_value;
3542 if (int_value != long_value || errno == ERANGE)
3544 gchar *value_utf8 = _g_utf8_make_valid (value);
3547 G_KEY_FILE_ERROR_INVALID_VALUE,
3548 _("Integer value '%s' out of range"),
3550 g_free (value_utf8);
3559 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3563 return g_strdup_printf ("%d", value);
3567 g_key_file_parse_value_as_double (GKeyFile *key_file,
3571 gchar *end_of_valid_d;
3572 gdouble double_value = 0;
3574 double_value = g_ascii_strtod (value, &end_of_valid_d);
3576 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3578 gchar *value_utf8 = _g_utf8_make_valid (value);
3579 g_set_error (error, G_KEY_FILE_ERROR,
3580 G_KEY_FILE_ERROR_INVALID_VALUE,
3581 _("Value '%s' cannot be interpreted "
3582 "as a float number."),
3584 g_free (value_utf8);
3587 return double_value;
3591 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3597 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3599 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3602 value_utf8 = _g_utf8_make_valid (value);
3603 g_set_error (error, G_KEY_FILE_ERROR,
3604 G_KEY_FILE_ERROR_INVALID_VALUE,
3605 _("Value '%s' cannot be interpreted "
3606 "as a boolean."), value_utf8);
3607 g_free (value_utf8);
3613 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3617 return g_strdup ("true");
3619 return g_strdup ("false");
3623 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3630 string = g_string_sized_new (512);
3632 lines = g_strsplit (value, "\n", 0);
3634 for (i = 0; lines[i] != NULL; i++)
3636 if (lines[i][0] != '#')
3637 g_string_append_printf (string, "%s\n", lines[i]);
3639 g_string_append_printf (string, "%s\n", lines[i] + 1);
3643 return g_string_free (string, FALSE);
3647 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3648 const gchar *comment)
3654 string = g_string_sized_new (512);
3656 lines = g_strsplit (comment, "\n", 0);
3658 for (i = 0; lines[i] != NULL; i++)
3659 g_string_append_printf (string, "#%s%s", lines[i],
3660 lines[i + 1] == NULL? "" : "\n");
3663 return g_string_free (string, FALSE);
3666 #define __G_KEY_FILE_C__
3667 #include "galiasdef.c"