2024-05-15 10:59:43 +08:00
|
|
|
#include "xml_opr.h"
|
2024-08-27 20:15:41 +08:00
|
|
|
|
2024-05-19 23:09:01 +08:00
|
|
|
#include <filesystem>
|
|
|
|
|
2024-08-27 20:15:41 +08:00
|
|
|
#include "../public_def.h"
|
|
|
|
|
2024-05-19 23:09:01 +08:00
|
|
|
namespace fs = std::filesystem;
|
2024-05-15 10:59:43 +08:00
|
|
|
|
|
|
|
CXmlOpr::CXmlOpr() = default;
|
|
|
|
CXmlOpr::~CXmlOpr() = default;
|
|
|
|
|
2024-05-15 23:06:48 +08:00
|
|
|
bool CXmlOpr::open(const std::string& xml_path)
|
2024-05-15 10:59:43 +08:00
|
|
|
{
|
2024-05-19 23:33:06 +08:00
|
|
|
#ifdef _WIN32
|
2024-05-24 09:18:06 +08:00
|
|
|
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
|
2024-05-19 23:33:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
2024-08-27 20:15:41 +08:00
|
|
|
xml_path_ = CUtil::utf8_to_gbk(xml_path);
|
2024-05-19 23:33:06 +08:00
|
|
|
#else
|
2024-05-15 10:59:43 +08:00
|
|
|
if (doc_.LoadFile(xml_path.c_str()) != tinyxml2::XML_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-15 13:17:24 +08:00
|
|
|
xml_path_ = xml_path;
|
2024-08-27 20:15:41 +08:00
|
|
|
#endif
|
2024-05-15 10:59:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-19 23:09:01 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-08-27 17:30:48 +08:00
|
|
|
void CXmlOpr::set_baseinfo(const OneGroupIni& base)
|
2024-05-15 10:59:43 +08:00
|
|
|
{
|
|
|
|
opr_base_ = base;
|
|
|
|
}
|
|
|
|
|
2024-08-27 17:30:48 +08:00
|
|
|
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec, const std::string& unit)
|
2024-05-19 23:09:01 +08:00
|
|
|
{
|
|
|
|
vec.clear();
|
2024-08-27 17:30:48 +08:00
|
|
|
|
2024-08-27 20:15:41 +08:00
|
|
|
parent_node2_ = parent_node_;
|
2024-08-27 17:30:48 +08:00
|
|
|
|
|
|
|
if (!unit.empty()) {
|
2024-08-27 20:15:41 +08:00
|
|
|
parent_node2_ = parent_node2_->FirstChildElement(unit.c_str());
|
2024-08-27 17:30:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opr_base_.relative_nodes.empty()) {
|
|
|
|
auto v = CUtil::splitString(opr_base_.relative_nodes, ",");
|
|
|
|
// 向下子项跳跃
|
|
|
|
for (const auto& it : v) {
|
2024-08-27 20:15:41 +08:00
|
|
|
parent_node2_ = parent_node2_->FirstChildElement(it.c_str());
|
2024-08-27 17:30:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:15:41 +08:00
|
|
|
auto purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
2024-05-19 23:09:01 +08:00
|
|
|
while (purpose_node) {
|
|
|
|
vec.push_back(purpose_node);
|
|
|
|
purpose_node = purpose_node->NextSiblingElement();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-15 10:59:43 +08:00
|
|
|
bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
|
|
|
|
{
|
|
|
|
std::string next_node{};
|
2024-08-27 17:30:48 +08:00
|
|
|
std::string node_path = opr_base_.item_key;
|
2024-05-16 12:18:20 +08:00
|
|
|
keys_.clear();
|
2024-08-27 17:30:48 +08:00
|
|
|
auto keys = CUtil::splitString(opr_base_.propertis, ",");
|
2024-05-16 12:18:20 +08:00
|
|
|
for (const auto& item : keys) {
|
|
|
|
if (item.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keys_.push_back(item);
|
|
|
|
}
|
2024-05-15 10:59:43 +08:00
|
|
|
|
2024-08-27 17:30:48 +08:00
|
|
|
auto nodes = CUtil::splitString(opr_base_.main_nodes, "/");
|
2024-05-15 10:59:43 +08:00
|
|
|
for (const auto& item : nodes) {
|
|
|
|
if (item.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-15 17:34:07 +08:00
|
|
|
if (parent_node_ == nullptr) {
|
|
|
|
parent_node_ = doc_.FirstChildElement(item.c_str());
|
2024-05-15 23:06:48 +08:00
|
|
|
} else {
|
2024-05-15 17:34:07 +08:00
|
|
|
parent_node_ = parent_node_->FirstChildElement(item.c_str());
|
2024-05-15 10:59:43 +08:00
|
|
|
}
|
|
|
|
}
|
2024-08-27 17:30:48 +08:00
|
|
|
|
2024-08-27 20:15:41 +08:00
|
|
|
if (parent_node_ == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-27 17:30:48 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:15:41 +08:00
|
|
|
bool CXmlOpr::get_all_unit(std::vector<std::string>& units)
|
2024-08-27 17:30:48 +08:00
|
|
|
{
|
|
|
|
units = units_;
|
2024-05-15 10:59:43 +08:00
|
|
|
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();
|
|
|
|
// 先找到最后一个节点
|
2024-08-27 20:15:41 +08:00
|
|
|
Element_t* last_node = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
2024-05-18 01:26:52 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
// 删除原有的节点
|
2024-08-27 20:15:41 +08:00
|
|
|
Element_t* fnode = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
2024-05-18 01:26:52 +08:00
|
|
|
Element_t* fnext = fnode->NextSiblingElement();
|
|
|
|
while (fnode != last_node_bk) {
|
2024-08-27 20:15:41 +08:00
|
|
|
parent_node2_->DeleteChild(fnode);
|
2024-05-18 01:26:52 +08:00
|
|
|
fnode = fnext;
|
|
|
|
fnext = fnode->NextSiblingElement();
|
|
|
|
}
|
2024-08-27 20:15:41 +08:00
|
|
|
parent_node2_->DeleteChild(last_node_bk);
|
2024-05-18 01:26:52 +08:00
|
|
|
}
|
|
|
|
|
2024-05-15 23:06:48 +08:00
|
|
|
void CXmlOpr::insert_brother_node(Element_t* brother, Element_t* newer)
|
2024-05-15 17:34:07 +08:00
|
|
|
{
|
|
|
|
if (!brother || !newer) {
|
|
|
|
return;
|
|
|
|
}
|
2024-08-27 20:15:41 +08:00
|
|
|
parent_node2_->InsertAfterChild(brother, newer);
|
2024-05-15 17:34:07 +08:00
|
|
|
}
|
|
|
|
|
2024-05-15 23:06:48 +08:00
|
|
|
Element_t* CXmlOpr::copy_element(Element_t* ele)
|
2024-05-15 17:34:07 +08:00
|
|
|
{
|
|
|
|
if (!ele) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2024-08-27 17:30:48 +08:00
|
|
|
Element_t* ret = doc_.NewElement(ele->Name());
|
2024-05-15 17:34:07 +08:00
|
|
|
const auto* attribute = ele->FirstAttribute();
|
|
|
|
while (attribute) {
|
|
|
|
ret->SetAttribute(attribute->Name(), attribute->Value());
|
|
|
|
attribute = attribute->Next();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-19 00:48:03 +08:00
|
|
|
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)
|
|
|
|
{
|
2024-08-27 20:15:41 +08:00
|
|
|
auto* own_ele = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
2024-05-19 00:48:03 +08:00
|
|
|
if (own_ele == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(data.c_str());
|
|
|
|
if (doc.Error()) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-08-27 17:30:48 +08:00
|
|
|
const auto* import_ele = doc.FirstChildElement(opr_base_.item_key.c_str());
|
2024-05-19 00:48:03 +08:00
|
|
|
if (import_ele == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto* attribute = own_ele->FirstAttribute();
|
2024-08-27 17:30:48 +08:00
|
|
|
int own_cnt = 0;
|
2024-05-19 00:48:03 +08:00
|
|
|
while (attribute) {
|
|
|
|
++own_cnt;
|
|
|
|
if (import_ele->FindAttribute(attribute->Name()) == nullptr) {
|
2024-05-19 21:02:50 +08:00
|
|
|
return false;
|
2024-05-19 00:48:03 +08:00
|
|
|
}
|
|
|
|
attribute = attribute->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* attr_import = import_ele->FirstAttribute();
|
2024-08-27 17:30:48 +08:00
|
|
|
int import_cnt = 0;
|
2024-05-19 00:48:03 +08:00
|
|
|
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 检测。
|
2024-05-19 21:02:50 +08:00
|
|
|
bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count)
|
2024-05-19 00:48:03 +08:00
|
|
|
{
|
2024-05-19 16:50:22 +08:00
|
|
|
success_count = 0;
|
2024-08-27 20:15:41 +08:00
|
|
|
auto* last_item = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
2024-05-19 16:50:22 +08:00
|
|
|
if (last_item == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-19 23:09:01 +08:00
|
|
|
// 检查重复性
|
2024-05-19 16:50:22 +08:00
|
|
|
for (const auto& data : vec) {
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(data.c_str());
|
2024-08-27 17:30:48 +08:00
|
|
|
auto* item = doc.FirstChildElement(opr_base_.item_key.c_str());
|
2024-05-19 16:50:22 +08:00
|
|
|
if (item == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-19 23:09:01 +08:00
|
|
|
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);
|
2024-08-28 08:42:45 +08:00
|
|
|
last_item = nitem;
|
2024-05-19 16:50:22 +08:00
|
|
|
}
|
|
|
|
return true;
|
2024-05-19 00:48:03 +08:00
|
|
|
}
|
|
|
|
|
2024-05-15 23:06:48 +08:00
|
|
|
void CXmlOpr::del_element(Element_t* ele)
|
|
|
|
{
|
2024-08-27 20:15:41 +08:00
|
|
|
parent_node2_->DeleteChild(ele);
|
2024-05-15 23:06:48 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2024-08-27 20:15:41 +08:00
|
|
|
return check_key_exists(property[0].value);
|
2024-05-19 23:09:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CXmlOpr::check_key_exists(const std::string& key)
|
|
|
|
{
|
|
|
|
if (keys_.size() < 1 || key.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-08-27 20:15:41 +08:00
|
|
|
Element_t* purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
2024-05-16 12:18:20 +08:00
|
|
|
while (purpose_node) {
|
|
|
|
const char* value = purpose_node->Attribute(keys_[0].c_str());
|
2024-05-19 23:09:01 +08:00
|
|
|
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;
|
|
|
|
}
|
2024-05-15 23:06:48 +08:00
|
|
|
|
2024-05-19 21:02:50 +08:00
|
|
|
void CXmlOpr::get_attributes(Element_t* ele, Property_t& vec)
|
2024-05-15 23:06:48 +08:00
|
|
|
{
|
|
|
|
if (ele == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vec.clear();
|
|
|
|
const auto* attribute = ele->FirstAttribute();
|
|
|
|
while (attribute) {
|
|
|
|
vec.emplace_back(attribute->Name(), attribute->Value());
|
|
|
|
attribute = attribute->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 21:02:50 +08:00
|
|
|
void CXmlOpr::attributes_to_element(Element_t* ele, const Property_t& vec)
|
2024-05-15 23:06:48 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|