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;
933 GList *group_node, *key_file_node;
935 g_return_val_if_fail (key_file != NULL, NULL);
937 data_string = g_string_sized_new (2 * key_file->approximate_size);
939 for (group_node = g_list_last (key_file->groups);
941 group_node = group_node->prev)
943 GKeyFileGroup *group;
945 group = (GKeyFileGroup *) group_node->data;
947 if (group->comment != NULL)
948 g_string_append_printf (data_string, "%s\n", group->comment->value);
949 if (group->name != NULL)
950 g_string_append_printf (data_string, "[%s]\n", group->name);
952 for (key_file_node = g_list_last (group->key_value_pairs);
953 key_file_node != NULL;
954 key_file_node = key_file_node->prev)
956 GKeyFileKeyValuePair *pair;
958 pair = (GKeyFileKeyValuePair *) key_file_node->data;
960 if (pair->key != NULL)
961 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
963 g_string_append_printf (data_string, "%s\n", pair->value);
968 *length = data_string->len;
970 data = data_string->str;
972 g_string_free (data_string, FALSE);
978 * g_key_file_get_keys:
979 * @key_file: a #GKeyFile
980 * @group_name: a group name
981 * @length: return location for the number of keys returned, or %NULL
982 * @error: return location for a #GError, or %NULL
984 * Returns all keys for the group name @group_name. The array of
985 * returned keys will be %NULL-terminated, so @length may
986 * optionally be %NULL. In the event that the @group_name cannot
987 * be found, %NULL is returned and @error is set to
988 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
990 * Return value: a newly-allocated %NULL-terminated array of
991 * strings. Use g_strfreev() to free it.
996 g_key_file_get_keys (GKeyFile *key_file,
997 const gchar *group_name,
1001 GKeyFileGroup *group;
1006 g_return_val_if_fail (key_file != NULL, NULL);
1007 g_return_val_if_fail (group_name != NULL, NULL);
1009 group = g_key_file_lookup_group (key_file, group_name);
1013 g_set_error (error, G_KEY_FILE_ERROR,
1014 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1015 _("Key file does not have group '%s'"),
1016 group_name ? group_name : "(null)");
1021 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1023 GKeyFileKeyValuePair *pair;
1025 pair = (GKeyFileKeyValuePair *) tmp->data;
1031 keys = g_new0 (gchar *, num_keys + 1);
1034 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1036 GKeyFileKeyValuePair *pair;
1038 pair = (GKeyFileKeyValuePair *) tmp->data;
1042 keys[i] = g_strdup (pair->key);
1047 keys[num_keys] = NULL;
1056 * g_key_file_get_start_group:
1057 * @key_file: a #GKeyFile
1059 * Returns the name of the start group of the file.
1061 * Return value: The start group of the key file.
1066 g_key_file_get_start_group (GKeyFile *key_file)
1068 g_return_val_if_fail (key_file != NULL, NULL);
1070 if (key_file->start_group)
1071 return g_strdup (key_file->start_group->name);
1077 * g_key_file_get_groups:
1078 * @key_file: a #GKeyFile
1079 * @length: return location for the number of returned groups, or %NULL
1081 * Returns all groups in the key file loaded with @key_file. The
1082 * array of returned groups will be %NULL-terminated, so @length may
1083 * optionally be %NULL.
1085 * Return value: a newly-allocated %NULL-terminated array of strings.
1086 * Use g_strfreev() to free it.
1090 g_key_file_get_groups (GKeyFile *key_file,
1095 gsize i, num_groups;
1097 g_return_val_if_fail (key_file != NULL, NULL);
1099 num_groups = g_list_length (key_file->groups);
1101 g_assert (num_groups > 0);
1103 /* Only need num_groups instead of num_groups + 1
1104 * because the first group of the file (last in the
1105 * list) is always the comment group at the top,
1108 groups = g_new0 (gchar *, num_groups);
1110 group_node = g_list_last (key_file->groups);
1112 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1115 for (group_node = group_node->prev;
1117 group_node = group_node->prev)
1119 GKeyFileGroup *group;
1121 group = (GKeyFileGroup *) group_node->data;
1123 g_assert (group->name != NULL);
1125 groups[i++] = g_strdup (group->name);
1136 * g_key_file_get_value:
1137 * @key_file: a #GKeyFile
1138 * @group_name: a group name
1140 * @error: return location for a #GError, or %NULL
1142 * Returns the value associated with @key under @group_name.
1144 * In the event the key cannot be found, %NULL is returned and
1145 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1146 * event that the @group_name cannot be found, %NULL is returned
1147 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1149 * Return value: a newly allocated string or %NULL if the specified
1150 * key cannot be found.
1155 g_key_file_get_value (GKeyFile *key_file,
1156 const gchar *group_name,
1160 GKeyFileGroup *group;
1161 GKeyFileKeyValuePair *pair;
1162 gchar *value = NULL;
1164 g_return_val_if_fail (key_file != NULL, NULL);
1165 g_return_val_if_fail (group_name != NULL, NULL);
1166 g_return_val_if_fail (key != NULL, NULL);
1168 group = g_key_file_lookup_group (key_file, group_name);
1172 g_set_error (error, G_KEY_FILE_ERROR,
1173 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1174 _("Key file does not have group '%s'"),
1175 group_name ? group_name : "(null)");
1179 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1182 value = g_strdup (pair->value);
1184 g_set_error (error, G_KEY_FILE_ERROR,
1185 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1186 _("Key file does not have key '%s'"), key);
1192 * g_key_file_set_value:
1193 * @key_file: a #GKeyFile
1194 * @group_name: a group name
1198 * Associates a new value with @key under @group_name. If @key
1199 * cannot be found then it is created. If @group_name cannot be
1200 * found then it is created.
1205 g_key_file_set_value (GKeyFile *key_file,
1206 const gchar *group_name,
1210 GKeyFileGroup *group;
1211 GKeyFileKeyValuePair *pair;
1213 g_return_if_fail (key_file != NULL);
1214 g_return_if_fail (group_name != NULL);
1215 g_return_if_fail (key != NULL);
1216 g_return_if_fail (value != NULL);
1218 group = g_key_file_lookup_group (key_file, group_name);
1222 g_key_file_add_group (key_file, group_name);
1223 group = (GKeyFileGroup *) key_file->groups->data;
1225 g_key_file_add_key (key_file, group, key, value);
1229 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1232 g_key_file_add_key (key_file, group, key, value);
1235 g_free (pair->value);
1236 pair->value = g_strdup (value);
1242 * g_key_file_get_string:
1243 * @key_file: a #GKeyFile
1244 * @group_name: a group name
1246 * @error: return location for a #GError, or %NULL
1248 * Returns the value associated with @key under @group_name.
1250 * In the event the key cannot be found, %NULL is returned and
1251 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1252 * event that the @group_name cannot be found, %NULL is returned
1253 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1255 * Return value: a newly allocated string or %NULL if the specified
1256 * key cannot be found.
1261 g_key_file_get_string (GKeyFile *key_file,
1262 const gchar *group_name,
1266 gchar *value, *string_value;
1267 GError *key_file_error;
1269 g_return_val_if_fail (key_file != NULL, NULL);
1270 g_return_val_if_fail (group_name != NULL, NULL);
1271 g_return_val_if_fail (key != NULL, NULL);
1273 key_file_error = NULL;
1275 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1279 g_propagate_error (error, key_file_error);
1283 if (!g_utf8_validate (value, -1, NULL))
1285 g_set_error (error, G_KEY_FILE_ERROR,
1286 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1287 _("Key file contains key '%s' with value '%s' "
1288 "which is not UTF-8"), key, value);
1293 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1299 if (g_error_matches (key_file_error,
1301 G_KEY_FILE_ERROR_INVALID_VALUE))
1303 g_set_error (error, G_KEY_FILE_ERROR,
1304 G_KEY_FILE_ERROR_INVALID_VALUE,
1305 _("Key file contains key '%s' "
1306 "which has value that cannot be interpreted."),
1308 g_error_free (key_file_error);
1311 g_propagate_error (error, key_file_error);
1314 return string_value;
1318 * g_key_file_set_string:
1319 * @key_file: a #GKeyFile
1320 * @group_name: a group name
1324 * Associates a new string value with @key under @group_name. If
1325 * @key cannot be found then it is created. If @group_name
1326 * cannot be found then it is created.
1331 g_key_file_set_string (GKeyFile *key_file,
1332 const gchar *group_name,
1334 const gchar *string)
1338 g_return_if_fail (key_file != NULL);
1339 g_return_if_fail (group_name != NULL);
1340 g_return_if_fail (key != NULL);
1341 g_return_if_fail (string != NULL);
1343 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1344 g_key_file_set_value (key_file, group_name, key, value);
1349 * g_key_file_get_string_list:
1350 * @key_file: a #GKeyFile
1351 * @group_name: a group name
1353 * @length: return location for the number of returned strings, or %NULL
1354 * @error: return location for a #GError, or %NULL
1356 * Returns the values associated with @key under @group_name.
1358 * In the event the key cannot be found, %NULL is returned and
1359 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1360 * event that the @group_name cannot be found, %NULL is returned
1361 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1363 * Return value: a %NULL-terminated string array or %NULL if the specified
1364 * key cannot be found. The array should be freed with g_strfreev().
1369 g_key_file_get_string_list (GKeyFile *key_file,
1370 const gchar *group_name,
1375 GError *key_file_error = NULL;
1376 gchar *value, *string_value, **values;
1378 GSList *p, *pieces = NULL;
1380 g_return_val_if_fail (key_file != NULL, NULL);
1381 g_return_val_if_fail (group_name != NULL, NULL);
1382 g_return_val_if_fail (key != NULL, NULL);
1384 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1388 g_propagate_error (error, key_file_error);
1392 if (!g_utf8_validate (value, -1, NULL))
1394 g_set_error (error, G_KEY_FILE_ERROR,
1395 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1396 _("Key file contains key '%s' with value '%s' "
1397 "which is not UTF-8"), key, value);
1402 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1404 g_free (string_value);
1408 if (g_error_matches (key_file_error,
1410 G_KEY_FILE_ERROR_INVALID_VALUE))
1412 g_set_error (error, G_KEY_FILE_ERROR,
1413 G_KEY_FILE_ERROR_INVALID_VALUE,
1414 _("Key file contains key '%s' "
1415 "which has value that cannot be interpreted."),
1417 g_error_free (key_file_error);
1420 g_propagate_error (error, key_file_error);
1423 len = g_slist_length (pieces);
1424 values = g_new0 (gchar *, len + 1);
1425 for (p = pieces, i = 0; p; p = p->next)
1426 values[i++] = p->data;
1429 g_slist_free (pieces);
1438 * g_key_file_set_string_list:
1439 * @key_file: a #GKeyFile
1440 * @group_name: a group name
1442 * @list: an array of locale string values
1443 * @length: number of locale string values in @list
1445 * Associates a list of string values for @key under @group_name.
1446 * If @key cannot be found then it is created. If @group_name
1447 * cannot be found then it is created.
1452 g_key_file_set_string_list (GKeyFile *key_file,
1453 const gchar *group_name,
1455 const gchar * const list[],
1458 GString *value_list;
1461 g_return_if_fail (key_file != NULL);
1462 g_return_if_fail (group_name != NULL);
1463 g_return_if_fail (key != NULL);
1464 g_return_if_fail (list != NULL);
1466 value_list = g_string_sized_new (length * 128);
1467 for (i = 0; list[i] != NULL && i < length; i++)
1471 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1472 g_string_append (value_list, value);
1473 g_string_append_c (value_list, key_file->list_separator);
1478 g_key_file_set_value (key_file, group_name, key, value_list->str);
1479 g_string_free (value_list, TRUE);
1483 * g_key_file_set_locale_string:
1484 * @key_file: a #GKeyFile
1485 * @group_name: a group name
1490 * Associates a string value for @key and @locale under
1491 * @group_name. If the translation for @key cannot be found
1492 * then it is created.
1497 g_key_file_set_locale_string (GKeyFile *key_file,
1498 const gchar *group_name,
1500 const gchar *locale,
1501 const gchar *string)
1503 gchar *full_key, *value;
1505 g_return_if_fail (key_file != NULL);
1506 g_return_if_fail (group_name != NULL);
1507 g_return_if_fail (key != NULL);
1508 g_return_if_fail (locale != NULL);
1509 g_return_if_fail (string != NULL);
1511 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1512 full_key = g_strdup_printf ("%s[%s]", key, locale);
1513 g_key_file_set_value (key_file, group_name, full_key, value);
1518 extern GSList *_g_compute_locale_variants (const gchar *locale);
1521 * g_key_file_get_locale_string:
1522 * @key_file: a #GKeyFile
1523 * @group_name: a group name
1525 * @locale: a locale or %NULL
1526 * @error: return location for a #GError, or %NULL
1528 * Returns the value associated with @key under @group_name
1529 * translated in the given @locale if available. If @locale is
1530 * %NULL then the current locale is assumed.
1532 * If @key cannot be found then %NULL is returned and @error is set to
1533 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1534 * with @key cannot be interpreted or no suitable translation can
1535 * be found then the untranslated value is returned.
1537 * Return value: a newly allocated string or %NULL if the specified
1538 * key cannot be found.
1543 g_key_file_get_locale_string (GKeyFile *key_file,
1544 const gchar *group_name,
1546 const gchar *locale,
1549 gchar *candidate_key, *translated_value;
1550 GError *key_file_error;
1552 gboolean free_languages = FALSE;
1555 g_return_val_if_fail (key_file != NULL, NULL);
1556 g_return_val_if_fail (group_name != NULL, NULL);
1557 g_return_val_if_fail (key != NULL, NULL);
1559 candidate_key = NULL;
1560 translated_value = NULL;
1561 key_file_error = NULL;
1567 list = _g_compute_locale_variants (locale);
1569 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1570 for (l = list, i = 0; l; l = l->next, i++)
1571 languages[i] = l->data;
1572 languages[i] = NULL;
1574 g_slist_free (list);
1575 free_languages = TRUE;
1579 languages = (gchar **) g_get_language_names ();
1580 free_languages = FALSE;
1583 for (i = 0; languages[i]; i++)
1585 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1587 translated_value = g_key_file_get_string (key_file,
1589 candidate_key, NULL);
1590 g_free (candidate_key);
1592 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1595 g_free (translated_value);
1596 translated_value = NULL;
1599 /* Fallback to untranslated key
1601 if (!translated_value)
1603 translated_value = g_key_file_get_string (key_file, group_name, key,
1606 if (!translated_value)
1607 g_propagate_error (error, key_file_error);
1611 g_strfreev (languages);
1613 return translated_value;
1617 * g_key_file_get_locale_string_list:
1618 * @key_file: a #GKeyFile
1619 * @group_name: a group name
1622 * @length: return location for the number of returned strings or %NULL
1623 * @error: return location for a #GError or %NULL
1625 * Returns the values associated with @key under @group_name
1626 * translated in the given @locale if available. If @locale is
1627 * %NULL then the current locale is assumed.
1629 * If @key cannot be found then %NULL is returned and @error is set to
1630 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1631 * with @key cannot be interpreted or no suitable translations
1632 * can be found then the untranslated values are returned.
1633 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1635 * Return value: a newly allocated %NULL-terminated string array
1636 * or %NULL if the key isn't found. The string array should be freed
1637 * with g_strfreev().
1642 g_key_file_get_locale_string_list (GKeyFile *key_file,
1643 const gchar *group_name,
1645 const gchar *locale,
1649 GError *key_file_error;
1650 gchar **values, *value;
1652 g_return_val_if_fail (key_file != NULL, NULL);
1653 g_return_val_if_fail (group_name != NULL, NULL);
1654 g_return_val_if_fail (key != NULL, NULL);
1656 key_file_error = NULL;
1658 value = g_key_file_get_locale_string (key_file, group_name,
1663 g_propagate_error (error, key_file_error);
1668 if (value[strlen (value) - 1] == ';')
1669 value[strlen (value) - 1] = '\0';
1671 values = g_strsplit (value, ";", 0);
1676 *length = g_strv_length (values);
1682 * g_key_file_set_locale_string_list:
1683 * @key_file: a #GKeyFile
1684 * @group_name: a group name
1687 * @list: a %NULL-terminated array of locale string values
1688 * @length: the length of @list
1690 * Associates a list of string values for @key and @locale under
1691 * @group_name. If the translation for @key cannot be found then
1697 g_key_file_set_locale_string_list (GKeyFile *key_file,
1698 const gchar *group_name,
1700 const gchar *locale,
1701 const gchar * const list[],
1704 GString *value_list;
1708 g_return_if_fail (key_file != NULL);
1709 g_return_if_fail (group_name != NULL);
1710 g_return_if_fail (key != NULL);
1711 g_return_if_fail (locale != NULL);
1712 g_return_if_fail (length != 0);
1714 value_list = g_string_sized_new (length * 128);
1715 for (i = 0; list[i] != NULL && i < length; i++)
1719 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1721 g_string_append (value_list, value);
1722 g_string_append_c (value_list, ';');
1727 full_key = g_strdup_printf ("%s[%s]", key, locale);
1728 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1730 g_string_free (value_list, TRUE);
1734 * g_key_file_get_boolean:
1735 * @key_file: a #GKeyFile
1736 * @group_name: a group name
1738 * @error: return location for a #GError
1740 * Returns the value associated with @key under @group_name as a
1743 * If @key cannot be found then the return value is undefined and
1744 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1745 * the value associated with @key cannot be interpreted as a boolean
1746 * then the return value is also undefined and @error is set to
1747 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1749 * Return value: the value associated with the key as a boolean
1753 g_key_file_get_boolean (GKeyFile *key_file,
1754 const gchar *group_name,
1758 GError *key_file_error = NULL;
1760 gboolean bool_value;
1762 g_return_val_if_fail (key_file != NULL, FALSE);
1763 g_return_val_if_fail (group_name != NULL, FALSE);
1764 g_return_val_if_fail (key != NULL, FALSE);
1766 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1770 g_propagate_error (error, key_file_error);
1774 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1780 if (g_error_matches (key_file_error,
1782 G_KEY_FILE_ERROR_INVALID_VALUE))
1784 g_set_error (error, G_KEY_FILE_ERROR,
1785 G_KEY_FILE_ERROR_INVALID_VALUE,
1786 _("Key file contains key '%s' "
1787 "which has value that cannot be interpreted."),
1789 g_error_free (key_file_error);
1792 g_propagate_error (error, key_file_error);
1799 * g_key_file_set_boolean:
1800 * @key_file: a #GKeyFile
1801 * @group_name: a group name
1803 * @value: %TRUE or %FALSE
1805 * Associates a new boolean value with @key under @group_name.
1806 * If @key cannot be found then it is created.
1811 g_key_file_set_boolean (GKeyFile *key_file,
1812 const gchar *group_name,
1818 g_return_if_fail (key_file != NULL);
1819 g_return_if_fail (group_name != NULL);
1820 g_return_if_fail (key != NULL);
1822 result = g_key_file_parse_boolean_as_value (key_file, value);
1823 g_key_file_set_value (key_file, group_name, key, result);
1828 * g_key_file_get_boolean_list:
1829 * @key_file: a #GKeyFile
1830 * @group_name: a group name
1832 * @length: the number of booleans returned
1833 * @error: return location for a #GError
1835 * Returns the values associated with @key under @group_name as
1836 * booleans. If @group_name is %NULL, the start_group is used.
1838 * If @key cannot be found then the return value is undefined and
1839 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1840 * the values associated with @key cannot be interpreted as booleans
1841 * then the return value is also undefined and @error is set to
1842 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1844 * Return value: the values associated with the key as a boolean
1849 g_key_file_get_boolean_list (GKeyFile *key_file,
1850 const gchar *group_name,
1855 GError *key_file_error;
1857 gboolean *bool_values;
1860 g_return_val_if_fail (key_file != NULL, NULL);
1861 g_return_val_if_fail (group_name != NULL, NULL);
1862 g_return_val_if_fail (key != NULL, NULL);
1864 key_file_error = NULL;
1866 values = g_key_file_get_string_list (key_file, group_name, key,
1867 &num_bools, &key_file_error);
1870 g_propagate_error (error, key_file_error);
1875 bool_values = g_new0 (gboolean, num_bools);
1877 for (i = 0; i < num_bools; i++)
1879 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1885 g_propagate_error (error, key_file_error);
1886 g_strfreev (values);
1887 g_free (bool_values);
1892 g_strfreev (values);
1895 *length = num_bools;
1901 * g_key_file_set_boolean_list:
1902 * @key_file: a #GKeyFile
1903 * @group_name: a group name
1905 * @list: an array of boolean values
1906 * @length: length of @list
1908 * Associates a list of boolean values with @key under
1909 * @group_name. If @key cannot be found then it is created.
1910 * If @group_name is %NULL, the start_group is used.
1915 g_key_file_set_boolean_list (GKeyFile *key_file,
1916 const gchar *group_name,
1921 GString *value_list;
1924 g_return_if_fail (key_file != NULL);
1925 g_return_if_fail (group_name != NULL);
1926 g_return_if_fail (key != NULL);
1927 g_return_if_fail (list != NULL);
1929 value_list = g_string_sized_new (length * 8);
1930 for (i = 0; i < length; i++)
1934 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1936 g_string_append (value_list, value);
1937 g_string_append_c (value_list, key_file->list_separator);
1942 g_key_file_set_value (key_file, group_name, key, value_list->str);
1943 g_string_free (value_list, TRUE);
1947 * g_key_file_get_integer:
1948 * @key_file: a #GKeyFile
1949 * @group_name: a group name
1951 * @error: return location for a #GError
1953 * Returns the value associated with @key under @group_name as an
1954 * integer. If @group_name is %NULL, the start_group is used.
1956 * If @key cannot be found then the return value is undefined and
1957 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1958 * the value associated with @key cannot be interpreted as an integer
1959 * then the return value is also undefined and @error is set to
1960 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1962 * Return value: the value associated with the key as an integer.
1967 g_key_file_get_integer (GKeyFile *key_file,
1968 const gchar *group_name,
1972 GError *key_file_error;
1976 g_return_val_if_fail (key_file != NULL, -1);
1977 g_return_val_if_fail (group_name != NULL, -1);
1978 g_return_val_if_fail (key != NULL, -1);
1980 key_file_error = NULL;
1982 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1986 g_propagate_error (error, key_file_error);
1990 int_value = g_key_file_parse_value_as_integer (key_file, value,
1996 if (g_error_matches (key_file_error,
1998 G_KEY_FILE_ERROR_INVALID_VALUE))
2000 g_set_error (error, G_KEY_FILE_ERROR,
2001 G_KEY_FILE_ERROR_INVALID_VALUE,
2002 _("Key file contains key '%s' in group '%s' "
2003 "which has value that cannot be interpreted."), key,
2005 g_error_free (key_file_error);
2008 g_propagate_error (error, key_file_error);
2015 * g_key_file_set_integer:
2016 * @key_file: a #GKeyFile
2017 * @group_name: a group name
2019 * @value: an integer value
2021 * Associates a new integer value with @key under @group_name.
2022 * If @key cannot be found then it is created.
2027 g_key_file_set_integer (GKeyFile *key_file,
2028 const gchar *group_name,
2034 g_return_if_fail (key_file != NULL);
2035 g_return_if_fail (group_name != NULL);
2036 g_return_if_fail (key != NULL);
2038 result = g_key_file_parse_integer_as_value (key_file, value);
2039 g_key_file_set_value (key_file, group_name, key, result);
2044 * g_key_file_get_integer_list:
2045 * @key_file: a #GKeyFile
2046 * @group_name: a group name
2048 * @length: the number of integers returned
2049 * @error: return location for a #GError
2051 * Returns the values associated with @key under @group_name as
2054 * If @key cannot be found then the return value is undefined and
2055 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2056 * the values associated with @key cannot be interpreted as integers
2057 * then the return value is also undefined and @error is set to
2058 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2060 * Return value: the values associated with the key as a integer
2065 g_key_file_get_integer_list (GKeyFile *key_file,
2066 const gchar *group_name,
2071 GError *key_file_error = NULL;
2076 g_return_val_if_fail (key_file != NULL, NULL);
2077 g_return_val_if_fail (group_name != NULL, NULL);
2078 g_return_val_if_fail (key != NULL, NULL);
2080 values = g_key_file_get_string_list (key_file, group_name, key,
2081 &num_ints, &key_file_error);
2084 g_propagate_error (error, key_file_error);
2089 int_values = g_new0 (gint, num_ints);
2091 for (i = 0; i < num_ints; i++)
2093 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2099 g_propagate_error (error, key_file_error);
2100 g_strfreev (values);
2101 g_free (int_values);
2106 g_strfreev (values);
2115 * g_key_file_set_integer_list:
2116 * @key_file: a #GKeyFile
2117 * @group_name: a group name
2119 * @list: an array of integer values
2120 * @length: number of integer values in @list
2122 * Associates a list of integer values with @key under
2123 * @group_name. If @key cannot be found then it is created.
2128 g_key_file_set_integer_list (GKeyFile *key_file,
2129 const gchar *group_name,
2137 g_return_if_fail (key_file != NULL);
2138 g_return_if_fail (group_name != NULL);
2139 g_return_if_fail (key != NULL);
2140 g_return_if_fail (list != NULL);
2142 values = g_string_sized_new (length * 16);
2143 for (i = 0; i < length; i++)
2147 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2149 g_string_append (values, value);
2150 g_string_append_c (values, ';');
2155 g_key_file_set_value (key_file, group_name, key, values->str);
2156 g_string_free (values, TRUE);
2160 * g_key_file_get_double:
2161 * @key_file: a #GKeyFile
2162 * @group_name: a group name
2164 * @error: return location for a #GError
2166 * Returns the value associated with @key under @group_name as an
2167 * integer. If @group_name is %NULL, the start_group is used.
2169 * If @key cannot be found then the return value is undefined and
2170 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2171 * the value associated with @key cannot be interpreted as a double
2172 * then the return value is also undefined and @error is set to
2173 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2175 * Return value: the value associated with the key as a double.
2180 g_key_file_get_double (GKeyFile *key_file,
2181 const gchar *group_name,
2185 GError *key_file_error;
2187 gdouble double_value;
2189 g_return_val_if_fail (key_file != NULL, -1);
2190 g_return_val_if_fail (group_name != NULL, -1);
2191 g_return_val_if_fail (key != NULL, -1);
2193 key_file_error = NULL;
2195 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2199 g_propagate_error (error, key_file_error);
2203 double_value = g_key_file_parse_value_as_double (key_file, value,
2209 if (g_error_matches (key_file_error,
2211 G_KEY_FILE_ERROR_INVALID_VALUE))
2213 g_set_error (error, G_KEY_FILE_ERROR,
2214 G_KEY_FILE_ERROR_INVALID_VALUE,
2215 _("Key file contains key '%s' in group '%s' "
2216 "which has value that cannot be interpreted."), key,
2218 g_error_free (key_file_error);
2221 g_propagate_error (error, key_file_error);
2224 return double_value;
2228 * g_key_file_set_double:
2229 * @key_file: a #GKeyFile
2230 * @group_name: a group name
2232 * @value: an double value
2234 * Associates a new double value with @key under @group_name.
2235 * If @key cannot be found then it is created. If @group_name
2236 * is %NULL, the start group is used.
2241 g_key_file_set_double (GKeyFile *key_file,
2242 const gchar *group_name,
2246 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2248 g_return_if_fail (key_file != NULL);
2249 g_return_if_fail (group_name != NULL);
2250 g_return_if_fail (key != NULL);
2252 g_ascii_dtostr ( result, sizeof (result), value );
2253 g_key_file_set_value (key_file, group_name, key, result);
2257 * g_key_file_get_double_list:
2258 * @key_file: a #GKeyFile
2259 * @group_name: a group name
2261 * @length: the number of doubles returned
2262 * @error: return location for a #GError
2264 * Returns the values associated with @key under @group_name as
2265 * doubles. If @group_name is %NULL, the start group is used.
2267 * If @key cannot be found then the return value is undefined and
2268 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2269 * the values associated with @key cannot be interpreted as doubles
2270 * then the return value is also undefined and @error is set to
2271 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2273 * Return value: the values associated with the key as a double
2278 g_key_file_get_double_list (GKeyFile *key_file,
2279 const gchar *group_name,
2284 GError *key_file_error = NULL;
2286 gdouble *double_values;
2287 gsize i, num_doubles;
2289 g_return_val_if_fail (key_file != NULL, NULL);
2290 g_return_val_if_fail (group_name != NULL, NULL);
2291 g_return_val_if_fail (key != NULL, NULL);
2293 values = g_key_file_get_string_list (key_file, group_name, key,
2294 &num_doubles, &key_file_error);
2297 g_propagate_error (error, key_file_error);
2302 double_values = g_new0 (gdouble, num_doubles);
2304 for (i = 0; i < num_doubles; i++)
2306 double_values[i] = g_key_file_parse_value_as_double (key_file,
2312 g_propagate_error (error, key_file_error);
2313 g_strfreev (values);
2314 g_free (double_values);
2319 g_strfreev (values);
2322 *length = num_doubles;
2324 return double_values;
2328 * g_key_file_set_double_list:
2329 * @key_file: a #GKeyFile
2330 * @group_name: a group name
2332 * @list: an array of double values
2333 * @length: number of double values in @list
2335 * Associates a list of double values with @key under
2336 * @group_name. If @key cannot be found then it is created.
2337 * If @group_name is %NULL the start group is used.
2342 g_key_file_set_double_list (GKeyFile *key_file,
2343 const gchar *group_name,
2351 g_return_if_fail (key_file != NULL);
2352 g_return_if_fail (group_name != NULL);
2353 g_return_if_fail (key != NULL);
2354 g_return_if_fail (list != NULL);
2356 values = g_string_sized_new (length * 16);
2357 for (i = 0; i < length; i++)
2359 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2361 g_ascii_dtostr( result, sizeof (result), list[i] );
2363 g_string_append (values, result);
2364 g_string_append_c (values, ';');
2367 g_key_file_set_value (key_file, group_name, key, values->str);
2368 g_string_free (values, TRUE);
2372 g_key_file_set_key_comment (GKeyFile *key_file,
2373 const gchar *group_name,
2375 const gchar *comment,
2378 GKeyFileGroup *group;
2379 GKeyFileKeyValuePair *pair;
2380 GList *key_node, *comment_node, *tmp;
2382 group = g_key_file_lookup_group (key_file, group_name);
2385 g_set_error (error, G_KEY_FILE_ERROR,
2386 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2387 _("Key file does not have group '%s'"),
2388 group_name ? group_name : "(null)");
2393 /* First find the key the comments are supposed to be
2396 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2398 if (key_node == NULL)
2400 g_set_error (error, G_KEY_FILE_ERROR,
2401 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2402 _("Key file does not have key '%s' in group '%s'"),
2407 /* Then find all the comments already associated with the
2410 tmp = key_node->next;
2413 GKeyFileKeyValuePair *pair;
2415 pair = (GKeyFileKeyValuePair *) tmp->data;
2417 if (pair->key != NULL)
2422 g_key_file_remove_key_value_pair_node (key_file, group,
2426 if (comment == NULL)
2429 /* Now we can add our new comment
2431 pair = g_new0 (GKeyFileKeyValuePair, 1);
2434 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2436 key_node = g_list_insert (key_node, pair, 1);
2440 g_key_file_set_group_comment (GKeyFile *key_file,
2441 const gchar *group_name,
2442 const gchar *comment,
2445 GKeyFileGroup *group;
2447 group = g_key_file_lookup_group (key_file, group_name);
2450 g_set_error (error, G_KEY_FILE_ERROR,
2451 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2452 _("Key file does not have group '%s'"),
2453 group_name ? group_name : "(null)");
2458 /* First remove any existing comment
2462 g_key_file_key_value_pair_free (group->comment);
2463 group->comment = NULL;
2466 if (comment == NULL)
2469 /* Now we can add our new comment
2471 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2473 group->comment->key = NULL;
2474 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2478 g_key_file_set_top_comment (GKeyFile *key_file,
2479 const gchar *comment,
2483 GKeyFileGroup *group;
2484 GKeyFileKeyValuePair *pair;
2486 /* The last group in the list should be the top (comments only)
2489 g_assert (key_file->groups != NULL);
2490 group_node = g_list_last (key_file->groups);
2491 group = (GKeyFileGroup *) group_node->data;
2492 g_assert (group->name == NULL);
2494 /* Note all keys must be comments at the top of
2495 * the file, so we can just free it all.
2497 if (group->key_value_pairs != NULL)
2499 g_list_foreach (group->key_value_pairs,
2500 (GFunc) g_key_file_key_value_pair_free,
2502 g_list_free (group->key_value_pairs);
2503 group->key_value_pairs = NULL;
2506 if (comment == NULL)
2509 pair = g_new0 (GKeyFileKeyValuePair, 1);
2512 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2514 group->key_value_pairs =
2515 g_list_prepend (group->key_value_pairs, pair);
2519 * g_key_file_set_comment:
2520 * @key_file: a #GKeyFile
2521 * @group_name: a group name, or %NULL
2523 * @comment: a comment
2524 * @error: return location for a #GError
2526 * Places a comment above @key from @group_name.
2527 * @group_name. If @key is %NULL then @comment will
2528 * be written above @group_name. If both @key
2529 * and @group_name are NULL, then @comment will
2530 * be written above the first group in the file.
2535 g_key_file_set_comment (GKeyFile *key_file,
2536 const gchar *group_name,
2538 const gchar *comment,
2541 g_return_if_fail (key_file != NULL);
2543 if (group_name != NULL && key != NULL)
2544 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2545 else if (group_name != NULL)
2546 g_key_file_set_group_comment (key_file, group_name, comment, error);
2548 g_key_file_set_top_comment (key_file, comment, error);
2550 if (comment != NULL)
2551 key_file->approximate_size += strlen (comment);
2555 g_key_file_get_key_comment (GKeyFile *key_file,
2556 const gchar *group_name,
2560 GKeyFileGroup *group;
2561 GKeyFileKeyValuePair *pair;
2562 GList *key_node, *tmp;
2566 group = g_key_file_lookup_group (key_file, group_name);
2569 g_set_error (error, G_KEY_FILE_ERROR,
2570 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2571 _("Key file does not have group '%s'"),
2572 group_name ? group_name : "(null)");
2577 /* First find the key the comments are supposed to be
2580 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2582 if (key_node == NULL)
2584 g_set_error (error, G_KEY_FILE_ERROR,
2585 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2586 _("Key file does not have key '%s' in group '%s'"),
2593 /* Then find all the comments already associated with the
2594 * key and concatentate them.
2596 tmp = key_node->next;
2597 if (!key_node->next)
2600 pair = (GKeyFileKeyValuePair *) tmp->data;
2601 if (pair->key != NULL)
2606 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2608 if (pair->key != NULL)
2614 while (tmp != key_node)
2616 GKeyFileKeyValuePair *pair;
2618 pair = (GKeyFileKeyValuePair *) tmp->data;
2621 string = g_string_sized_new (512);
2623 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2624 g_string_append (string, comment);
2632 comment = string->str;
2633 g_string_free (string, FALSE);
2642 get_group_comment (GKeyFile *key_file,
2643 GKeyFileGroup *group,
2652 tmp = group->key_value_pairs;
2655 GKeyFileKeyValuePair *pair;
2657 pair = (GKeyFileKeyValuePair *) tmp->data;
2659 if (pair->key != NULL)
2665 if (tmp->next == NULL)
2673 GKeyFileKeyValuePair *pair;
2675 pair = (GKeyFileKeyValuePair *) tmp->data;
2678 string = g_string_sized_new (512);
2680 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2681 g_string_append (string, comment);
2688 return g_string_free (string, FALSE);
2694 g_key_file_get_group_comment (GKeyFile *key_file,
2695 const gchar *group_name,
2699 GKeyFileGroup *group;
2701 group_node = g_key_file_lookup_group_node (key_file, group_name);
2704 g_set_error (error, G_KEY_FILE_ERROR,
2705 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2706 _("Key file does not have group '%s'"),
2707 group_name ? group_name : "(null)");
2712 group = (GKeyFileGroup *)group_node->data;
2714 return g_strdup (group->comment->value);
2716 group_node = group_node->next;
2717 group = (GKeyFileGroup *)group_node->data;
2718 return get_group_comment (key_file, group, error);
2722 g_key_file_get_top_comment (GKeyFile *key_file,
2726 GKeyFileGroup *group;
2728 /* The last group in the list should be the top (comments only)
2731 g_assert (key_file->groups != NULL);
2732 group_node = g_list_last (key_file->groups);
2733 group = (GKeyFileGroup *) group_node->data;
2734 g_assert (group->name == NULL);
2736 return get_group_comment (key_file, group, error);
2740 * g_key_file_get_comment:
2741 * @key_file: a #GKeyFile
2742 * @group_name: a group name, or %NULL
2744 * @error: return location for a #GError
2746 * Retrieves a comment above @key from @group_name.
2747 * @group_name. If @key is %NULL then @comment will
2748 * be read from above @group_name. If both @key
2749 * and @group_name are NULL, then @comment will
2750 * be read from above the first group in the file.
2752 * Returns: a comment that should be freed with g_free()
2757 g_key_file_get_comment (GKeyFile *key_file,
2758 const gchar *group_name,
2762 g_return_val_if_fail (key_file != NULL, NULL);
2764 if (group_name != NULL && key != NULL)
2765 return g_key_file_get_key_comment (key_file, group_name, key, error);
2766 else if (group_name != NULL)
2767 return g_key_file_get_group_comment (key_file, group_name, error);
2769 return g_key_file_get_top_comment (key_file, error);
2773 * g_key_file_remove_comment:
2774 * @key_file: a #GKeyFile
2775 * @group_name: a group name, or %NULL
2777 * @error: return location for a #GError
2779 * Removes a comment above @key from @group_name.
2780 * @group_name. If @key is %NULL then @comment will
2781 * be written above @group_name. If both @key
2782 * and @group_name are NULL, then @comment will
2783 * be written above the first group in the file.
2789 g_key_file_remove_comment (GKeyFile *key_file,
2790 const gchar *group_name,
2794 g_return_if_fail (key_file != NULL);
2796 if (group_name != NULL && key != NULL)
2797 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2798 else if (group_name != NULL)
2799 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2801 g_key_file_set_top_comment (key_file, NULL, error);
2805 * g_key_file_has_group:
2806 * @key_file: a #GKeyFile
2807 * @group_name: a group name
2809 * Looks whether the key file has the group @group_name.
2811 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2816 g_key_file_has_group (GKeyFile *key_file,
2817 const gchar *group_name)
2819 g_return_val_if_fail (key_file != NULL, FALSE);
2820 g_return_val_if_fail (group_name != NULL, FALSE);
2822 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2826 * g_key_file_has_key:
2827 * @key_file: a #GKeyFile
2828 * @group_name: a group name
2830 * @error: return location for a #GError
2832 * Looks whether the key file has the key @key in the group
2835 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2841 g_key_file_has_key (GKeyFile *key_file,
2842 const gchar *group_name,
2846 GKeyFileKeyValuePair *pair;
2847 GKeyFileGroup *group;
2849 g_return_val_if_fail (key_file != NULL, FALSE);
2850 g_return_val_if_fail (group_name != NULL, FALSE);
2851 g_return_val_if_fail (key != NULL, FALSE);
2853 group = g_key_file_lookup_group (key_file, group_name);
2857 g_set_error (error, G_KEY_FILE_ERROR,
2858 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2859 _("Key file does not have group '%s'"),
2860 group_name ? group_name : "(null)");
2865 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2867 return pair != NULL;
2871 g_key_file_add_group (GKeyFile *key_file,
2872 const gchar *group_name)
2874 GKeyFileGroup *group;
2876 g_return_if_fail (key_file != NULL);
2877 g_return_if_fail (group_name != NULL);
2879 if (g_key_file_lookup_group_node (key_file, group_name) != NULL)
2882 group = g_new0 (GKeyFileGroup, 1);
2883 group->name = g_strdup (group_name);
2884 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2885 key_file->groups = g_list_prepend (key_file->groups, group);
2886 key_file->approximate_size += strlen (group_name) + 3;
2887 key_file->current_group = group;
2889 if (key_file->start_group == NULL)
2890 key_file->start_group = group;
2894 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2899 g_free (pair->value);
2904 /* Be careful not to call this function on a node with data in the
2905 * lookup map without removing it from the lookup map, first.
2907 * Some current cases where this warning is not a concern are
2909 * - the node being removed is a comment node
2910 * - the entire lookup map is getting destroyed soon after
2914 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2915 GKeyFileGroup *group,
2919 GKeyFileKeyValuePair *pair;
2921 pair = (GKeyFileKeyValuePair *) pair_node->data;
2923 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2925 if (pair->key != NULL)
2926 key_file->approximate_size -= strlen (pair->key) + 1;
2928 g_assert (pair->value != NULL);
2929 key_file->approximate_size -= strlen (pair->value);
2931 g_key_file_key_value_pair_free (pair);
2933 g_list_free_1 (pair_node);
2937 g_key_file_remove_group_node (GKeyFile *key_file,
2940 GKeyFileGroup *group;
2943 group = (GKeyFileGroup *) group_node->data;
2945 /* If the current group gets deleted make the current group the last
2948 if (key_file->current_group == group)
2950 /* groups should always contain at least the top comment group,
2951 * unless g_key_file_clear has been called
2953 if (key_file->groups)
2954 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2956 key_file->current_group = NULL;
2959 /* If the start group gets deleted make the start group the first
2962 if (key_file->start_group == group)
2964 tmp = g_list_last (key_file->groups);
2967 if (tmp != group_node &&
2968 ((GKeyFileGroup *) tmp->data)->name != NULL)
2975 key_file->start_group = (GKeyFileGroup *) tmp->data;
2977 key_file->start_group = NULL;
2980 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2982 if (group->name != NULL)
2983 key_file->approximate_size -= strlen (group->name) + 3;
2985 tmp = group->key_value_pairs;
2992 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2995 g_assert (group->key_value_pairs == NULL);
2997 if (group->lookup_map)
2999 g_hash_table_destroy (group->lookup_map);
3000 group->lookup_map = NULL;
3003 g_free ((gchar *) group->name);
3005 g_list_free_1 (group_node);
3009 * g_key_file_remove_group:
3010 * @key_file: a #GKeyFile
3011 * @group_name: a group name
3012 * @error: return location for a #GError or %NULL
3014 * Removes the specified group, @group_name,
3015 * from the key file.
3020 g_key_file_remove_group (GKeyFile *key_file,
3021 const gchar *group_name,
3026 g_return_if_fail (key_file != NULL);
3027 g_return_if_fail (group_name != NULL);
3029 group_node = g_key_file_lookup_group_node (key_file, group_name);
3033 g_set_error (error, G_KEY_FILE_ERROR,
3034 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3035 _("Key file does not have group '%s'"),
3040 g_key_file_remove_group_node (key_file, group_node);
3044 g_key_file_add_key (GKeyFile *key_file,
3045 GKeyFileGroup *group,
3049 GKeyFileKeyValuePair *pair;
3051 pair = g_new0 (GKeyFileKeyValuePair, 1);
3053 pair->key = g_strdup (key);
3054 pair->value = g_strdup (value);
3056 g_hash_table_replace (group->lookup_map, pair->key, pair);
3057 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3058 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3062 * g_key_file_remove_key:
3063 * @key_file: a #GKeyFile
3064 * @group_name: a group name
3065 * @key: a key name to remove
3066 * @error: return location for a #GError or %NULL
3068 * Removes @key in @group_name from the key file.
3073 g_key_file_remove_key (GKeyFile *key_file,
3074 const gchar *group_name,
3078 GKeyFileGroup *group;
3079 GKeyFileKeyValuePair *pair;
3081 g_return_if_fail (key_file != NULL);
3082 g_return_if_fail (group_name != NULL);
3083 g_return_if_fail (key != NULL);
3087 group = g_key_file_lookup_group (key_file, group_name);
3090 g_set_error (error, G_KEY_FILE_ERROR,
3091 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3092 _("Key file does not have group '%s'"),
3093 group_name ? group_name : "(null)");
3097 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3101 g_set_error (error, G_KEY_FILE_ERROR,
3102 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3103 _("Key file does not have key '%s' in group '%s'"),
3108 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3110 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3111 g_hash_table_remove (group->lookup_map, pair->key);
3112 g_key_file_key_value_pair_free (pair);
3116 g_key_file_lookup_group_node (GKeyFile *key_file,
3117 const gchar *group_name)
3119 GKeyFileGroup *group;
3122 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3124 group = (GKeyFileGroup *) tmp->data;
3126 if (group && group->name && strcmp (group->name, group_name) == 0)
3133 static GKeyFileGroup *
3134 g_key_file_lookup_group (GKeyFile *key_file,
3135 const gchar *group_name)
3139 group_node = g_key_file_lookup_group_node (key_file, group_name);
3141 if (group_node != NULL)
3142 return (GKeyFileGroup *) group_node->data;
3148 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3149 GKeyFileGroup *group,
3154 for (key_node = group->key_value_pairs;
3156 key_node = key_node->next)
3158 GKeyFileKeyValuePair *pair;
3160 pair = (GKeyFileKeyValuePair *) key_node->data;
3162 if (pair->key && strcmp (pair->key, key) == 0)
3169 static GKeyFileKeyValuePair *
3170 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3171 GKeyFileGroup *group,
3174 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3177 /* Lines starting with # or consisting entirely of whitespace are merely
3178 * recorded, not parsed. This function assumes all leading whitespace
3179 * has been stripped.
3182 g_key_file_line_is_comment (const gchar *line)
3184 return (*line == '#' || *line == '\0' || *line == '\n');
3187 /* A group in a key file is made up of a starting '[' followed by one
3188 * or more letters making up the group name followed by ']'.
3191 g_key_file_line_is_group (const gchar *line)
3199 p = g_utf8_next_char (p);
3201 /* Group name must be non-empty
3203 if (!*p || *p == ']')
3206 while (*p && *p != ']')
3207 p = g_utf8_next_char (p);
3216 g_key_file_line_is_key_value_pair (const gchar *line)
3220 p = (gchar *) g_utf8_strchr (line, -1, '=');
3225 /* Key must be non-empty
3234 g_key_file_parse_value_as_string (GKeyFile *key_file,
3239 gchar *string_value, *p, *q0, *q;
3241 string_value = g_new0 (gchar, strlen (value) + 1);
3243 p = (gchar *) value;
3244 q0 = q = string_value;
3274 g_set_error (error, G_KEY_FILE_ERROR,
3275 G_KEY_FILE_ERROR_INVALID_VALUE,
3276 _("Key file contains escape character "
3281 if (pieces && *p == key_file->list_separator)
3282 *q = key_file->list_separator;
3296 g_set_error (error, G_KEY_FILE_ERROR,
3297 G_KEY_FILE_ERROR_INVALID_VALUE,
3298 _("Key file contains invalid escape "
3299 "sequence '%s'"), sequence);
3308 if (pieces && (*p == key_file->list_separator))
3310 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3326 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3327 *pieces = g_slist_reverse (*pieces);
3330 return string_value;
3334 g_key_file_parse_string_as_value (GKeyFile *key_file,
3335 const gchar *string,
3336 gboolean escape_separator)
3338 gchar *value, *p, *q;
3340 gboolean parsing_leading_space;
3342 length = strlen (string) + 1;
3344 /* Worst case would be that every character needs to be escaped.
3345 * In other words every character turns to two characters
3347 value = g_new0 (gchar, 2 * length);
3349 p = (gchar *) string;
3351 parsing_leading_space = TRUE;
3352 while (p < (string + length - 1))
3354 gchar escaped_character[3] = { '\\', 0, 0 };
3359 if (parsing_leading_space)
3361 escaped_character[1] = 's';
3362 strcpy (q, escaped_character);
3372 if (parsing_leading_space)
3374 escaped_character[1] = 't';
3375 strcpy (q, escaped_character);
3385 escaped_character[1] = 'n';
3386 strcpy (q, escaped_character);
3390 escaped_character[1] = 'r';
3391 strcpy (q, escaped_character);
3395 escaped_character[1] = '\\';
3396 strcpy (q, escaped_character);
3398 parsing_leading_space = FALSE;
3401 if (escape_separator && *p == key_file->list_separator)
3403 escaped_character[1] = key_file->list_separator;
3404 strcpy (q, escaped_character);
3406 parsing_leading_space = TRUE;
3412 parsing_leading_space = FALSE;
3424 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3428 gchar *end_of_valid_int;
3433 long_value = strtol (value, &end_of_valid_int, 10);
3435 if (*value == '\0' || *end_of_valid_int != '\0')
3437 g_set_error (error, G_KEY_FILE_ERROR,
3438 G_KEY_FILE_ERROR_INVALID_VALUE,
3439 _("Value '%s' cannot be interpreted as a number."), value);
3443 int_value = long_value;
3444 if (int_value != long_value || errno == ERANGE)
3448 G_KEY_FILE_ERROR_INVALID_VALUE,
3449 _("Integer value '%s' out of range"), value);
3457 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3461 return g_strdup_printf ("%d", value);
3465 g_key_file_parse_value_as_double (GKeyFile *key_file,
3469 gchar *end_of_valid_d;
3470 gdouble double_value = 0;
3472 double_value = g_ascii_strtod (value, &end_of_valid_d);
3474 if (*end_of_valid_d != '\0')
3475 g_set_error (error, G_KEY_FILE_ERROR,
3476 G_KEY_FILE_ERROR_INVALID_VALUE,
3477 _("Value '%s' cannot be interpreted as a float number."), value);
3479 return double_value;
3483 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3489 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3491 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3495 g_set_error (error, G_KEY_FILE_ERROR,
3496 G_KEY_FILE_ERROR_INVALID_VALUE,
3497 _("Value '%s' cannot be interpreted as a boolean."), value);
3503 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3507 return g_strdup ("true");
3509 return g_strdup ("false");
3513 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3517 gchar **lines, *comment;
3520 string = g_string_sized_new (512);
3522 lines = g_strsplit (value, "\n", 0);
3524 for (i = 0; lines[i] != NULL; i++)
3526 if (lines[i][0] != '#')
3527 g_string_append_printf (string, "%s\n", lines[i]);
3529 g_string_append_printf (string, "%s\n", lines[i] + 1);
3533 comment = string->str;
3535 g_string_free (string, FALSE);
3541 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3542 const gchar *comment)
3545 gchar **lines, *value;
3548 string = g_string_sized_new (512);
3550 lines = g_strsplit (comment, "\n", 0);
3552 for (i = 0; lines[i] != NULL; i++)
3553 g_string_append_printf (string, "#%s%s", lines[i],
3554 lines[i + 1] == NULL? "" : "\n");
3557 value = string->str;
3559 g_string_free (string, FALSE);
3564 #define __G_KEY_FILE_C__
3565 #include "galiasdef.c"