[glibmm] add Glib::file_set_contents()



commit 64e2d8d1ac595ac2839c9821341aa102395dfb71
Author: Jonathon Jongsma <jonathon quotidian org>
Date:   Thu Jun 18 22:51:37 2009 -0500

    add Glib::file_set_contents()

 ChangeLog              |    6 ++++++
 glib/src/fileutils.ccg |   19 +++++++++++++++++++
 glib/src/fileutils.hg  |   42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d87b724..fafd4fe 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2009-06-18  Jonathon Jongsma  <jonathon quotidian org>
 
+	* glib/src/fileutils.ccg:
+	* glib/src/fileutils.hg: add Glib::file_set_contents() to match
+	  Glib::file_get_contents().  Fixes Bug #586187
+
+2009-06-18  Jonathon Jongsma  <jonathon quotidian org>
+
 	* configure.ac: bump version and glib version requirement
 
 2009-06-18  Jonathon Jongsma  <jonathon quotidian org>
diff --git a/glib/src/fileutils.ccg b/glib/src/fileutils.ccg
index 576cfc3..77bd1f1 100644
--- a/glib/src/fileutils.ccg
+++ b/glib/src/fileutils.ccg
@@ -179,5 +179,24 @@ std::string file_get_contents(const std::string& filename)
   return std::string(contents.get(), length);
 }
 
+void
+file_set_contents(const std::string& filename,
+                  const gchar *contents,
+                  gssize length)
+{
+  GError* error  = 0;
+
+  g_file_set_contents(filename.c_str(), contents, length, &error);
+
+  if(error)
+    Glib::Error::throw_exception(error);
+}
+
+void
+file_set_contents (const std::string& filename, const std::string& contents)
+{
+    file_set_contents(filename, contents.c_str(), contents.size());
+}
+
 } // namespace Glib
 
diff --git a/glib/src/fileutils.hg b/glib/src/fileutils.hg
index a3a95b7..4deae38 100644
--- a/glib/src/fileutils.hg
+++ b/glib/src/fileutils.hg
@@ -396,5 +396,47 @@ int file_open_tmp(std::string& name_used);
  */
 std::string file_get_contents(const std::string& filename);
 
+/** Writes all of @a contents to a file named @a filename, with good error checking.
+ *
+ * @param filename name of a file to write @a contents to, in the GLib file name
+ *   encoding
+ * @param contents string to write to the file
+ * @param length length of @a contents, or -1 if @a contents is a nul-terminated string
+ *
+ * If a file called @a filename already exists it will be overwritten.
+ *
+ * This write is atomic in the sense that it is first written to a temporary
+ * file which is then renamed to the final name. Notes:
+ * <ol>
+ * <li>
+ *    On Unix, if @a filename already exists hard links to @filename will break.
+ *    Also since the file is recreated, existing permissions, access control
+ *    lists, metadata etc. may be lost. If @filename is a symbolic link,
+ *    the link itself will be replaced, not the linked file.
+ * </li>
+ * <li>
+ *   On Windows renaming a file will not remove an existing file with the
+ *   new name, so on Windows there is a race condition between the existing
+ *   file being removed and the temporary file being renamed.
+ * </li>
+ * <li>
+ *   On Windows there is no way to remove a file that is open to some
+ *   process, or mapped into memory. Thus, this function will fail if
+ *   @filename already exists and is open.
+ * </li>
+ * </ol>
+ *
+ * If the call was not successful, an exception is thrown.
+ * Possible error codes are those in the FileError enumeration.
+ *
+ * @newin2p22
+ **/
+void file_set_contents (const std::string& filename, const gchar *contents, gssize length);
+/** A variant of file_set_contents which accepts a standard C++ string
+ *
+ * @newin2p22
+ * */
+void file_set_contents (const std::string& filename, const std::string& contents);
+
 } // namespace Glib
 



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