This repository was archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_parser.cpp
More file actions
115 lines (108 loc) · 2.72 KB
/
xml_parser.cpp
File metadata and controls
115 lines (108 loc) · 2.72 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
#include <iostream>
#include <map>
#include "xml_parser.h"
#include "io.h"
std::map<char, std::string> *XmlParser::parse_attributes() {
std::map<char, std::string> *attributeMap = new std::map<char, std::string>();
char key;
bool readingValueString = false;
substring.erase();
while (index < text->length()) {
c = (*text)[index++];
switch (c) {
case ' ':
if (!readingValueString) {
// New attribute coming up
substring.erase();
} else {
// The space is part of a string value
substring += c;
}
break;
case '=':
key = key_from_string(&substring);
break;
case '"':
if (readingValueString) {
(*attributeMap)[key] = substring;
readingValueString = false;
} else {
readingValueString = true;
substring.erase();
}
break;
case '>':
// Closing bracket found
substring.erase();
return attributeMap;
default:
substring += c;
break;
}
}
std::cout << IO_RED "ERROR: XmlParser::parse_attributes()"
<< IO_NORMAL << std::endl;
return attributeMap;
}
void XmlParser::parse_error() {
std::map<char, std::string> *attributeMap;
attributeMap = parse_attributes();
errorShouldRetry = (*attributeMap)[ATTR_ERROR_SHOULD_RETRY];
}
void XmlParser::parse_element_open() {
substring.erase();
c = (*text)[index++];
if (c == '/') {
// Element closing
substring.erase();
while (index < text->length()) {
c = (*text)[index++];
// Is it an element we care about?
if (c == ' ' || c == '>') {
if (!substring.compare("Error")) {
// Error complete
errorText = pText;
std::cout << IO_RED "ERROR: "
<< errorText << IO_NORMAL
<< "Should Retry? " << errorShouldRetry << std::endl;
}
element_close_actions();
substring.erase();
break;
} else {
substring += c;
}
}
} else {
// Element opening
substring.erase();
substring += c;
while (index < text->length()) {
c = (*text)[index++];
// Is it an element we care about?
if (c == ' ' || c == '>') {
if (!substring.compare("Error")) {
// Received Error from NextBus API
parse_error();
}
element_open_actions();
substring.erase();
break;
} else {
substring += c;
pText.erase();
}
}
}
}
void XmlParser::parse() {
while (index < text->length()) {
c = (*text)[index++];
if (c == '<') {
// Found an opening bracket
parse_element_open();
} else {
pText += c;
}
}
}