big-change:添加两种类型结构读写支持

This commit is contained in:
2024-08-27 17:30:48 +08:00
parent ff68e756ff
commit b05783191f
20 changed files with 975 additions and 195 deletions

View File

@@ -1,4 +1,5 @@
#include "xml_opr.h"
#include "../public_def.h"
#include <filesystem>
namespace fs = std::filesystem;
@@ -34,15 +35,30 @@ bool CXmlOpr::backup_file(const std::string& desti_folder, const std::string& ti
return fs::copy_file(xml_path_, des);
}
void CXmlOpr::set_baseinfo(const OprBase& base)
void CXmlOpr::set_baseinfo(const OneGroupIni& base)
{
opr_base_ = base;
}
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec)
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec, const std::string& unit)
{
vec.clear();
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
Element_t* temp = parent_node_;
if (!unit.empty()) {
temp = temp->FirstChildElement(unit.c_str());
}
if (!opr_base_.relative_nodes.empty()) {
auto v = CUtil::splitString(opr_base_.relative_nodes, ",");
// 向下子项跳跃
for (const auto& it : v) {
temp = temp->FirstChildElement(it.c_str());
}
}
auto purpose_node = temp->FirstChildElement(opr_base_.item_key.c_str());
while (purpose_node) {
vec.push_back(purpose_node);
purpose_node = purpose_node->NextSiblingElement();
@@ -53,9 +69,9 @@ bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec)
bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
{
std::string next_node{};
std::string node_path = opr_base_.node_path;
std::string node_path = opr_base_.item_key;
keys_.clear();
auto keys = CUtil::splitString(opr_base_.purpose, ",");
auto keys = CUtil::splitString(opr_base_.propertis, ",");
for (const auto& item : keys) {
if (item.empty()) {
continue;
@@ -63,7 +79,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
keys_.push_back(item);
}
auto nodes = CUtil::splitString(opr_base_.node_path, "/");
auto nodes = CUtil::splitString(opr_base_.main_nodes, "/");
for (const auto& item : nodes) {
if (item.empty()) {
@@ -76,7 +92,27 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
parent_node_ = parent_node_->FirstChildElement(item.c_str());
}
}
get_all_elements(vec);
if (opr_base_.is_same) {
units_.clear();
auto p = parent_node_->FirstChildElement();
while (p) {
units_.push_back(p->Name());
p = p->NextSiblingElement();
}
if (!units_.empty()) {
get_all_elements(vec, units_[0]);
}
} else {
get_all_elements(vec);
}
return true;
}
bool CXmlOpr::get_all_unit(std::vector<std::string> &units)
{
units = units_;
return true;
}
@@ -85,7 +121,7 @@ void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>
{
out.clear();
// 先找到最后一个节点
Element_t* last_node = parent_node_->LastChildElement(opr_base_.the_node.c_str());
Element_t* last_node = parent_node_->LastChildElement(opr_base_.item_key.c_str());
Element_t* last_node_bk = last_node;
if (last_node == nullptr) {
return;
@@ -97,7 +133,7 @@ void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>
last_node = n;
}
// 删除原有的节点
Element_t* fnode = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
Element_t* fnode = parent_node_->FirstChildElement(opr_base_.item_key.c_str());
Element_t* fnext = fnode->NextSiblingElement();
while (fnode != last_node_bk) {
parent_node_->DeleteChild(fnode);
@@ -120,7 +156,7 @@ Element_t* CXmlOpr::copy_element(Element_t* ele)
if (!ele) {
return nullptr;
}
Element_t* ret = doc_.NewElement(ele->Name());
Element_t* ret = doc_.NewElement(ele->Name());
const auto* attribute = ele->FirstAttribute();
while (attribute) {
ret->SetAttribute(attribute->Name(), attribute->Value());
@@ -141,7 +177,7 @@ bool CXmlOpr::check_valid_xml_data(const std::string& data)
bool CXmlOpr::check_same_struct(const std::string& data)
{
auto* own_ele = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
auto* own_ele = parent_node_->FirstChildElement(opr_base_.item_key.c_str());
if (own_ele == nullptr) {
return true;
}
@@ -150,12 +186,12 @@ bool CXmlOpr::check_same_struct(const std::string& data)
if (doc.Error()) {
return false;
}
const auto* import_ele = doc.FirstChildElement(opr_base_.the_node.c_str());
const auto* import_ele = doc.FirstChildElement(opr_base_.item_key.c_str());
if (import_ele == nullptr) {
return false;
}
const auto* attribute = own_ele->FirstAttribute();
int own_cnt = 0;
int own_cnt = 0;
while (attribute) {
++own_cnt;
if (import_ele->FindAttribute(attribute->Name()) == nullptr) {
@@ -165,7 +201,7 @@ bool CXmlOpr::check_same_struct(const std::string& data)
}
const auto* attr_import = import_ele->FirstAttribute();
int import_cnt = 0;
int import_cnt = 0;
while (attr_import) {
++import_cnt;
attr_import = attr_import->Next();
@@ -183,7 +219,7 @@ bool CXmlOpr::check_same_struct(const std::string& data)
bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count)
{
success_count = 0;
auto* last_item = parent_node_->LastChildElement(opr_base_.the_node.c_str());
auto* last_item = parent_node_->LastChildElement(opr_base_.item_key.c_str());
if (last_item == nullptr) {
return false;
}
@@ -191,7 +227,7 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t
for (const auto& data : vec) {
tinyxml2::XMLDocument doc;
doc.Parse(data.c_str());
auto* item = doc.FirstChildElement(opr_base_.the_node.c_str());
auto* item = doc.FirstChildElement(opr_base_.item_key.c_str());
if (item == nullptr) {
continue;
}
@@ -224,7 +260,7 @@ bool CXmlOpr::check_key_exists(const std::string& key)
if (keys_.size() < 1 || key.empty()) {
return false;
}
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.item_key.c_str());
while (purpose_node) {
const char* value = purpose_node->Attribute(keys_[0].c_str());
if (key == std::string(value)) {