summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-print/src/print.c
blob: 789808deb3222ad324ab26278e21bde296b99839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <mruby.h>

#ifdef MRB_NO_STDIO
# error print conflicts 'MRB_NO_STDIO' in your build configuration
#endif

#include <mruby/string.h>
#include <string.h>
#if defined(_WIN32)
# include <windows.h>
# include <io.h>
#ifdef _MSC_VER
# define isatty(x) _isatty(x)
# define fileno(x) _fileno(x)
#endif
#endif

static void
printstr(mrb_state *mrb, const char *p, mrb_int len)
{
#if defined(_WIN32)
  if (isatty(fileno(stdout))) {
    DWORD written;
    int wlen = MultiByteToWideChar(CP_UTF8, 0, p, (int)len, NULL, 0);
    wchar_t* utf16 = (wchar_t*)mrb_malloc(mrb, (wlen+1) * sizeof(wchar_t));
    if (MultiByteToWideChar(CP_UTF8, 0, p, (int)len, utf16, wlen) > 0) {
      utf16[wlen] = 0;
      WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
                    utf16, (DWORD)wlen, &written, NULL);
    }
    mrb_free(mrb, utf16);
  } else
#endif
    fwrite(p, (size_t)len, 1, stdout);
  fflush(stdout);
}

static mrb_value
mrb_printstr(mrb_state *mrb, mrb_value self)
{
  mrb_value s = mrb_get_arg1(mrb);

  if (mrb_string_p(s)) {
    printstr(mrb, RSTRING_PTR(s), RSTRING_LEN(s));
  }
  return s;
}

void
mrb_mruby_print_gem_init(mrb_state* mrb)
{
  struct RClass *krn;
  krn = mrb->kernel_module;
  mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, MRB_ARGS_REQ(1));
}

void
mrb_mruby_print_gem_final(mrb_state* mrb)
{
}