zoukankan      html  css  js  c++  java
  • 使用boost中的property_tree实现配置文件

    property_tree是专为配置文件而写,支持xml,ini和json格式文件
     
    ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择。
     
    使用property_tree也很简单,boost自带的帮助中有个5分钟指南
     
    这里写一下使用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>
    定义结构体如下:
     1 struct person
    2 {
    3 int age;
    4 std::string name;
    5 };
    6
    7 struct debug_persons
    8 {
    9 int itsTotalNumber;
    10 std::vector<person> itsPersons;
    11 void load(const std::string& filename);
    12 void save(const std::string& filename);
    13 };
    2个载入和保存的函数:
     1 void debug_persons::save( const std::string& filename )
    2 {
    3 using boost::property_tree::ptree;
    4 ptree pt;
    5
    6 pt.put("debug.total", itsTotalNumber);
    7
    8 BOOST_FOREACH(const person& p,itsPersons)
    9 {
    10 ptree child;
    11 child.put("age",p.age);
    12 child.put("name",p.name);
    13 pt.add_child("debug.persons.person",child);
    14 }
    15 // Write property tree to XML file
    16 write_xml(filename, pt);
    17
    18 }
    19
    20 void debug_persons::load( const std::string& filename )
    21 {
    22 using boost::property_tree::ptree;
    23 ptree pt;
    24
    25 read_xml(filename, pt);
    26 itsTotalNumber = pt.get<int>("debug.total");
    27
    28 BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.persons"))
    29 {
    30 //m_modules.insert(v.second.data());
    31 person p;
    32 p.age = v.second.get<int>("age");
    33 p.name = v.second.get<std::string>("name");
    34 itsPersons.push_back(p);
    35 }
    36 }
    main中的代码
     1 int _tmain(int argc, _TCHAR* argv[])
    2 {
    3
    4 try
    5 {
    6   debug_persons dp;
    7   dp.load("debug_persons.xml");
    8   std::cout<<dp<<std::endl;
    9   person p;
    10   p.age = 100;
    11   p.name = "old man";
    12   dp.itsPersons.push_back(p);
    13   dp.save("new.xml");
    14   std::cout << "Success ";
    15 }
    16 catch (std::exception &e)
    17 {
    18 std::cout << "Error: " << e.what() << " ";
    19 }
    20 return 0;
    21 }
    这里为了调试输出增加了以下代码:
     1 std::ostream& operator<<(std::ostream& o,const debug_persons& dp)
    2 {
    3   o << "totoal:" << dp.itsTotalNumber << " ";
    4   o << "persons ";
    5   BOOST_FOREACH(const person& p,dp.itsPersons)
    6   {
    7     o << " person: Age:" << p.age << " Nmae:" << p.name << " ";
    8   }
    9 return o;
    10 }
    ps:需要包含以下文件
    1 #include <boost/property_tree/ptree.hpp>
    2 #include <boost/property_tree/xml_parser.hpp>
    3 #include <boost/foreach.hpp>
    4 #include <vector>
    5 #include <string>
    6 #include <exception>
    7 #include <iostream>
  • 相关阅读:
    对象遍历 for in ,数组遍历for in 与 for of 的区别
    计算一个数组中key值相同的数量
    VUE的两种跳转push和replace对比区别
    微信公众号二次分享ios分享失败问题
    获得对象中的键或值
    第一个table根据checkbox选择tr,在另一个table中显示对应索引的tr(jq遍历的运用)
    checkbox 全选反选 获得所有的checkbox
    为什么jQuery要return this.each()?
    用jq代码写出一个轮播图。
    页面滚动到一定位置,两个div 朝中间运动。
  • 原文地址:https://www.cnblogs.com/lidabo/p/3905827.html
Copyright © 2011-2022 走看看