-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attribute Parser using C++
101 lines (97 loc) · 3.24 KB
/
Attribute Parser using C++
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<string> getQuery(vector<string> query) {
int l, st;
string buf;
query.clear();
getline(cin, buf);
l = buf.length();
st = 0;
for (int i = 0; i < l; i++) {
if (buf[i] == '.') {
query.push_back(buf.substr(st, i - st));
st = i + 1;
}
else if (buf[i] == '~') {
query.push_back(buf.substr(st, i - st));
st = i;
query.push_back(buf.substr(st, l - st));
}
}
return query;
}
void resolveQuery(vector<string> query, vector<string> listing) {
int lQ = query.size();
int lL = listing.size();
int iQ = 0;
int listS, attL, lineL;
vector<string> currentTagList;
for (int j = 0; j < lL; j++) {
if (listing[j][1] == '/') {
currentTagList.pop_back(); //we remove last tag from the list, valid listing means, we dont need to check what tag it is
}
else {
int k = 1;
while (listing[j][k] != ' ' && listing[j][k] != '>') {
k++;
}
string newTag = listing[j].substr(1, k-1);
currentTagList.push_back(newTag);
if (newTag == query[iQ]) {
iQ++;
if (query[iQ][0] == '~') { //we are on the attribute query, our value is on this line, or nowhere
listS = currentTagList.size();
for (int i = 0; i < listS; i++) {
if (currentTagList[i] != query[i]) {
cout << "Not Found!" << endl;
return;
}
}
attL = query[iQ].length() - 1; //don't count '~'
lineL = listing[j].length();
for (int i = 1; i + attL < lineL; i++) {
if (listing[j].substr(i, attL + 1) == ' ' + query[iQ].substr(1, attL)) {
k = i + attL;
while (listing[j][k] != '"') {
k++;
}
k++;
while (listing[j][k] != '"') {
cout << listing[j][k];
k++;
}
cout << endl;
return;
}
}
cout << "Not Found!" << endl; //since we didn't find <attribute> on this line we can safetly return
return;
}
}
}
}
cout << "Not Found!" << endl; //if we gone through the whole list without finding out tag we do not forget to print the result
return;
}
int main() {
vector<string> listing;
vector<string> query;
int t, q;
string buffer;
cin >> t >> q;
listing.reserve(t);
getline(cin, buffer); //clearing cin from last endl
for (int i = 0; i < t; i++) {
getline(cin, buffer);
listing.push_back(buffer);
}
for (int i = 0; i < q; i++) {
query = getQuery(query);
resolveQuery(query, listing);
}
return 0;
}