@@ -2258,13 +2258,33 @@ make_basic_authentication_header(const std::string &username,
2258
2258
2259
2259
namespace detail {
2260
2260
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
+
2261
2277
struct FileStat {
2262
2278
FileStat (const std::string &path);
2263
2279
bool is_file () const ;
2264
2280
bool is_dir () const ;
2265
2281
2266
2282
private:
2283
+ #if defined(_WIN32)
2284
+ struct _stat st_;
2285
+ #else
2267
2286
struct stat st_;
2287
+ #endif
2268
2288
int ret_ = -1 ;
2269
2289
};
2270
2290
@@ -2639,7 +2659,12 @@ inline bool is_valid_path(const std::string &path) {
2639
2659
}
2640
2660
2641
2661
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
2642
2666
ret_ = stat (path.c_str (), &st_);
2667
+ #endif
2643
2668
}
2644
2669
inline bool FileStat::is_file () const {
2645
2670
return ret_ >= 0 && S_ISREG (st_.st_mode );
@@ -2909,19 +2934,8 @@ inline bool mmap::open(const char *path) {
2909
2934
close ();
2910
2935
2911
2936
#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 ; }
2925
2939
2926
2940
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
2927
2941
hFile_ = ::CreateFile2 (wpath.c_str (), GENERIC_READ, FILE_SHARE_READ,
0 commit comments