[gedit] Handle empty input gracefully



commit 6386ded64b523a71bbd27d2641e6998b07160c82
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Sun May 16 11:47:59 2010 +0200

    Handle empty input gracefully

 gedit/gedit-smart-charset-converter.c |   15 +++++++++++++++
 tests/smart-converter.c               |   22 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)
---
diff --git a/gedit/gedit-smart-charset-converter.c b/gedit/gedit-smart-charset-converter.c
index d15b6ec..e563d14 100644
--- a/gedit/gedit-smart-charset-converter.c
+++ b/gedit/gedit-smart-charset-converter.c
@@ -139,6 +139,11 @@ try_convert (GCharsetConverter *converter,
 	gboolean ret;
 	gsize out_size;
 
+	if (inbuf == NULL || inbuf_size == 0)
+	{
+		return FALSE;
+	}
+
 	err = NULL;
 	nread = 0;
 	nwritten = 0;
@@ -200,6 +205,12 @@ guess_encoding (GeditSmartCharsetConverter *smart,
 {
 	GCharsetConverter *conv = NULL;
 
+	if (inbuf == NULL || inbuf_size == 0)
+	{
+		smart->priv->is_utf8 = TRUE;
+		return NULL;
+	}
+
 	if (smart->priv->encodings != NULL &&
 	    smart->priv->encodings->next == NULL)
 		smart->priv->use_first = TRUE;
@@ -390,6 +401,10 @@ gedit_smart_charset_converter_get_guessed (GeditSmartCharsetConverter *smart)
 	{
 		return (const GeditEncoding *)smart->priv->current_encoding->data;
 	}
+	else if (smart->priv->is_utf8)
+	{
+		return gedit_encoding_get_utf8 ();
+	}
 
 	return NULL;
 }
diff --git a/tests/smart-converter.c b/tests/smart-converter.c
index 4f069ea..6680b4c 100644
--- a/tests/smart-converter.c
+++ b/tests/smart-converter.c
@@ -287,6 +287,27 @@ test_xxx_xxx ()
 }
 
 static void
+test_empty ()
+{
+	const GeditEncoding *guessed;
+	gchar *out;
+	GSList *encodings = NULL;
+
+	/* testing the case of an empty file and list of encodings with no
+	   utf-8. In this case, the smart converter cannot determine the right
+	   encoding (because there is no input), but should still default to
+	   utf-8 for the detection */
+	encodings = g_slist_prepend (encodings, (gpointer)gedit_encoding_get_from_charset ("UTF-16"));
+	encodings = g_slist_prepend (encodings, (gpointer)gedit_encoding_get_from_charset ("ISO-8859-15"));
+
+	out = do_test ("", NULL, encodings, 0, &guessed);
+
+	g_assert_cmpstr (out, ==, "");
+
+	g_assert (guessed == gedit_encoding_get_utf8 ());
+}
+
+static void
 test_guessed ()
 {
 	GSList *encs = NULL;
@@ -326,6 +347,7 @@ int main (int   argc,
 	g_test_add_func ("/smart-converter/utf8-utf8", test_utf8_utf8);
 	//g_test_add_func ("/smart-converter/xxx-xxx", test_xxx_xxx);
 	g_test_add_func ("/smart-converter/guessed", test_guessed);
+	g_test_add_func ("/smart-converter/empty", test_empty);
 
 	return g_test_run ();
 }



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