1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
5 * Written by Ray Strode <rstrode@redhat.com>
6 * Matthias Clasen <mclasen@redhat.com>
8 * GLib is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * GLib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with GLib; see the file COPYING.LIB. If not,
20 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
34 #include <sys/types.h>
43 #define S_ISREG(mode) ((mode)&_S_IFREG)
46 #endif /* G_OS_WIN23 */
51 #include "gfileutils.h"
57 #include "gmessages.h"
60 #include "gstrfuncs.h"
65 typedef struct _GKeyFileGroup GKeyFileGroup;
70 GHashTable *group_hash;
72 GKeyFileGroup *start_group;
73 GKeyFileGroup *current_group;
75 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
77 /* Used for sizing the output buffer during serialization
79 gsize approximate_size;
88 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
92 const gchar *name; /* NULL for above first group (which will be comments) */
94 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
95 gboolean has_trailing_blank_line;
97 GList *key_value_pairs;
99 /* Used in parallel with key_value_pairs for
100 * increased lookup performance
102 GHashTable *lookup_map;
105 struct _GKeyFileKeyValuePair
107 gchar *key; /* NULL for comments */
111 static gint find_file_in_data_dirs (const gchar *file,
112 const gchar **data_dirs,
115 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
119 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
120 const gchar *group_name);
121 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
122 const gchar *group_name);
124 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
125 GKeyFileGroup *group,
127 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
128 GKeyFileGroup *group,
131 static void g_key_file_remove_group_node (GKeyFile *key_file,
133 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
134 GKeyFileGroup *group,
137 static void g_key_file_add_key (GKeyFile *key_file,
138 GKeyFileGroup *group,
141 static void g_key_file_add_group (GKeyFile *key_file,
142 const gchar *group_name);
143 static gboolean g_key_file_is_group_name (const gchar *name);
144 static gboolean g_key_file_is_key_name (const gchar *name);
145 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
146 static gboolean g_key_file_line_is_comment (const gchar *line);
147 static gboolean g_key_file_line_is_group (const gchar *line);
148 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
149 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
153 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
155 gboolean escape_separator);
156 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
159 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
161 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
164 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
167 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
169 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
171 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
172 const gchar *comment);
173 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
177 static void g_key_file_parse_comment (GKeyFile *key_file,
181 static void g_key_file_parse_group (GKeyFile *key_file,
185 static gchar *key_get_locale (const gchar *key);
186 static void g_key_file_parse_data (GKeyFile *key_file,
190 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
195 g_key_file_error_quark (void)
197 return g_quark_from_static_string ("g-key-file-error-quark");
201 g_key_file_init (GKeyFile *key_file)
203 key_file->current_group = g_slice_new0 (GKeyFileGroup);
204 key_file->groups = g_list_prepend (NULL, key_file->current_group);
205 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
206 key_file->start_group = NULL;
207 key_file->parse_buffer = g_string_sized_new (128);
208 key_file->approximate_size = 0;
209 key_file->list_separator = ';';
211 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
215 g_key_file_clear (GKeyFile *key_file)
217 GList *tmp, *group_node;
219 if (key_file->locales)
221 g_strfreev (key_file->locales);
222 key_file->locales = NULL;
225 if (key_file->parse_buffer)
227 g_string_free (key_file->parse_buffer, TRUE);
228 key_file->parse_buffer = NULL;
231 tmp = key_file->groups;
236 g_key_file_remove_group_node (key_file, group_node);
239 g_warn_if_fail (key_file->groups == NULL);
246 * Creates a new empty #GKeyFile object. Use
247 * g_key_file_load_from_file(), g_key_file_load_from_data(),
248 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
249 * read an existing key file.
251 * Return value: an empty #GKeyFile.
256 g_key_file_new (void)
260 key_file = g_slice_new0 (GKeyFile);
261 g_key_file_init (key_file);
267 * g_key_file_set_list_separator:
268 * @key_file: a #GKeyFile
269 * @separator: the separator
271 * Sets the character which is used to separate
272 * values in lists. Typically ';' or ',' are used
273 * as separators. The default list separator is ';'.
278 g_key_file_set_list_separator (GKeyFile *key_file,
281 g_return_if_fail (key_file != NULL);
283 key_file->list_separator = separator;
287 /* Iterates through all the directories in *dirs trying to
288 * open file. When it successfully locates and opens a file it
289 * returns the file descriptor to the open file. It also
290 * outputs the absolute path of the file in output_file.
293 find_file_in_data_dirs (const gchar *file,
298 const gchar **data_dirs, *data_dir;
310 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
312 gchar *candidate_file, *sub_dir;
314 candidate_file = (gchar *) file;
315 sub_dir = g_strdup ("");
316 while (candidate_file != NULL && fd < 0)
320 path = g_build_filename (data_dir, sub_dir,
321 candidate_file, NULL);
323 fd = g_open (path, O_RDONLY, 0);
331 candidate_file = strchr (candidate_file, '-');
333 if (candidate_file == NULL)
339 sub_dir = g_strndup (file, candidate_file - file - 1);
341 for (p = sub_dir; *p != '\0'; p++)
344 *p = G_DIR_SEPARATOR;
353 g_set_error (error, G_KEY_FILE_ERROR,
354 G_KEY_FILE_ERROR_NOT_FOUND,
355 _("Valid key file could not be "
356 "found in search dirs"));
359 if (output_file != NULL && fd > 0)
360 *output_file = g_strdup (path);
368 g_key_file_load_from_fd (GKeyFile *key_file,
373 GError *key_file_error = NULL;
375 struct stat stat_buf;
376 gchar read_buf[4096];
378 if (fstat (fd, &stat_buf) < 0)
380 g_set_error (error, G_FILE_ERROR,
381 g_file_error_from_errno (errno),
382 "%s", g_strerror (errno));
386 if (!S_ISREG (stat_buf.st_mode))
388 g_set_error (error, G_KEY_FILE_ERROR,
389 G_KEY_FILE_ERROR_PARSE,
390 _("Not a regular file"));
394 if (stat_buf.st_size == 0)
396 g_set_error (error, G_KEY_FILE_ERROR,
397 G_KEY_FILE_ERROR_PARSE,
402 if (key_file->approximate_size > 0)
404 g_key_file_clear (key_file);
405 g_key_file_init (key_file);
407 key_file->flags = flags;
412 bytes_read = read (fd, read_buf, 4096);
414 if (bytes_read == 0) /* End of File */
419 if (errno == EINTR || errno == EAGAIN)
422 g_set_error (error, G_FILE_ERROR,
423 g_file_error_from_errno (errno),
424 "%s", g_strerror (errno));
428 g_key_file_parse_data (key_file,
429 read_buf, bytes_read,
432 while (!key_file_error);
436 g_propagate_error (error, key_file_error);
440 g_key_file_flush_parse_buffer (key_file, &key_file_error);
444 g_propagate_error (error, key_file_error);
452 * g_key_file_load_from_file:
453 * @key_file: an empty #GKeyFile struct
454 * @file: the path of a filename to load, in the GLib filename encoding
455 * @flags: flags from #GKeyFileFlags
456 * @error: return location for a #GError, or %NULL
458 * Loads a key file into an empty #GKeyFile structure.
459 * If the file could not be loaded then %error is set to
460 * either a #GFileError or #GKeyFileError.
462 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
467 g_key_file_load_from_file (GKeyFile *key_file,
472 GError *key_file_error = NULL;
475 g_return_val_if_fail (key_file != NULL, FALSE);
476 g_return_val_if_fail (file != NULL, FALSE);
478 fd = g_open (file, O_RDONLY, 0);
482 g_set_error (error, G_FILE_ERROR,
483 g_file_error_from_errno (errno),
484 "%s", g_strerror (errno));
488 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
493 g_propagate_error (error, key_file_error);
501 * g_key_file_load_from_data:
502 * @key_file: an empty #GKeyFile struct
503 * @data: key file loaded in memory
504 * @length: the length of @data in bytes
505 * @flags: flags from #GKeyFileFlags
506 * @error: return location for a #GError, or %NULL
508 * Loads a key file from memory into an empty #GKeyFile structure.
509 * If the object cannot be created then %error is set to a #GKeyFileError.
511 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
516 g_key_file_load_from_data (GKeyFile *key_file,
522 GError *key_file_error = NULL;
524 g_return_val_if_fail (key_file != NULL, FALSE);
525 g_return_val_if_fail (data != NULL, FALSE);
526 g_return_val_if_fail (length != 0, FALSE);
528 if (length == (gsize)-1)
529 length = strlen (data);
531 if (key_file->approximate_size > 0)
533 g_key_file_clear (key_file);
534 g_key_file_init (key_file);
536 key_file->flags = flags;
538 g_key_file_parse_data (key_file, data, length, &key_file_error);
542 g_propagate_error (error, key_file_error);
546 g_key_file_flush_parse_buffer (key_file, &key_file_error);
550 g_propagate_error (error, key_file_error);
558 * g_key_file_load_from_dirs:
559 * @key_file: an empty #GKeyFile struct
560 * @file: a relative path to a filename to open and parse
561 * @search_dirs: %NULL-terminated array of directories to search
562 * @full_path: return location for a string containing the full path
563 * of the file, or %NULL
564 * @flags: flags from #GKeyFileFlags
565 * @error: return location for a #GError, or %NULL
567 * This function looks for a key file named @file in the paths
568 * specified in @search_dirs, loads the file into @key_file and
569 * returns the file's full path in @full_path. If the file could not
570 * be loaded then an %error is set to either a #GFileError or
573 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
578 g_key_file_load_from_dirs (GKeyFile *key_file,
580 const gchar **search_dirs,
585 GError *key_file_error = NULL;
586 const gchar **data_dirs;
591 g_return_val_if_fail (key_file != NULL, FALSE);
592 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
593 g_return_val_if_fail (search_dirs != NULL, FALSE);
596 data_dirs = search_dirs;
598 while (*data_dirs != NULL && !found_file)
600 g_free (output_path);
602 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
608 g_propagate_error (error, key_file_error);
612 found_file = g_key_file_load_from_fd (key_file, fd, flags,
618 g_propagate_error (error, key_file_error);
623 if (found_file && full_path)
624 *full_path = output_path;
626 g_free (output_path);
632 * g_key_file_load_from_data_dirs:
633 * @key_file: an empty #GKeyFile struct
634 * @file: a relative path to a filename to open and parse
635 * @full_path: return location for a string containing the full path
636 * of the file, or %NULL
637 * @flags: flags from #GKeyFileFlags
638 * @error: return location for a #GError, or %NULL
640 * This function looks for a key file named @file in the paths
641 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
642 * loads the file into @key_file and returns the file's full path in
643 * @full_path. If the file could not be loaded then an %error is
644 * set to either a #GFileError or #GKeyFileError.
646 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
650 g_key_file_load_from_data_dirs (GKeyFile *key_file,
656 gchar **all_data_dirs;
657 const gchar * user_data_dir;
658 const gchar * const * system_data_dirs;
662 g_return_val_if_fail (key_file != NULL, FALSE);
663 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
665 user_data_dir = g_get_user_data_dir ();
666 system_data_dirs = g_get_system_data_dirs ();
667 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
670 all_data_dirs[i++] = g_strdup (user_data_dir);
673 while (system_data_dirs[j] != NULL)
674 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
675 all_data_dirs[i] = NULL;
677 found_file = g_key_file_load_from_dirs (key_file,
679 (const gchar **)all_data_dirs,
684 g_strfreev (all_data_dirs);
691 * @key_file: a #GKeyFile
698 g_key_file_free (GKeyFile *key_file)
700 g_return_if_fail (key_file != NULL);
702 g_key_file_clear (key_file);
703 g_slice_free (GKeyFile, key_file);
706 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
707 * true for locales that match those in g_get_language_names().
710 g_key_file_locale_is_interesting (GKeyFile *key_file,
715 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
718 for (i = 0; key_file->locales[i] != NULL; i++)
720 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
728 g_key_file_parse_line (GKeyFile *key_file,
733 GError *parse_error = NULL;
736 g_return_if_fail (key_file != NULL);
737 g_return_if_fail (line != NULL);
739 line_start = (gchar *) line;
740 while (g_ascii_isspace (*line_start))
743 if (g_key_file_line_is_comment (line_start))
744 g_key_file_parse_comment (key_file, line, length, &parse_error);
745 else if (g_key_file_line_is_group (line_start))
746 g_key_file_parse_group (key_file, line_start,
747 length - (line_start - line),
749 else if (g_key_file_line_is_key_value_pair (line_start))
750 g_key_file_parse_key_value_pair (key_file, line_start,
751 length - (line_start - line),
755 gchar *line_utf8 = _g_utf8_make_valid (line);
756 g_set_error (error, G_KEY_FILE_ERROR,
757 G_KEY_FILE_ERROR_PARSE,
758 _("Key file contains line '%s' which is not "
759 "a key-value pair, group, or comment"),
767 g_propagate_error (error, parse_error);
771 g_key_file_parse_comment (GKeyFile *key_file,
776 GKeyFileKeyValuePair *pair;
778 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
781 g_warn_if_fail (key_file->current_group != NULL);
783 pair = g_slice_new (GKeyFileKeyValuePair);
785 pair->value = g_strndup (line, length);
787 key_file->current_group->key_value_pairs =
788 g_list_prepend (key_file->current_group->key_value_pairs, pair);
790 if (length == 0 || line[0] != '#')
791 key_file->current_group->has_trailing_blank_line = TRUE;
795 g_key_file_parse_group (GKeyFile *key_file,
801 const gchar *group_name_start, *group_name_end;
803 /* advance past opening '['
805 group_name_start = line + 1;
806 group_name_end = line + length - 1;
808 while (*group_name_end != ']')
811 group_name = g_strndup (group_name_start,
812 group_name_end - group_name_start);
814 if (!g_key_file_is_group_name (group_name))
816 g_set_error (error, G_KEY_FILE_ERROR,
817 G_KEY_FILE_ERROR_PARSE,
818 _("Invalid group name: %s"), group_name);
823 g_key_file_add_group (key_file, group_name);
828 g_key_file_parse_key_value_pair (GKeyFile *key_file,
833 gchar *key, *value, *key_end, *value_start, *locale;
834 gsize key_len, value_len;
836 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
838 g_set_error (error, G_KEY_FILE_ERROR,
839 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
840 _("Key file does not start with a group"));
844 key_end = value_start = strchr (line, '=');
846 g_warn_if_fail (key_end != NULL);
851 /* Pull the key name from the line (chomping trailing whitespace)
853 while (g_ascii_isspace (*key_end))
856 key_len = key_end - line + 2;
858 g_warn_if_fail (key_len <= length);
860 key = g_strndup (line, key_len - 1);
862 if (!g_key_file_is_key_name (key))
864 g_set_error (error, G_KEY_FILE_ERROR,
865 G_KEY_FILE_ERROR_PARSE,
866 _("Invalid key name: %s"), key);
871 /* Pull the value from the line (chugging leading whitespace)
873 while (g_ascii_isspace (*value_start))
876 value_len = line + length - value_start + 1;
878 value = g_strndup (value_start, value_len);
880 g_warn_if_fail (key_file->start_group != NULL);
882 if (key_file->current_group
883 && key_file->current_group->name
884 && strcmp (key_file->start_group->name,
885 key_file->current_group->name) == 0
886 && strcmp (key, "Encoding") == 0)
888 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
890 gchar *value_utf8 = _g_utf8_make_valid (value);
891 g_set_error (error, G_KEY_FILE_ERROR,
892 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
893 _("Key file contains unsupported "
894 "encoding '%s'"), value_utf8);
903 /* Is this key a translation? If so, is it one that we care about?
905 locale = key_get_locale (key);
907 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
908 g_key_file_add_key (key_file, key_file->current_group, key, value);
916 key_get_locale (const gchar *key)
920 locale = g_strrstr (key, "[");
922 if (locale && strlen (locale) <= 2)
926 locale = g_strndup (locale + 1, strlen (locale) - 2);
932 g_key_file_parse_data (GKeyFile *key_file,
940 g_return_if_fail (key_file != NULL);
941 g_return_if_fail (data != NULL);
945 for (i = 0; i < length; i++)
949 if (i > 0 && data[i - 1] == '\r')
950 g_string_erase (key_file->parse_buffer,
951 key_file->parse_buffer->len - 1,
954 /* When a newline is encountered flush the parse buffer so that the
955 * line can be parsed. Note that completely blank lines won't show
956 * up in the parse buffer, so they get parsed directly.
958 if (key_file->parse_buffer->len > 0)
959 g_key_file_flush_parse_buffer (key_file, &parse_error);
961 g_key_file_parse_comment (key_file, "", 1, &parse_error);
965 g_propagate_error (error, parse_error);
970 g_string_append_c (key_file->parse_buffer, data[i]);
973 key_file->approximate_size += length;
977 g_key_file_flush_parse_buffer (GKeyFile *key_file,
980 GError *file_error = NULL;
982 g_return_if_fail (key_file != NULL);
986 if (key_file->parse_buffer->len > 0)
988 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
989 key_file->parse_buffer->len,
991 g_string_erase (key_file->parse_buffer, 0, -1);
995 g_propagate_error (error, file_error);
1002 * g_key_file_to_data:
1003 * @key_file: a #GKeyFile
1004 * @length: return location for the length of the
1005 * returned string, or %NULL
1006 * @error: return location for a #GError, or %NULL
1008 * This function outputs @key_file as a string.
1010 * Return value: a newly allocated string holding
1011 * the contents of the #GKeyFile
1016 g_key_file_to_data (GKeyFile *key_file,
1020 GString *data_string;
1021 GList *group_node, *key_file_node;
1022 gboolean has_blank_line = TRUE;
1024 g_return_val_if_fail (key_file != NULL, NULL);
1026 data_string = g_string_sized_new (2 * key_file->approximate_size);
1028 for (group_node = g_list_last (key_file->groups);
1030 group_node = group_node->prev)
1032 GKeyFileGroup *group;
1034 group = (GKeyFileGroup *) group_node->data;
1036 /* separate groups by at least an empty line */
1037 if (!has_blank_line)
1038 g_string_append_c (data_string, '\n');
1039 has_blank_line = group->has_trailing_blank_line;
1041 if (group->comment != NULL)
1042 g_string_append_printf (data_string, "%s\n", group->comment->value);
1044 if (group->name != NULL)
1045 g_string_append_printf (data_string, "[%s]\n", group->name);
1047 for (key_file_node = g_list_last (group->key_value_pairs);
1048 key_file_node != NULL;
1049 key_file_node = key_file_node->prev)
1051 GKeyFileKeyValuePair *pair;
1053 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1055 if (pair->key != NULL)
1056 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1058 g_string_append_printf (data_string, "%s\n", pair->value);
1063 *length = data_string->len;
1065 return g_string_free (data_string, FALSE);
1069 * g_key_file_get_keys:
1070 * @key_file: a #GKeyFile
1071 * @group_name: a group name
1072 * @length: return location for the number of keys returned, or %NULL
1073 * @error: return location for a #GError, or %NULL
1075 * Returns all keys for the group name @group_name. The array of
1076 * returned keys will be %NULL-terminated, so @length may
1077 * optionally be %NULL. In the event that the @group_name cannot
1078 * be found, %NULL is returned and @error is set to
1079 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1081 * Return value: a newly-allocated %NULL-terminated array of strings.
1082 * Use g_strfreev() to free it.
1087 g_key_file_get_keys (GKeyFile *key_file,
1088 const gchar *group_name,
1092 GKeyFileGroup *group;
1097 g_return_val_if_fail (key_file != NULL, NULL);
1098 g_return_val_if_fail (group_name != NULL, NULL);
1100 group = g_key_file_lookup_group (key_file, group_name);
1104 g_set_error (error, G_KEY_FILE_ERROR,
1105 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1106 _("Key file does not have group '%s'"),
1107 group_name ? group_name : "(null)");
1112 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1114 GKeyFileKeyValuePair *pair;
1116 pair = (GKeyFileKeyValuePair *) tmp->data;
1122 keys = g_new (gchar *, num_keys + 1);
1125 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1127 GKeyFileKeyValuePair *pair;
1129 pair = (GKeyFileKeyValuePair *) tmp->data;
1133 keys[i] = g_strdup (pair->key);
1138 keys[num_keys] = NULL;
1147 * g_key_file_get_start_group:
1148 * @key_file: a #GKeyFile
1150 * Returns the name of the start group of the file.
1152 * Return value: The start group of the key file.
1157 g_key_file_get_start_group (GKeyFile *key_file)
1159 g_return_val_if_fail (key_file != NULL, NULL);
1161 if (key_file->start_group)
1162 return g_strdup (key_file->start_group->name);
1168 * g_key_file_get_groups:
1169 * @key_file: a #GKeyFile
1170 * @length: return location for the number of returned groups, or %NULL
1172 * Returns all groups in the key file loaded with @key_file.
1173 * The array of returned groups will be %NULL-terminated, so
1174 * @length may optionally be %NULL.
1176 * Return value: a newly-allocated %NULL-terminated array of strings.
1177 * Use g_strfreev() to free it.
1181 g_key_file_get_groups (GKeyFile *key_file,
1186 gsize i, num_groups;
1188 g_return_val_if_fail (key_file != NULL, NULL);
1190 num_groups = g_list_length (key_file->groups);
1192 g_return_val_if_fail (num_groups > 0, NULL);
1194 group_node = g_list_last (key_file->groups);
1196 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1198 /* Only need num_groups instead of num_groups + 1
1199 * because the first group of the file (last in the
1200 * list) is always the comment group at the top,
1203 groups = g_new (gchar *, num_groups);
1207 for (group_node = group_node->prev;
1209 group_node = group_node->prev)
1211 GKeyFileGroup *group;
1213 group = (GKeyFileGroup *) group_node->data;
1215 g_warn_if_fail (group->name != NULL);
1217 groups[i++] = g_strdup (group->name);
1228 * g_key_file_get_value:
1229 * @key_file: a #GKeyFile
1230 * @group_name: a group name
1232 * @error: return location for a #GError, or %NULL
1234 * Returns the value associated with @key under @group_name.
1236 * In the event the key cannot be found, %NULL is returned and
1237 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1238 * event that the @group_name cannot be found, %NULL is returned
1239 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1241 * Return value: a newly allocated string or %NULL if the specified
1242 * key cannot be found.
1247 g_key_file_get_value (GKeyFile *key_file,
1248 const gchar *group_name,
1252 GKeyFileGroup *group;
1253 GKeyFileKeyValuePair *pair;
1254 gchar *value = NULL;
1256 g_return_val_if_fail (key_file != NULL, NULL);
1257 g_return_val_if_fail (group_name != NULL, NULL);
1258 g_return_val_if_fail (key != NULL, NULL);
1260 group = g_key_file_lookup_group (key_file, group_name);
1264 g_set_error (error, G_KEY_FILE_ERROR,
1265 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1266 _("Key file does not have group '%s'"),
1267 group_name ? group_name : "(null)");
1271 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1274 value = g_strdup (pair->value);
1276 g_set_error (error, G_KEY_FILE_ERROR,
1277 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1278 _("Key file does not have key '%s'"), key);
1284 * g_key_file_set_value:
1285 * @key_file: a #GKeyFile
1286 * @group_name: a group name
1290 * Associates a new value with @key under @group_name.
1291 * If @key cannot be found then it is created.
1292 * If @group_name cannot be found then it is created.
1297 g_key_file_set_value (GKeyFile *key_file,
1298 const gchar *group_name,
1302 GKeyFileGroup *group;
1303 GKeyFileKeyValuePair *pair;
1305 g_return_if_fail (key_file != NULL);
1306 g_return_if_fail (g_key_file_is_group_name (group_name));
1307 g_return_if_fail (g_key_file_is_key_name (key));
1308 g_return_if_fail (value != NULL);
1310 group = g_key_file_lookup_group (key_file, group_name);
1314 g_key_file_add_group (key_file, group_name);
1315 group = (GKeyFileGroup *) key_file->groups->data;
1317 g_key_file_add_key (key_file, group, key, value);
1321 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1324 g_key_file_add_key (key_file, group, key, value);
1327 g_free (pair->value);
1328 pair->value = g_strdup (value);
1334 * g_key_file_get_string:
1335 * @key_file: a #GKeyFile
1336 * @group_name: a group name
1338 * @error: return location for a #GError, or %NULL
1340 * Returns the value associated with @key under @group_name.
1342 * In the event the key cannot be found, %NULL is returned and
1343 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1344 * event that the @group_name cannot be found, %NULL is returned
1345 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1347 * Return value: a newly allocated string or %NULL if the specified
1348 * key cannot be found.
1353 g_key_file_get_string (GKeyFile *key_file,
1354 const gchar *group_name,
1358 gchar *value, *string_value;
1359 GError *key_file_error;
1361 g_return_val_if_fail (key_file != NULL, NULL);
1362 g_return_val_if_fail (group_name != NULL, NULL);
1363 g_return_val_if_fail (key != NULL, NULL);
1365 key_file_error = NULL;
1367 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1371 g_propagate_error (error, key_file_error);
1375 if (!g_utf8_validate (value, -1, NULL))
1377 gchar *value_utf8 = _g_utf8_make_valid (value);
1378 g_set_error (error, G_KEY_FILE_ERROR,
1379 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1380 _("Key file contains key '%s' with value '%s' "
1381 "which is not UTF-8"), key, value_utf8);
1382 g_free (value_utf8);
1388 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1394 if (g_error_matches (key_file_error,
1396 G_KEY_FILE_ERROR_INVALID_VALUE))
1398 g_set_error (error, G_KEY_FILE_ERROR,
1399 G_KEY_FILE_ERROR_INVALID_VALUE,
1400 _("Key file contains key '%s' "
1401 "which has value that cannot be interpreted."),
1403 g_error_free (key_file_error);
1406 g_propagate_error (error, key_file_error);
1409 return string_value;
1413 * g_key_file_set_string:
1414 * @key_file: a #GKeyFile
1415 * @group_name: a group name
1419 * Associates a new string value with @key under @group_name.
1420 * If @key cannot be found then it is created.
1421 * If @group_name cannot be found then it is created.
1426 g_key_file_set_string (GKeyFile *key_file,
1427 const gchar *group_name,
1429 const gchar *string)
1433 g_return_if_fail (key_file != NULL);
1434 g_return_if_fail (string != NULL);
1436 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1437 g_key_file_set_value (key_file, group_name, key, value);
1442 * g_key_file_get_string_list:
1443 * @key_file: a #GKeyFile
1444 * @group_name: a group name
1446 * @length: return location for the number of returned strings, or %NULL
1447 * @error: return location for a #GError, or %NULL
1449 * Returns the values associated with @key under @group_name.
1451 * In the event the key cannot be found, %NULL is returned and
1452 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1453 * event that the @group_name cannot be found, %NULL is returned
1454 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1456 * Return value: a %NULL-terminated string array or %NULL if the specified
1457 * key cannot be found. The array should be freed with g_strfreev().
1462 g_key_file_get_string_list (GKeyFile *key_file,
1463 const gchar *group_name,
1468 GError *key_file_error = NULL;
1469 gchar *value, *string_value, **values;
1471 GSList *p, *pieces = NULL;
1473 g_return_val_if_fail (key_file != NULL, NULL);
1474 g_return_val_if_fail (group_name != NULL, NULL);
1475 g_return_val_if_fail (key != NULL, NULL);
1480 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1484 g_propagate_error (error, key_file_error);
1488 if (!g_utf8_validate (value, -1, NULL))
1490 gchar *value_utf8 = _g_utf8_make_valid (value);
1491 g_set_error (error, G_KEY_FILE_ERROR,
1492 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1493 _("Key file contains key '%s' with value '%s' "
1494 "which is not UTF-8"), key, value_utf8);
1495 g_free (value_utf8);
1501 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1503 g_free (string_value);
1507 if (g_error_matches (key_file_error,
1509 G_KEY_FILE_ERROR_INVALID_VALUE))
1511 g_set_error (error, G_KEY_FILE_ERROR,
1512 G_KEY_FILE_ERROR_INVALID_VALUE,
1513 _("Key file contains key '%s' "
1514 "which has value that cannot be interpreted."),
1516 g_error_free (key_file_error);
1519 g_propagate_error (error, key_file_error);
1522 len = g_slist_length (pieces);
1523 values = g_new (gchar *, len + 1);
1524 for (p = pieces, i = 0; p; p = p->next)
1525 values[i++] = p->data;
1528 g_slist_free (pieces);
1537 * g_key_file_set_string_list:
1538 * @key_file: a #GKeyFile
1539 * @group_name: a group name
1541 * @list: an array of locale string values
1542 * @length: number of locale string values in @list
1544 * Associates a list of string values for @key under @group_name.
1545 * If @key cannot be found then it is created.
1546 * If @group_name cannot be found then it is created.
1551 g_key_file_set_string_list (GKeyFile *key_file,
1552 const gchar *group_name,
1554 const gchar * const list[],
1557 GString *value_list;
1560 g_return_if_fail (key_file != NULL);
1561 g_return_if_fail (list != NULL);
1563 value_list = g_string_sized_new (length * 128);
1564 for (i = 0; i < length && list[i] != NULL; i++)
1568 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1569 g_string_append (value_list, value);
1570 g_string_append_c (value_list, key_file->list_separator);
1575 g_key_file_set_value (key_file, group_name, key, value_list->str);
1576 g_string_free (value_list, TRUE);
1580 * g_key_file_set_locale_string:
1581 * @key_file: a #GKeyFile
1582 * @group_name: a group name
1587 * Associates a string value for @key and @locale under @group_name.
1588 * If the translation for @key cannot be found then it is created.
1593 g_key_file_set_locale_string (GKeyFile *key_file,
1594 const gchar *group_name,
1596 const gchar *locale,
1597 const gchar *string)
1599 gchar *full_key, *value;
1601 g_return_if_fail (key_file != NULL);
1602 g_return_if_fail (key != NULL);
1603 g_return_if_fail (locale != NULL);
1604 g_return_if_fail (string != NULL);
1606 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1607 full_key = g_strdup_printf ("%s[%s]", key, locale);
1608 g_key_file_set_value (key_file, group_name, full_key, value);
1613 extern GSList *_g_compute_locale_variants (const gchar *locale);
1616 * g_key_file_get_locale_string:
1617 * @key_file: a #GKeyFile
1618 * @group_name: a group name
1620 * @locale: a locale or %NULL
1621 * @error: return location for a #GError, or %NULL
1623 * Returns the value associated with @key under @group_name
1624 * translated in the given @locale if available. If @locale is
1625 * %NULL then the current locale is assumed.
1627 * If @key cannot be found then %NULL is returned and @error is set
1628 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1629 * with @key cannot be interpreted or no suitable translation can
1630 * be found then the untranslated value is returned.
1632 * Return value: a newly allocated string or %NULL if the specified
1633 * key cannot be found.
1638 g_key_file_get_locale_string (GKeyFile *key_file,
1639 const gchar *group_name,
1641 const gchar *locale,
1644 gchar *candidate_key, *translated_value;
1645 GError *key_file_error;
1647 gboolean free_languages = FALSE;
1650 g_return_val_if_fail (key_file != NULL, NULL);
1651 g_return_val_if_fail (group_name != NULL, NULL);
1652 g_return_val_if_fail (key != NULL, NULL);
1654 candidate_key = NULL;
1655 translated_value = NULL;
1656 key_file_error = NULL;
1662 list = _g_compute_locale_variants (locale);
1664 languages = g_new (gchar *, g_slist_length (list) + 1);
1665 for (l = list, i = 0; l; l = l->next, i++)
1666 languages[i] = l->data;
1667 languages[i] = NULL;
1669 g_slist_free (list);
1670 free_languages = TRUE;
1674 languages = (gchar **) g_get_language_names ();
1675 free_languages = FALSE;
1678 for (i = 0; languages[i]; i++)
1680 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1682 translated_value = g_key_file_get_string (key_file,
1684 candidate_key, NULL);
1685 g_free (candidate_key);
1687 if (translated_value)
1690 g_free (translated_value);
1691 translated_value = NULL;
1694 /* Fallback to untranslated key
1696 if (!translated_value)
1698 translated_value = g_key_file_get_string (key_file, group_name, key,
1701 if (!translated_value)
1702 g_propagate_error (error, key_file_error);
1706 g_strfreev (languages);
1708 return translated_value;
1712 * g_key_file_get_locale_string_list:
1713 * @key_file: a #GKeyFile
1714 * @group_name: a group name
1717 * @length: return location for the number of returned strings or %NULL
1718 * @error: return location for a #GError or %NULL
1720 * Returns the values associated with @key under @group_name
1721 * translated in the given @locale if available. If @locale is
1722 * %NULL then the current locale is assumed.
1724 * If @key cannot be found then %NULL is returned and @error is set
1725 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1726 * with @key cannot be interpreted or no suitable translations
1727 * can be found then the untranslated values are returned. The
1728 * returned array is %NULL-terminated, so @length may optionally
1731 * Return value: a newly allocated %NULL-terminated string array
1732 * or %NULL if the key isn't found. The string array should be freed
1733 * with g_strfreev().
1738 g_key_file_get_locale_string_list (GKeyFile *key_file,
1739 const gchar *group_name,
1741 const gchar *locale,
1745 GError *key_file_error;
1746 gchar **values, *value;
1748 g_return_val_if_fail (key_file != NULL, NULL);
1749 g_return_val_if_fail (group_name != NULL, NULL);
1750 g_return_val_if_fail (key != NULL, NULL);
1752 key_file_error = NULL;
1754 value = g_key_file_get_locale_string (key_file, group_name,
1759 g_propagate_error (error, key_file_error);
1768 if (value[strlen (value) - 1] == ';')
1769 value[strlen (value) - 1] = '\0';
1771 values = g_strsplit (value, ";", 0);
1776 *length = g_strv_length (values);
1782 * g_key_file_set_locale_string_list:
1783 * @key_file: a #GKeyFile
1784 * @group_name: a group name
1787 * @list: a %NULL-terminated array of locale string values
1788 * @length: the length of @list
1790 * Associates a list of string values for @key and @locale under
1791 * @group_name. If the translation for @key cannot be found then
1797 g_key_file_set_locale_string_list (GKeyFile *key_file,
1798 const gchar *group_name,
1800 const gchar *locale,
1801 const gchar * const list[],
1804 GString *value_list;
1808 g_return_if_fail (key_file != NULL);
1809 g_return_if_fail (key != NULL);
1810 g_return_if_fail (locale != NULL);
1811 g_return_if_fail (length != 0);
1813 value_list = g_string_sized_new (length * 128);
1814 for (i = 0; i < length && list[i] != NULL; i++)
1818 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1820 g_string_append (value_list, value);
1821 g_string_append_c (value_list, ';');
1826 full_key = g_strdup_printf ("%s[%s]", key, locale);
1827 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1829 g_string_free (value_list, TRUE);
1833 * g_key_file_get_boolean:
1834 * @key_file: a #GKeyFile
1835 * @group_name: a group name
1837 * @error: return location for a #GError
1839 * Returns the value associated with @key under @group_name as a
1842 * If @key cannot be found then %FALSE is returned and @error is set
1843 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1844 * associated with @key cannot be interpreted as a boolean then %FALSE
1845 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1847 * Return value: the value associated with the key as a boolean,
1848 * or %FALSE if the key was not found or could not be parsed.
1853 g_key_file_get_boolean (GKeyFile *key_file,
1854 const gchar *group_name,
1858 GError *key_file_error = NULL;
1860 gboolean bool_value;
1862 g_return_val_if_fail (key_file != NULL, FALSE);
1863 g_return_val_if_fail (group_name != NULL, FALSE);
1864 g_return_val_if_fail (key != NULL, FALSE);
1866 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1870 g_propagate_error (error, key_file_error);
1874 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1880 if (g_error_matches (key_file_error,
1882 G_KEY_FILE_ERROR_INVALID_VALUE))
1884 g_set_error (error, G_KEY_FILE_ERROR,
1885 G_KEY_FILE_ERROR_INVALID_VALUE,
1886 _("Key file contains key '%s' "
1887 "which has value that cannot be interpreted."),
1889 g_error_free (key_file_error);
1892 g_propagate_error (error, key_file_error);
1899 * g_key_file_set_boolean:
1900 * @key_file: a #GKeyFile
1901 * @group_name: a group name
1903 * @value: %TRUE or %FALSE
1905 * Associates a new boolean value with @key under @group_name.
1906 * If @key cannot be found then it is created.
1911 g_key_file_set_boolean (GKeyFile *key_file,
1912 const gchar *group_name,
1918 g_return_if_fail (key_file != NULL);
1920 result = g_key_file_parse_boolean_as_value (key_file, value);
1921 g_key_file_set_value (key_file, group_name, key, result);
1926 * g_key_file_get_boolean_list:
1927 * @key_file: a #GKeyFile
1928 * @group_name: a group name
1930 * @length: the number of booleans returned
1931 * @error: return location for a #GError
1933 * Returns the values associated with @key under @group_name as
1934 * booleans. If @group_name is %NULL, the start_group is used.
1936 * If @key cannot be found then %NULL is returned and @error is set to
1937 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1938 * with @key cannot be interpreted as booleans then %NULL is returned
1939 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1941 * Return value: the values associated with the key as a list of
1942 * booleans, or %NULL if the key was not found or could not be parsed.
1947 g_key_file_get_boolean_list (GKeyFile *key_file,
1948 const gchar *group_name,
1953 GError *key_file_error;
1955 gboolean *bool_values;
1958 g_return_val_if_fail (key_file != NULL, NULL);
1959 g_return_val_if_fail (group_name != NULL, NULL);
1960 g_return_val_if_fail (key != NULL, NULL);
1965 key_file_error = NULL;
1967 values = g_key_file_get_string_list (key_file, group_name, key,
1968 &num_bools, &key_file_error);
1971 g_propagate_error (error, key_file_error);
1976 bool_values = g_new (gboolean, num_bools);
1978 for (i = 0; i < num_bools; i++)
1980 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1986 g_propagate_error (error, key_file_error);
1987 g_strfreev (values);
1988 g_free (bool_values);
1993 g_strfreev (values);
1996 *length = num_bools;
2002 * g_key_file_set_boolean_list:
2003 * @key_file: a #GKeyFile
2004 * @group_name: a group name
2006 * @list: an array of boolean values
2007 * @length: length of @list
2009 * Associates a list of boolean values with @key under @group_name.
2010 * If @key cannot be found then it is created.
2011 * If @group_name is %NULL, the start_group is used.
2016 g_key_file_set_boolean_list (GKeyFile *key_file,
2017 const gchar *group_name,
2022 GString *value_list;
2025 g_return_if_fail (key_file != NULL);
2026 g_return_if_fail (list != NULL);
2028 value_list = g_string_sized_new (length * 8);
2029 for (i = 0; i < length; i++)
2033 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2035 g_string_append (value_list, value);
2036 g_string_append_c (value_list, key_file->list_separator);
2041 g_key_file_set_value (key_file, group_name, key, value_list->str);
2042 g_string_free (value_list, TRUE);
2046 * g_key_file_get_integer:
2047 * @key_file: a #GKeyFile
2048 * @group_name: a group name
2050 * @error: return location for a #GError
2052 * Returns the value associated with @key under @group_name as an
2053 * integer. If @group_name is %NULL, the start group is used.
2055 * If @key cannot be found then 0 is returned and @error is set to
2056 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2057 * with @key cannot be interpreted as an integer then 0 is returned
2058 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2060 * Return value: the value associated with the key as an integer, or
2061 * 0 if the key was not found or could not be parsed.
2066 g_key_file_get_integer (GKeyFile *key_file,
2067 const gchar *group_name,
2071 GError *key_file_error;
2075 g_return_val_if_fail (key_file != NULL, -1);
2076 g_return_val_if_fail (group_name != NULL, -1);
2077 g_return_val_if_fail (key != NULL, -1);
2079 key_file_error = NULL;
2081 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2085 g_propagate_error (error, key_file_error);
2089 int_value = g_key_file_parse_value_as_integer (key_file, value,
2095 if (g_error_matches (key_file_error,
2097 G_KEY_FILE_ERROR_INVALID_VALUE))
2099 g_set_error (error, G_KEY_FILE_ERROR,
2100 G_KEY_FILE_ERROR_INVALID_VALUE,
2101 _("Key file contains key '%s' in group '%s' "
2102 "which has value that cannot be interpreted."), key,
2104 g_error_free (key_file_error);
2107 g_propagate_error (error, key_file_error);
2114 * g_key_file_set_integer:
2115 * @key_file: a #GKeyFile
2116 * @group_name: a group name
2118 * @value: an integer value
2120 * Associates a new integer value with @key under @group_name.
2121 * If @key cannot be found then it is created.
2126 g_key_file_set_integer (GKeyFile *key_file,
2127 const gchar *group_name,
2133 g_return_if_fail (key_file != NULL);
2135 result = g_key_file_parse_integer_as_value (key_file, value);
2136 g_key_file_set_value (key_file, group_name, key, result);
2141 * g_key_file_get_integer_list:
2142 * @key_file: a #GKeyFile
2143 * @group_name: a group name
2145 * @length: the number of integers returned
2146 * @error: return location for a #GError
2148 * Returns the values associated with @key under @group_name as
2151 * If @key cannot be found then %NULL is returned and @error is set to
2152 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2153 * with @key cannot be interpreted as integers then %NULL is returned
2154 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2156 * Return value: the values associated with the key as a list of
2157 * integers, or %NULL if the key was not found or could not be parsed.
2162 g_key_file_get_integer_list (GKeyFile *key_file,
2163 const gchar *group_name,
2168 GError *key_file_error = NULL;
2173 g_return_val_if_fail (key_file != NULL, NULL);
2174 g_return_val_if_fail (group_name != NULL, NULL);
2175 g_return_val_if_fail (key != NULL, NULL);
2180 values = g_key_file_get_string_list (key_file, group_name, key,
2181 &num_ints, &key_file_error);
2184 g_propagate_error (error, key_file_error);
2189 int_values = g_new (gint, num_ints);
2191 for (i = 0; i < num_ints; i++)
2193 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2199 g_propagate_error (error, key_file_error);
2200 g_strfreev (values);
2201 g_free (int_values);
2206 g_strfreev (values);
2215 * g_key_file_set_integer_list:
2216 * @key_file: a #GKeyFile
2217 * @group_name: a group name
2219 * @list: an array of integer values
2220 * @length: number of integer values in @list
2222 * Associates a list of integer values with @key under @group_name.
2223 * If @key cannot be found then it is created.
2228 g_key_file_set_integer_list (GKeyFile *key_file,
2229 const gchar *group_name,
2237 g_return_if_fail (key_file != NULL);
2238 g_return_if_fail (list != NULL);
2240 values = g_string_sized_new (length * 16);
2241 for (i = 0; i < length; i++)
2245 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2247 g_string_append (values, value);
2248 g_string_append_c (values, ';');
2253 g_key_file_set_value (key_file, group_name, key, values->str);
2254 g_string_free (values, TRUE);
2258 * g_key_file_get_double:
2259 * @key_file: a #GKeyFile
2260 * @group_name: a group name
2262 * @error: return location for a #GError
2264 * Returns the value associated with @key under @group_name as a
2265 * double. If @group_name is %NULL, the start_group is used.
2267 * If @key cannot be found then 0.0 is returned and @error is set to
2268 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2269 * with @key cannot be interpreted as a double then 0.0 is returned
2270 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2272 * Return value: the value associated with the key as a double, or
2273 * 0.0 if the key was not found or could not be parsed.
2278 g_key_file_get_double (GKeyFile *key_file,
2279 const gchar *group_name,
2283 GError *key_file_error;
2285 gdouble double_value;
2287 g_return_val_if_fail (key_file != NULL, -1);
2288 g_return_val_if_fail (group_name != NULL, -1);
2289 g_return_val_if_fail (key != NULL, -1);
2291 key_file_error = NULL;
2293 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2297 g_propagate_error (error, key_file_error);
2301 double_value = g_key_file_parse_value_as_double (key_file, value,
2307 if (g_error_matches (key_file_error,
2309 G_KEY_FILE_ERROR_INVALID_VALUE))
2311 g_set_error (error, G_KEY_FILE_ERROR,
2312 G_KEY_FILE_ERROR_INVALID_VALUE,
2313 _("Key file contains key '%s' in group '%s' "
2314 "which has value that cannot be interpreted."), key,
2316 g_error_free (key_file_error);
2319 g_propagate_error (error, key_file_error);
2322 return double_value;
2326 * g_key_file_set_double:
2327 * @key_file: a #GKeyFile
2328 * @group_name: a group name
2330 * @value: an double value
2332 * Associates a new double value with @key under @group_name.
2333 * If @key cannot be found then it is created.
2334 * If @group_name is %NULL, the start group is used.
2339 g_key_file_set_double (GKeyFile *key_file,
2340 const gchar *group_name,
2344 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2346 g_return_if_fail (key_file != NULL);
2348 g_ascii_dtostr (result, sizeof (result), value);
2349 g_key_file_set_value (key_file, group_name, key, result);
2353 * g_key_file_get_double_list:
2354 * @key_file: a #GKeyFile
2355 * @group_name: a group name
2357 * @length: the number of doubles returned
2358 * @error: return location for a #GError
2360 * Returns the values associated with @key under @group_name as
2361 * doubles. If @group_name is %NULL, the start group is used.
2363 * If @key cannot be found then %NULL is returned and @error is set to
2364 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2365 * with @key cannot be interpreted as doubles then %NULL is returned
2366 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2368 * Return value: the values associated with the key as a list of
2369 * doubles, or %NULL if the key was not found or could not be parsed.
2374 g_key_file_get_double_list (GKeyFile *key_file,
2375 const gchar *group_name,
2380 GError *key_file_error = NULL;
2382 gdouble *double_values;
2383 gsize i, num_doubles;
2385 g_return_val_if_fail (key_file != NULL, NULL);
2386 g_return_val_if_fail (group_name != NULL, NULL);
2387 g_return_val_if_fail (key != NULL, NULL);
2392 values = g_key_file_get_string_list (key_file, group_name, key,
2393 &num_doubles, &key_file_error);
2396 g_propagate_error (error, key_file_error);
2401 double_values = g_new (gdouble, num_doubles);
2403 for (i = 0; i < num_doubles; i++)
2405 double_values[i] = g_key_file_parse_value_as_double (key_file,
2411 g_propagate_error (error, key_file_error);
2412 g_strfreev (values);
2413 g_free (double_values);
2418 g_strfreev (values);
2421 *length = num_doubles;
2423 return double_values;
2427 * g_key_file_set_double_list:
2428 * @key_file: a #GKeyFile
2429 * @group_name: a group name
2431 * @list: an array of double values
2432 * @length: number of double values in @list
2434 * Associates a list of double values with @key under
2435 * @group_name. If @key cannot be found then it is created.
2436 * If @group_name is %NULL the start group is used.
2441 g_key_file_set_double_list (GKeyFile *key_file,
2442 const gchar *group_name,
2450 g_return_if_fail (key_file != NULL);
2451 g_return_if_fail (list != NULL);
2453 values = g_string_sized_new (length * 16);
2454 for (i = 0; i < length; i++)
2456 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2458 g_ascii_dtostr( result, sizeof (result), list[i] );
2460 g_string_append (values, result);
2461 g_string_append_c (values, ';');
2464 g_key_file_set_value (key_file, group_name, key, values->str);
2465 g_string_free (values, TRUE);
2469 g_key_file_set_key_comment (GKeyFile *key_file,
2470 const gchar *group_name,
2472 const gchar *comment,
2475 GKeyFileGroup *group;
2476 GKeyFileKeyValuePair *pair;
2477 GList *key_node, *comment_node, *tmp;
2479 group = g_key_file_lookup_group (key_file, group_name);
2482 g_set_error (error, G_KEY_FILE_ERROR,
2483 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2484 _("Key file does not have group '%s'"),
2485 group_name ? group_name : "(null)");
2490 /* First find the key the comments are supposed to be
2493 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2495 if (key_node == NULL)
2497 g_set_error (error, G_KEY_FILE_ERROR,
2498 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2499 _("Key file does not have key '%s' in group '%s'"),
2504 /* Then find all the comments already associated with the
2507 tmp = key_node->next;
2510 GKeyFileKeyValuePair *pair;
2512 pair = (GKeyFileKeyValuePair *) tmp->data;
2514 if (pair->key != NULL)
2519 g_key_file_remove_key_value_pair_node (key_file, group,
2523 if (comment == NULL)
2526 /* Now we can add our new comment
2528 pair = g_slice_new (GKeyFileKeyValuePair);
2530 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2532 key_node = g_list_insert (key_node, pair, 1);
2538 g_key_file_set_group_comment (GKeyFile *key_file,
2539 const gchar *group_name,
2540 const gchar *comment,
2543 GKeyFileGroup *group;
2545 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2547 group = g_key_file_lookup_group (key_file, group_name);
2550 g_set_error (error, G_KEY_FILE_ERROR,
2551 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2552 _("Key file does not have group '%s'"),
2553 group_name ? group_name : "(null)");
2558 /* First remove any existing comment
2562 g_key_file_key_value_pair_free (group->comment);
2563 group->comment = NULL;
2566 if (comment == NULL)
2569 /* Now we can add our new comment
2571 group->comment = g_slice_new (GKeyFileKeyValuePair);
2572 group->comment->key = NULL;
2573 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2579 g_key_file_set_top_comment (GKeyFile *key_file,
2580 const gchar *comment,
2584 GKeyFileGroup *group;
2585 GKeyFileKeyValuePair *pair;
2587 /* The last group in the list should be the top (comments only)
2590 g_warn_if_fail (key_file->groups != NULL);
2591 group_node = g_list_last (key_file->groups);
2592 group = (GKeyFileGroup *) group_node->data;
2593 g_warn_if_fail (group->name == NULL);
2595 /* Note all keys must be comments at the top of
2596 * the file, so we can just free it all.
2598 if (group->key_value_pairs != NULL)
2600 g_list_foreach (group->key_value_pairs,
2601 (GFunc) g_key_file_key_value_pair_free,
2603 g_list_free (group->key_value_pairs);
2604 group->key_value_pairs = NULL;
2607 if (comment == NULL)
2610 pair = g_slice_new (GKeyFileKeyValuePair);
2612 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2614 group->key_value_pairs =
2615 g_list_prepend (group->key_value_pairs, pair);
2621 * g_key_file_set_comment:
2622 * @key_file: a #GKeyFile
2623 * @group_name: a group name, or %NULL
2625 * @comment: a comment
2626 * @error: return location for a #GError
2628 * Places a comment above @key from @group_name.
2629 * If @key is %NULL then @comment will be written above @group_name.
2630 * If both @key and @group_name are %NULL, then @comment will be
2631 * written above the first group in the file.
2633 * Returns: %TRUE if the comment was written, %FALSE otherwise
2638 g_key_file_set_comment (GKeyFile *key_file,
2639 const gchar *group_name,
2641 const gchar *comment,
2644 g_return_val_if_fail (key_file != NULL, FALSE);
2646 if (group_name != NULL && key != NULL)
2648 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2651 else if (group_name != NULL)
2653 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2658 if (!g_key_file_set_top_comment (key_file, comment, error))
2662 if (comment != NULL)
2663 key_file->approximate_size += strlen (comment);
2669 g_key_file_get_key_comment (GKeyFile *key_file,
2670 const gchar *group_name,
2674 GKeyFileGroup *group;
2675 GKeyFileKeyValuePair *pair;
2676 GList *key_node, *tmp;
2680 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2682 group = g_key_file_lookup_group (key_file, group_name);
2685 g_set_error (error, G_KEY_FILE_ERROR,
2686 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2687 _("Key file does not have group '%s'"),
2688 group_name ? group_name : "(null)");
2693 /* First find the key the comments are supposed to be
2696 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2698 if (key_node == NULL)
2700 g_set_error (error, G_KEY_FILE_ERROR,
2701 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2702 _("Key file does not have key '%s' in group '%s'"),
2709 /* Then find all the comments already associated with the
2710 * key and concatentate them.
2712 tmp = key_node->next;
2713 if (!key_node->next)
2716 pair = (GKeyFileKeyValuePair *) tmp->data;
2717 if (pair->key != NULL)
2722 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2724 if (pair->key != NULL)
2730 while (tmp != key_node)
2732 GKeyFileKeyValuePair *pair;
2734 pair = (GKeyFileKeyValuePair *) tmp->data;
2737 string = g_string_sized_new (512);
2739 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2740 g_string_append (string, comment);
2748 comment = string->str;
2749 g_string_free (string, FALSE);
2758 get_group_comment (GKeyFile *key_file,
2759 GKeyFileGroup *group,
2768 tmp = group->key_value_pairs;
2771 GKeyFileKeyValuePair *pair;
2773 pair = (GKeyFileKeyValuePair *) tmp->data;
2775 if (pair->key != NULL)
2781 if (tmp->next == NULL)
2789 GKeyFileKeyValuePair *pair;
2791 pair = (GKeyFileKeyValuePair *) tmp->data;
2794 string = g_string_sized_new (512);
2796 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2797 g_string_append (string, comment);
2804 return g_string_free (string, FALSE);
2810 g_key_file_get_group_comment (GKeyFile *key_file,
2811 const gchar *group_name,
2815 GKeyFileGroup *group;
2817 group = g_key_file_lookup_group (key_file, group_name);
2820 g_set_error (error, G_KEY_FILE_ERROR,
2821 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2822 _("Key file does not have group '%s'"),
2823 group_name ? group_name : "(null)");
2829 return g_strdup (group->comment->value);
2831 group_node = g_key_file_lookup_group_node (key_file, group_name);
2832 group_node = group_node->next;
2833 group = (GKeyFileGroup *)group_node->data;
2834 return get_group_comment (key_file, group, error);
2838 g_key_file_get_top_comment (GKeyFile *key_file,
2842 GKeyFileGroup *group;
2844 /* The last group in the list should be the top (comments only)
2847 g_warn_if_fail (key_file->groups != NULL);
2848 group_node = g_list_last (key_file->groups);
2849 group = (GKeyFileGroup *) group_node->data;
2850 g_warn_if_fail (group->name == NULL);
2852 return get_group_comment (key_file, group, error);
2856 * g_key_file_get_comment:
2857 * @key_file: a #GKeyFile
2858 * @group_name: a group name, or %NULL
2860 * @error: return location for a #GError
2862 * Retrieves a comment above @key from @group_name.
2863 * If @key is %NULL then @comment will be read from above
2864 * @group_name. If both @key and @group_name are %NULL, then
2865 * @comment will be read from above the first group in the file.
2867 * Returns: a comment that should be freed with g_free()
2872 g_key_file_get_comment (GKeyFile *key_file,
2873 const gchar *group_name,
2877 g_return_val_if_fail (key_file != NULL, NULL);
2879 if (group_name != NULL && key != NULL)
2880 return g_key_file_get_key_comment (key_file, group_name, key, error);
2881 else if (group_name != NULL)
2882 return g_key_file_get_group_comment (key_file, group_name, error);
2884 return g_key_file_get_top_comment (key_file, error);
2888 * g_key_file_remove_comment:
2889 * @key_file: a #GKeyFile
2890 * @group_name: a group name, or %NULL
2892 * @error: return location for a #GError
2894 * Removes a comment above @key from @group_name.
2895 * If @key is %NULL then @comment will be removed above @group_name.
2896 * If both @key and @group_name are %NULL, then @comment will
2897 * be removed above the first group in the file.
2899 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2905 g_key_file_remove_comment (GKeyFile *key_file,
2906 const gchar *group_name,
2910 g_return_val_if_fail (key_file != NULL, FALSE);
2912 if (group_name != NULL && key != NULL)
2913 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2914 else if (group_name != NULL)
2915 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2917 return g_key_file_set_top_comment (key_file, NULL, error);
2921 * g_key_file_has_group:
2922 * @key_file: a #GKeyFile
2923 * @group_name: a group name
2925 * Looks whether the key file has the group @group_name.
2927 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2932 g_key_file_has_group (GKeyFile *key_file,
2933 const gchar *group_name)
2935 g_return_val_if_fail (key_file != NULL, FALSE);
2936 g_return_val_if_fail (group_name != NULL, FALSE);
2938 return g_key_file_lookup_group (key_file, group_name) != NULL;
2942 * g_key_file_has_key:
2943 * @key_file: a #GKeyFile
2944 * @group_name: a group name
2946 * @error: return location for a #GError
2948 * Looks whether the key file has the key @key in the group
2951 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2957 g_key_file_has_key (GKeyFile *key_file,
2958 const gchar *group_name,
2962 GKeyFileKeyValuePair *pair;
2963 GKeyFileGroup *group;
2965 g_return_val_if_fail (key_file != NULL, FALSE);
2966 g_return_val_if_fail (group_name != NULL, FALSE);
2967 g_return_val_if_fail (key != NULL, FALSE);
2969 group = g_key_file_lookup_group (key_file, group_name);
2973 g_set_error (error, G_KEY_FILE_ERROR,
2974 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2975 _("Key file does not have group '%s'"),
2976 group_name ? group_name : "(null)");
2981 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2983 return pair != NULL;
2987 g_key_file_add_group (GKeyFile *key_file,
2988 const gchar *group_name)
2990 GKeyFileGroup *group;
2992 g_return_if_fail (key_file != NULL);
2993 g_return_if_fail (g_key_file_is_group_name (group_name));
2995 group = g_key_file_lookup_group (key_file, group_name);
2998 key_file->current_group = group;
3002 group = g_slice_new0 (GKeyFileGroup);
3003 group->name = g_strdup (group_name);
3004 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3005 key_file->groups = g_list_prepend (key_file->groups, group);
3006 key_file->approximate_size += strlen (group_name) + 3;
3007 key_file->current_group = group;
3009 if (key_file->start_group == NULL)
3010 key_file->start_group = group;
3012 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3016 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3021 g_free (pair->value);
3022 g_slice_free (GKeyFileKeyValuePair, pair);
3026 /* Be careful not to call this function on a node with data in the
3027 * lookup map without removing it from the lookup map, first.
3029 * Some current cases where this warning is not a concern are
3031 * - the node being removed is a comment node
3032 * - the entire lookup map is getting destroyed soon after
3036 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3037 GKeyFileGroup *group,
3041 GKeyFileKeyValuePair *pair;
3043 pair = (GKeyFileKeyValuePair *) pair_node->data;
3045 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3047 if (pair->key != NULL)
3048 key_file->approximate_size -= strlen (pair->key) + 1;
3050 g_warn_if_fail (pair->value != NULL);
3051 key_file->approximate_size -= strlen (pair->value);
3053 g_key_file_key_value_pair_free (pair);
3055 g_list_free_1 (pair_node);
3059 g_key_file_remove_group_node (GKeyFile *key_file,
3062 GKeyFileGroup *group;
3065 group = (GKeyFileGroup *) group_node->data;
3068 g_hash_table_remove (key_file->group_hash, group->name);
3070 /* If the current group gets deleted make the current group the last
3073 if (key_file->current_group == group)
3075 /* groups should always contain at least the top comment group,
3076 * unless g_key_file_clear has been called
3078 if (key_file->groups)
3079 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3081 key_file->current_group = NULL;
3084 /* If the start group gets deleted make the start group the first
3087 if (key_file->start_group == group)
3089 tmp = g_list_last (key_file->groups);
3092 if (tmp != group_node &&
3093 ((GKeyFileGroup *) tmp->data)->name != NULL)
3100 key_file->start_group = (GKeyFileGroup *) tmp->data;
3102 key_file->start_group = NULL;
3105 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3107 if (group->name != NULL)
3108 key_file->approximate_size -= strlen (group->name) + 3;
3110 tmp = group->key_value_pairs;
3117 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3120 g_warn_if_fail (group->key_value_pairs == NULL);
3122 if (group->lookup_map)
3124 g_hash_table_destroy (group->lookup_map);
3125 group->lookup_map = NULL;
3128 g_free ((gchar *) group->name);
3129 g_slice_free (GKeyFileGroup, group);
3130 g_list_free_1 (group_node);
3134 * g_key_file_remove_group:
3135 * @key_file: a #GKeyFile
3136 * @group_name: a group name
3137 * @error: return location for a #GError or %NULL
3139 * Removes the specified group, @group_name,
3140 * from the key file.
3142 * Returns: %TRUE if the group was removed, %FALSE otherwise
3147 g_key_file_remove_group (GKeyFile *key_file,
3148 const gchar *group_name,
3153 g_return_val_if_fail (key_file != NULL, FALSE);
3154 g_return_val_if_fail (group_name != NULL, FALSE);
3156 group_node = g_key_file_lookup_group_node (key_file, group_name);
3160 g_set_error (error, G_KEY_FILE_ERROR,
3161 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3162 _("Key file does not have group '%s'"),
3167 g_key_file_remove_group_node (key_file, group_node);
3173 g_key_file_add_key (GKeyFile *key_file,
3174 GKeyFileGroup *group,
3178 GKeyFileKeyValuePair *pair;
3180 pair = g_slice_new (GKeyFileKeyValuePair);
3181 pair->key = g_strdup (key);
3182 pair->value = g_strdup (value);
3184 g_hash_table_replace (group->lookup_map, pair->key, pair);
3185 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3186 group->has_trailing_blank_line = FALSE;
3187 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3191 * g_key_file_remove_key:
3192 * @key_file: a #GKeyFile
3193 * @group_name: a group name
3194 * @key: a key name to remove
3195 * @error: return location for a #GError or %NULL
3197 * Removes @key in @group_name from the key file.
3199 * Returns: %TRUE if the key was removed, %FALSE otherwise
3204 g_key_file_remove_key (GKeyFile *key_file,
3205 const gchar *group_name,
3209 GKeyFileGroup *group;
3210 GKeyFileKeyValuePair *pair;
3212 g_return_val_if_fail (key_file != NULL, FALSE);
3213 g_return_val_if_fail (group_name != NULL, FALSE);
3214 g_return_val_if_fail (key != NULL, FALSE);
3218 group = g_key_file_lookup_group (key_file, group_name);
3221 g_set_error (error, G_KEY_FILE_ERROR,
3222 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3223 _("Key file does not have group '%s'"),
3224 group_name ? group_name : "(null)");
3228 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3232 g_set_error (error, G_KEY_FILE_ERROR,
3233 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3234 _("Key file does not have key '%s' in group '%s'"),
3239 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3241 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3242 g_hash_table_remove (group->lookup_map, pair->key);
3243 g_key_file_key_value_pair_free (pair);
3249 g_key_file_lookup_group_node (GKeyFile *key_file,
3250 const gchar *group_name)
3252 GKeyFileGroup *group;
3255 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3257 group = (GKeyFileGroup *) tmp->data;
3259 if (group && group->name && strcmp (group->name, group_name) == 0)
3266 static GKeyFileGroup *
3267 g_key_file_lookup_group (GKeyFile *key_file,
3268 const gchar *group_name)
3270 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3274 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3275 GKeyFileGroup *group,
3280 for (key_node = group->key_value_pairs;
3282 key_node = key_node->next)
3284 GKeyFileKeyValuePair *pair;
3286 pair = (GKeyFileKeyValuePair *) key_node->data;
3288 if (pair->key && strcmp (pair->key, key) == 0)
3295 static GKeyFileKeyValuePair *
3296 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3297 GKeyFileGroup *group,
3300 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3303 /* Lines starting with # or consisting entirely of whitespace are merely
3304 * recorded, not parsed. This function assumes all leading whitespace
3305 * has been stripped.
3308 g_key_file_line_is_comment (const gchar *line)
3310 return (*line == '#' || *line == '\0' || *line == '\n');
3314 g_key_file_is_group_name (const gchar *name)
3321 p = q = (gchar *) name;
3322 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3323 q = g_utf8_find_next_char (q, NULL);
3325 if (*q != '\0' || q == p)
3332 g_key_file_is_key_name (const gchar *name)
3339 p = q = (gchar *) name;
3340 /* We accept a little more than the desktop entry spec says,
3341 * since gnome-vfs uses mime-types as keys in its cache.
3343 while (*q && *q != '=' && *q != '[' && *q != ']')
3344 q = g_utf8_find_next_char (q, NULL);
3346 /* No empty keys, please */
3350 /* We accept spaces in the middle of keys to not break
3351 * existing apps, but we don't tolerate initial or final
3352 * spaces, which would lead to silent corruption when
3353 * rereading the file.
3355 if (*p == ' ' || q[-1] == ' ')
3361 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3362 q = g_utf8_find_next_char (q, NULL);
3376 /* A group in a key file is made up of a starting '[' followed by one
3377 * or more letters making up the group name followed by ']'.
3380 g_key_file_line_is_group (const gchar *line)
3390 while (*p && *p != ']')
3391 p = g_utf8_find_next_char (p, NULL);
3396 /* silently accept whitespace after the ] */
3397 p = g_utf8_find_next_char (p, NULL);
3398 while (*p == ' ' || *p == '\t')
3399 p = g_utf8_find_next_char (p, NULL);
3408 g_key_file_line_is_key_value_pair (const gchar *line)
3412 p = (gchar *) g_utf8_strchr (line, -1, '=');
3417 /* Key must be non-empty
3426 g_key_file_parse_value_as_string (GKeyFile *key_file,
3431 gchar *string_value, *p, *q0, *q;
3433 string_value = g_new (gchar, strlen (value) + 1);
3435 p = (gchar *) value;
3436 q0 = q = string_value;
3466 g_set_error (error, G_KEY_FILE_ERROR,
3467 G_KEY_FILE_ERROR_INVALID_VALUE,
3468 _("Key file contains escape character "
3473 if (pieces && *p == key_file->list_separator)
3474 *q = key_file->list_separator;
3488 g_set_error (error, G_KEY_FILE_ERROR,
3489 G_KEY_FILE_ERROR_INVALID_VALUE,
3490 _("Key file contains invalid escape "
3491 "sequence '%s'"), sequence);
3500 if (pieces && (*p == key_file->list_separator))
3502 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3518 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3519 *pieces = g_slist_reverse (*pieces);
3522 return string_value;
3526 g_key_file_parse_string_as_value (GKeyFile *key_file,
3527 const gchar *string,
3528 gboolean escape_separator)
3530 gchar *value, *p, *q;
3532 gboolean parsing_leading_space;
3534 length = strlen (string) + 1;
3536 /* Worst case would be that every character needs to be escaped.
3537 * In other words every character turns to two characters
3539 value = g_new (gchar, 2 * length);
3541 p = (gchar *) string;
3543 parsing_leading_space = TRUE;
3544 while (p < (string + length - 1))
3546 gchar escaped_character[3] = { '\\', 0, 0 };
3551 if (parsing_leading_space)
3553 escaped_character[1] = 's';
3554 strcpy (q, escaped_character);
3564 if (parsing_leading_space)
3566 escaped_character[1] = 't';
3567 strcpy (q, escaped_character);
3577 escaped_character[1] = 'n';
3578 strcpy (q, escaped_character);
3582 escaped_character[1] = 'r';
3583 strcpy (q, escaped_character);
3587 escaped_character[1] = '\\';
3588 strcpy (q, escaped_character);
3590 parsing_leading_space = FALSE;
3593 if (escape_separator && *p == key_file->list_separator)
3595 escaped_character[1] = key_file->list_separator;
3596 strcpy (q, escaped_character);
3598 parsing_leading_space = TRUE;
3604 parsing_leading_space = FALSE;
3616 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3620 gchar *end_of_valid_int;
3625 long_value = strtol (value, &end_of_valid_int, 10);
3627 if (*value == '\0' || *end_of_valid_int != '\0')
3629 gchar *value_utf8 = _g_utf8_make_valid (value);
3630 g_set_error (error, G_KEY_FILE_ERROR,
3631 G_KEY_FILE_ERROR_INVALID_VALUE,
3632 _("Value '%s' cannot be interpreted "
3633 "as a number."), value_utf8);
3634 g_free (value_utf8);
3639 int_value = long_value;
3640 if (int_value != long_value || errno == ERANGE)
3642 gchar *value_utf8 = _g_utf8_make_valid (value);
3645 G_KEY_FILE_ERROR_INVALID_VALUE,
3646 _("Integer value '%s' out of range"),
3648 g_free (value_utf8);
3657 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3661 return g_strdup_printf ("%d", value);
3665 g_key_file_parse_value_as_double (GKeyFile *key_file,
3669 gchar *end_of_valid_d;
3670 gdouble double_value = 0;
3672 double_value = g_ascii_strtod (value, &end_of_valid_d);
3674 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3676 gchar *value_utf8 = _g_utf8_make_valid (value);
3677 g_set_error (error, G_KEY_FILE_ERROR,
3678 G_KEY_FILE_ERROR_INVALID_VALUE,
3679 _("Value '%s' cannot be interpreted "
3680 "as a float number."),
3682 g_free (value_utf8);
3685 return double_value;
3689 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3695 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3697 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3700 value_utf8 = _g_utf8_make_valid (value);
3701 g_set_error (error, G_KEY_FILE_ERROR,
3702 G_KEY_FILE_ERROR_INVALID_VALUE,
3703 _("Value '%s' cannot be interpreted "
3704 "as a boolean."), value_utf8);
3705 g_free (value_utf8);
3711 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3715 return g_strdup ("true");
3717 return g_strdup ("false");
3721 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3728 string = g_string_sized_new (512);
3730 lines = g_strsplit (value, "\n", 0);
3732 for (i = 0; lines[i] != NULL; i++)
3734 if (lines[i][0] != '#')
3735 g_string_append_printf (string, "%s\n", lines[i]);
3737 g_string_append_printf (string, "%s\n", lines[i] + 1);
3741 return g_string_free (string, FALSE);
3745 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3746 const gchar *comment)
3752 string = g_string_sized_new (512);
3754 lines = g_strsplit (comment, "\n", 0);
3756 for (i = 0; lines[i] != NULL; i++)
3757 g_string_append_printf (string, "#%s%s", lines[i],
3758 lines[i + 1] == NULL? "" : "\n");
3761 return g_string_free (string, FALSE);
3764 #define __G_KEY_FILE_C__
3765 #include "galiasdef.c"