hkimi / src /common /console.cpp
Ozomozyan's picture
hkimi 0.1.0
68888be verified
Raw History Blame Contribute Delete
3.93 kB
#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