[gedit] Properly handle the case of empty text buffer



commit d883f94161d4557b8b5de667be9b9b8e0abbfdd5
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Jan 24 14:05:59 2010 +0100

    Properly handle the case of empty text buffer

 gedit/gedit-document-input-stream.c |   29 +++++++++++++++++------------
 tests/document-input-stream.c       |   24 ++++++++++++++++++------
 2 files changed, 35 insertions(+), 18 deletions(-)
---
diff --git a/gedit/gedit-document-input-stream.c b/gedit/gedit-document-input-stream.c
index 0bd5900..31edf19 100644
--- a/gedit/gedit-document-input-stream.c
+++ b/gedit/gedit-document-input-stream.c
@@ -360,7 +360,7 @@ gedit_document_input_stream_read (GInputStream  *stream,
 				  GError       **error)
 {
 	GeditDocumentInputStream *dstream;
-	gssize space_left, read, n, newline_size;
+	gssize space_left, read, n;
 
 	dstream = GEDIT_DOCUMENT_INPUT_STREAM (stream);
 
@@ -391,22 +391,27 @@ gedit_document_input_stream_read (GInputStream  *stream,
 		space_left -= n;
 	} while (space_left > 0 && n != 0 && dstream->priv->bytes_partial == 0);
 
-	/* make sure files are always terminated with \n (see bug #95676). Note
-	   that we strip the trailing \n when loading the file */
-	newline_size = get_new_line_size (dstream);
-
+	/* Make sure that non-empty files are always terminated with \n (see bug #95676).
+	 * Note that we strip the trailing \n when loading the file */
 	if (gtk_text_iter_is_end (&dstream->priv->pos) &&
-	    space_left >= newline_size &&
-	    !dstream->priv->newline_added)
+	    !gtk_text_iter_is_start (&dstream->priv->pos))
 	{
-		const gchar *newline;
+		gssize newline_size;
+
+		newline_size = get_new_line_size (dstream);
 
-		newline = get_new_line (dstream);
+		if (space_left >= newline_size &&
+		    !dstream->priv->newline_added)
+		{
+			const gchar *newline;
 
-		memcpy (buffer + read, newline, newline_size);
+			newline = get_new_line (dstream);
 
-		read += newline_size;
-		dstream->priv->newline_added = TRUE;
+			memcpy (buffer + read, newline, newline_size);
+
+			read += newline_size;
+			dstream->priv->newline_added = TRUE;
+		}
 	}
 
 	return read;
diff --git a/tests/document-input-stream.c b/tests/document-input-stream.c
index 0aeb2b9..b7bb000 100644
--- a/tests/document-input-stream.c
+++ b/tests/document-input-stream.c
@@ -31,31 +31,34 @@ static void
 test_consecutive_read (const gchar *inbuf,
 		       const gchar *outbuf,
 		       GeditDocumentNewlineType type,
-		       gsize mem)
+		       gsize read_chunk_len)
 {
 	GtkTextBuffer *buf;
 	GInputStream *in;
-	gsize r, n;
+	gsize outlen;
+	gssize n, r;
 	GError *err = NULL;
 	gchar *b;
 
 	buf = gtk_text_buffer_new (NULL);
-
 	gtk_text_buffer_set_text (buf, inbuf, -1);
 
 	b = g_malloc (200);
 	in = gedit_document_input_stream_new (buf, type);
 
+	outlen = strlen (outbuf);
 	n = 0;
 
 	do
 	{
-		r = g_input_stream_read (in, b + n, mem, NULL, &err);
-		n += r;
+		r = g_input_stream_read (in, b + n, read_chunk_len, NULL, &err);
+		g_assert_cmpint (r, >=, 0);
 		g_assert_no_error (err);
+
+		n += r;
 	} while (r != 0);
 
-	g_assert_cmpint (n, >, 0);
+	g_assert_cmpint (n, ==, outlen);
 
 	b[n] = '\0';
 
@@ -63,6 +66,13 @@ test_consecutive_read (const gchar *inbuf,
 }
 
 static void
+test_empty ()
+{
+	/* empty file should not have a trailing newline */
+	test_consecutive_read ("", "", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 10);
+}
+
+static void
 test_consecutive_cut_char ()
 {
 	/* first \n is read then fo and then is added \r but not \n */
@@ -124,6 +134,8 @@ int main (int   argc,
 	g_type_init ();
 	g_test_init (&argc, &argv, NULL);
 
+	g_test_add_func ("/document-input-stream/empty", test_empty);
+
 	g_test_add_func ("/document-input-stream/consecutive_cut_char", test_consecutive_cut_char);
 	g_test_add_func ("/document-input-stream/consecutive_big_read", test_consecutive_big_read);
 	g_test_add_func ("/document-input-stream/consecutive_middle_read", test_consecutive_middle_read);



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