|
Boost Users : |
Subject: Re: [Boost-users] [locale] IOStream encoding conversions?
From: Artyom Beilis (artyomtnk_at_[hidden])
Date: 2011-10-24 04:51:02
> Currently I'm messing about with Boost.Locale, I'm intending to use it
> for my project, but I don't know how to do something along the lines of
> specifying the external encoding (usually the user's locale, en_US.UTF-8
> in my case), and the internal coding of my program (UTF-16),
> to have input and output converted, possibly using a codecvt. A possible example
> would be translating UTF-16-â¥POSIX UTF-8 paths, or UTF-8-â¥Windows UTF-16
> paths.
I assume you use Windows and wide characters. It is enough to imbue
the locale to the output **wide** stream and write to it.
boost::locale::generator gen;
// stdout
std::wcout.imbue(gen(""));
std::wcout << L"ש×××" << std::endl;
// UTF-8 file I/O
std::wfstream stream;
stream.imbue(gen(""));
stream.open(name,std::wfstream::out);
stream << L"ש×××" << std::endl;
Would actually output the text in narrow encoding.
Also there is an example how to create custom wide streams that would
convert wide to narrow encoding
http://svn.boost.org/svn/boost/trunk/libs/locale/doc/html/charset_handling.html
Also for converting things like path UTF-8/UTF-16 you can use utf_to_utf functions
that work just on chunks of text.
http://svn.boost.org/svn/boost/trunk/libs/locale/doc/html/group__codepage.html#ga2de4d9d38004e5d7146abf2bd6a3df0b
Â
Artyom Beilis
--------------
CppCMS - C++ Web Framework: http://cppcms.sf.net/
CppDB - C++ SQL Connectivity: http://cppcms.sf.net/sql/cppdb/
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net