-
Notifications
You must be signed in to change notification settings - Fork 0
/
oicompare.cc
110 lines (93 loc) · 2.53 KB
/
oicompare.cc
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
#include <algorithm>
#include <array>
#include <cctype>
#include <cerrno>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <fmt/format.h>
#include <mio/mmap.hpp>
#include "oicompare.hh"
#include "print_format.hh"
#include "translations.hh"
using namespace std::string_view_literals;
namespace
{
class mapped_file
{
public:
mapped_file (const std::filesystem::path &path) : mmap_{create_mmap (path)}
{
}
constexpr const mio::mmap_source &
mmap () const noexcept
{
return mmap_;
}
private:
static mio::mmap_source
create_mmap (const std::filesystem::path &path)
{
auto size = std::filesystem::file_size (path);
if (size == 0)
// mmap() will not let us map something of size 0
return {};
return {path.native (), 0, size};
}
mio::mmap_source mmap_;
};
oicompare::translations::translation
parse_translation (std::string_view name)
{
using namespace oicompare::translations;
if (name == "english_abbreviated"sv)
return english_translation<kind::abbreviated>::print;
else if (name == "english_full"sv)
return english_translation<kind::full>::print;
else if (name == "english_terse"sv)
return english_translation<kind::terse>::print;
else if (name == "polish_abbreviated"sv)
return polish_translation<kind::abbreviated>::print;
else if (name == "polish_full"sv)
return polish_translation<kind::full>::print;
else if (name == "polish_terse"sv)
return polish_translation<kind::terse>::print;
else
return nullptr;
}
}
int
main (int argc, char **argv)
{
if (argc == 2
&& (std::string_view{argv[1]} == "--version"sv
|| std::string_view{argv[1]} == "-v"sv))
{
fmt::println ("oicompare version {}", oicompare::VERSION);
return EXIT_SUCCESS;
}
if (argc < 3 || argc > 4) [[unlikely]]
{
fmt::println (stderr, "Usage: {} FILE1 FILE2 [TRANSLATION]", argv[0]);
return 2;
}
mapped_file file1{argv[1]};
mapped_file file2{argv[2]};
std::string_view translation_name = argc < 4 ? "english_terse"sv : argv[3];
oicompare::translations::translation translation;
if (auto parsed_translation = parse_translation (translation_name))
translation = parsed_translation;
else
{
fmt::println (stderr, "Unknown translation: {}", translation_name);
return 2;
}
auto result = oicompare::compare (file1.mmap (), file2.mmap ());
translation (result);
return result ? EXIT_FAILURE : EXIT_SUCCESS;
}