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 gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
158 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
161 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
163 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
165 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
166 const gchar *comment);
167 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
171 static void g_key_file_parse_comment (GKeyFile *key_file,
175 static void g_key_file_parse_group (GKeyFile *key_file,
179 static gchar *key_get_locale (const gchar *key);
180 static void g_key_file_parse_data (GKeyFile *key_file,
184 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
189 g_key_file_error_quark (void)
191 return g_quark_from_static_string ("g-key-file-error-quark");
195 g_key_file_init (GKeyFile *key_file)
197 key_file->current_group = g_new0 (GKeyFileGroup, 1);
198 key_file->groups = g_list_prepend (NULL, key_file->current_group);
199 key_file->start_group = NULL;
200 key_file->parse_buffer = g_string_sized_new (128);
201 key_file->approximate_size = 0;
202 key_file->list_separator = ';';
207 g_key_file_clear (GKeyFile *key_file)
209 GList *tmp, *group_node;
211 if (key_file->parse_buffer)
212 g_string_free (key_file->parse_buffer, TRUE);
214 tmp = key_file->groups;
219 g_key_file_remove_group_node (key_file, group_node);
222 g_assert (key_file->groups == NULL);
229 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
230 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
231 * read an existing key file.
233 * Return value: an empty #GKeyFile.
238 g_key_file_new (void)
242 key_file = g_new0 (GKeyFile, 1);
243 g_key_file_init (key_file);
249 * g_key_file_set_list_separator:
250 * @key_file: a #GKeyFile
251 * @separator: the separator
253 * Sets the character which is used to separate
254 * values in lists. Typically ';' or ',' are used
255 * as separators. The default list separator is ';'.
260 g_key_file_set_list_separator (GKeyFile *key_file,
263 key_file->list_separator = separator;
267 /* Iterates through all the directories in *dirs trying to
268 * open file. When it successfully locates and opens a file it
269 * returns the file descriptor to the open file. It also
270 * outputs the absolute path of the file in output_file and
271 * leaves the unchecked directories in *dirs.
274 find_file_in_data_dirs (const gchar *file,
279 gchar **data_dirs, *data_dir, *path;
290 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
292 gchar *candidate_file, *sub_dir;
294 candidate_file = (gchar *) file;
295 sub_dir = g_strdup ("");
296 while (candidate_file != NULL && fd < 0)
300 path = g_build_filename (data_dir, sub_dir,
301 candidate_file, NULL);
303 fd = g_open (path, O_RDONLY, 0);
311 candidate_file = strchr (candidate_file, '-');
313 if (candidate_file == NULL)
319 sub_dir = g_strndup (file, candidate_file - file - 1);
321 for (p = sub_dir; *p != '\0'; p++)
324 *p = G_DIR_SEPARATOR;
335 g_set_error (error, G_KEY_FILE_ERROR,
336 G_KEY_FILE_ERROR_NOT_FOUND,
337 _("Valid key file could not be "
338 "found in data dirs"));
341 if (output_file != NULL && fd > 0)
342 *output_file = g_strdup (path);
350 g_key_file_load_from_fd (GKeyFile *key_file,
355 GError *key_file_error = NULL;
357 struct stat stat_buf;
358 gchar read_buf[4096];
360 if (fstat (fd, &stat_buf) < 0)
362 g_set_error (error, G_FILE_ERROR,
363 g_file_error_from_errno (errno),
364 "%s", g_strerror (errno));
368 if (!S_ISREG (stat_buf.st_mode))
370 g_set_error (error, G_KEY_FILE_ERROR,
371 G_KEY_FILE_ERROR_PARSE,
372 _("Not a regular file"));
376 if (stat_buf.st_size == 0)
378 g_set_error (error, G_KEY_FILE_ERROR,
379 G_KEY_FILE_ERROR_PARSE,
384 if (key_file->approximate_size > 0)
386 g_key_file_clear (key_file);
387 g_key_file_init (key_file);
389 key_file->flags = flags;
394 bytes_read = read (fd, read_buf, 4096);
396 if (bytes_read == 0) /* End of File */
401 if (errno == EINTR || errno == EAGAIN)
404 g_set_error (error, G_FILE_ERROR,
405 g_file_error_from_errno (errno),
406 "%s", g_strerror (errno));
410 g_key_file_parse_data (key_file,
411 read_buf, bytes_read,
414 while (!key_file_error);
418 g_propagate_error (error, key_file_error);
422 g_key_file_flush_parse_buffer (key_file, &key_file_error);
426 g_propagate_error (error, key_file_error);
434 * g_key_file_load_from_file:
435 * @key_file: an empty #GKeyFile struct
436 * @file: the path of a filename to load, in the GLib file name encoding
437 * @flags: flags from #GKeyFileFlags
438 * @error: return location for a #GError, or %NULL
440 * Loads a key file into an empty #GKeyFile structure.
441 * If the file could not be loaded then %error is set to
442 * either a #GFileError or #GKeyFileError.
444 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
448 g_key_file_load_from_file (GKeyFile *key_file,
453 GError *key_file_error = NULL;
456 g_return_val_if_fail (key_file != NULL, FALSE);
457 g_return_val_if_fail (file != NULL, FALSE);
459 fd = g_open (file, O_RDONLY, 0);
463 g_set_error (error, G_FILE_ERROR,
464 g_file_error_from_errno (errno),
465 "%s", g_strerror (errno));
469 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
474 g_propagate_error (error, key_file_error);
482 * g_key_file_load_from_data:
483 * @key_file: an empty #GKeyFile struct
484 * @data: key file loaded in memory.
485 * @length: the length of @data in bytes
486 * @flags: flags from #GKeyFileFlags
487 * @error: return location for a #GError, or %NULL
489 * Loads a key file from memory into an empty #GKeyFile structure. If
490 * the object cannot be created then %error is set to a
493 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
497 g_key_file_load_from_data (GKeyFile *key_file,
503 GError *key_file_error = NULL;
505 g_return_val_if_fail (key_file != NULL, FALSE);
506 g_return_val_if_fail (data != NULL, FALSE);
507 g_return_val_if_fail (length != 0, FALSE);
509 if (length == (gsize)-1)
510 length = strlen (data);
512 if (key_file->approximate_size > 0)
514 g_key_file_clear (key_file);
515 g_key_file_init (key_file);
517 key_file->flags = flags;
519 g_key_file_parse_data (key_file, data, length, &key_file_error);
523 g_propagate_error (error, key_file_error);
527 g_key_file_flush_parse_buffer (key_file, &key_file_error);
531 g_propagate_error (error, key_file_error);
539 * g_key_file_load_from_data_dirs:
540 * @key_file: an empty #GKeyFile struct
541 * @file: a relative path to a filename to open and parse
542 * @full_path: return location for a string containing the full path
543 * of the file, or %NULL
544 * @flags: flags from #GKeyFileFlags
545 * @error: return location for a #GError, or %NULL
547 * This function looks for a key file named @file in the paths
548 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
549 * loads the file into @key_file and returns the file's full path in
550 * @full_path. If the file could not be loaded then an %error is
551 * set to either a #GFileError or #GKeyFileError.
553 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
557 g_key_file_load_from_data_dirs (GKeyFile *key_file,
563 GError *key_file_error = NULL;
564 gchar **all_data_dirs, **data_dirs;
565 const gchar * user_data_dir;
566 const gchar * const * system_data_dirs;
572 g_return_val_if_fail (key_file != NULL, FALSE);
573 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
575 user_data_dir = g_get_user_data_dir ();
576 system_data_dirs = g_get_system_data_dirs ();
577 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
580 all_data_dirs[i++] = g_strdup (user_data_dir);
583 while (system_data_dirs[j] != NULL)
584 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
587 data_dirs = all_data_dirs;
589 while (*data_dirs != NULL && !found_file)
591 g_free (output_path);
593 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
599 g_propagate_error (error, key_file_error);
603 found_file = g_key_file_load_from_fd (key_file, fd, flags,
609 g_propagate_error (error, key_file_error);
614 if (found_file && full_path)
615 *full_path = output_path;
617 g_free (output_path);
619 g_strfreev (all_data_dirs);
626 * @key_file: a #GKeyFile
633 g_key_file_free (GKeyFile *key_file)
635 g_return_if_fail (key_file != NULL);
637 g_key_file_clear (key_file);
641 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
642 * true for locales that match those in g_get_language_names().
645 g_key_file_locale_is_interesting (GKeyFile *key_file,
648 const gchar * const * current_locales;
651 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
654 current_locales = g_get_language_names ();
656 for (i = 0; current_locales[i] != NULL; i++)
658 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
666 g_key_file_parse_line (GKeyFile *key_file,
671 GError *parse_error = NULL;
674 g_return_if_fail (key_file != NULL);
675 g_return_if_fail (line != NULL);
677 line_start = (gchar *) line;
678 while (g_ascii_isspace (*line_start))
681 if (g_key_file_line_is_comment (line_start))
682 g_key_file_parse_comment (key_file, line, length, &parse_error);
683 else if (g_key_file_line_is_group (line_start))
684 g_key_file_parse_group (key_file, line_start,
685 length - (line_start - line),
687 else if (g_key_file_line_is_key_value_pair (line_start))
688 g_key_file_parse_key_value_pair (key_file, line_start,
689 length - (line_start - line),
693 g_set_error (error, G_KEY_FILE_ERROR,
694 G_KEY_FILE_ERROR_PARSE,
695 _("Key file contains line '%s' which is not "
696 "a key-value pair, group, or comment"), line);
701 g_propagate_error (error, parse_error);
705 g_key_file_parse_comment (GKeyFile *key_file,
710 GKeyFileKeyValuePair *pair;
712 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
715 g_assert (key_file->current_group != NULL);
717 pair = g_new0 (GKeyFileKeyValuePair, 1);
720 pair->value = g_strndup (line, length);
722 key_file->current_group->key_value_pairs =
723 g_list_prepend (key_file->current_group->key_value_pairs, pair);
727 g_key_file_parse_group (GKeyFile *key_file,
733 const gchar *group_name_start, *group_name_end;
735 /* advance past opening '['
737 group_name_start = line + 1;
738 group_name_end = line + length - 1;
740 while (*group_name_end != ']')
743 group_name = g_strndup (group_name_start,
744 group_name_end - group_name_start);
746 g_key_file_add_group (key_file, group_name);
751 g_key_file_parse_key_value_pair (GKeyFile *key_file,
756 gchar *key, *value, *key_end, *value_start, *locale;
757 gsize key_len, value_len;
759 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
761 g_set_error (error, G_KEY_FILE_ERROR,
762 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
763 _("Key file does not start with a group"));
767 key_end = value_start = strchr (line, '=');
769 g_assert (key_end != NULL);
774 /* Pull the key name from the line (chomping trailing whitespace)
776 while (g_ascii_isspace (*key_end))
779 key_len = key_end - line + 2;
781 g_assert (key_len <= length);
783 key = g_strndup (line, key_len - 1);
785 /* Pull the value from the line (chugging leading whitespace)
787 while (g_ascii_isspace (*value_start))
790 value_len = line + length - value_start + 1;
792 value = g_strndup (value_start, value_len);
794 g_assert (key_file->start_group != NULL);
796 if (key_file->current_group
797 && key_file->current_group->name
798 && strcmp (key_file->start_group->name,
799 key_file->current_group->name) == 0
800 && strcmp (key, "Encoding") == 0)
802 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
804 g_set_error (error, G_KEY_FILE_ERROR,
805 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
806 _("Key file contains unsupported encoding '%s'"), value);
814 /* Is this key a translation? If so, is it one that we care about?
816 locale = key_get_locale (key);
818 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
819 g_key_file_add_key (key_file, key_file->current_group, key, value);
827 key_get_locale (const gchar *key)
831 locale = g_strrstr (key, "[");
833 if (locale && strlen (locale) <= 2)
837 locale = g_strndup (locale + 1, strlen (locale) - 2);
843 g_key_file_parse_data (GKeyFile *key_file,
851 g_return_if_fail (key_file != NULL);
852 g_return_if_fail (data != NULL);
856 for (i = 0; i < length; i++)
860 if (i > 0 && data[i - 1] == '\r')
861 g_string_erase (key_file->parse_buffer,
862 key_file->parse_buffer->len - 1,
865 /* When a newline is encountered flush the parse buffer so that the
866 * line can be parsed. Note that completely blank lines won't show
867 * up in the parse buffer, so they get parsed directly.
869 if (key_file->parse_buffer->len > 0)
870 g_key_file_flush_parse_buffer (key_file, &parse_error);
872 g_key_file_parse_comment (key_file, "", 1, &parse_error);
876 g_propagate_error (error, parse_error);
881 g_string_append_c (key_file->parse_buffer, data[i]);
884 key_file->approximate_size += length;
888 g_key_file_flush_parse_buffer (GKeyFile *key_file,
891 GError *file_error = NULL;
893 g_return_if_fail (key_file != NULL);
897 if (key_file->parse_buffer->len > 0)
899 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
900 key_file->parse_buffer->len,
902 g_string_erase (key_file->parse_buffer, 0, -1);
906 g_propagate_error (error, file_error);
913 * g_key_file_to_data:
914 * @key_file: a #GKeyFile
915 * @length: return location for the length of the
916 * returned string, or %NULL
917 * @error: return location for a #GError, or %NULL
919 * This function outputs @key_file as a string.
921 * Return value: a newly allocated string holding
922 * the contents of the #GKeyFile
927 g_key_file_to_data (GKeyFile *key_file,
931 GString *data_string;
932 GList *group_node, *key_file_node;
934 g_return_val_if_fail (key_file != NULL, NULL);
936 data_string = g_string_sized_new (2 * key_file->approximate_size);
938 for (group_node = g_list_last (key_file->groups);
940 group_node = group_node->prev)
942 GKeyFileGroup *group;
944 group = (GKeyFileGroup *) group_node->data;
946 /* separate groups by an empty line */
947 if (group_node->next)
948 g_string_append_c (data_string, '\n');
950 if (group->comment != NULL)
951 g_string_append_printf (data_string, "%s\n", group->comment->value);
952 if (group->name != NULL)
953 g_string_append_printf (data_string, "[%s]\n", group->name);
955 for (key_file_node = g_list_last (group->key_value_pairs);
956 key_file_node != NULL;
957 key_file_node = key_file_node->prev)
959 GKeyFileKeyValuePair *pair;
961 pair = (GKeyFileKeyValuePair *) key_file_node->data;
963 if (pair->key != NULL)
964 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
966 g_string_append_printf (data_string, "%s\n", pair->value);
971 *length = data_string->len;
973 return g_string_free (data_string, FALSE);
977 * g_key_file_get_keys:
978 * @key_file: a #GKeyFile
979 * @group_name: a group name
980 * @length: return location for the number of keys returned, or %NULL
981 * @error: return location for a #GError, or %NULL
983 * Returns all keys for the group name @group_name. The array of
984 * returned keys will be %NULL-terminated, so @length may
985 * optionally be %NULL. In the event that the @group_name cannot
986 * be found, %NULL is returned and @error is set to
987 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
989 * Return value: a newly-allocated %NULL-terminated array of
990 * strings. Use g_strfreev() to free it.
995 g_key_file_get_keys (GKeyFile *key_file,
996 const gchar *group_name,
1000 GKeyFileGroup *group;
1005 g_return_val_if_fail (key_file != NULL, NULL);
1006 g_return_val_if_fail (group_name != NULL, NULL);
1008 group = g_key_file_lookup_group (key_file, group_name);
1012 g_set_error (error, G_KEY_FILE_ERROR,
1013 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1014 _("Key file does not have group '%s'"),
1015 group_name ? group_name : "(null)");
1020 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1022 GKeyFileKeyValuePair *pair;
1024 pair = (GKeyFileKeyValuePair *) tmp->data;
1030 keys = g_new0 (gchar *, num_keys + 1);
1033 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1035 GKeyFileKeyValuePair *pair;
1037 pair = (GKeyFileKeyValuePair *) tmp->data;
1041 keys[i] = g_strdup (pair->key);
1046 keys[num_keys] = NULL;
1055 * g_key_file_get_start_group:
1056 * @key_file: a #GKeyFile
1058 * Returns the name of the start group of the file.
1060 * Return value: The start group of the key file.
1065 g_key_file_get_start_group (GKeyFile *key_file)
1067 g_return_val_if_fail (key_file != NULL, NULL);
1069 if (key_file->start_group)
1070 return g_strdup (key_file->start_group->name);
1076 * g_key_file_get_groups:
1077 * @key_file: a #GKeyFile
1078 * @length: return location for the number of returned groups, or %NULL
1080 * Returns all groups in the key file loaded with @key_file. The
1081 * array of returned groups will be %NULL-terminated, so @length may
1082 * optionally be %NULL.
1084 * Return value: a newly-allocated %NULL-terminated array of strings.
1085 * Use g_strfreev() to free it.
1089 g_key_file_get_groups (GKeyFile *key_file,
1094 gsize i, num_groups;
1096 g_return_val_if_fail (key_file != NULL, NULL);
1098 num_groups = g_list_length (key_file->groups);
1100 g_assert (num_groups > 0);
1102 /* Only need num_groups instead of num_groups + 1
1103 * because the first group of the file (last in the
1104 * list) is always the comment group at the top,
1107 groups = g_new0 (gchar *, num_groups);
1109 group_node = g_list_last (key_file->groups);
1111 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1114 for (group_node = group_node->prev;
1116 group_node = group_node->prev)
1118 GKeyFileGroup *group;
1120 group = (GKeyFileGroup *) group_node->data;
1122 g_assert (group->name != NULL);
1124 groups[i++] = g_strdup (group->name);
1135 * g_key_file_get_value:
1136 * @key_file: a #GKeyFile
1137 * @group_name: a group name
1139 * @error: return location for a #GError, or %NULL
1141 * Returns the value associated with @key under @group_name.
1143 * In the event the key cannot be found, %NULL is returned and
1144 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1145 * event that the @group_name cannot be found, %NULL is returned
1146 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1148 * Return value: a newly allocated string or %NULL if the specified
1149 * key cannot be found.
1154 g_key_file_get_value (GKeyFile *key_file,
1155 const gchar *group_name,
1159 GKeyFileGroup *group;
1160 GKeyFileKeyValuePair *pair;
1161 gchar *value = NULL;
1163 g_return_val_if_fail (key_file != NULL, NULL);
1164 g_return_val_if_fail (group_name != NULL, NULL);
1165 g_return_val_if_fail (key != NULL, NULL);
1167 group = g_key_file_lookup_group (key_file, group_name);
1171 g_set_error (error, G_KEY_FILE_ERROR,
1172 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1173 _("Key file does not have group '%s'"),
1174 group_name ? group_name : "(null)");
1178 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1181 value = g_strdup (pair->value);
1183 g_set_error (error, G_KEY_FILE_ERROR,
1184 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1185 _("Key file does not have key '%s'"), key);
1191 * g_key_file_set_value:
1192 * @key_file: a #GKeyFile
1193 * @group_name: a group name
1197 * Associates a new value with @key under @group_name. If @key
1198 * cannot be found then it is created. If @group_name cannot be
1199 * found then it is created.
1204 g_key_file_set_value (GKeyFile *key_file,
1205 const gchar *group_name,
1209 GKeyFileGroup *group;
1210 GKeyFileKeyValuePair *pair;
1212 g_return_if_fail (key_file != NULL);
1213 g_return_if_fail (group_name != NULL);
1214 g_return_if_fail (key != NULL);
1215 g_return_if_fail (value != NULL);
1217 group = g_key_file_lookup_group (key_file, group_name);
1221 g_key_file_add_group (key_file, group_name);
1222 group = (GKeyFileGroup *) key_file->groups->data;
1224 g_key_file_add_key (key_file, group, key, value);
1228 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1231 g_key_file_add_key (key_file, group, key, value);
1234 g_free (pair->value);
1235 pair->value = g_strdup (value);
1241 * g_key_file_get_string:
1242 * @key_file: a #GKeyFile
1243 * @group_name: a group name
1245 * @error: return location for a #GError, or %NULL
1247 * Returns the value associated with @key under @group_name.
1249 * In the event the key cannot be found, %NULL is returned and
1250 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1251 * event that the @group_name cannot be found, %NULL is returned
1252 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1254 * Return value: a newly allocated string or %NULL if the specified
1255 * key cannot be found.
1260 g_key_file_get_string (GKeyFile *key_file,
1261 const gchar *group_name,
1265 gchar *value, *string_value;
1266 GError *key_file_error;
1268 g_return_val_if_fail (key_file != NULL, NULL);
1269 g_return_val_if_fail (group_name != NULL, NULL);
1270 g_return_val_if_fail (key != NULL, NULL);
1272 key_file_error = NULL;
1274 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1278 g_propagate_error (error, key_file_error);
1282 if (!g_utf8_validate (value, -1, NULL))
1284 g_set_error (error, G_KEY_FILE_ERROR,
1285 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1286 _("Key file contains key '%s' with value '%s' "
1287 "which is not UTF-8"), key, value);
1292 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1298 if (g_error_matches (key_file_error,
1300 G_KEY_FILE_ERROR_INVALID_VALUE))
1302 g_set_error (error, G_KEY_FILE_ERROR,
1303 G_KEY_FILE_ERROR_INVALID_VALUE,
1304 _("Key file contains key '%s' "
1305 "which has value that cannot be interpreted."),
1307 g_error_free (key_file_error);
1310 g_propagate_error (error, key_file_error);
1313 return string_value;
1317 * g_key_file_set_string:
1318 * @key_file: a #GKeyFile
1319 * @group_name: a group name
1323 * Associates a new string value with @key under @group_name. If
1324 * @key cannot be found then it is created. If @group_name
1325 * cannot be found then it is created.
1330 g_key_file_set_string (GKeyFile *key_file,
1331 const gchar *group_name,
1333 const gchar *string)
1337 g_return_if_fail (key_file != NULL);
1338 g_return_if_fail (group_name != NULL);
1339 g_return_if_fail (key != NULL);
1340 g_return_if_fail (string != NULL);
1342 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1343 g_key_file_set_value (key_file, group_name, key, value);
1348 * g_key_file_get_string_list:
1349 * @key_file: a #GKeyFile
1350 * @group_name: a group name
1352 * @length: return location for the number of returned strings, or %NULL
1353 * @error: return location for a #GError, or %NULL
1355 * Returns the values associated with @key under @group_name.
1357 * In the event the key cannot be found, %NULL is returned and
1358 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1359 * event that the @group_name cannot be found, %NULL is returned
1360 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1362 * Return value: a %NULL-terminated string array or %NULL if the specified
1363 * key cannot be found. The array should be freed with g_strfreev().
1368 g_key_file_get_string_list (GKeyFile *key_file,
1369 const gchar *group_name,
1374 GError *key_file_error = NULL;
1375 gchar *value, *string_value, **values;
1377 GSList *p, *pieces = NULL;
1379 g_return_val_if_fail (key_file != NULL, NULL);
1380 g_return_val_if_fail (group_name != NULL, NULL);
1381 g_return_val_if_fail (key != NULL, NULL);
1383 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1387 g_propagate_error (error, key_file_error);
1391 if (!g_utf8_validate (value, -1, NULL))
1393 g_set_error (error, G_KEY_FILE_ERROR,
1394 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1395 _("Key file contains key '%s' with value '%s' "
1396 "which is not UTF-8"), key, value);
1401 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1403 g_free (string_value);
1407 if (g_error_matches (key_file_error,
1409 G_KEY_FILE_ERROR_INVALID_VALUE))
1411 g_set_error (error, G_KEY_FILE_ERROR,
1412 G_KEY_FILE_ERROR_INVALID_VALUE,
1413 _("Key file contains key '%s' "
1414 "which has value that cannot be interpreted."),
1416 g_error_free (key_file_error);
1419 g_propagate_error (error, key_file_error);
1422 len = g_slist_length (pieces);
1423 values = g_new0 (gchar *, len + 1);
1424 for (p = pieces, i = 0; p; p = p->next)
1425 values[i++] = p->data;
1428 g_slist_free (pieces);
1437 * g_key_file_set_string_list:
1438 * @key_file: a #GKeyFile
1439 * @group_name: a group name
1441 * @list: an array of locale string values
1442 * @length: number of locale string values in @list
1444 * Associates a list of string values for @key under @group_name.
1445 * If @key cannot be found then it is created. If @group_name
1446 * cannot be found then it is created.
1451 g_key_file_set_string_list (GKeyFile *key_file,
1452 const gchar *group_name,
1454 const gchar * const list[],
1457 GString *value_list;
1460 g_return_if_fail (key_file != NULL);
1461 g_return_if_fail (group_name != NULL);
1462 g_return_if_fail (key != NULL);
1463 g_return_if_fail (list != NULL);
1465 value_list = g_string_sized_new (length * 128);
1466 for (i = 0; i < length && list[i] != NULL; i++)
1470 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1471 g_string_append (value_list, value);
1472 g_string_append_c (value_list, key_file->list_separator);
1477 g_key_file_set_value (key_file, group_name, key, value_list->str);
1478 g_string_free (value_list, TRUE);
1482 * g_key_file_set_locale_string:
1483 * @key_file: a #GKeyFile
1484 * @group_name: a group name
1489 * Associates a string value for @key and @locale under
1490 * @group_name. If the translation for @key cannot be found
1491 * then it is created.
1496 g_key_file_set_locale_string (GKeyFile *key_file,
1497 const gchar *group_name,
1499 const gchar *locale,
1500 const gchar *string)
1502 gchar *full_key, *value;
1504 g_return_if_fail (key_file != NULL);
1505 g_return_if_fail (group_name != NULL);
1506 g_return_if_fail (key != NULL);
1507 g_return_if_fail (locale != NULL);
1508 g_return_if_fail (string != NULL);
1510 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1511 full_key = g_strdup_printf ("%s[%s]", key, locale);
1512 g_key_file_set_value (key_file, group_name, full_key, value);
1517 extern GSList *_g_compute_locale_variants (const gchar *locale);
1520 * g_key_file_get_locale_string:
1521 * @key_file: a #GKeyFile
1522 * @group_name: a group name
1524 * @locale: a locale or %NULL
1525 * @error: return location for a #GError, or %NULL
1527 * Returns the value associated with @key under @group_name
1528 * translated in the given @locale if available. If @locale is
1529 * %NULL then the current locale is assumed.
1531 * If @key cannot be found then %NULL is returned and @error is set to
1532 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1533 * with @key cannot be interpreted or no suitable translation can
1534 * be found then the untranslated value is returned.
1536 * Return value: a newly allocated string or %NULL if the specified
1537 * key cannot be found.
1542 g_key_file_get_locale_string (GKeyFile *key_file,
1543 const gchar *group_name,
1545 const gchar *locale,
1548 gchar *candidate_key, *translated_value;
1549 GError *key_file_error;
1551 gboolean free_languages = FALSE;
1554 g_return_val_if_fail (key_file != NULL, NULL);
1555 g_return_val_if_fail (group_name != NULL, NULL);
1556 g_return_val_if_fail (key != NULL, NULL);
1558 candidate_key = NULL;
1559 translated_value = NULL;
1560 key_file_error = NULL;
1566 list = _g_compute_locale_variants (locale);
1568 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1569 for (l = list, i = 0; l; l = l->next, i++)
1570 languages[i] = l->data;
1571 languages[i] = NULL;
1573 g_slist_free (list);
1574 free_languages = TRUE;
1578 languages = (gchar **) g_get_language_names ();
1579 free_languages = FALSE;
1582 for (i = 0; languages[i]; i++)
1584 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1586 translated_value = g_key_file_get_string (key_file,
1588 candidate_key, NULL);
1589 g_free (candidate_key);
1591 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1594 g_free (translated_value);
1595 translated_value = NULL;
1598 /* Fallback to untranslated key
1600 if (!translated_value)
1602 translated_value = g_key_file_get_string (key_file, group_name, key,
1605 if (!translated_value)
1606 g_propagate_error (error, key_file_error);
1610 g_strfreev (languages);
1612 return translated_value;
1616 * g_key_file_get_locale_string_list:
1617 * @key_file: a #GKeyFile
1618 * @group_name: a group name
1621 * @length: return location for the number of returned strings or %NULL
1622 * @error: return location for a #GError or %NULL
1624 * Returns the values associated with @key under @group_name
1625 * translated in the given @locale if available. If @locale is
1626 * %NULL then the current locale is assumed.
1628 * If @key cannot be found then %NULL is returned and @error is set to
1629 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1630 * with @key cannot be interpreted or no suitable translations
1631 * can be found then the untranslated values are returned.
1632 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1634 * Return value: a newly allocated %NULL-terminated string array
1635 * or %NULL if the key isn't found. The string array should be freed
1636 * with g_strfreev().
1641 g_key_file_get_locale_string_list (GKeyFile *key_file,
1642 const gchar *group_name,
1644 const gchar *locale,
1648 GError *key_file_error;
1649 gchar **values, *value;
1651 g_return_val_if_fail (key_file != NULL, NULL);
1652 g_return_val_if_fail (group_name != NULL, NULL);
1653 g_return_val_if_fail (key != NULL, NULL);
1655 key_file_error = NULL;
1657 value = g_key_file_get_locale_string (key_file, group_name,
1662 g_propagate_error (error, key_file_error);
1667 if (value[strlen (value) - 1] == ';')
1668 value[strlen (value) - 1] = '\0';
1670 values = g_strsplit (value, ";", 0);
1675 *length = g_strv_length (values);
1681 * g_key_file_set_locale_string_list:
1682 * @key_file: a #GKeyFile
1683 * @group_name: a group name
1686 * @list: a %NULL-terminated array of locale string values
1687 * @length: the length of @list
1689 * Associates a list of string values for @key and @locale under
1690 * @group_name. If the translation for @key cannot be found then
1696 g_key_file_set_locale_string_list (GKeyFile *key_file,
1697 const gchar *group_name,
1699 const gchar *locale,
1700 const gchar * const list[],
1703 GString *value_list;
1707 g_return_if_fail (key_file != NULL);
1708 g_return_if_fail (group_name != NULL);
1709 g_return_if_fail (key != NULL);
1710 g_return_if_fail (locale != NULL);
1711 g_return_if_fail (length != 0);
1713 value_list = g_string_sized_new (length * 128);
1714 for (i = 0; i < length && list[i] != NULL; i++)
1718 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1720 g_string_append (value_list, value);
1721 g_string_append_c (value_list, ';');
1726 full_key = g_strdup_printf ("%s[%s]", key, locale);
1727 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1729 g_string_free (value_list, TRUE);
1733 * g_key_file_get_boolean:
1734 * @key_file: a #GKeyFile
1735 * @group_name: a group name
1737 * @error: return location for a #GError
1739 * Returns the value associated with @key under @group_name as a
1742 * If @key cannot be found then the return value is undefined and
1743 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1744 * the value associated with @key cannot be interpreted as a boolean
1745 * then the return value is also undefined and @error is set to
1746 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1748 * Return value: the value associated with the key as a boolean
1752 g_key_file_get_boolean (GKeyFile *key_file,
1753 const gchar *group_name,
1757 GError *key_file_error = NULL;
1759 gboolean bool_value;
1761 g_return_val_if_fail (key_file != NULL, FALSE);
1762 g_return_val_if_fail (group_name != NULL, FALSE);
1763 g_return_val_if_fail (key != NULL, FALSE);
1765 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1769 g_propagate_error (error, key_file_error);
1773 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1779 if (g_error_matches (key_file_error,
1781 G_KEY_FILE_ERROR_INVALID_VALUE))
1783 g_set_error (error, G_KEY_FILE_ERROR,
1784 G_KEY_FILE_ERROR_INVALID_VALUE,
1785 _("Key file contains key '%s' "
1786 "which has value that cannot be interpreted."),
1788 g_error_free (key_file_error);
1791 g_propagate_error (error, key_file_error);
1798 * g_key_file_set_boolean:
1799 * @key_file: a #GKeyFile
1800 * @group_name: a group name
1802 * @value: %TRUE or %FALSE
1804 * Associates a new boolean value with @key under @group_name.
1805 * If @key cannot be found then it is created.
1810 g_key_file_set_boolean (GKeyFile *key_file,
1811 const gchar *group_name,
1817 g_return_if_fail (key_file != NULL);
1818 g_return_if_fail (group_name != NULL);
1819 g_return_if_fail (key != NULL);
1821 result = g_key_file_parse_boolean_as_value (key_file, value);
1822 g_key_file_set_value (key_file, group_name, key, result);
1827 * g_key_file_get_boolean_list:
1828 * @key_file: a #GKeyFile
1829 * @group_name: a group name
1831 * @length: the number of booleans returned
1832 * @error: return location for a #GError
1834 * Returns the values associated with @key under @group_name as
1835 * booleans. If @group_name is %NULL, the start_group is used.
1837 * If @key cannot be found then the return value is undefined and
1838 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1839 * the values associated with @key cannot be interpreted as booleans
1840 * then the return value is also undefined and @error is set to
1841 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1843 * Return value: the values associated with the key as a boolean
1848 g_key_file_get_boolean_list (GKeyFile *key_file,
1849 const gchar *group_name,
1854 GError *key_file_error;
1856 gboolean *bool_values;
1859 g_return_val_if_fail (key_file != NULL, NULL);
1860 g_return_val_if_fail (group_name != NULL, NULL);
1861 g_return_val_if_fail (key != NULL, NULL);
1863 key_file_error = NULL;
1865 values = g_key_file_get_string_list (key_file, group_name, key,
1866 &num_bools, &key_file_error);
1869 g_propagate_error (error, key_file_error);
1874 bool_values = g_new0 (gboolean, num_bools);
1876 for (i = 0; i < num_bools; i++)
1878 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1884 g_propagate_error (error, key_file_error);
1885 g_strfreev (values);
1886 g_free (bool_values);
1891 g_strfreev (values);
1894 *length = num_bools;
1900 * g_key_file_set_boolean_list:
1901 * @key_file: a #GKeyFile
1902 * @group_name: a group name
1904 * @list: an array of boolean values
1905 * @length: length of @list
1907 * Associates a list of boolean values with @key under
1908 * @group_name. If @key cannot be found then it is created.
1909 * If @group_name is %NULL, the start_group is used.
1914 g_key_file_set_boolean_list (GKeyFile *key_file,
1915 const gchar *group_name,
1920 GString *value_list;
1923 g_return_if_fail (key_file != NULL);
1924 g_return_if_fail (group_name != NULL);
1925 g_return_if_fail (key != NULL);
1926 g_return_if_fail (list != NULL);
1928 value_list = g_string_sized_new (length * 8);
1929 for (i = 0; i < length; i++)
1933 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1935 g_string_append (value_list, value);
1936 g_string_append_c (value_list, key_file->list_separator);
1941 g_key_file_set_value (key_file, group_name, key, value_list->str);
1942 g_string_free (value_list, TRUE);
1946 * g_key_file_get_integer:
1947 * @key_file: a #GKeyFile
1948 * @group_name: a group name
1950 * @error: return location for a #GError
1952 * Returns the value associated with @key under @group_name as an
1953 * integer. If @group_name is %NULL, the start_group is used.
1955 * If @key cannot be found then the return value is undefined and
1956 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1957 * the value associated with @key cannot be interpreted as an integer
1958 * then the return value is also undefined and @error is set to
1959 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1961 * Return value: the value associated with the key as an integer.
1966 g_key_file_get_integer (GKeyFile *key_file,
1967 const gchar *group_name,
1971 GError *key_file_error;
1975 g_return_val_if_fail (key_file != NULL, -1);
1976 g_return_val_if_fail (group_name != NULL, -1);
1977 g_return_val_if_fail (key != NULL, -1);
1979 key_file_error = NULL;
1981 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1985 g_propagate_error (error, key_file_error);
1989 int_value = g_key_file_parse_value_as_integer (key_file, value,
1995 if (g_error_matches (key_file_error,
1997 G_KEY_FILE_ERROR_INVALID_VALUE))
1999 g_set_error (error, G_KEY_FILE_ERROR,
2000 G_KEY_FILE_ERROR_INVALID_VALUE,
2001 _("Key file contains key '%s' in group '%s' "
2002 "which has value that cannot be interpreted."), key,
2004 g_error_free (key_file_error);
2007 g_propagate_error (error, key_file_error);
2014 * g_key_file_set_integer:
2015 * @key_file: a #GKeyFile
2016 * @group_name: a group name
2018 * @value: an integer value
2020 * Associates a new integer value with @key under @group_name.
2021 * If @key cannot be found then it is created.
2026 g_key_file_set_integer (GKeyFile *key_file,
2027 const gchar *group_name,
2033 g_return_if_fail (key_file != NULL);
2034 g_return_if_fail (group_name != NULL);
2035 g_return_if_fail (key != NULL);
2037 result = g_key_file_parse_integer_as_value (key_file, value);
2038 g_key_file_set_value (key_file, group_name, key, result);
2043 * g_key_file_get_integer_list:
2044 * @key_file: a #GKeyFile
2045 * @group_name: a group name
2047 * @length: the number of integers returned
2048 * @error: return location for a #GError
2050 * Returns the values associated with @key under @group_name as
2053 * If @key cannot be found then the return value is undefined and
2054 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2055 * the values associated with @key cannot be interpreted as integers
2056 * then the return value is also undefined and @error is set to
2057 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2059 * Return value: the values associated with the key as a integer
2064 g_key_file_get_integer_list (GKeyFile *key_file,
2065 const gchar *group_name,
2070 GError *key_file_error = NULL;
2075 g_return_val_if_fail (key_file != NULL, NULL);
2076 g_return_val_if_fail (group_name != NULL, NULL);
2077 g_return_val_if_fail (key != NULL, NULL);
2079 values = g_key_file_get_string_list (key_file, group_name, key,
2080 &num_ints, &key_file_error);
2083 g_propagate_error (error, key_file_error);
2088 int_values = g_new0 (gint, num_ints);
2090 for (i = 0; i < num_ints; i++)
2092 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2098 g_propagate_error (error, key_file_error);
2099 g_strfreev (values);
2100 g_free (int_values);
2105 g_strfreev (values);
2114 * g_key_file_set_integer_list:
2115 * @key_file: a #GKeyFile
2116 * @group_name: a group name
2118 * @list: an array of integer values
2119 * @length: number of integer values in @list
2121 * Associates a list of integer values with @key under
2122 * @group_name. If @key cannot be found then it is created.
2127 g_key_file_set_integer_list (GKeyFile *key_file,
2128 const gchar *group_name,
2136 g_return_if_fail (key_file != NULL);
2137 g_return_if_fail (group_name != NULL);
2138 g_return_if_fail (key != NULL);
2139 g_return_if_fail (list != NULL);
2141 values = g_string_sized_new (length * 16);
2142 for (i = 0; i < length; i++)
2146 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2148 g_string_append (values, value);
2149 g_string_append_c (values, ';');
2154 g_key_file_set_value (key_file, group_name, key, values->str);
2155 g_string_free (values, TRUE);
2159 * g_key_file_get_double:
2160 * @key_file: a #GKeyFile
2161 * @group_name: a group name
2163 * @error: return location for a #GError
2165 * Returns the value associated with @key under @group_name as an
2166 * integer. If @group_name is %NULL, the start_group is used.
2168 * If @key cannot be found then the return value is undefined and
2169 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2170 * the value associated with @key cannot be interpreted as a double
2171 * then the return value is also undefined and @error is set to
2172 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2174 * Return value: the value associated with the key as a double.
2179 g_key_file_get_double (GKeyFile *key_file,
2180 const gchar *group_name,
2184 GError *key_file_error;
2186 gdouble double_value;
2188 g_return_val_if_fail (key_file != NULL, -1);
2189 g_return_val_if_fail (group_name != NULL, -1);
2190 g_return_val_if_fail (key != NULL, -1);
2192 key_file_error = NULL;
2194 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2198 g_propagate_error (error, key_file_error);
2202 double_value = g_key_file_parse_value_as_double (key_file, value,
2208 if (g_error_matches (key_file_error,
2210 G_KEY_FILE_ERROR_INVALID_VALUE))
2212 g_set_error (error, G_KEY_FILE_ERROR,
2213 G_KEY_FILE_ERROR_INVALID_VALUE,
2214 _("Key file contains key '%s' in group '%s' "
2215 "which has value that cannot be interpreted."), key,
2217 g_error_free (key_file_error);
2220 g_propagate_error (error, key_file_error);
2223 return double_value;
2227 * g_key_file_set_double:
2228 * @key_file: a #GKeyFile
2229 * @group_name: a group name
2231 * @value: an double value
2233 * Associates a new double value with @key under @group_name.
2234 * If @key cannot be found then it is created. If @group_name
2235 * is %NULL, the start group is used.
2240 g_key_file_set_double (GKeyFile *key_file,
2241 const gchar *group_name,
2245 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2247 g_return_if_fail (key_file != NULL);
2248 g_return_if_fail (group_name != NULL);
2249 g_return_if_fail (key != NULL);
2251 g_ascii_dtostr ( result, sizeof (result), value );
2252 g_key_file_set_value (key_file, group_name, key, result);
2256 * g_key_file_get_double_list:
2257 * @key_file: a #GKeyFile
2258 * @group_name: a group name
2260 * @length: the number of doubles returned
2261 * @error: return location for a #GError
2263 * Returns the values associated with @key under @group_name as
2264 * doubles. If @group_name is %NULL, the start group is used.
2266 * If @key cannot be found then the return value is undefined and
2267 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2268 * the values associated with @key cannot be interpreted as doubles
2269 * then the return value is also undefined and @error is set to
2270 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2272 * Return value: the values associated with the key as a double
2277 g_key_file_get_double_list (GKeyFile *key_file,
2278 const gchar *group_name,
2283 GError *key_file_error = NULL;
2285 gdouble *double_values;
2286 gsize i, num_doubles;
2288 g_return_val_if_fail (key_file != NULL, NULL);
2289 g_return_val_if_fail (group_name != NULL, NULL);
2290 g_return_val_if_fail (key != NULL, NULL);
2292 values = g_key_file_get_string_list (key_file, group_name, key,
2293 &num_doubles, &key_file_error);
2296 g_propagate_error (error, key_file_error);
2301 double_values = g_new0 (gdouble, num_doubles);
2303 for (i = 0; i < num_doubles; i++)
2305 double_values[i] = g_key_file_parse_value_as_double (key_file,
2311 g_propagate_error (error, key_file_error);
2312 g_strfreev (values);
2313 g_free (double_values);
2318 g_strfreev (values);
2321 *length = num_doubles;
2323 return double_values;
2327 * g_key_file_set_double_list:
2328 * @key_file: a #GKeyFile
2329 * @group_name: a group name
2331 * @list: an array of double values
2332 * @length: number of double values in @list
2334 * Associates a list of double values with @key under
2335 * @group_name. If @key cannot be found then it is created.
2336 * If @group_name is %NULL the start group is used.
2341 g_key_file_set_double_list (GKeyFile *key_file,
2342 const gchar *group_name,
2350 g_return_if_fail (key_file != NULL);
2351 g_return_if_fail (group_name != NULL);
2352 g_return_if_fail (key != NULL);
2353 g_return_if_fail (list != NULL);
2355 values = g_string_sized_new (length * 16);
2356 for (i = 0; i < length; i++)
2358 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2360 g_ascii_dtostr( result, sizeof (result), list[i] );
2362 g_string_append (values, result);
2363 g_string_append_c (values, ';');
2366 g_key_file_set_value (key_file, group_name, key, values->str);
2367 g_string_free (values, TRUE);
2371 g_key_file_set_key_comment (GKeyFile *key_file,
2372 const gchar *group_name,
2374 const gchar *comment,
2377 GKeyFileGroup *group;
2378 GKeyFileKeyValuePair *pair;
2379 GList *key_node, *comment_node, *tmp;
2381 group = g_key_file_lookup_group (key_file, group_name);
2384 g_set_error (error, G_KEY_FILE_ERROR,
2385 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2386 _("Key file does not have group '%s'"),
2387 group_name ? group_name : "(null)");
2392 /* First find the key the comments are supposed to be
2395 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2397 if (key_node == NULL)
2399 g_set_error (error, G_KEY_FILE_ERROR,
2400 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2401 _("Key file does not have key '%s' in group '%s'"),
2406 /* Then find all the comments already associated with the
2409 tmp = key_node->next;
2412 GKeyFileKeyValuePair *pair;
2414 pair = (GKeyFileKeyValuePair *) tmp->data;
2416 if (pair->key != NULL)
2421 g_key_file_remove_key_value_pair_node (key_file, group,
2425 if (comment == NULL)
2428 /* Now we can add our new comment
2430 pair = g_new0 (GKeyFileKeyValuePair, 1);
2433 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2435 key_node = g_list_insert (key_node, pair, 1);
2439 g_key_file_set_group_comment (GKeyFile *key_file,
2440 const gchar *group_name,
2441 const gchar *comment,
2444 GKeyFileGroup *group;
2446 group = g_key_file_lookup_group (key_file, group_name);
2449 g_set_error (error, G_KEY_FILE_ERROR,
2450 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2451 _("Key file does not have group '%s'"),
2452 group_name ? group_name : "(null)");
2457 /* First remove any existing comment
2461 g_key_file_key_value_pair_free (group->comment);
2462 group->comment = NULL;
2465 if (comment == NULL)
2468 /* Now we can add our new comment
2470 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2472 group->comment->key = NULL;
2473 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2477 g_key_file_set_top_comment (GKeyFile *key_file,
2478 const gchar *comment,
2482 GKeyFileGroup *group;
2483 GKeyFileKeyValuePair *pair;
2485 /* The last group in the list should be the top (comments only)
2488 g_assert (key_file->groups != NULL);
2489 group_node = g_list_last (key_file->groups);
2490 group = (GKeyFileGroup *) group_node->data;
2491 g_assert (group->name == NULL);
2493 /* Note all keys must be comments at the top of
2494 * the file, so we can just free it all.
2496 if (group->key_value_pairs != NULL)
2498 g_list_foreach (group->key_value_pairs,
2499 (GFunc) g_key_file_key_value_pair_free,
2501 g_list_free (group->key_value_pairs);
2502 group->key_value_pairs = NULL;
2505 if (comment == NULL)
2508 pair = g_new0 (GKeyFileKeyValuePair, 1);
2511 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2513 group->key_value_pairs =
2514 g_list_prepend (group->key_value_pairs, pair);
2518 * g_key_file_set_comment:
2519 * @key_file: a #GKeyFile
2520 * @group_name: a group name, or %NULL
2522 * @comment: a comment
2523 * @error: return location for a #GError
2525 * Places a comment above @key from @group_name.
2526 * @group_name. If @key is %NULL then @comment will
2527 * be written above @group_name. If both @key
2528 * and @group_name are NULL, then @comment will
2529 * be written above the first group in the file.
2534 g_key_file_set_comment (GKeyFile *key_file,
2535 const gchar *group_name,
2537 const gchar *comment,
2540 g_return_if_fail (key_file != NULL);
2542 if (group_name != NULL && key != NULL)
2543 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2544 else if (group_name != NULL)
2545 g_key_file_set_group_comment (key_file, group_name, comment, error);
2547 g_key_file_set_top_comment (key_file, comment, error);
2549 if (comment != NULL)
2550 key_file->approximate_size += strlen (comment);
2554 g_key_file_get_key_comment (GKeyFile *key_file,
2555 const gchar *group_name,
2559 GKeyFileGroup *group;
2560 GKeyFileKeyValuePair *pair;
2561 GList *key_node, *tmp;
2565 group = g_key_file_lookup_group (key_file, group_name);
2568 g_set_error (error, G_KEY_FILE_ERROR,
2569 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2570 _("Key file does not have group '%s'"),
2571 group_name ? group_name : "(null)");
2576 /* First find the key the comments are supposed to be
2579 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2581 if (key_node == NULL)
2583 g_set_error (error, G_KEY_FILE_ERROR,
2584 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2585 _("Key file does not have key '%s' in group '%s'"),
2592 /* Then find all the comments already associated with the
2593 * key and concatentate them.
2595 tmp = key_node->next;
2596 if (!key_node->next)
2599 pair = (GKeyFileKeyValuePair *) tmp->data;
2600 if (pair->key != NULL)
2605 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2607 if (pair->key != NULL)
2613 while (tmp != key_node)
2615 GKeyFileKeyValuePair *pair;
2617 pair = (GKeyFileKeyValuePair *) tmp->data;
2620 string = g_string_sized_new (512);
2622 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2623 g_string_append (string, comment);
2631 comment = string->str;
2632 g_string_free (string, FALSE);
2641 get_group_comment (GKeyFile *key_file,
2642 GKeyFileGroup *group,
2651 tmp = group->key_value_pairs;
2654 GKeyFileKeyValuePair *pair;
2656 pair = (GKeyFileKeyValuePair *) tmp->data;
2658 if (pair->key != NULL)
2664 if (tmp->next == NULL)
2672 GKeyFileKeyValuePair *pair;
2674 pair = (GKeyFileKeyValuePair *) tmp->data;
2677 string = g_string_sized_new (512);
2679 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2680 g_string_append (string, comment);
2687 return g_string_free (string, FALSE);
2693 g_key_file_get_group_comment (GKeyFile *key_file,
2694 const gchar *group_name,
2698 GKeyFileGroup *group;
2700 group_node = g_key_file_lookup_group_node (key_file, group_name);
2703 g_set_error (error, G_KEY_FILE_ERROR,
2704 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2705 _("Key file does not have group '%s'"),
2706 group_name ? group_name : "(null)");
2711 group = (GKeyFileGroup *)group_node->data;
2713 return g_strdup (group->comment->value);
2715 group_node = group_node->next;
2716 group = (GKeyFileGroup *)group_node->data;
2717 return get_group_comment (key_file, group, error);
2721 g_key_file_get_top_comment (GKeyFile *key_file,
2725 GKeyFileGroup *group;
2727 /* The last group in the list should be the top (comments only)
2730 g_assert (key_file->groups != NULL);
2731 group_node = g_list_last (key_file->groups);
2732 group = (GKeyFileGroup *) group_node->data;
2733 g_assert (group->name == NULL);
2735 return get_group_comment (key_file, group, error);
2739 * g_key_file_get_comment:
2740 * @key_file: a #GKeyFile
2741 * @group_name: a group name, or %NULL
2743 * @error: return location for a #GError
2745 * Retrieves a comment above @key from @group_name.
2746 * @group_name. If @key is %NULL then @comment will
2747 * be read from above @group_name. If both @key
2748 * and @group_name are NULL, then @comment will
2749 * be read from above the first group in the file.
2751 * Returns: a comment that should be freed with g_free()
2756 g_key_file_get_comment (GKeyFile *key_file,
2757 const gchar *group_name,
2761 g_return_val_if_fail (key_file != NULL, NULL);
2763 if (group_name != NULL && key != NULL)
2764 return g_key_file_get_key_comment (key_file, group_name, key, error);
2765 else if (group_name != NULL)
2766 return g_key_file_get_group_comment (key_file, group_name, error);
2768 return g_key_file_get_top_comment (key_file, error);
2772 * g_key_file_remove_comment:
2773 * @key_file: a #GKeyFile
2774 * @group_name: a group name, or %NULL
2776 * @error: return location for a #GError
2778 * Removes a comment above @key from @group_name.
2779 * @group_name. If @key is %NULL then @comment will
2780 * be written above @group_name. If both @key
2781 * and @group_name are NULL, then @comment will
2782 * be written above the first group in the file.
2788 g_key_file_remove_comment (GKeyFile *key_file,
2789 const gchar *group_name,
2793 g_return_if_fail (key_file != NULL);
2795 if (group_name != NULL && key != NULL)
2796 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2797 else if (group_name != NULL)
2798 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2800 g_key_file_set_top_comment (key_file, NULL, error);
2804 * g_key_file_has_group:
2805 * @key_file: a #GKeyFile
2806 * @group_name: a group name
2808 * Looks whether the key file has the group @group_name.
2810 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2815 g_key_file_has_group (GKeyFile *key_file,
2816 const gchar *group_name)
2818 g_return_val_if_fail (key_file != NULL, FALSE);
2819 g_return_val_if_fail (group_name != NULL, FALSE);
2821 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2825 * g_key_file_has_key:
2826 * @key_file: a #GKeyFile
2827 * @group_name: a group name
2829 * @error: return location for a #GError
2831 * Looks whether the key file has the key @key in the group
2834 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2840 g_key_file_has_key (GKeyFile *key_file,
2841 const gchar *group_name,
2845 GKeyFileKeyValuePair *pair;
2846 GKeyFileGroup *group;
2848 g_return_val_if_fail (key_file != NULL, FALSE);
2849 g_return_val_if_fail (group_name != NULL, FALSE);
2850 g_return_val_if_fail (key != NULL, FALSE);
2852 group = g_key_file_lookup_group (key_file, group_name);
2856 g_set_error (error, G_KEY_FILE_ERROR,
2857 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2858 _("Key file does not have group '%s'"),
2859 group_name ? group_name : "(null)");
2864 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2866 return pair != NULL;
2870 g_key_file_add_group (GKeyFile *key_file,
2871 const gchar *group_name)
2873 GKeyFileGroup *group;
2875 g_return_if_fail (key_file != NULL);
2876 g_return_if_fail (group_name != NULL);
2878 if (g_key_file_lookup_group_node (key_file, group_name) != NULL)
2881 group = g_new0 (GKeyFileGroup, 1);
2882 group->name = g_strdup (group_name);
2883 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2884 key_file->groups = g_list_prepend (key_file->groups, group);
2885 key_file->approximate_size += strlen (group_name) + 3;
2886 key_file->current_group = group;
2888 if (key_file->start_group == NULL)
2889 key_file->start_group = group;
2893 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2898 g_free (pair->value);
2903 /* Be careful not to call this function on a node with data in the
2904 * lookup map without removing it from the lookup map, first.
2906 * Some current cases where this warning is not a concern are
2908 * - the node being removed is a comment node
2909 * - the entire lookup map is getting destroyed soon after
2913 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2914 GKeyFileGroup *group,
2918 GKeyFileKeyValuePair *pair;
2920 pair = (GKeyFileKeyValuePair *) pair_node->data;
2922 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2924 if (pair->key != NULL)
2925 key_file->approximate_size -= strlen (pair->key) + 1;
2927 g_assert (pair->value != NULL);
2928 key_file->approximate_size -= strlen (pair->value);
2930 g_key_file_key_value_pair_free (pair);
2932 g_list_free_1 (pair_node);
2936 g_key_file_remove_group_node (GKeyFile *key_file,
2939 GKeyFileGroup *group;
2942 group = (GKeyFileGroup *) group_node->data;
2944 /* If the current group gets deleted make the current group the last
2947 if (key_file->current_group == group)
2949 /* groups should always contain at least the top comment group,
2950 * unless g_key_file_clear has been called
2952 if (key_file->groups)
2953 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2955 key_file->current_group = NULL;
2958 /* If the start group gets deleted make the start group the first
2961 if (key_file->start_group == group)
2963 tmp = g_list_last (key_file->groups);
2966 if (tmp != group_node &&
2967 ((GKeyFileGroup *) tmp->data)->name != NULL)
2974 key_file->start_group = (GKeyFileGroup *) tmp->data;
2976 key_file->start_group = NULL;
2979 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2981 if (group->name != NULL)
2982 key_file->approximate_size -= strlen (group->name) + 3;
2984 tmp = group->key_value_pairs;
2991 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2994 g_assert (group->key_value_pairs == NULL);
2996 if (group->lookup_map)
2998 g_hash_table_destroy (group->lookup_map);
2999 group->lookup_map = NULL;
3002 g_free ((gchar *) group->name);
3004 g_list_free_1 (group_node);
3008 * g_key_file_remove_group:
3009 * @key_file: a #GKeyFile
3010 * @group_name: a group name
3011 * @error: return location for a #GError or %NULL
3013 * Removes the specified group, @group_name,
3014 * from the key file.
3019 g_key_file_remove_group (GKeyFile *key_file,
3020 const gchar *group_name,
3025 g_return_if_fail (key_file != NULL);
3026 g_return_if_fail (group_name != NULL);
3028 group_node = g_key_file_lookup_group_node (key_file, group_name);
3032 g_set_error (error, G_KEY_FILE_ERROR,
3033 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3034 _("Key file does not have group '%s'"),
3039 g_key_file_remove_group_node (key_file, group_node);
3043 g_key_file_add_key (GKeyFile *key_file,
3044 GKeyFileGroup *group,
3048 GKeyFileKeyValuePair *pair;
3050 pair = g_new0 (GKeyFileKeyValuePair, 1);
3052 pair->key = g_strdup (key);
3053 pair->value = g_strdup (value);
3055 g_hash_table_replace (group->lookup_map, pair->key, pair);
3056 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3057 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3061 * g_key_file_remove_key:
3062 * @key_file: a #GKeyFile
3063 * @group_name: a group name
3064 * @key: a key name to remove
3065 * @error: return location for a #GError or %NULL
3067 * Removes @key in @group_name from the key file.
3072 g_key_file_remove_key (GKeyFile *key_file,
3073 const gchar *group_name,
3077 GKeyFileGroup *group;
3078 GKeyFileKeyValuePair *pair;
3080 g_return_if_fail (key_file != NULL);
3081 g_return_if_fail (group_name != NULL);
3082 g_return_if_fail (key != NULL);
3086 group = g_key_file_lookup_group (key_file, group_name);
3089 g_set_error (error, G_KEY_FILE_ERROR,
3090 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3091 _("Key file does not have group '%s'"),
3092 group_name ? group_name : "(null)");
3096 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3100 g_set_error (error, G_KEY_FILE_ERROR,
3101 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3102 _("Key file does not have key '%s' in group '%s'"),
3107 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3109 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3110 g_hash_table_remove (group->lookup_map, pair->key);
3111 g_key_file_key_value_pair_free (pair);
3115 g_key_file_lookup_group_node (GKeyFile *key_file,
3116 const gchar *group_name)
3118 GKeyFileGroup *group;
3121 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3123 group = (GKeyFileGroup *) tmp->data;
3125 if (group && group->name && strcmp (group->name, group_name) == 0)
3132 static GKeyFileGroup *
3133 g_key_file_lookup_group (GKeyFile *key_file,
3134 const gchar *group_name)
3138 group_node = g_key_file_lookup_group_node (key_file, group_name);
3140 if (group_node != NULL)
3141 return (GKeyFileGroup *) group_node->data;
3147 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3148 GKeyFileGroup *group,
3153 for (key_node = group->key_value_pairs;
3155 key_node = key_node->next)
3157 GKeyFileKeyValuePair *pair;
3159 pair = (GKeyFileKeyValuePair *) key_node->data;
3161 if (pair->key && strcmp (pair->key, key) == 0)
3168 static GKeyFileKeyValuePair *
3169 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3170 GKeyFileGroup *group,
3173 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3176 /* Lines starting with # or consisting entirely of whitespace are merely
3177 * recorded, not parsed. This function assumes all leading whitespace
3178 * has been stripped.
3181 g_key_file_line_is_comment (const gchar *line)
3183 return (*line == '#' || *line == '\0' || *line == '\n');
3186 /* A group in a key file is made up of a starting '[' followed by one
3187 * or more letters making up the group name followed by ']'.
3190 g_key_file_line_is_group (const gchar *line)
3198 p = g_utf8_next_char (p);
3200 /* Group name must be non-empty
3202 if (!*p || *p == ']')
3205 while (*p && *p != ']')
3206 p = g_utf8_next_char (p);
3215 g_key_file_line_is_key_value_pair (const gchar *line)
3219 p = (gchar *) g_utf8_strchr (line, -1, '=');
3224 /* Key must be non-empty
3233 g_key_file_parse_value_as_string (GKeyFile *key_file,
3238 gchar *string_value, *p, *q0, *q;
3240 string_value = g_new0 (gchar, strlen (value) + 1);
3242 p = (gchar *) value;
3243 q0 = q = string_value;
3273 g_set_error (error, G_KEY_FILE_ERROR,
3274 G_KEY_FILE_ERROR_INVALID_VALUE,
3275 _("Key file contains escape character "
3280 if (pieces && *p == key_file->list_separator)
3281 *q = key_file->list_separator;
3295 g_set_error (error, G_KEY_FILE_ERROR,
3296 G_KEY_FILE_ERROR_INVALID_VALUE,
3297 _("Key file contains invalid escape "
3298 "sequence '%s'"), sequence);
3307 if (pieces && (*p == key_file->list_separator))
3309 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3325 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3326 *pieces = g_slist_reverse (*pieces);
3329 return string_value;
3333 g_key_file_parse_string_as_value (GKeyFile *key_file,
3334 const gchar *string,
3335 gboolean escape_separator)
3337 gchar *value, *p, *q;
3339 gboolean parsing_leading_space;
3341 length = strlen (string) + 1;
3343 /* Worst case would be that every character needs to be escaped.
3344 * In other words every character turns to two characters
3346 value = g_new0 (gchar, 2 * length);
3348 p = (gchar *) string;
3350 parsing_leading_space = TRUE;
3351 while (p < (string + length - 1))
3353 gchar escaped_character[3] = { '\\', 0, 0 };
3358 if (parsing_leading_space)
3360 escaped_character[1] = 's';
3361 strcpy (q, escaped_character);
3371 if (parsing_leading_space)
3373 escaped_character[1] = 't';
3374 strcpy (q, escaped_character);
3384 escaped_character[1] = 'n';
3385 strcpy (q, escaped_character);
3389 escaped_character[1] = 'r';
3390 strcpy (q, escaped_character);
3394 escaped_character[1] = '\\';
3395 strcpy (q, escaped_character);
3397 parsing_leading_space = FALSE;
3400 if (escape_separator && *p == key_file->list_separator)
3402 escaped_character[1] = key_file->list_separator;
3403 strcpy (q, escaped_character);
3405 parsing_leading_space = TRUE;
3411 parsing_leading_space = FALSE;
3423 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3427 gchar *end_of_valid_int;
3432 long_value = strtol (value, &end_of_valid_int, 10);
3434 if (*value == '\0' || *end_of_valid_int != '\0')
3436 g_set_error (error, G_KEY_FILE_ERROR,
3437 G_KEY_FILE_ERROR_INVALID_VALUE,
3438 _("Value '%s' cannot be interpreted as a number."), value);
3442 int_value = long_value;
3443 if (int_value != long_value || errno == ERANGE)
3447 G_KEY_FILE_ERROR_INVALID_VALUE,
3448 _("Integer value '%s' out of range"), value);
3456 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3460 return g_strdup_printf ("%d", value);
3464 g_key_file_parse_value_as_double (GKeyFile *key_file,
3468 gchar *end_of_valid_d;
3469 gdouble double_value = 0;
3471 double_value = g_ascii_strtod (value, &end_of_valid_d);
3473 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3474 g_set_error (error, G_KEY_FILE_ERROR,
3475 G_KEY_FILE_ERROR_INVALID_VALUE,
3476 _("Value '%s' cannot be interpreted as a float number."), value);
3478 return double_value;
3482 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3488 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3490 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3494 g_set_error (error, G_KEY_FILE_ERROR,
3495 G_KEY_FILE_ERROR_INVALID_VALUE,
3496 _("Value '%s' cannot be interpreted as a boolean."), value);
3502 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3506 return g_strdup ("true");
3508 return g_strdup ("false");
3512 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3519 string = g_string_sized_new (512);
3521 lines = g_strsplit (value, "\n", 0);
3523 for (i = 0; lines[i] != NULL; i++)
3525 if (lines[i][0] != '#')
3526 g_string_append_printf (string, "%s\n", lines[i]);
3528 g_string_append_printf (string, "%s\n", lines[i] + 1);
3532 return g_string_free (string, FALSE);
3536 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3537 const gchar *comment)
3543 string = g_string_sized_new (512);
3545 lines = g_strsplit (comment, "\n", 0);
3547 for (i = 0; lines[i] != NULL; i++)
3548 g_string_append_printf (string, "#%s%s", lines[i],
3549 lines[i + 1] == NULL? "" : "\n");
3552 return g_string_free (string, FALSE);
3555 #define __G_KEY_FILE_C__
3556 #include "galiasdef.c"