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>
42 #define fstat(a,b) _fstati64(a,b)
46 #define S_ISREG(mode) ((mode)&_S_IFREG)
49 #endif /* G_OS_WIN23 */
54 #include "gfileutils.h"
60 #include "gmessages.h"
63 #include "gstrfuncs.h"
68 typedef struct _GKeyFileGroup GKeyFileGroup;
73 GHashTable *group_hash;
75 GKeyFileGroup *start_group;
76 GKeyFileGroup *current_group;
78 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
80 /* Used for sizing the output buffer during serialization
82 gsize approximate_size;
91 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
95 const gchar *name; /* NULL for above first group (which will be comments) */
97 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
98 gboolean has_trailing_blank_line;
100 GList *key_value_pairs;
102 /* Used in parallel with key_value_pairs for
103 * increased lookup performance
105 GHashTable *lookup_map;
108 struct _GKeyFileKeyValuePair
110 gchar *key; /* NULL for comments */
114 static gint find_file_in_data_dirs (const gchar *file,
115 const gchar **data_dirs,
118 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
122 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
123 const gchar *group_name);
124 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
125 const gchar *group_name);
127 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
128 GKeyFileGroup *group,
130 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
131 GKeyFileGroup *group,
134 static void g_key_file_remove_group_node (GKeyFile *key_file,
136 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
137 GKeyFileGroup *group,
140 static void g_key_file_add_key (GKeyFile *key_file,
141 GKeyFileGroup *group,
144 static void g_key_file_add_group (GKeyFile *key_file,
145 const gchar *group_name);
146 static gboolean g_key_file_is_group_name (const gchar *name);
147 static gboolean g_key_file_is_key_name (const gchar *name);
148 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
149 static gboolean g_key_file_line_is_comment (const gchar *line);
150 static gboolean g_key_file_line_is_group (const gchar *line);
151 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
152 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
156 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
158 gboolean escape_separator);
159 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
162 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
164 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
167 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
170 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
172 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
174 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
175 const gchar *comment);
176 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
180 static void g_key_file_parse_comment (GKeyFile *key_file,
184 static void g_key_file_parse_group (GKeyFile *key_file,
188 static gchar *key_get_locale (const gchar *key);
189 static void g_key_file_parse_data (GKeyFile *key_file,
193 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
198 g_key_file_error_quark (void)
200 return g_quark_from_static_string ("g-key-file-error-quark");
204 g_key_file_init (GKeyFile *key_file)
206 key_file->current_group = g_slice_new0 (GKeyFileGroup);
207 key_file->groups = g_list_prepend (NULL, key_file->current_group);
208 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
209 key_file->start_group = NULL;
210 key_file->parse_buffer = g_string_sized_new (128);
211 key_file->approximate_size = 0;
212 key_file->list_separator = ';';
214 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
218 g_key_file_clear (GKeyFile *key_file)
220 GList *tmp, *group_node;
222 if (key_file->locales)
224 g_strfreev (key_file->locales);
225 key_file->locales = NULL;
228 if (key_file->parse_buffer)
230 g_string_free (key_file->parse_buffer, TRUE);
231 key_file->parse_buffer = NULL;
234 tmp = key_file->groups;
239 g_key_file_remove_group_node (key_file, group_node);
242 g_hash_table_destroy (key_file->group_hash);
243 key_file->group_hash = NULL;
245 g_warn_if_fail (key_file->groups == NULL);
252 * Creates a new empty #GKeyFile object. Use
253 * g_key_file_load_from_file(), g_key_file_load_from_data(),
254 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
255 * read an existing key file.
257 * Return value: an empty #GKeyFile.
262 g_key_file_new (void)
266 key_file = g_slice_new0 (GKeyFile);
267 g_key_file_init (key_file);
273 * g_key_file_set_list_separator:
274 * @key_file: a #GKeyFile
275 * @separator: the separator
277 * Sets the character which is used to separate
278 * values in lists. Typically ';' or ',' are used
279 * as separators. The default list separator is ';'.
284 g_key_file_set_list_separator (GKeyFile *key_file,
287 g_return_if_fail (key_file != NULL);
289 key_file->list_separator = separator;
293 /* Iterates through all the directories in *dirs trying to
294 * open file. When it successfully locates and opens a file it
295 * returns the file descriptor to the open file. It also
296 * outputs the absolute path of the file in output_file.
299 find_file_in_data_dirs (const gchar *file,
304 const gchar **data_dirs, *data_dir;
316 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
318 gchar *candidate_file, *sub_dir;
320 candidate_file = (gchar *) file;
321 sub_dir = g_strdup ("");
322 while (candidate_file != NULL && fd < 0)
326 path = g_build_filename (data_dir, sub_dir,
327 candidate_file, NULL);
329 fd = g_open (path, O_RDONLY, 0);
337 candidate_file = strchr (candidate_file, '-');
339 if (candidate_file == NULL)
345 sub_dir = g_strndup (file, candidate_file - file - 1);
347 for (p = sub_dir; *p != '\0'; p++)
350 *p = G_DIR_SEPARATOR;
359 g_set_error_literal (error, G_KEY_FILE_ERROR,
360 G_KEY_FILE_ERROR_NOT_FOUND,
361 _("Valid key file could not be "
362 "found in search dirs"));
365 if (output_file != NULL && fd > 0)
366 *output_file = g_strdup (path);
374 g_key_file_load_from_fd (GKeyFile *key_file,
379 GError *key_file_error = NULL;
381 struct stat stat_buf;
382 gchar read_buf[4096];
384 if (fstat (fd, &stat_buf) < 0)
386 g_set_error_literal (error, G_FILE_ERROR,
387 g_file_error_from_errno (errno),
392 if (!S_ISREG (stat_buf.st_mode))
394 g_set_error_literal (error, G_KEY_FILE_ERROR,
395 G_KEY_FILE_ERROR_PARSE,
396 _("Not a regular file"));
400 if (stat_buf.st_size == 0)
402 g_set_error_literal (error, G_KEY_FILE_ERROR,
403 G_KEY_FILE_ERROR_PARSE,
408 if (key_file->approximate_size > 0)
410 g_key_file_clear (key_file);
411 g_key_file_init (key_file);
413 key_file->flags = flags;
417 bytes_read = read (fd, read_buf, 4096);
419 if (bytes_read == 0) /* End of File */
424 if (errno == EINTR || errno == EAGAIN)
427 g_set_error_literal (error, G_FILE_ERROR,
428 g_file_error_from_errno (errno),
433 g_key_file_parse_data (key_file,
434 read_buf, bytes_read,
437 while (!key_file_error);
441 g_propagate_error (error, key_file_error);
445 g_key_file_flush_parse_buffer (key_file, &key_file_error);
449 g_propagate_error (error, key_file_error);
457 * g_key_file_load_from_file:
458 * @key_file: an empty #GKeyFile struct
459 * @file: the path of a filename to load, in the GLib filename encoding
460 * @flags: flags from #GKeyFileFlags
461 * @error: return location for a #GError, or %NULL
463 * Loads a key file into an empty #GKeyFile structure.
464 * If the file could not be loaded then %error is set to
465 * either a #GFileError or #GKeyFileError.
467 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
472 g_key_file_load_from_file (GKeyFile *key_file,
477 GError *key_file_error = NULL;
480 g_return_val_if_fail (key_file != NULL, FALSE);
481 g_return_val_if_fail (file != NULL, FALSE);
483 fd = g_open (file, O_RDONLY, 0);
487 g_set_error_literal (error, G_FILE_ERROR,
488 g_file_error_from_errno (errno),
493 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
498 g_propagate_error (error, key_file_error);
506 * g_key_file_load_from_data:
507 * @key_file: an empty #GKeyFile struct
508 * @data: key file loaded in memory
509 * @length: the length of @data in bytes
510 * @flags: flags from #GKeyFileFlags
511 * @error: return location for a #GError, or %NULL
513 * Loads a key file from memory into an empty #GKeyFile structure.
514 * If the object cannot be created then %error is set to a #GKeyFileError.
516 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
521 g_key_file_load_from_data (GKeyFile *key_file,
527 GError *key_file_error = NULL;
529 g_return_val_if_fail (key_file != NULL, FALSE);
530 g_return_val_if_fail (data != NULL, FALSE);
531 g_return_val_if_fail (length != 0, FALSE);
533 if (length == (gsize)-1)
534 length = strlen (data);
536 if (key_file->approximate_size > 0)
538 g_key_file_clear (key_file);
539 g_key_file_init (key_file);
541 key_file->flags = flags;
543 g_key_file_parse_data (key_file, data, length, &key_file_error);
547 g_propagate_error (error, key_file_error);
551 g_key_file_flush_parse_buffer (key_file, &key_file_error);
555 g_propagate_error (error, key_file_error);
563 * g_key_file_load_from_dirs:
564 * @key_file: an empty #GKeyFile struct
565 * @file: a relative path to a filename to open and parse
566 * @search_dirs: %NULL-terminated array of directories to search
567 * @full_path: return location for a string containing the full path
568 * of the file, or %NULL
569 * @flags: flags from #GKeyFileFlags
570 * @error: return location for a #GError, or %NULL
572 * This function looks for a key file named @file in the paths
573 * specified in @search_dirs, loads the file into @key_file and
574 * returns the file's full path in @full_path. If the file could not
575 * be loaded then an %error is set to either a #GFileError or
578 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
583 g_key_file_load_from_dirs (GKeyFile *key_file,
585 const gchar **search_dirs,
590 GError *key_file_error = NULL;
591 const gchar **data_dirs;
596 g_return_val_if_fail (key_file != NULL, FALSE);
597 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
598 g_return_val_if_fail (search_dirs != NULL, FALSE);
601 data_dirs = search_dirs;
603 while (*data_dirs != NULL && !found_file)
605 g_free (output_path);
607 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
613 g_propagate_error (error, key_file_error);
617 found_file = g_key_file_load_from_fd (key_file, fd, flags,
623 g_propagate_error (error, key_file_error);
628 if (found_file && full_path)
629 *full_path = output_path;
631 g_free (output_path);
637 * g_key_file_load_from_data_dirs:
638 * @key_file: an empty #GKeyFile struct
639 * @file: a relative path to a filename to open and parse
640 * @full_path: return location for a string containing the full path
641 * of the file, or %NULL
642 * @flags: flags from #GKeyFileFlags
643 * @error: return location for a #GError, or %NULL
645 * This function looks for a key file named @file in the paths
646 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
647 * loads the file into @key_file and returns the file's full path in
648 * @full_path. If the file could not be loaded then an %error is
649 * set to either a #GFileError or #GKeyFileError.
651 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
655 g_key_file_load_from_data_dirs (GKeyFile *key_file,
661 gchar **all_data_dirs;
662 const gchar * user_data_dir;
663 const gchar * const * system_data_dirs;
667 g_return_val_if_fail (key_file != NULL, FALSE);
668 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
670 user_data_dir = g_get_user_data_dir ();
671 system_data_dirs = g_get_system_data_dirs ();
672 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
675 all_data_dirs[i++] = g_strdup (user_data_dir);
678 while (system_data_dirs[j] != NULL)
679 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
680 all_data_dirs[i] = NULL;
682 found_file = g_key_file_load_from_dirs (key_file,
684 (const gchar **)all_data_dirs,
689 g_strfreev (all_data_dirs);
696 * @key_file: a #GKeyFile
703 g_key_file_free (GKeyFile *key_file)
705 g_return_if_fail (key_file != NULL);
707 g_key_file_clear (key_file);
708 g_slice_free (GKeyFile, key_file);
711 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
712 * true for locales that match those in g_get_language_names().
715 g_key_file_locale_is_interesting (GKeyFile *key_file,
720 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
723 for (i = 0; key_file->locales[i] != NULL; i++)
725 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
733 g_key_file_parse_line (GKeyFile *key_file,
738 GError *parse_error = NULL;
741 g_return_if_fail (key_file != NULL);
742 g_return_if_fail (line != NULL);
744 line_start = (gchar *) line;
745 while (g_ascii_isspace (*line_start))
748 if (g_key_file_line_is_comment (line_start))
749 g_key_file_parse_comment (key_file, line, length, &parse_error);
750 else if (g_key_file_line_is_group (line_start))
751 g_key_file_parse_group (key_file, line_start,
752 length - (line_start - line),
754 else if (g_key_file_line_is_key_value_pair (line_start))
755 g_key_file_parse_key_value_pair (key_file, line_start,
756 length - (line_start - line),
760 gchar *line_utf8 = _g_utf8_make_valid (line);
761 g_set_error (error, G_KEY_FILE_ERROR,
762 G_KEY_FILE_ERROR_PARSE,
763 _("Key file contains line '%s' which is not "
764 "a key-value pair, group, or comment"),
772 g_propagate_error (error, parse_error);
776 g_key_file_parse_comment (GKeyFile *key_file,
781 GKeyFileKeyValuePair *pair;
783 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
786 g_warn_if_fail (key_file->current_group != NULL);
788 pair = g_slice_new (GKeyFileKeyValuePair);
790 pair->value = g_strndup (line, length);
792 key_file->current_group->key_value_pairs =
793 g_list_prepend (key_file->current_group->key_value_pairs, pair);
795 if (length == 0 || line[0] != '#')
796 key_file->current_group->has_trailing_blank_line = TRUE;
800 g_key_file_parse_group (GKeyFile *key_file,
806 const gchar *group_name_start, *group_name_end;
808 /* advance past opening '['
810 group_name_start = line + 1;
811 group_name_end = line + length - 1;
813 while (*group_name_end != ']')
816 group_name = g_strndup (group_name_start,
817 group_name_end - group_name_start);
819 if (!g_key_file_is_group_name (group_name))
821 g_set_error (error, G_KEY_FILE_ERROR,
822 G_KEY_FILE_ERROR_PARSE,
823 _("Invalid group name: %s"), group_name);
828 g_key_file_add_group (key_file, group_name);
833 g_key_file_parse_key_value_pair (GKeyFile *key_file,
838 gchar *key, *value, *key_end, *value_start, *locale;
839 gsize key_len, value_len;
841 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
843 g_set_error_literal (error, G_KEY_FILE_ERROR,
844 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
845 _("Key file does not start with a group"));
849 key_end = value_start = strchr (line, '=');
851 g_warn_if_fail (key_end != NULL);
856 /* Pull the key name from the line (chomping trailing whitespace)
858 while (g_ascii_isspace (*key_end))
861 key_len = key_end - line + 2;
863 g_warn_if_fail (key_len <= length);
865 key = g_strndup (line, key_len - 1);
867 if (!g_key_file_is_key_name (key))
869 g_set_error (error, G_KEY_FILE_ERROR,
870 G_KEY_FILE_ERROR_PARSE,
871 _("Invalid key name: %s"), key);
876 /* Pull the value from the line (chugging leading whitespace)
878 while (g_ascii_isspace (*value_start))
881 value_len = line + length - value_start + 1;
883 value = g_strndup (value_start, value_len);
885 g_warn_if_fail (key_file->start_group != NULL);
887 if (key_file->current_group
888 && key_file->current_group->name
889 && strcmp (key_file->start_group->name,
890 key_file->current_group->name) == 0
891 && strcmp (key, "Encoding") == 0)
893 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
895 gchar *value_utf8 = _g_utf8_make_valid (value);
896 g_set_error (error, G_KEY_FILE_ERROR,
897 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
898 _("Key file contains unsupported "
899 "encoding '%s'"), value_utf8);
908 /* Is this key a translation? If so, is it one that we care about?
910 locale = key_get_locale (key);
912 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
913 g_key_file_add_key (key_file, key_file->current_group, key, value);
921 key_get_locale (const gchar *key)
925 locale = g_strrstr (key, "[");
927 if (locale && strlen (locale) <= 2)
931 locale = g_strndup (locale + 1, strlen (locale) - 2);
937 g_key_file_parse_data (GKeyFile *key_file,
945 g_return_if_fail (key_file != NULL);
946 g_return_if_fail (data != NULL);
950 for (i = 0; i < length; i++)
954 if (i > 0 && data[i - 1] == '\r')
955 g_string_erase (key_file->parse_buffer,
956 key_file->parse_buffer->len - 1,
959 /* When a newline is encountered flush the parse buffer so that the
960 * line can be parsed. Note that completely blank lines won't show
961 * up in the parse buffer, so they get parsed directly.
963 if (key_file->parse_buffer->len > 0)
964 g_key_file_flush_parse_buffer (key_file, &parse_error);
966 g_key_file_parse_comment (key_file, "", 1, &parse_error);
970 g_propagate_error (error, parse_error);
975 g_string_append_c (key_file->parse_buffer, data[i]);
978 key_file->approximate_size += length;
982 g_key_file_flush_parse_buffer (GKeyFile *key_file,
985 GError *file_error = NULL;
987 g_return_if_fail (key_file != NULL);
991 if (key_file->parse_buffer->len > 0)
993 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
994 key_file->parse_buffer->len,
996 g_string_erase (key_file->parse_buffer, 0, -1);
1000 g_propagate_error (error, file_error);
1007 * g_key_file_to_data:
1008 * @key_file: a #GKeyFile
1009 * @length: return location for the length of the
1010 * returned string, or %NULL
1011 * @error: return location for a #GError, or %NULL
1013 * This function outputs @key_file as a string.
1015 * Note that this function never reports an error,
1016 * so it is safe to pass %NULL as @error.
1018 * Return value: a newly allocated string holding
1019 * the contents of the #GKeyFile
1024 g_key_file_to_data (GKeyFile *key_file,
1028 GString *data_string;
1029 GList *group_node, *key_file_node;
1030 gboolean has_blank_line = TRUE;
1032 g_return_val_if_fail (key_file != NULL, NULL);
1034 data_string = g_string_sized_new (2 * key_file->approximate_size);
1036 for (group_node = g_list_last (key_file->groups);
1038 group_node = group_node->prev)
1040 GKeyFileGroup *group;
1042 group = (GKeyFileGroup *) group_node->data;
1044 /* separate groups by at least an empty line */
1045 if (!has_blank_line)
1046 g_string_append_c (data_string, '\n');
1047 has_blank_line = group->has_trailing_blank_line;
1049 if (group->comment != NULL)
1050 g_string_append_printf (data_string, "%s\n", group->comment->value);
1052 if (group->name != NULL)
1053 g_string_append_printf (data_string, "[%s]\n", group->name);
1055 for (key_file_node = g_list_last (group->key_value_pairs);
1056 key_file_node != NULL;
1057 key_file_node = key_file_node->prev)
1059 GKeyFileKeyValuePair *pair;
1061 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1063 if (pair->key != NULL)
1064 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1066 g_string_append_printf (data_string, "%s\n", pair->value);
1071 *length = data_string->len;
1073 return g_string_free (data_string, FALSE);
1077 * g_key_file_get_keys:
1078 * @key_file: a #GKeyFile
1079 * @group_name: a group name
1080 * @length: return location for the number of keys returned, or %NULL
1081 * @error: return location for a #GError, or %NULL
1083 * Returns all keys for the group name @group_name. The array of
1084 * returned keys will be %NULL-terminated, so @length may
1085 * optionally be %NULL. In the event that the @group_name cannot
1086 * be found, %NULL is returned and @error is set to
1087 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1089 * Return value: a newly-allocated %NULL-terminated array of strings.
1090 * Use g_strfreev() to free it.
1095 g_key_file_get_keys (GKeyFile *key_file,
1096 const gchar *group_name,
1100 GKeyFileGroup *group;
1105 g_return_val_if_fail (key_file != NULL, NULL);
1106 g_return_val_if_fail (group_name != NULL, NULL);
1108 group = g_key_file_lookup_group (key_file, group_name);
1112 g_set_error (error, G_KEY_FILE_ERROR,
1113 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1114 _("Key file does not have group '%s'"),
1115 group_name ? group_name : "(null)");
1120 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1122 GKeyFileKeyValuePair *pair;
1124 pair = (GKeyFileKeyValuePair *) tmp->data;
1130 keys = g_new (gchar *, num_keys + 1);
1133 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1135 GKeyFileKeyValuePair *pair;
1137 pair = (GKeyFileKeyValuePair *) tmp->data;
1141 keys[i] = g_strdup (pair->key);
1146 keys[num_keys] = NULL;
1155 * g_key_file_get_start_group:
1156 * @key_file: a #GKeyFile
1158 * Returns the name of the start group of the file.
1160 * Return value: The start group of the key file.
1165 g_key_file_get_start_group (GKeyFile *key_file)
1167 g_return_val_if_fail (key_file != NULL, NULL);
1169 if (key_file->start_group)
1170 return g_strdup (key_file->start_group->name);
1176 * g_key_file_get_groups:
1177 * @key_file: a #GKeyFile
1178 * @length: return location for the number of returned groups, or %NULL
1180 * Returns all groups in the key file loaded with @key_file.
1181 * The array of returned groups will be %NULL-terminated, so
1182 * @length may optionally be %NULL.
1184 * Return value: a newly-allocated %NULL-terminated array of strings.
1185 * Use g_strfreev() to free it.
1189 g_key_file_get_groups (GKeyFile *key_file,
1194 gsize i, num_groups;
1196 g_return_val_if_fail (key_file != NULL, NULL);
1198 num_groups = g_list_length (key_file->groups);
1200 g_return_val_if_fail (num_groups > 0, NULL);
1202 group_node = g_list_last (key_file->groups);
1204 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1206 /* Only need num_groups instead of num_groups + 1
1207 * because the first group of the file (last in the
1208 * list) is always the comment group at the top,
1211 groups = g_new (gchar *, num_groups);
1215 for (group_node = group_node->prev;
1217 group_node = group_node->prev)
1219 GKeyFileGroup *group;
1221 group = (GKeyFileGroup *) group_node->data;
1223 g_warn_if_fail (group->name != NULL);
1225 groups[i++] = g_strdup (group->name);
1236 * g_key_file_get_value:
1237 * @key_file: a #GKeyFile
1238 * @group_name: a group name
1240 * @error: return location for a #GError, or %NULL
1242 * Returns the raw value associated with @key under @group_name.
1243 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1245 * In the event the key cannot be found, %NULL is returned and
1246 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1247 * event that the @group_name cannot be found, %NULL is returned
1248 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1251 * Return value: a newly allocated string or %NULL if the specified
1252 * key cannot be found.
1257 g_key_file_get_value (GKeyFile *key_file,
1258 const gchar *group_name,
1262 GKeyFileGroup *group;
1263 GKeyFileKeyValuePair *pair;
1264 gchar *value = NULL;
1266 g_return_val_if_fail (key_file != NULL, NULL);
1267 g_return_val_if_fail (group_name != NULL, NULL);
1268 g_return_val_if_fail (key != NULL, NULL);
1270 group = g_key_file_lookup_group (key_file, group_name);
1274 g_set_error (error, G_KEY_FILE_ERROR,
1275 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1276 _("Key file does not have group '%s'"),
1277 group_name ? group_name : "(null)");
1281 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1284 value = g_strdup (pair->value);
1286 g_set_error (error, G_KEY_FILE_ERROR,
1287 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1288 _("Key file does not have key '%s'"), key);
1294 * g_key_file_set_value:
1295 * @key_file: a #GKeyFile
1296 * @group_name: a group name
1300 * Associates a new value with @key under @group_name.
1302 * If @key cannot be found then it is created. If @group_name cannot
1303 * be found then it is created. To set an UTF-8 string which may contain
1304 * characters that need escaping (such as newlines or spaces), use
1305 * g_key_file_set_string().
1310 g_key_file_set_value (GKeyFile *key_file,
1311 const gchar *group_name,
1315 GKeyFileGroup *group;
1316 GKeyFileKeyValuePair *pair;
1318 g_return_if_fail (key_file != NULL);
1319 g_return_if_fail (g_key_file_is_group_name (group_name));
1320 g_return_if_fail (g_key_file_is_key_name (key));
1321 g_return_if_fail (value != NULL);
1323 group = g_key_file_lookup_group (key_file, group_name);
1327 g_key_file_add_group (key_file, group_name);
1328 group = (GKeyFileGroup *) key_file->groups->data;
1330 g_key_file_add_key (key_file, group, key, value);
1334 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1337 g_key_file_add_key (key_file, group, key, value);
1340 g_free (pair->value);
1341 pair->value = g_strdup (value);
1347 * g_key_file_get_string:
1348 * @key_file: a #GKeyFile
1349 * @group_name: a group name
1351 * @error: return location for a #GError, or %NULL
1353 * Returns the string value associated with @key under @group_name.
1354 * Unlike g_key_file_get_value(), this function handles escape sequences
1357 * In the event the key cannot be found, %NULL is returned and
1358 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1359 * event that the @group_name cannot be found, %NULL is returned
1360 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1362 * Return value: a newly allocated string or %NULL if the specified
1363 * key cannot be found.
1368 g_key_file_get_string (GKeyFile *key_file,
1369 const gchar *group_name,
1373 gchar *value, *string_value;
1374 GError *key_file_error;
1376 g_return_val_if_fail (key_file != NULL, NULL);
1377 g_return_val_if_fail (group_name != NULL, NULL);
1378 g_return_val_if_fail (key != NULL, NULL);
1380 key_file_error = NULL;
1382 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1386 g_propagate_error (error, key_file_error);
1390 if (!g_utf8_validate (value, -1, NULL))
1392 gchar *value_utf8 = _g_utf8_make_valid (value);
1393 g_set_error (error, G_KEY_FILE_ERROR,
1394 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1395 _("Key file contains key '%s' with value '%s' "
1396 "which is not UTF-8"), key, value_utf8);
1397 g_free (value_utf8);
1403 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1409 if (g_error_matches (key_file_error,
1411 G_KEY_FILE_ERROR_INVALID_VALUE))
1413 g_set_error (error, G_KEY_FILE_ERROR,
1414 G_KEY_FILE_ERROR_INVALID_VALUE,
1415 _("Key file contains key '%s' "
1416 "which has value that cannot be interpreted."),
1418 g_error_free (key_file_error);
1421 g_propagate_error (error, key_file_error);
1424 return string_value;
1428 * g_key_file_set_string:
1429 * @key_file: a #GKeyFile
1430 * @group_name: a group name
1434 * Associates a new string value with @key under @group_name.
1435 * If @key cannot be found then it is created.
1436 * If @group_name cannot be found then it is created.
1437 * Unlike g_key_file_set_value(), this function handles characters
1438 * that need escaping, such as newlines.
1443 g_key_file_set_string (GKeyFile *key_file,
1444 const gchar *group_name,
1446 const gchar *string)
1450 g_return_if_fail (key_file != NULL);
1451 g_return_if_fail (string != NULL);
1453 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1454 g_key_file_set_value (key_file, group_name, key, value);
1459 * g_key_file_get_string_list:
1460 * @key_file: a #GKeyFile
1461 * @group_name: a group name
1463 * @length: return location for the number of returned strings, or %NULL
1464 * @error: return location for a #GError, or %NULL
1466 * Returns the values associated with @key under @group_name.
1468 * In the event the key cannot be found, %NULL is returned and
1469 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1470 * event that the @group_name cannot be found, %NULL is returned
1471 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1473 * Return value: a %NULL-terminated string array or %NULL if the specified
1474 * key cannot be found. The array should be freed with g_strfreev().
1479 g_key_file_get_string_list (GKeyFile *key_file,
1480 const gchar *group_name,
1485 GError *key_file_error = NULL;
1486 gchar *value, *string_value, **values;
1488 GSList *p, *pieces = NULL;
1490 g_return_val_if_fail (key_file != NULL, NULL);
1491 g_return_val_if_fail (group_name != NULL, NULL);
1492 g_return_val_if_fail (key != NULL, NULL);
1497 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1501 g_propagate_error (error, key_file_error);
1505 if (!g_utf8_validate (value, -1, NULL))
1507 gchar *value_utf8 = _g_utf8_make_valid (value);
1508 g_set_error (error, G_KEY_FILE_ERROR,
1509 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1510 _("Key file contains key '%s' with value '%s' "
1511 "which is not UTF-8"), key, value_utf8);
1512 g_free (value_utf8);
1518 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1520 g_free (string_value);
1524 if (g_error_matches (key_file_error,
1526 G_KEY_FILE_ERROR_INVALID_VALUE))
1528 g_set_error (error, G_KEY_FILE_ERROR,
1529 G_KEY_FILE_ERROR_INVALID_VALUE,
1530 _("Key file contains key '%s' "
1531 "which has a value that cannot be interpreted."),
1533 g_error_free (key_file_error);
1536 g_propagate_error (error, key_file_error);
1541 len = g_slist_length (pieces);
1542 values = g_new (gchar *, len + 1);
1543 for (p = pieces, i = 0; p; p = p->next)
1544 values[i++] = p->data;
1547 g_slist_free (pieces);
1556 * g_key_file_set_string_list:
1557 * @key_file: a #GKeyFile
1558 * @group_name: a group name
1560 * @list: an array of string values
1561 * @length: number of string values in @list
1563 * Associates a list of string values for @key under @group_name.
1564 * If @key cannot be found then it is created.
1565 * If @group_name cannot be found then it is created.
1570 g_key_file_set_string_list (GKeyFile *key_file,
1571 const gchar *group_name,
1573 const gchar * const list[],
1576 GString *value_list;
1579 g_return_if_fail (key_file != NULL);
1580 g_return_if_fail (list != NULL || length == 0);
1582 value_list = g_string_sized_new (length * 128);
1583 for (i = 0; i < length && list[i] != NULL; i++)
1587 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1588 g_string_append (value_list, value);
1589 g_string_append_c (value_list, key_file->list_separator);
1594 g_key_file_set_value (key_file, group_name, key, value_list->str);
1595 g_string_free (value_list, TRUE);
1599 * g_key_file_set_locale_string:
1600 * @key_file: a #GKeyFile
1601 * @group_name: a group name
1603 * @locale: a locale identifier
1606 * Associates a string value for @key and @locale under @group_name.
1607 * If the translation for @key cannot be found then it is created.
1612 g_key_file_set_locale_string (GKeyFile *key_file,
1613 const gchar *group_name,
1615 const gchar *locale,
1616 const gchar *string)
1618 gchar *full_key, *value;
1620 g_return_if_fail (key_file != NULL);
1621 g_return_if_fail (key != NULL);
1622 g_return_if_fail (locale != NULL);
1623 g_return_if_fail (string != NULL);
1625 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1626 full_key = g_strdup_printf ("%s[%s]", key, locale);
1627 g_key_file_set_value (key_file, group_name, full_key, value);
1632 extern GSList *_g_compute_locale_variants (const gchar *locale);
1635 * g_key_file_get_locale_string:
1636 * @key_file: a #GKeyFile
1637 * @group_name: a group name
1639 * @locale: a locale identifier or %NULL
1640 * @error: return location for a #GError, or %NULL
1642 * Returns the value associated with @key under @group_name
1643 * translated in the given @locale if available. If @locale is
1644 * %NULL then the current locale is assumed.
1646 * If @key cannot be found then %NULL is returned and @error is set
1647 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1648 * with @key cannot be interpreted or no suitable translation can
1649 * be found then the untranslated value is returned.
1651 * Return value: a newly allocated string or %NULL if the specified
1652 * key cannot be found.
1657 g_key_file_get_locale_string (GKeyFile *key_file,
1658 const gchar *group_name,
1660 const gchar *locale,
1663 gchar *candidate_key, *translated_value;
1664 GError *key_file_error;
1666 gboolean free_languages = FALSE;
1669 g_return_val_if_fail (key_file != NULL, NULL);
1670 g_return_val_if_fail (group_name != NULL, NULL);
1671 g_return_val_if_fail (key != NULL, NULL);
1673 candidate_key = NULL;
1674 translated_value = NULL;
1675 key_file_error = NULL;
1681 list = _g_compute_locale_variants (locale);
1683 languages = g_new (gchar *, g_slist_length (list) + 1);
1684 for (l = list, i = 0; l; l = l->next, i++)
1685 languages[i] = l->data;
1686 languages[i] = NULL;
1688 g_slist_free (list);
1689 free_languages = TRUE;
1693 languages = (gchar **) g_get_language_names ();
1694 free_languages = FALSE;
1697 for (i = 0; languages[i]; i++)
1699 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1701 translated_value = g_key_file_get_string (key_file,
1703 candidate_key, NULL);
1704 g_free (candidate_key);
1706 if (translated_value)
1709 g_free (translated_value);
1710 translated_value = NULL;
1713 /* Fallback to untranslated key
1715 if (!translated_value)
1717 translated_value = g_key_file_get_string (key_file, group_name, key,
1720 if (!translated_value)
1721 g_propagate_error (error, key_file_error);
1725 g_strfreev (languages);
1727 return translated_value;
1731 * g_key_file_get_locale_string_list:
1732 * @key_file: a #GKeyFile
1733 * @group_name: a group name
1735 * @locale: a locale identifier or %NULL
1736 * @length: return location for the number of returned strings or %NULL
1737 * @error: return location for a #GError or %NULL
1739 * Returns the values associated with @key under @group_name
1740 * translated in the given @locale if available. If @locale is
1741 * %NULL then the current locale is assumed.
1743 * If @key cannot be found then %NULL is returned and @error is set
1744 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1745 * with @key cannot be interpreted or no suitable translations
1746 * can be found then the untranslated values are returned. The
1747 * returned array is %NULL-terminated, so @length may optionally
1750 * Return value: a newly allocated %NULL-terminated string array
1751 * or %NULL if the key isn't found. The string array should be freed
1752 * with g_strfreev().
1757 g_key_file_get_locale_string_list (GKeyFile *key_file,
1758 const gchar *group_name,
1760 const gchar *locale,
1764 GError *key_file_error;
1765 gchar **values, *value;
1766 char list_separator[2];
1769 g_return_val_if_fail (key_file != NULL, NULL);
1770 g_return_val_if_fail (group_name != NULL, NULL);
1771 g_return_val_if_fail (key != NULL, NULL);
1773 key_file_error = NULL;
1775 value = g_key_file_get_locale_string (key_file, group_name,
1780 g_propagate_error (error, key_file_error);
1789 len = strlen (value);
1790 if (value[len - 1] == key_file->list_separator)
1791 value[len - 1] = '\0';
1793 list_separator[0] = key_file->list_separator;
1794 list_separator[1] = '\0';
1795 values = g_strsplit (value, list_separator, 0);
1800 *length = g_strv_length (values);
1806 * g_key_file_set_locale_string_list:
1807 * @key_file: a #GKeyFile
1808 * @group_name: a group name
1810 * @locale: a locale identifier
1811 * @list: a %NULL-terminated array of locale string values
1812 * @length: the length of @list
1814 * Associates a list of string values for @key and @locale under
1815 * @group_name. If the translation for @key cannot be found then
1821 g_key_file_set_locale_string_list (GKeyFile *key_file,
1822 const gchar *group_name,
1824 const gchar *locale,
1825 const gchar * const list[],
1828 GString *value_list;
1832 g_return_if_fail (key_file != NULL);
1833 g_return_if_fail (key != NULL);
1834 g_return_if_fail (locale != NULL);
1835 g_return_if_fail (length != 0);
1837 value_list = g_string_sized_new (length * 128);
1838 for (i = 0; i < length && list[i] != NULL; i++)
1842 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1843 g_string_append (value_list, value);
1844 g_string_append_c (value_list, key_file->list_separator);
1849 full_key = g_strdup_printf ("%s[%s]", key, locale);
1850 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1852 g_string_free (value_list, TRUE);
1856 * g_key_file_get_boolean:
1857 * @key_file: a #GKeyFile
1858 * @group_name: a group name
1860 * @error: return location for a #GError
1862 * Returns the value associated with @key under @group_name as a
1865 * If @key cannot be found then %FALSE is returned and @error is set
1866 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1867 * associated with @key cannot be interpreted as a boolean then %FALSE
1868 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1870 * Return value: the value associated with the key as a boolean,
1871 * or %FALSE if the key was not found or could not be parsed.
1876 g_key_file_get_boolean (GKeyFile *key_file,
1877 const gchar *group_name,
1881 GError *key_file_error = NULL;
1883 gboolean bool_value;
1885 g_return_val_if_fail (key_file != NULL, FALSE);
1886 g_return_val_if_fail (group_name != NULL, FALSE);
1887 g_return_val_if_fail (key != NULL, FALSE);
1889 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1893 g_propagate_error (error, key_file_error);
1897 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1903 if (g_error_matches (key_file_error,
1905 G_KEY_FILE_ERROR_INVALID_VALUE))
1907 g_set_error (error, G_KEY_FILE_ERROR,
1908 G_KEY_FILE_ERROR_INVALID_VALUE,
1909 _("Key file contains key '%s' "
1910 "which has value that cannot be interpreted."),
1912 g_error_free (key_file_error);
1915 g_propagate_error (error, key_file_error);
1922 * g_key_file_set_boolean:
1923 * @key_file: a #GKeyFile
1924 * @group_name: a group name
1926 * @value: %TRUE or %FALSE
1928 * Associates a new boolean value with @key under @group_name.
1929 * If @key cannot be found then it is created.
1934 g_key_file_set_boolean (GKeyFile *key_file,
1935 const gchar *group_name,
1941 g_return_if_fail (key_file != NULL);
1943 result = g_key_file_parse_boolean_as_value (key_file, value);
1944 g_key_file_set_value (key_file, group_name, key, result);
1949 * g_key_file_get_boolean_list:
1950 * @key_file: a #GKeyFile
1951 * @group_name: a group name
1953 * @length: the number of booleans returned
1954 * @error: return location for a #GError
1956 * Returns the values associated with @key under @group_name as
1959 * If @key cannot be found then %NULL is returned and @error is set to
1960 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1961 * with @key cannot be interpreted as booleans then %NULL is returned
1962 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1964 * Return value: the values associated with the key as a list of
1965 * booleans, or %NULL if the key was not found or could not be parsed.
1970 g_key_file_get_boolean_list (GKeyFile *key_file,
1971 const gchar *group_name,
1976 GError *key_file_error;
1978 gboolean *bool_values;
1981 g_return_val_if_fail (key_file != NULL, NULL);
1982 g_return_val_if_fail (group_name != NULL, NULL);
1983 g_return_val_if_fail (key != NULL, NULL);
1988 key_file_error = NULL;
1990 values = g_key_file_get_string_list (key_file, group_name, key,
1991 &num_bools, &key_file_error);
1994 g_propagate_error (error, key_file_error);
1999 bool_values = g_new (gboolean, num_bools);
2001 for (i = 0; i < num_bools; i++)
2003 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2009 g_propagate_error (error, key_file_error);
2010 g_strfreev (values);
2011 g_free (bool_values);
2016 g_strfreev (values);
2019 *length = num_bools;
2025 * g_key_file_set_boolean_list:
2026 * @key_file: a #GKeyFile
2027 * @group_name: a group name
2029 * @list: an array of boolean values
2030 * @length: length of @list
2032 * Associates a list of boolean values with @key under @group_name.
2033 * If @key cannot be found then it is created.
2034 * If @group_name is %NULL, the start_group is used.
2039 g_key_file_set_boolean_list (GKeyFile *key_file,
2040 const gchar *group_name,
2045 GString *value_list;
2048 g_return_if_fail (key_file != NULL);
2049 g_return_if_fail (list != NULL);
2051 value_list = g_string_sized_new (length * 8);
2052 for (i = 0; i < length; i++)
2056 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2058 g_string_append (value_list, value);
2059 g_string_append_c (value_list, key_file->list_separator);
2064 g_key_file_set_value (key_file, group_name, key, value_list->str);
2065 g_string_free (value_list, TRUE);
2069 * g_key_file_get_integer:
2070 * @key_file: a #GKeyFile
2071 * @group_name: a group name
2073 * @error: return location for a #GError
2075 * Returns the value associated with @key under @group_name as an
2078 * If @key cannot be found then 0 is returned and @error is set to
2079 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2080 * with @key cannot be interpreted as an integer then 0 is returned
2081 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2083 * Return value: the value associated with the key as an integer, or
2084 * 0 if the key was not found or could not be parsed.
2089 g_key_file_get_integer (GKeyFile *key_file,
2090 const gchar *group_name,
2094 GError *key_file_error;
2098 g_return_val_if_fail (key_file != NULL, -1);
2099 g_return_val_if_fail (group_name != NULL, -1);
2100 g_return_val_if_fail (key != NULL, -1);
2102 key_file_error = NULL;
2104 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2108 g_propagate_error (error, key_file_error);
2112 int_value = g_key_file_parse_value_as_integer (key_file, value,
2118 if (g_error_matches (key_file_error,
2120 G_KEY_FILE_ERROR_INVALID_VALUE))
2122 g_set_error (error, G_KEY_FILE_ERROR,
2123 G_KEY_FILE_ERROR_INVALID_VALUE,
2124 _("Key file contains key '%s' in group '%s' "
2125 "which has value that cannot be interpreted."), key,
2127 g_error_free (key_file_error);
2130 g_propagate_error (error, key_file_error);
2137 * g_key_file_set_integer:
2138 * @key_file: a #GKeyFile
2139 * @group_name: a group name
2141 * @value: an integer value
2143 * Associates a new integer value with @key under @group_name.
2144 * If @key cannot be found then it is created.
2149 g_key_file_set_integer (GKeyFile *key_file,
2150 const gchar *group_name,
2156 g_return_if_fail (key_file != NULL);
2158 result = g_key_file_parse_integer_as_value (key_file, value);
2159 g_key_file_set_value (key_file, group_name, key, result);
2164 * g_key_file_get_integer_list:
2165 * @key_file: a #GKeyFile
2166 * @group_name: a group name
2168 * @length: the number of integers returned
2169 * @error: return location for a #GError
2171 * Returns the values associated with @key under @group_name as
2174 * If @key cannot be found then %NULL is returned and @error is set to
2175 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2176 * with @key cannot be interpreted as integers then %NULL is returned
2177 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2179 * Return value: the values associated with the key as a list of
2180 * integers, or %NULL if the key was not found or could not be parsed.
2185 g_key_file_get_integer_list (GKeyFile *key_file,
2186 const gchar *group_name,
2191 GError *key_file_error = NULL;
2196 g_return_val_if_fail (key_file != NULL, NULL);
2197 g_return_val_if_fail (group_name != NULL, NULL);
2198 g_return_val_if_fail (key != NULL, NULL);
2203 values = g_key_file_get_string_list (key_file, group_name, key,
2204 &num_ints, &key_file_error);
2207 g_propagate_error (error, key_file_error);
2212 int_values = g_new (gint, num_ints);
2214 for (i = 0; i < num_ints; i++)
2216 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2222 g_propagate_error (error, key_file_error);
2223 g_strfreev (values);
2224 g_free (int_values);
2229 g_strfreev (values);
2238 * g_key_file_set_integer_list:
2239 * @key_file: a #GKeyFile
2240 * @group_name: a group name
2242 * @list: an array of integer values
2243 * @length: number of integer values in @list
2245 * Associates a list of integer values with @key under @group_name.
2246 * If @key cannot be found then it is created.
2251 g_key_file_set_integer_list (GKeyFile *key_file,
2252 const gchar *group_name,
2260 g_return_if_fail (key_file != NULL);
2261 g_return_if_fail (list != NULL);
2263 values = g_string_sized_new (length * 16);
2264 for (i = 0; i < length; i++)
2268 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2270 g_string_append (values, value);
2271 g_string_append_c (values, key_file->list_separator);
2276 g_key_file_set_value (key_file, group_name, key, values->str);
2277 g_string_free (values, TRUE);
2281 * g_key_file_get_double:
2282 * @key_file: a #GKeyFile
2283 * @group_name: a group name
2285 * @error: return location for a #GError
2287 * Returns the value associated with @key under @group_name as a
2288 * double. If @group_name is %NULL, the start_group is used.
2290 * If @key cannot be found then 0.0 is returned and @error is set to
2291 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2292 * with @key cannot be interpreted as a double then 0.0 is returned
2293 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2295 * Return value: the value associated with the key as a double, or
2296 * 0.0 if the key was not found or could not be parsed.
2301 g_key_file_get_double (GKeyFile *key_file,
2302 const gchar *group_name,
2306 GError *key_file_error;
2308 gdouble double_value;
2310 g_return_val_if_fail (key_file != NULL, -1);
2311 g_return_val_if_fail (group_name != NULL, -1);
2312 g_return_val_if_fail (key != NULL, -1);
2314 key_file_error = NULL;
2316 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2320 g_propagate_error (error, key_file_error);
2324 double_value = g_key_file_parse_value_as_double (key_file, value,
2330 if (g_error_matches (key_file_error,
2332 G_KEY_FILE_ERROR_INVALID_VALUE))
2334 g_set_error (error, G_KEY_FILE_ERROR,
2335 G_KEY_FILE_ERROR_INVALID_VALUE,
2336 _("Key file contains key '%s' in group '%s' "
2337 "which has value that cannot be interpreted."), key,
2339 g_error_free (key_file_error);
2342 g_propagate_error (error, key_file_error);
2345 return double_value;
2349 * g_key_file_set_double:
2350 * @key_file: a #GKeyFile
2351 * @group_name: a group name
2353 * @value: an double value
2355 * Associates a new double value with @key under @group_name.
2356 * If @key cannot be found then it is created.
2361 g_key_file_set_double (GKeyFile *key_file,
2362 const gchar *group_name,
2366 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2368 g_return_if_fail (key_file != NULL);
2370 g_ascii_dtostr (result, sizeof (result), value);
2371 g_key_file_set_value (key_file, group_name, key, result);
2375 * g_key_file_get_double_list:
2376 * @key_file: a #GKeyFile
2377 * @group_name: a group name
2379 * @length: the number of doubles returned
2380 * @error: return location for a #GError
2382 * Returns the values associated with @key under @group_name as
2385 * If @key cannot be found then %NULL is returned and @error is set to
2386 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2387 * with @key cannot be interpreted as doubles then %NULL is returned
2388 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2390 * Return value: the values associated with the key as a list of
2391 * doubles, or %NULL if the key was not found or could not be parsed.
2396 g_key_file_get_double_list (GKeyFile *key_file,
2397 const gchar *group_name,
2402 GError *key_file_error = NULL;
2404 gdouble *double_values;
2405 gsize i, num_doubles;
2407 g_return_val_if_fail (key_file != NULL, NULL);
2408 g_return_val_if_fail (group_name != NULL, NULL);
2409 g_return_val_if_fail (key != NULL, NULL);
2414 values = g_key_file_get_string_list (key_file, group_name, key,
2415 &num_doubles, &key_file_error);
2418 g_propagate_error (error, key_file_error);
2423 double_values = g_new (gdouble, num_doubles);
2425 for (i = 0; i < num_doubles; i++)
2427 double_values[i] = g_key_file_parse_value_as_double (key_file,
2433 g_propagate_error (error, key_file_error);
2434 g_strfreev (values);
2435 g_free (double_values);
2440 g_strfreev (values);
2443 *length = num_doubles;
2445 return double_values;
2449 * g_key_file_set_double_list:
2450 * @key_file: a #GKeyFile
2451 * @group_name: a group name
2453 * @list: an array of double values
2454 * @length: number of double values in @list
2456 * Associates a list of double values with @key under
2457 * @group_name. If @key cannot be found then it is created.
2462 g_key_file_set_double_list (GKeyFile *key_file,
2463 const gchar *group_name,
2471 g_return_if_fail (key_file != NULL);
2472 g_return_if_fail (list != NULL);
2474 values = g_string_sized_new (length * 16);
2475 for (i = 0; i < length; i++)
2477 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2479 g_ascii_dtostr( result, sizeof (result), list[i] );
2481 g_string_append (values, result);
2482 g_string_append_c (values, key_file->list_separator);
2485 g_key_file_set_value (key_file, group_name, key, values->str);
2486 g_string_free (values, TRUE);
2490 g_key_file_set_key_comment (GKeyFile *key_file,
2491 const gchar *group_name,
2493 const gchar *comment,
2496 GKeyFileGroup *group;
2497 GKeyFileKeyValuePair *pair;
2498 GList *key_node, *comment_node, *tmp;
2500 group = g_key_file_lookup_group (key_file, group_name);
2503 g_set_error (error, G_KEY_FILE_ERROR,
2504 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2505 _("Key file does not have group '%s'"),
2506 group_name ? group_name : "(null)");
2511 /* First find the key the comments are supposed to be
2514 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2516 if (key_node == NULL)
2518 g_set_error (error, G_KEY_FILE_ERROR,
2519 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2520 _("Key file does not have key '%s' in group '%s'"),
2525 /* Then find all the comments already associated with the
2528 tmp = key_node->next;
2531 pair = (GKeyFileKeyValuePair *) tmp->data;
2533 if (pair->key != NULL)
2538 g_key_file_remove_key_value_pair_node (key_file, group,
2542 if (comment == NULL)
2545 /* Now we can add our new comment
2547 pair = g_slice_new (GKeyFileKeyValuePair);
2549 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2551 key_node = g_list_insert (key_node, pair, 1);
2557 g_key_file_set_group_comment (GKeyFile *key_file,
2558 const gchar *group_name,
2559 const gchar *comment,
2562 GKeyFileGroup *group;
2564 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2566 group = g_key_file_lookup_group (key_file, group_name);
2569 g_set_error (error, G_KEY_FILE_ERROR,
2570 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2571 _("Key file does not have group '%s'"),
2572 group_name ? group_name : "(null)");
2577 /* First remove any existing comment
2581 g_key_file_key_value_pair_free (group->comment);
2582 group->comment = NULL;
2585 if (comment == NULL)
2588 /* Now we can add our new comment
2590 group->comment = g_slice_new (GKeyFileKeyValuePair);
2591 group->comment->key = NULL;
2592 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2598 g_key_file_set_top_comment (GKeyFile *key_file,
2599 const gchar *comment,
2603 GKeyFileGroup *group;
2604 GKeyFileKeyValuePair *pair;
2606 /* The last group in the list should be the top (comments only)
2609 g_warn_if_fail (key_file->groups != NULL);
2610 group_node = g_list_last (key_file->groups);
2611 group = (GKeyFileGroup *) group_node->data;
2612 g_warn_if_fail (group->name == NULL);
2614 /* Note all keys must be comments at the top of
2615 * the file, so we can just free it all.
2617 if (group->key_value_pairs != NULL)
2619 g_list_foreach (group->key_value_pairs,
2620 (GFunc) g_key_file_key_value_pair_free,
2622 g_list_free (group->key_value_pairs);
2623 group->key_value_pairs = NULL;
2626 if (comment == NULL)
2629 pair = g_slice_new (GKeyFileKeyValuePair);
2631 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2633 group->key_value_pairs =
2634 g_list_prepend (group->key_value_pairs, pair);
2640 * g_key_file_set_comment:
2641 * @key_file: a #GKeyFile
2642 * @group_name: a group name, or %NULL
2644 * @comment: a comment
2645 * @error: return location for a #GError
2647 * Places a comment above @key from @group_name.
2648 * If @key is %NULL then @comment will be written above @group_name.
2649 * If both @key and @group_name are %NULL, then @comment will be
2650 * written above the first group in the file.
2652 * Returns: %TRUE if the comment was written, %FALSE otherwise
2657 g_key_file_set_comment (GKeyFile *key_file,
2658 const gchar *group_name,
2660 const gchar *comment,
2663 g_return_val_if_fail (key_file != NULL, FALSE);
2665 if (group_name != NULL && key != NULL)
2667 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2670 else if (group_name != NULL)
2672 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2677 if (!g_key_file_set_top_comment (key_file, comment, error))
2681 if (comment != NULL)
2682 key_file->approximate_size += strlen (comment);
2688 g_key_file_get_key_comment (GKeyFile *key_file,
2689 const gchar *group_name,
2693 GKeyFileGroup *group;
2694 GKeyFileKeyValuePair *pair;
2695 GList *key_node, *tmp;
2699 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2701 group = g_key_file_lookup_group (key_file, group_name);
2704 g_set_error (error, G_KEY_FILE_ERROR,
2705 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2706 _("Key file does not have group '%s'"),
2707 group_name ? group_name : "(null)");
2712 /* First find the key the comments are supposed to be
2715 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2717 if (key_node == NULL)
2719 g_set_error (error, G_KEY_FILE_ERROR,
2720 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2721 _("Key file does not have key '%s' in group '%s'"),
2728 /* Then find all the comments already associated with the
2729 * key and concatentate them.
2731 tmp = key_node->next;
2732 if (!key_node->next)
2735 pair = (GKeyFileKeyValuePair *) tmp->data;
2736 if (pair->key != NULL)
2741 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2743 if (pair->key != NULL)
2749 while (tmp != key_node)
2751 pair = (GKeyFileKeyValuePair *) tmp->data;
2754 string = g_string_sized_new (512);
2756 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2757 g_string_append (string, comment);
2765 comment = string->str;
2766 g_string_free (string, FALSE);
2775 get_group_comment (GKeyFile *key_file,
2776 GKeyFileGroup *group,
2785 tmp = group->key_value_pairs;
2788 GKeyFileKeyValuePair *pair;
2790 pair = (GKeyFileKeyValuePair *) tmp->data;
2792 if (pair->key != NULL)
2798 if (tmp->next == NULL)
2806 GKeyFileKeyValuePair *pair;
2808 pair = (GKeyFileKeyValuePair *) tmp->data;
2811 string = g_string_sized_new (512);
2813 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2814 g_string_append (string, comment);
2821 return g_string_free (string, FALSE);
2827 g_key_file_get_group_comment (GKeyFile *key_file,
2828 const gchar *group_name,
2832 GKeyFileGroup *group;
2834 group = g_key_file_lookup_group (key_file, group_name);
2837 g_set_error (error, G_KEY_FILE_ERROR,
2838 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2839 _("Key file does not have group '%s'"),
2840 group_name ? group_name : "(null)");
2846 return g_strdup (group->comment->value);
2848 group_node = g_key_file_lookup_group_node (key_file, group_name);
2849 group_node = group_node->next;
2850 group = (GKeyFileGroup *)group_node->data;
2851 return get_group_comment (key_file, group, error);
2855 g_key_file_get_top_comment (GKeyFile *key_file,
2859 GKeyFileGroup *group;
2861 /* The last group in the list should be the top (comments only)
2864 g_warn_if_fail (key_file->groups != NULL);
2865 group_node = g_list_last (key_file->groups);
2866 group = (GKeyFileGroup *) group_node->data;
2867 g_warn_if_fail (group->name == NULL);
2869 return get_group_comment (key_file, group, error);
2873 * g_key_file_get_comment:
2874 * @key_file: a #GKeyFile
2875 * @group_name: a group name, or %NULL
2877 * @error: return location for a #GError
2879 * Retrieves a comment above @key from @group_name.
2880 * If @key is %NULL then @comment will be read from above
2881 * @group_name. If both @key and @group_name are %NULL, then
2882 * @comment will be read from above the first group in the file.
2884 * Returns: a comment that should be freed with g_free()
2889 g_key_file_get_comment (GKeyFile *key_file,
2890 const gchar *group_name,
2894 g_return_val_if_fail (key_file != NULL, NULL);
2896 if (group_name != NULL && key != NULL)
2897 return g_key_file_get_key_comment (key_file, group_name, key, error);
2898 else if (group_name != NULL)
2899 return g_key_file_get_group_comment (key_file, group_name, error);
2901 return g_key_file_get_top_comment (key_file, error);
2905 * g_key_file_remove_comment:
2906 * @key_file: a #GKeyFile
2907 * @group_name: a group name, or %NULL
2909 * @error: return location for a #GError
2911 * Removes a comment above @key from @group_name.
2912 * If @key is %NULL then @comment will be removed above @group_name.
2913 * If both @key and @group_name are %NULL, then @comment will
2914 * be removed above the first group in the file.
2916 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2922 g_key_file_remove_comment (GKeyFile *key_file,
2923 const gchar *group_name,
2927 g_return_val_if_fail (key_file != NULL, FALSE);
2929 if (group_name != NULL && key != NULL)
2930 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2931 else if (group_name != NULL)
2932 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2934 return g_key_file_set_top_comment (key_file, NULL, error);
2938 * g_key_file_has_group:
2939 * @key_file: a #GKeyFile
2940 * @group_name: a group name
2942 * Looks whether the key file has the group @group_name.
2944 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2949 g_key_file_has_group (GKeyFile *key_file,
2950 const gchar *group_name)
2952 g_return_val_if_fail (key_file != NULL, FALSE);
2953 g_return_val_if_fail (group_name != NULL, FALSE);
2955 return g_key_file_lookup_group (key_file, group_name) != NULL;
2959 * g_key_file_has_key:
2960 * @key_file: a #GKeyFile
2961 * @group_name: a group name
2963 * @error: return location for a #GError
2965 * Looks whether the key file has the key @key in the group
2968 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2974 g_key_file_has_key (GKeyFile *key_file,
2975 const gchar *group_name,
2979 GKeyFileKeyValuePair *pair;
2980 GKeyFileGroup *group;
2982 g_return_val_if_fail (key_file != NULL, FALSE);
2983 g_return_val_if_fail (group_name != NULL, FALSE);
2984 g_return_val_if_fail (key != NULL, FALSE);
2986 group = g_key_file_lookup_group (key_file, group_name);
2990 g_set_error (error, G_KEY_FILE_ERROR,
2991 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2992 _("Key file does not have group '%s'"),
2993 group_name ? group_name : "(null)");
2998 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3000 return pair != NULL;
3004 g_key_file_add_group (GKeyFile *key_file,
3005 const gchar *group_name)
3007 GKeyFileGroup *group;
3009 g_return_if_fail (key_file != NULL);
3010 g_return_if_fail (g_key_file_is_group_name (group_name));
3012 group = g_key_file_lookup_group (key_file, group_name);
3015 key_file->current_group = group;
3019 group = g_slice_new0 (GKeyFileGroup);
3020 group->name = g_strdup (group_name);
3021 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3022 key_file->groups = g_list_prepend (key_file->groups, group);
3023 key_file->approximate_size += strlen (group_name) + 3;
3024 key_file->current_group = group;
3026 if (key_file->start_group == NULL)
3027 key_file->start_group = group;
3029 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3033 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3038 g_free (pair->value);
3039 g_slice_free (GKeyFileKeyValuePair, pair);
3043 /* Be careful not to call this function on a node with data in the
3044 * lookup map without removing it from the lookup map, first.
3046 * Some current cases where this warning is not a concern are
3048 * - the node being removed is a comment node
3049 * - the entire lookup map is getting destroyed soon after
3053 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3054 GKeyFileGroup *group,
3058 GKeyFileKeyValuePair *pair;
3060 pair = (GKeyFileKeyValuePair *) pair_node->data;
3062 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3064 if (pair->key != NULL)
3065 key_file->approximate_size -= strlen (pair->key) + 1;
3067 g_warn_if_fail (pair->value != NULL);
3068 key_file->approximate_size -= strlen (pair->value);
3070 g_key_file_key_value_pair_free (pair);
3072 g_list_free_1 (pair_node);
3076 g_key_file_remove_group_node (GKeyFile *key_file,
3079 GKeyFileGroup *group;
3082 group = (GKeyFileGroup *) group_node->data;
3085 g_hash_table_remove (key_file->group_hash, group->name);
3087 /* If the current group gets deleted make the current group the last
3090 if (key_file->current_group == group)
3092 /* groups should always contain at least the top comment group,
3093 * unless g_key_file_clear has been called
3095 if (key_file->groups)
3096 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3098 key_file->current_group = NULL;
3101 /* If the start group gets deleted make the start group the first
3104 if (key_file->start_group == group)
3106 tmp = g_list_last (key_file->groups);
3109 if (tmp != group_node &&
3110 ((GKeyFileGroup *) tmp->data)->name != NULL)
3117 key_file->start_group = (GKeyFileGroup *) tmp->data;
3119 key_file->start_group = NULL;
3122 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3124 if (group->name != NULL)
3125 key_file->approximate_size -= strlen (group->name) + 3;
3127 tmp = group->key_value_pairs;
3134 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3137 g_warn_if_fail (group->key_value_pairs == NULL);
3139 if (group->lookup_map)
3141 g_hash_table_destroy (group->lookup_map);
3142 group->lookup_map = NULL;
3145 g_free ((gchar *) group->name);
3146 g_slice_free (GKeyFileGroup, group);
3147 g_list_free_1 (group_node);
3151 * g_key_file_remove_group:
3152 * @key_file: a #GKeyFile
3153 * @group_name: a group name
3154 * @error: return location for a #GError or %NULL
3156 * Removes the specified group, @group_name,
3157 * from the key file.
3159 * Returns: %TRUE if the group was removed, %FALSE otherwise
3164 g_key_file_remove_group (GKeyFile *key_file,
3165 const gchar *group_name,
3170 g_return_val_if_fail (key_file != NULL, FALSE);
3171 g_return_val_if_fail (group_name != NULL, FALSE);
3173 group_node = g_key_file_lookup_group_node (key_file, group_name);
3177 g_set_error (error, G_KEY_FILE_ERROR,
3178 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3179 _("Key file does not have group '%s'"),
3184 g_key_file_remove_group_node (key_file, group_node);
3190 g_key_file_add_key (GKeyFile *key_file,
3191 GKeyFileGroup *group,
3195 GKeyFileKeyValuePair *pair;
3197 pair = g_slice_new (GKeyFileKeyValuePair);
3198 pair->key = g_strdup (key);
3199 pair->value = g_strdup (value);
3201 g_hash_table_replace (group->lookup_map, pair->key, pair);
3202 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3203 group->has_trailing_blank_line = FALSE;
3204 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3208 * g_key_file_remove_key:
3209 * @key_file: a #GKeyFile
3210 * @group_name: a group name
3211 * @key: a key name to remove
3212 * @error: return location for a #GError or %NULL
3214 * Removes @key in @group_name from the key file.
3216 * Returns: %TRUE if the key was removed, %FALSE otherwise
3221 g_key_file_remove_key (GKeyFile *key_file,
3222 const gchar *group_name,
3226 GKeyFileGroup *group;
3227 GKeyFileKeyValuePair *pair;
3229 g_return_val_if_fail (key_file != NULL, FALSE);
3230 g_return_val_if_fail (group_name != NULL, FALSE);
3231 g_return_val_if_fail (key != NULL, FALSE);
3235 group = g_key_file_lookup_group (key_file, group_name);
3238 g_set_error (error, G_KEY_FILE_ERROR,
3239 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3240 _("Key file does not have group '%s'"),
3241 group_name ? group_name : "(null)");
3245 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3249 g_set_error (error, G_KEY_FILE_ERROR,
3250 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3251 _("Key file does not have key '%s' in group '%s'"),
3256 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3258 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3259 g_hash_table_remove (group->lookup_map, pair->key);
3260 g_key_file_key_value_pair_free (pair);
3266 g_key_file_lookup_group_node (GKeyFile *key_file,
3267 const gchar *group_name)
3269 GKeyFileGroup *group;
3272 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3274 group = (GKeyFileGroup *) tmp->data;
3276 if (group && group->name && strcmp (group->name, group_name) == 0)
3283 static GKeyFileGroup *
3284 g_key_file_lookup_group (GKeyFile *key_file,
3285 const gchar *group_name)
3287 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3291 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3292 GKeyFileGroup *group,
3297 for (key_node = group->key_value_pairs;
3299 key_node = key_node->next)
3301 GKeyFileKeyValuePair *pair;
3303 pair = (GKeyFileKeyValuePair *) key_node->data;
3305 if (pair->key && strcmp (pair->key, key) == 0)
3312 static GKeyFileKeyValuePair *
3313 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3314 GKeyFileGroup *group,
3317 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3320 /* Lines starting with # or consisting entirely of whitespace are merely
3321 * recorded, not parsed. This function assumes all leading whitespace
3322 * has been stripped.
3325 g_key_file_line_is_comment (const gchar *line)
3327 return (*line == '#' || *line == '\0' || *line == '\n');
3331 g_key_file_is_group_name (const gchar *name)
3338 p = q = (gchar *) name;
3339 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3340 q = g_utf8_find_next_char (q, NULL);
3342 if (*q != '\0' || q == p)
3349 g_key_file_is_key_name (const gchar *name)
3356 p = q = (gchar *) name;
3357 /* We accept a little more than the desktop entry spec says,
3358 * since gnome-vfs uses mime-types as keys in its cache.
3360 while (*q && *q != '=' && *q != '[' && *q != ']')
3361 q = g_utf8_find_next_char (q, NULL);
3363 /* No empty keys, please */
3367 /* We accept spaces in the middle of keys to not break
3368 * existing apps, but we don't tolerate initial or final
3369 * spaces, which would lead to silent corruption when
3370 * rereading the file.
3372 if (*p == ' ' || q[-1] == ' ')
3378 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3379 q = g_utf8_find_next_char (q, NULL);
3393 /* A group in a key file is made up of a starting '[' followed by one
3394 * or more letters making up the group name followed by ']'.
3397 g_key_file_line_is_group (const gchar *line)
3407 while (*p && *p != ']')
3408 p = g_utf8_find_next_char (p, NULL);
3413 /* silently accept whitespace after the ] */
3414 p = g_utf8_find_next_char (p, NULL);
3415 while (*p == ' ' || *p == '\t')
3416 p = g_utf8_find_next_char (p, NULL);
3425 g_key_file_line_is_key_value_pair (const gchar *line)
3429 p = (gchar *) g_utf8_strchr (line, -1, '=');
3434 /* Key must be non-empty
3443 g_key_file_parse_value_as_string (GKeyFile *key_file,
3448 gchar *string_value, *p, *q0, *q;
3450 string_value = g_new (gchar, strlen (value) + 1);
3452 p = (gchar *) value;
3453 q0 = q = string_value;
3483 g_set_error_literal (error, G_KEY_FILE_ERROR,
3484 G_KEY_FILE_ERROR_INVALID_VALUE,
3485 _("Key file contains escape character "
3490 if (pieces && *p == key_file->list_separator)
3491 *q = key_file->list_separator;
3505 g_set_error (error, G_KEY_FILE_ERROR,
3506 G_KEY_FILE_ERROR_INVALID_VALUE,
3507 _("Key file contains invalid escape "
3508 "sequence '%s'"), sequence);
3517 if (pieces && (*p == key_file->list_separator))
3519 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3535 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3536 *pieces = g_slist_reverse (*pieces);
3539 return string_value;
3543 g_key_file_parse_string_as_value (GKeyFile *key_file,
3544 const gchar *string,
3545 gboolean escape_separator)
3547 gchar *value, *p, *q;
3549 gboolean parsing_leading_space;
3551 length = strlen (string) + 1;
3553 /* Worst case would be that every character needs to be escaped.
3554 * In other words every character turns to two characters
3556 value = g_new (gchar, 2 * length);
3558 p = (gchar *) string;
3560 parsing_leading_space = TRUE;
3561 while (p < (string + length - 1))
3563 gchar escaped_character[3] = { '\\', 0, 0 };
3568 if (parsing_leading_space)
3570 escaped_character[1] = 's';
3571 strcpy (q, escaped_character);
3581 if (parsing_leading_space)
3583 escaped_character[1] = 't';
3584 strcpy (q, escaped_character);
3594 escaped_character[1] = 'n';
3595 strcpy (q, escaped_character);
3599 escaped_character[1] = 'r';
3600 strcpy (q, escaped_character);
3604 escaped_character[1] = '\\';
3605 strcpy (q, escaped_character);
3607 parsing_leading_space = FALSE;
3610 if (escape_separator && *p == key_file->list_separator)
3612 escaped_character[1] = key_file->list_separator;
3613 strcpy (q, escaped_character);
3615 parsing_leading_space = TRUE;
3621 parsing_leading_space = FALSE;
3633 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3637 gchar *end_of_valid_int;
3642 long_value = strtol (value, &end_of_valid_int, 10);
3644 if (*value == '\0' || *end_of_valid_int != '\0')
3646 gchar *value_utf8 = _g_utf8_make_valid (value);
3647 g_set_error (error, G_KEY_FILE_ERROR,
3648 G_KEY_FILE_ERROR_INVALID_VALUE,
3649 _("Value '%s' cannot be interpreted "
3650 "as a number."), value_utf8);
3651 g_free (value_utf8);
3656 int_value = long_value;
3657 if (int_value != long_value || errno == ERANGE)
3659 gchar *value_utf8 = _g_utf8_make_valid (value);
3662 G_KEY_FILE_ERROR_INVALID_VALUE,
3663 _("Integer value '%s' out of range"),
3665 g_free (value_utf8);
3674 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3678 return g_strdup_printf ("%d", value);
3682 g_key_file_parse_value_as_double (GKeyFile *key_file,
3686 gchar *end_of_valid_d;
3687 gdouble double_value = 0;
3689 double_value = g_ascii_strtod (value, &end_of_valid_d);
3691 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3693 gchar *value_utf8 = _g_utf8_make_valid (value);
3694 g_set_error (error, G_KEY_FILE_ERROR,
3695 G_KEY_FILE_ERROR_INVALID_VALUE,
3696 _("Value '%s' cannot be interpreted "
3697 "as a float number."),
3699 g_free (value_utf8);
3702 return double_value;
3706 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3712 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3714 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3717 value_utf8 = _g_utf8_make_valid (value);
3718 g_set_error (error, G_KEY_FILE_ERROR,
3719 G_KEY_FILE_ERROR_INVALID_VALUE,
3720 _("Value '%s' cannot be interpreted "
3721 "as a boolean."), value_utf8);
3722 g_free (value_utf8);
3728 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3732 return g_strdup ("true");
3734 return g_strdup ("false");
3738 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3745 string = g_string_sized_new (512);
3747 lines = g_strsplit (value, "\n", 0);
3749 for (i = 0; lines[i] != NULL; i++)
3751 if (lines[i][0] != '#')
3752 g_string_append_printf (string, "%s\n", lines[i]);
3754 g_string_append_printf (string, "%s\n", lines[i] + 1);
3758 return g_string_free (string, FALSE);
3762 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3763 const gchar *comment)
3769 string = g_string_sized_new (512);
3771 lines = g_strsplit (comment, "\n", 0);
3773 for (i = 0; lines[i] != NULL; i++)
3774 g_string_append_printf (string, "#%s%s", lines[i],
3775 lines[i + 1] == NULL? "" : "\n");
3778 return g_string_free (string, FALSE);
3781 #define __G_KEY_FILE_C__
3782 #include "galiasdef.c"