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 * Return value: a newly allocated string holding
1014 * the contents of the #GKeyFile
1019 g_key_file_to_data (GKeyFile *key_file,
1023 GString *data_string;
1024 GList *group_node, *key_file_node;
1025 gboolean has_blank_line = TRUE;
1027 g_return_val_if_fail (key_file != NULL, NULL);
1029 data_string = g_string_sized_new (2 * key_file->approximate_size);
1031 for (group_node = g_list_last (key_file->groups);
1033 group_node = group_node->prev)
1035 GKeyFileGroup *group;
1037 group = (GKeyFileGroup *) group_node->data;
1039 /* separate groups by at least an empty line */
1040 if (!has_blank_line)
1041 g_string_append_c (data_string, '\n');
1042 has_blank_line = group->has_trailing_blank_line;
1044 if (group->comment != NULL)
1045 g_string_append_printf (data_string, "%s\n", group->comment->value);
1047 if (group->name != NULL)
1048 g_string_append_printf (data_string, "[%s]\n", group->name);
1050 for (key_file_node = g_list_last (group->key_value_pairs);
1051 key_file_node != NULL;
1052 key_file_node = key_file_node->prev)
1054 GKeyFileKeyValuePair *pair;
1056 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1058 if (pair->key != NULL)
1059 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1061 g_string_append_printf (data_string, "%s\n", pair->value);
1066 *length = data_string->len;
1068 return g_string_free (data_string, FALSE);
1072 * g_key_file_get_keys:
1073 * @key_file: a #GKeyFile
1074 * @group_name: a group name
1075 * @length: return location for the number of keys returned, or %NULL
1076 * @error: return location for a #GError, or %NULL
1078 * Returns all keys for the group name @group_name. The array of
1079 * returned keys will be %NULL-terminated, so @length may
1080 * optionally be %NULL. In the event that the @group_name cannot
1081 * be found, %NULL is returned and @error is set to
1082 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1084 * Return value: a newly-allocated %NULL-terminated array of strings.
1085 * Use g_strfreev() to free it.
1090 g_key_file_get_keys (GKeyFile *key_file,
1091 const gchar *group_name,
1095 GKeyFileGroup *group;
1100 g_return_val_if_fail (key_file != NULL, NULL);
1101 g_return_val_if_fail (group_name != NULL, NULL);
1103 group = g_key_file_lookup_group (key_file, group_name);
1107 g_set_error (error, G_KEY_FILE_ERROR,
1108 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1109 _("Key file does not have group '%s'"),
1110 group_name ? group_name : "(null)");
1115 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1117 GKeyFileKeyValuePair *pair;
1119 pair = (GKeyFileKeyValuePair *) tmp->data;
1125 keys = g_new (gchar *, num_keys + 1);
1128 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1130 GKeyFileKeyValuePair *pair;
1132 pair = (GKeyFileKeyValuePair *) tmp->data;
1136 keys[i] = g_strdup (pair->key);
1141 keys[num_keys] = NULL;
1150 * g_key_file_get_start_group:
1151 * @key_file: a #GKeyFile
1153 * Returns the name of the start group of the file.
1155 * Return value: The start group of the key file.
1160 g_key_file_get_start_group (GKeyFile *key_file)
1162 g_return_val_if_fail (key_file != NULL, NULL);
1164 if (key_file->start_group)
1165 return g_strdup (key_file->start_group->name);
1171 * g_key_file_get_groups:
1172 * @key_file: a #GKeyFile
1173 * @length: return location for the number of returned groups, or %NULL
1175 * Returns all groups in the key file loaded with @key_file.
1176 * The array of returned groups will be %NULL-terminated, so
1177 * @length may optionally be %NULL.
1179 * Return value: a newly-allocated %NULL-terminated array of strings.
1180 * Use g_strfreev() to free it.
1184 g_key_file_get_groups (GKeyFile *key_file,
1189 gsize i, num_groups;
1191 g_return_val_if_fail (key_file != NULL, NULL);
1193 num_groups = g_list_length (key_file->groups);
1195 g_return_val_if_fail (num_groups > 0, NULL);
1197 group_node = g_list_last (key_file->groups);
1199 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1201 /* Only need num_groups instead of num_groups + 1
1202 * because the first group of the file (last in the
1203 * list) is always the comment group at the top,
1206 groups = g_new (gchar *, num_groups);
1210 for (group_node = group_node->prev;
1212 group_node = group_node->prev)
1214 GKeyFileGroup *group;
1216 group = (GKeyFileGroup *) group_node->data;
1218 g_warn_if_fail (group->name != NULL);
1220 groups[i++] = g_strdup (group->name);
1231 * g_key_file_get_value:
1232 * @key_file: a #GKeyFile
1233 * @group_name: a group name
1235 * @error: return location for a #GError, or %NULL
1237 * Returns the value associated with @key under @group_name.
1239 * In the event the key cannot be found, %NULL is returned and
1240 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1241 * event that the @group_name cannot be found, %NULL is returned
1242 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1244 * Return value: a newly allocated string or %NULL if the specified
1245 * key cannot be found.
1250 g_key_file_get_value (GKeyFile *key_file,
1251 const gchar *group_name,
1255 GKeyFileGroup *group;
1256 GKeyFileKeyValuePair *pair;
1257 gchar *value = NULL;
1259 g_return_val_if_fail (key_file != NULL, NULL);
1260 g_return_val_if_fail (group_name != NULL, NULL);
1261 g_return_val_if_fail (key != NULL, NULL);
1263 group = g_key_file_lookup_group (key_file, group_name);
1267 g_set_error (error, G_KEY_FILE_ERROR,
1268 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1269 _("Key file does not have group '%s'"),
1270 group_name ? group_name : "(null)");
1274 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1277 value = g_strdup (pair->value);
1279 g_set_error (error, G_KEY_FILE_ERROR,
1280 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1281 _("Key file does not have key '%s'"), key);
1287 * g_key_file_set_value:
1288 * @key_file: a #GKeyFile
1289 * @group_name: a group name
1293 * Associates a new value with @key under @group_name.
1294 * If @key cannot be found then it is created.
1295 * If @group_name cannot be found then it is created.
1300 g_key_file_set_value (GKeyFile *key_file,
1301 const gchar *group_name,
1305 GKeyFileGroup *group;
1306 GKeyFileKeyValuePair *pair;
1308 g_return_if_fail (key_file != NULL);
1309 g_return_if_fail (g_key_file_is_group_name (group_name));
1310 g_return_if_fail (g_key_file_is_key_name (key));
1311 g_return_if_fail (value != NULL);
1313 group = g_key_file_lookup_group (key_file, group_name);
1317 g_key_file_add_group (key_file, group_name);
1318 group = (GKeyFileGroup *) key_file->groups->data;
1320 g_key_file_add_key (key_file, group, key, value);
1324 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1327 g_key_file_add_key (key_file, group, key, value);
1330 g_free (pair->value);
1331 pair->value = g_strdup (value);
1337 * g_key_file_get_string:
1338 * @key_file: a #GKeyFile
1339 * @group_name: a group name
1341 * @error: return location for a #GError, or %NULL
1343 * Returns the value associated with @key under @group_name.
1345 * In the event the key cannot be found, %NULL is returned and
1346 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1347 * event that the @group_name cannot be found, %NULL is returned
1348 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1350 * Return value: a newly allocated string or %NULL if the specified
1351 * key cannot be found.
1356 g_key_file_get_string (GKeyFile *key_file,
1357 const gchar *group_name,
1361 gchar *value, *string_value;
1362 GError *key_file_error;
1364 g_return_val_if_fail (key_file != NULL, NULL);
1365 g_return_val_if_fail (group_name != NULL, NULL);
1366 g_return_val_if_fail (key != NULL, NULL);
1368 key_file_error = NULL;
1370 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1374 g_propagate_error (error, key_file_error);
1378 if (!g_utf8_validate (value, -1, NULL))
1380 gchar *value_utf8 = _g_utf8_make_valid (value);
1381 g_set_error (error, G_KEY_FILE_ERROR,
1382 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1383 _("Key file contains key '%s' with value '%s' "
1384 "which is not UTF-8"), key, value_utf8);
1385 g_free (value_utf8);
1391 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1397 if (g_error_matches (key_file_error,
1399 G_KEY_FILE_ERROR_INVALID_VALUE))
1401 g_set_error (error, G_KEY_FILE_ERROR,
1402 G_KEY_FILE_ERROR_INVALID_VALUE,
1403 _("Key file contains key '%s' "
1404 "which has value that cannot be interpreted."),
1406 g_error_free (key_file_error);
1409 g_propagate_error (error, key_file_error);
1412 return string_value;
1416 * g_key_file_set_string:
1417 * @key_file: a #GKeyFile
1418 * @group_name: a group name
1422 * Associates a new string value with @key under @group_name.
1423 * If @key cannot be found then it is created.
1424 * If @group_name cannot be found then it is created.
1429 g_key_file_set_string (GKeyFile *key_file,
1430 const gchar *group_name,
1432 const gchar *string)
1436 g_return_if_fail (key_file != NULL);
1437 g_return_if_fail (string != NULL);
1439 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1440 g_key_file_set_value (key_file, group_name, key, value);
1445 * g_key_file_get_string_list:
1446 * @key_file: a #GKeyFile
1447 * @group_name: a group name
1449 * @length: return location for the number of returned strings, or %NULL
1450 * @error: return location for a #GError, or %NULL
1452 * Returns the values associated with @key under @group_name.
1454 * In the event the key cannot be found, %NULL is returned and
1455 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1456 * event that the @group_name cannot be found, %NULL is returned
1457 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1459 * Return value: a %NULL-terminated string array or %NULL if the specified
1460 * key cannot be found. The array should be freed with g_strfreev().
1465 g_key_file_get_string_list (GKeyFile *key_file,
1466 const gchar *group_name,
1471 GError *key_file_error = NULL;
1472 gchar *value, *string_value, **values;
1474 GSList *p, *pieces = NULL;
1476 g_return_val_if_fail (key_file != NULL, NULL);
1477 g_return_val_if_fail (group_name != NULL, NULL);
1478 g_return_val_if_fail (key != NULL, NULL);
1483 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1487 g_propagate_error (error, key_file_error);
1491 if (!g_utf8_validate (value, -1, NULL))
1493 gchar *value_utf8 = _g_utf8_make_valid (value);
1494 g_set_error (error, G_KEY_FILE_ERROR,
1495 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1496 _("Key file contains key '%s' with value '%s' "
1497 "which is not UTF-8"), key, value_utf8);
1498 g_free (value_utf8);
1504 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1506 g_free (string_value);
1510 if (g_error_matches (key_file_error,
1512 G_KEY_FILE_ERROR_INVALID_VALUE))
1514 g_set_error (error, G_KEY_FILE_ERROR,
1515 G_KEY_FILE_ERROR_INVALID_VALUE,
1516 _("Key file contains key '%s' "
1517 "which has value that cannot be interpreted."),
1519 g_error_free (key_file_error);
1522 g_propagate_error (error, key_file_error);
1527 len = g_slist_length (pieces);
1528 values = g_new (gchar *, len + 1);
1529 for (p = pieces, i = 0; p; p = p->next)
1530 values[i++] = p->data;
1533 g_slist_free (pieces);
1542 * g_key_file_set_string_list:
1543 * @key_file: a #GKeyFile
1544 * @group_name: a group name
1546 * @list: an array of locale string values
1547 * @length: number of locale string values in @list
1549 * Associates a list of string values for @key under @group_name.
1550 * If @key cannot be found then it is created.
1551 * If @group_name cannot be found then it is created.
1556 g_key_file_set_string_list (GKeyFile *key_file,
1557 const gchar *group_name,
1559 const gchar * const list[],
1562 GString *value_list;
1565 g_return_if_fail (key_file != NULL);
1566 g_return_if_fail (list != NULL);
1568 value_list = g_string_sized_new (length * 128);
1569 for (i = 0; i < length && list[i] != NULL; i++)
1573 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1574 g_string_append (value_list, value);
1575 g_string_append_c (value_list, key_file->list_separator);
1580 g_key_file_set_value (key_file, group_name, key, value_list->str);
1581 g_string_free (value_list, TRUE);
1585 * g_key_file_set_locale_string:
1586 * @key_file: a #GKeyFile
1587 * @group_name: a group name
1592 * Associates a string value for @key and @locale under @group_name.
1593 * If the translation for @key cannot be found then it is created.
1598 g_key_file_set_locale_string (GKeyFile *key_file,
1599 const gchar *group_name,
1601 const gchar *locale,
1602 const gchar *string)
1604 gchar *full_key, *value;
1606 g_return_if_fail (key_file != NULL);
1607 g_return_if_fail (key != NULL);
1608 g_return_if_fail (locale != NULL);
1609 g_return_if_fail (string != NULL);
1611 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1612 full_key = g_strdup_printf ("%s[%s]", key, locale);
1613 g_key_file_set_value (key_file, group_name, full_key, value);
1618 extern GSList *_g_compute_locale_variants (const gchar *locale);
1621 * g_key_file_get_locale_string:
1622 * @key_file: a #GKeyFile
1623 * @group_name: a group name
1625 * @locale: a locale or %NULL
1626 * @error: return location for a #GError, or %NULL
1628 * Returns the value associated with @key under @group_name
1629 * translated in the given @locale if available. If @locale is
1630 * %NULL then the current locale is assumed.
1632 * If @key cannot be found then %NULL is returned and @error is set
1633 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1634 * with @key cannot be interpreted or no suitable translation can
1635 * be found then the untranslated value is returned.
1637 * Return value: a newly allocated string or %NULL if the specified
1638 * key cannot be found.
1643 g_key_file_get_locale_string (GKeyFile *key_file,
1644 const gchar *group_name,
1646 const gchar *locale,
1649 gchar *candidate_key, *translated_value;
1650 GError *key_file_error;
1652 gboolean free_languages = FALSE;
1655 g_return_val_if_fail (key_file != NULL, NULL);
1656 g_return_val_if_fail (group_name != NULL, NULL);
1657 g_return_val_if_fail (key != NULL, NULL);
1659 candidate_key = NULL;
1660 translated_value = NULL;
1661 key_file_error = NULL;
1667 list = _g_compute_locale_variants (locale);
1669 languages = g_new (gchar *, g_slist_length (list) + 1);
1670 for (l = list, i = 0; l; l = l->next, i++)
1671 languages[i] = l->data;
1672 languages[i] = NULL;
1674 g_slist_free (list);
1675 free_languages = TRUE;
1679 languages = (gchar **) g_get_language_names ();
1680 free_languages = FALSE;
1683 for (i = 0; languages[i]; i++)
1685 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1687 translated_value = g_key_file_get_string (key_file,
1689 candidate_key, NULL);
1690 g_free (candidate_key);
1692 if (translated_value)
1695 g_free (translated_value);
1696 translated_value = NULL;
1699 /* Fallback to untranslated key
1701 if (!translated_value)
1703 translated_value = g_key_file_get_string (key_file, group_name, key,
1706 if (!translated_value)
1707 g_propagate_error (error, key_file_error);
1711 g_strfreev (languages);
1713 return translated_value;
1717 * g_key_file_get_locale_string_list:
1718 * @key_file: a #GKeyFile
1719 * @group_name: a group name
1722 * @length: return location for the number of returned strings or %NULL
1723 * @error: return location for a #GError or %NULL
1725 * Returns the values associated with @key under @group_name
1726 * translated in the given @locale if available. If @locale is
1727 * %NULL then the current locale is assumed.
1729 * If @key cannot be found then %NULL is returned and @error is set
1730 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1731 * with @key cannot be interpreted or no suitable translations
1732 * can be found then the untranslated values are returned. The
1733 * returned array is %NULL-terminated, so @length may optionally
1736 * Return value: a newly allocated %NULL-terminated string array
1737 * or %NULL if the key isn't found. The string array should be freed
1738 * with g_strfreev().
1743 g_key_file_get_locale_string_list (GKeyFile *key_file,
1744 const gchar *group_name,
1746 const gchar *locale,
1750 GError *key_file_error;
1751 gchar **values, *value;
1753 g_return_val_if_fail (key_file != NULL, NULL);
1754 g_return_val_if_fail (group_name != NULL, NULL);
1755 g_return_val_if_fail (key != NULL, NULL);
1757 key_file_error = NULL;
1759 value = g_key_file_get_locale_string (key_file, group_name,
1764 g_propagate_error (error, key_file_error);
1773 if (value[strlen (value) - 1] == ';')
1774 value[strlen (value) - 1] = '\0';
1776 values = g_strsplit (value, ";", 0);
1781 *length = g_strv_length (values);
1787 * g_key_file_set_locale_string_list:
1788 * @key_file: a #GKeyFile
1789 * @group_name: a group name
1792 * @list: a %NULL-terminated array of locale string values
1793 * @length: the length of @list
1795 * Associates a list of string values for @key and @locale under
1796 * @group_name. If the translation for @key cannot be found then
1802 g_key_file_set_locale_string_list (GKeyFile *key_file,
1803 const gchar *group_name,
1805 const gchar *locale,
1806 const gchar * const list[],
1809 GString *value_list;
1813 g_return_if_fail (key_file != NULL);
1814 g_return_if_fail (key != NULL);
1815 g_return_if_fail (locale != NULL);
1816 g_return_if_fail (length != 0);
1818 value_list = g_string_sized_new (length * 128);
1819 for (i = 0; i < length && list[i] != NULL; i++)
1823 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1825 g_string_append (value_list, value);
1826 g_string_append_c (value_list, ';');
1831 full_key = g_strdup_printf ("%s[%s]", key, locale);
1832 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1834 g_string_free (value_list, TRUE);
1838 * g_key_file_get_boolean:
1839 * @key_file: a #GKeyFile
1840 * @group_name: a group name
1842 * @error: return location for a #GError
1844 * Returns the value associated with @key under @group_name as a
1847 * If @key cannot be found then %FALSE is returned and @error is set
1848 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1849 * associated with @key cannot be interpreted as a boolean then %FALSE
1850 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1852 * Return value: the value associated with the key as a boolean,
1853 * or %FALSE if the key was not found or could not be parsed.
1858 g_key_file_get_boolean (GKeyFile *key_file,
1859 const gchar *group_name,
1863 GError *key_file_error = NULL;
1865 gboolean bool_value;
1867 g_return_val_if_fail (key_file != NULL, FALSE);
1868 g_return_val_if_fail (group_name != NULL, FALSE);
1869 g_return_val_if_fail (key != NULL, FALSE);
1871 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1875 g_propagate_error (error, key_file_error);
1879 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1885 if (g_error_matches (key_file_error,
1887 G_KEY_FILE_ERROR_INVALID_VALUE))
1889 g_set_error (error, G_KEY_FILE_ERROR,
1890 G_KEY_FILE_ERROR_INVALID_VALUE,
1891 _("Key file contains key '%s' "
1892 "which has value that cannot be interpreted."),
1894 g_error_free (key_file_error);
1897 g_propagate_error (error, key_file_error);
1904 * g_key_file_set_boolean:
1905 * @key_file: a #GKeyFile
1906 * @group_name: a group name
1908 * @value: %TRUE or %FALSE
1910 * Associates a new boolean value with @key under @group_name.
1911 * If @key cannot be found then it is created.
1916 g_key_file_set_boolean (GKeyFile *key_file,
1917 const gchar *group_name,
1923 g_return_if_fail (key_file != NULL);
1925 result = g_key_file_parse_boolean_as_value (key_file, value);
1926 g_key_file_set_value (key_file, group_name, key, result);
1931 * g_key_file_get_boolean_list:
1932 * @key_file: a #GKeyFile
1933 * @group_name: a group name
1935 * @length: the number of booleans returned
1936 * @error: return location for a #GError
1938 * Returns the values associated with @key under @group_name as
1941 * If @key cannot be found then %NULL is returned and @error is set to
1942 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1943 * with @key cannot be interpreted as booleans then %NULL is returned
1944 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1946 * Return value: the values associated with the key as a list of
1947 * booleans, or %NULL if the key was not found or could not be parsed.
1952 g_key_file_get_boolean_list (GKeyFile *key_file,
1953 const gchar *group_name,
1958 GError *key_file_error;
1960 gboolean *bool_values;
1963 g_return_val_if_fail (key_file != NULL, NULL);
1964 g_return_val_if_fail (group_name != NULL, NULL);
1965 g_return_val_if_fail (key != NULL, NULL);
1970 key_file_error = NULL;
1972 values = g_key_file_get_string_list (key_file, group_name, key,
1973 &num_bools, &key_file_error);
1976 g_propagate_error (error, key_file_error);
1981 bool_values = g_new (gboolean, num_bools);
1983 for (i = 0; i < num_bools; i++)
1985 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1991 g_propagate_error (error, key_file_error);
1992 g_strfreev (values);
1993 g_free (bool_values);
1998 g_strfreev (values);
2001 *length = num_bools;
2007 * g_key_file_set_boolean_list:
2008 * @key_file: a #GKeyFile
2009 * @group_name: a group name
2011 * @list: an array of boolean values
2012 * @length: length of @list
2014 * Associates a list of boolean values with @key under @group_name.
2015 * If @key cannot be found then it is created.
2016 * If @group_name is %NULL, the start_group is used.
2021 g_key_file_set_boolean_list (GKeyFile *key_file,
2022 const gchar *group_name,
2027 GString *value_list;
2030 g_return_if_fail (key_file != NULL);
2031 g_return_if_fail (list != NULL);
2033 value_list = g_string_sized_new (length * 8);
2034 for (i = 0; i < length; i++)
2038 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2040 g_string_append (value_list, value);
2041 g_string_append_c (value_list, key_file->list_separator);
2046 g_key_file_set_value (key_file, group_name, key, value_list->str);
2047 g_string_free (value_list, TRUE);
2051 * g_key_file_get_integer:
2052 * @key_file: a #GKeyFile
2053 * @group_name: a group name
2055 * @error: return location for a #GError
2057 * Returns the value associated with @key under @group_name as an
2060 * If @key cannot be found then 0 is returned and @error is set to
2061 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2062 * with @key cannot be interpreted as an integer then 0 is returned
2063 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2065 * Return value: the value associated with the key as an integer, or
2066 * 0 if the key was not found or could not be parsed.
2071 g_key_file_get_integer (GKeyFile *key_file,
2072 const gchar *group_name,
2076 GError *key_file_error;
2080 g_return_val_if_fail (key_file != NULL, -1);
2081 g_return_val_if_fail (group_name != NULL, -1);
2082 g_return_val_if_fail (key != NULL, -1);
2084 key_file_error = NULL;
2086 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2090 g_propagate_error (error, key_file_error);
2094 int_value = g_key_file_parse_value_as_integer (key_file, value,
2100 if (g_error_matches (key_file_error,
2102 G_KEY_FILE_ERROR_INVALID_VALUE))
2104 g_set_error (error, G_KEY_FILE_ERROR,
2105 G_KEY_FILE_ERROR_INVALID_VALUE,
2106 _("Key file contains key '%s' in group '%s' "
2107 "which has value that cannot be interpreted."), key,
2109 g_error_free (key_file_error);
2112 g_propagate_error (error, key_file_error);
2119 * g_key_file_set_integer:
2120 * @key_file: a #GKeyFile
2121 * @group_name: a group name
2123 * @value: an integer value
2125 * Associates a new integer value with @key under @group_name.
2126 * If @key cannot be found then it is created.
2131 g_key_file_set_integer (GKeyFile *key_file,
2132 const gchar *group_name,
2138 g_return_if_fail (key_file != NULL);
2140 result = g_key_file_parse_integer_as_value (key_file, value);
2141 g_key_file_set_value (key_file, group_name, key, result);
2146 * g_key_file_get_integer_list:
2147 * @key_file: a #GKeyFile
2148 * @group_name: a group name
2150 * @length: the number of integers returned
2151 * @error: return location for a #GError
2153 * Returns the values associated with @key under @group_name as
2156 * If @key cannot be found then %NULL is returned and @error is set to
2157 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2158 * with @key cannot be interpreted as integers then %NULL is returned
2159 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2161 * Return value: the values associated with the key as a list of
2162 * integers, or %NULL if the key was not found or could not be parsed.
2167 g_key_file_get_integer_list (GKeyFile *key_file,
2168 const gchar *group_name,
2173 GError *key_file_error = NULL;
2178 g_return_val_if_fail (key_file != NULL, NULL);
2179 g_return_val_if_fail (group_name != NULL, NULL);
2180 g_return_val_if_fail (key != NULL, NULL);
2185 values = g_key_file_get_string_list (key_file, group_name, key,
2186 &num_ints, &key_file_error);
2189 g_propagate_error (error, key_file_error);
2194 int_values = g_new (gint, num_ints);
2196 for (i = 0; i < num_ints; i++)
2198 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2204 g_propagate_error (error, key_file_error);
2205 g_strfreev (values);
2206 g_free (int_values);
2211 g_strfreev (values);
2220 * g_key_file_set_integer_list:
2221 * @key_file: a #GKeyFile
2222 * @group_name: a group name
2224 * @list: an array of integer values
2225 * @length: number of integer values in @list
2227 * Associates a list of integer values with @key under @group_name.
2228 * If @key cannot be found then it is created.
2233 g_key_file_set_integer_list (GKeyFile *key_file,
2234 const gchar *group_name,
2242 g_return_if_fail (key_file != NULL);
2243 g_return_if_fail (list != NULL);
2245 values = g_string_sized_new (length * 16);
2246 for (i = 0; i < length; i++)
2250 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2252 g_string_append (values, value);
2253 g_string_append_c (values, ';');
2258 g_key_file_set_value (key_file, group_name, key, values->str);
2259 g_string_free (values, TRUE);
2263 * g_key_file_get_double:
2264 * @key_file: a #GKeyFile
2265 * @group_name: a group name
2267 * @error: return location for a #GError
2269 * Returns the value associated with @key under @group_name as a
2270 * double. If @group_name is %NULL, the start_group is used.
2272 * If @key cannot be found then 0.0 is returned and @error is set to
2273 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2274 * with @key cannot be interpreted as a double then 0.0 is returned
2275 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2277 * Return value: the value associated with the key as a double, or
2278 * 0.0 if the key was not found or could not be parsed.
2283 g_key_file_get_double (GKeyFile *key_file,
2284 const gchar *group_name,
2288 GError *key_file_error;
2290 gdouble double_value;
2292 g_return_val_if_fail (key_file != NULL, -1);
2293 g_return_val_if_fail (group_name != NULL, -1);
2294 g_return_val_if_fail (key != NULL, -1);
2296 key_file_error = NULL;
2298 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2302 g_propagate_error (error, key_file_error);
2306 double_value = g_key_file_parse_value_as_double (key_file, value,
2312 if (g_error_matches (key_file_error,
2314 G_KEY_FILE_ERROR_INVALID_VALUE))
2316 g_set_error (error, G_KEY_FILE_ERROR,
2317 G_KEY_FILE_ERROR_INVALID_VALUE,
2318 _("Key file contains key '%s' in group '%s' "
2319 "which has value that cannot be interpreted."), key,
2321 g_error_free (key_file_error);
2324 g_propagate_error (error, key_file_error);
2327 return double_value;
2331 * g_key_file_set_double:
2332 * @key_file: a #GKeyFile
2333 * @group_name: a group name
2335 * @value: an double value
2337 * Associates a new double value with @key under @group_name.
2338 * If @key cannot be found then it is created.
2343 g_key_file_set_double (GKeyFile *key_file,
2344 const gchar *group_name,
2348 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2350 g_return_if_fail (key_file != NULL);
2352 g_ascii_dtostr (result, sizeof (result), value);
2353 g_key_file_set_value (key_file, group_name, key, result);
2357 * g_key_file_get_double_list:
2358 * @key_file: a #GKeyFile
2359 * @group_name: a group name
2361 * @length: the number of doubles returned
2362 * @error: return location for a #GError
2364 * Returns the values associated with @key under @group_name as
2367 * If @key cannot be found then %NULL is returned and @error is set to
2368 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2369 * with @key cannot be interpreted as doubles then %NULL is returned
2370 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2372 * Return value: the values associated with the key as a list of
2373 * doubles, or %NULL if the key was not found or could not be parsed.
2378 g_key_file_get_double_list (GKeyFile *key_file,
2379 const gchar *group_name,
2384 GError *key_file_error = NULL;
2386 gdouble *double_values;
2387 gsize i, num_doubles;
2389 g_return_val_if_fail (key_file != NULL, NULL);
2390 g_return_val_if_fail (group_name != NULL, NULL);
2391 g_return_val_if_fail (key != NULL, NULL);
2396 values = g_key_file_get_string_list (key_file, group_name, key,
2397 &num_doubles, &key_file_error);
2400 g_propagate_error (error, key_file_error);
2405 double_values = g_new (gdouble, num_doubles);
2407 for (i = 0; i < num_doubles; i++)
2409 double_values[i] = g_key_file_parse_value_as_double (key_file,
2415 g_propagate_error (error, key_file_error);
2416 g_strfreev (values);
2417 g_free (double_values);
2422 g_strfreev (values);
2425 *length = num_doubles;
2427 return double_values;
2431 * g_key_file_set_double_list:
2432 * @key_file: a #GKeyFile
2433 * @group_name: a group name
2435 * @list: an array of double values
2436 * @length: number of double values in @list
2438 * Associates a list of double values with @key under
2439 * @group_name. If @key cannot be found then it is created.
2444 g_key_file_set_double_list (GKeyFile *key_file,
2445 const gchar *group_name,
2453 g_return_if_fail (key_file != NULL);
2454 g_return_if_fail (list != NULL);
2456 values = g_string_sized_new (length * 16);
2457 for (i = 0; i < length; i++)
2459 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2461 g_ascii_dtostr( result, sizeof (result), list[i] );
2463 g_string_append (values, result);
2464 g_string_append_c (values, ';');
2467 g_key_file_set_value (key_file, group_name, key, values->str);
2468 g_string_free (values, TRUE);
2472 g_key_file_set_key_comment (GKeyFile *key_file,
2473 const gchar *group_name,
2475 const gchar *comment,
2478 GKeyFileGroup *group;
2479 GKeyFileKeyValuePair *pair;
2480 GList *key_node, *comment_node, *tmp;
2482 group = g_key_file_lookup_group (key_file, group_name);
2485 g_set_error (error, G_KEY_FILE_ERROR,
2486 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2487 _("Key file does not have group '%s'"),
2488 group_name ? group_name : "(null)");
2493 /* First find the key the comments are supposed to be
2496 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2498 if (key_node == NULL)
2500 g_set_error (error, G_KEY_FILE_ERROR,
2501 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2502 _("Key file does not have key '%s' in group '%s'"),
2507 /* Then find all the comments already associated with the
2510 tmp = key_node->next;
2513 GKeyFileKeyValuePair *pair;
2515 pair = (GKeyFileKeyValuePair *) tmp->data;
2517 if (pair->key != NULL)
2522 g_key_file_remove_key_value_pair_node (key_file, group,
2526 if (comment == NULL)
2529 /* Now we can add our new comment
2531 pair = g_slice_new (GKeyFileKeyValuePair);
2533 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2535 key_node = g_list_insert (key_node, pair, 1);
2541 g_key_file_set_group_comment (GKeyFile *key_file,
2542 const gchar *group_name,
2543 const gchar *comment,
2546 GKeyFileGroup *group;
2548 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2550 group = g_key_file_lookup_group (key_file, group_name);
2553 g_set_error (error, G_KEY_FILE_ERROR,
2554 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2555 _("Key file does not have group '%s'"),
2556 group_name ? group_name : "(null)");
2561 /* First remove any existing comment
2565 g_key_file_key_value_pair_free (group->comment);
2566 group->comment = NULL;
2569 if (comment == NULL)
2572 /* Now we can add our new comment
2574 group->comment = g_slice_new (GKeyFileKeyValuePair);
2575 group->comment->key = NULL;
2576 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2582 g_key_file_set_top_comment (GKeyFile *key_file,
2583 const gchar *comment,
2587 GKeyFileGroup *group;
2588 GKeyFileKeyValuePair *pair;
2590 /* The last group in the list should be the top (comments only)
2593 g_warn_if_fail (key_file->groups != NULL);
2594 group_node = g_list_last (key_file->groups);
2595 group = (GKeyFileGroup *) group_node->data;
2596 g_warn_if_fail (group->name == NULL);
2598 /* Note all keys must be comments at the top of
2599 * the file, so we can just free it all.
2601 if (group->key_value_pairs != NULL)
2603 g_list_foreach (group->key_value_pairs,
2604 (GFunc) g_key_file_key_value_pair_free,
2606 g_list_free (group->key_value_pairs);
2607 group->key_value_pairs = NULL;
2610 if (comment == NULL)
2613 pair = g_slice_new (GKeyFileKeyValuePair);
2615 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2617 group->key_value_pairs =
2618 g_list_prepend (group->key_value_pairs, pair);
2624 * g_key_file_set_comment:
2625 * @key_file: a #GKeyFile
2626 * @group_name: a group name, or %NULL
2628 * @comment: a comment
2629 * @error: return location for a #GError
2631 * Places a comment above @key from @group_name.
2632 * If @key is %NULL then @comment will be written above @group_name.
2633 * If both @key and @group_name are %NULL, then @comment will be
2634 * written above the first group in the file.
2636 * Returns: %TRUE if the comment was written, %FALSE otherwise
2641 g_key_file_set_comment (GKeyFile *key_file,
2642 const gchar *group_name,
2644 const gchar *comment,
2647 g_return_val_if_fail (key_file != NULL, FALSE);
2649 if (group_name != NULL && key != NULL)
2651 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2654 else if (group_name != NULL)
2656 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2661 if (!g_key_file_set_top_comment (key_file, comment, error))
2665 if (comment != NULL)
2666 key_file->approximate_size += strlen (comment);
2672 g_key_file_get_key_comment (GKeyFile *key_file,
2673 const gchar *group_name,
2677 GKeyFileGroup *group;
2678 GKeyFileKeyValuePair *pair;
2679 GList *key_node, *tmp;
2683 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2685 group = g_key_file_lookup_group (key_file, group_name);
2688 g_set_error (error, G_KEY_FILE_ERROR,
2689 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2690 _("Key file does not have group '%s'"),
2691 group_name ? group_name : "(null)");
2696 /* First find the key the comments are supposed to be
2699 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2701 if (key_node == NULL)
2703 g_set_error (error, G_KEY_FILE_ERROR,
2704 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2705 _("Key file does not have key '%s' in group '%s'"),
2712 /* Then find all the comments already associated with the
2713 * key and concatentate them.
2715 tmp = key_node->next;
2716 if (!key_node->next)
2719 pair = (GKeyFileKeyValuePair *) tmp->data;
2720 if (pair->key != NULL)
2725 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2727 if (pair->key != NULL)
2733 while (tmp != key_node)
2735 GKeyFileKeyValuePair *pair;
2737 pair = (GKeyFileKeyValuePair *) tmp->data;
2740 string = g_string_sized_new (512);
2742 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2743 g_string_append (string, comment);
2751 comment = string->str;
2752 g_string_free (string, FALSE);
2761 get_group_comment (GKeyFile *key_file,
2762 GKeyFileGroup *group,
2771 tmp = group->key_value_pairs;
2774 GKeyFileKeyValuePair *pair;
2776 pair = (GKeyFileKeyValuePair *) tmp->data;
2778 if (pair->key != NULL)
2784 if (tmp->next == NULL)
2792 GKeyFileKeyValuePair *pair;
2794 pair = (GKeyFileKeyValuePair *) tmp->data;
2797 string = g_string_sized_new (512);
2799 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2800 g_string_append (string, comment);
2807 return g_string_free (string, FALSE);
2813 g_key_file_get_group_comment (GKeyFile *key_file,
2814 const gchar *group_name,
2818 GKeyFileGroup *group;
2820 group = g_key_file_lookup_group (key_file, group_name);
2823 g_set_error (error, G_KEY_FILE_ERROR,
2824 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2825 _("Key file does not have group '%s'"),
2826 group_name ? group_name : "(null)");
2832 return g_strdup (group->comment->value);
2834 group_node = g_key_file_lookup_group_node (key_file, group_name);
2835 group_node = group_node->next;
2836 group = (GKeyFileGroup *)group_node->data;
2837 return get_group_comment (key_file, group, error);
2841 g_key_file_get_top_comment (GKeyFile *key_file,
2845 GKeyFileGroup *group;
2847 /* The last group in the list should be the top (comments only)
2850 g_warn_if_fail (key_file->groups != NULL);
2851 group_node = g_list_last (key_file->groups);
2852 group = (GKeyFileGroup *) group_node->data;
2853 g_warn_if_fail (group->name == NULL);
2855 return get_group_comment (key_file, group, error);
2859 * g_key_file_get_comment:
2860 * @key_file: a #GKeyFile
2861 * @group_name: a group name, or %NULL
2863 * @error: return location for a #GError
2865 * Retrieves a comment above @key from @group_name.
2866 * If @key is %NULL then @comment will be read from above
2867 * @group_name. If both @key and @group_name are %NULL, then
2868 * @comment will be read from above the first group in the file.
2870 * Returns: a comment that should be freed with g_free()
2875 g_key_file_get_comment (GKeyFile *key_file,
2876 const gchar *group_name,
2880 g_return_val_if_fail (key_file != NULL, NULL);
2882 if (group_name != NULL && key != NULL)
2883 return g_key_file_get_key_comment (key_file, group_name, key, error);
2884 else if (group_name != NULL)
2885 return g_key_file_get_group_comment (key_file, group_name, error);
2887 return g_key_file_get_top_comment (key_file, error);
2891 * g_key_file_remove_comment:
2892 * @key_file: a #GKeyFile
2893 * @group_name: a group name, or %NULL
2895 * @error: return location for a #GError
2897 * Removes a comment above @key from @group_name.
2898 * If @key is %NULL then @comment will be removed above @group_name.
2899 * If both @key and @group_name are %NULL, then @comment will
2900 * be removed above the first group in the file.
2902 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2908 g_key_file_remove_comment (GKeyFile *key_file,
2909 const gchar *group_name,
2913 g_return_val_if_fail (key_file != NULL, FALSE);
2915 if (group_name != NULL && key != NULL)
2916 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2917 else if (group_name != NULL)
2918 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2920 return g_key_file_set_top_comment (key_file, NULL, error);
2924 * g_key_file_has_group:
2925 * @key_file: a #GKeyFile
2926 * @group_name: a group name
2928 * Looks whether the key file has the group @group_name.
2930 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2935 g_key_file_has_group (GKeyFile *key_file,
2936 const gchar *group_name)
2938 g_return_val_if_fail (key_file != NULL, FALSE);
2939 g_return_val_if_fail (group_name != NULL, FALSE);
2941 return g_key_file_lookup_group (key_file, group_name) != NULL;
2945 * g_key_file_has_key:
2946 * @key_file: a #GKeyFile
2947 * @group_name: a group name
2949 * @error: return location for a #GError
2951 * Looks whether the key file has the key @key in the group
2954 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2960 g_key_file_has_key (GKeyFile *key_file,
2961 const gchar *group_name,
2965 GKeyFileKeyValuePair *pair;
2966 GKeyFileGroup *group;
2968 g_return_val_if_fail (key_file != NULL, FALSE);
2969 g_return_val_if_fail (group_name != NULL, FALSE);
2970 g_return_val_if_fail (key != NULL, FALSE);
2972 group = g_key_file_lookup_group (key_file, group_name);
2976 g_set_error (error, G_KEY_FILE_ERROR,
2977 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2978 _("Key file does not have group '%s'"),
2979 group_name ? group_name : "(null)");
2984 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2986 return pair != NULL;
2990 g_key_file_add_group (GKeyFile *key_file,
2991 const gchar *group_name)
2993 GKeyFileGroup *group;
2995 g_return_if_fail (key_file != NULL);
2996 g_return_if_fail (g_key_file_is_group_name (group_name));
2998 group = g_key_file_lookup_group (key_file, group_name);
3001 key_file->current_group = group;
3005 group = g_slice_new0 (GKeyFileGroup);
3006 group->name = g_strdup (group_name);
3007 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3008 key_file->groups = g_list_prepend (key_file->groups, group);
3009 key_file->approximate_size += strlen (group_name) + 3;
3010 key_file->current_group = group;
3012 if (key_file->start_group == NULL)
3013 key_file->start_group = group;
3015 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3019 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3024 g_free (pair->value);
3025 g_slice_free (GKeyFileKeyValuePair, pair);
3029 /* Be careful not to call this function on a node with data in the
3030 * lookup map without removing it from the lookup map, first.
3032 * Some current cases where this warning is not a concern are
3034 * - the node being removed is a comment node
3035 * - the entire lookup map is getting destroyed soon after
3039 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3040 GKeyFileGroup *group,
3044 GKeyFileKeyValuePair *pair;
3046 pair = (GKeyFileKeyValuePair *) pair_node->data;
3048 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3050 if (pair->key != NULL)
3051 key_file->approximate_size -= strlen (pair->key) + 1;
3053 g_warn_if_fail (pair->value != NULL);
3054 key_file->approximate_size -= strlen (pair->value);
3056 g_key_file_key_value_pair_free (pair);
3058 g_list_free_1 (pair_node);
3062 g_key_file_remove_group_node (GKeyFile *key_file,
3065 GKeyFileGroup *group;
3068 group = (GKeyFileGroup *) group_node->data;
3071 g_hash_table_remove (key_file->group_hash, group->name);
3073 /* If the current group gets deleted make the current group the last
3076 if (key_file->current_group == group)
3078 /* groups should always contain at least the top comment group,
3079 * unless g_key_file_clear has been called
3081 if (key_file->groups)
3082 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3084 key_file->current_group = NULL;
3087 /* If the start group gets deleted make the start group the first
3090 if (key_file->start_group == group)
3092 tmp = g_list_last (key_file->groups);
3095 if (tmp != group_node &&
3096 ((GKeyFileGroup *) tmp->data)->name != NULL)
3103 key_file->start_group = (GKeyFileGroup *) tmp->data;
3105 key_file->start_group = NULL;
3108 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3110 if (group->name != NULL)
3111 key_file->approximate_size -= strlen (group->name) + 3;
3113 tmp = group->key_value_pairs;
3120 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3123 g_warn_if_fail (group->key_value_pairs == NULL);
3125 if (group->lookup_map)
3127 g_hash_table_destroy (group->lookup_map);
3128 group->lookup_map = NULL;
3131 g_free ((gchar *) group->name);
3132 g_slice_free (GKeyFileGroup, group);
3133 g_list_free_1 (group_node);
3137 * g_key_file_remove_group:
3138 * @key_file: a #GKeyFile
3139 * @group_name: a group name
3140 * @error: return location for a #GError or %NULL
3142 * Removes the specified group, @group_name,
3143 * from the key file.
3145 * Returns: %TRUE if the group was removed, %FALSE otherwise
3150 g_key_file_remove_group (GKeyFile *key_file,
3151 const gchar *group_name,
3156 g_return_val_if_fail (key_file != NULL, FALSE);
3157 g_return_val_if_fail (group_name != NULL, FALSE);
3159 group_node = g_key_file_lookup_group_node (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'"),
3170 g_key_file_remove_group_node (key_file, group_node);
3176 g_key_file_add_key (GKeyFile *key_file,
3177 GKeyFileGroup *group,
3181 GKeyFileKeyValuePair *pair;
3183 pair = g_slice_new (GKeyFileKeyValuePair);
3184 pair->key = g_strdup (key);
3185 pair->value = g_strdup (value);
3187 g_hash_table_replace (group->lookup_map, pair->key, pair);
3188 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3189 group->has_trailing_blank_line = FALSE;
3190 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3194 * g_key_file_remove_key:
3195 * @key_file: a #GKeyFile
3196 * @group_name: a group name
3197 * @key: a key name to remove
3198 * @error: return location for a #GError or %NULL
3200 * Removes @key in @group_name from the key file.
3202 * Returns: %TRUE if the key was removed, %FALSE otherwise
3207 g_key_file_remove_key (GKeyFile *key_file,
3208 const gchar *group_name,
3212 GKeyFileGroup *group;
3213 GKeyFileKeyValuePair *pair;
3215 g_return_val_if_fail (key_file != NULL, FALSE);
3216 g_return_val_if_fail (group_name != NULL, FALSE);
3217 g_return_val_if_fail (key != NULL, FALSE);
3221 group = g_key_file_lookup_group (key_file, group_name);
3224 g_set_error (error, G_KEY_FILE_ERROR,
3225 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3226 _("Key file does not have group '%s'"),
3227 group_name ? group_name : "(null)");
3231 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3235 g_set_error (error, G_KEY_FILE_ERROR,
3236 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3237 _("Key file does not have key '%s' in group '%s'"),
3242 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3244 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3245 g_hash_table_remove (group->lookup_map, pair->key);
3246 g_key_file_key_value_pair_free (pair);
3252 g_key_file_lookup_group_node (GKeyFile *key_file,
3253 const gchar *group_name)
3255 GKeyFileGroup *group;
3258 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3260 group = (GKeyFileGroup *) tmp->data;
3262 if (group && group->name && strcmp (group->name, group_name) == 0)
3269 static GKeyFileGroup *
3270 g_key_file_lookup_group (GKeyFile *key_file,
3271 const gchar *group_name)
3273 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3277 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3278 GKeyFileGroup *group,
3283 for (key_node = group->key_value_pairs;
3285 key_node = key_node->next)
3287 GKeyFileKeyValuePair *pair;
3289 pair = (GKeyFileKeyValuePair *) key_node->data;
3291 if (pair->key && strcmp (pair->key, key) == 0)
3298 static GKeyFileKeyValuePair *
3299 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3300 GKeyFileGroup *group,
3303 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3306 /* Lines starting with # or consisting entirely of whitespace are merely
3307 * recorded, not parsed. This function assumes all leading whitespace
3308 * has been stripped.
3311 g_key_file_line_is_comment (const gchar *line)
3313 return (*line == '#' || *line == '\0' || *line == '\n');
3317 g_key_file_is_group_name (const gchar *name)
3324 p = q = (gchar *) name;
3325 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3326 q = g_utf8_find_next_char (q, NULL);
3328 if (*q != '\0' || q == p)
3335 g_key_file_is_key_name (const gchar *name)
3342 p = q = (gchar *) name;
3343 /* We accept a little more than the desktop entry spec says,
3344 * since gnome-vfs uses mime-types as keys in its cache.
3346 while (*q && *q != '=' && *q != '[' && *q != ']')
3347 q = g_utf8_find_next_char (q, NULL);
3349 /* No empty keys, please */
3353 /* We accept spaces in the middle of keys to not break
3354 * existing apps, but we don't tolerate initial or final
3355 * spaces, which would lead to silent corruption when
3356 * rereading the file.
3358 if (*p == ' ' || q[-1] == ' ')
3364 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3365 q = g_utf8_find_next_char (q, NULL);
3379 /* A group in a key file is made up of a starting '[' followed by one
3380 * or more letters making up the group name followed by ']'.
3383 g_key_file_line_is_group (const gchar *line)
3393 while (*p && *p != ']')
3394 p = g_utf8_find_next_char (p, NULL);
3399 /* silently accept whitespace after the ] */
3400 p = g_utf8_find_next_char (p, NULL);
3401 while (*p == ' ' || *p == '\t')
3402 p = g_utf8_find_next_char (p, NULL);
3411 g_key_file_line_is_key_value_pair (const gchar *line)
3415 p = (gchar *) g_utf8_strchr (line, -1, '=');
3420 /* Key must be non-empty
3429 g_key_file_parse_value_as_string (GKeyFile *key_file,
3434 gchar *string_value, *p, *q0, *q;
3436 string_value = g_new (gchar, strlen (value) + 1);
3438 p = (gchar *) value;
3439 q0 = q = string_value;
3469 g_set_error (error, G_KEY_FILE_ERROR,
3470 G_KEY_FILE_ERROR_INVALID_VALUE,
3471 _("Key file contains escape character "
3476 if (pieces && *p == key_file->list_separator)
3477 *q = key_file->list_separator;
3491 g_set_error (error, G_KEY_FILE_ERROR,
3492 G_KEY_FILE_ERROR_INVALID_VALUE,
3493 _("Key file contains invalid escape "
3494 "sequence '%s'"), sequence);
3503 if (pieces && (*p == key_file->list_separator))
3505 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3521 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3522 *pieces = g_slist_reverse (*pieces);
3525 return string_value;
3529 g_key_file_parse_string_as_value (GKeyFile *key_file,
3530 const gchar *string,
3531 gboolean escape_separator)
3533 gchar *value, *p, *q;
3535 gboolean parsing_leading_space;
3537 length = strlen (string) + 1;
3539 /* Worst case would be that every character needs to be escaped.
3540 * In other words every character turns to two characters
3542 value = g_new (gchar, 2 * length);
3544 p = (gchar *) string;
3546 parsing_leading_space = TRUE;
3547 while (p < (string + length - 1))
3549 gchar escaped_character[3] = { '\\', 0, 0 };
3554 if (parsing_leading_space)
3556 escaped_character[1] = 's';
3557 strcpy (q, escaped_character);
3567 if (parsing_leading_space)
3569 escaped_character[1] = 't';
3570 strcpy (q, escaped_character);
3580 escaped_character[1] = 'n';
3581 strcpy (q, escaped_character);
3585 escaped_character[1] = 'r';
3586 strcpy (q, escaped_character);
3590 escaped_character[1] = '\\';
3591 strcpy (q, escaped_character);
3593 parsing_leading_space = FALSE;
3596 if (escape_separator && *p == key_file->list_separator)
3598 escaped_character[1] = key_file->list_separator;
3599 strcpy (q, escaped_character);
3601 parsing_leading_space = TRUE;
3607 parsing_leading_space = FALSE;
3619 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3623 gchar *end_of_valid_int;
3628 long_value = strtol (value, &end_of_valid_int, 10);
3630 if (*value == '\0' || *end_of_valid_int != '\0')
3632 gchar *value_utf8 = _g_utf8_make_valid (value);
3633 g_set_error (error, G_KEY_FILE_ERROR,
3634 G_KEY_FILE_ERROR_INVALID_VALUE,
3635 _("Value '%s' cannot be interpreted "
3636 "as a number."), value_utf8);
3637 g_free (value_utf8);
3642 int_value = long_value;
3643 if (int_value != long_value || errno == ERANGE)
3645 gchar *value_utf8 = _g_utf8_make_valid (value);
3648 G_KEY_FILE_ERROR_INVALID_VALUE,
3649 _("Integer value '%s' out of range"),
3651 g_free (value_utf8);
3660 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3664 return g_strdup_printf ("%d", value);
3668 g_key_file_parse_value_as_double (GKeyFile *key_file,
3672 gchar *end_of_valid_d;
3673 gdouble double_value = 0;
3675 double_value = g_ascii_strtod (value, &end_of_valid_d);
3677 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3679 gchar *value_utf8 = _g_utf8_make_valid (value);
3680 g_set_error (error, G_KEY_FILE_ERROR,
3681 G_KEY_FILE_ERROR_INVALID_VALUE,
3682 _("Value '%s' cannot be interpreted "
3683 "as a float number."),
3685 g_free (value_utf8);
3688 return double_value;
3692 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3698 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3700 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3703 value_utf8 = _g_utf8_make_valid (value);
3704 g_set_error (error, G_KEY_FILE_ERROR,
3705 G_KEY_FILE_ERROR_INVALID_VALUE,
3706 _("Value '%s' cannot be interpreted "
3707 "as a boolean."), value_utf8);
3708 g_free (value_utf8);
3714 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3718 return g_strdup ("true");
3720 return g_strdup ("false");
3724 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3731 string = g_string_sized_new (512);
3733 lines = g_strsplit (value, "\n", 0);
3735 for (i = 0; lines[i] != NULL; i++)
3737 if (lines[i][0] != '#')
3738 g_string_append_printf (string, "%s\n", lines[i]);
3740 g_string_append_printf (string, "%s\n", lines[i] + 1);
3744 return g_string_free (string, FALSE);
3748 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3749 const gchar *comment)
3755 string = g_string_sized_new (512);
3757 lines = g_strsplit (comment, "\n", 0);
3759 for (i = 0; lines[i] != NULL; i++)
3760 g_string_append_printf (string, "#%s%s", lines[i],
3761 lines[i + 1] == NULL? "" : "\n");
3764 return g_string_free (string, FALSE);
3767 #define __G_KEY_FILE_C__
3768 #include "galiasdef.c"