-
Notifications
You must be signed in to change notification settings - Fork 3
/
cfg_parser.h
171 lines (140 loc) · 3.69 KB
/
cfg_parser.h
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
/************************************************
Simple Config File Parser
Adapted from web-tutorial by sarmanu: 26 July 2010 - 03:58 AM
http://www.dreamincode.net/forums/topic/183191-create-a-simple-configuration-file-parser/
Edited and modified by Sergey Zhidkov
*************************************************/
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <fstream>
void exitWithError(const std::string& error)
{
std::cout << error;
std::cin.ignore();
std::cin.get();
exit(EXIT_FAILURE);
}
class Convert
{
public:
template <typename T>
static std::string T_to_string(T const &val)
{
std::ostringstream ostr;
ostr << val;
return ostr.str();
}
template <typename T>
static T string_to_T(std::string const &val)
{
std::istringstream istr(val);
T returnVal;
if (!(istr >> returnVal))
exitWithError("CFG: Not a valid " + (std::string)typeid(T).name() + " received!\n");
return returnVal;
}
template <>
static std::string string_to_T(std::string const &val)
{
return val;
}
};
class ConfigFile
{
private:
std::map<std::string, std::string> contents;
std::string fName;
void removeComment(std::string &line) const
{
if (line.find(';') != line.npos)
line.erase(line.find(';'));
}
bool onlyWhitespace(const std::string &line) const
{
return (line.find_first_not_of(' ') == line.npos);
}
bool validLine(const std::string &line) const
{
std::string temp = line;
temp.erase(0, temp.find_first_not_of("\t "));
if (temp[0] == '=')
return false;
for (size_t i = temp.find('=') + 1; i < temp.length(); i++)
if (temp[i] != ' ')
return true;
return false;
}
void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
{
key = line.substr(0, sepPos);
if (key.find('\t') != line.npos || key.find(' ') != line.npos)
key.erase(key.find_first_of("\t "));
}
void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
{
value = line.substr(sepPos + 1);
value.erase(0, value.find_first_not_of("\t "));
value.erase(value.find_last_not_of("\t ") + 1);
}
void extractContents(const std::string &line)
{
std::string temp = line;
temp.erase(0, temp.find_first_not_of("\t "));
size_t sepPos = temp.find('=');
std::string key, value;
extractKey(key, sepPos, temp);
extractValue(value, sepPos, temp);
if (!keyExists(key))
contents.insert(std::pair<std::string, std::string>(key, value));
else
exitWithError("CFG: Can only have unique key names!\n");
}
void parseLine(const std::string &line, size_t const lineNo)
{
if (line.find('=') == line.npos)
exitWithError("CFG: Couldn't find separator on line: " + Convert::T_to_string(lineNo) + "\n");
if (!validLine(line))
exitWithError("CFG: Bad format for line: " + Convert::T_to_string(lineNo) + "\n");
extractContents(line);
}
void ExtractKeys()
{
std::ifstream file;
file.open(fName.c_str());
if (!file)
exitWithError("CFG: File " + fName + " couldn't be found!\n");
std::string line;
size_t lineNo = 0;
while (std::getline(file, line))
{
lineNo++;
std::string temp = line;
if (temp.empty())
continue;
removeComment(temp);
if (onlyWhitespace(temp))
continue;
parseLine(temp, lineNo);
}
file.close();
}
public:
ConfigFile(const std::string &fName)
{
this->fName = fName;
ExtractKeys();
}
bool keyExists(const std::string &key) const
{
return contents.find(key) != contents.end();
}
template <typename ValueType>
ValueType getValueOfKey(const std::string &key, ValueType const &defaultValue = ValueType()) const
{
if (!keyExists(key))
return defaultValue;
return Convert::string_to_T<ValueType>(contents.find(key)->second);
}
};