diff options
author | Chun Kuan Lee <ken2812221@gmail.com> | 2018-08-05 16:38:25 +0000 |
---|---|---|
committer | Chun Kuan Lee <ken2812221@gmail.com> | 2018-09-30 15:08:16 +0800 |
commit | 380c843217139b180457889699c65b37ae3b4a87 (patch) | |
tree | 14f563548a07e34860364c8046e83ef8671730a9 /src/util.cpp | |
parent | 9b8bb5f1402a07736be7d649c2253253672d04e1 (diff) |
utils: Convert Windows args to utf-8 string
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp index fa624aee90..1002302904 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -61,6 +61,7 @@ #include <codecvt> #include <io.h> /* for _commit */ +#include <shellapi.h> #include <shlobj.h> #endif @@ -1200,6 +1201,10 @@ void SetupEnvironment() } catch (const std::runtime_error&) { setenv("LC_ALL", "C", 1); } +#elif defined(WIN32) + // Set the default input/output charset is utf-8 + SetConsoleCP(CP_UTF8); + SetConsoleOutputCP(CP_UTF8); #endif // The path locale is lazy initialized and to avoid deinitialization errors // in multithreading environments, it is set explicitly by the main thread. @@ -1265,3 +1270,30 @@ int ScheduleBatchPriority() return 1; #endif } + +namespace util { +#ifdef WIN32 +WinCmdLineArgs::WinCmdLineArgs() +{ + wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc); + std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf8_cvt; + argv = new char*[argc]; + args.resize(argc); + for (int i = 0; i < argc; i++) { + args[i] = utf8_cvt.to_bytes(wargv[i]); + argv[i] = &*args[i].begin(); + } + LocalFree(wargv); +} + +WinCmdLineArgs::~WinCmdLineArgs() +{ + delete[] argv; +} + +std::pair<int, char**> WinCmdLineArgs::get() +{ + return std::make_pair(argc, argv); +} +#endif +} // namespace util |