[glib] Add g_key_file_save_to_file()



commit 5d7a7df867543644a4dc55ba980833743ffa1859
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Oct 3 12:39:53 2013 -0400

    Add g_key_file_save_to_file()
    
    To write a keyfile to disk.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=309224

 docs/reference/glib/glib-sections.txt |    1 +
 glib/gkeyfile.c                       |   38 +++++++++++++++++++++++++++++++++
 glib/gkeyfile.h                       |    4 +++
 3 files changed, 43 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 9c2a7f8..3b5170c 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 ae20689..cf063e1 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 b37070e..3fdfc29 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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]