Borke out some of the settings sqlite code to it's own module.

This commit is contained in:
Linus Probert 2018-04-16 21:19:26 +02:00
parent f65f135bdf
commit cab3b501fc
4 changed files with 79 additions and 21 deletions

View File

@ -121,6 +121,7 @@ add_executable(breakhack
src/vector2d
src/map_room_modifiers
src/sqlite3
src/db
src/settings
)

41
src/db.c Normal file
View File

@ -0,0 +1,41 @@
#include "db.h"
#include "sqlite3.h"
#include "util.h"
bool
db_open(const char *file, sqlite3 **db)
{
debug("Opening DB: %s", file);
int result = sqlite3_open(file, db);
if (result) {
error("Failed to open db: %s", sqlite3_errmsg(db));
}
return result == 0;
}
bool
db_execute_stmnt(const char *stmnt,
sqlite3 *db,
int(*cb)(void*, int, char**, char**),
void *cb_arg)
{
debug("Executing sql: %s", stmnt);
char *errorMsg = NULL;
if (sqlite3_exec(db, stmnt, cb, cb_arg, &errorMsg)) {
error("Faled to execute statement: %s", stmnt);
error("Sqlite3: %s", errorMsg);
sqlite3_free(errorMsg);
return false;
}
return true;
}
void db_close(sqlite3 **db)
{
char *fname = sqlite3_db_filename(*db, NULL);
debug("Closing db: %s", fname);
sqlite3_close(*db);
*db = NULL;
}

19
src/db.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef DB_H_
#define DB_H_
#include <stdbool.h>
#include "sqlite3.h"
bool
db_open(const char *file, sqlite3 **db);
bool
db_execute_stmnt(const char *stmnt,
sqlite3 *db,
int(*cb)(void*, int, char**, char**),
void *cb_arg);
void
db_close(sqlite3 **db);
#endif // DB_H_

View File

@ -22,6 +22,7 @@
#include "sqlite3.h"
#include "util.h"
#include "defines.h"
#include "db.h"
#define SETTINGS_DB_FILE ".data.db"
@ -43,14 +44,8 @@ execute_statement(const char *stmt,
int(*cb)(void*, int, char**, char**),
void *cb_arg)
{
char *errorMsg;
set_default_settings();
if (sqlite3_exec(db, stmt, cb, cb_arg, &errorMsg)) {
error("Faled to execture statement: %s", stmt);
error("Sqlite3: %s", errorMsg);
sqlite3_free(errorMsg);
sqlite3_close(db);
if (!db_execute_stmnt(stmt, db, cb, cb_arg)) {
db_close(&db);
fatal("Exiting");
}
}
@ -77,7 +72,8 @@ load_settings_cb(void *unused, int count, char **values, char **colNames)
if (!strcmp(KEY_SOUND_ENABLED, values[i])) {
settings.sound_enabled = (bool)atoi(values[i + 1]);
i += 2;
} else if (!strcmp(KEY_MUSIC_ENABLED, values[i])) {
}
else if (!strcmp(KEY_MUSIC_ENABLED, values[i])) {
settings.music_enabled = (bool)atoi(values[i + 1]);
i += 2;
}
@ -94,12 +90,11 @@ load_settings(void)
void
settings_init(void)
{
int result = sqlite3_open(SETTINGS_DB_FILE, &db);
if (result) {
error("Failed to open settings db: %s", sqlite3_errmsg(db));
sqlite3_close(db);
if (!db_open(SETTINGS_DB_FILE, &db)) {
db_close(&db);
fatal("Exiting");
}
set_default_settings();
create_tables();
load_settings();
}
@ -107,6 +102,8 @@ settings_init(void)
static void
save_setting_int(const char *key, int value)
{
// TODO(Linus): Move this into db.c, probably using varargs
const char *stmtStr = "INSERT OR REPLACE INTO settings_int(key, value) values (?, ?)";
const char *pzTest;
sqlite3_stmt *stmt;
@ -147,5 +144,5 @@ void
settings_close(void)
{
settings_save();
sqlite3_close(db);
db_close(&db);
}