Added sqlite
This commit is contained in:
parent
3b8e40e888
commit
dd378a45cf
2
.gitmodules
vendored
2
.gitmodules
vendored
|
@ -1,3 +1,3 @@
|
||||||
[submodule "vendor/libzip"]
|
[submodule "vendor/libzip"]
|
||||||
path = vendor/libzip
|
path = vendor/libzip
|
||||||
url = https://github.com/nih-at/libzip
|
url = https://github.com/nih-at/libzip
|
|
@ -1,4 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
SET(CXX_STANDARD 17)
|
||||||
|
|
||||||
|
|
||||||
project(AnkiParser)
|
project(AnkiParser)
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
find_package(sqlite3 REQUIRED)
|
||||||
|
|
||||||
file(GLOB_RECURSE target_src
|
file(GLOB_RECURSE target_src
|
||||||
"src/*.cpp"
|
"src/*.cpp"
|
||||||
)
|
)
|
||||||
|
@ -14,8 +16,10 @@ add_library(ankiparser STATIC
|
||||||
target_include_directories(ankiparser PUBLIC
|
target_include_directories(ankiparser PUBLIC
|
||||||
include
|
include
|
||||||
libzip::zip
|
libzip::zip
|
||||||
|
${SQLite3_INCLUDE_DIR}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(ankiparser PUBLIC
|
target_link_libraries(ankiparser PUBLIC
|
||||||
libzip::zip
|
libzip::zip
|
||||||
|
${SQLite3_LIBRARY}
|
||||||
)
|
)
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
struct sqlite3;
|
||||||
|
|
||||||
namespace Anki
|
namespace Anki
|
||||||
{
|
{
|
||||||
class Package
|
class Package
|
||||||
|
@ -14,6 +16,12 @@ namespace Anki
|
||||||
int Open(const std::string& filename);
|
int Open(const std::string& filename);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char* collection;
|
void DumpAnki2File(const char* raw, size_t size);
|
||||||
|
|
||||||
|
private:
|
||||||
|
sqlite3* collection;
|
||||||
|
std::string tmpDbName;
|
||||||
|
|
||||||
|
static bool sqlite_init;
|
||||||
};
|
};
|
||||||
}
|
}
|
|
@ -1,12 +1,33 @@
|
||||||
#include "Package.hpp"
|
#include "Package.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include <zip.h>
|
#include <zip.h>
|
||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
|
bool Anki::Package::sqlite_init = false;
|
||||||
|
|
||||||
|
static int callback(void* NotUsed, int argc, char** argv, char** azColName)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < argc; i++)
|
||||||
|
{
|
||||||
|
std::cout << argv[i] << "\t";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
namespace Anki
|
namespace Anki
|
||||||
{
|
{
|
||||||
Package::Package() :
|
Package::Package() :
|
||||||
collection(nullptr)
|
collection(nullptr)
|
||||||
{
|
{
|
||||||
|
if (!sqlite_init)
|
||||||
|
{
|
||||||
|
srand(time(NULL));
|
||||||
|
sqlite3_initialize();
|
||||||
|
sqlite_init = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Package::Package(const std::string& filename) :
|
Package::Package(const std::string& filename) :
|
||||||
|
@ -17,7 +38,7 @@ namespace Anki
|
||||||
|
|
||||||
Package::~Package()
|
Package::~Package()
|
||||||
{
|
{
|
||||||
if (collection != nullptr) delete collection;
|
sqlite3_close(collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Package::Open(const std::string& filename)
|
int Package::Open(const std::string& filename)
|
||||||
|
@ -31,16 +52,46 @@ namespace Anki
|
||||||
zip_stat_init(&st);
|
zip_stat_init(&st);
|
||||||
zip_stat(archive, collection_filename, 0, &st);
|
zip_stat(archive, collection_filename, 0, &st);
|
||||||
|
|
||||||
collection = new char[st.size];
|
char* collection_raw = new char[st.size];
|
||||||
|
|
||||||
zip_file* collection_file = zip_fopen(archive, collection_filename, 0);
|
zip_file* collection_file = zip_fopen(archive, collection_filename, 0);
|
||||||
zip_fread(collection_file, collection, st.size);
|
zip_fread(collection_file, collection_raw, st.size);
|
||||||
zip_fclose(collection_file);
|
zip_fclose(collection_file);
|
||||||
|
|
||||||
zip_close(archive);
|
zip_close(archive);
|
||||||
|
|
||||||
std::cout << collection << std::endl;
|
DumpAnki2File(collection_raw, st.size);
|
||||||
|
|
||||||
|
delete[] collection_raw;
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Package::DumpAnki2File(const char* raw, size_t size)
|
||||||
|
{
|
||||||
|
// SQLite is kinda stupid and can't create a database from data stored in memory.
|
||||||
|
// It insists on opening a database file instead. I don't see a way around that unfortunately
|
||||||
|
// So I have to dump the database into a file first
|
||||||
|
// tmpDbName = std::to_string(rand()) + ".db";
|
||||||
|
tmpDbName = "test.db";
|
||||||
|
std::ofstream tmp_db(tmpDbName, std::ios::binary);
|
||||||
|
tmp_db.write(raw, size);
|
||||||
|
tmp_db.close();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int result = sqlite3_open(tmpDbName.c_str(), &collection);
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
std::cerr << result << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* errMsg = nullptr;
|
||||||
|
result = sqlite3_exec(collection, "SELECT decks FROM col;", callback, 0, &errMsg);
|
||||||
|
if (result != SQLITE_OK)
|
||||||
|
{
|
||||||
|
std::cerr << errMsg << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <Package.hpp>
|
#include <Package.hpp>
|
||||||
#include <zip.h>
|
#include <zip.h>
|
||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue