change:格式化xml单元设定一个窗口,可以选择性格式化哪些具体内容。

This commit is contained in:
2024-09-15 15:08:32 +08:00
parent 0f65ef220a
commit 693a371e0d
8 changed files with 376 additions and 17 deletions

View File

@@ -262,14 +262,33 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec,
}
return true;
}
std::string CXmlOpr::handle_space(const std::string& content)
std::string CXmlOpr::handle_space(const std::string& content,
const std::vector<std::string>& keychars)
{
std::string result;
size_t pos = 0;
size_t len = content.length();
std::ostringstream oss;
for (auto i = size_t(0); i < keychars.size(); ++i) {
if (i > 0) {
oss << "|";
}
std::string op(keychars[i]);
for (const char& c : op) {
if (c == '|' || c == '\\' || c == '(' || c == ')' || c == '[' ||
c == ']' || c == '{' || c == '}' || c == '^' || c == '$' ||
c == '.' || c == '*' || c == '+' || c == '?' || c == '|' ||
c == '\\') {
oss << '\\'; // 添加转义字符
}
oss << c;
}
}
// Define a regular expression for the operators
std::regex operators_regex(R"(&&|!=|==|<|>|\|\||\+|-|\*|/)");
// std::regex operators_regex(R"(&&|!=|==|<|>|\|\||\+|-|\*|/)");
std::regex operators_regex(oss.str());
while (pos < len) {
size_t start_quote = content.find('"', pos);
@@ -316,7 +335,8 @@ std::string CXmlOpr::handle_space(const std::string& content)
return result;
}
bool CXmlOpr::handle_transfer(const std::string& path)
bool CXmlOpr::handle_transfer(const std::string& path,
const std::vector<std::string>& keychars)
{
std::ifstream file(path);
if (!file.is_open()) {
@@ -361,13 +381,18 @@ bool CXmlOpr::handle_transfer(const std::string& path)
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;
if (keychars.size() < 1) {
ofile << result;
} else {
// 处理空格格式化
std::string sec_handle = handle_space(result, keychars);
ofile << sec_handle;
}
return true;
}
@@ -410,7 +435,8 @@ bool CXmlOpr::save()
return true;
}
bool CXmlOpr::handle_save(const std::string& path)
bool CXmlOpr::handle_save(const std::string& path,
const std::vector<std::string>& keychars)
{
if (!open(path)) {
return false;
@@ -419,7 +445,7 @@ bool CXmlOpr::handle_save(const std::string& path)
if (ret != tinyxml2::XML_SUCCESS) {
return false;
}
if (!handle_transfer(xml_path_)) {
if (!handle_transfer(xml_path_, keychars)) {
return false;
}
return true;