Function: ingest_args(...) -> vector Takes argc and argv arguments, and converts them to a vector of UTF-8 encoded strings, as an STL vector . In particular, it handles character encoding shenanigans on Windows. Note: raw_argc and raw_argv are not actually read at all on Windows. On Windows we call GetCommandLineW to get the arguments in wchar_t format, ignoring the regular argc/
| 78 | // TODO: potential opportunity to roll common stuff into common/console.cpp |
| 79 | // in relation to Windows wchar_t shenanigans. |
| 80 | static std::vector<std::string> ingest_args(int raw_argc, char ** raw_argv) { |
| 81 | std::vector<std::string> argv; |
| 82 | |
| 83 | // Handle Windows, if given non-ASCII arguments. |
| 84 | // We convert wchar_t arguments into UTF-8 char* on this platform. |
| 85 | // Lets you invoke 'tokenize' on Windows cmd.exe with non-ASCII characters |
| 86 | // without throwing tantrums. |
| 87 | #if defined(_WIN32) |
| 88 | int argc; |
| 89 | const LPWSTR cmdline_wargv = GetCommandLineW(); |
| 90 | LPWSTR * wargv = CommandLineToArgvW(cmdline_wargv, &argc); |
| 91 | |
| 92 | // silence unused arg warnings |
| 93 | (void) raw_argc; |
| 94 | (void) raw_argv; |
| 95 | |
| 96 | for (int i = 0; i < argc; ++i) { |
| 97 | int length_needed = WideCharToMultiByte(CP_UTF8, 0, wargv[i], wcslen(wargv[i]), 0, 0, NULL, NULL); |
| 98 | char * output_buf = (char *) calloc(length_needed+1, sizeof(char)); |
| 99 | GGML_ASSERT(output_buf); |
| 100 | |
| 101 | WideCharToMultiByte(CP_UTF8, 0, wargv[i], wcslen(wargv[i]), output_buf, length_needed, NULL, NULL); |
| 102 | output_buf[length_needed] = '\0'; |
| 103 | |
| 104 | argv.push_back(output_buf); |
| 105 | free(output_buf); |
| 106 | } |
| 107 | |
| 108 | LocalFree((HLOCAL) wargv); |
| 109 | #else |
| 110 | int argc = raw_argc; |
| 111 | for (int i = 0; i < argc; ++i) { |
| 112 | argv.push_back(raw_argv[i]); |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | GGML_ASSERT((unsigned int) argc == argv.size()); |
| 117 | |
| 118 | return argv; |
| 119 | } |
| 120 | |
| 121 | // |
| 122 | // Function: write_utf8_cstr_to_stdout(const char *) -> <writes to stdout> |