Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Jan 20, 2024
1 parent 7a6df53 commit 66ecc16
Showing 1 changed file with 5 additions and 33 deletions.
38 changes: 5 additions & 33 deletions 3rd/lua/utf8_crt.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,9 @@ unsigned long __stdcall utf8_FormatMessageA(
return ret;
}

static UINT GetOutputCP(FILE* stream) {
HANDLE handle = (HANDLE)_get_osfhandle(_fileno(stream));
switch (GetFileType(handle)) {
case FILE_TYPE_DISK:
return CP_UTF8;
case FILE_TYPE_CHAR: {
DWORD mode;
if (GetConsoleMode(handle, &mode)) {
return GetConsoleOutputCP();
}
return CP_UTF8;
}
case FILE_TYPE_PIPE:
default:
return CP_UTF8;
}
}

static void ConsoleWrite(FILE* stream, const char* s, int l) {
UINT cp = GetOutputCP(stream);
if (cp == CP_UTF8) {
HANDLE handle = (HANDLE)_get_osfhandle(_fileno(stream));
if (FILE_TYPE_CHAR != GetFileType(handle)) {
fwrite(s, sizeof(char), l, stream);
return;
}
Expand All @@ -220,19 +202,9 @@ static void ConsoleWrite(FILE* stream, const char* s, int l) {
if (wmsg) {
wsz = MultiByteToWideChar(CP_UTF8, 0, s, l, wmsg, wsz);
if (wsz > 0) {
int sz = WideCharToMultiByte(cp, 0, wmsg, wsz, NULL, 0, NULL, NULL);
if (sz > 0) {
char* msg = (char*)calloc(sz, sizeof(char));
if (msg) {
sz = WideCharToMultiByte(cp, 0, wmsg, wsz, msg, sz, NULL, NULL);
if (sz > 0) {
fwrite(msg, sizeof(char), sz, stream);
free(msg);
free(wmsg);
return;
}
free(msg);
}
if (WriteConsoleW(handle, wmsg, wsz, NULL, NULL)) {
free(wmsg);
return;
}
}
free(wmsg);
Expand Down

0 comments on commit 66ecc16

Please sign in to comment.