-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
87 lines (74 loc) · 2.03 KB
/
Copy pathparser.cpp
File metadata and controls
87 lines (74 loc) · 2.03 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
#include <algorithm>
#include <sstream>
#include "parser.hpp"
#include "util.hpp"
#include "xString.hpp"
using std::make_unique;
using std::getline, std::hex, std::ifstream;
using std::string, std::stringstream, std::vector;
short Parser::parseShort(const string& text) const {
if (!std::ranges::all_of(text, isdigit))
util::syntaxError(*this);
return static_cast<short>(stoi(text));
}
Parser::Parser(const string& filePath) {
file = make_unique<ifstream>();
file->open(filePath);
}
bool Parser::readNextLine() {
while (getline(*file, line)) {
//Count empty lines too
lineNumber++;
words = xString::split(line, '=', MAX_WORDS);
//Skip empty lines
if (words.empty())
continue;
//Check syntax
if (util::contains(paramKeys, getKey())) {
if (getWordsNumber() != MAX_WORDS)
util::syntaxError(*this);
}
else if (!util::contains(singleKeys, getKey()))
util::syntaxError(*this);
return true;
}
//End of file
return false;
}
std::string Parser::getString(const std::string& otherwise) {
if (getWordsNumber() == 1)
return otherwise;
return words.at(1);
}
Pair<string> Parser::getStrings() {
const vector<string> values = xString::split(getString(), ' ', 2);
if (values.size() < 2)
util::syntaxError(*this);
return {
values.at(0),
values.at(1)
};
}
short Parser::getShort(const short otherwise) {
if (getWordsNumber() == 1)
return otherwise;
return parseShort(getString());
}
Pair<short> Parser::getShorts() {
const vector<string> values = xString::split(getString(), ' ', 2);
if (values.size() < 2)
util::syntaxError(*this);
return {
parseShort(values.at(0)),
parseShort(values.at(1))
};
}
short Parser::getHexColor(const short otherwise) {
if (getWordsNumber() == 1)
return otherwise;
stringstream s;
s << getString();
short out;
s >> hex >> out;
return out;
}