-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
123 lines (101 loc) · 2.5 KB
/
Copy pathconfig.cpp
File metadata and controls
123 lines (101 loc) · 2.5 KB
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
#include "stdafx.h"
#include "config.h"
#include "libs/pugixml/src/pugixml.hpp"
Config::Config(const char* source, Log* log)
{
this->log = log;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(source);
if (result.status == 1)
{
std::string sourceString = std::string(source);
log->log("Config error. Cannot open " + sourceString);
log->log("Program exited due to config error");
delete log;
delete this;
exit(1);
}
secondsToEat = (unsigned)doc.child("config").child("secondsToEat").text().as_int();
secondsToThink = (unsigned)doc.child("config").child("secondsToThink").text().as_int();
outputPeriod = (unsigned)doc.child("config").child("outputPeriod").text().as_int();
timeOut = (unsigned)doc.child("config").child("timeOut").text().as_int();
outputFilePath = doc.child("config").child("outputFile").text().as_string();
for (pugi::xml_node philosofer : doc.child("config").child("philosofers").children("philosofer"))
{
std::string philosoferName = philosofer.text().as_string();
philosoferNames.push_back(philosoferName);
}
validate();
}
void Config::validate()
{
bool error = false;
if (secondsToEat < 1)
{
log->log("Config error. SecondsToEat tag should present and be 1 or more");
error = true;
}
if (secondsToThink < 1)
{
log->log("Config error. SecondsToThink tag should present and be 1 or more");
error = true;
}
if (outputPeriod < 1)
{
log->log("Config error. OutputPeriod tag should present and be 1 or more");
error = true;
}
if (timeOut < 1)
{
log->log("Config error. Timeout tag should present and be 1 or more");
error = true;
}
if (outputFilePath == "")
{
log->log("Config error. outputFilePath tag should present");
error = true;
}
if (philosoferNames.size() < 1)
{
log->log("Config error. Philosofers tag should contain at least 1 philosofer tag");
error = true;
}
for (std::string philosoferName : philosoferNames)
{
if (philosoferName == "")
{
log->log("Config error. Each philosofer should have a name");
error = true;
break;
}
}
if (error)
{
log->log("Program exited due to config error");
exit(1);
}
}
unsigned Config::getOutputPeriod()
{
return outputPeriod;
}
unsigned Config::getTimeOut()
{
return timeOut;
}
unsigned Config::getSecondsToEat()
{
return secondsToEat;
}
unsigned Config::getSecondsToThink()
{
return secondsToThink;
}
std::string Config::getOutputFilePath()
{
return outputFilePath;
}
std::vector<std::string> Config::getPhilosoferNames()
{
return philosoferNames;
}