g_file_write()



Is it useful to add g_file_write() to complement
g_file_get_contents()? It seems a feature many applications could use.

Patch attached (not tested on Windows). It is also available at

   http://www.daimi.au.dk/~sandmann/filewrite.patch

Søren


Index: gfileutils.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gfileutils.c,v
retrieving revision 1.59
diff -u -r1.59 gfileutils.c
--- gfileutils.c	24 Feb 2005 23:46:36 -0000	1.59
+++ gfileutils.c	6 Mar 2005 20:22:13 -0000
@@ -811,6 +811,109 @@
 
 #endif
 
+/**
+ * g_file_write:
+ * @filename: name of a file to write @contents to, in the GLib file name encoding
+ * @contents: string to write to the file
+ * @length: length of @contents, or -1 if @contents is nul-terminated
+ * @error: return location for a #GError, or %NULL
+ *
+ * Writes all of @contents to a file named @filename, with good error checking. If
+ * a file called @filename already exists it will be overwritten.
+ *
+ * If the call was sucessful, it returns %TRUE. If the call was not successful,
+ * it returns  %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible
+ * error codes are those in the #GFileError enumeration.
+ *
+ * Return value: %TRUE on success, %FALSE if an error occurred
+ **/
+gboolean
+g_file_write (const gchar *filename,
+	      const gchar *contents,
+	      gsize	   length,
+	      GError	 **error)
+{
+  FILE *file;
+  gchar *normalized_filename;
+  gchar *display_filename;
+  
+  g_return_val_if_fail (filename != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  
+#ifdef G_OS_WIN32
+  g_return_val_if_fail (g_utf8_validate (filename, -1, NULL), FALSE);
+  
+  normalized_filename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
+#else
+  normalized_filename = g_strdup (filename);
+#endif
+  
+  display_filename = g_filename_display_name (filename);
+  
+  if (length == -1)
+    length = strlen (contents);
+  
+  errno = 0;
+  
+  file = fopen (normalized_filename, "wb");
+  
+  if (!file)
+    {
+      g_set_error (error,
+		   G_FILE_ERROR,
+		   g_file_error_from_errno (errno),
+		   _("Failed to open file '%s': fopen() failed: %s"),
+		   display_filename, 
+		   g_strerror (errno));
+
+      goto bail;
+    }
+  
+  if (length > 0)
+    {
+      size_t n_written;
+      
+      errno = 0;
+      
+      n_written = fwrite (contents, 1, length, file);
+      
+      if (n_written < length)
+	{
+	  g_set_error (error,
+		       G_FILE_ERROR,
+		       g_file_error_from_errno (errno),
+		       _("Failed to write file '%s': fwrite() failed: %s"),
+		       display_filename,
+		       g_strerror (errno));
+
+	  goto bail;
+	}
+    }
+  
+  errno = 0;
+  
+  if (fclose (file) == EOF)
+    {
+      g_set_error (error,
+		   G_FILE_ERROR,
+		   g_file_error_from_errno (errno),
+		   _("Failed to close file '%s': fclose() failed: %s"),
+		   display_filename, 
+		   g_strerror (errno));
+
+      goto bail;
+    }
+
+  return TRUE;
+  
+ bail:
+  g_free (normalized_filename);
+  g_free (display_filename);
+  
+  return FALSE;
+}
+
+
 /*
  * mkstemp() implementation is from the GNU C library.
  * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.


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