用boost中的property_tree实现配置文件
来源:互联网 发布:淘宝代销商品怎么下单 编辑:程序博客网 时间:2024/04/27 04:22
property_tree是专为配置文件而写,支持xml,ini和json格式文件
ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择。
使用property_tree也很简单,boost自带的帮助中有个5分钟指南
http://www.boost.org/doc/libs/1_46_1/doc/html/boost_propertytree/tutorial.html
这里写一下使用xml来保存多维数组,在有些情况下一维数组并不能满足要求。
举个简单的例子吧:
xml格式如下:
<debug>
<total>3</total>
<persons>
<person>
<age>23</age>
<name>hugo</name>
</person>
<person>
<age>23</age>
<name>mayer</name>
</person>
<person>
<age>30</age>
<name>boy</name>
</person>
</persons>
</debug>
定义结构体如下:
struct person
{
int age;
std::string name;
};
struct debug_persons
{
int itsTotalNumber;
std::vector<person> itsPersons;
void load(const std::string& filename);
void save(const std::string& filename);
};
2个载入和保存的函数:
void debug_persons::save( const std::string& filename )
{
using boost::property_tree::ptree;
ptree pt;
pt.put("debug.total", itsTotalNumber);
BOOST_FOREACH(const person& p,itsPersons)
{
ptree child;
child.put("age",p.age);
child.put("name",p.name);
pt.add_child("debug.persons.person",child);
}
// Write property tree to XML file
write_xml(filename, pt);
}
void debug_persons::load( const std::string& filename )
{
using boost::property_tree::ptree;
ptree pt;
read_xml(filename, pt);
itsTotalNumber = pt.get<int>("debug.total");
BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.persons"))
{
//m_modules.insert(v.second.data());
person p;
p.age = v.second.get<int>("age");
p.name = v.second.get<std::string>("name");
itsPersons.push_back(p);
}
}
main中的代码
int _tmain(int argc, _TCHAR* argv[])
{
try
{
debug_persons dp;
dp.load("debug_persons.xml");
std::cout<<dp<<std::endl;
person p;
p.age = 100;
p.name = "old man";
dp.itsPersons.push_back(p);
dp.save("new.xml");
std::cout << "Success\n";
}
catch (std::exception &e)
{
std::cout << "Error: " << e.what() << "\n";
}
return 0;
}
这里为了调试输出增加了以下代码:
std::ostream& operator<<(std::ostream& o,const debug_persons& dp)
{
o << "totoal:" << dp.itsTotalNumber << "\n";
o << "persons\n";
BOOST_FOREACH(const person& p,dp.itsPersons)
{
o << "\tperson: Age:" << p.age << " Nmae:" << p.name << "\n";
}
return o;
}
ps:需要包含以下文件
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <vector>
#include <string>
#include <exception>
#include <iostream>
ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择。
使用property_tree也很简单,boost自带的帮助中有个5分钟指南
http://www.boost.org/doc/libs/1_46_1/doc/html/boost_propertytree/tutorial.html
这里写一下使用xml来保存多维数组,在有些情况下一维数组并不能满足要求。
举个简单的例子吧:
xml格式如下:
<debug>
<total>3</total>
<persons>
<person>
<age>23</age>
<name>hugo</name>
</person>
<person>
<age>23</age>
<name>mayer</name>
</person>
<person>
<age>30</age>
<name>boy</name>
</person>
</persons>
</debug>
定义结构体如下:
struct person
{
int age;
std::string name;
};
struct debug_persons
{
int itsTotalNumber;
std::vector<person> itsPersons;
void load(const std::string& filename);
void save(const std::string& filename);
};
2个载入和保存的函数:
void debug_persons::save( const std::string& filename )
{
using boost::property_tree::ptree;
ptree pt;
pt.put("debug.total", itsTotalNumber);
BOOST_FOREACH(const person& p,itsPersons)
{
ptree child;
child.put("age",p.age);
child.put("name",p.name);
pt.add_child("debug.persons.person",child);
}
// Write property tree to XML file
write_xml(filename, pt);
}
void debug_persons::load( const std::string& filename )
{
using boost::property_tree::ptree;
ptree pt;
read_xml(filename, pt);
itsTotalNumber = pt.get<int>("debug.total");
BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.persons"))
{
//m_modules.insert(v.second.data());
person p;
p.age = v.second.get<int>("age");
p.name = v.second.get<std::string>("name");
itsPersons.push_back(p);
}
}
main中的代码
int _tmain(int argc, _TCHAR* argv[])
{
try
{
debug_persons dp;
dp.load("debug_persons.xml");
std::cout<<dp<<std::endl;
person p;
p.age = 100;
p.name = "old man";
dp.itsPersons.push_back(p);
dp.save("new.xml");
std::cout << "Success\n";
}
catch (std::exception &e)
{
std::cout << "Error: " << e.what() << "\n";
}
return 0;
}
这里为了调试输出增加了以下代码:
std::ostream& operator<<(std::ostream& o,const debug_persons& dp)
{
o << "totoal:" << dp.itsTotalNumber << "\n";
o << "persons\n";
BOOST_FOREACH(const person& p,dp.itsPersons)
{
o << "\tperson: Age:" << p.age << " Nmae:" << p.name << "\n";
}
return o;
}
ps:需要包含以下文件
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <vector>
#include <string>
#include <exception>
#include <iostream>
0 0
- 用boost中的property_tree实现配置文件
- Boost property_tree 解析配置文件
- c++用boost的property_tree库来操作xml配置文件
- 使用boost property_tree 生成XML配置文件
- 使用 boost::property_tree::ptree 读取配置文件
- boost::property_tree
- boost::property_tree
- boost::property_tree
- boost::property_tree
- boost.property_tree
- BOOST property_tree
- boost中property_tree操作内存中的数据
- 简单使用boost property_tree组件获取配置文件信息
- boost.property_tree使用示例
- boost::property_tree实例用法
- boost property_tree遍历
- boost::property_tree讲解
- boost库property_tree示例
- 【jQuery】使用one()方法绑定元素的一次性事件
- ActivatePowerMode插件,xcode版本
- Python学习二:Django Web入门
- Lustre文件锁问题
- Java多线程总结(5)— 原子性、可见性、有序性和并发库的原子性操作
- 用boost中的property_tree实现配置文件
- Android分区解释
- 按钮设置监听
- Android内存优化杂谈
- hibernate3.0执行存储过程
- 模块化Activity-Fragment
- 如何将ubuntu中的顶部任务栏变成半透明
- JAVA面向对象的思想
- java中数据持久层查询操作