OneLevelXmlOpr/src/xml_opr.cpp

301 lines
8.0 KiB
C++
Raw Normal View History

#include "xml_opr.h"
#include <filesystem>
2024-05-19 23:33:06 +08:00
#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;
CXmlOpr::~CXmlOpr() = default;
bool CXmlOpr::open(const std::string& xml_path)
{
2024-05-19 23:33:06 +08:00
#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) {
return false;
}
2024-05-19 23:33:06 +08:00
#endif
2024-05-15 13:17:24 +08:00
xml_path_ = xml_path;
return true;
}
bool CXmlOpr::backup_file(const std::string& desti_folder, const std::string& time)
{
if (!fs::exists(xml_path_)) {
return false;
}
if (!fs::exists(desti_folder)) {
fs::create_directories(desti_folder);
}
fs::path file_path = fs::path(xml_path_);
fs::path des = fs::path(desti_folder).append(file_path.stem().string() + "_" + time + ".xml");
return fs::copy_file(xml_path_, des);
}
void CXmlOpr::set_baseinfo(const OprBase& base)
{
opr_base_ = base;
}
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec)
{
vec.clear();
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
while (purpose_node) {
vec.push_back(purpose_node);
purpose_node = purpose_node->NextSiblingElement();
}
return true;
}
bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
{
std::string next_node{};
std::string node_path = opr_base_.node_path;
2024-05-16 12:18:20 +08:00
keys_.clear();
auto keys = splitString(opr_base_.purpose, ",");
for (const auto& item : keys) {
if (item.empty()) {
continue;
}
keys_.push_back(item);
}
auto nodes = splitString(opr_base_.node_path, "/");
for (const auto& item : nodes) {
if (item.empty()) {
continue;
}
if (parent_node_ == nullptr) {
parent_node_ = doc_.FirstChildElement(item.c_str());
} else {
parent_node_ = parent_node_->FirstChildElement(item.c_str());
}
}
get_all_elements(vec);
return true;
}
2024-05-15 13:17:24 +08:00
2024-05-18 01:26:52 +08:00
// 排序后(仅指针排序)的节点进行复制(让实际内容有序),删除原始节点
void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>& out)
{
out.clear();
// 先找到最后一个节点
Element_t* last_node = parent_node_->LastChildElement(opr_base_.the_node.c_str());
Element_t* last_node_bk = last_node;
if (last_node == nullptr) {
return;
}
for (const auto& data : vec) {
Element_t* n = copy_element(data);
out.push_back(n);
insert_brother_node(last_node, n);
last_node = n;
}
// 删除原有的节点
Element_t* fnode = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
Element_t* fnext = fnode->NextSiblingElement();
while (fnode != last_node_bk) {
parent_node_->DeleteChild(fnode);
fnode = fnext;
fnext = fnode->NextSiblingElement();
}
parent_node_->DeleteChild(last_node_bk);
}
void CXmlOpr::insert_brother_node(Element_t* brother, Element_t* newer)
{
if (!brother || !newer) {
return;
}
parent_node_->InsertAfterChild(brother, newer);
}
Element_t* CXmlOpr::copy_element(Element_t* ele)
{
if (!ele) {
return nullptr;
}
Element_t* ret = doc_.NewElement(ele->Name());
const auto* attribute = ele->FirstAttribute();
while (attribute) {
ret->SetAttribute(attribute->Name(), attribute->Value());
attribute = attribute->Next();
}
return ret;
}
bool CXmlOpr::check_valid_xml_data(const std::string& data)
{
tinyxml2::XMLDocument doc;
doc.Parse(data.c_str());
if (doc.Error()) {
return false;
}
return true;
}
bool CXmlOpr::check_same_struct(const std::string& data)
{
auto* own_ele = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
if (own_ele == nullptr) {
return true;
}
tinyxml2::XMLDocument doc;
doc.Parse(data.c_str());
if (doc.Error()) {
return false;
}
const auto* import_ele = doc.FirstChildElement(opr_base_.the_node.c_str());
if (import_ele == nullptr) {
return false;
}
const auto* attribute = own_ele->FirstAttribute();
2024-05-19 16:50:22 +08:00
int own_cnt = 0;
while (attribute) {
++own_cnt;
if (import_ele->FindAttribute(attribute->Name()) == nullptr) {
return false;
}
attribute = attribute->Next();
}
const auto* attr_import = import_ele->FirstAttribute();
2024-05-19 16:50:22 +08:00
int import_cnt = 0;
while (attr_import) {
++import_cnt;
attr_import = attr_import->Next();
}
if (import_cnt != own_cnt) {
return false;
}
return true;
}
2024-05-19 16:50:22 +08:00
// Warning: 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
// 且导入前每条数据请自行使用 check_same_struct 检测。
bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count)
{
2024-05-19 16:50:22 +08:00
success_count = 0;
auto* last_item = parent_node_->LastChildElement(opr_base_.the_node.c_str());
if (last_item == nullptr) {
return false;
}
// 检查重复性
2024-05-19 16:50:22 +08:00
for (const auto& data : vec) {
tinyxml2::XMLDocument doc;
doc.Parse(data.c_str());
auto* item = doc.FirstChildElement(opr_base_.the_node.c_str());
if (item == nullptr) {
continue;
}
const char* key_str = item->Attribute(keys_[0].c_str());
if (check_key_exists(std::string(key_str))) {
continue;
}
2024-05-19 16:50:22 +08:00
++success_count;
auto* nitem = copy_element(item);
insert_brother_node(last_item, nitem);
}
return true;
}
void CXmlOpr::del_element(Element_t* ele)
{
parent_node_->DeleteChild(ele);
}
2024-05-16 12:18:20 +08:00
bool CXmlOpr::check_key_exists(const Property_t& property)
{
if (keys_.size() < 1 || property.size() < 1) {
return false;
}
return check_key_exists(property[0].key);
}
bool CXmlOpr::check_key_exists(const std::string& key)
{
if (keys_.size() < 1 || key.empty()) {
return false;
}
2024-05-16 12:18:20 +08:00
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
while (purpose_node) {
const char* value = purpose_node->Attribute(keys_[0].c_str());
if (key == std::string(value)) {
2024-05-16 12:18:20 +08:00
return true;
}
purpose_node = purpose_node->NextSiblingElement();
}
return false;
}
2024-05-15 13:17:24 +08:00
bool CXmlOpr::save()
{
auto ret = doc_.SaveFile(xml_path_.c_str());
if (ret != tinyxml2::XML_SUCCESS) {
return false;
}
return true;
}
void CXmlOpr::get_attributes(Element_t* ele, Property_t& vec)
{
if (ele == nullptr) {
return;
}
vec.clear();
const auto* attribute = ele->FirstAttribute();
while (attribute) {
vec.emplace_back(attribute->Name(), attribute->Value());
attribute = attribute->Next();
}
}
void CXmlOpr::attributes_to_element(Element_t* ele, const Property_t& vec)
{
if (ele == nullptr) {
return;
}
for (const auto& data : vec) {
ele->SetAttribute(data.key.c_str(), data.value.c_str());
}
}
SKeyValue::SKeyValue(const char* pkey, const char* pvalue)
{
key = std::string(pkey);
value = std::string(pvalue);
}