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 */
92 gboolean has_trailing_blank_line;
94 GList *key_value_pairs;
96 /* Used in parallel with key_value_pairs for
97 * increased lookup performance
99 GHashTable *lookup_map;
102 struct _GKeyFileKeyValuePair
104 gchar *key; /* NULL for comments */
108 static gint find_file_in_data_dirs (const gchar *file,
109 const gchar **data_dirs,
112 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
116 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
117 const gchar *group_name);
118 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
119 const gchar *group_name);
121 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
122 GKeyFileGroup *group,
124 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
125 GKeyFileGroup *group,
128 static void g_key_file_remove_group_node (GKeyFile *key_file,
130 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
131 GKeyFileGroup *group,
134 static void g_key_file_add_key (GKeyFile *key_file,
135 GKeyFileGroup *group,
138 static void g_key_file_add_group (GKeyFile *key_file,
139 const gchar *group_name);
140 static gboolean g_key_file_is_group_name (const gchar *name);
141 static gboolean g_key_file_is_key_name (const gchar *name);
142 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
143 static gboolean g_key_file_line_is_comment (const gchar *line);
144 static gboolean g_key_file_line_is_group (const gchar *line);
145 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
146 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
150 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
152 gboolean escape_separator);
153 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
156 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
158 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
161 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
164 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
166 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
168 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
169 const gchar *comment);
170 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
174 static void g_key_file_parse_comment (GKeyFile *key_file,
178 static void g_key_file_parse_group (GKeyFile *key_file,
182 static gchar *key_get_locale (const gchar *key);
183 static void g_key_file_parse_data (GKeyFile *key_file,
187 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
192 g_key_file_error_quark (void)
194 return g_quark_from_static_string ("g-key-file-error-quark");
198 g_key_file_init (GKeyFile *key_file)
200 key_file->current_group = g_slice_new0 (GKeyFileGroup);
201 key_file->groups = g_list_prepend (NULL, key_file->current_group);
202 key_file->start_group = NULL;
203 key_file->parse_buffer = g_string_sized_new (128);
204 key_file->approximate_size = 0;
205 key_file->list_separator = ';';
210 g_key_file_clear (GKeyFile *key_file)
212 GList *tmp, *group_node;
214 if (key_file->parse_buffer)
215 g_string_free (key_file->parse_buffer, TRUE);
217 tmp = key_file->groups;
222 g_key_file_remove_group_node (key_file, group_node);
225 g_assert (key_file->groups == NULL);
232 * Creates a new empty #GKeyFile object. Use
233 * g_key_file_load_from_file(), g_key_file_load_from_data(),
234 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
235 * read an existing key file.
237 * Return value: an empty #GKeyFile.
242 g_key_file_new (void)
246 key_file = g_slice_new0 (GKeyFile);
247 g_key_file_init (key_file);
253 * g_key_file_set_list_separator:
254 * @key_file: a #GKeyFile
255 * @separator: the separator
257 * Sets the character which is used to separate
258 * values in lists. Typically ';' or ',' are used
259 * as separators. The default list separator is ';'.
264 g_key_file_set_list_separator (GKeyFile *key_file,
267 g_return_if_fail (key_file != NULL);
269 key_file->list_separator = separator;
273 /* Iterates through all the directories in *dirs trying to
274 * open file. When it successfully locates and opens a file it
275 * returns the file descriptor to the open file. It also
276 * outputs the absolute path of the file in output_file.
279 find_file_in_data_dirs (const gchar *file,
284 const gchar **data_dirs, *data_dir;
296 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
298 gchar *candidate_file, *sub_dir;
300 candidate_file = (gchar *) file;
301 sub_dir = g_strdup ("");
302 while (candidate_file != NULL && fd < 0)
306 path = g_build_filename (data_dir, sub_dir,
307 candidate_file, NULL);
309 fd = g_open (path, O_RDONLY, 0);
317 candidate_file = strchr (candidate_file, '-');
319 if (candidate_file == NULL)
325 sub_dir = g_strndup (file, candidate_file - file - 1);
327 for (p = sub_dir; *p != '\0'; p++)
330 *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 search 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_dirs:
544 * @key_file: an empty #GKeyFile struct
545 * @file: a relative path to a filename to open and parse
546 * @search_dirs: %NULL-terminated array of directories to search
547 * @full_path: return location for a string containing the full path
548 * of the file, or %NULL
549 * @flags: flags from #GKeyFileFlags
550 * @error: return location for a #GError, or %NULL
552 * This function looks for a key file named @file in the paths
553 * specified in @search_dirs, loads the file into @key_file and
554 * returns the file's full path in @full_path. If the file could not
555 * be loaded then an %error is set to either a #GFileError or
558 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
563 g_key_file_load_from_dirs (GKeyFile *key_file,
565 const gchar **search_dirs,
570 GError *key_file_error = NULL;
571 const gchar **data_dirs;
576 g_return_val_if_fail (key_file != NULL, FALSE);
577 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
578 g_return_val_if_fail (search_dirs != NULL, FALSE);
581 data_dirs = search_dirs;
583 while (*data_dirs != NULL && !found_file)
585 g_free (output_path);
587 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
593 g_propagate_error (error, key_file_error);
597 found_file = g_key_file_load_from_fd (key_file, fd, flags,
603 g_propagate_error (error, key_file_error);
608 if (found_file && full_path)
609 *full_path = output_path;
611 g_free (output_path);
617 * g_key_file_load_from_data_dirs:
618 * @key_file: an empty #GKeyFile struct
619 * @file: a relative path to a filename to open and parse
620 * @full_path: return location for a string containing the full path
621 * of the file, or %NULL
622 * @flags: flags from #GKeyFileFlags
623 * @error: return location for a #GError, or %NULL
625 * This function looks for a key file named @file in the paths
626 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
627 * loads the file into @key_file and returns the file's full path in
628 * @full_path. If the file could not be loaded then an %error is
629 * set to either a #GFileError or #GKeyFileError.
631 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
635 g_key_file_load_from_data_dirs (GKeyFile *key_file,
641 gchar **all_data_dirs;
642 const gchar * user_data_dir;
643 const gchar * const * system_data_dirs;
647 g_return_val_if_fail (key_file != NULL, FALSE);
648 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
650 user_data_dir = g_get_user_data_dir ();
651 system_data_dirs = g_get_system_data_dirs ();
652 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
655 all_data_dirs[i++] = g_strdup (user_data_dir);
658 while (system_data_dirs[j] != NULL)
659 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
660 all_data_dirs[i] = NULL;
662 found_file = g_key_file_load_from_dirs (key_file,
664 (const gchar **)all_data_dirs,
669 g_strfreev (all_data_dirs);
676 * @key_file: a #GKeyFile
683 g_key_file_free (GKeyFile *key_file)
685 g_return_if_fail (key_file != NULL);
687 g_key_file_clear (key_file);
688 g_slice_free (GKeyFile, key_file);
691 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
692 * true for locales that match those in g_get_language_names().
695 g_key_file_locale_is_interesting (GKeyFile *key_file,
698 const gchar * const * current_locales;
701 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
704 current_locales = g_get_language_names ();
706 for (i = 0; current_locales[i] != NULL; i++)
708 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
716 g_key_file_parse_line (GKeyFile *key_file,
721 GError *parse_error = NULL;
724 g_return_if_fail (key_file != NULL);
725 g_return_if_fail (line != NULL);
727 line_start = (gchar *) line;
728 while (g_ascii_isspace (*line_start))
731 if (g_key_file_line_is_comment (line_start))
732 g_key_file_parse_comment (key_file, line, length, &parse_error);
733 else if (g_key_file_line_is_group (line_start))
734 g_key_file_parse_group (key_file, line_start,
735 length - (line_start - line),
737 else if (g_key_file_line_is_key_value_pair (line_start))
738 g_key_file_parse_key_value_pair (key_file, line_start,
739 length - (line_start - line),
743 gchar *line_utf8 = _g_utf8_make_valid (line);
744 g_set_error (error, G_KEY_FILE_ERROR,
745 G_KEY_FILE_ERROR_PARSE,
746 _("Key file contains line '%s' which is not "
747 "a key-value pair, group, or comment"),
755 g_propagate_error (error, parse_error);
759 g_key_file_parse_comment (GKeyFile *key_file,
764 GKeyFileKeyValuePair *pair;
766 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
769 g_assert (key_file->current_group != NULL);
771 pair = g_slice_new (GKeyFileKeyValuePair);
773 pair->value = g_strndup (line, length);
775 key_file->current_group->key_value_pairs =
776 g_list_prepend (key_file->current_group->key_value_pairs, pair);
778 if (length == 0 || line[0] != '#')
779 key_file->current_group->has_trailing_blank_line = TRUE;
783 g_key_file_parse_group (GKeyFile *key_file,
789 const gchar *group_name_start, *group_name_end;
791 /* advance past opening '['
793 group_name_start = line + 1;
794 group_name_end = line + length - 1;
796 while (*group_name_end != ']')
799 group_name = g_strndup (group_name_start,
800 group_name_end - group_name_start);
802 if (!g_key_file_is_group_name (group_name))
804 g_set_error (error, G_KEY_FILE_ERROR,
805 G_KEY_FILE_ERROR_PARSE,
806 _("Invalid group name: %s"), group_name);
811 g_key_file_add_group (key_file, group_name);
816 g_key_file_parse_key_value_pair (GKeyFile *key_file,
821 gchar *key, *value, *key_end, *value_start, *locale;
822 gsize key_len, value_len;
824 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
826 g_set_error (error, G_KEY_FILE_ERROR,
827 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
828 _("Key file does not start with a group"));
832 key_end = value_start = strchr (line, '=');
834 g_assert (key_end != NULL);
839 /* Pull the key name from the line (chomping trailing whitespace)
841 while (g_ascii_isspace (*key_end))
844 key_len = key_end - line + 2;
846 g_assert (key_len <= length);
848 key = g_strndup (line, key_len - 1);
850 if (!g_key_file_is_key_name (key))
852 g_set_error (error, G_KEY_FILE_ERROR,
853 G_KEY_FILE_ERROR_PARSE,
854 _("Invalid key name: %s"), key);
859 /* Pull the value from the line (chugging leading whitespace)
861 while (g_ascii_isspace (*value_start))
864 value_len = line + length - value_start + 1;
866 value = g_strndup (value_start, value_len);
868 g_assert (key_file->start_group != NULL);
870 if (key_file->current_group
871 && key_file->current_group->name
872 && strcmp (key_file->start_group->name,
873 key_file->current_group->name) == 0
874 && strcmp (key, "Encoding") == 0)
876 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
878 gchar *value_utf8 = _g_utf8_make_valid (value);
879 g_set_error (error, G_KEY_FILE_ERROR,
880 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
881 _("Key file contains unsupported "
882 "encoding '%s'"), value_utf8);
891 /* Is this key a translation? If so, is it one that we care about?
893 locale = key_get_locale (key);
895 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
896 g_key_file_add_key (key_file, key_file->current_group, key, value);
904 key_get_locale (const gchar *key)
908 locale = g_strrstr (key, "[");
910 if (locale && strlen (locale) <= 2)
914 locale = g_strndup (locale + 1, strlen (locale) - 2);
920 g_key_file_parse_data (GKeyFile *key_file,
928 g_return_if_fail (key_file != NULL);
929 g_return_if_fail (data != NULL);
933 for (i = 0; i < length; i++)
937 if (i > 0 && data[i - 1] == '\r')
938 g_string_erase (key_file->parse_buffer,
939 key_file->parse_buffer->len - 1,
942 /* When a newline is encountered flush the parse buffer so that the
943 * line can be parsed. Note that completely blank lines won't show
944 * up in the parse buffer, so they get parsed directly.
946 if (key_file->parse_buffer->len > 0)
947 g_key_file_flush_parse_buffer (key_file, &parse_error);
949 g_key_file_parse_comment (key_file, "", 1, &parse_error);
953 g_propagate_error (error, parse_error);
958 g_string_append_c (key_file->parse_buffer, data[i]);
961 key_file->approximate_size += length;
965 g_key_file_flush_parse_buffer (GKeyFile *key_file,
968 GError *file_error = NULL;
970 g_return_if_fail (key_file != NULL);
974 if (key_file->parse_buffer->len > 0)
976 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
977 key_file->parse_buffer->len,
979 g_string_erase (key_file->parse_buffer, 0, -1);
983 g_propagate_error (error, file_error);
990 * g_key_file_to_data:
991 * @key_file: a #GKeyFile
992 * @length: return location for the length of the
993 * returned string, or %NULL
994 * @error: return location for a #GError, or %NULL
996 * This function outputs @key_file as a string.
998 * Return value: a newly allocated string holding
999 * the contents of the #GKeyFile
1004 g_key_file_to_data (GKeyFile *key_file,
1008 GString *data_string;
1009 GList *group_node, *key_file_node;
1010 gboolean has_blank_line = TRUE;
1012 g_return_val_if_fail (key_file != NULL, NULL);
1014 data_string = g_string_sized_new (2 * key_file->approximate_size);
1016 for (group_node = g_list_last (key_file->groups);
1018 group_node = group_node->prev)
1020 GKeyFileGroup *group;
1022 group = (GKeyFileGroup *) group_node->data;
1024 /* separate groups by at least an empty line */
1025 if (!has_blank_line)
1026 g_string_append_c (data_string, '\n');
1027 has_blank_line = group->has_trailing_blank_line;
1029 if (group->comment != NULL)
1030 g_string_append_printf (data_string, "%s\n", group->comment->value);
1032 if (group->name != NULL)
1033 g_string_append_printf (data_string, "[%s]\n", group->name);
1035 for (key_file_node = g_list_last (group->key_value_pairs);
1036 key_file_node != NULL;
1037 key_file_node = key_file_node->prev)
1039 GKeyFileKeyValuePair *pair;
1041 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1043 if (pair->key != NULL)
1044 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1046 g_string_append_printf (data_string, "%s\n", pair->value);
1051 *length = data_string->len;
1053 return g_string_free (data_string, FALSE);
1057 * g_key_file_get_keys:
1058 * @key_file: a #GKeyFile
1059 * @group_name: a group name
1060 * @length: return location for the number of keys returned, or %NULL
1061 * @error: return location for a #GError, or %NULL
1063 * Returns all keys for the group name @group_name. The array of
1064 * returned keys will be %NULL-terminated, so @length may
1065 * optionally be %NULL. In the event that the @group_name cannot
1066 * be found, %NULL is returned and @error is set to
1067 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1069 * Return value: a newly-allocated %NULL-terminated array of
1070 * strings. Use g_strfreev() to free it.
1075 g_key_file_get_keys (GKeyFile *key_file,
1076 const gchar *group_name,
1080 GKeyFileGroup *group;
1085 g_return_val_if_fail (key_file != NULL, NULL);
1086 g_return_val_if_fail (group_name != NULL, NULL);
1088 group = g_key_file_lookup_group (key_file, group_name);
1092 g_set_error (error, G_KEY_FILE_ERROR,
1093 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1094 _("Key file does not have group '%s'"),
1095 group_name ? group_name : "(null)");
1100 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1102 GKeyFileKeyValuePair *pair;
1104 pair = (GKeyFileKeyValuePair *) tmp->data;
1110 keys = g_new (gchar *, num_keys + 1);
1113 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1115 GKeyFileKeyValuePair *pair;
1117 pair = (GKeyFileKeyValuePair *) tmp->data;
1121 keys[i] = g_strdup (pair->key);
1126 keys[num_keys] = NULL;
1135 * g_key_file_get_start_group:
1136 * @key_file: a #GKeyFile
1138 * Returns the name of the start group of the file.
1140 * Return value: The start group of the key file.
1145 g_key_file_get_start_group (GKeyFile *key_file)
1147 g_return_val_if_fail (key_file != NULL, NULL);
1149 if (key_file->start_group)
1150 return g_strdup (key_file->start_group->name);
1156 * g_key_file_get_groups:
1157 * @key_file: a #GKeyFile
1158 * @length: return location for the number of returned groups, or %NULL
1160 * Returns all groups in the key file loaded with @key_file. The
1161 * array of returned groups will be %NULL-terminated, so @length may
1162 * optionally be %NULL.
1164 * Return value: a newly-allocated %NULL-terminated array of strings.
1165 * Use g_strfreev() to free it.
1169 g_key_file_get_groups (GKeyFile *key_file,
1174 gsize i, num_groups;
1176 g_return_val_if_fail (key_file != NULL, NULL);
1178 num_groups = g_list_length (key_file->groups);
1180 g_assert (num_groups > 0);
1182 /* Only need num_groups instead of num_groups + 1
1183 * because the first group of the file (last in the
1184 * list) is always the comment group at the top,
1187 groups = g_new (gchar *, num_groups);
1189 group_node = g_list_last (key_file->groups);
1191 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1194 for (group_node = group_node->prev;
1196 group_node = group_node->prev)
1198 GKeyFileGroup *group;
1200 group = (GKeyFileGroup *) group_node->data;
1202 g_assert (group->name != NULL);
1204 groups[i++] = g_strdup (group->name);
1215 * g_key_file_get_value:
1216 * @key_file: a #GKeyFile
1217 * @group_name: a group name
1219 * @error: return location for a #GError, or %NULL
1221 * Returns the value associated with @key under @group_name.
1223 * In the event the key cannot be found, %NULL is returned and
1224 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1225 * event that the @group_name cannot be found, %NULL is returned
1226 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1228 * Return value: a newly allocated string or %NULL if the specified
1229 * key cannot be found.
1234 g_key_file_get_value (GKeyFile *key_file,
1235 const gchar *group_name,
1239 GKeyFileGroup *group;
1240 GKeyFileKeyValuePair *pair;
1241 gchar *value = NULL;
1243 g_return_val_if_fail (key_file != NULL, NULL);
1244 g_return_val_if_fail (group_name != NULL, NULL);
1245 g_return_val_if_fail (key != NULL, NULL);
1247 group = g_key_file_lookup_group (key_file, group_name);
1251 g_set_error (error, G_KEY_FILE_ERROR,
1252 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1253 _("Key file does not have group '%s'"),
1254 group_name ? group_name : "(null)");
1258 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1261 value = g_strdup (pair->value);
1263 g_set_error (error, G_KEY_FILE_ERROR,
1264 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1265 _("Key file does not have key '%s'"), key);
1271 * g_key_file_set_value:
1272 * @key_file: a #GKeyFile
1273 * @group_name: a group name
1277 * Associates a new value with @key under @group_name. If @key
1278 * cannot be found then it is created. If @group_name cannot be
1279 * found then it is created.
1284 g_key_file_set_value (GKeyFile *key_file,
1285 const gchar *group_name,
1289 GKeyFileGroup *group;
1290 GKeyFileKeyValuePair *pair;
1292 g_return_if_fail (key_file != NULL);
1293 g_return_if_fail (g_key_file_is_group_name (group_name));
1294 g_return_if_fail (g_key_file_is_key_name (key));
1295 g_return_if_fail (value != NULL);
1297 group = g_key_file_lookup_group (key_file, group_name);
1301 g_key_file_add_group (key_file, group_name);
1302 group = (GKeyFileGroup *) key_file->groups->data;
1304 g_key_file_add_key (key_file, group, key, value);
1308 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1311 g_key_file_add_key (key_file, group, key, value);
1314 g_free (pair->value);
1315 pair->value = g_strdup (value);
1321 * g_key_file_get_string:
1322 * @key_file: a #GKeyFile
1323 * @group_name: a group name
1325 * @error: return location for a #GError, or %NULL
1327 * Returns the value associated with @key under @group_name.
1329 * In the event the key cannot be found, %NULL is returned and
1330 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1331 * event that the @group_name cannot be found, %NULL is returned
1332 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1334 * Return value: a newly allocated string or %NULL if the specified
1335 * key cannot be found.
1340 g_key_file_get_string (GKeyFile *key_file,
1341 const gchar *group_name,
1345 gchar *value, *string_value;
1346 GError *key_file_error;
1348 g_return_val_if_fail (key_file != NULL, NULL);
1349 g_return_val_if_fail (group_name != NULL, NULL);
1350 g_return_val_if_fail (key != NULL, NULL);
1352 key_file_error = NULL;
1354 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1358 g_propagate_error (error, key_file_error);
1362 if (!g_utf8_validate (value, -1, NULL))
1364 gchar *value_utf8 = _g_utf8_make_valid (value);
1365 g_set_error (error, G_KEY_FILE_ERROR,
1366 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1367 _("Key file contains key '%s' with value '%s' "
1368 "which is not UTF-8"), key, value_utf8);
1369 g_free (value_utf8);
1375 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1381 if (g_error_matches (key_file_error,
1383 G_KEY_FILE_ERROR_INVALID_VALUE))
1385 g_set_error (error, G_KEY_FILE_ERROR,
1386 G_KEY_FILE_ERROR_INVALID_VALUE,
1387 _("Key file contains key '%s' "
1388 "which has value that cannot be interpreted."),
1390 g_error_free (key_file_error);
1393 g_propagate_error (error, key_file_error);
1396 return string_value;
1400 * g_key_file_set_string:
1401 * @key_file: a #GKeyFile
1402 * @group_name: a group name
1406 * Associates a new string value with @key under @group_name. If
1407 * @key cannot be found then it is created. If @group_name
1408 * cannot be found then it is created.
1413 g_key_file_set_string (GKeyFile *key_file,
1414 const gchar *group_name,
1416 const gchar *string)
1420 g_return_if_fail (key_file != NULL);
1421 g_return_if_fail (string != NULL);
1423 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1424 g_key_file_set_value (key_file, group_name, key, value);
1429 * g_key_file_get_string_list:
1430 * @key_file: a #GKeyFile
1431 * @group_name: a group name
1433 * @length: return location for the number of returned strings, or %NULL
1434 * @error: return location for a #GError, or %NULL
1436 * Returns the values associated with @key under @group_name.
1438 * In the event the key cannot be found, %NULL is returned and
1439 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1440 * event that the @group_name cannot be found, %NULL is returned
1441 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1443 * Return value: a %NULL-terminated string array or %NULL if the specified
1444 * key cannot be found. The array should be freed with g_strfreev().
1449 g_key_file_get_string_list (GKeyFile *key_file,
1450 const gchar *group_name,
1455 GError *key_file_error = NULL;
1456 gchar *value, *string_value, **values;
1458 GSList *p, *pieces = NULL;
1460 g_return_val_if_fail (key_file != NULL, NULL);
1461 g_return_val_if_fail (group_name != NULL, NULL);
1462 g_return_val_if_fail (key != NULL, NULL);
1464 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1468 g_propagate_error (error, key_file_error);
1472 if (!g_utf8_validate (value, -1, NULL))
1474 gchar *value_utf8 = _g_utf8_make_valid (value);
1475 g_set_error (error, G_KEY_FILE_ERROR,
1476 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1477 _("Key file contains key '%s' with value '%s' "
1478 "which is not UTF-8"), key, value_utf8);
1479 g_free (value_utf8);
1485 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1487 g_free (string_value);
1491 if (g_error_matches (key_file_error,
1493 G_KEY_FILE_ERROR_INVALID_VALUE))
1495 g_set_error (error, G_KEY_FILE_ERROR,
1496 G_KEY_FILE_ERROR_INVALID_VALUE,
1497 _("Key file contains key '%s' "
1498 "which has value that cannot be interpreted."),
1500 g_error_free (key_file_error);
1503 g_propagate_error (error, key_file_error);
1506 len = g_slist_length (pieces);
1507 values = g_new (gchar *, len + 1);
1508 for (p = pieces, i = 0; p; p = p->next)
1509 values[i++] = p->data;
1512 g_slist_free (pieces);
1521 * g_key_file_set_string_list:
1522 * @key_file: a #GKeyFile
1523 * @group_name: a group name
1525 * @list: an array of locale string values
1526 * @length: number of locale string values in @list
1528 * Associates a list of string values for @key under @group_name.
1529 * If @key cannot be found then it is created. If @group_name
1530 * cannot be found then it is created.
1535 g_key_file_set_string_list (GKeyFile *key_file,
1536 const gchar *group_name,
1538 const gchar * const list[],
1541 GString *value_list;
1544 g_return_if_fail (key_file != NULL);
1545 g_return_if_fail (list != NULL);
1547 value_list = g_string_sized_new (length * 128);
1548 for (i = 0; i < length && list[i] != NULL; i++)
1552 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1553 g_string_append (value_list, value);
1554 g_string_append_c (value_list, key_file->list_separator);
1559 g_key_file_set_value (key_file, group_name, key, value_list->str);
1560 g_string_free (value_list, TRUE);
1564 * g_key_file_set_locale_string:
1565 * @key_file: a #GKeyFile
1566 * @group_name: a group name
1571 * Associates a string value for @key and @locale under
1572 * @group_name. If the translation for @key cannot be found
1573 * then it is created.
1578 g_key_file_set_locale_string (GKeyFile *key_file,
1579 const gchar *group_name,
1581 const gchar *locale,
1582 const gchar *string)
1584 gchar *full_key, *value;
1586 g_return_if_fail (key_file != NULL);
1587 g_return_if_fail (key != NULL);
1588 g_return_if_fail (locale != NULL);
1589 g_return_if_fail (string != NULL);
1591 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1592 full_key = g_strdup_printf ("%s[%s]", key, locale);
1593 g_key_file_set_value (key_file, group_name, full_key, value);
1598 extern GSList *_g_compute_locale_variants (const gchar *locale);
1601 * g_key_file_get_locale_string:
1602 * @key_file: a #GKeyFile
1603 * @group_name: a group name
1605 * @locale: a locale or %NULL
1606 * @error: return location for a #GError, or %NULL
1608 * Returns the value associated with @key under @group_name
1609 * translated in the given @locale if available. If @locale is
1610 * %NULL then the current locale is assumed.
1612 * If @key cannot be found then %NULL is returned and @error is set to
1613 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1614 * with @key cannot be interpreted or no suitable translation can
1615 * be found then the untranslated value is returned.
1617 * Return value: a newly allocated string or %NULL if the specified
1618 * key cannot be found.
1623 g_key_file_get_locale_string (GKeyFile *key_file,
1624 const gchar *group_name,
1626 const gchar *locale,
1629 gchar *candidate_key, *translated_value;
1630 GError *key_file_error;
1632 gboolean free_languages = FALSE;
1635 g_return_val_if_fail (key_file != NULL, NULL);
1636 g_return_val_if_fail (group_name != NULL, NULL);
1637 g_return_val_if_fail (key != NULL, NULL);
1639 candidate_key = NULL;
1640 translated_value = NULL;
1641 key_file_error = NULL;
1647 list = _g_compute_locale_variants (locale);
1649 languages = g_new (gchar *, g_slist_length (list) + 1);
1650 for (l = list, i = 0; l; l = l->next, i++)
1651 languages[i] = l->data;
1652 languages[i] = NULL;
1654 g_slist_free (list);
1655 free_languages = TRUE;
1659 languages = (gchar **) g_get_language_names ();
1660 free_languages = FALSE;
1663 for (i = 0; languages[i]; i++)
1665 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1667 translated_value = g_key_file_get_string (key_file,
1669 candidate_key, NULL);
1670 g_free (candidate_key);
1672 if (translated_value)
1675 g_free (translated_value);
1676 translated_value = NULL;
1679 /* Fallback to untranslated key
1681 if (!translated_value)
1683 translated_value = g_key_file_get_string (key_file, group_name, key,
1686 if (!translated_value)
1687 g_propagate_error (error, key_file_error);
1691 g_strfreev (languages);
1693 return translated_value;
1697 * g_key_file_get_locale_string_list:
1698 * @key_file: a #GKeyFile
1699 * @group_name: a group name
1702 * @length: return location for the number of returned strings or %NULL
1703 * @error: return location for a #GError or %NULL
1705 * Returns the values associated with @key under @group_name
1706 * translated in the given @locale if available. If @locale is
1707 * %NULL then the current locale is assumed.
1709 * If @key cannot be found then %NULL is returned and @error is set to
1710 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1711 * with @key cannot be interpreted or no suitable translations
1712 * can be found then the untranslated values are returned.
1713 * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1715 * Return value: a newly allocated %NULL-terminated string array
1716 * or %NULL if the key isn't found. The string array should be freed
1717 * with g_strfreev().
1722 g_key_file_get_locale_string_list (GKeyFile *key_file,
1723 const gchar *group_name,
1725 const gchar *locale,
1729 GError *key_file_error;
1730 gchar **values, *value;
1732 g_return_val_if_fail (key_file != NULL, NULL);
1733 g_return_val_if_fail (group_name != NULL, NULL);
1734 g_return_val_if_fail (key != NULL, NULL);
1736 key_file_error = NULL;
1738 value = g_key_file_get_locale_string (key_file, group_name,
1743 g_propagate_error (error, key_file_error);
1748 if (value[strlen (value) - 1] == ';')
1749 value[strlen (value) - 1] = '\0';
1751 values = g_strsplit (value, ";", 0);
1756 *length = g_strv_length (values);
1762 * g_key_file_set_locale_string_list:
1763 * @key_file: a #GKeyFile
1764 * @group_name: a group name
1767 * @list: a %NULL-terminated array of locale string values
1768 * @length: the length of @list
1770 * Associates a list of string values for @key and @locale under
1771 * @group_name. If the translation for @key cannot be found then
1777 g_key_file_set_locale_string_list (GKeyFile *key_file,
1778 const gchar *group_name,
1780 const gchar *locale,
1781 const gchar * const list[],
1784 GString *value_list;
1788 g_return_if_fail (key_file != NULL);
1789 g_return_if_fail (key != NULL);
1790 g_return_if_fail (locale != NULL);
1791 g_return_if_fail (length != 0);
1793 value_list = g_string_sized_new (length * 128);
1794 for (i = 0; i < length && list[i] != NULL; i++)
1798 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1800 g_string_append (value_list, value);
1801 g_string_append_c (value_list, ';');
1806 full_key = g_strdup_printf ("%s[%s]", key, locale);
1807 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1809 g_string_free (value_list, TRUE);
1813 * g_key_file_get_boolean:
1814 * @key_file: a #GKeyFile
1815 * @group_name: a group name
1817 * @error: return location for a #GError
1819 * Returns the value associated with @key under @group_name as a
1822 * If @key cannot be found then %FALSE is returned and @error is set
1823 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1824 * associated with @key cannot be interpreted as a boolean then %FALSE
1825 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1827 * Return value: the value associated with the key as a boolean, or
1828 * %FALSE if the key was not found or could not be parsed.
1833 g_key_file_get_boolean (GKeyFile *key_file,
1834 const gchar *group_name,
1838 GError *key_file_error = NULL;
1840 gboolean bool_value;
1842 g_return_val_if_fail (key_file != NULL, FALSE);
1843 g_return_val_if_fail (group_name != NULL, FALSE);
1844 g_return_val_if_fail (key != NULL, FALSE);
1846 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1850 g_propagate_error (error, key_file_error);
1854 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1860 if (g_error_matches (key_file_error,
1862 G_KEY_FILE_ERROR_INVALID_VALUE))
1864 g_set_error (error, G_KEY_FILE_ERROR,
1865 G_KEY_FILE_ERROR_INVALID_VALUE,
1866 _("Key file contains key '%s' "
1867 "which has value that cannot be interpreted."),
1869 g_error_free (key_file_error);
1872 g_propagate_error (error, key_file_error);
1879 * g_key_file_set_boolean:
1880 * @key_file: a #GKeyFile
1881 * @group_name: a group name
1883 * @value: %TRUE or %FALSE
1885 * Associates a new boolean value with @key under @group_name.
1886 * If @key cannot be found then it is created.
1891 g_key_file_set_boolean (GKeyFile *key_file,
1892 const gchar *group_name,
1898 g_return_if_fail (key_file != NULL);
1900 result = g_key_file_parse_boolean_as_value (key_file, value);
1901 g_key_file_set_value (key_file, group_name, key, result);
1906 * g_key_file_get_boolean_list:
1907 * @key_file: a #GKeyFile
1908 * @group_name: a group name
1910 * @length: the number of booleans returned
1911 * @error: return location for a #GError
1913 * Returns the values associated with @key under @group_name as
1914 * booleans. If @group_name is %NULL, the start_group is used.
1916 * If @key cannot be found then %NULL is returned and @error is set to
1917 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1918 * with @key cannot be interpreted as booleans then %NULL is returned
1919 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1921 * Return value: the values associated with the key as a list of
1922 * booleans, or %NULL if the key was not found or could not be parsed.
1927 g_key_file_get_boolean_list (GKeyFile *key_file,
1928 const gchar *group_name,
1933 GError *key_file_error;
1935 gboolean *bool_values;
1938 g_return_val_if_fail (key_file != NULL, NULL);
1939 g_return_val_if_fail (group_name != NULL, NULL);
1940 g_return_val_if_fail (key != NULL, NULL);
1942 key_file_error = NULL;
1944 values = g_key_file_get_string_list (key_file, group_name, key,
1945 &num_bools, &key_file_error);
1948 g_propagate_error (error, key_file_error);
1953 bool_values = g_new (gboolean, num_bools);
1955 for (i = 0; i < num_bools; i++)
1957 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1963 g_propagate_error (error, key_file_error);
1964 g_strfreev (values);
1965 g_free (bool_values);
1970 g_strfreev (values);
1973 *length = num_bools;
1979 * g_key_file_set_boolean_list:
1980 * @key_file: a #GKeyFile
1981 * @group_name: a group name
1983 * @list: an array of boolean values
1984 * @length: length of @list
1986 * Associates a list of boolean values with @key under
1987 * @group_name. If @key cannot be found then it is created.
1988 * If @group_name is %NULL, the start_group is used.
1993 g_key_file_set_boolean_list (GKeyFile *key_file,
1994 const gchar *group_name,
1999 GString *value_list;
2002 g_return_if_fail (key_file != NULL);
2003 g_return_if_fail (list != NULL);
2005 value_list = g_string_sized_new (length * 8);
2006 for (i = 0; i < length; i++)
2010 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2012 g_string_append (value_list, value);
2013 g_string_append_c (value_list, key_file->list_separator);
2018 g_key_file_set_value (key_file, group_name, key, value_list->str);
2019 g_string_free (value_list, TRUE);
2023 * g_key_file_get_integer:
2024 * @key_file: a #GKeyFile
2025 * @group_name: a group name
2027 * @error: return location for a #GError
2029 * Returns the value associated with @key under @group_name as an
2030 * integer. If @group_name is %NULL, the start_group is used.
2032 * If @key cannot be found then 0 is returned and @error is set to
2033 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2034 * with @key cannot be interpreted as an integer then 0 is returned
2035 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2037 * Return value: the value associated with the key as an integer, or
2038 * 0 if the key was not found or could not be parsed.
2043 g_key_file_get_integer (GKeyFile *key_file,
2044 const gchar *group_name,
2048 GError *key_file_error;
2052 g_return_val_if_fail (key_file != NULL, -1);
2053 g_return_val_if_fail (group_name != NULL, -1);
2054 g_return_val_if_fail (key != NULL, -1);
2056 key_file_error = NULL;
2058 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2062 g_propagate_error (error, key_file_error);
2066 int_value = g_key_file_parse_value_as_integer (key_file, value,
2072 if (g_error_matches (key_file_error,
2074 G_KEY_FILE_ERROR_INVALID_VALUE))
2076 g_set_error (error, G_KEY_FILE_ERROR,
2077 G_KEY_FILE_ERROR_INVALID_VALUE,
2078 _("Key file contains key '%s' in group '%s' "
2079 "which has value that cannot be interpreted."), key,
2081 g_error_free (key_file_error);
2084 g_propagate_error (error, key_file_error);
2091 * g_key_file_set_integer:
2092 * @key_file: a #GKeyFile
2093 * @group_name: a group name
2095 * @value: an integer value
2097 * Associates a new integer value with @key under @group_name.
2098 * If @key cannot be found then it is created.
2103 g_key_file_set_integer (GKeyFile *key_file,
2104 const gchar *group_name,
2110 g_return_if_fail (key_file != NULL);
2112 result = g_key_file_parse_integer_as_value (key_file, value);
2113 g_key_file_set_value (key_file, group_name, key, result);
2118 * g_key_file_get_integer_list:
2119 * @key_file: a #GKeyFile
2120 * @group_name: a group name
2122 * @length: the number of integers returned
2123 * @error: return location for a #GError
2125 * Returns the values associated with @key under @group_name as
2128 * If @key cannot be found then %NULL is returned and @error is set to
2129 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2130 * with @key cannot be interpreted as integers then %NULL is returned
2131 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2133 * Return value: the values associated with the key as a list of
2134 * integers, or %NULL if the key was not found or could not be parsed.
2139 g_key_file_get_integer_list (GKeyFile *key_file,
2140 const gchar *group_name,
2145 GError *key_file_error = NULL;
2150 g_return_val_if_fail (key_file != NULL, NULL);
2151 g_return_val_if_fail (group_name != NULL, NULL);
2152 g_return_val_if_fail (key != NULL, NULL);
2154 values = g_key_file_get_string_list (key_file, group_name, key,
2155 &num_ints, &key_file_error);
2158 g_propagate_error (error, key_file_error);
2163 int_values = g_new (gint, num_ints);
2165 for (i = 0; i < num_ints; i++)
2167 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2173 g_propagate_error (error, key_file_error);
2174 g_strfreev (values);
2175 g_free (int_values);
2180 g_strfreev (values);
2189 * g_key_file_set_integer_list:
2190 * @key_file: a #GKeyFile
2191 * @group_name: a group name
2193 * @list: an array of integer values
2194 * @length: number of integer values in @list
2196 * Associates a list of integer values with @key under
2197 * @group_name. If @key cannot be found then it is created.
2202 g_key_file_set_integer_list (GKeyFile *key_file,
2203 const gchar *group_name,
2211 g_return_if_fail (key_file != NULL);
2212 g_return_if_fail (list != NULL);
2214 values = g_string_sized_new (length * 16);
2215 for (i = 0; i < length; i++)
2219 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2221 g_string_append (values, value);
2222 g_string_append_c (values, ';');
2227 g_key_file_set_value (key_file, group_name, key, values->str);
2228 g_string_free (values, TRUE);
2232 * g_key_file_get_double:
2233 * @key_file: a #GKeyFile
2234 * @group_name: a group name
2236 * @error: return location for a #GError
2238 * Returns the value associated with @key under @group_name as a
2239 * double. If @group_name is %NULL, the start_group is used.
2241 * If @key cannot be found then 0.0 is returned and @error is set to
2242 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2243 * with @key cannot be interpreted as a double then 0.0 is returned
2244 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2246 * Return value: the value associated with the key as a double, or
2247 * 0.0 if the key was not found or could not be parsed.
2252 g_key_file_get_double (GKeyFile *key_file,
2253 const gchar *group_name,
2257 GError *key_file_error;
2259 gdouble double_value;
2261 g_return_val_if_fail (key_file != NULL, -1);
2262 g_return_val_if_fail (group_name != NULL, -1);
2263 g_return_val_if_fail (key != NULL, -1);
2265 key_file_error = NULL;
2267 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2271 g_propagate_error (error, key_file_error);
2275 double_value = g_key_file_parse_value_as_double (key_file, value,
2281 if (g_error_matches (key_file_error,
2283 G_KEY_FILE_ERROR_INVALID_VALUE))
2285 g_set_error (error, G_KEY_FILE_ERROR,
2286 G_KEY_FILE_ERROR_INVALID_VALUE,
2287 _("Key file contains key '%s' in group '%s' "
2288 "which has value that cannot be interpreted."), key,
2290 g_error_free (key_file_error);
2293 g_propagate_error (error, key_file_error);
2296 return double_value;
2300 * g_key_file_set_double:
2301 * @key_file: a #GKeyFile
2302 * @group_name: a group name
2304 * @value: an double value
2306 * Associates a new double value with @key under @group_name.
2307 * If @key cannot be found then it is created. If @group_name
2308 * is %NULL, the start group is used.
2313 g_key_file_set_double (GKeyFile *key_file,
2314 const gchar *group_name,
2318 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2320 g_return_if_fail (key_file != NULL);
2322 g_ascii_dtostr (result, sizeof (result), value);
2323 g_key_file_set_value (key_file, group_name, key, result);
2327 * g_key_file_get_double_list:
2328 * @key_file: a #GKeyFile
2329 * @group_name: a group name
2331 * @length: the number of doubles returned
2332 * @error: return location for a #GError
2334 * Returns the values associated with @key under @group_name as
2335 * doubles. If @group_name is %NULL, the start group is used.
2337 * If @key cannot be found then %NULL is returned and @error is set to
2338 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2339 * with @key cannot be interpreted as doubles then %NULL is returned
2340 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2342 * Return value: the values associated with the key as a list of
2343 * doubles, or %NULL if the key was not found or could not be parsed.
2348 g_key_file_get_double_list (GKeyFile *key_file,
2349 const gchar *group_name,
2354 GError *key_file_error = NULL;
2356 gdouble *double_values;
2357 gsize i, num_doubles;
2359 g_return_val_if_fail (key_file != NULL, NULL);
2360 g_return_val_if_fail (group_name != NULL, NULL);
2361 g_return_val_if_fail (key != NULL, NULL);
2363 values = g_key_file_get_string_list (key_file, group_name, key,
2364 &num_doubles, &key_file_error);
2367 g_propagate_error (error, key_file_error);
2372 double_values = g_new (gdouble, num_doubles);
2374 for (i = 0; i < num_doubles; i++)
2376 double_values[i] = g_key_file_parse_value_as_double (key_file,
2382 g_propagate_error (error, key_file_error);
2383 g_strfreev (values);
2384 g_free (double_values);
2389 g_strfreev (values);
2392 *length = num_doubles;
2394 return double_values;
2398 * g_key_file_set_double_list:
2399 * @key_file: a #GKeyFile
2400 * @group_name: a group name
2402 * @list: an array of double values
2403 * @length: number of double values in @list
2405 * Associates a list of double values with @key under
2406 * @group_name. If @key cannot be found then it is created.
2407 * If @group_name is %NULL the start group is used.
2412 g_key_file_set_double_list (GKeyFile *key_file,
2413 const gchar *group_name,
2421 g_return_if_fail (key_file != NULL);
2422 g_return_if_fail (list != NULL);
2424 values = g_string_sized_new (length * 16);
2425 for (i = 0; i < length; i++)
2427 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2429 g_ascii_dtostr( result, sizeof (result), list[i] );
2431 g_string_append (values, result);
2432 g_string_append_c (values, ';');
2435 g_key_file_set_value (key_file, group_name, key, values->str);
2436 g_string_free (values, TRUE);
2440 g_key_file_set_key_comment (GKeyFile *key_file,
2441 const gchar *group_name,
2443 const gchar *comment,
2446 GKeyFileGroup *group;
2447 GKeyFileKeyValuePair *pair;
2448 GList *key_node, *comment_node, *tmp;
2450 group = g_key_file_lookup_group (key_file, group_name);
2453 g_set_error (error, G_KEY_FILE_ERROR,
2454 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2455 _("Key file does not have group '%s'"),
2456 group_name ? group_name : "(null)");
2461 /* First find the key the comments are supposed to be
2464 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2466 if (key_node == NULL)
2468 g_set_error (error, G_KEY_FILE_ERROR,
2469 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2470 _("Key file does not have key '%s' in group '%s'"),
2475 /* Then find all the comments already associated with the
2478 tmp = key_node->next;
2481 GKeyFileKeyValuePair *pair;
2483 pair = (GKeyFileKeyValuePair *) tmp->data;
2485 if (pair->key != NULL)
2490 g_key_file_remove_key_value_pair_node (key_file, group,
2494 if (comment == NULL)
2497 /* Now we can add our new comment
2499 pair = g_slice_new (GKeyFileKeyValuePair);
2501 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2503 key_node = g_list_insert (key_node, pair, 1);
2507 g_key_file_set_group_comment (GKeyFile *key_file,
2508 const gchar *group_name,
2509 const gchar *comment,
2512 GKeyFileGroup *group;
2514 g_return_if_fail (g_key_file_is_group_name (group_name));
2516 group = g_key_file_lookup_group (key_file, group_name);
2519 g_set_error (error, G_KEY_FILE_ERROR,
2520 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2521 _("Key file does not have group '%s'"),
2522 group_name ? group_name : "(null)");
2527 /* First remove any existing comment
2531 g_key_file_key_value_pair_free (group->comment);
2532 group->comment = NULL;
2535 if (comment == NULL)
2538 /* Now we can add our new comment
2540 group->comment = g_slice_new (GKeyFileKeyValuePair);
2541 group->comment->key = NULL;
2542 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2546 g_key_file_set_top_comment (GKeyFile *key_file,
2547 const gchar *comment,
2551 GKeyFileGroup *group;
2552 GKeyFileKeyValuePair *pair;
2554 /* The last group in the list should be the top (comments only)
2557 g_assert (key_file->groups != NULL);
2558 group_node = g_list_last (key_file->groups);
2559 group = (GKeyFileGroup *) group_node->data;
2560 g_assert (group->name == NULL);
2562 /* Note all keys must be comments at the top of
2563 * the file, so we can just free it all.
2565 if (group->key_value_pairs != NULL)
2567 g_list_foreach (group->key_value_pairs,
2568 (GFunc) g_key_file_key_value_pair_free,
2570 g_list_free (group->key_value_pairs);
2571 group->key_value_pairs = NULL;
2574 if (comment == NULL)
2577 pair = g_slice_new (GKeyFileKeyValuePair);
2579 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2581 group->key_value_pairs =
2582 g_list_prepend (group->key_value_pairs, pair);
2586 * g_key_file_set_comment:
2587 * @key_file: a #GKeyFile
2588 * @group_name: a group name, or %NULL
2590 * @comment: a comment
2591 * @error: return location for a #GError
2593 * Places a comment above @key from @group_name.
2594 * @group_name. If @key is %NULL then @comment will
2595 * be written above @group_name. If both @key
2596 * and @group_name are NULL, then @comment will
2597 * be written above the first group in the file.
2602 g_key_file_set_comment (GKeyFile *key_file,
2603 const gchar *group_name,
2605 const gchar *comment,
2608 g_return_if_fail (key_file != NULL);
2610 if (group_name != NULL && key != NULL)
2611 g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2612 else if (group_name != NULL)
2613 g_key_file_set_group_comment (key_file, group_name, comment, error);
2615 g_key_file_set_top_comment (key_file, comment, error);
2617 if (comment != NULL)
2618 key_file->approximate_size += strlen (comment);
2622 g_key_file_get_key_comment (GKeyFile *key_file,
2623 const gchar *group_name,
2627 GKeyFileGroup *group;
2628 GKeyFileKeyValuePair *pair;
2629 GList *key_node, *tmp;
2633 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2635 group = g_key_file_lookup_group (key_file, group_name);
2638 g_set_error (error, G_KEY_FILE_ERROR,
2639 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2640 _("Key file does not have group '%s'"),
2641 group_name ? group_name : "(null)");
2646 /* First find the key the comments are supposed to be
2649 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2651 if (key_node == NULL)
2653 g_set_error (error, G_KEY_FILE_ERROR,
2654 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2655 _("Key file does not have key '%s' in group '%s'"),
2662 /* Then find all the comments already associated with the
2663 * key and concatentate them.
2665 tmp = key_node->next;
2666 if (!key_node->next)
2669 pair = (GKeyFileKeyValuePair *) tmp->data;
2670 if (pair->key != NULL)
2675 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2677 if (pair->key != NULL)
2683 while (tmp != key_node)
2685 GKeyFileKeyValuePair *pair;
2687 pair = (GKeyFileKeyValuePair *) tmp->data;
2690 string = g_string_sized_new (512);
2692 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2693 g_string_append (string, comment);
2701 comment = string->str;
2702 g_string_free (string, FALSE);
2711 get_group_comment (GKeyFile *key_file,
2712 GKeyFileGroup *group,
2721 tmp = group->key_value_pairs;
2724 GKeyFileKeyValuePair *pair;
2726 pair = (GKeyFileKeyValuePair *) tmp->data;
2728 if (pair->key != NULL)
2734 if (tmp->next == NULL)
2742 GKeyFileKeyValuePair *pair;
2744 pair = (GKeyFileKeyValuePair *) tmp->data;
2747 string = g_string_sized_new (512);
2749 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2750 g_string_append (string, comment);
2757 return g_string_free (string, FALSE);
2763 g_key_file_get_group_comment (GKeyFile *key_file,
2764 const gchar *group_name,
2768 GKeyFileGroup *group;
2770 group_node = g_key_file_lookup_group_node (key_file, group_name);
2773 g_set_error (error, G_KEY_FILE_ERROR,
2774 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2775 _("Key file does not have group '%s'"),
2776 group_name ? group_name : "(null)");
2781 group = (GKeyFileGroup *)group_node->data;
2783 return g_strdup (group->comment->value);
2785 group_node = group_node->next;
2786 group = (GKeyFileGroup *)group_node->data;
2787 return get_group_comment (key_file, group, error);
2791 g_key_file_get_top_comment (GKeyFile *key_file,
2795 GKeyFileGroup *group;
2797 /* The last group in the list should be the top (comments only)
2800 g_assert (key_file->groups != NULL);
2801 group_node = g_list_last (key_file->groups);
2802 group = (GKeyFileGroup *) group_node->data;
2803 g_assert (group->name == NULL);
2805 return get_group_comment (key_file, group, error);
2809 * g_key_file_get_comment:
2810 * @key_file: a #GKeyFile
2811 * @group_name: a group name, or %NULL
2813 * @error: return location for a #GError
2815 * Retrieves a comment above @key from @group_name.
2816 * @group_name. If @key is %NULL then @comment will
2817 * be read from above @group_name. If both @key
2818 * and @group_name are NULL, then @comment will
2819 * be read from above the first group in the file.
2821 * Returns: a comment that should be freed with g_free()
2826 g_key_file_get_comment (GKeyFile *key_file,
2827 const gchar *group_name,
2831 g_return_val_if_fail (key_file != NULL, NULL);
2833 if (group_name != NULL && key != NULL)
2834 return g_key_file_get_key_comment (key_file, group_name, key, error);
2835 else if (group_name != NULL)
2836 return g_key_file_get_group_comment (key_file, group_name, error);
2838 return g_key_file_get_top_comment (key_file, error);
2842 * g_key_file_remove_comment:
2843 * @key_file: a #GKeyFile
2844 * @group_name: a group name, or %NULL
2846 * @error: return location for a #GError
2848 * Removes a comment above @key from @group_name.
2849 * @group_name. If @key is %NULL then @comment will
2850 * be written above @group_name. If both @key
2851 * and @group_name are NULL, then @comment will
2852 * be written above the first group in the file.
2858 g_key_file_remove_comment (GKeyFile *key_file,
2859 const gchar *group_name,
2863 g_return_if_fail (key_file != NULL);
2865 if (group_name != NULL && key != NULL)
2866 g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2867 else if (group_name != NULL)
2868 g_key_file_set_group_comment (key_file, group_name, NULL, error);
2870 g_key_file_set_top_comment (key_file, NULL, error);
2874 * g_key_file_has_group:
2875 * @key_file: a #GKeyFile
2876 * @group_name: a group name
2878 * Looks whether the key file has the group @group_name.
2880 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2885 g_key_file_has_group (GKeyFile *key_file,
2886 const gchar *group_name)
2888 g_return_val_if_fail (key_file != NULL, FALSE);
2889 g_return_val_if_fail (group_name != NULL, FALSE);
2891 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2895 * g_key_file_has_key:
2896 * @key_file: a #GKeyFile
2897 * @group_name: a group name
2899 * @error: return location for a #GError
2901 * Looks whether the key file has the key @key in the group
2904 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2910 g_key_file_has_key (GKeyFile *key_file,
2911 const gchar *group_name,
2915 GKeyFileKeyValuePair *pair;
2916 GKeyFileGroup *group;
2918 g_return_val_if_fail (key_file != NULL, FALSE);
2919 g_return_val_if_fail (group_name != NULL, FALSE);
2920 g_return_val_if_fail (key != NULL, FALSE);
2922 group = g_key_file_lookup_group (key_file, group_name);
2926 g_set_error (error, G_KEY_FILE_ERROR,
2927 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2928 _("Key file does not have group '%s'"),
2929 group_name ? group_name : "(null)");
2934 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2936 return pair != NULL;
2940 g_key_file_add_group (GKeyFile *key_file,
2941 const gchar *group_name)
2943 GKeyFileGroup *group;
2945 g_return_if_fail (key_file != NULL);
2946 g_return_if_fail (g_key_file_is_group_name (group_name));
2948 group = g_key_file_lookup_group (key_file, group_name);
2951 key_file->current_group = group;
2955 group = g_slice_new0 (GKeyFileGroup);
2956 group->name = g_strdup (group_name);
2957 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2958 key_file->groups = g_list_prepend (key_file->groups, group);
2959 key_file->approximate_size += strlen (group_name) + 3;
2960 key_file->current_group = group;
2962 if (key_file->start_group == NULL)
2963 key_file->start_group = group;
2967 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2972 g_free (pair->value);
2973 g_slice_free (GKeyFileKeyValuePair, pair);
2977 /* Be careful not to call this function on a node with data in the
2978 * lookup map without removing it from the lookup map, first.
2980 * Some current cases where this warning is not a concern are
2982 * - the node being removed is a comment node
2983 * - the entire lookup map is getting destroyed soon after
2987 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2988 GKeyFileGroup *group,
2992 GKeyFileKeyValuePair *pair;
2994 pair = (GKeyFileKeyValuePair *) pair_node->data;
2996 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2998 if (pair->key != NULL)
2999 key_file->approximate_size -= strlen (pair->key) + 1;
3001 g_assert (pair->value != NULL);
3002 key_file->approximate_size -= strlen (pair->value);
3004 g_key_file_key_value_pair_free (pair);
3006 g_list_free_1 (pair_node);
3010 g_key_file_remove_group_node (GKeyFile *key_file,
3013 GKeyFileGroup *group;
3016 group = (GKeyFileGroup *) group_node->data;
3018 /* If the current group gets deleted make the current group the last
3021 if (key_file->current_group == group)
3023 /* groups should always contain at least the top comment group,
3024 * unless g_key_file_clear has been called
3026 if (key_file->groups)
3027 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3029 key_file->current_group = NULL;
3032 /* If the start group gets deleted make the start group the first
3035 if (key_file->start_group == group)
3037 tmp = g_list_last (key_file->groups);
3040 if (tmp != group_node &&
3041 ((GKeyFileGroup *) tmp->data)->name != NULL)
3048 key_file->start_group = (GKeyFileGroup *) tmp->data;
3050 key_file->start_group = NULL;
3053 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3055 if (group->name != NULL)
3056 key_file->approximate_size -= strlen (group->name) + 3;
3058 tmp = group->key_value_pairs;
3065 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3068 g_assert (group->key_value_pairs == NULL);
3070 if (group->lookup_map)
3072 g_hash_table_destroy (group->lookup_map);
3073 group->lookup_map = NULL;
3076 g_free ((gchar *) group->name);
3077 g_slice_free (GKeyFileGroup, group);
3078 g_list_free_1 (group_node);
3082 * g_key_file_remove_group:
3083 * @key_file: a #GKeyFile
3084 * @group_name: a group name
3085 * @error: return location for a #GError or %NULL
3087 * Removes the specified group, @group_name,
3088 * from the key file.
3093 g_key_file_remove_group (GKeyFile *key_file,
3094 const gchar *group_name,
3099 g_return_if_fail (key_file != NULL);
3100 g_return_if_fail (group_name != NULL);
3102 group_node = g_key_file_lookup_group_node (key_file, group_name);
3106 g_set_error (error, G_KEY_FILE_ERROR,
3107 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3108 _("Key file does not have group '%s'"),
3113 g_key_file_remove_group_node (key_file, group_node);
3117 g_key_file_add_key (GKeyFile *key_file,
3118 GKeyFileGroup *group,
3122 GKeyFileKeyValuePair *pair;
3124 pair = g_slice_new (GKeyFileKeyValuePair);
3125 pair->key = g_strdup (key);
3126 pair->value = g_strdup (value);
3128 g_hash_table_replace (group->lookup_map, pair->key, pair);
3129 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3130 group->has_trailing_blank_line = FALSE;
3131 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3135 * g_key_file_remove_key:
3136 * @key_file: a #GKeyFile
3137 * @group_name: a group name
3138 * @key: a key name to remove
3139 * @error: return location for a #GError or %NULL
3141 * Removes @key in @group_name from the key file.
3146 g_key_file_remove_key (GKeyFile *key_file,
3147 const gchar *group_name,
3151 GKeyFileGroup *group;
3152 GKeyFileKeyValuePair *pair;
3154 g_return_if_fail (key_file != NULL);
3155 g_return_if_fail (group_name != NULL);
3156 g_return_if_fail (key != NULL);
3160 group = g_key_file_lookup_group (key_file, group_name);
3163 g_set_error (error, G_KEY_FILE_ERROR,
3164 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3165 _("Key file does not have group '%s'"),
3166 group_name ? group_name : "(null)");
3170 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3174 g_set_error (error, G_KEY_FILE_ERROR,
3175 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3176 _("Key file does not have key '%s' in group '%s'"),
3181 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3183 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3184 g_hash_table_remove (group->lookup_map, pair->key);
3185 g_key_file_key_value_pair_free (pair);
3189 g_key_file_lookup_group_node (GKeyFile *key_file,
3190 const gchar *group_name)
3192 GKeyFileGroup *group;
3195 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3197 group = (GKeyFileGroup *) tmp->data;
3199 if (group && group->name && strcmp (group->name, group_name) == 0)
3206 static GKeyFileGroup *
3207 g_key_file_lookup_group (GKeyFile *key_file,
3208 const gchar *group_name)
3212 group_node = g_key_file_lookup_group_node (key_file, group_name);
3214 if (group_node != NULL)
3215 return (GKeyFileGroup *) group_node->data;
3221 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3222 GKeyFileGroup *group,
3227 for (key_node = group->key_value_pairs;
3229 key_node = key_node->next)
3231 GKeyFileKeyValuePair *pair;
3233 pair = (GKeyFileKeyValuePair *) key_node->data;
3235 if (pair->key && strcmp (pair->key, key) == 0)
3242 static GKeyFileKeyValuePair *
3243 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3244 GKeyFileGroup *group,
3247 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3250 /* Lines starting with # or consisting entirely of whitespace are merely
3251 * recorded, not parsed. This function assumes all leading whitespace
3252 * has been stripped.
3255 g_key_file_line_is_comment (const gchar *line)
3257 return (*line == '#' || *line == '\0' || *line == '\n');
3261 g_key_file_is_group_name (const gchar *name)
3268 p = q = (gchar *) name;
3269 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3270 q = g_utf8_find_next_char (q, NULL);
3272 if (*q != '\0' || q == p)
3279 g_key_file_is_key_name (const gchar *name)
3286 p = q = (gchar *) name;
3287 /* We accept a little more than the desktop entry spec says,
3288 * since gnome-vfs uses mime-types as keys in its cache.
3290 while (*q && *q != '=' && *q != '[' && *q != ']')
3291 q = g_utf8_find_next_char (q, NULL);
3293 /* No empty keys, please */
3297 /* We accept spaces in the middle of keys to not break
3298 * existing apps, but we don't tolerate initial of final
3299 * spaces, which would lead to silent corruption when
3300 * rereading the file.
3302 if (*p == ' ' || q[-1] == ' ')
3308 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3309 q = g_utf8_find_next_char (q, NULL);
3323 /* A group in a key file is made up of a starting '[' followed by one
3324 * or more letters making up the group name followed by ']'.
3327 g_key_file_line_is_group (const gchar *line)
3337 while (*p && *p != ']')
3338 p = g_utf8_find_next_char (p, NULL);
3343 /* silently accept whitespace after the ] */
3344 p = g_utf8_find_next_char (p, NULL);
3345 while (*p == ' ' || *p == '\t')
3346 p = g_utf8_find_next_char (p, NULL);
3355 g_key_file_line_is_key_value_pair (const gchar *line)
3359 p = (gchar *) g_utf8_strchr (line, -1, '=');
3364 /* Key must be non-empty
3373 g_key_file_parse_value_as_string (GKeyFile *key_file,
3378 gchar *string_value, *p, *q0, *q;
3380 string_value = g_new (gchar, strlen (value) + 1);
3382 p = (gchar *) value;
3383 q0 = q = string_value;
3413 g_set_error (error, G_KEY_FILE_ERROR,
3414 G_KEY_FILE_ERROR_INVALID_VALUE,
3415 _("Key file contains escape character "
3420 if (pieces && *p == key_file->list_separator)
3421 *q = key_file->list_separator;
3435 g_set_error (error, G_KEY_FILE_ERROR,
3436 G_KEY_FILE_ERROR_INVALID_VALUE,
3437 _("Key file contains invalid escape "
3438 "sequence '%s'"), sequence);
3447 if (pieces && (*p == key_file->list_separator))
3449 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3465 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3466 *pieces = g_slist_reverse (*pieces);
3469 return string_value;
3473 g_key_file_parse_string_as_value (GKeyFile *key_file,
3474 const gchar *string,
3475 gboolean escape_separator)
3477 gchar *value, *p, *q;
3479 gboolean parsing_leading_space;
3481 length = strlen (string) + 1;
3483 /* Worst case would be that every character needs to be escaped.
3484 * In other words every character turns to two characters
3486 value = g_new (gchar, 2 * length);
3488 p = (gchar *) string;
3490 parsing_leading_space = TRUE;
3491 while (p < (string + length - 1))
3493 gchar escaped_character[3] = { '\\', 0, 0 };
3498 if (parsing_leading_space)
3500 escaped_character[1] = 's';
3501 strcpy (q, escaped_character);
3511 if (parsing_leading_space)
3513 escaped_character[1] = 't';
3514 strcpy (q, escaped_character);
3524 escaped_character[1] = 'n';
3525 strcpy (q, escaped_character);
3529 escaped_character[1] = 'r';
3530 strcpy (q, escaped_character);
3534 escaped_character[1] = '\\';
3535 strcpy (q, escaped_character);
3537 parsing_leading_space = FALSE;
3540 if (escape_separator && *p == key_file->list_separator)
3542 escaped_character[1] = key_file->list_separator;
3543 strcpy (q, escaped_character);
3545 parsing_leading_space = TRUE;
3551 parsing_leading_space = FALSE;
3563 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3567 gchar *end_of_valid_int;
3572 long_value = strtol (value, &end_of_valid_int, 10);
3574 if (*value == '\0' || *end_of_valid_int != '\0')
3576 gchar *value_utf8 = _g_utf8_make_valid (value);
3577 g_set_error (error, G_KEY_FILE_ERROR,
3578 G_KEY_FILE_ERROR_INVALID_VALUE,
3579 _("Value '%s' cannot be interpreted "
3580 "as a number."), value_utf8);
3581 g_free (value_utf8);
3586 int_value = long_value;
3587 if (int_value != long_value || errno == ERANGE)
3589 gchar *value_utf8 = _g_utf8_make_valid (value);
3592 G_KEY_FILE_ERROR_INVALID_VALUE,
3593 _("Integer value '%s' out of range"),
3595 g_free (value_utf8);
3604 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3608 return g_strdup_printf ("%d", value);
3612 g_key_file_parse_value_as_double (GKeyFile *key_file,
3616 gchar *end_of_valid_d;
3617 gdouble double_value = 0;
3619 double_value = g_ascii_strtod (value, &end_of_valid_d);
3621 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3623 gchar *value_utf8 = _g_utf8_make_valid (value);
3624 g_set_error (error, G_KEY_FILE_ERROR,
3625 G_KEY_FILE_ERROR_INVALID_VALUE,
3626 _("Value '%s' cannot be interpreted "
3627 "as a float number."),
3629 g_free (value_utf8);
3632 return double_value;
3636 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3642 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3644 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3647 value_utf8 = _g_utf8_make_valid (value);
3648 g_set_error (error, G_KEY_FILE_ERROR,
3649 G_KEY_FILE_ERROR_INVALID_VALUE,
3650 _("Value '%s' cannot be interpreted "
3651 "as a boolean."), value_utf8);
3652 g_free (value_utf8);
3658 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3662 return g_strdup ("true");
3664 return g_strdup ("false");
3668 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3675 string = g_string_sized_new (512);
3677 lines = g_strsplit (value, "\n", 0);
3679 for (i = 0; lines[i] != NULL; i++)
3681 if (lines[i][0] != '#')
3682 g_string_append_printf (string, "%s\n", lines[i]);
3684 g_string_append_printf (string, "%s\n", lines[i] + 1);
3688 return g_string_free (string, FALSE);
3692 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3693 const gchar *comment)
3699 string = g_string_sized_new (512);
3701 lines = g_strsplit (comment, "\n", 0);
3703 for (i = 0; lines[i] != NULL; i++)
3704 g_string_append_printf (string, "#%s%s", lines[i],
3705 lines[i + 1] == NULL? "" : "\n");
3708 return g_string_free (string, FALSE);
3711 #define __G_KEY_FILE_C__
3712 #include "galiasdef.c"