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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值