Tests: Simplify implementation of execute_process encoding test executable

Replace use of KWSys ConsoleBuf with direct encoding conversion calls.
This commit is contained in:
Brad King
2025-04-17 19:28:05 -04:00
parent 407363abd0
commit 3f8ad3e894

View File

@@ -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 <windows.h>
#endif
int main(int argc, char* argv[])
{
#ifdef _WIN32
cmsys::ConsoleBuf::Manager consoleOut(std::cout);
#endif
if (argc <= 2) {
std::cout << "Usage: testEncoding <encoding> <file>" << 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<char>(file)),
std::istreambuf_iterator<char>());
#ifdef _WIN32
if (codePage) {
if (int wlen = MultiByteToWideChar(CP_UTF8, 0, text.data(),
int(text.size()), nullptr, 0)) {
std::vector<wchar_t> 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<char> 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;
}