I'm writing a portable library that deals with files and directories. I want to use UTF-8 for my input (directory paths) and output (file paths). The problem is, Windows gives me a choice between UTF-16-that-used-to-be-UCS-2, and codepages. So I have to convert all my UTF-8 strings to UTF-16, pass them to WinAPI, and convert the results back to UTF-8. C++11 seems to provide <locale>
library just for that, except from what I understood, none of the predefined specializations uses UTF-8 as internal (ie. my-side) coding - the closest there is is UTF-16-to-UTF-8, which is the exact opposite of what I want. So here's first question:
1) How to use codecvt thingamajigs to convert my UTF-8 strings to UTF-16 for WinAPI calls, and the UTF-16 results back to UTF-8?
Another problem: I'm also targetting Linux. On Linux, there is a very good support for many different locales - and I don't want to be any different. Hopefully everyone will use UTF-8 on their Linux machines, but there is no strict guarantee of that. So I thought it would be a good idea to extend the above Windows-specific behavior and always do UTF-8-to-system-locale-coding. Except that I don't see there's any way in C++11's <locale>
library to get current system encoding! Default std::locale constructor makes specified-by-myself locale, and if I don't do it, it returns classic "C" locale. And there are no other getters I'm aware of. So here's second question:
2) How to detect current system locale? Something in <locale>
? Maybe some standard C library function, or (less portable but okay in this case) something in POSIX API?