参考网址:

.ini配置文件解析器–minIni (qq.com)

peitianyu/config_ini (github.com)

使用

1
2
3
4
5
// config_ini
Config *USINGINFOPAHT;
USINGINFOPAHT = cnf_read_config(ConfigFile_name, '#', ':');
cnf_get_value(USINGINFOPAHT, "scan", "angle_min");
angle_min = USINGINFOPAHT->re_double;
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
/*
gcc -o minIni.o -c minIni.c
g++ -o test2.o -c test2.cc
g++ -o test2 test2.o minIni.o
./test2
*/


#include <cassert>
#include <iostream>
#include <string>
using namespace std ;

#include "minIni.h"

int main(void)
{
minIni ini("test.ini");
string s;

/* string reading */
s = ini.gets( "first", "string" , "aap" );
assert(s == "noot");
s = ini.gets( "second", "string" , "aap" );
assert(s == "mies");
s = ini.gets( "first", "dummy" , "aap" );
assert(s == "aap");
cout << "1. String reading tests passed" << endl ;


/* value reading */
long n;
n = ini.getl("first", "val", -1 );
assert(n==1);
n = ini.getl("second", "val", -1);
assert(n==2);
n = ini.getl("first", "dummy", -1);
assert(n==-1);
cout << "2. Value reading tests passed" << endl ;


/* string writing */
bool b;
b = ini.put("first", "alt", "flagged as \"correct\"");
assert(b);
s = ini.gets("first", "alt", "aap");
assert(s=="flagged as \"correct\"");

b = ini.put("second", "alt", "correct");
assert(b);
s = ini.gets("second", "alt", "aap");
assert(s=="correct");

b = ini.put("third", "alt", "correct");
assert(b);
s = ini.gets("third", "alt", "aap" );
assert(s=="correct");
cout << "3. String writing tests passed" << endl;

/* section/key enumeration */
cout << "4. section/key enumeration; file contents follows" << endl;
string section;
for (int is = 0; section = ini.getsection(is), section.length() > 0; is++) {
cout << " [" << section.c_str() << "]" << endl;
for (int ik = 0; s = ini.getkey(section, ik), s.length() > 0; ik++) {
cout << "\t" << s.c_str() << endl;
}
}

/* string deletion */
b = ini.del("first", "alt");
assert(b);
b = ini.del("second", "alt");
assert(b);
b = ini.del("third");
assert(b);
cout << "5. string deletion passed " << endl;

return 0;
}