-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathConfigParser.h
More file actions
213 lines (165 loc) · 6.42 KB
/
Copy pathConfigParser.h
File metadata and controls
213 lines (165 loc) · 6.42 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Created by romanov on 4/28/16.
//
#ifndef RCDB_CPP_CONFIGPARSER_H
#define RCDB_CPP_CONFIGPARSER_H
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include "StringUtils.h"
namespace rcdb
{
class ConfigSection
{
public:
int SlotNumber = -1;
std::string Name;
std::vector<std::vector<std::string> > Rows;
std::map<std::string, std::string> NameValues;
std::map<std::string, std::vector<std::string>> NameVectors;
};
class SlotSection
{
public:
std::string FullLine;
std::string Name;
int SlotNumber;
};
//Result of parsing the config file
class ConfigFileParseResult
{
public:
ConfigFileParseResult(std::vector<std::string> sectionNames)
{
for(auto sectionName: sectionNames) {
this->SectionNames.push_back(sectionName);
}
}
std::vector<std::string> SectionNames;
std::map<std::string, rcdb::ConfigSection> Sections;
std::map<int, rcdb::ConfigSection> SectionsBySlotNumber; /// If it is sectioned file
std::vector<std::string> FoundSectionNames;
};
class ConfigParser
{
public:
static ConfigFileParseResult Parse(std::string content, std::vector<std::string> awaitedSections)
{
auto lines = StringUtils::Split(content, "\n");
return Parse(lines, awaitedSections);
}
static std::vector<rcdb::SlotSection> FindSlotSections(std::vector<std::string> lines, std::string slotSectionStart)
{
using namespace std;
std::vector<rcdb::SlotSection> result;
for(string line : lines) {
//trim the line
line.erase(line.find_last_not_of(" \n\r\t") + 1);
//Skip comments
if (line.find("#") == 0 ||line.find("----") == 0 || line.find("====") == 0) {
continue;
}
// Split tokens by lexical rules
std::vector<std::string> tokens = StringUtils::LexicalSplit(line, true);
for(string& token:tokens) {
StringUtils::trim(token);
}
// Skip if there is no tokens
if (tokens.size() == 0 || tokens[0] == "") continue;
if(slotSectionStart == tokens[0] && tokens.size()>1) {
SlotSection section;
section.FullLine = line;
section.Name = slotSectionStart;
section.SlotNumber = std::stoi(tokens[1]);
result.push_back(section);
}
}
return result;
}
static ConfigFileParseResult Parse(std::vector<std::string> lines, std::vector<std::string> awaitedSections, bool sectionMayContainSpace=false)
{
using namespace std;
auto result = ConfigFileParseResult(awaitedSections);
auto currentSection = ConfigSection();
currentSection.Name = string("");
for(string line : lines)
{
//trim the line
line.erase(line.find_last_not_of(" \n\r\t")+1);
//Skip comments
if (line.find("#") == 0 ||line.find("----") == 0 || line.find("====") == 0) {
continue;
}
// Split tokens by lexical rules
std::vector<std::string> tokens = StringUtils::LexicalSplit(line, true);
for(string& token:tokens) {
StringUtils::trim(token);
}
// Skip if there is no tokens
if (tokens.size() == 0 || tokens[0] == "") continue;
bool isSection = false;
for(auto awaitedSection: awaitedSections) {
if(awaitedSection == (sectionMayContainSpace? line:tokens[0])) {
isSection = true;
break;
}
}
// Is this a section name?
if (isSection) {
// Yehoooo new section! Save the old one!
if (currentSection.Name != "") {
result.Sections[currentSection.Name] = currentSection;
}
currentSection = ConfigSection();
currentSection.Name = (sectionMayContainSpace? line:tokens[0]);
result.FoundSectionNames.push_back(currentSection.Name);
continue;
}
currentSection.Rows.push_back(tokens);
// We have a name-value pair
if (tokens.size() == 2)
{
currentSection.NameValues[tokens[0]] = tokens[1];
}
// We have a name-value pair
if (tokens.size() > 2)
{
vector<string> values;
for(size_t i=1; i<tokens.size(); i++ ){
values.push_back(tokens[i]);
}
currentSection.NameVectors[tokens[0]] = values;
}
}
// Save the section we filled
if (currentSection.Name != "") {
result.Sections[currentSection.Name] = currentSection;
}
return result;
}
static ConfigFileParseResult ParseWithSlots(std::string content, std::string slotPrefix)
{
auto lines = StringUtils::Split(content, "\n");
return ParseWithSlots(lines, slotPrefix);
}
static ConfigFileParseResult ParseWithSlots(std::vector<std::string> lines, std::string slotPrefix)
{
using namespace std;
auto sections = FindSlotSections(lines, slotPrefix);
vector<string> sectionNames;
//First auto find sections
for (uint i = 0; i < sections.size(); ++i) {
sectionNames.push_back(sections[i].FullLine);
}
// Parse file the usual way
auto result = Parse(lines, sectionNames, true);
// Update parse result
for (uint i = 0; i < sections.size(); ++i) {
result.SectionsBySlotNumber[sections[i].SlotNumber] = result.Sections[sections[i].FullLine];
}
return result;
}
};
}
#endif //RCDB_CPP_CONFIGPARSER_H