#include #include #include #include #include #include #include #include #include namespace cab { auto magic::open(magic::flags flags, std::string_view database) noexcept -> std::expected { 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 magic::process(std::filesystem::path path) -> std::expected { auto result = ::magic_file(m_cookie, path.native().c_str()); if (!result) { return std::unexpected{std::string{::magic_error(m_cookie)}}; } return result; } auto magic::process(int file_descriptor) -> std::expected { auto result = ::magic_descriptor(m_cookie, file_descriptor); if (!result) { return std::unexpected{std::string{::magic_error(m_cookie)}}; } return result; } auto swap(magic & lhs, magic & rhs) noexcept -> void { auto temp = std::move(lhs); lhs = std::move(rhs); rhs = std::move(lhs); } } // namespace cab