-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniParser.h
99 lines (74 loc) · 2.61 KB
/
IniParser.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
#ifndef INIPARSER_H
#define INIPARSER_H
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include "exceptions.h"
class IniParser {
private:
std::string filename_;
std::map<std::string, std::map<std::string, std::string>> data;
enum class LineType {
// список типов строк
section,
field,
comment,
empty,
unknown
};
static LineType get_line_type (const std::string& line) {
// метод для определения типа строки
if (line.rfind(';') == 0) {
return LineType::comment;
} else if (line.size() == 0) {
return LineType::empty;
} else if (line.find('[') != std::string::npos
&& line.find(']') != std::string::npos
&& line.find('=') == std::string::npos) {
return LineType::section;
} else if (line.find('=') != std::string::npos
&& line.find('[') == std::string::npos
&& line.find(']') == std::string::npos) {
return LineType::field;
} else {
return LineType::unknown;
}
};
static std::vector<std::string> split_line(const std::string& line, const char delimiter) {
// метод для разбиения строки поля на ключ и значение
std::stringstream line_stream(line);
std::string segment;
std::vector<std::string> segments;
while(std::getline(line_stream, segment, delimiter))
{
segments.push_back(segment);
}
if (segments.size() == 2) {
return segments;
} else if (segments.size() == 1){
throw NoValueInLine();
} else {
throw BadValueLine();
}
}
std::string _get_value(const std::string& value_path);
void process_section_line(std::string& line, std::string& current_section);
void process_field_line(std::string& line, const std::string& current_section, const int& line_number);
public:
IniParser(std::string filename);
// деструктр
~IniParser();
// метод для получения значения конкретного поля файла
template <typename VALUE_TYPE>
VALUE_TYPE get_value(std::string value_path);
template <>
std::string get_value(std::string value_path);
template <>
int get_value(std::string value_path);
template <>
float get_value(std::string value_path);
template <>
unsigned short get_value(std::string value_path);
};
#endif // INIPARSER_H