|
14 | 14 | # define NOMINMAX |
15 | 15 | #endif |
16 | 16 | #include <windows.h> |
| 17 | +#include <shellapi.h> |
17 | 18 | #endif |
18 | 19 |
|
19 | 20 | #define JSON_ASSERT GGML_ASSERT |
@@ -1626,7 +1627,44 @@ static void add_rpc_devices(const std::string & servers) { |
1626 | 1627 | } |
1627 | 1628 | } |
1628 | 1629 |
|
| 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 | + |
1629 | 1659 | 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 | + |
1630 | 1668 | auto ctx_arg = common_params_parser_init(params, ex, print_usage); |
1631 | 1669 | const common_params params_org = ctx_arg.params; // the example can modify the default params |
1632 | 1670 |
|
|
0 commit comments