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,53 +1,108 @@
#include "config.h"
#include <QDir>
#include <filesystem>
namespace fs = std::filesystem;
bool ConfigIni::set_work_exe(const std::string& dir)
CGroupIni::CGroupIni()
{
work_dir_ = dir;
default_set();
}
auto ini_path = fs::path(work_dir_).parent_path().append("xmlopr.ini");
if (!fs::exists(ini_path)) {
return false;
void CGroupIni::default_set()
{
QDir dir;
std::string path = dir.homePath().toStdString();
fs::path p(path);
p.append(".config");
if (!fs::exists(p)) {
fs::create_directories(p);
}
ini_path_ = ini_path.string();
if (!parse_ini()) {
work_dir_ = p.string();
p.append("OneLevelXmlOpr.ini");
work_file_ = p.string();
}
bool CGroupIni::load_ini()
{
SI_Error rc = ini_.LoadFile(work_file_.c_str());
if (rc != SI_OK) {
rc = ini_.SaveFile(work_file_.c_str());
if (rc < 0) {
return false;
}
return true;
}
return true;
}
void CGroupIni::get_all_node(StrVec_t& vec)
{
vec.clear();
CSimpleIni::TNamesDepend secs;
ini_.GetAllSections(secs);
for (const auto& sec : secs) {
vec.push_back(sec.pItem);
}
}
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(), "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);
ini_.SetLongValue(group.name.c_str(), "MaxBlankAdd", group.max_blank_add);
ini_.SetBoolValue(group.name.c_str(), "IsSameStruct", group.is_same);
if (ini_.SaveFile(work_file_.c_str()) != SI_OK) {
return false;
}
return true;
}
bool ConfigIni::set_xml_path(const std::string& path)
bool CGroupIni::get_item(OneGroupIni& group)
{
if (ini_.IsEmpty()) {
StrVec_t vec;
get_all_node(vec);
bool find = false;
for (const auto& item : vec) {
if (group.name != item) {
continue;
}
find = true;
break;
}
if (!find) {
return false;
}
ini_.SetValue("Basic", "xml_path", path.c_str());
if (ini_.SaveFile(ini_path_.c_str()) != SI_OK) {
group.main_nodes = ini_.GetValue(group.name.c_str(), "MainNodes");
group.relative_nodes = ini_.GetValue(group.name.c_str(), "RelativeNodes");
group.item_key = ini_.GetValue(group.name.c_str(), "ItemKey");
group.propertis = ini_.GetValue(group.name.c_str(), "Properties");
group.max_col_len = ini_.GetLongValue(group.name.c_str(), "MaxColLen");
group.max_blank_add = ini_.GetLongValue(group.name.c_str(), "MaxBlankAdd");
group.is_same = ini_.GetBoolValue(group.name.c_str(), "IsSameStruct");
return true;
}
bool CGroupIni::del_item(const std::string &key)
{
ini_.Delete(key.c_str(), "MainNodes", true);
ini_.Delete(key.c_str(), "RelativeNodes", true);
ini_.Delete(key.c_str(), "ItemKey", true);
ini_.Delete(key.c_str(), "Properties", true);
ini_.Delete(key.c_str(), "MaxColLen", true);
ini_.Delete(key.c_str(), "MaxBlankAdd", true);
if (ini_.SaveFile(work_file_.c_str()) != SI_OK) {
return false;
}
return true;
}
OprBase ConfigIni::get_config()
{
return opr_base_;
}
bool ConfigIni::parse_ini()
{
if (ini_.LoadFile(ini_path_.c_str()) != SI_OK) {
return false;
}
ini_.SetUnicode();
opr_base_.node_path = ini_.GetValue("Basic", "oper_node");
opr_base_.purpose = ini_.GetValue("Basic", "purpose");
opr_base_.the_node = ini_.GetValue("Basic", "the_node");
opr_base_.xml_path = ini_.GetValue("Basic", "xml_path");
opr_base_.allow_max_width = ini_.GetLongValue("Basic", "max_col_width");
opr_base_.blank_width = ini_.GetLongValue("Basic", "blank_width");
return true;
}