1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
5 * Written by Ray Strode <rstrode@redhat.com>
6 * Matthias Clasen <mclasen@redhat.com>
8 * GLib is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * GLib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with GLib; see the file COPYING.LIB. If not,
20 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
34 #include <sys/types.h>
43 #define S_ISREG(mode) ((mode)&_S_IFREG)
46 #endif /* G_OS_WIN23 */
51 #include "gfileutils.h"
57 #include "gmessages.h"
60 #include "gstrfuncs.h"
65 typedef struct _GKeyFileGroup GKeyFileGroup;
70 GHashTable *group_hash;
72 GKeyFileGroup *start_group;
73 GKeyFileGroup *current_group;
75 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
77 /* Used for sizing the output buffer during serialization
79 gsize approximate_size;
86 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
90 const gchar *name; /* NULL for above first group (which will be comments) */
92 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
93 gboolean has_trailing_blank_line;
95 GList *key_value_pairs;
97 /* Used in parallel with key_value_pairs for
98 * increased lookup performance
100 GHashTable *lookup_map;
103 struct _GKeyFileKeyValuePair
105 gchar *key; /* NULL for comments */
109 static gint find_file_in_data_dirs (const gchar *file,
110 const gchar **data_dirs,
113 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
117 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
118 const gchar *group_name);
119 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
120 const gchar *group_name);
122 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
123 GKeyFileGroup *group,
125 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
126 GKeyFileGroup *group,
129 static void g_key_file_remove_group_node (GKeyFile *key_file,
131 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
132 GKeyFileGroup *group,
135 static void g_key_file_add_key (GKeyFile *key_file,
136 GKeyFileGroup *group,
139 static void g_key_file_add_group (GKeyFile *key_file,
140 const gchar *group_name);
141 static gboolean g_key_file_is_group_name (const gchar *name);
142 static gboolean g_key_file_is_key_name (const gchar *name);
143 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
144 static gboolean g_key_file_line_is_comment (const gchar *line);
145 static gboolean g_key_file_line_is_group (const gchar *line);
146 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
147 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
151 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
153 gboolean escape_separator);
154 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
157 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
159 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
162 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
165 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
167 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
169 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
170 const gchar *comment);
171 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
175 static void g_key_file_parse_comment (GKeyFile *key_file,
179 static void g_key_file_parse_group (GKeyFile *key_file,
183 static gchar *key_get_locale (const gchar *key);
184 static void g_key_file_parse_data (GKeyFile *key_file,
188 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
193 g_key_file_error_quark (void)
195 return g_quark_from_static_string ("g-key-file-error-quark");
199 g_key_file_init (GKeyFile *key_file)
201 key_file->current_group = g_slice_new0 (GKeyFileGroup);
202 key_file->groups = g_list_prepend (NULL, key_file->current_group);
203 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
204 key_file->start_group = NULL;
205 key_file->parse_buffer = g_string_sized_new (128);
206 key_file->approximate_size = 0;
207 key_file->list_separator = ';';
212 g_key_file_clear (GKeyFile *key_file)
214 GList *tmp, *group_node;
216 if (key_file->parse_buffer)
217 g_string_free (key_file->parse_buffer, TRUE);
219 tmp = key_file->groups;
224 g_key_file_remove_group_node (key_file, group_node);
227 g_assert (key_file->groups == NULL);
234 * Creates a new empty #GKeyFile object. Use
235 * g_key_file_load_from_file(), g_key_file_load_from_data(),
236 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
237 * read an existing key file.
239 * Return value: an empty #GKeyFile.
244 g_key_file_new (void)
248 key_file = g_slice_new0 (GKeyFile);
249 g_key_file_init (key_file);
255 * g_key_file_set_list_separator:
256 * @key_file: a #GKeyFile
257 * @separator: the separator
259 * Sets the character which is used to separate
260 * values in lists. Typically ';' or ',' are used
261 * as separators. The default list separator is ';'.
266 g_key_file_set_list_separator (GKeyFile *key_file,
269 g_return_if_fail (key_file != NULL);
271 key_file->list_separator = separator;
275 /* Iterates through all the directories in *dirs trying to
276 * open file. When it successfully locates and opens a file it
277 * returns the file descriptor to the open file. It also
278 * outputs the absolute path of the file in output_file.
281 find_file_in_data_dirs (const gchar *file,
286 const gchar **data_dirs, *data_dir;
298 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
300 gchar *candidate_file, *sub_dir;
302 candidate_file = (gchar *) file;
303 sub_dir = g_strdup ("");
304 while (candidate_file != NULL && fd < 0)
308 path = g_build_filename (data_dir, sub_dir,
309 candidate_file, NULL);
311 fd = g_open (path, O_RDONLY, 0);
319 candidate_file = strchr (candidate_file, '-');
321 if (candidate_file == NULL)
327 sub_dir = g_strndup (file, candidate_file - file - 1);
329 for (p = sub_dir; *p != '\0'; p++)
332 *p = G_DIR_SEPARATOR;
341 g_set_error (error, G_KEY_FILE_ERROR,
342 G_KEY_FILE_ERROR_NOT_FOUND,
343 _("Valid key file could not be "
344 "found in search dirs"));
347 if (output_file != NULL && fd > 0)
348 *output_file = g_strdup (path);
356 g_key_file_load_from_fd (GKeyFile *key_file,
361 GError *key_file_error = NULL;
363 struct stat stat_buf;
364 gchar read_buf[4096];
366 if (fstat (fd, &stat_buf) < 0)
368 g_set_error (error, G_FILE_ERROR,
369 g_file_error_from_errno (errno),
370 "%s", g_strerror (errno));
374 if (!S_ISREG (stat_buf.st_mode))
376 g_set_error (error, G_KEY_FILE_ERROR,
377 G_KEY_FILE_ERROR_PARSE,
378 _("Not a regular file"));
382 if (stat_buf.st_size == 0)
384 g_set_error (error, G_KEY_FILE_ERROR,
385 G_KEY_FILE_ERROR_PARSE,
390 if (key_file->approximate_size > 0)
392 g_key_file_clear (key_file);
393 g_key_file_init (key_file);
395 key_file->flags = flags;
400 bytes_read = read (fd, read_buf, 4096);
402 if (bytes_read == 0) /* End of File */
407 if (errno == EINTR || errno == EAGAIN)
410 g_set_error (error, G_FILE_ERROR,
411 g_file_error_from_errno (errno),
412 "%s", g_strerror (errno));
416 g_key_file_parse_data (key_file,
417 read_buf, bytes_read,
420 while (!key_file_error);
424 g_propagate_error (error, key_file_error);
428 g_key_file_flush_parse_buffer (key_file, &key_file_error);
432 g_propagate_error (error, key_file_error);
440 * g_key_file_load_from_file:
441 * @key_file: an empty #GKeyFile struct
442 * @file: the path of a filename to load, in the GLib filename encoding
443 * @flags: flags from #GKeyFileFlags
444 * @error: return location for a #GError, or %NULL
446 * Loads a key file into an empty #GKeyFile structure.
447 * If the file could not be loaded then %error is set to
448 * either a #GFileError or #GKeyFileError.
450 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
455 g_key_file_load_from_file (GKeyFile *key_file,
460 GError *key_file_error = NULL;
463 g_return_val_if_fail (key_file != NULL, FALSE);
464 g_return_val_if_fail (file != NULL, FALSE);
466 fd = g_open (file, O_RDONLY, 0);
470 g_set_error (error, G_FILE_ERROR,
471 g_file_error_from_errno (errno),
472 "%s", g_strerror (errno));
476 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
481 g_propagate_error (error, key_file_error);
489 * g_key_file_load_from_data:
490 * @key_file: an empty #GKeyFile struct
491 * @data: key file loaded in memory
492 * @length: the length of @data in bytes
493 * @flags: flags from #GKeyFileFlags
494 * @error: return location for a #GError, or %NULL
496 * Loads a key file from memory into an empty #GKeyFile structure.
497 * If the object cannot be created then %error is set to a #GKeyFileError.
499 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
504 g_key_file_load_from_data (GKeyFile *key_file,
510 GError *key_file_error = NULL;
512 g_return_val_if_fail (key_file != NULL, FALSE);
513 g_return_val_if_fail (data != NULL, FALSE);
514 g_return_val_if_fail (length != 0, FALSE);
516 if (length == (gsize)-1)
517 length = strlen (data);
519 if (key_file->approximate_size > 0)
521 g_key_file_clear (key_file);
522 g_key_file_init (key_file);
524 key_file->flags = flags;
526 g_key_file_parse_data (key_file, data, length, &key_file_error);
530 g_propagate_error (error, key_file_error);
534 g_key_file_flush_parse_buffer (key_file, &key_file_error);
538 g_propagate_error (error, key_file_error);
546 * g_key_file_load_from_dirs:
547 * @key_file: an empty #GKeyFile struct
548 * @file: a relative path to a filename to open and parse
549 * @search_dirs: %NULL-terminated array of directories to search
550 * @full_path: return location for a string containing the full path
551 * of the file, or %NULL
552 * @flags: flags from #GKeyFileFlags
553 * @error: return location for a #GError, or %NULL
555 * This function looks for a key file named @file in the paths
556 * specified in @search_dirs, loads the file into @key_file and
557 * returns the file's full path in @full_path. If the file could not
558 * be loaded then an %error is set to either a #GFileError or
561 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
566 g_key_file_load_from_dirs (GKeyFile *key_file,
568 const gchar **search_dirs,
573 GError *key_file_error = NULL;
574 const gchar **data_dirs;
579 g_return_val_if_fail (key_file != NULL, FALSE);
580 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
581 g_return_val_if_fail (search_dirs != NULL, FALSE);
584 data_dirs = search_dirs;
586 while (*data_dirs != NULL && !found_file)
588 g_free (output_path);
590 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
596 g_propagate_error (error, key_file_error);
600 found_file = g_key_file_load_from_fd (key_file, fd, flags,
606 g_propagate_error (error, key_file_error);
611 if (found_file && full_path)
612 *full_path = output_path;
614 g_free (output_path);
620 * g_key_file_load_from_data_dirs:
621 * @key_file: an empty #GKeyFile struct
622 * @file: a relative path to a filename to open and parse
623 * @full_path: return location for a string containing the full path
624 * of the file, or %NULL
625 * @flags: flags from #GKeyFileFlags
626 * @error: return location for a #GError, or %NULL
628 * This function looks for a key file named @file in the paths
629 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
630 * loads the file into @key_file and returns the file's full path in
631 * @full_path. If the file could not be loaded then an %error is
632 * set to either a #GFileError or #GKeyFileError.
634 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
638 g_key_file_load_from_data_dirs (GKeyFile *key_file,
644 gchar **all_data_dirs;
645 const gchar * user_data_dir;
646 const gchar * const * system_data_dirs;
650 g_return_val_if_fail (key_file != NULL, FALSE);
651 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
653 user_data_dir = g_get_user_data_dir ();
654 system_data_dirs = g_get_system_data_dirs ();
655 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
658 all_data_dirs[i++] = g_strdup (user_data_dir);
661 while (system_data_dirs[j] != NULL)
662 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
663 all_data_dirs[i] = NULL;
665 found_file = g_key_file_load_from_dirs (key_file,
667 (const gchar **)all_data_dirs,
672 g_strfreev (all_data_dirs);
679 * @key_file: a #GKeyFile
686 g_key_file_free (GKeyFile *key_file)
688 g_return_if_fail (key_file != NULL);
690 g_key_file_clear (key_file);
691 g_slice_free (GKeyFile, key_file);
694 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
695 * true for locales that match those in g_get_language_names().
698 g_key_file_locale_is_interesting (GKeyFile *key_file,
701 const gchar * const * current_locales;
704 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
707 current_locales = g_get_language_names ();
709 for (i = 0; current_locales[i] != NULL; i++)
711 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
719 g_key_file_parse_line (GKeyFile *key_file,
724 GError *parse_error = NULL;
727 g_return_if_fail (key_file != NULL);
728 g_return_if_fail (line != NULL);
730 line_start = (gchar *) line;
731 while (g_ascii_isspace (*line_start))
734 if (g_key_file_line_is_comment (line_start))
735 g_key_file_parse_comment (key_file, line, length, &parse_error);
736 else if (g_key_file_line_is_group (line_start))
737 g_key_file_parse_group (key_file, line_start,
738 length - (line_start - line),
740 else if (g_key_file_line_is_key_value_pair (line_start))
741 g_key_file_parse_key_value_pair (key_file, line_start,
742 length - (line_start - line),
746 gchar *line_utf8 = _g_utf8_make_valid (line);
747 g_set_error (error, G_KEY_FILE_ERROR,
748 G_KEY_FILE_ERROR_PARSE,
749 _("Key file contains line '%s' which is not "
750 "a key-value pair, group, or comment"),
758 g_propagate_error (error, parse_error);
762 g_key_file_parse_comment (GKeyFile *key_file,
767 GKeyFileKeyValuePair *pair;
769 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
772 g_assert (key_file->current_group != NULL);
774 pair = g_slice_new (GKeyFileKeyValuePair);
776 pair->value = g_strndup (line, length);
778 key_file->current_group->key_value_pairs =
779 g_list_prepend (key_file->current_group->key_value_pairs, pair);
781 if (length == 0 || line[0] != '#')
782 key_file->current_group->has_trailing_blank_line = TRUE;
786 g_key_file_parse_group (GKeyFile *key_file,
792 const gchar *group_name_start, *group_name_end;
794 /* advance past opening '['
796 group_name_start = line + 1;
797 group_name_end = line + length - 1;
799 while (*group_name_end != ']')
802 group_name = g_strndup (group_name_start,
803 group_name_end - group_name_start);
805 if (!g_key_file_is_group_name (group_name))
807 g_set_error (error, G_KEY_FILE_ERROR,
808 G_KEY_FILE_ERROR_PARSE,
809 _("Invalid group name: %s"), group_name);
814 g_key_file_add_group (key_file, group_name);
819 g_key_file_parse_key_value_pair (GKeyFile *key_file,
824 gchar *key, *value, *key_end, *value_start, *locale;
825 gsize key_len, value_len;
827 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
829 g_set_error (error, G_KEY_FILE_ERROR,
830 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
831 _("Key file does not start with a group"));
835 key_end = value_start = strchr (line, '=');
837 g_assert (key_end != NULL);
842 /* Pull the key name from the line (chomping trailing whitespace)
844 while (g_ascii_isspace (*key_end))
847 key_len = key_end - line + 2;
849 g_assert (key_len <= length);
851 key = g_strndup (line, key_len - 1);
853 if (!g_key_file_is_key_name (key))
855 g_set_error (error, G_KEY_FILE_ERROR,
856 G_KEY_FILE_ERROR_PARSE,
857 _("Invalid key name: %s"), key);
862 /* Pull the value from the line (chugging leading whitespace)
864 while (g_ascii_isspace (*value_start))
867 value_len = line + length - value_start + 1;
869 value = g_strndup (value_start, value_len);
871 g_assert (key_file->start_group != NULL);
873 if (key_file->current_group
874 && key_file->current_group->name
875 && strcmp (key_file->start_group->name,
876 key_file->current_group->name) == 0
877 && strcmp (key, "Encoding") == 0)
879 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
881 gchar *value_utf8 = _g_utf8_make_valid (value);
882 g_set_error (error, G_KEY_FILE_ERROR,
883 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
884 _("Key file contains unsupported "
885 "encoding '%s'"), value_utf8);
894 /* Is this key a translation? If so, is it one that we care about?
896 locale = key_get_locale (key);
898 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
899 g_key_file_add_key (key_file, key_file->current_group, key, value);
907 key_get_locale (const gchar *key)
911 locale = g_strrstr (key, "[");
913 if (locale && strlen (locale) <= 2)
917 locale = g_strndup (locale + 1, strlen (locale) - 2);
923 g_key_file_parse_data (GKeyFile *key_file,
931 g_return_if_fail (key_file != NULL);
932 g_return_if_fail (data != NULL);
936 for (i = 0; i < length; i++)
940 if (i > 0 && data[i - 1] == '\r')
941 g_string_erase (key_file->parse_buffer,
942 key_file->parse_buffer->len - 1,
945 /* When a newline is encountered flush the parse buffer so that the
946 * line can be parsed. Note that completely blank lines won't show
947 * up in the parse buffer, so they get parsed directly.
949 if (key_file->parse_buffer->len > 0)
950 g_key_file_flush_parse_buffer (key_file, &parse_error);
952 g_key_file_parse_comment (key_file, "", 1, &parse_error);
956 g_propagate_error (error, parse_error);
961 g_string_append_c (key_file->parse_buffer, data[i]);
964 key_file->approximate_size += length;
968 g_key_file_flush_parse_buffer (GKeyFile *key_file,
971 GError *file_error = NULL;
973 g_return_if_fail (key_file != NULL);
977 if (key_file->parse_buffer->len > 0)
979 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
980 key_file->parse_buffer->len,
982 g_string_erase (key_file->parse_buffer, 0, -1);
986 g_propagate_error (error, file_error);
993 * g_key_file_to_data:
994 * @key_file: a #GKeyFile
995 * @length: return location for the length of the
996 * returned string, or %NULL
997 * @error: return location for a #GError, or %NULL
999 * This function outputs @key_file as a string.
1001 * Return value: a newly allocated string holding
1002 * the contents of the #GKeyFile
1007 g_key_file_to_data (GKeyFile *key_file,
1011 GString *data_string;
1012 GList *group_node, *key_file_node;
1013 gboolean has_blank_line = TRUE;
1015 g_return_val_if_fail (key_file != NULL, NULL);
1017 data_string = g_string_sized_new (2 * key_file->approximate_size);
1019 for (group_node = g_list_last (key_file->groups);
1021 group_node = group_node->prev)
1023 GKeyFileGroup *group;
1025 group = (GKeyFileGroup *) group_node->data;
1027 /* separate groups by at least an empty line */
1028 if (!has_blank_line)
1029 g_string_append_c (data_string, '\n');
1030 has_blank_line = group->has_trailing_blank_line;
1032 if (group->comment != NULL)
1033 g_string_append_printf (data_string, "%s\n", group->comment->value);
1035 if (group->name != NULL)
1036 g_string_append_printf (data_string, "[%s]\n", group->name);
1038 for (key_file_node = g_list_last (group->key_value_pairs);
1039 key_file_node != NULL;
1040 key_file_node = key_file_node->prev)
1042 GKeyFileKeyValuePair *pair;
1044 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1046 if (pair->key != NULL)
1047 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1049 g_string_append_printf (data_string, "%s\n", pair->value);
1054 *length = data_string->len;
1056 return g_string_free (data_string, FALSE);
1060 * g_key_file_get_keys:
1061 * @key_file: a #GKeyFile
1062 * @group_name: a group name
1063 * @length: return location for the number of keys returned, or %NULL
1064 * @error: return location for a #GError, or %NULL
1066 * Returns all keys for the group name @group_name. The array of
1067 * returned keys will be %NULL-terminated, so @length may
1068 * optionally be %NULL. In the event that the @group_name cannot
1069 * be found, %NULL is returned and @error is set to
1070 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1072 * Return value: a newly-allocated %NULL-terminated array of strings.
1073 * Use g_strfreev() to free it.
1078 g_key_file_get_keys (GKeyFile *key_file,
1079 const gchar *group_name,
1083 GKeyFileGroup *group;
1088 g_return_val_if_fail (key_file != NULL, NULL);
1089 g_return_val_if_fail (group_name != NULL, NULL);
1091 group = g_key_file_lookup_group (key_file, group_name);
1095 g_set_error (error, G_KEY_FILE_ERROR,
1096 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1097 _("Key file does not have group '%s'"),
1098 group_name ? group_name : "(null)");
1103 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1105 GKeyFileKeyValuePair *pair;
1107 pair = (GKeyFileKeyValuePair *) tmp->data;
1113 keys = g_new (gchar *, num_keys + 1);
1116 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1118 GKeyFileKeyValuePair *pair;
1120 pair = (GKeyFileKeyValuePair *) tmp->data;
1124 keys[i] = g_strdup (pair->key);
1129 keys[num_keys] = NULL;
1138 * g_key_file_get_start_group:
1139 * @key_file: a #GKeyFile
1141 * Returns the name of the start group of the file.
1143 * Return value: The start group of the key file.
1148 g_key_file_get_start_group (GKeyFile *key_file)
1150 g_return_val_if_fail (key_file != NULL, NULL);
1152 if (key_file->start_group)
1153 return g_strdup (key_file->start_group->name);
1159 * g_key_file_get_groups:
1160 * @key_file: a #GKeyFile
1161 * @length: return location for the number of returned groups, or %NULL
1163 * Returns all groups in the key file loaded with @key_file.
1164 * The array of returned groups will be %NULL-terminated, so
1165 * @length may optionally be %NULL.
1167 * Return value: a newly-allocated %NULL-terminated array of strings.
1168 * Use g_strfreev() to free it.
1172 g_key_file_get_groups (GKeyFile *key_file,
1177 gsize i, num_groups;
1179 g_return_val_if_fail (key_file != NULL, NULL);
1181 num_groups = g_list_length (key_file->groups);
1183 g_assert (num_groups > 0);
1185 /* Only need num_groups instead of num_groups + 1
1186 * because the first group of the file (last in the
1187 * list) is always the comment group at the top,
1190 groups = g_new (gchar *, num_groups);
1192 group_node = g_list_last (key_file->groups);
1194 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1197 for (group_node = group_node->prev;
1199 group_node = group_node->prev)
1201 GKeyFileGroup *group;
1203 group = (GKeyFileGroup *) group_node->data;
1205 g_assert (group->name != NULL);
1207 groups[i++] = g_strdup (group->name);
1218 * g_key_file_get_value:
1219 * @key_file: a #GKeyFile
1220 * @group_name: a group name
1222 * @error: return location for a #GError, or %NULL
1224 * Returns the value associated with @key under @group_name.
1226 * In the event the key cannot be found, %NULL is returned and
1227 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1228 * event that the @group_name cannot be found, %NULL is returned
1229 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1231 * Return value: a newly allocated string or %NULL if the specified
1232 * key cannot be found.
1237 g_key_file_get_value (GKeyFile *key_file,
1238 const gchar *group_name,
1242 GKeyFileGroup *group;
1243 GKeyFileKeyValuePair *pair;
1244 gchar *value = NULL;
1246 g_return_val_if_fail (key_file != NULL, NULL);
1247 g_return_val_if_fail (group_name != NULL, NULL);
1248 g_return_val_if_fail (key != NULL, NULL);
1250 group = g_key_file_lookup_group (key_file, group_name);
1254 g_set_error (error, G_KEY_FILE_ERROR,
1255 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1256 _("Key file does not have group '%s'"),
1257 group_name ? group_name : "(null)");
1261 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1264 value = g_strdup (pair->value);
1266 g_set_error (error, G_KEY_FILE_ERROR,
1267 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1268 _("Key file does not have key '%s'"), key);
1274 * g_key_file_set_value:
1275 * @key_file: a #GKeyFile
1276 * @group_name: a group name
1280 * Associates a new value with @key under @group_name.
1281 * If @key cannot be found then it is created.
1282 * If @group_name cannot be found then it is created.
1287 g_key_file_set_value (GKeyFile *key_file,
1288 const gchar *group_name,
1292 GKeyFileGroup *group;
1293 GKeyFileKeyValuePair *pair;
1295 g_return_if_fail (key_file != NULL);
1296 g_return_if_fail (g_key_file_is_group_name (group_name));
1297 g_return_if_fail (g_key_file_is_key_name (key));
1298 g_return_if_fail (value != NULL);
1300 group = g_key_file_lookup_group (key_file, group_name);
1304 g_key_file_add_group (key_file, group_name);
1305 group = (GKeyFileGroup *) key_file->groups->data;
1307 g_key_file_add_key (key_file, group, key, value);
1311 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1314 g_key_file_add_key (key_file, group, key, value);
1317 g_free (pair->value);
1318 pair->value = g_strdup (value);
1324 * g_key_file_get_string:
1325 * @key_file: a #GKeyFile
1326 * @group_name: a group name
1328 * @error: return location for a #GError, or %NULL
1330 * Returns the value associated with @key under @group_name.
1332 * In the event the key cannot be found, %NULL is returned and
1333 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1334 * event that the @group_name cannot be found, %NULL is returned
1335 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1337 * Return value: a newly allocated string or %NULL if the specified
1338 * key cannot be found.
1343 g_key_file_get_string (GKeyFile *key_file,
1344 const gchar *group_name,
1348 gchar *value, *string_value;
1349 GError *key_file_error;
1351 g_return_val_if_fail (key_file != NULL, NULL);
1352 g_return_val_if_fail (group_name != NULL, NULL);
1353 g_return_val_if_fail (key != NULL, NULL);
1355 key_file_error = NULL;
1357 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1361 g_propagate_error (error, key_file_error);
1365 if (!g_utf8_validate (value, -1, NULL))
1367 gchar *value_utf8 = _g_utf8_make_valid (value);
1368 g_set_error (error, G_KEY_FILE_ERROR,
1369 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1370 _("Key file contains key '%s' with value '%s' "
1371 "which is not UTF-8"), key, value_utf8);
1372 g_free (value_utf8);
1378 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1384 if (g_error_matches (key_file_error,
1386 G_KEY_FILE_ERROR_INVALID_VALUE))
1388 g_set_error (error, G_KEY_FILE_ERROR,
1389 G_KEY_FILE_ERROR_INVALID_VALUE,
1390 _("Key file contains key '%s' "
1391 "which has value that cannot be interpreted."),
1393 g_error_free (key_file_error);
1396 g_propagate_error (error, key_file_error);
1399 return string_value;
1403 * g_key_file_set_string:
1404 * @key_file: a #GKeyFile
1405 * @group_name: a group name
1409 * Associates a new string value with @key under @group_name.
1410 * If @key cannot be found then it is created.
1411 * If @group_name cannot be found then it is created.
1416 g_key_file_set_string (GKeyFile *key_file,
1417 const gchar *group_name,
1419 const gchar *string)
1423 g_return_if_fail (key_file != NULL);
1424 g_return_if_fail (string != NULL);
1426 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1427 g_key_file_set_value (key_file, group_name, key, value);
1432 * g_key_file_get_string_list:
1433 * @key_file: a #GKeyFile
1434 * @group_name: a group name
1436 * @length: return location for the number of returned strings, or %NULL
1437 * @error: return location for a #GError, or %NULL
1439 * Returns the values associated with @key under @group_name.
1441 * In the event the key cannot be found, %NULL is returned and
1442 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1443 * event that the @group_name cannot be found, %NULL is returned
1444 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1446 * Return value: a %NULL-terminated string array or %NULL if the specified
1447 * key cannot be found. The array should be freed with g_strfreev().
1452 g_key_file_get_string_list (GKeyFile *key_file,
1453 const gchar *group_name,
1458 GError *key_file_error = NULL;
1459 gchar *value, *string_value, **values;
1461 GSList *p, *pieces = NULL;
1463 g_return_val_if_fail (key_file != NULL, NULL);
1464 g_return_val_if_fail (group_name != NULL, NULL);
1465 g_return_val_if_fail (key != NULL, NULL);
1470 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1474 g_propagate_error (error, key_file_error);
1478 if (!g_utf8_validate (value, -1, NULL))
1480 gchar *value_utf8 = _g_utf8_make_valid (value);
1481 g_set_error (error, G_KEY_FILE_ERROR,
1482 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1483 _("Key file contains key '%s' with value '%s' "
1484 "which is not UTF-8"), key, value_utf8);
1485 g_free (value_utf8);
1491 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1493 g_free (string_value);
1497 if (g_error_matches (key_file_error,
1499 G_KEY_FILE_ERROR_INVALID_VALUE))
1501 g_set_error (error, G_KEY_FILE_ERROR,
1502 G_KEY_FILE_ERROR_INVALID_VALUE,
1503 _("Key file contains key '%s' "
1504 "which has value that cannot be interpreted."),
1506 g_error_free (key_file_error);
1509 g_propagate_error (error, key_file_error);
1512 len = g_slist_length (pieces);
1513 values = g_new (gchar *, len + 1);
1514 for (p = pieces, i = 0; p; p = p->next)
1515 values[i++] = p->data;
1518 g_slist_free (pieces);
1527 * g_key_file_set_string_list:
1528 * @key_file: a #GKeyFile
1529 * @group_name: a group name
1531 * @list: an array of locale string values
1532 * @length: number of locale string values in @list
1534 * Associates a list of string values for @key under @group_name.
1535 * If @key cannot be found then it is created.
1536 * If @group_name cannot be found then it is created.
1541 g_key_file_set_string_list (GKeyFile *key_file,
1542 const gchar *group_name,
1544 const gchar * const list[],
1547 GString *value_list;
1550 g_return_if_fail (key_file != NULL);
1551 g_return_if_fail (list != NULL);
1553 value_list = g_string_sized_new (length * 128);
1554 for (i = 0; i < length && list[i] != NULL; i++)
1558 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1559 g_string_append (value_list, value);
1560 g_string_append_c (value_list, key_file->list_separator);
1565 g_key_file_set_value (key_file, group_name, key, value_list->str);
1566 g_string_free (value_list, TRUE);
1570 * g_key_file_set_locale_string:
1571 * @key_file: a #GKeyFile
1572 * @group_name: a group name
1577 * Associates a string value for @key and @locale under @group_name.
1578 * If the translation for @key cannot be found then it is created.
1583 g_key_file_set_locale_string (GKeyFile *key_file,
1584 const gchar *group_name,
1586 const gchar *locale,
1587 const gchar *string)
1589 gchar *full_key, *value;
1591 g_return_if_fail (key_file != NULL);
1592 g_return_if_fail (key != NULL);
1593 g_return_if_fail (locale != NULL);
1594 g_return_if_fail (string != NULL);
1596 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1597 full_key = g_strdup_printf ("%s[%s]", key, locale);
1598 g_key_file_set_value (key_file, group_name, full_key, value);
1603 extern GSList *_g_compute_locale_variants (const gchar *locale);
1606 * g_key_file_get_locale_string:
1607 * @key_file: a #GKeyFile
1608 * @group_name: a group name
1610 * @locale: a locale or %NULL
1611 * @error: return location for a #GError, or %NULL
1613 * Returns the value associated with @key under @group_name
1614 * translated in the given @locale if available. If @locale is
1615 * %NULL then the current locale is assumed.
1617 * If @key cannot be found then %NULL is returned and @error is set
1618 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1619 * with @key cannot be interpreted or no suitable translation can
1620 * be found then the untranslated value is returned.
1622 * Return value: a newly allocated string or %NULL if the specified
1623 * key cannot be found.
1628 g_key_file_get_locale_string (GKeyFile *key_file,
1629 const gchar *group_name,
1631 const gchar *locale,
1634 gchar *candidate_key, *translated_value;
1635 GError *key_file_error;
1637 gboolean free_languages = FALSE;
1640 g_return_val_if_fail (key_file != NULL, NULL);
1641 g_return_val_if_fail (group_name != NULL, NULL);
1642 g_return_val_if_fail (key != NULL, NULL);
1644 candidate_key = NULL;
1645 translated_value = NULL;
1646 key_file_error = NULL;
1652 list = _g_compute_locale_variants (locale);
1654 languages = g_new (gchar *, g_slist_length (list) + 1);
1655 for (l = list, i = 0; l; l = l->next, i++)
1656 languages[i] = l->data;
1657 languages[i] = NULL;
1659 g_slist_free (list);
1660 free_languages = TRUE;
1664 languages = (gchar **) g_get_language_names ();
1665 free_languages = FALSE;
1668 for (i = 0; languages[i]; i++)
1670 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1672 translated_value = g_key_file_get_string (key_file,
1674 candidate_key, NULL);
1675 g_free (candidate_key);
1677 if (translated_value)
1680 g_free (translated_value);
1681 translated_value = NULL;
1684 /* Fallback to untranslated key
1686 if (!translated_value)
1688 translated_value = g_key_file_get_string (key_file, group_name, key,
1691 if (!translated_value)
1692 g_propagate_error (error, key_file_error);
1696 g_strfreev (languages);
1698 return translated_value;
1702 * g_key_file_get_locale_string_list:
1703 * @key_file: a #GKeyFile
1704 * @group_name: a group name
1707 * @length: return location for the number of returned strings or %NULL
1708 * @error: return location for a #GError or %NULL
1710 * Returns the values associated with @key under @group_name
1711 * translated in the given @locale if available. If @locale is
1712 * %NULL then the current locale is assumed.
1714 * If @key cannot be found then %NULL is returned and @error is set
1715 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1716 * with @key cannot be interpreted or no suitable translations
1717 * can be found then the untranslated values are returned. The
1718 * returned array is %NULL-terminated, so @length may optionally
1721 * Return value: a newly allocated %NULL-terminated string array
1722 * or %NULL if the key isn't found. The string array should be freed
1723 * with g_strfreev().
1728 g_key_file_get_locale_string_list (GKeyFile *key_file,
1729 const gchar *group_name,
1731 const gchar *locale,
1735 GError *key_file_error;
1736 gchar **values, *value;
1738 g_return_val_if_fail (key_file != NULL, NULL);
1739 g_return_val_if_fail (group_name != NULL, NULL);
1740 g_return_val_if_fail (key != NULL, NULL);
1742 key_file_error = NULL;
1744 value = g_key_file_get_locale_string (key_file, group_name,
1749 g_propagate_error (error, key_file_error);
1758 if (value[strlen (value) - 1] == ';')
1759 value[strlen (value) - 1] = '\0';
1761 values = g_strsplit (value, ";", 0);
1766 *length = g_strv_length (values);
1772 * g_key_file_set_locale_string_list:
1773 * @key_file: a #GKeyFile
1774 * @group_name: a group name
1777 * @list: a %NULL-terminated array of locale string values
1778 * @length: the length of @list
1780 * Associates a list of string values for @key and @locale under
1781 * @group_name. If the translation for @key cannot be found then
1787 g_key_file_set_locale_string_list (GKeyFile *key_file,
1788 const gchar *group_name,
1790 const gchar *locale,
1791 const gchar * const list[],
1794 GString *value_list;
1798 g_return_if_fail (key_file != NULL);
1799 g_return_if_fail (key != NULL);
1800 g_return_if_fail (locale != NULL);
1801 g_return_if_fail (length != 0);
1803 value_list = g_string_sized_new (length * 128);
1804 for (i = 0; i < length && list[i] != NULL; i++)
1808 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1810 g_string_append (value_list, value);
1811 g_string_append_c (value_list, ';');
1816 full_key = g_strdup_printf ("%s[%s]", key, locale);
1817 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1819 g_string_free (value_list, TRUE);
1823 * g_key_file_get_boolean:
1824 * @key_file: a #GKeyFile
1825 * @group_name: a group name
1827 * @error: return location for a #GError
1829 * Returns the value associated with @key under @group_name as a
1832 * If @key cannot be found then %FALSE is returned and @error is set
1833 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1834 * associated with @key cannot be interpreted as a boolean then %FALSE
1835 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1837 * Return value: the value associated with the key as a boolean,
1838 * or %FALSE if the key was not found or could not be parsed.
1843 g_key_file_get_boolean (GKeyFile *key_file,
1844 const gchar *group_name,
1848 GError *key_file_error = NULL;
1850 gboolean bool_value;
1852 g_return_val_if_fail (key_file != NULL, FALSE);
1853 g_return_val_if_fail (group_name != NULL, FALSE);
1854 g_return_val_if_fail (key != NULL, FALSE);
1856 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1860 g_propagate_error (error, key_file_error);
1864 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1870 if (g_error_matches (key_file_error,
1872 G_KEY_FILE_ERROR_INVALID_VALUE))
1874 g_set_error (error, G_KEY_FILE_ERROR,
1875 G_KEY_FILE_ERROR_INVALID_VALUE,
1876 _("Key file contains key '%s' "
1877 "which has value that cannot be interpreted."),
1879 g_error_free (key_file_error);
1882 g_propagate_error (error, key_file_error);
1889 * g_key_file_set_boolean:
1890 * @key_file: a #GKeyFile
1891 * @group_name: a group name
1893 * @value: %TRUE or %FALSE
1895 * Associates a new boolean value with @key under @group_name.
1896 * If @key cannot be found then it is created.
1901 g_key_file_set_boolean (GKeyFile *key_file,
1902 const gchar *group_name,
1908 g_return_if_fail (key_file != NULL);
1910 result = g_key_file_parse_boolean_as_value (key_file, value);
1911 g_key_file_set_value (key_file, group_name, key, result);
1916 * g_key_file_get_boolean_list:
1917 * @key_file: a #GKeyFile
1918 * @group_name: a group name
1920 * @length: the number of booleans returned
1921 * @error: return location for a #GError
1923 * Returns the values associated with @key under @group_name as
1924 * booleans. If @group_name is %NULL, the start_group is used.
1926 * If @key cannot be found then %NULL is returned and @error is set to
1927 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1928 * with @key cannot be interpreted as booleans then %NULL is returned
1929 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1931 * Return value: the values associated with the key as a list of
1932 * booleans, or %NULL if the key was not found or could not be parsed.
1937 g_key_file_get_boolean_list (GKeyFile *key_file,
1938 const gchar *group_name,
1943 GError *key_file_error;
1945 gboolean *bool_values;
1948 g_return_val_if_fail (key_file != NULL, NULL);
1949 g_return_val_if_fail (group_name != NULL, NULL);
1950 g_return_val_if_fail (key != NULL, NULL);
1955 key_file_error = NULL;
1957 values = g_key_file_get_string_list (key_file, group_name, key,
1958 &num_bools, &key_file_error);
1961 g_propagate_error (error, key_file_error);
1966 bool_values = g_new (gboolean, num_bools);
1968 for (i = 0; i < num_bools; i++)
1970 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1976 g_propagate_error (error, key_file_error);
1977 g_strfreev (values);
1978 g_free (bool_values);
1983 g_strfreev (values);
1986 *length = num_bools;
1992 * g_key_file_set_boolean_list:
1993 * @key_file: a #GKeyFile
1994 * @group_name: a group name
1996 * @list: an array of boolean values
1997 * @length: length of @list
1999 * Associates a list of boolean values with @key under @group_name.
2000 * If @key cannot be found then it is created.
2001 * If @group_name is %NULL, the start_group is used.
2006 g_key_file_set_boolean_list (GKeyFile *key_file,
2007 const gchar *group_name,
2012 GString *value_list;
2015 g_return_if_fail (key_file != NULL);
2016 g_return_if_fail (list != NULL);
2018 value_list = g_string_sized_new (length * 8);
2019 for (i = 0; i < length; i++)
2023 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2025 g_string_append (value_list, value);
2026 g_string_append_c (value_list, key_file->list_separator);
2031 g_key_file_set_value (key_file, group_name, key, value_list->str);
2032 g_string_free (value_list, TRUE);
2036 * g_key_file_get_integer:
2037 * @key_file: a #GKeyFile
2038 * @group_name: a group name
2040 * @error: return location for a #GError
2042 * Returns the value associated with @key under @group_name as an
2043 * integer. If @group_name is %NULL, the start group is used.
2045 * If @key cannot be found then 0 is returned and @error is set to
2046 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2047 * with @key cannot be interpreted as an integer then 0 is returned
2048 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2050 * Return value: the value associated with the key as an integer, or
2051 * 0 if the key was not found or could not be parsed.
2056 g_key_file_get_integer (GKeyFile *key_file,
2057 const gchar *group_name,
2061 GError *key_file_error;
2065 g_return_val_if_fail (key_file != NULL, -1);
2066 g_return_val_if_fail (group_name != NULL, -1);
2067 g_return_val_if_fail (key != NULL, -1);
2069 key_file_error = NULL;
2071 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2075 g_propagate_error (error, key_file_error);
2079 int_value = g_key_file_parse_value_as_integer (key_file, value,
2085 if (g_error_matches (key_file_error,
2087 G_KEY_FILE_ERROR_INVALID_VALUE))
2089 g_set_error (error, G_KEY_FILE_ERROR,
2090 G_KEY_FILE_ERROR_INVALID_VALUE,
2091 _("Key file contains key '%s' in group '%s' "
2092 "which has value that cannot be interpreted."), key,
2094 g_error_free (key_file_error);
2097 g_propagate_error (error, key_file_error);
2104 * g_key_file_set_integer:
2105 * @key_file: a #GKeyFile
2106 * @group_name: a group name
2108 * @value: an integer value
2110 * Associates a new integer value with @key under @group_name.
2111 * If @key cannot be found then it is created.
2116 g_key_file_set_integer (GKeyFile *key_file,
2117 const gchar *group_name,
2123 g_return_if_fail (key_file != NULL);
2125 result = g_key_file_parse_integer_as_value (key_file, value);
2126 g_key_file_set_value (key_file, group_name, key, result);
2131 * g_key_file_get_integer_list:
2132 * @key_file: a #GKeyFile
2133 * @group_name: a group name
2135 * @length: the number of integers returned
2136 * @error: return location for a #GError
2138 * Returns the values associated with @key under @group_name as
2141 * If @key cannot be found then %NULL is returned and @error is set to
2142 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2143 * with @key cannot be interpreted as integers then %NULL is returned
2144 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2146 * Return value: the values associated with the key as a list of
2147 * integers, or %NULL if the key was not found or could not be parsed.
2152 g_key_file_get_integer_list (GKeyFile *key_file,
2153 const gchar *group_name,
2158 GError *key_file_error = NULL;
2163 g_return_val_if_fail (key_file != NULL, NULL);
2164 g_return_val_if_fail (group_name != NULL, NULL);
2165 g_return_val_if_fail (key != NULL, NULL);
2170 values = g_key_file_get_string_list (key_file, group_name, key,
2171 &num_ints, &key_file_error);
2174 g_propagate_error (error, key_file_error);
2179 int_values = g_new (gint, num_ints);
2181 for (i = 0; i < num_ints; i++)
2183 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2189 g_propagate_error (error, key_file_error);
2190 g_strfreev (values);
2191 g_free (int_values);
2196 g_strfreev (values);
2205 * g_key_file_set_integer_list:
2206 * @key_file: a #GKeyFile
2207 * @group_name: a group name
2209 * @list: an array of integer values
2210 * @length: number of integer values in @list
2212 * Associates a list of integer values with @key under @group_name.
2213 * If @key cannot be found then it is created.
2218 g_key_file_set_integer_list (GKeyFile *key_file,
2219 const gchar *group_name,
2227 g_return_if_fail (key_file != NULL);
2228 g_return_if_fail (list != NULL);
2230 values = g_string_sized_new (length * 16);
2231 for (i = 0; i < length; i++)
2235 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2237 g_string_append (values, value);
2238 g_string_append_c (values, ';');
2243 g_key_file_set_value (key_file, group_name, key, values->str);
2244 g_string_free (values, TRUE);
2248 * g_key_file_get_double:
2249 * @key_file: a #GKeyFile
2250 * @group_name: a group name
2252 * @error: return location for a #GError
2254 * Returns the value associated with @key under @group_name as a
2255 * double. If @group_name is %NULL, the start_group is used.
2257 * If @key cannot be found then 0.0 is returned and @error is set to
2258 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2259 * with @key cannot be interpreted as a double then 0.0 is returned
2260 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2262 * Return value: the value associated with the key as a double, or
2263 * 0.0 if the key was not found or could not be parsed.
2268 g_key_file_get_double (GKeyFile *key_file,
2269 const gchar *group_name,
2273 GError *key_file_error;
2275 gdouble double_value;
2277 g_return_val_if_fail (key_file != NULL, -1);
2278 g_return_val_if_fail (group_name != NULL, -1);
2279 g_return_val_if_fail (key != NULL, -1);
2281 key_file_error = NULL;
2283 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2287 g_propagate_error (error, key_file_error);
2291 double_value = g_key_file_parse_value_as_double (key_file, value,
2297 if (g_error_matches (key_file_error,
2299 G_KEY_FILE_ERROR_INVALID_VALUE))
2301 g_set_error (error, G_KEY_FILE_ERROR,
2302 G_KEY_FILE_ERROR_INVALID_VALUE,
2303 _("Key file contains key '%s' in group '%s' "
2304 "which has value that cannot be interpreted."), key,
2306 g_error_free (key_file_error);
2309 g_propagate_error (error, key_file_error);
2312 return double_value;
2316 * g_key_file_set_double:
2317 * @key_file: a #GKeyFile
2318 * @group_name: a group name
2320 * @value: an double value
2322 * Associates a new double value with @key under @group_name.
2323 * If @key cannot be found then it is created.
2324 * If @group_name is %NULL, the start group is used.
2329 g_key_file_set_double (GKeyFile *key_file,
2330 const gchar *group_name,
2334 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2336 g_return_if_fail (key_file != NULL);
2338 g_ascii_dtostr (result, sizeof (result), value);
2339 g_key_file_set_value (key_file, group_name, key, result);
2343 * g_key_file_get_double_list:
2344 * @key_file: a #GKeyFile
2345 * @group_name: a group name
2347 * @length: the number of doubles returned
2348 * @error: return location for a #GError
2350 * Returns the values associated with @key under @group_name as
2351 * doubles. If @group_name is %NULL, the start group is used.
2353 * If @key cannot be found then %NULL is returned and @error is set to
2354 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2355 * with @key cannot be interpreted as doubles then %NULL is returned
2356 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2358 * Return value: the values associated with the key as a list of
2359 * doubles, or %NULL if the key was not found or could not be parsed.
2364 g_key_file_get_double_list (GKeyFile *key_file,
2365 const gchar *group_name,
2370 GError *key_file_error = NULL;
2372 gdouble *double_values;
2373 gsize i, num_doubles;
2375 g_return_val_if_fail (key_file != NULL, NULL);
2376 g_return_val_if_fail (group_name != NULL, NULL);
2377 g_return_val_if_fail (key != NULL, NULL);
2382 values = g_key_file_get_string_list (key_file, group_name, key,
2383 &num_doubles, &key_file_error);
2386 g_propagate_error (error, key_file_error);
2391 double_values = g_new (gdouble, num_doubles);
2393 for (i = 0; i < num_doubles; i++)
2395 double_values[i] = g_key_file_parse_value_as_double (key_file,
2401 g_propagate_error (error, key_file_error);
2402 g_strfreev (values);
2403 g_free (double_values);
2408 g_strfreev (values);
2411 *length = num_doubles;
2413 return double_values;
2417 * g_key_file_set_double_list:
2418 * @key_file: a #GKeyFile
2419 * @group_name: a group name
2421 * @list: an array of double values
2422 * @length: number of double values in @list
2424 * Associates a list of double values with @key under
2425 * @group_name. If @key cannot be found then it is created.
2426 * If @group_name is %NULL the start group is used.
2431 g_key_file_set_double_list (GKeyFile *key_file,
2432 const gchar *group_name,
2440 g_return_if_fail (key_file != NULL);
2441 g_return_if_fail (list != NULL);
2443 values = g_string_sized_new (length * 16);
2444 for (i = 0; i < length; i++)
2446 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2448 g_ascii_dtostr( result, sizeof (result), list[i] );
2450 g_string_append (values, result);
2451 g_string_append_c (values, ';');
2454 g_key_file_set_value (key_file, group_name, key, values->str);
2455 g_string_free (values, TRUE);
2459 g_key_file_set_key_comment (GKeyFile *key_file,
2460 const gchar *group_name,
2462 const gchar *comment,
2465 GKeyFileGroup *group;
2466 GKeyFileKeyValuePair *pair;
2467 GList *key_node, *comment_node, *tmp;
2469 group = g_key_file_lookup_group (key_file, group_name);
2472 g_set_error (error, G_KEY_FILE_ERROR,
2473 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2474 _("Key file does not have group '%s'"),
2475 group_name ? group_name : "(null)");
2480 /* First find the key the comments are supposed to be
2483 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2485 if (key_node == NULL)
2487 g_set_error (error, G_KEY_FILE_ERROR,
2488 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2489 _("Key file does not have key '%s' in group '%s'"),
2494 /* Then find all the comments already associated with the
2497 tmp = key_node->next;
2500 GKeyFileKeyValuePair *pair;
2502 pair = (GKeyFileKeyValuePair *) tmp->data;
2504 if (pair->key != NULL)
2509 g_key_file_remove_key_value_pair_node (key_file, group,
2513 if (comment == NULL)
2516 /* Now we can add our new comment
2518 pair = g_slice_new (GKeyFileKeyValuePair);
2520 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2522 key_node = g_list_insert (key_node, pair, 1);
2528 g_key_file_set_group_comment (GKeyFile *key_file,
2529 const gchar *group_name,
2530 const gchar *comment,
2533 GKeyFileGroup *group;
2535 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2537 group = g_key_file_lookup_group (key_file, group_name);
2540 g_set_error (error, G_KEY_FILE_ERROR,
2541 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2542 _("Key file does not have group '%s'"),
2543 group_name ? group_name : "(null)");
2548 /* First remove any existing comment
2552 g_key_file_key_value_pair_free (group->comment);
2553 group->comment = NULL;
2556 if (comment == NULL)
2559 /* Now we can add our new comment
2561 group->comment = g_slice_new (GKeyFileKeyValuePair);
2562 group->comment->key = NULL;
2563 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2569 g_key_file_set_top_comment (GKeyFile *key_file,
2570 const gchar *comment,
2574 GKeyFileGroup *group;
2575 GKeyFileKeyValuePair *pair;
2577 /* The last group in the list should be the top (comments only)
2580 g_assert (key_file->groups != NULL);
2581 group_node = g_list_last (key_file->groups);
2582 group = (GKeyFileGroup *) group_node->data;
2583 g_assert (group->name == NULL);
2585 /* Note all keys must be comments at the top of
2586 * the file, so we can just free it all.
2588 if (group->key_value_pairs != NULL)
2590 g_list_foreach (group->key_value_pairs,
2591 (GFunc) g_key_file_key_value_pair_free,
2593 g_list_free (group->key_value_pairs);
2594 group->key_value_pairs = NULL;
2597 if (comment == NULL)
2600 pair = g_slice_new (GKeyFileKeyValuePair);
2602 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2604 group->key_value_pairs =
2605 g_list_prepend (group->key_value_pairs, pair);
2611 * g_key_file_set_comment:
2612 * @key_file: a #GKeyFile
2613 * @group_name: a group name, or %NULL
2615 * @comment: a comment
2616 * @error: return location for a #GError
2618 * Places a comment above @key from @group_name.
2619 * If @key is %NULL then @comment will be written above @group_name.
2620 * If both @key and @group_name are %NULL, then @comment will be
2621 * written above the first group in the file.
2623 * Returns: %TRUE if the comment was written, %FALSE otherwise
2628 g_key_file_set_comment (GKeyFile *key_file,
2629 const gchar *group_name,
2631 const gchar *comment,
2634 g_return_val_if_fail (key_file != NULL, FALSE);
2636 if (group_name != NULL && key != NULL)
2638 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2641 else if (group_name != NULL)
2643 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2648 if (!g_key_file_set_top_comment (key_file, comment, error))
2652 if (comment != NULL)
2653 key_file->approximate_size += strlen (comment);
2659 g_key_file_get_key_comment (GKeyFile *key_file,
2660 const gchar *group_name,
2664 GKeyFileGroup *group;
2665 GKeyFileKeyValuePair *pair;
2666 GList *key_node, *tmp;
2670 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2672 group = g_key_file_lookup_group (key_file, group_name);
2675 g_set_error (error, G_KEY_FILE_ERROR,
2676 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2677 _("Key file does not have group '%s'"),
2678 group_name ? group_name : "(null)");
2683 /* First find the key the comments are supposed to be
2686 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2688 if (key_node == NULL)
2690 g_set_error (error, G_KEY_FILE_ERROR,
2691 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2692 _("Key file does not have key '%s' in group '%s'"),
2699 /* Then find all the comments already associated with the
2700 * key and concatentate them.
2702 tmp = key_node->next;
2703 if (!key_node->next)
2706 pair = (GKeyFileKeyValuePair *) tmp->data;
2707 if (pair->key != NULL)
2712 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2714 if (pair->key != NULL)
2720 while (tmp != key_node)
2722 GKeyFileKeyValuePair *pair;
2724 pair = (GKeyFileKeyValuePair *) tmp->data;
2727 string = g_string_sized_new (512);
2729 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2730 g_string_append (string, comment);
2738 comment = string->str;
2739 g_string_free (string, FALSE);
2748 get_group_comment (GKeyFile *key_file,
2749 GKeyFileGroup *group,
2758 tmp = group->key_value_pairs;
2761 GKeyFileKeyValuePair *pair;
2763 pair = (GKeyFileKeyValuePair *) tmp->data;
2765 if (pair->key != NULL)
2771 if (tmp->next == NULL)
2779 GKeyFileKeyValuePair *pair;
2781 pair = (GKeyFileKeyValuePair *) tmp->data;
2784 string = g_string_sized_new (512);
2786 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2787 g_string_append (string, comment);
2794 return g_string_free (string, FALSE);
2800 g_key_file_get_group_comment (GKeyFile *key_file,
2801 const gchar *group_name,
2805 GKeyFileGroup *group;
2807 group = g_key_file_lookup_group (key_file, group_name);
2810 g_set_error (error, G_KEY_FILE_ERROR,
2811 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2812 _("Key file does not have group '%s'"),
2813 group_name ? group_name : "(null)");
2819 return g_strdup (group->comment->value);
2821 group_node = g_key_file_lookup_group_node (key_file, group_name);
2822 group_node = group_node->next;
2823 group = (GKeyFileGroup *)group_node->data;
2824 return get_group_comment (key_file, group, error);
2828 g_key_file_get_top_comment (GKeyFile *key_file,
2832 GKeyFileGroup *group;
2834 /* The last group in the list should be the top (comments only)
2837 g_assert (key_file->groups != NULL);
2838 group_node = g_list_last (key_file->groups);
2839 group = (GKeyFileGroup *) group_node->data;
2840 g_assert (group->name == NULL);
2842 return get_group_comment (key_file, group, error);
2846 * g_key_file_get_comment:
2847 * @key_file: a #GKeyFile
2848 * @group_name: a group name, or %NULL
2850 * @error: return location for a #GError
2852 * Retrieves a comment above @key from @group_name.
2853 * If @key is %NULL then @comment will be read from above
2854 * @group_name. If both @key and @group_name are %NULL, then
2855 * @comment will be read from above the first group in the file.
2857 * Returns: a comment that should be freed with g_free()
2862 g_key_file_get_comment (GKeyFile *key_file,
2863 const gchar *group_name,
2867 g_return_val_if_fail (key_file != NULL, NULL);
2869 if (group_name != NULL && key != NULL)
2870 return g_key_file_get_key_comment (key_file, group_name, key, error);
2871 else if (group_name != NULL)
2872 return g_key_file_get_group_comment (key_file, group_name, error);
2874 return g_key_file_get_top_comment (key_file, error);
2878 * g_key_file_remove_comment:
2879 * @key_file: a #GKeyFile
2880 * @group_name: a group name, or %NULL
2882 * @error: return location for a #GError
2884 * Removes a comment above @key from @group_name.
2885 * If @key is %NULL then @comment will be removed above @group_name.
2886 * If both @key and @group_name are %NULL, then @comment will
2887 * be removed above the first group in the file.
2889 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2895 g_key_file_remove_comment (GKeyFile *key_file,
2896 const gchar *group_name,
2900 g_return_val_if_fail (key_file != NULL, FALSE);
2902 if (group_name != NULL && key != NULL)
2903 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2904 else if (group_name != NULL)
2905 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2907 return g_key_file_set_top_comment (key_file, NULL, error);
2911 * g_key_file_has_group:
2912 * @key_file: a #GKeyFile
2913 * @group_name: a group name
2915 * Looks whether the key file has the group @group_name.
2917 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2922 g_key_file_has_group (GKeyFile *key_file,
2923 const gchar *group_name)
2925 g_return_val_if_fail (key_file != NULL, FALSE);
2926 g_return_val_if_fail (group_name != NULL, FALSE);
2928 return g_key_file_lookup_group (key_file, group_name) != NULL;
2932 * g_key_file_has_key:
2933 * @key_file: a #GKeyFile
2934 * @group_name: a group name
2936 * @error: return location for a #GError
2938 * Looks whether the key file has the key @key in the group
2941 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2947 g_key_file_has_key (GKeyFile *key_file,
2948 const gchar *group_name,
2952 GKeyFileKeyValuePair *pair;
2953 GKeyFileGroup *group;
2955 g_return_val_if_fail (key_file != NULL, FALSE);
2956 g_return_val_if_fail (group_name != NULL, FALSE);
2957 g_return_val_if_fail (key != NULL, FALSE);
2959 group = g_key_file_lookup_group (key_file, group_name);
2963 g_set_error (error, G_KEY_FILE_ERROR,
2964 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2965 _("Key file does not have group '%s'"),
2966 group_name ? group_name : "(null)");
2971 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2973 return pair != NULL;
2977 g_key_file_add_group (GKeyFile *key_file,
2978 const gchar *group_name)
2980 GKeyFileGroup *group;
2982 g_return_if_fail (key_file != NULL);
2983 g_return_if_fail (g_key_file_is_group_name (group_name));
2985 group = g_key_file_lookup_group (key_file, group_name);
2988 key_file->current_group = group;
2992 group = g_slice_new0 (GKeyFileGroup);
2993 group->name = g_strdup (group_name);
2994 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2995 key_file->groups = g_list_prepend (key_file->groups, group);
2996 key_file->approximate_size += strlen (group_name) + 3;
2997 key_file->current_group = group;
2999 if (key_file->start_group == NULL)
3000 key_file->start_group = group;
3002 g_hash_table_insert (key_file->group_hash, group->name, group);
3006 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3011 g_free (pair->value);
3012 g_slice_free (GKeyFileKeyValuePair, pair);
3016 /* Be careful not to call this function on a node with data in the
3017 * lookup map without removing it from the lookup map, first.
3019 * Some current cases where this warning is not a concern are
3021 * - the node being removed is a comment node
3022 * - the entire lookup map is getting destroyed soon after
3026 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3027 GKeyFileGroup *group,
3031 GKeyFileKeyValuePair *pair;
3033 pair = (GKeyFileKeyValuePair *) pair_node->data;
3035 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3037 if (pair->key != NULL)
3038 key_file->approximate_size -= strlen (pair->key) + 1;
3040 g_assert (pair->value != NULL);
3041 key_file->approximate_size -= strlen (pair->value);
3043 g_key_file_key_value_pair_free (pair);
3045 g_list_free_1 (pair_node);
3049 g_key_file_remove_group_node (GKeyFile *key_file,
3052 GKeyFileGroup *group;
3055 group = (GKeyFileGroup *) group_node->data;
3058 g_hash_table_remove (key_file->group_hash, group->name);
3060 /* If the current group gets deleted make the current group the last
3063 if (key_file->current_group == group)
3065 /* groups should always contain at least the top comment group,
3066 * unless g_key_file_clear has been called
3068 if (key_file->groups)
3069 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3071 key_file->current_group = NULL;
3074 /* If the start group gets deleted make the start group the first
3077 if (key_file->start_group == group)
3079 tmp = g_list_last (key_file->groups);
3082 if (tmp != group_node &&
3083 ((GKeyFileGroup *) tmp->data)->name != NULL)
3090 key_file->start_group = (GKeyFileGroup *) tmp->data;
3092 key_file->start_group = NULL;
3095 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3097 if (group->name != NULL)
3098 key_file->approximate_size -= strlen (group->name) + 3;
3100 tmp = group->key_value_pairs;
3107 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3110 g_assert (group->key_value_pairs == NULL);
3112 if (group->lookup_map)
3114 g_hash_table_destroy (group->lookup_map);
3115 group->lookup_map = NULL;
3118 g_free ((gchar *) group->name);
3119 g_slice_free (GKeyFileGroup, group);
3120 g_list_free_1 (group_node);
3124 * g_key_file_remove_group:
3125 * @key_file: a #GKeyFile
3126 * @group_name: a group name
3127 * @error: return location for a #GError or %NULL
3129 * Removes the specified group, @group_name,
3130 * from the key file.
3132 * Returns: %TRUE if the group was removed, %FALSE otherwise
3137 g_key_file_remove_group (GKeyFile *key_file,
3138 const gchar *group_name,
3143 g_return_val_if_fail (key_file != NULL, FALSE);
3144 g_return_val_if_fail (group_name != NULL, FALSE);
3146 group_node = g_key_file_lookup_group_node (key_file, group_name);
3150 g_set_error (error, G_KEY_FILE_ERROR,
3151 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3152 _("Key file does not have group '%s'"),
3157 g_key_file_remove_group_node (key_file, group_node);
3163 g_key_file_add_key (GKeyFile *key_file,
3164 GKeyFileGroup *group,
3168 GKeyFileKeyValuePair *pair;
3170 pair = g_slice_new (GKeyFileKeyValuePair);
3171 pair->key = g_strdup (key);
3172 pair->value = g_strdup (value);
3174 g_hash_table_replace (group->lookup_map, pair->key, pair);
3175 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3176 group->has_trailing_blank_line = FALSE;
3177 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3181 * g_key_file_remove_key:
3182 * @key_file: a #GKeyFile
3183 * @group_name: a group name
3184 * @key: a key name to remove
3185 * @error: return location for a #GError or %NULL
3187 * Removes @key in @group_name from the key file.
3189 * Returns: %TRUE if the key was removed, %FALSE otherwise
3194 g_key_file_remove_key (GKeyFile *key_file,
3195 const gchar *group_name,
3199 GKeyFileGroup *group;
3200 GKeyFileKeyValuePair *pair;
3202 g_return_val_if_fail (key_file != NULL, FALSE);
3203 g_return_val_if_fail (group_name != NULL, FALSE);
3204 g_return_val_if_fail (key != NULL, FALSE);
3208 group = g_key_file_lookup_group (key_file, group_name);
3211 g_set_error (error, G_KEY_FILE_ERROR,
3212 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3213 _("Key file does not have group '%s'"),
3214 group_name ? group_name : "(null)");
3218 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3222 g_set_error (error, G_KEY_FILE_ERROR,
3223 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3224 _("Key file does not have key '%s' in group '%s'"),
3229 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3231 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3232 g_hash_table_remove (group->lookup_map, pair->key);
3233 g_key_file_key_value_pair_free (pair);
3239 g_key_file_lookup_group_node (GKeyFile *key_file,
3240 const gchar *group_name)
3242 GKeyFileGroup *group;
3245 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3247 group = (GKeyFileGroup *) tmp->data;
3249 if (group && group->name && strcmp (group->name, group_name) == 0)
3256 static GKeyFileGroup *
3257 g_key_file_lookup_group (GKeyFile *key_file,
3258 const gchar *group_name)
3260 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3264 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3265 GKeyFileGroup *group,
3270 for (key_node = group->key_value_pairs;
3272 key_node = key_node->next)
3274 GKeyFileKeyValuePair *pair;
3276 pair = (GKeyFileKeyValuePair *) key_node->data;
3278 if (pair->key && strcmp (pair->key, key) == 0)
3285 static GKeyFileKeyValuePair *
3286 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3287 GKeyFileGroup *group,
3290 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3293 /* Lines starting with # or consisting entirely of whitespace are merely
3294 * recorded, not parsed. This function assumes all leading whitespace
3295 * has been stripped.
3298 g_key_file_line_is_comment (const gchar *line)
3300 return (*line == '#' || *line == '\0' || *line == '\n');
3304 g_key_file_is_group_name (const gchar *name)
3311 p = q = (gchar *) name;
3312 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3313 q = g_utf8_find_next_char (q, NULL);
3315 if (*q != '\0' || q == p)
3322 g_key_file_is_key_name (const gchar *name)
3329 p = q = (gchar *) name;
3330 /* We accept a little more than the desktop entry spec says,
3331 * since gnome-vfs uses mime-types as keys in its cache.
3333 while (*q && *q != '=' && *q != '[' && *q != ']')
3334 q = g_utf8_find_next_char (q, NULL);
3336 /* No empty keys, please */
3340 /* We accept spaces in the middle of keys to not break
3341 * existing apps, but we don't tolerate initial or final
3342 * spaces, which would lead to silent corruption when
3343 * rereading the file.
3345 if (*p == ' ' || q[-1] == ' ')
3351 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3352 q = g_utf8_find_next_char (q, NULL);
3366 /* A group in a key file is made up of a starting '[' followed by one
3367 * or more letters making up the group name followed by ']'.
3370 g_key_file_line_is_group (const gchar *line)
3380 while (*p && *p != ']')
3381 p = g_utf8_find_next_char (p, NULL);
3386 /* silently accept whitespace after the ] */
3387 p = g_utf8_find_next_char (p, NULL);
3388 while (*p == ' ' || *p == '\t')
3389 p = g_utf8_find_next_char (p, NULL);
3398 g_key_file_line_is_key_value_pair (const gchar *line)
3402 p = (gchar *) g_utf8_strchr (line, -1, '=');
3407 /* Key must be non-empty
3416 g_key_file_parse_value_as_string (GKeyFile *key_file,
3421 gchar *string_value, *p, *q0, *q;
3423 string_value = g_new (gchar, strlen (value) + 1);
3425 p = (gchar *) value;
3426 q0 = q = string_value;
3456 g_set_error (error, G_KEY_FILE_ERROR,
3457 G_KEY_FILE_ERROR_INVALID_VALUE,
3458 _("Key file contains escape character "
3463 if (pieces && *p == key_file->list_separator)
3464 *q = key_file->list_separator;
3478 g_set_error (error, G_KEY_FILE_ERROR,
3479 G_KEY_FILE_ERROR_INVALID_VALUE,
3480 _("Key file contains invalid escape "
3481 "sequence '%s'"), sequence);
3490 if (pieces && (*p == key_file->list_separator))
3492 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3508 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3509 *pieces = g_slist_reverse (*pieces);
3512 return string_value;
3516 g_key_file_parse_string_as_value (GKeyFile *key_file,
3517 const gchar *string,
3518 gboolean escape_separator)
3520 gchar *value, *p, *q;
3522 gboolean parsing_leading_space;
3524 length = strlen (string) + 1;
3526 /* Worst case would be that every character needs to be escaped.
3527 * In other words every character turns to two characters
3529 value = g_new (gchar, 2 * length);
3531 p = (gchar *) string;
3533 parsing_leading_space = TRUE;
3534 while (p < (string + length - 1))
3536 gchar escaped_character[3] = { '\\', 0, 0 };
3541 if (parsing_leading_space)
3543 escaped_character[1] = 's';
3544 strcpy (q, escaped_character);
3554 if (parsing_leading_space)
3556 escaped_character[1] = 't';
3557 strcpy (q, escaped_character);
3567 escaped_character[1] = 'n';
3568 strcpy (q, escaped_character);
3572 escaped_character[1] = 'r';
3573 strcpy (q, escaped_character);
3577 escaped_character[1] = '\\';
3578 strcpy (q, escaped_character);
3580 parsing_leading_space = FALSE;
3583 if (escape_separator && *p == key_file->list_separator)
3585 escaped_character[1] = key_file->list_separator;
3586 strcpy (q, escaped_character);
3588 parsing_leading_space = TRUE;
3594 parsing_leading_space = FALSE;
3606 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3610 gchar *end_of_valid_int;
3615 long_value = strtol (value, &end_of_valid_int, 10);
3617 if (*value == '\0' || *end_of_valid_int != '\0')
3619 gchar *value_utf8 = _g_utf8_make_valid (value);
3620 g_set_error (error, G_KEY_FILE_ERROR,
3621 G_KEY_FILE_ERROR_INVALID_VALUE,
3622 _("Value '%s' cannot be interpreted "
3623 "as a number."), value_utf8);
3624 g_free (value_utf8);
3629 int_value = long_value;
3630 if (int_value != long_value || errno == ERANGE)
3632 gchar *value_utf8 = _g_utf8_make_valid (value);
3635 G_KEY_FILE_ERROR_INVALID_VALUE,
3636 _("Integer value '%s' out of range"),
3638 g_free (value_utf8);
3647 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3651 return g_strdup_printf ("%d", value);
3655 g_key_file_parse_value_as_double (GKeyFile *key_file,
3659 gchar *end_of_valid_d;
3660 gdouble double_value = 0;
3662 double_value = g_ascii_strtod (value, &end_of_valid_d);
3664 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3666 gchar *value_utf8 = _g_utf8_make_valid (value);
3667 g_set_error (error, G_KEY_FILE_ERROR,
3668 G_KEY_FILE_ERROR_INVALID_VALUE,
3669 _("Value '%s' cannot be interpreted "
3670 "as a float number."),
3672 g_free (value_utf8);
3675 return double_value;
3679 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3685 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3687 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3690 value_utf8 = _g_utf8_make_valid (value);
3691 g_set_error (error, G_KEY_FILE_ERROR,
3692 G_KEY_FILE_ERROR_INVALID_VALUE,
3693 _("Value '%s' cannot be interpreted "
3694 "as a boolean."), value_utf8);
3695 g_free (value_utf8);
3701 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3705 return g_strdup ("true");
3707 return g_strdup ("false");
3711 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3718 string = g_string_sized_new (512);
3720 lines = g_strsplit (value, "\n", 0);
3722 for (i = 0; lines[i] != NULL; i++)
3724 if (lines[i][0] != '#')
3725 g_string_append_printf (string, "%s\n", lines[i]);
3727 g_string_append_printf (string, "%s\n", lines[i] + 1);
3731 return g_string_free (string, FALSE);
3735 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3736 const gchar *comment)
3742 string = g_string_sized_new (512);
3744 lines = g_strsplit (comment, "\n", 0);
3746 for (i = 0; lines[i] != NULL; i++)
3747 g_string_append_printf (string, "#%s%s", lines[i],
3748 lines[i + 1] == NULL? "" : "\n");
3751 return g_string_free (string, FALSE);
3754 #define __G_KEY_FILE_C__
3755 #include "galiasdef.c"