210 lines
5.0 KiB
C++
210 lines
5.0 KiB
C++
|
#include "xmlconfig.h"
|
|||
|
|
|||
|
#include <QFile>
|
|||
|
#include <QDebug>
|
|||
|
|
|||
|
using namespace Config;
|
|||
|
|
|||
|
XMLConfig::XMLConfig(QObject *parent)
|
|||
|
: Configration(parent)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
int XMLConfig::loadFile(const QString &path)
|
|||
|
{
|
|||
|
file_path = path;
|
|||
|
QFile config(path);
|
|||
|
if(!config.exists()){
|
|||
|
config.open(QIODevice::WriteOnly);
|
|||
|
|
|||
|
QTextStream tout(&config);
|
|||
|
tout << "<?xml version='1.0'?>" << endl;
|
|||
|
tout << "<config />" << endl;
|
|||
|
|
|||
|
tout.flush();
|
|||
|
config.flush();
|
|||
|
config.close();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
if(!config.open(QIODevice::Text|QIODevice::ReadOnly)){
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
QString err_str; int err_row, err_col;
|
|||
|
if(doc_ins.setContent(&config, false, &err_str, &err_row, &err_col)){
|
|||
|
return -2;
|
|||
|
}
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
QDir XMLConfig::currentDir() const
|
|||
|
{
|
|||
|
return QFileInfo(file_path).dir();
|
|||
|
}
|
|||
|
|
|||
|
void XMLConfig::save() const
|
|||
|
{
|
|||
|
QFile fout(this->file_path);
|
|||
|
if(!fout.open(QIODevice::WriteOnly | QIODevice::Text)){
|
|||
|
qDebug() << "配置文件保存错误" << QString("指定配置文件%1保存过程中,无法打开").arg(file_path);
|
|||
|
return;
|
|||
|
}
|
|||
|
QTextStream tout(&fout);
|
|||
|
tout << this->doc_ins.toString(4);
|
|||
|
tout.flush();
|
|||
|
fout.flush();
|
|||
|
fout.close();
|
|||
|
}
|
|||
|
|
|||
|
void XMLConfig::deleteX(const QList<QString> &path)
|
|||
|
{
|
|||
|
auto elm = local_exists_elm(doc_ins.documentElement(), path);
|
|||
|
if(elm.isNull())
|
|||
|
return;
|
|||
|
elm.parentNode().removeChild(elm);
|
|||
|
}
|
|||
|
|
|||
|
void XMLConfig::setConfig(const QList<QString> &path, const QString &value)
|
|||
|
{
|
|||
|
auto doc = doc_ins.documentElement();
|
|||
|
auto elm = rebuild_exists_elms(doc, path);
|
|||
|
elm.setAttribute("value", value);
|
|||
|
}
|
|||
|
|
|||
|
QString XMLConfig::getConfig(const QList<QString> &path) const
|
|||
|
{
|
|||
|
auto elm = local_exists_elm(doc_ins.documentElement(), path);
|
|||
|
if(elm.isNull())
|
|||
|
return "";
|
|||
|
return elm.attribute("value");
|
|||
|
}
|
|||
|
|
|||
|
void XMLConfig::setList(const QList<QString> &path, const QList<QString> &list)
|
|||
|
{
|
|||
|
auto root = doc_ins.documentElement();
|
|||
|
auto telm = rebuild_exists_elms(root, path);
|
|||
|
auto childs = telm.elementsByTagName("list");
|
|||
|
for(auto idx=0; idx<childs.count(); ++idx)
|
|||
|
telm.removeChild(childs.at(idx));
|
|||
|
|
|||
|
for(auto it : list){
|
|||
|
auto nelm = doc_ins.createElement("list");
|
|||
|
nelm.setAttribute("value", it);
|
|||
|
telm.appendChild(nelm);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
QList<QString> XMLConfig::getList(const QList<QString> &path) const
|
|||
|
{
|
|||
|
auto elm = local_exists_elm(doc_ins.documentElement(), path);
|
|||
|
|
|||
|
QList<QString> rets;
|
|||
|
auto childs = elm.elementsByTagName("list");
|
|||
|
for(auto idx=0; idx<childs.count(); ++idx){
|
|||
|
auto listitem = childs.at(idx).toElement();
|
|||
|
rets << listitem.attribute("value");
|
|||
|
}
|
|||
|
|
|||
|
return rets;
|
|||
|
}
|
|||
|
|
|||
|
void XMLConfig::setMap(const QList<QString> &path, const QHash<QString, QString> &map)
|
|||
|
{
|
|||
|
auto root = doc_ins.documentElement();
|
|||
|
auto telm = rebuild_exists_elms(root, path);
|
|||
|
auto childs = telm.elementsByTagName("map");
|
|||
|
for(auto idx=0; idx<childs.count(); ++idx)
|
|||
|
telm.removeChild(childs.at(idx));
|
|||
|
|
|||
|
for(auto &key : map.keys()){
|
|||
|
auto nelm = doc_ins.createElement("map");
|
|||
|
nelm.setAttribute("key", key);
|
|||
|
nelm.setAttribute("value", map[key]);
|
|||
|
telm.appendChild(nelm);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
QHash<QString, QString> XMLConfig::getMap(const QList<QString> &path) const
|
|||
|
{
|
|||
|
auto elm = local_exists_elm(doc_ins.documentElement(), path);
|
|||
|
|
|||
|
QHash<QString, QString> rets;
|
|||
|
auto childs = elm.elementsByTagName("map");
|
|||
|
for(auto idx=0; idx<childs.count(); ++idx){
|
|||
|
auto elmm = childs.at(idx).toElement();
|
|||
|
rets[elmm.attribute("key")] = elmm.attribute("value");
|
|||
|
}
|
|||
|
|
|||
|
return rets;
|
|||
|
}
|
|||
|
|
|||
|
QDomElement XMLConfig::local_exists_elm(const QDomElement &elm_base, const QList<QString> &path, int level) const
|
|||
|
{
|
|||
|
auto child = elm_base.elementsByTagName("path");
|
|||
|
for(auto idx=0; idx<child.count(); ++idx){
|
|||
|
auto ins = child.at(idx);
|
|||
|
if(ins.isElement()){
|
|||
|
|
|||
|
// 完成过滤,进行匹配
|
|||
|
auto eins = ins.toElement();
|
|||
|
if(eins.attribute("key") == path[level]){
|
|||
|
if(level == path.size()-1)
|
|||
|
return eins;
|
|||
|
return local_exists_elm(eins, path, level+1);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return QDomElement();
|
|||
|
}
|
|||
|
|
|||
|
QDomElement XMLConfig::rebuild_exists_elms(QDomElement &elm_base, const QList<QString> &path, int level)
|
|||
|
{
|
|||
|
auto child = elm_base.elementsByTagName("path");
|
|||
|
for(auto idx=0; idx<child.count(); ++idx){
|
|||
|
auto ins = child.at(idx);
|
|||
|
if(ins.isElement()){
|
|||
|
|
|||
|
// 完成过滤,进行匹配
|
|||
|
auto eins = ins.toElement();
|
|||
|
if(eins.attribute("key") == path[level]){
|
|||
|
if(level == path.size() - 1)
|
|||
|
return eins;
|
|||
|
return rebuild_exists_elms(eins, path, level+1);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
auto nelm = elm_base.ownerDocument().createElement("path");
|
|||
|
nelm.setAttribute("key", path[level]);
|
|||
|
elm_base.appendChild(nelm);
|
|||
|
|
|||
|
if(level < path.count() -1)
|
|||
|
return rebuild_exists_elms(nelm, path, level+1);
|
|||
|
return nelm;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|