handle wide characters in llama_file examples
This commit is contained in:
parent
8a11598b1e
commit
18504b6a34
4 changed files with 84 additions and 0 deletions
|
@ -716,7 +716,28 @@ struct llama_file {
|
|||
size_t size;
|
||||
|
||||
llama_file(const char * fname, const char * mode) {
|
||||
#ifdef _WIN32
|
||||
// temporarily change the locale to the system default to handle Unicode file names
|
||||
std::string oldLocale = std::setlocale(LC_ALL, nullptr);
|
||||
std::setlocale(LC_ALL, "");
|
||||
|
||||
// convert multi-byte string to wide-char string
|
||||
int wsize = MultiByteToWideChar(CP_UTF8, 0, fname, -1, nullptr, 0);
|
||||
std::vector<wchar_t> wfname(wsize);
|
||||
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname.data(), wsize);
|
||||
|
||||
// determine the correct wide-character mode string
|
||||
std::wstring wmode;
|
||||
for(; *mode; ++mode) {
|
||||
wmode += wchar_t(*mode);
|
||||
}
|
||||
|
||||
fp = _wfopen(wfname.data(), wmode.c_str());
|
||||
|
||||
std::setlocale(LC_ALL, oldLocale.c_str());
|
||||
#else
|
||||
fp = std::fopen(fname, mode);
|
||||
#endif
|
||||
if (fp == NULL) {
|
||||
size = 0;
|
||||
} else {
|
||||
|
|
|
@ -464,7 +464,28 @@ struct llama_file {
|
|||
size_t size;
|
||||
|
||||
llama_file(const char * fname, const char * mode) {
|
||||
#ifdef _WIN32
|
||||
// temporarily change the locale to the system default to handle Unicode file names
|
||||
std::string oldLocale = std::setlocale(LC_ALL, nullptr);
|
||||
std::setlocale(LC_ALL, "");
|
||||
|
||||
// convert multi-byte string to wide-char string
|
||||
int wsize = MultiByteToWideChar(CP_UTF8, 0, fname, -1, nullptr, 0);
|
||||
std::vector<wchar_t> wfname(wsize);
|
||||
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname.data(), wsize);
|
||||
|
||||
// determine the correct wide-character mode string
|
||||
std::wstring wmode;
|
||||
for(; *mode; ++mode) {
|
||||
wmode += wchar_t(*mode);
|
||||
}
|
||||
|
||||
fp = _wfopen(wfname.data(), wmode.c_str());
|
||||
|
||||
std::setlocale(LC_ALL, oldLocale.c_str());
|
||||
#else
|
||||
fp = std::fopen(fname, mode);
|
||||
#endif
|
||||
if (fp == NULL) {
|
||||
size = 0;
|
||||
} else {
|
||||
|
|
|
@ -952,7 +952,28 @@ struct llama_file {
|
|||
size_t size;
|
||||
|
||||
llama_file(const char * fname, const char * mode) {
|
||||
#ifdef _WIN32
|
||||
// temporarily change the locale to the system default to handle Unicode file names
|
||||
std::string oldLocale = std::setlocale(LC_ALL, nullptr);
|
||||
std::setlocale(LC_ALL, "");
|
||||
|
||||
// convert multi-byte string to wide-char string
|
||||
int wsize = MultiByteToWideChar(CP_UTF8, 0, fname, -1, nullptr, 0);
|
||||
std::vector<wchar_t> wfname(wsize);
|
||||
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname.data(), wsize);
|
||||
|
||||
// determine the correct wide-character mode string
|
||||
std::wstring wmode;
|
||||
for(; *mode; ++mode) {
|
||||
wmode += wchar_t(*mode);
|
||||
}
|
||||
|
||||
fp = _wfopen(wfname.data(), wmode.c_str());
|
||||
|
||||
std::setlocale(LC_ALL, oldLocale.c_str());
|
||||
#else
|
||||
fp = std::fopen(fname, mode);
|
||||
#endif
|
||||
if (fp == NULL) {
|
||||
size = 0;
|
||||
} else {
|
||||
|
|
21
llama.cpp
21
llama.cpp
|
@ -988,7 +988,28 @@ struct llama_file {
|
|||
size_t size;
|
||||
|
||||
llama_file(const char * fname, const char * mode) {
|
||||
#ifdef _WIN32
|
||||
// temporarily change the locale to the system default to handle Unicode file names
|
||||
std::string oldLocale = std::setlocale(LC_ALL, nullptr);
|
||||
std::setlocale(LC_ALL, "");
|
||||
|
||||
// convert multi-byte string to wide-char string
|
||||
int wsize = MultiByteToWideChar(CP_UTF8, 0, fname, -1, nullptr, 0);
|
||||
std::vector<wchar_t> wfname(wsize);
|
||||
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname.data(), wsize);
|
||||
|
||||
// determine the correct wide-character mode string
|
||||
std::wstring wmode;
|
||||
for(; *mode; ++mode) {
|
||||
wmode += wchar_t(*mode);
|
||||
}
|
||||
|
||||
fp = _wfopen(wfname.data(), wmode.c_str());
|
||||
|
||||
std::setlocale(LC_ALL, oldLocale.c_str());
|
||||
#else
|
||||
fp = std::fopen(fname, mode);
|
||||
#endif
|
||||
if (fp == NULL) {
|
||||
throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue