This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlparse.cpp
188 lines (181 loc) · 6.59 KB
/
xmlparse.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
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
// SPDX-FileCopyrightText: 2011-2012 Tasos Varoudis
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "xmlparse.h"
enum {
STEP_START,
STEP_ELEMENT_NAME,
STEP_ATTRIBUTE_NAME,
STEP_START_ATTRIBUTE_VALUE,
STEP_ATTRIBUTE_VALUE,
STEP_CLOSING
};
bool iscrlf(char c) {
// \n is MAC = 13, UNIX = 10, MS = 13,10
return (c == 10 || c == 13);
}
bool xmlelement::parse(std::ifstream &stream, bool parsesubelements) {
bool closed = false;
bool complete = false;
int step = STEP_START;
std::string buffer;
std::string attribute;
std::string value;
while (!complete && stream) {
char c = static_cast<char>(stream.get());
if (stream) {
switch (step) {
case STEP_START:
if (c == '<') {
step = 1;
buffer.clear();
}
break;
case STEP_ELEMENT_NAME:
if (isspace(c) || iscrlf(c)) {
name = buffer;
buffer.clear();
step = STEP_ATTRIBUTE_NAME;
} else if (c == '/') {
if (buffer.empty()) {
closetag = true;
} else {
name = buffer;
buffer.clear();
step = STEP_CLOSING;
}
} else if (c == '>') {
if (buffer.empty()) {
throw xmlerror("No element name");
} else {
name = buffer;
buffer.clear();
complete = true;
if (closetag) {
closed = true;
} else {
if (parsesubelements) {
closed = subparse(stream);
}
}
}
} else if (isalnum(c) || ispunct(c)) {
buffer += c;
} else {
badcharacter(c, "parsing element name");
}
break;
case STEP_CLOSING:
if (c == '>') {
// only get here if midway through tag
closed = true;
complete = true;
} else if (!isspace(c) && !iscrlf(c)) {
badcharacter(c, "closing element tag");
}
break;
case STEP_ATTRIBUTE_NAME:
if (isspace(c) || iscrlf(c)) {
if (attribute.empty()) {
attribute = buffer;
buffer.clear();
}
} else if (c == '=') {
if (attribute.empty()) {
attribute = buffer;
if (attribute.empty()) {
throw xmlerror("No attribute name");
}
}
buffer.clear();
step = STEP_START_ATTRIBUTE_VALUE;
} else if (c == '/') {
// could be closing a tag without specifying attribute value, but we'll be okay:
step = STEP_CLOSING;
} else if (c == '>') {
complete = true;
// could be closing a tag without specifying attribute value, but we'll be okay:
if (parsesubelements) {
closed = subparse(stream);
}
} else if (isalnum(c) || ispunct(c)) {
buffer += c;
} else {
badcharacter(c, "reading attribute name");
}
break;
case STEP_START_ATTRIBUTE_VALUE:
if (c == '\"') {
step = STEP_ATTRIBUTE_VALUE;
} else if (!isspace(c) && !iscrlf(c)) {
badcharacter(c, "expecting opening '\"'");
}
break;
case STEP_ATTRIBUTE_VALUE:
if (c == '\"') {
attributes[attribute] = buffer;
buffer.clear();
attribute.clear();
step = STEP_ATTRIBUTE_NAME;
} else {
// there was a bad char test for 'isprint(c)', but it didn't like certain
// characters!
buffer += c;
}
break;
}
}
}
if (!complete && step != STEP_START) {
throw xmlerror(std::string("Unexpected end of element ") + name);
}
return closed;
}
void xmlelement::badcharacter(char c, const std::string &location) {
if (isprint(c)) {
throw(std::string("Found '") + c + std::string("' while ") + location);
} else {
std::stringstream s;
s << "Found character " << int(c) << " while " << location;
throw(s.str());
}
}
bool xmlelement::subparse(std::ifstream &stream) {
bool complete = false;
while (!complete && stream) {
subelements.push_back(xmlelement());
if (!subelements.back().parse(stream, true)) {
throw xmlerror(std::string("Unexplained error"));
}
if (subelements.back().closetag) {
if (subelements.back().name == name) {
subelements.pop_back();
complete = true;
} else {
throw xmlerror(std::string("Element <") + name + std::string("> closed with </") +
subelements.back().name + std::string(">"));
}
}
}
if (!complete) {
throw xmlerror(std::string("Unexpected end of element ") + name);
}
return true;
}
std::ostream &operator<<(std::ostream &stream, const xmlelement &elem) {
stream << "<" << elem.name;
std::map<std::string, std::string>::const_iterator it;
for (it = elem.attributes.begin(); it != elem.attributes.end(); it++) {
stream << " " << it->first << "=\"" << it->second << "\" ";
}
if (elem.subelements.size() == 0) {
stream << " />" << std::endl;
} else {
stream << ">" << std::endl;
for (size_t i = 0; i < elem.subelements.size(); i++) {
stream << elem.subelements[i];
}
stream << "</" << elem.name << ">" << std::endl;
}
return stream;
}