zoukankan      html  css  js  c++  java
  • key_value 类型配置文件的解析

     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 != '
    ' && c != ' ' && c != 9)
    29         {
    30             buf[readBufLen++] = c;
    31         }
    32     }
    33     buf[readBufLen]='';
    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] = '';
    48             attrMap[key] = value;
    49             break;
    50         case ':':
    51             key[len] = '';
    52             len = 0;
    53             flag = 1;
    54             break;
    55         case ',':
    56             value[len] = '';
    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'
    szUsr = '%s'
    szPasswd = '%s'
    szDbName = '%s'
    szTableName = '%s'
    unPort = %d
    ",
    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
  • 相关阅读:
    MongoDB小结07
    MongoDB小结07
    MongoDB小结06
    MongoDB小结05
    MongoDB小结04
    MongoDB小结03
    MongoDB小结02
    MongoDB小结01
    this与super
    UVa 11174
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/5536394.html
Copyright © 2011-2022 走看看