#ifndef LIBCONFIG_H #define LIBCONFIG_H #include #include #include #include namespace Config { /** * @brief 配置文件解析过程异常 */ class ParseException: public std::exception { public: virtual QString reason() const = 0; virtual QString title() const = 0; }; template class SimpleException : public ParseException { public: SimpleException(const QString &title, const QString &reason) : ts(title), rs(reason) { this->buffer = QString("%1:%2").arg(title, reason).toLocal8Bit(); } T datas() const { return datas_store; } void setDatas(T val) { this->datas_store = val; } // exception interface public: virtual const char *what() const noexcept override { return this->buffer; } // ParseException interface public: virtual QString reason() const override { return rs; } virtual QString title() const override { return ts; } private: T datas_store; QString ts, rs; QByteArray buffer; }; /** * @brief 配置读写交互端口 */ class Configration : public QObject { public: explicit Configration(QObject *parent); virtual ~Configration() = default; /** * @brief 载入配置文件,如果文件不存在,则生成新文件 * @param path 指定配置文件路径 * @throw ParseException* 配置文件解析异常 */ virtual void loadFile(const QString &path) = 0; virtual QDir currentDir() const = 0; /** * @brief 保存配置内容会原始配置文件 * @throws ParseException* 保存过程中异常 */ virtual void save() const = 0; virtual void deleteX(const QList &path) = 0; virtual void setConfig(const QList &path, const QString &value) = 0; virtual QString getConfig(const QList &path) const = 0; virtual void setList(const QList &path, const QList &list) = 0; virtual QList getList(const QList &path) const = 0; virtual void setMap(const QList &path, const QHash &map) = 0; virtual QHash getMap(const QList &path) const = 0; }; class ConfigHelper { public: static QString getConfigAsDefault(Configration *ins, const QList &path, const QString & default_v) { auto xval = ins->getConfig(path); if(xval.trimmed() == ""){ ins->setConfig(path, default_v); ins->save(); xval = default_v; } return xval; } static QString getConfigAsDefaultSequence(QList seqs, const QList &path, const QString & default_v) { QString xval; for(auto &ins : seqs){ if(!ins) continue; xval = ins->getConfig(path); if(xval.trimmed() != "") break; } if(xval.trimmed() == ""){ seqs.last()->setConfig(path, default_v); seqs.last()->save(); xval = default_v; } return xval; } static QList getListAsDefault(Configration *ins, const QList &path, const QList & default_v) { auto xval = ins->getList(path); if(xval.size() == 0){ ins->setList(path, default_v); ins->save(); xval = default_v; } return xval; } static QList getListAsDefaultSequence(QList seqs, const QList &path, const QList & default_v) { QList xval; for(auto &ins : seqs){ if(!ins) continue; xval = ins->getList(path); if(!xval.size()) break; } if(!xval.size()){ seqs.last()->setList(path, default_v); seqs.last()->save(); xval = default_v; } return xval; } static QHash getHashAsDefault(Configration *ins, const QList &path, const QHash & default_v) { auto xval = ins->getMap(path); if(xval.size()==0){ ins->setMap(path, default_v); ins->save(); xval = default_v; } return xval; } static QHash getHashAsDefaultSequence(QList seqs, const QList &path, const QHash & default_v) { QHash xval; for(auto &ins : seqs){ if(!ins) continue; xval = ins->getMap(path); if(!xval.size()) break; } if(!xval.size()){ seqs.last()->setMap(path, default_v); seqs.last()->save(); xval = default_v; } return xval; } }; inline Configration::Configration(QObject *parent) : QObject(parent){} } #endif // LIBCONFIG_H