-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigManager.cpp
102 lines (89 loc) · 1.64 KB
/
ConfigManager.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
#define _CRT_SECURE_NO_WARNINGS
#include "ConfigManager.h"
using namespace rapidjson;
ConfigManager& ConfigManager::getInstance()
{
static ConfigManager instance;
return instance;
}
ConfigManager::ConfigManager()
{
try
{
init();
readConfig();
}
catch (FileOpeningError &err)
{
std::cout << err.what() << std::endl;
char c;
std::cin >> c;
exit(EXIT_FAILURE);
}
}
ConfigManager::~ConfigManager()
{
if (_configFile != nullptr)
{
fclose(_configFile);
}
}
void ConfigManager::init()
{
_configFile = fopen(_fileName.c_str(), "r");
if (!_configFile)
{
throw FileOpeningError();
}
}
void ConfigManager::readConfig()
{
char readBuffer[65536];
FileReadStream is(_configFile, readBuffer, sizeof(readBuffer));
ParseResult error = _config.ParseStream(is);
if (!error)
{
std::cout << "JSON parse error: " << rapidjson::GetParseError_En(error.Code()) << " " << error.Offset() << std::endl;
char c;
std::cin >> c;
exit(EXIT_FAILURE);
}
}
int ConfigManager::getIntegerValue(std::string field)
{
try
{
Type type = _config[field.c_str()].GetType();
if (type != kNumberType)
{
throw GetIntValueError(field);
}
}
catch (GetIntValueError &err)
{
std::cout << err.what();
char c;
std::cin >> c;
exit(EXIT_FAILURE);
}
return _config[field.c_str()].GetInt();
}
std::string ConfigManager::getStringValue(std::string field)
{
try
{
Type type = _config[field.c_str()].GetType();
if (type != kStringType)
{
throw GetStringValueError(field);
}
}
catch (GetStringValueError &err)
{
std::cout << err.what();
char c;
std::cin >> c;
exit(EXIT_FAILURE);
}
return _config[field.c_str()].GetString();
}