diff options
author | Your Name <you@example.com> | 2021-05-06 14:13:28 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-06 14:13:28 -0400 |
commit | 9f3802690f9dd9452e96d1d7a879291978d66e35 (patch) | |
tree | 6d6c17b39abdb9490119241bc4fc061744b46d7d /src/utils.cc | |
parent | 2a9f262e6db5906db445d465e500d7ba8c90fab3 (diff) | |
download | dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.tar.gz dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.tar.bz2 dmtool-9f3802690f9dd9452e96d1d7a879291978d66e35.zip |
Refactoring
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())); } |