aboutsummaryrefslogtreecommitdiff
path: root/src/utils.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.cc')
-rw-r--r--src/utils.cc33
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) {