dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Context.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <span>
13
14// Project includes
16#include <dfx-utilities/FileSystem.hpp>
17
18typedef struct magic_set * magic_t;
19
20namespace dfx::Magic
21{
49{
50public:
61 Context(int flags = -1, fs::path const & magicDb = {});
63 Context(Context && other) noexcept;
68
70 Context & operator=(Context && other) noexcept;
71
72public:
80 bool isReady() const noexcept { return _ctx != nullptr && _ready; }
81
90 std::string lastError() const;
91
92public:
100 std::string get(int fd) const;
101
107 std::string get(fs::path const & path) const;
108
114 std::string get(std::span<uint8_t> const & data) const;
115
116public:
122 static int defaultFlags;
123
124private:
125 magic_t _ctx;
126 bool _ready = false;
127};
128} // !namespace dfx::Magic
Convenience macros to explicitly control copy and move semantics.
std::string get(fs::path const &path) const
Identify content from a filesystem path.
DISABLE_COPY(Context)
Copying is disabled (single owner of the libmagic handle).
std::string get(int fd) const
Identify content from an open file descriptor.
bool isReady() const noexcept
Whether the context is initialized and ready to be used.
Definition Context.hpp:80
std::string get(std::span< uint8_t > const &data) const
Identify content from an in-memory buffer.
std::string lastError() const
Retrieve the last error message from libmagic.
Context(Context &&other) noexcept
Move-construct a context.
Context & operator=(Context &&other) noexcept
Move-assign a context.
Context(int flags=-1, fs::path const &magicDb={})
Construct a libmagic context and load the magic database.
~Context()
Destroy the context and release the underlying libmagic handle.
static int defaultFlags
Default libmagic flags used when the constructor argument flags is -1.
Definition Context.hpp:122
Definition Context.hpp:21