Commit 6c0dc1a7 authored by Yasuhiro Matsumoto's avatar Yasuhiro Matsumoto

print unicode on windows console

parent 2d29d140
...@@ -3,16 +3,32 @@ ...@@ -3,16 +3,32 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#if defined(__MINGW32__) || defined(__MINGW64__)
# include <windows.h>
# include <io.h>
#endif
static void static void
printstr(mrb_state *mrb, mrb_value obj) printstr(mrb_state *mrb, mrb_value obj)
{ {
if (mrb_string_p(obj)) { if (mrb_string_p(obj)) {
char* ptr = mrb_locale_from_utf8(RSTRING_PTR(obj), RSTRING_LEN(obj)); #if defined(__MINGW32__) || defined(__MINGW64__)
if (ptr) { if (isatty(fileno(stdout))) {
fwrite(ptr, strlen(ptr), 1, stdout); DWORD written;
mrb_locale_free(ptr); int mlen = RSTRING_LEN(obj);
char* utf8 = RSTRING_PTR(obj);
int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, NULL, 0);
wchar_t* utf16 = mrb_malloc(mrb, (wlen+1) * sizeof(wchar_t));
if (utf16 == NULL) return;
if (MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, utf16, wlen) > 0) {
utf16[wlen] = 0;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
utf16, wlen, &written, NULL);
} }
mrb_free(mrb, utf16);
} else
#endif
fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment