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_new0 (GKeyFileGroup, 1);
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_new0 (GKeyFile, 1);
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_new0 (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++]);
591 data_dirs = all_data_dirs;
593 while (*data_dirs != NULL && !found_file)
595 g_free (output_path);
597 fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
603 g_propagate_error (error, key_file_error);
607 found_file = g_key_file_load_from_fd (key_file, fd, flags,
613 g_propagate_error (error, key_file_error);
618 if (found_file && full_path)
619 *full_path = output_path;
621 g_free (output_path);
623 g_strfreev (all_data_dirs);
630 * @key_file: a #GKeyFile
637 g_key_file_free (GKeyFile *key_file)
639 g_return_if_fail (key_file != NULL);
641 g_key_file_clear (key_file);
645 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
646 * true for locales that match those in g_get_language_names().
649 g_key_file_locale_is_interesting (GKeyFile *key_file,
652 const gchar * const * current_locales;
655 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
658 current_locales = g_get_language_names ();
660 for (i = 0; current_locales[i] != NULL; i++)
662 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
670 g_key_file_parse_line (GKeyFile *key_file,
675 GError *parse_error = NULL;
678 g_return_if_fail (key_file != NULL);
679 g_return_if_fail (line != NULL);
681 line_start = (gchar *) line;
682 while (g_ascii_isspace (*line_start))
685 if (g_key_file_line_is_comment (line_start))
686 g_key_file_parse_comment (key_file, line, length, &parse_error);
687 else if (g_key_file_line_is_group (line_start))
688 g_key_file_parse_group (key_file, line_start,
689 length - (line_start - line),
691 else if (g_key_file_line_is_key_value_pair (line_start))
692 g_key_file_parse_key_value_pair (key_file, line_start,
693 length - (line_start - line),
697 gchar *line_utf8 = _g_utf8_make_valid (line);
698 g_set_error (error, G_KEY_FILE_ERROR,
699 G_KEY_FILE_ERROR_PARSE,
700 _("Key file contains line '%s' which is not "
701 "a key-value pair, group, or comment"),
709 g_propagate_error (error, parse_error);
713 g_key_file_parse_comment (GKeyFile *key_file,
718 GKeyFileKeyValuePair *pair;
720 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
723 g_assert (key_file->current_group != NULL);
725 pair = g_new0 (GKeyFileKeyValuePair, 1);
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_new0 (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_new0 (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_new0 (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_new0 (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_new0 (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_new0 (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_new0 (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_new0 (GKeyFileKeyValuePair, 1);
2449 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2451 key_node = g_list_insert (key_node, pair, 1);
2455 g_key_file_set_group_comment (GKeyFile *key_file,
2456 const gchar *group_name,
2457 const gchar *comment,
2460 GKeyFileGroup *group;
2462 g_return_if_fail (g_key_file_is_group_name (group_name));
2464 group = g_key_file_lookup_group (key_file, group_name);
2467 g_set_error (error, G_KEY_FILE_ERROR,
2468 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2469 _("Key file does not have group '%s'"),
2470 group_name ? group_name : "(null)");
2475 /* First remove any existing comment
2479 g_key_file_key_value_pair_free (group->comment);
2480 group->comment = NULL;
2483 if (comment == NULL)
2486 /* Now we can add our new comment
2488 group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2490 group->comment->key = NULL;
2491 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2495 g_key_file_set_top_comment (GKeyFile *key_file,
2496 const gchar *comment,
2500 GKeyFileGroup *group;
2501 GKeyFileKeyValuePair *pair;
2503 /* The last group in the list should be the top (comments only)
2506 g_assert (key_file->groups != NULL);
2507 group_node = g_list_last (key_file->groups);
2508 group = (GKeyFileGroup *) group_node->data;
2509 g_assert (group->name == NULL);
2511 /* Note all keys must be comments at the top of
2512 * the file, so we can just free it all.
2514 if (group->key_value_pairs != NULL)
2516 g_list_foreach (group->key_value_pairs,
2517 (GFunc) g_key_file_key_value_pair_free,
2519 g_list_free (group->key_value_pairs);
2520 group->key_value_pairs = NULL;
2523 if (comment == NULL)
2526 pair = g_new0 (GKeyFileKeyValuePair, 1);
2529 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2531 group->key_value_pairs =
2532 g_list_prepend (group->key_value_pairs, pair);
2536 * g_key_file_set_comment:
2537 * @key_file: a #GKeyFile
2538 * @group_name: a group name, or %NULL
2540 * @comment: a comment
2541 * @error: return location for a #GError
2543 * Places a comment above @key from @group_name.
2544 * @group_name. If @key is %NULL then @comment will
2545 * be written above @group_name. If both @key
2546 * and @group_name are NULL, then @comment will
2547 * be written above the first group in the file.
2552 g_key_file_set_comment (GKeyFile *key_file,
2553 const gchar *group_name,
2555 const gchar *comment,
2558 g_return_if_fail (key_file != NULL);
2560 if (group_name != NULL && key != NULL)
2561 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2562 else if (group_name != NULL)
2563 g_key_file_set_group_comment (key_file, group_name, comment, error);
2565 g_key_file_set_top_comment (key_file, comment, error);
2567 if (comment != NULL)
2568 key_file->approximate_size += strlen (comment);
2572 g_key_file_get_key_comment (GKeyFile *key_file,
2573 const gchar *group_name,
2577 GKeyFileGroup *group;
2578 GKeyFileKeyValuePair *pair;
2579 GList *key_node, *tmp;
2583 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2585 group = g_key_file_lookup_group (key_file, group_name);
2588 g_set_error (error, G_KEY_FILE_ERROR,
2589 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2590 _("Key file does not have group '%s'"),
2591 group_name ? group_name : "(null)");
2596 /* First find the key the comments are supposed to be
2599 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2601 if (key_node == NULL)
2603 g_set_error (error, G_KEY_FILE_ERROR,
2604 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2605 _("Key file does not have key '%s' in group '%s'"),
2612 /* Then find all the comments already associated with the
2613 * key and concatentate them.
2615 tmp = key_node->next;
2616 if (!key_node->next)
2619 pair = (GKeyFileKeyValuePair *) tmp->data;
2620 if (pair->key != NULL)
2625 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2627 if (pair->key != NULL)
2633 while (tmp != key_node)
2635 GKeyFileKeyValuePair *pair;
2637 pair = (GKeyFileKeyValuePair *) tmp->data;
2640 string = g_string_sized_new (512);
2642 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2643 g_string_append (string, comment);
2651 comment = string->str;
2652 g_string_free (string, FALSE);
2661 get_group_comment (GKeyFile *key_file,
2662 GKeyFileGroup *group,
2671 tmp = group->key_value_pairs;
2674 GKeyFileKeyValuePair *pair;
2676 pair = (GKeyFileKeyValuePair *) tmp->data;
2678 if (pair->key != NULL)
2684 if (tmp->next == NULL)
2692 GKeyFileKeyValuePair *pair;
2694 pair = (GKeyFileKeyValuePair *) tmp->data;
2697 string = g_string_sized_new (512);
2699 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2700 g_string_append (string, comment);
2707 return g_string_free (string, FALSE);
2713 g_key_file_get_group_comment (GKeyFile *key_file,
2714 const gchar *group_name,
2718 GKeyFileGroup *group;
2720 group_node = g_key_file_lookup_group_node (key_file, group_name);
2723 g_set_error (error, G_KEY_FILE_ERROR,
2724 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2725 _("Key file does not have group '%s'"),
2726 group_name ? group_name : "(null)");
2731 group = (GKeyFileGroup *)group_node->data;
2733 return g_strdup (group->comment->value);
2735 group_node = group_node->next;
2736 group = (GKeyFileGroup *)group_node->data;
2737 return get_group_comment (key_file, group, error);
2741 g_key_file_get_top_comment (GKeyFile *key_file,
2745 GKeyFileGroup *group;
2747 /* The last group in the list should be the top (comments only)
2750 g_assert (key_file->groups != NULL);
2751 group_node = g_list_last (key_file->groups);
2752 group = (GKeyFileGroup *) group_node->data;
2753 g_assert (group->name == NULL);
2755 return get_group_comment (key_file, group, error);
2759 * g_key_file_get_comment:
2760 * @key_file: a #GKeyFile
2761 * @group_name: a group name, or %NULL
2763 * @error: return location for a #GError
2765 * Retrieves a comment above @key from @group_name.
2766 * @group_name. If @key is %NULL then @comment will
2767 * be read from above @group_name. If both @key
2768 * and @group_name are NULL, then @comment will
2769 * be read from above the first group in the file.
2771 * Returns: a comment that should be freed with g_free()
2776 g_key_file_get_comment (GKeyFile *key_file,
2777 const gchar *group_name,
2781 g_return_val_if_fail (key_file != NULL, NULL);
2783 if (group_name != NULL && key != NULL)
2784 return g_key_file_get_key_comment (key_file, group_name, key, error);
2785 else if (group_name != NULL)
2786 return g_key_file_get_group_comment (key_file, group_name, error);
2788 return g_key_file_get_top_comment (key_file, error);
2792 * g_key_file_remove_comment:
2793 * @key_file: a #GKeyFile
2794 * @group_name: a group name, or %NULL
2796 * @error: return location for a #GError
2798 * Removes a comment above @key from @group_name.
2799 * @group_name. If @key is %NULL then @comment will
2800 * be written above @group_name. If both @key
2801 * and @group_name are NULL, then @comment will
2802 * be written above the first group in the file.
2808 g_key_file_remove_comment (GKeyFile *key_file,
2809 const gchar *group_name,
2813 g_return_if_fail (key_file != NULL);
2815 if (group_name != NULL && key != NULL)
2816 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2817 else if (group_name != NULL)
2818 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2820 g_key_file_set_top_comment (key_file, NULL, error);
2824 * g_key_file_has_group:
2825 * @key_file: a #GKeyFile
2826 * @group_name: a group name
2828 * Looks whether the key file has the group @group_name.
2830 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2835 g_key_file_has_group (GKeyFile *key_file,
2836 const gchar *group_name)
2838 g_return_val_if_fail (key_file != NULL, FALSE);
2839 g_return_val_if_fail (group_name != NULL, FALSE);
2841 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2845 * g_key_file_has_key:
2846 * @key_file: a #GKeyFile
2847 * @group_name: a group name
2849 * @error: return location for a #GError
2851 * Looks whether the key file has the key @key in the group
2854 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2860 g_key_file_has_key (GKeyFile *key_file,
2861 const gchar *group_name,
2865 GKeyFileKeyValuePair *pair;
2866 GKeyFileGroup *group;
2868 g_return_val_if_fail (key_file != NULL, FALSE);
2869 g_return_val_if_fail (group_name != NULL, FALSE);
2870 g_return_val_if_fail (key != NULL, FALSE);
2872 group = g_key_file_lookup_group (key_file, group_name);
2876 g_set_error (error, G_KEY_FILE_ERROR,
2877 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2878 _("Key file does not have group '%s'"),
2879 group_name ? group_name : "(null)");
2884 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2886 return pair != NULL;
2890 g_key_file_add_group (GKeyFile *key_file,
2891 const gchar *group_name)
2893 GKeyFileGroup *group;
2895 g_return_if_fail (key_file != NULL);
2896 g_return_if_fail (g_key_file_is_group_name (group_name));
2898 group = g_key_file_lookup_group (key_file, group_name);
2901 key_file->current_group = group;
2905 group = g_new0 (GKeyFileGroup, 1);
2906 group->name = g_strdup (group_name);
2907 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2908 key_file->groups = g_list_prepend (key_file->groups, group);
2909 key_file->approximate_size += strlen (group_name) + 3;
2910 key_file->current_group = group;
2912 if (key_file->start_group == NULL)
2913 key_file->start_group = group;
2917 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2922 g_free (pair->value);
2927 /* Be careful not to call this function on a node with data in the
2928 * lookup map without removing it from the lookup map, first.
2930 * Some current cases where this warning is not a concern are
2932 * - the node being removed is a comment node
2933 * - the entire lookup map is getting destroyed soon after
2937 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2938 GKeyFileGroup *group,
2942 GKeyFileKeyValuePair *pair;
2944 pair = (GKeyFileKeyValuePair *) pair_node->data;
2946 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2948 if (pair->key != NULL)
2949 key_file->approximate_size -= strlen (pair->key) + 1;
2951 g_assert (pair->value != NULL);
2952 key_file->approximate_size -= strlen (pair->value);
2954 g_key_file_key_value_pair_free (pair);
2956 g_list_free_1 (pair_node);
2960 g_key_file_remove_group_node (GKeyFile *key_file,
2963 GKeyFileGroup *group;
2966 group = (GKeyFileGroup *) group_node->data;
2968 /* If the current group gets deleted make the current group the last
2971 if (key_file->current_group == group)
2973 /* groups should always contain at least the top comment group,
2974 * unless g_key_file_clear has been called
2976 if (key_file->groups)
2977 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2979 key_file->current_group = NULL;
2982 /* If the start group gets deleted make the start group the first
2985 if (key_file->start_group == group)
2987 tmp = g_list_last (key_file->groups);
2990 if (tmp != group_node &&
2991 ((GKeyFileGroup *) tmp->data)->name != NULL)
2998 key_file->start_group = (GKeyFileGroup *) tmp->data;
3000 key_file->start_group = NULL;
3003 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3005 if (group->name != NULL)
3006 key_file->approximate_size -= strlen (group->name) + 3;
3008 tmp = group->key_value_pairs;
3015 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3018 g_assert (group->key_value_pairs == NULL);
3020 if (group->lookup_map)
3022 g_hash_table_destroy (group->lookup_map);
3023 group->lookup_map = NULL;
3026 g_free ((gchar *) group->name);
3028 g_list_free_1 (group_node);
3032 * g_key_file_remove_group:
3033 * @key_file: a #GKeyFile
3034 * @group_name: a group name
3035 * @error: return location for a #GError or %NULL
3037 * Removes the specified group, @group_name,
3038 * from the key file.
3043 g_key_file_remove_group (GKeyFile *key_file,
3044 const gchar *group_name,
3049 g_return_if_fail (key_file != NULL);
3050 g_return_if_fail (group_name != NULL);
3052 group_node = g_key_file_lookup_group_node (key_file, group_name);
3056 g_set_error (error, G_KEY_FILE_ERROR,
3057 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3058 _("Key file does not have group '%s'"),
3063 g_key_file_remove_group_node (key_file, group_node);
3067 g_key_file_add_key (GKeyFile *key_file,
3068 GKeyFileGroup *group,
3072 GKeyFileKeyValuePair *pair;
3074 pair = g_new0 (GKeyFileKeyValuePair, 1);
3076 pair->key = g_strdup (key);
3077 pair->value = g_strdup (value);
3079 g_hash_table_replace (group->lookup_map, pair->key, pair);
3080 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3081 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3085 * g_key_file_remove_key:
3086 * @key_file: a #GKeyFile
3087 * @group_name: a group name
3088 * @key: a key name to remove
3089 * @error: return location for a #GError or %NULL
3091 * Removes @key in @group_name from the key file.
3096 g_key_file_remove_key (GKeyFile *key_file,
3097 const gchar *group_name,
3101 GKeyFileGroup *group;
3102 GKeyFileKeyValuePair *pair;
3104 g_return_if_fail (key_file != NULL);
3105 g_return_if_fail (group_name != NULL);
3106 g_return_if_fail (key != NULL);
3110 group = g_key_file_lookup_group (key_file, group_name);
3113 g_set_error (error, G_KEY_FILE_ERROR,
3114 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3115 _("Key file does not have group '%s'"),
3116 group_name ? group_name : "(null)");
3120 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3124 g_set_error (error, G_KEY_FILE_ERROR,
3125 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3126 _("Key file does not have key '%s' in group '%s'"),
3131 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3133 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3134 g_hash_table_remove (group->lookup_map, pair->key);
3135 g_key_file_key_value_pair_free (pair);
3139 g_key_file_lookup_group_node (GKeyFile *key_file,
3140 const gchar *group_name)
3142 GKeyFileGroup *group;
3145 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3147 group = (GKeyFileGroup *) tmp->data;
3149 if (group && group->name && strcmp (group->name, group_name) == 0)
3156 static GKeyFileGroup *
3157 g_key_file_lookup_group (GKeyFile *key_file,
3158 const gchar *group_name)
3162 group_node = g_key_file_lookup_group_node (key_file, group_name);
3164 if (group_node != NULL)
3165 return (GKeyFileGroup *) group_node->data;
3171 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3172 GKeyFileGroup *group,
3177 for (key_node = group->key_value_pairs;
3179 key_node = key_node->next)
3181 GKeyFileKeyValuePair *pair;
3183 pair = (GKeyFileKeyValuePair *) key_node->data;
3185 if (pair->key && strcmp (pair->key, key) == 0)
3192 static GKeyFileKeyValuePair *
3193 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3194 GKeyFileGroup *group,
3197 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3200 /* Lines starting with # or consisting entirely of whitespace are merely
3201 * recorded, not parsed. This function assumes all leading whitespace
3202 * has been stripped.
3205 g_key_file_line_is_comment (const gchar *line)
3207 return (*line == '#' || *line == '\0' || *line == '\n');
3211 g_key_file_is_group_name (const gchar *name)
3218 p = q = (gchar *) name;
3219 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3220 q = g_utf8_next_char (q);
3222 if (*q != '\0' || q == p)
3229 g_key_file_is_key_name (const gchar *name)
3236 p = q = (gchar *) name;
3237 /* We accept a little more than the desktop entry spec says,
3238 * since gnome-vfs uses mime-types as keys in its cache.
3240 while (*q && *q != '=' && *q != '[' && *q != ']')
3241 q = g_utf8_next_char (q);
3243 /* No empty keys, please */
3247 /* We accept spaces in the middle of keys to not break
3248 * existing apps, but we don't tolerate initial of final
3249 * spaces, which would lead to silent corruption when
3250 * rereading the file.
3252 if (*p == ' ' || q[-1] == ' ')
3258 while (*q && (g_unichar_isalnum (g_utf8_get_char (q)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3259 q = g_utf8_next_char (q);
3273 /* A group in a key file is made up of a starting '[' followed by one
3274 * or more letters making up the group name followed by ']'.
3277 g_key_file_line_is_group (const gchar *line)
3287 while (*p && *p != ']')
3288 p = g_utf8_next_char (p);
3293 /* silently accept whitespace after the ] */
3294 p = g_utf8_next_char (p);
3295 while (*p == ' ' || *p == '\t')
3296 p = g_utf8_next_char (p);
3305 g_key_file_line_is_key_value_pair (const gchar *line)
3309 p = (gchar *) g_utf8_strchr (line, -1, '=');
3314 /* Key must be non-empty
3323 g_key_file_parse_value_as_string (GKeyFile *key_file,
3328 gchar *string_value, *p, *q0, *q;
3330 string_value = g_new0 (gchar, strlen (value) + 1);
3332 p = (gchar *) value;
3333 q0 = q = string_value;
3363 g_set_error (error, G_KEY_FILE_ERROR,
3364 G_KEY_FILE_ERROR_INVALID_VALUE,
3365 _("Key file contains escape character "
3370 if (pieces && *p == key_file->list_separator)
3371 *q = key_file->list_separator;
3385 g_set_error (error, G_KEY_FILE_ERROR,
3386 G_KEY_FILE_ERROR_INVALID_VALUE,
3387 _("Key file contains invalid escape "
3388 "sequence '%s'"), sequence);
3397 if (pieces && (*p == key_file->list_separator))
3399 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3415 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3416 *pieces = g_slist_reverse (*pieces);
3419 return string_value;
3423 g_key_file_parse_string_as_value (GKeyFile *key_file,
3424 const gchar *string,
3425 gboolean escape_separator)
3427 gchar *value, *p, *q;
3429 gboolean parsing_leading_space;
3431 length = strlen (string) + 1;
3433 /* Worst case would be that every character needs to be escaped.
3434 * In other words every character turns to two characters
3436 value = g_new0 (gchar, 2 * length);
3438 p = (gchar *) string;
3440 parsing_leading_space = TRUE;
3441 while (p < (string + length - 1))
3443 gchar escaped_character[3] = { '\\', 0, 0 };
3448 if (parsing_leading_space)
3450 escaped_character[1] = 's';
3451 strcpy (q, escaped_character);
3461 if (parsing_leading_space)
3463 escaped_character[1] = 't';
3464 strcpy (q, escaped_character);
3474 escaped_character[1] = 'n';
3475 strcpy (q, escaped_character);
3479 escaped_character[1] = 'r';
3480 strcpy (q, escaped_character);
3484 escaped_character[1] = '\\';
3485 strcpy (q, escaped_character);
3487 parsing_leading_space = FALSE;
3490 if (escape_separator && *p == key_file->list_separator)
3492 escaped_character[1] = key_file->list_separator;
3493 strcpy (q, escaped_character);
3495 parsing_leading_space = TRUE;
3501 parsing_leading_space = FALSE;
3513 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3517 gchar *end_of_valid_int;
3522 long_value = strtol (value, &end_of_valid_int, 10);
3524 if (*value == '\0' || *end_of_valid_int != '\0')
3526 gchar *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 number."), value_utf8);
3531 g_free (value_utf8);
3536 int_value = long_value;
3537 if (int_value != long_value || errno == ERANGE)
3539 gchar *value_utf8 = _g_utf8_make_valid (value);
3542 G_KEY_FILE_ERROR_INVALID_VALUE,
3543 _("Integer value '%s' out of range"),
3545 g_free (value_utf8);
3554 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3558 return g_strdup_printf ("%d", value);
3562 g_key_file_parse_value_as_double (GKeyFile *key_file,
3566 gchar *end_of_valid_d;
3567 gdouble double_value = 0;
3569 double_value = g_ascii_strtod (value, &end_of_valid_d);
3571 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3573 gchar *value_utf8 = _g_utf8_make_valid (value);
3574 g_set_error (error, G_KEY_FILE_ERROR,
3575 G_KEY_FILE_ERROR_INVALID_VALUE,
3576 _("Value '%s' cannot be interpreted "
3577 "as a float number."),
3579 g_free (value_utf8);
3582 return double_value;
3586 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3592 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3594 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3597 value_utf8 = _g_utf8_make_valid (value);
3598 g_set_error (error, G_KEY_FILE_ERROR,
3599 G_KEY_FILE_ERROR_INVALID_VALUE,
3600 _("Value '%s' cannot be interpreted "
3601 "as a boolean."), value_utf8);
3602 g_free (value_utf8);
3608 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3612 return g_strdup ("true");
3614 return g_strdup ("false");
3618 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3625 string = g_string_sized_new (512);
3627 lines = g_strsplit (value, "\n", 0);
3629 for (i = 0; lines[i] != NULL; i++)
3631 if (lines[i][0] != '#')
3632 g_string_append_printf (string, "%s\n", lines[i]);
3634 g_string_append_printf (string, "%s\n", lines[i] + 1);
3638 return g_string_free (string, FALSE);
3642 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3643 const gchar *comment)
3649 string = g_string_sized_new (512);
3651 lines = g_strsplit (comment, "\n", 0);
3653 for (i = 0; lines[i] != NULL; i++)
3654 g_string_append_printf (string, "#%s%s", lines[i],
3655 lines[i + 1] == NULL? "" : "\n");
3658 return g_string_free (string, FALSE);
3661 #define __G_KEY_FILE_C__
3662 #include "galiasdef.c"