From 9f3802690f9dd9452e96d1d7a879291978d66e35 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 6 May 2021 14:13:28 -0400 Subject: Refactoring --- src/utils.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/utils.cc') 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 #include -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 utils::loadAllJson(const std::string& directory) { return cache[directory]; } std::vector 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())); } -- cgit v1.2.3