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_assert (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_assert (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_assert (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_assert (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_assert (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_assert (num_groups > 0);
1194 /* Only need num_groups instead of num_groups + 1
1195 * because the first group of the file (last in the
1196 * list) is always the comment group at the top,
1199 groups = g_new (gchar *, num_groups);
1201 group_node = g_list_last (key_file->groups);
1203 g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1206 for (group_node = group_node->prev;
1208 group_node = group_node->prev)
1210 GKeyFileGroup *group;
1212 group = (GKeyFileGroup *) group_node->data;
1214 g_assert (group->name != NULL);
1216 groups[i++] = g_strdup (group->name);
1227 * g_key_file_get_value:
1228 * @key_file: a #GKeyFile
1229 * @group_name: a group name
1231 * @error: return location for a #GError, or %NULL
1233 * Returns the value associated with @key under @group_name.
1235 * In the event the key cannot be found, %NULL is returned and
1236 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1237 * event that the @group_name cannot be found, %NULL is returned
1238 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1240 * Return value: a newly allocated string or %NULL if the specified
1241 * key cannot be found.
1246 g_key_file_get_value (GKeyFile *key_file,
1247 const gchar *group_name,
1251 GKeyFileGroup *group;
1252 GKeyFileKeyValuePair *pair;
1253 gchar *value = NULL;
1255 g_return_val_if_fail (key_file != NULL, NULL);
1256 g_return_val_if_fail (group_name != NULL, NULL);
1257 g_return_val_if_fail (key != NULL, NULL);
1259 group = g_key_file_lookup_group (key_file, group_name);
1263 g_set_error (error, G_KEY_FILE_ERROR,
1264 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1265 _("Key file does not have group '%s'"),
1266 group_name ? group_name : "(null)");
1270 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1273 value = g_strdup (pair->value);
1275 g_set_error (error, G_KEY_FILE_ERROR,
1276 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1277 _("Key file does not have key '%s'"), key);
1283 * g_key_file_set_value:
1284 * @key_file: a #GKeyFile
1285 * @group_name: a group name
1289 * Associates a new value with @key under @group_name.
1290 * If @key cannot be found then it is created.
1291 * If @group_name cannot be found then it is created.
1296 g_key_file_set_value (GKeyFile *key_file,
1297 const gchar *group_name,
1301 GKeyFileGroup *group;
1302 GKeyFileKeyValuePair *pair;
1304 g_return_if_fail (key_file != NULL);
1305 g_return_if_fail (g_key_file_is_group_name (group_name));
1306 g_return_if_fail (g_key_file_is_key_name (key));
1307 g_return_if_fail (value != NULL);
1309 group = g_key_file_lookup_group (key_file, group_name);
1313 g_key_file_add_group (key_file, group_name);
1314 group = (GKeyFileGroup *) key_file->groups->data;
1316 g_key_file_add_key (key_file, group, key, value);
1320 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1323 g_key_file_add_key (key_file, group, key, value);
1326 g_free (pair->value);
1327 pair->value = g_strdup (value);
1333 * g_key_file_get_string:
1334 * @key_file: a #GKeyFile
1335 * @group_name: a group name
1337 * @error: return location for a #GError, or %NULL
1339 * Returns the value associated with @key under @group_name.
1341 * In the event the key cannot be found, %NULL is returned and
1342 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1343 * event that the @group_name cannot be found, %NULL is returned
1344 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1346 * Return value: a newly allocated string or %NULL if the specified
1347 * key cannot be found.
1352 g_key_file_get_string (GKeyFile *key_file,
1353 const gchar *group_name,
1357 gchar *value, *string_value;
1358 GError *key_file_error;
1360 g_return_val_if_fail (key_file != NULL, NULL);
1361 g_return_val_if_fail (group_name != NULL, NULL);
1362 g_return_val_if_fail (key != NULL, NULL);
1364 key_file_error = NULL;
1366 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1370 g_propagate_error (error, key_file_error);
1374 if (!g_utf8_validate (value, -1, NULL))
1376 gchar *value_utf8 = _g_utf8_make_valid (value);
1377 g_set_error (error, G_KEY_FILE_ERROR,
1378 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1379 _("Key file contains key '%s' with value '%s' "
1380 "which is not UTF-8"), key, value_utf8);
1381 g_free (value_utf8);
1387 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1393 if (g_error_matches (key_file_error,
1395 G_KEY_FILE_ERROR_INVALID_VALUE))
1397 g_set_error (error, G_KEY_FILE_ERROR,
1398 G_KEY_FILE_ERROR_INVALID_VALUE,
1399 _("Key file contains key '%s' "
1400 "which has value that cannot be interpreted."),
1402 g_error_free (key_file_error);
1405 g_propagate_error (error, key_file_error);
1408 return string_value;
1412 * g_key_file_set_string:
1413 * @key_file: a #GKeyFile
1414 * @group_name: a group name
1418 * Associates a new string value with @key under @group_name.
1419 * If @key cannot be found then it is created.
1420 * If @group_name cannot be found then it is created.
1425 g_key_file_set_string (GKeyFile *key_file,
1426 const gchar *group_name,
1428 const gchar *string)
1432 g_return_if_fail (key_file != NULL);
1433 g_return_if_fail (string != NULL);
1435 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1436 g_key_file_set_value (key_file, group_name, key, value);
1441 * g_key_file_get_string_list:
1442 * @key_file: a #GKeyFile
1443 * @group_name: a group name
1445 * @length: return location for the number of returned strings, or %NULL
1446 * @error: return location for a #GError, or %NULL
1448 * Returns the values associated with @key under @group_name.
1450 * In the event the key cannot be found, %NULL is returned and
1451 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1452 * event that the @group_name cannot be found, %NULL is returned
1453 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1455 * Return value: a %NULL-terminated string array or %NULL if the specified
1456 * key cannot be found. The array should be freed with g_strfreev().
1461 g_key_file_get_string_list (GKeyFile *key_file,
1462 const gchar *group_name,
1467 GError *key_file_error = NULL;
1468 gchar *value, *string_value, **values;
1470 GSList *p, *pieces = NULL;
1472 g_return_val_if_fail (key_file != NULL, NULL);
1473 g_return_val_if_fail (group_name != NULL, NULL);
1474 g_return_val_if_fail (key != NULL, NULL);
1479 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1483 g_propagate_error (error, key_file_error);
1487 if (!g_utf8_validate (value, -1, NULL))
1489 gchar *value_utf8 = _g_utf8_make_valid (value);
1490 g_set_error (error, G_KEY_FILE_ERROR,
1491 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1492 _("Key file contains key '%s' with value '%s' "
1493 "which is not UTF-8"), key, value_utf8);
1494 g_free (value_utf8);
1500 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1502 g_free (string_value);
1506 if (g_error_matches (key_file_error,
1508 G_KEY_FILE_ERROR_INVALID_VALUE))
1510 g_set_error (error, G_KEY_FILE_ERROR,
1511 G_KEY_FILE_ERROR_INVALID_VALUE,
1512 _("Key file contains key '%s' "
1513 "which has value that cannot be interpreted."),
1515 g_error_free (key_file_error);
1518 g_propagate_error (error, key_file_error);
1521 len = g_slist_length (pieces);
1522 values = g_new (gchar *, len + 1);
1523 for (p = pieces, i = 0; p; p = p->next)
1524 values[i++] = p->data;
1527 g_slist_free (pieces);
1536 * g_key_file_set_string_list:
1537 * @key_file: a #GKeyFile
1538 * @group_name: a group name
1540 * @list: an array of locale string values
1541 * @length: number of locale string values in @list
1543 * Associates a list of string values for @key under @group_name.
1544 * If @key cannot be found then it is created.
1545 * If @group_name cannot be found then it is created.
1550 g_key_file_set_string_list (GKeyFile *key_file,
1551 const gchar *group_name,
1553 const gchar * const list[],
1556 GString *value_list;
1559 g_return_if_fail (key_file != NULL);
1560 g_return_if_fail (list != NULL);
1562 value_list = g_string_sized_new (length * 128);
1563 for (i = 0; i < length && list[i] != NULL; i++)
1567 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1568 g_string_append (value_list, value);
1569 g_string_append_c (value_list, key_file->list_separator);
1574 g_key_file_set_value (key_file, group_name, key, value_list->str);
1575 g_string_free (value_list, TRUE);
1579 * g_key_file_set_locale_string:
1580 * @key_file: a #GKeyFile
1581 * @group_name: a group name
1586 * Associates a string value for @key and @locale under @group_name.
1587 * If the translation for @key cannot be found then it is created.
1592 g_key_file_set_locale_string (GKeyFile *key_file,
1593 const gchar *group_name,
1595 const gchar *locale,
1596 const gchar *string)
1598 gchar *full_key, *value;
1600 g_return_if_fail (key_file != NULL);
1601 g_return_if_fail (key != NULL);
1602 g_return_if_fail (locale != NULL);
1603 g_return_if_fail (string != NULL);
1605 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1606 full_key = g_strdup_printf ("%s[%s]", key, locale);
1607 g_key_file_set_value (key_file, group_name, full_key, value);
1612 extern GSList *_g_compute_locale_variants (const gchar *locale);
1615 * g_key_file_get_locale_string:
1616 * @key_file: a #GKeyFile
1617 * @group_name: a group name
1619 * @locale: a locale or %NULL
1620 * @error: return location for a #GError, or %NULL
1622 * Returns the value associated with @key under @group_name
1623 * translated in the given @locale if available. If @locale is
1624 * %NULL then the current locale is assumed.
1626 * If @key cannot be found then %NULL is returned and @error is set
1627 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1628 * with @key cannot be interpreted or no suitable translation can
1629 * be found then the untranslated value is returned.
1631 * Return value: a newly allocated string or %NULL if the specified
1632 * key cannot be found.
1637 g_key_file_get_locale_string (GKeyFile *key_file,
1638 const gchar *group_name,
1640 const gchar *locale,
1643 gchar *candidate_key, *translated_value;
1644 GError *key_file_error;
1646 gboolean free_languages = FALSE;
1649 g_return_val_if_fail (key_file != NULL, NULL);
1650 g_return_val_if_fail (group_name != NULL, NULL);
1651 g_return_val_if_fail (key != NULL, NULL);
1653 candidate_key = NULL;
1654 translated_value = NULL;
1655 key_file_error = NULL;
1661 list = _g_compute_locale_variants (locale);
1663 languages = g_new (gchar *, g_slist_length (list) + 1);
1664 for (l = list, i = 0; l; l = l->next, i++)
1665 languages[i] = l->data;
1666 languages[i] = NULL;
1668 g_slist_free (list);
1669 free_languages = TRUE;
1673 languages = (gchar **) g_get_language_names ();
1674 free_languages = FALSE;
1677 for (i = 0; languages[i]; i++)
1679 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1681 translated_value = g_key_file_get_string (key_file,
1683 candidate_key, NULL);
1684 g_free (candidate_key);
1686 if (translated_value)
1689 g_free (translated_value);
1690 translated_value = NULL;
1693 /* Fallback to untranslated key
1695 if (!translated_value)
1697 translated_value = g_key_file_get_string (key_file, group_name, key,
1700 if (!translated_value)
1701 g_propagate_error (error, key_file_error);
1705 g_strfreev (languages);
1707 return translated_value;
1711 * g_key_file_get_locale_string_list:
1712 * @key_file: a #GKeyFile
1713 * @group_name: a group name
1716 * @length: return location for the number of returned strings or %NULL
1717 * @error: return location for a #GError or %NULL
1719 * Returns the values associated with @key under @group_name
1720 * translated in the given @locale if available. If @locale is
1721 * %NULL then the current locale is assumed.
1723 * If @key cannot be found then %NULL is returned and @error is set
1724 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1725 * with @key cannot be interpreted or no suitable translations
1726 * can be found then the untranslated values are returned. The
1727 * returned array is %NULL-terminated, so @length may optionally
1730 * Return value: a newly allocated %NULL-terminated string array
1731 * or %NULL if the key isn't found. The string array should be freed
1732 * with g_strfreev().
1737 g_key_file_get_locale_string_list (GKeyFile *key_file,
1738 const gchar *group_name,
1740 const gchar *locale,
1744 GError *key_file_error;
1745 gchar **values, *value;
1747 g_return_val_if_fail (key_file != NULL, NULL);
1748 g_return_val_if_fail (group_name != NULL, NULL);
1749 g_return_val_if_fail (key != NULL, NULL);
1751 key_file_error = NULL;
1753 value = g_key_file_get_locale_string (key_file, group_name,
1758 g_propagate_error (error, key_file_error);
1767 if (value[strlen (value) - 1] == ';')
1768 value[strlen (value) - 1] = '\0';
1770 values = g_strsplit (value, ";", 0);
1775 *length = g_strv_length (values);
1781 * g_key_file_set_locale_string_list:
1782 * @key_file: a #GKeyFile
1783 * @group_name: a group name
1786 * @list: a %NULL-terminated array of locale string values
1787 * @length: the length of @list
1789 * Associates a list of string values for @key and @locale under
1790 * @group_name. If the translation for @key cannot be found then
1796 g_key_file_set_locale_string_list (GKeyFile *key_file,
1797 const gchar *group_name,
1799 const gchar *locale,
1800 const gchar * const list[],
1803 GString *value_list;
1807 g_return_if_fail (key_file != NULL);
1808 g_return_if_fail (key != NULL);
1809 g_return_if_fail (locale != NULL);
1810 g_return_if_fail (length != 0);
1812 value_list = g_string_sized_new (length * 128);
1813 for (i = 0; i < length && list[i] != NULL; i++)
1817 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1819 g_string_append (value_list, value);
1820 g_string_append_c (value_list, ';');
1825 full_key = g_strdup_printf ("%s[%s]", key, locale);
1826 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1828 g_string_free (value_list, TRUE);
1832 * g_key_file_get_boolean:
1833 * @key_file: a #GKeyFile
1834 * @group_name: a group name
1836 * @error: return location for a #GError
1838 * Returns the value associated with @key under @group_name as a
1841 * If @key cannot be found then %FALSE is returned and @error is set
1842 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1843 * associated with @key cannot be interpreted as a boolean then %FALSE
1844 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1846 * Return value: the value associated with the key as a boolean,
1847 * or %FALSE if the key was not found or could not be parsed.
1852 g_key_file_get_boolean (GKeyFile *key_file,
1853 const gchar *group_name,
1857 GError *key_file_error = NULL;
1859 gboolean bool_value;
1861 g_return_val_if_fail (key_file != NULL, FALSE);
1862 g_return_val_if_fail (group_name != NULL, FALSE);
1863 g_return_val_if_fail (key != NULL, FALSE);
1865 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1869 g_propagate_error (error, key_file_error);
1873 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1879 if (g_error_matches (key_file_error,
1881 G_KEY_FILE_ERROR_INVALID_VALUE))
1883 g_set_error (error, G_KEY_FILE_ERROR,
1884 G_KEY_FILE_ERROR_INVALID_VALUE,
1885 _("Key file contains key '%s' "
1886 "which has value that cannot be interpreted."),
1888 g_error_free (key_file_error);
1891 g_propagate_error (error, key_file_error);
1898 * g_key_file_set_boolean:
1899 * @key_file: a #GKeyFile
1900 * @group_name: a group name
1902 * @value: %TRUE or %FALSE
1904 * Associates a new boolean value with @key under @group_name.
1905 * If @key cannot be found then it is created.
1910 g_key_file_set_boolean (GKeyFile *key_file,
1911 const gchar *group_name,
1917 g_return_if_fail (key_file != NULL);
1919 result = g_key_file_parse_boolean_as_value (key_file, value);
1920 g_key_file_set_value (key_file, group_name, key, result);
1925 * g_key_file_get_boolean_list:
1926 * @key_file: a #GKeyFile
1927 * @group_name: a group name
1929 * @length: the number of booleans returned
1930 * @error: return location for a #GError
1932 * Returns the values associated with @key under @group_name as
1933 * booleans. If @group_name is %NULL, the start_group is used.
1935 * If @key cannot be found then %NULL is returned and @error is set to
1936 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1937 * with @key cannot be interpreted as booleans then %NULL is returned
1938 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1940 * Return value: the values associated with the key as a list of
1941 * booleans, or %NULL if the key was not found or could not be parsed.
1946 g_key_file_get_boolean_list (GKeyFile *key_file,
1947 const gchar *group_name,
1952 GError *key_file_error;
1954 gboolean *bool_values;
1957 g_return_val_if_fail (key_file != NULL, NULL);
1958 g_return_val_if_fail (group_name != NULL, NULL);
1959 g_return_val_if_fail (key != NULL, NULL);
1964 key_file_error = NULL;
1966 values = g_key_file_get_string_list (key_file, group_name, key,
1967 &num_bools, &key_file_error);
1970 g_propagate_error (error, key_file_error);
1975 bool_values = g_new (gboolean, num_bools);
1977 for (i = 0; i < num_bools; i++)
1979 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1985 g_propagate_error (error, key_file_error);
1986 g_strfreev (values);
1987 g_free (bool_values);
1992 g_strfreev (values);
1995 *length = num_bools;
2001 * g_key_file_set_boolean_list:
2002 * @key_file: a #GKeyFile
2003 * @group_name: a group name
2005 * @list: an array of boolean values
2006 * @length: length of @list
2008 * Associates a list of boolean values with @key under @group_name.
2009 * If @key cannot be found then it is created.
2010 * If @group_name is %NULL, the start_group is used.
2015 g_key_file_set_boolean_list (GKeyFile *key_file,
2016 const gchar *group_name,
2021 GString *value_list;
2024 g_return_if_fail (key_file != NULL);
2025 g_return_if_fail (list != NULL);
2027 value_list = g_string_sized_new (length * 8);
2028 for (i = 0; i < length; i++)
2032 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2034 g_string_append (value_list, value);
2035 g_string_append_c (value_list, key_file->list_separator);
2040 g_key_file_set_value (key_file, group_name, key, value_list->str);
2041 g_string_free (value_list, TRUE);
2045 * g_key_file_get_integer:
2046 * @key_file: a #GKeyFile
2047 * @group_name: a group name
2049 * @error: return location for a #GError
2051 * Returns the value associated with @key under @group_name as an
2052 * integer. If @group_name is %NULL, the start group is used.
2054 * If @key cannot be found then 0 is returned and @error is set to
2055 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2056 * with @key cannot be interpreted as an integer then 0 is returned
2057 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2059 * Return value: the value associated with the key as an integer, or
2060 * 0 if the key was not found or could not be parsed.
2065 g_key_file_get_integer (GKeyFile *key_file,
2066 const gchar *group_name,
2070 GError *key_file_error;
2074 g_return_val_if_fail (key_file != NULL, -1);
2075 g_return_val_if_fail (group_name != NULL, -1);
2076 g_return_val_if_fail (key != NULL, -1);
2078 key_file_error = NULL;
2080 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2084 g_propagate_error (error, key_file_error);
2088 int_value = g_key_file_parse_value_as_integer (key_file, value,
2094 if (g_error_matches (key_file_error,
2096 G_KEY_FILE_ERROR_INVALID_VALUE))
2098 g_set_error (error, G_KEY_FILE_ERROR,
2099 G_KEY_FILE_ERROR_INVALID_VALUE,
2100 _("Key file contains key '%s' in group '%s' "
2101 "which has value that cannot be interpreted."), key,
2103 g_error_free (key_file_error);
2106 g_propagate_error (error, key_file_error);
2113 * g_key_file_set_integer:
2114 * @key_file: a #GKeyFile
2115 * @group_name: a group name
2117 * @value: an integer value
2119 * Associates a new integer value with @key under @group_name.
2120 * If @key cannot be found then it is created.
2125 g_key_file_set_integer (GKeyFile *key_file,
2126 const gchar *group_name,
2132 g_return_if_fail (key_file != NULL);
2134 result = g_key_file_parse_integer_as_value (key_file, value);
2135 g_key_file_set_value (key_file, group_name, key, result);
2140 * g_key_file_get_integer_list:
2141 * @key_file: a #GKeyFile
2142 * @group_name: a group name
2144 * @length: the number of integers returned
2145 * @error: return location for a #GError
2147 * Returns the values associated with @key under @group_name as
2150 * If @key cannot be found then %NULL is returned and @error is set to
2151 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2152 * with @key cannot be interpreted as integers then %NULL is returned
2153 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2155 * Return value: the values associated with the key as a list of
2156 * integers, or %NULL if the key was not found or could not be parsed.
2161 g_key_file_get_integer_list (GKeyFile *key_file,
2162 const gchar *group_name,
2167 GError *key_file_error = NULL;
2172 g_return_val_if_fail (key_file != NULL, NULL);
2173 g_return_val_if_fail (group_name != NULL, NULL);
2174 g_return_val_if_fail (key != NULL, NULL);
2179 values = g_key_file_get_string_list (key_file, group_name, key,
2180 &num_ints, &key_file_error);
2183 g_propagate_error (error, key_file_error);
2188 int_values = g_new (gint, num_ints);
2190 for (i = 0; i < num_ints; i++)
2192 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2198 g_propagate_error (error, key_file_error);
2199 g_strfreev (values);
2200 g_free (int_values);
2205 g_strfreev (values);
2214 * g_key_file_set_integer_list:
2215 * @key_file: a #GKeyFile
2216 * @group_name: a group name
2218 * @list: an array of integer values
2219 * @length: number of integer values in @list
2221 * Associates a list of integer values with @key under @group_name.
2222 * If @key cannot be found then it is created.
2227 g_key_file_set_integer_list (GKeyFile *key_file,
2228 const gchar *group_name,
2236 g_return_if_fail (key_file != NULL);
2237 g_return_if_fail (list != NULL);
2239 values = g_string_sized_new (length * 16);
2240 for (i = 0; i < length; i++)
2244 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2246 g_string_append (values, value);
2247 g_string_append_c (values, ';');
2252 g_key_file_set_value (key_file, group_name, key, values->str);
2253 g_string_free (values, TRUE);
2257 * g_key_file_get_double:
2258 * @key_file: a #GKeyFile
2259 * @group_name: a group name
2261 * @error: return location for a #GError
2263 * Returns the value associated with @key under @group_name as a
2264 * double. If @group_name is %NULL, the start_group is used.
2266 * If @key cannot be found then 0.0 is returned and @error is set to
2267 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2268 * with @key cannot be interpreted as a double then 0.0 is returned
2269 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2271 * Return value: the value associated with the key as a double, or
2272 * 0.0 if the key was not found or could not be parsed.
2277 g_key_file_get_double (GKeyFile *key_file,
2278 const gchar *group_name,
2282 GError *key_file_error;
2284 gdouble double_value;
2286 g_return_val_if_fail (key_file != NULL, -1);
2287 g_return_val_if_fail (group_name != NULL, -1);
2288 g_return_val_if_fail (key != NULL, -1);
2290 key_file_error = NULL;
2292 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2296 g_propagate_error (error, key_file_error);
2300 double_value = g_key_file_parse_value_as_double (key_file, value,
2306 if (g_error_matches (key_file_error,
2308 G_KEY_FILE_ERROR_INVALID_VALUE))
2310 g_set_error (error, G_KEY_FILE_ERROR,
2311 G_KEY_FILE_ERROR_INVALID_VALUE,
2312 _("Key file contains key '%s' in group '%s' "
2313 "which has value that cannot be interpreted."), key,
2315 g_error_free (key_file_error);
2318 g_propagate_error (error, key_file_error);
2321 return double_value;
2325 * g_key_file_set_double:
2326 * @key_file: a #GKeyFile
2327 * @group_name: a group name
2329 * @value: an double value
2331 * Associates a new double value with @key under @group_name.
2332 * If @key cannot be found then it is created.
2333 * If @group_name is %NULL, the start group is used.
2338 g_key_file_set_double (GKeyFile *key_file,
2339 const gchar *group_name,
2343 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2345 g_return_if_fail (key_file != NULL);
2347 g_ascii_dtostr (result, sizeof (result), value);
2348 g_key_file_set_value (key_file, group_name, key, result);
2352 * g_key_file_get_double_list:
2353 * @key_file: a #GKeyFile
2354 * @group_name: a group name
2356 * @length: the number of doubles returned
2357 * @error: return location for a #GError
2359 * Returns the values associated with @key under @group_name as
2360 * doubles. If @group_name is %NULL, the start group is used.
2362 * If @key cannot be found then %NULL is returned and @error is set to
2363 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2364 * with @key cannot be interpreted as doubles then %NULL is returned
2365 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2367 * Return value: the values associated with the key as a list of
2368 * doubles, or %NULL if the key was not found or could not be parsed.
2373 g_key_file_get_double_list (GKeyFile *key_file,
2374 const gchar *group_name,
2379 GError *key_file_error = NULL;
2381 gdouble *double_values;
2382 gsize i, num_doubles;
2384 g_return_val_if_fail (key_file != NULL, NULL);
2385 g_return_val_if_fail (group_name != NULL, NULL);
2386 g_return_val_if_fail (key != NULL, NULL);
2391 values = g_key_file_get_string_list (key_file, group_name, key,
2392 &num_doubles, &key_file_error);
2395 g_propagate_error (error, key_file_error);
2400 double_values = g_new (gdouble, num_doubles);
2402 for (i = 0; i < num_doubles; i++)
2404 double_values[i] = g_key_file_parse_value_as_double (key_file,
2410 g_propagate_error (error, key_file_error);
2411 g_strfreev (values);
2412 g_free (double_values);
2417 g_strfreev (values);
2420 *length = num_doubles;
2422 return double_values;
2426 * g_key_file_set_double_list:
2427 * @key_file: a #GKeyFile
2428 * @group_name: a group name
2430 * @list: an array of double values
2431 * @length: number of double values in @list
2433 * Associates a list of double values with @key under
2434 * @group_name. If @key cannot be found then it is created.
2435 * If @group_name is %NULL the start group is used.
2440 g_key_file_set_double_list (GKeyFile *key_file,
2441 const gchar *group_name,
2449 g_return_if_fail (key_file != NULL);
2450 g_return_if_fail (list != NULL);
2452 values = g_string_sized_new (length * 16);
2453 for (i = 0; i < length; i++)
2455 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2457 g_ascii_dtostr( result, sizeof (result), list[i] );
2459 g_string_append (values, result);
2460 g_string_append_c (values, ';');
2463 g_key_file_set_value (key_file, group_name, key, values->str);
2464 g_string_free (values, TRUE);
2468 g_key_file_set_key_comment (GKeyFile *key_file,
2469 const gchar *group_name,
2471 const gchar *comment,
2474 GKeyFileGroup *group;
2475 GKeyFileKeyValuePair *pair;
2476 GList *key_node, *comment_node, *tmp;
2478 group = g_key_file_lookup_group (key_file, group_name);
2481 g_set_error (error, G_KEY_FILE_ERROR,
2482 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2483 _("Key file does not have group '%s'"),
2484 group_name ? group_name : "(null)");
2489 /* First find the key the comments are supposed to be
2492 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2494 if (key_node == NULL)
2496 g_set_error (error, G_KEY_FILE_ERROR,
2497 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2498 _("Key file does not have key '%s' in group '%s'"),
2503 /* Then find all the comments already associated with the
2506 tmp = key_node->next;
2509 GKeyFileKeyValuePair *pair;
2511 pair = (GKeyFileKeyValuePair *) tmp->data;
2513 if (pair->key != NULL)
2518 g_key_file_remove_key_value_pair_node (key_file, group,
2522 if (comment == NULL)
2525 /* Now we can add our new comment
2527 pair = g_slice_new (GKeyFileKeyValuePair);
2529 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2531 key_node = g_list_insert (key_node, pair, 1);
2537 g_key_file_set_group_comment (GKeyFile *key_file,
2538 const gchar *group_name,
2539 const gchar *comment,
2542 GKeyFileGroup *group;
2544 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2546 group = g_key_file_lookup_group (key_file, group_name);
2549 g_set_error (error, G_KEY_FILE_ERROR,
2550 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2551 _("Key file does not have group '%s'"),
2552 group_name ? group_name : "(null)");
2557 /* First remove any existing comment
2561 g_key_file_key_value_pair_free (group->comment);
2562 group->comment = NULL;
2565 if (comment == NULL)
2568 /* Now we can add our new comment
2570 group->comment = g_slice_new (GKeyFileKeyValuePair);
2571 group->comment->key = NULL;
2572 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2578 g_key_file_set_top_comment (GKeyFile *key_file,
2579 const gchar *comment,
2583 GKeyFileGroup *group;
2584 GKeyFileKeyValuePair *pair;
2586 /* The last group in the list should be the top (comments only)
2589 g_assert (key_file->groups != NULL);
2590 group_node = g_list_last (key_file->groups);
2591 group = (GKeyFileGroup *) group_node->data;
2592 g_assert (group->name == NULL);
2594 /* Note all keys must be comments at the top of
2595 * the file, so we can just free it all.
2597 if (group->key_value_pairs != NULL)
2599 g_list_foreach (group->key_value_pairs,
2600 (GFunc) g_key_file_key_value_pair_free,
2602 g_list_free (group->key_value_pairs);
2603 group->key_value_pairs = NULL;
2606 if (comment == NULL)
2609 pair = g_slice_new (GKeyFileKeyValuePair);
2611 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2613 group->key_value_pairs =
2614 g_list_prepend (group->key_value_pairs, pair);
2620 * g_key_file_set_comment:
2621 * @key_file: a #GKeyFile
2622 * @group_name: a group name, or %NULL
2624 * @comment: a comment
2625 * @error: return location for a #GError
2627 * Places a comment above @key from @group_name.
2628 * If @key is %NULL then @comment will be written above @group_name.
2629 * If both @key and @group_name are %NULL, then @comment will be
2630 * written above the first group in the file.
2632 * Returns: %TRUE if the comment was written, %FALSE otherwise
2637 g_key_file_set_comment (GKeyFile *key_file,
2638 const gchar *group_name,
2640 const gchar *comment,
2643 g_return_val_if_fail (key_file != NULL, FALSE);
2645 if (group_name != NULL && key != NULL)
2647 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2650 else if (group_name != NULL)
2652 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2657 if (!g_key_file_set_top_comment (key_file, comment, error))
2661 if (comment != NULL)
2662 key_file->approximate_size += strlen (comment);
2668 g_key_file_get_key_comment (GKeyFile *key_file,
2669 const gchar *group_name,
2673 GKeyFileGroup *group;
2674 GKeyFileKeyValuePair *pair;
2675 GList *key_node, *tmp;
2679 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2681 group = g_key_file_lookup_group (key_file, group_name);
2684 g_set_error (error, G_KEY_FILE_ERROR,
2685 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2686 _("Key file does not have group '%s'"),
2687 group_name ? group_name : "(null)");
2692 /* First find the key the comments are supposed to be
2695 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2697 if (key_node == NULL)
2699 g_set_error (error, G_KEY_FILE_ERROR,
2700 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2701 _("Key file does not have key '%s' in group '%s'"),
2708 /* Then find all the comments already associated with the
2709 * key and concatentate them.
2711 tmp = key_node->next;
2712 if (!key_node->next)
2715 pair = (GKeyFileKeyValuePair *) tmp->data;
2716 if (pair->key != NULL)
2721 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2723 if (pair->key != NULL)
2729 while (tmp != key_node)
2731 GKeyFileKeyValuePair *pair;
2733 pair = (GKeyFileKeyValuePair *) tmp->data;
2736 string = g_string_sized_new (512);
2738 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2739 g_string_append (string, comment);
2747 comment = string->str;
2748 g_string_free (string, FALSE);
2757 get_group_comment (GKeyFile *key_file,
2758 GKeyFileGroup *group,
2767 tmp = group->key_value_pairs;
2770 GKeyFileKeyValuePair *pair;
2772 pair = (GKeyFileKeyValuePair *) tmp->data;
2774 if (pair->key != NULL)
2780 if (tmp->next == NULL)
2788 GKeyFileKeyValuePair *pair;
2790 pair = (GKeyFileKeyValuePair *) tmp->data;
2793 string = g_string_sized_new (512);
2795 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2796 g_string_append (string, comment);
2803 return g_string_free (string, FALSE);
2809 g_key_file_get_group_comment (GKeyFile *key_file,
2810 const gchar *group_name,
2814 GKeyFileGroup *group;
2816 group = g_key_file_lookup_group (key_file, group_name);
2819 g_set_error (error, G_KEY_FILE_ERROR,
2820 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2821 _("Key file does not have group '%s'"),
2822 group_name ? group_name : "(null)");
2828 return g_strdup (group->comment->value);
2830 group_node = g_key_file_lookup_group_node (key_file, group_name);
2831 group_node = group_node->next;
2832 group = (GKeyFileGroup *)group_node->data;
2833 return get_group_comment (key_file, group, error);
2837 g_key_file_get_top_comment (GKeyFile *key_file,
2841 GKeyFileGroup *group;
2843 /* The last group in the list should be the top (comments only)
2846 g_assert (key_file->groups != NULL);
2847 group_node = g_list_last (key_file->groups);
2848 group = (GKeyFileGroup *) group_node->data;
2849 g_assert (group->name == NULL);
2851 return get_group_comment (key_file, group, error);
2855 * g_key_file_get_comment:
2856 * @key_file: a #GKeyFile
2857 * @group_name: a group name, or %NULL
2859 * @error: return location for a #GError
2861 * Retrieves a comment above @key from @group_name.
2862 * If @key is %NULL then @comment will be read from above
2863 * @group_name. If both @key and @group_name are %NULL, then
2864 * @comment will be read from above the first group in the file.
2866 * Returns: a comment that should be freed with g_free()
2871 g_key_file_get_comment (GKeyFile *key_file,
2872 const gchar *group_name,
2876 g_return_val_if_fail (key_file != NULL, NULL);
2878 if (group_name != NULL && key != NULL)
2879 return g_key_file_get_key_comment (key_file, group_name, key, error);
2880 else if (group_name != NULL)
2881 return g_key_file_get_group_comment (key_file, group_name, error);
2883 return g_key_file_get_top_comment (key_file, error);
2887 * g_key_file_remove_comment:
2888 * @key_file: a #GKeyFile
2889 * @group_name: a group name, or %NULL
2891 * @error: return location for a #GError
2893 * Removes a comment above @key from @group_name.
2894 * If @key is %NULL then @comment will be removed above @group_name.
2895 * If both @key and @group_name are %NULL, then @comment will
2896 * be removed above the first group in the file.
2898 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2904 g_key_file_remove_comment (GKeyFile *key_file,
2905 const gchar *group_name,
2909 g_return_val_if_fail (key_file != NULL, FALSE);
2911 if (group_name != NULL && key != NULL)
2912 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2913 else if (group_name != NULL)
2914 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2916 return g_key_file_set_top_comment (key_file, NULL, error);
2920 * g_key_file_has_group:
2921 * @key_file: a #GKeyFile
2922 * @group_name: a group name
2924 * Looks whether the key file has the group @group_name.
2926 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2931 g_key_file_has_group (GKeyFile *key_file,
2932 const gchar *group_name)
2934 g_return_val_if_fail (key_file != NULL, FALSE);
2935 g_return_val_if_fail (group_name != NULL, FALSE);
2937 return g_key_file_lookup_group (key_file, group_name) != NULL;
2941 * g_key_file_has_key:
2942 * @key_file: a #GKeyFile
2943 * @group_name: a group name
2945 * @error: return location for a #GError
2947 * Looks whether the key file has the key @key in the group
2950 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2956 g_key_file_has_key (GKeyFile *key_file,
2957 const gchar *group_name,
2961 GKeyFileKeyValuePair *pair;
2962 GKeyFileGroup *group;
2964 g_return_val_if_fail (key_file != NULL, FALSE);
2965 g_return_val_if_fail (group_name != NULL, FALSE);
2966 g_return_val_if_fail (key != NULL, FALSE);
2968 group = g_key_file_lookup_group (key_file, group_name);
2972 g_set_error (error, G_KEY_FILE_ERROR,
2973 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2974 _("Key file does not have group '%s'"),
2975 group_name ? group_name : "(null)");
2980 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2982 return pair != NULL;
2986 g_key_file_add_group (GKeyFile *key_file,
2987 const gchar *group_name)
2989 GKeyFileGroup *group;
2991 g_return_if_fail (key_file != NULL);
2992 g_return_if_fail (g_key_file_is_group_name (group_name));
2994 group = g_key_file_lookup_group (key_file, group_name);
2997 key_file->current_group = group;
3001 group = g_slice_new0 (GKeyFileGroup);
3002 group->name = g_strdup (group_name);
3003 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3004 key_file->groups = g_list_prepend (key_file->groups, group);
3005 key_file->approximate_size += strlen (group_name) + 3;
3006 key_file->current_group = group;
3008 if (key_file->start_group == NULL)
3009 key_file->start_group = group;
3011 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3015 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3020 g_free (pair->value);
3021 g_slice_free (GKeyFileKeyValuePair, pair);
3025 /* Be careful not to call this function on a node with data in the
3026 * lookup map without removing it from the lookup map, first.
3028 * Some current cases where this warning is not a concern are
3030 * - the node being removed is a comment node
3031 * - the entire lookup map is getting destroyed soon after
3035 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3036 GKeyFileGroup *group,
3040 GKeyFileKeyValuePair *pair;
3042 pair = (GKeyFileKeyValuePair *) pair_node->data;
3044 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3046 if (pair->key != NULL)
3047 key_file->approximate_size -= strlen (pair->key) + 1;
3049 g_assert (pair->value != NULL);
3050 key_file->approximate_size -= strlen (pair->value);
3052 g_key_file_key_value_pair_free (pair);
3054 g_list_free_1 (pair_node);
3058 g_key_file_remove_group_node (GKeyFile *key_file,
3061 GKeyFileGroup *group;
3064 group = (GKeyFileGroup *) group_node->data;
3067 g_hash_table_remove (key_file->group_hash, group->name);
3069 /* If the current group gets deleted make the current group the last
3072 if (key_file->current_group == group)
3074 /* groups should always contain at least the top comment group,
3075 * unless g_key_file_clear has been called
3077 if (key_file->groups)
3078 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3080 key_file->current_group = NULL;
3083 /* If the start group gets deleted make the start group the first
3086 if (key_file->start_group == group)
3088 tmp = g_list_last (key_file->groups);
3091 if (tmp != group_node &&
3092 ((GKeyFileGroup *) tmp->data)->name != NULL)
3099 key_file->start_group = (GKeyFileGroup *) tmp->data;
3101 key_file->start_group = NULL;
3104 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3106 if (group->name != NULL)
3107 key_file->approximate_size -= strlen (group->name) + 3;
3109 tmp = group->key_value_pairs;
3116 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3119 g_assert (group->key_value_pairs == NULL);
3121 if (group->lookup_map)
3123 g_hash_table_destroy (group->lookup_map);
3124 group->lookup_map = NULL;
3127 g_free ((gchar *) group->name);
3128 g_slice_free (GKeyFileGroup, group);
3129 g_list_free_1 (group_node);
3133 * g_key_file_remove_group:
3134 * @key_file: a #GKeyFile
3135 * @group_name: a group name
3136 * @error: return location for a #GError or %NULL
3138 * Removes the specified group, @group_name,
3139 * from the key file.
3141 * Returns: %TRUE if the group was removed, %FALSE otherwise
3146 g_key_file_remove_group (GKeyFile *key_file,
3147 const gchar *group_name,
3152 g_return_val_if_fail (key_file != NULL, FALSE);
3153 g_return_val_if_fail (group_name != NULL, FALSE);
3155 group_node = g_key_file_lookup_group_node (key_file, group_name);
3159 g_set_error (error, G_KEY_FILE_ERROR,
3160 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3161 _("Key file does not have group '%s'"),
3166 g_key_file_remove_group_node (key_file, group_node);
3172 g_key_file_add_key (GKeyFile *key_file,
3173 GKeyFileGroup *group,
3177 GKeyFileKeyValuePair *pair;
3179 pair = g_slice_new (GKeyFileKeyValuePair);
3180 pair->key = g_strdup (key);
3181 pair->value = g_strdup (value);
3183 g_hash_table_replace (group->lookup_map, pair->key, pair);
3184 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3185 group->has_trailing_blank_line = FALSE;
3186 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3190 * g_key_file_remove_key:
3191 * @key_file: a #GKeyFile
3192 * @group_name: a group name
3193 * @key: a key name to remove
3194 * @error: return location for a #GError or %NULL
3196 * Removes @key in @group_name from the key file.
3198 * Returns: %TRUE if the key was removed, %FALSE otherwise
3203 g_key_file_remove_key (GKeyFile *key_file,
3204 const gchar *group_name,
3208 GKeyFileGroup *group;
3209 GKeyFileKeyValuePair *pair;
3211 g_return_val_if_fail (key_file != NULL, FALSE);
3212 g_return_val_if_fail (group_name != NULL, FALSE);
3213 g_return_val_if_fail (key != NULL, FALSE);
3217 group = g_key_file_lookup_group (key_file, group_name);
3220 g_set_error (error, G_KEY_FILE_ERROR,
3221 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3222 _("Key file does not have group '%s'"),
3223 group_name ? group_name : "(null)");
3227 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3231 g_set_error (error, G_KEY_FILE_ERROR,
3232 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3233 _("Key file does not have key '%s' in group '%s'"),
3238 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3240 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3241 g_hash_table_remove (group->lookup_map, pair->key);
3242 g_key_file_key_value_pair_free (pair);
3248 g_key_file_lookup_group_node (GKeyFile *key_file,
3249 const gchar *group_name)
3251 GKeyFileGroup *group;
3254 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3256 group = (GKeyFileGroup *) tmp->data;
3258 if (group && group->name && strcmp (group->name, group_name) == 0)
3265 static GKeyFileGroup *
3266 g_key_file_lookup_group (GKeyFile *key_file,
3267 const gchar *group_name)
3269 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3273 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3274 GKeyFileGroup *group,
3279 for (key_node = group->key_value_pairs;
3281 key_node = key_node->next)
3283 GKeyFileKeyValuePair *pair;
3285 pair = (GKeyFileKeyValuePair *) key_node->data;
3287 if (pair->key && strcmp (pair->key, key) == 0)
3294 static GKeyFileKeyValuePair *
3295 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3296 GKeyFileGroup *group,
3299 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3302 /* Lines starting with # or consisting entirely of whitespace are merely
3303 * recorded, not parsed. This function assumes all leading whitespace
3304 * has been stripped.
3307 g_key_file_line_is_comment (const gchar *line)
3309 return (*line == '#' || *line == '\0' || *line == '\n');
3313 g_key_file_is_group_name (const gchar *name)
3320 p = q = (gchar *) name;
3321 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3322 q = g_utf8_find_next_char (q, NULL);
3324 if (*q != '\0' || q == p)
3331 g_key_file_is_key_name (const gchar *name)
3338 p = q = (gchar *) name;
3339 /* We accept a little more than the desktop entry spec says,
3340 * since gnome-vfs uses mime-types as keys in its cache.
3342 while (*q && *q != '=' && *q != '[' && *q != ']')
3343 q = g_utf8_find_next_char (q, NULL);
3345 /* No empty keys, please */
3349 /* We accept spaces in the middle of keys to not break
3350 * existing apps, but we don't tolerate initial or final
3351 * spaces, which would lead to silent corruption when
3352 * rereading the file.
3354 if (*p == ' ' || q[-1] == ' ')
3360 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3361 q = g_utf8_find_next_char (q, NULL);
3375 /* A group in a key file is made up of a starting '[' followed by one
3376 * or more letters making up the group name followed by ']'.
3379 g_key_file_line_is_group (const gchar *line)
3389 while (*p && *p != ']')
3390 p = g_utf8_find_next_char (p, NULL);
3395 /* silently accept whitespace after the ] */
3396 p = g_utf8_find_next_char (p, NULL);
3397 while (*p == ' ' || *p == '\t')
3398 p = g_utf8_find_next_char (p, NULL);
3407 g_key_file_line_is_key_value_pair (const gchar *line)
3411 p = (gchar *) g_utf8_strchr (line, -1, '=');
3416 /* Key must be non-empty
3425 g_key_file_parse_value_as_string (GKeyFile *key_file,
3430 gchar *string_value, *p, *q0, *q;
3432 string_value = g_new (gchar, strlen (value) + 1);
3434 p = (gchar *) value;
3435 q0 = q = string_value;
3465 g_set_error (error, G_KEY_FILE_ERROR,
3466 G_KEY_FILE_ERROR_INVALID_VALUE,
3467 _("Key file contains escape character "
3472 if (pieces && *p == key_file->list_separator)
3473 *q = key_file->list_separator;
3487 g_set_error (error, G_KEY_FILE_ERROR,
3488 G_KEY_FILE_ERROR_INVALID_VALUE,
3489 _("Key file contains invalid escape "
3490 "sequence '%s'"), sequence);
3499 if (pieces && (*p == key_file->list_separator))
3501 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3517 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3518 *pieces = g_slist_reverse (*pieces);
3521 return string_value;
3525 g_key_file_parse_string_as_value (GKeyFile *key_file,
3526 const gchar *string,
3527 gboolean escape_separator)
3529 gchar *value, *p, *q;
3531 gboolean parsing_leading_space;
3533 length = strlen (string) + 1;
3535 /* Worst case would be that every character needs to be escaped.
3536 * In other words every character turns to two characters
3538 value = g_new (gchar, 2 * length);
3540 p = (gchar *) string;
3542 parsing_leading_space = TRUE;
3543 while (p < (string + length - 1))
3545 gchar escaped_character[3] = { '\\', 0, 0 };
3550 if (parsing_leading_space)
3552 escaped_character[1] = 's';
3553 strcpy (q, escaped_character);
3563 if (parsing_leading_space)
3565 escaped_character[1] = 't';
3566 strcpy (q, escaped_character);
3576 escaped_character[1] = 'n';
3577 strcpy (q, escaped_character);
3581 escaped_character[1] = 'r';
3582 strcpy (q, escaped_character);
3586 escaped_character[1] = '\\';
3587 strcpy (q, escaped_character);
3589 parsing_leading_space = FALSE;
3592 if (escape_separator && *p == key_file->list_separator)
3594 escaped_character[1] = key_file->list_separator;
3595 strcpy (q, escaped_character);
3597 parsing_leading_space = TRUE;
3603 parsing_leading_space = FALSE;
3615 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3619 gchar *end_of_valid_int;
3624 long_value = strtol (value, &end_of_valid_int, 10);
3626 if (*value == '\0' || *end_of_valid_int != '\0')
3628 gchar *value_utf8 = _g_utf8_make_valid (value);
3629 g_set_error (error, G_KEY_FILE_ERROR,
3630 G_KEY_FILE_ERROR_INVALID_VALUE,
3631 _("Value '%s' cannot be interpreted "
3632 "as a number."), value_utf8);
3633 g_free (value_utf8);
3638 int_value = long_value;
3639 if (int_value != long_value || errno == ERANGE)
3641 gchar *value_utf8 = _g_utf8_make_valid (value);
3644 G_KEY_FILE_ERROR_INVALID_VALUE,
3645 _("Integer value '%s' out of range"),
3647 g_free (value_utf8);
3656 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3660 return g_strdup_printf ("%d", value);
3664 g_key_file_parse_value_as_double (GKeyFile *key_file,
3668 gchar *end_of_valid_d;
3669 gdouble double_value = 0;
3671 double_value = g_ascii_strtod (value, &end_of_valid_d);
3673 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3675 gchar *value_utf8 = _g_utf8_make_valid (value);
3676 g_set_error (error, G_KEY_FILE_ERROR,
3677 G_KEY_FILE_ERROR_INVALID_VALUE,
3678 _("Value '%s' cannot be interpreted "
3679 "as a float number."),
3681 g_free (value_utf8);
3684 return double_value;
3688 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3694 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3696 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3699 value_utf8 = _g_utf8_make_valid (value);
3700 g_set_error (error, G_KEY_FILE_ERROR,
3701 G_KEY_FILE_ERROR_INVALID_VALUE,
3702 _("Value '%s' cannot be interpreted "
3703 "as a boolean."), value_utf8);
3704 g_free (value_utf8);
3710 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3714 return g_strdup ("true");
3716 return g_strdup ("false");
3720 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3727 string = g_string_sized_new (512);
3729 lines = g_strsplit (value, "\n", 0);
3731 for (i = 0; lines[i] != NULL; i++)
3733 if (lines[i][0] != '#')
3734 g_string_append_printf (string, "%s\n", lines[i]);
3736 g_string_append_printf (string, "%s\n", lines[i] + 1);
3740 return g_string_free (string, FALSE);
3744 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3745 const gchar *comment)
3751 string = g_string_sized_new (512);
3753 lines = g_strsplit (comment, "\n", 0);
3755 for (i = 0; lines[i] != NULL; i++)
3756 g_string_append_printf (string, "#%s%s", lines[i],
3757 lines[i + 1] == NULL? "" : "\n");
3760 return g_string_free (string, FALSE);
3763 #define __G_KEY_FILE_C__
3764 #include "galiasdef.c"