-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleXml.hpp
More file actions
83 lines (77 loc) · 2.52 KB
/
SimpleXml.hpp
File metadata and controls
83 lines (77 loc) · 2.52 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
#pragma once
#include <string>
#include <iostream>
class SimpleXml {
public:
static void skipWhitespace(std::string &xml, size_t &pos) {
while (xml[pos] == ' ' || xml[pos] == '\n') {
pos++;
}
}
static void skipToQuote(std::string &xml, size_t &pos) {
while (xml[pos] != '"') {
pos++;
}
pos++;
}
static void consume(char c, std::string &xml, size_t &pos) {
if (pos >= xml.length()) {
std::cout << "EOF" << std::endl;
exit(1);
}
if (xml[pos] != c) {
std::cout << "Expected " << c << std::endl;
exit(1);
}
pos++;
}
static void consume(const std::string &str, std::string &xml, size_t &pos) {
for (char c: str) {
consume(c, xml, pos);
}
}
static std::tuple<size_t, std::string, std::string, bool> parseBoardXml(std::string &xml, size_t &pos) {
skipWhitespace(xml, pos);
consume("<level number=\"", xml, pos);
size_t levelNr = 0;
while (xml[pos] >= '0' && xml[pos] <= '9') {
levelNr *= 10;
levelNr += xml[pos] - '0';
pos++;
}
consume('"', xml, pos);
skipWhitespace(xml, pos);
bool hasSolution = false;
if (xml[pos] == 's') {
consume("solution=\"", xml, pos);
skipToQuote(xml, pos);
skipWhitespace(xml, pos);
hasSolution = true;
}
if (xml[pos] == 'a') {
consume("author=\"", xml, pos);
skipToQuote(xml, pos);
skipWhitespace(xml, pos);
}
consume("color=\"", xml, pos);
std::string color = "";
while (xml[pos] != '"') {
skipWhitespace(xml, pos);
color += xml[pos];
pos++;
}
pos++;
skipWhitespace(xml, pos);
consume("modifier=\"", xml, pos);
std::string modifier = "";
while (xml[pos] != '"') {
skipWhitespace(xml, pos);
modifier += xml[pos];
pos++;
}
pos++;
skipWhitespace(xml, pos);
consume("/>", xml, pos);
return std::make_tuple(levelNr, color, modifier, hasSolution);
}
};