Libmagicxx
Libmagicxx is a C++ wrapper library over the Magic Number Recognition Library. It provides an easy-to-use interface through the magic
class, enabling the identification of file types based on their content rather than file extensions.
Key Features
- Modern C++: Utilizes modern C++ standards and features, ensuring clean, efficient, and maintainable code.
- High-Level API: Provides a simple and intuitive high-level API for easy integration and use.
- Simple Integration with CMake: Easily integrate file type recognition into your CMake-based C++ projects.
Requirements to Build Libmagicxx
- Git
- GCC with libstdc++ or Clang with libc++ (C++23)
- CMake
- GNU Make or Ninja
- Libtool
- Autoconf
How to Build Libmagicxx
Clone the libmagicxx repo.
git clone https://github.com/oguztoraman/libmagicxx
Install the necessary dependencies using the install_dependencies.sh bash script.
cd libmagicxx && ./install_dependencies.sh
Build the project using the build.sh bash script.
./build.sh -d build -b Release -c g++ -t
For more options, use:
./build.sh -h
Usage: ./build.sh [-d build_dir] [-b build_type] [-c compiler] [-t] [-h]
-d build_dir Specify the build directory (default: release_build).
-b build_type Specify the CMake build type (default: Release).
-c compiler Specify the compiler (g++ or clang++, default: g++).
-t Build and run tests (default: OFF).
-h Display this message.
How to Use Libmagicxx in a CMake-based Project
Clone the libmagicxx repo into your project.
git clone https://github.com/oguztoraman/libmagicxx
Install the necessary dependencies using the install_dependencies.sh bash script.
cd libmagicxx && ./install_dependencies.sh
Add the following lines to the top level CMakeLists.txt
file of your project to include and link libmagicxx.
add_subdirectory(libmagicxx)
add_compile_options("$<$<CXX_COMPILER_ID:Clang>:-stdlib=libc++>")
target_link_libraries(<name of your project>
<PUBLIC or PRIVATE or INTERFACE> "magicxx;$<$<CXX_COMPILER_ID:Clang>:c++>"
)
Include the magic.hpp
header file in your source files. Below is an example code snippet that demonstrates how to print the MIME type of the default database file using the magic
class.
#include <print>
#include <iostream>
auto main(int, char**) -> int
{
magic m{magic::flags::mime};
std::println(std::cout, "{}", m.identify_file(magic::default_database_file));
}
The magic class provides a C++ wrapper over the Magic Number Recognition Library. The magic class is ...
Definition magic.hpp:26
Documentation
For comprehensive guides, API references, and detailed information, visit the documentation site.
Source Code
Explore the source code of Libmagicxx on GitHub.
License
Libmagicxx is licensed under the GNU Lesser General Public License Version 3. For more details, see the COPYING.LESSER file.
Other licenses:
- File and the Magic Number Recognition Library: See the COPYING file.
- GoogleTest: See the LICENSE file.