Skip to content

Commit cdd8931

Browse files
committed
Added handling for non-ASCII arguments on Windows
1 parent bc07349 commit cdd8931

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

common/arg.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# define NOMINMAX
1515
#endif
1616
#include <windows.h>
17+
#include <shellapi.h>
1718
#endif
1819

1920
#define JSON_ASSERT GGML_ASSERT
@@ -1626,7 +1627,44 @@ static void add_rpc_devices(const std::string & servers) {
16261627
}
16271628
}
16281629

1630+
#ifdef _WIN32
1631+
struct Utf8Argv {
1632+
std::vector<std::string> buf;
1633+
std::vector<char*> ptrs;
1634+
};
1635+
1636+
static Utf8Argv make_utf8_argv_from_winapi() {
1637+
Utf8Argv out;
1638+
int wargc = 0;
1639+
LPWSTR* wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
1640+
if (!wargv) return out;
1641+
1642+
out.buf.reserve(wargc);
1643+
for (int i = 0; i < wargc; ++i) {
1644+
int n = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wargv[i], -1, nullptr, 0, nullptr, nullptr);
1645+
if (n <= 0) { out.buf.emplace_back(); continue; }
1646+
auto& s = out.buf.emplace_back();
1647+
s.resize(static_cast<size_t>(n - 1));
1648+
(void)WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, s.data(), n, nullptr, nullptr);
1649+
}
1650+
LocalFree(wargv);
1651+
1652+
out.ptrs.reserve(out.buf.size() + 1);
1653+
for (auto& s : out.buf) out.ptrs.push_back(s.data());
1654+
out.ptrs.push_back(nullptr);
1655+
return out;
1656+
}
1657+
#endif
1658+
16291659
bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **)) {
1660+
#ifdef _WIN32
1661+
auto utf8 = make_utf8_argv_from_winapi();
1662+
if (!utf8.ptrs.empty()) {
1663+
argc = static_cast<int>(utf8.buf.size());
1664+
argv = utf8.ptrs.data();
1665+
}
1666+
#endif
1667+
16301668
auto ctx_arg = common_params_parser_init(params, ex, print_usage);
16311669
const common_params params_org = ctx_arg.params; // the example can modify the default params
16321670

0 commit comments

Comments
 (0)