-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (67 loc) · 1.61 KB
/
main.cpp
File metadata and controls
76 lines (67 loc) · 1.61 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
#include "argument.hpp"
#include "delimiter.hpp"
#include "parser.hpp"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <generator>
#include <iostream>
#include <print>
#include <ranges>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
auto main(const int argc, char const* const argv[]) -> int
{
try
{
const utilities::arguments arguments {argc, argv};
const auto media_type = arguments.option<iri::delimiter_pair, 1>("media-type");
const auto included_files = arguments.option<std::filesystem::path>("include");
for (const auto&& [content_type, content] : iri::parse(media_type))
{
switch (content_type)
{
using enum iri::type;
case text:
{
std::print("{}", content);
continue;
}
case directive:
{
auto predicate = [&](const auto& element) noexcept { return element.filename() == content; };
if (const auto match = std::ranges::find_if(included_files, predicate); match == included_files.end())
{
throw std::runtime_error {std::format("missing include of file '{}'", content)};
}
else
{
std::cout << std::ifstream {*match, std::ios::binary}.rdbuf();
}
continue;
}
default:
{
throw std::logic_error {"charlie messed up"};
}
}
}
}
catch (const std::runtime_error& error)
{
std::println(std::cerr, "iri: error: {}", error.what());
return 1;
}
catch (const std::logic_error& error)
{
std::println(std::cerr, "iri: logic_error: {}", error.what());
return 1;
}
catch (...)
{
std::println(std::cerr, "iri: error: uncaught exception");
throw;
}
}