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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#ifndef RSS_H
#define RSS_H
#include <cstddef>
#include <ctime>
#include <memory>
#include <regex>
#include <stack>
#include <stdexcept>
#include <string>
#include <iostream>
#include <utility>
#include <variant>
#include <vector>
class XML_leaf {
public:
std::string Raw;
std::string Name;
std::string Value = "";
std::vector<XML_leaf> leafs{};
XML_leaf(std::string raw, std::string value) {
Raw = raw;
Name = getTag(raw);
Value = value;
};
XML_leaf(std::string raw) {
// The smallest valid (?) XML element is <></>
if (raw.length() < 5 || raw[0] != '<')
throw std::runtime_error("The XML " + raw + " is invalid");
auto tokens = TokenizeXML(trimSpaces(raw));
if (tokens.size() < 3 || !isTag(tokens[0]))
throw new std::runtime_error("Invalid XML");
auto result = std::make_unique<XML_leaf>("<>", "");
std::stack<std::unique_ptr<XML_leaf>> stack{};
// Building a tree
for (size_t i = 1; i < tokens.size(); i++) {
const auto currentToken = tokens[i];
if (!isTag(currentToken)) {
if (stack.size() == 0)
continue;
stack.top()->setValue(currentToken);
continue;
}
if (stack.size() == 0 || !isClosingTagOf(currentToken, stack.top()->Raw)) {
if (IsSelfClosingTag(currentToken)) {
stack.top()->leafs.push_back(std::move(XML_leaf(currentToken, "")));
continue;
}
stack.push(std::make_unique<XML_leaf>(currentToken, ""));
continue;
}
auto complete = std::move(stack.top());
stack.pop();
if (stack.size() == 0) { // The node is closing a doc
result.swap(complete);
break;
}
stack.top()->leafs.push_back(std::move(*complete));
}
this->Raw = result->Raw;
this->Name = result->Name;
this->Value = result->Value;
this->leafs = result->leafs;
}
XML_leaf GetChild(std::string query) {
for (auto child : leafs) {
// std::cout << "Comparing " << child.Raw << " and " << query << std::endl;
if (child.Name == query)
return child;
}
return XML_leaf("<>", "empty");
}
std::vector<XML_leaf> GetChildren(std::string query) {
std::vector<XML_leaf> outp = {};
for (auto child : leafs) {
// std::cout << "Comparing " << child.Raw << " and " << query << std::endl;
if (child.Name == query)
outp.push_back(child);
}
return outp;
}
std::string GetValue() {
if (isCDATA(Value))
return getCDATA(Value, true);
return Value;
}
private:
void setValue(std::string newValue) {
Value = newValue;
}
static std::string trimSpaces(std::string raw) {
auto outp = std::regex_replace(raw, std::regex("\n"), "");
outp = std::regex_replace(outp, std::regex("> <"), "><");
outp = std::regex_replace(outp, std::regex(" +"), " ");
return outp;
}
static std::vector<std::string> TokenizeXML(std::string raw) {
std::vector<std::string> outp{};
// std::cout << "Called TokenizeXML()" << '\n';
ulong idx = 0;
while (idx < raw.length()) {
char ptr = raw[idx];
ulong next_idx = 0;
if (ptr == '<') {
if (isCDATA(raw.substr(idx, raw.length() - idx)))
next_idx = raw.find("]]>", idx) + 2;
else
next_idx = raw.find('>', idx);
} else {
next_idx = raw.find('<', idx) - 1;
}
outp.push_back(raw.substr(idx, (next_idx - idx) + 1));
idx = next_idx + 1;
}
return outp;
};
static bool isTag(std::string token){
return token.length() > 2
&& token[0] == '<'
&& token[token.length() - 1] == '>'
&& token[1] != '!';
}
static bool isCDATA(std::string token) {
return token.find("<![CDATA[") == 0;
}
// Assumes the string is CDATA
static std::string getCDATA(std::string token, bool toRawString = false) {
// 9 = "<!CDATA[".length
// 3 = "]]>".length
if (token.length() < 9 + 3) return token;
auto data = token.substr(9, token.length() - (9+3));
if (toRawString) {
data = std::regex_replace(data, std::regex("<[a-zA-Z/]+>"), "");
}
return data;
}
static bool isClosingTagOf(std::string token, std::string opening_tag){
if (!(token.length() > 3
&& token[0] == '<'
&& token[1] == '/'
&& token[token.length() - 1] == '>'))
return false;
if (!isTag(opening_tag)) return false;
return getTag(token).substr(1, token.length() - 1) == getTag(opening_tag);
}
static bool IsSelfClosingTag(std::string token) {
return token.length() > 3
&& token[0] == '<'
&& token[token.length() - 1] == '>'
&& token[token.length() - 2] == '/';
}
static std::string getTag(std::string full) {
if (!isTag(full)) return full;
auto space_idx = full.find_first_of(' ');
if (space_idx == std::variant_npos) {
return full.substr(1, full.length() - 2);
} else {
return full.substr(1, space_idx - 1);
}
}
};
struct ChannelInfo {
std::string Title = "";
std::string URL = "";
ChannelInfo(std::string url) {
URL = url;
};
ChannelInfo() {
};
void print() {
std::cout << Title <<'(' << URL << ")\n";
}
};
struct RSS_Entry {
std::string URL = "";
std::string Title = "";
std::string Contents = "";
std::tm pubDate = {};
RSS_Entry(XML_leaf node);
void print();
};
class RSS {
public:
std::string URL;
ChannelInfo channelInfo;
std::vector<RSS_Entry> Entries = {};
RSS(std::string url);
void print();
void print_latest(int minutesSpan);
private:
std::string request();
void parse(std::string contents);
};
#endif // RSS_H
|