juk3n/fluffypublic Fork
⑂ 523550e
Commits
⬇ Clone ▾
git clone https://git.rickub.com/juk3n/fluffy.git
git clone ssh://git@rickub.com/juk3n/fluffy.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

added basic command handling

Adrian Duraj committed 2024-06-15T12:04:53+02:00 Browse files
523550e parent: aa0bddd
modified src/main.cpp +36 -22
@@ -56,6 +56,40 @@ void initialize_database(sqlite3* database) {
5656 printMessage("Database Initialized");
5757 }
5858
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+
5993 int main(int argc, char const *argv[]) {
6094 bool newCreation = not std::filesystem::exists("data.db");
6195
@@ -89,34 +123,14 @@ int main(int argc, char const *argv[]) {
89123 const unsigned char* gameName = sqlite3_column_text(stmt, 1);
90124 const unsigned char* gamePath = sqlite3_column_text(stmt, 2);
91125
92- std::cout << gameName << ": " << gamePath << std::endl;
93126 std::string name{reinterpret_cast<const char*>(gameName)};
94127 std::string path{reinterpret_cast<const char*>(gamePath)};
95128 games[name] = path;
96129 }
97130 sqlite3_finalize(stmt);
98131
99- if (argc == 2) {
100- return 0;
101- }
102-
103- if (argc == 4) {
104- std::string command{ argv[1] };
105- std::string gameName{ argv[2] };
106- auto localPath = std::filesystem::path(argv[3]);
107- std::string globalPath = std::filesystem::absolute(localPath).lexically_normal().string();
108-
109- std::string temp{};
110- for (const auto a : globalPath) {
111- if (a != ' ') {
112- temp += a;
113- }
114- else {
115- temp += "\' \'";
116- }
117- }
118-
119- addGame(database, games.size() + 1, gameName, temp);
132+ if (argc > 1) {
133+ handleCommand(argc, argv, database);
120134 return 0;
121135 }
122136
@@ -56,6 +56,40 @@ void initialize_database(sqlite3* database) {
56 printMessage("Database Initialized"); 56 printMessage("Database Initialized");
57 }57 }
58 58
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+
59 int main(int argc, char const *argv[]) {93 int main(int argc, char const *argv[]) {
60 bool newCreation = not std::filesystem::exists("data.db");94 bool newCreation = not std::filesystem::exists("data.db");
61 95
@@ -89,34 +123,14 @@ int main(int argc, char const *argv[]) {
89 const unsigned char* gameName = sqlite3_column_text(stmt, 1);123 const unsigned char* gameName = sqlite3_column_text(stmt, 1);
90 const unsigned char* gamePath = sqlite3_column_text(stmt, 2);124 const unsigned char* gamePath = sqlite3_column_text(stmt, 2);
91 125
92- std::cout << gameName << ": " << gamePath << std::endl;
93 std::string name{reinterpret_cast<const char*>(gameName)};126 std::string name{reinterpret_cast<const char*>(gameName)};
94 std::string path{reinterpret_cast<const char*>(gamePath)};127 std::string path{reinterpret_cast<const char*>(gamePath)};
95 games[name] = path;128 games[name] = path;
96 }129 }
97 sqlite3_finalize(stmt);130 sqlite3_finalize(stmt);
98 131
99- if (argc == 2) {132+ if (argc > 1) {
100- return 0;133+ handleCommand(argc, argv, database);
101- }
102-
103- if (argc == 4) {
104- std::string command{ argv[1] };
105- std::string gameName{ argv[2] };
106- auto localPath = std::filesystem::path(argv[3]);
107- std::string globalPath = std::filesystem::absolute(localPath).lexically_normal().string();
108-
109- std::string temp{};
110- for (const auto a : globalPath) {
111- if (a != ' ') {
112- temp += a;
113- }
114- else {
115- temp += "\' \'";
116- }
117- }
118-
119- addGame(database, games.size() + 1, gameName, temp);
120 return 0;134 return 0;
121 }135 }
122 136