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

Implementation of the Magic class and supporting utilities. More...

#include <cmath>
#include <array>
#include <utility>
#include "magic.hpp"
#include <magic.h>
Include dependency graph for magic.cpp:

Classes

class  Recognition::Magic::MagicPrivate
 PIMPL implementation class for Magic. More...
struct  Recognition::Magic::MagicPrivate::FlagsConverter
 Converter between C++ flag types and libmagic flag values. More...
struct  Recognition::Magic::MagicPrivate::LibmagicPairConverter
 Extracts values from libmagic constant pairs. More...

Namespaces

namespace  Recognition
 Root namespace for the libmagicxx library.
namespace  Recognition::Detail
 Internal namespace for libmagic C library integration.

Functions

std::string Recognition::ToString (const Magic::FileTypeEntryT &file_type_entry, const std::string &type_separator=" -> ")
 Convert a file type entry to a string.
std::string Recognition::ToString (const Magic::FileTypeMapT &file_type_map, const std::string &type_separator=" -> ", const std::string &file_separator="\n")
 Convert a file type map to a string.
std::string Recognition::ToString (const Magic::ExpectedFileTypeT &expected_file_type)
 Convert an expected file type result to a string.
std::string Recognition::ToString (const Magic::ExpectedFileTypeEntryT &expected_file_type_entry, const std::string &type_separator=" -> ")
 Convert an expected file type entry to a string.
std::string Recognition::ToString (const Magic::ExpectedFileTypeMapT &expected_file_type_map, const std::string &type_separator=" -> ", const std::string &file_separator="\n")
 Convert an expected file type map to a string.
std::string Recognition::ToString (Magic::Flags flag)
 Convert a Magic flag to its string name.
std::string Recognition::ToString (const Magic::FlagsContainerT &flags, const std::string &separator=", ")
 Convert a container of flags to a string.
std::string Recognition::ToString (Magic::Parameters parameter)
 Convert a Magic parameter to its string name.
std::string Recognition::ToString (const Magic::ParameterValueT &parameter_value, const std::string &value_separator=": ")
 Convert a parameter-value pair to a string.
std::string Recognition::ToString (const Magic::ParameterValueMapT &parameters, const std::string &value_separator=": ", const std::string &parameter_separator=", ")
 Convert a parameter-value map to a string.

Detailed Description

Implementation of the Magic class and supporting utilities.

This file contains the complete implementation of the libmagicxx library, including:

  • Magic::MagicPrivate (PIMPL implementation class)
  • Magic class public method implementations
  • ToString() free function overloads for string conversion

Architecture

The implementation uses the PIMPL (Pointer to IMPLementation) idiom to:

  • Hide implementation details from the public header
  • Minimize compilation dependencies (libmagic headers are internal)
  • Enable ABI stability across library versions

libmagic Integration

The underlying libmagic C library is wrapped in the Detail namespace to isolate its symbols. All libmagic calls go through MagicPrivate, which:

  • Manages the magic cookie (libmagic handle) via RAII
  • Converts between C++23 types and C types
  • Translates libmagic error codes to exceptions or std::expected

Key Components

Component Purpose
Detail::magic_* Wrapped libmagic C functions
MagicPrivate PIMPL class holding cookie and state
MagicPrivate::FlagsConverter Converts between flag representations
MagicPrivate::LibmagicPairConverter Extracts values from flag/param pairs
MagicPrivate::ThrowExceptionOnFailure Error handling template
ToString() overloads String conversion utilities

Lifecycle Implementation

The three-state lifecycle (Closed -> Opened -> Valid) is tracked by:

  • m_cookie: nullptr means Closed, non-null means Opened
  • m_is_database_loaded: true only after successful LoadDatabaseFile()
  • IsValid() = IsOpen() && IsDatabaseLoaded()
See also
magic.hpp for the public API documentation
Magic class documentation for lifecycle state diagram
Author
Oğuz Toraman