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