Create temporary files instead of permanent ones

This commit is contained in:
Robert 2020-12-14 16:08:14 +01:00
parent dd378a45cf
commit c78c5aeacd
2 changed files with 6 additions and 7 deletions

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
SET(CXX_STANDARD 17) SET(CMAKE_CXX_STANDARD 17)
project(AnkiParser) project(AnkiParser)

View file

@ -1,6 +1,7 @@
#include "Package.hpp" #include "Package.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <filesystem>
#include <zip.h> #include <zip.h>
#include <sqlite3.h> #include <sqlite3.h>
@ -39,6 +40,7 @@ namespace Anki
Package::~Package() Package::~Package()
{ {
sqlite3_close(collection); sqlite3_close(collection);
std::remove(tmpDbName.c_str());
} }
int Package::Open(const std::string& filename) int Package::Open(const std::string& filename)
@ -70,20 +72,17 @@ namespace Anki
void Package::DumpAnki2File(const char* raw, size_t size) void Package::DumpAnki2File(const char* raw, size_t size)
{ {
// SQLite is kinda stupid and can't create a database from data stored in memory. // 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 // It also doesn't want file pointers. It just wants a filename...
// So I have to dump the database into a file first // So I have to dump the database into a file first
// tmpDbName = std::to_string(rand()) + ".db"; tmpDbName = std::filesystem::temp_directory_path().generic_string() + std::to_string(rand()) + ".db";
tmpDbName = "test.db";
std::ofstream tmp_db(tmpDbName, std::ios::binary); std::ofstream tmp_db(tmpDbName, std::ios::binary);
tmp_db.write(raw, size); tmp_db.write(raw, size);
tmp_db.close(); tmp_db.close();
int result = sqlite3_open(tmpDbName.c_str(), &collection); int result = sqlite3_open(tmpDbName.c_str(), &collection);
if (result) if (result)
{ {
std::cerr << result << std::endl; std::cerr << sqlite3_errmsg(collection) << " (" << result << ")" << std::endl;
return; return;
} }