Convert STFS filenames from Win-1252 to UTF-8
This commit is contained in:
committed by
Radosław Gliński
parent
60b31af811
commit
6cba5ba7e6
@@ -14,6 +14,9 @@
|
||||
#include <locale>
|
||||
|
||||
#include "xenia/base/platform.h"
|
||||
#if XE_PLATFORM_WIN32
|
||||
#include "xenia/base/platform_win.h"
|
||||
#endif
|
||||
|
||||
#if !XE_PLATFORM_WIN32
|
||||
#include <strings.h>
|
||||
@@ -58,4 +61,78 @@ std::u16string to_utf16(const std::string_view source) {
|
||||
return utfcpp::utf8to16(source);
|
||||
}
|
||||
|
||||
std::string utf8_to_win1252(const std::string_view source) {
|
||||
#if XE_PLATFORM_WIN32
|
||||
std::string input_str(source);
|
||||
int srclen = static_cast<int>(input_str.size());
|
||||
|
||||
int wlen =
|
||||
MultiByteToWideChar(CP_UTF8, 0, input_str.c_str(), srclen, NULL, 0);
|
||||
if (!wlen) {
|
||||
return "";
|
||||
}
|
||||
std::vector<WCHAR> wbuf(wlen);
|
||||
int result = MultiByteToWideChar(CP_UTF8, 0, input_str.c_str(), srclen,
|
||||
wbuf.data(), wlen);
|
||||
if (!result) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int len = WideCharToMultiByte(1252, 0, wbuf.data(), wlen, NULL, 0, "_", NULL);
|
||||
if (!len) {
|
||||
return "";
|
||||
}
|
||||
std::vector<CHAR> buf(len);
|
||||
result = WideCharToMultiByte(1252, 0, wbuf.data(), wlen, buf.data(), len, "_",
|
||||
NULL);
|
||||
if (!result) {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string output_str(buf.begin(), buf.end());
|
||||
return output_str;
|
||||
#else
|
||||
// TODO: Use iconv on POSIX.
|
||||
std::string output_str(source);
|
||||
return output_str;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string win1252_to_utf8(const std::string_view source) {
|
||||
#if XE_PLATFORM_WIN32
|
||||
std::string input_str(source);
|
||||
int srclen = static_cast<int>(input_str.size());
|
||||
|
||||
int wlen = MultiByteToWideChar(1252, 0, input_str.c_str(), srclen, NULL, 0);
|
||||
if (!wlen) {
|
||||
return "";
|
||||
}
|
||||
std::vector<WCHAR> wbuf(wlen);
|
||||
int result = MultiByteToWideChar(1252, 0, input_str.c_str(), srclen,
|
||||
wbuf.data(), wlen);
|
||||
if (!result) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int len =
|
||||
WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wlen, NULL, 0, "_", NULL);
|
||||
if (!len) {
|
||||
return "";
|
||||
}
|
||||
std::vector<CHAR> buf(len);
|
||||
result = WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wlen, buf.data(), len,
|
||||
"_", NULL);
|
||||
if (!result) {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string output_str(buf.begin(), buf.end());
|
||||
return output_str;
|
||||
#else
|
||||
// TODO: Use iconv on POSIX.
|
||||
std::string output_str(source);
|
||||
return output_str;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace xe
|
||||
|
||||
Reference in New Issue
Block a user