-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniParser.cpp
103 lines (91 loc) · 2.06 KB
/
IniParser.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
using namespace std;
string configPath = "config.ini";
class SimpleIniFileParser
{
public:
SimpleIniFileParser() { }
SimpleIniFileParser(const string& iniPath)
{
this->loadFromFile(iniPath);
}
void loadFromFile(const string& iniPath)
{
this->parameters.clear();
ifstream fin(iniPath.c_str());
string line;
while (getline(fin, line))
{
this->processLine(line);
}
}
string toString()
{
string out;
for (map<string, int>::const_iterator itr = this->parameters.begin();
itr != this->parameters.end();
++itr)
{
if (itr != this->parameters.begin())
{
out += "\n";
}
out += "parameters[" + itr->first + "] = " + to_string(itr->second);
}
return out;
}
private:
map<string, int> parameters;
static std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
static std::string trim(std::string& str)
{
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ') + 1); //surfixing spaces
return str;
}
void processLine(const string& line)
{
vector<string> tokens = split(line, '=');
if (tokens.size() != 2)
{
return;
}
this->parameters[trim(tokens[0])] = stoi(tokens[1]);
}
};
void writeConfigFile(const string &iniPath)
{
ofstream fout(iniPath.c_str());
fout << "BatteryConsumptionRate = 1" << endl;
fout << "MaxSteps = 1200" << endl;
fout << "MaxStepsAfterWinner=200" << endl;
fout << "BatteryCapacity=400" << endl;
fout << "BatteryRachargeRate=20" << endl;
fout.close();
ifstream fin(iniPath.c_str());
string line;
cout << "Original config ini file: " << endl;
while (getline(fin, line))
{
cout << line << endl;
}
}
int run()
{
SimpleIniFileParser iniParser(iniPath);
cout << endl << endl << "Parameters read:" << endl;
cout << iniParser.toString() << endl;
return 0;
}