您的位置:首页 > 运维架构

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

2009-12-03 17:58 471 查看
首先是无耻的抄袭了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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: