From ac1ac444aac3a805be34ecf9b59daa772fbca5eb Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 14 Dec 2020 13:26:00 +0100 Subject: [PATCH] Added package object --- .gitmodules | 3 +++ CMakeLists.txt | 19 ++------------ ankiparser/CMakeLists.txt | 21 ++++++++++++++++ ankiparser/include/Package.hpp | 19 ++++++++++++++ ankiparser/src/Package.cpp | 46 ++++++++++++++++++++++++++++++++++ examples/CMakeLists.txt | 3 +++ examples/test.cpp | 22 ++++++++++++++++ vendor/libzip | 1 + 8 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 ankiparser/CMakeLists.txt create mode 100644 ankiparser/include/Package.hpp create mode 100644 ankiparser/src/Package.cpp create mode 100644 examples/CMakeLists.txt create mode 100644 examples/test.cpp create mode 160000 vendor/libzip diff --git a/.gitmodules b/.gitmodules index e69de29..43dde67 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/libzip"] + path = vendor/libzip + url = https://github.com/nih-at/libzip diff --git a/CMakeLists.txt b/CMakeLists.txt index 363231b..be6f2ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,21 +2,6 @@ cmake_minimum_required(VERSION 3.10) project(AnkiParser) -file(GLOB_RECURSE target_src - "src/*.cpp" -) - -file(GLOB_RECURSE target_inc - "include/*.hpp" -) - add_subdirectory(vendor/libzip) - -add_library(ankiparser - ${target_inc} - ${target_src} -) - -target_include_directories(ankiparser PRIVATE - ${target_inc} -) \ No newline at end of file +add_subdirectory(ankiparser) +add_subdirectory(examples) \ No newline at end of file diff --git a/ankiparser/CMakeLists.txt b/ankiparser/CMakeLists.txt new file mode 100644 index 0000000..d19937f --- /dev/null +++ b/ankiparser/CMakeLists.txt @@ -0,0 +1,21 @@ +file(GLOB_RECURSE target_src + "src/*.cpp" +) + +file(GLOB_RECURSE target_inc + "include/*.hpp" +) + +add_library(ankiparser STATIC + ${target_inc} + ${target_src} +) + +target_include_directories(ankiparser PUBLIC + include + libzip::zip +) + +target_link_libraries(ankiparser PUBLIC + libzip::zip +) \ No newline at end of file diff --git a/ankiparser/include/Package.hpp b/ankiparser/include/Package.hpp new file mode 100644 index 0000000..9389bd2 --- /dev/null +++ b/ankiparser/include/Package.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace Anki +{ + class Package + { + public: + Package(); + Package(const std::string& filename); + ~Package(); + + int Open(const std::string& filename); + + private: + char* collection; + }; +} \ No newline at end of file diff --git a/ankiparser/src/Package.cpp b/ankiparser/src/Package.cpp new file mode 100644 index 0000000..a6d8c9a --- /dev/null +++ b/ankiparser/src/Package.cpp @@ -0,0 +1,46 @@ +#include "Package.hpp" +#include +#include + +namespace Anki +{ + Package::Package() : + collection(nullptr) + { + } + + Package::Package(const std::string& filename) : + Package() + { + if(Open(filename)) throw "Failed to load package"; + } + + Package::~Package() + { + if (collection != nullptr) delete collection; + } + + int Package::Open(const std::string& filename) + { + int err = 0; + zip* archive = zip_open(filename.c_str(), 0, &err); + if (err) return err; + + static const char* collection_filename = "collection.anki2"; + struct zip_stat st; + zip_stat_init(&st); + zip_stat(archive, collection_filename, 0, &st); + + collection = new char[st.size]; + + zip_file* collection_file = zip_fopen(archive, collection_filename, 0); + zip_fread(collection_file, collection, st.size); + zip_fclose(collection_file); + + zip_close(archive); + + std::cout << collection << std::endl; + + return err; + } +} \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..bf05891 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(test test.cpp) +target_include_directories(test PRIVATE ankiparser) +target_link_libraries(test PRIVATE ankiparser) \ No newline at end of file diff --git a/examples/test.cpp b/examples/test.cpp new file mode 100644 index 0000000..42b6bb2 --- /dev/null +++ b/examples/test.cpp @@ -0,0 +1,22 @@ +#include +#include + +int main(int argc, char** argv) +{ + if (argc != 2) + { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + + try + { + Anki::Package pkg(argv[1]); + } + catch (const char* e) + { + std::cerr << e << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/vendor/libzip b/vendor/libzip new file mode 160000 index 0000000..159b945 --- /dev/null +++ b/vendor/libzip @@ -0,0 +1 @@ +Subproject commit 159b94530e4af364fb859b9fea7207b698b09562