aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/CMakeLists.txt3
-rw-r--r--src/crepe/DB.cpp67
-rw-r--r--src/crepe/DB.h34
-rw-r--r--src/crepe/api/SaveManager.cpp11
-rw-r--r--src/crepe/api/SaveManager.h11
5 files changed, 124 insertions, 2 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index 97f987a..750a47d 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -12,8 +12,8 @@ target_sources(crepe PUBLIC
CollisionSystem.cpp
Collider.cpp
SDLContext.cpp
-
RenderSystem.cpp
+ DB.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -33,6 +33,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
RenderSystem.h
ValueBroker.h
ValueBroker.hpp
+ DB.h
)
add_subdirectory(api)
diff --git a/src/crepe/DB.cpp b/src/crepe/DB.cpp
new file mode 100644
index 0000000..b8448a7
--- /dev/null
+++ b/src/crepe/DB.cpp
@@ -0,0 +1,67 @@
+#include <cstring>
+
+#include "util/log.h"
+
+#include "DB.h"
+
+using namespace std;
+using namespace crepe;
+
+DB::DB(const char * path) {
+ dbg_trace();
+ int ret;
+
+ // init database struct
+ libdb::DB * db;
+ if ((ret = libdb::db_create(&db, NULL, 0)) != 0)
+ throw nullptr; // TODO: exception
+ this->db = { db, [] (libdb::DB * db) { db->close(db, 0); } };
+
+ // load or create database file
+ if ((ret = this->db->open(this->db.get(), NULL, path, NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0) {
+ throw nullptr;
+ }
+
+ // create cursor
+ libdb::DBC * cursor;
+ if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0) {
+ throw nullptr;
+ }
+ this->cursor = { cursor, [] (libdb::DBC * cursor) { cursor->close(cursor); } };
+}
+
+
+libdb::DBT DB::to_thing(const string & thing) {
+ libdb::DBT thang;
+ memset(&thang, 0, sizeof(libdb::DBT));
+ thang.data = (void *) thing.data();
+ thang.size = thing.size();
+ return thang;
+}
+
+string DB::get(const string & key) {
+ libdb::DBT db_key = this->to_thing(key);
+ libdb::DBT db_val;
+ memset(&db_val, 0, sizeof(libdb::DBT));
+
+ // int ret = this->cursor->get(this->cursor.get(), NULL, &db_key, &db_val);
+ return "";
+}
+
+void DB::set(const string & key, const string & value) {
+ libdb::DBT db_key = this->to_thing(key);
+ libdb::DBT db_val = this->to_thing(value);
+ int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0);
+ // TODO: check flags
+ // TODO: check ret
+}
+
+bool DB::has(const std::string & key) {
+ try {
+ this->get(key);
+ } catch (...) {
+ return false;
+ }
+ return true;
+}
+
diff --git a/src/crepe/DB.h b/src/crepe/DB.h
new file mode 100644
index 0000000..010ef42
--- /dev/null
+++ b/src/crepe/DB.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <string>
+#include <functional>
+#include <memory>
+
+namespace libdb {
+extern "C" {
+#include <db.h>
+}
+}
+
+namespace crepe {
+
+class DB {
+public:
+ DB(const char * path);
+ virtual ~DB() = default;
+
+public:
+ std::string get(const std::string & key);
+ void set(const std::string & key, const std::string & value);
+ bool has(const std::string & key);
+
+private:
+ std::unique_ptr<libdb::DB, std::function<void(libdb::DB *)>> db;
+ std::unique_ptr<libdb::DBC, std::function<void(libdb::DBC *)>> cursor;
+
+private:
+ libdb::DBT to_thing(const std::string & thing);
+};
+
+}
+
diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp
index a05e2c7..83ff0fa 100644
--- a/src/crepe/api/SaveManager.cpp
+++ b/src/crepe/api/SaveManager.cpp
@@ -1,8 +1,19 @@
+#include "../DB.h"
+#include "util/log.h"
+
#include "SaveManager.h"
+using namespace std;
+using namespace crepe;
using namespace crepe::api;
+SaveManager::SaveManager() {
+ dbg_trace();
+ this->db = make_unique<DB>("./save.crepe.db");
+}
+
SaveManager & SaveManager::get_instance() {
+ dbg_trace();
static SaveManager instance;
return instance;
}
diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h
index 78cd4ba..110735d 100644
--- a/src/crepe/api/SaveManager.h
+++ b/src/crepe/api/SaveManager.h
@@ -1,7 +1,13 @@
#pragma once
+#include <memory>
+
#include "../ValueBroker.h"
+namespace crepe {
+class DB;
+}
+
namespace crepe::api {
class SaveManager {
@@ -22,7 +28,7 @@ public:
bool has(const char * key);
private:
- SaveManager() = default;
+ SaveManager();
virtual ~SaveManager() = default;
public:
@@ -32,6 +38,9 @@ public:
SaveManager(SaveManager &&) = delete;
SaveManager & operator = (const SaveManager &) = delete;
SaveManager & operator = (SaveManager &&) = delete;
+
+private:
+ std::unique_ptr<DB> db = nullptr;
};
}