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 if (group->comment != NULL)
947 g_string_append_printf (data_string, "%s\n", group->comment->value);
948 if (group->name != NULL)
949 g_string_append_printf (data_string, "[%s]\n", group->name);
951 for (key_file_node = g_list_last (group->key_value_pairs);
952 key_file_node != NULL;
953 key_file_node = key_file_node->prev)
955 GKeyFileKeyValuePair *pair;
957 pair = (GKeyFileKeyValuePair *) key_file_node->data;
959 if (pair->key != NULL)
960 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
962 g_string_append_printf (data_string, "%s\n", pair->value);
967 *length = data_string->len;
969 return g_string_free (data_string, FALSE);
973 * g_key_file_get_keys:
974 * @key_file: a #GKeyFile
975 * @group_name: a group name
976 * @length: return location for the number of keys returned, or %NULL
977 * @error: return location for a #GError, or %NULL
979 * Returns all keys for the group name @group_name. The array of
980 * returned keys will be %NULL-terminated, so @length may
981 * optionally be %NULL. In the event that the @group_name cannot
982 * be found, %NULL is returned and @error is set to
983 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
985 * Return value: a newly-allocated %NULL-terminated array of
986 * strings. Use g_strfreev() to free it.
991 g_key_file_get_keys (GKeyFile *key_file,
992 const gchar *group_name,
996 GKeyFileGroup *group;
1001 g_return_val_if_fail (key_file != NULL, NULL);
1002 g_return_val_if_fail (group_name != NULL, NULL);
1004 group = g_key_file_lookup_group (key_file, group_name);
1008 g_set_error (error, G_KEY_FILE_ERROR,
1009 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1010 _("Key file does not have group '%s'"),
1011 group_name ? group_name : "(null)");
1016 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1018 GKeyFileKeyValuePair *pair;
1020 pair = (GKeyFileKeyValuePair *) tmp->data;
1026 keys = g_new0 (gchar *, num_keys + 1);
1029 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1031 GKeyFileKeyValuePair *pair;
1033 pair = (GKeyFileKeyValuePair *) tmp->data;
1037 keys[i] = g_strdup (pair->key);
1042 keys[num_keys] = NULL;
1051 * g_key_file_get_start_group:
1052 * @key_file: a #GKeyFile
1054 * Returns the name of the start group of the file.
1056 * Return value: The start group of the key file.
1061 g_key_file_get_start_group (GKeyFile *key_file)
1063 g_return_val_if_fail (key_file != NULL, NULL);
1065 if (key_file->start_group)
1066 return g_strdup (key_file->start_group->name);
1072 * g_key_file_get_groups:
1073 * @key_file: a #GKeyFile
1074 * @length: return location for the number of returned groups, or %NULL
1076 * Returns all groups in the key file loaded with @key_file. The
1077 * array of returned groups will be %NULL-terminated, so @length may
1078 * optionally be %NULL.
1080 * Return value: a newly-allocated %NULL-terminated array of strings.
1081 * Use g_strfreev() to free it.
1085 g_key_file_get_groups (GKeyFile *key_file,
1090 gsize i, num_groups;
1092 g_return_val_if_fail (key_file != NULL, NULL);
1094 num_groups = g_list_length (key_file->groups);
1096 g_assert (num_groups > 0);
1098 /* Only need num_groups instead of num_groups + 1
1099 * because the first group of the file (last in the
1100 * list) is always the comment group at the top,
1103 groups = g_new0 (gchar *, num_groups);
1105 group_node = g_list_last (key_file->groups);
1107 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1110 for (group_node = group_node->prev;
1112 group_node = group_node->prev)
1114 GKeyFileGroup *group;
1116 group = (GKeyFileGroup *) group_node->data;
1118 g_assert (group->name != NULL);
1120 groups[i++] = g_strdup (group->name);
1131 * g_key_file_get_value:
1132 * @key_file: a #GKeyFile
1133 * @group_name: a group name
1135 * @error: return location for a #GError, or %NULL
1137 * Returns the value associated with @key under @group_name.
1139 * In the event the key cannot be found, %NULL is returned and
1140 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1141 * event that the @group_name cannot be found, %NULL is returned
1142 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1144 * Return value: a newly allocated string or %NULL if the specified
1145 * key cannot be found.
1150 g_key_file_get_value (GKeyFile *key_file,
1151 const gchar *group_name,
1155 GKeyFileGroup *group;
1156 GKeyFileKeyValuePair *pair;
1157 gchar *value = NULL;
1159 g_return_val_if_fail (key_file != NULL, NULL);
1160 g_return_val_if_fail (group_name != NULL, NULL);
1161 g_return_val_if_fail (key != NULL, NULL);
1163 group = g_key_file_lookup_group (key_file, group_name);
1167 g_set_error (error, G_KEY_FILE_ERROR,
1168 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1169 _("Key file does not have group '%s'"),
1170 group_name ? group_name : "(null)");
1174 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1177 value = g_strdup (pair->value);
1179 g_set_error (error, G_KEY_FILE_ERROR,
1180 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1181 _("Key file does not have key '%s'"), key);
1187 * g_key_file_set_value:
1188 * @key_file: a #GKeyFile
1189 * @group_name: a group name
1193 * Associates a new value with @key under @group_name. If @key
1194 * cannot be found then it is created. If @group_name cannot be
1195 * found then it is created.
1200 g_key_file_set_value (GKeyFile *key_file,
1201 const gchar *group_name,
1205 GKeyFileGroup *group;
1206 GKeyFileKeyValuePair *pair;
1208 g_return_if_fail (key_file != NULL);
1209 g_return_if_fail (group_name != NULL);
1210 g_return_if_fail (key != NULL);
1211 g_return_if_fail (value != NULL);
1213 group = g_key_file_lookup_group (key_file, group_name);
1217 g_key_file_add_group (key_file, group_name);
1218 group = (GKeyFileGroup *) key_file->groups->data;
1220 g_key_file_add_key (key_file, group, key, value);
1224 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1227 g_key_file_add_key (key_file, group, key, value);
1230 g_free (pair->value);
1231 pair->value = g_strdup (value);
1237 * g_key_file_get_string:
1238 * @key_file: a #GKeyFile
1239 * @group_name: a group name
1241 * @error: return location for a #GError, or %NULL
1243 * Returns the value associated with @key under @group_name.
1245 * In the event the key cannot be found, %NULL is returned and
1246 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1247 * event that the @group_name cannot be found, %NULL is returned
1248 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1250 * Return value: a newly allocated string or %NULL if the specified
1251 * key cannot be found.
1256 g_key_file_get_string (GKeyFile *key_file,
1257 const gchar *group_name,
1261 gchar *value, *string_value;
1262 GError *key_file_error;
1264 g_return_val_if_fail (key_file != NULL, NULL);
1265 g_return_val_if_fail (group_name != NULL, NULL);
1266 g_return_val_if_fail (key != NULL, NULL);
1268 key_file_error = NULL;
1270 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1274 g_propagate_error (error, key_file_error);
1278 if (!g_utf8_validate (value, -1, NULL))
1280 g_set_error (error, G_KEY_FILE_ERROR,
1281 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1282 _("Key file contains key '%s' with value '%s' "
1283 "which is not UTF-8"), key, value);
1288 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1294 if (g_error_matches (key_file_error,
1296 G_KEY_FILE_ERROR_INVALID_VALUE))
1298 g_set_error (error, G_KEY_FILE_ERROR,
1299 G_KEY_FILE_ERROR_INVALID_VALUE,
1300 _("Key file contains key '%s' "
1301 "which has value that cannot be interpreted."),
1303 g_error_free (key_file_error);
1306 g_propagate_error (error, key_file_error);
1309 return string_value;
1313 * g_key_file_set_string:
1314 * @key_file: a #GKeyFile
1315 * @group_name: a group name
1319 * Associates a new string value with @key under @group_name. If
1320 * @key cannot be found then it is created. If @group_name
1321 * cannot be found then it is created.
1326 g_key_file_set_string (GKeyFile *key_file,
1327 const gchar *group_name,
1329 const gchar *string)
1333 g_return_if_fail (key_file != NULL);
1334 g_return_if_fail (group_name != NULL);
1335 g_return_if_fail (key != NULL);
1336 g_return_if_fail (string != NULL);
1338 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1339 g_key_file_set_value (key_file, group_name, key, value);
1344 * g_key_file_get_string_list:
1345 * @key_file: a #GKeyFile
1346 * @group_name: a group name
1348 * @length: return location for the number of returned strings, or %NULL
1349 * @error: return location for a #GError, or %NULL
1351 * Returns the values associated with @key under @group_name.
1353 * In the event the key cannot be found, %NULL is returned and
1354 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1355 * event that the @group_name cannot be found, %NULL is returned
1356 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1358 * Return value: a %NULL-terminated string array or %NULL if the specified
1359 * key cannot be found. The array should be freed with g_strfreev().
1364 g_key_file_get_string_list (GKeyFile *key_file,
1365 const gchar *group_name,
1370 GError *key_file_error = NULL;
1371 gchar *value, *string_value, **values;
1373 GSList *p, *pieces = NULL;
1375 g_return_val_if_fail (key_file != NULL, NULL);
1376 g_return_val_if_fail (group_name != NULL, NULL);
1377 g_return_val_if_fail (key != NULL, NULL);
1379 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1383 g_propagate_error (error, key_file_error);
1387 if (!g_utf8_validate (value, -1, NULL))
1389 g_set_error (error, G_KEY_FILE_ERROR,
1390 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1391 _("Key file contains key '%s' with value '%s' "
1392 "which is not UTF-8"), key, value);
1397 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1399 g_free (string_value);
1403 if (g_error_matches (key_file_error,
1405 G_KEY_FILE_ERROR_INVALID_VALUE))
1407 g_set_error (error, G_KEY_FILE_ERROR,
1408 G_KEY_FILE_ERROR_INVALID_VALUE,
1409 _("Key file contains key '%s' "
1410 "which has value that cannot be interpreted."),
1412 g_error_free (key_file_error);
1415 g_propagate_error (error, key_file_error);
1418 len = g_slist_length (pieces);
1419 values = g_new0 (gchar *, len + 1);
1420 for (p = pieces, i = 0; p; p = p->next)
1421 values[i++] = p->data;
1424 g_slist_free (pieces);
1433 * g_key_file_set_string_list:
1434 * @key_file: a #GKeyFile
1435 * @group_name: a group name
1437 * @list: an array of locale string values
1438 * @length: number of locale string values in @list
1440 * Associates a list of string values for @key under @group_name.
1441 * If @key cannot be found then it is created. If @group_name
1442 * cannot be found then it is created.
1447 g_key_file_set_string_list (GKeyFile *key_file,
1448 const gchar *group_name,
1450 const gchar * const list[],
1453 GString *value_list;
1456 g_return_if_fail (key_file != NULL);
1457 g_return_if_fail (group_name != NULL);
1458 g_return_if_fail (key != NULL);
1459 g_return_if_fail (list != NULL);
1461 value_list = g_string_sized_new (length * 128);
1462 for (i = 0; i < length && list[i] != NULL; i++)
1466 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1467 g_string_append (value_list, value);
1468 g_string_append_c (value_list, key_file->list_separator);
1473 g_key_file_set_value (key_file, group_name, key, value_list->str);
1474 g_string_free (value_list, TRUE);
1478 * g_key_file_set_locale_string:
1479 * @key_file: a #GKeyFile
1480 * @group_name: a group name
1485 * Associates a string value for @key and @locale under
1486 * @group_name. If the translation for @key cannot be found
1487 * then it is created.
1492 g_key_file_set_locale_string (GKeyFile *key_file,
1493 const gchar *group_name,
1495 const gchar *locale,
1496 const gchar *string)
1498 gchar *full_key, *value;
1500 g_return_if_fail (key_file != NULL);
1501 g_return_if_fail (group_name != NULL);
1502 g_return_if_fail (key != NULL);
1503 g_return_if_fail (locale != NULL);
1504 g_return_if_fail (string != NULL);
1506 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1507 full_key = g_strdup_printf ("%s[%s]", key, locale);
1508 g_key_file_set_value (key_file, group_name, full_key, value);
1513 extern GSList *_g_compute_locale_variants (const gchar *locale);
1516 * g_key_file_get_locale_string:
1517 * @key_file: a #GKeyFile
1518 * @group_name: a group name
1520 * @locale: a locale or %NULL
1521 * @error: return location for a #GError, or %NULL
1523 * Returns the value associated with @key under @group_name
1524 * translated in the given @locale if available. If @locale is
1525 * %NULL then the current locale is assumed.
1527 * If @key cannot be found then %NULL is returned and @error is set to
1528 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1529 * with @key cannot be interpreted or no suitable translation can
1530 * be found then the untranslated value is returned.
1532 * Return value: a newly allocated string or %NULL if the specified
1533 * key cannot be found.
1538 g_key_file_get_locale_string (GKeyFile *key_file,
1539 const gchar *group_name,
1541 const gchar *locale,
1544 gchar *candidate_key, *translated_value;
1545 GError *key_file_error;
1547 gboolean free_languages = FALSE;
1550 g_return_val_if_fail (key_file != NULL, NULL);
1551 g_return_val_if_fail (group_name != NULL, NULL);
1552 g_return_val_if_fail (key != NULL, NULL);
1554 candidate_key = NULL;
1555 translated_value = NULL;
1556 key_file_error = NULL;
1562 list = _g_compute_locale_variants (locale);
1564 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1565 for (l = list, i = 0; l; l = l->next, i++)
1566 languages[i] = l->data;
1567 languages[i] = NULL;
1569 g_slist_free (list);
1570 free_languages = TRUE;
1574 languages = (gchar **) g_get_language_names ();
1575 free_languages = FALSE;
1578 for (i = 0; languages[i]; i++)
1580 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1582 translated_value = g_key_file_get_string (key_file,
1584 candidate_key, NULL);
1585 g_free (candidate_key);
1587 if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1590 g_free (translated_value);
1591 translated_value = NULL;
1594 /* Fallback to untranslated key
1596 if (!translated_value)
1598 translated_value = g_key_file_get_string (key_file, group_name, key,
1601 if (!translated_value)
1602 g_propagate_error (error, key_file_error);
1606 g_strfreev (languages);
1608 return translated_value;
1612 * g_key_file_get_locale_string_list:
1613 * @key_file: a #GKeyFile
1614 * @group_name: a group name
1617 * @length: return location for the number of returned strings or %NULL
1618 * @error: return location for a #GError or %NULL
1620 * Returns the values associated with @key under @group_name
1621 * translated in the given @locale if available. If @locale is
1622 * %NULL then the current locale is assumed.
1624 * If @key cannot be found then %NULL is returned and @error is set to
1625 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1626 * with @key cannot be interpreted or no suitable translations
1627 * can be found then the untranslated values are returned.
1628 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1630 * Return value: a newly allocated %NULL-terminated string array
1631 * or %NULL if the key isn't found. The string array should be freed
1632 * with g_strfreev().
1637 g_key_file_get_locale_string_list (GKeyFile *key_file,
1638 const gchar *group_name,
1640 const gchar *locale,
1644 GError *key_file_error;
1645 gchar **values, *value;
1647 g_return_val_if_fail (key_file != NULL, NULL);
1648 g_return_val_if_fail (group_name != NULL, NULL);
1649 g_return_val_if_fail (key != NULL, NULL);
1651 key_file_error = NULL;
1653 value = g_key_file_get_locale_string (key_file, group_name,
1658 g_propagate_error (error, key_file_error);
1663 if (value[strlen (value) - 1] == ';')
1664 value[strlen (value) - 1] = '\0';
1666 values = g_strsplit (value, ";", 0);
1671 *length = g_strv_length (values);
1677 * g_key_file_set_locale_string_list:
1678 * @key_file: a #GKeyFile
1679 * @group_name: a group name
1682 * @list: a %NULL-terminated array of locale string values
1683 * @length: the length of @list
1685 * Associates a list of string values for @key and @locale under
1686 * @group_name. If the translation for @key cannot be found then
1692 g_key_file_set_locale_string_list (GKeyFile *key_file,
1693 const gchar *group_name,
1695 const gchar *locale,
1696 const gchar * const list[],
1699 GString *value_list;
1703 g_return_if_fail (key_file != NULL);
1704 g_return_if_fail (group_name != NULL);
1705 g_return_if_fail (key != NULL);
1706 g_return_if_fail (locale != NULL);
1707 g_return_if_fail (length != 0);
1709 value_list = g_string_sized_new (length * 128);
1710 for (i = 0; i < length && list[i] != NULL; i++)
1714 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1716 g_string_append (value_list, value);
1717 g_string_append_c (value_list, ';');
1722 full_key = g_strdup_printf ("%s[%s]", key, locale);
1723 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1725 g_string_free (value_list, TRUE);
1729 * g_key_file_get_boolean:
1730 * @key_file: a #GKeyFile
1731 * @group_name: a group name
1733 * @error: return location for a #GError
1735 * Returns the value associated with @key under @group_name as a
1738 * If @key cannot be found then the return value is undefined and
1739 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1740 * the value associated with @key cannot be interpreted as a boolean
1741 * then the return value is also undefined and @error is set to
1742 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1744 * Return value: the value associated with the key as a boolean
1748 g_key_file_get_boolean (GKeyFile *key_file,
1749 const gchar *group_name,
1753 GError *key_file_error = NULL;
1755 gboolean bool_value;
1757 g_return_val_if_fail (key_file != NULL, FALSE);
1758 g_return_val_if_fail (group_name != NULL, FALSE);
1759 g_return_val_if_fail (key != NULL, FALSE);
1761 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1765 g_propagate_error (error, key_file_error);
1769 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1775 if (g_error_matches (key_file_error,
1777 G_KEY_FILE_ERROR_INVALID_VALUE))
1779 g_set_error (error, G_KEY_FILE_ERROR,
1780 G_KEY_FILE_ERROR_INVALID_VALUE,
1781 _("Key file contains key '%s' "
1782 "which has value that cannot be interpreted."),
1784 g_error_free (key_file_error);
1787 g_propagate_error (error, key_file_error);
1794 * g_key_file_set_boolean:
1795 * @key_file: a #GKeyFile
1796 * @group_name: a group name
1798 * @value: %TRUE or %FALSE
1800 * Associates a new boolean value with @key under @group_name.
1801 * If @key cannot be found then it is created.
1806 g_key_file_set_boolean (GKeyFile *key_file,
1807 const gchar *group_name,
1813 g_return_if_fail (key_file != NULL);
1814 g_return_if_fail (group_name != NULL);
1815 g_return_if_fail (key != NULL);
1817 result = g_key_file_parse_boolean_as_value (key_file, value);
1818 g_key_file_set_value (key_file, group_name, key, result);
1823 * g_key_file_get_boolean_list:
1824 * @key_file: a #GKeyFile
1825 * @group_name: a group name
1827 * @length: the number of booleans returned
1828 * @error: return location for a #GError
1830 * Returns the values associated with @key under @group_name as
1831 * booleans. If @group_name is %NULL, the start_group is used.
1833 * If @key cannot be found then the return value is undefined and
1834 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1835 * the values associated with @key cannot be interpreted as booleans
1836 * then the return value is also undefined and @error is set to
1837 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1839 * Return value: the values associated with the key as a boolean
1844 g_key_file_get_boolean_list (GKeyFile *key_file,
1845 const gchar *group_name,
1850 GError *key_file_error;
1852 gboolean *bool_values;
1855 g_return_val_if_fail (key_file != NULL, NULL);
1856 g_return_val_if_fail (group_name != NULL, NULL);
1857 g_return_val_if_fail (key != NULL, NULL);
1859 key_file_error = NULL;
1861 values = g_key_file_get_string_list (key_file, group_name, key,
1862 &num_bools, &key_file_error);
1865 g_propagate_error (error, key_file_error);
1870 bool_values = g_new0 (gboolean, num_bools);
1872 for (i = 0; i < num_bools; i++)
1874 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1880 g_propagate_error (error, key_file_error);
1881 g_strfreev (values);
1882 g_free (bool_values);
1887 g_strfreev (values);
1890 *length = num_bools;
1896 * g_key_file_set_boolean_list:
1897 * @key_file: a #GKeyFile
1898 * @group_name: a group name
1900 * @list: an array of boolean values
1901 * @length: length of @list
1903 * Associates a list of boolean values with @key under
1904 * @group_name. If @key cannot be found then it is created.
1905 * If @group_name is %NULL, the start_group is used.
1910 g_key_file_set_boolean_list (GKeyFile *key_file,
1911 const gchar *group_name,
1916 GString *value_list;
1919 g_return_if_fail (key_file != NULL);
1920 g_return_if_fail (group_name != NULL);
1921 g_return_if_fail (key != NULL);
1922 g_return_if_fail (list != NULL);
1924 value_list = g_string_sized_new (length * 8);
1925 for (i = 0; i < length; i++)
1929 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1931 g_string_append (value_list, value);
1932 g_string_append_c (value_list, key_file->list_separator);
1937 g_key_file_set_value (key_file, group_name, key, value_list->str);
1938 g_string_free (value_list, TRUE);
1942 * g_key_file_get_integer:
1943 * @key_file: a #GKeyFile
1944 * @group_name: a group name
1946 * @error: return location for a #GError
1948 * Returns the value associated with @key under @group_name as an
1949 * integer. If @group_name is %NULL, the start_group is used.
1951 * If @key cannot be found then the return value is undefined and
1952 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1953 * the value associated with @key cannot be interpreted as an integer
1954 * then the return value is also undefined and @error is set to
1955 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1957 * Return value: the value associated with the key as an integer.
1962 g_key_file_get_integer (GKeyFile *key_file,
1963 const gchar *group_name,
1967 GError *key_file_error;
1971 g_return_val_if_fail (key_file != NULL, -1);
1972 g_return_val_if_fail (group_name != NULL, -1);
1973 g_return_val_if_fail (key != NULL, -1);
1975 key_file_error = NULL;
1977 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1981 g_propagate_error (error, key_file_error);
1985 int_value = g_key_file_parse_value_as_integer (key_file, value,
1991 if (g_error_matches (key_file_error,
1993 G_KEY_FILE_ERROR_INVALID_VALUE))
1995 g_set_error (error, G_KEY_FILE_ERROR,
1996 G_KEY_FILE_ERROR_INVALID_VALUE,
1997 _("Key file contains key '%s' in group '%s' "
1998 "which has value that cannot be interpreted."), key,
2000 g_error_free (key_file_error);
2003 g_propagate_error (error, key_file_error);
2010 * g_key_file_set_integer:
2011 * @key_file: a #GKeyFile
2012 * @group_name: a group name
2014 * @value: an integer value
2016 * Associates a new integer value with @key under @group_name.
2017 * If @key cannot be found then it is created.
2022 g_key_file_set_integer (GKeyFile *key_file,
2023 const gchar *group_name,
2029 g_return_if_fail (key_file != NULL);
2030 g_return_if_fail (group_name != NULL);
2031 g_return_if_fail (key != NULL);
2033 result = g_key_file_parse_integer_as_value (key_file, value);
2034 g_key_file_set_value (key_file, group_name, key, result);
2039 * g_key_file_get_integer_list:
2040 * @key_file: a #GKeyFile
2041 * @group_name: a group name
2043 * @length: the number of integers returned
2044 * @error: return location for a #GError
2046 * Returns the values associated with @key under @group_name as
2049 * If @key cannot be found then the return value is undefined and
2050 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2051 * the values associated with @key cannot be interpreted as integers
2052 * then the return value is also undefined and @error is set to
2053 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2055 * Return value: the values associated with the key as a integer
2060 g_key_file_get_integer_list (GKeyFile *key_file,
2061 const gchar *group_name,
2066 GError *key_file_error = NULL;
2071 g_return_val_if_fail (key_file != NULL, NULL);
2072 g_return_val_if_fail (group_name != NULL, NULL);
2073 g_return_val_if_fail (key != NULL, NULL);
2075 values = g_key_file_get_string_list (key_file, group_name, key,
2076 &num_ints, &key_file_error);
2079 g_propagate_error (error, key_file_error);
2084 int_values = g_new0 (gint, num_ints);
2086 for (i = 0; i < num_ints; i++)
2088 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2094 g_propagate_error (error, key_file_error);
2095 g_strfreev (values);
2096 g_free (int_values);
2101 g_strfreev (values);
2110 * g_key_file_set_integer_list:
2111 * @key_file: a #GKeyFile
2112 * @group_name: a group name
2114 * @list: an array of integer values
2115 * @length: number of integer values in @list
2117 * Associates a list of integer values with @key under
2118 * @group_name. If @key cannot be found then it is created.
2123 g_key_file_set_integer_list (GKeyFile *key_file,
2124 const gchar *group_name,
2132 g_return_if_fail (key_file != NULL);
2133 g_return_if_fail (group_name != NULL);
2134 g_return_if_fail (key != NULL);
2135 g_return_if_fail (list != NULL);
2137 values = g_string_sized_new (length * 16);
2138 for (i = 0; i < length; i++)
2142 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2144 g_string_append (values, value);
2145 g_string_append_c (values, ';');
2150 g_key_file_set_value (key_file, group_name, key, values->str);
2151 g_string_free (values, TRUE);
2155 * g_key_file_get_double:
2156 * @key_file: a #GKeyFile
2157 * @group_name: a group name
2159 * @error: return location for a #GError
2161 * Returns the value associated with @key under @group_name as an
2162 * integer. If @group_name is %NULL, the start_group is used.
2164 * If @key cannot be found then the return value is undefined and
2165 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2166 * the value associated with @key cannot be interpreted as a double
2167 * then the return value is also undefined and @error is set to
2168 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2170 * Return value: the value associated with the key as a double.
2175 g_key_file_get_double (GKeyFile *key_file,
2176 const gchar *group_name,
2180 GError *key_file_error;
2182 gdouble double_value;
2184 g_return_val_if_fail (key_file != NULL, -1);
2185 g_return_val_if_fail (group_name != NULL, -1);
2186 g_return_val_if_fail (key != NULL, -1);
2188 key_file_error = NULL;
2190 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2194 g_propagate_error (error, key_file_error);
2198 double_value = g_key_file_parse_value_as_double (key_file, value,
2204 if (g_error_matches (key_file_error,
2206 G_KEY_FILE_ERROR_INVALID_VALUE))
2208 g_set_error (error, G_KEY_FILE_ERROR,
2209 G_KEY_FILE_ERROR_INVALID_VALUE,
2210 _("Key file contains key '%s' in group '%s' "
2211 "which has value that cannot be interpreted."), key,
2213 g_error_free (key_file_error);
2216 g_propagate_error (error, key_file_error);
2219 return double_value;
2223 * g_key_file_set_double:
2224 * @key_file: a #GKeyFile
2225 * @group_name: a group name
2227 * @value: an double value
2229 * Associates a new double value with @key under @group_name.
2230 * If @key cannot be found then it is created. If @group_name
2231 * is %NULL, the start group is used.
2236 g_key_file_set_double (GKeyFile *key_file,
2237 const gchar *group_name,
2241 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2243 g_return_if_fail (key_file != NULL);
2244 g_return_if_fail (group_name != NULL);
2245 g_return_if_fail (key != NULL);
2247 g_ascii_dtostr ( result, sizeof (result), value );
2248 g_key_file_set_value (key_file, group_name, key, result);
2252 * g_key_file_get_double_list:
2253 * @key_file: a #GKeyFile
2254 * @group_name: a group name
2256 * @length: the number of doubles returned
2257 * @error: return location for a #GError
2259 * Returns the values associated with @key under @group_name as
2260 * doubles. If @group_name is %NULL, the start group is used.
2262 * If @key cannot be found then the return value is undefined and
2263 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2264 * the values associated with @key cannot be interpreted as doubles
2265 * then the return value is also undefined and @error is set to
2266 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2268 * Return value: the values associated with the key as a double
2273 g_key_file_get_double_list (GKeyFile *key_file,
2274 const gchar *group_name,
2279 GError *key_file_error = NULL;
2281 gdouble *double_values;
2282 gsize i, num_doubles;
2284 g_return_val_if_fail (key_file != NULL, NULL);
2285 g_return_val_if_fail (group_name != NULL, NULL);
2286 g_return_val_if_fail (key != NULL, NULL);
2288 values = g_key_file_get_string_list (key_file, group_name, key,
2289 &num_doubles, &key_file_error);
2292 g_propagate_error (error, key_file_error);
2297 double_values = g_new0 (gdouble, num_doubles);
2299 for (i = 0; i < num_doubles; i++)
2301 double_values[i] = g_key_file_parse_value_as_double (key_file,
2307 g_propagate_error (error, key_file_error);
2308 g_strfreev (values);
2309 g_free (double_values);
2314 g_strfreev (values);
2317 *length = num_doubles;
2319 return double_values;
2323 * g_key_file_set_double_list:
2324 * @key_file: a #GKeyFile
2325 * @group_name: a group name
2327 * @list: an array of double values
2328 * @length: number of double values in @list
2330 * Associates a list of double values with @key under
2331 * @group_name. If @key cannot be found then it is created.
2332 * If @group_name is %NULL the start group is used.
2337 g_key_file_set_double_list (GKeyFile *key_file,
2338 const gchar *group_name,
2346 g_return_if_fail (key_file != NULL);
2347 g_return_if_fail (group_name != NULL);
2348 g_return_if_fail (key != NULL);
2349 g_return_if_fail (list != NULL);
2351 values = g_string_sized_new (length * 16);
2352 for (i = 0; i < length; i++)
2354 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2356 g_ascii_dtostr( result, sizeof (result), list[i] );
2358 g_string_append (values, result);
2359 g_string_append_c (values, ';');
2362 g_key_file_set_value (key_file, group_name, key, values->str);
2363 g_string_free (values, TRUE);
2367 g_key_file_set_key_comment (GKeyFile *key_file,
2368 const gchar *group_name,
2370 const gchar *comment,
2373 GKeyFileGroup *group;
2374 GKeyFileKeyValuePair *pair;
2375 GList *key_node, *comment_node, *tmp;
2377 group = g_key_file_lookup_group (key_file, group_name);
2380 g_set_error (error, G_KEY_FILE_ERROR,
2381 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2382 _("Key file does not have group '%s'"),
2383 group_name ? group_name : "(null)");
2388 /* First find the key the comments are supposed to be
2391 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2393 if (key_node == NULL)
2395 g_set_error (error, G_KEY_FILE_ERROR,
2396 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2397 _("Key file does not have key '%s' in group '%s'"),
2402 /* Then find all the comments already associated with the
2405 tmp = key_node->next;
2408 GKeyFileKeyValuePair *pair;
2410 pair = (GKeyFileKeyValuePair *) tmp->data;
2412 if (pair->key != NULL)
2417 g_key_file_remove_key_value_pair_node (key_file, group,
2421 if (comment == NULL)
2424 /* Now we can add our new comment
2426 pair = g_new0 (GKeyFileKeyValuePair, 1);
2429 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2431 key_node = g_list_insert (key_node, pair, 1);
2435 g_key_file_set_group_comment (GKeyFile *key_file,
2436 const gchar *group_name,
2437 const gchar *comment,
2440 GKeyFileGroup *group;
2442 group = g_key_file_lookup_group (key_file, group_name);
2445 g_set_error (error, G_KEY_FILE_ERROR,
2446 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2447 _("Key file does not have group '%s'"),
2448 group_name ? group_name : "(null)");
2453 /* First remove any existing comment
2457 g_key_file_key_value_pair_free (group->comment);
2458 group->comment = NULL;
2461 if (comment == NULL)
2464 /* Now we can add our new comment
2466 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2468 group->comment->key = NULL;
2469 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2473 g_key_file_set_top_comment (GKeyFile *key_file,
2474 const gchar *comment,
2478 GKeyFileGroup *group;
2479 GKeyFileKeyValuePair *pair;
2481 /* The last group in the list should be the top (comments only)
2484 g_assert (key_file->groups != NULL);
2485 group_node = g_list_last (key_file->groups);
2486 group = (GKeyFileGroup *) group_node->data;
2487 g_assert (group->name == NULL);
2489 /* Note all keys must be comments at the top of
2490 * the file, so we can just free it all.
2492 if (group->key_value_pairs != NULL)
2494 g_list_foreach (group->key_value_pairs,
2495 (GFunc) g_key_file_key_value_pair_free,
2497 g_list_free (group->key_value_pairs);
2498 group->key_value_pairs = NULL;
2501 if (comment == NULL)
2504 pair = g_new0 (GKeyFileKeyValuePair, 1);
2507 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2509 group->key_value_pairs =
2510 g_list_prepend (group->key_value_pairs, pair);
2514 * g_key_file_set_comment:
2515 * @key_file: a #GKeyFile
2516 * @group_name: a group name, or %NULL
2518 * @comment: a comment
2519 * @error: return location for a #GError
2521 * Places a comment above @key from @group_name.
2522 * @group_name. If @key is %NULL then @comment will
2523 * be written above @group_name. If both @key
2524 * and @group_name are NULL, then @comment will
2525 * be written above the first group in the file.
2530 g_key_file_set_comment (GKeyFile *key_file,
2531 const gchar *group_name,
2533 const gchar *comment,
2536 g_return_if_fail (key_file != NULL);
2538 if (group_name != NULL && key != NULL)
2539 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2540 else if (group_name != NULL)
2541 g_key_file_set_group_comment (key_file, group_name, comment, error);
2543 g_key_file_set_top_comment (key_file, comment, error);
2545 if (comment != NULL)
2546 key_file->approximate_size += strlen (comment);
2550 g_key_file_get_key_comment (GKeyFile *key_file,
2551 const gchar *group_name,
2555 GKeyFileGroup *group;
2556 GKeyFileKeyValuePair *pair;
2557 GList *key_node, *tmp;
2561 group = g_key_file_lookup_group (key_file, group_name);
2564 g_set_error (error, G_KEY_FILE_ERROR,
2565 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2566 _("Key file does not have group '%s'"),
2567 group_name ? group_name : "(null)");
2572 /* First find the key the comments are supposed to be
2575 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2577 if (key_node == NULL)
2579 g_set_error (error, G_KEY_FILE_ERROR,
2580 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2581 _("Key file does not have key '%s' in group '%s'"),
2588 /* Then find all the comments already associated with the
2589 * key and concatentate them.
2591 tmp = key_node->next;
2592 if (!key_node->next)
2595 pair = (GKeyFileKeyValuePair *) tmp->data;
2596 if (pair->key != NULL)
2601 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2603 if (pair->key != NULL)
2609 while (tmp != key_node)
2611 GKeyFileKeyValuePair *pair;
2613 pair = (GKeyFileKeyValuePair *) tmp->data;
2616 string = g_string_sized_new (512);
2618 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2619 g_string_append (string, comment);
2627 comment = string->str;
2628 g_string_free (string, FALSE);
2637 get_group_comment (GKeyFile *key_file,
2638 GKeyFileGroup *group,
2647 tmp = group->key_value_pairs;
2650 GKeyFileKeyValuePair *pair;
2652 pair = (GKeyFileKeyValuePair *) tmp->data;
2654 if (pair->key != NULL)
2660 if (tmp->next == NULL)
2668 GKeyFileKeyValuePair *pair;
2670 pair = (GKeyFileKeyValuePair *) tmp->data;
2673 string = g_string_sized_new (512);
2675 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2676 g_string_append (string, comment);
2683 return g_string_free (string, FALSE);
2689 g_key_file_get_group_comment (GKeyFile *key_file,
2690 const gchar *group_name,
2694 GKeyFileGroup *group;
2696 group_node = g_key_file_lookup_group_node (key_file, group_name);
2699 g_set_error (error, G_KEY_FILE_ERROR,
2700 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2701 _("Key file does not have group '%s'"),
2702 group_name ? group_name : "(null)");
2707 group = (GKeyFileGroup *)group_node->data;
2709 return g_strdup (group->comment->value);
2711 group_node = group_node->next;
2712 group = (GKeyFileGroup *)group_node->data;
2713 return get_group_comment (key_file, group, error);
2717 g_key_file_get_top_comment (GKeyFile *key_file,
2721 GKeyFileGroup *group;
2723 /* The last group in the list should be the top (comments only)
2726 g_assert (key_file->groups != NULL);
2727 group_node = g_list_last (key_file->groups);
2728 group = (GKeyFileGroup *) group_node->data;
2729 g_assert (group->name == NULL);
2731 return get_group_comment (key_file, group, error);
2735 * g_key_file_get_comment:
2736 * @key_file: a #GKeyFile
2737 * @group_name: a group name, or %NULL
2739 * @error: return location for a #GError
2741 * Retrieves a comment above @key from @group_name.
2742 * @group_name. If @key is %NULL then @comment will
2743 * be read from above @group_name. If both @key
2744 * and @group_name are NULL, then @comment will
2745 * be read from above the first group in the file.
2747 * Returns: a comment that should be freed with g_free()
2752 g_key_file_get_comment (GKeyFile *key_file,
2753 const gchar *group_name,
2757 g_return_val_if_fail (key_file != NULL, NULL);
2759 if (group_name != NULL && key != NULL)
2760 return g_key_file_get_key_comment (key_file, group_name, key, error);
2761 else if (group_name != NULL)
2762 return g_key_file_get_group_comment (key_file, group_name, error);
2764 return g_key_file_get_top_comment (key_file, error);
2768 * g_key_file_remove_comment:
2769 * @key_file: a #GKeyFile
2770 * @group_name: a group name, or %NULL
2772 * @error: return location for a #GError
2774 * Removes a comment above @key from @group_name.
2775 * @group_name. If @key is %NULL then @comment will
2776 * be written above @group_name. If both @key
2777 * and @group_name are NULL, then @comment will
2778 * be written above the first group in the file.
2784 g_key_file_remove_comment (GKeyFile *key_file,
2785 const gchar *group_name,
2789 g_return_if_fail (key_file != NULL);
2791 if (group_name != NULL && key != NULL)
2792 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2793 else if (group_name != NULL)
2794 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2796 g_key_file_set_top_comment (key_file, NULL, error);
2800 * g_key_file_has_group:
2801 * @key_file: a #GKeyFile
2802 * @group_name: a group name
2804 * Looks whether the key file has the group @group_name.
2806 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2811 g_key_file_has_group (GKeyFile *key_file,
2812 const gchar *group_name)
2814 g_return_val_if_fail (key_file != NULL, FALSE);
2815 g_return_val_if_fail (group_name != NULL, FALSE);
2817 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2821 * g_key_file_has_key:
2822 * @key_file: a #GKeyFile
2823 * @group_name: a group name
2825 * @error: return location for a #GError
2827 * Looks whether the key file has the key @key in the group
2830 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2836 g_key_file_has_key (GKeyFile *key_file,
2837 const gchar *group_name,
2841 GKeyFileKeyValuePair *pair;
2842 GKeyFileGroup *group;
2844 g_return_val_if_fail (key_file != NULL, FALSE);
2845 g_return_val_if_fail (group_name != NULL, FALSE);
2846 g_return_val_if_fail (key != NULL, FALSE);
2848 group = g_key_file_lookup_group (key_file, group_name);
2852 g_set_error (error, G_KEY_FILE_ERROR,
2853 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2854 _("Key file does not have group '%s'"),
2855 group_name ? group_name : "(null)");
2860 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2862 return pair != NULL;
2866 g_key_file_add_group (GKeyFile *key_file,
2867 const gchar *group_name)
2869 GKeyFileGroup *group;
2871 g_return_if_fail (key_file != NULL);
2872 g_return_if_fail (group_name != NULL);
2874 if (g_key_file_lookup_group_node (key_file, group_name) != NULL)
2877 group = g_new0 (GKeyFileGroup, 1);
2878 group->name = g_strdup (group_name);
2879 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2880 key_file->groups = g_list_prepend (key_file->groups, group);
2881 key_file->approximate_size += strlen (group_name) + 3;
2882 key_file->current_group = group;
2884 if (key_file->start_group == NULL)
2885 key_file->start_group = group;
2889 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2894 g_free (pair->value);
2899 /* Be careful not to call this function on a node with data in the
2900 * lookup map without removing it from the lookup map, first.
2902 * Some current cases where this warning is not a concern are
2904 * - the node being removed is a comment node
2905 * - the entire lookup map is getting destroyed soon after
2909 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2910 GKeyFileGroup *group,
2914 GKeyFileKeyValuePair *pair;
2916 pair = (GKeyFileKeyValuePair *) pair_node->data;
2918 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2920 if (pair->key != NULL)
2921 key_file->approximate_size -= strlen (pair->key) + 1;
2923 g_assert (pair->value != NULL);
2924 key_file->approximate_size -= strlen (pair->value);
2926 g_key_file_key_value_pair_free (pair);
2928 g_list_free_1 (pair_node);
2932 g_key_file_remove_group_node (GKeyFile *key_file,
2935 GKeyFileGroup *group;
2938 group = (GKeyFileGroup *) group_node->data;
2940 /* If the current group gets deleted make the current group the last
2943 if (key_file->current_group == group)
2945 /* groups should always contain at least the top comment group,
2946 * unless g_key_file_clear has been called
2948 if (key_file->groups)
2949 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2951 key_file->current_group = NULL;
2954 /* If the start group gets deleted make the start group the first
2957 if (key_file->start_group == group)
2959 tmp = g_list_last (key_file->groups);
2962 if (tmp != group_node &&
2963 ((GKeyFileGroup *) tmp->data)->name != NULL)
2970 key_file->start_group = (GKeyFileGroup *) tmp->data;
2972 key_file->start_group = NULL;
2975 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2977 if (group->name != NULL)
2978 key_file->approximate_size -= strlen (group->name) + 3;
2980 tmp = group->key_value_pairs;
2987 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2990 g_assert (group->key_value_pairs == NULL);
2992 if (group->lookup_map)
2994 g_hash_table_destroy (group->lookup_map);
2995 group->lookup_map = NULL;
2998 g_free ((gchar *) group->name);
3000 g_list_free_1 (group_node);
3004 * g_key_file_remove_group:
3005 * @key_file: a #GKeyFile
3006 * @group_name: a group name
3007 * @error: return location for a #GError or %NULL
3009 * Removes the specified group, @group_name,
3010 * from the key file.
3015 g_key_file_remove_group (GKeyFile *key_file,
3016 const gchar *group_name,
3021 g_return_if_fail (key_file != NULL);
3022 g_return_if_fail (group_name != NULL);
3024 group_node = g_key_file_lookup_group_node (key_file, group_name);
3028 g_set_error (error, G_KEY_FILE_ERROR,
3029 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3030 _("Key file does not have group '%s'"),
3035 g_key_file_remove_group_node (key_file, group_node);
3039 g_key_file_add_key (GKeyFile *key_file,
3040 GKeyFileGroup *group,
3044 GKeyFileKeyValuePair *pair;
3046 pair = g_new0 (GKeyFileKeyValuePair, 1);
3048 pair->key = g_strdup (key);
3049 pair->value = g_strdup (value);
3051 g_hash_table_replace (group->lookup_map, pair->key, pair);
3052 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3053 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3057 * g_key_file_remove_key:
3058 * @key_file: a #GKeyFile
3059 * @group_name: a group name
3060 * @key: a key name to remove
3061 * @error: return location for a #GError or %NULL
3063 * Removes @key in @group_name from the key file.
3068 g_key_file_remove_key (GKeyFile *key_file,
3069 const gchar *group_name,
3073 GKeyFileGroup *group;
3074 GKeyFileKeyValuePair *pair;
3076 g_return_if_fail (key_file != NULL);
3077 g_return_if_fail (group_name != NULL);
3078 g_return_if_fail (key != NULL);
3082 group = g_key_file_lookup_group (key_file, group_name);
3085 g_set_error (error, G_KEY_FILE_ERROR,
3086 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3087 _("Key file does not have group '%s'"),
3088 group_name ? group_name : "(null)");
3092 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3096 g_set_error (error, G_KEY_FILE_ERROR,
3097 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3098 _("Key file does not have key '%s' in group '%s'"),
3103 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3105 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3106 g_hash_table_remove (group->lookup_map, pair->key);
3107 g_key_file_key_value_pair_free (pair);
3111 g_key_file_lookup_group_node (GKeyFile *key_file,
3112 const gchar *group_name)
3114 GKeyFileGroup *group;
3117 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3119 group = (GKeyFileGroup *) tmp->data;
3121 if (group && group->name && strcmp (group->name, group_name) == 0)
3128 static GKeyFileGroup *
3129 g_key_file_lookup_group (GKeyFile *key_file,
3130 const gchar *group_name)
3134 group_node = g_key_file_lookup_group_node (key_file, group_name);
3136 if (group_node != NULL)
3137 return (GKeyFileGroup *) group_node->data;
3143 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3144 GKeyFileGroup *group,
3149 for (key_node = group->key_value_pairs;
3151 key_node = key_node->next)
3153 GKeyFileKeyValuePair *pair;
3155 pair = (GKeyFileKeyValuePair *) key_node->data;
3157 if (pair->key && strcmp (pair->key, key) == 0)
3164 static GKeyFileKeyValuePair *
3165 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3166 GKeyFileGroup *group,
3169 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3172 /* Lines starting with # or consisting entirely of whitespace are merely
3173 * recorded, not parsed. This function assumes all leading whitespace
3174 * has been stripped.
3177 g_key_file_line_is_comment (const gchar *line)
3179 return (*line == '#' || *line == '\0' || *line == '\n');
3182 /* A group in a key file is made up of a starting '[' followed by one
3183 * or more letters making up the group name followed by ']'.
3186 g_key_file_line_is_group (const gchar *line)
3194 p = g_utf8_next_char (p);
3196 /* Group name must be non-empty
3198 if (!*p || *p == ']')
3201 while (*p && *p != ']')
3202 p = g_utf8_next_char (p);
3211 g_key_file_line_is_key_value_pair (const gchar *line)
3215 p = (gchar *) g_utf8_strchr (line, -1, '=');
3220 /* Key must be non-empty
3229 g_key_file_parse_value_as_string (GKeyFile *key_file,
3234 gchar *string_value, *p, *q0, *q;
3236 string_value = g_new0 (gchar, strlen (value) + 1);
3238 p = (gchar *) value;
3239 q0 = q = string_value;
3269 g_set_error (error, G_KEY_FILE_ERROR,
3270 G_KEY_FILE_ERROR_INVALID_VALUE,
3271 _("Key file contains escape character "
3276 if (pieces && *p == key_file->list_separator)
3277 *q = key_file->list_separator;
3291 g_set_error (error, G_KEY_FILE_ERROR,
3292 G_KEY_FILE_ERROR_INVALID_VALUE,
3293 _("Key file contains invalid escape "
3294 "sequence '%s'"), sequence);
3303 if (pieces && (*p == key_file->list_separator))
3305 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3321 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3322 *pieces = g_slist_reverse (*pieces);
3325 return string_value;
3329 g_key_file_parse_string_as_value (GKeyFile *key_file,
3330 const gchar *string,
3331 gboolean escape_separator)
3333 gchar *value, *p, *q;
3335 gboolean parsing_leading_space;
3337 length = strlen (string) + 1;
3339 /* Worst case would be that every character needs to be escaped.
3340 * In other words every character turns to two characters
3342 value = g_new0 (gchar, 2 * length);
3344 p = (gchar *) string;
3346 parsing_leading_space = TRUE;
3347 while (p < (string + length - 1))
3349 gchar escaped_character[3] = { '\\', 0, 0 };
3354 if (parsing_leading_space)
3356 escaped_character[1] = 's';
3357 strcpy (q, escaped_character);
3367 if (parsing_leading_space)
3369 escaped_character[1] = 't';
3370 strcpy (q, escaped_character);
3380 escaped_character[1] = 'n';
3381 strcpy (q, escaped_character);
3385 escaped_character[1] = 'r';
3386 strcpy (q, escaped_character);
3390 escaped_character[1] = '\\';
3391 strcpy (q, escaped_character);
3393 parsing_leading_space = FALSE;
3396 if (escape_separator && *p == key_file->list_separator)
3398 escaped_character[1] = key_file->list_separator;
3399 strcpy (q, escaped_character);
3401 parsing_leading_space = TRUE;
3407 parsing_leading_space = FALSE;
3419 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3423 gchar *end_of_valid_int;
3428 long_value = strtol (value, &end_of_valid_int, 10);
3430 if (*value == '\0' || *end_of_valid_int != '\0')
3432 g_set_error (error, G_KEY_FILE_ERROR,
3433 G_KEY_FILE_ERROR_INVALID_VALUE,
3434 _("Value '%s' cannot be interpreted as a number."), value);
3438 int_value = long_value;
3439 if (int_value != long_value || errno == ERANGE)
3443 G_KEY_FILE_ERROR_INVALID_VALUE,
3444 _("Integer value '%s' out of range"), value);
3452 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3456 return g_strdup_printf ("%d", value);
3460 g_key_file_parse_value_as_double (GKeyFile *key_file,
3464 gchar *end_of_valid_d;
3465 gdouble double_value = 0;
3467 double_value = g_ascii_strtod (value, &end_of_valid_d);
3469 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3470 g_set_error (error, G_KEY_FILE_ERROR,
3471 G_KEY_FILE_ERROR_INVALID_VALUE,
3472 _("Value '%s' cannot be interpreted as a float number."), value);
3474 return double_value;
3478 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3484 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3486 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3490 g_set_error (error, G_KEY_FILE_ERROR,
3491 G_KEY_FILE_ERROR_INVALID_VALUE,
3492 _("Value '%s' cannot be interpreted as a boolean."), value);
3498 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3502 return g_strdup ("true");
3504 return g_strdup ("false");
3508 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3515 string = g_string_sized_new (512);
3517 lines = g_strsplit (value, "\n", 0);
3519 for (i = 0; lines[i] != NULL; i++)
3521 if (lines[i][0] != '#')
3522 g_string_append_printf (string, "%s\n", lines[i]);
3524 g_string_append_printf (string, "%s\n", lines[i] + 1);
3528 return g_string_free (string, FALSE);
3532 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3533 const gchar *comment)
3539 string = g_string_sized_new (512);
3541 lines = g_strsplit (comment, "\n", 0);
3543 for (i = 0; lines[i] != NULL; i++)
3544 g_string_append_printf (string, "#%s%s", lines[i],
3545 lines[i + 1] == NULL? "" : "\n");
3548 return g_string_free (string, FALSE);
3551 #define __G_KEY_FILE_C__
3552 #include "galiasdef.c"