码迷,mamicode.com
首页 > 其他好文 > 详细

key_value 类型配置文件的解析

时间:2016-05-27 23:45:32      阅读:844      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 #include <string>
 3 #include <stdint.h>
 4 #include <map>
 5 #include <fstream>
 6 #include <stdlib.h>
 7 #include <stdio.h>
 8 
 9 enum
10 {
11     enmMaxConfigWordLen = 32,
12 };
13 
14 typedef std::map<std::string,std::string> ATTRMAP;
15 
16 
17 void GetTextFileContent(const char *fileName,char buf[],const uint32_t maxBufLen,uint32_t &readBufLen)
18 {
19     std::ifstream file(fileName,std::ios::in);
20     if(!file)
21     {
22         return ;
23     }
24     char c;
25     readBufLen = 0;
26     while(file.get(c) && readBufLen < maxBufLen)
27     {
28         if(c != \n && c !=   && c != 9)
29         {
30             buf[readBufLen++] = c;
31         }
32     }
33     buf[readBufLen]=\0;
34 }
35 
36 void GetAllAttrFromStr(const char *buf,const uint32_t bufLen,ATTRMAP &attrMap)
37 {
38     char key[enmMaxConfigWordLen],value[enmMaxConfigWordLen];
39     int32_t flag = 0,len = 0;       //0->key,1->value
40     for(uint32_t i = 0;i < bufLen; ++i)
41     {
42         const char &c = buf[i];
43         switch(c)
44         {
45         case {:flag=0;break;
46         case }:
47             value[len] = \0;
48             attrMap[key] = value;
49             break;
50         case ::
51             key[len] = \0;
52             len = 0;
53             flag = 1;
54             break;
55         case ,:
56             value[len] = \0;
57             len = 0;
58             flag = 0;
59             attrMap[key] = value;
60             break;
61         default:
62             if( flag == 0 )
63             {
64                 key[len++] = c;
65             }
66             else if( flag == 1 )
67             {
68                 value[len++] = c;
69             }
70             break;
71         }
72     }
73 }
74 
75 
76 int32_t main()
77 {
78     const uint32_t maxFileBufLen = 1024;
79     char buf[maxFileBufLen];
80     uint32_t bufLen;
81     GetTextFileContent("dbconfig.cfg",buf,maxFileBufLen,bufLen);
82     ATTRMAP attrMap;
83     GetAllAttrFromStr(buf,bufLen,attrMap);
84     std::string szHost = attrMap["host"];
85     std::string szUsr = attrMap["user"];
86     std::string szPasswd = attrMap["passwd"];
87     std::string szDbName = attrMap["dbname"];
88     std::string szTableName = attrMap["tablename"];
89     uint32_t unPort = atol(attrMap["port"].c_str());
90     printf("szHost = ‘%s‘\nszUsr = ‘%s‘\nszPasswd = ‘%s‘\nszDbName = ‘%s‘\nszTableName = ‘%s‘\nunPort = %d\n",91         szHost.c_str(),szUsr.c_str(),szPasswd.c_str(),szDbName.c_str(),szTableName.c_str(),unPort);
92     return 0;
93 }

测试文件:

{
    host:192.168.75.134,
    port:3306,
    user:sdo,
    passwd:123456,
    dbname:sdo_config,
    tablename:sdo_item
}

测试结果:

szHost = 192.168.75.134
szUsr = sdo
szPasswd = 123456
szDbName = sdo_config
szTableName = sdo_item
unPort = 3306

 

key_value 类型配置文件的解析

标签:

原文地址:http://www.cnblogs.com/tangxin-blog/p/5536394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!