-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (75 loc) · 2.57 KB
/
Copy pathmain.cpp
File metadata and controls
88 lines (75 loc) · 2.57 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
#include <iostream>
#include <fstream>
#include "aggregator.h"
namespace
{
void usage(const std::string_view program_name)
{
std::cout << "Usage: " << program_name << std::endl;
std::cout << "The first parameter should be a path to an input csv file." << std::endl;
std::cout << "The second parameter should be a path to an output xml file." << std::endl;
}
}
int main(int argc, char* argv[])
{
static constexpr int expected_args_count = 3;
if (argc != expected_args_count)
{
usage(argv[0]);
return -1;
}
try
{
const std::string_view input_path = argv[1];
std::ifstream input_file_stream{input_path.data()};
if (!input_file_stream.is_open())
{
std::cerr << "failed to open " << input_path << std::endl;
return -1;
}
Aggregator::SelRequestsStorage sel_requests_storage{};
Aggregator::CntRequestsStorage cnt_requests_storage{};
std::string line{};
while (getline(input_file_stream, line, input_file_stream.widen('\n')))
{
auto request = Aggregator::tokenize(line);
if (request.empty())
{
throw std::invalid_argument("The request can't be empty.");
}
switch (Aggregator::get_request_type(request))
{
case Aggregator::RequestType::sel:
{
auto sel_request = Aggregator::create_sel_request(request);
const auto& sel_request_uuid = sel_request.uuid;
sel_requests_storage.emplace(sel_request_uuid, std::move(sel_request));
break;
}
case Aggregator::RequestType::cnt:
{
auto cnt_request = Aggregator::create_cnt_request(request);
cnt_requests_storage.emplace_back(std::move(cnt_request));
break;
}
default:
break;
}
}
auto banners = process_banners(sel_requests_storage, cnt_requests_storage);
std::string xml = serialize(banners);
const std::string_view output_path = argv[2];
std::ofstream output_file_stream{output_path.data()};
output_file_stream << xml;
std::cout << std::format("XML file saved successfully to {}", output_path) << std::endl;
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
catch (...)
{
std::cerr << "An error occurred!" << std::endl;
}
return 0;
}