-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (124 loc) · 3.46 KB
/
main.cpp
File metadata and controls
146 lines (124 loc) · 3.46 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
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
namespace fs = std::filesystem;
#include "in_stream.hpp"
#include "tag_tree.hpp"
//https://www.hackerrank.com/challenges/attribute-parser/problem
void print_usage(const std::string &prg_name)
{
std::cout << "Usage: " << prg_name << " [--parser|-p <parser type>] <input filename>" << std::endl;
std::cout << R"VVV(
<parser type> could be "tag_tree" or "in_stream"
various algorithms for finding attribute value
<input filename> file has following format:
Lines in HRML format
Empty line
Lines with queries in following format
<tag>.<tag>.<tag>~<attribute>
Empty line
)VVV";
std::cout << "HRML is artifical markup language. Each line has only one opening or closing tag."
" Opening tags can also contain attributes in form <attribute name> = \"<attribute value>\"."
" Tags can be nested.";
std::cout << R"VVV(
For example:
<tag1 value = "Hello World">
<tag2 name = "Name1">
</tag2>
<tag3 another="another" final="final">
</tag3>
</tag1>
tag1.tag2~name
tag1~name
tag1~value
output should be:
tag1.tag2~name="Name1"
tag1~name not found
tag1~value="Hello World"
)VVV";
}
struct Input
{
std::stringstream text_stream;
std::stringstream query_stream;
};
struct Config
{
std::string parser{"tag_tree"};
std::string filename;
};
Input prepare_input(const Config &config)
{
Input input;
std::ifstream input_file(config.filename);
if (!input_file)
throw std::runtime_error(std::string("Error opening file \"") + config.filename + "\" for reading");
std::string line;
while (getline(input_file, line) && !line.empty())
input.text_stream << line << std::endl;
while (getline(input_file, line) && !line.empty())
input.query_stream << line << std::endl;
return input;
}
std::optional<Config> make_config(int argc, char **argv)
{
auto prg_name = fs::path(argv[0]).filename();
Config config;
if (argc < 2)
{
print_usage(prg_name);
return std::nullopt;
}
for (int i = 1; i < argc; i++)
{
std::string_view arg(argv[i]);
if (arg == "--parser" || arg == "-p")
{
config.parser = argv[++i];
}
else if (arg == "--help" || arg == "-h")
{
print_usage(prg_name);
return std::nullopt;
}
else
{
config.filename = arg;
}
}
return {config};
}
std::unique_ptr<ITagValue> make_parser(const Config &config, std::istream &input_stream)
{
std::unique_ptr<ITagValue> res;
if (config.parser == "tag_tree")
res = std::make_unique<TagTree>(input_stream);
else if (config.parser == "in_stream")
res = std::make_unique<InStream>(input_stream);
else
throw std::runtime_error(std::string("unknown parser: ") + config.parser);
return res;
}
int main(int argc, char **argv)
try
{
auto config = make_config(argc, argv);
if (!config)
return 0;
auto input = prepare_input(*config);
auto parser = make_parser(*config, input.text_stream);
std::string query;
while (getline(input.query_stream, query))
{
std::cout << parser->get_value(query) << std::endl;
}
return 0;
}
catch (const std::runtime_error &ex)
{
std::cerr << ex.what() << std::endl;
return -1;
}