zoukankan      html  css  js  c++  java
  • boost::property_tree读取解析ini文件--推荐

    boost::property_tree读取解析ini文件

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #include "stdafx.h"  
    2. #include <iostream>  
    3. #include <boost/property_tree/ptree.hpp>  
    4. #include <boost/property_tree/ini_parser.hpp>  
    5.   
    6. int main()  
    7. {  
    8.     boost::property_tree::ptree pt;  
    9.     boost::property_tree::ini_parser::read_ini("D:\Overlay.ini", pt);  
    10.   
    11.     std::cout << pt.get<std::string>("OVERLAY.OverlayFontName") << std::endl;  
    12.   
    13.     pt.put<std::string>("OVERLAY.OverlayFontName","宋体");  
    14.   
    15.     std::cout << pt.get<std::string>("OVERLAY.OverlayFontName") << std::endl;  
    16.   
    17.     boost::property_tree::ini_parser::write_ini("D:\Overlay.ini",pt);  
    18.   
    19.     return 0;  
    20. }  


    在C++11下,宽字节和单字节转换就简单了。使用std::wstring_convert和std::codecvt_utf8 来处理UTF8与WChar之间的互转.

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1.  #include <iostream>  
    2. #include <string>  
    3. #include <locale>  
    4. #include <codecvt>  
    5. #include <fstream>  
    6.   
    7. int main(int argc, char *argv[])  
    8. {  
    9.    std::wstring str = L"123,宋体!";  
    10.   
    11.    std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;  
    12.   
    13.    std::string narrowStr = conv.to_bytes(str);  
    14.    {  
    15.       std::ofstream ofs ("c:\test.txt");  
    16.       ofs << narrowStr;  
    17.    }  
    18.   
    19.    std::wstring wideStr = conv.from_bytes(narrowStr);  
    20.    {  
    21.       std::locale::global(std::locale("Chinese-simplified"));  
    22.       std::wofstream ofs (L"c:\testW.txt");  
    23.       ofs << wideStr;  
    24.    }  
    25. }  

    另外可以保存函数,使用ptree:

    1 struct debug_simple
    2 {
    3     int itsNumber;
    4     std::string itsName; //这里使用string就可以
    5     void load(const std::string& filename); //载入函数
    6     void save(const std::string& filename); //保存函数
    7 };

    保存函数,使用ptree:

    复制代码
     1 void debug_simple::save( const std::string& filename )
     2 {
     3     using boost::property_tree::ptree;
     4     ptree pt;
     5 
     6     pt.put("debug.number",itsNumber);
     7     pt.put("debug.name",itsName);
     8 
     9     write_xml(filename,pt);
    10 }
    复制代码

    载入函数使用的wptree,读取的值为wstring,需转换成string

    复制代码
     1 void debug_simple::load( const std::string& filename )
     2 {
     3     using boost::property_tree::wptree;
     4     wptree wpt;
     5     read_xml(filename, wpt);
     6 
     7     itsNumber = wpt.get<int>(L"debug.number");
     8     std::wstring wStr = wpt.get<std::wstring>(L"debug.name");
     9     itsName = std::string(wStr.begin(),wStr.end()); //wstring转string
    10 }
    main函数:
    复制代码
     1 int _tmain(int argc, _TCHAR* argv[])
     2 {
     3     
     4     try
     5     {
     6         debug_simple ds,read;
     7         ds.itsName = "汉字english";
     8         ds.itsNumber = 20;
     9 
    10         ds.save("simple.xml");
    11         read.load("simple.xml");
    12 
    13         std::cout<<read.itsNumber<<read.itsName;
    14 
    15     }
    16     catch (std::exception &e)
    17     {
    18         std::cout << "Error: " << e.what() << "
    ";
    19     }
    20     return 0;
    21 }
     

    由于.ini文件是utf-8格式的,所以操作时要utf-8到unicode转换,或unicode到utf-8转换;

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. // PropertyTree.cpp : 定义控制台应用程序的入口点。  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5. #include <iostream>  
    6. using namespace std;  
    7.   
    8. #include <boost/property_tree/ptree.hpp>  
    9. #include <boost/property_tree/ini_parser.hpp>  
    10.   
    11. //unicode 转UTF8  
    12. //参数1是UTF8字符串当前位置指针,这里必须要是指针,因为必须要通过第1个字符进行判断才知道一个完整的字符的编码要向后取多少个字符    
    13. //参数2是返回的UCS-2编码的Unicode字符    
    14. inline int UTF82UnicodeOne(const char* utf8, wchar_t& wch)    
    15. {    
    16.     if (utf8==NULL)  
    17.     {  
    18.         return -1;  
    19.     }  
    20.   
    21.     //首字符的Ascii码大于0xC0才需要向后判断,否则,就肯定是单个ANSI字符了    
    22.     unsigned char firstCh = utf8[0];    
    23.     if (firstCh >= 0xC0)    
    24.     {    
    25.         //根据首字符的高位判断这是几个字母的UTF8编码    
    26.         int afters, code;    
    27.         if ((firstCh & 0xE0) == 0xC0)    
    28.         {    
    29.             afters = 2;    
    30.             code = firstCh & 0x1F;    
    31.         }    
    32.         else if ((firstCh & 0xF0) == 0xE0)    
    33.         {    
    34.             afters = 3;    
    35.             code = firstCh & 0xF;    
    36.         }    
    37.         else if ((firstCh & 0xF8) == 0xF0)    
    38.         {    
    39.             afters = 4;    
    40.             code = firstCh & 0x7;    
    41.         }    
    42.         else if ((firstCh & 0xFC) == 0xF8)    
    43.         {    
    44.             afters = 5;    
    45.             code = firstCh & 0x3;    
    46.         }    
    47.         else if ((firstCh & 0xFE) == 0xFC)    
    48.         {    
    49.             afters = 6;    
    50.             code = firstCh & 0x1;    
    51.         }    
    52.         else    
    53.         {    
    54.             wch = firstCh;    
    55.             return 1;    
    56.         }    
    57.   
    58.         //知道了字节数量之后,还需要向后检查一下,如果检查失败,就简单的认为此UTF8编码有问题,或者不是UTF8编码,于是当成一个ANSI来返回处理    
    59.         for(int k = 1; k < afters; ++ k)    
    60.         {    
    61.             if ((utf8[k] & 0xC0) != 0x80)    
    62.             {    
    63.                 //判断失败,不符合UTF8编码的规则,直接当成一个ANSI字符返回    
    64.                 wch = firstCh;    
    65.                 return 1;    
    66.             }    
    67.   
    68.             code <<= 6;    
    69.             code |= (unsigned char)utf8[k] & 0x3F;    
    70.         }    
    71.   
    72.         wch = code;    
    73.         return afters;    
    74.     }    
    75.     else    
    76.     {    
    77.         wch = firstCh;    
    78.     }    
    79.     return 1;    
    80. }    
    81.   
    82. //参数1是UTF8编码的字符串    
    83. //参数2是输出的UCS-2的Unicode字符串    
    84. //参数3是参数1字符串的长度    
    85. //使用的时候需要注意参数2所指向的内存块足够用。其实安全的办法是判断一下pUniBuf是否为NULL,如果为NULL则只统计输出长度不写pUniBuf,这样    
    86. //通过两次函数调用就可以计算出实际所需要的Unicode缓存输出长度。当然,更简单的思路是:无论如何转换,UTF8的字符数量不可能比Unicode少,所    
    87. //以可以简单的按照sizeof(wchar_t) * utf8Leng来分配pUniBuf的内存……    
    88. int UTF82Unicode(const char* utf8Buf, wchar_t *pUniBuf, int utf8Leng)    
    89. {       
    90.   
    91.     if ((utf8Buf==NULL)||(pUniBuf==NULL))  
    92.     {  
    93.         return -1;  
    94.     }  
    95.   
    96.     int i = 0, count = 0;    
    97.     while(i < utf8Leng)    
    98.     {    
    99.         i += UTF82UnicodeOne(utf8Buf + i, pUniBuf[count]);    
    100.         count ++;    
    101.     }    
    102.   
    103.     return count;    
    104. }    
    105.   
    106. inline int Unicode2UTF8One(unsigned wchar, char *utf8)    
    107. {    
    108.   
    109.     if (utf8==NULL)  
    110.     {  
    111.         return -1;  
    112.     }  
    113.   
    114.     int len = 0;    
    115.     if (wchar < 0xC0)    
    116.     {     
    117.         utf8[len ++] = (char)wchar;    
    118.     }    
    119.     else if (wchar < 0x800)    
    120.     {    
    121.         utf8[len ++] = 0xc0 | (wchar >> 6);    
    122.         utf8[len ++] = 0x80 | (wchar & 0x3f);    
    123.     }    
    124.     else if (wchar < 0x10000)    
    125.     {    
    126.         utf8[len ++] = 0xe0 | (wchar >> 12);    
    127.         utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);    
    128.         utf8[len ++] = 0x80 | (wchar & 0x3f);    
    129.     }    
    130.     else if (wchar < 0x200000)     
    131.     {    
    132.         utf8[len ++] = 0xf0 | ((int)wchar >> 18);    
    133.         utf8[len ++] = 0x80 | ((wchar >> 12) & 0x3f);    
    134.         utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);    
    135.         utf8[len ++] = 0x80 | (wchar & 0x3f);    
    136.     }    
    137.     else if (wchar < 0x4000000)    
    138.     {    
    139.         utf8[len ++] = 0xf8 | ((int)wchar >> 24);    
    140.         utf8[len ++] = 0x80 | ((wchar >> 18) & 0x3f);    
    141.         utf8[len ++] = 0x80 | ((wchar >> 12) & 0x3f);    
    142.         utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);    
    143.         utf8[len ++] = 0x80 | (wchar & 0x3f);    
    144.     }    
    145.     else if (wchar < 0x80000000)    
    146.     {    
    147.         utf8[len ++] = 0xfc | ((int)wchar >> 30);    
    148.         utf8[len ++] = 0x80 | ((wchar >> 24) & 0x3f);    
    149.         utf8[len ++] = 0x80 | ((wchar >> 18) & 0x3f);    
    150.         utf8[len ++] = 0x80 | ((wchar >> 12) & 0x3f);    
    151.         utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);    
    152.         utf8[len ++] = 0x80 | (wchar & 0x3f);    
    153.     }    
    154.   
    155.     return len;    
    156. }    
    157.   
    158. //  
    159. int Unicode2UTF8( const wchar_t *pUniBuf,char* utf8Buf, int UniLeng)    
    160. {     
    161.     if ((utf8Buf==NULL)||(pUniBuf==NULL))  
    162.     {  
    163.         return -1;  
    164.     }  
    165.   
    166.     int count = 0, i = 0;    
    167.     while(i < UniLeng)    
    168.     {    
    169.         count += Unicode2UTF8One(pUniBuf[i], utf8Buf+count);    
    170.         i ++;    
    171.     }    
    172.   
    173.     return count;    
    174. }    
    175.   
    176. int main()  
    177. {  
    178.     boost::property_tree::ptree pt;  
    179.     using boost::property_tree::wptree;   
    180.     wptree wpt;  
    181.   
    182.     boost::property_tree::ini_parser::read_ini("D:\Overlay.ini", pt);  
    183.   
    184.     std::string fontName=pt.get<std::string>("OVERLAY.OverlayFontName") ;  
    185.   
    186.     wchar_t wfontName[128]={0};  
    187.   
    188.     UTF82Unicode(fontName.c_str(),wfontName,fontName.length());  
    189.     std::wstring  wstrfontName=wfontName;  
    190.   
    191.     //std::wcout << wstrfontName.c_str()<< std::endl;  
    192.       
    193.     /*std::wstring */  
    194.     wstrfontName=_T("我是谁");  
    195.     char cfontName[128]={0};  
    196.   
    197.     Unicode2UTF8(wstrfontName.c_str(),cfontName,wstrfontName.length());  
    198.   
    199.     pt.put<std::string>("OVERLAY.OverlayFontName",cfontName);  
    200.   
    201.     //std::cout << pt.get<std::string>("OVERLAY.OverlayFontName") << std::endl;  
    202.   
    203.     boost::property_tree::ini_parser::write_ini("D:\Overlay.ini",pt);  
    204.   
    205.     return 0;  
    206. }  
  • 相关阅读:
    Maven
    Maven
    Tomcat8
    首先,定义一个时钟类——Clock,它包括三个int型 成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒) 进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义 一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这 些对象调用方法show()来显示时钟的时间。
    首先定义一个描述银行账户的Account类,包括成员变 量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次, 编写一个主类,在主类中测试Account类的功能。
    .编写一个Java应用程序,该程序中有3个类:Lader、Circle和主类A。具体要求如下:Lader类具有类型为double的上底、下底、高、面积属性,具有返回面积的功能,包括一个构造方法对上底、下底、高进行初始化。Circle类具有类型为double的半径、周长和面积属性,具有返回周长、面积的功能,包括一个构造方法对半径进行初始化。主类A用来测试类Lader和类Circle的功能
    输入三个整数x,y,z,请把这三个数由小到大输出。
    有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
    (1)编写西游记人物类(XiYouJiRenWu) 其中属性有:身高(height),名字(name),武器(weapon) 方法有:显示名字(printName),显示武器(printWeapon) (2)在主类的main方法中创建二个对象:zhuBaJie,sunWuKong。并分别为他 们的两个属性(name,weapon)赋值,最后分别调用printName, printWeapon方法
    输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
  • 原文地址:https://www.cnblogs.com/lidabo/p/3906069.html
Copyright © 2011-2022 走看看