diff --git a/Platform_Win32Console.cpp b/Platform_Win32Console.cpp index df51542d6efd89215cd86ad3eca1dba323bccc54..52ffdc31143cfdd5436711076d40637e459efd50 100644 --- a/Platform_Win32Console.cpp +++ b/Platform_Win32Console.cpp @@ -4,6 +4,12 @@ // Since we need std::min() and std::max()... #define NOMINMAX +// Unicode WFILE macro +// https://stackoverflow.com/a/3291315/5035474 +#define WIDEN2(x) L ## x +#define WIDEN(x) WIDEN2(x) +#define WFILE WIDEN(__FILE__) + #include <numeric> #include <sstream> #include <vector> @@ -17,7 +23,7 @@ void fatal(std::wstring_view message) } void fatal_from_lasterror( - std::string_view file, unsigned int line, DWORD lasterror + std::wstring_view file, unsigned int line, DWORD lasterror ) { auto fail = [lasterror]() { @@ -46,25 +52,8 @@ void fatal_from_lasterror( fail(); } - // std::wstring_convert is deprecated... So, MultiByteToWideChar it is. - int file_len = static_cast<int>(file.size()); - int file_utf16_len = MultiByteToWideChar( - CP_UTF8, 0, file.data(), file_len, nullptr, 0 - ); - if(file_utf16_len == 0) { - fail(); - } - - // The terminating '\0' is already included here. - std::wstring file_utf16(file_utf16_len, '\0'); - if(!MultiByteToWideChar( - CP_UTF8, 0, file.data(), file_len, file_utf16.data(), file_utf16_len - )) { - fail(); - } - std::wstringstream message; - message << file_utf16 << ":" << std::to_wstring(line) << ": " << formatted; + message << file << ":" << std::to_wstring(line) << ": " << formatted; fatal(message.str()); } @@ -78,7 +67,7 @@ std::unordered_set<char> Platform_Win32Console::get_input(void) DWORD events_available; if(!GetNumberOfConsoleInputEvents(handle_in, &events_available)) { - fatal_from_lasterror(__FILE__, __LINE__, GetLastError()); + fatal_from_lasterror(WFILE, __LINE__, GetLastError()); } // ReadConsoleInput blocks if we have no events. So, don't even call it @@ -93,7 +82,7 @@ std::unordered_set<char> Platform_Win32Console::get_input(void) if(!ReadConsoleInput( handle_in, input_records.data(), events_available, &events_read )) { - fatal_from_lasterror(__FILE__, __LINE__, GetLastError()); + fatal_from_lasterror(WFILE, __LINE__, GetLastError()); } for(auto const &record : input_records) { @@ -201,22 +190,22 @@ Platform_Win32Console Platform_Win32Console::init() // Make sure to create a dedicated window, even if we launch from a shell FreeConsole(); // We don't care if this fails if(!AllocConsole()) { - fatal_from_lasterror(__FILE__, __LINE__, GetLastError()); + fatal_from_lasterror(WFILE, __LINE__, GetLastError()); } auto handle_out = GetStdHandle(STD_OUTPUT_HANDLE); if(handle_out == INVALID_HANDLE_VALUE) { - fatal_from_lasterror(__FILE__, __LINE__, GetLastError()); + fatal_from_lasterror(WFILE, __LINE__, GetLastError()); } auto handle_in = GetStdHandle(STD_INPUT_HANDLE); if(handle_in == INVALID_HANDLE_VALUE) { - fatal_from_lasterror(__FILE__, __LINE__, GetLastError()); + fatal_from_lasterror(WFILE, __LINE__, GetLastError()); } CONSOLE_FONT_INFOEX cfi = { sizeof(CONSOLE_FONT_INFOEX) }; if(!GetCurrentConsoleFontEx(handle_out, false, &cfi)) { - fatal_from_lasterror(__FILE__, __LINE__, GetLastError()); + fatal_from_lasterror(WFILE, __LINE__, GetLastError()); } // Make sure to enforce a default Windows font that actually has U+2580 @@ -225,7 +214,7 @@ Platform_Win32Console Platform_Win32Console::init() cfi.dwFontSize.X = 4; cfi.dwFontSize.Y = 4; if(!SetCurrentConsoleFontEx(handle_out, false, &cfi)) { - fatal_from_lasterror(__FILE__, __LINE__, GetLastError()); + fatal_from_lasterror(WFILE, __LINE__, GetLastError()); } CONSOLE_CURSOR_INFO cursor_info = { 1, false };