16
16
#endif
17
17
18
18
#if defined (_WIN32)
19
+ #include < fcntl.h>
20
+ #include < io.h>
19
21
#pragma comment(lib,"kernel32.lib")
20
22
extern " C" __declspec(dllimport) void * __stdcall GetStdHandle (unsigned long nStdHandle);
21
23
extern " C" __declspec(dllimport) int __stdcall GetConsoleMode (void * hConsoleHandle, unsigned long * lpMode);
22
24
extern " C" __declspec(dllimport) int __stdcall SetConsoleMode (void * hConsoleHandle, unsigned long dwMode);
23
25
extern " C" __declspec(dllimport) int __stdcall SetConsoleCP (unsigned int wCodePageID);
24
26
extern " C" __declspec(dllimport) int __stdcall SetConsoleOutputCP (unsigned int wCodePageID);
27
+ extern " C" __declspec(dllimport) int __stdcall WideCharToMultiByte (unsigned int CodePage, unsigned long dwFlags,
28
+ const wchar_t * lpWideCharStr, int cchWideChar,
29
+ char * lpMultiByteStr, int cbMultiByte,
30
+ const char * lpDefaultChar, bool * lpUsedDefaultChar);
31
+ #define CP_UTF8 65001
25
32
#endif
26
33
27
34
bool gpt_params_parse (int argc, char ** argv, gpt_params & params) {
@@ -307,12 +314,20 @@ void win32_console_init(bool enable_color) {
307
314
SetConsoleMode (hConOut, dwMode | 0x4 ); // ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
308
315
}
309
316
// Set console output codepage to UTF8
310
- SetConsoleOutputCP (65001 ); // CP_UTF8
317
+ SetConsoleOutputCP (CP_UTF8);
311
318
}
312
319
void * hConIn = GetStdHandle ((unsigned long )-10 ); // STD_INPUT_HANDLE (-10)
313
320
if (hConIn && hConIn != (void *)-1 && GetConsoleMode (hConIn, &dwMode)) {
314
- // Set console input codepage to UTF8
315
- SetConsoleCP ( 65001 ); // CP_UTF8
321
+ // Set console input codepage to UTF16
322
+ _setmode ( _fileno (stdin), _O_WTEXT);
316
323
}
317
324
}
325
+
326
+ // Convert a wide Unicode string to an UTF8 string
327
+ void win32_utf8_encode (const std::wstring & wstr, std::string & str) {
328
+ int size_needed = WideCharToMultiByte (CP_UTF8, 0 , &wstr[0 ], (int )wstr.size (), NULL , 0 , NULL , NULL );
329
+ std::string strTo (size_needed, 0 );
330
+ WideCharToMultiByte (CP_UTF8, 0 , &wstr[0 ], (int )wstr.size (), &strTo[0 ], size_needed, NULL , NULL );
331
+ str = strTo;
332
+ }
318
333
#endif
0 commit comments