-
Notifications
You must be signed in to change notification settings - Fork 0
/
exmlparser.cpp
executable file
·112 lines (107 loc) · 3.14 KB
/
exmlparser.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
104
105
106
107
108
109
110
111
112
#include "exmlparser.h"
#include <fstream>
#include <cctype>
bool match(const std::string& str, const std::string& line,
const int oldIndex, int& newIndex) {
const int ls = line.size();
const int iMax = str.size();
newIndex = oldIndex;
for(int i = 0; i < iMax; i++, newIndex++) {
const int lineIndex = oldIndex + i;
if(lineIndex >= ls) return false;
const auto strC = str[i];
const auto lineC = line[lineIndex];
if(strC != lineC) return false;
}
return true;
}
void skipSpaces(const std::string& line, int& index) {
const int ls = line.size();
if(index >= ls) return;
while(line[index] == ' ') {
index++;
if(index >= ls) break;
}
}
bool readId(const std::string& line,
const int oldIndex,
int& newIndex,
int& id) {
const int ls = line.size();
newIndex = oldIndex;
if(newIndex >= ls) return false;
{
const bool q = line[newIndex++] == '"';
if(!q) return false;
}
if(newIndex >= ls) return false;
std::string numberStr;
while(newIndex < ls) {
const auto c = line[newIndex];
if(c == '"') break;
if(!std::isdigit(c)) return false;
numberStr = numberStr + c;
newIndex++;
}
id = std::stoi(numberStr);
{
if(newIndex >= ls) return false;
const bool q = line[newIndex++] == '"';
if(!q) return false;
}
return true;
}
bool eXmlParser::sParse(eTextStrings& strings,
const std::string& filePath) {
std::ifstream file(filePath);
if(!file.good()) {
printf("File missing %s\n", filePath.c_str());
return false;
}
eTextGroup* group = nullptr;
std::string line;
while(std::getline(file, line)) {
if(line.empty()) continue;
const int ls = line.size();
int index = 0;
{
skipSpaces(line, index);
if(index >= ls) continue;
}
{
int newIndex;
const bool isGroup = match("<group id=", line, index, newIndex);
if(isGroup) {
index = newIndex;
if(index >= ls) continue;
int id;
const bool r = readId(line, index, newIndex, id);
if(!r) continue;
index = newIndex;
if(index >= ls) continue;
group = &strings[id];
continue;
}
}
{
int newIndex;
const bool isString = match("<string id=", line, index, newIndex);
if(isString) {
index = newIndex;
if(index >= ls) continue;
int id;
const bool r = readId(line, index, newIndex, id);
if(!r) continue;
index = newIndex + 1;
if(index >= ls) continue;
if(!group) continue;
auto& str = (*group)[id];
while(index < ls && line[index] != '<') {
str.push_back(line[index++]);
}
continue;
}
}
}
return true;
}