diff --git a/src/db.c b/src/db.c index b89711a..0bcbcc5 100644 --- a/src/db.c +++ b/src/db.c @@ -13,12 +13,34 @@ Fonctions d'interaction avec la base de données #include "include/struct.h" +char* get_db_path() { + #ifndef DB_FILE + //! We should check that $HOME/.config already exists even if it should be the case + char* base_path = ".config/takl.sqlite3"; + + char* home_dir = getenv("HOME"); + assert(home_dir != NULL); + + char* db_path = malloc(sizeof(char)*(strlen(base_path)+strlen(home_dir)+1)); + sprintf(db_path, "%s/%s", home_dir, base_path); + + return db_path; + #else + char* db_path = malloc(sizeof(char)*(strlen(DB_FILE)+1)); + memcpy(db_path, DB_FILE, sizeof(char)*(strlen(DB_FILE)+1)); + + return db_path; + #endif +} + + sqlite3* get_db() { sqlite3* db; + char* db_path = get_db_path(); - if (access(DB_FILE, F_OK) != 0) { + if (access(db_path, F_OK) != 0) { // Create DB - sqlite3_open(DB_FILE, &db); + sqlite3_open(db_path, &db); char* zErrMsg = NULL; int ret = sqlite3_exec( @@ -44,11 +66,12 @@ sqlite3* get_db() { printf(GREEN "OK" RESET " Base de données créée\n"); } - int ret = sqlite3_open(DB_FILE, &db); + int ret = sqlite3_open(db_path, &db); if (ret != SQLITE_OK) { fprintf(stderr, "(get_db) Unable to open db\n"); } + free(db_path); return db; } diff --git a/src/include/config.h b/src/include/config.h index 61c5dc0..fead9da 100644 --- a/src/include/config.h +++ b/src/include/config.h @@ -2,7 +2,9 @@ #define DEF_CONFIG_H #define VERSION "0.0.0" -#define DB_FILE "takl.sqlite3" + +// By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here +//#define DB_FILE "takl.sqlite3" #define MAX_TASK_ID 10000 // max is set to MAX_TASK_ID-1 #define NEW_TASK_ID_MAX_RETRIES 10000 // number of retries before giving up