ver:一个可用版本,基本包含导入导出数据,备份,替换,搜索等功能。
This commit is contained in:
@@ -39,6 +39,7 @@ void CDataEdit::set_xml_opr(CXmlOpr* xml_opr)
|
||||
|
||||
void CDataEdit::import_data()
|
||||
{
|
||||
is_import_sucess_ = false;
|
||||
QString data = ui->plainTextEdit->toPlainText();
|
||||
if (data.trimmed().isEmpty()) {
|
||||
CUtil::msg(this, u8"内容为空");
|
||||
@@ -50,6 +51,10 @@ void CDataEdit::import_data()
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CUtil::affirm(this, u8"提示", u8"如果出现重复的 key 将跳过,继续吗?")) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::string> valid_data{};
|
||||
QStringList list = data.trimmed().split("\n");
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
@@ -72,6 +77,7 @@ void CDataEdit::import_data()
|
||||
QString info = QString(u8"需要导入 %1 条数据,成功导入 %2 条。").arg(task_count).arg(success_count);
|
||||
CUtil::msg(this, info);
|
||||
close();
|
||||
is_import_sucess_ = true;
|
||||
}
|
||||
|
||||
void CDataEdit::showEvent(QShowEvent* event)
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "xml_opr.h"
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
CXmlOpr::CXmlOpr() = default;
|
||||
CXmlOpr::~CXmlOpr() = default;
|
||||
@@ -12,11 +15,35 @@ bool CXmlOpr::open(const std::string& 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{};
|
||||
@@ -43,12 +70,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
|
||||
parent_node_ = parent_node_->FirstChildElement(item.c_str());
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
get_all_elements(vec);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -159,6 +181,7 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t
|
||||
if (last_item == nullptr) {
|
||||
return false;
|
||||
}
|
||||
// 检查重复性
|
||||
for (const auto& data : vec) {
|
||||
tinyxml2::XMLDocument doc;
|
||||
doc.Parse(data.c_str());
|
||||
@@ -166,6 +189,10 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t
|
||||
if (item == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const char* key_str = item->Attribute(keys_[0].c_str());
|
||||
if (check_key_exists(std::string(key_str))) {
|
||||
continue;
|
||||
}
|
||||
++success_count;
|
||||
auto* nitem = copy_element(item);
|
||||
insert_brother_node(last_item, nitem);
|
||||
@@ -183,10 +210,18 @@ 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;
|
||||
}
|
||||
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 (property[0].value == std::string(value)) {
|
||||
if (key == std::string(value)) {
|
||||
return true;
|
||||
}
|
||||
purpose_node = purpose_node->NextSiblingElement();
|
||||
|
||||
@@ -22,18 +22,21 @@ public:
|
||||
|
||||
public:
|
||||
bool open(const std::string& xml_path);
|
||||
bool backup_file(const std::string& desti_folder, const std::string& time);
|
||||
void set_baseinfo(const OprBase& base);
|
||||
bool parse_xml(std::vector<Element_t*>& vec);
|
||||
bool get_all_elements(std::vector<Element_t*>& vec);
|
||||
void copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>& out);
|
||||
void insert_brother_node(Element_t* brother, Element_t* newer);
|
||||
void del_element(Element_t* ele);
|
||||
bool check_key_exists(const Property_t& property);
|
||||
bool check_key_exists(const std::string& key);
|
||||
bool save();
|
||||
|
||||
public:
|
||||
Element_t* copy_element(Element_t* ele);
|
||||
// 检查 xml 格式合法性
|
||||
bool check_valid_xml_data(const std::string& data);
|
||||
bool check_valid_xml_data(const std::string& data);
|
||||
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
|
||||
bool check_same_struct(const std::string& data);
|
||||
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
|
||||
|
||||
Reference in New Issue
Block a user