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;
71 GKeyFileGroup *start_group;
72 GKeyFileGroup *current_group;
74 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
76 /* Used for sizing the output buffer during serialization
78 gsize approximate_size;
85 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
89 const gchar *name; /* NULL for above first group (which will be comments) */
91 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
92 gboolean has_trailing_blank_line;
94 GList *key_value_pairs;
96 /* Used in parallel with key_value_pairs for
97 * increased lookup performance
99 GHashTable *lookup_map;
102 struct _GKeyFileKeyValuePair
104 gchar *key; /* NULL for comments */
108 static gint find_file_in_data_dirs (const gchar *file,
109 const gchar **data_dirs,
112 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
116 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
117 const gchar *group_name);
118 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
119 const gchar *group_name);
121 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
122 GKeyFileGroup *group,
124 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
125 GKeyFileGroup *group,
128 static void g_key_file_remove_group_node (GKeyFile *key_file,
130 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
131 GKeyFileGroup *group,
134 static void g_key_file_add_key (GKeyFile *key_file,
135 GKeyFileGroup *group,
138 static void g_key_file_add_group (GKeyFile *key_file,
139 const gchar *group_name);
140 static gboolean g_key_file_is_group_name (const gchar *name);
141 static gboolean g_key_file_is_key_name (const gchar *name);
142 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
143 static gboolean g_key_file_line_is_comment (const gchar *line);
144 static gboolean g_key_file_line_is_group (const gchar *line);
145 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
146 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
150 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
152 gboolean escape_separator);
153 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
156 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
158 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
161 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
164 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
166 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
168 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
169 const gchar *comment);
170 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
174 static void g_key_file_parse_comment (GKeyFile *key_file,
178 static void g_key_file_parse_group (GKeyFile *key_file,
182 static gchar *key_get_locale (const gchar *key);
183 static void g_key_file_parse_data (GKeyFile *key_file,
187 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
192 g_key_file_error_quark (void)
194 return g_quark_from_static_string ("g-key-file-error-quark");
198 g_key_file_init (GKeyFile *key_file)
200 key_file->current_group = g_slice_new0 (GKeyFileGroup);
201 key_file->groups = g_list_prepend (NULL, key_file->current_group);
202 key_file->start_group = NULL;
203 key_file->parse_buffer = g_string_sized_new (128);
204 key_file->approximate_size = 0;
205 key_file->list_separator = ';';
210 g_key_file_clear (GKeyFile *key_file)
212 GList *tmp, *group_node;
214 if (key_file->parse_buffer)
215 g_string_free (key_file->parse_buffer, TRUE);
217 tmp = key_file->groups;
222 g_key_file_remove_group_node (key_file, group_node);
225 g_assert (key_file->groups == NULL);
232 * Creates a new empty #GKeyFile object. Use
233 * g_key_file_load_from_file(), g_key_file_load_from_data(),
234 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
235 * read an existing key file.
237 * Return value: an empty #GKeyFile.
242 g_key_file_new (void)
246 key_file = g_slice_new0 (GKeyFile);
247 g_key_file_init (key_file);
253 * g_key_file_set_list_separator:
254 * @key_file: a #GKeyFile
255 * @separator: the separator
257 * Sets the character which is used to separate
258 * values in lists. Typically ';' or ',' are used
259 * as separators. The default list separator is ';'.
264 g_key_file_set_list_separator (GKeyFile *key_file,
267 g_return_if_fail (key_file != NULL);
269 key_file->list_separator = separator;
273 /* Iterates through all the directories in *dirs trying to
274 * open file. When it successfully locates and opens a file it
275 * returns the file descriptor to the open file. It also
276 * outputs the absolute path of the file in output_file.
279 find_file_in_data_dirs (const gchar *file,
284 const gchar **data_dirs, *data_dir;
296 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
298 gchar *candidate_file, *sub_dir;
300 candidate_file = (gchar *) file;
301 sub_dir = g_strdup ("");
302 while (candidate_file != NULL && fd < 0)
306 path = g_build_filename (data_dir, sub_dir,
307 candidate_file, NULL);
309 fd = g_open (path, O_RDONLY, 0);
317 candidate_file = strchr (candidate_file, '-');
319 if (candidate_file == NULL)
325 sub_dir = g_strndup (file, candidate_file - file - 1);
327 for (p = sub_dir; *p != '\0'; p++)
330 *p = G_DIR_SEPARATOR;
339 g_set_error (error, G_KEY_FILE_ERROR,
340 G_KEY_FILE_ERROR_NOT_FOUND,
341 _("Valid key file could not be "
342 "found in search dirs"));
345 if (output_file != NULL && fd > 0)
346 *output_file = g_strdup (path);
354 g_key_file_load_from_fd (GKeyFile *key_file,
359 GError *key_file_error = NULL;
361 struct stat stat_buf;
362 gchar read_buf[4096];
364 if (fstat (fd, &stat_buf) < 0)
366 g_set_error (error, G_FILE_ERROR,
367 g_file_error_from_errno (errno),
368 "%s", g_strerror (errno));
372 if (!S_ISREG (stat_buf.st_mode))
374 g_set_error (error, G_KEY_FILE_ERROR,
375 G_KEY_FILE_ERROR_PARSE,
376 _("Not a regular file"));
380 if (stat_buf.st_size == 0)
382 g_set_error (error, G_KEY_FILE_ERROR,
383 G_KEY_FILE_ERROR_PARSE,
388 if (key_file->approximate_size > 0)
390 g_key_file_clear (key_file);
391 g_key_file_init (key_file);
393 key_file->flags = flags;
398 bytes_read = read (fd, read_buf, 4096);
400 if (bytes_read == 0) /* End of File */
405 if (errno == EINTR || errno == EAGAIN)
408 g_set_error (error, G_FILE_ERROR,
409 g_file_error_from_errno (errno),
410 "%s", g_strerror (errno));
414 g_key_file_parse_data (key_file,
415 read_buf, bytes_read,
418 while (!key_file_error);
422 g_propagate_error (error, key_file_error);
426 g_key_file_flush_parse_buffer (key_file, &key_file_error);
430 g_propagate_error (error, key_file_error);
438 * g_key_file_load_from_file:
439 * @key_file: an empty #GKeyFile struct
440 * @file: the path of a filename to load, in the GLib filename encoding
441 * @flags: flags from #GKeyFileFlags
442 * @error: return location for a #GError, or %NULL
444 * Loads a key file into an empty #GKeyFile structure.
445 * If the file could not be loaded then %error is set to
446 * either a #GFileError or #GKeyFileError.
448 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
453 g_key_file_load_from_file (GKeyFile *key_file,
458 GError *key_file_error = NULL;
461 g_return_val_if_fail (key_file != NULL, FALSE);
462 g_return_val_if_fail (file != NULL, FALSE);
464 fd = g_open (file, O_RDONLY, 0);
468 g_set_error (error, G_FILE_ERROR,
469 g_file_error_from_errno (errno),
470 "%s", g_strerror (errno));
474 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
479 g_propagate_error (error, key_file_error);
487 * g_key_file_load_from_data:
488 * @key_file: an empty #GKeyFile struct
489 * @data: key file loaded in memory
490 * @length: the length of @data in bytes
491 * @flags: flags from #GKeyFileFlags
492 * @error: return location for a #GError, or %NULL
494 * Loads a key file from memory into an empty #GKeyFile structure.
495 * If the object cannot be created then %error is set to a #GKeyFileError.
497 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
502 g_key_file_load_from_data (GKeyFile *key_file,
508 GError *key_file_error = NULL;
510 g_return_val_if_fail (key_file != NULL, FALSE);
511 g_return_val_if_fail (data != NULL, FALSE);
512 g_return_val_if_fail (length != 0, FALSE);
514 if (length == (gsize)-1)
515 length = strlen (data);
517 if (key_file->approximate_size > 0)
519 g_key_file_clear (key_file);
520 g_key_file_init (key_file);
522 key_file->flags = flags;
524 g_key_file_parse_data (key_file, data, length, &key_file_error);
528 g_propagate_error (error, key_file_error);
532 g_key_file_flush_parse_buffer (key_file, &key_file_error);
536 g_propagate_error (error, key_file_error);
544 * g_key_file_load_from_dirs:
545 * @key_file: an empty #GKeyFile struct
546 * @file: a relative path to a filename to open and parse
547 * @search_dirs: %NULL-terminated array of directories to search
548 * @full_path: return location for a string containing the full path
549 * of the file, or %NULL
550 * @flags: flags from #GKeyFileFlags
551 * @error: return location for a #GError, or %NULL
553 * This function looks for a key file named @file in the paths
554 * specified in @search_dirs, loads the file into @key_file and
555 * returns the file's full path in @full_path. If the file could not
556 * be loaded then an %error is set to either a #GFileError or
559 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
564 g_key_file_load_from_dirs (GKeyFile *key_file,
566 const gchar **search_dirs,
571 GError *key_file_error = NULL;
572 const gchar **data_dirs;
577 g_return_val_if_fail (key_file != NULL, FALSE);
578 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
579 g_return_val_if_fail (search_dirs != NULL, FALSE);
582 data_dirs = search_dirs;
584 while (*data_dirs != NULL && !found_file)
586 g_free (output_path);
588 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
594 g_propagate_error (error, key_file_error);
598 found_file = g_key_file_load_from_fd (key_file, fd, flags,
604 g_propagate_error (error, key_file_error);
609 if (found_file && full_path)
610 *full_path = output_path;
612 g_free (output_path);
618 * g_key_file_load_from_data_dirs:
619 * @key_file: an empty #GKeyFile struct
620 * @file: a relative path to a filename to open and parse
621 * @full_path: return location for a string containing the full path
622 * of the file, or %NULL
623 * @flags: flags from #GKeyFileFlags
624 * @error: return location for a #GError, or %NULL
626 * This function looks for a key file named @file in the paths
627 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
628 * loads the file into @key_file and returns the file's full path in
629 * @full_path. If the file could not be loaded then an %error is
630 * set to either a #GFileError or #GKeyFileError.
632 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
636 g_key_file_load_from_data_dirs (GKeyFile *key_file,
642 gchar **all_data_dirs;
643 const gchar * user_data_dir;
644 const gchar * const * system_data_dirs;
648 g_return_val_if_fail (key_file != NULL, FALSE);
649 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
651 user_data_dir = g_get_user_data_dir ();
652 system_data_dirs = g_get_system_data_dirs ();
653 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
656 all_data_dirs[i++] = g_strdup (user_data_dir);
659 while (system_data_dirs[j] != NULL)
660 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
661 all_data_dirs[i] = NULL;
663 found_file = g_key_file_load_from_dirs (key_file,
665 (const gchar **)all_data_dirs,
670 g_strfreev (all_data_dirs);
677 * @key_file: a #GKeyFile
684 g_key_file_free (GKeyFile *key_file)
686 g_return_if_fail (key_file != NULL);
688 g_key_file_clear (key_file);
689 g_slice_free (GKeyFile, key_file);
692 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
693 * true for locales that match those in g_get_language_names().
696 g_key_file_locale_is_interesting (GKeyFile *key_file,
699 const gchar * const * current_locales;
702 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
705 current_locales = g_get_language_names ();
707 for (i = 0; current_locales[i] != NULL; i++)
709 if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
717 g_key_file_parse_line (GKeyFile *key_file,
722 GError *parse_error = NULL;
725 g_return_if_fail (key_file != NULL);
726 g_return_if_fail (line != NULL);
728 line_start = (gchar *) line;
729 while (g_ascii_isspace (*line_start))
732 if (g_key_file_line_is_comment (line_start))
733 g_key_file_parse_comment (key_file, line, length, &parse_error);
734 else if (g_key_file_line_is_group (line_start))
735 g_key_file_parse_group (key_file, line_start,
736 length - (line_start - line),
738 else if (g_key_file_line_is_key_value_pair (line_start))
739 g_key_file_parse_key_value_pair (key_file, line_start,
740 length - (line_start - line),
744 gchar *line_utf8 = _g_utf8_make_valid (line);
745 g_set_error (error, G_KEY_FILE_ERROR,
746 G_KEY_FILE_ERROR_PARSE,
747 _("Key file contains line '%s' which is not "
748 "a key-value pair, group, or comment"),
756 g_propagate_error (error, parse_error);
760 g_key_file_parse_comment (GKeyFile *key_file,
765 GKeyFileKeyValuePair *pair;
767 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
770 g_assert (key_file->current_group != NULL);
772 pair = g_slice_new (GKeyFileKeyValuePair);
774 pair->value = g_strndup (line, length);
776 key_file->current_group->key_value_pairs =
777 g_list_prepend (key_file->current_group->key_value_pairs, pair);
779 if (length == 0 || line[0] != '#')
780 key_file->current_group->has_trailing_blank_line = TRUE;
784 g_key_file_parse_group (GKeyFile *key_file,
790 const gchar *group_name_start, *group_name_end;
792 /* advance past opening '['
794 group_name_start = line + 1;
795 group_name_end = line + length - 1;
797 while (*group_name_end != ']')
800 group_name = g_strndup (group_name_start,
801 group_name_end - group_name_start);
803 if (!g_key_file_is_group_name (group_name))
805 g_set_error (error, G_KEY_FILE_ERROR,
806 G_KEY_FILE_ERROR_PARSE,
807 _("Invalid group name: %s"), group_name);
812 g_key_file_add_group (key_file, group_name);
817 g_key_file_parse_key_value_pair (GKeyFile *key_file,
822 gchar *key, *value, *key_end, *value_start, *locale;
823 gsize key_len, value_len;
825 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
827 g_set_error (error, G_KEY_FILE_ERROR,
828 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
829 _("Key file does not start with a group"));
833 key_end = value_start = strchr (line, '=');
835 g_assert (key_end != NULL);
840 /* Pull the key name from the line (chomping trailing whitespace)
842 while (g_ascii_isspace (*key_end))
845 key_len = key_end - line + 2;
847 g_assert (key_len <= length);
849 key = g_strndup (line, key_len - 1);
851 if (!g_key_file_is_key_name (key))
853 g_set_error (error, G_KEY_FILE_ERROR,
854 G_KEY_FILE_ERROR_PARSE,
855 _("Invalid key name: %s"), key);
860 /* Pull the value from the line (chugging leading whitespace)
862 while (g_ascii_isspace (*value_start))
865 value_len = line + length - value_start + 1;
867 value = g_strndup (value_start, value_len);
869 g_assert (key_file->start_group != NULL);
871 if (key_file->current_group
872 && key_file->current_group->name
873 && strcmp (key_file->start_group->name,
874 key_file->current_group->name) == 0
875 && strcmp (key, "Encoding") == 0)
877 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
879 gchar *value_utf8 = _g_utf8_make_valid (value);
880 g_set_error (error, G_KEY_FILE_ERROR,
881 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
882 _("Key file contains unsupported "
883 "encoding '%s'"), value_utf8);
892 /* Is this key a translation? If so, is it one that we care about?
894 locale = key_get_locale (key);
896 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
897 g_key_file_add_key (key_file, key_file->current_group, key, value);
905 key_get_locale (const gchar *key)
909 locale = g_strrstr (key, "[");
911 if (locale && strlen (locale) <= 2)
915 locale = g_strndup (locale + 1, strlen (locale) - 2);
921 g_key_file_parse_data (GKeyFile *key_file,
929 g_return_if_fail (key_file != NULL);
930 g_return_if_fail (data != NULL);
934 for (i = 0; i < length; i++)
938 if (i > 0 && data[i - 1] == '\r')
939 g_string_erase (key_file->parse_buffer,
940 key_file->parse_buffer->len - 1,
943 /* When a newline is encountered flush the parse buffer so that the
944 * line can be parsed. Note that completely blank lines won't show
945 * up in the parse buffer, so they get parsed directly.
947 if (key_file->parse_buffer->len > 0)
948 g_key_file_flush_parse_buffer (key_file, &parse_error);
950 g_key_file_parse_comment (key_file, "", 1, &parse_error);
954 g_propagate_error (error, parse_error);
959 g_string_append_c (key_file->parse_buffer, data[i]);
962 key_file->approximate_size += length;
966 g_key_file_flush_parse_buffer (GKeyFile *key_file,
969 GError *file_error = NULL;
971 g_return_if_fail (key_file != NULL);
975 if (key_file->parse_buffer->len > 0)
977 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
978 key_file->parse_buffer->len,
980 g_string_erase (key_file->parse_buffer, 0, -1);
984 g_propagate_error (error, file_error);
991 * g_key_file_to_data:
992 * @key_file: a #GKeyFile
993 * @length: return location for the length of the
994 * returned string, or %NULL
995 * @error: return location for a #GError, or %NULL
997 * This function outputs @key_file as a string.
999 * Return value: a newly allocated string holding
1000 * the contents of the #GKeyFile
1005 g_key_file_to_data (GKeyFile *key_file,
1009 GString *data_string;
1010 GList *group_node, *key_file_node;
1011 gboolean has_blank_line = TRUE;
1013 g_return_val_if_fail (key_file != NULL, NULL);
1015 data_string = g_string_sized_new (2 * key_file->approximate_size);
1017 for (group_node = g_list_last (key_file->groups);
1019 group_node = group_node->prev)
1021 GKeyFileGroup *group;
1023 group = (GKeyFileGroup *) group_node->data;
1025 /* separate groups by at least an empty line */
1026 if (!has_blank_line)
1027 g_string_append_c (data_string, '\n');
1028 has_blank_line = group->has_trailing_blank_line;
1030 if (group->comment != NULL)
1031 g_string_append_printf (data_string, "%s\n", group->comment->value);
1033 if (group->name != NULL)
1034 g_string_append_printf (data_string, "[%s]\n", group->name);
1036 for (key_file_node = g_list_last (group->key_value_pairs);
1037 key_file_node != NULL;
1038 key_file_node = key_file_node->prev)
1040 GKeyFileKeyValuePair *pair;
1042 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1044 if (pair->key != NULL)
1045 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1047 g_string_append_printf (data_string, "%s\n", pair->value);
1052 *length = data_string->len;
1054 return g_string_free (data_string, FALSE);
1058 * g_key_file_get_keys:
1059 * @key_file: a #GKeyFile
1060 * @group_name: a group name
1061 * @length: return location for the number of keys returned, or %NULL
1062 * @error: return location for a #GError, or %NULL
1064 * Returns all keys for the group name @group_name. The array of
1065 * returned keys will be %NULL-terminated, so @length may
1066 * optionally be %NULL. In the event that the @group_name cannot
1067 * be found, %NULL is returned and @error is set to
1068 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1070 * Return value: a newly-allocated %NULL-terminated array of strings.
1071 * Use g_strfreev() to free it.
1076 g_key_file_get_keys (GKeyFile *key_file,
1077 const gchar *group_name,
1081 GKeyFileGroup *group;
1086 g_return_val_if_fail (key_file != NULL, NULL);
1087 g_return_val_if_fail (group_name != NULL, NULL);
1089 group = g_key_file_lookup_group (key_file, group_name);
1093 g_set_error (error, G_KEY_FILE_ERROR,
1094 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1095 _("Key file does not have group '%s'"),
1096 group_name ? group_name : "(null)");
1101 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1103 GKeyFileKeyValuePair *pair;
1105 pair = (GKeyFileKeyValuePair *) tmp->data;
1111 keys = g_new (gchar *, num_keys + 1);
1114 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1116 GKeyFileKeyValuePair *pair;
1118 pair = (GKeyFileKeyValuePair *) tmp->data;
1122 keys[i] = g_strdup (pair->key);
1127 keys[num_keys] = NULL;
1136 * g_key_file_get_start_group:
1137 * @key_file: a #GKeyFile
1139 * Returns the name of the start group of the file.
1141 * Return value: The start group of the key file.
1146 g_key_file_get_start_group (GKeyFile *key_file)
1148 g_return_val_if_fail (key_file != NULL, NULL);
1150 if (key_file->start_group)
1151 return g_strdup (key_file->start_group->name);
1157 * g_key_file_get_groups:
1158 * @key_file: a #GKeyFile
1159 * @length: return location for the number of returned groups, or %NULL
1161 * Returns all groups in the key file loaded with @key_file.
1162 * The array of returned groups will be %NULL-terminated, so
1163 * @length may optionally be %NULL.
1165 * Return value: a newly-allocated %NULL-terminated array of strings.
1166 * Use g_strfreev() to free it.
1170 g_key_file_get_groups (GKeyFile *key_file,
1175 gsize i, num_groups;
1177 g_return_val_if_fail (key_file != NULL, NULL);
1179 num_groups = g_list_length (key_file->groups);
1181 g_assert (num_groups > 0);
1183 /* Only need num_groups instead of num_groups + 1
1184 * because the first group of the file (last in the
1185 * list) is always the comment group at the top,
1188 groups = g_new (gchar *, num_groups);
1190 group_node = g_list_last (key_file->groups);
1192 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1195 for (group_node = group_node->prev;
1197 group_node = group_node->prev)
1199 GKeyFileGroup *group;
1201 group = (GKeyFileGroup *) group_node->data;
1203 g_assert (group->name != NULL);
1205 groups[i++] = g_strdup (group->name);
1216 * g_key_file_get_value:
1217 * @key_file: a #GKeyFile
1218 * @group_name: a group name
1220 * @error: return location for a #GError, or %NULL
1222 * Returns the value associated with @key under @group_name.
1224 * In the event the key cannot be found, %NULL is returned and
1225 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1226 * event that the @group_name cannot be found, %NULL is returned
1227 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1229 * Return value: a newly allocated string or %NULL if the specified
1230 * key cannot be found.
1235 g_key_file_get_value (GKeyFile *key_file,
1236 const gchar *group_name,
1240 GKeyFileGroup *group;
1241 GKeyFileKeyValuePair *pair;
1242 gchar *value = NULL;
1244 g_return_val_if_fail (key_file != NULL, NULL);
1245 g_return_val_if_fail (group_name != NULL, NULL);
1246 g_return_val_if_fail (key != NULL, NULL);
1248 group = g_key_file_lookup_group (key_file, group_name);
1252 g_set_error (error, G_KEY_FILE_ERROR,
1253 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1254 _("Key file does not have group '%s'"),
1255 group_name ? group_name : "(null)");
1259 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1262 value = g_strdup (pair->value);
1264 g_set_error (error, G_KEY_FILE_ERROR,
1265 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1266 _("Key file does not have key '%s'"), key);
1272 * g_key_file_set_value:
1273 * @key_file: a #GKeyFile
1274 * @group_name: a group name
1278 * Associates a new value with @key under @group_name.
1279 * If @key cannot be found then it is created.
1280 * If @group_name cannot be found then it is created.
1285 g_key_file_set_value (GKeyFile *key_file,
1286 const gchar *group_name,
1290 GKeyFileGroup *group;
1291 GKeyFileKeyValuePair *pair;
1293 g_return_if_fail (key_file != NULL);
1294 g_return_if_fail (g_key_file_is_group_name (group_name));
1295 g_return_if_fail (g_key_file_is_key_name (key));
1296 g_return_if_fail (value != NULL);
1298 group = g_key_file_lookup_group (key_file, group_name);
1302 g_key_file_add_group (key_file, group_name);
1303 group = (GKeyFileGroup *) key_file->groups->data;
1305 g_key_file_add_key (key_file, group, key, value);
1309 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1312 g_key_file_add_key (key_file, group, key, value);
1315 g_free (pair->value);
1316 pair->value = g_strdup (value);
1322 * g_key_file_get_string:
1323 * @key_file: a #GKeyFile
1324 * @group_name: a group name
1326 * @error: return location for a #GError, or %NULL
1328 * Returns the value associated with @key under @group_name.
1330 * In the event the key cannot be found, %NULL is returned and
1331 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1332 * event that the @group_name cannot be found, %NULL is returned
1333 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1335 * Return value: a newly allocated string or %NULL if the specified
1336 * key cannot be found.
1341 g_key_file_get_string (GKeyFile *key_file,
1342 const gchar *group_name,
1346 gchar *value, *string_value;
1347 GError *key_file_error;
1349 g_return_val_if_fail (key_file != NULL, NULL);
1350 g_return_val_if_fail (group_name != NULL, NULL);
1351 g_return_val_if_fail (key != NULL, NULL);
1353 key_file_error = NULL;
1355 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1359 g_propagate_error (error, key_file_error);
1363 if (!g_utf8_validate (value, -1, NULL))
1365 gchar *value_utf8 = _g_utf8_make_valid (value);
1366 g_set_error (error, G_KEY_FILE_ERROR,
1367 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1368 _("Key file contains key '%s' with value '%s' "
1369 "which is not UTF-8"), key, value_utf8);
1370 g_free (value_utf8);
1376 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1382 if (g_error_matches (key_file_error,
1384 G_KEY_FILE_ERROR_INVALID_VALUE))
1386 g_set_error (error, G_KEY_FILE_ERROR,
1387 G_KEY_FILE_ERROR_INVALID_VALUE,
1388 _("Key file contains key '%s' "
1389 "which has value that cannot be interpreted."),
1391 g_error_free (key_file_error);
1394 g_propagate_error (error, key_file_error);
1397 return string_value;
1401 * g_key_file_set_string:
1402 * @key_file: a #GKeyFile
1403 * @group_name: a group name
1407 * Associates a new string value with @key under @group_name.
1408 * If @key cannot be found then it is created.
1409 * If @group_name cannot be found then it is created.
1414 g_key_file_set_string (GKeyFile *key_file,
1415 const gchar *group_name,
1417 const gchar *string)
1421 g_return_if_fail (key_file != NULL);
1422 g_return_if_fail (string != NULL);
1424 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1425 g_key_file_set_value (key_file, group_name, key, value);
1430 * g_key_file_get_string_list:
1431 * @key_file: a #GKeyFile
1432 * @group_name: a group name
1434 * @length: return location for the number of returned strings, or %NULL
1435 * @error: return location for a #GError, or %NULL
1437 * Returns the values associated with @key under @group_name.
1439 * In the event the key cannot be found, %NULL is returned and
1440 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1441 * event that the @group_name cannot be found, %NULL is returned
1442 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1444 * Return value: a %NULL-terminated string array or %NULL if the specified
1445 * key cannot be found. The array should be freed with g_strfreev().
1450 g_key_file_get_string_list (GKeyFile *key_file,
1451 const gchar *group_name,
1456 GError *key_file_error = NULL;
1457 gchar *value, *string_value, **values;
1459 GSList *p, *pieces = NULL;
1461 g_return_val_if_fail (key_file != NULL, NULL);
1462 g_return_val_if_fail (group_name != NULL, NULL);
1463 g_return_val_if_fail (key != NULL, NULL);
1465 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1469 g_propagate_error (error, key_file_error);
1473 if (!g_utf8_validate (value, -1, NULL))
1475 gchar *value_utf8 = _g_utf8_make_valid (value);
1476 g_set_error (error, G_KEY_FILE_ERROR,
1477 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1478 _("Key file contains key '%s' with value '%s' "
1479 "which is not UTF-8"), key, value_utf8);
1480 g_free (value_utf8);
1486 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1488 g_free (string_value);
1492 if (g_error_matches (key_file_error,
1494 G_KEY_FILE_ERROR_INVALID_VALUE))
1496 g_set_error (error, G_KEY_FILE_ERROR,
1497 G_KEY_FILE_ERROR_INVALID_VALUE,
1498 _("Key file contains key '%s' "
1499 "which has value that cannot be interpreted."),
1501 g_error_free (key_file_error);
1504 g_propagate_error (error, key_file_error);
1507 len = g_slist_length (pieces);
1508 values = g_new (gchar *, len + 1);
1509 for (p = pieces, i = 0; p; p = p->next)
1510 values[i++] = p->data;
1513 g_slist_free (pieces);
1522 * g_key_file_set_string_list:
1523 * @key_file: a #GKeyFile
1524 * @group_name: a group name
1526 * @list: an array of locale string values
1527 * @length: number of locale string values in @list
1529 * Associates a list of string values for @key under @group_name.
1530 * If @key cannot be found then it is created.
1531 * If @group_name cannot be found then it is created.
1536 g_key_file_set_string_list (GKeyFile *key_file,
1537 const gchar *group_name,
1539 const gchar * const list[],
1542 GString *value_list;
1545 g_return_if_fail (key_file != NULL);
1546 g_return_if_fail (list != NULL);
1548 value_list = g_string_sized_new (length * 128);
1549 for (i = 0; i < length && list[i] != NULL; i++)
1553 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1554 g_string_append (value_list, value);
1555 g_string_append_c (value_list, key_file->list_separator);
1560 g_key_file_set_value (key_file, group_name, key, value_list->str);
1561 g_string_free (value_list, TRUE);
1565 * g_key_file_set_locale_string:
1566 * @key_file: a #GKeyFile
1567 * @group_name: a group name
1572 * Associates a string value for @key and @locale under @group_name.
1573 * If the translation for @key cannot be found then it is created.
1578 g_key_file_set_locale_string (GKeyFile *key_file,
1579 const gchar *group_name,
1581 const gchar *locale,
1582 const gchar *string)
1584 gchar *full_key, *value;
1586 g_return_if_fail (key_file != NULL);
1587 g_return_if_fail (key != NULL);
1588 g_return_if_fail (locale != NULL);
1589 g_return_if_fail (string != NULL);
1591 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1592 full_key = g_strdup_printf ("%s[%s]", key, locale);
1593 g_key_file_set_value (key_file, group_name, full_key, value);
1598 extern GSList *_g_compute_locale_variants (const gchar *locale);
1601 * g_key_file_get_locale_string:
1602 * @key_file: a #GKeyFile
1603 * @group_name: a group name
1605 * @locale: a locale or %NULL
1606 * @error: return location for a #GError, or %NULL
1608 * Returns the value associated with @key under @group_name
1609 * translated in the given @locale if available. If @locale is
1610 * %NULL then the current locale is assumed.
1612 * If @key cannot be found then %NULL is returned and @error is set
1613 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1614 * with @key cannot be interpreted or no suitable translation can
1615 * be found then the untranslated value is returned.
1617 * Return value: a newly allocated string or %NULL if the specified
1618 * key cannot be found.
1623 g_key_file_get_locale_string (GKeyFile *key_file,
1624 const gchar *group_name,
1626 const gchar *locale,
1629 gchar *candidate_key, *translated_value;
1630 GError *key_file_error;
1632 gboolean free_languages = FALSE;
1635 g_return_val_if_fail (key_file != NULL, NULL);
1636 g_return_val_if_fail (group_name != NULL, NULL);
1637 g_return_val_if_fail (key != NULL, NULL);
1639 candidate_key = NULL;
1640 translated_value = NULL;
1641 key_file_error = NULL;
1647 list = _g_compute_locale_variants (locale);
1649 languages = g_new (gchar *, g_slist_length (list) + 1);
1650 for (l = list, i = 0; l; l = l->next, i++)
1651 languages[i] = l->data;
1652 languages[i] = NULL;
1654 g_slist_free (list);
1655 free_languages = TRUE;
1659 languages = (gchar **) g_get_language_names ();
1660 free_languages = FALSE;
1663 for (i = 0; languages[i]; i++)
1665 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1667 translated_value = g_key_file_get_string (key_file,
1669 candidate_key, NULL);
1670 g_free (candidate_key);
1672 if (translated_value)
1675 g_free (translated_value);
1676 translated_value = NULL;
1679 /* Fallback to untranslated key
1681 if (!translated_value)
1683 translated_value = g_key_file_get_string (key_file, group_name, key,
1686 if (!translated_value)
1687 g_propagate_error (error, key_file_error);
1691 g_strfreev (languages);
1693 return translated_value;
1697 * g_key_file_get_locale_string_list:
1698 * @key_file: a #GKeyFile
1699 * @group_name: a group name
1702 * @length: return location for the number of returned strings or %NULL
1703 * @error: return location for a #GError or %NULL
1705 * Returns the values associated with @key under @group_name
1706 * translated in the given @locale if available. If @locale is
1707 * %NULL then the current locale is assumed.
1709 * If @key cannot be found then %NULL is returned and @error is set
1710 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1711 * with @key cannot be interpreted or no suitable translations
1712 * can be found then the untranslated values are returned. The
1713 * returned array is %NULL-terminated, so @length may optionally
1716 * Return value: a newly allocated %NULL-terminated string array
1717 * or %NULL if the key isn't found. The string array should be freed
1718 * with g_strfreev().
1723 g_key_file_get_locale_string_list (GKeyFile *key_file,
1724 const gchar *group_name,
1726 const gchar *locale,
1730 GError *key_file_error;
1731 gchar **values, *value;
1733 g_return_val_if_fail (key_file != NULL, NULL);
1734 g_return_val_if_fail (group_name != NULL, NULL);
1735 g_return_val_if_fail (key != NULL, NULL);
1737 key_file_error = NULL;
1739 value = g_key_file_get_locale_string (key_file, group_name,
1744 g_propagate_error (error, key_file_error);
1749 if (value[strlen (value) - 1] == ';')
1750 value[strlen (value) - 1] = '\0';
1752 values = g_strsplit (value, ";", 0);
1757 *length = g_strv_length (values);
1763 * g_key_file_set_locale_string_list:
1764 * @key_file: a #GKeyFile
1765 * @group_name: a group name
1768 * @list: a %NULL-terminated array of locale string values
1769 * @length: the length of @list
1771 * Associates a list of string values for @key and @locale under
1772 * @group_name. If the translation for @key cannot be found then
1778 g_key_file_set_locale_string_list (GKeyFile *key_file,
1779 const gchar *group_name,
1781 const gchar *locale,
1782 const gchar * const list[],
1785 GString *value_list;
1789 g_return_if_fail (key_file != NULL);
1790 g_return_if_fail (key != NULL);
1791 g_return_if_fail (locale != NULL);
1792 g_return_if_fail (length != 0);
1794 value_list = g_string_sized_new (length * 128);
1795 for (i = 0; i < length && list[i] != NULL; i++)
1799 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1801 g_string_append (value_list, value);
1802 g_string_append_c (value_list, ';');
1807 full_key = g_strdup_printf ("%s[%s]", key, locale);
1808 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1810 g_string_free (value_list, TRUE);
1814 * g_key_file_get_boolean:
1815 * @key_file: a #GKeyFile
1816 * @group_name: a group name
1818 * @error: return location for a #GError
1820 * Returns the value associated with @key under @group_name as a
1823 * If @key cannot be found then %FALSE is returned and @error is set
1824 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1825 * associated with @key cannot be interpreted as a boolean then %FALSE
1826 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1828 * Return value: the value associated with the key as a boolean,
1829 * or %FALSE if the key was not found or could not be parsed.
1834 g_key_file_get_boolean (GKeyFile *key_file,
1835 const gchar *group_name,
1839 GError *key_file_error = NULL;
1841 gboolean bool_value;
1843 g_return_val_if_fail (key_file != NULL, FALSE);
1844 g_return_val_if_fail (group_name != NULL, FALSE);
1845 g_return_val_if_fail (key != NULL, FALSE);
1847 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1851 g_propagate_error (error, key_file_error);
1855 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1861 if (g_error_matches (key_file_error,
1863 G_KEY_FILE_ERROR_INVALID_VALUE))
1865 g_set_error (error, G_KEY_FILE_ERROR,
1866 G_KEY_FILE_ERROR_INVALID_VALUE,
1867 _("Key file contains key '%s' "
1868 "which has value that cannot be interpreted."),
1870 g_error_free (key_file_error);
1873 g_propagate_error (error, key_file_error);
1880 * g_key_file_set_boolean:
1881 * @key_file: a #GKeyFile
1882 * @group_name: a group name
1884 * @value: %TRUE or %FALSE
1886 * Associates a new boolean value with @key under @group_name.
1887 * If @key cannot be found then it is created.
1892 g_key_file_set_boolean (GKeyFile *key_file,
1893 const gchar *group_name,
1899 g_return_if_fail (key_file != NULL);
1901 result = g_key_file_parse_boolean_as_value (key_file, value);
1902 g_key_file_set_value (key_file, group_name, key, result);
1907 * g_key_file_get_boolean_list:
1908 * @key_file: a #GKeyFile
1909 * @group_name: a group name
1911 * @length: the number of booleans returned
1912 * @error: return location for a #GError
1914 * Returns the values associated with @key under @group_name as
1915 * booleans. If @group_name is %NULL, the start_group is used.
1917 * If @key cannot be found then %NULL is returned and @error is set to
1918 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1919 * with @key cannot be interpreted as booleans then %NULL is returned
1920 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1922 * Return value: the values associated with the key as a list of
1923 * booleans, or %NULL if the key was not found or could not be parsed.
1928 g_key_file_get_boolean_list (GKeyFile *key_file,
1929 const gchar *group_name,
1934 GError *key_file_error;
1936 gboolean *bool_values;
1939 g_return_val_if_fail (key_file != NULL, NULL);
1940 g_return_val_if_fail (group_name != NULL, NULL);
1941 g_return_val_if_fail (key != NULL, NULL);
1943 key_file_error = NULL;
1945 values = g_key_file_get_string_list (key_file, group_name, key,
1946 &num_bools, &key_file_error);
1949 g_propagate_error (error, key_file_error);
1954 bool_values = g_new (gboolean, num_bools);
1956 for (i = 0; i < num_bools; i++)
1958 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1964 g_propagate_error (error, key_file_error);
1965 g_strfreev (values);
1966 g_free (bool_values);
1971 g_strfreev (values);
1974 *length = num_bools;
1980 * g_key_file_set_boolean_list:
1981 * @key_file: a #GKeyFile
1982 * @group_name: a group name
1984 * @list: an array of boolean values
1985 * @length: length of @list
1987 * Associates a list of boolean values with @key under @group_name.
1988 * If @key cannot be found then it is created.
1989 * If @group_name is %NULL, the start_group is used.
1994 g_key_file_set_boolean_list (GKeyFile *key_file,
1995 const gchar *group_name,
2000 GString *value_list;
2003 g_return_if_fail (key_file != NULL);
2004 g_return_if_fail (list != NULL);
2006 value_list = g_string_sized_new (length * 8);
2007 for (i = 0; i < length; i++)
2011 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2013 g_string_append (value_list, value);
2014 g_string_append_c (value_list, key_file->list_separator);
2019 g_key_file_set_value (key_file, group_name, key, value_list->str);
2020 g_string_free (value_list, TRUE);
2024 * g_key_file_get_integer:
2025 * @key_file: a #GKeyFile
2026 * @group_name: a group name
2028 * @error: return location for a #GError
2030 * Returns the value associated with @key under @group_name as an
2031 * integer. If @group_name is %NULL, the start group is used.
2033 * If @key cannot be found then 0 is returned and @error is set to
2034 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2035 * with @key cannot be interpreted as an integer then 0 is returned
2036 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2038 * Return value: the value associated with the key as an integer, or
2039 * 0 if the key was not found or could not be parsed.
2044 g_key_file_get_integer (GKeyFile *key_file,
2045 const gchar *group_name,
2049 GError *key_file_error;
2053 g_return_val_if_fail (key_file != NULL, -1);
2054 g_return_val_if_fail (group_name != NULL, -1);
2055 g_return_val_if_fail (key != NULL, -1);
2057 key_file_error = NULL;
2059 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2063 g_propagate_error (error, key_file_error);
2067 int_value = g_key_file_parse_value_as_integer (key_file, value,
2073 if (g_error_matches (key_file_error,
2075 G_KEY_FILE_ERROR_INVALID_VALUE))
2077 g_set_error (error, G_KEY_FILE_ERROR,
2078 G_KEY_FILE_ERROR_INVALID_VALUE,
2079 _("Key file contains key '%s' in group '%s' "
2080 "which has value that cannot be interpreted."), key,
2082 g_error_free (key_file_error);
2085 g_propagate_error (error, key_file_error);
2092 * g_key_file_set_integer:
2093 * @key_file: a #GKeyFile
2094 * @group_name: a group name
2096 * @value: an integer value
2098 * Associates a new integer value with @key under @group_name.
2099 * If @key cannot be found then it is created.
2104 g_key_file_set_integer (GKeyFile *key_file,
2105 const gchar *group_name,
2111 g_return_if_fail (key_file != NULL);
2113 result = g_key_file_parse_integer_as_value (key_file, value);
2114 g_key_file_set_value (key_file, group_name, key, result);
2119 * g_key_file_get_integer_list:
2120 * @key_file: a #GKeyFile
2121 * @group_name: a group name
2123 * @length: the number of integers returned
2124 * @error: return location for a #GError
2126 * Returns the values associated with @key under @group_name as
2129 * If @key cannot be found then %NULL is returned and @error is set to
2130 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2131 * with @key cannot be interpreted as integers then %NULL is returned
2132 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2134 * Return value: the values associated with the key as a list of
2135 * integers, or %NULL if the key was not found or could not be parsed.
2140 g_key_file_get_integer_list (GKeyFile *key_file,
2141 const gchar *group_name,
2146 GError *key_file_error = NULL;
2151 g_return_val_if_fail (key_file != NULL, NULL);
2152 g_return_val_if_fail (group_name != NULL, NULL);
2153 g_return_val_if_fail (key != NULL, NULL);
2155 values = g_key_file_get_string_list (key_file, group_name, key,
2156 &num_ints, &key_file_error);
2159 g_propagate_error (error, key_file_error);
2164 int_values = g_new (gint, num_ints);
2166 for (i = 0; i < num_ints; i++)
2168 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2174 g_propagate_error (error, key_file_error);
2175 g_strfreev (values);
2176 g_free (int_values);
2181 g_strfreev (values);
2190 * g_key_file_set_integer_list:
2191 * @key_file: a #GKeyFile
2192 * @group_name: a group name
2194 * @list: an array of integer values
2195 * @length: number of integer values in @list
2197 * Associates a list of integer values with @key under @group_name.
2198 * If @key cannot be found then it is created.
2203 g_key_file_set_integer_list (GKeyFile *key_file,
2204 const gchar *group_name,
2212 g_return_if_fail (key_file != NULL);
2213 g_return_if_fail (list != NULL);
2215 values = g_string_sized_new (length * 16);
2216 for (i = 0; i < length; i++)
2220 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2222 g_string_append (values, value);
2223 g_string_append_c (values, ';');
2228 g_key_file_set_value (key_file, group_name, key, values->str);
2229 g_string_free (values, TRUE);
2233 * g_key_file_get_double:
2234 * @key_file: a #GKeyFile
2235 * @group_name: a group name
2237 * @error: return location for a #GError
2239 * Returns the value associated with @key under @group_name as a
2240 * double. If @group_name is %NULL, the start_group is used.
2242 * If @key cannot be found then 0.0 is returned and @error is set to
2243 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2244 * with @key cannot be interpreted as a double then 0.0 is returned
2245 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2247 * Return value: the value associated with the key as a double, or
2248 * 0.0 if the key was not found or could not be parsed.
2253 g_key_file_get_double (GKeyFile *key_file,
2254 const gchar *group_name,
2258 GError *key_file_error;
2260 gdouble double_value;
2262 g_return_val_if_fail (key_file != NULL, -1);
2263 g_return_val_if_fail (group_name != NULL, -1);
2264 g_return_val_if_fail (key != NULL, -1);
2266 key_file_error = NULL;
2268 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2272 g_propagate_error (error, key_file_error);
2276 double_value = g_key_file_parse_value_as_double (key_file, value,
2282 if (g_error_matches (key_file_error,
2284 G_KEY_FILE_ERROR_INVALID_VALUE))
2286 g_set_error (error, G_KEY_FILE_ERROR,
2287 G_KEY_FILE_ERROR_INVALID_VALUE,
2288 _("Key file contains key '%s' in group '%s' "
2289 "which has value that cannot be interpreted."), key,
2291 g_error_free (key_file_error);
2294 g_propagate_error (error, key_file_error);
2297 return double_value;
2301 * g_key_file_set_double:
2302 * @key_file: a #GKeyFile
2303 * @group_name: a group name
2305 * @value: an double value
2307 * Associates a new double value with @key under @group_name.
2308 * If @key cannot be found then it is created.
2309 * If @group_name is %NULL, the start group is used.
2314 g_key_file_set_double (GKeyFile *key_file,
2315 const gchar *group_name,
2319 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2321 g_return_if_fail (key_file != NULL);
2323 g_ascii_dtostr (result, sizeof (result), value);
2324 g_key_file_set_value (key_file, group_name, key, result);
2328 * g_key_file_get_double_list:
2329 * @key_file: a #GKeyFile
2330 * @group_name: a group name
2332 * @length: the number of doubles returned
2333 * @error: return location for a #GError
2335 * Returns the values associated with @key under @group_name as
2336 * doubles. If @group_name is %NULL, the start group is used.
2338 * If @key cannot be found then %NULL is returned and @error is set to
2339 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2340 * with @key cannot be interpreted as doubles then %NULL is returned
2341 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2343 * Return value: the values associated with the key as a list of
2344 * doubles, or %NULL if the key was not found or could not be parsed.
2349 g_key_file_get_double_list (GKeyFile *key_file,
2350 const gchar *group_name,
2355 GError *key_file_error = NULL;
2357 gdouble *double_values;
2358 gsize i, num_doubles;
2360 g_return_val_if_fail (key_file != NULL, NULL);
2361 g_return_val_if_fail (group_name != NULL, NULL);
2362 g_return_val_if_fail (key != NULL, NULL);
2364 values = g_key_file_get_string_list (key_file, group_name, key,
2365 &num_doubles, &key_file_error);
2368 g_propagate_error (error, key_file_error);
2373 double_values = g_new (gdouble, num_doubles);
2375 for (i = 0; i < num_doubles; i++)
2377 double_values[i] = g_key_file_parse_value_as_double (key_file,
2383 g_propagate_error (error, key_file_error);
2384 g_strfreev (values);
2385 g_free (double_values);
2390 g_strfreev (values);
2393 *length = num_doubles;
2395 return double_values;
2399 * g_key_file_set_double_list:
2400 * @key_file: a #GKeyFile
2401 * @group_name: a group name
2403 * @list: an array of double values
2404 * @length: number of double values in @list
2406 * Associates a list of double values with @key under
2407 * @group_name. If @key cannot be found then it is created.
2408 * If @group_name is %NULL the start group is used.
2413 g_key_file_set_double_list (GKeyFile *key_file,
2414 const gchar *group_name,
2422 g_return_if_fail (key_file != NULL);
2423 g_return_if_fail (list != NULL);
2425 values = g_string_sized_new (length * 16);
2426 for (i = 0; i < length; i++)
2428 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2430 g_ascii_dtostr( result, sizeof (result), list[i] );
2432 g_string_append (values, result);
2433 g_string_append_c (values, ';');
2436 g_key_file_set_value (key_file, group_name, key, values->str);
2437 g_string_free (values, TRUE);
2441 g_key_file_set_key_comment (GKeyFile *key_file,
2442 const gchar *group_name,
2444 const gchar *comment,
2447 GKeyFileGroup *group;
2448 GKeyFileKeyValuePair *pair;
2449 GList *key_node, *comment_node, *tmp;
2451 group = g_key_file_lookup_group (key_file, group_name);
2454 g_set_error (error, G_KEY_FILE_ERROR,
2455 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2456 _("Key file does not have group '%s'"),
2457 group_name ? group_name : "(null)");
2462 /* First find the key the comments are supposed to be
2465 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2467 if (key_node == NULL)
2469 g_set_error (error, G_KEY_FILE_ERROR,
2470 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2471 _("Key file does not have key '%s' in group '%s'"),
2476 /* Then find all the comments already associated with the
2479 tmp = key_node->next;
2482 GKeyFileKeyValuePair *pair;
2484 pair = (GKeyFileKeyValuePair *) tmp->data;
2486 if (pair->key != NULL)
2491 g_key_file_remove_key_value_pair_node (key_file, group,
2495 if (comment == NULL)
2498 /* Now we can add our new comment
2500 pair = g_slice_new (GKeyFileKeyValuePair);
2502 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2504 key_node = g_list_insert (key_node, pair, 1);
2510 g_key_file_set_group_comment (GKeyFile *key_file,
2511 const gchar *group_name,
2512 const gchar *comment,
2515 GKeyFileGroup *group;
2517 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2519 group = g_key_file_lookup_group (key_file, group_name);
2522 g_set_error (error, G_KEY_FILE_ERROR,
2523 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2524 _("Key file does not have group '%s'"),
2525 group_name ? group_name : "(null)");
2530 /* First remove any existing comment
2534 g_key_file_key_value_pair_free (group->comment);
2535 group->comment = NULL;
2538 if (comment == NULL)
2541 /* Now we can add our new comment
2543 group->comment = g_slice_new (GKeyFileKeyValuePair);
2544 group->comment->key = NULL;
2545 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2551 g_key_file_set_top_comment (GKeyFile *key_file,
2552 const gchar *comment,
2556 GKeyFileGroup *group;
2557 GKeyFileKeyValuePair *pair;
2559 /* The last group in the list should be the top (comments only)
2562 g_assert (key_file->groups != NULL);
2563 group_node = g_list_last (key_file->groups);
2564 group = (GKeyFileGroup *) group_node->data;
2565 g_assert (group->name == NULL);
2567 /* Note all keys must be comments at the top of
2568 * the file, so we can just free it all.
2570 if (group->key_value_pairs != NULL)
2572 g_list_foreach (group->key_value_pairs,
2573 (GFunc) g_key_file_key_value_pair_free,
2575 g_list_free (group->key_value_pairs);
2576 group->key_value_pairs = NULL;
2579 if (comment == NULL)
2582 pair = g_slice_new (GKeyFileKeyValuePair);
2584 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2586 group->key_value_pairs =
2587 g_list_prepend (group->key_value_pairs, pair);
2593 * g_key_file_set_comment:
2594 * @key_file: a #GKeyFile
2595 * @group_name: a group name, or %NULL
2597 * @comment: a comment
2598 * @error: return location for a #GError
2600 * Places a comment above @key from @group_name.
2601 * If @key is %NULL then @comment will be written above @group_name.
2602 * If both @key and @group_name are %NULL, then @comment will be
2603 * written above the first group in the file.
2605 * Returns: %TRUE if the comment was written, %FALSE otherwise
2610 g_key_file_set_comment (GKeyFile *key_file,
2611 const gchar *group_name,
2613 const gchar *comment,
2616 g_return_val_if_fail (key_file != NULL, FALSE);
2618 if (group_name != NULL && key != NULL)
2620 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2623 else if (group_name != NULL)
2625 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2630 if (!g_key_file_set_top_comment (key_file, comment, error))
2634 if (comment != NULL)
2635 key_file->approximate_size += strlen (comment);
2641 g_key_file_get_key_comment (GKeyFile *key_file,
2642 const gchar *group_name,
2646 GKeyFileGroup *group;
2647 GKeyFileKeyValuePair *pair;
2648 GList *key_node, *tmp;
2652 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2654 group = g_key_file_lookup_group (key_file, group_name);
2657 g_set_error (error, G_KEY_FILE_ERROR,
2658 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2659 _("Key file does not have group '%s'"),
2660 group_name ? group_name : "(null)");
2665 /* First find the key the comments are supposed to be
2668 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2670 if (key_node == NULL)
2672 g_set_error (error, G_KEY_FILE_ERROR,
2673 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2674 _("Key file does not have key '%s' in group '%s'"),
2681 /* Then find all the comments already associated with the
2682 * key and concatentate them.
2684 tmp = key_node->next;
2685 if (!key_node->next)
2688 pair = (GKeyFileKeyValuePair *) tmp->data;
2689 if (pair->key != NULL)
2694 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2696 if (pair->key != NULL)
2702 while (tmp != key_node)
2704 GKeyFileKeyValuePair *pair;
2706 pair = (GKeyFileKeyValuePair *) tmp->data;
2709 string = g_string_sized_new (512);
2711 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2712 g_string_append (string, comment);
2720 comment = string->str;
2721 g_string_free (string, FALSE);
2730 get_group_comment (GKeyFile *key_file,
2731 GKeyFileGroup *group,
2740 tmp = group->key_value_pairs;
2743 GKeyFileKeyValuePair *pair;
2745 pair = (GKeyFileKeyValuePair *) tmp->data;
2747 if (pair->key != NULL)
2753 if (tmp->next == NULL)
2761 GKeyFileKeyValuePair *pair;
2763 pair = (GKeyFileKeyValuePair *) tmp->data;
2766 string = g_string_sized_new (512);
2768 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2769 g_string_append (string, comment);
2776 return g_string_free (string, FALSE);
2782 g_key_file_get_group_comment (GKeyFile *key_file,
2783 const gchar *group_name,
2787 GKeyFileGroup *group;
2789 group_node = g_key_file_lookup_group_node (key_file, group_name);
2792 g_set_error (error, G_KEY_FILE_ERROR,
2793 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2794 _("Key file does not have group '%s'"),
2795 group_name ? group_name : "(null)");
2800 group = (GKeyFileGroup *)group_node->data;
2802 return g_strdup (group->comment->value);
2804 group_node = group_node->next;
2805 group = (GKeyFileGroup *)group_node->data;
2806 return get_group_comment (key_file, group, error);
2810 g_key_file_get_top_comment (GKeyFile *key_file,
2814 GKeyFileGroup *group;
2816 /* The last group in the list should be the top (comments only)
2819 g_assert (key_file->groups != NULL);
2820 group_node = g_list_last (key_file->groups);
2821 group = (GKeyFileGroup *) group_node->data;
2822 g_assert (group->name == NULL);
2824 return get_group_comment (key_file, group, error);
2828 * g_key_file_get_comment:
2829 * @key_file: a #GKeyFile
2830 * @group_name: a group name, or %NULL
2832 * @error: return location for a #GError
2834 * Retrieves a comment above @key from @group_name.
2835 * If @key is %NULL then @comment will be read from above
2836 * @group_name. If both @key and @group_name are %NULL, then
2837 * @comment will be read from above the first group in the file.
2839 * Returns: a comment that should be freed with g_free()
2844 g_key_file_get_comment (GKeyFile *key_file,
2845 const gchar *group_name,
2849 g_return_val_if_fail (key_file != NULL, NULL);
2851 if (group_name != NULL && key != NULL)
2852 return g_key_file_get_key_comment (key_file, group_name, key, error);
2853 else if (group_name != NULL)
2854 return g_key_file_get_group_comment (key_file, group_name, error);
2856 return g_key_file_get_top_comment (key_file, error);
2860 * g_key_file_remove_comment:
2861 * @key_file: a #GKeyFile
2862 * @group_name: a group name, or %NULL
2864 * @error: return location for a #GError
2866 * Removes a comment above @key from @group_name.
2867 * If @key is %NULL then @comment will be written above @group_name.
2868 * If both @key and @group_name are %NULL, then @comment will
2869 * be written above the first group in the file.
2871 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2877 g_key_file_remove_comment (GKeyFile *key_file,
2878 const gchar *group_name,
2882 g_return_val_if_fail (key_file != NULL, FALSE);
2884 if (group_name != NULL && key != NULL)
2885 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2886 else if (group_name != NULL)
2887 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2889 return g_key_file_set_top_comment (key_file, NULL, error);
2893 * g_key_file_has_group:
2894 * @key_file: a #GKeyFile
2895 * @group_name: a group name
2897 * Looks whether the key file has the group @group_name.
2899 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2904 g_key_file_has_group (GKeyFile *key_file,
2905 const gchar *group_name)
2907 g_return_val_if_fail (key_file != NULL, FALSE);
2908 g_return_val_if_fail (group_name != NULL, FALSE);
2910 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2914 * g_key_file_has_key:
2915 * @key_file: a #GKeyFile
2916 * @group_name: a group name
2918 * @error: return location for a #GError
2920 * Looks whether the key file has the key @key in the group
2923 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2929 g_key_file_has_key (GKeyFile *key_file,
2930 const gchar *group_name,
2934 GKeyFileKeyValuePair *pair;
2935 GKeyFileGroup *group;
2937 g_return_val_if_fail (key_file != NULL, FALSE);
2938 g_return_val_if_fail (group_name != NULL, FALSE);
2939 g_return_val_if_fail (key != NULL, FALSE);
2941 group = g_key_file_lookup_group (key_file, group_name);
2945 g_set_error (error, G_KEY_FILE_ERROR,
2946 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2947 _("Key file does not have group '%s'"),
2948 group_name ? group_name : "(null)");
2953 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2955 return pair != NULL;
2959 g_key_file_add_group (GKeyFile *key_file,
2960 const gchar *group_name)
2962 GKeyFileGroup *group;
2964 g_return_if_fail (key_file != NULL);
2965 g_return_if_fail (g_key_file_is_group_name (group_name));
2967 group = g_key_file_lookup_group (key_file, group_name);
2970 key_file->current_group = group;
2974 group = g_slice_new0 (GKeyFileGroup);
2975 group->name = g_strdup (group_name);
2976 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2977 key_file->groups = g_list_prepend (key_file->groups, group);
2978 key_file->approximate_size += strlen (group_name) + 3;
2979 key_file->current_group = group;
2981 if (key_file->start_group == NULL)
2982 key_file->start_group = group;
2986 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2991 g_free (pair->value);
2992 g_slice_free (GKeyFileKeyValuePair, pair);
2996 /* Be careful not to call this function on a node with data in the
2997 * lookup map without removing it from the lookup map, first.
2999 * Some current cases where this warning is not a concern are
3001 * - the node being removed is a comment node
3002 * - the entire lookup map is getting destroyed soon after
3006 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3007 GKeyFileGroup *group,
3011 GKeyFileKeyValuePair *pair;
3013 pair = (GKeyFileKeyValuePair *) pair_node->data;
3015 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3017 if (pair->key != NULL)
3018 key_file->approximate_size -= strlen (pair->key) + 1;
3020 g_assert (pair->value != NULL);
3021 key_file->approximate_size -= strlen (pair->value);
3023 g_key_file_key_value_pair_free (pair);
3025 g_list_free_1 (pair_node);
3029 g_key_file_remove_group_node (GKeyFile *key_file,
3032 GKeyFileGroup *group;
3035 group = (GKeyFileGroup *) group_node->data;
3037 /* If the current group gets deleted make the current group the last
3040 if (key_file->current_group == group)
3042 /* groups should always contain at least the top comment group,
3043 * unless g_key_file_clear has been called
3045 if (key_file->groups)
3046 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3048 key_file->current_group = NULL;
3051 /* If the start group gets deleted make the start group the first
3054 if (key_file->start_group == group)
3056 tmp = g_list_last (key_file->groups);
3059 if (tmp != group_node &&
3060 ((GKeyFileGroup *) tmp->data)->name != NULL)
3067 key_file->start_group = (GKeyFileGroup *) tmp->data;
3069 key_file->start_group = NULL;
3072 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3074 if (group->name != NULL)
3075 key_file->approximate_size -= strlen (group->name) + 3;
3077 tmp = group->key_value_pairs;
3084 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3087 g_assert (group->key_value_pairs == NULL);
3089 if (group->lookup_map)
3091 g_hash_table_destroy (group->lookup_map);
3092 group->lookup_map = NULL;
3095 g_free ((gchar *) group->name);
3096 g_slice_free (GKeyFileGroup, group);
3097 g_list_free_1 (group_node);
3101 * g_key_file_remove_group:
3102 * @key_file: a #GKeyFile
3103 * @group_name: a group name
3104 * @error: return location for a #GError or %NULL
3106 * Removes the specified group, @group_name,
3107 * from the key file.
3109 * Returns: %TRUE if the group was removed, %FALSE otherwise
3114 g_key_file_remove_group (GKeyFile *key_file,
3115 const gchar *group_name,
3120 g_return_val_if_fail (key_file != NULL, FALSE);
3121 g_return_val_if_fail (group_name != NULL, FALSE);
3123 group_node = g_key_file_lookup_group_node (key_file, group_name);
3127 g_set_error (error, G_KEY_FILE_ERROR,
3128 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3129 _("Key file does not have group '%s'"),
3134 g_key_file_remove_group_node (key_file, group_node);
3140 g_key_file_add_key (GKeyFile *key_file,
3141 GKeyFileGroup *group,
3145 GKeyFileKeyValuePair *pair;
3147 pair = g_slice_new (GKeyFileKeyValuePair);
3148 pair->key = g_strdup (key);
3149 pair->value = g_strdup (value);
3151 g_hash_table_replace (group->lookup_map, pair->key, pair);
3152 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3153 group->has_trailing_blank_line = FALSE;
3154 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3158 * g_key_file_remove_key:
3159 * @key_file: a #GKeyFile
3160 * @group_name: a group name
3161 * @key: a key name to remove
3162 * @error: return location for a #GError or %NULL
3164 * Removes @key in @group_name from the key file.
3166 * Returns: %TRUE if the key was removed, %FALSE otherwise
3171 g_key_file_remove_key (GKeyFile *key_file,
3172 const gchar *group_name,
3176 GKeyFileGroup *group;
3177 GKeyFileKeyValuePair *pair;
3179 g_return_val_if_fail (key_file != NULL, FALSE);
3180 g_return_val_if_fail (group_name != NULL, FALSE);
3181 g_return_val_if_fail (key != NULL, FALSE);
3185 group = g_key_file_lookup_group (key_file, group_name);
3188 g_set_error (error, G_KEY_FILE_ERROR,
3189 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3190 _("Key file does not have group '%s'"),
3191 group_name ? group_name : "(null)");
3195 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3199 g_set_error (error, G_KEY_FILE_ERROR,
3200 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3201 _("Key file does not have key '%s' in group '%s'"),
3206 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3208 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3209 g_hash_table_remove (group->lookup_map, pair->key);
3210 g_key_file_key_value_pair_free (pair);
3216 g_key_file_lookup_group_node (GKeyFile *key_file,
3217 const gchar *group_name)
3219 GKeyFileGroup *group;
3222 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3224 group = (GKeyFileGroup *) tmp->data;
3226 if (group && group->name && strcmp (group->name, group_name) == 0)
3233 static GKeyFileGroup *
3234 g_key_file_lookup_group (GKeyFile *key_file,
3235 const gchar *group_name)
3239 group_node = g_key_file_lookup_group_node (key_file, group_name);
3241 if (group_node != NULL)
3242 return (GKeyFileGroup *) group_node->data;
3248 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3249 GKeyFileGroup *group,
3254 for (key_node = group->key_value_pairs;
3256 key_node = key_node->next)
3258 GKeyFileKeyValuePair *pair;
3260 pair = (GKeyFileKeyValuePair *) key_node->data;
3262 if (pair->key && strcmp (pair->key, key) == 0)
3269 static GKeyFileKeyValuePair *
3270 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3271 GKeyFileGroup *group,
3274 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3277 /* Lines starting with # or consisting entirely of whitespace are merely
3278 * recorded, not parsed. This function assumes all leading whitespace
3279 * has been stripped.
3282 g_key_file_line_is_comment (const gchar *line)
3284 return (*line == '#' || *line == '\0' || *line == '\n');
3288 g_key_file_is_group_name (const gchar *name)
3295 p = q = (gchar *) name;
3296 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3297 q = g_utf8_find_next_char (q, NULL);
3299 if (*q != '\0' || q == p)
3306 g_key_file_is_key_name (const gchar *name)
3313 p = q = (gchar *) name;
3314 /* We accept a little more than the desktop entry spec says,
3315 * since gnome-vfs uses mime-types as keys in its cache.
3317 while (*q && *q != '=' && *q != '[' && *q != ']')
3318 q = g_utf8_find_next_char (q, NULL);
3320 /* No empty keys, please */
3324 /* We accept spaces in the middle of keys to not break
3325 * existing apps, but we don't tolerate initial of final
3326 * spaces, which would lead to silent corruption when
3327 * rereading the file.
3329 if (*p == ' ' || q[-1] == ' ')
3335 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3336 q = g_utf8_find_next_char (q, NULL);
3350 /* A group in a key file is made up of a starting '[' followed by one
3351 * or more letters making up the group name followed by ']'.
3354 g_key_file_line_is_group (const gchar *line)
3364 while (*p && *p != ']')
3365 p = g_utf8_find_next_char (p, NULL);
3370 /* silently accept whitespace after the ] */
3371 p = g_utf8_find_next_char (p, NULL);
3372 while (*p == ' ' || *p == '\t')
3373 p = g_utf8_find_next_char (p, NULL);
3382 g_key_file_line_is_key_value_pair (const gchar *line)
3386 p = (gchar *) g_utf8_strchr (line, -1, '=');
3391 /* Key must be non-empty
3400 g_key_file_parse_value_as_string (GKeyFile *key_file,
3405 gchar *string_value, *p, *q0, *q;
3407 string_value = g_new (gchar, strlen (value) + 1);
3409 p = (gchar *) value;
3410 q0 = q = string_value;
3440 g_set_error (error, G_KEY_FILE_ERROR,
3441 G_KEY_FILE_ERROR_INVALID_VALUE,
3442 _("Key file contains escape character "
3447 if (pieces && *p == key_file->list_separator)
3448 *q = key_file->list_separator;
3462 g_set_error (error, G_KEY_FILE_ERROR,
3463 G_KEY_FILE_ERROR_INVALID_VALUE,
3464 _("Key file contains invalid escape "
3465 "sequence '%s'"), sequence);
3474 if (pieces && (*p == key_file->list_separator))
3476 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3492 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3493 *pieces = g_slist_reverse (*pieces);
3496 return string_value;
3500 g_key_file_parse_string_as_value (GKeyFile *key_file,
3501 const gchar *string,
3502 gboolean escape_separator)
3504 gchar *value, *p, *q;
3506 gboolean parsing_leading_space;
3508 length = strlen (string) + 1;
3510 /* Worst case would be that every character needs to be escaped.
3511 * In other words every character turns to two characters
3513 value = g_new (gchar, 2 * length);
3515 p = (gchar *) string;
3517 parsing_leading_space = TRUE;
3518 while (p < (string + length - 1))
3520 gchar escaped_character[3] = { '\\', 0, 0 };
3525 if (parsing_leading_space)
3527 escaped_character[1] = 's';
3528 strcpy (q, escaped_character);
3538 if (parsing_leading_space)
3540 escaped_character[1] = 't';
3541 strcpy (q, escaped_character);
3551 escaped_character[1] = 'n';
3552 strcpy (q, escaped_character);
3556 escaped_character[1] = 'r';
3557 strcpy (q, escaped_character);
3561 escaped_character[1] = '\\';
3562 strcpy (q, escaped_character);
3564 parsing_leading_space = FALSE;
3567 if (escape_separator && *p == key_file->list_separator)
3569 escaped_character[1] = key_file->list_separator;
3570 strcpy (q, escaped_character);
3572 parsing_leading_space = TRUE;
3578 parsing_leading_space = FALSE;
3590 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3594 gchar *end_of_valid_int;
3599 long_value = strtol (value, &end_of_valid_int, 10);
3601 if (*value == '\0' || *end_of_valid_int != '\0')
3603 gchar *value_utf8 = _g_utf8_make_valid (value);
3604 g_set_error (error, G_KEY_FILE_ERROR,
3605 G_KEY_FILE_ERROR_INVALID_VALUE,
3606 _("Value '%s' cannot be interpreted "
3607 "as a number."), value_utf8);
3608 g_free (value_utf8);
3613 int_value = long_value;
3614 if (int_value != long_value || errno == ERANGE)
3616 gchar *value_utf8 = _g_utf8_make_valid (value);
3619 G_KEY_FILE_ERROR_INVALID_VALUE,
3620 _("Integer value '%s' out of range"),
3622 g_free (value_utf8);
3631 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3635 return g_strdup_printf ("%d", value);
3639 g_key_file_parse_value_as_double (GKeyFile *key_file,
3643 gchar *end_of_valid_d;
3644 gdouble double_value = 0;
3646 double_value = g_ascii_strtod (value, &end_of_valid_d);
3648 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3650 gchar *value_utf8 = _g_utf8_make_valid (value);
3651 g_set_error (error, G_KEY_FILE_ERROR,
3652 G_KEY_FILE_ERROR_INVALID_VALUE,
3653 _("Value '%s' cannot be interpreted "
3654 "as a float number."),
3656 g_free (value_utf8);
3659 return double_value;
3663 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3669 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3671 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3674 value_utf8 = _g_utf8_make_valid (value);
3675 g_set_error (error, G_KEY_FILE_ERROR,
3676 G_KEY_FILE_ERROR_INVALID_VALUE,
3677 _("Value '%s' cannot be interpreted "
3678 "as a boolean."), value_utf8);
3679 g_free (value_utf8);
3685 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3689 return g_strdup ("true");
3691 return g_strdup ("false");
3695 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3702 string = g_string_sized_new (512);
3704 lines = g_strsplit (value, "\n", 0);
3706 for (i = 0; lines[i] != NULL; i++)
3708 if (lines[i][0] != '#')
3709 g_string_append_printf (string, "%s\n", lines[i]);
3711 g_string_append_printf (string, "%s\n", lines[i] + 1);
3715 return g_string_free (string, FALSE);
3719 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3720 const gchar *comment)
3726 string = g_string_sized_new (512);
3728 lines = g_strsplit (comment, "\n", 0);
3730 for (i = 0; lines[i] != NULL; i++)
3731 g_string_append_printf (string, "#%s%s", lines[i],
3732 lines[i + 1] == NULL? "" : "\n");
3735 return g_string_free (string, FALSE);
3738 #define __G_KEY_FILE_C__
3739 #include "galiasdef.c"