fix:解决中文乱码BUG。

This commit is contained in:
2024-05-24 09:18:06 +08:00
parent 019740f550
commit 3d1fcc5dc8
6 changed files with 90 additions and 80 deletions

View File

@@ -1,32 +1,6 @@
#include "xml_opr.h"
#include <filesystem>
#ifdef _WIN32
#include <windows.h>
std::string utf8_to_gbk(const std::string& utf8_str)
{
// UTF-8 to Wide Char (UTF-16)
int wide_char_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_len == 0) {
throw std::runtime_error("Failed to convert UTF-8 to wide char");
}
std::wstring wide_str(wide_char_len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, &wide_str[0], wide_char_len);
// Wide Char (UTF-16) to GBK
int gbk_len = WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (gbk_len == 0) {
throw std::runtime_error("Failed to convert wide char to GBK");
}
std::string gbk_str(gbk_len, 0);
WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, &gbk_str[0], gbk_len, nullptr, nullptr);
return gbk_str;
}
#endif
namespace fs = std::filesystem;
CXmlOpr::CXmlOpr() = default;
@@ -35,7 +9,7 @@ CXmlOpr::~CXmlOpr() = default;
bool CXmlOpr::open(const std::string& xml_path)
{
#ifdef _WIN32
if (doc_.LoadFile(utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
return false;
}
#else
@@ -81,7 +55,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
std::string next_node{};
std::string node_path = opr_base_.node_path;
keys_.clear();
auto keys = splitString(opr_base_.purpose, ",");
auto keys = CUtil::splitString(opr_base_.purpose, ",");
for (const auto& item : keys) {
if (item.empty()) {
continue;
@@ -89,7 +63,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
keys_.push_back(item);
}
auto nodes = splitString(opr_base_.node_path, "/");
auto nodes = CUtil::splitString(opr_base_.node_path, "/");
for (const auto& item : nodes) {
if (item.empty()) {