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 fstat(a,b) _fstat(a,b)
47 #define S_ISREG(mode) ((mode)&_S_IFREG)
50 #endif /* G_OS_WIN23 */
55 #include "gfileutils.h"
61 #include "gmessages.h"
64 #include "gstrfuncs.h"
69 typedef struct _GKeyFileGroup GKeyFileGroup;
74 GHashTable *group_hash;
76 GKeyFileGroup *start_group;
77 GKeyFileGroup *current_group;
79 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
81 /* Used for sizing the output buffer during serialization
83 gsize approximate_size;
92 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
96 const gchar *name; /* NULL for above first group (which will be comments) */
98 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
99 gboolean has_trailing_blank_line;
101 GList *key_value_pairs;
103 /* Used in parallel with key_value_pairs for
104 * increased lookup performance
106 GHashTable *lookup_map;
109 struct _GKeyFileKeyValuePair
111 gchar *key; /* NULL for comments */
115 static gint find_file_in_data_dirs (const gchar *file,
116 const gchar **data_dirs,
119 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
123 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
124 const gchar *group_name);
125 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
126 const gchar *group_name);
128 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
129 GKeyFileGroup *group,
131 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
132 GKeyFileGroup *group,
135 static void g_key_file_remove_group_node (GKeyFile *key_file,
137 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
138 GKeyFileGroup *group,
141 static void g_key_file_add_key (GKeyFile *key_file,
142 GKeyFileGroup *group,
145 static void g_key_file_add_group (GKeyFile *key_file,
146 const gchar *group_name);
147 static gboolean g_key_file_is_group_name (const gchar *name);
148 static gboolean g_key_file_is_key_name (const gchar *name);
149 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
150 static gboolean g_key_file_line_is_comment (const gchar *line);
151 static gboolean g_key_file_line_is_group (const gchar *line);
152 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
153 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
157 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
159 gboolean escape_separator);
160 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
163 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
165 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
168 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
171 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
173 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
175 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
176 const gchar *comment);
177 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
181 static void g_key_file_parse_comment (GKeyFile *key_file,
185 static void g_key_file_parse_group (GKeyFile *key_file,
189 static gchar *key_get_locale (const gchar *key);
190 static void g_key_file_parse_data (GKeyFile *key_file,
194 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
199 g_key_file_error_quark (void)
201 return g_quark_from_static_string ("g-key-file-error-quark");
205 g_key_file_init (GKeyFile *key_file)
207 key_file->current_group = g_slice_new0 (GKeyFileGroup);
208 key_file->groups = g_list_prepend (NULL, key_file->current_group);
209 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
210 key_file->start_group = NULL;
211 key_file->parse_buffer = g_string_sized_new (128);
212 key_file->approximate_size = 0;
213 key_file->list_separator = ';';
215 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
219 g_key_file_clear (GKeyFile *key_file)
221 GList *tmp, *group_node;
223 if (key_file->locales)
225 g_strfreev (key_file->locales);
226 key_file->locales = NULL;
229 if (key_file->parse_buffer)
231 g_string_free (key_file->parse_buffer, TRUE);
232 key_file->parse_buffer = NULL;
235 tmp = key_file->groups;
240 g_key_file_remove_group_node (key_file, group_node);
243 g_hash_table_destroy (key_file->group_hash);
244 key_file->group_hash = NULL;
246 g_warn_if_fail (key_file->groups == NULL);
253 * Creates a new empty #GKeyFile object. Use
254 * g_key_file_load_from_file(), g_key_file_load_from_data(),
255 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
256 * read an existing key file.
258 * Return value: an empty #GKeyFile.
263 g_key_file_new (void)
267 key_file = g_slice_new0 (GKeyFile);
268 g_key_file_init (key_file);
274 * g_key_file_set_list_separator:
275 * @key_file: a #GKeyFile
276 * @separator: the separator
278 * Sets the character which is used to separate
279 * values in lists. Typically ';' or ',' are used
280 * as separators. The default list separator is ';'.
285 g_key_file_set_list_separator (GKeyFile *key_file,
288 g_return_if_fail (key_file != NULL);
290 key_file->list_separator = separator;
294 /* Iterates through all the directories in *dirs trying to
295 * open file. When it successfully locates and opens a file it
296 * returns the file descriptor to the open file. It also
297 * outputs the absolute path of the file in output_file.
300 find_file_in_data_dirs (const gchar *file,
305 const gchar **data_dirs, *data_dir;
317 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
319 gchar *candidate_file, *sub_dir;
321 candidate_file = (gchar *) file;
322 sub_dir = g_strdup ("");
323 while (candidate_file != NULL && fd < 0)
327 path = g_build_filename (data_dir, sub_dir,
328 candidate_file, NULL);
330 fd = g_open (path, O_RDONLY, 0);
338 candidate_file = strchr (candidate_file, '-');
340 if (candidate_file == NULL)
346 sub_dir = g_strndup (file, candidate_file - file - 1);
348 for (p = sub_dir; *p != '\0'; p++)
351 *p = G_DIR_SEPARATOR;
360 g_set_error_literal (error, G_KEY_FILE_ERROR,
361 G_KEY_FILE_ERROR_NOT_FOUND,
362 _("Valid key file could not be "
363 "found in search dirs"));
366 if (output_file != NULL && fd > 0)
367 *output_file = g_strdup (path);
375 g_key_file_load_from_fd (GKeyFile *key_file,
380 GError *key_file_error = NULL;
382 struct stat stat_buf;
383 gchar read_buf[4096];
385 if (fstat (fd, &stat_buf) < 0)
387 g_set_error_literal (error, G_FILE_ERROR,
388 g_file_error_from_errno (errno),
393 if (!S_ISREG (stat_buf.st_mode))
395 g_set_error_literal (error, G_KEY_FILE_ERROR,
396 G_KEY_FILE_ERROR_PARSE,
397 _("Not a regular file"));
401 if (stat_buf.st_size == 0)
403 g_set_error_literal (error, G_KEY_FILE_ERROR,
404 G_KEY_FILE_ERROR_PARSE,
409 if (key_file->approximate_size > 0)
411 g_key_file_clear (key_file);
412 g_key_file_init (key_file);
414 key_file->flags = flags;
419 bytes_read = read (fd, read_buf, 4096);
421 if (bytes_read == 0) /* End of File */
426 if (errno == EINTR || errno == EAGAIN)
429 g_set_error_literal (error, G_FILE_ERROR,
430 g_file_error_from_errno (errno),
435 g_key_file_parse_data (key_file,
436 read_buf, bytes_read,
439 while (!key_file_error);
443 g_propagate_error (error, key_file_error);
447 g_key_file_flush_parse_buffer (key_file, &key_file_error);
451 g_propagate_error (error, key_file_error);
459 * g_key_file_load_from_file:
460 * @key_file: an empty #GKeyFile struct
461 * @file: the path of a filename to load, in the GLib filename encoding
462 * @flags: flags from #GKeyFileFlags
463 * @error: return location for a #GError, or %NULL
465 * Loads a key file into an empty #GKeyFile structure.
466 * If the file could not be loaded then %error is set to
467 * either a #GFileError or #GKeyFileError.
469 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
474 g_key_file_load_from_file (GKeyFile *key_file,
479 GError *key_file_error = NULL;
482 g_return_val_if_fail (key_file != NULL, FALSE);
483 g_return_val_if_fail (file != NULL, FALSE);
485 fd = g_open (file, O_RDONLY, 0);
489 g_set_error_literal (error, G_FILE_ERROR,
490 g_file_error_from_errno (errno),
495 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
500 g_propagate_error (error, key_file_error);
508 * g_key_file_load_from_data:
509 * @key_file: an empty #GKeyFile struct
510 * @data: key file loaded in memory
511 * @length: the length of @data in bytes
512 * @flags: flags from #GKeyFileFlags
513 * @error: return location for a #GError, or %NULL
515 * Loads a key file from memory into an empty #GKeyFile structure.
516 * If the object cannot be created then %error is set to a #GKeyFileError.
518 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
523 g_key_file_load_from_data (GKeyFile *key_file,
529 GError *key_file_error = NULL;
531 g_return_val_if_fail (key_file != NULL, FALSE);
532 g_return_val_if_fail (data != NULL, FALSE);
533 g_return_val_if_fail (length != 0, FALSE);
535 if (length == (gsize)-1)
536 length = strlen (data);
538 if (key_file->approximate_size > 0)
540 g_key_file_clear (key_file);
541 g_key_file_init (key_file);
543 key_file->flags = flags;
545 g_key_file_parse_data (key_file, data, length, &key_file_error);
549 g_propagate_error (error, key_file_error);
553 g_key_file_flush_parse_buffer (key_file, &key_file_error);
557 g_propagate_error (error, key_file_error);
565 * g_key_file_load_from_dirs:
566 * @key_file: an empty #GKeyFile struct
567 * @file: a relative path to a filename to open and parse
568 * @search_dirs: %NULL-terminated array of directories to search
569 * @full_path: return location for a string containing the full path
570 * of the file, or %NULL
571 * @flags: flags from #GKeyFileFlags
572 * @error: return location for a #GError, or %NULL
574 * This function looks for a key file named @file in the paths
575 * specified in @search_dirs, loads the file into @key_file and
576 * returns the file's full path in @full_path. If the file could not
577 * be loaded then an %error is set to either a #GFileError or
580 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
585 g_key_file_load_from_dirs (GKeyFile *key_file,
587 const gchar **search_dirs,
592 GError *key_file_error = NULL;
593 const gchar **data_dirs;
598 g_return_val_if_fail (key_file != NULL, FALSE);
599 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
600 g_return_val_if_fail (search_dirs != NULL, FALSE);
603 data_dirs = search_dirs;
605 while (*data_dirs != NULL && !found_file)
607 g_free (output_path);
609 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
615 g_propagate_error (error, key_file_error);
619 found_file = g_key_file_load_from_fd (key_file, fd, flags,
625 g_propagate_error (error, key_file_error);
630 if (found_file && full_path)
631 *full_path = output_path;
633 g_free (output_path);
639 * g_key_file_load_from_data_dirs:
640 * @key_file: an empty #GKeyFile struct
641 * @file: a relative path to a filename to open and parse
642 * @full_path: return location for a string containing the full path
643 * of the file, or %NULL
644 * @flags: flags from #GKeyFileFlags
645 * @error: return location for a #GError, or %NULL
647 * This function looks for a key file named @file in the paths
648 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
649 * loads the file into @key_file and returns the file's full path in
650 * @full_path. If the file could not be loaded then an %error is
651 * set to either a #GFileError or #GKeyFileError.
653 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
657 g_key_file_load_from_data_dirs (GKeyFile *key_file,
663 gchar **all_data_dirs;
664 const gchar * user_data_dir;
665 const gchar * const * system_data_dirs;
669 g_return_val_if_fail (key_file != NULL, FALSE);
670 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
672 user_data_dir = g_get_user_data_dir ();
673 system_data_dirs = g_get_system_data_dirs ();
674 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
677 all_data_dirs[i++] = g_strdup (user_data_dir);
680 while (system_data_dirs[j] != NULL)
681 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
682 all_data_dirs[i] = NULL;
684 found_file = g_key_file_load_from_dirs (key_file,
686 (const gchar **)all_data_dirs,
691 g_strfreev (all_data_dirs);
698 * @key_file: a #GKeyFile
705 g_key_file_free (GKeyFile *key_file)
707 g_return_if_fail (key_file != NULL);
709 g_key_file_clear (key_file);
710 g_slice_free (GKeyFile, key_file);
713 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
714 * true for locales that match those in g_get_language_names().
717 g_key_file_locale_is_interesting (GKeyFile *key_file,
722 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
725 for (i = 0; key_file->locales[i] != NULL; i++)
727 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
735 g_key_file_parse_line (GKeyFile *key_file,
740 GError *parse_error = NULL;
743 g_return_if_fail (key_file != NULL);
744 g_return_if_fail (line != NULL);
746 line_start = (gchar *) line;
747 while (g_ascii_isspace (*line_start))
750 if (g_key_file_line_is_comment (line_start))
751 g_key_file_parse_comment (key_file, line, length, &parse_error);
752 else if (g_key_file_line_is_group (line_start))
753 g_key_file_parse_group (key_file, line_start,
754 length - (line_start - line),
756 else if (g_key_file_line_is_key_value_pair (line_start))
757 g_key_file_parse_key_value_pair (key_file, line_start,
758 length - (line_start - line),
762 gchar *line_utf8 = _g_utf8_make_valid (line);
763 g_set_error (error, G_KEY_FILE_ERROR,
764 G_KEY_FILE_ERROR_PARSE,
765 _("Key file contains line '%s' which is not "
766 "a key-value pair, group, or comment"),
774 g_propagate_error (error, parse_error);
778 g_key_file_parse_comment (GKeyFile *key_file,
783 GKeyFileKeyValuePair *pair;
785 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
788 g_warn_if_fail (key_file->current_group != NULL);
790 pair = g_slice_new (GKeyFileKeyValuePair);
792 pair->value = g_strndup (line, length);
794 key_file->current_group->key_value_pairs =
795 g_list_prepend (key_file->current_group->key_value_pairs, pair);
797 if (length == 0 || line[0] != '#')
798 key_file->current_group->has_trailing_blank_line = TRUE;
802 g_key_file_parse_group (GKeyFile *key_file,
808 const gchar *group_name_start, *group_name_end;
810 /* advance past opening '['
812 group_name_start = line + 1;
813 group_name_end = line + length - 1;
815 while (*group_name_end != ']')
818 group_name = g_strndup (group_name_start,
819 group_name_end - group_name_start);
821 if (!g_key_file_is_group_name (group_name))
823 g_set_error (error, G_KEY_FILE_ERROR,
824 G_KEY_FILE_ERROR_PARSE,
825 _("Invalid group name: %s"), group_name);
830 g_key_file_add_group (key_file, group_name);
835 g_key_file_parse_key_value_pair (GKeyFile *key_file,
840 gchar *key, *value, *key_end, *value_start, *locale;
841 gsize key_len, value_len;
843 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
845 g_set_error_literal (error, G_KEY_FILE_ERROR,
846 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
847 _("Key file does not start with a group"));
851 key_end = value_start = strchr (line, '=');
853 g_warn_if_fail (key_end != NULL);
858 /* Pull the key name from the line (chomping trailing whitespace)
860 while (g_ascii_isspace (*key_end))
863 key_len = key_end - line + 2;
865 g_warn_if_fail (key_len <= length);
867 key = g_strndup (line, key_len - 1);
869 if (!g_key_file_is_key_name (key))
871 g_set_error (error, G_KEY_FILE_ERROR,
872 G_KEY_FILE_ERROR_PARSE,
873 _("Invalid key name: %s"), key);
878 /* Pull the value from the line (chugging leading whitespace)
880 while (g_ascii_isspace (*value_start))
883 value_len = line + length - value_start + 1;
885 value = g_strndup (value_start, value_len);
887 g_warn_if_fail (key_file->start_group != NULL);
889 if (key_file->current_group
890 && key_file->current_group->name
891 && strcmp (key_file->start_group->name,
892 key_file->current_group->name) == 0
893 && strcmp (key, "Encoding") == 0)
895 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
897 gchar *value_utf8 = _g_utf8_make_valid (value);
898 g_set_error (error, G_KEY_FILE_ERROR,
899 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
900 _("Key file contains unsupported "
901 "encoding '%s'"), value_utf8);
910 /* Is this key a translation? If so, is it one that we care about?
912 locale = key_get_locale (key);
914 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
915 g_key_file_add_key (key_file, key_file->current_group, key, value);
923 key_get_locale (const gchar *key)
927 locale = g_strrstr (key, "[");
929 if (locale && strlen (locale) <= 2)
933 locale = g_strndup (locale + 1, strlen (locale) - 2);
939 g_key_file_parse_data (GKeyFile *key_file,
947 g_return_if_fail (key_file != NULL);
948 g_return_if_fail (data != NULL);
952 for (i = 0; i < length; i++)
956 if (i > 0 && data[i - 1] == '\r')
957 g_string_erase (key_file->parse_buffer,
958 key_file->parse_buffer->len - 1,
961 /* When a newline is encountered flush the parse buffer so that the
962 * line can be parsed. Note that completely blank lines won't show
963 * up in the parse buffer, so they get parsed directly.
965 if (key_file->parse_buffer->len > 0)
966 g_key_file_flush_parse_buffer (key_file, &parse_error);
968 g_key_file_parse_comment (key_file, "", 1, &parse_error);
972 g_propagate_error (error, parse_error);
977 g_string_append_c (key_file->parse_buffer, data[i]);
980 key_file->approximate_size += length;
984 g_key_file_flush_parse_buffer (GKeyFile *key_file,
987 GError *file_error = NULL;
989 g_return_if_fail (key_file != NULL);
993 if (key_file->parse_buffer->len > 0)
995 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
996 key_file->parse_buffer->len,
998 g_string_erase (key_file->parse_buffer, 0, -1);
1002 g_propagate_error (error, file_error);
1009 * g_key_file_to_data:
1010 * @key_file: a #GKeyFile
1011 * @length: return location for the length of the
1012 * returned string, or %NULL
1013 * @error: return location for a #GError, or %NULL
1015 * This function outputs @key_file as a string.
1017 * Note that this function never reports an error,
1018 * so it is safe to pass %NULL as @error.
1020 * Return value: a newly allocated string holding
1021 * the contents of the #GKeyFile
1026 g_key_file_to_data (GKeyFile *key_file,
1030 GString *data_string;
1031 GList *group_node, *key_file_node;
1032 gboolean has_blank_line = TRUE;
1034 g_return_val_if_fail (key_file != NULL, NULL);
1036 data_string = g_string_sized_new (2 * key_file->approximate_size);
1038 for (group_node = g_list_last (key_file->groups);
1040 group_node = group_node->prev)
1042 GKeyFileGroup *group;
1044 group = (GKeyFileGroup *) group_node->data;
1046 /* separate groups by at least an empty line */
1047 if (!has_blank_line)
1048 g_string_append_c (data_string, '\n');
1049 has_blank_line = group->has_trailing_blank_line;
1051 if (group->comment != NULL)
1052 g_string_append_printf (data_string, "%s\n", group->comment->value);
1054 if (group->name != NULL)
1055 g_string_append_printf (data_string, "[%s]\n", group->name);
1057 for (key_file_node = g_list_last (group->key_value_pairs);
1058 key_file_node != NULL;
1059 key_file_node = key_file_node->prev)
1061 GKeyFileKeyValuePair *pair;
1063 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1065 if (pair->key != NULL)
1066 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1068 g_string_append_printf (data_string, "%s\n", pair->value);
1073 *length = data_string->len;
1075 return g_string_free (data_string, FALSE);
1079 * g_key_file_get_keys:
1080 * @key_file: a #GKeyFile
1081 * @group_name: a group name
1082 * @length: return location for the number of keys returned, or %NULL
1083 * @error: return location for a #GError, or %NULL
1085 * Returns all keys for the group name @group_name. The array of
1086 * returned keys will be %NULL-terminated, so @length may
1087 * optionally be %NULL. In the event that the @group_name cannot
1088 * be found, %NULL is returned and @error is set to
1089 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1091 * Return value: a newly-allocated %NULL-terminated array of strings.
1092 * Use g_strfreev() to free it.
1097 g_key_file_get_keys (GKeyFile *key_file,
1098 const gchar *group_name,
1102 GKeyFileGroup *group;
1107 g_return_val_if_fail (key_file != NULL, NULL);
1108 g_return_val_if_fail (group_name != NULL, NULL);
1110 group = g_key_file_lookup_group (key_file, group_name);
1114 g_set_error (error, G_KEY_FILE_ERROR,
1115 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1116 _("Key file does not have group '%s'"),
1117 group_name ? group_name : "(null)");
1122 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1124 GKeyFileKeyValuePair *pair;
1126 pair = (GKeyFileKeyValuePair *) tmp->data;
1132 keys = g_new (gchar *, num_keys + 1);
1135 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1137 GKeyFileKeyValuePair *pair;
1139 pair = (GKeyFileKeyValuePair *) tmp->data;
1143 keys[i] = g_strdup (pair->key);
1148 keys[num_keys] = NULL;
1157 * g_key_file_get_start_group:
1158 * @key_file: a #GKeyFile
1160 * Returns the name of the start group of the file.
1162 * Return value: The start group of the key file.
1167 g_key_file_get_start_group (GKeyFile *key_file)
1169 g_return_val_if_fail (key_file != NULL, NULL);
1171 if (key_file->start_group)
1172 return g_strdup (key_file->start_group->name);
1178 * g_key_file_get_groups:
1179 * @key_file: a #GKeyFile
1180 * @length: return location for the number of returned groups, or %NULL
1182 * Returns all groups in the key file loaded with @key_file.
1183 * The array of returned groups will be %NULL-terminated, so
1184 * @length may optionally be %NULL.
1186 * Return value: a newly-allocated %NULL-terminated array of strings.
1187 * Use g_strfreev() to free it.
1191 g_key_file_get_groups (GKeyFile *key_file,
1196 gsize i, num_groups;
1198 g_return_val_if_fail (key_file != NULL, NULL);
1200 num_groups = g_list_length (key_file->groups);
1202 g_return_val_if_fail (num_groups > 0, NULL);
1204 group_node = g_list_last (key_file->groups);
1206 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1208 /* Only need num_groups instead of num_groups + 1
1209 * because the first group of the file (last in the
1210 * list) is always the comment group at the top,
1213 groups = g_new (gchar *, num_groups);
1217 for (group_node = group_node->prev;
1219 group_node = group_node->prev)
1221 GKeyFileGroup *group;
1223 group = (GKeyFileGroup *) group_node->data;
1225 g_warn_if_fail (group->name != NULL);
1227 groups[i++] = g_strdup (group->name);
1238 * g_key_file_get_value:
1239 * @key_file: a #GKeyFile
1240 * @group_name: a group name
1242 * @error: return location for a #GError, or %NULL
1244 * Returns the raw value associated with @key under @group_name.
1245 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1247 * In the event the key cannot be found, %NULL is returned and
1248 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1249 * event that the @group_name cannot be found, %NULL is returned
1250 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1253 * Return value: a newly allocated string or %NULL if the specified
1254 * key cannot be found.
1259 g_key_file_get_value (GKeyFile *key_file,
1260 const gchar *group_name,
1264 GKeyFileGroup *group;
1265 GKeyFileKeyValuePair *pair;
1266 gchar *value = NULL;
1268 g_return_val_if_fail (key_file != NULL, NULL);
1269 g_return_val_if_fail (group_name != NULL, NULL);
1270 g_return_val_if_fail (key != NULL, NULL);
1272 group = g_key_file_lookup_group (key_file, group_name);
1276 g_set_error (error, G_KEY_FILE_ERROR,
1277 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1278 _("Key file does not have group '%s'"),
1279 group_name ? group_name : "(null)");
1283 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1286 value = g_strdup (pair->value);
1288 g_set_error (error, G_KEY_FILE_ERROR,
1289 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1290 _("Key file does not have key '%s'"), key);
1296 * g_key_file_set_value:
1297 * @key_file: a #GKeyFile
1298 * @group_name: a group name
1302 * Associates a new value with @key under @group_name.
1304 * If @key cannot be found then it is created. If @group_name cannot
1305 * be found then it is created. To set an UTF-8 string which may contain
1306 * characters that need escaping (such as newlines or spaces), use
1307 * g_key_file_set_string().
1312 g_key_file_set_value (GKeyFile *key_file,
1313 const gchar *group_name,
1317 GKeyFileGroup *group;
1318 GKeyFileKeyValuePair *pair;
1320 g_return_if_fail (key_file != NULL);
1321 g_return_if_fail (g_key_file_is_group_name (group_name));
1322 g_return_if_fail (g_key_file_is_key_name (key));
1323 g_return_if_fail (value != NULL);
1325 group = g_key_file_lookup_group (key_file, group_name);
1329 g_key_file_add_group (key_file, group_name);
1330 group = (GKeyFileGroup *) key_file->groups->data;
1332 g_key_file_add_key (key_file, group, key, value);
1336 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1339 g_key_file_add_key (key_file, group, key, value);
1342 g_free (pair->value);
1343 pair->value = g_strdup (value);
1349 * g_key_file_get_string:
1350 * @key_file: a #GKeyFile
1351 * @group_name: a group name
1353 * @error: return location for a #GError, or %NULL
1355 * Returns the string value associated with @key under @group_name.
1356 * Unlike g_key_file_get_value(), this function handles escape sequences
1359 * In the event the key cannot be found, %NULL is returned and
1360 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1361 * event that the @group_name cannot be found, %NULL is returned
1362 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1364 * Return value: a newly allocated string or %NULL if the specified
1365 * key cannot be found.
1370 g_key_file_get_string (GKeyFile *key_file,
1371 const gchar *group_name,
1375 gchar *value, *string_value;
1376 GError *key_file_error;
1378 g_return_val_if_fail (key_file != NULL, NULL);
1379 g_return_val_if_fail (group_name != NULL, NULL);
1380 g_return_val_if_fail (key != NULL, NULL);
1382 key_file_error = NULL;
1384 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1388 g_propagate_error (error, key_file_error);
1392 if (!g_utf8_validate (value, -1, NULL))
1394 gchar *value_utf8 = _g_utf8_make_valid (value);
1395 g_set_error (error, G_KEY_FILE_ERROR,
1396 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1397 _("Key file contains key '%s' with value '%s' "
1398 "which is not UTF-8"), key, value_utf8);
1399 g_free (value_utf8);
1405 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1411 if (g_error_matches (key_file_error,
1413 G_KEY_FILE_ERROR_INVALID_VALUE))
1415 g_set_error (error, G_KEY_FILE_ERROR,
1416 G_KEY_FILE_ERROR_INVALID_VALUE,
1417 _("Key file contains key '%s' "
1418 "which has value that cannot be interpreted."),
1420 g_error_free (key_file_error);
1423 g_propagate_error (error, key_file_error);
1426 return string_value;
1430 * g_key_file_set_string:
1431 * @key_file: a #GKeyFile
1432 * @group_name: a group name
1436 * Associates a new string value with @key under @group_name.
1437 * If @key cannot be found then it is created.
1438 * If @group_name cannot be found then it is created.
1439 * Unlike g_key_file_set_value(), this function handles characters
1440 * that need escaping, such as newlines.
1445 g_key_file_set_string (GKeyFile *key_file,
1446 const gchar *group_name,
1448 const gchar *string)
1452 g_return_if_fail (key_file != NULL);
1453 g_return_if_fail (string != NULL);
1455 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1456 g_key_file_set_value (key_file, group_name, key, value);
1461 * g_key_file_get_string_list:
1462 * @key_file: a #GKeyFile
1463 * @group_name: a group name
1465 * @length: return location for the number of returned strings, or %NULL
1466 * @error: return location for a #GError, or %NULL
1468 * Returns the values associated with @key under @group_name.
1470 * In the event the key cannot be found, %NULL is returned and
1471 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1472 * event that the @group_name cannot be found, %NULL is returned
1473 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1475 * Return value: a %NULL-terminated string array or %NULL if the specified
1476 * key cannot be found. The array should be freed with g_strfreev().
1481 g_key_file_get_string_list (GKeyFile *key_file,
1482 const gchar *group_name,
1487 GError *key_file_error = NULL;
1488 gchar *value, *string_value, **values;
1490 GSList *p, *pieces = NULL;
1492 g_return_val_if_fail (key_file != NULL, NULL);
1493 g_return_val_if_fail (group_name != NULL, NULL);
1494 g_return_val_if_fail (key != NULL, NULL);
1499 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1503 g_propagate_error (error, key_file_error);
1507 if (!g_utf8_validate (value, -1, NULL))
1509 gchar *value_utf8 = _g_utf8_make_valid (value);
1510 g_set_error (error, G_KEY_FILE_ERROR,
1511 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1512 _("Key file contains key '%s' with value '%s' "
1513 "which is not UTF-8"), key, value_utf8);
1514 g_free (value_utf8);
1520 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1522 g_free (string_value);
1526 if (g_error_matches (key_file_error,
1528 G_KEY_FILE_ERROR_INVALID_VALUE))
1530 g_set_error (error, G_KEY_FILE_ERROR,
1531 G_KEY_FILE_ERROR_INVALID_VALUE,
1532 _("Key file contains key '%s' "
1533 "which has a value that cannot be interpreted."),
1535 g_error_free (key_file_error);
1538 g_propagate_error (error, key_file_error);
1543 len = g_slist_length (pieces);
1544 values = g_new (gchar *, len + 1);
1545 for (p = pieces, i = 0; p; p = p->next)
1546 values[i++] = p->data;
1549 g_slist_free (pieces);
1558 * g_key_file_set_string_list:
1559 * @key_file: a #GKeyFile
1560 * @group_name: a group name
1562 * @list: an array of string values
1563 * @length: number of string values in @list
1565 * Associates a list of string values for @key under @group_name.
1566 * If @key cannot be found then it is created.
1567 * If @group_name cannot be found then it is created.
1572 g_key_file_set_string_list (GKeyFile *key_file,
1573 const gchar *group_name,
1575 const gchar * const list[],
1578 GString *value_list;
1581 g_return_if_fail (key_file != NULL);
1582 g_return_if_fail (list != NULL || length == 0);
1584 value_list = g_string_sized_new (length * 128);
1585 for (i = 0; i < length && list[i] != NULL; i++)
1589 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1590 g_string_append (value_list, value);
1591 g_string_append_c (value_list, key_file->list_separator);
1596 g_key_file_set_value (key_file, group_name, key, value_list->str);
1597 g_string_free (value_list, TRUE);
1601 * g_key_file_set_locale_string:
1602 * @key_file: a #GKeyFile
1603 * @group_name: a group name
1605 * @locale: a locale identifier
1608 * Associates a string value for @key and @locale under @group_name.
1609 * If the translation for @key cannot be found then it is created.
1614 g_key_file_set_locale_string (GKeyFile *key_file,
1615 const gchar *group_name,
1617 const gchar *locale,
1618 const gchar *string)
1620 gchar *full_key, *value;
1622 g_return_if_fail (key_file != NULL);
1623 g_return_if_fail (key != NULL);
1624 g_return_if_fail (locale != NULL);
1625 g_return_if_fail (string != NULL);
1627 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1628 full_key = g_strdup_printf ("%s[%s]", key, locale);
1629 g_key_file_set_value (key_file, group_name, full_key, value);
1634 extern GSList *_g_compute_locale_variants (const gchar *locale);
1637 * g_key_file_get_locale_string:
1638 * @key_file: a #GKeyFile
1639 * @group_name: a group name
1641 * @locale: a locale identifier or %NULL
1642 * @error: return location for a #GError, or %NULL
1644 * Returns the value associated with @key under @group_name
1645 * translated in the given @locale if available. If @locale is
1646 * %NULL then the current locale is assumed.
1648 * If @key cannot be found then %NULL is returned and @error is set
1649 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1650 * with @key cannot be interpreted or no suitable translation can
1651 * be found then the untranslated value is returned.
1653 * Return value: a newly allocated string or %NULL if the specified
1654 * key cannot be found.
1659 g_key_file_get_locale_string (GKeyFile *key_file,
1660 const gchar *group_name,
1662 const gchar *locale,
1665 gchar *candidate_key, *translated_value;
1666 GError *key_file_error;
1668 gboolean free_languages = FALSE;
1671 g_return_val_if_fail (key_file != NULL, NULL);
1672 g_return_val_if_fail (group_name != NULL, NULL);
1673 g_return_val_if_fail (key != NULL, NULL);
1675 candidate_key = NULL;
1676 translated_value = NULL;
1677 key_file_error = NULL;
1683 list = _g_compute_locale_variants (locale);
1685 languages = g_new (gchar *, g_slist_length (list) + 1);
1686 for (l = list, i = 0; l; l = l->next, i++)
1687 languages[i] = l->data;
1688 languages[i] = NULL;
1690 g_slist_free (list);
1691 free_languages = TRUE;
1695 languages = (gchar **) g_get_language_names ();
1696 free_languages = FALSE;
1699 for (i = 0; languages[i]; i++)
1701 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1703 translated_value = g_key_file_get_string (key_file,
1705 candidate_key, NULL);
1706 g_free (candidate_key);
1708 if (translated_value)
1711 g_free (translated_value);
1712 translated_value = NULL;
1715 /* Fallback to untranslated key
1717 if (!translated_value)
1719 translated_value = g_key_file_get_string (key_file, group_name, key,
1722 if (!translated_value)
1723 g_propagate_error (error, key_file_error);
1727 g_strfreev (languages);
1729 return translated_value;
1733 * g_key_file_get_locale_string_list:
1734 * @key_file: a #GKeyFile
1735 * @group_name: a group name
1737 * @locale: a locale identifier or %NULL
1738 * @length: return location for the number of returned strings or %NULL
1739 * @error: return location for a #GError or %NULL
1741 * Returns the values associated with @key under @group_name
1742 * translated in the given @locale if available. If @locale is
1743 * %NULL then the current locale is assumed.
1745 * If @key cannot be found then %NULL is returned and @error is set
1746 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1747 * with @key cannot be interpreted or no suitable translations
1748 * can be found then the untranslated values are returned. The
1749 * returned array is %NULL-terminated, so @length may optionally
1752 * Return value: a newly allocated %NULL-terminated string array
1753 * or %NULL if the key isn't found. The string array should be freed
1754 * with g_strfreev().
1759 g_key_file_get_locale_string_list (GKeyFile *key_file,
1760 const gchar *group_name,
1762 const gchar *locale,
1766 GError *key_file_error;
1767 gchar **values, *value;
1768 char list_separator[2];
1771 g_return_val_if_fail (key_file != NULL, NULL);
1772 g_return_val_if_fail (group_name != NULL, NULL);
1773 g_return_val_if_fail (key != NULL, NULL);
1775 key_file_error = NULL;
1777 value = g_key_file_get_locale_string (key_file, group_name,
1782 g_propagate_error (error, key_file_error);
1791 len = strlen (value);
1792 if (value[len - 1] == key_file->list_separator)
1793 value[len - 1] = '\0';
1795 list_separator[0] = key_file->list_separator;
1796 list_separator[1] = '\0';
1797 values = g_strsplit (value, list_separator, 0);
1802 *length = g_strv_length (values);
1808 * g_key_file_set_locale_string_list:
1809 * @key_file: a #GKeyFile
1810 * @group_name: a group name
1812 * @locale: a locale identifier
1813 * @list: a %NULL-terminated array of locale string values
1814 * @length: the length of @list
1816 * Associates a list of string values for @key and @locale under
1817 * @group_name. If the translation for @key cannot be found then
1823 g_key_file_set_locale_string_list (GKeyFile *key_file,
1824 const gchar *group_name,
1826 const gchar *locale,
1827 const gchar * const list[],
1830 GString *value_list;
1834 g_return_if_fail (key_file != NULL);
1835 g_return_if_fail (key != NULL);
1836 g_return_if_fail (locale != NULL);
1837 g_return_if_fail (length != 0);
1839 value_list = g_string_sized_new (length * 128);
1840 for (i = 0; i < length && list[i] != NULL; i++)
1844 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1845 g_string_append (value_list, value);
1846 g_string_append_c (value_list, key_file->list_separator);
1851 full_key = g_strdup_printf ("%s[%s]", key, locale);
1852 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1854 g_string_free (value_list, TRUE);
1858 * g_key_file_get_boolean:
1859 * @key_file: a #GKeyFile
1860 * @group_name: a group name
1862 * @error: return location for a #GError
1864 * Returns the value associated with @key under @group_name as a
1867 * If @key cannot be found then %FALSE is returned and @error is set
1868 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1869 * associated with @key cannot be interpreted as a boolean then %FALSE
1870 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1872 * Return value: the value associated with the key as a boolean,
1873 * or %FALSE if the key was not found or could not be parsed.
1878 g_key_file_get_boolean (GKeyFile *key_file,
1879 const gchar *group_name,
1883 GError *key_file_error = NULL;
1885 gboolean bool_value;
1887 g_return_val_if_fail (key_file != NULL, FALSE);
1888 g_return_val_if_fail (group_name != NULL, FALSE);
1889 g_return_val_if_fail (key != NULL, FALSE);
1891 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1895 g_propagate_error (error, key_file_error);
1899 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1905 if (g_error_matches (key_file_error,
1907 G_KEY_FILE_ERROR_INVALID_VALUE))
1909 g_set_error (error, G_KEY_FILE_ERROR,
1910 G_KEY_FILE_ERROR_INVALID_VALUE,
1911 _("Key file contains key '%s' "
1912 "which has value that cannot be interpreted."),
1914 g_error_free (key_file_error);
1917 g_propagate_error (error, key_file_error);
1924 * g_key_file_set_boolean:
1925 * @key_file: a #GKeyFile
1926 * @group_name: a group name
1928 * @value: %TRUE or %FALSE
1930 * Associates a new boolean value with @key under @group_name.
1931 * If @key cannot be found then it is created.
1936 g_key_file_set_boolean (GKeyFile *key_file,
1937 const gchar *group_name,
1943 g_return_if_fail (key_file != NULL);
1945 result = g_key_file_parse_boolean_as_value (key_file, value);
1946 g_key_file_set_value (key_file, group_name, key, result);
1951 * g_key_file_get_boolean_list:
1952 * @key_file: a #GKeyFile
1953 * @group_name: a group name
1955 * @length: the number of booleans returned
1956 * @error: return location for a #GError
1958 * Returns the values associated with @key under @group_name as
1961 * If @key cannot be found then %NULL is returned and @error is set to
1962 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1963 * with @key cannot be interpreted as booleans then %NULL is returned
1964 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1966 * Return value: the values associated with the key as a list of
1967 * booleans, or %NULL if the key was not found or could not be parsed.
1972 g_key_file_get_boolean_list (GKeyFile *key_file,
1973 const gchar *group_name,
1978 GError *key_file_error;
1980 gboolean *bool_values;
1983 g_return_val_if_fail (key_file != NULL, NULL);
1984 g_return_val_if_fail (group_name != NULL, NULL);
1985 g_return_val_if_fail (key != NULL, NULL);
1990 key_file_error = NULL;
1992 values = g_key_file_get_string_list (key_file, group_name, key,
1993 &num_bools, &key_file_error);
1996 g_propagate_error (error, key_file_error);
2001 bool_values = g_new (gboolean, num_bools);
2003 for (i = 0; i < num_bools; i++)
2005 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2011 g_propagate_error (error, key_file_error);
2012 g_strfreev (values);
2013 g_free (bool_values);
2018 g_strfreev (values);
2021 *length = num_bools;
2027 * g_key_file_set_boolean_list:
2028 * @key_file: a #GKeyFile
2029 * @group_name: a group name
2031 * @list: an array of boolean values
2032 * @length: length of @list
2034 * Associates a list of boolean values with @key under @group_name.
2035 * If @key cannot be found then it is created.
2036 * If @group_name is %NULL, the start_group is used.
2041 g_key_file_set_boolean_list (GKeyFile *key_file,
2042 const gchar *group_name,
2047 GString *value_list;
2050 g_return_if_fail (key_file != NULL);
2051 g_return_if_fail (list != NULL);
2053 value_list = g_string_sized_new (length * 8);
2054 for (i = 0; i < length; i++)
2058 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2060 g_string_append (value_list, value);
2061 g_string_append_c (value_list, key_file->list_separator);
2066 g_key_file_set_value (key_file, group_name, key, value_list->str);
2067 g_string_free (value_list, TRUE);
2071 * g_key_file_get_integer:
2072 * @key_file: a #GKeyFile
2073 * @group_name: a group name
2075 * @error: return location for a #GError
2077 * Returns the value associated with @key under @group_name as an
2080 * If @key cannot be found then 0 is returned and @error is set to
2081 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2082 * with @key cannot be interpreted as an integer then 0 is returned
2083 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2085 * Return value: the value associated with the key as an integer, or
2086 * 0 if the key was not found or could not be parsed.
2091 g_key_file_get_integer (GKeyFile *key_file,
2092 const gchar *group_name,
2096 GError *key_file_error;
2100 g_return_val_if_fail (key_file != NULL, -1);
2101 g_return_val_if_fail (group_name != NULL, -1);
2102 g_return_val_if_fail (key != NULL, -1);
2104 key_file_error = NULL;
2106 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2110 g_propagate_error (error, key_file_error);
2114 int_value = g_key_file_parse_value_as_integer (key_file, value,
2120 if (g_error_matches (key_file_error,
2122 G_KEY_FILE_ERROR_INVALID_VALUE))
2124 g_set_error (error, G_KEY_FILE_ERROR,
2125 G_KEY_FILE_ERROR_INVALID_VALUE,
2126 _("Key file contains key '%s' in group '%s' "
2127 "which has value that cannot be interpreted."), key,
2129 g_error_free (key_file_error);
2132 g_propagate_error (error, key_file_error);
2139 * g_key_file_set_integer:
2140 * @key_file: a #GKeyFile
2141 * @group_name: a group name
2143 * @value: an integer value
2145 * Associates a new integer value with @key under @group_name.
2146 * If @key cannot be found then it is created.
2151 g_key_file_set_integer (GKeyFile *key_file,
2152 const gchar *group_name,
2158 g_return_if_fail (key_file != NULL);
2160 result = g_key_file_parse_integer_as_value (key_file, value);
2161 g_key_file_set_value (key_file, group_name, key, result);
2166 * g_key_file_get_integer_list:
2167 * @key_file: a #GKeyFile
2168 * @group_name: a group name
2170 * @length: the number of integers returned
2171 * @error: return location for a #GError
2173 * Returns the values associated with @key under @group_name as
2176 * If @key cannot be found then %NULL is returned and @error is set to
2177 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2178 * with @key cannot be interpreted as integers then %NULL is returned
2179 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2181 * Return value: the values associated with the key as a list of
2182 * integers, or %NULL if the key was not found or could not be parsed.
2187 g_key_file_get_integer_list (GKeyFile *key_file,
2188 const gchar *group_name,
2193 GError *key_file_error = NULL;
2198 g_return_val_if_fail (key_file != NULL, NULL);
2199 g_return_val_if_fail (group_name != NULL, NULL);
2200 g_return_val_if_fail (key != NULL, NULL);
2205 values = g_key_file_get_string_list (key_file, group_name, key,
2206 &num_ints, &key_file_error);
2209 g_propagate_error (error, key_file_error);
2214 int_values = g_new (gint, num_ints);
2216 for (i = 0; i < num_ints; i++)
2218 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2224 g_propagate_error (error, key_file_error);
2225 g_strfreev (values);
2226 g_free (int_values);
2231 g_strfreev (values);
2240 * g_key_file_set_integer_list:
2241 * @key_file: a #GKeyFile
2242 * @group_name: a group name
2244 * @list: an array of integer values
2245 * @length: number of integer values in @list
2247 * Associates a list of integer values with @key under @group_name.
2248 * If @key cannot be found then it is created.
2253 g_key_file_set_integer_list (GKeyFile *key_file,
2254 const gchar *group_name,
2262 g_return_if_fail (key_file != NULL);
2263 g_return_if_fail (list != NULL);
2265 values = g_string_sized_new (length * 16);
2266 for (i = 0; i < length; i++)
2270 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2272 g_string_append (values, value);
2273 g_string_append_c (values, key_file->list_separator);
2278 g_key_file_set_value (key_file, group_name, key, values->str);
2279 g_string_free (values, TRUE);
2283 * g_key_file_get_double:
2284 * @key_file: a #GKeyFile
2285 * @group_name: a group name
2287 * @error: return location for a #GError
2289 * Returns the value associated with @key under @group_name as a
2290 * double. If @group_name is %NULL, the start_group is used.
2292 * If @key cannot be found then 0.0 is returned and @error is set to
2293 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2294 * with @key cannot be interpreted as a double then 0.0 is returned
2295 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2297 * Return value: the value associated with the key as a double, or
2298 * 0.0 if the key was not found or could not be parsed.
2303 g_key_file_get_double (GKeyFile *key_file,
2304 const gchar *group_name,
2308 GError *key_file_error;
2310 gdouble double_value;
2312 g_return_val_if_fail (key_file != NULL, -1);
2313 g_return_val_if_fail (group_name != NULL, -1);
2314 g_return_val_if_fail (key != NULL, -1);
2316 key_file_error = NULL;
2318 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2322 g_propagate_error (error, key_file_error);
2326 double_value = g_key_file_parse_value_as_double (key_file, value,
2332 if (g_error_matches (key_file_error,
2334 G_KEY_FILE_ERROR_INVALID_VALUE))
2336 g_set_error (error, G_KEY_FILE_ERROR,
2337 G_KEY_FILE_ERROR_INVALID_VALUE,
2338 _("Key file contains key '%s' in group '%s' "
2339 "which has value that cannot be interpreted."), key,
2341 g_error_free (key_file_error);
2344 g_propagate_error (error, key_file_error);
2347 return double_value;
2351 * g_key_file_set_double:
2352 * @key_file: a #GKeyFile
2353 * @group_name: a group name
2355 * @value: an double value
2357 * Associates a new double value with @key under @group_name.
2358 * If @key cannot be found then it is created.
2363 g_key_file_set_double (GKeyFile *key_file,
2364 const gchar *group_name,
2368 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2370 g_return_if_fail (key_file != NULL);
2372 g_ascii_dtostr (result, sizeof (result), value);
2373 g_key_file_set_value (key_file, group_name, key, result);
2377 * g_key_file_get_double_list:
2378 * @key_file: a #GKeyFile
2379 * @group_name: a group name
2381 * @length: the number of doubles returned
2382 * @error: return location for a #GError
2384 * Returns the values associated with @key under @group_name as
2387 * If @key cannot be found then %NULL is returned and @error is set to
2388 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2389 * with @key cannot be interpreted as doubles then %NULL is returned
2390 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2392 * Return value: the values associated with the key as a list of
2393 * doubles, or %NULL if the key was not found or could not be parsed.
2398 g_key_file_get_double_list (GKeyFile *key_file,
2399 const gchar *group_name,
2404 GError *key_file_error = NULL;
2406 gdouble *double_values;
2407 gsize i, num_doubles;
2409 g_return_val_if_fail (key_file != NULL, NULL);
2410 g_return_val_if_fail (group_name != NULL, NULL);
2411 g_return_val_if_fail (key != NULL, NULL);
2416 values = g_key_file_get_string_list (key_file, group_name, key,
2417 &num_doubles, &key_file_error);
2420 g_propagate_error (error, key_file_error);
2425 double_values = g_new (gdouble, num_doubles);
2427 for (i = 0; i < num_doubles; i++)
2429 double_values[i] = g_key_file_parse_value_as_double (key_file,
2435 g_propagate_error (error, key_file_error);
2436 g_strfreev (values);
2437 g_free (double_values);
2442 g_strfreev (values);
2445 *length = num_doubles;
2447 return double_values;
2451 * g_key_file_set_double_list:
2452 * @key_file: a #GKeyFile
2453 * @group_name: a group name
2455 * @list: an array of double values
2456 * @length: number of double values in @list
2458 * Associates a list of double values with @key under
2459 * @group_name. If @key cannot be found then it is created.
2464 g_key_file_set_double_list (GKeyFile *key_file,
2465 const gchar *group_name,
2473 g_return_if_fail (key_file != NULL);
2474 g_return_if_fail (list != NULL);
2476 values = g_string_sized_new (length * 16);
2477 for (i = 0; i < length; i++)
2479 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2481 g_ascii_dtostr( result, sizeof (result), list[i] );
2483 g_string_append (values, result);
2484 g_string_append_c (values, key_file->list_separator);
2487 g_key_file_set_value (key_file, group_name, key, values->str);
2488 g_string_free (values, TRUE);
2492 g_key_file_set_key_comment (GKeyFile *key_file,
2493 const gchar *group_name,
2495 const gchar *comment,
2498 GKeyFileGroup *group;
2499 GKeyFileKeyValuePair *pair;
2500 GList *key_node, *comment_node, *tmp;
2502 group = g_key_file_lookup_group (key_file, group_name);
2505 g_set_error (error, G_KEY_FILE_ERROR,
2506 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2507 _("Key file does not have group '%s'"),
2508 group_name ? group_name : "(null)");
2513 /* First find the key the comments are supposed to be
2516 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2518 if (key_node == NULL)
2520 g_set_error (error, G_KEY_FILE_ERROR,
2521 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2522 _("Key file does not have key '%s' in group '%s'"),
2527 /* Then find all the comments already associated with the
2530 tmp = key_node->next;
2533 pair = (GKeyFileKeyValuePair *) tmp->data;
2535 if (pair->key != NULL)
2540 g_key_file_remove_key_value_pair_node (key_file, group,
2544 if (comment == NULL)
2547 /* Now we can add our new comment
2549 pair = g_slice_new (GKeyFileKeyValuePair);
2551 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2553 key_node = g_list_insert (key_node, pair, 1);
2559 g_key_file_set_group_comment (GKeyFile *key_file,
2560 const gchar *group_name,
2561 const gchar *comment,
2564 GKeyFileGroup *group;
2566 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2568 group = g_key_file_lookup_group (key_file, group_name);
2571 g_set_error (error, G_KEY_FILE_ERROR,
2572 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2573 _("Key file does not have group '%s'"),
2574 group_name ? group_name : "(null)");
2579 /* First remove any existing comment
2583 g_key_file_key_value_pair_free (group->comment);
2584 group->comment = NULL;
2587 if (comment == NULL)
2590 /* Now we can add our new comment
2592 group->comment = g_slice_new (GKeyFileKeyValuePair);
2593 group->comment->key = NULL;
2594 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2600 g_key_file_set_top_comment (GKeyFile *key_file,
2601 const gchar *comment,
2605 GKeyFileGroup *group;
2606 GKeyFileKeyValuePair *pair;
2608 /* The last group in the list should be the top (comments only)
2611 g_warn_if_fail (key_file->groups != NULL);
2612 group_node = g_list_last (key_file->groups);
2613 group = (GKeyFileGroup *) group_node->data;
2614 g_warn_if_fail (group->name == NULL);
2616 /* Note all keys must be comments at the top of
2617 * the file, so we can just free it all.
2619 if (group->key_value_pairs != NULL)
2621 g_list_foreach (group->key_value_pairs,
2622 (GFunc) g_key_file_key_value_pair_free,
2624 g_list_free (group->key_value_pairs);
2625 group->key_value_pairs = NULL;
2628 if (comment == NULL)
2631 pair = g_slice_new (GKeyFileKeyValuePair);
2633 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2635 group->key_value_pairs =
2636 g_list_prepend (group->key_value_pairs, pair);
2642 * g_key_file_set_comment:
2643 * @key_file: a #GKeyFile
2644 * @group_name: a group name, or %NULL
2646 * @comment: a comment
2647 * @error: return location for a #GError
2649 * Places a comment above @key from @group_name.
2650 * If @key is %NULL then @comment will be written above @group_name.
2651 * If both @key and @group_name are %NULL, then @comment will be
2652 * written above the first group in the file.
2654 * Returns: %TRUE if the comment was written, %FALSE otherwise
2659 g_key_file_set_comment (GKeyFile *key_file,
2660 const gchar *group_name,
2662 const gchar *comment,
2665 g_return_val_if_fail (key_file != NULL, FALSE);
2667 if (group_name != NULL && key != NULL)
2669 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2672 else if (group_name != NULL)
2674 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2679 if (!g_key_file_set_top_comment (key_file, comment, error))
2683 if (comment != NULL)
2684 key_file->approximate_size += strlen (comment);
2690 g_key_file_get_key_comment (GKeyFile *key_file,
2691 const gchar *group_name,
2695 GKeyFileGroup *group;
2696 GKeyFileKeyValuePair *pair;
2697 GList *key_node, *tmp;
2701 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2703 group = g_key_file_lookup_group (key_file, group_name);
2706 g_set_error (error, G_KEY_FILE_ERROR,
2707 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2708 _("Key file does not have group '%s'"),
2709 group_name ? group_name : "(null)");
2714 /* First find the key the comments are supposed to be
2717 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2719 if (key_node == NULL)
2721 g_set_error (error, G_KEY_FILE_ERROR,
2722 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2723 _("Key file does not have key '%s' in group '%s'"),
2730 /* Then find all the comments already associated with the
2731 * key and concatentate them.
2733 tmp = key_node->next;
2734 if (!key_node->next)
2737 pair = (GKeyFileKeyValuePair *) tmp->data;
2738 if (pair->key != NULL)
2743 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2745 if (pair->key != NULL)
2751 while (tmp != key_node)
2753 pair = (GKeyFileKeyValuePair *) tmp->data;
2756 string = g_string_sized_new (512);
2758 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2759 g_string_append (string, comment);
2767 comment = string->str;
2768 g_string_free (string, FALSE);
2777 get_group_comment (GKeyFile *key_file,
2778 GKeyFileGroup *group,
2787 tmp = group->key_value_pairs;
2790 GKeyFileKeyValuePair *pair;
2792 pair = (GKeyFileKeyValuePair *) tmp->data;
2794 if (pair->key != NULL)
2800 if (tmp->next == NULL)
2808 GKeyFileKeyValuePair *pair;
2810 pair = (GKeyFileKeyValuePair *) tmp->data;
2813 string = g_string_sized_new (512);
2815 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2816 g_string_append (string, comment);
2823 return g_string_free (string, FALSE);
2829 g_key_file_get_group_comment (GKeyFile *key_file,
2830 const gchar *group_name,
2834 GKeyFileGroup *group;
2836 group = g_key_file_lookup_group (key_file, group_name);
2839 g_set_error (error, G_KEY_FILE_ERROR,
2840 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2841 _("Key file does not have group '%s'"),
2842 group_name ? group_name : "(null)");
2848 return g_strdup (group->comment->value);
2850 group_node = g_key_file_lookup_group_node (key_file, group_name);
2851 group_node = group_node->next;
2852 group = (GKeyFileGroup *)group_node->data;
2853 return get_group_comment (key_file, group, error);
2857 g_key_file_get_top_comment (GKeyFile *key_file,
2861 GKeyFileGroup *group;
2863 /* The last group in the list should be the top (comments only)
2866 g_warn_if_fail (key_file->groups != NULL);
2867 group_node = g_list_last (key_file->groups);
2868 group = (GKeyFileGroup *) group_node->data;
2869 g_warn_if_fail (group->name == NULL);
2871 return get_group_comment (key_file, group, error);
2875 * g_key_file_get_comment:
2876 * @key_file: a #GKeyFile
2877 * @group_name: a group name, or %NULL
2879 * @error: return location for a #GError
2881 * Retrieves a comment above @key from @group_name.
2882 * If @key is %NULL then @comment will be read from above
2883 * @group_name. If both @key and @group_name are %NULL, then
2884 * @comment will be read from above the first group in the file.
2886 * Returns: a comment that should be freed with g_free()
2891 g_key_file_get_comment (GKeyFile *key_file,
2892 const gchar *group_name,
2896 g_return_val_if_fail (key_file != NULL, NULL);
2898 if (group_name != NULL && key != NULL)
2899 return g_key_file_get_key_comment (key_file, group_name, key, error);
2900 else if (group_name != NULL)
2901 return g_key_file_get_group_comment (key_file, group_name, error);
2903 return g_key_file_get_top_comment (key_file, error);
2907 * g_key_file_remove_comment:
2908 * @key_file: a #GKeyFile
2909 * @group_name: a group name, or %NULL
2911 * @error: return location for a #GError
2913 * Removes a comment above @key from @group_name.
2914 * If @key is %NULL then @comment will be removed above @group_name.
2915 * If both @key and @group_name are %NULL, then @comment will
2916 * be removed above the first group in the file.
2918 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2924 g_key_file_remove_comment (GKeyFile *key_file,
2925 const gchar *group_name,
2929 g_return_val_if_fail (key_file != NULL, FALSE);
2931 if (group_name != NULL && key != NULL)
2932 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2933 else if (group_name != NULL)
2934 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2936 return g_key_file_set_top_comment (key_file, NULL, error);
2940 * g_key_file_has_group:
2941 * @key_file: a #GKeyFile
2942 * @group_name: a group name
2944 * Looks whether the key file has the group @group_name.
2946 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2951 g_key_file_has_group (GKeyFile *key_file,
2952 const gchar *group_name)
2954 g_return_val_if_fail (key_file != NULL, FALSE);
2955 g_return_val_if_fail (group_name != NULL, FALSE);
2957 return g_key_file_lookup_group (key_file, group_name) != NULL;
2961 * g_key_file_has_key:
2962 * @key_file: a #GKeyFile
2963 * @group_name: a group name
2965 * @error: return location for a #GError
2967 * Looks whether the key file has the key @key in the group
2970 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2976 g_key_file_has_key (GKeyFile *key_file,
2977 const gchar *group_name,
2981 GKeyFileKeyValuePair *pair;
2982 GKeyFileGroup *group;
2984 g_return_val_if_fail (key_file != NULL, FALSE);
2985 g_return_val_if_fail (group_name != NULL, FALSE);
2986 g_return_val_if_fail (key != NULL, FALSE);
2988 group = g_key_file_lookup_group (key_file, group_name);
2992 g_set_error (error, G_KEY_FILE_ERROR,
2993 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2994 _("Key file does not have group '%s'"),
2995 group_name ? group_name : "(null)");
3000 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3002 return pair != NULL;
3006 g_key_file_add_group (GKeyFile *key_file,
3007 const gchar *group_name)
3009 GKeyFileGroup *group;
3011 g_return_if_fail (key_file != NULL);
3012 g_return_if_fail (g_key_file_is_group_name (group_name));
3014 group = g_key_file_lookup_group (key_file, group_name);
3017 key_file->current_group = group;
3021 group = g_slice_new0 (GKeyFileGroup);
3022 group->name = g_strdup (group_name);
3023 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3024 key_file->groups = g_list_prepend (key_file->groups, group);
3025 key_file->approximate_size += strlen (group_name) + 3;
3026 key_file->current_group = group;
3028 if (key_file->start_group == NULL)
3029 key_file->start_group = group;
3031 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3035 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3040 g_free (pair->value);
3041 g_slice_free (GKeyFileKeyValuePair, pair);
3045 /* Be careful not to call this function on a node with data in the
3046 * lookup map without removing it from the lookup map, first.
3048 * Some current cases where this warning is not a concern are
3050 * - the node being removed is a comment node
3051 * - the entire lookup map is getting destroyed soon after
3055 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3056 GKeyFileGroup *group,
3060 GKeyFileKeyValuePair *pair;
3062 pair = (GKeyFileKeyValuePair *) pair_node->data;
3064 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3066 if (pair->key != NULL)
3067 key_file->approximate_size -= strlen (pair->key) + 1;
3069 g_warn_if_fail (pair->value != NULL);
3070 key_file->approximate_size -= strlen (pair->value);
3072 g_key_file_key_value_pair_free (pair);
3074 g_list_free_1 (pair_node);
3078 g_key_file_remove_group_node (GKeyFile *key_file,
3081 GKeyFileGroup *group;
3084 group = (GKeyFileGroup *) group_node->data;
3087 g_hash_table_remove (key_file->group_hash, group->name);
3089 /* If the current group gets deleted make the current group the last
3092 if (key_file->current_group == group)
3094 /* groups should always contain at least the top comment group,
3095 * unless g_key_file_clear has been called
3097 if (key_file->groups)
3098 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3100 key_file->current_group = NULL;
3103 /* If the start group gets deleted make the start group the first
3106 if (key_file->start_group == group)
3108 tmp = g_list_last (key_file->groups);
3111 if (tmp != group_node &&
3112 ((GKeyFileGroup *) tmp->data)->name != NULL)
3119 key_file->start_group = (GKeyFileGroup *) tmp->data;
3121 key_file->start_group = NULL;
3124 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3126 if (group->name != NULL)
3127 key_file->approximate_size -= strlen (group->name) + 3;
3129 tmp = group->key_value_pairs;
3136 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3139 g_warn_if_fail (group->key_value_pairs == NULL);
3141 if (group->lookup_map)
3143 g_hash_table_destroy (group->lookup_map);
3144 group->lookup_map = NULL;
3147 g_free ((gchar *) group->name);
3148 g_slice_free (GKeyFileGroup, group);
3149 g_list_free_1 (group_node);
3153 * g_key_file_remove_group:
3154 * @key_file: a #GKeyFile
3155 * @group_name: a group name
3156 * @error: return location for a #GError or %NULL
3158 * Removes the specified group, @group_name,
3159 * from the key file.
3161 * Returns: %TRUE if the group was removed, %FALSE otherwise
3166 g_key_file_remove_group (GKeyFile *key_file,
3167 const gchar *group_name,
3172 g_return_val_if_fail (key_file != NULL, FALSE);
3173 g_return_val_if_fail (group_name != NULL, FALSE);
3175 group_node = g_key_file_lookup_group_node (key_file, group_name);
3179 g_set_error (error, G_KEY_FILE_ERROR,
3180 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3181 _("Key file does not have group '%s'"),
3186 g_key_file_remove_group_node (key_file, group_node);
3192 g_key_file_add_key (GKeyFile *key_file,
3193 GKeyFileGroup *group,
3197 GKeyFileKeyValuePair *pair;
3199 pair = g_slice_new (GKeyFileKeyValuePair);
3200 pair->key = g_strdup (key);
3201 pair->value = g_strdup (value);
3203 g_hash_table_replace (group->lookup_map, pair->key, pair);
3204 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3205 group->has_trailing_blank_line = FALSE;
3206 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3210 * g_key_file_remove_key:
3211 * @key_file: a #GKeyFile
3212 * @group_name: a group name
3213 * @key: a key name to remove
3214 * @error: return location for a #GError or %NULL
3216 * Removes @key in @group_name from the key file.
3218 * Returns: %TRUE if the key was removed, %FALSE otherwise
3223 g_key_file_remove_key (GKeyFile *key_file,
3224 const gchar *group_name,
3228 GKeyFileGroup *group;
3229 GKeyFileKeyValuePair *pair;
3231 g_return_val_if_fail (key_file != NULL, FALSE);
3232 g_return_val_if_fail (group_name != NULL, FALSE);
3233 g_return_val_if_fail (key != NULL, FALSE);
3237 group = g_key_file_lookup_group (key_file, group_name);
3240 g_set_error (error, G_KEY_FILE_ERROR,
3241 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3242 _("Key file does not have group '%s'"),
3243 group_name ? group_name : "(null)");
3247 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3251 g_set_error (error, G_KEY_FILE_ERROR,
3252 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3253 _("Key file does not have key '%s' in group '%s'"),
3258 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3260 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3261 g_hash_table_remove (group->lookup_map, pair->key);
3262 g_key_file_key_value_pair_free (pair);
3268 g_key_file_lookup_group_node (GKeyFile *key_file,
3269 const gchar *group_name)
3271 GKeyFileGroup *group;
3274 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3276 group = (GKeyFileGroup *) tmp->data;
3278 if (group && group->name && strcmp (group->name, group_name) == 0)
3285 static GKeyFileGroup *
3286 g_key_file_lookup_group (GKeyFile *key_file,
3287 const gchar *group_name)
3289 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3293 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3294 GKeyFileGroup *group,
3299 for (key_node = group->key_value_pairs;
3301 key_node = key_node->next)
3303 GKeyFileKeyValuePair *pair;
3305 pair = (GKeyFileKeyValuePair *) key_node->data;
3307 if (pair->key && strcmp (pair->key, key) == 0)
3314 static GKeyFileKeyValuePair *
3315 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3316 GKeyFileGroup *group,
3319 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3322 /* Lines starting with # or consisting entirely of whitespace are merely
3323 * recorded, not parsed. This function assumes all leading whitespace
3324 * has been stripped.
3327 g_key_file_line_is_comment (const gchar *line)
3329 return (*line == '#' || *line == '\0' || *line == '\n');
3333 g_key_file_is_group_name (const gchar *name)
3340 p = q = (gchar *) name;
3341 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3342 q = g_utf8_find_next_char (q, NULL);
3344 if (*q != '\0' || q == p)
3351 g_key_file_is_key_name (const gchar *name)
3358 p = q = (gchar *) name;
3359 /* We accept a little more than the desktop entry spec says,
3360 * since gnome-vfs uses mime-types as keys in its cache.
3362 while (*q && *q != '=' && *q != '[' && *q != ']')
3363 q = g_utf8_find_next_char (q, NULL);
3365 /* No empty keys, please */
3369 /* We accept spaces in the middle of keys to not break
3370 * existing apps, but we don't tolerate initial or final
3371 * spaces, which would lead to silent corruption when
3372 * rereading the file.
3374 if (*p == ' ' || q[-1] == ' ')
3380 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3381 q = g_utf8_find_next_char (q, NULL);
3395 /* A group in a key file is made up of a starting '[' followed by one
3396 * or more letters making up the group name followed by ']'.
3399 g_key_file_line_is_group (const gchar *line)
3409 while (*p && *p != ']')
3410 p = g_utf8_find_next_char (p, NULL);
3415 /* silently accept whitespace after the ] */
3416 p = g_utf8_find_next_char (p, NULL);
3417 while (*p == ' ' || *p == '\t')
3418 p = g_utf8_find_next_char (p, NULL);
3427 g_key_file_line_is_key_value_pair (const gchar *line)
3431 p = (gchar *) g_utf8_strchr (line, -1, '=');
3436 /* Key must be non-empty
3445 g_key_file_parse_value_as_string (GKeyFile *key_file,
3450 gchar *string_value, *p, *q0, *q;
3452 string_value = g_new (gchar, strlen (value) + 1);
3454 p = (gchar *) value;
3455 q0 = q = string_value;
3485 g_set_error_literal (error, G_KEY_FILE_ERROR,
3486 G_KEY_FILE_ERROR_INVALID_VALUE,
3487 _("Key file contains escape character "
3492 if (pieces && *p == key_file->list_separator)
3493 *q = key_file->list_separator;
3507 g_set_error (error, G_KEY_FILE_ERROR,
3508 G_KEY_FILE_ERROR_INVALID_VALUE,
3509 _("Key file contains invalid escape "
3510 "sequence '%s'"), sequence);
3519 if (pieces && (*p == key_file->list_separator))
3521 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3537 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3538 *pieces = g_slist_reverse (*pieces);
3541 return string_value;
3545 g_key_file_parse_string_as_value (GKeyFile *key_file,
3546 const gchar *string,
3547 gboolean escape_separator)
3549 gchar *value, *p, *q;
3551 gboolean parsing_leading_space;
3553 length = strlen (string) + 1;
3555 /* Worst case would be that every character needs to be escaped.
3556 * In other words every character turns to two characters
3558 value = g_new (gchar, 2 * length);
3560 p = (gchar *) string;
3562 parsing_leading_space = TRUE;
3563 while (p < (string + length - 1))
3565 gchar escaped_character[3] = { '\\', 0, 0 };
3570 if (parsing_leading_space)
3572 escaped_character[1] = 's';
3573 strcpy (q, escaped_character);
3583 if (parsing_leading_space)
3585 escaped_character[1] = 't';
3586 strcpy (q, escaped_character);
3596 escaped_character[1] = 'n';
3597 strcpy (q, escaped_character);
3601 escaped_character[1] = 'r';
3602 strcpy (q, escaped_character);
3606 escaped_character[1] = '\\';
3607 strcpy (q, escaped_character);
3609 parsing_leading_space = FALSE;
3612 if (escape_separator && *p == key_file->list_separator)
3614 escaped_character[1] = key_file->list_separator;
3615 strcpy (q, escaped_character);
3617 parsing_leading_space = TRUE;
3623 parsing_leading_space = FALSE;
3635 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3639 gchar *end_of_valid_int;
3644 long_value = strtol (value, &end_of_valid_int, 10);
3646 if (*value == '\0' || *end_of_valid_int != '\0')
3648 gchar *value_utf8 = _g_utf8_make_valid (value);
3649 g_set_error (error, G_KEY_FILE_ERROR,
3650 G_KEY_FILE_ERROR_INVALID_VALUE,
3651 _("Value '%s' cannot be interpreted "
3652 "as a number."), value_utf8);
3653 g_free (value_utf8);
3658 int_value = long_value;
3659 if (int_value != long_value || errno == ERANGE)
3661 gchar *value_utf8 = _g_utf8_make_valid (value);
3664 G_KEY_FILE_ERROR_INVALID_VALUE,
3665 _("Integer value '%s' out of range"),
3667 g_free (value_utf8);
3676 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3680 return g_strdup_printf ("%d", value);
3684 g_key_file_parse_value_as_double (GKeyFile *key_file,
3688 gchar *end_of_valid_d;
3689 gdouble double_value = 0;
3691 double_value = g_ascii_strtod (value, &end_of_valid_d);
3693 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3695 gchar *value_utf8 = _g_utf8_make_valid (value);
3696 g_set_error (error, G_KEY_FILE_ERROR,
3697 G_KEY_FILE_ERROR_INVALID_VALUE,
3698 _("Value '%s' cannot be interpreted "
3699 "as a float number."),
3701 g_free (value_utf8);
3704 return double_value;
3708 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3714 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3716 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3719 value_utf8 = _g_utf8_make_valid (value);
3720 g_set_error (error, G_KEY_FILE_ERROR,
3721 G_KEY_FILE_ERROR_INVALID_VALUE,
3722 _("Value '%s' cannot be interpreted "
3723 "as a boolean."), value_utf8);
3724 g_free (value_utf8);
3730 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3734 return g_strdup ("true");
3736 return g_strdup ("false");
3740 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3747 string = g_string_sized_new (512);
3749 lines = g_strsplit (value, "\n", 0);
3751 for (i = 0; lines[i] != NULL; i++)
3753 if (lines[i][0] != '#')
3754 g_string_append_printf (string, "%s\n", lines[i]);
3756 g_string_append_printf (string, "%s\n", lines[i] + 1);
3760 return g_string_free (string, FALSE);
3764 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3765 const gchar *comment)
3771 string = g_string_sized_new (512);
3773 lines = g_strsplit (comment, "\n", 0);
3775 for (i = 0; lines[i] != NULL; i++)
3776 g_string_append_printf (string, "#%s%s", lines[i],
3777 lines[i + 1] == NULL? "" : "\n");
3780 return g_string_free (string, FALSE);
3783 #define __G_KEY_FILE_C__
3784 #include "galiasdef.c"