summaryrefslogtreecommitdiff
path: root/cabinet/magic.hpp
blob: 9e078dea9cf8f486927db90eab251b35ce890b58 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef CABINET_MAGIC_HPP
#define CABINET_MAGIC_HPP

#include <magic.h>

#include <expected>
#include <filesystem>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>

namespace cab
{

  struct magic
  {
    enum struct flags : decltype(MAGIC_NONE)
    {
      none = MAGIC_NONE,
      print_debug = MAGIC_DEBUG,
      follow_symlinks = MAGIC_SYMLINK,
      inspect_compressed = MAGIC_COMPRESS,
      inspect_devices = MAGIC_DEVICES,
      mime_type = MAGIC_MIME_TYPE,
      mime_encoding = MAGIC_MIME_ENCODING,
      mime = MAGIC_MIME,
      all_matches = MAGIC_CONTINUE,
      check_database = MAGIC_CHECK,
      preserve_access_time = MAGIC_PRESERVE_ATIME,
      raw = MAGIC_RAW,
      forward_errors = MAGIC_ERROR,
      apple = MAGIC_APPLE,
      extensions = MAGIC_EXTENSION,
      transparent_compression = MAGIC_COMPRESS_TRANSP,
      emx_dont_check_application_type = MAGIC_NO_CHECK_APPTYPE,
      no_elf_details = MAGIC_NO_CHECK_ELF,
      no_text_encodings = MAGIC_NO_CHECK_ENCODING,
      no_magic_files = MAGIC_NO_CHECK_SOFT,
      dont_inspect_tar = MAGIC_NO_CHECK_TAR,
      no_text_files = MAGIC_NO_CHECK_TEXT,
      no_tokens = MAGIC_NO_CHECK_TOKENS,
      no_json = MAGIC_NO_CHECK_JSON,
      no_csv = MAGIC_NO_CHECK_CSV,
      no_simh = MAGIC_NO_CHECK_SIMH,
    };

    auto static open(flags flags, std::string_view database = {}) noexcept -> std::expected<magic, std::error_code>;

    ~magic();
    magic(magic const &) = delete;
    magic(magic && other) noexcept;

    auto operator=(magic const &) -> magic & = delete;
    auto operator=(magic && other) noexcept -> magic &;

    auto process(std::filesystem::path path) -> std::expected<std::string, std::error_code>;
    auto process(int file_descriptor) -> std::expected<std::string, std::error_code>;

  private:
    auto handle_result(char const * result) -> std::expected<std::string, std::error_code>;

    explicit magic(::magic_t cookie) noexcept;

    ::magic_t m_cookie{};
  };

  auto swap(magic & lhs, magic & rhs) noexcept -> void;

  constexpr auto operator|(magic::flags lhs, magic::flags rhs) noexcept -> magic::flags
  {
    return static_cast<magic::flags>(std::to_underlying(lhs) | std::to_underlying(rhs));
  }

}  // namespace cab

#endif