-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattr_parser.cpp
143 lines (121 loc) · 3.74 KB
/
attr_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
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#define TEST
using namespace std;
vector<string> split_string(string);
int main() {
vector<string> result;
//take a line from user input
//parse no of lines , parse no of queries
int no_lines = 0;
int no_queries = 0;
string infoLine;
#ifdef TEST
ifstream myfile("input.txt");
if(myfile.is_open()){
getline(myfile, infoLine);
}
#else
getline(cin, infoLine);
#endif
vector<string> infos = split_string(infoLine);
no_lines = stoi(infos.at(0));
no_queries = stoi(infos.at(1));
// take input lines from user
vector<string> lines(no_lines);
for(uint8_t i=0; i<lines.size(); i++){
string aLine;
#ifdef TEST
if(myfile.is_open()){
getline(myfile, aLine);
}
#else
getline(cin, aLine);
#endif
lines.at(i) = aLine;
}
//iterate in a for loop , parse & process each query
for(uint8_t i=0; i<no_queries; i++)
{
string aQuery;
#ifdef TEST
if(myfile.is_open()){
getline(myfile, aQuery);
}
#else
getline(cin, aQuery);
#endif
string tag2find;
//dissect the query
//parse until the '~' character
size_t tildePos = aQuery.find_last_of("~");
if(tildePos != std::string::npos){
string till_tilde = aQuery.substr(0, tildePos);
size_t dotPos = till_tilde.find_last_of(".");
if(dotPos != std::string::npos){
tag2find = till_tilde.substr(dotPos+1);
}//indexed tag query
else{
tag2find = till_tilde;
}
string attr2find = aQuery.substr(tildePos+1);
// cout << "-------------------" << endl;
// cout << tag2find << "," << attr2find << endl;
// cout << "-------------------" << endl;
bool found = false;
for(uint8_t i=0; i<lines.size(); i++){
if(lines.at(i).find(tag2find) != std::string::npos)
{
//cout << "$tag: " << tag2find << "Found in: " << lines.at(i) << endl;
//attr should be in this line
size_t attrStart = lines.at(i).find(attr2find);
if(attrStart != std::string::npos){
string atrrValueStr = lines.at(i).substr(attrStart+1);
//cout << "atrrValueStr:" << atrrValueStr << endl;
size_t firstQt = atrrValueStr.find_first_of("\"");
string afterFirstQt = atrrValueStr.substr(firstQt+1);
size_t secondQt = afterFirstQt.find_first_of("\"");
string resultValue = afterFirstQt.substr(0, secondQt);
//cout << resultValue << endl;
result.push_back(resultValue);
found = true;
}// attr is found
}// tag is found
}//iterate thru each input line to find the value
if(!found){
result.push_back("Not Found!");
}//if value is not found
}//valid query
}//iterate thru each query
#ifdef TEST
myfile.close();
#endif
//cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" <<endl;
for(auto eachElem:result){
cout << eachElem << endl;
}
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}