anjuta r4411 - in trunk: . libanjuta



Author: naba
Date: Sun Nov 30 11:51:32 2008
New Revision: 4411
URL: http://svn.gnome.org/viewvc/anjuta?rev=4411&view=rev

Log:
	* libanjuta/anjuta-utils.[ch]: Improved copy file funtion to
	use gio.



Modified:
   trunk/ChangeLog
   trunk/libanjuta/anjuta-utils.c
   trunk/libanjuta/anjuta-utils.h

Modified: trunk/libanjuta/anjuta-utils.c
==============================================================================
--- trunk/libanjuta/anjuta-utils.c	(original)
+++ trunk/libanjuta/anjuta-utils.c	Sun Nov 30 11:51:32 2008
@@ -59,69 +59,97 @@
 
 #define FILE_BUFFER_SIZE 1024
 
-gboolean
-anjuta_util_copy_file (gchar * src, gchar * dest, gboolean show_error)
+static void
+anjuta_util_from_file_to_file (GInputStream *istream,
+							   GOutputStream *ostream)
 {
-	FILE *input_fp, *output_fp;
+	gsize bytes = 1;
+	GError *error = NULL;
 	gchar buffer[FILE_BUFFER_SIZE];
-	gint bytes_read, bytes_written;
-	gboolean error;
-	
-	error = TRUE;
 	
-	input_fp = fopen (src, "rb");
-	if (input_fp == NULL)
+	while (bytes != 0 && bytes != -1)
 	{
-		if( show_error)
-			anjuta_util_dialog_error_system (NULL, errno,
-											 _("Unable to read file: %s."),
-											 src);
-		return FALSE;
+		bytes = g_input_stream_read (istream, buffer,
+					     sizeof (buffer),
+					     NULL, &error);
+		if (error)
+			break;
+
+		g_output_stream_write (ostream, buffer,
+				       bytes,
+				       NULL, &error);
+		if (error)
+			break;
 	}
 	
-	output_fp = fopen (dest, "wb");
-	if (output_fp == NULL)
+	if (error)
 	{
-		if( show_error)
-			anjuta_util_dialog_error_system (NULL, errno,
-											 _("Unable to create file: %s."),
-											 dest);
-		fclose (input_fp);
-		return TRUE;
+		g_warning (error->message);
+		g_error_free (error);
+		error = NULL;
 	}
 	
-	for (;;)
+	if (!g_output_stream_close (ostream, NULL, &error))
 	{
-		bytes_read = fread (buffer, 1, FILE_BUFFER_SIZE, input_fp);
-		if (bytes_read != FILE_BUFFER_SIZE && ferror (input_fp))
-		{
-			error = FALSE;
-			break;
-		}
+		g_warning (error->message);
+		g_error_free (error);
+		error = NULL;
+	}
+	if (!g_input_stream_close (istream, NULL, &error))
+	{
+		g_warning (error->message);
+		g_error_free (error);
+	}
+}
+
+/**
+ * anjuta_util_copy_file:
+ * @src: the file where copy
+ * @dest: the path to copy the @src
+ * @show_error: TRUE to show a dialog error
+ *
+ * Copies @src to @dest and shows a dialog error in case is needed.
+ *
+ * Returns: TRUE if there was an error copying the file.
+ */
+gboolean
+anjuta_util_copy_file (const gchar * src, const gchar * dest, gboolean show_error)
+{
+	GFile *src_file, *dest_file;
+	GFileInputStream *istream;
+	GFileOutputStream *ostream;
+	GError *error = NULL;
+	gboolean toret = FALSE;
+	
+	src_file = g_file_new_for_path (src);
+	dest_file = g_file_new_for_path (dest);
+	
+	istream = g_file_read (src_file, NULL, &error);
+	if (error)
+		goto free;
 		
-		if (bytes_read)
-		{
-			bytes_written = fwrite (buffer, 1, bytes_read, output_fp);
-			if (bytes_read != bytes_written)
-			{
-				error = FALSE;
-				break;
-			}
-		}
+	ostream = g_file_create (dest_file, G_FILE_CREATE_NONE,
+							 NULL, &error);
+	if (error)
+		goto free;
+	
+	anjuta_util_from_file_to_file (G_INPUT_STREAM (istream), G_OUTPUT_STREAM (ostream));
+	
+free: if (error)
+	{
+		if (show_error)
+			anjuta_util_dialog_error_system (NULL, error->code,
+											 error->message);
+	
+		g_warning ("%s", error->message);
 		
-		if (bytes_read != FILE_BUFFER_SIZE && feof (input_fp))
-		{
-			break;
-		}
+		toret = TRUE;
 	}
 	
-	fclose (input_fp);
-	fclose (output_fp);
+	g_object_unref (src_file);
+	g_object_unref (dest_file);
 	
-	if( show_error && (error == FALSE))
-		anjuta_util_dialog_error_system (NULL, errno,
-										 _("Unable to complete file copy"));
-	return error;
+	return toret;
 }
 
 void

Modified: trunk/libanjuta/anjuta-utils.h
==============================================================================
--- trunk/libanjuta/anjuta-utils.h	(original)
+++ trunk/libanjuta/anjuta-utils.h	Sun Nov 30 11:51:32 2008
@@ -29,7 +29,7 @@
 
 G_BEGIN_DECLS
 
-gboolean anjuta_util_copy_file (gchar * src, gchar * dest, gboolean show_error);
+gboolean anjuta_util_copy_file (const gchar * src, const gchar * dest, gboolean show_error);
 
 gboolean anjuta_util_diff(const gchar* uri, const gchar* text);
 



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