Re: Displaying a UTF-8 file in a text view
- From: David Nečas (Yeti) <yeti physics muni cz>
- To: gtk-list gnome org
- Subject: Re: Displaying a UTF-8 file in a text view
- Date: Sun, 25 Feb 2007 17:09:14 +0100
On Sun, Feb 25, 2007 at 03:31:54PM +0000, Kumar Appaiah wrote:
> I have commented that line out now, but still no use.
>
> ...
>
> In that case, I then set n to strlen(actualText), which should give me the right
> value, as strlen checks for '\0'. But now, I get this:
>
> (llf_utf:21130): Gtk-CRITICAL **: gtk_text_buffer_emit_insert: assertion
> `g_utf8_validate (text, len, NULL)' failed
So if we leave out all the redundant things (and add at
least basic sanity checks), this will remain:
GtkTextBuffer *textbuffer;
gchar *buf;
const gchar *errpos;
GError *err = NULL;
gsize n;
if (g_file_get_contents(filename, &buf, &n, &err)) {
if (g_utf8_validate(buf, n, &errpos)) {
textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textarea));
gtk_text_buffer_set_text(textbuffer, buf, n);
}
else
g_printerr("Invalid byte at position %d\n", (gint)(errpos - buf));
g_free(buf);
}
else {
g_printerr("Cannot read %s: %s\n", filename, err->message);
g_clear_error(&err);
}
This works, and if it doesn't it will tell you what's wrong.
Yeti
--
Whatever.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]