diff --git a/README.md b/README.md index ca3ccfe..d3961b8 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ And also my first serious C++ project. ## Roadmap - [X] Basic K/V store - [X] Remove/add entries -- [ ] Serialisation and saving +- [X] Serialisation and saving - [ ] Networking diff --git a/main.cpp b/main.cpp index 6eb50c7..30711a3 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,14 @@ void save_database(Db& database, std::string file_name) { db_file.close(); } +void load_database(Db& database, std::string file_name) { + std::ifstream db_file(file_name); + std::string raw_file_text; + db_file >> raw_file_text; + db_file.close(); + database = nlohmann::json::parse(raw_file_text); +} + void add_entry(Db& database, std::string key, std::string value) { database.push_back({key, value}); } @@ -45,6 +53,10 @@ void prompt_user(Db& database) { std::string file; std::cin >> file; save_database(database, file); + } else if (mode == "load") { + std::string file; + std::cin >> file; + load_database(database, file); } else { prompt_user(database); } @@ -52,12 +64,6 @@ void prompt_user(Db& database) { int main() { Db database; - - database = { - { "name", "username" }, - { "john", "unwanted_guest"}, - { "laith", "ducc" } - }; while (true) { for (std::vector entry: database) {