blob: 96e212feb1e503f72a0ecdc4fbb371ff0b6f8c2a (
plain)
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
|
#include <cabinet/magic.hpp>
#include <magic.h>
#include <cerrno>
#include <expected>
#include <string_view>
#include <system_error>
#include <utility>
namespace cab
{
auto magic::open(magic::flags flags, std::string_view database) noexcept -> std::expected<magic, std::error_code>
{
auto cookie = magic_open(std::to_underlying(flags));
if (!cookie)
{
return std::unexpected{
std::error_code{errno, std::system_category()}
};
}
if (auto success = magic_load(cookie, database.data()); success < 0)
{
return std::unexpected{std::make_error_code(std::errc::invalid_argument)};
}
return magic{cookie};
}
magic::magic(::magic_t cookie) noexcept
: m_cookie{cookie}
{}
magic::magic(magic && other) noexcept
: m_cookie{std::exchange(other.m_cookie, nullptr)}
{}
magic::~magic()
{
if (m_cookie)
{
magic_close(m_cookie);
}
}
auto magic::operator=(magic && other) noexcept -> magic &
{
std::swap(m_cookie, other.m_cookie);
return *this;
}
auto swap(magic & lhs, magic & rhs) noexcept -> void
{
auto temp = std::move(lhs);
lhs = std::move(rhs);
rhs = std::move(lhs);
}
} // namespace cab
|