diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 9c2a7f81a..3b5170ccc 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1822,6 +1822,7 @@ g_key_file_load_from_data g_key_file_load_from_data_dirs g_key_file_load_from_dirs g_key_file_to_data +g_key_file_save_to_file g_key_file_get_start_group g_key_file_get_groups g_key_file_get_keys diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c index ae206897d..cf063e173 100644 --- a/glib/gkeyfile.c +++ b/glib/gkeyfile.c @@ -4372,3 +4372,41 @@ g_key_file_parse_comment_as_value (GKeyFile *key_file, return g_string_free (string, FALSE); } + +/** + * g_key_file_save_to_file: + * @key_file: a #GKeyFile + * @filename: the name of the file to write to + * @error: a pointer to a %NULL #GError, or %NULL + * + * Writes the contents of @key_file to @filename using + * g_file_set_contents(). + * + * This function can fail for any of the reasons that + * g_file_set_contents() may fail. + * + * Returns: %TRUE if successful, else %FALSE with @error set + * + * Since: 2.40 + */ +gboolean +g_key_file_save_to_file (GKeyFile *key_file, + const gchar *filename, + GError **error) +{ + gchar *contents; + gboolean success; + gsize length; + + g_return_val_if_fail (key_file != NULL, FALSE); + g_return_val_if_fail (filename != NULL, FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + contents = g_key_file_to_data (key_file, &length, NULL); + g_assert (contents != NULL); + + success = g_file_set_contents (filename, contents, length, error); + g_free (contents); + + return success; +} diff --git a/glib/gkeyfile.h b/glib/gkeyfile.h index b37070e36..3fdfc2972 100644 --- a/glib/gkeyfile.h +++ b/glib/gkeyfile.h @@ -94,6 +94,10 @@ GLIB_AVAILABLE_IN_ALL gchar *g_key_file_to_data (GKeyFile *key_file, gsize *length, GError **error) G_GNUC_MALLOC; +GLIB_AVAILABLE_IN_2_40 +gboolean g_key_file_save_to_file (GKeyFile *key_file, + const gchar *filename, + GError **error); GLIB_AVAILABLE_IN_ALL gchar *g_key_file_get_start_group (GKeyFile *key_file) G_GNUC_MALLOC; GLIB_AVAILABLE_IN_ALL