mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-19 03:33:56 +00:00
handle unicode file open
This commit is contained in:
@@ -25,4 +25,40 @@ inline std::string u16_to_u8(std::u16string_view u16_view) {
|
||||
}
|
||||
}
|
||||
return utf8_str;
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string w_to_u8(std::wstring_view w_view) {
|
||||
std::string utf8_str;
|
||||
utf8_str.reserve(w_view.size() * 2);
|
||||
for (char16_t ch : w_view) {
|
||||
if (ch <= 0x7F) {
|
||||
utf8_str.push_back(static_cast<char>(ch));
|
||||
} else if (ch <= 0x7FF) {
|
||||
utf8_str.push_back(static_cast<char>(0xC0 | (ch >> 6)));
|
||||
utf8_str.push_back(static_cast<char>(0x80 | (ch & 0x3F)));
|
||||
} else {
|
||||
utf8_str.push_back(static_cast<char>(0xE0 | (ch >> 12)));
|
||||
utf8_str.push_back(static_cast<char>(0x80 | ((ch >> 6) & 0x3F)));
|
||||
utf8_str.push_back(static_cast<char>(0x80 | (ch & 0x3F)));
|
||||
}
|
||||
}
|
||||
return utf8_str;
|
||||
}
|
||||
|
||||
#ifndef OS_WINDOWS
|
||||
inline int open_unicode(FILE** handle, std::u16string fileName, std::u16string mode)
|
||||
{
|
||||
*handle = fopen(u16_to_u8(fileName).c_str(), u16_to_u8(mode).c_str());
|
||||
return errno;
|
||||
}
|
||||
#else
|
||||
inline std::wstring u16_to_w(const std::u16string& u16str) {
|
||||
return std::wstring(reinterpret_cast<const wchar_t*>(u16str.data()), u16str.size());
|
||||
}
|
||||
|
||||
inline auto open_unicode(FILE** handle, std::u16string fileName, std::u16string mode)
|
||||
{
|
||||
return _wfopen_s(handle, u16_to_w(fileName).c_str(), u16_to_w(mode).c_str());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user