Re: A stupid question about libtinymail



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

OK, I've discovered two more things and wrote a patch.

It seems that while incorrect, the old code worked just fine (to some
extent), due to the fact that gtk_text_buffer_get_text generously
returned the whole string up to the end, because of the negative offset.
:) Which caused me some a headache, while trying to figure out why both
with and without my correction, it gave the same incorrect result. :)

It turned out that there is another problem. As byte and character
offsets do not match, it skipped characters when the text contained
non-latin UTF-8 characters.

I include a patch here. I did work on my tests, but I can't guarantee
its absolute correctness. :) It's a bit inefficient, but it works, and
so far I prefer this, to a memory stream with a copy of the whole text
inside it.

Patch SHA1 sum:
48e176b0f660dadf0a438a8283dc613269649041
gtk-text-buffer-stream-read-fix.patch

P.S. It is against 0.0.5, but it should apply to the latest version just
fine, as it seems that there are no changes in the repository.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHXsZhZ2zN07dXrWoRAt/YAJ9fJK3flh4wIv2u6LvKwtJGm48zKwCfbGJI
Jloo9C88pzkpbFapTCFpd1E=
=p0sQ
-----END PGP SIGNATURE-----
--- libtinymail-0.0.5-old/libtinymailui-gtk/tny-gtk-text-buffer-stream.c	2007-12-10 15:48:54.000000000 +0200
+++ libtinymail-0.0.5/libtinymailui-gtk/tny-gtk-text-buffer-stream.c	2007-12-11 18:33:37.000000000 +0200
@@ -94,28 +94,23 @@
 tny_gtk_text_buffer_stream_read_default (TnyStream *self, char *buffer, gsize n)
 {
 	TnyGtkTextBufferStreamPriv *priv = TNY_GTK_TEXT_BUFFER_STREAM_GET_PRIVATE (self);
-	GtkTextIter dest, end;
+	GtkTextIter chunk_end;
 	gchar *buf;
-	gint cur_offset, end_offset, rlength;
-	gtk_text_buffer_get_end_iter (priv->buffer, &end);
+	const gchar *valid_end;
+	gint cur_offset;
 
 	cur_offset = gtk_text_iter_get_offset (&(priv->cur));    
-	end_offset = gtk_text_iter_get_offset (&end);
-
-	if (cur_offset + (gint)n > end_offset)
-		rlength = end_offset - cur_offset;
-	else rlength = (gint)n;
 
-	gtk_text_buffer_get_start_iter (priv->buffer, &dest);
-	gtk_text_iter_set_offset (&dest, rlength);
+	gtk_text_buffer_get_iter_at_offset(priv->buffer, &chunk_end, (gint)n + cur_offset);
 
-
-	buf = gtk_text_buffer_get_text (priv->buffer, &(priv->cur), &dest, TRUE);
-	strncpy (buffer, buf, rlength);
+	buf = gtk_text_buffer_get_text (priv->buffer, &(priv->cur), &chunk_end, TRUE);
+	strncpy (buffer, buf, n);
 	g_free (buf);
-	gtk_text_iter_set_offset (&(priv->cur), cur_offset + rlength);
 
-	return (gssize) rlength;
+	g_utf8_validate(buffer, n, &valid_end);
+	gtk_text_iter_set_offset (&(priv->cur), cur_offset + g_utf8_strlen(buffer, valid_end - buffer));
+
+	return (gssize) (valid_end - buffer);
 }
 
 static gssize


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