-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_parser.cpp
118 lines (102 loc) · 3.2 KB
/
html_parser.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
#include <bits/stdc++.h>
using namespace std;
class Tag {
public:
string tagname;
unordered_map<string, string> attributes;
unordered_map<string, Tag*> children;
void parseTag(string s) {
if(s[1]=='/') return; // Ignore closing tags
int i = 1; // start from index 1, since index 0 is '<'
// Extracting the tag name
while (i < s.length() && s[i] != ' ' && s[i] != '>') {
tagname += s[i++];
}
// Parsing attributes if present
while (i < s.length()) {
if (s[i] == ' ') {
i++;
string attr, val;
// Parsing attribute name
while (i < s.length() && s[i] != ' ') {
attr += s[i++];
}
// Skipping the ' = "' part
i += 4; // this skips ' = "'
// Parsing attribute value
while (i < s.length() && s[i] != '"') {
val += s[i++];
}
i++; // skip the closing '"'
attributes[attr] = val;
} else {
i++;
}
}
}
};
int main() {
int a, q;
cout << "Enter number of tags:\n";
cin >> a;
cout << "Enter number of queries:\n";
cin >> q;
cin.ignore(); // To ignore newline character after integer input
vector<Tag*> tags(a);
unordered_map<string, Tag*> tagMap;
stack<Tag*> tagStack;
cout << "Enter tags:\n";
for (int i = 0; i < a; i++) {
string t;
getline(cin, t); // Use getline to handle spaces correctly
Tag* newTag = new Tag();
newTag->parseTag(t);
if (t[1] != '/') { // Not a closing tag
if (!tagStack.empty()) {
// Link the current tag to its parent
Tag* parent = tagStack.top();
parent->children[newTag->tagname] = newTag;
}
tagStack.push(newTag);
tagMap[newTag->tagname] = newTag; // Map tagname to the Tag object
} else { // Closing tag
tagStack.pop();
}
tags[i] = newTag;
}
cout << "Enter queries:\n";
for (int i = 0; i < q; i++) {
string query;
cin >> query;
stringstream ss(query);
string tagPath, attr;
// Extracting tag path and attribute name
getline(ss, tagPath, '~');
getline(ss, attr);
stringstream tagStream(tagPath);
string tagName;
Tag* currentTag = nullptr;
bool found = true;
while (getline(tagStream, tagName, '.')) {
if (currentTag == nullptr) {
currentTag = tagMap[tagName];
} else {
currentTag = currentTag->children[tagName];
}
if (currentTag == nullptr) {
found = false;
break;
}
}
if (found && currentTag->attributes.find(attr) != currentTag->attributes.end()) {
cout << currentTag->attributes[attr] << endl;
} else {
cout << "Not Found!" << endl;
}
}
// Free allocated memory
for(auto tag: tags) {
delete tag;
}
return 0;
}