Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
Constructors

Magic construction methods. More...

Collaboration diagram for Constructors:

Functions

 Recognition::Magic::Magic () noexcept
 Default constructor. Creates an unopened Magic instance.
 Recognition::Magic::Magic (FlagsMaskT flags_mask, const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE)
 Construct and initialize Magic with flags and database.
 Recognition::Magic::Magic (FlagsMaskT flags_mask, const std::nothrow_t &tag, const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE) noexcept
 Construct and initialize Magic with flags and database (noexcept version).
 Recognition::Magic::Magic (const FlagsContainerT &flags_container, const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE)
 Construct and initialize Magic with a container of flags.
 Recognition::Magic::Magic (const FlagsContainerT &flags_container, const std::nothrow_t &tag, const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE) noexcept
 Construct and initialize Magic with a container of flags (noexcept version).

Detailed Description

Magic construction methods.

Constructors for creating Magic instances in various initial states.

Function Documentation

◆ Magic() [1/5]

Recognition::Magic::Magic ( )
noexcept

Default constructor. Creates an unopened Magic instance.

The instance is not valid for file identification until Open() and LoadDatabaseFile() are called.

Magic magic; // Not yet valid
// Now valid for identification
Magic() noexcept
Default constructor. Creates an unopened Magic instance.
Definition magic.cpp:2421
void LoadDatabaseFile(const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE)
Load a magic database file.
Definition magic.cpp:2638
@ Mime
Definition magic.hpp:328
void Open(FlagsMaskT flags_mask)
Open Magic with specified flags.
Definition magic.cpp:2651
See also
Open()
LoadDatabaseFile()
IsValid()
Since
10.0.0

◆ Magic() [2/5]

Recognition::Magic::Magic ( const FlagsContainerT & flags_container,
const std::filesystem::path & database_file = DEFAULT_DATABASE_FILE )
explicit

Construct and initialize Magic with a container of flags.

Alternative constructor accepting a container of individual flags instead of a bitmask. Useful when flags are determined at runtime.

Parameters
[in]flags_containerVector or other container of Flags values.
[in]database_filePath to magic database file (default: DEFAULT_DATABASE_FILE).
Exceptions
MagicOpenErrorIf opening the Magic instance fails.
EmptyPathIf database_file path is empty.
PathDoesNotExistIf database_file does not exist.
PathIsNotRegularFileIf database_file is not a regular file.
MagicLoadDatabaseFileErrorIf loading the database fails.
std::vector<Magic::Flags> flags = {Magic::Flags::Mime, Magic::Flags::Compress};
Magic magic{flags};
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Definition magic.hpp:216
@ Compress
Definition magic.hpp:319
Note
The ".mgc" extension is automatically appended to the database path if needed.
See also
FlagsContainerT
Since
10.0.0

◆ Magic() [3/5]

Recognition::Magic::Magic ( const FlagsContainerT & flags_container,
const std::nothrow_t & tag,
const std::filesystem::path & database_file = DEFAULT_DATABASE_FILE )
noexcept

Construct and initialize Magic with a container of flags (noexcept version).

Non-throwing variant that silently fails if initialization errors occur. Check IsValid() after construction to verify success.

Parameters
[in]flags_containerVector or other container of Flags values.
[in]tagPass std::nothrow to select this overload.
[in]database_filePath to magic database file (default: DEFAULT_DATABASE_FILE).
Note
The ".mgc" extension is automatically appended to the database path if needed.
See also
IsValid()
Since
10.0.0

◆ Magic() [4/5]

Recognition::Magic::Magic ( FlagsMaskT flags_mask,
const std::filesystem::path & database_file = DEFAULT_DATABASE_FILE )
explicit

Construct and initialize Magic with flags and database.

This constructor opens Magic with the specified flags and loads the magic database file in a single step. On success, the instance is immediately ready for file identification.

Parameters
[in]flags_maskConfiguration flags (use Flags enum values combined with |).
[in]database_filePath to magic database file (default: DEFAULT_DATABASE_FILE).
Exceptions
MagicOpenErrorIf opening the Magic instance fails.
EmptyPathIf database_file path is empty.
PathDoesNotExistIf database_file does not exist.
PathIsNotRegularFileIf database_file is not a regular file.
MagicLoadDatabaseFileErrorIf loading the database fails.
// Use MIME output with default database
// Combine flags
// Custom database path
Magic magic3{Magic::Flags::Mime, "/custom/path/magic"};
Note
The ".mgc" extension is automatically appended to the database path if needed.
See also
LoadDatabaseFile()
Since
10.0.0

◆ Magic() [5/5]

Recognition::Magic::Magic ( FlagsMaskT flags_mask,
const std::nothrow_t & tag,
const std::filesystem::path & database_file = DEFAULT_DATABASE_FILE )
noexcept

Construct and initialize Magic with flags and database (noexcept version).

Non-throwing variant that silently fails if initialization errors occur. Check IsValid() after construction to verify success.

Parameters
[in]flags_maskConfiguration flags (use Flags enum values combined with |).
[in]tagPass std::nothrow to select this overload.
[in]database_filePath to magic database file (default: DEFAULT_DATABASE_FILE).
Magic magic{Magic::Flags::Mime, std::nothrow};
if (magic.IsValid()) {
// Safe to use
}
bool IsValid() const noexcept
Check if the Magic instance is valid for file identification.
Definition magic.cpp:2633
Note
The ".mgc" extension is automatically appended to the database path if needed.
See also
IsValid()
Since
10.0.0