Add g_key_file_save_to_file()
authorRyan Lortie <desrt@desrt.ca>
Thu, 3 Oct 2013 16:39:53 +0000 (12:39 -0400)
committerRyan Lortie <desrt@desrt.ca>
Fri, 4 Oct 2013 16:18:20 +0000 (12:18 -0400)
To write a keyfile to disk.

https://bugzilla.gnome.org/show_bug.cgi?id=309224

docs/reference/glib/glib-sections.txt
glib/gkeyfile.c
glib/gkeyfile.h

index 9c2a7f8..3b5170c 100644 (file)
@@ -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
index ae20689..cf063e1 100644 (file)
@@ -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;
+}
index b37070e..3fdfc29 100644 (file)
@@ -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