codec:处理编码问题。
This commit is contained in:
parent
34174d94f1
commit
df1dd36d3b
@ -105,7 +105,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
|
|||||||
|
|
||||||
ini_.set_work_exe(exe_path_);
|
ini_.set_work_exe(exe_path_);
|
||||||
base_ = ini_.get_config();
|
base_ = ini_.get_config();
|
||||||
ui->edStatus->setText(QString::fromLocal8Bit(base_.xml_path.c_str()));
|
ui->edStatus->setText(QString::fromStdString(base_.xml_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWidget::copy_key()
|
void MainWidget::copy_key()
|
||||||
|
@ -78,6 +78,6 @@ private:
|
|||||||
std::size_t cur_page_{1};
|
std::size_t cur_page_{1};
|
||||||
std::size_t all_page_{1};
|
std::size_t all_page_{1};
|
||||||
CAttributeEdit* attri_edit_{};
|
CAttributeEdit* attri_edit_{};
|
||||||
OprBase base_{};
|
OprBase base_{};
|
||||||
};
|
};
|
||||||
#endif // MAINWIDGET_H
|
#endif // MAINWIDGET_H
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
# 介绍
|
# 介绍
|
||||||
|
|
||||||
仅一层级的xml编辑器。
|
仅一层级的xml编辑器。
|
||||||
|
|
||||||
|
# 编译环境
|
||||||
|
|
||||||
|
在 `msvc Qt5.14.2` 和 `vs2017` 环境下通过。
|
@ -1,6 +1,6 @@
|
|||||||
本工具目前仅实现最基础的功能,未做各种兼容性处理与判断,因此有一些默认的约定。
|
本工具目前仅实现最基础的功能,未做各种兼容性处理与判断,因此有一些默认的约定。
|
||||||
|
|
||||||
(1)工具默认读取的配置名称为xmlopr.ini,位置在可执行文件同目录下。
|
(1)工具默认读取的配置名称为xmlopr.ini,位置在可执行文件同目录下(必须要有)。
|
||||||
(2)在windows下,ini内容如果含中文,需要GBK或者ANSI编码。
|
(2)ini配置文件为UTF-8编码。
|
||||||
(3)ini配置中的purpose一栏,请把不会重复的字段放在首位,程序会把这个字段作为检索key来查找存储数据。
|
(3)ini配置中的purpose一栏,请把不会重复的字段放在首位,程序会把这个字段作为检索key来查找存储数据。
|
||||||
(4)程序仅在启动时加载ini配置,更改需要重新打开程序。
|
(4)程序仅在启动时加载ini配置,更改需要重新打开程序。
|
@ -2,4 +2,4 @@
|
|||||||
oper_node=IODEF/ITEMS/
|
oper_node=IODEF/ITEMS/
|
||||||
the_node=ITEM
|
the_node=ITEM
|
||||||
purpose=name,cat,pr,pw
|
purpose=name,cat,pr,pw
|
||||||
xml_path=D:\中文文件夹 测试\conf-io.xml
|
xml_path=D:\conf-io.xml
|
@ -22,7 +22,7 @@ bool ConfigIni::set_work_exe(const std::string& dir)
|
|||||||
void ConfigIni::set_xml_path(const std::string& path)
|
void ConfigIni::set_xml_path(const std::string& path)
|
||||||
{
|
{
|
||||||
if (ini_.IsEmpty()) {
|
if (ini_.IsEmpty()) {
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
ini_.SetValue("Basic", "xml_path", path.c_str());
|
ini_.SetValue("Basic", "xml_path", path.c_str());
|
||||||
ini_.SaveFile(ini_path_.c_str());
|
ini_.SaveFile(ini_path_.c_str());
|
||||||
@ -38,6 +38,7 @@ bool ConfigIni::parse_ini()
|
|||||||
if (ini_.LoadFile(ini_path_.c_str()) != SI_OK) {
|
if (ini_.LoadFile(ini_path_.c_str()) != SI_OK) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
ini_.SetUnicode();
|
||||||
opr_base_.node_path = ini_.GetValue("Basic", "oper_node");
|
opr_base_.node_path = ini_.GetValue("Basic", "oper_node");
|
||||||
opr_base_.purpose = ini_.GetValue("Basic", "purpose");
|
opr_base_.purpose = ini_.GetValue("Basic", "purpose");
|
||||||
opr_base_.the_node = ini_.GetValue("Basic", "the_node");
|
opr_base_.the_node = ini_.GetValue("Basic", "the_node");
|
||||||
|
@ -1,6 +1,32 @@
|
|||||||
#include "xml_opr.h"
|
#include "xml_opr.h"
|
||||||
#include <filesystem>
|
#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;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
CXmlOpr::CXmlOpr() = default;
|
CXmlOpr::CXmlOpr() = default;
|
||||||
@ -8,9 +34,15 @@ CXmlOpr::~CXmlOpr() = default;
|
|||||||
|
|
||||||
bool CXmlOpr::open(const std::string& xml_path)
|
bool CXmlOpr::open(const std::string& xml_path)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (doc_.LoadFile(utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#else
|
||||||
if (doc_.LoadFile(xml_path.c_str()) != tinyxml2::XML_SUCCESS) {
|
if (doc_.LoadFile(xml_path.c_str()) != tinyxml2::XML_SUCCESS) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
xml_path_ = xml_path;
|
xml_path_ = xml_path;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user