Boost logo

Boost :

Subject: Re: [boost] [gsoc] Request Feedback for Boost.Ustr Unicode String Adapter
From: Daniel James (dnljms_at_[hidden])
Date: 2011-08-12 08:19:50


On 9 August 2011 11:45, Soares Chen Ruo Fei <crf_at_[hidden]> wrote:
>
> Currently Boost.Ustr provides limited support towards I/O. Due to
> portability issues I think it is very hard for Boost.Ustr alone to
> solve problems such as printing Unicode strings to the screen. In
> fact, I have no idea on how to print even raw Unicode strings onto the
> Windows terminal. (Any Windows expert knows how to solve this?)

I'm not a Windows expert, but I needed to do this for quickbook, I
wasn't able to find a complete solution, but what I've got sort of
works. Maybe someone else knows better. I've a horrible feeling that
someone is going to point out a much simpler solution that makes what
I do look silly and pointless.

Quickbook always uses a wide stream for output on windows. When
running from an IDE this worked fine, but at the command line it would
be converted to the current code page - losing characters that the
code page doesn't support. So when running from the console you need
to tell windows to use UTF-16:

    #include <io.h>
    #include <fcntl.h>

    int main()
    {
        if (_isatty(_fileno(stdout))) _setmode(_fileno(stdout), _O_U16TEXT);
        if (_isatty(_fileno(stderr))) _setmode(_fileno(stderr), _O_U16TEXT);
    }

Annoyingly _O_U16TEXT was largely undocumented until recently, I don't
know if it was available before Visual Studio 2008. The last time I
checked, it wasn't available in mingw. Here's the MSDN page for
_setmode:

http://msdn.microsoft.com/en-us/library/tw4k6df8%28v=VS.100%29.aspx

The '_isatty(_fileno(stdout))' checks that you are writing to the
console. You don't want to write UTF-16 when output is piped into a
program that expects 8 bit characters.

A better solution might be to use the UTF-8 code page for output, but
that didn't seem to work, at least not on XP.

Finally, remember to make sure your console is using a font that can
display the characters you're outputting.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk