[270] | 1 | #include "mruby.h"
|
---|
| 2 | #include "mruby/string.h"
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <string.h>
|
---|
| 5 | #include <stdlib.h>
|
---|
| 6 | #if defined(__MINGW32__) || defined(__MINGW64__)
|
---|
| 7 | # include <windows.h>
|
---|
| 8 | # include <io.h>
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
| 11 | static void
|
---|
| 12 | printstr(mrb_state *mrb, mrb_value obj)
|
---|
| 13 | {
|
---|
| 14 | if (mrb_string_p(obj)) {
|
---|
| 15 | #if defined(__MINGW32__) || defined(__MINGW64__)
|
---|
| 16 | if (isatty(fileno(stdout))) {
|
---|
| 17 | DWORD written;
|
---|
| 18 | int mlen = RSTRING_LEN(obj);
|
---|
| 19 | char* utf8 = RSTRING_PTR(obj);
|
---|
| 20 | int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, NULL, 0);
|
---|
| 21 | wchar_t* utf16 = mrb_malloc(mrb, (wlen+1) * sizeof(wchar_t));
|
---|
| 22 | if (utf16 == NULL) return;
|
---|
| 23 | if (MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, utf16, wlen) > 0) {
|
---|
| 24 | utf16[wlen] = 0;
|
---|
| 25 | WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
|
---|
| 26 | utf16, wlen, &written, NULL);
|
---|
| 27 | }
|
---|
| 28 | mrb_free(mrb, utf16);
|
---|
| 29 | } else
|
---|
| 30 | #endif
|
---|
| 31 | fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | /* 15.3.1.2.9 */
|
---|
| 36 | /* 15.3.1.3.34 */
|
---|
| 37 | mrb_value
|
---|
| 38 | mrb_printstr(mrb_state *mrb, mrb_value self)
|
---|
| 39 | {
|
---|
| 40 | mrb_value argv;
|
---|
| 41 |
|
---|
| 42 | mrb_get_args(mrb, "o", &argv);
|
---|
| 43 | printstr(mrb, argv);
|
---|
| 44 |
|
---|
| 45 | return argv;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void
|
---|
| 49 | mrb_mruby_print_gem_init(mrb_state* mrb)
|
---|
| 50 | {
|
---|
| 51 | struct RClass *krn;
|
---|
| 52 | krn = mrb->kernel_module;
|
---|
| 53 | mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, MRB_ARGS_REQ(1));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void
|
---|
| 57 | mrb_mruby_print_gem_final(mrb_state* mrb)
|
---|
| 58 | {
|
---|
| 59 | }
|
---|