BOOST1.41带来的新玩意property tree--很好的配置文件处理类库

首先是无耻的抄袭了boost文档中的例子:

boost_1_41_0/libs/property_tree/examples/debug_settings.cpp

……

关键是修改的save函数中的一点东西(红色的两行).这样它就可以将xml文件的编码格式修改为中文了.嘿嘿.使用的是GB18030编码.

使用C++方便的操作xml方式的配置文件.感觉这应该是对ini配置文件的一次比较好的升级.还是boost提供的.真不错.

待考察稳定了后,将陆续增加到自己的应用程序开发中.

 

void debug_settings::save(const std::string &filename)

{

// Create empty property tree object

using boost::property_tree::ptree;

    ptree pt;

// Put log filename in property tree

    pt.put("debug.filename", m_file);

// Put debug level in property tree

    pt.put("debug.level", m_level);

pt.add("debug.modules.module", "abc");

pt.add("debug.modules.module", "def");

pt.add("debug.modules.module", "我是中国人");

// Iterate over modules in set and put them in property

// tree. Note that put function places new key at the

// end of list of keys. This is fine in most of the

// situations. If you want to place item at some other

// place (i.e. at front or somewhere in the middle),

// this can be achieved using combination of insert

// and put_value functions

BOOST_FOREACH(const std::string &name, m_modules)

{

      pt.add("debug.modules.module", name);

}

// Write property tree to XML file

// 通过定制的xml设置类将xml文件的编码集设置为gb18030.这样就不必将xml文件转换为utf-8了

boost::property_tree::xml_parser::xml_writer_settings  ss('*' ,0,"gb18030");

write_xml(filename, pt,std::locale(),ss);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值