@@ -2258,13 +2258,33 @@ make_basic_authentication_header(const std::string &username,
22582258
22592259namespace 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+
22612277struct FileStat {
22622278 FileStat (const std::string &path);
22632279 bool is_file () const ;
22642280 bool is_dir () const ;
22652281
22662282private:
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
26412661inline 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}
26442669inline 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,
0 commit comments