Open shivendra14 opened 5 years ago
https://stackoverflow.com/questions/8831143/windows-api-ansi-functions-and-utf-8
Can somebody help to fix this CreateFileA
and add support of UTF8
This is the code fix I have done locally to make this work:
std::wstring s2ws(const std::string& s)
{
int slength = (int)s.length() + 1;
int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, 0, 0);
auto buf = std::make_unique<wchar_t[]>(len);
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, buf.get(), len);
return buf.release();
}
template<
typename String,
typename = typename std::enable_if<
std::is_same<typename char_type<String>::type, char>::value
>::type
> file_handle_type open_file_helper(const String& path, const access_mode mode)
{
return ::CreateFileW(s2ws(path).c_str(),
mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
}
Thanks for using mio! And apologies for the late reply. mio doesn't support utf8 out of the box, sorry about the inconvenience. I have very little time for open source these days, so I would be glad if someone took up the task of adding utf8 support.
@jdarpinian,
std::wstring s2ws(const std::string& s)
you could simply have two overloads for toWideStr
that accept std::string
and std::wstring
and always do:
::CreateFileW(toWideStr(path).c_str(), ...
without using all that template mumbo-jumbo.
return buf.release();
FYI, this obviously leaks memory. Change it to return buf.get();
I was trying to mmap a file which has special characters in file name.
アイコン
mmap = mio::make_mmap_source(file.GetFullPath().as_UTF8(), error);
But the mapping fails.. does mio accepts UTF8? or should I be doing something else to read such file correctly.
This returns an invalid handle.