File size: 3,933 Bytes
68888be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "common/console.h"

#include <algorithm>
#include <cstdio>
#include <cwchar>
#include <iostream>

#ifdef _WIN32
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <shellapi.h>
#else
#include <unistd.h>
#endif

namespace hk {

#ifdef _WIN32

static std::string narrow(const wchar_t* w, int n) {
    if (n == 0) return std::string();
    int len = WideCharToMultiByte(CP_UTF8, 0, w, n, nullptr, 0, nullptr, nullptr);
    std::string s((size_t)(len > 0 ? len : 0), '\0');
    if (len > 0) WideCharToMultiByte(CP_UTF8, 0, w, n, &s[0], len, nullptr, nullptr);
    return s;
}

static std::wstring widen(const std::string& s) {
    if (s.empty()) return std::wstring();
    int len = MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), nullptr, 0);
    std::wstring w((size_t)(len > 0 ? len : 0), L'\0');
    if (len > 0) MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), &w[0], len);
    return w;
}

std::vector<std::string> utf8_args(int argc, char** argv) {
    std::vector<std::string> out;
    int n = 0;
    LPWSTR* w = CommandLineToArgvW(GetCommandLineW(), &n);
    if (!w) {
        for (int i = 0; i < argc; ++i) out.push_back(argv[i]);
        return out;
    }
    for (int i = 0; i < n; ++i) out.push_back(narrow(w[i], (int)wcslen(w[i])));
    LocalFree(w);
    return out;
}

void console_init() {
    SetConsoleOutputCP(CP_UTF8);
    // No "\n" -> "\r\n" rewriting: piped output (token ids, text, JSON) must be the same on every OS.
    _setmode(_fileno(stdout), _O_BINARY);
    _setmode(_fileno(stderr), _O_BINARY);
}

static bool is_console(DWORD which, HANDLE* h) {
    *h = GetStdHandle(which);
    DWORD mode = 0;
    return *h != nullptr && *h != INVALID_HANDLE_VALUE && GetConsoleMode(*h, &mode) != 0;
}

void console_write(const std::string& utf8) {
    HANDLE h;
    fflush(stdout);
    if (is_console(STD_OUTPUT_HANDLE, &h)) {
        // a real console: UTF-16, right whatever the code page
        std::wstring w = widen(utf8);
        size_t done = 0;
        while (done < w.size()) {
            DWORD wrote = 0;
            DWORD chunk = (DWORD)std::min<size_t>(w.size() - done, 8192);
            if (!WriteConsoleW(h, w.data() + done, chunk, &wrote, nullptr) || wrote == 0) break;
            done += wrote;
        }
        return;
    }
    fwrite(utf8.data(), 1, utf8.size(), stdout);   // a pipe or a file: plain UTF-8 bytes
    fflush(stdout);
}

bool console_read_line(std::string& line) {
    line.clear();
    HANDLE h;
    if (is_console(STD_INPUT_HANDLE, &h)) {
        std::wstring w;
        wchar_t buf[1024];
        for (;;) {
            DWORD got = 0;
            if (!ReadConsoleW(h, buf, 1024, &got, nullptr) || got == 0) {
                if (w.empty()) return false;
                break;
            }
            w.append(buf, got);
            if (!w.empty() && w.back() == L'\n') break;
        }
        if (!w.empty() && w[0] == 0x1A) return false;   // Ctrl+Z: end of input
        while (!w.empty() && (w.back() == L'\n' || w.back() == L'\r')) w.pop_back();
        line = narrow(w.data(), (int)w.size());
        return true;
    }
    if (!std::getline(std::cin, line)) return false;
    while (!line.empty() && line.back() == '\r') line.pop_back();
    return true;
}

bool stdin_is_terminal() { return _isatty(_fileno(stdin)) != 0; }

#else

std::vector<std::string> utf8_args(int argc, char** argv) {
    std::vector<std::string> out;
    for (int i = 0; i < argc; ++i) out.push_back(argv[i]);
    return out;
}

void console_init() {}

void console_write(const std::string& utf8) {
    fwrite(utf8.data(), 1, utf8.size(), stdout);
    fflush(stdout);
}

bool console_read_line(std::string& line) {
    line.clear();
    if (!std::getline(std::cin, line)) return false;
    while (!line.empty() && line.back() == '\r') line.pop_back();
    return true;
}

bool stdin_is_terminal() { return isatty(0) != 0; }

#endif

}  // namespace hk