INI配置文件分析小例子

随手写个解析INI配置字符串的小例子 带测试

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <map>
#include <string>
#include "unittest.h"
 
 
 
bool IniParser(std::string inputStr,std::map<std::string,std::string>& keyValueMap){
    bool ret = false;
    size_t keyStart = 0;
 
    for(keyStart= 0 ;keyStart < inputStr.size();keyStart++){
        if(inputStr[keyStart] == ' '){
            continue;   //去除空格
        }else if( inputStr[keyStart] == ';' || inputStr[keyStart] == '#' ||
                  inputStr[keyStart] == '[' || inputStr[keyStart] == ']' || inputStr[keyStart] == '='){
            return ret; // 各种意外开头 直接返回失败
        }else{
            break;
        }
    }
    size_t findPos = inputStr.find_first_of("=",keyStart);
    if(std::string::npos == findPos){
        return ret;//没有找到"="
    }
    std::string keyString =  inputStr.substr(keyStart,findPos -keyStart);
 
   // std::cout << "keyString:" << keyString << std::endl;
 
    for(keyStart= findPos+1 ;keyStart < inputStr.size();keyStart++){
        if(inputStr[keyStart] == ' '){
            continue;   //去除空格
        }else{
            break;
        }
    }
 
    std::string valueString = inputStr.substr(keyStart);
 
    //std::cout << "valueString:" << valueString << std::endl;
 
 
    keyValueMap.insert(std::pair<std::string,std::string>(keyString,valueString));
    ret = true;
    return ret;
}
 
std::string testStr1 = "key=value";
std::string testStr2 = "  key  =  value";
 
std::string testStr3 = "# key=value";
std::string testStr4 = "== key=value";
std::string testStr5 = "[ 23423key=value";
std::string testStr6 = "]324234key=value";
std::string testStr7 = ";key= = value";
std::string testStr8 = "  key  = = value";
 
 
 
 
int main(int argc, char *argv[])
{
 
    std::map<std::string,std::string> keyValueMap;
 
 
    EXCEPT_EQ( IniParser(testStr1,keyValueMap),true);
    EXCEPT_EQ( IniParser(testStr2,keyValueMap),true);
    EXCEPT_EQ( IniParser(testStr3,keyValueMap),false);
    EXCEPT_EQ( IniParser(testStr4,keyValueMap),false);
    EXCEPT_EQ( IniParser(testStr5,keyValueMap),false);
    EXCEPT_EQ( IniParser(testStr6,keyValueMap),false);
    EXCEPT_EQ( IniParser(testStr7,keyValueMap),false);
    EXCEPT_EQ( IniParser(testStr8,keyValueMap),true);
 
 
 
 
    PRINT_TEST_RESULT();
    return 0;
}

  测试头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef UNITTEST_H
#define UNITTEST_H
#include <iostream>
 
 
 
class MyTestClass {
    static size_t testCount_;
    static size_t testPass_;
public:
    template<typename E,typename A>
    bool ExceptEqual(E e, A a)
    {
        testCount_++;
        if (e == a){
            testPass_++;
            return true;
        }
        return false;
    }
 
    void PrintResult(){
        std::cout << testPass_ << "/" << testCount_ << "\t" << "("<<(testPass_ * 100.0 / testCount_) << "%)" << " passed." << std::endl;
    }
};
size_t MyTestClass::testCount_ = 0;
size_t MyTestClass::testPass_ = 0;
 
#define ERROR_PRINT(expect,actual)                  \
std::cerr << __FILE__ <<":"<< __LINE__ << "\r\nexpect: " << expect << "  actual: " << actual << std::endl;
 
 
#define EXCEPT_EQ(expect, actual)           \
do{                                         \
    MyTestClass TEST;                       \
    if(!TEST.ExceptEqual(expect, actual)){  \
        ERROR_PRINT(expect,actual)          \
    }                                       \
}while(0)
 
#define PRINT_TEST_RESULT()                 \
do{                                         \
    MyTestClass TEST;                       \
    TEST.PrintResult();                     \
}while(0)
 
#endif // UNITTEST_H

  

posted on   itdef  阅读(203)  评论(1编辑  收藏  举报

编辑推荐:
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
历史上的今天:
2014-12-28 系统性能监控界面学习

导航

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示