Skip to content

Commit cc89f4d

Browse files
committed
Fix problems on English locale
1 parent 7604602 commit cc89f4d

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

httplib.h

+27-13
Original file line numberDiff line numberDiff line change
@@ -2258,13 +2258,33 @@ make_basic_authentication_header(const std::string &username,
22582258

22592259
namespace detail {
22602260

2261+
#if defined(_WIN32)
2262+
std::wstring u8string_to_wstring(const char *s) {
2263+
std::wstring ws;
2264+
auto len = static_cast<int>(strlen(s));
2265+
auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0);
2266+
if (wlen > 0) {
2267+
ws.resize(wlen);
2268+
wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, const_cast<LPWSTR>(reinterpret_cast<LPCWSTR>(ws.data())), wlen);
2269+
if (wlen != ws.size()) {
2270+
ws.clear();
2271+
}
2272+
}
2273+
return ws;
2274+
}
2275+
#endif
2276+
22612277
struct FileStat {
22622278
FileStat(const std::string &path);
22632279
bool is_file() const;
22642280
bool is_dir() const;
22652281

22662282
private:
2283+
#if defined(_WIN32)
2284+
struct _stat st_;
2285+
#else
22672286
struct stat st_;
2287+
#endif
22682288
int ret_ = -1;
22692289
};
22702290

@@ -2639,7 +2659,12 @@ inline bool is_valid_path(const std::string &path) {
26392659
}
26402660

26412661
inline FileStat::FileStat(const std::string &path) {
2662+
#if defined(_WIN32)
2663+
auto wpath = u8string_to_wstring(path.c_str());
2664+
ret_ = _wstat(wpath.c_str(), &st_);
2665+
#else
26422666
ret_ = stat(path.c_str(), &st_);
2667+
#endif
26432668
}
26442669
inline bool FileStat::is_file() const {
26452670
return ret_ >= 0 && S_ISREG(st_.st_mode);
@@ -2909,19 +2934,8 @@ inline bool mmap::open(const char *path) {
29092934
close();
29102935

29112936
#if defined(_WIN32)
2912-
std::wstring wpath;
2913-
{
2914-
auto cp = ::GetACP();
2915-
2916-
auto len = static_cast<int>(strlen(path));
2917-
auto wlen = ::MultiByteToWideChar(cp, 0, path, len, nullptr, 0);
2918-
if (wlen <= 0) { return false; }
2919-
2920-
wpath.resize(wlen);
2921-
auto pwpath = const_cast<LPWSTR>(reinterpret_cast<LPCWSTR>(wpath.data()));
2922-
wlen = ::MultiByteToWideChar(cp, 0, path, len, pwpath, wlen);
2923-
if (wlen != wpath.size()) { return false; }
2924-
}
2937+
auto wpath = u8string_to_wstring(path);
2938+
if (wpath.empty()) { return false; }
29252939

29262940
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
29272941
hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,

test/test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -5287,7 +5287,7 @@ TEST(MountTest, MultibytesPathName) {
52875287

52885288
Client cli("localhost", PORT);
52895289

5290-
auto res = cli.Get("/日本語Dir/日本語File.txt");
5290+
auto res = cli.Get(u8"/日本語Dir/日本語File.txt");
52915291
ASSERT_TRUE(res);
52925292
EXPECT_EQ(StatusCode::OK_200, res->status);
52935293
EXPECT_EQ(u8"日本語コンテンツ", res->body);

0 commit comments

Comments
 (0)