|
17 | 17 | # define NOMINMAX |
18 | 18 | #endif |
19 | 19 | #include <windows.h> |
| 20 | +#include <shellapi.h> |
20 | 21 | #endif |
21 | 22 |
|
22 | 23 | #define JSON_ASSERT GGML_ASSERT |
@@ -893,7 +894,44 @@ bool common_params_to_map(int argc, char ** argv, llama_example ex, std::map<com |
893 | 894 | return true; |
894 | 895 | } |
895 | 896 |
|
| 897 | +#ifdef _WIN32 |
| 898 | +struct utf8_argv { |
| 899 | + std::vector<std::string> buf; |
| 900 | + std::vector<char*> ptrs; |
| 901 | +}; |
| 902 | + |
| 903 | +static utf8_argv make_utf8_argv() { |
| 904 | + utf8_argv out; |
| 905 | + int wargc = 0; |
| 906 | + LPWSTR* wargv = CommandLineToArgvW(GetCommandLineW(), &wargc); |
| 907 | + if (!wargv) return out; |
| 908 | + |
| 909 | + out.buf.reserve(wargc); |
| 910 | + for (int i = 0; i < wargc; ++i) { |
| 911 | + int n = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wargv[i], -1, nullptr, 0, nullptr, nullptr); |
| 912 | + if (n <= 0) { out.buf.emplace_back(); continue; } |
| 913 | + auto& s = out.buf.emplace_back(); |
| 914 | + s.resize(static_cast<size_t>(n - 1)); |
| 915 | + (void)WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, s.data(), n, nullptr, nullptr); |
| 916 | + } |
| 917 | + LocalFree(wargv); |
| 918 | + |
| 919 | + out.ptrs.reserve(out.buf.size() + 1); |
| 920 | + for (auto& s : out.buf) out.ptrs.push_back(s.data()); |
| 921 | + out.ptrs.push_back(nullptr); |
| 922 | + return out; |
| 923 | +} |
| 924 | +#endif |
| 925 | + |
896 | 926 | bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **)) { |
| 927 | +#ifdef _WIN32 |
| 928 | + auto utf8 = make_utf8_argv(); |
| 929 | + if (!utf8.ptrs.empty()) { |
| 930 | + argc = static_cast<int>(utf8.buf.size()); |
| 931 | + argv = utf8.ptrs.data(); |
| 932 | + } |
| 933 | +#endif |
| 934 | + |
897 | 935 | auto ctx_arg = common_params_parser_init(params, ex, print_usage); |
898 | 936 | const common_params params_org = ctx_arg.params; // the example can modify the default params |
899 | 937 |
|
|
0 commit comments