Introduce WindowsScopedCodePage RAII class

This commit is contained in:
Hennadii Stepanov 2025-04-29 15:58:18 +01:00
parent 5744db77b2
commit ce7f41c01b
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
6 changed files with 28 additions and 0 deletions

View file

@ -1315,6 +1315,7 @@ MAIN_FUNCTION
#ifdef WIN32
common::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
common::WindowsScopedCodePage windows_terminal_output_code_page{CP_UTF8};
#endif
SetupEnvironment();
if (!SetupNetworking()) {

View file

@ -100,6 +100,7 @@ MAIN_FUNCTION
#ifdef WIN32
common::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
common::WindowsScopedCodePage windows_terminal_output_code_page{CP_UTF8};
#endif
int exit_status;

View file

@ -257,6 +257,7 @@ MAIN_FUNCTION
#ifdef WIN32
common::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
common::WindowsScopedCodePage windows_terminal_output_code_page{CP_UTF8};
#endif
NodeContext node;

View file

@ -889,5 +889,16 @@ std::pair<int, char**> WinCmdLineArgs::get()
{
return std::make_pair(argc, argv);
}
WindowsScopedCodePage::WindowsScopedCodePage(UINT new_console_outp_code_page)
: m_original_code_page{GetConsoleOutputCP()}
{
SetConsoleOutputCP(new_console_outp_code_page);
}
WindowsScopedCodePage::~WindowsScopedCodePage()
{
SetConsoleOutputCP(m_original_code_page);
}
#endif
} // namespace common

View file

@ -492,6 +492,19 @@ private:
char** argv;
std::vector<std::string> args;
};
/**
* A RAII-style class for temporarily changing the terminal output code page during runtime.
*/
class WindowsScopedCodePage
{
public:
WindowsScopedCodePage(UINT new_console_outp_code_page);
~WindowsScopedCodePage();
private:
UINT m_original_code_page;
};
#endif
} // namespace common

View file

@ -481,6 +481,7 @@ int GuiMain(int argc, char* argv[])
#ifdef WIN32
common::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
common::WindowsScopedCodePage windows_terminal_output_code_page{CP_UTF8};
#endif
std::unique_ptr<interfaces::Init> init = interfaces::MakeGuiInit(argc, argv);