1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
4 * Copyright 2009-2010 Collabora Ltd.
5 * Copyright 2009 Nokia Corporation
7 * Written by Ray Strode <rstrode@redhat.com>
8 * Matthias Clasen <mclasen@redhat.com>
10 * GLib is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * GLib is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GLib; see the file COPYING.LIB. If not,
22 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
36 #include <sys/types.h>
44 #define fstat(a,b) _fstati64(a,b)
48 #define S_ISREG(mode) ((mode)&_S_IFREG)
51 #endif /* G_OS_WIN23 */
56 #include "gfileutils.h"
62 #include "gmessages.h"
65 #include "gstrfuncs.h"
69 typedef struct _GKeyFileGroup GKeyFileGroup
;
74 GHashTable
*group_hash
;
76 GKeyFileGroup
*start_group
;
77 GKeyFileGroup
*current_group
;
79 GString
*parse_buffer
; /* Holds up to one line of not-yet-parsed data */
81 /* Used for sizing the output buffer during serialization
83 gsize approximate_size
;
92 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair
;
96 const gchar
*name
; /* NULL for above first group (which will be comments) */
98 GKeyFileKeyValuePair
*comment
; /* Special comment that is stuck to the top of a group */
99 gboolean has_trailing_blank_line
;
101 GList
*key_value_pairs
;
103 /* Used in parallel with key_value_pairs for
104 * increased lookup performance
106 GHashTable
*lookup_map
;
109 struct _GKeyFileKeyValuePair
111 gchar
*key
; /* NULL for comments */
115 static gint
find_file_in_data_dirs (const gchar
*file
,
116 const gchar
**data_dirs
,
119 static gboolean
g_key_file_load_from_fd (GKeyFile
*key_file
,
123 static GList
*g_key_file_lookup_group_node (GKeyFile
*key_file
,
124 const gchar
*group_name
);
125 static GKeyFileGroup
*g_key_file_lookup_group (GKeyFile
*key_file
,
126 const gchar
*group_name
);
128 static GList
*g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
129 GKeyFileGroup
*group
,
131 static GKeyFileKeyValuePair
*g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
132 GKeyFileGroup
*group
,
135 static void g_key_file_remove_group_node (GKeyFile
*key_file
,
137 static void g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
138 GKeyFileGroup
*group
,
141 static void g_key_file_add_key (GKeyFile
*key_file
,
142 GKeyFileGroup
*group
,
145 static void g_key_file_add_group (GKeyFile
*key_file
,
146 const gchar
*group_name
);
147 static gboolean
g_key_file_is_group_name (const gchar
*name
);
148 static gboolean
g_key_file_is_key_name (const gchar
*name
);
149 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
);
150 static gboolean
g_key_file_line_is_comment (const gchar
*line
);
151 static gboolean
g_key_file_line_is_group (const gchar
*line
);
152 static gboolean
g_key_file_line_is_key_value_pair (const gchar
*line
);
153 static gchar
*g_key_file_parse_value_as_string (GKeyFile
*key_file
,
157 static gchar
*g_key_file_parse_string_as_value (GKeyFile
*key_file
,
159 gboolean escape_separator
);
160 static gint
g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
163 static gchar
*g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
165 static gdouble
g_key_file_parse_value_as_double (GKeyFile
*key_file
,
168 static gboolean
g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
171 static gchar
*g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
173 static gchar
*g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
175 static gchar
*g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
176 const gchar
*comment
);
177 static void g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
181 static void g_key_file_parse_comment (GKeyFile
*key_file
,
185 static void g_key_file_parse_group (GKeyFile
*key_file
,
189 static gchar
*key_get_locale (const gchar
*key
);
190 static void g_key_file_parse_data (GKeyFile
*key_file
,
194 static void g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
199 g_key_file_error_quark (void)
201 return g_quark_from_static_string ("g-key-file-error-quark");
205 g_key_file_init (GKeyFile
*key_file
)
207 key_file
->current_group
= g_slice_new0 (GKeyFileGroup
);
208 key_file
->groups
= g_list_prepend (NULL
, key_file
->current_group
);
209 key_file
->group_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
210 key_file
->start_group
= NULL
;
211 key_file
->parse_buffer
= g_string_sized_new (128);
212 key_file
->approximate_size
= 0;
213 key_file
->list_separator
= ';';
215 key_file
->locales
= g_strdupv ((gchar
**)g_get_language_names ());
219 g_key_file_clear (GKeyFile
*key_file
)
221 GList
*tmp
, *group_node
;
223 if (key_file
->locales
)
225 g_strfreev (key_file
->locales
);
226 key_file
->locales
= NULL
;
229 if (key_file
->parse_buffer
)
231 g_string_free (key_file
->parse_buffer
, TRUE
);
232 key_file
->parse_buffer
= NULL
;
235 tmp
= key_file
->groups
;
240 g_key_file_remove_group_node (key_file
, group_node
);
243 g_hash_table_destroy (key_file
->group_hash
);
244 key_file
->group_hash
= NULL
;
246 g_warn_if_fail (key_file
->groups
== NULL
);
253 * Creates a new empty #GKeyFile object. Use
254 * g_key_file_load_from_file(), g_key_file_load_from_data(),
255 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
256 * read an existing key file.
258 * Return value: an empty #GKeyFile.
263 g_key_file_new (void)
267 key_file
= g_slice_new0 (GKeyFile
);
268 g_key_file_init (key_file
);
274 * g_key_file_set_list_separator:
275 * @key_file: a #GKeyFile
276 * @separator: the separator
278 * Sets the character which is used to separate
279 * values in lists. Typically ';' or ',' are used
280 * as separators. The default list separator is ';'.
285 g_key_file_set_list_separator (GKeyFile
*key_file
,
288 g_return_if_fail (key_file
!= NULL
);
290 key_file
->list_separator
= separator
;
294 /* Iterates through all the directories in *dirs trying to
295 * open file. When it successfully locates and opens a file it
296 * returns the file descriptor to the open file. It also
297 * outputs the absolute path of the file in output_file.
300 find_file_in_data_dirs (const gchar
*file
,
305 const gchar
**data_dirs
, *data_dir
;
317 while (data_dirs
&& (data_dir
= *data_dirs
) && fd
< 0)
319 gchar
*candidate_file
, *sub_dir
;
321 candidate_file
= (gchar
*) file
;
322 sub_dir
= g_strdup ("");
323 while (candidate_file
!= NULL
&& fd
< 0)
327 path
= g_build_filename (data_dir
, sub_dir
,
328 candidate_file
, NULL
);
330 fd
= g_open (path
, O_RDONLY
, 0);
338 candidate_file
= strchr (candidate_file
, '-');
340 if (candidate_file
== NULL
)
346 sub_dir
= g_strndup (file
, candidate_file
- file
- 1);
348 for (p
= sub_dir
; *p
!= '\0'; p
++)
351 *p
= G_DIR_SEPARATOR
;
360 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
361 G_KEY_FILE_ERROR_NOT_FOUND
,
362 _("Valid key file could not be "
363 "found in search dirs"));
366 if (output_file
!= NULL
&& fd
> 0)
367 *output_file
= g_strdup (path
);
375 g_key_file_load_from_fd (GKeyFile
*key_file
,
380 GError
*key_file_error
= NULL
;
382 struct stat stat_buf
;
383 gchar read_buf
[4096];
385 if (fstat (fd
, &stat_buf
) < 0)
387 g_set_error_literal (error
, G_FILE_ERROR
,
388 g_file_error_from_errno (errno
),
393 if (!S_ISREG (stat_buf
.st_mode
))
395 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
396 G_KEY_FILE_ERROR_PARSE
,
397 _("Not a regular file"));
401 if (stat_buf
.st_size
== 0)
403 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
404 G_KEY_FILE_ERROR_PARSE
,
409 if (key_file
->approximate_size
> 0)
411 g_key_file_clear (key_file
);
412 g_key_file_init (key_file
);
414 key_file
->flags
= flags
;
418 bytes_read
= read (fd
, read_buf
, 4096);
420 if (bytes_read
== 0) /* End of File */
425 if (errno
== EINTR
|| errno
== EAGAIN
)
428 g_set_error_literal (error
, G_FILE_ERROR
,
429 g_file_error_from_errno (errno
),
434 g_key_file_parse_data (key_file
,
435 read_buf
, bytes_read
,
438 while (!key_file_error
);
442 g_propagate_error (error
, key_file_error
);
446 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
450 g_propagate_error (error
, key_file_error
);
458 * g_key_file_load_from_file:
459 * @key_file: an empty #GKeyFile struct
460 * @file: the path of a filename to load, in the GLib filename encoding
461 * @flags: flags from #GKeyFileFlags
462 * @error: return location for a #GError, or %NULL
464 * Loads a key file into an empty #GKeyFile structure.
465 * If the file could not be loaded then %error is set to
466 * either a #GFileError or #GKeyFileError.
468 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
473 g_key_file_load_from_file (GKeyFile
*key_file
,
478 GError
*key_file_error
= NULL
;
481 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
482 g_return_val_if_fail (file
!= NULL
, FALSE
);
484 fd
= g_open (file
, O_RDONLY
, 0);
488 g_set_error_literal (error
, G_FILE_ERROR
,
489 g_file_error_from_errno (errno
),
494 g_key_file_load_from_fd (key_file
, fd
, flags
, &key_file_error
);
499 g_propagate_error (error
, key_file_error
);
507 * g_key_file_load_from_data:
508 * @key_file: an empty #GKeyFile struct
509 * @data: key file loaded in memory
510 * @length: the length of @data in bytes
511 * @flags: flags from #GKeyFileFlags
512 * @error: return location for a #GError, or %NULL
514 * Loads a key file from memory into an empty #GKeyFile structure.
515 * If the object cannot be created then %error is set to a #GKeyFileError.
517 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
522 g_key_file_load_from_data (GKeyFile
*key_file
,
528 GError
*key_file_error
= NULL
;
530 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
531 g_return_val_if_fail (data
!= NULL
, FALSE
);
532 g_return_val_if_fail (length
!= 0, FALSE
);
534 if (length
== (gsize
)-1)
535 length
= strlen (data
);
537 if (key_file
->approximate_size
> 0)
539 g_key_file_clear (key_file
);
540 g_key_file_init (key_file
);
542 key_file
->flags
= flags
;
544 g_key_file_parse_data (key_file
, data
, length
, &key_file_error
);
548 g_propagate_error (error
, key_file_error
);
552 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
556 g_propagate_error (error
, key_file_error
);
564 * g_key_file_load_from_dirs:
565 * @key_file: an empty #GKeyFile struct
566 * @file: a relative path to a filename to open and parse
567 * @search_dirs: %NULL-terminated array of directories to search
568 * @full_path: return location for a string containing the full path
569 * of the file, or %NULL
570 * @flags: flags from #GKeyFileFlags
571 * @error: return location for a #GError, or %NULL
573 * This function looks for a key file named @file in the paths
574 * specified in @search_dirs, loads the file into @key_file and
575 * returns the file's full path in @full_path. If the file could not
576 * be loaded then an %error is set to either a #GFileError or
579 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
584 g_key_file_load_from_dirs (GKeyFile
*key_file
,
586 const gchar
**search_dirs
,
591 GError
*key_file_error
= NULL
;
592 const gchar
**data_dirs
;
597 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
598 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
599 g_return_val_if_fail (search_dirs
!= NULL
, FALSE
);
602 data_dirs
= search_dirs
;
604 while (*data_dirs
!= NULL
&& !found_file
)
606 g_free (output_path
);
608 fd
= find_file_in_data_dirs (file
, data_dirs
, &output_path
,
614 g_propagate_error (error
, key_file_error
);
618 found_file
= g_key_file_load_from_fd (key_file
, fd
, flags
,
624 g_propagate_error (error
, key_file_error
);
629 if (found_file
&& full_path
)
630 *full_path
= output_path
;
632 g_free (output_path
);
638 * g_key_file_load_from_data_dirs:
639 * @key_file: an empty #GKeyFile struct
640 * @file: a relative path to a filename to open and parse
641 * @full_path: return location for a string containing the full path
642 * of the file, or %NULL
643 * @flags: flags from #GKeyFileFlags
644 * @error: return location for a #GError, or %NULL
646 * This function looks for a key file named @file in the paths
647 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
648 * loads the file into @key_file and returns the file's full path in
649 * @full_path. If the file could not be loaded then an %error is
650 * set to either a #GFileError or #GKeyFileError.
652 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
656 g_key_file_load_from_data_dirs (GKeyFile
*key_file
,
662 gchar
**all_data_dirs
;
663 const gchar
* user_data_dir
;
664 const gchar
* const * system_data_dirs
;
668 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
669 g_return_val_if_fail (!g_path_is_absolute (file
), FALSE
);
671 user_data_dir
= g_get_user_data_dir ();
672 system_data_dirs
= g_get_system_data_dirs ();
673 all_data_dirs
= g_new (gchar
*, g_strv_length ((gchar
**)system_data_dirs
) + 2);
676 all_data_dirs
[i
++] = g_strdup (user_data_dir
);
679 while (system_data_dirs
[j
] != NULL
)
680 all_data_dirs
[i
++] = g_strdup (system_data_dirs
[j
++]);
681 all_data_dirs
[i
] = NULL
;
683 found_file
= g_key_file_load_from_dirs (key_file
,
685 (const gchar
**)all_data_dirs
,
690 g_strfreev (all_data_dirs
);
697 * @key_file: a #GKeyFile
704 g_key_file_free (GKeyFile
*key_file
)
706 g_return_if_fail (key_file
!= NULL
);
708 g_key_file_clear (key_file
);
709 g_slice_free (GKeyFile
, key_file
);
712 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
713 * true for locales that match those in g_get_language_names().
716 g_key_file_locale_is_interesting (GKeyFile
*key_file
,
721 if (key_file
->flags
& G_KEY_FILE_KEEP_TRANSLATIONS
)
724 for (i
= 0; key_file
->locales
[i
] != NULL
; i
++)
726 if (g_ascii_strcasecmp (key_file
->locales
[i
], locale
) == 0)
734 g_key_file_parse_line (GKeyFile
*key_file
,
739 GError
*parse_error
= NULL
;
742 g_return_if_fail (key_file
!= NULL
);
743 g_return_if_fail (line
!= NULL
);
745 line_start
= (gchar
*) line
;
746 while (g_ascii_isspace (*line_start
))
749 if (g_key_file_line_is_comment (line_start
))
750 g_key_file_parse_comment (key_file
, line
, length
, &parse_error
);
751 else if (g_key_file_line_is_group (line_start
))
752 g_key_file_parse_group (key_file
, line_start
,
753 length
- (line_start
- line
),
755 else if (g_key_file_line_is_key_value_pair (line_start
))
756 g_key_file_parse_key_value_pair (key_file
, line_start
,
757 length
- (line_start
- line
),
761 gchar
*line_utf8
= _g_utf8_make_valid (line
);
762 g_set_error (error
, G_KEY_FILE_ERROR
,
763 G_KEY_FILE_ERROR_PARSE
,
764 _("Key file contains line '%s' which is not "
765 "a key-value pair, group, or comment"),
773 g_propagate_error (error
, parse_error
);
777 g_key_file_parse_comment (GKeyFile
*key_file
,
782 GKeyFileKeyValuePair
*pair
;
784 if (!(key_file
->flags
& G_KEY_FILE_KEEP_COMMENTS
))
787 g_warn_if_fail (key_file
->current_group
!= NULL
);
789 pair
= g_slice_new (GKeyFileKeyValuePair
);
791 pair
->value
= g_strndup (line
, length
);
793 key_file
->current_group
->key_value_pairs
=
794 g_list_prepend (key_file
->current_group
->key_value_pairs
, pair
);
796 if (length
== 0 || line
[0] != '#')
797 key_file
->current_group
->has_trailing_blank_line
= TRUE
;
801 g_key_file_parse_group (GKeyFile
*key_file
,
807 const gchar
*group_name_start
, *group_name_end
;
809 /* advance past opening '['
811 group_name_start
= line
+ 1;
812 group_name_end
= line
+ length
- 1;
814 while (*group_name_end
!= ']')
817 group_name
= g_strndup (group_name_start
,
818 group_name_end
- group_name_start
);
820 if (!g_key_file_is_group_name (group_name
))
822 g_set_error (error
, G_KEY_FILE_ERROR
,
823 G_KEY_FILE_ERROR_PARSE
,
824 _("Invalid group name: %s"), group_name
);
829 g_key_file_add_group (key_file
, group_name
);
834 g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
839 gchar
*key
, *value
, *key_end
, *value_start
, *locale
;
840 gsize key_len
, value_len
;
842 if (key_file
->current_group
== NULL
|| key_file
->current_group
->name
== NULL
)
844 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
845 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
846 _("Key file does not start with a group"));
850 key_end
= value_start
= strchr (line
, '=');
852 g_warn_if_fail (key_end
!= NULL
);
857 /* Pull the key name from the line (chomping trailing whitespace)
859 while (g_ascii_isspace (*key_end
))
862 key_len
= key_end
- line
+ 2;
864 g_warn_if_fail (key_len
<= length
);
866 key
= g_strndup (line
, key_len
- 1);
868 if (!g_key_file_is_key_name (key
))
870 g_set_error (error
, G_KEY_FILE_ERROR
,
871 G_KEY_FILE_ERROR_PARSE
,
872 _("Invalid key name: %s"), key
);
877 /* Pull the value from the line (chugging leading whitespace)
879 while (g_ascii_isspace (*value_start
))
882 value_len
= line
+ length
- value_start
+ 1;
884 value
= g_strndup (value_start
, value_len
);
886 g_warn_if_fail (key_file
->start_group
!= NULL
);
888 if (key_file
->current_group
889 && key_file
->current_group
->name
890 && strcmp (key_file
->start_group
->name
,
891 key_file
->current_group
->name
) == 0
892 && strcmp (key
, "Encoding") == 0)
894 if (g_ascii_strcasecmp (value
, "UTF-8") != 0)
896 gchar
*value_utf8
= _g_utf8_make_valid (value
);
897 g_set_error (error
, G_KEY_FILE_ERROR
,
898 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
899 _("Key file contains unsupported "
900 "encoding '%s'"), value_utf8
);
909 /* Is this key a translation? If so, is it one that we care about?
911 locale
= key_get_locale (key
);
913 if (locale
== NULL
|| g_key_file_locale_is_interesting (key_file
, locale
))
914 g_key_file_add_key (key_file
, key_file
->current_group
, key
, value
);
922 key_get_locale (const gchar
*key
)
926 locale
= g_strrstr (key
, "[");
928 if (locale
&& strlen (locale
) <= 2)
932 locale
= g_strndup (locale
+ 1, strlen (locale
) - 2);
938 g_key_file_parse_data (GKeyFile
*key_file
,
946 g_return_if_fail (key_file
!= NULL
);
947 g_return_if_fail (data
!= NULL
);
951 for (i
= 0; i
< length
; i
++)
955 if (i
> 0 && data
[i
- 1] == '\r')
956 g_string_erase (key_file
->parse_buffer
,
957 key_file
->parse_buffer
->len
- 1,
960 /* When a newline is encountered flush the parse buffer so that the
961 * line can be parsed. Note that completely blank lines won't show
962 * up in the parse buffer, so they get parsed directly.
964 if (key_file
->parse_buffer
->len
> 0)
965 g_key_file_flush_parse_buffer (key_file
, &parse_error
);
967 g_key_file_parse_comment (key_file
, "", 1, &parse_error
);
971 g_propagate_error (error
, parse_error
);
976 g_string_append_c (key_file
->parse_buffer
, data
[i
]);
979 key_file
->approximate_size
+= length
;
983 g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
986 GError
*file_error
= NULL
;
988 g_return_if_fail (key_file
!= NULL
);
992 if (key_file
->parse_buffer
->len
> 0)
994 g_key_file_parse_line (key_file
, key_file
->parse_buffer
->str
,
995 key_file
->parse_buffer
->len
,
997 g_string_erase (key_file
->parse_buffer
, 0, -1);
1001 g_propagate_error (error
, file_error
);
1008 * g_key_file_to_data:
1009 * @key_file: a #GKeyFile
1010 * @length: return location for the length of the
1011 * returned string, or %NULL
1012 * @error: return location for a #GError, or %NULL
1014 * This function outputs @key_file as a string.
1016 * Note that this function never reports an error,
1017 * so it is safe to pass %NULL as @error.
1019 * Return value: a newly allocated string holding
1020 * the contents of the #GKeyFile
1025 g_key_file_to_data (GKeyFile
*key_file
,
1029 GString
*data_string
;
1030 GList
*group_node
, *key_file_node
;
1031 gboolean has_blank_line
= TRUE
;
1033 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1035 data_string
= g_string_sized_new (2 * key_file
->approximate_size
);
1037 for (group_node
= g_list_last (key_file
->groups
);
1039 group_node
= group_node
->prev
)
1041 GKeyFileGroup
*group
;
1043 group
= (GKeyFileGroup
*) group_node
->data
;
1045 /* separate groups by at least an empty line */
1046 if (!has_blank_line
)
1047 g_string_append_c (data_string
, '\n');
1048 has_blank_line
= group
->has_trailing_blank_line
;
1050 if (group
->comment
!= NULL
)
1051 g_string_append_printf (data_string
, "%s\n", group
->comment
->value
);
1053 if (group
->name
!= NULL
)
1054 g_string_append_printf (data_string
, "[%s]\n", group
->name
);
1056 for (key_file_node
= g_list_last (group
->key_value_pairs
);
1057 key_file_node
!= NULL
;
1058 key_file_node
= key_file_node
->prev
)
1060 GKeyFileKeyValuePair
*pair
;
1062 pair
= (GKeyFileKeyValuePair
*) key_file_node
->data
;
1064 if (pair
->key
!= NULL
)
1065 g_string_append_printf (data_string
, "%s=%s\n", pair
->key
, pair
->value
);
1067 g_string_append_printf (data_string
, "%s\n", pair
->value
);
1072 *length
= data_string
->len
;
1074 return g_string_free (data_string
, FALSE
);
1078 * g_key_file_get_keys:
1079 * @key_file: a #GKeyFile
1080 * @group_name: a group name
1081 * @length: return location for the number of keys returned, or %NULL
1082 * @error: return location for a #GError, or %NULL
1084 * Returns all keys for the group name @group_name. The array of
1085 * returned keys will be %NULL-terminated, so @length may
1086 * optionally be %NULL. In the event that the @group_name cannot
1087 * be found, %NULL is returned and @error is set to
1088 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1090 * Return value: a newly-allocated %NULL-terminated array of strings.
1091 * Use g_strfreev() to free it.
1096 g_key_file_get_keys (GKeyFile
*key_file
,
1097 const gchar
*group_name
,
1101 GKeyFileGroup
*group
;
1106 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1107 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1109 group
= g_key_file_lookup_group (key_file
, group_name
);
1113 g_set_error (error
, G_KEY_FILE_ERROR
,
1114 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1115 _("Key file does not have group '%s'"),
1116 group_name
? group_name
: "(null)");
1121 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1123 GKeyFileKeyValuePair
*pair
;
1125 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1131 keys
= g_new (gchar
*, num_keys
+ 1);
1134 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1136 GKeyFileKeyValuePair
*pair
;
1138 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1142 keys
[i
] = g_strdup (pair
->key
);
1147 keys
[num_keys
] = NULL
;
1156 * g_key_file_get_start_group:
1157 * @key_file: a #GKeyFile
1159 * Returns the name of the start group of the file.
1161 * Return value: The start group of the key file.
1166 g_key_file_get_start_group (GKeyFile
*key_file
)
1168 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1170 if (key_file
->start_group
)
1171 return g_strdup (key_file
->start_group
->name
);
1177 * g_key_file_get_groups:
1178 * @key_file: a #GKeyFile
1179 * @length: return location for the number of returned groups, or %NULL
1181 * Returns all groups in the key file loaded with @key_file.
1182 * The array of returned groups will be %NULL-terminated, so
1183 * @length may optionally be %NULL.
1185 * Return value: a newly-allocated %NULL-terminated array of strings.
1186 * Use g_strfreev() to free it.
1190 g_key_file_get_groups (GKeyFile
*key_file
,
1195 gsize i
, num_groups
;
1197 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1199 num_groups
= g_list_length (key_file
->groups
);
1201 g_return_val_if_fail (num_groups
> 0, NULL
);
1203 group_node
= g_list_last (key_file
->groups
);
1205 g_return_val_if_fail (((GKeyFileGroup
*) group_node
->data
)->name
== NULL
, NULL
);
1207 /* Only need num_groups instead of num_groups + 1
1208 * because the first group of the file (last in the
1209 * list) is always the comment group at the top,
1212 groups
= g_new (gchar
*, num_groups
);
1216 for (group_node
= group_node
->prev
;
1218 group_node
= group_node
->prev
)
1220 GKeyFileGroup
*group
;
1222 group
= (GKeyFileGroup
*) group_node
->data
;
1224 g_warn_if_fail (group
->name
!= NULL
);
1226 groups
[i
++] = g_strdup (group
->name
);
1237 * g_key_file_get_value:
1238 * @key_file: a #GKeyFile
1239 * @group_name: a group name
1241 * @error: return location for a #GError, or %NULL
1243 * Returns the raw value associated with @key under @group_name.
1244 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1246 * In the event the key cannot be found, %NULL is returned and
1247 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1248 * event that the @group_name cannot be found, %NULL is returned
1249 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1252 * Return value: a newly allocated string or %NULL if the specified
1253 * key cannot be found.
1258 g_key_file_get_value (GKeyFile
*key_file
,
1259 const gchar
*group_name
,
1263 GKeyFileGroup
*group
;
1264 GKeyFileKeyValuePair
*pair
;
1265 gchar
*value
= NULL
;
1267 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1268 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1269 g_return_val_if_fail (key
!= NULL
, NULL
);
1271 group
= g_key_file_lookup_group (key_file
, group_name
);
1275 g_set_error (error
, G_KEY_FILE_ERROR
,
1276 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
1277 _("Key file does not have group '%s'"),
1278 group_name
? group_name
: "(null)");
1282 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1285 value
= g_strdup (pair
->value
);
1287 g_set_error (error
, G_KEY_FILE_ERROR
,
1288 G_KEY_FILE_ERROR_KEY_NOT_FOUND
,
1289 _("Key file does not have key '%s'"), key
);
1295 * g_key_file_set_value:
1296 * @key_file: a #GKeyFile
1297 * @group_name: a group name
1301 * Associates a new value with @key under @group_name.
1303 * If @key cannot be found then it is created. If @group_name cannot
1304 * be found then it is created. To set an UTF-8 string which may contain
1305 * characters that need escaping (such as newlines or spaces), use
1306 * g_key_file_set_string().
1311 g_key_file_set_value (GKeyFile
*key_file
,
1312 const gchar
*group_name
,
1316 GKeyFileGroup
*group
;
1317 GKeyFileKeyValuePair
*pair
;
1319 g_return_if_fail (key_file
!= NULL
);
1320 g_return_if_fail (g_key_file_is_group_name (group_name
));
1321 g_return_if_fail (g_key_file_is_key_name (key
));
1322 g_return_if_fail (value
!= NULL
);
1324 group
= g_key_file_lookup_group (key_file
, group_name
);
1328 g_key_file_add_group (key_file
, group_name
);
1329 group
= (GKeyFileGroup
*) key_file
->groups
->data
;
1331 g_key_file_add_key (key_file
, group
, key
, value
);
1335 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1338 g_key_file_add_key (key_file
, group
, key
, value
);
1341 g_free (pair
->value
);
1342 pair
->value
= g_strdup (value
);
1348 * g_key_file_get_string:
1349 * @key_file: a #GKeyFile
1350 * @group_name: a group name
1352 * @error: return location for a #GError, or %NULL
1354 * Returns the string value associated with @key under @group_name.
1355 * Unlike g_key_file_get_value(), this function handles escape sequences
1358 * In the event the key cannot be found, %NULL is returned and
1359 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1360 * event that the @group_name cannot be found, %NULL is returned
1361 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1363 * Return value: a newly allocated string or %NULL if the specified
1364 * key cannot be found.
1369 g_key_file_get_string (GKeyFile
*key_file
,
1370 const gchar
*group_name
,
1374 gchar
*value
, *string_value
;
1375 GError
*key_file_error
;
1377 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1378 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1379 g_return_val_if_fail (key
!= NULL
, NULL
);
1381 key_file_error
= NULL
;
1383 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1387 g_propagate_error (error
, key_file_error
);
1391 if (!g_utf8_validate (value
, -1, NULL
))
1393 gchar
*value_utf8
= _g_utf8_make_valid (value
);
1394 g_set_error (error
, G_KEY_FILE_ERROR
,
1395 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1396 _("Key file contains key '%s' with value '%s' "
1397 "which is not UTF-8"), key
, value_utf8
);
1398 g_free (value_utf8
);
1404 string_value
= g_key_file_parse_value_as_string (key_file
, value
, NULL
,
1410 if (g_error_matches (key_file_error
,
1412 G_KEY_FILE_ERROR_INVALID_VALUE
))
1414 g_set_error (error
, G_KEY_FILE_ERROR
,
1415 G_KEY_FILE_ERROR_INVALID_VALUE
,
1416 _("Key file contains key '%s' "
1417 "which has value that cannot be interpreted."),
1419 g_error_free (key_file_error
);
1422 g_propagate_error (error
, key_file_error
);
1425 return string_value
;
1429 * g_key_file_set_string:
1430 * @key_file: a #GKeyFile
1431 * @group_name: a group name
1435 * Associates a new string value with @key under @group_name.
1436 * If @key cannot be found then it is created.
1437 * If @group_name cannot be found then it is created.
1438 * Unlike g_key_file_set_value(), this function handles characters
1439 * that need escaping, such as newlines.
1444 g_key_file_set_string (GKeyFile
*key_file
,
1445 const gchar
*group_name
,
1447 const gchar
*string
)
1451 g_return_if_fail (key_file
!= NULL
);
1452 g_return_if_fail (string
!= NULL
);
1454 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
1455 g_key_file_set_value (key_file
, group_name
, key
, value
);
1460 * g_key_file_get_string_list:
1461 * @key_file: a #GKeyFile
1462 * @group_name: a group name
1464 * @length: return location for the number of returned strings, or %NULL
1465 * @error: return location for a #GError, or %NULL
1467 * Returns the values associated with @key under @group_name.
1469 * In the event the key cannot be found, %NULL is returned and
1470 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1471 * event that the @group_name cannot be found, %NULL is returned
1472 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1474 * Return value: a %NULL-terminated string array or %NULL if the specified
1475 * key cannot be found. The array should be freed with g_strfreev().
1480 g_key_file_get_string_list (GKeyFile
*key_file
,
1481 const gchar
*group_name
,
1486 GError
*key_file_error
= NULL
;
1487 gchar
*value
, *string_value
, **values
;
1489 GSList
*p
, *pieces
= NULL
;
1491 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1492 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1493 g_return_val_if_fail (key
!= NULL
, NULL
);
1498 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1502 g_propagate_error (error
, key_file_error
);
1506 if (!g_utf8_validate (value
, -1, NULL
))
1508 gchar
*value_utf8
= _g_utf8_make_valid (value
);
1509 g_set_error (error
, G_KEY_FILE_ERROR
,
1510 G_KEY_FILE_ERROR_UNKNOWN_ENCODING
,
1511 _("Key file contains key '%s' with value '%s' "
1512 "which is not UTF-8"), key
, value_utf8
);
1513 g_free (value_utf8
);
1519 string_value
= g_key_file_parse_value_as_string (key_file
, value
, &pieces
, &key_file_error
);
1521 g_free (string_value
);
1525 if (g_error_matches (key_file_error
,
1527 G_KEY_FILE_ERROR_INVALID_VALUE
))
1529 g_set_error (error
, G_KEY_FILE_ERROR
,
1530 G_KEY_FILE_ERROR_INVALID_VALUE
,
1531 _("Key file contains key '%s' "
1532 "which has a value that cannot be interpreted."),
1534 g_error_free (key_file_error
);
1537 g_propagate_error (error
, key_file_error
);
1542 len
= g_slist_length (pieces
);
1543 values
= g_new (gchar
*, len
+ 1);
1544 for (p
= pieces
, i
= 0; p
; p
= p
->next
)
1545 values
[i
++] = p
->data
;
1548 g_slist_free (pieces
);
1557 * g_key_file_set_string_list:
1558 * @key_file: a #GKeyFile
1559 * @group_name: a group name
1561 * @list: an array of string values
1562 * @length: number of string values in @list
1564 * Associates a list of string values for @key under @group_name.
1565 * If @key cannot be found then it is created.
1566 * If @group_name cannot be found then it is created.
1571 g_key_file_set_string_list (GKeyFile
*key_file
,
1572 const gchar
*group_name
,
1574 const gchar
* const list
[],
1577 GString
*value_list
;
1580 g_return_if_fail (key_file
!= NULL
);
1581 g_return_if_fail (list
!= NULL
|| length
== 0);
1583 value_list
= g_string_sized_new (length
* 128);
1584 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
1588 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
1589 g_string_append (value_list
, value
);
1590 g_string_append_c (value_list
, key_file
->list_separator
);
1595 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
1596 g_string_free (value_list
, TRUE
);
1600 * g_key_file_set_locale_string:
1601 * @key_file: a #GKeyFile
1602 * @group_name: a group name
1604 * @locale: a locale identifier
1607 * Associates a string value for @key and @locale under @group_name.
1608 * If the translation for @key cannot be found then it is created.
1613 g_key_file_set_locale_string (GKeyFile
*key_file
,
1614 const gchar
*group_name
,
1616 const gchar
*locale
,
1617 const gchar
*string
)
1619 gchar
*full_key
, *value
;
1621 g_return_if_fail (key_file
!= NULL
);
1622 g_return_if_fail (key
!= NULL
);
1623 g_return_if_fail (locale
!= NULL
);
1624 g_return_if_fail (string
!= NULL
);
1626 value
= g_key_file_parse_string_as_value (key_file
, string
, FALSE
);
1627 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
1628 g_key_file_set_value (key_file
, group_name
, full_key
, value
);
1633 extern GSList
*_g_compute_locale_variants (const gchar
*locale
);
1636 * g_key_file_get_locale_string:
1637 * @key_file: a #GKeyFile
1638 * @group_name: a group name
1640 * @locale: a locale identifier or %NULL
1641 * @error: return location for a #GError, or %NULL
1643 * Returns the value associated with @key under @group_name
1644 * translated in the given @locale if available. If @locale is
1645 * %NULL then the current locale is assumed.
1647 * If @key cannot be found then %NULL is returned and @error is set
1648 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1649 * with @key cannot be interpreted or no suitable translation can
1650 * be found then the untranslated value is returned.
1652 * Return value: a newly allocated string or %NULL if the specified
1653 * key cannot be found.
1658 g_key_file_get_locale_string (GKeyFile
*key_file
,
1659 const gchar
*group_name
,
1661 const gchar
*locale
,
1664 gchar
*candidate_key
, *translated_value
;
1665 GError
*key_file_error
;
1667 gboolean free_languages
= FALSE
;
1670 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1671 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1672 g_return_val_if_fail (key
!= NULL
, NULL
);
1674 candidate_key
= NULL
;
1675 translated_value
= NULL
;
1676 key_file_error
= NULL
;
1682 list
= _g_compute_locale_variants (locale
);
1684 languages
= g_new (gchar
*, g_slist_length (list
) + 1);
1685 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
1686 languages
[i
] = l
->data
;
1687 languages
[i
] = NULL
;
1689 g_slist_free (list
);
1690 free_languages
= TRUE
;
1694 languages
= (gchar
**) g_get_language_names ();
1695 free_languages
= FALSE
;
1698 for (i
= 0; languages
[i
]; i
++)
1700 candidate_key
= g_strdup_printf ("%s[%s]", key
, languages
[i
]);
1702 translated_value
= g_key_file_get_string (key_file
,
1704 candidate_key
, NULL
);
1705 g_free (candidate_key
);
1707 if (translated_value
)
1710 g_free (translated_value
);
1711 translated_value
= NULL
;
1714 /* Fallback to untranslated key
1716 if (!translated_value
)
1718 translated_value
= g_key_file_get_string (key_file
, group_name
, key
,
1721 if (!translated_value
)
1722 g_propagate_error (error
, key_file_error
);
1726 g_strfreev (languages
);
1728 return translated_value
;
1732 * g_key_file_get_locale_string_list:
1733 * @key_file: a #GKeyFile
1734 * @group_name: a group name
1736 * @locale: a locale identifier or %NULL
1737 * @length: return location for the number of returned strings or %NULL
1738 * @error: return location for a #GError or %NULL
1740 * Returns the values associated with @key under @group_name
1741 * translated in the given @locale if available. If @locale is
1742 * %NULL then the current locale is assumed.
1744 * If @key cannot be found then %NULL is returned and @error is set
1745 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1746 * with @key cannot be interpreted or no suitable translations
1747 * can be found then the untranslated values are returned. The
1748 * returned array is %NULL-terminated, so @length may optionally
1751 * Return value: a newly allocated %NULL-terminated string array
1752 * or %NULL if the key isn't found. The string array should be freed
1753 * with g_strfreev().
1758 g_key_file_get_locale_string_list (GKeyFile
*key_file
,
1759 const gchar
*group_name
,
1761 const gchar
*locale
,
1765 GError
*key_file_error
;
1766 gchar
**values
, *value
;
1767 char list_separator
[2];
1770 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1771 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1772 g_return_val_if_fail (key
!= NULL
, NULL
);
1774 key_file_error
= NULL
;
1776 value
= g_key_file_get_locale_string (key_file
, group_name
,
1781 g_propagate_error (error
, key_file_error
);
1790 len
= strlen (value
);
1791 if (value
[len
- 1] == key_file
->list_separator
)
1792 value
[len
- 1] = '\0';
1794 list_separator
[0] = key_file
->list_separator
;
1795 list_separator
[1] = '\0';
1796 values
= g_strsplit (value
, list_separator
, 0);
1801 *length
= g_strv_length (values
);
1807 * g_key_file_set_locale_string_list:
1808 * @key_file: a #GKeyFile
1809 * @group_name: a group name
1811 * @locale: a locale identifier
1812 * @list: a %NULL-terminated array of locale string values
1813 * @length: the length of @list
1815 * Associates a list of string values for @key and @locale under
1816 * @group_name. If the translation for @key cannot be found then
1822 g_key_file_set_locale_string_list (GKeyFile
*key_file
,
1823 const gchar
*group_name
,
1825 const gchar
*locale
,
1826 const gchar
* const list
[],
1829 GString
*value_list
;
1833 g_return_if_fail (key_file
!= NULL
);
1834 g_return_if_fail (key
!= NULL
);
1835 g_return_if_fail (locale
!= NULL
);
1836 g_return_if_fail (length
!= 0);
1838 value_list
= g_string_sized_new (length
* 128);
1839 for (i
= 0; i
< length
&& list
[i
] != NULL
; i
++)
1843 value
= g_key_file_parse_string_as_value (key_file
, list
[i
], TRUE
);
1844 g_string_append (value_list
, value
);
1845 g_string_append_c (value_list
, key_file
->list_separator
);
1850 full_key
= g_strdup_printf ("%s[%s]", key
, locale
);
1851 g_key_file_set_value (key_file
, group_name
, full_key
, value_list
->str
);
1853 g_string_free (value_list
, TRUE
);
1857 * g_key_file_get_boolean:
1858 * @key_file: a #GKeyFile
1859 * @group_name: a group name
1861 * @error: return location for a #GError
1863 * Returns the value associated with @key under @group_name as a
1866 * If @key cannot be found then %FALSE is returned and @error is set
1867 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1868 * associated with @key cannot be interpreted as a boolean then %FALSE
1869 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1871 * Return value: the value associated with the key as a boolean,
1872 * or %FALSE if the key was not found or could not be parsed.
1877 g_key_file_get_boolean (GKeyFile
*key_file
,
1878 const gchar
*group_name
,
1882 GError
*key_file_error
= NULL
;
1884 gboolean bool_value
;
1886 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
1887 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
1888 g_return_val_if_fail (key
!= NULL
, FALSE
);
1890 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1894 g_propagate_error (error
, key_file_error
);
1898 bool_value
= g_key_file_parse_value_as_boolean (key_file
, value
,
1904 if (g_error_matches (key_file_error
,
1906 G_KEY_FILE_ERROR_INVALID_VALUE
))
1908 g_set_error (error
, G_KEY_FILE_ERROR
,
1909 G_KEY_FILE_ERROR_INVALID_VALUE
,
1910 _("Key file contains key '%s' "
1911 "which has value that cannot be interpreted."),
1913 g_error_free (key_file_error
);
1916 g_propagate_error (error
, key_file_error
);
1923 * g_key_file_set_boolean:
1924 * @key_file: a #GKeyFile
1925 * @group_name: a group name
1927 * @value: %TRUE or %FALSE
1929 * Associates a new boolean value with @key under @group_name.
1930 * If @key cannot be found then it is created.
1935 g_key_file_set_boolean (GKeyFile
*key_file
,
1936 const gchar
*group_name
,
1942 g_return_if_fail (key_file
!= NULL
);
1944 result
= g_key_file_parse_boolean_as_value (key_file
, value
);
1945 g_key_file_set_value (key_file
, group_name
, key
, result
);
1950 * g_key_file_get_boolean_list:
1951 * @key_file: a #GKeyFile
1952 * @group_name: a group name
1954 * @length: the number of booleans returned
1955 * @error: return location for a #GError
1957 * Returns the values associated with @key under @group_name as
1960 * If @key cannot be found then %NULL is returned and @error is set to
1961 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1962 * with @key cannot be interpreted as booleans then %NULL is returned
1963 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1965 * Return value: the values associated with the key as a list of
1966 * booleans, or %NULL if the key was not found or could not be parsed.
1971 g_key_file_get_boolean_list (GKeyFile
*key_file
,
1972 const gchar
*group_name
,
1977 GError
*key_file_error
;
1979 gboolean
*bool_values
;
1982 g_return_val_if_fail (key_file
!= NULL
, NULL
);
1983 g_return_val_if_fail (group_name
!= NULL
, NULL
);
1984 g_return_val_if_fail (key
!= NULL
, NULL
);
1989 key_file_error
= NULL
;
1991 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
1992 &num_bools
, &key_file_error
);
1995 g_propagate_error (error
, key_file_error
);
2000 bool_values
= g_new (gboolean
, num_bools
);
2002 for (i
= 0; i
< num_bools
; i
++)
2004 bool_values
[i
] = g_key_file_parse_value_as_boolean (key_file
,
2010 g_propagate_error (error
, key_file_error
);
2011 g_strfreev (values
);
2012 g_free (bool_values
);
2017 g_strfreev (values
);
2020 *length
= num_bools
;
2026 * g_key_file_set_boolean_list:
2027 * @key_file: a #GKeyFile
2028 * @group_name: a group name
2030 * @list: an array of boolean values
2031 * @length: length of @list
2033 * Associates a list of boolean values with @key under @group_name.
2034 * If @key cannot be found then it is created.
2035 * If @group_name is %NULL, the start_group is used.
2040 g_key_file_set_boolean_list (GKeyFile
*key_file
,
2041 const gchar
*group_name
,
2046 GString
*value_list
;
2049 g_return_if_fail (key_file
!= NULL
);
2050 g_return_if_fail (list
!= NULL
);
2052 value_list
= g_string_sized_new (length
* 8);
2053 for (i
= 0; i
< length
; i
++)
2057 value
= g_key_file_parse_boolean_as_value (key_file
, list
[i
]);
2059 g_string_append (value_list
, value
);
2060 g_string_append_c (value_list
, key_file
->list_separator
);
2065 g_key_file_set_value (key_file
, group_name
, key
, value_list
->str
);
2066 g_string_free (value_list
, TRUE
);
2070 * g_key_file_get_integer:
2071 * @key_file: a #GKeyFile
2072 * @group_name: a group name
2074 * @error: return location for a #GError
2076 * Returns the value associated with @key under @group_name as an
2079 * If @key cannot be found then 0 is returned and @error is set to
2080 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2081 * with @key cannot be interpreted as an integer then 0 is returned
2082 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2084 * Return value: the value associated with the key as an integer, or
2085 * 0 if the key was not found or could not be parsed.
2090 g_key_file_get_integer (GKeyFile
*key_file
,
2091 const gchar
*group_name
,
2095 GError
*key_file_error
;
2099 g_return_val_if_fail (key_file
!= NULL
, -1);
2100 g_return_val_if_fail (group_name
!= NULL
, -1);
2101 g_return_val_if_fail (key
!= NULL
, -1);
2103 key_file_error
= NULL
;
2105 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2109 g_propagate_error (error
, key_file_error
);
2113 int_value
= g_key_file_parse_value_as_integer (key_file
, value
,
2119 if (g_error_matches (key_file_error
,
2121 G_KEY_FILE_ERROR_INVALID_VALUE
))
2123 g_set_error (error
, G_KEY_FILE_ERROR
,
2124 G_KEY_FILE_ERROR_INVALID_VALUE
,
2125 _("Key file contains key '%s' in group '%s' "
2126 "which has value that cannot be interpreted."), key
,
2128 g_error_free (key_file_error
);
2131 g_propagate_error (error
, key_file_error
);
2138 * g_key_file_set_integer:
2139 * @key_file: a #GKeyFile
2140 * @group_name: a group name
2142 * @value: an integer value
2144 * Associates a new integer value with @key under @group_name.
2145 * If @key cannot be found then it is created.
2150 g_key_file_set_integer (GKeyFile
*key_file
,
2151 const gchar
*group_name
,
2157 g_return_if_fail (key_file
!= NULL
);
2159 result
= g_key_file_parse_integer_as_value (key_file
, value
);
2160 g_key_file_set_value (key_file
, group_name
, key
, result
);
2165 * g_key_file_get_int64:
2166 * @key_file: a non-%NULL #GKeyFile
2167 * @group_name: a non-%NULL group name
2168 * @key: a non-%NULL key
2169 * @error: return location for a #GError
2171 * Returns the value associated with @key under @group_name as a signed
2172 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2173 * 64-bit results without truncation.
2175 * Returns: the value associated with the key as a signed 64-bit integer, or
2176 * 0 if the key was not found or could not be parsed.
2181 g_key_file_get_int64 (GKeyFile
*key_file
,
2182 const gchar
*group_name
,
2189 g_return_val_if_fail (key_file
!= NULL
, -1);
2190 g_return_val_if_fail (group_name
!= NULL
, -1);
2191 g_return_val_if_fail (key
!= NULL
, -1);
2193 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2198 v
= g_ascii_strtoll (s
, &end
, 10);
2200 if (*s
== '\0' || *end
!= '\0')
2202 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2203 "Key '%s' in group '%s' has value '%s' where int64 was expected",
2204 key
, group_name
, s
);
2213 * g_key_file_set_int64:
2214 * @key_file: a #GKeyFile
2215 * @group_name: a group name
2217 * @value: an integer value
2219 * Associates a new integer value with @key under @group_name.
2220 * If @key cannot be found then it is created.
2225 g_key_file_set_int64 (GKeyFile
*key_file
,
2226 const gchar
*group_name
,
2232 g_return_if_fail (key_file
!= NULL
);
2234 result
= g_strdup_printf ("%" G_GINT64_FORMAT
, value
);
2235 g_key_file_set_value (key_file
, group_name
, key
, result
);
2240 * g_key_file_get_uint64:
2241 * @key_file: a non-%NULL #GKeyFile
2242 * @group_name: a non-%NULL group name
2243 * @key: a non-%NULL key
2244 * @error: return location for a #GError
2246 * Returns the value associated with @key under @group_name as an unsigned
2247 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2248 * large positive results without truncation.
2250 * Returns: the value associated with the key as an unsigned 64-bit integer,
2251 * or 0 if the key was not found or could not be parsed.
2256 g_key_file_get_uint64 (GKeyFile
*key_file
,
2257 const gchar
*group_name
,
2264 g_return_val_if_fail (key_file
!= NULL
, -1);
2265 g_return_val_if_fail (group_name
!= NULL
, -1);
2266 g_return_val_if_fail (key
!= NULL
, -1);
2268 s
= g_key_file_get_value (key_file
, group_name
, key
, error
);
2273 v
= g_ascii_strtoull (s
, &end
, 10);
2275 if (*s
== '\0' || *end
!= '\0')
2277 g_set_error (error
, G_KEY_FILE_ERROR
, G_KEY_FILE_ERROR_INVALID_VALUE
,
2278 "Key '%s' in group '%s' has value '%s' where uint64 was expected",
2279 key
, group_name
, s
);
2288 * g_key_file_set_uint64:
2289 * @key_file: a #GKeyFile
2290 * @group_name: a group name
2292 * @value: an integer value
2294 * Associates a new integer value with @key under @group_name.
2295 * If @key cannot be found then it is created.
2300 g_key_file_set_uint64 (GKeyFile
*key_file
,
2301 const gchar
*group_name
,
2307 g_return_if_fail (key_file
!= NULL
);
2309 result
= g_strdup_printf ("%" G_GUINT64_FORMAT
, value
);
2310 g_key_file_set_value (key_file
, group_name
, key
, result
);
2315 * g_key_file_get_integer_list:
2316 * @key_file: a #GKeyFile
2317 * @group_name: a group name
2319 * @length: the number of integers returned
2320 * @error: return location for a #GError
2322 * Returns the values associated with @key under @group_name as
2325 * If @key cannot be found then %NULL is returned and @error is set to
2326 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2327 * with @key cannot be interpreted as integers then %NULL is returned
2328 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2330 * Return value: the values associated with the key as a list of
2331 * integers, or %NULL if the key was not found or could not be parsed.
2336 g_key_file_get_integer_list (GKeyFile
*key_file
,
2337 const gchar
*group_name
,
2342 GError
*key_file_error
= NULL
;
2347 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2348 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2349 g_return_val_if_fail (key
!= NULL
, NULL
);
2354 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2355 &num_ints
, &key_file_error
);
2358 g_propagate_error (error
, key_file_error
);
2363 int_values
= g_new (gint
, num_ints
);
2365 for (i
= 0; i
< num_ints
; i
++)
2367 int_values
[i
] = g_key_file_parse_value_as_integer (key_file
,
2373 g_propagate_error (error
, key_file_error
);
2374 g_strfreev (values
);
2375 g_free (int_values
);
2380 g_strfreev (values
);
2389 * g_key_file_set_integer_list:
2390 * @key_file: a #GKeyFile
2391 * @group_name: a group name
2393 * @list: an array of integer values
2394 * @length: number of integer values in @list
2396 * Associates a list of integer values with @key under @group_name.
2397 * If @key cannot be found then it is created.
2402 g_key_file_set_integer_list (GKeyFile
*key_file
,
2403 const gchar
*group_name
,
2411 g_return_if_fail (key_file
!= NULL
);
2412 g_return_if_fail (list
!= NULL
);
2414 values
= g_string_sized_new (length
* 16);
2415 for (i
= 0; i
< length
; i
++)
2419 value
= g_key_file_parse_integer_as_value (key_file
, list
[i
]);
2421 g_string_append (values
, value
);
2422 g_string_append_c (values
, key_file
->list_separator
);
2427 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
2428 g_string_free (values
, TRUE
);
2432 * g_key_file_get_double:
2433 * @key_file: a #GKeyFile
2434 * @group_name: a group name
2436 * @error: return location for a #GError
2438 * Returns the value associated with @key under @group_name as a
2439 * double. If @group_name is %NULL, the start_group is used.
2441 * If @key cannot be found then 0.0 is returned and @error is set to
2442 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2443 * with @key cannot be interpreted as a double then 0.0 is returned
2444 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2446 * Return value: the value associated with the key as a double, or
2447 * 0.0 if the key was not found or could not be parsed.
2452 g_key_file_get_double (GKeyFile
*key_file
,
2453 const gchar
*group_name
,
2457 GError
*key_file_error
;
2459 gdouble double_value
;
2461 g_return_val_if_fail (key_file
!= NULL
, -1);
2462 g_return_val_if_fail (group_name
!= NULL
, -1);
2463 g_return_val_if_fail (key
!= NULL
, -1);
2465 key_file_error
= NULL
;
2467 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
2471 g_propagate_error (error
, key_file_error
);
2475 double_value
= g_key_file_parse_value_as_double (key_file
, value
,
2481 if (g_error_matches (key_file_error
,
2483 G_KEY_FILE_ERROR_INVALID_VALUE
))
2485 g_set_error (error
, G_KEY_FILE_ERROR
,
2486 G_KEY_FILE_ERROR_INVALID_VALUE
,
2487 _("Key file contains key '%s' in group '%s' "
2488 "which has value that cannot be interpreted."), key
,
2490 g_error_free (key_file_error
);
2493 g_propagate_error (error
, key_file_error
);
2496 return double_value
;
2500 * g_key_file_set_double:
2501 * @key_file: a #GKeyFile
2502 * @group_name: a group name
2504 * @value: an double value
2506 * Associates a new double value with @key under @group_name.
2507 * If @key cannot be found then it is created.
2512 g_key_file_set_double (GKeyFile
*key_file
,
2513 const gchar
*group_name
,
2517 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
2519 g_return_if_fail (key_file
!= NULL
);
2521 g_ascii_dtostr (result
, sizeof (result
), value
);
2522 g_key_file_set_value (key_file
, group_name
, key
, result
);
2526 * g_key_file_get_double_list:
2527 * @key_file: a #GKeyFile
2528 * @group_name: a group name
2530 * @length: the number of doubles returned
2531 * @error: return location for a #GError
2533 * Returns the values associated with @key under @group_name as
2536 * If @key cannot be found then %NULL is returned and @error is set to
2537 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2538 * with @key cannot be interpreted as doubles then %NULL is returned
2539 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2541 * Return value: the values associated with the key as a list of
2542 * doubles, or %NULL if the key was not found or could not be parsed.
2547 g_key_file_get_double_list (GKeyFile
*key_file
,
2548 const gchar
*group_name
,
2553 GError
*key_file_error
= NULL
;
2555 gdouble
*double_values
;
2556 gsize i
, num_doubles
;
2558 g_return_val_if_fail (key_file
!= NULL
, NULL
);
2559 g_return_val_if_fail (group_name
!= NULL
, NULL
);
2560 g_return_val_if_fail (key
!= NULL
, NULL
);
2565 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2566 &num_doubles
, &key_file_error
);
2569 g_propagate_error (error
, key_file_error
);
2574 double_values
= g_new (gdouble
, num_doubles
);
2576 for (i
= 0; i
< num_doubles
; i
++)
2578 double_values
[i
] = g_key_file_parse_value_as_double (key_file
,
2584 g_propagate_error (error
, key_file_error
);
2585 g_strfreev (values
);
2586 g_free (double_values
);
2591 g_strfreev (values
);
2594 *length
= num_doubles
;
2596 return double_values
;
2600 * g_key_file_set_double_list:
2601 * @key_file: a #GKeyFile
2602 * @group_name: a group name
2604 * @list: an array of double values
2605 * @length: number of double values in @list
2607 * Associates a list of double values with @key under
2608 * @group_name. If @key cannot be found then it is created.
2613 g_key_file_set_double_list (GKeyFile
*key_file
,
2614 const gchar
*group_name
,
2622 g_return_if_fail (key_file
!= NULL
);
2623 g_return_if_fail (list
!= NULL
);
2625 values
= g_string_sized_new (length
* 16);
2626 for (i
= 0; i
< length
; i
++)
2628 gchar result
[G_ASCII_DTOSTR_BUF_SIZE
];
2630 g_ascii_dtostr( result
, sizeof (result
), list
[i
] );
2632 g_string_append (values
, result
);
2633 g_string_append_c (values
, key_file
->list_separator
);
2636 g_key_file_set_value (key_file
, group_name
, key
, values
->str
);
2637 g_string_free (values
, TRUE
);
2641 g_key_file_set_key_comment (GKeyFile
*key_file
,
2642 const gchar
*group_name
,
2644 const gchar
*comment
,
2647 GKeyFileGroup
*group
;
2648 GKeyFileKeyValuePair
*pair
;
2649 GList
*key_node
, *comment_node
, *tmp
;
2651 group
= g_key_file_lookup_group (key_file
, group_name
);
2654 g_set_error (error
, G_KEY_FILE_ERROR
,
2655 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
2656 _("Key file does not have group '%s'"),
2657 group_name
? group_name
: "(null)");
2662 /* First find the key the comments are supposed to be
2665 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
2667 if (key_node
== NULL
)
2669 g_set_error (error
, G_KEY_FILE_ERROR
,
2670 G_KEY_FILE_ERROR_KEY_NOT_FOUND
,
2671 _("Key file does not have key '%s' in group '%s'"),
2676 /* Then find all the comments already associated with the
2679 tmp
= key_node
->next
;
2682 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
2684 if (pair
->key
!= NULL
)
2689 g_key_file_remove_key_value_pair_node (key_file
, group
,
2693 if (comment
== NULL
)
2696 /* Now we can add our new comment
2698 pair
= g_slice_new (GKeyFileKeyValuePair
);
2700 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
2702 key_node
= g_list_insert (key_node
, pair
, 1);
2708 g_key_file_set_group_comment (GKeyFile
*key_file
,
2709 const gchar
*group_name
,
2710 const gchar
*comment
,
2713 GKeyFileGroup
*group
;
2715 g_return_val_if_fail (g_key_file_is_group_name (group_name
), FALSE
);
2717 group
= g_key_file_lookup_group (key_file
, group_name
);
2720 g_set_error (error
, G_KEY_FILE_ERROR
,
2721 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
2722 _("Key file does not have group '%s'"),
2723 group_name
? group_name
: "(null)");
2728 /* First remove any existing comment
2732 g_key_file_key_value_pair_free (group
->comment
);
2733 group
->comment
= NULL
;
2736 if (comment
== NULL
)
2739 /* Now we can add our new comment
2741 group
->comment
= g_slice_new (GKeyFileKeyValuePair
);
2742 group
->comment
->key
= NULL
;
2743 group
->comment
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
2749 g_key_file_set_top_comment (GKeyFile
*key_file
,
2750 const gchar
*comment
,
2754 GKeyFileGroup
*group
;
2755 GKeyFileKeyValuePair
*pair
;
2757 /* The last group in the list should be the top (comments only)
2760 g_warn_if_fail (key_file
->groups
!= NULL
);
2761 group_node
= g_list_last (key_file
->groups
);
2762 group
= (GKeyFileGroup
*) group_node
->data
;
2763 g_warn_if_fail (group
->name
== NULL
);
2765 /* Note all keys must be comments at the top of
2766 * the file, so we can just free it all.
2768 if (group
->key_value_pairs
!= NULL
)
2770 g_list_foreach (group
->key_value_pairs
,
2771 (GFunc
) g_key_file_key_value_pair_free
,
2773 g_list_free (group
->key_value_pairs
);
2774 group
->key_value_pairs
= NULL
;
2777 if (comment
== NULL
)
2780 pair
= g_slice_new (GKeyFileKeyValuePair
);
2782 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
2784 group
->key_value_pairs
=
2785 g_list_prepend (group
->key_value_pairs
, pair
);
2791 * g_key_file_set_comment:
2792 * @key_file: a #GKeyFile
2793 * @group_name: a group name, or %NULL
2795 * @comment: a comment
2796 * @error: return location for a #GError
2798 * Places a comment above @key from @group_name.
2799 * If @key is %NULL then @comment will be written above @group_name.
2800 * If both @key and @group_name are %NULL, then @comment will be
2801 * written above the first group in the file.
2803 * Returns: %TRUE if the comment was written, %FALSE otherwise
2808 g_key_file_set_comment (GKeyFile
*key_file
,
2809 const gchar
*group_name
,
2811 const gchar
*comment
,
2814 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
2816 if (group_name
!= NULL
&& key
!= NULL
)
2818 if (!g_key_file_set_key_comment (key_file
, group_name
, key
, comment
, error
))
2821 else if (group_name
!= NULL
)
2823 if (!g_key_file_set_group_comment (key_file
, group_name
, comment
, error
))
2828 if (!g_key_file_set_top_comment (key_file
, comment
, error
))
2832 if (comment
!= NULL
)
2833 key_file
->approximate_size
+= strlen (comment
);
2839 g_key_file_get_key_comment (GKeyFile
*key_file
,
2840 const gchar
*group_name
,
2844 GKeyFileGroup
*group
;
2845 GKeyFileKeyValuePair
*pair
;
2846 GList
*key_node
, *tmp
;
2850 g_return_val_if_fail (g_key_file_is_group_name (group_name
), NULL
);
2852 group
= g_key_file_lookup_group (key_file
, group_name
);
2855 g_set_error (error
, G_KEY_FILE_ERROR
,
2856 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
2857 _("Key file does not have group '%s'"),
2858 group_name
? group_name
: "(null)");
2863 /* First find the key the comments are supposed to be
2866 key_node
= g_key_file_lookup_key_value_pair_node (key_file
, group
, key
);
2868 if (key_node
== NULL
)
2870 g_set_error (error
, G_KEY_FILE_ERROR
,
2871 G_KEY_FILE_ERROR_KEY_NOT_FOUND
,
2872 _("Key file does not have key '%s' in group '%s'"),
2879 /* Then find all the comments already associated with the
2880 * key and concatentate them.
2882 tmp
= key_node
->next
;
2883 if (!key_node
->next
)
2886 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
2887 if (pair
->key
!= NULL
)
2892 pair
= (GKeyFileKeyValuePair
*) tmp
->next
->data
;
2894 if (pair
->key
!= NULL
)
2900 while (tmp
!= key_node
)
2902 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
2905 string
= g_string_sized_new (512);
2907 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
2908 g_string_append (string
, comment
);
2916 comment
= string
->str
;
2917 g_string_free (string
, FALSE
);
2926 get_group_comment (GKeyFile
*key_file
,
2927 GKeyFileGroup
*group
,
2936 tmp
= group
->key_value_pairs
;
2939 GKeyFileKeyValuePair
*pair
;
2941 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
2943 if (pair
->key
!= NULL
)
2949 if (tmp
->next
== NULL
)
2957 GKeyFileKeyValuePair
*pair
;
2959 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
2962 string
= g_string_sized_new (512);
2964 comment
= g_key_file_parse_value_as_comment (key_file
, pair
->value
);
2965 g_string_append (string
, comment
);
2972 return g_string_free (string
, FALSE
);
2978 g_key_file_get_group_comment (GKeyFile
*key_file
,
2979 const gchar
*group_name
,
2983 GKeyFileGroup
*group
;
2985 group
= g_key_file_lookup_group (key_file
, group_name
);
2988 g_set_error (error
, G_KEY_FILE_ERROR
,
2989 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
2990 _("Key file does not have group '%s'"),
2991 group_name
? group_name
: "(null)");
2997 return g_strdup (group
->comment
->value
);
2999 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3000 group_node
= group_node
->next
;
3001 group
= (GKeyFileGroup
*)group_node
->data
;
3002 return get_group_comment (key_file
, group
, error
);
3006 g_key_file_get_top_comment (GKeyFile
*key_file
,
3010 GKeyFileGroup
*group
;
3012 /* The last group in the list should be the top (comments only)
3015 g_warn_if_fail (key_file
->groups
!= NULL
);
3016 group_node
= g_list_last (key_file
->groups
);
3017 group
= (GKeyFileGroup
*) group_node
->data
;
3018 g_warn_if_fail (group
->name
== NULL
);
3020 return get_group_comment (key_file
, group
, error
);
3024 * g_key_file_get_comment:
3025 * @key_file: a #GKeyFile
3026 * @group_name: a group name, or %NULL
3028 * @error: return location for a #GError
3030 * Retrieves a comment above @key from @group_name.
3031 * If @key is %NULL then @comment will be read from above
3032 * @group_name. If both @key and @group_name are %NULL, then
3033 * @comment will be read from above the first group in the file.
3035 * Returns: a comment that should be freed with g_free()
3040 g_key_file_get_comment (GKeyFile
*key_file
,
3041 const gchar
*group_name
,
3045 g_return_val_if_fail (key_file
!= NULL
, NULL
);
3047 if (group_name
!= NULL
&& key
!= NULL
)
3048 return g_key_file_get_key_comment (key_file
, group_name
, key
, error
);
3049 else if (group_name
!= NULL
)
3050 return g_key_file_get_group_comment (key_file
, group_name
, error
);
3052 return g_key_file_get_top_comment (key_file
, error
);
3056 * g_key_file_remove_comment:
3057 * @key_file: a #GKeyFile
3058 * @group_name: a group name, or %NULL
3060 * @error: return location for a #GError
3062 * Removes a comment above @key from @group_name.
3063 * If @key is %NULL then @comment will be removed above @group_name.
3064 * If both @key and @group_name are %NULL, then @comment will
3065 * be removed above the first group in the file.
3067 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3073 g_key_file_remove_comment (GKeyFile
*key_file
,
3074 const gchar
*group_name
,
3078 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3080 if (group_name
!= NULL
&& key
!= NULL
)
3081 return g_key_file_set_key_comment (key_file
, group_name
, key
, NULL
, error
);
3082 else if (group_name
!= NULL
)
3083 return g_key_file_set_group_comment (key_file
, group_name
, NULL
, error
);
3085 return g_key_file_set_top_comment (key_file
, NULL
, error
);
3089 * g_key_file_has_group:
3090 * @key_file: a #GKeyFile
3091 * @group_name: a group name
3093 * Looks whether the key file has the group @group_name.
3095 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3100 g_key_file_has_group (GKeyFile
*key_file
,
3101 const gchar
*group_name
)
3103 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3104 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3106 return g_key_file_lookup_group (key_file
, group_name
) != NULL
;
3110 * g_key_file_has_key:
3111 * @key_file: a #GKeyFile
3112 * @group_name: a group name
3114 * @error: return location for a #GError
3116 * Looks whether the key file has the key @key in the group
3119 * Return value: %TRUE if @key is a part of @group_name, %FALSE
3125 g_key_file_has_key (GKeyFile
*key_file
,
3126 const gchar
*group_name
,
3130 GKeyFileKeyValuePair
*pair
;
3131 GKeyFileGroup
*group
;
3133 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3134 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3135 g_return_val_if_fail (key
!= NULL
, FALSE
);
3137 group
= g_key_file_lookup_group (key_file
, group_name
);
3141 g_set_error (error
, G_KEY_FILE_ERROR
,
3142 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3143 _("Key file does not have group '%s'"),
3144 group_name
? group_name
: "(null)");
3149 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3151 return pair
!= NULL
;
3155 g_key_file_add_group (GKeyFile
*key_file
,
3156 const gchar
*group_name
)
3158 GKeyFileGroup
*group
;
3160 g_return_if_fail (key_file
!= NULL
);
3161 g_return_if_fail (g_key_file_is_group_name (group_name
));
3163 group
= g_key_file_lookup_group (key_file
, group_name
);
3166 key_file
->current_group
= group
;
3170 group
= g_slice_new0 (GKeyFileGroup
);
3171 group
->name
= g_strdup (group_name
);
3172 group
->lookup_map
= g_hash_table_new (g_str_hash
, g_str_equal
);
3173 key_file
->groups
= g_list_prepend (key_file
->groups
, group
);
3174 key_file
->approximate_size
+= strlen (group_name
) + 3;
3175 key_file
->current_group
= group
;
3177 if (key_file
->start_group
== NULL
)
3178 key_file
->start_group
= group
;
3180 g_hash_table_insert (key_file
->group_hash
, (gpointer
)group
->name
, group
);
3184 g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
)
3189 g_free (pair
->value
);
3190 g_slice_free (GKeyFileKeyValuePair
, pair
);
3194 /* Be careful not to call this function on a node with data in the
3195 * lookup map without removing it from the lookup map, first.
3197 * Some current cases where this warning is not a concern are
3199 * - the node being removed is a comment node
3200 * - the entire lookup map is getting destroyed soon after
3204 g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
3205 GKeyFileGroup
*group
,
3209 GKeyFileKeyValuePair
*pair
;
3211 pair
= (GKeyFileKeyValuePair
*) pair_node
->data
;
3213 group
->key_value_pairs
= g_list_remove_link (group
->key_value_pairs
, pair_node
);
3215 if (pair
->key
!= NULL
)
3216 key_file
->approximate_size
-= strlen (pair
->key
) + 1;
3218 g_warn_if_fail (pair
->value
!= NULL
);
3219 key_file
->approximate_size
-= strlen (pair
->value
);
3221 g_key_file_key_value_pair_free (pair
);
3223 g_list_free_1 (pair_node
);
3227 g_key_file_remove_group_node (GKeyFile
*key_file
,
3230 GKeyFileGroup
*group
;
3233 group
= (GKeyFileGroup
*) group_node
->data
;
3236 g_hash_table_remove (key_file
->group_hash
, group
->name
);
3238 /* If the current group gets deleted make the current group the last
3241 if (key_file
->current_group
== group
)
3243 /* groups should always contain at least the top comment group,
3244 * unless g_key_file_clear has been called
3246 if (key_file
->groups
)
3247 key_file
->current_group
= (GKeyFileGroup
*) key_file
->groups
->data
;
3249 key_file
->current_group
= NULL
;
3252 /* If the start group gets deleted make the start group the first
3255 if (key_file
->start_group
== group
)
3257 tmp
= g_list_last (key_file
->groups
);
3260 if (tmp
!= group_node
&&
3261 ((GKeyFileGroup
*) tmp
->data
)->name
!= NULL
)
3268 key_file
->start_group
= (GKeyFileGroup
*) tmp
->data
;
3270 key_file
->start_group
= NULL
;
3273 key_file
->groups
= g_list_remove_link (key_file
->groups
, group_node
);
3275 if (group
->name
!= NULL
)
3276 key_file
->approximate_size
-= strlen (group
->name
) + 3;
3278 tmp
= group
->key_value_pairs
;
3285 g_key_file_remove_key_value_pair_node (key_file
, group
, pair_node
);
3288 g_warn_if_fail (group
->key_value_pairs
== NULL
);
3290 if (group
->lookup_map
)
3292 g_hash_table_destroy (group
->lookup_map
);
3293 group
->lookup_map
= NULL
;
3296 g_free ((gchar
*) group
->name
);
3297 g_slice_free (GKeyFileGroup
, group
);
3298 g_list_free_1 (group_node
);
3302 * g_key_file_remove_group:
3303 * @key_file: a #GKeyFile
3304 * @group_name: a group name
3305 * @error: return location for a #GError or %NULL
3307 * Removes the specified group, @group_name,
3308 * from the key file.
3310 * Returns: %TRUE if the group was removed, %FALSE otherwise
3315 g_key_file_remove_group (GKeyFile
*key_file
,
3316 const gchar
*group_name
,
3321 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3322 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3324 group_node
= g_key_file_lookup_group_node (key_file
, group_name
);
3328 g_set_error (error
, G_KEY_FILE_ERROR
,
3329 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3330 _("Key file does not have group '%s'"),
3335 g_key_file_remove_group_node (key_file
, group_node
);
3341 g_key_file_add_key (GKeyFile
*key_file
,
3342 GKeyFileGroup
*group
,
3346 GKeyFileKeyValuePair
*pair
;
3348 pair
= g_slice_new (GKeyFileKeyValuePair
);
3349 pair
->key
= g_strdup (key
);
3350 pair
->value
= g_strdup (value
);
3352 g_hash_table_replace (group
->lookup_map
, pair
->key
, pair
);
3353 group
->key_value_pairs
= g_list_prepend (group
->key_value_pairs
, pair
);
3354 group
->has_trailing_blank_line
= FALSE
;
3355 key_file
->approximate_size
+= strlen (key
) + strlen (value
) + 2;
3359 * g_key_file_remove_key:
3360 * @key_file: a #GKeyFile
3361 * @group_name: a group name
3362 * @key: a key name to remove
3363 * @error: return location for a #GError or %NULL
3365 * Removes @key in @group_name from the key file.
3367 * Returns: %TRUE if the key was removed, %FALSE otherwise
3372 g_key_file_remove_key (GKeyFile
*key_file
,
3373 const gchar
*group_name
,
3377 GKeyFileGroup
*group
;
3378 GKeyFileKeyValuePair
*pair
;
3380 g_return_val_if_fail (key_file
!= NULL
, FALSE
);
3381 g_return_val_if_fail (group_name
!= NULL
, FALSE
);
3382 g_return_val_if_fail (key
!= NULL
, FALSE
);
3386 group
= g_key_file_lookup_group (key_file
, group_name
);
3389 g_set_error (error
, G_KEY_FILE_ERROR
,
3390 G_KEY_FILE_ERROR_GROUP_NOT_FOUND
,
3391 _("Key file does not have group '%s'"),
3392 group_name
? group_name
: "(null)");
3396 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3400 g_set_error (error
, G_KEY_FILE_ERROR
,
3401 G_KEY_FILE_ERROR_KEY_NOT_FOUND
,
3402 _("Key file does not have key '%s' in group '%s'"),
3407 key_file
->approximate_size
-= strlen (pair
->key
) + strlen (pair
->value
) + 2;
3409 group
->key_value_pairs
= g_list_remove (group
->key_value_pairs
, pair
);
3410 g_hash_table_remove (group
->lookup_map
, pair
->key
);
3411 g_key_file_key_value_pair_free (pair
);
3417 g_key_file_lookup_group_node (GKeyFile
*key_file
,
3418 const gchar
*group_name
)
3420 GKeyFileGroup
*group
;
3423 for (tmp
= key_file
->groups
; tmp
!= NULL
; tmp
= tmp
->next
)
3425 group
= (GKeyFileGroup
*) tmp
->data
;
3427 if (group
&& group
->name
&& strcmp (group
->name
, group_name
) == 0)
3434 static GKeyFileGroup
*
3435 g_key_file_lookup_group (GKeyFile
*key_file
,
3436 const gchar
*group_name
)
3438 return (GKeyFileGroup
*)g_hash_table_lookup (key_file
->group_hash
, group_name
);
3442 g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
3443 GKeyFileGroup
*group
,
3448 for (key_node
= group
->key_value_pairs
;
3450 key_node
= key_node
->next
)
3452 GKeyFileKeyValuePair
*pair
;
3454 pair
= (GKeyFileKeyValuePair
*) key_node
->data
;
3456 if (pair
->key
&& strcmp (pair
->key
, key
) == 0)
3463 static GKeyFileKeyValuePair
*
3464 g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
3465 GKeyFileGroup
*group
,
3468 return (GKeyFileKeyValuePair
*) g_hash_table_lookup (group
->lookup_map
, key
);
3471 /* Lines starting with # or consisting entirely of whitespace are merely
3472 * recorded, not parsed. This function assumes all leading whitespace
3473 * has been stripped.
3476 g_key_file_line_is_comment (const gchar
*line
)
3478 return (*line
== '#' || *line
== '\0' || *line
== '\n');
3482 g_key_file_is_group_name (const gchar
*name
)
3489 p
= q
= (gchar
*) name
;
3490 while (*q
&& *q
!= ']' && *q
!= '[' && !g_ascii_iscntrl (*q
))
3491 q
= g_utf8_find_next_char (q
, NULL
);
3493 if (*q
!= '\0' || q
== p
)
3500 g_key_file_is_key_name (const gchar
*name
)
3507 p
= q
= (gchar
*) name
;
3508 /* We accept a little more than the desktop entry spec says,
3509 * since gnome-vfs uses mime-types as keys in its cache.
3511 while (*q
&& *q
!= '=' && *q
!= '[' && *q
!= ']')
3512 q
= g_utf8_find_next_char (q
, NULL
);
3514 /* No empty keys, please */
3518 /* We accept spaces in the middle of keys to not break
3519 * existing apps, but we don't tolerate initial or final
3520 * spaces, which would lead to silent corruption when
3521 * rereading the file.
3523 if (*p
== ' ' || q
[-1] == ' ')
3529 while (*q
&& (g_unichar_isalnum (g_utf8_get_char_validated (q
, -1)) || *q
== '-' || *q
== '_' || *q
== '.' || *q
== '@'))
3530 q
= g_utf8_find_next_char (q
, NULL
);
3544 /* A group in a key file is made up of a starting '[' followed by one
3545 * or more letters making up the group name followed by ']'.
3548 g_key_file_line_is_group (const gchar
*line
)
3558 while (*p
&& *p
!= ']')
3559 p
= g_utf8_find_next_char (p
, NULL
);
3564 /* silently accept whitespace after the ] */
3565 p
= g_utf8_find_next_char (p
, NULL
);
3566 while (*p
== ' ' || *p
== '\t')
3567 p
= g_utf8_find_next_char (p
, NULL
);
3576 g_key_file_line_is_key_value_pair (const gchar
*line
)
3580 p
= (gchar
*) g_utf8_strchr (line
, -1, '=');
3585 /* Key must be non-empty
3594 g_key_file_parse_value_as_string (GKeyFile
*key_file
,
3599 gchar
*string_value
, *p
, *q0
, *q
;
3601 string_value
= g_new (gchar
, strlen (value
) + 1);
3603 p
= (gchar
*) value
;
3604 q0
= q
= string_value
;
3634 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
3635 G_KEY_FILE_ERROR_INVALID_VALUE
,
3636 _("Key file contains escape character "
3641 if (pieces
&& *p
== key_file
->list_separator
)
3642 *q
= key_file
->list_separator
;
3656 g_set_error (error
, G_KEY_FILE_ERROR
,
3657 G_KEY_FILE_ERROR_INVALID_VALUE
,
3658 _("Key file contains invalid escape "
3659 "sequence '%s'"), sequence
);
3668 if (pieces
&& (*p
== key_file
->list_separator
))
3670 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
3686 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
3687 *pieces
= g_slist_reverse (*pieces
);
3690 return string_value
;
3694 g_key_file_parse_string_as_value (GKeyFile
*key_file
,
3695 const gchar
*string
,
3696 gboolean escape_separator
)
3698 gchar
*value
, *p
, *q
;
3700 gboolean parsing_leading_space
;
3702 length
= strlen (string
) + 1;
3704 /* Worst case would be that every character needs to be escaped.
3705 * In other words every character turns to two characters
3707 value
= g_new (gchar
, 2 * length
);
3709 p
= (gchar
*) string
;
3711 parsing_leading_space
= TRUE
;
3712 while (p
< (string
+ length
- 1))
3714 gchar escaped_character
[3] = { '\\', 0, 0 };
3719 if (parsing_leading_space
)
3721 escaped_character
[1] = 's';
3722 strcpy (q
, escaped_character
);
3732 if (parsing_leading_space
)
3734 escaped_character
[1] = 't';
3735 strcpy (q
, escaped_character
);
3745 escaped_character
[1] = 'n';
3746 strcpy (q
, escaped_character
);
3750 escaped_character
[1] = 'r';
3751 strcpy (q
, escaped_character
);
3755 escaped_character
[1] = '\\';
3756 strcpy (q
, escaped_character
);
3758 parsing_leading_space
= FALSE
;
3761 if (escape_separator
&& *p
== key_file
->list_separator
)
3763 escaped_character
[1] = key_file
->list_separator
;
3764 strcpy (q
, escaped_character
);
3766 parsing_leading_space
= TRUE
;
3772 parsing_leading_space
= FALSE
;
3784 g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
3788 gchar
*end_of_valid_int
;
3793 long_value
= strtol (value
, &end_of_valid_int
, 10);
3795 if (*value
== '\0' || *end_of_valid_int
!= '\0')
3797 gchar
*value_utf8
= _g_utf8_make_valid (value
);
3798 g_set_error (error
, G_KEY_FILE_ERROR
,
3799 G_KEY_FILE_ERROR_INVALID_VALUE
,
3800 _("Value '%s' cannot be interpreted "
3801 "as a number."), value_utf8
);
3802 g_free (value_utf8
);
3807 int_value
= long_value
;
3808 if (int_value
!= long_value
|| errno
== ERANGE
)
3810 gchar
*value_utf8
= _g_utf8_make_valid (value
);
3813 G_KEY_FILE_ERROR_INVALID_VALUE
,
3814 _("Integer value '%s' out of range"),
3816 g_free (value_utf8
);
3825 g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
3829 return g_strdup_printf ("%d", value
);
3833 g_key_file_parse_value_as_double (GKeyFile
*key_file
,
3837 gchar
*end_of_valid_d
;
3838 gdouble double_value
= 0;
3840 double_value
= g_ascii_strtod (value
, &end_of_valid_d
);
3842 if (*end_of_valid_d
!= '\0' || end_of_valid_d
== value
)
3844 gchar
*value_utf8
= _g_utf8_make_valid (value
);
3845 g_set_error (error
, G_KEY_FILE_ERROR
,
3846 G_KEY_FILE_ERROR_INVALID_VALUE
,
3847 _("Value '%s' cannot be interpreted "
3848 "as a float number."),
3850 g_free (value_utf8
);
3853 return double_value
;
3857 g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
3863 if (strcmp (value
, "true") == 0 || strcmp (value
, "1") == 0)
3865 else if (strcmp (value
, "false") == 0 || strcmp (value
, "0") == 0)
3868 value_utf8
= _g_utf8_make_valid (value
);
3869 g_set_error (error
, G_KEY_FILE_ERROR
,
3870 G_KEY_FILE_ERROR_INVALID_VALUE
,
3871 _("Value '%s' cannot be interpreted "
3872 "as a boolean."), value_utf8
);
3873 g_free (value_utf8
);
3879 g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
3883 return g_strdup ("true");
3885 return g_strdup ("false");
3889 g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
3896 string
= g_string_sized_new (512);
3898 lines
= g_strsplit (value
, "\n", 0);
3900 for (i
= 0; lines
[i
] != NULL
; i
++)
3902 if (lines
[i
][0] != '#')
3903 g_string_append_printf (string
, "%s\n", lines
[i
]);
3905 g_string_append_printf (string
, "%s\n", lines
[i
] + 1);
3909 return g_string_free (string
, FALSE
);
3913 g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
3914 const gchar
*comment
)
3920 string
= g_string_sized_new (512);
3922 lines
= g_strsplit (comment
, "\n", 0);
3924 for (i
= 0; lines
[i
] != NULL
; i
++)
3925 g_string_append_printf (string
, "#%s%s", lines
[i
],
3926 lines
[i
+ 1] == NULL
? "" : "\n");
3929 return g_string_free (string
, FALSE
);