unicode - What most correct way to set the encoding in C++? -


how best of set encoding in c++?

i got used working unicode (and wchar_t, wstring, wcin, wcout , l" ... "). save source in utf-8.

at moment use mingw (windows 7) , run program in windows console (cmd.exe), can use gcc on gnu\linux , run promgram in linux console utf-8 encoding.

at times want compile source on windows , on linux , want unicode symbols correctly inputed , outputed.

when faced next problem encodings, googled. found different councils: setlocale(lc_all, "") , setlocale(lc_all, "xx_xx.utf-8"), std::setlocale(lc_all, "") , std::setlocale(lc_all, "xx_xx.utf-8") <clocale>,

setconsolecp() , setconsoleoutputcp() <windows.h> , many, many others.

at last bothered shamanism , want ask you: how correct establish encoding?

i need unicode symbol/string correctly inputed , outputed.

this possible, although making windows command prompt console unicode-aware takes special magic. doubt of implementations of standard library functions going this, unfortunately.

you'll find number of questions on stack overflow, this 1 one. basically, console uses called (somewhat erroneously) "oem" code page default. want change utf-8 code page, value of defined cp_utf8. this, you'll need call both setconsolecp function (to set input code page) , setconsoleoutputcp function (to set output code page). code this:

if (!setconsolecp(cp_utf8)) {     // error occurred; handle it. call getlasterror() more information.     // ... } if (!setconsoleoutputcp(cp_utf8)) {     // error occurred; handle it. call getlasterror() more information.     // ... } 

for robustness, might want make sure utf-8 code page supported first, before trying set , use it. calling isvalidcodepage function. example:

if (isvalidcodepage(cp_utf8)) {     // we're good, set console code page... } 

you have change font default ("raster fonts") contains requisite unicode character glyphs—e.g., lucida console or consolas (reference). that's trivial using setcurrentconsolefontex function.

unfortunately, function not exist in versions of windows prior vista. if absolutely need support these older operating systems, thing know call undocumented setconsolefont function. normally, advise strongly against using undocumented functions, think it's less of problem here since only using in old versions of operating system. know aren't going change. on newer versions available, call supported function. sample untested code:

bool iswinvistaorlater() {     osversioninfoex osvi;     osvi.dwosversioninfosize = sizeof(osvi);     getversionex(reinterpret_cast<lposversioninfo>(&osvi));      if (osvi.dwplatformid == ver_platform_win32_nt)     {         return osvi.dwmajorversion >= 6;     }     return false; }  void setconsoletounicodefont() {     handle hconsole = getstdhandle(std_output_handle);     if (iswinvistaorlater())     {         // call documented function.         typedef bool (winapi * pfsetcurrentconsolefontex)(handle, bool, pconsole_font_infoex);         hmodule hmod = getmodulehandle(text("kernel32.dll"));         pfsetcurrentconsolefontex pfsccfx = (pfsetcurrentconsolefontex)getprocaddress(hmod, "setcurrentconsolefontex");          console_font_infoex cfix;         cfix.cbsize       = sizeof(cfix);         cfix.nfont        = 12;         cfix.dwfontsize.x = 8;         cfix.dwfontsize.y = 14;         cfix.fontfamily   = ff_dontcare;         cfix.fontweight   = 400;  // normal weight         lstrcpy(cfix.facename, text("lucida console"));          pfsccfx(hconsole,                 false, /* set font current window size */                 &cfix);     }     else     {         // there no supported function on these older versions,         // have call undocumented one.         typedef bool (winapi * pfsetconsolefont)(handle, dword);         hmodule hmod = getmodulehandle(text("kernel32.dll"));         pfsetconsolefont pfscf = (pfsetconsolefont)getprocaddress(hmod, "setconsolefont");         pfscf(hconsole, 12);     } } 

notice i've left adding required error checking exercise reader. focus here on technique , readability; cluttering error handling confuse matters.

i have no idea how of on linux. suspect it's lot less work, since people tell me os uses utf-8 internally. either way, you're on own that; making windows purr enough work 1 answer!


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -