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);
1468 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1472 g_propagate_error (error, key_file_error);
1476 if (!g_utf8_validate (value, -1, NULL))
1478 gchar *value_utf8 = _g_utf8_make_valid (value);
1479 g_set_error (error, G_KEY_FILE_ERROR,
1480 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1481 _("Key file contains key '%s' with value '%s' "
1482 "which is not UTF-8"), key, value_utf8);
1483 g_free (value_utf8);
1489 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1491 g_free (string_value);
1495 if (g_error_matches (key_file_error,
1497 G_KEY_FILE_ERROR_INVALID_VALUE))
1499 g_set_error (error, G_KEY_FILE_ERROR,
1500 G_KEY_FILE_ERROR_INVALID_VALUE,
1501 _("Key file contains key '%s' "
1502 "which has value that cannot be interpreted."),
1504 g_error_free (key_file_error);
1507 g_propagate_error (error, key_file_error);
1510 len = g_slist_length (pieces);
1511 values = g_new (gchar *, len + 1);
1512 for (p = pieces, i = 0; p; p = p->next)
1513 values[i++] = p->data;
1516 g_slist_free (pieces);
1525 * g_key_file_set_string_list:
1526 * @key_file: a #GKeyFile
1527 * @group_name: a group name
1529 * @list: an array of locale string values
1530 * @length: number of locale string values in @list
1532 * Associates a list of string values for @key under @group_name.
1533 * If @key cannot be found then it is created.
1534 * If @group_name cannot be found then it is created.
1539 g_key_file_set_string_list (GKeyFile *key_file,
1540 const gchar *group_name,
1542 const gchar * const list[],
1545 GString *value_list;
1548 g_return_if_fail (key_file != NULL);
1549 g_return_if_fail (list != NULL);
1551 value_list = g_string_sized_new (length * 128);
1552 for (i = 0; i < length && list[i] != NULL; i++)
1556 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1557 g_string_append (value_list, value);
1558 g_string_append_c (value_list, key_file->list_separator);
1563 g_key_file_set_value (key_file, group_name, key, value_list->str);
1564 g_string_free (value_list, TRUE);
1568 * g_key_file_set_locale_string:
1569 * @key_file: a #GKeyFile
1570 * @group_name: a group name
1575 * Associates a string value for @key and @locale under @group_name.
1576 * If the translation for @key cannot be found then it is created.
1581 g_key_file_set_locale_string (GKeyFile *key_file,
1582 const gchar *group_name,
1584 const gchar *locale,
1585 const gchar *string)
1587 gchar *full_key, *value;
1589 g_return_if_fail (key_file != NULL);
1590 g_return_if_fail (key != NULL);
1591 g_return_if_fail (locale != NULL);
1592 g_return_if_fail (string != NULL);
1594 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1595 full_key = g_strdup_printf ("%s[%s]", key, locale);
1596 g_key_file_set_value (key_file, group_name, full_key, value);
1601 extern GSList *_g_compute_locale_variants (const gchar *locale);
1604 * g_key_file_get_locale_string:
1605 * @key_file: a #GKeyFile
1606 * @group_name: a group name
1608 * @locale: a locale or %NULL
1609 * @error: return location for a #GError, or %NULL
1611 * Returns the value associated with @key under @group_name
1612 * translated in the given @locale if available. If @locale is
1613 * %NULL then the current locale is assumed.
1615 * If @key cannot be found then %NULL is returned and @error is set
1616 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1617 * with @key cannot be interpreted or no suitable translation can
1618 * be found then the untranslated value is returned.
1620 * Return value: a newly allocated string or %NULL if the specified
1621 * key cannot be found.
1626 g_key_file_get_locale_string (GKeyFile *key_file,
1627 const gchar *group_name,
1629 const gchar *locale,
1632 gchar *candidate_key, *translated_value;
1633 GError *key_file_error;
1635 gboolean free_languages = FALSE;
1638 g_return_val_if_fail (key_file != NULL, NULL);
1639 g_return_val_if_fail (group_name != NULL, NULL);
1640 g_return_val_if_fail (key != NULL, NULL);
1642 candidate_key = NULL;
1643 translated_value = NULL;
1644 key_file_error = NULL;
1650 list = _g_compute_locale_variants (locale);
1652 languages = g_new (gchar *, g_slist_length (list) + 1);
1653 for (l = list, i = 0; l; l = l->next, i++)
1654 languages[i] = l->data;
1655 languages[i] = NULL;
1657 g_slist_free (list);
1658 free_languages = TRUE;
1662 languages = (gchar **) g_get_language_names ();
1663 free_languages = FALSE;
1666 for (i = 0; languages[i]; i++)
1668 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1670 translated_value = g_key_file_get_string (key_file,
1672 candidate_key, NULL);
1673 g_free (candidate_key);
1675 if (translated_value)
1678 g_free (translated_value);
1679 translated_value = NULL;
1682 /* Fallback to untranslated key
1684 if (!translated_value)
1686 translated_value = g_key_file_get_string (key_file, group_name, key,
1689 if (!translated_value)
1690 g_propagate_error (error, key_file_error);
1694 g_strfreev (languages);
1696 return translated_value;
1700 * g_key_file_get_locale_string_list:
1701 * @key_file: a #GKeyFile
1702 * @group_name: a group name
1705 * @length: return location for the number of returned strings or %NULL
1706 * @error: return location for a #GError or %NULL
1708 * Returns the values associated with @key under @group_name
1709 * translated in the given @locale if available. If @locale is
1710 * %NULL then the current locale is assumed.
1712 * If @key cannot be found then %NULL is returned and @error is set
1713 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1714 * with @key cannot be interpreted or no suitable translations
1715 * can be found then the untranslated values are returned. The
1716 * returned array is %NULL-terminated, so @length may optionally
1719 * Return value: a newly allocated %NULL-terminated string array
1720 * or %NULL if the key isn't found. The string array should be freed
1721 * with g_strfreev().
1726 g_key_file_get_locale_string_list (GKeyFile *key_file,
1727 const gchar *group_name,
1729 const gchar *locale,
1733 GError *key_file_error;
1734 gchar **values, *value;
1736 g_return_val_if_fail (key_file != NULL, NULL);
1737 g_return_val_if_fail (group_name != NULL, NULL);
1738 g_return_val_if_fail (key != NULL, NULL);
1740 key_file_error = NULL;
1742 value = g_key_file_get_locale_string (key_file, group_name,
1747 g_propagate_error (error, key_file_error);
1756 if (value[strlen (value) - 1] == ';')
1757 value[strlen (value) - 1] = '\0';
1759 values = g_strsplit (value, ";", 0);
1764 *length = g_strv_length (values);
1770 * g_key_file_set_locale_string_list:
1771 * @key_file: a #GKeyFile
1772 * @group_name: a group name
1775 * @list: a %NULL-terminated array of locale string values
1776 * @length: the length of @list
1778 * Associates a list of string values for @key and @locale under
1779 * @group_name. If the translation for @key cannot be found then
1785 g_key_file_set_locale_string_list (GKeyFile *key_file,
1786 const gchar *group_name,
1788 const gchar *locale,
1789 const gchar * const list[],
1792 GString *value_list;
1796 g_return_if_fail (key_file != NULL);
1797 g_return_if_fail (key != NULL);
1798 g_return_if_fail (locale != NULL);
1799 g_return_if_fail (length != 0);
1801 value_list = g_string_sized_new (length * 128);
1802 for (i = 0; i < length && list[i] != NULL; i++)
1806 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1808 g_string_append (value_list, value);
1809 g_string_append_c (value_list, ';');
1814 full_key = g_strdup_printf ("%s[%s]", key, locale);
1815 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1817 g_string_free (value_list, TRUE);
1821 * g_key_file_get_boolean:
1822 * @key_file: a #GKeyFile
1823 * @group_name: a group name
1825 * @error: return location for a #GError
1827 * Returns the value associated with @key under @group_name as a
1830 * If @key cannot be found then %FALSE is returned and @error is set
1831 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1832 * associated with @key cannot be interpreted as a boolean then %FALSE
1833 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1835 * Return value: the value associated with the key as a boolean,
1836 * or %FALSE if the key was not found or could not be parsed.
1841 g_key_file_get_boolean (GKeyFile *key_file,
1842 const gchar *group_name,
1846 GError *key_file_error = NULL;
1848 gboolean bool_value;
1850 g_return_val_if_fail (key_file != NULL, FALSE);
1851 g_return_val_if_fail (group_name != NULL, FALSE);
1852 g_return_val_if_fail (key != NULL, FALSE);
1854 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1858 g_propagate_error (error, key_file_error);
1862 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1868 if (g_error_matches (key_file_error,
1870 G_KEY_FILE_ERROR_INVALID_VALUE))
1872 g_set_error (error, G_KEY_FILE_ERROR,
1873 G_KEY_FILE_ERROR_INVALID_VALUE,
1874 _("Key file contains key '%s' "
1875 "which has value that cannot be interpreted."),
1877 g_error_free (key_file_error);
1880 g_propagate_error (error, key_file_error);
1887 * g_key_file_set_boolean:
1888 * @key_file: a #GKeyFile
1889 * @group_name: a group name
1891 * @value: %TRUE or %FALSE
1893 * Associates a new boolean value with @key under @group_name.
1894 * If @key cannot be found then it is created.
1899 g_key_file_set_boolean (GKeyFile *key_file,
1900 const gchar *group_name,
1906 g_return_if_fail (key_file != NULL);
1908 result = g_key_file_parse_boolean_as_value (key_file, value);
1909 g_key_file_set_value (key_file, group_name, key, result);
1914 * g_key_file_get_boolean_list:
1915 * @key_file: a #GKeyFile
1916 * @group_name: a group name
1918 * @length: the number of booleans returned
1919 * @error: return location for a #GError
1921 * Returns the values associated with @key under @group_name as
1922 * booleans. If @group_name is %NULL, the start_group is used.
1924 * If @key cannot be found then %NULL is returned and @error is set to
1925 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1926 * with @key cannot be interpreted as booleans then %NULL is returned
1927 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1929 * Return value: the values associated with the key as a list of
1930 * booleans, or %NULL if the key was not found or could not be parsed.
1935 g_key_file_get_boolean_list (GKeyFile *key_file,
1936 const gchar *group_name,
1941 GError *key_file_error;
1943 gboolean *bool_values;
1946 g_return_val_if_fail (key_file != NULL, NULL);
1947 g_return_val_if_fail (group_name != NULL, NULL);
1948 g_return_val_if_fail (key != NULL, NULL);
1953 key_file_error = NULL;
1955 values = g_key_file_get_string_list (key_file, group_name, key,
1956 &num_bools, &key_file_error);
1959 g_propagate_error (error, key_file_error);
1964 bool_values = g_new (gboolean, num_bools);
1966 for (i = 0; i < num_bools; i++)
1968 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1974 g_propagate_error (error, key_file_error);
1975 g_strfreev (values);
1976 g_free (bool_values);
1981 g_strfreev (values);
1984 *length = num_bools;
1990 * g_key_file_set_boolean_list:
1991 * @key_file: a #GKeyFile
1992 * @group_name: a group name
1994 * @list: an array of boolean values
1995 * @length: length of @list
1997 * Associates a list of boolean values with @key under @group_name.
1998 * If @key cannot be found then it is created.
1999 * If @group_name is %NULL, the start_group is used.
2004 g_key_file_set_boolean_list (GKeyFile *key_file,
2005 const gchar *group_name,
2010 GString *value_list;
2013 g_return_if_fail (key_file != NULL);
2014 g_return_if_fail (list != NULL);
2016 value_list = g_string_sized_new (length * 8);
2017 for (i = 0; i < length; i++)
2021 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2023 g_string_append (value_list, value);
2024 g_string_append_c (value_list, key_file->list_separator);
2029 g_key_file_set_value (key_file, group_name, key, value_list->str);
2030 g_string_free (value_list, TRUE);
2034 * g_key_file_get_integer:
2035 * @key_file: a #GKeyFile
2036 * @group_name: a group name
2038 * @error: return location for a #GError
2040 * Returns the value associated with @key under @group_name as an
2041 * integer. If @group_name is %NULL, the start group is used.
2043 * If @key cannot be found then 0 is returned and @error is set to
2044 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2045 * with @key cannot be interpreted as an integer then 0 is returned
2046 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2048 * Return value: the value associated with the key as an integer, or
2049 * 0 if the key was not found or could not be parsed.
2054 g_key_file_get_integer (GKeyFile *key_file,
2055 const gchar *group_name,
2059 GError *key_file_error;
2063 g_return_val_if_fail (key_file != NULL, -1);
2064 g_return_val_if_fail (group_name != NULL, -1);
2065 g_return_val_if_fail (key != NULL, -1);
2067 key_file_error = NULL;
2069 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2073 g_propagate_error (error, key_file_error);
2077 int_value = g_key_file_parse_value_as_integer (key_file, value,
2083 if (g_error_matches (key_file_error,
2085 G_KEY_FILE_ERROR_INVALID_VALUE))
2087 g_set_error (error, G_KEY_FILE_ERROR,
2088 G_KEY_FILE_ERROR_INVALID_VALUE,
2089 _("Key file contains key '%s' in group '%s' "
2090 "which has value that cannot be interpreted."), key,
2092 g_error_free (key_file_error);
2095 g_propagate_error (error, key_file_error);
2102 * g_key_file_set_integer:
2103 * @key_file: a #GKeyFile
2104 * @group_name: a group name
2106 * @value: an integer value
2108 * Associates a new integer value with @key under @group_name.
2109 * If @key cannot be found then it is created.
2114 g_key_file_set_integer (GKeyFile *key_file,
2115 const gchar *group_name,
2121 g_return_if_fail (key_file != NULL);
2123 result = g_key_file_parse_integer_as_value (key_file, value);
2124 g_key_file_set_value (key_file, group_name, key, result);
2129 * g_key_file_get_integer_list:
2130 * @key_file: a #GKeyFile
2131 * @group_name: a group name
2133 * @length: the number of integers returned
2134 * @error: return location for a #GError
2136 * Returns the values associated with @key under @group_name as
2139 * If @key cannot be found then %NULL is returned and @error is set to
2140 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2141 * with @key cannot be interpreted as integers then %NULL is returned
2142 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2144 * Return value: the values associated with the key as a list of
2145 * integers, or %NULL if the key was not found or could not be parsed.
2150 g_key_file_get_integer_list (GKeyFile *key_file,
2151 const gchar *group_name,
2156 GError *key_file_error = NULL;
2161 g_return_val_if_fail (key_file != NULL, NULL);
2162 g_return_val_if_fail (group_name != NULL, NULL);
2163 g_return_val_if_fail (key != NULL, NULL);
2168 values = g_key_file_get_string_list (key_file, group_name, key,
2169 &num_ints, &key_file_error);
2172 g_propagate_error (error, key_file_error);
2177 int_values = g_new (gint, num_ints);
2179 for (i = 0; i < num_ints; i++)
2181 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2187 g_propagate_error (error, key_file_error);
2188 g_strfreev (values);
2189 g_free (int_values);
2194 g_strfreev (values);
2203 * g_key_file_set_integer_list:
2204 * @key_file: a #GKeyFile
2205 * @group_name: a group name
2207 * @list: an array of integer values
2208 * @length: number of integer values in @list
2210 * Associates a list of integer values with @key under @group_name.
2211 * If @key cannot be found then it is created.
2216 g_key_file_set_integer_list (GKeyFile *key_file,
2217 const gchar *group_name,
2225 g_return_if_fail (key_file != NULL);
2226 g_return_if_fail (list != NULL);
2228 values = g_string_sized_new (length * 16);
2229 for (i = 0; i < length; i++)
2233 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2235 g_string_append (values, value);
2236 g_string_append_c (values, ';');
2241 g_key_file_set_value (key_file, group_name, key, values->str);
2242 g_string_free (values, TRUE);
2246 * g_key_file_get_double:
2247 * @key_file: a #GKeyFile
2248 * @group_name: a group name
2250 * @error: return location for a #GError
2252 * Returns the value associated with @key under @group_name as a
2253 * double. If @group_name is %NULL, the start_group is used.
2255 * If @key cannot be found then 0.0 is returned and @error is set to
2256 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2257 * with @key cannot be interpreted as a double then 0.0 is returned
2258 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2260 * Return value: the value associated with the key as a double, or
2261 * 0.0 if the key was not found or could not be parsed.
2266 g_key_file_get_double (GKeyFile *key_file,
2267 const gchar *group_name,
2271 GError *key_file_error;
2273 gdouble double_value;
2275 g_return_val_if_fail (key_file != NULL, -1);
2276 g_return_val_if_fail (group_name != NULL, -1);
2277 g_return_val_if_fail (key != NULL, -1);
2279 key_file_error = NULL;
2281 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2285 g_propagate_error (error, key_file_error);
2289 double_value = g_key_file_parse_value_as_double (key_file, value,
2295 if (g_error_matches (key_file_error,
2297 G_KEY_FILE_ERROR_INVALID_VALUE))
2299 g_set_error (error, G_KEY_FILE_ERROR,
2300 G_KEY_FILE_ERROR_INVALID_VALUE,
2301 _("Key file contains key '%s' in group '%s' "
2302 "which has value that cannot be interpreted."), key,
2304 g_error_free (key_file_error);
2307 g_propagate_error (error, key_file_error);
2310 return double_value;
2314 * g_key_file_set_double:
2315 * @key_file: a #GKeyFile
2316 * @group_name: a group name
2318 * @value: an double value
2320 * Associates a new double value with @key under @group_name.
2321 * If @key cannot be found then it is created.
2322 * If @group_name is %NULL, the start group is used.
2327 g_key_file_set_double (GKeyFile *key_file,
2328 const gchar *group_name,
2332 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2334 g_return_if_fail (key_file != NULL);
2336 g_ascii_dtostr (result, sizeof (result), value);
2337 g_key_file_set_value (key_file, group_name, key, result);
2341 * g_key_file_get_double_list:
2342 * @key_file: a #GKeyFile
2343 * @group_name: a group name
2345 * @length: the number of doubles returned
2346 * @error: return location for a #GError
2348 * Returns the values associated with @key under @group_name as
2349 * doubles. If @group_name is %NULL, the start group is used.
2351 * If @key cannot be found then %NULL is returned and @error is set to
2352 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2353 * with @key cannot be interpreted as doubles then %NULL is returned
2354 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2356 * Return value: the values associated with the key as a list of
2357 * doubles, or %NULL if the key was not found or could not be parsed.
2362 g_key_file_get_double_list (GKeyFile *key_file,
2363 const gchar *group_name,
2368 GError *key_file_error = NULL;
2370 gdouble *double_values;
2371 gsize i, num_doubles;
2373 g_return_val_if_fail (key_file != NULL, NULL);
2374 g_return_val_if_fail (group_name != NULL, NULL);
2375 g_return_val_if_fail (key != NULL, NULL);
2380 values = g_key_file_get_string_list (key_file, group_name, key,
2381 &num_doubles, &key_file_error);
2384 g_propagate_error (error, key_file_error);
2389 double_values = g_new (gdouble, num_doubles);
2391 for (i = 0; i < num_doubles; i++)
2393 double_values[i] = g_key_file_parse_value_as_double (key_file,
2399 g_propagate_error (error, key_file_error);
2400 g_strfreev (values);
2401 g_free (double_values);
2406 g_strfreev (values);
2409 *length = num_doubles;
2411 return double_values;
2415 * g_key_file_set_double_list:
2416 * @key_file: a #GKeyFile
2417 * @group_name: a group name
2419 * @list: an array of double values
2420 * @length: number of double values in @list
2422 * Associates a list of double values with @key under
2423 * @group_name. If @key cannot be found then it is created.
2424 * If @group_name is %NULL the start group is used.
2429 g_key_file_set_double_list (GKeyFile *key_file,
2430 const gchar *group_name,
2438 g_return_if_fail (key_file != NULL);
2439 g_return_if_fail (list != NULL);
2441 values = g_string_sized_new (length * 16);
2442 for (i = 0; i < length; i++)
2444 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2446 g_ascii_dtostr( result, sizeof (result), list[i] );
2448 g_string_append (values, result);
2449 g_string_append_c (values, ';');
2452 g_key_file_set_value (key_file, group_name, key, values->str);
2453 g_string_free (values, TRUE);
2457 g_key_file_set_key_comment (GKeyFile *key_file,
2458 const gchar *group_name,
2460 const gchar *comment,
2463 GKeyFileGroup *group;
2464 GKeyFileKeyValuePair *pair;
2465 GList *key_node, *comment_node, *tmp;
2467 group = g_key_file_lookup_group (key_file, group_name);
2470 g_set_error (error, G_KEY_FILE_ERROR,
2471 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2472 _("Key file does not have group '%s'"),
2473 group_name ? group_name : "(null)");
2478 /* First find the key the comments are supposed to be
2481 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2483 if (key_node == NULL)
2485 g_set_error (error, G_KEY_FILE_ERROR,
2486 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2487 _("Key file does not have key '%s' in group '%s'"),
2492 /* Then find all the comments already associated with the
2495 tmp = key_node->next;
2498 GKeyFileKeyValuePair *pair;
2500 pair = (GKeyFileKeyValuePair *) tmp->data;
2502 if (pair->key != NULL)
2507 g_key_file_remove_key_value_pair_node (key_file, group,
2511 if (comment == NULL)
2514 /* Now we can add our new comment
2516 pair = g_slice_new (GKeyFileKeyValuePair);
2518 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2520 key_node = g_list_insert (key_node, pair, 1);
2526 g_key_file_set_group_comment (GKeyFile *key_file,
2527 const gchar *group_name,
2528 const gchar *comment,
2531 GKeyFileGroup *group;
2533 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2535 group = g_key_file_lookup_group (key_file, group_name);
2538 g_set_error (error, G_KEY_FILE_ERROR,
2539 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2540 _("Key file does not have group '%s'"),
2541 group_name ? group_name : "(null)");
2546 /* First remove any existing comment
2550 g_key_file_key_value_pair_free (group->comment);
2551 group->comment = NULL;
2554 if (comment == NULL)
2557 /* Now we can add our new comment
2559 group->comment = g_slice_new (GKeyFileKeyValuePair);
2560 group->comment->key = NULL;
2561 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2567 g_key_file_set_top_comment (GKeyFile *key_file,
2568 const gchar *comment,
2572 GKeyFileGroup *group;
2573 GKeyFileKeyValuePair *pair;
2575 /* The last group in the list should be the top (comments only)
2578 g_assert (key_file->groups != NULL);
2579 group_node = g_list_last (key_file->groups);
2580 group = (GKeyFileGroup *) group_node->data;
2581 g_assert (group->name == NULL);
2583 /* Note all keys must be comments at the top of
2584 * the file, so we can just free it all.
2586 if (group->key_value_pairs != NULL)
2588 g_list_foreach (group->key_value_pairs,
2589 (GFunc) g_key_file_key_value_pair_free,
2591 g_list_free (group->key_value_pairs);
2592 group->key_value_pairs = NULL;
2595 if (comment == NULL)
2598 pair = g_slice_new (GKeyFileKeyValuePair);
2600 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2602 group->key_value_pairs =
2603 g_list_prepend (group->key_value_pairs, pair);
2609 * g_key_file_set_comment:
2610 * @key_file: a #GKeyFile
2611 * @group_name: a group name, or %NULL
2613 * @comment: a comment
2614 * @error: return location for a #GError
2616 * Places a comment above @key from @group_name.
2617 * If @key is %NULL then @comment will be written above @group_name.
2618 * If both @key and @group_name are %NULL, then @comment will be
2619 * written above the first group in the file.
2621 * Returns: %TRUE if the comment was written, %FALSE otherwise
2626 g_key_file_set_comment (GKeyFile *key_file,
2627 const gchar *group_name,
2629 const gchar *comment,
2632 g_return_val_if_fail (key_file != NULL, FALSE);
2634 if (group_name != NULL && key != NULL)
2636 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2639 else if (group_name != NULL)
2641 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2646 if (!g_key_file_set_top_comment (key_file, comment, error))
2650 if (comment != NULL)
2651 key_file->approximate_size += strlen (comment);
2657 g_key_file_get_key_comment (GKeyFile *key_file,
2658 const gchar *group_name,
2662 GKeyFileGroup *group;
2663 GKeyFileKeyValuePair *pair;
2664 GList *key_node, *tmp;
2668 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2670 group = g_key_file_lookup_group (key_file, group_name);
2673 g_set_error (error, G_KEY_FILE_ERROR,
2674 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2675 _("Key file does not have group '%s'"),
2676 group_name ? group_name : "(null)");
2681 /* First find the key the comments are supposed to be
2684 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2686 if (key_node == NULL)
2688 g_set_error (error, G_KEY_FILE_ERROR,
2689 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2690 _("Key file does not have key '%s' in group '%s'"),
2697 /* Then find all the comments already associated with the
2698 * key and concatentate them.
2700 tmp = key_node->next;
2701 if (!key_node->next)
2704 pair = (GKeyFileKeyValuePair *) tmp->data;
2705 if (pair->key != NULL)
2710 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2712 if (pair->key != NULL)
2718 while (tmp != key_node)
2720 GKeyFileKeyValuePair *pair;
2722 pair = (GKeyFileKeyValuePair *) tmp->data;
2725 string = g_string_sized_new (512);
2727 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2728 g_string_append (string, comment);
2736 comment = string->str;
2737 g_string_free (string, FALSE);
2746 get_group_comment (GKeyFile *key_file,
2747 GKeyFileGroup *group,
2756 tmp = group->key_value_pairs;
2759 GKeyFileKeyValuePair *pair;
2761 pair = (GKeyFileKeyValuePair *) tmp->data;
2763 if (pair->key != NULL)
2769 if (tmp->next == NULL)
2777 GKeyFileKeyValuePair *pair;
2779 pair = (GKeyFileKeyValuePair *) tmp->data;
2782 string = g_string_sized_new (512);
2784 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2785 g_string_append (string, comment);
2792 return g_string_free (string, FALSE);
2798 g_key_file_get_group_comment (GKeyFile *key_file,
2799 const gchar *group_name,
2803 GKeyFileGroup *group;
2805 group_node = g_key_file_lookup_group_node (key_file, group_name);
2808 g_set_error (error, G_KEY_FILE_ERROR,
2809 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2810 _("Key file does not have group '%s'"),
2811 group_name ? group_name : "(null)");
2816 group = (GKeyFileGroup *)group_node->data;
2818 return g_strdup (group->comment->value);
2820 group_node = group_node->next;
2821 group = (GKeyFileGroup *)group_node->data;
2822 return get_group_comment (key_file, group, error);
2826 g_key_file_get_top_comment (GKeyFile *key_file,
2830 GKeyFileGroup *group;
2832 /* The last group in the list should be the top (comments only)
2835 g_assert (key_file->groups != NULL);
2836 group_node = g_list_last (key_file->groups);
2837 group = (GKeyFileGroup *) group_node->data;
2838 g_assert (group->name == NULL);
2840 return get_group_comment (key_file, group, error);
2844 * g_key_file_get_comment:
2845 * @key_file: a #GKeyFile
2846 * @group_name: a group name, or %NULL
2848 * @error: return location for a #GError
2850 * Retrieves a comment above @key from @group_name.
2851 * If @key is %NULL then @comment will be read from above
2852 * @group_name. If both @key and @group_name are %NULL, then
2853 * @comment will be read from above the first group in the file.
2855 * Returns: a comment that should be freed with g_free()
2860 g_key_file_get_comment (GKeyFile *key_file,
2861 const gchar *group_name,
2865 g_return_val_if_fail (key_file != NULL, NULL);
2867 if (group_name != NULL && key != NULL)
2868 return g_key_file_get_key_comment (key_file, group_name, key, error);
2869 else if (group_name != NULL)
2870 return g_key_file_get_group_comment (key_file, group_name, error);
2872 return g_key_file_get_top_comment (key_file, error);
2876 * g_key_file_remove_comment:
2877 * @key_file: a #GKeyFile
2878 * @group_name: a group name, or %NULL
2880 * @error: return location for a #GError
2882 * Removes a comment above @key from @group_name.
2883 * If @key is %NULL then @comment will be removed above @group_name.
2884 * If both @key and @group_name are %NULL, then @comment will
2885 * be removed above the first group in the file.
2887 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2893 g_key_file_remove_comment (GKeyFile *key_file,
2894 const gchar *group_name,
2898 g_return_val_if_fail (key_file != NULL, FALSE);
2900 if (group_name != NULL && key != NULL)
2901 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2902 else if (group_name != NULL)
2903 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2905 return g_key_file_set_top_comment (key_file, NULL, error);
2909 * g_key_file_has_group:
2910 * @key_file: a #GKeyFile
2911 * @group_name: a group name
2913 * Looks whether the key file has the group @group_name.
2915 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2920 g_key_file_has_group (GKeyFile *key_file,
2921 const gchar *group_name)
2923 g_return_val_if_fail (key_file != NULL, FALSE);
2924 g_return_val_if_fail (group_name != NULL, FALSE);
2926 return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2930 * g_key_file_has_key:
2931 * @key_file: a #GKeyFile
2932 * @group_name: a group name
2934 * @error: return location for a #GError
2936 * Looks whether the key file has the key @key in the group
2939 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2945 g_key_file_has_key (GKeyFile *key_file,
2946 const gchar *group_name,
2950 GKeyFileKeyValuePair *pair;
2951 GKeyFileGroup *group;
2953 g_return_val_if_fail (key_file != NULL, FALSE);
2954 g_return_val_if_fail (group_name != NULL, FALSE);
2955 g_return_val_if_fail (key != NULL, FALSE);
2957 group = g_key_file_lookup_group (key_file, group_name);
2961 g_set_error (error, G_KEY_FILE_ERROR,
2962 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2963 _("Key file does not have group '%s'"),
2964 group_name ? group_name : "(null)");
2969 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2971 return pair != NULL;
2975 g_key_file_add_group (GKeyFile *key_file,
2976 const gchar *group_name)
2978 GKeyFileGroup *group;
2980 g_return_if_fail (key_file != NULL);
2981 g_return_if_fail (g_key_file_is_group_name (group_name));
2983 group = g_key_file_lookup_group (key_file, group_name);
2986 key_file->current_group = group;
2990 group = g_slice_new0 (GKeyFileGroup);
2991 group->name = g_strdup (group_name);
2992 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2993 key_file->groups = g_list_prepend (key_file->groups, group);
2994 key_file->approximate_size += strlen (group_name) + 3;
2995 key_file->current_group = group;
2997 if (key_file->start_group == NULL)
2998 key_file->start_group = group;
3002 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3007 g_free (pair->value);
3008 g_slice_free (GKeyFileKeyValuePair, pair);
3012 /* Be careful not to call this function on a node with data in the
3013 * lookup map without removing it from the lookup map, first.
3015 * Some current cases where this warning is not a concern are
3017 * - the node being removed is a comment node
3018 * - the entire lookup map is getting destroyed soon after
3022 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3023 GKeyFileGroup *group,
3027 GKeyFileKeyValuePair *pair;
3029 pair = (GKeyFileKeyValuePair *) pair_node->data;
3031 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3033 if (pair->key != NULL)
3034 key_file->approximate_size -= strlen (pair->key) + 1;
3036 g_assert (pair->value != NULL);
3037 key_file->approximate_size -= strlen (pair->value);
3039 g_key_file_key_value_pair_free (pair);
3041 g_list_free_1 (pair_node);
3045 g_key_file_remove_group_node (GKeyFile *key_file,
3048 GKeyFileGroup *group;
3051 group = (GKeyFileGroup *) group_node->data;
3053 /* If the current group gets deleted make the current group the last
3056 if (key_file->current_group == group)
3058 /* groups should always contain at least the top comment group,
3059 * unless g_key_file_clear has been called
3061 if (key_file->groups)
3062 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3064 key_file->current_group = NULL;
3067 /* If the start group gets deleted make the start group the first
3070 if (key_file->start_group == group)
3072 tmp = g_list_last (key_file->groups);
3075 if (tmp != group_node &&
3076 ((GKeyFileGroup *) tmp->data)->name != NULL)
3083 key_file->start_group = (GKeyFileGroup *) tmp->data;
3085 key_file->start_group = NULL;
3088 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3090 if (group->name != NULL)
3091 key_file->approximate_size -= strlen (group->name) + 3;
3093 tmp = group->key_value_pairs;
3100 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3103 g_assert (group->key_value_pairs == NULL);
3105 if (group->lookup_map)
3107 g_hash_table_destroy (group->lookup_map);
3108 group->lookup_map = NULL;
3111 g_free ((gchar *) group->name);
3112 g_slice_free (GKeyFileGroup, group);
3113 g_list_free_1 (group_node);
3117 * g_key_file_remove_group:
3118 * @key_file: a #GKeyFile
3119 * @group_name: a group name
3120 * @error: return location for a #GError or %NULL
3122 * Removes the specified group, @group_name,
3123 * from the key file.
3125 * Returns: %TRUE if the group was removed, %FALSE otherwise
3130 g_key_file_remove_group (GKeyFile *key_file,
3131 const gchar *group_name,
3136 g_return_val_if_fail (key_file != NULL, FALSE);
3137 g_return_val_if_fail (group_name != NULL, FALSE);
3139 group_node = g_key_file_lookup_group_node (key_file, group_name);
3143 g_set_error (error, G_KEY_FILE_ERROR,
3144 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3145 _("Key file does not have group '%s'"),
3150 g_key_file_remove_group_node (key_file, group_node);
3156 g_key_file_add_key (GKeyFile *key_file,
3157 GKeyFileGroup *group,
3161 GKeyFileKeyValuePair *pair;
3163 pair = g_slice_new (GKeyFileKeyValuePair);
3164 pair->key = g_strdup (key);
3165 pair->value = g_strdup (value);
3167 g_hash_table_replace (group->lookup_map, pair->key, pair);
3168 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3169 group->has_trailing_blank_line = FALSE;
3170 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3174 * g_key_file_remove_key:
3175 * @key_file: a #GKeyFile
3176 * @group_name: a group name
3177 * @key: a key name to remove
3178 * @error: return location for a #GError or %NULL
3180 * Removes @key in @group_name from the key file.
3182 * Returns: %TRUE if the key was removed, %FALSE otherwise
3187 g_key_file_remove_key (GKeyFile *key_file,
3188 const gchar *group_name,
3192 GKeyFileGroup *group;
3193 GKeyFileKeyValuePair *pair;
3195 g_return_val_if_fail (key_file != NULL, FALSE);
3196 g_return_val_if_fail (group_name != NULL, FALSE);
3197 g_return_val_if_fail (key != NULL, FALSE);
3201 group = g_key_file_lookup_group (key_file, group_name);
3204 g_set_error (error, G_KEY_FILE_ERROR,
3205 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3206 _("Key file does not have group '%s'"),
3207 group_name ? group_name : "(null)");
3211 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3215 g_set_error (error, G_KEY_FILE_ERROR,
3216 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3217 _("Key file does not have key '%s' in group '%s'"),
3222 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3224 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3225 g_hash_table_remove (group->lookup_map, pair->key);
3226 g_key_file_key_value_pair_free (pair);
3232 g_key_file_lookup_group_node (GKeyFile *key_file,
3233 const gchar *group_name)
3235 GKeyFileGroup *group;
3238 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3240 group = (GKeyFileGroup *) tmp->data;
3242 if (group && group->name && strcmp (group->name, group_name) == 0)
3249 static GKeyFileGroup *
3250 g_key_file_lookup_group (GKeyFile *key_file,
3251 const gchar *group_name)
3255 group_node = g_key_file_lookup_group_node (key_file, group_name);
3257 if (group_node != NULL)
3258 return (GKeyFileGroup *) group_node->data;
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"