forked from WasmEdge/WasmEdge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PO] Use
system/winapi.h
instead of windows.h
Signed-off-by: Shen-Ta Hsieh <[email protected]>
- Loading branch information
Showing
3 changed files
with
43 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,50 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2019-2022 Second State INC | ||
// | ||
#include "wasmedge/wincli_helper.h" | ||
|
||
#if WASMEDGE_WINCLI_HELPER_ENABLE | ||
#include <Windows.h> | ||
|
||
#include <string.h> | ||
#include "system/winapi.h" | ||
#include <cwchar> | ||
|
||
static const char *UTF16ToUTF8(const wchar_t *U16Str) { | ||
static const char *EmptyString = ""; | ||
|
||
int U16StrLen = int(wcslen(U16Str)); | ||
auto BufSize = WideCharToMultiByte(CP_UTF8, 0, U16Str, U16StrLen, nullptr, 0, | ||
nullptr, nullptr); | ||
auto BufSize = winapi::WideCharToMultiByte( | ||
winapi::CP_UTF8_, 0, U16Str, U16StrLen, nullptr, 0, nullptr, nullptr); | ||
if (BufSize == 0) { | ||
return EmptyString; | ||
} | ||
// for '\0' | ||
BufSize = BufSize + 1; | ||
++BufSize; | ||
|
||
char *Buf = new char[size_t(BufSize)]; | ||
::memset(Buf, 0, size_t(BufSize)); | ||
char *Buf = new char[static_cast<size_t>(BufSize)]; | ||
|
||
WideCharToMultiByte(CP_UTF8, 0, U16Str, U16StrLen, Buf, BufSize, nullptr, | ||
nullptr); | ||
winapi::WideCharToMultiByte(winapi::CP_UTF8_, 0, U16Str, U16StrLen, Buf, | ||
BufSize, nullptr, nullptr); | ||
return Buf; | ||
} | ||
|
||
void WasmEdge_SetConsoleOutputCPtoUTF8() { SetConsoleOutputCP(CP_UTF8); } | ||
wchar_t *WasmEdge_GetCommandLineW() { return GetCommandLineW(); } | ||
void WasmEdge_SetConsoleOutputCPtoUTF8() { | ||
#if WINAPI_PARTITION_DESKTOP | ||
winapi::SetConsoleOutputCP(CP_UTF8_); | ||
#endif | ||
} | ||
wchar_t *WasmEdge_GetCommandLineW() { return winapi::GetCommandLineW(); } | ||
|
||
const char **WasmEdge_CommandLineToUTF8ArgvW(const wchar_t *CmdLine, | ||
int *Argc) { | ||
auto ArgvW = CommandLineToArgvW(CmdLine, Argc); | ||
auto ArgvW = winapi::CommandLineToArgvW(CmdLine, Argc); | ||
|
||
if (ArgvW == nullptr) | ||
return nullptr; | ||
|
||
const char **Argv = new const char *[size_t(*Argc)]; | ||
const char **Argv = new const char *[static_cast<size_t>(*Argc)]; | ||
for (int I = 0; I < *Argc; I++) { | ||
Argv[I] = UTF16ToUTF8(ArgvW[I]); | ||
} | ||
|
||
LocalFree(ArgvW); | ||
winapi::LocalFree(ArgvW); | ||
return Argv; | ||
} | ||
#endif |