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 gboolean g_key_file_is_group_name (const gchar *name);
140 static gboolean g_key_file_is_key_name (const gchar *name);
141 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
142 static gboolean g_key_file_line_is_comment (const gchar *line);
143 static gboolean g_key_file_line_is_group (const gchar *line);
144 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
145 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
149 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
151 gboolean escape_separator);
152 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
155 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
157 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
160 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
163 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
165 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
167 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
168 const gchar *comment);
169 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
173 static void g_key_file_parse_comment (GKeyFile *key_file,
177 static void g_key_file_parse_group (GKeyFile *key_file,
181 static gchar *key_get_locale (const gchar *key);
182 static void g_key_file_parse_data (GKeyFile *key_file,
186 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
191 g_key_file_error_quark (void)
193 return g_quark_from_static_string ("g-key-file-error-quark");
197 g_key_file_init (GKeyFile *key_file)
199 key_file->current_group = g_slice_new0 (GKeyFileGroup);
200 key_file->groups = g_list_prepend (NULL, key_file->current_group);
201 key_file->start_group = NULL;
202 key_file->parse_buffer = g_string_sized_new (128);
203 key_file->approximate_size = 0;
204 key_file->list_separator = ';';
209 g_key_file_clear (GKeyFile *key_file)
211 GList *tmp, *group_node;
213 if (key_file->parse_buffer)
214 g_string_free (key_file->parse_buffer, TRUE);
216 tmp = key_file->groups;
221 g_key_file_remove_group_node (key_file, group_node);
224 g_assert (key_file->groups == NULL);
231 * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
232 * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
233 * read an existing key file.
235 * Return value: an empty #GKeyFile.
240 g_key_file_new (void)
244 key_file = g_slice_new0 (GKeyFile);
245 g_key_file_init (key_file);
251 * g_key_file_set_list_separator:
252 * @key_file: a #GKeyFile
253 * @separator: the separator
255 * Sets the character which is used to separate
256 * values in lists. Typically ';' or ',' are used
257 * as separators. The default list separator is ';'.
262 g_key_file_set_list_separator (GKeyFile *key_file,
265 g_return_if_fail (key_file != NULL);
267 key_file->list_separator = separator;
271 /* Iterates through all the directories in *dirs trying to
272 * open file. When it successfully locates and opens a file it
273 * returns the file descriptor to the open file. It also
274 * outputs the absolute path of the file in output_file and
275 * leaves the unchecked directories in *dirs.
278 find_file_in_data_dirs (const gchar *file,
283 gchar **data_dirs, *data_dir, *path;
294 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
296 gchar *candidate_file, *sub_dir;
298 candidate_file = (gchar *) file;
299 sub_dir = g_strdup ("");
300 while (candidate_file != NULL && fd < 0)
304 path = g_build_filename (data_dir, sub_dir,
305 candidate_file, NULL);
307 fd = g_open (path, O_RDONLY, 0);
315 candidate_file = strchr (candidate_file, '-');
317 if (candidate_file == NULL)
323 sub_dir = g_strndup (file, candidate_file - file - 1);
325 for (p = sub_dir; *p != '\0'; p++)
328 *p = G_DIR_SEPARATOR;
339 g_set_error (error, G_KEY_FILE_ERROR,
340 G_KEY_FILE_ERROR_NOT_FOUND,
341 _("Valid key file could not be "
342 "found in data dirs"));
345 if (output_file != NULL && fd > 0)
346 *output_file = g_strdup (path);
354 g_key_file_load_from_fd (GKeyFile *key_file,
359 GError *key_file_error = NULL;
361 struct stat stat_buf;
362 gchar read_buf[4096];
364 if (fstat (fd, &stat_buf) < 0)
366 g_set_error (error, G_FILE_ERROR,
367 g_file_error_from_errno (errno),
368 "%s", g_strerror (errno));
372 if (!S_ISREG (stat_buf.st_mode))
374 g_set_error (error, G_KEY_FILE_ERROR,
375 G_KEY_FILE_ERROR_PARSE,
376 _("Not a regular file"));
380 if (stat_buf.st_size == 0)
382 g_set_error (error, G_KEY_FILE_ERROR,
383 G_KEY_FILE_ERROR_PARSE,
388 if (key_file->approximate_size > 0)
390 g_key_file_clear (key_file);
391 g_key_file_init (key_file);
393 key_file->flags = flags;
398 bytes_read = read (fd, read_buf, 4096);
400 if (bytes_read == 0) /* End of File */
405 if (errno == EINTR || errno == EAGAIN)
408 g_set_error (error, G_FILE_ERROR,
409 g_file_error_from_errno (errno),
410 "%s", g_strerror (errno));
414 g_key_file_parse_data (key_file,
415 read_buf, bytes_read,
418 while (!key_file_error);
422 g_propagate_error (error, key_file_error);
426 g_key_file_flush_parse_buffer (key_file, &key_file_error);
430 g_propagate_error (error, key_file_error);
438 * g_key_file_load_from_file:
439 * @key_file: an empty #GKeyFile struct
440 * @file: the path of a filename to load, in the GLib file name encoding
441 * @flags: flags from #GKeyFileFlags
442 * @error: return location for a #GError, or %NULL
444 * Loads a key file into an empty #GKeyFile structure.
445 * If the file could not be loaded then %error is set to
446 * either a #GFileError or #GKeyFileError.
448 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
452 g_key_file_load_from_file (GKeyFile *key_file,
457 GError *key_file_error = NULL;
460 g_return_val_if_fail (key_file != NULL, FALSE);
461 g_return_val_if_fail (file != NULL, FALSE);
463 fd = g_open (file, O_RDONLY, 0);
467 g_set_error (error, G_FILE_ERROR,
468 g_file_error_from_errno (errno),
469 "%s", g_strerror (errno));
473 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
478 g_propagate_error (error, key_file_error);
486 * g_key_file_load_from_data:
487 * @key_file: an empty #GKeyFile struct
488 * @data: key file loaded in memory.
489 * @length: the length of @data in bytes
490 * @flags: flags from #GKeyFileFlags
491 * @error: return location for a #GError, or %NULL
493 * Loads a key file from memory into an empty #GKeyFile structure. If
494 * the object cannot be created then %error is set to a
497 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
501 g_key_file_load_from_data (GKeyFile *key_file,
507 GError *key_file_error = NULL;
509 g_return_val_if_fail (key_file != NULL, FALSE);
510 g_return_val_if_fail (data != NULL, FALSE);
511 g_return_val_if_fail (length != 0, FALSE);
513 if (length == (gsize)-1)
514 length = strlen (data);
516 if (key_file->approximate_size > 0)
518 g_key_file_clear (key_file);
519 g_key_file_init (key_file);
521 key_file->flags = flags;
523 g_key_file_parse_data (key_file, data, length, &key_file_error);
527 g_propagate_error (error, key_file_error);
531 g_key_file_flush_parse_buffer (key_file, &key_file_error);
535 g_propagate_error (error, key_file_error);
543 * g_key_file_load_from_data_dirs:
544 * @key_file: an empty #GKeyFile struct
545 * @file: a relative path to a filename to open and parse
546 * @full_path: return location for a string containing the full path
547 * of the file, or %NULL
548 * @flags: flags from #GKeyFileFlags
549 * @error: return location for a #GError, or %NULL
551 * This function looks for a key file named @file in the paths
552 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
553 * loads the file into @key_file and returns the file's full path in
554 * @full_path. If the file could not be loaded then an %error is
555 * set to either a #GFileError or #GKeyFileError.
557 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
561 g_key_file_load_from_data_dirs (GKeyFile *key_file,
567 GError *key_file_error = NULL;
568 gchar **all_data_dirs, **data_dirs;
569 const gchar * user_data_dir;
570 const gchar * const * system_data_dirs;
576 g_return_val_if_fail (key_file != NULL, FALSE);
577 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
579 user_data_dir = g_get_user_data_dir ();
580 system_data_dirs = g_get_system_data_dirs ();
581 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
584 all_data_dirs[i++] = g_strdup (user_data_dir);
587 while (system_data_dirs[j] != NULL)
588 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
589 all_data_dirs[i] = NULL;
592 data_dirs = all_data_dirs;
594 while (*data_dirs != NULL && !found_file)
596 g_free (output_path);
598 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
604 g_propagate_error (error, key_file_error);
608 found_file = g_key_file_load_from_fd (key_file, fd, flags,
614 g_propagate_error (error, key_file_error);
619 if (found_file && full_path)
620 *full_path = output_path;
622 g_free (output_path);
624 g_strfreev (all_data_dirs);
631 * @key_file: a #GKeyFile
638 g_key_file_free (GKeyFile *key_file)
640 g_return_if_fail (key_file != NULL);
642 g_key_file_clear (key_file);
643 g_slice_free (GKeyFile, key_file);
646 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
647 * true for locales that match those in g_get_language_names().
650 g_key_file_locale_is_interesting (GKeyFile *key_file,
653 const gchar * const * current_locales;
656 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
659 current_locales = g_get_language_names ();
661 for (i = 0; current_locales[i] != NULL; i++)
663 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
671 g_key_file_parse_line (GKeyFile *key_file,
676 GError *parse_error = NULL;
679 g_return_if_fail (key_file != NULL);
680 g_return_if_fail (line != NULL);
682 line_start = (gchar *) line;
683 while (g_ascii_isspace (*line_start))
686 if (g_key_file_line_is_comment (line_start))
687 g_key_file_parse_comment (key_file, line, length, &parse_error);
688 else if (g_key_file_line_is_group (line_start))
689 g_key_file_parse_group (key_file, line_start,
690 length - (line_start - line),
692 else if (g_key_file_line_is_key_value_pair (line_start))
693 g_key_file_parse_key_value_pair (key_file, line_start,
694 length - (line_start - line),
698 gchar *line_utf8 = _g_utf8_make_valid (line);
699 g_set_error (error, G_KEY_FILE_ERROR,
700 G_KEY_FILE_ERROR_PARSE,
701 _("Key file contains line '%s' which is not "
702 "a key-value pair, group, or comment"),
710 g_propagate_error (error, parse_error);
714 g_key_file_parse_comment (GKeyFile *key_file,
719 GKeyFileKeyValuePair *pair;
721 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
724 g_assert (key_file->current_group != NULL);
726 pair = g_slice_new (GKeyFileKeyValuePair);
728 pair->value = g_strndup (line, length);
730 key_file->current_group->key_value_pairs =
731 g_list_prepend (key_file->current_group->key_value_pairs, pair);
735 g_key_file_parse_group (GKeyFile *key_file,
741 const gchar *group_name_start, *group_name_end;
743 /* advance past opening '['
745 group_name_start = line + 1;
746 group_name_end = line + length - 1;
748 while (*group_name_end != ']')
751 group_name = g_strndup (group_name_start,
752 group_name_end - group_name_start);
754 if (!g_key_file_is_group_name (group_name))
756 g_set_error (error, G_KEY_FILE_ERROR,
757 G_KEY_FILE_ERROR_PARSE,
758 _("Invalid group name: %s"), group_name);
763 g_key_file_add_group (key_file, group_name);
768 g_key_file_parse_key_value_pair (GKeyFile *key_file,
773 gchar *key, *value, *key_end, *value_start, *locale;
774 gsize key_len, value_len;
776 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
778 g_set_error (error, G_KEY_FILE_ERROR,
779 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
780 _("Key file does not start with a group"));
784 key_end = value_start = strchr (line, '=');
786 g_assert (key_end != NULL);
791 /* Pull the key name from the line (chomping trailing whitespace)
793 while (g_ascii_isspace (*key_end))
796 key_len = key_end - line + 2;
798 g_assert (key_len <= length);
800 key = g_strndup (line, key_len - 1);
802 if (!g_key_file_is_key_name (key))
804 g_set_error (error, G_KEY_FILE_ERROR,
805 G_KEY_FILE_ERROR_PARSE,
806 _("Invalid key name: %s"), key);
811 /* Pull the value from the line (chugging leading whitespace)
813 while (g_ascii_isspace (*value_start))
816 value_len = line + length - value_start + 1;
818 value = g_strndup (value_start, value_len);
820 g_assert (key_file->start_group != NULL);
822 if (key_file->current_group
823 && key_file->current_group->name
824 && strcmp (key_file->start_group->name,
825 key_file->current_group->name) == 0
826 && strcmp (key, "Encoding") == 0)
828 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
830 gchar *value_utf8 = _g_utf8_make_valid (value);
831 g_set_error (error, G_KEY_FILE_ERROR,
832 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
833 _("Key file contains unsupported "
834 "encoding '%s'"), value_utf8);
843 /* Is this key a translation? If so, is it one that we care about?
845 locale = key_get_locale (key);
847 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
848 g_key_file_add_key (key_file, key_file->current_group, key, value);
856 key_get_locale (const gchar *key)
860 locale = g_strrstr (key, "[");
862 if (locale && strlen (locale) <= 2)
866 locale = g_strndup (locale + 1, strlen (locale) - 2);
872 g_key_file_parse_data (GKeyFile *key_file,
880 g_return_if_fail (key_file != NULL);
881 g_return_if_fail (data != NULL);
885 for (i = 0; i < length; i++)
889 if (i > 0 && data[i - 1] == '\r')
890 g_string_erase (key_file->parse_buffer,
891 key_file->parse_buffer->len - 1,
894 /* When a newline is encountered flush the parse buffer so that the
895 * line can be parsed. Note that completely blank lines won't show
896 * up in the parse buffer, so they get parsed directly.
898 if (key_file->parse_buffer->len > 0)
899 g_key_file_flush_parse_buffer (key_file, &parse_error);
901 g_key_file_parse_comment (key_file, "", 1, &parse_error);
905 g_propagate_error (error, parse_error);
910 g_string_append_c (key_file->parse_buffer, data[i]);
913 key_file->approximate_size += length;
917 g_key_file_flush_parse_buffer (GKeyFile *key_file,
920 GError *file_error = NULL;
922 g_return_if_fail (key_file != NULL);
926 if (key_file->parse_buffer->len > 0)
928 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
929 key_file->parse_buffer->len,
931 g_string_erase (key_file->parse_buffer, 0, -1);
935 g_propagate_error (error, file_error);
942 * g_key_file_to_data:
943 * @key_file: a #GKeyFile
944 * @length: return location for the length of the
945 * returned string, or %NULL
946 * @error: return location for a #GError, or %NULL
948 * This function outputs @key_file as a string.
950 * Return value: a newly allocated string holding
951 * the contents of the #GKeyFile
956 g_key_file_to_data (GKeyFile *key_file,
960 GString *data_string;
961 GList *group_node, *key_file_node;
963 g_return_val_if_fail (key_file != NULL, NULL);
965 data_string = g_string_sized_new (2 * key_file->approximate_size);
967 for (group_node = g_list_last (key_file->groups);
969 group_node = group_node->prev)
971 GKeyFileGroup *group;
973 group = (GKeyFileGroup *) group_node->data;
975 if (group->comment != NULL)
976 g_string_append_printf (data_string, "%s\n", group->comment->value);
977 else if (group_node->next) /* separate groups by at least an empty line */
978 g_string_append_c (data_string, '\n');
980 if (group->name != NULL)
981 g_string_append_printf (data_string, "[%s]\n", group->name);
983 for (key_file_node = g_list_last (group->key_value_pairs);
984 key_file_node != NULL;
985 key_file_node = key_file_node->prev)
987 GKeyFileKeyValuePair *pair;
989 pair = (GKeyFileKeyValuePair *) key_file_node->data;
991 if (pair->key != NULL)
992 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
994 g_string_append_printf (data_string, "%s\n", pair->value);
999 *length = data_string->len;
1001 return g_string_free (data_string, FALSE);
1005 * g_key_file_get_keys:
1006 * @key_file: a #GKeyFile
1007 * @group_name: a group name
1008 * @length: return location for the number of keys returned, or %NULL
1009 * @error: return location for a #GError, or %NULL
1011 * Returns all keys for the group name @group_name. The array of
1012 * returned keys will be %NULL-terminated, so @length may
1013 * optionally be %NULL. In the event that the @group_name cannot
1014 * be found, %NULL is returned and @error is set to
1015 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1017 * Return value: a newly-allocated %NULL-terminated array of
1018 * strings. Use g_strfreev() to free it.
1023 g_key_file_get_keys (GKeyFile *key_file,
1024 const gchar *group_name,
1028 GKeyFileGroup *group;
1033 g_return_val_if_fail (key_file != NULL, NULL);
1034 g_return_val_if_fail (group_name != NULL, NULL);
1036 group = g_key_file_lookup_group (key_file, group_name);
1040 g_set_error (error, G_KEY_FILE_ERROR,
1041 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1042 _("Key file does not have group '%s'"),
1043 group_name ? group_name : "(null)");
1048 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1050 GKeyFileKeyValuePair *pair;
1052 pair = (GKeyFileKeyValuePair *) tmp->data;
1058 keys = g_new (gchar *, num_keys + 1);
1061 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1063 GKeyFileKeyValuePair *pair;
1065 pair = (GKeyFileKeyValuePair *) tmp->data;
1069 keys[i] = g_strdup (pair->key);
1074 keys[num_keys] = NULL;
1083 * g_key_file_get_start_group:
1084 * @key_file: a #GKeyFile
1086 * Returns the name of the start group of the file.
1088 * Return value: The start group of the key file.
1093 g_key_file_get_start_group (GKeyFile *key_file)
1095 g_return_val_if_fail (key_file != NULL, NULL);
1097 if (key_file->start_group)
1098 return g_strdup (key_file->start_group->name);
1104 * g_key_file_get_groups:
1105 * @key_file: a #GKeyFile
1106 * @length: return location for the number of returned groups, or %NULL
1108 * Returns all groups in the key file loaded with @key_file. The
1109 * array of returned groups will be %NULL-terminated, so @length may
1110 * optionally be %NULL.
1112 * Return value: a newly-allocated %NULL-terminated array of strings.
1113 * Use g_strfreev() to free it.
1117 g_key_file_get_groups (GKeyFile *key_file,
1122 gsize i, num_groups;
1124 g_return_val_if_fail (key_file != NULL, NULL);
1126 num_groups = g_list_length (key_file->groups);
1128 g_assert (num_groups > 0);
1130 /* Only need num_groups instead of num_groups + 1
1131 * because the first group of the file (last in the
1132 * list) is always the comment group at the top,
1135 groups = g_new (gchar *, num_groups);
1137 group_node = g_list_last (key_file->groups);
1139 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1142 for (group_node = group_node->prev;
1144 group_node = group_node->prev)
1146 GKeyFileGroup *group;
1148 group = (GKeyFileGroup *) group_node->data;
1150 g_assert (group->name != NULL);
1152 groups[i++] = g_strdup (group->name);
1163 * g_key_file_get_value:
1164 * @key_file: a #GKeyFile
1165 * @group_name: a group name
1167 * @error: return location for a #GError, or %NULL
1169 * Returns the value associated with @key under @group_name.
1171 * In the event the key cannot be found, %NULL is returned and
1172 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1173 * event that the @group_name cannot be found, %NULL is returned
1174 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1176 * Return value: a newly allocated string or %NULL if the specified
1177 * key cannot be found.
1182 g_key_file_get_value (GKeyFile *key_file,
1183 const gchar *group_name,
1187 GKeyFileGroup *group;
1188 GKeyFileKeyValuePair *pair;
1189 gchar *value = NULL;
1191 g_return_val_if_fail (key_file != NULL, NULL);
1192 g_return_val_if_fail (group_name != NULL, NULL);
1193 g_return_val_if_fail (key != NULL, NULL);
1195 group = g_key_file_lookup_group (key_file, group_name);
1199 g_set_error (error, G_KEY_FILE_ERROR,
1200 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1201 _("Key file does not have group '%s'"),
1202 group_name ? group_name : "(null)");
1206 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1209 value = g_strdup (pair->value);
1211 g_set_error (error, G_KEY_FILE_ERROR,
1212 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1213 _("Key file does not have key '%s'"), key);
1219 * g_key_file_set_value:
1220 * @key_file: a #GKeyFile
1221 * @group_name: a group name
1225 * Associates a new value with @key under @group_name. If @key
1226 * cannot be found then it is created. If @group_name cannot be
1227 * found then it is created.
1232 g_key_file_set_value (GKeyFile *key_file,
1233 const gchar *group_name,
1237 GKeyFileGroup *group;
1238 GKeyFileKeyValuePair *pair;
1240 g_return_if_fail (key_file != NULL);
1241 g_return_if_fail (g_key_file_is_group_name (group_name));
1242 g_return_if_fail (g_key_file_is_key_name (key));
1243 g_return_if_fail (value != NULL);
1245 group = g_key_file_lookup_group (key_file, group_name);
1249 g_key_file_add_group (key_file, group_name);
1250 group = (GKeyFileGroup *) key_file->groups->data;
1252 g_key_file_add_key (key_file, group, key, value);
1256 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1259 g_key_file_add_key (key_file, group, key, value);
1262 g_free (pair->value);
1263 pair->value = g_strdup (value);
1269 * g_key_file_get_string:
1270 * @key_file: a #GKeyFile
1271 * @group_name: a group name
1273 * @error: return location for a #GError, or %NULL
1275 * Returns the value associated with @key under @group_name.
1277 * In the event the key cannot be found, %NULL is returned and
1278 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1279 * event that the @group_name cannot be found, %NULL is returned
1280 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1282 * Return value: a newly allocated string or %NULL if the specified
1283 * key cannot be found.
1288 g_key_file_get_string (GKeyFile *key_file,
1289 const gchar *group_name,
1293 gchar *value, *string_value;
1294 GError *key_file_error;
1296 g_return_val_if_fail (key_file != NULL, NULL);
1297 g_return_val_if_fail (group_name != NULL, NULL);
1298 g_return_val_if_fail (key != NULL, NULL);
1300 key_file_error = NULL;
1302 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1306 g_propagate_error (error, key_file_error);
1310 if (!g_utf8_validate (value, -1, NULL))
1312 gchar *value_utf8 = _g_utf8_make_valid (value);
1313 g_set_error (error, G_KEY_FILE_ERROR,
1314 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1315 _("Key file contains key '%s' with value '%s' "
1316 "which is not UTF-8"), key, value_utf8);
1317 g_free (value_utf8);
1323 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1329 if (g_error_matches (key_file_error,
1331 G_KEY_FILE_ERROR_INVALID_VALUE))
1333 g_set_error (error, G_KEY_FILE_ERROR,
1334 G_KEY_FILE_ERROR_INVALID_VALUE,
1335 _("Key file contains key '%s' "
1336 "which has value that cannot be interpreted."),
1338 g_error_free (key_file_error);
1341 g_propagate_error (error, key_file_error);
1344 return string_value;
1348 * g_key_file_set_string:
1349 * @key_file: a #GKeyFile
1350 * @group_name: a group name
1354 * Associates a new string value with @key under @group_name. If
1355 * @key cannot be found then it is created. If @group_name
1356 * cannot be found then it is created.
1361 g_key_file_set_string (GKeyFile *key_file,
1362 const gchar *group_name,
1364 const gchar *string)
1368 g_return_if_fail (key_file != NULL);
1369 g_return_if_fail (string != NULL);
1371 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1372 g_key_file_set_value (key_file, group_name, key, value);
1377 * g_key_file_get_string_list:
1378 * @key_file: a #GKeyFile
1379 * @group_name: a group name
1381 * @length: return location for the number of returned strings, or %NULL
1382 * @error: return location for a #GError, or %NULL
1384 * Returns the values associated with @key under @group_name.
1386 * In the event the key cannot be found, %NULL is returned and
1387 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1388 * event that the @group_name cannot be found, %NULL is returned
1389 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1391 * Return value: a %NULL-terminated string array or %NULL if the specified
1392 * key cannot be found. The array should be freed with g_strfreev().
1397 g_key_file_get_string_list (GKeyFile *key_file,
1398 const gchar *group_name,
1403 GError *key_file_error = NULL;
1404 gchar *value, *string_value, **values;
1406 GSList *p, *pieces = NULL;
1408 g_return_val_if_fail (key_file != NULL, NULL);
1409 g_return_val_if_fail (group_name != NULL, NULL);
1410 g_return_val_if_fail (key != NULL, NULL);
1412 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1416 g_propagate_error (error, key_file_error);
1420 if (!g_utf8_validate (value, -1, NULL))
1422 gchar *value_utf8 = _g_utf8_make_valid (value);
1423 g_set_error (error, G_KEY_FILE_ERROR,
1424 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1425 _("Key file contains key '%s' with value '%s' "
1426 "which is not UTF-8"), key, value_utf8);
1427 g_free (value_utf8);
1433 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1435 g_free (string_value);
1439 if (g_error_matches (key_file_error,
1441 G_KEY_FILE_ERROR_INVALID_VALUE))
1443 g_set_error (error, G_KEY_FILE_ERROR,
1444 G_KEY_FILE_ERROR_INVALID_VALUE,
1445 _("Key file contains key '%s' "
1446 "which has value that cannot be interpreted."),
1448 g_error_free (key_file_error);
1451 g_propagate_error (error, key_file_error);
1454 len = g_slist_length (pieces);
1455 values = g_new (gchar *, len + 1);
1456 for (p = pieces, i = 0; p; p = p->next)
1457 values[i++] = p->data;
1460 g_slist_free (pieces);
1469 * g_key_file_set_string_list:
1470 * @key_file: a #GKeyFile
1471 * @group_name: a group name
1473 * @list: an array of locale string values
1474 * @length: number of locale string values in @list
1476 * Associates a list of string values for @key under @group_name.
1477 * If @key cannot be found then it is created. If @group_name
1478 * cannot be found then it is created.
1483 g_key_file_set_string_list (GKeyFile *key_file,
1484 const gchar *group_name,
1486 const gchar * const list[],
1489 GString *value_list;
1492 g_return_if_fail (key_file != NULL);
1493 g_return_if_fail (list != NULL);
1495 value_list = g_string_sized_new (length * 128);
1496 for (i = 0; i < length && list[i] != NULL; i++)
1500 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1501 g_string_append (value_list, value);
1502 g_string_append_c (value_list, key_file->list_separator);
1507 g_key_file_set_value (key_file, group_name, key, value_list->str);
1508 g_string_free (value_list, TRUE);
1512 * g_key_file_set_locale_string:
1513 * @key_file: a #GKeyFile
1514 * @group_name: a group name
1519 * Associates a string value for @key and @locale under
1520 * @group_name. If the translation for @key cannot be found
1521 * then it is created.
1526 g_key_file_set_locale_string (GKeyFile *key_file,
1527 const gchar *group_name,
1529 const gchar *locale,
1530 const gchar *string)
1532 gchar *full_key, *value;
1534 g_return_if_fail (key_file != NULL);
1535 g_return_if_fail (key != NULL);
1536 g_return_if_fail (locale != NULL);
1537 g_return_if_fail (string != NULL);
1539 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1540 full_key = g_strdup_printf ("%s[%s]", key, locale);
1541 g_key_file_set_value (key_file, group_name, full_key, value);
1546 extern GSList *_g_compute_locale_variants (const gchar *locale);
1549 * g_key_file_get_locale_string:
1550 * @key_file: a #GKeyFile
1551 * @group_name: a group name
1553 * @locale: a locale or %NULL
1554 * @error: return location for a #GError, or %NULL
1556 * Returns the value associated with @key under @group_name
1557 * translated in the given @locale if available. If @locale is
1558 * %NULL then the current locale is assumed.
1560 * If @key cannot be found then %NULL is returned and @error is set to
1561 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1562 * with @key cannot be interpreted or no suitable translation can
1563 * be found then the untranslated value is returned.
1565 * Return value: a newly allocated string or %NULL if the specified
1566 * key cannot be found.
1571 g_key_file_get_locale_string (GKeyFile *key_file,
1572 const gchar *group_name,
1574 const gchar *locale,
1577 gchar *candidate_key, *translated_value;
1578 GError *key_file_error;
1580 gboolean free_languages = FALSE;
1583 g_return_val_if_fail (key_file != NULL, NULL);
1584 g_return_val_if_fail (group_name != NULL, NULL);
1585 g_return_val_if_fail (key != NULL, NULL);
1587 candidate_key = NULL;
1588 translated_value = NULL;
1589 key_file_error = NULL;
1595 list = _g_compute_locale_variants (locale);
1597 languages = g_new (gchar *, g_slist_length (list) + 1);
1598 for (l = list, i = 0; l; l = l->next, i++)
1599 languages[i] = l->data;
1600 languages[i] = NULL;
1602 g_slist_free (list);
1603 free_languages = TRUE;
1607 languages = (gchar **) g_get_language_names ();
1608 free_languages = FALSE;
1611 for (i = 0; languages[i]; i++)
1613 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1615 translated_value = g_key_file_get_string (key_file,
1617 candidate_key, NULL);
1618 g_free (candidate_key);
1620 if (translated_value)
1623 g_free (translated_value);
1624 translated_value = NULL;
1627 /* Fallback to untranslated key
1629 if (!translated_value)
1631 translated_value = g_key_file_get_string (key_file, group_name, key,
1634 if (!translated_value)
1635 g_propagate_error (error, key_file_error);
1639 g_strfreev (languages);
1641 return translated_value;
1645 * g_key_file_get_locale_string_list:
1646 * @key_file: a #GKeyFile
1647 * @group_name: a group name
1650 * @length: return location for the number of returned strings or %NULL
1651 * @error: return location for a #GError or %NULL
1653 * Returns the values associated with @key under @group_name
1654 * translated in the given @locale if available. If @locale is
1655 * %NULL then the current locale is assumed.
1657 * If @key cannot be found then %NULL is returned and @error is set to
1658 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1659 * with @key cannot be interpreted or no suitable translations
1660 * can be found then the untranslated values are returned.
1661 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1663 * Return value: a newly allocated %NULL-terminated string array
1664 * or %NULL if the key isn't found. The string array should be freed
1665 * with g_strfreev().
1670 g_key_file_get_locale_string_list (GKeyFile *key_file,
1671 const gchar *group_name,
1673 const gchar *locale,
1677 GError *key_file_error;
1678 gchar **values, *value;
1680 g_return_val_if_fail (key_file != NULL, NULL);
1681 g_return_val_if_fail (group_name != NULL, NULL);
1682 g_return_val_if_fail (key != NULL, NULL);
1684 key_file_error = NULL;
1686 value = g_key_file_get_locale_string (key_file, group_name,
1691 g_propagate_error (error, key_file_error);
1696 if (value[strlen (value) - 1] == ';')
1697 value[strlen (value) - 1] = '\0';
1699 values = g_strsplit (value, ";", 0);
1704 *length = g_strv_length (values);
1710 * g_key_file_set_locale_string_list:
1711 * @key_file: a #GKeyFile
1712 * @group_name: a group name
1715 * @list: a %NULL-terminated array of locale string values
1716 * @length: the length of @list
1718 * Associates a list of string values for @key and @locale under
1719 * @group_name. If the translation for @key cannot be found then
1725 g_key_file_set_locale_string_list (GKeyFile *key_file,
1726 const gchar *group_name,
1728 const gchar *locale,
1729 const gchar * const list[],
1732 GString *value_list;
1736 g_return_if_fail (key_file != NULL);
1737 g_return_if_fail (key != NULL);
1738 g_return_if_fail (locale != NULL);
1739 g_return_if_fail (length != 0);
1741 value_list = g_string_sized_new (length * 128);
1742 for (i = 0; i < length && list[i] != NULL; i++)
1746 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1748 g_string_append (value_list, value);
1749 g_string_append_c (value_list, ';');
1754 full_key = g_strdup_printf ("%s[%s]", key, locale);
1755 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1757 g_string_free (value_list, TRUE);
1761 * g_key_file_get_boolean:
1762 * @key_file: a #GKeyFile
1763 * @group_name: a group name
1765 * @error: return location for a #GError
1767 * Returns the value associated with @key under @group_name as a
1770 * If @key cannot be found then the return value is undefined and
1771 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1772 * the value associated with @key cannot be interpreted as a boolean
1773 * then the return value is also undefined and @error is set to
1774 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1776 * Return value: the value associated with the key as a boolean
1780 g_key_file_get_boolean (GKeyFile *key_file,
1781 const gchar *group_name,
1785 GError *key_file_error = NULL;
1787 gboolean bool_value;
1789 g_return_val_if_fail (key_file != NULL, FALSE);
1790 g_return_val_if_fail (group_name != NULL, FALSE);
1791 g_return_val_if_fail (key != NULL, FALSE);
1793 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1797 g_propagate_error (error, key_file_error);
1801 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1807 if (g_error_matches (key_file_error,
1809 G_KEY_FILE_ERROR_INVALID_VALUE))
1811 g_set_error (error, G_KEY_FILE_ERROR,
1812 G_KEY_FILE_ERROR_INVALID_VALUE,
1813 _("Key file contains key '%s' "
1814 "which has value that cannot be interpreted."),
1816 g_error_free (key_file_error);
1819 g_propagate_error (error, key_file_error);
1826 * g_key_file_set_boolean:
1827 * @key_file: a #GKeyFile
1828 * @group_name: a group name
1830 * @value: %TRUE or %FALSE
1832 * Associates a new boolean value with @key under @group_name.
1833 * If @key cannot be found then it is created.
1838 g_key_file_set_boolean (GKeyFile *key_file,
1839 const gchar *group_name,
1845 g_return_if_fail (key_file != NULL);
1847 result = g_key_file_parse_boolean_as_value (key_file, value);
1848 g_key_file_set_value (key_file, group_name, key, result);
1853 * g_key_file_get_boolean_list:
1854 * @key_file: a #GKeyFile
1855 * @group_name: a group name
1857 * @length: the number of booleans returned
1858 * @error: return location for a #GError
1860 * Returns the values associated with @key under @group_name as
1861 * booleans. If @group_name is %NULL, the start_group is used.
1863 * If @key cannot be found then the return value is undefined and
1864 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1865 * the values associated with @key cannot be interpreted as booleans
1866 * then the return value is also undefined and @error is set to
1867 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1869 * Return value: the values associated with the key as a boolean
1874 g_key_file_get_boolean_list (GKeyFile *key_file,
1875 const gchar *group_name,
1880 GError *key_file_error;
1882 gboolean *bool_values;
1885 g_return_val_if_fail (key_file != NULL, NULL);
1886 g_return_val_if_fail (group_name != NULL, NULL);
1887 g_return_val_if_fail (key != NULL, NULL);
1889 key_file_error = NULL;
1891 values = g_key_file_get_string_list (key_file, group_name, key,
1892 &num_bools, &key_file_error);
1895 g_propagate_error (error, key_file_error);
1900 bool_values = g_new (gboolean, num_bools);
1902 for (i = 0; i < num_bools; i++)
1904 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1910 g_propagate_error (error, key_file_error);
1911 g_strfreev (values);
1912 g_free (bool_values);
1917 g_strfreev (values);
1920 *length = num_bools;
1926 * g_key_file_set_boolean_list:
1927 * @key_file: a #GKeyFile
1928 * @group_name: a group name
1930 * @list: an array of boolean values
1931 * @length: length of @list
1933 * Associates a list of boolean values with @key under
1934 * @group_name. If @key cannot be found then it is created.
1935 * If @group_name is %NULL, the start_group is used.
1940 g_key_file_set_boolean_list (GKeyFile *key_file,
1941 const gchar *group_name,
1946 GString *value_list;
1949 g_return_if_fail (key_file != NULL);
1950 g_return_if_fail (list != NULL);
1952 value_list = g_string_sized_new (length * 8);
1953 for (i = 0; i < length; i++)
1957 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1959 g_string_append (value_list, value);
1960 g_string_append_c (value_list, key_file->list_separator);
1965 g_key_file_set_value (key_file, group_name, key, value_list->str);
1966 g_string_free (value_list, TRUE);
1970 * g_key_file_get_integer:
1971 * @key_file: a #GKeyFile
1972 * @group_name: a group name
1974 * @error: return location for a #GError
1976 * Returns the value associated with @key under @group_name as an
1977 * integer. If @group_name is %NULL, the start_group is used.
1979 * If @key cannot be found then the return value is undefined and
1980 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1981 * the value associated with @key cannot be interpreted as an integer
1982 * then the return value is also undefined and @error is set to
1983 * #G_KEY_FILE_ERROR_INVALID_VALUE.
1985 * Return value: the value associated with the key as an integer.
1990 g_key_file_get_integer (GKeyFile *key_file,
1991 const gchar *group_name,
1995 GError *key_file_error;
1999 g_return_val_if_fail (key_file != NULL, -1);
2000 g_return_val_if_fail (group_name != NULL, -1);
2001 g_return_val_if_fail (key != NULL, -1);
2003 key_file_error = NULL;
2005 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2009 g_propagate_error (error, key_file_error);
2013 int_value = g_key_file_parse_value_as_integer (key_file, value,
2019 if (g_error_matches (key_file_error,
2021 G_KEY_FILE_ERROR_INVALID_VALUE))
2023 g_set_error (error, G_KEY_FILE_ERROR,
2024 G_KEY_FILE_ERROR_INVALID_VALUE,
2025 _("Key file contains key '%s' in group '%s' "
2026 "which has value that cannot be interpreted."), key,
2028 g_error_free (key_file_error);
2031 g_propagate_error (error, key_file_error);
2038 * g_key_file_set_integer:
2039 * @key_file: a #GKeyFile
2040 * @group_name: a group name
2042 * @value: an integer value
2044 * Associates a new integer value with @key under @group_name.
2045 * If @key cannot be found then it is created.
2050 g_key_file_set_integer (GKeyFile *key_file,
2051 const gchar *group_name,
2057 g_return_if_fail (key_file != NULL);
2059 result = g_key_file_parse_integer_as_value (key_file, value);
2060 g_key_file_set_value (key_file, group_name, key, result);
2065 * g_key_file_get_integer_list:
2066 * @key_file: a #GKeyFile
2067 * @group_name: a group name
2069 * @length: the number of integers returned
2070 * @error: return location for a #GError
2072 * Returns the values associated with @key under @group_name as
2075 * If @key cannot be found then the return value is undefined and
2076 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2077 * the values associated with @key cannot be interpreted as integers
2078 * then the return value is also undefined and @error is set to
2079 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2081 * Return value: the values associated with the key as a integer
2086 g_key_file_get_integer_list (GKeyFile *key_file,
2087 const gchar *group_name,
2092 GError *key_file_error = NULL;
2097 g_return_val_if_fail (key_file != NULL, NULL);
2098 g_return_val_if_fail (group_name != NULL, NULL);
2099 g_return_val_if_fail (key != NULL, NULL);
2101 values = g_key_file_get_string_list (key_file, group_name, key,
2102 &num_ints, &key_file_error);
2105 g_propagate_error (error, key_file_error);
2110 int_values = g_new (gint, num_ints);
2112 for (i = 0; i < num_ints; i++)
2114 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2120 g_propagate_error (error, key_file_error);
2121 g_strfreev (values);
2122 g_free (int_values);
2127 g_strfreev (values);
2136 * g_key_file_set_integer_list:
2137 * @key_file: a #GKeyFile
2138 * @group_name: a group name
2140 * @list: an array of integer values
2141 * @length: number of integer values in @list
2143 * Associates a list of integer values with @key under
2144 * @group_name. If @key cannot be found then it is created.
2149 g_key_file_set_integer_list (GKeyFile *key_file,
2150 const gchar *group_name,
2158 g_return_if_fail (key_file != NULL);
2159 g_return_if_fail (list != NULL);
2161 values = g_string_sized_new (length * 16);
2162 for (i = 0; i < length; i++)
2166 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2168 g_string_append (values, value);
2169 g_string_append_c (values, ';');
2174 g_key_file_set_value (key_file, group_name, key, values->str);
2175 g_string_free (values, TRUE);
2179 * g_key_file_get_double:
2180 * @key_file: a #GKeyFile
2181 * @group_name: a group name
2183 * @error: return location for a #GError
2185 * Returns the value associated with @key under @group_name as a
2186 * double. If @group_name is %NULL, the start_group is used.
2188 * If @key cannot be found then the return value is undefined and
2189 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2190 * the value associated with @key cannot be interpreted as a double
2191 * then the return value is also undefined and @error is set to
2192 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2194 * Return value: the value associated with the key as a double.
2199 g_key_file_get_double (GKeyFile *key_file,
2200 const gchar *group_name,
2204 GError *key_file_error;
2206 gdouble double_value;
2208 g_return_val_if_fail (key_file != NULL, -1);
2209 g_return_val_if_fail (group_name != NULL, -1);
2210 g_return_val_if_fail (key != NULL, -1);
2212 key_file_error = NULL;
2214 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2218 g_propagate_error (error, key_file_error);
2222 double_value = g_key_file_parse_value_as_double (key_file, value,
2228 if (g_error_matches (key_file_error,
2230 G_KEY_FILE_ERROR_INVALID_VALUE))
2232 g_set_error (error, G_KEY_FILE_ERROR,
2233 G_KEY_FILE_ERROR_INVALID_VALUE,
2234 _("Key file contains key '%s' in group '%s' "
2235 "which has value that cannot be interpreted."), key,
2237 g_error_free (key_file_error);
2240 g_propagate_error (error, key_file_error);
2243 return double_value;
2247 * g_key_file_set_double:
2248 * @key_file: a #GKeyFile
2249 * @group_name: a group name
2251 * @value: an double value
2253 * Associates a new double value with @key under @group_name.
2254 * If @key cannot be found then it is created. If @group_name
2255 * is %NULL, the start group is used.
2260 g_key_file_set_double (GKeyFile *key_file,
2261 const gchar *group_name,
2265 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2267 g_return_if_fail (key_file != NULL);
2269 g_ascii_dtostr (result, sizeof (result), value);
2270 g_key_file_set_value (key_file, group_name, key, result);
2274 * g_key_file_get_double_list:
2275 * @key_file: a #GKeyFile
2276 * @group_name: a group name
2278 * @length: the number of doubles returned
2279 * @error: return location for a #GError
2281 * Returns the values associated with @key under @group_name as
2282 * doubles. If @group_name is %NULL, the start group is used.
2284 * If @key cannot be found then the return value is undefined and
2285 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2286 * the values associated with @key cannot be interpreted as doubles
2287 * then the return value is also undefined and @error is set to
2288 * #G_KEY_FILE_ERROR_INVALID_VALUE.
2290 * Return value: the values associated with the key as a double
2295 g_key_file_get_double_list (GKeyFile *key_file,
2296 const gchar *group_name,
2301 GError *key_file_error = NULL;
2303 gdouble *double_values;
2304 gsize i, num_doubles;
2306 g_return_val_if_fail (key_file != NULL, NULL);
2307 g_return_val_if_fail (group_name != NULL, NULL);
2308 g_return_val_if_fail (key != NULL, NULL);
2310 values = g_key_file_get_string_list (key_file, group_name, key,
2311 &num_doubles, &key_file_error);
2314 g_propagate_error (error, key_file_error);
2319 double_values = g_new (gdouble, num_doubles);
2321 for (i = 0; i < num_doubles; i++)
2323 double_values[i] = g_key_file_parse_value_as_double (key_file,
2329 g_propagate_error (error, key_file_error);
2330 g_strfreev (values);
2331 g_free (double_values);
2336 g_strfreev (values);
2339 *length = num_doubles;
2341 return double_values;
2345 * g_key_file_set_double_list:
2346 * @key_file: a #GKeyFile
2347 * @group_name: a group name
2349 * @list: an array of double values
2350 * @length: number of double values in @list
2352 * Associates a list of double values with @key under
2353 * @group_name. If @key cannot be found then it is created.
2354 * If @group_name is %NULL the start group is used.
2359 g_key_file_set_double_list (GKeyFile *key_file,
2360 const gchar *group_name,
2368 g_return_if_fail (key_file != NULL);
2369 g_return_if_fail (list != NULL);
2371 values = g_string_sized_new (length * 16);
2372 for (i = 0; i < length; i++)
2374 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2376 g_ascii_dtostr( result, sizeof (result), list[i] );
2378 g_string_append (values, result);
2379 g_string_append_c (values, ';');
2382 g_key_file_set_value (key_file, group_name, key, values->str);
2383 g_string_free (values, TRUE);
2387 g_key_file_set_key_comment (GKeyFile *key_file,
2388 const gchar *group_name,
2390 const gchar *comment,
2393 GKeyFileGroup *group;
2394 GKeyFileKeyValuePair *pair;
2395 GList *key_node, *comment_node, *tmp;
2397 group = g_key_file_lookup_group (key_file, group_name);
2400 g_set_error (error, G_KEY_FILE_ERROR,
2401 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2402 _("Key file does not have group '%s'"),
2403 group_name ? group_name : "(null)");
2408 /* First find the key the comments are supposed to be
2411 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2413 if (key_node == NULL)
2415 g_set_error (error, G_KEY_FILE_ERROR,
2416 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2417 _("Key file does not have key '%s' in group '%s'"),
2422 /* Then find all the comments already associated with the
2425 tmp = key_node->next;
2428 GKeyFileKeyValuePair *pair;
2430 pair = (GKeyFileKeyValuePair *) tmp->data;
2432 if (pair->key != NULL)
2437 g_key_file_remove_key_value_pair_node (key_file, group,
2441 if (comment == NULL)
2444 /* Now we can add our new comment
2446 pair = g_slice_new (GKeyFileKeyValuePair);
2448 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2450 key_node = g_list_insert (key_node, pair, 1);
2454 g_key_file_set_group_comment (GKeyFile *key_file,
2455 const gchar *group_name,
2456 const gchar *comment,
2459 GKeyFileGroup *group;
2461 g_return_if_fail (g_key_file_is_group_name (group_name));
2463 group = g_key_file_lookup_group (key_file, group_name);
2466 g_set_error (error, G_KEY_FILE_ERROR,
2467 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2468 _("Key file does not have group '%s'"),
2469 group_name ? group_name : "(null)");
2474 /* First remove any existing comment
2478 g_key_file_key_value_pair_free (group->comment);
2479 group->comment = NULL;
2482 if (comment == NULL)
2485 /* Now we can add our new comment
2487 group->comment = g_slice_new (GKeyFileKeyValuePair);
2488 group->comment->key = NULL;
2489 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2493 g_key_file_set_top_comment (GKeyFile *key_file,
2494 const gchar *comment,
2498 GKeyFileGroup *group;
2499 GKeyFileKeyValuePair *pair;
2501 /* The last group in the list should be the top (comments only)
2504 g_assert (key_file->groups != NULL);
2505 group_node = g_list_last (key_file->groups);
2506 group = (GKeyFileGroup *) group_node->data;
2507 g_assert (group->name == NULL);
2509 /* Note all keys must be comments at the top of
2510 * the file, so we can just free it all.
2512 if (group->key_value_pairs != NULL)
2514 g_list_foreach (group->key_value_pairs,
2515 (GFunc) g_key_file_key_value_pair_free,
2517 g_list_free (group->key_value_pairs);
2518 group->key_value_pairs = NULL;
2521 if (comment == NULL)
2524 pair = g_slice_new (GKeyFileKeyValuePair);
2526 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2528 group->key_value_pairs =
2529 g_list_prepend (group->key_value_pairs, pair);
2533 * g_key_file_set_comment:
2534 * @key_file: a #GKeyFile
2535 * @group_name: a group name, or %NULL
2537 * @comment: a comment
2538 * @error: return location for a #GError
2540 * Places a comment above @key from @group_name.
2541 * @group_name. If @key is %NULL then @comment will
2542 * be written above @group_name. If both @key
2543 * and @group_name are NULL, then @comment will
2544 * be written above the first group in the file.
2549 g_key_file_set_comment (GKeyFile *key_file,
2550 const gchar *group_name,
2552 const gchar *comment,
2555 g_return_if_fail (key_file != NULL);
2557 if (group_name != NULL && key != NULL)
2558 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2559 else if (group_name != NULL)
2560 g_key_file_set_group_comment (key_file, group_name, comment, error);
2562 g_key_file_set_top_comment (key_file, comment, error);
2564 if (comment != NULL)
2565 key_file->approximate_size += strlen (comment);
2569 g_key_file_get_key_comment (GKeyFile *key_file,
2570 const gchar *group_name,
2574 GKeyFileGroup *group;
2575 GKeyFileKeyValuePair *pair;
2576 GList *key_node, *tmp;
2580 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2582 group = g_key_file_lookup_group (key_file, group_name);
2585 g_set_error (error, G_KEY_FILE_ERROR,
2586 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2587 _("Key file does not have group '%s'"),
2588 group_name ? group_name : "(null)");
2593 /* First find the key the comments are supposed to be
2596 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2598 if (key_node == NULL)
2600 g_set_error (error, G_KEY_FILE_ERROR,
2601 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2602 _("Key file does not have key '%s' in group '%s'"),
2609 /* Then find all the comments already associated with the
2610 * key and concatentate them.
2612 tmp = key_node->next;
2613 if (!key_node->next)
2616 pair = (GKeyFileKeyValuePair *) tmp->data;
2617 if (pair->key != NULL)
2622 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2624 if (pair->key != NULL)
2630 while (tmp != key_node)
2632 GKeyFileKeyValuePair *pair;
2634 pair = (GKeyFileKeyValuePair *) tmp->data;
2637 string = g_string_sized_new (512);
2639 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2640 g_string_append (string, comment);
2648 comment = string->str;
2649 g_string_free (string, FALSE);
2658 get_group_comment (GKeyFile *key_file,
2659 GKeyFileGroup *group,
2668 tmp = group->key_value_pairs;
2671 GKeyFileKeyValuePair *pair;
2673 pair = (GKeyFileKeyValuePair *) tmp->data;
2675 if (pair->key != NULL)
2681 if (tmp->next == NULL)
2689 GKeyFileKeyValuePair *pair;
2691 pair = (GKeyFileKeyValuePair *) tmp->data;
2694 string = g_string_sized_new (512);
2696 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2697 g_string_append (string, comment);
2704 return g_string_free (string, FALSE);
2710 g_key_file_get_group_comment (GKeyFile *key_file,
2711 const gchar *group_name,
2715 GKeyFileGroup *group;
2717 group_node = g_key_file_lookup_group_node (key_file, group_name);
2720 g_set_error (error, G_KEY_FILE_ERROR,
2721 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2722 _("Key file does not have group '%s'"),
2723 group_name ? group_name : "(null)");
2728 group = (GKeyFileGroup *)group_node->data;
2730 return g_strdup (group->comment->value);
2732 group_node = group_node->next;
2733 group = (GKeyFileGroup *)group_node->data;
2734 return get_group_comment (key_file, group, error);
2738 g_key_file_get_top_comment (GKeyFile *key_file,
2742 GKeyFileGroup *group;
2744 /* The last group in the list should be the top (comments only)
2747 g_assert (key_file->groups != NULL);
2748 group_node = g_list_last (key_file->groups);
2749 group = (GKeyFileGroup *) group_node->data;
2750 g_assert (group->name == NULL);
2752 return get_group_comment (key_file, group, error);
2756 * g_key_file_get_comment:
2757 * @key_file: a #GKeyFile
2758 * @group_name: a group name, or %NULL
2760 * @error: return location for a #GError
2762 * Retrieves a comment above @key from @group_name.
2763 * @group_name. If @key is %NULL then @comment will
2764 * be read from above @group_name. If both @key
2765 * and @group_name are NULL, then @comment will
2766 * be read from above the first group in the file.
2768 * Returns: a comment that should be freed with g_free()
2773 g_key_file_get_comment (GKeyFile *key_file,
2774 const gchar *group_name,
2778 g_return_val_if_fail (key_file != NULL, NULL);
2780 if (group_name != NULL && key != NULL)
2781 return g_key_file_get_key_comment (key_file, group_name, key, error);
2782 else if (group_name != NULL)
2783 return g_key_file_get_group_comment (key_file, group_name, error);
2785 return g_key_file_get_top_comment (key_file, error);
2789 * g_key_file_remove_comment:
2790 * @key_file: a #GKeyFile
2791 * @group_name: a group name, or %NULL
2793 * @error: return location for a #GError
2795 * Removes a comment above @key from @group_name.
2796 * @group_name. If @key is %NULL then @comment will
2797 * be written above @group_name. If both @key
2798 * and @group_name are NULL, then @comment will
2799 * be written above the first group in the file.
2805 g_key_file_remove_comment (GKeyFile *key_file,
2806 const gchar *group_name,
2810 g_return_if_fail (key_file != NULL);
2812 if (group_name != NULL && key != NULL)
2813 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2814 else if (group_name != NULL)
2815 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2817 g_key_file_set_top_comment (key_file, NULL, error);
2821 * g_key_file_has_group:
2822 * @key_file: a #GKeyFile
2823 * @group_name: a group name
2825 * Looks whether the key file has the group @group_name.
2827 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2832 g_key_file_has_group (GKeyFile *key_file,
2833 const gchar *group_name)
2835 g_return_val_if_fail (key_file != NULL, FALSE);
2836 g_return_val_if_fail (group_name != NULL, FALSE);
2838 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2842 * g_key_file_has_key:
2843 * @key_file: a #GKeyFile
2844 * @group_name: a group name
2846 * @error: return location for a #GError
2848 * Looks whether the key file has the key @key in the group
2851 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2857 g_key_file_has_key (GKeyFile *key_file,
2858 const gchar *group_name,
2862 GKeyFileKeyValuePair *pair;
2863 GKeyFileGroup *group;
2865 g_return_val_if_fail (key_file != NULL, FALSE);
2866 g_return_val_if_fail (group_name != NULL, FALSE);
2867 g_return_val_if_fail (key != NULL, FALSE);
2869 group = g_key_file_lookup_group (key_file, group_name);
2873 g_set_error (error, G_KEY_FILE_ERROR,
2874 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2875 _("Key file does not have group '%s'"),
2876 group_name ? group_name : "(null)");
2881 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2883 return pair != NULL;
2887 g_key_file_add_group (GKeyFile *key_file,
2888 const gchar *group_name)
2890 GKeyFileGroup *group;
2892 g_return_if_fail (key_file != NULL);
2893 g_return_if_fail (g_key_file_is_group_name (group_name));
2895 group = g_key_file_lookup_group (key_file, group_name);
2898 key_file->current_group = group;
2902 group = g_slice_new0 (GKeyFileGroup);
2903 group->name = g_strdup (group_name);
2904 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2905 key_file->groups = g_list_prepend (key_file->groups, group);
2906 key_file->approximate_size += strlen (group_name) + 3;
2907 key_file->current_group = group;
2909 if (key_file->start_group == NULL)
2910 key_file->start_group = group;
2914 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2919 g_free (pair->value);
2920 g_slice_free (GKeyFileKeyValuePair, pair);
2924 /* Be careful not to call this function on a node with data in the
2925 * lookup map without removing it from the lookup map, first.
2927 * Some current cases where this warning is not a concern are
2929 * - the node being removed is a comment node
2930 * - the entire lookup map is getting destroyed soon after
2934 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2935 GKeyFileGroup *group,
2939 GKeyFileKeyValuePair *pair;
2941 pair = (GKeyFileKeyValuePair *) pair_node->data;
2943 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2945 if (pair->key != NULL)
2946 key_file->approximate_size -= strlen (pair->key) + 1;
2948 g_assert (pair->value != NULL);
2949 key_file->approximate_size -= strlen (pair->value);
2951 g_key_file_key_value_pair_free (pair);
2953 g_list_free_1 (pair_node);
2957 g_key_file_remove_group_node (GKeyFile *key_file,
2960 GKeyFileGroup *group;
2963 group = (GKeyFileGroup *) group_node->data;
2965 /* If the current group gets deleted make the current group the last
2968 if (key_file->current_group == group)
2970 /* groups should always contain at least the top comment group,
2971 * unless g_key_file_clear has been called
2973 if (key_file->groups)
2974 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2976 key_file->current_group = NULL;
2979 /* If the start group gets deleted make the start group the first
2982 if (key_file->start_group == group)
2984 tmp = g_list_last (key_file->groups);
2987 if (tmp != group_node &&
2988 ((GKeyFileGroup *) tmp->data)->name != NULL)
2995 key_file->start_group = (GKeyFileGroup *) tmp->data;
2997 key_file->start_group = NULL;
3000 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3002 if (group->name != NULL)
3003 key_file->approximate_size -= strlen (group->name) + 3;
3005 tmp = group->key_value_pairs;
3012 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3015 g_assert (group->key_value_pairs == NULL);
3017 if (group->lookup_map)
3019 g_hash_table_destroy (group->lookup_map);
3020 group->lookup_map = NULL;
3023 g_free ((gchar *) group->name);
3024 g_slice_free (GKeyFileGroup, group);
3025 g_list_free_1 (group_node);
3029 * g_key_file_remove_group:
3030 * @key_file: a #GKeyFile
3031 * @group_name: a group name
3032 * @error: return location for a #GError or %NULL
3034 * Removes the specified group, @group_name,
3035 * from the key file.
3040 g_key_file_remove_group (GKeyFile *key_file,
3041 const gchar *group_name,
3046 g_return_if_fail (key_file != NULL);
3047 g_return_if_fail (group_name != NULL);
3049 group_node = g_key_file_lookup_group_node (key_file, group_name);
3053 g_set_error (error, G_KEY_FILE_ERROR,
3054 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3055 _("Key file does not have group '%s'"),
3060 g_key_file_remove_group_node (key_file, group_node);
3064 g_key_file_add_key (GKeyFile *key_file,
3065 GKeyFileGroup *group,
3069 GKeyFileKeyValuePair *pair;
3071 pair = g_slice_new (GKeyFileKeyValuePair);
3072 pair->key = g_strdup (key);
3073 pair->value = g_strdup (value);
3075 g_hash_table_replace (group->lookup_map, pair->key, pair);
3076 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3077 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3081 * g_key_file_remove_key:
3082 * @key_file: a #GKeyFile
3083 * @group_name: a group name
3084 * @key: a key name to remove
3085 * @error: return location for a #GError or %NULL
3087 * Removes @key in @group_name from the key file.
3092 g_key_file_remove_key (GKeyFile *key_file,
3093 const gchar *group_name,
3097 GKeyFileGroup *group;
3098 GKeyFileKeyValuePair *pair;
3100 g_return_if_fail (key_file != NULL);
3101 g_return_if_fail (group_name != NULL);
3102 g_return_if_fail (key != NULL);
3106 group = g_key_file_lookup_group (key_file, group_name);
3109 g_set_error (error, G_KEY_FILE_ERROR,
3110 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3111 _("Key file does not have group '%s'"),
3112 group_name ? group_name : "(null)");
3116 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3120 g_set_error (error, G_KEY_FILE_ERROR,
3121 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3122 _("Key file does not have key '%s' in group '%s'"),
3127 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3129 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3130 g_hash_table_remove (group->lookup_map, pair->key);
3131 g_key_file_key_value_pair_free (pair);
3135 g_key_file_lookup_group_node (GKeyFile *key_file,
3136 const gchar *group_name)
3138 GKeyFileGroup *group;
3141 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3143 group = (GKeyFileGroup *) tmp->data;
3145 if (group && group->name && strcmp (group->name, group_name) == 0)
3152 static GKeyFileGroup *
3153 g_key_file_lookup_group (GKeyFile *key_file,
3154 const gchar *group_name)
3158 group_node = g_key_file_lookup_group_node (key_file, group_name);
3160 if (group_node != NULL)
3161 return (GKeyFileGroup *) group_node->data;
3167 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3168 GKeyFileGroup *group,
3173 for (key_node = group->key_value_pairs;
3175 key_node = key_node->next)
3177 GKeyFileKeyValuePair *pair;
3179 pair = (GKeyFileKeyValuePair *) key_node->data;
3181 if (pair->key && strcmp (pair->key, key) == 0)
3188 static GKeyFileKeyValuePair *
3189 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3190 GKeyFileGroup *group,
3193 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3196 /* Lines starting with # or consisting entirely of whitespace are merely
3197 * recorded, not parsed. This function assumes all leading whitespace
3198 * has been stripped.
3201 g_key_file_line_is_comment (const gchar *line)
3203 return (*line == '#' || *line == '\0' || *line == '\n');
3207 g_key_file_is_group_name (const gchar *name)
3214 p = q = (gchar *) name;
3215 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3216 q = g_utf8_next_char (q);
3218 if (*q != '\0' || q == p)
3225 g_key_file_is_key_name (const gchar *name)
3232 p = q = (gchar *) name;
3233 /* We accept a little more than the desktop entry spec says,
3234 * since gnome-vfs uses mime-types as keys in its cache.
3236 while (*q && *q != '=' && *q != '[' && *q != ']')
3237 q = g_utf8_next_char (q);
3239 /* No empty keys, please */
3243 /* We accept spaces in the middle of keys to not break
3244 * existing apps, but we don't tolerate initial of final
3245 * spaces, which would lead to silent corruption when
3246 * rereading the file.
3248 if (*p == ' ' || q[-1] == ' ')
3254 while (*q && (g_unichar_isalnum (g_utf8_get_char (q)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3255 q = g_utf8_next_char (q);
3269 /* A group in a key file is made up of a starting '[' followed by one
3270 * or more letters making up the group name followed by ']'.
3273 g_key_file_line_is_group (const gchar *line)
3283 while (*p && *p != ']')
3284 p = g_utf8_next_char (p);
3289 /* silently accept whitespace after the ] */
3290 p = g_utf8_next_char (p);
3291 while (*p == ' ' || *p == '\t')
3292 p = g_utf8_next_char (p);
3301 g_key_file_line_is_key_value_pair (const gchar *line)
3305 p = (gchar *) g_utf8_strchr (line, -1, '=');
3310 /* Key must be non-empty
3319 g_key_file_parse_value_as_string (GKeyFile *key_file,
3324 gchar *string_value, *p, *q0, *q;
3326 string_value = g_new (gchar, strlen (value) + 1);
3328 p = (gchar *) value;
3329 q0 = q = string_value;
3359 g_set_error (error, G_KEY_FILE_ERROR,
3360 G_KEY_FILE_ERROR_INVALID_VALUE,
3361 _("Key file contains escape character "
3366 if (pieces && *p == key_file->list_separator)
3367 *q = key_file->list_separator;
3381 g_set_error (error, G_KEY_FILE_ERROR,
3382 G_KEY_FILE_ERROR_INVALID_VALUE,
3383 _("Key file contains invalid escape "
3384 "sequence '%s'"), sequence);
3393 if (pieces && (*p == key_file->list_separator))
3395 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3411 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3412 *pieces = g_slist_reverse (*pieces);
3415 return string_value;
3419 g_key_file_parse_string_as_value (GKeyFile *key_file,
3420 const gchar *string,
3421 gboolean escape_separator)
3423 gchar *value, *p, *q;
3425 gboolean parsing_leading_space;
3427 length = strlen (string) + 1;
3429 /* Worst case would be that every character needs to be escaped.
3430 * In other words every character turns to two characters
3432 value = g_new (gchar, 2 * length);
3434 p = (gchar *) string;
3436 parsing_leading_space = TRUE;
3437 while (p < (string + length - 1))
3439 gchar escaped_character[3] = { '\\', 0, 0 };
3444 if (parsing_leading_space)
3446 escaped_character[1] = 's';
3447 strcpy (q, escaped_character);
3457 if (parsing_leading_space)
3459 escaped_character[1] = 't';
3460 strcpy (q, escaped_character);
3470 escaped_character[1] = 'n';
3471 strcpy (q, escaped_character);
3475 escaped_character[1] = 'r';
3476 strcpy (q, escaped_character);
3480 escaped_character[1] = '\\';
3481 strcpy (q, escaped_character);
3483 parsing_leading_space = FALSE;
3486 if (escape_separator && *p == key_file->list_separator)
3488 escaped_character[1] = key_file->list_separator;
3489 strcpy (q, escaped_character);
3491 parsing_leading_space = TRUE;
3497 parsing_leading_space = FALSE;
3509 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3513 gchar *end_of_valid_int;
3518 long_value = strtol (value, &end_of_valid_int, 10);
3520 if (*value == '\0' || *end_of_valid_int != '\0')
3522 gchar *value_utf8 = _g_utf8_make_valid (value);
3523 g_set_error (error, G_KEY_FILE_ERROR,
3524 G_KEY_FILE_ERROR_INVALID_VALUE,
3525 _("Value '%s' cannot be interpreted "
3526 "as a number."), value_utf8);
3527 g_free (value_utf8);
3532 int_value = long_value;
3533 if (int_value != long_value || errno == ERANGE)
3535 gchar *value_utf8 = _g_utf8_make_valid (value);
3538 G_KEY_FILE_ERROR_INVALID_VALUE,
3539 _("Integer value '%s' out of range"),
3541 g_free (value_utf8);
3550 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3554 return g_strdup_printf ("%d", value);
3558 g_key_file_parse_value_as_double (GKeyFile *key_file,
3562 gchar *end_of_valid_d;
3563 gdouble double_value = 0;
3565 double_value = g_ascii_strtod (value, &end_of_valid_d);
3567 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3569 gchar *value_utf8 = _g_utf8_make_valid (value);
3570 g_set_error (error, G_KEY_FILE_ERROR,
3571 G_KEY_FILE_ERROR_INVALID_VALUE,
3572 _("Value '%s' cannot be interpreted "
3573 "as a float number."),
3575 g_free (value_utf8);
3578 return double_value;
3582 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3588 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3590 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3593 value_utf8 = _g_utf8_make_valid (value);
3594 g_set_error (error, G_KEY_FILE_ERROR,
3595 G_KEY_FILE_ERROR_INVALID_VALUE,
3596 _("Value '%s' cannot be interpreted "
3597 "as a boolean."), value_utf8);
3598 g_free (value_utf8);
3604 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3608 return g_strdup ("true");
3610 return g_strdup ("false");
3614 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3621 string = g_string_sized_new (512);
3623 lines = g_strsplit (value, "\n", 0);
3625 for (i = 0; lines[i] != NULL; i++)
3627 if (lines[i][0] != '#')
3628 g_string_append_printf (string, "%s\n", lines[i]);
3630 g_string_append_printf (string, "%s\n", lines[i] + 1);
3634 return g_string_free (string, FALSE);
3638 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3639 const gchar *comment)
3645 string = g_string_sized_new (512);
3647 lines = g_strsplit (comment, "\n", 0);
3649 for (i = 0; lines[i] != NULL; i++)
3650 g_string_append_printf (string, "#%s%s", lines[i],
3651 lines[i + 1] == NULL? "" : "\n");
3654 return g_string_free (string, FALSE);
3657 #define __G_KEY_FILE_C__
3658 #include "galiasdef.c"