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