diff options
Diffstat (limited to 'frontend/generate_dungeon.cpp')
-rw-r--r-- | frontend/generate_dungeon.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp new file mode 100644 index 0000000..c6ddcbf --- /dev/null +++ b/frontend/generate_dungeon.cpp @@ -0,0 +1,55 @@ +#include <memory> +#include <functional> +#include <sqlite3.h> + +#include "backend/Dungeon.h" + +#include "generate_dungeon.h" +#include "Exception.h" + +using namespace std; + +class DB { + typedef unique_ptr<sqlite3, function<void(sqlite3*)>> unique_sqlite3; + typedef unique_ptr<sqlite3_stmt, function<void(sqlite3_stmt*)>> unique_sqlite3_stmt; + +public: + DB(const string & path) { + sqlite3 * db = NULL; + int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL); + this->db = { + db, + [] (sqlite3 * db) { + sqlite3_close_v2(db); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_open_v2"); + } + + unique_sqlite3_stmt prepare(const string & query) { + sqlite3_stmt * stmt = NULL; + int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL); + unique_sqlite3_stmt uniq_stmt = { + stmt, + [] (sqlite3_stmt * stmt) { + sqlite3_finalize(stmt); + }, + }; + if (ret != SQLITE_OK) + throw Exception("sqlite3_prepare_v2"); + return uniq_stmt; + } + +private: + unique_sqlite3 db = NULL; +}; + +unique_ptr<Dungeon> generate_dungeon() { + unique_ptr<Dungeon> dungeon = make_unique<Dungeon>(); + + DB db { "kerkersendraken.db" }; + + return dungeon; +} + |