• tintxml 操作封装,包括CDATA使用


    #ifndef SDK_CONFIG_H_
    #define SDK_CONFIG_H_
    
    #include "xml/tinyxml.h"
    
    // ----------------------------------------------------------------------------
    // 配置文件保存在xml文件中,每个子功能对应一个配置文件
    // ----------------------------------------------------------------------------
    class Config
    {
    public:
        SDKEXPORT Config(const char* fileName = "config");
        SDKEXPORT ~Config();
    
    public:
        // 获取整型值,当查询值不存在时,保存默认值
        SDKEXPORT int getInt(const char* element, const int def = 0);
    
        // 设置整型值
        SDKEXPORT bool setInt(const char* element, const int val);
    
        // 获取布尔值
        SDKEXPORT bool getBool(const char* element, const bool def = false);
    
        // 设置布尔值
        SDKEXPORT bool setBool(const char* element, const bool val);
    
        // 获取文本
        SDKEXPORT shared_ptr<wstring> getText(const char* element, const wchar_t* def = L"", const bool cdata = false);
    
        // 设置文本
        SDKEXPORT bool setText(const char* element, const wchar_t* val, const bool cdata = false);
    
    private:
        // 如果载入失败,则说明文件不存在或不规范,需要重建配置文件
        SDKEXPORT bool loadCfgFile();
    
        // 重新加载
        SDKEXPORT bool reLoad();
    
        // 创建配置文件
        SDKEXPORT bool createCfgFile();
    
    private:
        // 获取根结点
        TiXmlElement* getRoot();
    
    private:
        static const string _cfgFilePath;
        const string _cfgFile;
    
        TiXmlDocument _doc;
        TiXmlElement* _root;
    };
    
    #endif //  SDK_CONFIG_H_
    

    #include "config.h"
    
    const string Config::_cfgFilePath = *WC2MB((*getDataPath()).c_str());
    
    Config::Config(const char* fileName) : _cfgFile(_cfgFilePath + "\" + fileName + ".xml"),
            _doc(_cfgFile.c_str()), _root(NULL)
    {
        if (!loadCfgFile())
        {
            if (createCfgFile()) reLoad();
        }
    }
    
    Config::~Config()
    {
        _doc.SaveFile();
    }
    
    bool Config::loadCfgFile()
    {
        return _doc.LoadFile();
    }
    
    bool Config::reLoad()
    {
        return loadCfgFile();
    }
    
    bool Config::createCfgFile()
    {
        TiXmlDocument doc(_cfgFile.c_str());
        TiXmlDeclaration* dec = new TiXmlDeclaration("1.0", "utf-8", "");
        doc.LinkEndChild(dec);
    
        TiXmlComment* cmt = new TiXmlComment((*WC2MB(DEF_AppName)).c_str());
        doc.LinkEndChild(cmt);
    
        TiXmlElement* root = new TiXmlElement("Config");
        doc.LinkEndChild(root);
    
        return doc.SaveFile();
    }
    
    TiXmlElement* Config::getRoot()
    {
        if (_root == NULL) _root = _doc.RootElement();
        ASSERT(_root);
        return _root;
    }
    
    int Config::getInt(const char* element, const int def)
    {
        TiXmlElement* target = getRoot()->FirstChildElement(element);
    
        if (target != NULL)
        {
            int val;
            int ret = target->QueryIntAttribute("int", &val);
            if (ret == TIXML_SUCCESS) return val;
            else if (ret == TIXML_NO_ATTRIBUTE) target->SetAttribute("int", def);
        }
        else
        {
            TiXmlElement* add = new TiXmlElement(element);
            add->SetAttribute("int", def);
            getRoot()->LinkEndChild(add);
        }
    
        return def;
    }
    
    bool Config::setInt(const char* element, const int val)
    {
        TiXmlElement* target = getRoot()->FirstChildElement(element);
    
        if (target != NULL)
        {
            target->SetAttribute("int", val);
            return true;
        }
    
        return false;
    }
    
    bool Config::getBool(const char* element, const bool def)
    {
        int i = def ? 1 : 0;
        TiXmlElement* target = getRoot()->FirstChildElement(element);
    
        if (target != NULL)
        {
            int val;
            int ret = target->QueryIntAttribute("bool", &val);
            if (ret == TIXML_SUCCESS) return val ? true : false;
            else if (ret == TIXML_NO_ATTRIBUTE) target->SetAttribute("bool", i);
        }
        else
        {
            TiXmlElement* add = new TiXmlElement(element);
            add->SetAttribute("bool", i);
            getRoot()->LinkEndChild(add);
        }
    
        return def;
    }
    
    bool Config::setBool(const char* element, const bool val)
    {
        TiXmlElement* target = getRoot()->FirstChildElement(element);
    
        if (target != NULL)
        {
            target->SetAttribute("bool", val);
            return true;
        }
    
        return false;
    }
    
    shared_ptr<wstring> Config::getText(const char* element, const wchar_t* def, const bool cdata)
    {
        TiXmlElement* target = getRoot()->FirstChildElement(element);
    
        if (target != NULL)
        {
            return UT2WC(target->GetText());
        }
        else
        {
            TiXmlElement* add = new TiXmlElement(element);
            TiXmlText* txt = new TiXmlText((*WC2UT(def)).c_str());
            txt->SetCDATA(cdata);
            add->LinkEndChild(txt);
            getRoot()->LinkEndChild(add);
        }
    
        return shared_ptr<wstring>(new wstring(def));
    }
    
    bool Config::setText(const char* element, const wchar_t* val, const bool cdata)
    {
        TiXmlElement* target = getRoot()->FirstChildElement(element);
    
        if (target != NULL)
        {
            target->Clear();
            TiXmlText* txt = new TiXmlText((*WC2UT(val)).c_str());
            txt->SetCDATA(cdata);
            target->LinkEndChild(txt);
            return true;
        }
    
        return false;
    }




  • 相关阅读:
    Map集合的四种遍历
    java 请求 google translate
    Linux 内核初级管理
    Linux GRUB
    Linux 系统启动流程
    Linux 任务计划 crontab
    Linux 进程管理工具
    Linux sudo实作
    Linux 进程管理
    Linux 网络配置命令:ip、ss
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318538.html
Copyright © 2020-2023  润新知