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;
70 GHashTable *group_hash;
72 GKeyFileGroup *start_group;
73 GKeyFileGroup *current_group;
75 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
77 /* Used for sizing the output buffer during serialization
79 gsize approximate_size;
88 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
92 const gchar *name; /* NULL for above first group (which will be comments) */
94 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
95 gboolean has_trailing_blank_line;
97 GList *key_value_pairs;
99 /* Used in parallel with key_value_pairs for
100 * increased lookup performance
102 GHashTable *lookup_map;
105 struct _GKeyFileKeyValuePair
107 gchar *key; /* NULL for comments */
111 static gint find_file_in_data_dirs (const gchar *file,
112 const gchar **data_dirs,
115 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
119 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
120 const gchar *group_name);
121 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
122 const gchar *group_name);
124 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
125 GKeyFileGroup *group,
127 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
128 GKeyFileGroup *group,
131 static void g_key_file_remove_group_node (GKeyFile *key_file,
133 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
134 GKeyFileGroup *group,
137 static void g_key_file_add_key (GKeyFile *key_file,
138 GKeyFileGroup *group,
141 static void g_key_file_add_group (GKeyFile *key_file,
142 const gchar *group_name);
143 static gboolean g_key_file_is_group_name (const gchar *name);
144 static gboolean g_key_file_is_key_name (const gchar *name);
145 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
146 static gboolean g_key_file_line_is_comment (const gchar *line);
147 static gboolean g_key_file_line_is_group (const gchar *line);
148 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
149 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
153 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
155 gboolean escape_separator);
156 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
159 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
161 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
164 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
167 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
169 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
171 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
172 const gchar *comment);
173 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
177 static void g_key_file_parse_comment (GKeyFile *key_file,
181 static void g_key_file_parse_group (GKeyFile *key_file,
185 static gchar *key_get_locale (const gchar *key);
186 static void g_key_file_parse_data (GKeyFile *key_file,
190 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
195 g_key_file_error_quark (void)
197 return g_quark_from_static_string ("g-key-file-error-quark");
201 g_key_file_init (GKeyFile *key_file)
203 key_file->current_group = g_slice_new0 (GKeyFileGroup);
204 key_file->groups = g_list_prepend (NULL, key_file->current_group);
205 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
206 key_file->start_group = NULL;
207 key_file->parse_buffer = g_string_sized_new (128);
208 key_file->approximate_size = 0;
209 key_file->list_separator = ';';
211 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
215 g_key_file_clear (GKeyFile *key_file)
217 GList *tmp, *group_node;
219 if (key_file->locales)
221 g_strfreev (key_file->locales);
222 key_file->locales = NULL;
225 if (key_file->parse_buffer)
227 g_string_free (key_file->parse_buffer, TRUE);
228 key_file->parse_buffer = NULL;
231 tmp = key_file->groups;
236 g_key_file_remove_group_node (key_file, group_node);
239 g_hash_table_destroy (key_file->group_hash);
240 key_file->group_hash = NULL;
242 g_warn_if_fail (key_file->groups == NULL);
249 * Creates a new empty #GKeyFile object. Use
250 * g_key_file_load_from_file(), g_key_file_load_from_data(),
251 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
252 * read an existing key file.
254 * Return value: an empty #GKeyFile.
259 g_key_file_new (void)
263 key_file = g_slice_new0 (GKeyFile);
264 g_key_file_init (key_file);
270 * g_key_file_set_list_separator:
271 * @key_file: a #GKeyFile
272 * @separator: the separator
274 * Sets the character which is used to separate
275 * values in lists. Typically ';' or ',' are used
276 * as separators. The default list separator is ';'.
281 g_key_file_set_list_separator (GKeyFile *key_file,
284 g_return_if_fail (key_file != NULL);
286 key_file->list_separator = separator;
290 /* Iterates through all the directories in *dirs trying to
291 * open file. When it successfully locates and opens a file it
292 * returns the file descriptor to the open file. It also
293 * outputs the absolute path of the file in output_file.
296 find_file_in_data_dirs (const gchar *file,
301 const gchar **data_dirs, *data_dir;
313 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
315 gchar *candidate_file, *sub_dir;
317 candidate_file = (gchar *) file;
318 sub_dir = g_strdup ("");
319 while (candidate_file != NULL && fd < 0)
323 path = g_build_filename (data_dir, sub_dir,
324 candidate_file, NULL);
326 fd = g_open (path, O_RDONLY, 0);
334 candidate_file = strchr (candidate_file, '-');
336 if (candidate_file == NULL)
342 sub_dir = g_strndup (file, candidate_file - file - 1);
344 for (p = sub_dir; *p != '\0'; p++)
347 *p = G_DIR_SEPARATOR;
356 g_set_error (error, G_KEY_FILE_ERROR,
357 G_KEY_FILE_ERROR_NOT_FOUND,
358 _("Valid key file could not be "
359 "found in search dirs"));
362 if (output_file != NULL && fd > 0)
363 *output_file = g_strdup (path);
371 g_key_file_load_from_fd (GKeyFile *key_file,
376 GError *key_file_error = NULL;
378 struct stat stat_buf;
379 gchar read_buf[4096];
381 if (fstat (fd, &stat_buf) < 0)
383 g_set_error (error, G_FILE_ERROR,
384 g_file_error_from_errno (errno),
385 "%s", g_strerror (errno));
389 if (!S_ISREG (stat_buf.st_mode))
391 g_set_error (error, G_KEY_FILE_ERROR,
392 G_KEY_FILE_ERROR_PARSE,
393 _("Not a regular file"));
397 if (stat_buf.st_size == 0)
399 g_set_error (error, G_KEY_FILE_ERROR,
400 G_KEY_FILE_ERROR_PARSE,
405 if (key_file->approximate_size > 0)
407 g_key_file_clear (key_file);
408 g_key_file_init (key_file);
410 key_file->flags = flags;
415 bytes_read = read (fd, read_buf, 4096);
417 if (bytes_read == 0) /* End of File */
422 if (errno == EINTR || errno == EAGAIN)
425 g_set_error (error, G_FILE_ERROR,
426 g_file_error_from_errno (errno),
427 "%s", g_strerror (errno));
431 g_key_file_parse_data (key_file,
432 read_buf, bytes_read,
435 while (!key_file_error);
439 g_propagate_error (error, key_file_error);
443 g_key_file_flush_parse_buffer (key_file, &key_file_error);
447 g_propagate_error (error, key_file_error);
455 * g_key_file_load_from_file:
456 * @key_file: an empty #GKeyFile struct
457 * @file: the path of a filename to load, in the GLib filename encoding
458 * @flags: flags from #GKeyFileFlags
459 * @error: return location for a #GError, or %NULL
461 * Loads a key file into an empty #GKeyFile structure.
462 * If the file could not be loaded then %error is set to
463 * either a #GFileError or #GKeyFileError.
465 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
470 g_key_file_load_from_file (GKeyFile *key_file,
475 GError *key_file_error = NULL;
478 g_return_val_if_fail (key_file != NULL, FALSE);
479 g_return_val_if_fail (file != NULL, FALSE);
481 fd = g_open (file, O_RDONLY, 0);
485 g_set_error (error, G_FILE_ERROR,
486 g_file_error_from_errno (errno),
487 "%s", g_strerror (errno));
491 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
496 g_propagate_error (error, key_file_error);
504 * g_key_file_load_from_data:
505 * @key_file: an empty #GKeyFile struct
506 * @data: key file loaded in memory
507 * @length: the length of @data in bytes
508 * @flags: flags from #GKeyFileFlags
509 * @error: return location for a #GError, or %NULL
511 * Loads a key file from memory into an empty #GKeyFile structure.
512 * If the object cannot be created then %error is set to a #GKeyFileError.
514 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
519 g_key_file_load_from_data (GKeyFile *key_file,
525 GError *key_file_error = NULL;
527 g_return_val_if_fail (key_file != NULL, FALSE);
528 g_return_val_if_fail (data != NULL, FALSE);
529 g_return_val_if_fail (length != 0, FALSE);
531 if (length == (gsize)-1)
532 length = strlen (data);
534 if (key_file->approximate_size > 0)
536 g_key_file_clear (key_file);
537 g_key_file_init (key_file);
539 key_file->flags = flags;
541 g_key_file_parse_data (key_file, data, length, &key_file_error);
545 g_propagate_error (error, key_file_error);
549 g_key_file_flush_parse_buffer (key_file, &key_file_error);
553 g_propagate_error (error, key_file_error);
561 * g_key_file_load_from_dirs:
562 * @key_file: an empty #GKeyFile struct
563 * @file: a relative path to a filename to open and parse
564 * @search_dirs: %NULL-terminated array of directories to search
565 * @full_path: return location for a string containing the full path
566 * of the file, or %NULL
567 * @flags: flags from #GKeyFileFlags
568 * @error: return location for a #GError, or %NULL
570 * This function looks for a key file named @file in the paths
571 * specified in @search_dirs, loads the file into @key_file and
572 * returns the file's full path in @full_path. If the file could not
573 * be loaded then an %error is set to either a #GFileError or
576 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
581 g_key_file_load_from_dirs (GKeyFile *key_file,
583 const gchar **search_dirs,
588 GError *key_file_error = NULL;
589 const gchar **data_dirs;
594 g_return_val_if_fail (key_file != NULL, FALSE);
595 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
596 g_return_val_if_fail (search_dirs != NULL, FALSE);
599 data_dirs = search_dirs;
601 while (*data_dirs != NULL && !found_file)
603 g_free (output_path);
605 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
611 g_propagate_error (error, key_file_error);
615 found_file = g_key_file_load_from_fd (key_file, fd, flags,
621 g_propagate_error (error, key_file_error);
626 if (found_file && full_path)
627 *full_path = output_path;
629 g_free (output_path);
635 * g_key_file_load_from_data_dirs:
636 * @key_file: an empty #GKeyFile struct
637 * @file: a relative path to a filename to open and parse
638 * @full_path: return location for a string containing the full path
639 * of the file, or %NULL
640 * @flags: flags from #GKeyFileFlags
641 * @error: return location for a #GError, or %NULL
643 * This function looks for a key file named @file in the paths
644 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
645 * loads the file into @key_file and returns the file's full path in
646 * @full_path. If the file could not be loaded then an %error is
647 * set to either a #GFileError or #GKeyFileError.
649 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
653 g_key_file_load_from_data_dirs (GKeyFile *key_file,
659 gchar **all_data_dirs;
660 const gchar * user_data_dir;
661 const gchar * const * system_data_dirs;
665 g_return_val_if_fail (key_file != NULL, FALSE);
666 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
668 user_data_dir = g_get_user_data_dir ();
669 system_data_dirs = g_get_system_data_dirs ();
670 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
673 all_data_dirs[i++] = g_strdup (user_data_dir);
676 while (system_data_dirs[j] != NULL)
677 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
678 all_data_dirs[i] = NULL;
680 found_file = g_key_file_load_from_dirs (key_file,
682 (const gchar **)all_data_dirs,
687 g_strfreev (all_data_dirs);
694 * @key_file: a #GKeyFile
701 g_key_file_free (GKeyFile *key_file)
703 g_return_if_fail (key_file != NULL);
705 g_key_file_clear (key_file);
706 g_slice_free (GKeyFile, key_file);
709 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
710 * true for locales that match those in g_get_language_names().
713 g_key_file_locale_is_interesting (GKeyFile *key_file,
718 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
721 for (i = 0; key_file->locales[i] != NULL; i++)
723 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
731 g_key_file_parse_line (GKeyFile *key_file,
736 GError *parse_error = NULL;
739 g_return_if_fail (key_file != NULL);
740 g_return_if_fail (line != NULL);
742 line_start = (gchar *) line;
743 while (g_ascii_isspace (*line_start))
746 if (g_key_file_line_is_comment (line_start))
747 g_key_file_parse_comment (key_file, line, length, &parse_error);
748 else if (g_key_file_line_is_group (line_start))
749 g_key_file_parse_group (key_file, line_start,
750 length - (line_start - line),
752 else if (g_key_file_line_is_key_value_pair (line_start))
753 g_key_file_parse_key_value_pair (key_file, line_start,
754 length - (line_start - line),
758 gchar *line_utf8 = _g_utf8_make_valid (line);
759 g_set_error (error, G_KEY_FILE_ERROR,
760 G_KEY_FILE_ERROR_PARSE,
761 _("Key file contains line '%s' which is not "
762 "a key-value pair, group, or comment"),
770 g_propagate_error (error, parse_error);
774 g_key_file_parse_comment (GKeyFile *key_file,
779 GKeyFileKeyValuePair *pair;
781 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
784 g_warn_if_fail (key_file->current_group != NULL);
786 pair = g_slice_new (GKeyFileKeyValuePair);
788 pair->value = g_strndup (line, length);
790 key_file->current_group->key_value_pairs =
791 g_list_prepend (key_file->current_group->key_value_pairs, pair);
793 if (length == 0 || line[0] != '#')
794 key_file->current_group->has_trailing_blank_line = TRUE;
798 g_key_file_parse_group (GKeyFile *key_file,
804 const gchar *group_name_start, *group_name_end;
806 /* advance past opening '['
808 group_name_start = line + 1;
809 group_name_end = line + length - 1;
811 while (*group_name_end != ']')
814 group_name = g_strndup (group_name_start,
815 group_name_end - group_name_start);
817 if (!g_key_file_is_group_name (group_name))
819 g_set_error (error, G_KEY_FILE_ERROR,
820 G_KEY_FILE_ERROR_PARSE,
821 _("Invalid group name: %s"), group_name);
826 g_key_file_add_group (key_file, group_name);
831 g_key_file_parse_key_value_pair (GKeyFile *key_file,
836 gchar *key, *value, *key_end, *value_start, *locale;
837 gsize key_len, value_len;
839 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
841 g_set_error (error, G_KEY_FILE_ERROR,
842 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
843 _("Key file does not start with a group"));
847 key_end = value_start = strchr (line, '=');
849 g_warn_if_fail (key_end != NULL);
854 /* Pull the key name from the line (chomping trailing whitespace)
856 while (g_ascii_isspace (*key_end))
859 key_len = key_end - line + 2;
861 g_warn_if_fail (key_len <= length);
863 key = g_strndup (line, key_len - 1);
865 if (!g_key_file_is_key_name (key))
867 g_set_error (error, G_KEY_FILE_ERROR,
868 G_KEY_FILE_ERROR_PARSE,
869 _("Invalid key name: %s"), key);
874 /* Pull the value from the line (chugging leading whitespace)
876 while (g_ascii_isspace (*value_start))
879 value_len = line + length - value_start + 1;
881 value = g_strndup (value_start, value_len);
883 g_warn_if_fail (key_file->start_group != NULL);
885 if (key_file->current_group
886 && key_file->current_group->name
887 && strcmp (key_file->start_group->name,
888 key_file->current_group->name) == 0
889 && strcmp (key, "Encoding") == 0)
891 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
893 gchar *value_utf8 = _g_utf8_make_valid (value);
894 g_set_error (error, G_KEY_FILE_ERROR,
895 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
896 _("Key file contains unsupported "
897 "encoding '%s'"), value_utf8);
906 /* Is this key a translation? If so, is it one that we care about?
908 locale = key_get_locale (key);
910 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
911 g_key_file_add_key (key_file, key_file->current_group, key, value);
919 key_get_locale (const gchar *key)
923 locale = g_strrstr (key, "[");
925 if (locale && strlen (locale) <= 2)
929 locale = g_strndup (locale + 1, strlen (locale) - 2);
935 g_key_file_parse_data (GKeyFile *key_file,
943 g_return_if_fail (key_file != NULL);
944 g_return_if_fail (data != NULL);
948 for (i = 0; i < length; i++)
952 if (i > 0 && data[i - 1] == '\r')
953 g_string_erase (key_file->parse_buffer,
954 key_file->parse_buffer->len - 1,
957 /* When a newline is encountered flush the parse buffer so that the
958 * line can be parsed. Note that completely blank lines won't show
959 * up in the parse buffer, so they get parsed directly.
961 if (key_file->parse_buffer->len > 0)
962 g_key_file_flush_parse_buffer (key_file, &parse_error);
964 g_key_file_parse_comment (key_file, "", 1, &parse_error);
968 g_propagate_error (error, parse_error);
973 g_string_append_c (key_file->parse_buffer, data[i]);
976 key_file->approximate_size += length;
980 g_key_file_flush_parse_buffer (GKeyFile *key_file,
983 GError *file_error = NULL;
985 g_return_if_fail (key_file != NULL);
989 if (key_file->parse_buffer->len > 0)
991 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
992 key_file->parse_buffer->len,
994 g_string_erase (key_file->parse_buffer, 0, -1);
998 g_propagate_error (error, file_error);
1005 * g_key_file_to_data:
1006 * @key_file: a #GKeyFile
1007 * @length: return location for the length of the
1008 * returned string, or %NULL
1009 * @error: return location for a #GError, or %NULL
1011 * This function outputs @key_file as a string.
1013 * Note that this function never reports an error,
1014 * so it is safe to pass %NULL as @error.
1016 * Return value: a newly allocated string holding
1017 * the contents of the #GKeyFile
1022 g_key_file_to_data (GKeyFile *key_file,
1026 GString *data_string;
1027 GList *group_node, *key_file_node;
1028 gboolean has_blank_line = TRUE;
1030 g_return_val_if_fail (key_file != NULL, NULL);
1032 data_string = g_string_sized_new (2 * key_file->approximate_size);
1034 for (group_node = g_list_last (key_file->groups);
1036 group_node = group_node->prev)
1038 GKeyFileGroup *group;
1040 group = (GKeyFileGroup *) group_node->data;
1042 /* separate groups by at least an empty line */
1043 if (!has_blank_line)
1044 g_string_append_c (data_string, '\n');
1045 has_blank_line = group->has_trailing_blank_line;
1047 if (group->comment != NULL)
1048 g_string_append_printf (data_string, "%s\n", group->comment->value);
1050 if (group->name != NULL)
1051 g_string_append_printf (data_string, "[%s]\n", group->name);
1053 for (key_file_node = g_list_last (group->key_value_pairs);
1054 key_file_node != NULL;
1055 key_file_node = key_file_node->prev)
1057 GKeyFileKeyValuePair *pair;
1059 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1061 if (pair->key != NULL)
1062 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1064 g_string_append_printf (data_string, "%s\n", pair->value);
1069 *length = data_string->len;
1071 return g_string_free (data_string, FALSE);
1075 * g_key_file_get_keys:
1076 * @key_file: a #GKeyFile
1077 * @group_name: a group name
1078 * @length: return location for the number of keys returned, or %NULL
1079 * @error: return location for a #GError, or %NULL
1081 * Returns all keys for the group name @group_name. The array of
1082 * returned keys will be %NULL-terminated, so @length may
1083 * optionally be %NULL. In the event that the @group_name cannot
1084 * be found, %NULL is returned and @error is set to
1085 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1087 * Return value: a newly-allocated %NULL-terminated array of strings.
1088 * Use g_strfreev() to free it.
1093 g_key_file_get_keys (GKeyFile *key_file,
1094 const gchar *group_name,
1098 GKeyFileGroup *group;
1103 g_return_val_if_fail (key_file != NULL, NULL);
1104 g_return_val_if_fail (group_name != NULL, NULL);
1106 group = g_key_file_lookup_group (key_file, group_name);
1110 g_set_error (error, G_KEY_FILE_ERROR,
1111 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1112 _("Key file does not have group '%s'"),
1113 group_name ? group_name : "(null)");
1118 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1120 GKeyFileKeyValuePair *pair;
1122 pair = (GKeyFileKeyValuePair *) tmp->data;
1128 keys = g_new (gchar *, num_keys + 1);
1131 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1133 GKeyFileKeyValuePair *pair;
1135 pair = (GKeyFileKeyValuePair *) tmp->data;
1139 keys[i] = g_strdup (pair->key);
1144 keys[num_keys] = NULL;
1153 * g_key_file_get_start_group:
1154 * @key_file: a #GKeyFile
1156 * Returns the name of the start group of the file.
1158 * Return value: The start group of the key file.
1163 g_key_file_get_start_group (GKeyFile *key_file)
1165 g_return_val_if_fail (key_file != NULL, NULL);
1167 if (key_file->start_group)
1168 return g_strdup (key_file->start_group->name);
1174 * g_key_file_get_groups:
1175 * @key_file: a #GKeyFile
1176 * @length: return location for the number of returned groups, or %NULL
1178 * Returns all groups in the key file loaded with @key_file.
1179 * The array of returned groups will be %NULL-terminated, so
1180 * @length may optionally be %NULL.
1182 * Return value: a newly-allocated %NULL-terminated array of strings.
1183 * Use g_strfreev() to free it.
1187 g_key_file_get_groups (GKeyFile *key_file,
1192 gsize i, num_groups;
1194 g_return_val_if_fail (key_file != NULL, NULL);
1196 num_groups = g_list_length (key_file->groups);
1198 g_return_val_if_fail (num_groups > 0, NULL);
1200 group_node = g_list_last (key_file->groups);
1202 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1204 /* Only need num_groups instead of num_groups + 1
1205 * because the first group of the file (last in the
1206 * list) is always the comment group at the top,
1209 groups = g_new (gchar *, num_groups);
1213 for (group_node = group_node->prev;
1215 group_node = group_node->prev)
1217 GKeyFileGroup *group;
1219 group = (GKeyFileGroup *) group_node->data;
1221 g_warn_if_fail (group->name != NULL);
1223 groups[i++] = g_strdup (group->name);
1234 * g_key_file_get_value:
1235 * @key_file: a #GKeyFile
1236 * @group_name: a group name
1238 * @error: return location for a #GError, or %NULL
1240 * Returns the value associated with @key under @group_name.
1242 * In the event the key cannot be found, %NULL is returned and
1243 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1244 * event that the @group_name cannot be found, %NULL is returned
1245 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1247 * Return value: a newly allocated string or %NULL if the specified
1248 * key cannot be found.
1253 g_key_file_get_value (GKeyFile *key_file,
1254 const gchar *group_name,
1258 GKeyFileGroup *group;
1259 GKeyFileKeyValuePair *pair;
1260 gchar *value = NULL;
1262 g_return_val_if_fail (key_file != NULL, NULL);
1263 g_return_val_if_fail (group_name != NULL, NULL);
1264 g_return_val_if_fail (key != NULL, NULL);
1266 group = g_key_file_lookup_group (key_file, group_name);
1270 g_set_error (error, G_KEY_FILE_ERROR,
1271 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1272 _("Key file does not have group '%s'"),
1273 group_name ? group_name : "(null)");
1277 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1280 value = g_strdup (pair->value);
1282 g_set_error (error, G_KEY_FILE_ERROR,
1283 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1284 _("Key file does not have key '%s'"), key);
1290 * g_key_file_set_value:
1291 * @key_file: a #GKeyFile
1292 * @group_name: a group name
1296 * Associates a new value with @key under @group_name.
1297 * If @key cannot be found then it is created.
1298 * If @group_name cannot be found then it is created.
1303 g_key_file_set_value (GKeyFile *key_file,
1304 const gchar *group_name,
1308 GKeyFileGroup *group;
1309 GKeyFileKeyValuePair *pair;
1311 g_return_if_fail (key_file != NULL);
1312 g_return_if_fail (g_key_file_is_group_name (group_name));
1313 g_return_if_fail (g_key_file_is_key_name (key));
1314 g_return_if_fail (value != NULL);
1316 group = g_key_file_lookup_group (key_file, group_name);
1320 g_key_file_add_group (key_file, group_name);
1321 group = (GKeyFileGroup *) key_file->groups->data;
1323 g_key_file_add_key (key_file, group, key, value);
1327 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1330 g_key_file_add_key (key_file, group, key, value);
1333 g_free (pair->value);
1334 pair->value = g_strdup (value);
1340 * g_key_file_get_string:
1341 * @key_file: a #GKeyFile
1342 * @group_name: a group name
1344 * @error: return location for a #GError, or %NULL
1346 * Returns the value associated with @key under @group_name.
1348 * In the event the key cannot be found, %NULL is returned and
1349 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1350 * event that the @group_name cannot be found, %NULL is returned
1351 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1353 * Return value: a newly allocated string or %NULL if the specified
1354 * key cannot be found.
1359 g_key_file_get_string (GKeyFile *key_file,
1360 const gchar *group_name,
1364 gchar *value, *string_value;
1365 GError *key_file_error;
1367 g_return_val_if_fail (key_file != NULL, NULL);
1368 g_return_val_if_fail (group_name != NULL, NULL);
1369 g_return_val_if_fail (key != NULL, NULL);
1371 key_file_error = NULL;
1373 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1377 g_propagate_error (error, key_file_error);
1381 if (!g_utf8_validate (value, -1, NULL))
1383 gchar *value_utf8 = _g_utf8_make_valid (value);
1384 g_set_error (error, G_KEY_FILE_ERROR,
1385 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1386 _("Key file contains key '%s' with value '%s' "
1387 "which is not UTF-8"), key, value_utf8);
1388 g_free (value_utf8);
1394 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1400 if (g_error_matches (key_file_error,
1402 G_KEY_FILE_ERROR_INVALID_VALUE))
1404 g_set_error (error, G_KEY_FILE_ERROR,
1405 G_KEY_FILE_ERROR_INVALID_VALUE,
1406 _("Key file contains key '%s' "
1407 "which has value that cannot be interpreted."),
1409 g_error_free (key_file_error);
1412 g_propagate_error (error, key_file_error);
1415 return string_value;
1419 * g_key_file_set_string:
1420 * @key_file: a #GKeyFile
1421 * @group_name: a group name
1425 * Associates a new string value with @key under @group_name.
1426 * If @key cannot be found then it is created.
1427 * If @group_name cannot be found then it is created.
1432 g_key_file_set_string (GKeyFile *key_file,
1433 const gchar *group_name,
1435 const gchar *string)
1439 g_return_if_fail (key_file != NULL);
1440 g_return_if_fail (string != NULL);
1442 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1443 g_key_file_set_value (key_file, group_name, key, value);
1448 * g_key_file_get_string_list:
1449 * @key_file: a #GKeyFile
1450 * @group_name: a group name
1452 * @length: return location for the number of returned strings, or %NULL
1453 * @error: return location for a #GError, or %NULL
1455 * Returns the values associated with @key under @group_name.
1457 * In the event the key cannot be found, %NULL is returned and
1458 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1459 * event that the @group_name cannot be found, %NULL is returned
1460 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1462 * Return value: a %NULL-terminated string array or %NULL if the specified
1463 * key cannot be found. The array should be freed with g_strfreev().
1468 g_key_file_get_string_list (GKeyFile *key_file,
1469 const gchar *group_name,
1474 GError *key_file_error = NULL;
1475 gchar *value, *string_value, **values;
1477 GSList *p, *pieces = NULL;
1479 g_return_val_if_fail (key_file != NULL, NULL);
1480 g_return_val_if_fail (group_name != NULL, NULL);
1481 g_return_val_if_fail (key != NULL, NULL);
1486 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1490 g_propagate_error (error, key_file_error);
1494 if (!g_utf8_validate (value, -1, NULL))
1496 gchar *value_utf8 = _g_utf8_make_valid (value);
1497 g_set_error (error, G_KEY_FILE_ERROR,
1498 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1499 _("Key file contains key '%s' with value '%s' "
1500 "which is not UTF-8"), key, value_utf8);
1501 g_free (value_utf8);
1507 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1509 g_free (string_value);
1513 if (g_error_matches (key_file_error,
1515 G_KEY_FILE_ERROR_INVALID_VALUE))
1517 g_set_error (error, G_KEY_FILE_ERROR,
1518 G_KEY_FILE_ERROR_INVALID_VALUE,
1519 _("Key file contains key '%s' "
1520 "which has value that cannot be interpreted."),
1522 g_error_free (key_file_error);
1525 g_propagate_error (error, key_file_error);
1530 len = g_slist_length (pieces);
1531 values = g_new (gchar *, len + 1);
1532 for (p = pieces, i = 0; p; p = p->next)
1533 values[i++] = p->data;
1536 g_slist_free (pieces);
1545 * g_key_file_set_string_list:
1546 * @key_file: a #GKeyFile
1547 * @group_name: a group name
1549 * @list: an array of locale string values
1550 * @length: number of locale string values in @list
1552 * Associates a list of string values for @key under @group_name.
1553 * If @key cannot be found then it is created.
1554 * If @group_name cannot be found then it is created.
1559 g_key_file_set_string_list (GKeyFile *key_file,
1560 const gchar *group_name,
1562 const gchar * const list[],
1565 GString *value_list;
1568 g_return_if_fail (key_file != NULL);
1569 g_return_if_fail (list != NULL);
1571 value_list = g_string_sized_new (length * 128);
1572 for (i = 0; i < length && list[i] != NULL; i++)
1576 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1577 g_string_append (value_list, value);
1578 g_string_append_c (value_list, key_file->list_separator);
1583 g_key_file_set_value (key_file, group_name, key, value_list->str);
1584 g_string_free (value_list, TRUE);
1588 * g_key_file_set_locale_string:
1589 * @key_file: a #GKeyFile
1590 * @group_name: a group name
1595 * Associates a string value for @key and @locale under @group_name.
1596 * If the translation for @key cannot be found then it is created.
1601 g_key_file_set_locale_string (GKeyFile *key_file,
1602 const gchar *group_name,
1604 const gchar *locale,
1605 const gchar *string)
1607 gchar *full_key, *value;
1609 g_return_if_fail (key_file != NULL);
1610 g_return_if_fail (key != NULL);
1611 g_return_if_fail (locale != NULL);
1612 g_return_if_fail (string != NULL);
1614 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1615 full_key = g_strdup_printf ("%s[%s]", key, locale);
1616 g_key_file_set_value (key_file, group_name, full_key, value);
1621 extern GSList *_g_compute_locale_variants (const gchar *locale);
1624 * g_key_file_get_locale_string:
1625 * @key_file: a #GKeyFile
1626 * @group_name: a group name
1628 * @locale: a locale or %NULL
1629 * @error: return location for a #GError, or %NULL
1631 * Returns the value associated with @key under @group_name
1632 * translated in the given @locale if available. If @locale is
1633 * %NULL then the current locale is assumed.
1635 * If @key cannot be found then %NULL is returned and @error is set
1636 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1637 * with @key cannot be interpreted or no suitable translation can
1638 * be found then the untranslated value is returned.
1640 * Return value: a newly allocated string or %NULL if the specified
1641 * key cannot be found.
1646 g_key_file_get_locale_string (GKeyFile *key_file,
1647 const gchar *group_name,
1649 const gchar *locale,
1652 gchar *candidate_key, *translated_value;
1653 GError *key_file_error;
1655 gboolean free_languages = FALSE;
1658 g_return_val_if_fail (key_file != NULL, NULL);
1659 g_return_val_if_fail (group_name != NULL, NULL);
1660 g_return_val_if_fail (key != NULL, NULL);
1662 candidate_key = NULL;
1663 translated_value = NULL;
1664 key_file_error = NULL;
1670 list = _g_compute_locale_variants (locale);
1672 languages = g_new (gchar *, g_slist_length (list) + 1);
1673 for (l = list, i = 0; l; l = l->next, i++)
1674 languages[i] = l->data;
1675 languages[i] = NULL;
1677 g_slist_free (list);
1678 free_languages = TRUE;
1682 languages = (gchar **) g_get_language_names ();
1683 free_languages = FALSE;
1686 for (i = 0; languages[i]; i++)
1688 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1690 translated_value = g_key_file_get_string (key_file,
1692 candidate_key, NULL);
1693 g_free (candidate_key);
1695 if (translated_value)
1698 g_free (translated_value);
1699 translated_value = NULL;
1702 /* Fallback to untranslated key
1704 if (!translated_value)
1706 translated_value = g_key_file_get_string (key_file, group_name, key,
1709 if (!translated_value)
1710 g_propagate_error (error, key_file_error);
1714 g_strfreev (languages);
1716 return translated_value;
1720 * g_key_file_get_locale_string_list:
1721 * @key_file: a #GKeyFile
1722 * @group_name: a group name
1725 * @length: return location for the number of returned strings or %NULL
1726 * @error: return location for a #GError or %NULL
1728 * Returns the values associated with @key under @group_name
1729 * translated in the given @locale if available. If @locale is
1730 * %NULL then the current locale is assumed.
1732 * If @key cannot be found then %NULL is returned and @error is set
1733 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1734 * with @key cannot be interpreted or no suitable translations
1735 * can be found then the untranslated values are returned. The
1736 * returned array is %NULL-terminated, so @length may optionally
1739 * Return value: a newly allocated %NULL-terminated string array
1740 * or %NULL if the key isn't found. The string array should be freed
1741 * with g_strfreev().
1746 g_key_file_get_locale_string_list (GKeyFile *key_file,
1747 const gchar *group_name,
1749 const gchar *locale,
1753 GError *key_file_error;
1754 gchar **values, *value;
1756 g_return_val_if_fail (key_file != NULL, NULL);
1757 g_return_val_if_fail (group_name != NULL, NULL);
1758 g_return_val_if_fail (key != NULL, NULL);
1760 key_file_error = NULL;
1762 value = g_key_file_get_locale_string (key_file, group_name,
1767 g_propagate_error (error, key_file_error);
1776 if (value[strlen (value) - 1] == ';')
1777 value[strlen (value) - 1] = '\0';
1779 values = g_strsplit (value, ";", 0);
1784 *length = g_strv_length (values);
1790 * g_key_file_set_locale_string_list:
1791 * @key_file: a #GKeyFile
1792 * @group_name: a group name
1795 * @list: a %NULL-terminated array of locale string values
1796 * @length: the length of @list
1798 * Associates a list of string values for @key and @locale under
1799 * @group_name. If the translation for @key cannot be found then
1805 g_key_file_set_locale_string_list (GKeyFile *key_file,
1806 const gchar *group_name,
1808 const gchar *locale,
1809 const gchar * const list[],
1812 GString *value_list;
1816 g_return_if_fail (key_file != NULL);
1817 g_return_if_fail (key != NULL);
1818 g_return_if_fail (locale != NULL);
1819 g_return_if_fail (length != 0);
1821 value_list = g_string_sized_new (length * 128);
1822 for (i = 0; i < length && list[i] != NULL; i++)
1826 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1828 g_string_append (value_list, value);
1829 g_string_append_c (value_list, ';');
1834 full_key = g_strdup_printf ("%s[%s]", key, locale);
1835 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1837 g_string_free (value_list, TRUE);
1841 * g_key_file_get_boolean:
1842 * @key_file: a #GKeyFile
1843 * @group_name: a group name
1845 * @error: return location for a #GError
1847 * Returns the value associated with @key under @group_name as a
1850 * If @key cannot be found then %FALSE is returned and @error is set
1851 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1852 * associated with @key cannot be interpreted as a boolean then %FALSE
1853 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1855 * Return value: the value associated with the key as a boolean,
1856 * or %FALSE if the key was not found or could not be parsed.
1861 g_key_file_get_boolean (GKeyFile *key_file,
1862 const gchar *group_name,
1866 GError *key_file_error = NULL;
1868 gboolean bool_value;
1870 g_return_val_if_fail (key_file != NULL, FALSE);
1871 g_return_val_if_fail (group_name != NULL, FALSE);
1872 g_return_val_if_fail (key != NULL, FALSE);
1874 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1878 g_propagate_error (error, key_file_error);
1882 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1888 if (g_error_matches (key_file_error,
1890 G_KEY_FILE_ERROR_INVALID_VALUE))
1892 g_set_error (error, G_KEY_FILE_ERROR,
1893 G_KEY_FILE_ERROR_INVALID_VALUE,
1894 _("Key file contains key '%s' "
1895 "which has value that cannot be interpreted."),
1897 g_error_free (key_file_error);
1900 g_propagate_error (error, key_file_error);
1907 * g_key_file_set_boolean:
1908 * @key_file: a #GKeyFile
1909 * @group_name: a group name
1911 * @value: %TRUE or %FALSE
1913 * Associates a new boolean value with @key under @group_name.
1914 * If @key cannot be found then it is created.
1919 g_key_file_set_boolean (GKeyFile *key_file,
1920 const gchar *group_name,
1926 g_return_if_fail (key_file != NULL);
1928 result = g_key_file_parse_boolean_as_value (key_file, value);
1929 g_key_file_set_value (key_file, group_name, key, result);
1934 * g_key_file_get_boolean_list:
1935 * @key_file: a #GKeyFile
1936 * @group_name: a group name
1938 * @length: the number of booleans returned
1939 * @error: return location for a #GError
1941 * Returns the values associated with @key under @group_name as
1944 * If @key cannot be found then %NULL is returned and @error is set to
1945 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1946 * with @key cannot be interpreted as booleans then %NULL is returned
1947 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1949 * Return value: the values associated with the key as a list of
1950 * booleans, or %NULL if the key was not found or could not be parsed.
1955 g_key_file_get_boolean_list (GKeyFile *key_file,
1956 const gchar *group_name,
1961 GError *key_file_error;
1963 gboolean *bool_values;
1966 g_return_val_if_fail (key_file != NULL, NULL);
1967 g_return_val_if_fail (group_name != NULL, NULL);
1968 g_return_val_if_fail (key != NULL, NULL);
1973 key_file_error = NULL;
1975 values = g_key_file_get_string_list (key_file, group_name, key,
1976 &num_bools, &key_file_error);
1979 g_propagate_error (error, key_file_error);
1984 bool_values = g_new (gboolean, num_bools);
1986 for (i = 0; i < num_bools; i++)
1988 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1994 g_propagate_error (error, key_file_error);
1995 g_strfreev (values);
1996 g_free (bool_values);
2001 g_strfreev (values);
2004 *length = num_bools;
2010 * g_key_file_set_boolean_list:
2011 * @key_file: a #GKeyFile
2012 * @group_name: a group name
2014 * @list: an array of boolean values
2015 * @length: length of @list
2017 * Associates a list of boolean values with @key under @group_name.
2018 * If @key cannot be found then it is created.
2019 * If @group_name is %NULL, the start_group is used.
2024 g_key_file_set_boolean_list (GKeyFile *key_file,
2025 const gchar *group_name,
2030 GString *value_list;
2033 g_return_if_fail (key_file != NULL);
2034 g_return_if_fail (list != NULL);
2036 value_list = g_string_sized_new (length * 8);
2037 for (i = 0; i < length; i++)
2041 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2043 g_string_append (value_list, value);
2044 g_string_append_c (value_list, key_file->list_separator);
2049 g_key_file_set_value (key_file, group_name, key, value_list->str);
2050 g_string_free (value_list, TRUE);
2054 * g_key_file_get_integer:
2055 * @key_file: a #GKeyFile
2056 * @group_name: a group name
2058 * @error: return location for a #GError
2060 * Returns the value associated with @key under @group_name as an
2063 * If @key cannot be found then 0 is returned and @error is set to
2064 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2065 * with @key cannot be interpreted as an integer then 0 is returned
2066 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2068 * Return value: the value associated with the key as an integer, or
2069 * 0 if the key was not found or could not be parsed.
2074 g_key_file_get_integer (GKeyFile *key_file,
2075 const gchar *group_name,
2079 GError *key_file_error;
2083 g_return_val_if_fail (key_file != NULL, -1);
2084 g_return_val_if_fail (group_name != NULL, -1);
2085 g_return_val_if_fail (key != NULL, -1);
2087 key_file_error = NULL;
2089 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2093 g_propagate_error (error, key_file_error);
2097 int_value = g_key_file_parse_value_as_integer (key_file, value,
2103 if (g_error_matches (key_file_error,
2105 G_KEY_FILE_ERROR_INVALID_VALUE))
2107 g_set_error (error, G_KEY_FILE_ERROR,
2108 G_KEY_FILE_ERROR_INVALID_VALUE,
2109 _("Key file contains key '%s' in group '%s' "
2110 "which has value that cannot be interpreted."), key,
2112 g_error_free (key_file_error);
2115 g_propagate_error (error, key_file_error);
2122 * g_key_file_set_integer:
2123 * @key_file: a #GKeyFile
2124 * @group_name: a group name
2126 * @value: an integer value
2128 * Associates a new integer value with @key under @group_name.
2129 * If @key cannot be found then it is created.
2134 g_key_file_set_integer (GKeyFile *key_file,
2135 const gchar *group_name,
2141 g_return_if_fail (key_file != NULL);
2143 result = g_key_file_parse_integer_as_value (key_file, value);
2144 g_key_file_set_value (key_file, group_name, key, result);
2149 * g_key_file_get_integer_list:
2150 * @key_file: a #GKeyFile
2151 * @group_name: a group name
2153 * @length: the number of integers returned
2154 * @error: return location for a #GError
2156 * Returns the values associated with @key under @group_name as
2159 * If @key cannot be found then %NULL is returned and @error is set to
2160 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2161 * with @key cannot be interpreted as integers then %NULL is returned
2162 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2164 * Return value: the values associated with the key as a list of
2165 * integers, or %NULL if the key was not found or could not be parsed.
2170 g_key_file_get_integer_list (GKeyFile *key_file,
2171 const gchar *group_name,
2176 GError *key_file_error = NULL;
2181 g_return_val_if_fail (key_file != NULL, NULL);
2182 g_return_val_if_fail (group_name != NULL, NULL);
2183 g_return_val_if_fail (key != NULL, NULL);
2188 values = g_key_file_get_string_list (key_file, group_name, key,
2189 &num_ints, &key_file_error);
2192 g_propagate_error (error, key_file_error);
2197 int_values = g_new (gint, num_ints);
2199 for (i = 0; i < num_ints; i++)
2201 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2207 g_propagate_error (error, key_file_error);
2208 g_strfreev (values);
2209 g_free (int_values);
2214 g_strfreev (values);
2223 * g_key_file_set_integer_list:
2224 * @key_file: a #GKeyFile
2225 * @group_name: a group name
2227 * @list: an array of integer values
2228 * @length: number of integer values in @list
2230 * Associates a list of integer values with @key under @group_name.
2231 * If @key cannot be found then it is created.
2236 g_key_file_set_integer_list (GKeyFile *key_file,
2237 const gchar *group_name,
2245 g_return_if_fail (key_file != NULL);
2246 g_return_if_fail (list != NULL);
2248 values = g_string_sized_new (length * 16);
2249 for (i = 0; i < length; i++)
2253 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2255 g_string_append (values, value);
2256 g_string_append_c (values, ';');
2261 g_key_file_set_value (key_file, group_name, key, values->str);
2262 g_string_free (values, TRUE);
2266 * g_key_file_get_double:
2267 * @key_file: a #GKeyFile
2268 * @group_name: a group name
2270 * @error: return location for a #GError
2272 * Returns the value associated with @key under @group_name as a
2273 * double. If @group_name is %NULL, the start_group is used.
2275 * If @key cannot be found then 0.0 is returned and @error is set to
2276 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2277 * with @key cannot be interpreted as a double then 0.0 is returned
2278 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2280 * Return value: the value associated with the key as a double, or
2281 * 0.0 if the key was not found or could not be parsed.
2286 g_key_file_get_double (GKeyFile *key_file,
2287 const gchar *group_name,
2291 GError *key_file_error;
2293 gdouble double_value;
2295 g_return_val_if_fail (key_file != NULL, -1);
2296 g_return_val_if_fail (group_name != NULL, -1);
2297 g_return_val_if_fail (key != NULL, -1);
2299 key_file_error = NULL;
2301 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2305 g_propagate_error (error, key_file_error);
2309 double_value = g_key_file_parse_value_as_double (key_file, value,
2315 if (g_error_matches (key_file_error,
2317 G_KEY_FILE_ERROR_INVALID_VALUE))
2319 g_set_error (error, G_KEY_FILE_ERROR,
2320 G_KEY_FILE_ERROR_INVALID_VALUE,
2321 _("Key file contains key '%s' in group '%s' "
2322 "which has value that cannot be interpreted."), key,
2324 g_error_free (key_file_error);
2327 g_propagate_error (error, key_file_error);
2330 return double_value;
2334 * g_key_file_set_double:
2335 * @key_file: a #GKeyFile
2336 * @group_name: a group name
2338 * @value: an double value
2340 * Associates a new double value with @key under @group_name.
2341 * If @key cannot be found then it is created.
2346 g_key_file_set_double (GKeyFile *key_file,
2347 const gchar *group_name,
2351 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2353 g_return_if_fail (key_file != NULL);
2355 g_ascii_dtostr (result, sizeof (result), value);
2356 g_key_file_set_value (key_file, group_name, key, result);
2360 * g_key_file_get_double_list:
2361 * @key_file: a #GKeyFile
2362 * @group_name: a group name
2364 * @length: the number of doubles returned
2365 * @error: return location for a #GError
2367 * Returns the values associated with @key under @group_name as
2370 * If @key cannot be found then %NULL is returned and @error is set to
2371 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2372 * with @key cannot be interpreted as doubles then %NULL is returned
2373 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2375 * Return value: the values associated with the key as a list of
2376 * doubles, or %NULL if the key was not found or could not be parsed.
2381 g_key_file_get_double_list (GKeyFile *key_file,
2382 const gchar *group_name,
2387 GError *key_file_error = NULL;
2389 gdouble *double_values;
2390 gsize i, num_doubles;
2392 g_return_val_if_fail (key_file != NULL, NULL);
2393 g_return_val_if_fail (group_name != NULL, NULL);
2394 g_return_val_if_fail (key != NULL, NULL);
2399 values = g_key_file_get_string_list (key_file, group_name, key,
2400 &num_doubles, &key_file_error);
2403 g_propagate_error (error, key_file_error);
2408 double_values = g_new (gdouble, num_doubles);
2410 for (i = 0; i < num_doubles; i++)
2412 double_values[i] = g_key_file_parse_value_as_double (key_file,
2418 g_propagate_error (error, key_file_error);
2419 g_strfreev (values);
2420 g_free (double_values);
2425 g_strfreev (values);
2428 *length = num_doubles;
2430 return double_values;
2434 * g_key_file_set_double_list:
2435 * @key_file: a #GKeyFile
2436 * @group_name: a group name
2438 * @list: an array of double values
2439 * @length: number of double values in @list
2441 * Associates a list of double values with @key under
2442 * @group_name. If @key cannot be found then it is created.
2447 g_key_file_set_double_list (GKeyFile *key_file,
2448 const gchar *group_name,
2456 g_return_if_fail (key_file != NULL);
2457 g_return_if_fail (list != NULL);
2459 values = g_string_sized_new (length * 16);
2460 for (i = 0; i < length; i++)
2462 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2464 g_ascii_dtostr( result, sizeof (result), list[i] );
2466 g_string_append (values, result);
2467 g_string_append_c (values, ';');
2470 g_key_file_set_value (key_file, group_name, key, values->str);
2471 g_string_free (values, TRUE);
2475 g_key_file_set_key_comment (GKeyFile *key_file,
2476 const gchar *group_name,
2478 const gchar *comment,
2481 GKeyFileGroup *group;
2482 GKeyFileKeyValuePair *pair;
2483 GList *key_node, *comment_node, *tmp;
2485 group = g_key_file_lookup_group (key_file, group_name);
2488 g_set_error (error, G_KEY_FILE_ERROR,
2489 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2490 _("Key file does not have group '%s'"),
2491 group_name ? group_name : "(null)");
2496 /* First find the key the comments are supposed to be
2499 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2501 if (key_node == NULL)
2503 g_set_error (error, G_KEY_FILE_ERROR,
2504 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2505 _("Key file does not have key '%s' in group '%s'"),
2510 /* Then find all the comments already associated with the
2513 tmp = key_node->next;
2516 GKeyFileKeyValuePair *pair;
2518 pair = (GKeyFileKeyValuePair *) tmp->data;
2520 if (pair->key != NULL)
2525 g_key_file_remove_key_value_pair_node (key_file, group,
2529 if (comment == NULL)
2532 /* Now we can add our new comment
2534 pair = g_slice_new (GKeyFileKeyValuePair);
2536 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2538 key_node = g_list_insert (key_node, pair, 1);
2544 g_key_file_set_group_comment (GKeyFile *key_file,
2545 const gchar *group_name,
2546 const gchar *comment,
2549 GKeyFileGroup *group;
2551 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2553 group = g_key_file_lookup_group (key_file, group_name);
2556 g_set_error (error, G_KEY_FILE_ERROR,
2557 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2558 _("Key file does not have group '%s'"),
2559 group_name ? group_name : "(null)");
2564 /* First remove any existing comment
2568 g_key_file_key_value_pair_free (group->comment);
2569 group->comment = NULL;
2572 if (comment == NULL)
2575 /* Now we can add our new comment
2577 group->comment = g_slice_new (GKeyFileKeyValuePair);
2578 group->comment->key = NULL;
2579 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2585 g_key_file_set_top_comment (GKeyFile *key_file,
2586 const gchar *comment,
2590 GKeyFileGroup *group;
2591 GKeyFileKeyValuePair *pair;
2593 /* The last group in the list should be the top (comments only)
2596 g_warn_if_fail (key_file->groups != NULL);
2597 group_node = g_list_last (key_file->groups);
2598 group = (GKeyFileGroup *) group_node->data;
2599 g_warn_if_fail (group->name == NULL);
2601 /* Note all keys must be comments at the top of
2602 * the file, so we can just free it all.
2604 if (group->key_value_pairs != NULL)
2606 g_list_foreach (group->key_value_pairs,
2607 (GFunc) g_key_file_key_value_pair_free,
2609 g_list_free (group->key_value_pairs);
2610 group->key_value_pairs = NULL;
2613 if (comment == NULL)
2616 pair = g_slice_new (GKeyFileKeyValuePair);
2618 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2620 group->key_value_pairs =
2621 g_list_prepend (group->key_value_pairs, pair);
2627 * g_key_file_set_comment:
2628 * @key_file: a #GKeyFile
2629 * @group_name: a group name, or %NULL
2631 * @comment: a comment
2632 * @error: return location for a #GError
2634 * Places a comment above @key from @group_name.
2635 * If @key is %NULL then @comment will be written above @group_name.
2636 * If both @key and @group_name are %NULL, then @comment will be
2637 * written above the first group in the file.
2639 * Returns: %TRUE if the comment was written, %FALSE otherwise
2644 g_key_file_set_comment (GKeyFile *key_file,
2645 const gchar *group_name,
2647 const gchar *comment,
2650 g_return_val_if_fail (key_file != NULL, FALSE);
2652 if (group_name != NULL && key != NULL)
2654 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2657 else if (group_name != NULL)
2659 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2664 if (!g_key_file_set_top_comment (key_file, comment, error))
2668 if (comment != NULL)
2669 key_file->approximate_size += strlen (comment);
2675 g_key_file_get_key_comment (GKeyFile *key_file,
2676 const gchar *group_name,
2680 GKeyFileGroup *group;
2681 GKeyFileKeyValuePair *pair;
2682 GList *key_node, *tmp;
2686 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2688 group = g_key_file_lookup_group (key_file, group_name);
2691 g_set_error (error, G_KEY_FILE_ERROR,
2692 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2693 _("Key file does not have group '%s'"),
2694 group_name ? group_name : "(null)");
2699 /* First find the key the comments are supposed to be
2702 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2704 if (key_node == NULL)
2706 g_set_error (error, G_KEY_FILE_ERROR,
2707 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2708 _("Key file does not have key '%s' in group '%s'"),
2715 /* Then find all the comments already associated with the
2716 * key and concatentate them.
2718 tmp = key_node->next;
2719 if (!key_node->next)
2722 pair = (GKeyFileKeyValuePair *) tmp->data;
2723 if (pair->key != NULL)
2728 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2730 if (pair->key != NULL)
2736 while (tmp != key_node)
2738 GKeyFileKeyValuePair *pair;
2740 pair = (GKeyFileKeyValuePair *) tmp->data;
2743 string = g_string_sized_new (512);
2745 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2746 g_string_append (string, comment);
2754 comment = string->str;
2755 g_string_free (string, FALSE);
2764 get_group_comment (GKeyFile *key_file,
2765 GKeyFileGroup *group,
2774 tmp = group->key_value_pairs;
2777 GKeyFileKeyValuePair *pair;
2779 pair = (GKeyFileKeyValuePair *) tmp->data;
2781 if (pair->key != NULL)
2787 if (tmp->next == NULL)
2795 GKeyFileKeyValuePair *pair;
2797 pair = (GKeyFileKeyValuePair *) tmp->data;
2800 string = g_string_sized_new (512);
2802 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2803 g_string_append (string, comment);
2810 return g_string_free (string, FALSE);
2816 g_key_file_get_group_comment (GKeyFile *key_file,
2817 const gchar *group_name,
2821 GKeyFileGroup *group;
2823 group = g_key_file_lookup_group (key_file, group_name);
2826 g_set_error (error, G_KEY_FILE_ERROR,
2827 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2828 _("Key file does not have group '%s'"),
2829 group_name ? group_name : "(null)");
2835 return g_strdup (group->comment->value);
2837 group_node = g_key_file_lookup_group_node (key_file, group_name);
2838 group_node = group_node->next;
2839 group = (GKeyFileGroup *)group_node->data;
2840 return get_group_comment (key_file, group, error);
2844 g_key_file_get_top_comment (GKeyFile *key_file,
2848 GKeyFileGroup *group;
2850 /* The last group in the list should be the top (comments only)
2853 g_warn_if_fail (key_file->groups != NULL);
2854 group_node = g_list_last (key_file->groups);
2855 group = (GKeyFileGroup *) group_node->data;
2856 g_warn_if_fail (group->name == NULL);
2858 return get_group_comment (key_file, group, error);
2862 * g_key_file_get_comment:
2863 * @key_file: a #GKeyFile
2864 * @group_name: a group name, or %NULL
2866 * @error: return location for a #GError
2868 * Retrieves a comment above @key from @group_name.
2869 * If @key is %NULL then @comment will be read from above
2870 * @group_name. If both @key and @group_name are %NULL, then
2871 * @comment will be read from above the first group in the file.
2873 * Returns: a comment that should be freed with g_free()
2878 g_key_file_get_comment (GKeyFile *key_file,
2879 const gchar *group_name,
2883 g_return_val_if_fail (key_file != NULL, NULL);
2885 if (group_name != NULL && key != NULL)
2886 return g_key_file_get_key_comment (key_file, group_name, key, error);
2887 else if (group_name != NULL)
2888 return g_key_file_get_group_comment (key_file, group_name, error);
2890 return g_key_file_get_top_comment (key_file, error);
2894 * g_key_file_remove_comment:
2895 * @key_file: a #GKeyFile
2896 * @group_name: a group name, or %NULL
2898 * @error: return location for a #GError
2900 * Removes a comment above @key from @group_name.
2901 * If @key is %NULL then @comment will be removed above @group_name.
2902 * If both @key and @group_name are %NULL, then @comment will
2903 * be removed above the first group in the file.
2905 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2911 g_key_file_remove_comment (GKeyFile *key_file,
2912 const gchar *group_name,
2916 g_return_val_if_fail (key_file != NULL, FALSE);
2918 if (group_name != NULL && key != NULL)
2919 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2920 else if (group_name != NULL)
2921 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2923 return g_key_file_set_top_comment (key_file, NULL, error);
2927 * g_key_file_has_group:
2928 * @key_file: a #GKeyFile
2929 * @group_name: a group name
2931 * Looks whether the key file has the group @group_name.
2933 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2938 g_key_file_has_group (GKeyFile *key_file,
2939 const gchar *group_name)
2941 g_return_val_if_fail (key_file != NULL, FALSE);
2942 g_return_val_if_fail (group_name != NULL, FALSE);
2944 return g_key_file_lookup_group (key_file, group_name) != NULL;
2948 * g_key_file_has_key:
2949 * @key_file: a #GKeyFile
2950 * @group_name: a group name
2952 * @error: return location for a #GError
2954 * Looks whether the key file has the key @key in the group
2957 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2963 g_key_file_has_key (GKeyFile *key_file,
2964 const gchar *group_name,
2968 GKeyFileKeyValuePair *pair;
2969 GKeyFileGroup *group;
2971 g_return_val_if_fail (key_file != NULL, FALSE);
2972 g_return_val_if_fail (group_name != NULL, FALSE);
2973 g_return_val_if_fail (key != NULL, FALSE);
2975 group = g_key_file_lookup_group (key_file, group_name);
2979 g_set_error (error, G_KEY_FILE_ERROR,
2980 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2981 _("Key file does not have group '%s'"),
2982 group_name ? group_name : "(null)");
2987 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2989 return pair != NULL;
2993 g_key_file_add_group (GKeyFile *key_file,
2994 const gchar *group_name)
2996 GKeyFileGroup *group;
2998 g_return_if_fail (key_file != NULL);
2999 g_return_if_fail (g_key_file_is_group_name (group_name));
3001 group = g_key_file_lookup_group (key_file, group_name);
3004 key_file->current_group = group;
3008 group = g_slice_new0 (GKeyFileGroup);
3009 group->name = g_strdup (group_name);
3010 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3011 key_file->groups = g_list_prepend (key_file->groups, group);
3012 key_file->approximate_size += strlen (group_name) + 3;
3013 key_file->current_group = group;
3015 if (key_file->start_group == NULL)
3016 key_file->start_group = group;
3018 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3022 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3027 g_free (pair->value);
3028 g_slice_free (GKeyFileKeyValuePair, pair);
3032 /* Be careful not to call this function on a node with data in the
3033 * lookup map without removing it from the lookup map, first.
3035 * Some current cases where this warning is not a concern are
3037 * - the node being removed is a comment node
3038 * - the entire lookup map is getting destroyed soon after
3042 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3043 GKeyFileGroup *group,
3047 GKeyFileKeyValuePair *pair;
3049 pair = (GKeyFileKeyValuePair *) pair_node->data;
3051 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3053 if (pair->key != NULL)
3054 key_file->approximate_size -= strlen (pair->key) + 1;
3056 g_warn_if_fail (pair->value != NULL);
3057 key_file->approximate_size -= strlen (pair->value);
3059 g_key_file_key_value_pair_free (pair);
3061 g_list_free_1 (pair_node);
3065 g_key_file_remove_group_node (GKeyFile *key_file,
3068 GKeyFileGroup *group;
3071 group = (GKeyFileGroup *) group_node->data;
3074 g_hash_table_remove (key_file->group_hash, group->name);
3076 /* If the current group gets deleted make the current group the last
3079 if (key_file->current_group == group)
3081 /* groups should always contain at least the top comment group,
3082 * unless g_key_file_clear has been called
3084 if (key_file->groups)
3085 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3087 key_file->current_group = NULL;
3090 /* If the start group gets deleted make the start group the first
3093 if (key_file->start_group == group)
3095 tmp = g_list_last (key_file->groups);
3098 if (tmp != group_node &&
3099 ((GKeyFileGroup *) tmp->data)->name != NULL)
3106 key_file->start_group = (GKeyFileGroup *) tmp->data;
3108 key_file->start_group = NULL;
3111 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3113 if (group->name != NULL)
3114 key_file->approximate_size -= strlen (group->name) + 3;
3116 tmp = group->key_value_pairs;
3123 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3126 g_warn_if_fail (group->key_value_pairs == NULL);
3128 if (group->lookup_map)
3130 g_hash_table_destroy (group->lookup_map);
3131 group->lookup_map = NULL;
3134 g_free ((gchar *) group->name);
3135 g_slice_free (GKeyFileGroup, group);
3136 g_list_free_1 (group_node);
3140 * g_key_file_remove_group:
3141 * @key_file: a #GKeyFile
3142 * @group_name: a group name
3143 * @error: return location for a #GError or %NULL
3145 * Removes the specified group, @group_name,
3146 * from the key file.
3148 * Returns: %TRUE if the group was removed, %FALSE otherwise
3153 g_key_file_remove_group (GKeyFile *key_file,
3154 const gchar *group_name,
3159 g_return_val_if_fail (key_file != NULL, FALSE);
3160 g_return_val_if_fail (group_name != NULL, FALSE);
3162 group_node = g_key_file_lookup_group_node (key_file, group_name);
3166 g_set_error (error, G_KEY_FILE_ERROR,
3167 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3168 _("Key file does not have group '%s'"),
3173 g_key_file_remove_group_node (key_file, group_node);
3179 g_key_file_add_key (GKeyFile *key_file,
3180 GKeyFileGroup *group,
3184 GKeyFileKeyValuePair *pair;
3186 pair = g_slice_new (GKeyFileKeyValuePair);
3187 pair->key = g_strdup (key);
3188 pair->value = g_strdup (value);
3190 g_hash_table_replace (group->lookup_map, pair->key, pair);
3191 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3192 group->has_trailing_blank_line = FALSE;
3193 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3197 * g_key_file_remove_key:
3198 * @key_file: a #GKeyFile
3199 * @group_name: a group name
3200 * @key: a key name to remove
3201 * @error: return location for a #GError or %NULL
3203 * Removes @key in @group_name from the key file.
3205 * Returns: %TRUE if the key was removed, %FALSE otherwise
3210 g_key_file_remove_key (GKeyFile *key_file,
3211 const gchar *group_name,
3215 GKeyFileGroup *group;
3216 GKeyFileKeyValuePair *pair;
3218 g_return_val_if_fail (key_file != NULL, FALSE);
3219 g_return_val_if_fail (group_name != NULL, FALSE);
3220 g_return_val_if_fail (key != NULL, FALSE);
3224 group = g_key_file_lookup_group (key_file, group_name);
3227 g_set_error (error, G_KEY_FILE_ERROR,
3228 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3229 _("Key file does not have group '%s'"),
3230 group_name ? group_name : "(null)");
3234 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3238 g_set_error (error, G_KEY_FILE_ERROR,
3239 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3240 _("Key file does not have key '%s' in group '%s'"),
3245 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3247 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3248 g_hash_table_remove (group->lookup_map, pair->key);
3249 g_key_file_key_value_pair_free (pair);
3255 g_key_file_lookup_group_node (GKeyFile *key_file,
3256 const gchar *group_name)
3258 GKeyFileGroup *group;
3261 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3263 group = (GKeyFileGroup *) tmp->data;
3265 if (group && group->name && strcmp (group->name, group_name) == 0)
3272 static GKeyFileGroup *
3273 g_key_file_lookup_group (GKeyFile *key_file,
3274 const gchar *group_name)
3276 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3280 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3281 GKeyFileGroup *group,
3286 for (key_node = group->key_value_pairs;
3288 key_node = key_node->next)
3290 GKeyFileKeyValuePair *pair;
3292 pair = (GKeyFileKeyValuePair *) key_node->data;
3294 if (pair->key && strcmp (pair->key, key) == 0)
3301 static GKeyFileKeyValuePair *
3302 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3303 GKeyFileGroup *group,
3306 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3309 /* Lines starting with # or consisting entirely of whitespace are merely
3310 * recorded, not parsed. This function assumes all leading whitespace
3311 * has been stripped.
3314 g_key_file_line_is_comment (const gchar *line)
3316 return (*line == '#' || *line == '\0' || *line == '\n');
3320 g_key_file_is_group_name (const gchar *name)
3327 p = q = (gchar *) name;
3328 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3329 q = g_utf8_find_next_char (q, NULL);
3331 if (*q != '\0' || q == p)
3338 g_key_file_is_key_name (const gchar *name)
3345 p = q = (gchar *) name;
3346 /* We accept a little more than the desktop entry spec says,
3347 * since gnome-vfs uses mime-types as keys in its cache.
3349 while (*q && *q != '=' && *q != '[' && *q != ']')
3350 q = g_utf8_find_next_char (q, NULL);
3352 /* No empty keys, please */
3356 /* We accept spaces in the middle of keys to not break
3357 * existing apps, but we don't tolerate initial or final
3358 * spaces, which would lead to silent corruption when
3359 * rereading the file.
3361 if (*p == ' ' || q[-1] == ' ')
3367 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3368 q = g_utf8_find_next_char (q, NULL);
3382 /* A group in a key file is made up of a starting '[' followed by one
3383 * or more letters making up the group name followed by ']'.
3386 g_key_file_line_is_group (const gchar *line)
3396 while (*p && *p != ']')
3397 p = g_utf8_find_next_char (p, NULL);
3402 /* silently accept whitespace after the ] */
3403 p = g_utf8_find_next_char (p, NULL);
3404 while (*p == ' ' || *p == '\t')
3405 p = g_utf8_find_next_char (p, NULL);
3414 g_key_file_line_is_key_value_pair (const gchar *line)
3418 p = (gchar *) g_utf8_strchr (line, -1, '=');
3423 /* Key must be non-empty
3432 g_key_file_parse_value_as_string (GKeyFile *key_file,
3437 gchar *string_value, *p, *q0, *q;
3439 string_value = g_new (gchar, strlen (value) + 1);
3441 p = (gchar *) value;
3442 q0 = q = string_value;
3472 g_set_error (error, G_KEY_FILE_ERROR,
3473 G_KEY_FILE_ERROR_INVALID_VALUE,
3474 _("Key file contains escape character "
3479 if (pieces && *p == key_file->list_separator)
3480 *q = key_file->list_separator;
3494 g_set_error (error, G_KEY_FILE_ERROR,
3495 G_KEY_FILE_ERROR_INVALID_VALUE,
3496 _("Key file contains invalid escape "
3497 "sequence '%s'"), sequence);
3506 if (pieces && (*p == key_file->list_separator))
3508 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3524 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3525 *pieces = g_slist_reverse (*pieces);
3528 return string_value;
3532 g_key_file_parse_string_as_value (GKeyFile *key_file,
3533 const gchar *string,
3534 gboolean escape_separator)
3536 gchar *value, *p, *q;
3538 gboolean parsing_leading_space;
3540 length = strlen (string) + 1;
3542 /* Worst case would be that every character needs to be escaped.
3543 * In other words every character turns to two characters
3545 value = g_new (gchar, 2 * length);
3547 p = (gchar *) string;
3549 parsing_leading_space = TRUE;
3550 while (p < (string + length - 1))
3552 gchar escaped_character[3] = { '\\', 0, 0 };
3557 if (parsing_leading_space)
3559 escaped_character[1] = 's';
3560 strcpy (q, escaped_character);
3570 if (parsing_leading_space)
3572 escaped_character[1] = 't';
3573 strcpy (q, escaped_character);
3583 escaped_character[1] = 'n';
3584 strcpy (q, escaped_character);
3588 escaped_character[1] = 'r';
3589 strcpy (q, escaped_character);
3593 escaped_character[1] = '\\';
3594 strcpy (q, escaped_character);
3596 parsing_leading_space = FALSE;
3599 if (escape_separator && *p == key_file->list_separator)
3601 escaped_character[1] = key_file->list_separator;
3602 strcpy (q, escaped_character);
3604 parsing_leading_space = TRUE;
3610 parsing_leading_space = FALSE;
3622 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3626 gchar *end_of_valid_int;
3631 long_value = strtol (value, &end_of_valid_int, 10);
3633 if (*value == '\0' || *end_of_valid_int != '\0')
3635 gchar *value_utf8 = _g_utf8_make_valid (value);
3636 g_set_error (error, G_KEY_FILE_ERROR,
3637 G_KEY_FILE_ERROR_INVALID_VALUE,
3638 _("Value '%s' cannot be interpreted "
3639 "as a number."), value_utf8);
3640 g_free (value_utf8);
3645 int_value = long_value;
3646 if (int_value != long_value || errno == ERANGE)
3648 gchar *value_utf8 = _g_utf8_make_valid (value);
3651 G_KEY_FILE_ERROR_INVALID_VALUE,
3652 _("Integer value '%s' out of range"),
3654 g_free (value_utf8);
3663 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3667 return g_strdup_printf ("%d", value);
3671 g_key_file_parse_value_as_double (GKeyFile *key_file,
3675 gchar *end_of_valid_d;
3676 gdouble double_value = 0;
3678 double_value = g_ascii_strtod (value, &end_of_valid_d);
3680 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3682 gchar *value_utf8 = _g_utf8_make_valid (value);
3683 g_set_error (error, G_KEY_FILE_ERROR,
3684 G_KEY_FILE_ERROR_INVALID_VALUE,
3685 _("Value '%s' cannot be interpreted "
3686 "as a float number."),
3688 g_free (value_utf8);
3691 return double_value;
3695 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3701 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3703 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3706 value_utf8 = _g_utf8_make_valid (value);
3707 g_set_error (error, G_KEY_FILE_ERROR,
3708 G_KEY_FILE_ERROR_INVALID_VALUE,
3709 _("Value '%s' cannot be interpreted "
3710 "as a boolean."), value_utf8);
3711 g_free (value_utf8);
3717 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3721 return g_strdup ("true");
3723 return g_strdup ("false");
3727 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3734 string = g_string_sized_new (512);
3736 lines = g_strsplit (value, "\n", 0);
3738 for (i = 0; lines[i] != NULL; i++)
3740 if (lines[i][0] != '#')
3741 g_string_append_printf (string, "%s\n", lines[i]);
3743 g_string_append_printf (string, "%s\n", lines[i] + 1);
3747 return g_string_free (string, FALSE);
3751 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3752 const gchar *comment)
3758 string = g_string_sized_new (512);
3760 lines = g_strsplit (comment, "\n", 0);
3762 for (i = 0; lines[i] != NULL; i++)
3763 g_string_append_printf (string, "#%s%s", lines[i],
3764 lines[i + 1] == NULL? "" : "\n");
3767 return g_string_free (string, FALSE);
3770 #define __G_KEY_FILE_C__
3771 #include "galiasdef.c"