Skip to content

Commit 65aa685

Browse files
authored
Merge pull request #9 from pavel-cpp/dev
Release 1.0.0
2 parents ba9b250 + 9c13734 commit 65aa685

36 files changed

+1681
-602
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
name: CMake Multi-Platform
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
BUILD_TYPE: Release
11+
12+
jobs:
13+
build-ubuntu:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
compiler: [gcc, clang]
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Set up compiler
23+
run: |
24+
if [ "${{ matrix.compiler }}" == "gcc" ]; then
25+
sudo apt-get update
26+
sudo apt-get install -y g++
27+
elif [ "${{ matrix.compiler }}" == "clang" ]; then
28+
sudo apt-get update
29+
sudo apt-get install -y clang
30+
fi
31+
shell: bash
32+
33+
- name: Configure CMake
34+
run: cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -B build
35+
shell: bash
36+
37+
- name: Build with CMake
38+
run: cmake --build build
39+
shell: bash
40+
41+
- name: Install Library
42+
run: cmake --install build --prefix ${{ github.workspace }}/mime_magic
43+
shell: bash
44+
45+
- name: Upload Build Artifacts
46+
uses: actions/upload-artifact@v3
47+
with:
48+
name: build-artifacts-ubuntu-${{ matrix.compiler }}
49+
path: ${{ github.workspace }}/mime_magic
50+
51+
test-ubuntu:
52+
runs-on: ubuntu-latest
53+
needs: build-ubuntu
54+
strategy:
55+
matrix:
56+
compiler: [gcc, clang]
57+
58+
steps:
59+
- uses: actions/checkout@v4
60+
61+
- name: Download Build Artifacts
62+
uses: actions/download-artifact@v3
63+
with:
64+
name: build-artifacts-ubuntu-${{ matrix.compiler }}
65+
path: ${{ github.workspace }}/mime_magic
66+
67+
- name: Configure Tests
68+
working-directory: ${{ github.workspace }}/tests
69+
run: cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -B build
70+
shell: bash
71+
72+
- name: Build Tests
73+
working-directory: ${{ github.workspace }}/tests
74+
run: cmake --build build
75+
shell: bash
76+
77+
- name: Run Tests
78+
working-directory: ${{ github.workspace }}/tests/build
79+
run: ./tests
80+
shell: bash
81+
82+
build-windows:
83+
runs-on: windows-latest
84+
steps:
85+
- uses: actions/checkout@v4
86+
87+
- name: Set up MSBuild
88+
uses: microsoft/[email protected]
89+
90+
- name: Install Visual Studio Build Tools
91+
run: |
92+
choco install visualstudio2019buildtools --package-parameters "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --includeOptional --quiet --norestart"
93+
shell: cmd
94+
95+
- name: Configure CMake
96+
run: cmake -G "Visual Studio 16 2019" -B ${{ github.workspace }}/build
97+
shell: cmd
98+
99+
- name: Build with CMake
100+
run: cmake --build ${{ github.workspace }}/build --config ${{ env.BUILD_TYPE }}
101+
shell: cmd
102+
103+
- name: Install Library
104+
run: cmake --install ${{ github.workspace }}/build --config ${{ env.BUILD_TYPE }} --prefix ${{ github.workspace }}/mime_magic
105+
shell: cmd
106+
107+
- name: Upload Build Artifacts
108+
uses: actions/upload-artifact@v3
109+
with:
110+
name: build-artifacts-windows
111+
path: ${{ github.workspace }}/mime_magic
112+
113+
test-windows:
114+
runs-on: windows-latest
115+
needs: build-windows
116+
steps:
117+
- uses: actions/checkout@v4
118+
119+
- name: Download Build Artifacts
120+
uses: actions/download-artifact@v3
121+
with:
122+
name: build-artifacts-windows
123+
path: ${{ github.workspace }}/mime_magic
124+
125+
- name: Configure Tests
126+
working-directory: ${{ github.workspace }}/tests
127+
run: cmake -G "Visual Studio 16 2019" -B build
128+
shell: cmd
129+
130+
- name: Build Tests
131+
working-directory: ${{ github.workspace }}/tests
132+
run: cmake --build build --config ${{ env.BUILD_TYPE }}
133+
shell: cmd
134+
135+
- name: Run Tests
136+
working-directory: ${{ github.workspace }}/tests/build
137+
run: ./tests
138+
shell: cmd
139+
140+
send_message:
141+
runs-on: ubuntu-latest
142+
needs: [test-ubuntu, test-windows]
143+
steps:
144+
- name: Send telegram message
145+
uses: appleboy/telegram-action@master
146+
with:
147+
to: ${{ secrets.TELEGRAM_TO }}
148+
token: ${{ secrets.TELEGRAM_TOKEN }}
149+
message: |
150+
mime_magic ready to merge
151+
Выполнил: ${{ github.actor }}
152+
Сообщение коммита: ${{ github.event.commits[0].message }}
153+
Ссылка на коммит: https://github.com/${{ github.repository }}/commit/${{ github.sha }}

CMakeLists.txt

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
11
cmake_minimum_required(VERSION 3.23)
2-
project(mime_magic)
2+
project(mime_magic VERSION 1.0.1 LANGUAGES CXX)
33

4-
include_directories(
5-
src
6-
src/loader
7-
srd/node
4+
add_subdirectory(src)
5+
6+
add_custom_target(copy_docs
7+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/docs ${CMAKE_BINARY_DIR}/docs
8+
COMMENT "Copying documentation"
9+
)
10+
11+
add_custom_target(copy_examples
12+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/examples ${CMAKE_BINARY_DIR}/examples
13+
COMMENT "Copying examples"
14+
)
15+
16+
add_dependencies(${PROJECT_NAME}_shared copy_docs)
17+
add_dependencies(${PROJECT_NAME}_shared copy_examples)
18+
add_dependencies(${PROJECT_NAME}_static copy_docs)
19+
add_dependencies(${PROJECT_NAME}_static copy_examples)
20+
21+
# Создание и установка конфигурационных файлов
22+
include(CMakePackageConfigHelpers)
23+
write_basic_package_version_file(
24+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
25+
VERSION ${PROJECT_VERSION}
26+
COMPATIBILITY AnyNewerVersion
827
)
928

10-
add_library(
11-
${PROJECT_NAME}
12-
STATIC
13-
src/node/mime_node.cpp
14-
src/loader/mime_loader.cpp
29+
configure_file(src/mime_magicConfig.cmake.in
30+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
31+
@ONLY
1532
)
1633

17-
add_executable(
18-
${PROJECT_NAME}_test
19-
test/main.cpp
34+
install(DIRECTORY ${CMAKE_BINARY_DIR}/docs DESTINATION ./)
35+
install(DIRECTORY ${CMAKE_BINARY_DIR}/examples DESTINATION ./)
36+
37+
install(FILES
38+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
39+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
40+
DESTINATION lib/cmake/${PROJECT_NAME}
2041
)
2142

22-
target_link_libraries(
23-
${PROJECT_NAME}_test
24-
PUBLIC
25-
${PROJECT_NAME}
26-
)
43+
install(EXPORT ${PROJECT_NAME}Targets
44+
FILE ${PROJECT_NAME}Targets.cmake
45+
NAMESPACE ${PROJECT_NAME}::
46+
DESTINATION lib/cmake/${PROJECT_NAME}
47+
)

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
1-
# cpp-mime-magic
2-
A MIME magic parser for identifying file types.
1+
# MIME Magic
2+
3+
## Overview
4+
MIME magic is a cross-platform library that
5+
is an implementation of the standard
6+
and has a number of differences from the standard,
7+
designed for parsing and executing magic files.
8+
9+
## Deployment
10+
To deploy the library you need
11+
follow these steps:
12+
13+
### Configure project
14+
In the below command you are free to change CMAKE_BUILD_TYPE
15+
if you want to configure the Debug version
16+
and the build directory to your own.
17+
```shell
18+
cmake -DCMAKE_BUILD_TYPE=Release -B cmake_build_release
19+
```
20+
> [!IMPORTANT]
21+
> If you are building a library for msbuild
22+
> specifying BUILD_TYPE is not necessary.
23+
###Building the project
24+
```shell
25+
Use the command below to build the library.
26+
You can change the build directory.
27+
cmake --build cmake_build_release
28+
```
29+
> [!IMPORTANT]
30+
> If you are building a library for msbuild
31+
> add `--config {BUILD_TYPE}`
32+
33+
### Installing the library
34+
Use below command
35+
```shell
36+
cmake --install ./build --prefix ./mime_magic
37+
```
38+
> [!IMPORTANT]
39+
> If you are building a library for msbuild
40+
> add `--config {BUILD_TYPE}`

bootstrap.bat

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@echo off
2+
3+
REM Check if the installation path argument is passed
4+
if "%~1"=="" (
5+
set /p INSTALLATION_PATH="Enter the installation path (for example, C:\Program Files\MyApp): "
6+
) else (
7+
set INSTALLATION_PATH=%~1
8+
)
9+
10+
REM Create a build directory
11+
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
12+
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
13+
14+
REM Putting the project together
15+
cmake --build build
16+
17+
REM Install the project
18+
cmake --install build --prefix %INSTALLATION_PATH%
19+
20+
rmdir /s /q build
21+
22+
pause

examples/basic_project/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.23)
2+
project(test LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
set(CMAKE_PREFIX_PATH ..\\mime_magic\\lib\\cmake\\MimeMagic)
7+
8+
find_package(MimeMagic REQUIRED)
9+
10+
add_executable(${PROJECT_NAME} main.cpp)
11+
12+
target_link_libraries(${PROJECT_NAME} MimeMagic::MimeMagic_static)

examples/basic_project/main.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <locale>
4+
#include <vector>
5+
#include <chrono>
6+
7+
#include "loader/mime_loader.h"
8+
9+
class Timer {
10+
public:
11+
Timer() {
12+
start = std::chrono::high_resolution_clock::now();
13+
}
14+
15+
~Timer() {
16+
auto end = std::chrono::high_resolution_clock::now();
17+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
18+
std::cout << "Time taken: " << duration.count() << " ms" << std::endl;
19+
}
20+
21+
private:
22+
std::chrono::time_point<std::chrono::high_resolution_clock> start;
23+
};
24+
25+
int main() {
26+
using namespace std;
27+
boolalpha(cout);
28+
system("chcp 1251");
29+
30+
magic::mime_list nodes;
31+
// You can check how it fast
32+
// {
33+
// Timer t;
34+
// nodes = std::move(magic::load("C:\\Sophus-NEW\\modules\\files.etl"));
35+
// }
36+
37+
nodes = magic::load("magic.etl");
38+
39+
cout << nodes.size() << " node workers SUCCESSFULLY LOADED!" << endl;
40+
41+
vector<char> data;
42+
data.resize(30);
43+
44+
{
45+
ifstream png("image.png", ios::in | ios::binary);
46+
png.read(data.data(), data.size());
47+
48+
cout << "PNG" << endl;
49+
cout << std::string(80, '=') << endl << endl;
50+
int i = 1;
51+
for (const auto& node: nodes) {
52+
auto response = node->process_data(data.data(), data.size());
53+
if (response.has_value()) {
54+
cout << dec << i << hex << ")\n" << response.value() << endl;
55+
cout << endl << std::string(80, '=') << endl << endl;
56+
break;
57+
}
58+
++i;
59+
}
60+
}
61+
data.clear();
62+
data.shrink_to_fit();
63+
data.resize(30);
64+
65+
{
66+
ifstream corrupt_png("corrupted-image.png", ios::in | ios::binary);
67+
corrupt_png.read(data.data(), data.size());
68+
69+
cout << "CORRUPT PNG" << endl;
70+
cout << std::string(80, '=') << endl << endl;
71+
size_t i = 1;
72+
for (const auto& node: nodes) {
73+
auto response = node->process_data(data.data(), data.size());
74+
if (response.has_value()) {
75+
cout << dec << i << hex << ")\n" << response.value() << endl;
76+
cout << endl << std::string(80, '=') << endl << endl;
77+
break;
78+
}
79+
++i;
80+
}
81+
}
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)