Added package object
This commit is contained in:
parent
c0fb378f2e
commit
ac1ac444aa
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "vendor/libzip"]
|
||||||
|
path = vendor/libzip
|
||||||
|
url = https://github.com/nih-at/libzip
|
|
@ -2,21 +2,6 @@ cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
project(AnkiParser)
|
project(AnkiParser)
|
||||||
|
|
||||||
file(GLOB_RECURSE target_src
|
|
||||||
"src/*.cpp"
|
|
||||||
)
|
|
||||||
|
|
||||||
file(GLOB_RECURSE target_inc
|
|
||||||
"include/*.hpp"
|
|
||||||
)
|
|
||||||
|
|
||||||
add_subdirectory(vendor/libzip)
|
add_subdirectory(vendor/libzip)
|
||||||
|
add_subdirectory(ankiparser)
|
||||||
add_library(ankiparser
|
add_subdirectory(examples)
|
||||||
${target_inc}
|
|
||||||
${target_src}
|
|
||||||
)
|
|
||||||
|
|
||||||
target_include_directories(ankiparser PRIVATE
|
|
||||||
${target_inc}
|
|
||||||
)
|
|
21
ankiparser/CMakeLists.txt
Normal file
21
ankiparser/CMakeLists.txt
Normal file
|
@ -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
|
||||||
|
)
|
19
ankiparser/include/Package.hpp
Normal file
19
ankiparser/include/Package.hpp
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace Anki
|
||||||
|
{
|
||||||
|
class Package
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Package();
|
||||||
|
Package(const std::string& filename);
|
||||||
|
~Package();
|
||||||
|
|
||||||
|
int Open(const std::string& filename);
|
||||||
|
|
||||||
|
private:
|
||||||
|
char* collection;
|
||||||
|
};
|
||||||
|
}
|
46
ankiparser/src/Package.cpp
Normal file
46
ankiparser/src/Package.cpp
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
#include "Package.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <zip.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
3
examples/CMakeLists.txt
Normal file
3
examples/CMakeLists.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
add_executable(test test.cpp)
|
||||||
|
target_include_directories(test PRIVATE ankiparser)
|
||||||
|
target_link_libraries(test PRIVATE ankiparser)
|
22
examples/test.cpp
Normal file
22
examples/test.cpp
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <Package.hpp>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
std::cerr << "Usage: " << argv[0] << " <apkg file>" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Anki::Package pkg(argv[1]);
|
||||||
|
}
|
||||||
|
catch (const char* e)
|
||||||
|
{
|
||||||
|
std::cerr << e << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
1
vendor/libzip
vendored
Submodule
1
vendor/libzip
vendored
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 159b94530e4af364fb859b9fea7207b698b09562
|
Loading…
Reference in a new issue