-
Notifications
You must be signed in to change notification settings - Fork 42
/
FirstExample.cpp
244 lines (212 loc) · 7.29 KB
/
FirstExample.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* MIT License
*
* Copyright(c) 2018 Jimmie Bergmann
*
* 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 <Yaml.hpp>
using namespace Yaml;
static void Example1();
static void Example2();
static void Example3();
static void Example4();
int main()
{
Example1();
Example2();
Example3();
Example4();
return 0;
}
void Example1()
{
const std::string data =
"data1 : \t | \t \n"
" Hello1\n"
" world1.\n"
"data2 : \t > \t \n"
" Hello2\n"
" world2.\n"
"data3: \t |- \t \n"
" Hello3\n"
" world3.\n"
"data4 : \t >- \t \n"
" Hello4\n"
" world4.\n"
"data5: |\n"
" hello: world5\n"
" foo: bar5.\n"
"data6: 123\n"
"data7: 123.6\n";
Node root;
try
{
Parse(root, data);
}
catch (const Exception e)
{
std::cout << "Exception " << e.Type() << ": " << e.what() << std::endl;
return;
}
std::cout << root["data1"].As<std::string>() << std::endl;
std::cout << root["data2"].As<std::string>() << std::endl;
std::cout << root["data3"].As<std::string>() << std::endl;
std::cout << root["data4"].As<std::string>() << std::endl;
std::cout << root["data5"].As<std::string>() << std::endl;
std::cout << root["data6"].As<int>(0) << std::endl;
std::cout << root["data7"].As<float>(0.0f) << std::endl;
}
void Example2()
{
const std::string data =
" - Hello world\n"
" - 123\n"
" - 123.4\n";
Node root;
try
{
Parse(root, data);
if(root.IsSequence() == false)
{
throw InternalException("Test: Root is not a sequence.");
}
}
catch (const Exception e)
{
std::cout << "Exception " << e.Type() << ": " << e.what() << std::endl;
return;
}
std::cout << root[0].As<std::string>() << std::endl;
std::cout << root[1].As<int>(0) << std::endl;
std::cout << root[2].As<float>(0.0f) << std::endl;
}
void Example3()
{
const std::string data =
"data1: \n"
" 123\n"
"data2: Hello world\n"
"data3:\n"
" - key1: 123\n"
" key2: Test\n"
" - Hello world\n"
" - 123\n"
" - 123.4\n";
Node root;
try
{
Parse(root, data);
}
catch (const Exception e)
{
std::cout << "Exception " << e.Type() << ": " << e.what() << std::endl;
return;
}
std::cout << root["data1"].As<int>(0) << std::endl;
std::cout << root["data2"].As<std::string>() << std::endl;
std::cout << root["data3"][0]["key1"].As<int>(0) << std::endl;
std::cout << root["data3"][0]["key2"].As<std::string>() << std::endl;
std::cout << root["data3"][1].As<std::string>() << std::endl;
std::cout << root["data3"][2].As<int>(0) << std::endl;
std::cout << root["data3"][3].As<float>(0.0f) << std::endl;
}
void Example4()
{
Node root;
try
{
Parse(root, "../examples/data1.txt");
}
catch (const Exception e)
{
std::cout << "Exception " << e.Type() << ": " << e.what() << std::endl;
return;
}
try
{
Node & server = root["server"];
Node & services = root["services"];
if(server.IsMap() == false)
{
throw InternalException("Server is not of type Map.");
}
if(services.IsSequence() == false)
{
throw InternalException("Services is not of type sequence.");
}
std::cout << "Server:" << std::endl;
std::cout << " max connections: " << server["max_connections"].As<int>(0) << std::endl;
std::cout << " com port : " << server["com_port"].As<unsigned short>(0) << std::endl;
std::cout << "Services:" << std::endl;
//for(size_t i = 0; i < services.Size(); i++)
size_t countS = 0;
for(auto itS = services.Begin(); itS != services.End(); itS++)
{
std::cout << " Service " << countS++ << std::endl;
Node & service = (*itS).second;
/* for(auto itSS = service.Begin(); itSS != service.End(); itSS++)
{
std::cout << " " << (*itSS).first << ": " << (*itSS).second.As<std::string>() << std::endl;
}*/
std::cout << " enabled: " << service["enabled"].As<bool>(false) << std::endl;
std::cout << " name: " << service["name"].As<std::string>() << std::endl;
std::cout << " protocol: " << service["protocol"].As<std::string>() << std::endl;
std::cout << " host: " << service["host"].As<std::string>() << std::endl;
std::cout << " port: " << service["port"].As<unsigned short>(0) << std::endl;
std::cout << " balancing: " << service["balancing"].As<std::string>("No balancing value.") << std::endl;
std::cout << " max_connections: " << service["max_connections"].As<int>(99999) << std::endl;
std::cout << " session: " << service["session"].As<std::string>("No session value.") << std::endl;
Node & nodes = service["nodes"];
if(nodes.IsSequence() == false)
{
throw InternalException("Nodes is not of type sequence.");
}
std::cout << " Nodes:" << std::endl;
//for(size_t i = 0; i < nodes.Size(); i++)
size_t countN = 0;
for(auto itN = nodes.Begin(); itN != nodes.End(); itN++)
{
std::cout << " Node " << countN++ << std::endl;
Node & node = (*itN).second;//nodes[i];
std::cout << " name: " << node["name"].As<std::string>() << std::endl;
std::cout << " protocol: " << node["protocol"].As<std::string>() << std::endl;
std::cout << " host: " << node["host"].As<std::string>() << std::endl;
std::cout << " port: " << node["port"].As<unsigned short>(0) << std::endl;
std::cout << std::endl;
}
std::cout << std::endl;
}
}
catch (const Exception e)
{
std::cout << "Example exception " << e.Type() << ": " << e.what() << std::endl;
return;
}
try
{
Serialize(root, "../bin/out.txt");
}
catch (const Exception e)
{
std::cout << "Exception " << e.Type() << ": " << e.what() << std::endl;
std::cin.get();
}
}