-
Notifications
You must be signed in to change notification settings - Fork 1
/
CppXMLConfigReader.cpp
134 lines (122 loc) · 3.2 KB
/
CppXMLConfigReader.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
#include "CppXMLConfigReader.h"
using namespace std;
// Constructor.
CppXMLConfigReader::CppXMLConfigReader(const string& configFileName, const string& applicationSettingsName, const string& rootSectionName)
{
unique_ptr<XMLConfig> mPtr=make_unique<XMLConfig>(configFileName, applicationSettingsName, rootSectionName);
m_XMLConfigManager = move(mPtr);
}
// Initialize the config reader here.
void CppXMLConfigReader::init()
{
try
{
m_XMLConfigManager->readConfigFile();
}
catch(const runtime_error& error)
{
cout << "Failed to initialize the XMLConfigManager. The error is = " << error.what() << endl;
}
if(m_XMLConfigManager->isConfigValid())
{
m_configMapItems = m_XMLConfigManager->getConfigMap();
}
}
// Valid status of the config.
bool CppXMLConfigReader::isConfigValid()
{
return(m_XMLConfigManager->isConfigValid());
}
map<string, string> CppXMLConfigReader::getConfigMap()
{
return(m_configMapItems);
}
// All the specific config type returners implemented here.
int CppXMLConfigReader::getIntValue(const string& key, const int& defaultVal) const
{
int returnVal = defaultVal;
auto iter = m_configMapItems.find(key.c_str());
if(iter != m_configMapItems.end())
{
try
{
returnVal = stoi(iter->second);
}
catch(const invalid_argument& e)
{
cout << "Exception happened while converting " << iter->second << " Into an integer value." << endl;
}
}
return(returnVal);
}
long CppXMLConfigReader::getLongValue(const string& key, const long& defaultVal) const
{
long returnVal = defaultVal;
auto iter = m_configMapItems.find(key.c_str());
if(iter != m_configMapItems.end())
{
try
{
returnVal = stol(iter->second);
}
catch(const invalid_argument& e)
{
cout << "Exception happened while converting " << iter->second << " Into a long value." << endl;
}
}
return(returnVal);
}
float CppXMLConfigReader::getFloatValue(const string& key, const float& defaultVal) const
{
float returnVal = defaultVal;
auto iter = m_configMapItems.find(key.c_str());
if(iter != m_configMapItems.end())
{
try
{
returnVal = stof(iter->second);
}
catch(const invalid_argument& e)
{
cout << "Exception happened while converting " << iter->second << " Into an float value." << endl;
}
}
return(returnVal);
}
double CppXMLConfigReader::getDoubleValue(const string& key, const double& defaultVal) const
{
double returnVal = defaultVal;
auto iter = m_configMapItems.find(key.c_str());
if(iter != m_configMapItems.end())
{
try
{
returnVal = stod(iter->second);
}
catch(const invalid_argument& e)
{
cout << "Exception happened while converting " << iter->second << " Into an double value." << endl;
}
}
return(returnVal);
}
bool CppXMLConfigReader::getBoolValue(const string& key, const bool& defaultVal) const
{
bool returnVal = defaultVal;
auto iter = m_configMapItems.find(key.c_str());
if(iter != m_configMapItems.end())
{
if(iter->second == "true" || iter->second == "1")
returnVal=true;
else
returnVal=false;
}
return(returnVal);
}
string CppXMLConfigReader::getStringValue(const string& key, const string& defaultVal) const
{
auto iter = m_configMapItems.find(key.c_str());
if(iter != m_configMapItems.end())
return(iter->second);
return(defaultVal);
}