Boost logo

Boost :

Subject: Re: [boost] Silly Boost.Locale default narrowstringencodinginWindows
From: Peter Dimov (pdimov_at_[hidden])
Date: 2011-10-28 10:10:43


Alf P. Steinbach wrote:

> This difference in conversion & wrapping effort was the reason that I used
> both the standard library and the OS API in my original example.

Using the OS API makes your program non-portable, so it's not clear what the
example is supposed to demonstrate. You may as well stick to wchar_t;
Windows 95 is ancient history and the whole wrapping effort is completely
unnecessary.

The portable version of your example would be something along the lines of:

#include "message_box.hpp"
#include <stdio.h>

int main()
{
    char buffer[ 80 ];
    sprintf( buffer, "The answer is %d!", 6*7 );

    message_box( buffer, "Title", mb_icon_information );
}

where message_box has implementations for the various OSes the program
supports. On Windows, it will utf8_decode its arguments and call
MessageBoxW.

A localizable version would not embed readable texts:

#include "message_box.hpp"
#include "get_text.hpp"
#include <stdio.h>

int main()
{
    char buffer[ 80 ]; // ignore buffer overflow for now
    sprintf( buffer, get_text( "the_answer_is" ).c_str(), 6*7 );

    message_box( buffer, get_text( "title" ), mb_icon_information );
}

Now get_text may return something in Chinese (UTF-8 encoded) and it will all
work.

It's also possible to use wchar_t for human-readable text throughout the
code base - this provides a layer of type safety. You'll have to replace
sprintf with swprintf then. Paths, however, are better kept as char[].


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