|
Boost : |
Subject: Re: [boost] [nowide] Easy Unicode For Windows: Request For Comments/Preliminary Review
From: Alex Perry (Alex.Perry_at_[hidden])
Date: 2012-05-29 12:19:29
>
> Hi, I just read the documentation, so far this library looks nice.
>
+1
>So main function
>
>
> int main(int argc,char **argv[,char **env])
> {
> ...
>
> }
>
>
>Simply changed to
>
>
> int main(int argc,char **argv[,char **env])
> {
> boost::nowide::args a(argc,argv[,env])
>
> ...
>
> }
Having done things similar to this before (if never with any elegance). I think that the above won't work in all situations.
The problem is that the CRT may have already applied some conversion to argv strings based on your current code page if your application was called with utf16le strings (this is rarely the case if typed at a command prompt but certainly can happen if called via a shortcut or via a user clicking of a file with an associated file type)
The solution is to use wmain for windows rather than main and do any conversion to utf8 there not in main eg finding some (rather hacked I admit) example I've done.
#ifdef WIN32
//Use wmain under windows to get unicode strings which we convert to utf-8 - standard conversion is to map
//to local code page rather than to utf-8
int wmain(int argc, wchar_t* wargv[])
{
//convert wargv to utf-8 strings
char ** argv = new char *[argc];
for ( int i = 0; i< argc; ++i )
{
utf8string temp( wargv[i] ); //cvt to utf8
argv[i] = new char[ temp.getBufferSize() ];
memcpy( argv[i], temp.c_str(), temp.getBufferSize() );
}
#else
int main(int argc, char* argv[])
{
#endif
Where utf8string is some class I've used doing efficient utf8,utf16,UCS32 conversions which behaves like a std:string with a few bells and whistles on.
It might be worth while adding this wmain workaround information into your library and providing a boost::nowide::args constructor which takes wchar_t
Hope this is of some use.
Alex
ps apologies if formatting is odd - nabble crashed on me trying to reply so ended up using Outlook and modifying reply manually from message digest which is never great ....
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk