Skip to content

Commit

Permalink
Support unicode characters in model path (#3681)
Browse files Browse the repository at this point in the history
* parse wide argv characters on windows

* cleanup

* move cleanup to end of `main`
  • Loading branch information
jmorganca committed Apr 16, 2024
1 parent 7afb2e1 commit 7c9792a
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions llm/ext_server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include "httplib.h"
#include "json.hpp"

#if defined(_WIN32)
#include <windows.h>
#endif

#include <cstddef>
#include <thread>
#include <chrono>
Expand Down Expand Up @@ -2770,8 +2774,28 @@ inline void signal_handler(int signal) {
shutdown_handler(signal);
}

int main(int argc, char **argv)
{
#if defined(_WIN32)
char* wchar_to_char(const wchar_t* wstr) {
if (wstr == nullptr) return nullptr;

// Determine the number of bytes needed for the UTF-8 string
int bytes = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
char* str = new char[bytes];

// Convert the wide-character string to a UTF-8 string
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, bytes, nullptr, nullptr);
return str;
}

int wmain(int argc, wchar_t **wargv) {
char** argv = new char*[argc];
for (int i = 0; i < argc; ++i) {
argv[i] = wchar_to_char(wargv[i]);
}
#else
int main(int argc, char **argv) {
#endif

#if SERVER_VERBOSE != 1
log_disable();
#endif
Expand Down Expand Up @@ -3282,6 +3306,11 @@ int main(int argc, char **argv)
return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
};
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);

for (int i = 0; i < argc; ++i) {
delete[] argv[i];
}
delete[] argv;
#endif
llama.queue_tasks.start_loop();
svr.stop();
Expand Down

0 comments on commit 7c9792a

Please sign in to comment.