diff options
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/sqliteinterface.cpp | 23 |
2 files changed, 19 insertions, 8 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c3186df..85a79c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,9 +43,9 @@ add_subdirectory(extern/pybind11) find_package(Ytdlp REQUIRED RUNTIME) if(UNIX) - file(TO_CMAKE_PATH "$ENV{HOME}/.local/share/${PROJECT_NAME}/" DEF_APPDATA) + file(TO_CMAKE_PATH "$ENV{HOME}/.local/share/${PROJECT_NAME}" DEF_APPDATA) elseif(WIN32) - file(TO_CMAKE_PATH "$ENV{LOCALAPPDATA}\\${PROJECT_NAME}\\" DEF_APPDATA) + file(TO_CMAKE_PATH "$ENV{LOCALAPPDATA}\\${PROJECT_NAME}" DEF_APPDATA) else() message(SEND_ERROR "OS not recognized !") file(TO_CMAKE_PATH "./data" DEF_APPDATA) diff --git a/src/sqliteinterface.cpp b/src/sqliteinterface.cpp index 96e4020..1ae59be 100644 --- a/src/sqliteinterface.cpp +++ b/src/sqliteinterface.cpp @@ -2,25 +2,36 @@ #include <iostream> #include <cstring> #include <filesystem> +#include <stdexcept> #include "sqliteinterface.hpp" +namespace fs = std::filesystem; +using namespace std; + void openDB() { - sqlite3 *db; + fs::path appdata = DEF_APPDATA; + if ( ! fs::exists(appdata) ) { + error_code ec; + fs::create_directories(appdata, ec); + cout << ec << endl; + } + cout << fs::exists(appdata) << endl; + + sqlite3 *db; - char filename[] = DEF_APPDATA; - strcat(filename, "ytcpp.db"); - int rc = sqlite3_open(filename, &db); + fs::path filename = appdata / "ytcpp.db"; + int rc = sqlite3_open(filename.c_str(), &db); if( rc ) { char errmsg[] = "Can't open database: "; strcat(errmsg, sqlite3_errmsg(db)); - std::cerr << errmsg << std::endl; + throw runtime_error(errmsg); } else { std::cout << "Opened database successfully" << std::endl; - sqlite3_close(db); } + sqlite3_close(db); } \ No newline at end of file |