? bug119922.patch
? depcomp
? intltool-extract.in
? intltool-merge.in
? intltool-update.in
? savecopy.patch
? data/gedit-2.10.pc
? data/gedit.desktop.in
? gedit/.gedit-convert.c.swp
? gedit/.gedit-debug.h.swp
Index: gedit/ChangeLog
===================================================================
RCS file: /cvs/gnome/gedit/gedit/ChangeLog,v
retrieving revision 1.733
diff -u -p -r1.733 ChangeLog
--- gedit/ChangeLog	25 Jan 2005 10:26:24 -0000	1.733
+++ gedit/ChangeLog	25 Jan 2005 19:01:09 -0000
@@ -1,5 +1,13 @@
 2005-01-25  Paolo Maggi  <paolo@gnome.org>
 
+	* gedit-convert.[ch] (gedit_convert_from_utf8): returns the size of
+	the converted text (as number of bytes)
+
+	* gedit-document.c (gedit_document_save_as_real): fixes 
+	bug #156423 (Saving in UTF-16 encoding destroys files)
+
+2005-01-25  Paolo Maggi  <paolo@gnome.org>
+
 	Fixed bug #115070 (RFE: usability: <esc> shoud close Search dialog)
 
 	Since nothing seems to change on the HIG side, I have decided 
Index: gedit/gedit-convert.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-convert.c,v
retrieving revision 1.4
diff -u -p -r1.4 gedit-convert.c
--- gedit/gedit-convert.c	2 Nov 2004 11:14:36 -0000	1.4
+++ gedit/gedit-convert.c	25 Jan 2005 19:01:09 -0000
@@ -214,10 +214,12 @@ gchar *
 gedit_convert_from_utf8 (const gchar          *content, 
 		         gsize                 len,
 		         const GeditEncoding  *encoding,
+			 gsize                *new_len,
 			 GError 	     **error)
 {
 	GError *conv_error         = NULL;
 	gchar  *converted_contents = NULL;
+	gsize   bytes_written = 0;
 
 	gedit_debug (DEBUG_UTILS, "");
 	
@@ -236,7 +238,7 @@ gedit_convert_from_utf8 (const gchar    
 					gedit_encoding_get_charset (encoding), 
 					"UTF-8",
 					NULL, 
-					NULL,
+					&bytes_written,
 					&conv_error); 
 
 	if (conv_error != NULL)
@@ -251,6 +253,11 @@ gedit_convert_from_utf8 (const gchar    
 		}
 
 		g_propagate_error (error, conv_error);
+	}
+	else
+	{
+		if (new_len != NULL)
+			*new_len = bytes_written;
 	}
 
 	return converted_contents;
Index: gedit/gedit-convert.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-convert.h,v
retrieving revision 1.2
diff -u -p -r1.2 gedit-convert.h
--- gedit/gedit-convert.h	6 Jun 2003 16:26:07 -0000	1.2
+++ gedit/gedit-convert.h	25 Jan 2005 19:01:09 -0000
@@ -52,6 +52,7 @@ gchar *gedit_convert_to_utf8   (const gc
 gchar *gedit_convert_from_utf8 (const gchar          *content, 
 				gsize                 len,
 				const GeditEncoding  *encoding,
+				gsize                *new_len,
 				GError              **error);
 
 #endif /* __GEDIT_CONVERT_H__ */
Index: gedit/gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.c,v
retrieving revision 1.108
diff -u -p -r1.108 gedit-document.c
--- gedit/gedit-document.c	11 Jan 2005 01:03:18 -0000	1.108
+++ gedit/gedit-document.c	25 Jan 2005 19:01:10 -0000
@@ -1748,9 +1748,10 @@ gedit_document_save_as_real (GeditDocume
 	mode_t saved_umask;
 	struct stat st;
 	gchar *chars = NULL;
-	gint chars_len;
 	gint fd;
 	gint retval;
+	gsize chars_len = 0;
+	gsize new_len = 0;
 	gboolean res;
 	gboolean add_cr;
 	GtkTextIter start_iter;
@@ -1875,14 +1876,24 @@ gedit_document_save_as_real (GeditDocume
 	chars = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc),
 					   &start_iter, &end_iter, TRUE);
 
+	chars_len = strlen (chars);
+
+	add_cr = FALSE;
+	
+	if (chars_len >= 1)
+	{
+		add_cr = (*(chars + chars_len - 1) != '\n');
+	}
+	
 	if (encoding != gedit_encoding_get_utf8 ())
 	{
 		GError *conv_error = NULL;
 		gchar* converted_file_contents = NULL;
 
 		converted_file_contents = gedit_convert_from_utf8 (chars, 
-								   -1, 
+								   chars_len, 
 								   encoding,
+								   &new_len,
 								   &conv_error);
 
 		if (conv_error != NULL)
@@ -1900,17 +1911,42 @@ gedit_document_save_as_real (GeditDocume
 			chars = converted_file_contents;
 		}
 	}
-
-	chars_len = strlen (chars);
-
-	add_cr = (*(chars + chars_len - 1) != '\n');
+	else
+	{
+		new_len = chars_len;
+	}
 
 	/* Save the file content */
-	res = (write (fd, chars, chars_len) == chars_len);
+	res = (write (fd, chars, new_len) == new_len);
 
 	if (res && add_cr)
+	{
 		/* Add \n if needed */
-		res = (write (fd, "\n", 1) == 1);
+		if (encoding != gedit_encoding_get_utf8 ())
+		{
+			gchar* converted_n = NULL;
+		
+			converted_n = gedit_convert_from_utf8 ("\n", 
+							       -1, 
+							       encoding,
+							       &new_len,
+							       NULL);
+			
+			if (converted_n == NULL)
+			{
+				g_warning ("Cannot add '\\n' at the end of the file.");
+			}
+			else
+			{
+				res = (write (fd, converted_n, new_len) == new_len);
+				g_free (converted_n);
+			}
+		}
+		else
+		{	
+			res = (write (fd, "\n", 1) == 1);
+		}
+	}
 
 	if (!res)
 	{
Index: plugins/ChangeLog
===================================================================
RCS file: /cvs/gnome/gedit/plugins/ChangeLog,v
retrieving revision 1.219
diff -u -p -r1.219 ChangeLog
--- plugins/ChangeLog	19 Jan 2005 10:56:36 -0000	1.219
+++ plugins/ChangeLog	25 Jan 2005 19:01:10 -0000
@@ -1,3 +1,9 @@
+2005-01-25  Paolo Maggi  <paolo@gnome.org>
+
+	* savecopy/savecopy.c (get_buffer_contents) (write_to_file)
+	(real_save_copy): fixes a mem leak, fixed bug #156423 (Saving 
+	in UTF-16 encoding destroys files). Patch by Paolo Borelli.
+
 2005-01-19  Paolo Maggi  <paolo@gnome.org>
 
 	* taglist/HTML.tags.xml.in: changes some string as suggested in 
Index: plugins/savecopy/savecopy.c
===================================================================
RCS file: /cvs/gnome/gedit/plugins/savecopy/savecopy.c,v
retrieving revision 1.9
diff -u -p -r1.9 savecopy.c
--- plugins/savecopy/savecopy.c	19 Jan 2005 10:55:04 -0000	1.9
+++ plugins/savecopy/savecopy.c	25 Jan 2005 19:01:10 -0000
@@ -60,40 +60,13 @@ G_MODULE_EXPORT GeditPluginState deactiv
 
 
 static gchar *
-get_contents (GeditDocument *doc, const GeditEncoding *encoding, GError **error)
+get_buffer_contents (GeditDocument *doc)
 {
-	gchar *chars;
 	GtkTextIter start, end;
 
 	gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc), &start, &end);
-	chars = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc),
-					   &start, &end, TRUE);
-
-	if (encoding != gedit_encoding_get_utf8 ())
-	{
-		GError *conv_error = NULL;
-		gchar* converted_file_contents = NULL;
-
-		converted_file_contents = gedit_convert_from_utf8 (chars, 
-								   -1, 
-								   encoding,
-								   &conv_error);
-
-		if (conv_error != NULL)
-		{
-			/* Conversion error */
-			g_propagate_error (error, conv_error);
-			g_free (chars);
-			return NULL;
-		}
-		else
-		{
-			g_free (chars);
-			chars = converted_file_contents;
-		}
-	}
-
-	return chars;
+	return gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (doc),
+					  &start, &end, TRUE);
 }
 
 static GnomeVFSResult
@@ -101,7 +74,6 @@ write_to_file (GnomeVFSHandle *handle, g
 {
 	GnomeVFSFileSize written;
 	GnomeVFSResult res = GNOME_VFS_OK;
-	gboolean add_cr;
 
 	while (len > 0)
 	{
@@ -114,12 +86,6 @@ write_to_file (GnomeVFSHandle *handle, g
 			break;
 	}
 
-	/* Add \n if needed */
-	add_cr = (*(data + len - 1) != '\n');
-
-	if (res == GNOME_VFS_OK && add_cr)
-		res = gnome_vfs_write (handle, "\n", 1, &written);
-
 	return res;
 }
 
@@ -131,15 +97,56 @@ real_save_copy (GeditDocument *doc,
 		GError **error)
 {
 	gchar *contents;
+	gboolean add_cr;
+	gsize len = 0;
+	gsize new_len = 0;
 	GnomeVFSHandle *handle;
 	mode_t saved_umask;
 	guint perms = 0;
 	GnomeVFSResult vfs_res;
 
-	contents = get_contents (doc, encoding, error);
+	contents = get_buffer_contents (doc);
 	if (contents == NULL)
 		return FALSE;
 
+	len = strlen (contents);
+
+	add_cr = FALSE;
+	
+	if (len >= 1)
+	{
+		add_cr = (*(contents + len - 1) != '\n');
+	}
+
+	if (encoding != gedit_encoding_get_utf8 ())
+	{
+		GError *conv_error = NULL;
+		gchar *converted_file_contents = NULL;
+
+		converted_file_contents = gedit_convert_from_utf8 (contents, 
+								   len, 
+								   encoding,
+								   &new_len,
+								   &conv_error);
+
+		if (conv_error != NULL)
+		{
+			/* Conversion error */
+			g_propagate_error (error, conv_error);
+			g_free (contents);
+			return FALSE;
+		}
+		else
+		{
+			g_free (contents);
+			contents = converted_file_contents;
+		}
+	}
+	else
+	{
+		new_len = len;
+	}
+
 	/* init default permissions */
 	saved_umask = umask(0);
 	perms = (GNOME_VFS_PERM_USER_READ   |
@@ -158,6 +165,8 @@ real_save_copy (GeditDocument *doc,
 				    perms);
 	if (vfs_res != GNOME_VFS_OK)
 	{
+		g_free (contents);
+
 		g_set_error (error,
 			     GEDIT_DOCUMENT_IO_ERROR, 
 			     vfs_res,
@@ -166,7 +175,8 @@ real_save_copy (GeditDocument *doc,
 		return FALSE;
 	}
 
-	vfs_res = write_to_file (handle, contents, strlen (contents));
+	vfs_res = write_to_file (handle, contents, new_len);
+	g_free (contents);
 	if (vfs_res != GNOME_VFS_OK)
 	{
 		g_set_error (error,
@@ -177,6 +187,46 @@ real_save_copy (GeditDocument *doc,
 		gnome_vfs_close (handle);
 
 		return FALSE;
+	}
+
+	/* make sure the file is \n terminated.
+	 * However if writing it fails we do not abort
+	 */
+	if (add_cr)
+	{
+		GnomeVFSFileSize written;
+
+		if (encoding != gedit_encoding_get_utf8 ())
+		{
+			gchar *converted_n = NULL;
+		
+			converted_n = gedit_convert_from_utf8 ("\n", 
+							       -1, 
+							       encoding,
+							       &new_len,
+							       NULL);
+
+			if (converted_n == NULL)
+			{
+				g_warning ("Cannot add '\\n' at the end of the file.");
+			}
+			else
+			{
+				vfs_res = gnome_vfs_write (handle, converted_n, new_len, &written);
+
+				if ((vfs_res != GNOME_VFS_OK) || (written != new_len))
+					g_warning ("Cannot add '\\n' at the end of the file.");
+
+				g_free (converted_n);
+			}
+		}
+		else
+		{
+			vfs_res = gnome_vfs_write (handle, "\n", 1, &written);
+
+			if ((vfs_res != GNOME_VFS_OK) || (written != 1))
+				g_warning ("Cannot add '\\n' at the end of the file.");
+		}
 	}
 
 	gnome_vfs_close (handle);
