The MuEmu-0.97k updata23 does not support GBK format, resulting in Chinese character garbling. How can this be resolved?
C++:
bool CMemScript::SetBuffer(char* path)
{
strcpy_s(this->m_path, path);
std::ifstream file(this->m_path, std::ios::binary | std::ios::ate);
if (!file.is_open())
{
SetLastError(0);
return false;
}
size_t fileSize = (size_t)file.tellg();
file.seekg(0, std::ios::beg);
this->m_buffer.resize(fileSize);
if (!file.read(&this->m_buffer[0], fileSize))
{
SetLastError(2);
return false;
}
file.close();
if (this->m_buffer.size() >= 3 && this->m_buffer[0] == char(0xEF) && this->m_buffer[1] == char(0xBB) && this->m_buffer[2] == char(0xBF))
{
this->m_buffer.erase(this->m_buffer.begin(), this->m_buffer.begin() + 3);
}
else if (IsEncodingAnsi())
{
std::string utf8Content(this->m_buffer.begin(), this->m_buffer.end());
std::wstring utf16Content;
utf16Content.resize(MultiByteToWideChar(CP_ACP, 0, utf8Content.c_str(), utf8Content.size(), 0, 0));
MultiByteToWideChar(CP_ACP, 0, utf8Content.c_str(), utf8Content.size(), &utf16Content[0], utf16Content.size());
int utf8Size = WideCharToMultiByte(CP_UTF8, 0, utf16Content.c_str(), utf16Content.size(), 0, 0, 0, 0);
std::string utf8Buffer(utf8Size, '\0');
WideCharToMultiByte(CP_UTF8, 0, utf16Content.c_str(), utf16Content.size(), &utf8Buffer[0], utf8Buffer.size(), 0, 0);
this->m_buffer.resize(utf8Buffer.size());
std::copy(utf8Buffer.begin(), utf8Buffer.end(), this->m_buffer.begin());
}
this->m_position = 0;
this->m_tick = GetTickCount();
this->m_line = 1;
return true;
}
bool CMemScript::IsEncodingAnsi()
{
for (size_t n = 0; n < this->m_buffer.size(); n++)
{
if ((this->m_buffer[n] & 0x80) == 0x00)
{
continue;
}
else if ((this->m_buffer[n] & 0xE0) == 0xC0)
{
if ((n + 1) >= this->m_buffer.size() || (this->m_buffer[n + 1] & 0xC0) != 0x80)
{
return true;
}
n += 1;
}
else if ((this->m_buffer[n] & 0xF0) == 0xE0)
{
if ((n + 2) >= this->m_buffer.size() || (this->m_buffer[n + 1] & 0xC0) != 0x80 || (this->m_buffer[n + 2] & 0xC0) != 0x80)
{
return true;
}
n += 2;
}
else
{
return true;
}
}
return false;
}