diff options
author | Your Name <you@example.com> | 2021-04-15 15:23:23 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-04-15 15:23:23 -0400 |
commit | dfce4d0398a8bafbb7ad7a31345af181c0269c09 (patch) | |
tree | 695162ff8cc25e146f52d9e26fe19ffa9934b3d6 /src/utils.cc | |
parent | 9034c3d2533177f7cb7a7ce939ec53f7fa63f60e (diff) | |
download | dmtool-dfce4d0398a8bafbb7ad7a31345af181c0269c09.tar.gz dmtool-dfce4d0398a8bafbb7ad7a31345af181c0269c09.tar.bz2 dmtool-dfce4d0398a8bafbb7ad7a31345af181c0269c09.zip |
Added spells
Diffstat (limited to 'src/utils.cc')
-rw-r--r-- | src/utils.cc | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/utils.cc b/src/utils.cc index 009754b..1df3308 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -1,5 +1,6 @@ #include "utils.h" #include "json.hpp" +#include "settings.h" #include <sstream> #include <fstream> #include <string> @@ -15,23 +16,35 @@ nlohmann::json utils::loadJson(const std::string& path) { return j; } +nlohmann::json utils::loadJson(const std::string& type, const std::string& name) { + for(auto data : utils::loadAllJson(settings::dummySettings.at(type))) { + if(data["name"] == name) { + return data; + } + } + throw std::invalid_argument("Unknown name: `" + name + "' for type `" + type + "'."); +} + void utils::saveJson(const nlohmann::json& data, const std::string& path) { std::ofstream f(path); f << std::setw(4) << data << std::endl; } -/*std::string utils::join(std::vector<std::string> parts, std::string joiner) { - std::stringstream out; - bool isFirst = true; - for(std::string p : parts) { - if(! isFirst) { - out << joiner; +static std::map<std::string, std::vector<nlohmann::json>> cache; + +std::vector<nlohmann::json> utils::loadAllJson(const std::string& directory) { + if(cache.contains(directory)) { + return cache[directory]; + } + std::vector<nlohmann::json> ret; + for(auto path : std::filesystem::recursive_directory_iterator(directory)) { + if(path.path().extension() == ".json") { + ret.push_back(utils::loadJson(path.path())); } - isFirst = false; - out << p; } - return out.str(); -}*/ + cache[directory] = ret; + return ret; +} // Accepts coins formatted "X Yp" where X is an integer and Y is any of c, s, e, g, p. int utils::coins2copper(const std::string& coins) { |