diff --git a/Tests/CMakeLib/testEncoding.cxx b/Tests/CMakeLib/testEncoding.cxx index a42895eae7..b36a0c3cef 100644 --- a/Tests/CMakeLib/testEncoding.cxx +++ b/Tests/CMakeLib/testEncoding.cxx @@ -5,41 +5,29 @@ #include "cmsys/FStream.hxx" #ifdef _WIN32 -# include "cmsys/ConsoleBuf.hxx" -#endif - -#ifdef _WIN32 -void setEncoding(cmsys::ConsoleBuf::Manager& buf, UINT codepage) -{ - cmsys::ConsoleBuf* cb = buf.GetConsoleBuf(); - if (cb) { - cb->input_pipe_codepage = codepage; - cb->output_pipe_codepage = codepage; - cb->input_file_codepage = codepage; - cb->output_file_codepage = codepage; - cb->activateCodepageChange(); - } -} +# include #endif int main(int argc, char* argv[]) { -#ifdef _WIN32 - cmsys::ConsoleBuf::Manager consoleOut(std::cout); -#endif if (argc <= 2) { std::cout << "Usage: testEncoding " << std::endl; return 1; } std::string const encoding(argv[1]); #ifdef _WIN32 + unsigned int codePage = 0; if ((encoding == "UTF8") || (encoding == "UTF-8")) { - setEncoding(consoleOut, CP_UTF8); + codePage = CP_UTF8; } else if (encoding == "ANSI") { - setEncoding(consoleOut, CP_ACP); + codePage = CP_ACP; } else if (encoding == "OEM") { - setEncoding(consoleOut, CP_OEMCP); - } // else AUTO + codePage = CP_OEMCP; + } else if (unsigned int consoleOutputCP = GetConsoleOutputCP()) { + codePage = consoleOutputCP; + } else if (unsigned int ansiCP = GetACP()) { + codePage = ansiCP; + } #endif cmsys::ifstream file(argv[2]); if (!file.is_open()) { @@ -48,6 +36,26 @@ int main(int argc, char* argv[]) } std::string text((std::istreambuf_iterator(file)), std::istreambuf_iterator()); +#ifdef _WIN32 + if (codePage) { + if (int wlen = MultiByteToWideChar(CP_UTF8, 0, text.data(), + int(text.size()), nullptr, 0)) { + std::vector w(wlen); + if (MultiByteToWideChar(CP_UTF8, 0, text.data(), int(text.size()), + w.data(), w.size())) { + if (int nlen = + WideCharToMultiByte(codePage, 0, w.data(), int(w.size()), + nullptr, 0, nullptr, nullptr)) { + std::vector n(nlen); + if (WideCharToMultiByte(codePage, 0, w.data(), int(w.size()), + n.data(), int(n.size()), nullptr, nullptr)) { + text = std::string(n.data(), n.size()); + } + } + } + } + } +#endif std::cout << text; return 0; }