5 adding the game with command fluffy add game name local path #6
Merged@github-juk3n wants to merge
5-adding-the-game-with-command-fluffy-add-game_name-local_path into mainMerged as
9c36b113a6fd.2 files changed
modified
CMakeLists.txt +5 -4 | @@ -1,11 +1,14 @@ | ||
| 1 | 1 | cmake_minimum_required(VERSION 3.14) |
| 2 | +set(CMAKE_CXX_STANDARD 20) | |
| 3 | +set(CMAKE_CXX_STANDARD_REQUIRED True) | |
| 4 | +project(fluffy LANGUAGES CXX VERSION 1.0.0) | |
| 2 | 5 | |
| 3 | 6 | include(FetchContent) |
| 4 | 7 | |
| 5 | 8 | set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) |
| 6 | 9 | FetchContent_Declare(ftxui |
| 7 | 10 | GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui |
| 8 | - GIT_TAG v5.0.0 | |
| 11 | + GIT_TAG v4.0.0 | |
| 9 | 12 | ) |
| 10 | 13 | FetchContent_MakeAvailable(ftxui) |
| 11 | 14 | if(NOT ftxui_POPULATED) |
| @@ -13,10 +16,8 @@ if(NOT ftxui_POPULATED) | ||
| 13 | 16 | add_subdirectory(${ftxui_SOURCE_DIR} ${ftxui_BINARY_DIR} EXCLUDE_FROM_ALL) |
| 14 | 17 | endif() |
| 15 | 18 | |
| 16 | -project(fluffy LANGUAGES CXX VERSION 1.0.0) | |
| 17 | 19 | |
| 18 | -set(CMAKE_CXX_STANDARD 20) | |
| 19 | -set(CMAKE_CXX_STANDARD_REQUIRED True) | |
| 20 | + | |
| 20 | 21 | |
| 21 | 22 | |
| 22 | 23 | find_package(SQLite3 REQUIRED) |
| @@ -1,11 +1,14 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.14) | 1 | cmake_minimum_required(VERSION 3.14) |
| 2 | +set(CMAKE_CXX_STANDARD 20) | ||
| 3 | +set(CMAKE_CXX_STANDARD_REQUIRED True) | ||
| 4 | +project(fluffy LANGUAGES CXX VERSION 1.0.0) | ||
| 2 | 5 | ||
| 3 | include(FetchContent) | 6 | include(FetchContent) |
| 4 | 7 | ||
| 5 | set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) | 8 | set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) |
| 6 | FetchContent_Declare(ftxui | 9 | FetchContent_Declare(ftxui |
| 7 | GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui | 10 | GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui |
| 8 | - GIT_TAG v5.0.0 | 11 | + GIT_TAG v4.0.0 |
| 9 | ) | 12 | ) |
| 10 | FetchContent_MakeAvailable(ftxui) | 13 | FetchContent_MakeAvailable(ftxui) |
| 11 | if(NOT ftxui_POPULATED) | 14 | if(NOT ftxui_POPULATED) |
| @@ -13,10 +16,8 @@ if(NOT ftxui_POPULATED) | |||
| 13 | add_subdirectory(${ftxui_SOURCE_DIR} ${ftxui_BINARY_DIR} EXCLUDE_FROM_ALL) | 16 | add_subdirectory(${ftxui_SOURCE_DIR} ${ftxui_BINARY_DIR} EXCLUDE_FROM_ALL) |
| 14 | endif() | 17 | endif() |
| 15 | 18 | ||
| 16 | -project(fluffy LANGUAGES CXX VERSION 1.0.0) | ||
| 17 | 19 | ||
| 18 | -set(CMAKE_CXX_STANDARD 20) | 20 | + |
| 19 | -set(CMAKE_CXX_STANDARD_REQUIRED True) | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | find_package(SQLite3 REQUIRED) | 23 | find_package(SQLite3 REQUIRED) |
modified
src/main.cpp +44 -8 | @@ -43,22 +43,54 @@ void execute_sql_command(sqlite3* database, std::string command) { | ||
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | -void addGame(sqlite3* database, std::string name, std::string path) { | |
| 47 | - static int val = 1; | |
| 46 | +void addGame(sqlite3* database, int id, std::string name, std::string path) { | |
| 48 | 47 | std::string command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES ("; |
| 49 | - command += std::to_string(val) + ", '" + name + "', \"" + path + "\");"; | |
| 50 | - val++; | |
| 48 | + command += std::to_string(id) + ", '" + name + "', \"" + path + "\");"; | |
| 51 | 49 | execute_sql_command(database, command); |
| 52 | 50 | } |
| 53 | 51 | |
| 54 | 52 | void initialize_database(sqlite3* database) { |
| 55 | 53 | execute_sql_command(database, database_initialization_command); |
| 56 | - addGame(database, "Devastating Fog", ""); | |
| 57 | - addGame(database, "No Pirates Here!", ""); | |
| 54 | + addGame(database, 1, "Devastating Fog", ""); | |
| 55 | + addGame(database, 2, "No Pirates Here!", ""); | |
| 58 | 56 | printMessage("Database Initialized"); |
| 59 | 57 | } |
| 60 | 58 | |
| 61 | -int main() { | |
| 59 | +void handleCommand(int argc, char const *argv[], sqlite3* database) { | |
| 60 | + std::string command{}; | |
| 61 | + switch (argc) { | |
| 62 | + case 2: | |
| 63 | + command = argv[1]; | |
| 64 | + if (command == "show") { | |
| 65 | + for (const auto& [game, path] : games) { | |
| 66 | + std::cout << game << ": " << path << std::endl; | |
| 67 | + } | |
| 68 | + } | |
| 69 | + break; | |
| 70 | + case 4: | |
| 71 | + command = argv[1]; | |
| 72 | + if (command == "add") { | |
| 73 | + std::string gameName{ argv[2] }; | |
| 74 | + auto localPath = std::filesystem::path(argv[3]); | |
| 75 | + std::string globalPath = std::filesystem::absolute(localPath).lexically_normal().string(); | |
| 76 | + | |
| 77 | + std::string temp{}; | |
| 78 | + for (const auto a : globalPath) { | |
| 79 | + if (a != ' ') { | |
| 80 | + temp += a; | |
| 81 | + } | |
| 82 | + else { | |
| 83 | + temp += "\' \'"; | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 87 | + addGame(database, games.size() + 1, gameName, temp); | |
| 88 | + } | |
| 89 | + break; | |
| 90 | + } | |
| 91 | +} | |
| 92 | + | |
| 93 | +int main(int argc, char const *argv[]) { | |
| 62 | 94 | bool newCreation = not std::filesystem::exists("data.db"); |
| 63 | 95 | |
| 64 | 96 | char* messageError; |
| @@ -91,13 +123,17 @@ int main() { | ||
| 91 | 123 | const unsigned char* gameName = sqlite3_column_text(stmt, 1); |
| 92 | 124 | const unsigned char* gamePath = sqlite3_column_text(stmt, 2); |
| 93 | 125 | |
| 94 | - std::cout << gameName << ": " << gamePath << std::endl; | |
| 95 | 126 | std::string name{reinterpret_cast<const char*>(gameName)}; |
| 96 | 127 | std::string path{reinterpret_cast<const char*>(gamePath)}; |
| 97 | 128 | games[name] = path; |
| 98 | 129 | } |
| 99 | 130 | sqlite3_finalize(stmt); |
| 100 | 131 | |
| 132 | + if (argc > 1) { | |
| 133 | + handleCommand(argc, argv, database); | |
| 134 | + return 0; | |
| 135 | + } | |
| 136 | + | |
| 101 | 137 | int exit_id = games.size(); |
| 102 | 138 | int choice = 0; |
| 103 | 139 | do { |
| @@ -43,22 +43,54 @@ void execute_sql_command(sqlite3* database, std::string command) { | |||
| 43 | } | 43 | } |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | -void addGame(sqlite3* database, std::string name, std::string path) { | 46 | +void addGame(sqlite3* database, int id, std::string name, std::string path) { |
| 47 | - static int val = 1; | ||
| 48 | std::string command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES ("; | 47 | std::string command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES ("; |
| 49 | - command += std::to_string(val) + ", '" + name + "', \"" + path + "\");"; | 48 | + command += std::to_string(id) + ", '" + name + "', \"" + path + "\");"; |
| 50 | - val++; | ||
| 51 | execute_sql_command(database, command); | 49 | execute_sql_command(database, command); |
| 52 | } | 50 | } |
| 53 | 51 | ||
| 54 | void initialize_database(sqlite3* database) { | 52 | void initialize_database(sqlite3* database) { |
| 55 | execute_sql_command(database, database_initialization_command); | 53 | execute_sql_command(database, database_initialization_command); |
| 56 | - addGame(database, "Devastating Fog", ""); | 54 | + addGame(database, 1, "Devastating Fog", ""); |
| 57 | - addGame(database, "No Pirates Here!", ""); | 55 | + addGame(database, 2, "No Pirates Here!", ""); |
| 58 | printMessage("Database Initialized"); | 56 | printMessage("Database Initialized"); |
| 59 | } | 57 | } |
| 60 | 58 | ||
| 61 | -int main() { | 59 | +void handleCommand(int argc, char const *argv[], sqlite3* database) { |
| 60 | + std::string command{}; | ||
| 61 | + switch (argc) { | ||
| 62 | + case 2: | ||
| 63 | + command = argv[1]; | ||
| 64 | + if (command == "show") { | ||
| 65 | + for (const auto& [game, path] : games) { | ||
| 66 | + std::cout << game << ": " << path << std::endl; | ||
| 67 | + } | ||
| 68 | + } | ||
| 69 | + break; | ||
| 70 | + case 4: | ||
| 71 | + command = argv[1]; | ||
| 72 | + if (command == "add") { | ||
| 73 | + std::string gameName{ argv[2] }; | ||
| 74 | + auto localPath = std::filesystem::path(argv[3]); | ||
| 75 | + std::string globalPath = std::filesystem::absolute(localPath).lexically_normal().string(); | ||
| 76 | + | ||
| 77 | + std::string temp{}; | ||
| 78 | + for (const auto a : globalPath) { | ||
| 79 | + if (a != ' ') { | ||
| 80 | + temp += a; | ||
| 81 | + } | ||
| 82 | + else { | ||
| 83 | + temp += "\' \'"; | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + addGame(database, games.size() + 1, gameName, temp); | ||
| 88 | + } | ||
| 89 | + break; | ||
| 90 | + } | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +int main(int argc, char const *argv[]) { | ||
| 62 | bool newCreation = not std::filesystem::exists("data.db"); | 94 | bool newCreation = not std::filesystem::exists("data.db"); |
| 63 | 95 | ||
| 64 | char* messageError; | 96 | char* messageError; |
| @@ -91,13 +123,17 @@ int main() { | |||
| 91 | const unsigned char* gameName = sqlite3_column_text(stmt, 1); | 123 | const unsigned char* gameName = sqlite3_column_text(stmt, 1); |
| 92 | const unsigned char* gamePath = sqlite3_column_text(stmt, 2); | 124 | const unsigned char* gamePath = sqlite3_column_text(stmt, 2); |
| 93 | 125 | ||
| 94 | - std::cout << gameName << ": " << gamePath << std::endl; | ||
| 95 | std::string name{reinterpret_cast<const char*>(gameName)}; | 126 | std::string name{reinterpret_cast<const char*>(gameName)}; |
| 96 | std::string path{reinterpret_cast<const char*>(gamePath)}; | 127 | std::string path{reinterpret_cast<const char*>(gamePath)}; |
| 97 | games[name] = path; | 128 | games[name] = path; |
| 98 | } | 129 | } |
| 99 | sqlite3_finalize(stmt); | 130 | sqlite3_finalize(stmt); |
| 100 | 131 | ||
| 132 | + if (argc > 1) { | ||
| 133 | + handleCommand(argc, argv, database); | ||
| 134 | + return 0; | ||
| 135 | + } | ||
| 136 | + | ||
| 101 | int exit_id = games.size(); | 137 | int exit_id = games.size(); |
| 102 | int choice = 0; | 138 | int choice = 0; |
| 103 | do { | 139 | do { |