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 gchar *line_utf8 = _g_utf8_make_valid (line);
694 g_set_error (error, G_KEY_FILE_ERROR,
695 G_KEY_FILE_ERROR_PARSE,
696 _("Key file contains line '%s' which is not "
697 "a key-value pair, group, or comment"),
705 g_propagate_error (error, parse_error);
709 g_key_file_parse_comment (GKeyFile *key_file,
714 GKeyFileKeyValuePair *pair;
716 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
719 g_assert (key_file->current_group != NULL);
721 pair = g_new0 (GKeyFileKeyValuePair, 1);
724 pair->value = g_strndup (line, length);
726 key_file->current_group->key_value_pairs =
727 g_list_prepend (key_file->current_group->key_value_pairs, pair);
731 g_key_file_parse_group (GKeyFile *key_file,
737 const gchar *group_name_start, *group_name_end;
739 /* advance past opening '['
741 group_name_start = line + 1;
742 group_name_end = line + length - 1;
744 while (*group_name_end != ']')
747 group_name = g_strndup (group_name_start,
748 group_name_end - group_name_start);
750 g_key_file_add_group (key_file, group_name);
755 g_key_file_parse_key_value_pair (GKeyFile *key_file,
760 gchar *key, *value, *key_end, *value_start, *locale;
761 gsize key_len, value_len;
763 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
765 g_set_error (error, G_KEY_FILE_ERROR,
766 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
767 _("Key file does not start with a group"));
771 key_end = value_start = strchr (line, '=');
773 g_assert (key_end != NULL);
778 /* Pull the key name from the line (chomping trailing whitespace)
780 while (g_ascii_isspace (*key_end))
783 key_len = key_end - line + 2;
785 g_assert (key_len <= length);
787 key = g_strndup (line, key_len - 1);
789 /* Pull the value from the line (chugging leading whitespace)
791 while (g_ascii_isspace (*value_start))
794 value_len = line + length - value_start + 1;
796 value = g_strndup (value_start, value_len);
798 g_assert (key_file->start_group != NULL);
800 if (key_file->current_group
801 && key_file->current_group->name
802 && strcmp (key_file->start_group->name,
803 key_file->current_group->name) == 0
804 && strcmp (key, "Encoding") == 0)
806 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
808 gchar *value_utf8 = _g_utf8_make_valid (value);
809 g_set_error (error, G_KEY_FILE_ERROR,
810 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
811 _("Key file contains unsupported "
812 "encoding '%s'"), value_utf8);
821 /* Is this key a translation? If so, is it one that we care about?
823 locale = key_get_locale (key);
825 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
826 g_key_file_add_key (key_file, key_file->current_group, key, value);
834 key_get_locale (const gchar *key)
838 locale = g_strrstr (key, "[");
840 if (locale && strlen (locale) <= 2)
844 locale = g_strndup (locale + 1, strlen (locale) - 2);
850 g_key_file_parse_data (GKeyFile *key_file,
858 g_return_if_fail (key_file != NULL);
859 g_return_if_fail (data != NULL);
863 for (i = 0; i < length; i++)
867 if (i > 0 && data[i - 1] == '\r')
868 g_string_erase (key_file->parse_buffer,
869 key_file->parse_buffer->len - 1,
872 /* When a newline is encountered flush the parse buffer so that the
873 * line can be parsed. Note that completely blank lines won't show
874 * up in the parse buffer, so they get parsed directly.
876 if (key_file->parse_buffer->len > 0)
877 g_key_file_flush_parse_buffer (key_file, &parse_error);
879 g_key_file_parse_comment (key_file, "", 1, &parse_error);
883 g_propagate_error (error, parse_error);
888 g_string_append_c (key_file->parse_buffer, data[i]);
891 key_file->approximate_size += length;
895 g_key_file_flush_parse_buffer (GKeyFile *key_file,
898 GError *file_error = NULL;
900 g_return_if_fail (key_file != NULL);
904 if (key_file->parse_buffer->len > 0)
906 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
907 key_file->parse_buffer->len,
909 g_string_erase (key_file->parse_buffer, 0, -1);
913 g_propagate_error (error, file_error);
920 * g_key_file_to_data:
921 * @key_file: a #GKeyFile
922 * @length: return location for the length of the
923 * returned string, or %NULL
924 * @error: return location for a #GError, or %NULL
926 * This function outputs @key_file as a string.
928 * Return value: a newly allocated string holding
929 * the contents of the #GKeyFile
934 g_key_file_to_data (GKeyFile *key_file,
938 GString *data_string;
939 GList *group_node, *key_file_node;
941 g_return_val_if_fail (key_file != NULL, NULL);
943 data_string = g_string_sized_new (2 * key_file->approximate_size);
945 for (group_node = g_list_last (key_file->groups);
947 group_node = group_node->prev)
949 GKeyFileGroup *group;
951 group = (GKeyFileGroup *) group_node->data;
953 if (group->comment != NULL)
954 g_string_append_printf (data_string, "%s\n", group->comment->value);
955 else if (group_node->next) /* separate groups by at least an empty line */
956 g_string_append_c (data_string, '\n');
958 if (group->name != NULL)
959 g_string_append_printf (data_string, "[%s]\n", group->name);
961 for (key_file_node = g_list_last (group->key_value_pairs);
962 key_file_node != NULL;
963 key_file_node = key_file_node->prev)
965 GKeyFileKeyValuePair *pair;
967 pair = (GKeyFileKeyValuePair *) key_file_node->data;
969 if (pair->key != NULL)
970 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
972 g_string_append_printf (data_string, "%s\n", pair->value);
977 *length = data_string->len;
979 return g_string_free (data_string, FALSE);
983 * g_key_file_get_keys:
984 * @key_file: a #GKeyFile
985 * @group_name: a group name
986 * @length: return location for the number of keys returned, or %NULL
987 * @error: return location for a #GError, or %NULL
989 * Returns all keys for the group name @group_name. The array of
990 * returned keys will be %NULL-terminated, so @length may
991 * optionally be %NULL. In the event that the @group_name cannot
992 * be found, %NULL is returned and @error is set to
993 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
995 * Return value: a newly-allocated %NULL-terminated array of
996 * strings. Use g_strfreev() to free it.
1001 g_key_file_get_keys (GKeyFile *key_file,
1002 const gchar *group_name,
1006 GKeyFileGroup *group;
1011 g_return_val_if_fail (key_file != NULL, NULL);
1012 g_return_val_if_fail (group_name != NULL, NULL);
1014 group = g_key_file_lookup_group (key_file, group_name);
1018 g_set_error (error, G_KEY_FILE_ERROR,
1019 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1020 _("Key file does not have group '%s'"),
1021 group_name ? group_name : "(null)");
1026 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1028 GKeyFileKeyValuePair *pair;
1030 pair = (GKeyFileKeyValuePair *) tmp->data;
1036 keys = g_new0 (gchar *, num_keys + 1);
1039 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1041 GKeyFileKeyValuePair *pair;
1043 pair = (GKeyFileKeyValuePair *) tmp->data;
1047 keys[i] = g_strdup (pair->key);
1052 keys[num_keys] = NULL;
1061 * g_key_file_get_start_group:
1062 * @key_file: a #GKeyFile
1064 * Returns the name of the start group of the file.
1066 * Return value: The start group of the key file.
1071 g_key_file_get_start_group (GKeyFile *key_file)
1073 g_return_val_if_fail (key_file != NULL, NULL);
1075 if (key_file->start_group)
1076 return g_strdup (key_file->start_group->name);
1082 * g_key_file_get_groups:
1083 * @key_file: a #GKeyFile
1084 * @length: return location for the number of returned groups, or %NULL
1086 * Returns all groups in the key file loaded with @key_file. The
1087 * array of returned groups will be %NULL-terminated, so @length may
1088 * optionally be %NULL.
1090 * Return value: a newly-allocated %NULL-terminated array of strings.
1091 * Use g_strfreev() to free it.
1095 g_key_file_get_groups (GKeyFile *key_file,
1100 gsize i, num_groups;
1102 g_return_val_if_fail (key_file != NULL, NULL);
1104 num_groups = g_list_length (key_file->groups);
1106 g_assert (num_groups > 0);
1108 /* Only need num_groups instead of num_groups + 1
1109 * because the first group of the file (last in the
1110 * list) is always the comment group at the top,
1113 groups = g_new0 (gchar *, num_groups);
1115 group_node = g_list_last (key_file->groups);
1117 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1120 for (group_node = group_node->prev;
1122 group_node = group_node->prev)
1124 GKeyFileGroup *group;
1126 group = (GKeyFileGroup *) group_node->data;
1128 g_assert (group->name != NULL);
1130 groups[i++] = g_strdup (group->name);
1141 * g_key_file_get_value:
1142 * @key_file: a #GKeyFile
1143 * @group_name: a group name
1145 * @error: return location for a #GError, or %NULL
1147 * Returns the value associated with @key under @group_name.
1149 * In the event the key cannot be found, %NULL is returned and
1150 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1151 * event that the @group_name cannot be found, %NULL is returned
1152 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1154 * Return value: a newly allocated string or %NULL if the specified
1155 * key cannot be found.
1160 g_key_file_get_value (GKeyFile *key_file,
1161 const gchar *group_name,
1165 GKeyFileGroup *group;
1166 GKeyFileKeyValuePair *pair;
1167 gchar *value = NULL;
1169 g_return_val_if_fail (key_file != NULL, NULL);
1170 g_return_val_if_fail (group_name != NULL, NULL);
1171 g_return_val_if_fail (key != NULL, NULL);
1173 group = g_key_file_lookup_group (key_file, group_name);
1177 g_set_error (error, G_KEY_FILE_ERROR,
1178 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1179 _("Key file does not have group '%s'"),
1180 group_name ? group_name : "(null)");
1184 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1187 value = g_strdup (pair->value);
1189 g_set_error (error, G_KEY_FILE_ERROR,
1190 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1191 _("Key file does not have key '%s'"), key);
1197 * g_key_file_set_value:
1198 * @key_file: a #GKeyFile
1199 * @group_name: a group name
1203 * Associates a new value with @key under @group_name. If @key
1204 * cannot be found then it is created. If @group_name cannot be
1205 * found then it is created.
1210 g_key_file_set_value (GKeyFile *key_file,
1211 const gchar *group_name,
1215 GKeyFileGroup *group;
1216 GKeyFileKeyValuePair *pair;
1218 g_return_if_fail (key_file != NULL);
1219 g_return_if_fail (group_name != NULL);
1220 g_return_if_fail (key != NULL);
1221 g_return_if_fail (value != NULL);
1223 group = g_key_file_lookup_group (key_file, group_name);
1227 g_key_file_add_group (key_file, group_name);
1228 group = (GKeyFileGroup *) key_file->groups->data;
1230 g_key_file_add_key (key_file, group, key, value);
1234 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1237 g_key_file_add_key (key_file, group, key, value);
1240 g_free (pair->value);
1241 pair->value = g_strdup (value);
1247 * g_key_file_get_string:
1248 * @key_file: a #GKeyFile
1249 * @group_name: a group name
1251 * @error: return location for a #GError, or %NULL
1253 * Returns the value associated with @key under @group_name.
1255 * In the event the key cannot be found, %NULL is returned and
1256 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1257 * event that the @group_name cannot be found, %NULL is returned
1258 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1260 * Return value: a newly allocated string or %NULL if the specified
1261 * key cannot be found.
1266 g_key_file_get_string (GKeyFile *key_file,
1267 const gchar *group_name,
1271 gchar *value, *string_value;
1272 GError *key_file_error;
1274 g_return_val_if_fail (key_file != NULL, NULL);
1275 g_return_val_if_fail (group_name != NULL, NULL);
1276 g_return_val_if_fail (key != NULL, NULL);
1278 key_file_error = NULL;
1280 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1284 g_propagate_error (error, key_file_error);
1288 if (!g_utf8_validate (value, -1, NULL))
1290 gchar *value_utf8 = _g_utf8_make_valid (value);
1291 g_set_error (error, G_KEY_FILE_ERROR,
1292 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1293 _("Key file contains key '%s' with value '%s' "
1294 "which is not UTF-8"), key, value_utf8);
1295 g_free (value_utf8);
1301 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1307 if (g_error_matches (key_file_error,
1309 G_KEY_FILE_ERROR_INVALID_VALUE))
1311 g_set_error (error, G_KEY_FILE_ERROR,
1312 G_KEY_FILE_ERROR_INVALID_VALUE,
1313 _("Key file contains key '%s' "
1314 "which has value that cannot be interpreted."),
1316 g_error_free (key_file_error);
1319 g_propagate_error (error, key_file_error);
1322 return string_value;
1326 * g_key_file_set_string:
1327 * @key_file: a #GKeyFile
1328 * @group_name: a group name
1332 * Associates a new string value with @key under @group_name. If
1333 * @key cannot be found then it is created. If @group_name
1334 * cannot be found then it is created.
1339 g_key_file_set_string (GKeyFile *key_file,
1340 const gchar *group_name,
1342 const gchar *string)
1346 g_return_if_fail (key_file != NULL);
1347 g_return_if_fail (group_name != NULL);
1348 g_return_if_fail (key != NULL);
1349 g_return_if_fail (string != NULL);
1351 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1352 g_key_file_set_value (key_file, group_name, key, value);
1357 * g_key_file_get_string_list:
1358 * @key_file: a #GKeyFile
1359 * @group_name: a group name
1361 * @length: return location for the number of returned strings, or %NULL
1362 * @error: return location for a #GError, or %NULL
1364 * Returns the values associated with @key under @group_name.
1366 * In the event the key cannot be found, %NULL is returned and
1367 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1368 * event that the @group_name cannot be found, %NULL is returned
1369 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1371 * Return value: a %NULL-terminated string array or %NULL if the specified
1372 * key cannot be found. The array should be freed with g_strfreev().
1377 g_key_file_get_string_list (GKeyFile *key_file,
1378 const gchar *group_name,
1383 GError *key_file_error = NULL;
1384 gchar *value, *string_value, **values;
1386 GSList *p, *pieces = NULL;
1388 g_return_val_if_fail (key_file != NULL, NULL);
1389 g_return_val_if_fail (group_name != NULL, NULL);
1390 g_return_val_if_fail (key != NULL, NULL);
1392 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1396 g_propagate_error (error, key_file_error);
1400 if (!g_utf8_validate (value, -1, NULL))
1402 gchar *value_utf8 = _g_utf8_make_valid (value);
1403 g_set_error (error, G_KEY_FILE_ERROR,
1404 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1405 _("Key file contains key '%s' with value '%s' "
1406 "which is not UTF-8"), key, value_utf8);
1407 g_free (value_utf8);
1413 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1415 g_free (string_value);
1419 if (g_error_matches (key_file_error,
1421 G_KEY_FILE_ERROR_INVALID_VALUE))
1423 g_set_error (error, G_KEY_FILE_ERROR,
1424 G_KEY_FILE_ERROR_INVALID_VALUE,
1425 _("Key file contains key '%s' "
1426 "which has value that cannot be interpreted."),
1428 g_error_free (key_file_error);
1431 g_propagate_error (error, key_file_error);
1434 len = g_slist_length (pieces);
1435 values = g_new0 (gchar *, len + 1);
1436 for (p = pieces, i = 0; p; p = p->next)
1437 values[i++] = p->data;
1440 g_slist_free (pieces);
1449 * g_key_file_set_string_list:
1450 * @key_file: a #GKeyFile
1451 * @group_name: a group name
1453 * @list: an array of locale string values
1454 * @length: number of locale string values in @list
1456 * Associates a list of string values for @key under @group_name.
1457 * If @key cannot be found then it is created. If @group_name
1458 * cannot be found then it is created.
1463 g_key_file_set_string_list (GKeyFile *key_file,
1464 const gchar *group_name,
1466 const gchar * const list[],
1469 GString *value_list;
1472 g_return_if_fail (key_file != NULL);
1473 g_return_if_fail (group_name != NULL);
1474 g_return_if_fail (key != NULL);
1475 g_return_if_fail (list != NULL);
1477 value_list = g_string_sized_new (length * 128);
1478 for (i = 0; i < length && list[i] != NULL; i++)
1482 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1483 g_string_append (value_list, value);
1484 g_string_append_c (value_list, key_file->list_separator);
1489 g_key_file_set_value (key_file, group_name, key, value_list->str);
1490 g_string_free (value_list, TRUE);
1494 * g_key_file_set_locale_string:
1495 * @key_file: a #GKeyFile
1496 * @group_name: a group name
1501 * Associates a string value for @key and @locale under
1502 * @group_name. If the translation for @key cannot be found
1503 * then it is created.
1508 g_key_file_set_locale_string (GKeyFile *key_file,
1509 const gchar *group_name,
1511 const gchar *locale,
1512 const gchar *string)
1514 gchar *full_key, *value;
1516 g_return_if_fail (key_file != NULL);
1517 g_return_if_fail (group_name != NULL);
1518 g_return_if_fail (key != NULL);
1519 g_return_if_fail (locale != NULL);
1520 g_return_if_fail (string != NULL);
1522 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1523 full_key = g_strdup_printf ("%s[%s]", key, locale);
1524 g_key_file_set_value (key_file, group_name, full_key, value);
1529 extern GSList *_g_compute_locale_variants (const gchar *locale);
1532 * g_key_file_get_locale_string:
1533 * @key_file: a #GKeyFile
1534 * @group_name: a group name
1536 * @locale: a locale or %NULL
1537 * @error: return location for a #GError, or %NULL
1539 * Returns the value associated with @key under @group_name
1540 * translated in the given @locale if available. If @locale is
1541 * %NULL then the current locale is assumed.
1543 * If @key cannot be found then %NULL is returned and @error is set to
1544 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1545 * with @key cannot be interpreted or no suitable translation can
1546 * be found then the untranslated value is returned.
1548 * Return value: a newly allocated string or %NULL if the specified
1549 * key cannot be found.
1554 g_key_file_get_locale_string (GKeyFile *key_file,
1555 const gchar *group_name,
1557 const gchar *locale,
1560 gchar *candidate_key, *translated_value;
1561 GError *key_file_error;
1563 gboolean free_languages = FALSE;
1566 g_return_val_if_fail (key_file != NULL, NULL);
1567 g_return_val_if_fail (group_name != NULL, NULL);
1568 g_return_val_if_fail (key != NULL, NULL);
1570 candidate_key = NULL;
1571 translated_value = NULL;
1572 key_file_error = NULL;
1578 list = _g_compute_locale_variants (locale);
1580 languages = g_new0 (gchar *, g_slist_length (list) + 1);
1581 for (l = list, i = 0; l; l = l->next, i++)
1582 languages[i] = l->data;
1583 languages[i] = NULL;
1585 g_slist_free (list);
1586 free_languages = TRUE;
1590 languages = (gchar **) g_get_language_names ();
1591 free_languages = FALSE;
1594 for (i = 0; languages[i]; i++)
1596 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1598 translated_value = g_key_file_get_string (key_file,
1600 candidate_key, NULL);
1601 g_free (candidate_key);
1603 if (translated_value)
1606 g_free (translated_value);
1607 translated_value = NULL;
1610 /* Fallback to untranslated key
1612 if (!translated_value)
1614 translated_value = g_key_file_get_string (key_file, group_name, key,
1617 if (!translated_value)
1618 g_propagate_error (error, key_file_error);
1622 g_strfreev (languages);
1624 return translated_value;
1628 * g_key_file_get_locale_string_list:
1629 * @key_file: a #GKeyFile
1630 * @group_name: a group name
1633 * @length: return location for the number of returned strings or %NULL
1634 * @error: return location for a #GError or %NULL
1636 * Returns the values associated with @key under @group_name
1637 * translated in the given @locale if available. If @locale is
1638 * %NULL then the current locale is assumed.
1640 * If @key cannot be found then %NULL is returned and @error is set to
1641 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1642 * with @key cannot be interpreted or no suitable translations
1643 * can be found then the untranslated values are returned.
1644 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1646 * Return value: a newly allocated %NULL-terminated string array
1647 * or %NULL if the key isn't found. The string array should be freed
1648 * with g_strfreev().
1653 g_key_file_get_locale_string_list (GKeyFile *key_file,
1654 const gchar *group_name,
1656 const gchar *locale,
1660 GError *key_file_error;
1661 gchar **values, *value;
1663 g_return_val_if_fail (key_file != NULL, NULL);
1664 g_return_val_if_fail (group_name != NULL, NULL);
1665 g_return_val_if_fail (key != NULL, NULL);
1667 key_file_error = NULL;
1669 value = g_key_file_get_locale_string (key_file, group_name,
1674 g_propagate_error (error, key_file_error);
1679 if (value[strlen (value) - 1] == ';')
1680 value[strlen (value) - 1] = '\0';
1682 values = g_strsplit (value, ";", 0);
1687 *length = g_strv_length (values);
1693 * g_key_file_set_locale_string_list:
1694 * @key_file: a #GKeyFile
1695 * @group_name: a group name
1698 * @list: a %NULL-terminated array of locale string values
1699 * @length: the length of @list
1701 * Associates a list of string values for @key and @locale under
1702 * @group_name. If the translation for @key cannot be found then
1708 g_key_file_set_locale_string_list (GKeyFile *key_file,
1709 const gchar *group_name,
1711 const gchar *locale,
1712 const gchar * const list[],
1715 GString *value_list;
1719 g_return_if_fail (key_file != NULL);
1720 g_return_if_fail (group_name != NULL);
1721 g_return_if_fail (key != NULL);
1722 g_return_if_fail (locale != NULL);
1723 g_return_if_fail (length != 0);
1725 value_list = g_string_sized_new (length * 128);
1726 for (i = 0; i < length && list[i] != NULL; i++)
1730 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1732 g_string_append (value_list, value);
1733 g_string_append_c (value_list, ';');
1738 full_key = g_strdup_printf ("%s[%s]", key, locale);
1739 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1741 g_string_free (value_list, TRUE);
1745 * g_key_file_get_boolean:
1746 * @key_file: a #GKeyFile
1747 * @group_name: a group name
1749 * @error: return location for a #GError
1751 * Returns the value associated with @key under @group_name as a
1754 * If @key cannot be found then the return value is undefined and
1755 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1756 * the value associated with @key cannot be interpreted as a boolean
1757 * then the return value is also undefined and @error is set to
1758 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1760 * Return value: the value associated with the key as a boolean
1764 g_key_file_get_boolean (GKeyFile *key_file,
1765 const gchar *group_name,
1769 GError *key_file_error = NULL;
1771 gboolean bool_value;
1773 g_return_val_if_fail (key_file != NULL, FALSE);
1774 g_return_val_if_fail (group_name != NULL, FALSE);
1775 g_return_val_if_fail (key != NULL, FALSE);
1777 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1781 g_propagate_error (error, key_file_error);
1785 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1791 if (g_error_matches (key_file_error,
1793 G_KEY_FILE_ERROR_INVALID_VALUE))
1795 g_set_error (error, G_KEY_FILE_ERROR,
1796 G_KEY_FILE_ERROR_INVALID_VALUE,
1797 _("Key file contains key '%s' "
1798 "which has value that cannot be interpreted."),
1800 g_error_free (key_file_error);
1803 g_propagate_error (error, key_file_error);
1810 * g_key_file_set_boolean:
1811 * @key_file: a #GKeyFile
1812 * @group_name: a group name
1814 * @value: %TRUE or %FALSE
1816 * Associates a new boolean value with @key under @group_name.
1817 * If @key cannot be found then it is created.
1822 g_key_file_set_boolean (GKeyFile *key_file,
1823 const gchar *group_name,
1829 g_return_if_fail (key_file != NULL);
1830 g_return_if_fail (group_name != NULL);
1831 g_return_if_fail (key != NULL);
1833 result = g_key_file_parse_boolean_as_value (key_file, value);
1834 g_key_file_set_value (key_file, group_name, key, result);
1839 * g_key_file_get_boolean_list:
1840 * @key_file: a #GKeyFile
1841 * @group_name: a group name
1843 * @length: the number of booleans returned
1844 * @error: return location for a #GError
1846 * Returns the values associated with @key under @group_name as
1847 * booleans. If @group_name is %NULL, the start_group is used.
1849 * If @key cannot be found then the return value is undefined and
1850 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1851 * the values associated with @key cannot be interpreted as booleans
1852 * then the return value is also undefined and @error is set to
1853 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1855 * Return value: the values associated with the key as a boolean
1860 g_key_file_get_boolean_list (GKeyFile *key_file,
1861 const gchar *group_name,
1866 GError *key_file_error;
1868 gboolean *bool_values;
1871 g_return_val_if_fail (key_file != NULL, NULL);
1872 g_return_val_if_fail (group_name != NULL, NULL);
1873 g_return_val_if_fail (key != NULL, NULL);
1875 key_file_error = NULL;
1877 values = g_key_file_get_string_list (key_file, group_name, key,
1878 &num_bools, &key_file_error);
1881 g_propagate_error (error, key_file_error);
1886 bool_values = g_new0 (gboolean, num_bools);
1888 for (i = 0; i < num_bools; i++)
1890 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1896 g_propagate_error (error, key_file_error);
1897 g_strfreev (values);
1898 g_free (bool_values);
1903 g_strfreev (values);
1906 *length = num_bools;
1912 * g_key_file_set_boolean_list:
1913 * @key_file: a #GKeyFile
1914 * @group_name: a group name
1916 * @list: an array of boolean values
1917 * @length: length of @list
1919 * Associates a list of boolean values with @key under
1920 * @group_name. If @key cannot be found then it is created.
1921 * If @group_name is %NULL, the start_group is used.
1926 g_key_file_set_boolean_list (GKeyFile *key_file,
1927 const gchar *group_name,
1932 GString *value_list;
1935 g_return_if_fail (key_file != NULL);
1936 g_return_if_fail (group_name != NULL);
1937 g_return_if_fail (key != NULL);
1938 g_return_if_fail (list != NULL);
1940 value_list = g_string_sized_new (length * 8);
1941 for (i = 0; i < length; i++)
1945 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1947 g_string_append (value_list, value);
1948 g_string_append_c (value_list, key_file->list_separator);
1953 g_key_file_set_value (key_file, group_name, key, value_list->str);
1954 g_string_free (value_list, TRUE);
1958 * g_key_file_get_integer:
1959 * @key_file: a #GKeyFile
1960 * @group_name: a group name
1962 * @error: return location for a #GError
1964 * Returns the value associated with @key under @group_name as an
1965 * integer. If @group_name is %NULL, the start_group is used.
1967 * If @key cannot be found then the return value is undefined and
1968 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1969 * the value associated with @key cannot be interpreted as an integer
1970 * then the return value is also undefined and @error is set to
1971 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1973 * Return value: the value associated with the key as an integer.
1978 g_key_file_get_integer (GKeyFile *key_file,
1979 const gchar *group_name,
1983 GError *key_file_error;
1987 g_return_val_if_fail (key_file != NULL, -1);
1988 g_return_val_if_fail (group_name != NULL, -1);
1989 g_return_val_if_fail (key != NULL, -1);
1991 key_file_error = NULL;
1993 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1997 g_propagate_error (error, key_file_error);
2001 int_value = g_key_file_parse_value_as_integer (key_file, value,
2007 if (g_error_matches (key_file_error,
2009 G_KEY_FILE_ERROR_INVALID_VALUE))
2011 g_set_error (error, G_KEY_FILE_ERROR,
2012 G_KEY_FILE_ERROR_INVALID_VALUE,
2013 _("Key file contains key '%s' in group '%s' "
2014 "which has value that cannot be interpreted."), key,
2016 g_error_free (key_file_error);
2019 g_propagate_error (error, key_file_error);
2026 * g_key_file_set_integer:
2027 * @key_file: a #GKeyFile
2028 * @group_name: a group name
2030 * @value: an integer value
2032 * Associates a new integer value with @key under @group_name.
2033 * If @key cannot be found then it is created.
2038 g_key_file_set_integer (GKeyFile *key_file,
2039 const gchar *group_name,
2045 g_return_if_fail (key_file != NULL);
2046 g_return_if_fail (group_name != NULL);
2047 g_return_if_fail (key != NULL);
2049 result = g_key_file_parse_integer_as_value (key_file, value);
2050 g_key_file_set_value (key_file, group_name, key, result);
2055 * g_key_file_get_integer_list:
2056 * @key_file: a #GKeyFile
2057 * @group_name: a group name
2059 * @length: the number of integers returned
2060 * @error: return location for a #GError
2062 * Returns the values associated with @key under @group_name as
2065 * If @key cannot be found then the return value is undefined and
2066 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2067 * the values associated with @key cannot be interpreted as integers
2068 * then the return value is also undefined and @error is set to
2069 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2071 * Return value: the values associated with the key as a integer
2076 g_key_file_get_integer_list (GKeyFile *key_file,
2077 const gchar *group_name,
2082 GError *key_file_error = NULL;
2087 g_return_val_if_fail (key_file != NULL, NULL);
2088 g_return_val_if_fail (group_name != NULL, NULL);
2089 g_return_val_if_fail (key != NULL, NULL);
2091 values = g_key_file_get_string_list (key_file, group_name, key,
2092 &num_ints, &key_file_error);
2095 g_propagate_error (error, key_file_error);
2100 int_values = g_new0 (gint, num_ints);
2102 for (i = 0; i < num_ints; i++)
2104 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2110 g_propagate_error (error, key_file_error);
2111 g_strfreev (values);
2112 g_free (int_values);
2117 g_strfreev (values);
2126 * g_key_file_set_integer_list:
2127 * @key_file: a #GKeyFile
2128 * @group_name: a group name
2130 * @list: an array of integer values
2131 * @length: number of integer values in @list
2133 * Associates a list of integer values with @key under
2134 * @group_name. If @key cannot be found then it is created.
2139 g_key_file_set_integer_list (GKeyFile *key_file,
2140 const gchar *group_name,
2148 g_return_if_fail (key_file != NULL);
2149 g_return_if_fail (group_name != NULL);
2150 g_return_if_fail (key != NULL);
2151 g_return_if_fail (list != NULL);
2153 values = g_string_sized_new (length * 16);
2154 for (i = 0; i < length; i++)
2158 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2160 g_string_append (values, value);
2161 g_string_append_c (values, ';');
2166 g_key_file_set_value (key_file, group_name, key, values->str);
2167 g_string_free (values, TRUE);
2171 * g_key_file_get_double:
2172 * @key_file: a #GKeyFile
2173 * @group_name: a group name
2175 * @error: return location for a #GError
2177 * Returns the value associated with @key under @group_name as an
2178 * integer. If @group_name is %NULL, the start_group is used.
2180 * If @key cannot be found then the return value is undefined and
2181 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2182 * the value associated with @key cannot be interpreted as a double
2183 * then the return value is also undefined and @error is set to
2184 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2186 * Return value: the value associated with the key as a double.
2191 g_key_file_get_double (GKeyFile *key_file,
2192 const gchar *group_name,
2196 GError *key_file_error;
2198 gdouble double_value;
2200 g_return_val_if_fail (key_file != NULL, -1);
2201 g_return_val_if_fail (group_name != NULL, -1);
2202 g_return_val_if_fail (key != NULL, -1);
2204 key_file_error = NULL;
2206 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2210 g_propagate_error (error, key_file_error);
2214 double_value = g_key_file_parse_value_as_double (key_file, value,
2220 if (g_error_matches (key_file_error,
2222 G_KEY_FILE_ERROR_INVALID_VALUE))
2224 g_set_error (error, G_KEY_FILE_ERROR,
2225 G_KEY_FILE_ERROR_INVALID_VALUE,
2226 _("Key file contains key '%s' in group '%s' "
2227 "which has value that cannot be interpreted."), key,
2229 g_error_free (key_file_error);
2232 g_propagate_error (error, key_file_error);
2235 return double_value;
2239 * g_key_file_set_double:
2240 * @key_file: a #GKeyFile
2241 * @group_name: a group name
2243 * @value: an double value
2245 * Associates a new double value with @key under @group_name.
2246 * If @key cannot be found then it is created. If @group_name
2247 * is %NULL, the start group is used.
2252 g_key_file_set_double (GKeyFile *key_file,
2253 const gchar *group_name,
2257 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2259 g_return_if_fail (key_file != NULL);
2260 g_return_if_fail (group_name != NULL);
2261 g_return_if_fail (key != NULL);
2263 g_ascii_dtostr ( result, sizeof (result), value );
2264 g_key_file_set_value (key_file, group_name, key, result);
2268 * g_key_file_get_double_list:
2269 * @key_file: a #GKeyFile
2270 * @group_name: a group name
2272 * @length: the number of doubles returned
2273 * @error: return location for a #GError
2275 * Returns the values associated with @key under @group_name as
2276 * doubles. If @group_name is %NULL, the start group is used.
2278 * If @key cannot be found then the return value is undefined and
2279 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2280 * the values associated with @key cannot be interpreted as doubles
2281 * then the return value is also undefined and @error is set to
2282 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2284 * Return value: the values associated with the key as a double
2289 g_key_file_get_double_list (GKeyFile *key_file,
2290 const gchar *group_name,
2295 GError *key_file_error = NULL;
2297 gdouble *double_values;
2298 gsize i, num_doubles;
2300 g_return_val_if_fail (key_file != NULL, NULL);
2301 g_return_val_if_fail (group_name != NULL, NULL);
2302 g_return_val_if_fail (key != NULL, NULL);
2304 values = g_key_file_get_string_list (key_file, group_name, key,
2305 &num_doubles, &key_file_error);
2308 g_propagate_error (error, key_file_error);
2313 double_values = g_new0 (gdouble, num_doubles);
2315 for (i = 0; i < num_doubles; i++)
2317 double_values[i] = g_key_file_parse_value_as_double (key_file,
2323 g_propagate_error (error, key_file_error);
2324 g_strfreev (values);
2325 g_free (double_values);
2330 g_strfreev (values);
2333 *length = num_doubles;
2335 return double_values;
2339 * g_key_file_set_double_list:
2340 * @key_file: a #GKeyFile
2341 * @group_name: a group name
2343 * @list: an array of double values
2344 * @length: number of double values in @list
2346 * Associates a list of double values with @key under
2347 * @group_name. If @key cannot be found then it is created.
2348 * If @group_name is %NULL the start group is used.
2353 g_key_file_set_double_list (GKeyFile *key_file,
2354 const gchar *group_name,
2362 g_return_if_fail (key_file != NULL);
2363 g_return_if_fail (group_name != NULL);
2364 g_return_if_fail (key != NULL);
2365 g_return_if_fail (list != NULL);
2367 values = g_string_sized_new (length * 16);
2368 for (i = 0; i < length; i++)
2370 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2372 g_ascii_dtostr( result, sizeof (result), list[i] );
2374 g_string_append (values, result);
2375 g_string_append_c (values, ';');
2378 g_key_file_set_value (key_file, group_name, key, values->str);
2379 g_string_free (values, TRUE);
2383 g_key_file_set_key_comment (GKeyFile *key_file,
2384 const gchar *group_name,
2386 const gchar *comment,
2389 GKeyFileGroup *group;
2390 GKeyFileKeyValuePair *pair;
2391 GList *key_node, *comment_node, *tmp;
2393 group = g_key_file_lookup_group (key_file, group_name);
2396 g_set_error (error, G_KEY_FILE_ERROR,
2397 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2398 _("Key file does not have group '%s'"),
2399 group_name ? group_name : "(null)");
2404 /* First find the key the comments are supposed to be
2407 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2409 if (key_node == NULL)
2411 g_set_error (error, G_KEY_FILE_ERROR,
2412 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2413 _("Key file does not have key '%s' in group '%s'"),
2418 /* Then find all the comments already associated with the
2421 tmp = key_node->next;
2424 GKeyFileKeyValuePair *pair;
2426 pair = (GKeyFileKeyValuePair *) tmp->data;
2428 if (pair->key != NULL)
2433 g_key_file_remove_key_value_pair_node (key_file, group,
2437 if (comment == NULL)
2440 /* Now we can add our new comment
2442 pair = g_new0 (GKeyFileKeyValuePair, 1);
2445 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2447 key_node = g_list_insert (key_node, pair, 1);
2451 g_key_file_set_group_comment (GKeyFile *key_file,
2452 const gchar *group_name,
2453 const gchar *comment,
2456 GKeyFileGroup *group;
2458 group = g_key_file_lookup_group (key_file, group_name);
2461 g_set_error (error, G_KEY_FILE_ERROR,
2462 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2463 _("Key file does not have group '%s'"),
2464 group_name ? group_name : "(null)");
2469 /* First remove any existing comment
2473 g_key_file_key_value_pair_free (group->comment);
2474 group->comment = NULL;
2477 if (comment == NULL)
2480 /* Now we can add our new comment
2482 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2484 group->comment->key = NULL;
2485 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2489 g_key_file_set_top_comment (GKeyFile *key_file,
2490 const gchar *comment,
2494 GKeyFileGroup *group;
2495 GKeyFileKeyValuePair *pair;
2497 /* The last group in the list should be the top (comments only)
2500 g_assert (key_file->groups != NULL);
2501 group_node = g_list_last (key_file->groups);
2502 group = (GKeyFileGroup *) group_node->data;
2503 g_assert (group->name == NULL);
2505 /* Note all keys must be comments at the top of
2506 * the file, so we can just free it all.
2508 if (group->key_value_pairs != NULL)
2510 g_list_foreach (group->key_value_pairs,
2511 (GFunc) g_key_file_key_value_pair_free,
2513 g_list_free (group->key_value_pairs);
2514 group->key_value_pairs = NULL;
2517 if (comment == NULL)
2520 pair = g_new0 (GKeyFileKeyValuePair, 1);
2523 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2525 group->key_value_pairs =
2526 g_list_prepend (group->key_value_pairs, pair);
2530 * g_key_file_set_comment:
2531 * @key_file: a #GKeyFile
2532 * @group_name: a group name, or %NULL
2534 * @comment: a comment
2535 * @error: return location for a #GError
2537 * Places a comment above @key from @group_name.
2538 * @group_name. If @key is %NULL then @comment will
2539 * be written above @group_name. If both @key
2540 * and @group_name are NULL, then @comment will
2541 * be written above the first group in the file.
2546 g_key_file_set_comment (GKeyFile *key_file,
2547 const gchar *group_name,
2549 const gchar *comment,
2552 g_return_if_fail (key_file != NULL);
2554 if (group_name != NULL && key != NULL)
2555 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2556 else if (group_name != NULL)
2557 g_key_file_set_group_comment (key_file, group_name, comment, error);
2559 g_key_file_set_top_comment (key_file, comment, error);
2561 if (comment != NULL)
2562 key_file->approximate_size += strlen (comment);
2566 g_key_file_get_key_comment (GKeyFile *key_file,
2567 const gchar *group_name,
2571 GKeyFileGroup *group;
2572 GKeyFileKeyValuePair *pair;
2573 GList *key_node, *tmp;
2577 group = g_key_file_lookup_group (key_file, group_name);
2580 g_set_error (error, G_KEY_FILE_ERROR,
2581 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2582 _("Key file does not have group '%s'"),
2583 group_name ? group_name : "(null)");
2588 /* First find the key the comments are supposed to be
2591 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2593 if (key_node == NULL)
2595 g_set_error (error, G_KEY_FILE_ERROR,
2596 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2597 _("Key file does not have key '%s' in group '%s'"),
2604 /* Then find all the comments already associated with the
2605 * key and concatentate them.
2607 tmp = key_node->next;
2608 if (!key_node->next)
2611 pair = (GKeyFileKeyValuePair *) tmp->data;
2612 if (pair->key != NULL)
2617 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2619 if (pair->key != NULL)
2625 while (tmp != key_node)
2627 GKeyFileKeyValuePair *pair;
2629 pair = (GKeyFileKeyValuePair *) tmp->data;
2632 string = g_string_sized_new (512);
2634 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2635 g_string_append (string, comment);
2643 comment = string->str;
2644 g_string_free (string, FALSE);
2653 get_group_comment (GKeyFile *key_file,
2654 GKeyFileGroup *group,
2663 tmp = group->key_value_pairs;
2666 GKeyFileKeyValuePair *pair;
2668 pair = (GKeyFileKeyValuePair *) tmp->data;
2670 if (pair->key != NULL)
2676 if (tmp->next == NULL)
2684 GKeyFileKeyValuePair *pair;
2686 pair = (GKeyFileKeyValuePair *) tmp->data;
2689 string = g_string_sized_new (512);
2691 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2692 g_string_append (string, comment);
2699 return g_string_free (string, FALSE);
2705 g_key_file_get_group_comment (GKeyFile *key_file,
2706 const gchar *group_name,
2710 GKeyFileGroup *group;
2712 group_node = g_key_file_lookup_group_node (key_file, group_name);
2715 g_set_error (error, G_KEY_FILE_ERROR,
2716 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2717 _("Key file does not have group '%s'"),
2718 group_name ? group_name : "(null)");
2723 group = (GKeyFileGroup *)group_node->data;
2725 return g_strdup (group->comment->value);
2727 group_node = group_node->next;
2728 group = (GKeyFileGroup *)group_node->data;
2729 return get_group_comment (key_file, group, error);
2733 g_key_file_get_top_comment (GKeyFile *key_file,
2737 GKeyFileGroup *group;
2739 /* The last group in the list should be the top (comments only)
2742 g_assert (key_file->groups != NULL);
2743 group_node = g_list_last (key_file->groups);
2744 group = (GKeyFileGroup *) group_node->data;
2745 g_assert (group->name == NULL);
2747 return get_group_comment (key_file, group, error);
2751 * g_key_file_get_comment:
2752 * @key_file: a #GKeyFile
2753 * @group_name: a group name, or %NULL
2755 * @error: return location for a #GError
2757 * Retrieves a comment above @key from @group_name.
2758 * @group_name. If @key is %NULL then @comment will
2759 * be read from above @group_name. If both @key
2760 * and @group_name are NULL, then @comment will
2761 * be read from above the first group in the file.
2763 * Returns: a comment that should be freed with g_free()
2768 g_key_file_get_comment (GKeyFile *key_file,
2769 const gchar *group_name,
2773 g_return_val_if_fail (key_file != NULL, NULL);
2775 if (group_name != NULL && key != NULL)
2776 return g_key_file_get_key_comment (key_file, group_name, key, error);
2777 else if (group_name != NULL)
2778 return g_key_file_get_group_comment (key_file, group_name, error);
2780 return g_key_file_get_top_comment (key_file, error);
2784 * g_key_file_remove_comment:
2785 * @key_file: a #GKeyFile
2786 * @group_name: a group name, or %NULL
2788 * @error: return location for a #GError
2790 * Removes a comment above @key from @group_name.
2791 * @group_name. If @key is %NULL then @comment will
2792 * be written above @group_name. If both @key
2793 * and @group_name are NULL, then @comment will
2794 * be written above the first group in the file.
2800 g_key_file_remove_comment (GKeyFile *key_file,
2801 const gchar *group_name,
2805 g_return_if_fail (key_file != NULL);
2807 if (group_name != NULL && key != NULL)
2808 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2809 else if (group_name != NULL)
2810 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2812 g_key_file_set_top_comment (key_file, NULL, error);
2816 * g_key_file_has_group:
2817 * @key_file: a #GKeyFile
2818 * @group_name: a group name
2820 * Looks whether the key file has the group @group_name.
2822 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2827 g_key_file_has_group (GKeyFile *key_file,
2828 const gchar *group_name)
2830 g_return_val_if_fail (key_file != NULL, FALSE);
2831 g_return_val_if_fail (group_name != NULL, FALSE);
2833 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2837 * g_key_file_has_key:
2838 * @key_file: a #GKeyFile
2839 * @group_name: a group name
2841 * @error: return location for a #GError
2843 * Looks whether the key file has the key @key in the group
2846 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2852 g_key_file_has_key (GKeyFile *key_file,
2853 const gchar *group_name,
2857 GKeyFileKeyValuePair *pair;
2858 GKeyFileGroup *group;
2860 g_return_val_if_fail (key_file != NULL, FALSE);
2861 g_return_val_if_fail (group_name != NULL, FALSE);
2862 g_return_val_if_fail (key != NULL, FALSE);
2864 group = g_key_file_lookup_group (key_file, group_name);
2868 g_set_error (error, G_KEY_FILE_ERROR,
2869 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2870 _("Key file does not have group '%s'"),
2871 group_name ? group_name : "(null)");
2876 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2878 return pair != NULL;
2882 g_key_file_add_group (GKeyFile *key_file,
2883 const gchar *group_name)
2885 GKeyFileGroup *group;
2887 g_return_if_fail (key_file != NULL);
2888 g_return_if_fail (group_name != NULL);
2890 group = g_key_file_lookup_group (key_file, group_name);
2893 key_file->current_group = group;
2897 group = g_new0 (GKeyFileGroup, 1);
2898 group->name = g_strdup (group_name);
2899 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2900 key_file->groups = g_list_prepend (key_file->groups, group);
2901 key_file->approximate_size += strlen (group_name) + 3;
2902 key_file->current_group = group;
2904 if (key_file->start_group == NULL)
2905 key_file->start_group = group;
2909 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2914 g_free (pair->value);
2919 /* Be careful not to call this function on a node with data in the
2920 * lookup map without removing it from the lookup map, first.
2922 * Some current cases where this warning is not a concern are
2924 * - the node being removed is a comment node
2925 * - the entire lookup map is getting destroyed soon after
2929 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2930 GKeyFileGroup *group,
2934 GKeyFileKeyValuePair *pair;
2936 pair = (GKeyFileKeyValuePair *) pair_node->data;
2938 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2940 if (pair->key != NULL)
2941 key_file->approximate_size -= strlen (pair->key) + 1;
2943 g_assert (pair->value != NULL);
2944 key_file->approximate_size -= strlen (pair->value);
2946 g_key_file_key_value_pair_free (pair);
2948 g_list_free_1 (pair_node);
2952 g_key_file_remove_group_node (GKeyFile *key_file,
2955 GKeyFileGroup *group;
2958 group = (GKeyFileGroup *) group_node->data;
2960 /* If the current group gets deleted make the current group the last
2963 if (key_file->current_group == group)
2965 /* groups should always contain at least the top comment group,
2966 * unless g_key_file_clear has been called
2968 if (key_file->groups)
2969 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2971 key_file->current_group = NULL;
2974 /* If the start group gets deleted make the start group the first
2977 if (key_file->start_group == group)
2979 tmp = g_list_last (key_file->groups);
2982 if (tmp != group_node &&
2983 ((GKeyFileGroup *) tmp->data)->name != NULL)
2990 key_file->start_group = (GKeyFileGroup *) tmp->data;
2992 key_file->start_group = NULL;
2995 key_file->groups = g_list_remove_link (key_file->groups, group_node);
2997 if (group->name != NULL)
2998 key_file->approximate_size -= strlen (group->name) + 3;
3000 tmp = group->key_value_pairs;
3007 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3010 g_assert (group->key_value_pairs == NULL);
3012 if (group->lookup_map)
3014 g_hash_table_destroy (group->lookup_map);
3015 group->lookup_map = NULL;
3018 g_free ((gchar *) group->name);
3020 g_list_free_1 (group_node);
3024 * g_key_file_remove_group:
3025 * @key_file: a #GKeyFile
3026 * @group_name: a group name
3027 * @error: return location for a #GError or %NULL
3029 * Removes the specified group, @group_name,
3030 * from the key file.
3035 g_key_file_remove_group (GKeyFile *key_file,
3036 const gchar *group_name,
3041 g_return_if_fail (key_file != NULL);
3042 g_return_if_fail (group_name != NULL);
3044 group_node = g_key_file_lookup_group_node (key_file, group_name);
3048 g_set_error (error, G_KEY_FILE_ERROR,
3049 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3050 _("Key file does not have group '%s'"),
3055 g_key_file_remove_group_node (key_file, group_node);
3059 g_key_file_add_key (GKeyFile *key_file,
3060 GKeyFileGroup *group,
3064 GKeyFileKeyValuePair *pair;
3066 pair = g_new0 (GKeyFileKeyValuePair, 1);
3068 pair->key = g_strdup (key);
3069 pair->value = g_strdup (value);
3071 g_hash_table_replace (group->lookup_map, pair->key, pair);
3072 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3073 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3077 * g_key_file_remove_key:
3078 * @key_file: a #GKeyFile
3079 * @group_name: a group name
3080 * @key: a key name to remove
3081 * @error: return location for a #GError or %NULL
3083 * Removes @key in @group_name from the key file.
3088 g_key_file_remove_key (GKeyFile *key_file,
3089 const gchar *group_name,
3093 GKeyFileGroup *group;
3094 GKeyFileKeyValuePair *pair;
3096 g_return_if_fail (key_file != NULL);
3097 g_return_if_fail (group_name != NULL);
3098 g_return_if_fail (key != NULL);
3102 group = g_key_file_lookup_group (key_file, group_name);
3105 g_set_error (error, G_KEY_FILE_ERROR,
3106 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3107 _("Key file does not have group '%s'"),
3108 group_name ? group_name : "(null)");
3112 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3116 g_set_error (error, G_KEY_FILE_ERROR,
3117 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3118 _("Key file does not have key '%s' in group '%s'"),
3123 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3125 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3126 g_hash_table_remove (group->lookup_map, pair->key);
3127 g_key_file_key_value_pair_free (pair);
3131 g_key_file_lookup_group_node (GKeyFile *key_file,
3132 const gchar *group_name)
3134 GKeyFileGroup *group;
3137 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3139 group = (GKeyFileGroup *) tmp->data;
3141 if (group && group->name && strcmp (group->name, group_name) == 0)
3148 static GKeyFileGroup *
3149 g_key_file_lookup_group (GKeyFile *key_file,
3150 const gchar *group_name)
3154 group_node = g_key_file_lookup_group_node (key_file, group_name);
3156 if (group_node != NULL)
3157 return (GKeyFileGroup *) group_node->data;
3163 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3164 GKeyFileGroup *group,
3169 for (key_node = group->key_value_pairs;
3171 key_node = key_node->next)
3173 GKeyFileKeyValuePair *pair;
3175 pair = (GKeyFileKeyValuePair *) key_node->data;
3177 if (pair->key && strcmp (pair->key, key) == 0)
3184 static GKeyFileKeyValuePair *
3185 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3186 GKeyFileGroup *group,
3189 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3192 /* Lines starting with # or consisting entirely of whitespace are merely
3193 * recorded, not parsed. This function assumes all leading whitespace
3194 * has been stripped.
3197 g_key_file_line_is_comment (const gchar *line)
3199 return (*line == '#' || *line == '\0' || *line == '\n');
3202 /* A group in a key file is made up of a starting '[' followed by one
3203 * or more letters making up the group name followed by ']'.
3206 g_key_file_line_is_group (const gchar *line)
3214 p = g_utf8_next_char (p);
3216 /* Group name must be non-empty
3218 if (!*p || *p == ']')
3221 while (*p && *p != ']')
3222 p = g_utf8_next_char (p);
3231 g_key_file_line_is_key_value_pair (const gchar *line)
3235 p = (gchar *) g_utf8_strchr (line, -1, '=');
3240 /* Key must be non-empty
3249 g_key_file_parse_value_as_string (GKeyFile *key_file,
3254 gchar *string_value, *p, *q0, *q;
3256 string_value = g_new0 (gchar, strlen (value) + 1);
3258 p = (gchar *) value;
3259 q0 = q = string_value;
3289 g_set_error (error, G_KEY_FILE_ERROR,
3290 G_KEY_FILE_ERROR_INVALID_VALUE,
3291 _("Key file contains escape character "
3296 if (pieces && *p == key_file->list_separator)
3297 *q = key_file->list_separator;
3311 g_set_error (error, G_KEY_FILE_ERROR,
3312 G_KEY_FILE_ERROR_INVALID_VALUE,
3313 _("Key file contains invalid escape "
3314 "sequence '%s'"), sequence);
3323 if (pieces && (*p == key_file->list_separator))
3325 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3341 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3342 *pieces = g_slist_reverse (*pieces);
3345 return string_value;
3349 g_key_file_parse_string_as_value (GKeyFile *key_file,
3350 const gchar *string,
3351 gboolean escape_separator)
3353 gchar *value, *p, *q;
3355 gboolean parsing_leading_space;
3357 length = strlen (string) + 1;
3359 /* Worst case would be that every character needs to be escaped.
3360 * In other words every character turns to two characters
3362 value = g_new0 (gchar, 2 * length);
3364 p = (gchar *) string;
3366 parsing_leading_space = TRUE;
3367 while (p < (string + length - 1))
3369 gchar escaped_character[3] = { '\\', 0, 0 };
3374 if (parsing_leading_space)
3376 escaped_character[1] = 's';
3377 strcpy (q, escaped_character);
3387 if (parsing_leading_space)
3389 escaped_character[1] = 't';
3390 strcpy (q, escaped_character);
3400 escaped_character[1] = 'n';
3401 strcpy (q, escaped_character);
3405 escaped_character[1] = 'r';
3406 strcpy (q, escaped_character);
3410 escaped_character[1] = '\\';
3411 strcpy (q, escaped_character);
3413 parsing_leading_space = FALSE;
3416 if (escape_separator && *p == key_file->list_separator)
3418 escaped_character[1] = key_file->list_separator;
3419 strcpy (q, escaped_character);
3421 parsing_leading_space = TRUE;
3427 parsing_leading_space = FALSE;
3439 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3443 gchar *end_of_valid_int;
3448 long_value = strtol (value, &end_of_valid_int, 10);
3450 if (*value == '\0' || *end_of_valid_int != '\0')
3452 gchar *value_utf8 = _g_utf8_make_valid (value);
3453 g_set_error (error, G_KEY_FILE_ERROR,
3454 G_KEY_FILE_ERROR_INVALID_VALUE,
3455 _("Value '%s' cannot be interpreted "
3456 "as a number."), value_utf8);
3457 g_free (value_utf8);
3462 int_value = long_value;
3463 if (int_value != long_value || errno == ERANGE)
3465 gchar *value_utf8 = _g_utf8_make_valid (value);
3468 G_KEY_FILE_ERROR_INVALID_VALUE,
3469 _("Integer value '%s' out of range"),
3471 g_free (value_utf8);
3480 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3484 return g_strdup_printf ("%d", value);
3488 g_key_file_parse_value_as_double (GKeyFile *key_file,
3492 gchar *end_of_valid_d;
3493 gdouble double_value = 0;
3495 double_value = g_ascii_strtod (value, &end_of_valid_d);
3497 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3499 gchar *value_utf8 = _g_utf8_make_valid (value);
3500 g_set_error (error, G_KEY_FILE_ERROR,
3501 G_KEY_FILE_ERROR_INVALID_VALUE,
3502 _("Value '%s' cannot be interpreted "
3503 "as a float number."),
3505 g_free (value_utf8);
3508 return double_value;
3512 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3520 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3522 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3526 value_utf8 = _g_utf8_make_valid (value);
3527 g_set_error (error, G_KEY_FILE_ERROR,
3528 G_KEY_FILE_ERROR_INVALID_VALUE,
3529 _("Value '%s' cannot be interpreted "
3530 "as a boolean."), value_utf8);
3531 g_free (value_utf8);
3537 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3541 return g_strdup ("true");
3543 return g_strdup ("false");
3547 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3554 string = g_string_sized_new (512);
3556 lines = g_strsplit (value, "\n", 0);
3558 for (i = 0; lines[i] != NULL; i++)
3560 if (lines[i][0] != '#')
3561 g_string_append_printf (string, "%s\n", lines[i]);
3563 g_string_append_printf (string, "%s\n", lines[i] + 1);
3567 return g_string_free (string, FALSE);
3571 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3572 const gchar *comment)
3578 string = g_string_sized_new (512);
3580 lines = g_strsplit (comment, "\n", 0);
3582 for (i = 0; lines[i] != NULL; i++)
3583 g_string_append_printf (string, "#%s%s", lines[i],
3584 lines[i + 1] == NULL? "" : "\n");
3587 return g_string_free (string, FALSE);
3590 #define __G_KEY_FILE_C__
3591 #include "galiasdef.c"