Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
264 lines (236 loc) · 7.74 KB
/
Copy pathmain.cpp
File metadata and controls
264 lines (236 loc) · 7.74 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* File: main.cpp
* Author: Furkancan Bilal Yüce
* Date: 11.02.2024
* Description: This project offers a solution to 'Attribute Parser' problem in HackerRank.
*/
#include "logger.hpp"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <optional>
#include <regex>
#include <stack>
#include <vector>
Logger logger;
// Structure to represent a tag in the HRML document
struct Tag
{
Tag* parent;
std::map<std::string, Tag> childrens;
std::map<std::string, std::string> attributes;
std::optional<std::string> getAttribute(const std::vector<std::string>& tags,
const std::string& attributeName) const
{
if (tags.empty())
{
auto it = attributes.find(attributeName);
if (it != attributes.end())
{
return it->second;
}
return std::nullopt; // Attribute not found
}
auto next_tag = tags.front();
auto it = childrens.find(next_tag);
if (it == childrens.end())
{
logger.warning("Tag could not found in childrens, tag: " + next_tag);
return std::nullopt;
}
std::vector<std::string> childTags(tags.begin() + 1, tags.end());
return childrens.at(next_tag).getAttribute(childTags, attributeName);
}
};
// Class to handle HRML parsing and queries
class HRMLEngine
{
public:
// A map to store all the Tags
std::map<std::string, Tag> tag_map;
// A stack to store state of nested tags
std::stack<std::string> tag_stack;
// Function to parse HRML input and construct tag hierarchy
void parseHRML(int line_count)
{
Tag* parentTag{nullptr};
Tag* currentTag{nullptr};
// Iterate over inputs from std::cin
for (int i = 0; i < line_count; ++i)
{
std::string line;
std::getline(std::cin, line);
// Use regex to extract tag names from each line of HRML input.
std::smatch match;
if (std::regex_search(line, match, tag_pattern))
{
std::string tag_name = match.str(1);
logger.info("Tag created: " + tag_name);
if (tag_stack.empty())
{
tag_map[tag_name] = Tag{};
tag_map[tag_name].parent = nullptr;
currentTag = &tag_map[tag_name];
// If there is no tags in stack, then parent tag should be
// current tag
parentTag = currentTag;
}
else
{
auto parent_name = tag_stack.top();
parentTag->childrens[tag_name] = Tag{};
parentTag->childrens[tag_name].parent = parentTag;
currentTag = &parentTag->childrens[tag_name];
}
tag_stack.push(tag_name);
// Use regex to extract attributes and values from each line of
// HRML input.
if (std::regex_search(line, match, attribute_pattern))
{
std::smatch match;
std::string::const_iterator searchStart(line.cbegin());
while (std::regex_search(searchStart, line.cend(), match, attribute_pattern))
{
auto attribute_name = match[1];
auto attribute_value = match[2];
currentTag->attributes[attribute_name] = attribute_value;
searchStart = match.suffix().first;
logger.info("Attribute called " + attribute_name.str() +
" is created with value of " + attribute_value.str());
}
}
else
{
logger.warning("No attribute found for the tag called " + tag_name);
}
parentTag = currentTag;
}
else
{
if (tag_stack.empty())
{
throw std::runtime_error("Closing tag found without opening tag");
}
std::smatch match;
if (std::regex_search(line, match, tag_end_pattern))
{
std::string tag_name = match.str(1);
logger.info("Tag closed: " + tag_name);
tag_stack.pop();
// If tag is closed then parentTag should be currentTag's
// parent(one level up)
parentTag = currentTag->parent;
}
else
{
logger.warning("No tag found for the line called " + line);
}
}
}
}
// Function to process queries and print attribute values
void processQueries(int line_count)
{
for (int i = 0; i < line_count; ++i)
{
std::string line;
std::getline(std::cin, line);
std::vector<std::string> tokens = tokenize(line, '~');
if (tokens.size() != 2)
{
throw std::runtime_error("Invalid input format: ");
}
std::vector<std::string> tags = tokenize(tokens[0], '.');
std::string attribute = tokens[1];
if (auto att = getAttribute(tags, attribute))
{
std::cout << *att << "\n";
}
else
{
std::cout << "Not Found!\n";
}
}
}
private:
// Function to parse a string into tokens based on a delimiter
std::vector<std::string> tokenize(const std::string& str, char delimiter)
{
using namespace std;
vector<string> tokens;
istringstream iss(str);
string token;
while (getline(iss, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
std::optional<std::string> getAttribute(const std::vector<std::string>& tags,
const std::string& attributeName)
{
if (tags.empty())
{
return std::nullopt; // No tags provided
}
auto& parentTag = tag_map[tags.front()];
if (tags.size() == 1)
{
return parentTag.getAttribute({}, attributeName);
}
std::vector<std::string> childTags(tags.begin() + 1, tags.end());
return parentTag.getAttribute(childTags, attributeName);
}
private:
const std::regex tag_pattern{"<(\\w+)"}; // Check for "<tag..."
const std::regex tag_end_pattern{"</(\\w+)>"}; // Check for "</tag..."
const std::regex attribute_pattern{
"(\\w+)\\s*=\\s*['\"]([^'\"]*)['\"]"}; // Check for attributes
};
void parseArguments(int argc, char* argv[])
{
if (argc != 3)
{
Logger::currentLogLevel = LogLevel::OFF;
return;
}
std::string opt = argv[1];
if (opt == "-l")
{
std::string level = argv[2];
if (level == "i")
{
Logger::currentLogLevel = LogLevel::INFO;
}
else if (level == "w")
{
Logger::currentLogLevel = LogLevel::WARNING;
}
else
{
Logger::currentLogLevel = LogLevel::OFF;
}
}
}
int main(int argc, char* argv[])
{
using namespace std;
parseArguments(argc, argv);
int N{}, Q{};
cin >> N >> Q;
cin.ignore();
HRMLEngine hrml;
try
{
hrml.parseHRML(N);
hrml.processQueries(Q);
}
catch (const exception& e)
{
cerr << e.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}