From 8ebe23fa154e25e09e61fac17aedfb5be709d22c Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 2 Apr 2021 14:38:39 -0700 Subject: [PATCH] :sparkles: Saving/Loading --- CMakeLists.txt | 9 ++++++--- include/bank.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ include/user.hpp | 9 +++++++++ main.cpp | 13 +++++-------- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8758108..af7a92a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,16 +3,19 @@ project(main) set (CMAKE_CXX_STANDARD 17) if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) +set(CMAKE_BUILD_TYPE Release) endif() set(CMAKE_CXX_FLAGS "-Wall -Wextra") set(CMAKE_CXX_FLAGS_DEBUG "-g") set(CMAKE_CXX_FLAGS_RELEASE "-O3") + +find_package( Threads ) add_executable(${PROJECT_NAME} main.cpp) target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC third_party) -# add_subdirectory(third_party/drogon) -# target_link_libraries(${PROJECT_NAME} PRIVATE drogon) \ No newline at end of file +add_subdirectory(third_party/drogon) +target_link_libraries(${PROJECT_NAME} PRIVATE drogon) +target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_THREAD_LIBS_INIT} ) \ No newline at end of file diff --git a/include/bank.hpp b/include/bank.hpp index 7225707..c53c7f1 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include "parallel-hashmap/parallel_hashmap/phmap.h" #include "user.hpp" @@ -80,4 +81,43 @@ public: return state; } + + //NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS + void Save() const + { + Json::StreamWriterBuilder builder; + const std::unique_ptr writer(builder.newStreamWriter()); + + std::ofstream user_save("users.json"); + Json::Value temp; + for (const auto &u : users) + { + std::cout << u.first << '\n'; + temp[u.first] = u.second.Serialize(); + } + writer->write(temp, &user_save); + user_save.close(); + } + //NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS + void Load() + { + Json::CharReaderBuilder builder; + + Json::Value temp; + std::ifstream user_save("users.json"); + builder["collectComments"] = true; + JSONCPP_STRING errs; + if (!parseFromStream(builder, user_save, &temp, &errs)) + { + user_save.close(); + } + else + { + user_save.close(); + for (const auto &u : temp.getMemberNames()) + { + users.try_emplace(u, temp[u]["balance"].asUInt(), std::forward(temp[u]["password"].asString())); + } + } + } } Bank; \ No newline at end of file diff --git a/include/user.hpp b/include/user.hpp index 8f1b824..186ba51 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include struct User @@ -20,4 +21,12 @@ struct User * @param init_pass initial password */ User(uint_fast32_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {} + + Json::Value Serialize() const + { + Json::Value res; + res["balance"] = balance; + res["password"] = password; + return res; + } }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index f1524e1..1ebf72f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,12 @@ #include +#include +#include #include "bank.hpp" int main() { - Bank.admin_pass = "root"; - std::cout << Bank.AddUser("Twix", "pass123") << '\n'; - std::cout << Bank.GetBal("Twixy") << '\n'; - std::cout << Bank.AdminAddUser("root", "Jollymonsam", 2500, "pass123") << '\n'; - std::cout << Bank.GetBal("Jollymonsam") << '\n'; - std::cout << Bank.SendFunds("Jollymonsam", "Twix", 333, "pass123") << '\n'; - std::cout << Bank.GetBal("Twix") << " : " << Bank.GetBal("Jollymonsam") << '\n'; - + Bank.Load(); + Bank.SendFunds("0", "1", 50, "root"); + Bank.Save(); return 0; } \ No newline at end of file