-
Notifications
You must be signed in to change notification settings - Fork 0
/
iniParse.cpp
99 lines (75 loc) · 1.73 KB
/
iniParse.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
#include "iniParse.h"
#include <iostream>
#include <fstream>
using std::cout;
iniParse::iniParse(string filename)
{
fileName = filename;
read();
}
void iniParse::read()
{
cout << "Czytam plik konfiguracyjny: " << fileName << "\n";
std::fstream file(fileName);
string configLine;
while(std::getline(file, configLine))
{
trimm(&configLine);
cout << "Czytam linijke: " << configLine << ".\n";
if(configLine[0] == ';')
continue;
else if(configLine[0] == '[')
{
sectionName = configLine.substr(1, configLine.length()-2);
cout << "Znaleziono sekcje: " << sectionName << ".\n";
}
else
{
size_t found = configLine.find('=');
if(found != configLine.npos)
{
StoreValue(configLine, found);
}
}
}
}
void iniParse::trimm(string *s)
{
size_t found = s->find_first_not_of(" \t");
if(found != s->npos)
*s = s->substr(found);
found = s->find_last_not_of(" \t");
if(found != s->npos)
*s = s->substr(0, found+1);
}
void iniParse::StoreValue(string lineWithValue, size_t delimiterPos)
{
string key = makeKey(lineWithValue.substr(0,delimiterPos));
string value = lineWithValue.substr(delimiterPos+1);
cout << "Zapisze wartosc: " << value << " pod kluczem: " << key << ".\n";
configuration.insert(pair<string,string>(key,value));
}
string iniParse::makeKey(string propName)
{
string ret = sectionName + "." + propName;
return ret;
}
int iniParse::getValue(string section, string property, int defaultValue)
{
string key = makeKey(section, property);
int ret;
try
{
ret = std::stoi(configuration.at(key));
}
catch(const std::out_of_range)
{
ret = defaultValue;
}
return ret;
}
string iniParse::makeKey(string section, string property)
{
string ret = section + "." + property;
return ret;
}