new-feature:1.添加带表达式内容的xml格式化。2.支持带特殊字符的读写,如& < > "。
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
#include "ui_attribute_edit.h"
|
||||
|
||||
CAttributeEdit::CAttributeEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CAttributeEdit)
|
||||
CAttributeEdit::CAttributeEdit(QWidget* parent)
|
||||
: QDialog(parent), ui(new Ui::CAttributeEdit)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
@@ -23,7 +24,8 @@ void CAttributeEdit::handle_ok()
|
||||
for (int i = 0; i < row; ++i) {
|
||||
QString key = table_->item(i, 0)->text();
|
||||
QString value = table_->item(i, 1)->text();
|
||||
property_.emplace_back(key.toStdString().c_str(), value.toStdString().c_str());
|
||||
property_.emplace_back(key.toStdString().c_str(),
|
||||
value.toStdString().c_str());
|
||||
}
|
||||
is_ok_ = true;
|
||||
close();
|
||||
@@ -87,7 +89,8 @@ void CAttributeEdit::init_table()
|
||||
table_->setColumnCount(list.size());
|
||||
table_->setHorizontalHeaderLabels(list);
|
||||
table_->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
table_->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
|
||||
table_->setSelectionMode(
|
||||
QAbstractItemView::SelectionMode::SingleSelection);
|
||||
table_->setColumnWidth(0, 100);
|
||||
table_->setColumnWidth(1, 550);
|
||||
QHBoxLayout* ly = new QHBoxLayout();
|
||||
|
||||
@@ -52,7 +52,8 @@ void CGroupIni::get_all_node(StrVec_t& vec)
|
||||
bool CGroupIni::add_item(const OneGroupIni& group)
|
||||
{
|
||||
ini_.SetValue(group.name.c_str(), "MainNodes", group.main_nodes.c_str());
|
||||
ini_.SetValue(group.name.c_str(), "RelativeNodes", group.relative_nodes.c_str());
|
||||
ini_.SetValue(group.name.c_str(), "RelativeNodes",
|
||||
group.relative_nodes.c_str());
|
||||
ini_.SetValue(group.name.c_str(), "ItemKey", group.item_key.c_str());
|
||||
ini_.SetValue(group.name.c_str(), "Properties", group.propertis.c_str());
|
||||
ini_.SetLongValue(group.name.c_str(), "MaxColLen", group.max_col_len);
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#ifndef CONIFG_HEADER
|
||||
#define CONIFG_HEADER
|
||||
|
||||
#include <string>
|
||||
#include <SimpleIni.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
/*
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ CDataEdit::CDataEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CDataEdit)
|
||||
|
||||
connect(ui->btnExit, &QPushButton::clicked, this, [&]() { close(); });
|
||||
connect(ui->btnReplace, &QPushButton::clicked, this, [&]() { replace(); });
|
||||
connect(ui->btnWithDraw, &QPushButton::clicked, this, [&]() { with_draw(); });
|
||||
connect(ui->btnWithDraw, &QPushButton::clicked, this,
|
||||
[&]() { with_draw(); });
|
||||
connect(ui->btnAdd, &QPushButton::clicked, this, [&]() { import_data(); });
|
||||
connect(ui->btnCopy, &QPushButton::clicked, this, [&]() {
|
||||
QClipboard* clip = QApplication::clipboard();
|
||||
@@ -56,7 +57,8 @@ void CDataEdit::import_data()
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CUtil::affirm(this, u8"提示", u8"如果出现重复的 key 将跳过,继续吗?")) {
|
||||
if (!CUtil::affirm(this, u8"提示",
|
||||
u8"如果出现重复的 key 将跳过,继续吗?")) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -79,7 +81,9 @@ void CDataEdit::import_data()
|
||||
CUtil::msg(this, u8"导入失败。");
|
||||
return;
|
||||
}
|
||||
QString info = QString(u8"需要导入 %1 条数据,成功导入 %2 条。").arg(task_count).arg(success_count);
|
||||
QString info = QString(u8"需要导入 %1 条数据,成功导入 %2 条。")
|
||||
.arg(task_count)
|
||||
.arg(success_count);
|
||||
CUtil::msg(this, info);
|
||||
close();
|
||||
is_import_sucess_ = true;
|
||||
|
||||
@@ -39,8 +39,8 @@ public:
|
||||
|
||||
private:
|
||||
Ui::CDataEdit* ui;
|
||||
QString data_{};
|
||||
CXmlOpr* xml_opr_{};
|
||||
QString data_{};
|
||||
CXmlOpr* xml_opr_{};
|
||||
};
|
||||
|
||||
#endif // DATA_EDIT_H
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "history.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
#include "src/ui_uhistory.h"
|
||||
#include "ui_uhistory.h"
|
||||
|
||||
CUIHistory::CUIHistory(QWidget* parent, CHistory* his) : QDialog(parent), ui(new Ui::CUIHistory)
|
||||
CUIHistory::CUIHistory(QWidget* parent, CHistory* his)
|
||||
: QDialog(parent), ui(new Ui::CUIHistory)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
his_ = his;
|
||||
@@ -38,7 +39,7 @@ void CUIHistory::select_ok()
|
||||
{
|
||||
auto re = ui->listWidget->selectedItems();
|
||||
if (re.empty()) {
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
cur_ = re[0]->text();
|
||||
close();
|
||||
@@ -48,7 +49,7 @@ void CUIHistory::del_item()
|
||||
{
|
||||
auto re = ui->listWidget->selectedItems();
|
||||
if (re.empty()) {
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
for (auto& item : re) {
|
||||
delete ui->listWidget->takeItem(ui->listWidget->row(item));
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define UHISTORY_H
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
#include "history.h"
|
||||
|
||||
namespace Ui {
|
||||
@@ -13,7 +14,7 @@ class CUIHistory : public QDialog
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CUIHistory(QWidget *parent, CHistory* his);
|
||||
explicit CUIHistory(QWidget* parent, CHistory* his);
|
||||
~CUIHistory();
|
||||
|
||||
private:
|
||||
@@ -25,7 +26,7 @@ public:
|
||||
QString cur_{};
|
||||
|
||||
private:
|
||||
Ui::CUIHistory *ui;
|
||||
Ui::CUIHistory* ui;
|
||||
CHistory* his_{};
|
||||
};
|
||||
|
||||
|
||||
162
src/xml_opr.cpp
162
src/xml_opr.cpp
@@ -1,6 +1,9 @@
|
||||
#include "xml_opr.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
|
||||
#include "../public_def.h"
|
||||
|
||||
@@ -12,7 +15,8 @@ CXmlOpr::~CXmlOpr() = default;
|
||||
bool CXmlOpr::open(const std::string& xml_path)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
|
||||
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) !=
|
||||
tinyxml2::XML_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
xml_path_ = CUtil::utf8_to_gbk(xml_path);
|
||||
@@ -25,7 +29,8 @@ bool CXmlOpr::open(const std::string& xml_path)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CXmlOpr::backup_file(const std::string& desti_folder, const std::string& time)
|
||||
bool CXmlOpr::backup_file(const std::string& desti_folder,
|
||||
const std::string& time)
|
||||
{
|
||||
if (!fs::exists(xml_path_)) {
|
||||
return false;
|
||||
@@ -34,7 +39,8 @@ bool CXmlOpr::backup_file(const std::string& desti_folder, const std::string& ti
|
||||
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");
|
||||
fs::path des = fs::path(desti_folder)
|
||||
.append(file_path.stem().string() + "_" + time + ".xml");
|
||||
return fs::copy_file(xml_path_, des);
|
||||
}
|
||||
|
||||
@@ -43,7 +49,8 @@ void CXmlOpr::set_baseinfo(const OneGroupIni& base)
|
||||
opr_base_ = base;
|
||||
}
|
||||
|
||||
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec, const std::string& unit)
|
||||
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec,
|
||||
const std::string& unit)
|
||||
{
|
||||
vec.clear();
|
||||
|
||||
@@ -61,7 +68,8 @@ bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec, const std::string&
|
||||
}
|
||||
}
|
||||
|
||||
auto purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
auto purpose_node =
|
||||
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
while (purpose_node) {
|
||||
vec.push_back(purpose_node);
|
||||
purpose_node = purpose_node->NextSiblingElement();
|
||||
@@ -123,11 +131,13 @@ bool CXmlOpr::get_all_unit(std::vector<std::string>& units)
|
||||
}
|
||||
|
||||
// 排序后(仅指针排序)的节点进行复制(让实际内容有序),删除原始节点
|
||||
void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>& out)
|
||||
void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec,
|
||||
std::vector<Element_t*>& out)
|
||||
{
|
||||
out.clear();
|
||||
// 先找到最后一个节点
|
||||
Element_t* last_node = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* last_node =
|
||||
parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* last_node_bk = last_node;
|
||||
if (last_node == nullptr) {
|
||||
return;
|
||||
@@ -139,7 +149,8 @@ void CXmlOpr::copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>
|
||||
last_node = n;
|
||||
}
|
||||
// 删除原有的节点
|
||||
Element_t* fnode = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* fnode =
|
||||
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* fnext = fnode->NextSiblingElement();
|
||||
while (fnode != last_node_bk) {
|
||||
parent_node2_->DeleteChild(fnode);
|
||||
@@ -183,7 +194,8 @@ bool CXmlOpr::check_valid_xml_data(const std::string& data)
|
||||
|
||||
bool CXmlOpr::check_same_struct(const std::string& data)
|
||||
{
|
||||
auto* own_ele = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
auto* own_ele =
|
||||
parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
if (own_ele == nullptr) {
|
||||
return true;
|
||||
}
|
||||
@@ -222,10 +234,12 @@ bool CXmlOpr::check_same_struct(const std::string& data)
|
||||
|
||||
// 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)
|
||||
bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec,
|
||||
std::size_t& success_count)
|
||||
{
|
||||
success_count = 0;
|
||||
auto* last_item = parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
auto* last_item =
|
||||
parent_node2_->LastChildElement(opr_base_.item_key.c_str());
|
||||
if (last_item == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@@ -248,6 +262,114 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::string CXmlOpr::handle_space(const std::string& content)
|
||||
{
|
||||
std::string result;
|
||||
size_t pos = 0;
|
||||
size_t len = content.length();
|
||||
|
||||
// Define a regular expression for the operators
|
||||
std::regex operators_regex(R"(&&|!=|==|<|>|\|\||\+|-|\*|/)");
|
||||
|
||||
while (pos < len) {
|
||||
size_t start_quote = content.find('"', pos);
|
||||
if (start_quote == std::string::npos) {
|
||||
result.append(content.substr(pos));
|
||||
break;
|
||||
}
|
||||
|
||||
// Append everything before the first quote
|
||||
result.append(content.substr(pos, start_quote - pos));
|
||||
|
||||
// Find the ending quote
|
||||
size_t end_quote = content.find('"', start_quote + 1);
|
||||
if (end_quote == std::string::npos) {
|
||||
result.append(content.substr(start_quote));
|
||||
break;
|
||||
}
|
||||
|
||||
// Extract the quoted content
|
||||
std::string quoted_content =
|
||||
content.substr(start_quote + 1, end_quote - start_quote - 1);
|
||||
std::string processed_content =
|
||||
std::regex_replace(quoted_content, operators_regex, " $& ");
|
||||
|
||||
// Process quoted content to replace multiple spaces with a single space
|
||||
std::istringstream iss(processed_content);
|
||||
std::ostringstream oss;
|
||||
std::string word;
|
||||
bool first = true;
|
||||
while (iss >> word) {
|
||||
if (!first) {
|
||||
oss << ' ';
|
||||
}
|
||||
oss << word;
|
||||
first = false;
|
||||
}
|
||||
|
||||
// Append processed content with quotes
|
||||
result.append("\"" + oss.str() + "\"");
|
||||
|
||||
// Move past the ending quote
|
||||
pos = end_quote + 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
bool CXmlOpr::handle_transfer(const std::string& path)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::istreambuf_iterator<char> ifter(file);
|
||||
std::istreambuf_iterator<char> iter;
|
||||
std::string content(ifter, iter);
|
||||
file.close();
|
||||
|
||||
size_t pos = 0;
|
||||
std::string result = content;
|
||||
|
||||
// Replace XML entities with their corresponding characters
|
||||
while ((pos = result.find("&", pos)) != std::string::npos) {
|
||||
result.replace(pos, 5, "&");
|
||||
pos += 1; // Move past the replaced character
|
||||
}
|
||||
|
||||
pos = 0;
|
||||
while ((pos = result.find("<", pos)) != std::string::npos) {
|
||||
result.replace(pos, 4, "<");
|
||||
pos += 1; // Move past the replaced character
|
||||
}
|
||||
|
||||
pos = 0;
|
||||
while ((pos = result.find(">", pos)) != std::string::npos) {
|
||||
result.replace(pos, 4, ">");
|
||||
pos += 1; // Move past the replaced character
|
||||
}
|
||||
|
||||
pos = 0;
|
||||
while ((pos = result.find(""", pos)) != std::string::npos) {
|
||||
result.replace(pos, 6, "\"");
|
||||
pos += 1; // Move past the replaced character
|
||||
}
|
||||
|
||||
pos = 0;
|
||||
while ((pos = result.find("'", pos)) != std::string::npos) {
|
||||
result.replace(pos, 6, "'");
|
||||
pos += 1; // Move past the replaced character
|
||||
}
|
||||
|
||||
// 处理空格格式化
|
||||
std::string sec_handle = handle_space(result);
|
||||
std::ofstream ofile(path);
|
||||
if (!ofile.is_open()) {
|
||||
return false;
|
||||
}
|
||||
ofile << sec_handle;
|
||||
return true;
|
||||
}
|
||||
|
||||
void CXmlOpr::del_element(Element_t* ele)
|
||||
{
|
||||
@@ -267,7 +389,8 @@ bool CXmlOpr::check_key_exists(const std::string& key)
|
||||
if (keys_.size() < 1 || key.empty()) {
|
||||
return false;
|
||||
}
|
||||
Element_t* purpose_node = parent_node2_->FirstChildElement(opr_base_.item_key.c_str());
|
||||
Element_t* purpose_node =
|
||||
parent_node2_->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)) {
|
||||
@@ -287,6 +410,21 @@ bool CXmlOpr::save()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CXmlOpr::handle_save(const std::string& path)
|
||||
{
|
||||
if (!open(path)) {
|
||||
return false;
|
||||
}
|
||||
auto ret = doc_.SaveFile(xml_path_.c_str());
|
||||
if (ret != tinyxml2::XML_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
if (!handle_transfer(xml_path_)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CXmlOpr::get_attributes(Element_t* ele, Property_t& vec)
|
||||
{
|
||||
if (ele == nullptr) {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#ifndef XML_OPERT_HEADER
|
||||
#define XML_OPERT_HEADER
|
||||
|
||||
#include "config.h"
|
||||
#include <string>
|
||||
#include <tinyxml2.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
struct SKeyValue {
|
||||
SKeyValue(const char* pkey, const char* pvalue);
|
||||
std::string key;
|
||||
@@ -26,13 +28,17 @@ public:
|
||||
void set_baseinfo(const OneGroupIni& base);
|
||||
bool parse_xml(std::vector<Element_t*>& vec);
|
||||
bool get_all_unit(std::vector<std::string>& units);
|
||||
bool get_all_elements(std::vector<Element_t*>& vec, const std::string& unit = "");
|
||||
void copy_and_del(std::vector<Element_t*>& vec, std::vector<Element_t*>& out);
|
||||
bool get_all_elements(std::vector<Element_t*>& vec,
|
||||
const std::string& unit = "");
|
||||
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();
|
||||
bool handle_save(const std::string& path);
|
||||
std::string handle_space(const std::string& content);
|
||||
|
||||
public:
|
||||
Element_t* copy_element(Element_t* ele);
|
||||
@@ -41,7 +47,10 @@ public:
|
||||
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
|
||||
bool check_same_struct(const std::string& data);
|
||||
// 不检查 xml 格式合法性,请自行调用 check_valid_xml_data
|
||||
bool import_newer_data(const std::vector<std::string>& vec, std::size_t& success_count);
|
||||
bool import_newer_data(const std::vector<std::string>& vec,
|
||||
std::size_t& success_count);
|
||||
// 处理转义
|
||||
bool handle_transfer(const std::string& path);
|
||||
|
||||
public:
|
||||
void get_attributes(Element_t* ele, Property_t& vec);
|
||||
|
||||
Reference in New Issue
Block a user