Re: utf text in a GtkTextView



On Sat, Jun 07, 2003 at 04:46:47PM -0400, Rajarshi Guha wrote: 
Now as far as I know the text was UTF8 encoded - is there anyway to
determine the encoding of a text file?

No. The way it needs to work is that either the file has a fixed
encoding determined by its MIME type, or the file has its encoding
labeled in some way. For example, XML and HTML files have a tag
indicating their encoding. ".txt" files are usually either in the
locale encoding or in UTF-8, but really all you can do is have a
"choose the encoding" option in the user interface allowing the user
to specify manually.

If you got that g_utf8_validate() warning, then your file was not
valid UTF-8. But even if the file isn't, you shouldn't get that
warning - you need to be validating untrusted data, such as file
contents, before putting it in a GtkTextView. If the file is not
validly encoded, you should open an error dialog and complain to the
user, or perhaps use g_convert_with_fallback() to try to load as much
of it as possible.

Do I need to use any special GTK function to insert different encodings?
Or is this something that is supposed to be managed outside of GTK

You need to get things in UTF-8 before passing them to GTK. GLib
offers a lot of functions to help with that, such as 
g_utf8_validate(), g_convert(), g_locale_to_utf8(), and so forth.

If you pass GTK non-UTF-8, then GTK will often crash but will at 
least spew warnings and not work. It's a bug to pass GTK any text that
isn't known to be valid UTF-8.

Havoc



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