Added package object
This commit is contained in:
parent
c0fb378f2e
commit
ac1ac444aa
8 changed files with 117 additions and 17 deletions
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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue