Re: Glib::ustring, error while reading an utf-8 encoded file

2009-01-21 Thread Murray Cumming
On Tue, 2009-01-20 at 14:56 +0100, Bastien Dalla Piazza wrote:
> 
> 1: as-is: "First line: plain text."
> 1: through Glib::ustring: "First line: plain text."
> 2: as-is: "deuxième ligne: des accents ici et là..."

You are lucky that that worked. It's unlikely to work with multi-byte
UTF-8 characters such as Asian characters.

> 2: through Glib::ustring: "
> Glib::ConvertError: Invalid byte sequence in conversion input
> 
> my locale is using utf-8 (fr_CH).
> 
> The question is: How to import the file content in an Glib::ustring
> when
> already knowing its encoding (not necessarily utf-8 though)?

The exception is actually coming from your use of << with the standard C
++ stream. The standard C++ streams can't do UTF-8 so there is an
automatic conversion. See "streams input/output" here:
http://www.gtkmm.org/docs/glibmm-2.4/docs/reference/html/classGlib_1_1ustring.html#_details

The answer, I think, is Just don't use std::cout and friends to show
UTF-8.

-- 
Murray Cumming
murr...@murrayc.com
www.murrayc.com
www.openismus.com


___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Glib::ustring, error while reading an utf-8 encoded file

2009-01-20 Thread Bastien Dalla Piazza
Hi everybody,

I don't have experience about charset issues so I tried the following to
experiment:
(it's in C++ using glibmm and giomm but I guess the problem is not
related to that, it's just I don't understand something.)

*
#include 
#include 
#include 
#include 
int main(int argc, char** argv)
{
Glib::init();
Gio::init();
if(argc!=2) return 1;
//open a file to read
Glib::RefPtr file=Gio::File::create_for_path(argv[1]);
Glib::RefPtr
fin=Gio::DataInputStream::create(file->read());
std::string line;
//try to read.
try{
int i=1;
while(fin->read_line(line)){
std::cout