forked from Gaaagaa/inifile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_read3.cpp
213 lines (192 loc) · 12 KB
/
test_read3.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @file test_read3.cpp
* Copyright (c) 2023 Gaaagaa. All rights reserved.
*
* @author : Gaaagaa
* @date : 2023-03-11
* @version : 1.0.0.0
* @brief : INI 的 (同步写入默认值)读测试 示例程序。
*/
/**
* The MIT License (MIT)
* Copyright (c) Gaaagaa. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "xini_file.h"
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
/**********************************************************/
/**
* @brief 测试 INI 的数据 带默认值 的读取操作。
*/
void test_ini_read(const std::string & xstr_file);
////////////////////////////////////////////////////////////////////////////////
/**********************************************************/
/**
* @brief 测试程序的入口 main() 函数。
*/
int main(int argc, char * argv[])
{
xini_file_t xfile;
if (argc < 2)
{
std::cout << "usage : "
<< argv[0]
<< " < ini file > [ locale : zh_CN.utf8 or en_US.utf8 ... ]"
<< std::endl;
return -1;
}
std::string xstr_file = argv[1];
if (argc >= 3)
{
std::cout << "locale : "
<< setlocale(LC_ALL, argv[2])
<< std::endl;
}
test_ini_read(xstr_file);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/**********************************************************/
/**
* @brief 测试 INI 的数据 带默认值 的读取操作。
*/
void test_ini_read(const std::string & xstr_file)
{
xini_file_t xini_file(xstr_file);
std::string vText1 = xini_file["section1"]["Text1" ].try_value(std::string("Default Text1") );
const char * vText2 = xini_file["section1"]["Text2" ].try_value("Default Text2" );
const char * vText3 = xini_file["section1"]["Text3" ].try_value("Default Text3" );
bool vBool1 = xini_file["section1"]["Bool1" ].try_value(true );
bool vBool2 = xini_file["section1"]["Bool2" ].try_value(false );
short vSInt1 = xini_file["section1"]["SInt1" ].try_value((short)12345 );
short vSInt2 = xini_file["section1"]["SInt2" ].try_value((short)0 );
unsigned short vUSInt1 = xini_file["section1"]["USInt1" ].try_value((unsigned short)256 );
unsigned short vUSInt2 = xini_file["section1"]["USInt2" ].try_value((unsigned short)128 );
int vInt1 = xini_file["section1"]["Int1" ].try_value(1234567890 );
int vInt2 = xini_file["section1"]["Int2" ].try_value(-1 );
unsigned int vUInt1 = xini_file["section1"]["UInt1" ].try_value(100000U );
unsigned int vUInt2 = xini_file["section1"]["UInt2" ].try_value(200000U );
long vLong1 = xini_file["section1"]["Long1" ].try_value(123456789L );
long vLong2 = xini_file["section1"]["Long2" ].try_value(987654321L );
unsigned long vULong1 = xini_file["section1"]["ULong1" ].try_value(10086UL );
unsigned long vULong2 = xini_file["section1"]["ULong2" ].try_value(10010UL );
long long vLLong1 = xini_file["section1"]["LLong1" ].try_value(10086LL );
long long vLLong2 = xini_file["section1"]["LLong2" ].try_value(10010LL );
unsigned long long vULLong1 = xini_file["section1"]["ULLong1" ].try_value(10086ULL );
unsigned long long vULLong2 = xini_file["section1"]["ULLong2" ].try_value(10010ULL );
float vFloat1 = xini_file["section1"]["Float1" ].try_value(3.1415926F );
float vFloat2 = xini_file["section1"]["Float2" ].try_value(-3.1415926F );
double vDouble1 = xini_file["section1"]["Double1" ].try_value(2.7182818284590452 );
double vDouble2 = xini_file["section1"]["Double2" ].try_value(-2.7182818284590452 );
long double vLDouble1 = xini_file["section1"]["LDouble1"].try_value((long double)0.61803398874989484820 );
long double vLDouble2 = xini_file["section1"]["LDouble2"].try_value((long double)-0.61803398874989484820);
std::cout.precision(20);
std::cout.setf(std::ios_base::boolalpha);
std::cout << "test_ini_read() output: " << std::endl;
std::cout << "----------------------------------------" << std::endl;
// 常规的读取操作
std::cout << "[section1]" << std::endl;
std::cout << "Text1 : " << vText1 << std::endl;
std::cout << "Text2 : " << vText2 << std::endl;
std::cout << "Text3 : " << vText3 << std::endl;
std::cout << "Bool1 : " << vBool1 << std::endl;
std::cout << "Bool2 : " << vBool2 << std::endl;
std::cout << "SInt1 : " << vSInt1 << std::endl;
std::cout << "SInt2 : " << vSInt2 << std::endl;
std::cout << "USInt1 : " << vUSInt1 << std::endl;
std::cout << "USInt2 : " << vUSInt2 << std::endl;
std::cout << "Int1 : " << vInt1 << std::endl;
std::cout << "Int2 : " << vInt2 << std::endl;
std::cout << "UInt1 : " << vUInt1 << std::endl;
std::cout << "UInt2 : " << vUInt2 << std::endl;
std::cout << "Long1 : " << vLong1 << std::endl;
std::cout << "Long2 : " << vLong2 << std::endl;
std::cout << "ULong1 : " << vULong1 << std::endl;
std::cout << "ULong2 : " << vULong2 << std::endl;
std::cout << "LLong1 : " << vLLong1 << std::endl;
std::cout << "LLong2 : " << vLLong2 << std::endl;
std::cout << "ULLong1 : " << vULLong1 << std::endl;
std::cout << "ULLong2 : " << vULLong2 << std::endl;
std::cout << "Float1 : " << vFloat1 << std::endl;
std::cout << "Float2 : " << vFloat2 << std::endl;
std::cout << "Double1 : " << vDouble1 << std::endl;
std::cout << "Double2 : " << vDouble2 << std::endl;
std::cout << "LDouble1 : " << vLDouble1 << std::endl;
std::cout << "LDouble2 : " << vLDouble2 << std::endl;
std::cout << "----------------------------------------" << std::endl;
vText1 = xini_file["section2"]["Text1" ].try_value(std::string("Default Text1") );
vText2 = xini_file["section2"]["Text2" ].try_value("Default Text2" );
vText3 = xini_file["section2"]["Text3" ].try_value("Default Text3" );
vBool1 = xini_file["section2"]["Bool1" ].try_value(true );
vBool2 = xini_file["section2"]["Bool2" ].try_value(false );
vSInt1 = xini_file["section2"]["SInt1" ].try_value((short)12345 );
vSInt2 = xini_file["section2"]["SInt2" ].try_value((short)0 );
vUSInt1 = xini_file["section2"]["USInt1" ].try_value((unsigned short)256 );
vUSInt2 = xini_file["section2"]["USInt2" ].try_value((unsigned short)128 );
vInt1 = xini_file["section2"]["Int1" ].try_value(1234567890 );
vInt2 = xini_file["section2"]["Int2" ].try_value(-1 );
vUInt1 = xini_file["section2"]["UInt1" ].try_value(100000U );
vUInt2 = xini_file["section2"]["UInt2" ].try_value(200000U );
vLong1 = xini_file["section2"]["Long1" ].try_value(123456789L );
vLong2 = xini_file["section2"]["Long2" ].try_value(987654321L );
vULong1 = xini_file["section2"]["ULong1" ].try_value(10086UL );
vULong2 = xini_file["section2"]["ULong2" ].try_value(10010UL );
vLLong1 = xini_file["section2"]["LLong1" ].try_value(10086LL );
vLLong2 = xini_file["section2"]["LLong2" ].try_value(10010LL );
vULLong1 = xini_file["section2"]["ULLong1" ].try_value(10086ULL );
vULLong2 = xini_file["section2"]["ULLong2" ].try_value(10010ULL );
vFloat1 = xini_file["section2"]["Float1" ].try_value(3.1415926F );
vFloat2 = xini_file["section2"]["Float2" ].try_value(-3.1415926F );
vDouble1 = xini_file["section2"]["Double1" ].try_value(2.7182818284590452 );
vDouble2 = xini_file["section2"]["Double2" ].try_value(-2.7182818284590452 );
vLDouble1 = xini_file["section2"]["LDouble1"].try_value((long double)0.61803398874989484820 );
vLDouble2 = xini_file["section2"]["LDouble2"].try_value((long double)-0.61803398874989484820);
// 常规的读取操作
std::cout << "[section2]" << std::endl;
std::cout << "Text1 : " << vText1 << std::endl;
std::cout << "Text2 : " << vText2 << std::endl;
std::cout << "Text3 : " << vText3 << std::endl;
std::cout << "Bool1 : " << vBool1 << std::endl;
std::cout << "Bool2 : " << vBool2 << std::endl;
std::cout << "SInt1 : " << vSInt1 << std::endl;
std::cout << "SInt2 : " << vSInt2 << std::endl;
std::cout << "USInt1 : " << vUSInt1 << std::endl;
std::cout << "USInt2 : " << vUSInt2 << std::endl;
std::cout << "Int1 : " << vInt1 << std::endl;
std::cout << "Int2 : " << vInt2 << std::endl;
std::cout << "UInt1 : " << vUInt1 << std::endl;
std::cout << "UInt2 : " << vUInt2 << std::endl;
std::cout << "Long1 : " << vLong1 << std::endl;
std::cout << "Long2 : " << vLong2 << std::endl;
std::cout << "ULong1 : " << vULong1 << std::endl;
std::cout << "ULong2 : " << vULong2 << std::endl;
std::cout << "LLong1 : " << vLLong1 << std::endl;
std::cout << "LLong2 : " << vLLong2 << std::endl;
std::cout << "ULLong1 : " << vULLong1 << std::endl;
std::cout << "ULLong2 : " << vULLong2 << std::endl;
std::cout << "Float1 : " << vFloat1 << std::endl;
std::cout << "Float2 : " << vFloat2 << std::endl;
std::cout << "Double1 : " << vDouble1 << std::endl;
std::cout << "Double2 : " << vDouble2 << std::endl;
std::cout << "LDouble1 : " << vLDouble1 << std::endl;
std::cout << "LDouble2 : " << vLDouble2 << std::endl;
std::cout << "----------------------------------------" << std::endl;
}
////////////////////////////////////////////////////////////////////////////////