
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
- 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 && ./scripts/install_dependencies.sh
Build the project using the build.sh bash script.
./scripts/build.sh -d build -b Release -c g++ -t
For more options, use:
./scripts/build.sh -h
Usage: ./scripts/build.sh [-d build_dir] [-b build_type] [-c compiler] [-t] [-e] [-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++).
-l library_type Specify the library type (STATIC or SHARED, default: SHARED).
-t Build and run tests (default: OFF).
-e Build and run examples (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 && ./scripts/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_include_directories(<name of your project>
<PUBLIC or PRIVATE or INTERFACE> ${magicxx_INCLUDE_DIR}
)
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:27
Examples
For more examples, refer to the examples directory.
Documentation
For comprehensive guides, API references, and detailed information, visit the documentation site.
Contributing
We welcome contributions! Please see the CONTRIBUTING.md file for guidelines on how to contribute.
Code of Conduct
We expect all contributors to adhere to the Code of Conduct. Please read it to understand the standards of behavior we expect from our community.
Security
For information on reporting security vulnerabilities, please see the SECURITY.md file.
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.