diff options
Diffstat (limited to 'src/utils.cc')
-rw-r--r-- | src/utils.cc | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/utils.cc b/src/utils.cc index 54fa38e..6f25c46 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -10,7 +10,9 @@ #include <map> #include <algorithm> -nlohmann::json utils::loadJson(const std::string& path) { +namespace fs = std::filesystem; + +nlohmann::json utils::loadJson(const fs::path& path) { std::ifstream f(path); nlohmann::json j; f >> j; @@ -35,7 +37,15 @@ nlohmann::json utils::findByName(const std::string& name) { throw std::invalid_argument("Could not find data matching: " + name); } -void utils::saveJson(const nlohmann::json& data, const std::string& path) { +int utils::parseInt(const std::string& s) { + try { + return std::stoi(s); + } catch(std::exception& e) { + throw std::runtime_error("An integer was expected but " + s + " was given"); + } +} + +void utils::saveJson(const nlohmann::json& data, const fs::path& path) { std::ofstream f(path); f << std::setw(4) << data << std::endl; } @@ -47,7 +57,7 @@ std::vector<nlohmann::json> utils::loadAllJson(const std::string& directory) { return cache[directory]; } std::vector<nlohmann::json> ret; - for(auto path : std::filesystem::recursive_directory_iterator(directory)) { + for(auto path : fs::recursive_directory_iterator(directory)) { if(path.path().extension() == ".json") { ret.push_back(utils::loadJson(path.path())); } |