summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-print/src
diff options
context:
space:
mode:
authorYasuhiro Matsumoto <[email protected]>2015-10-08 23:52:58 +0900
committerYasuhiro Matsumoto <[email protected]>2015-10-08 23:52:58 +0900
commit6c0dc1a7200ff38e0ff5d01dbb62dd28db5de158 (patch)
treee80310befe6e3674ed7d29e0223d8f0826c0df1f /mrbgems/mruby-print/src
parent2d29d140f5f8282328edc280ebfbbe5e7178dc5c (diff)
downloadmruby-6c0dc1a7200ff38e0ff5d01dbb62dd28db5de158.tar.gz
mruby-6c0dc1a7200ff38e0ff5d01dbb62dd28db5de158.zip
print unicode on windows console
Diffstat (limited to 'mrbgems/mruby-print/src')
-rw-r--r--mrbgems/mruby-print/src/print.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/mrbgems/mruby-print/src/print.c b/mrbgems/mruby-print/src/print.c
index e7e21dd4b..9b09b12cc 100644
--- a/mrbgems/mruby-print/src/print.c
+++ b/mrbgems/mruby-print/src/print.c
@@ -3,16 +3,32 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#if defined(__MINGW32__) || defined(__MINGW64__)
+# include <windows.h>
+# include <io.h>
+#endif
static void
printstr(mrb_state *mrb, mrb_value obj)
{
if (mrb_string_p(obj)) {
- char* ptr = mrb_locale_from_utf8(RSTRING_PTR(obj), RSTRING_LEN(obj));
- if (ptr) {
- fwrite(ptr, strlen(ptr), 1, stdout);
- mrb_locale_free(ptr);
- }
+#if defined(__MINGW32__) || defined(__MINGW64__)
+ if (isatty(fileno(stdout))) {
+ DWORD written;
+ 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);
}
}