diff options
author | Your Name <you@example.com> | 2021-04-13 15:14:34 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-04-13 15:14:34 -0400 |
commit | 2ab51e507d620c4479e07ca0ec47d22c8c66bc90 (patch) | |
tree | 90906ecb043c01034280c767b83a88eb6df6956f /src/utils.h | |
download | dmtool-2ab51e507d620c4479e07ca0ec47d22c8c66bc90.tar.gz dmtool-2ab51e507d620c4479e07ca0ec47d22c8c66bc90.tar.bz2 dmtool-2ab51e507d620c4479e07ca0ec47d22c8c66bc90.zip |
Initial commit
Diffstat (limited to 'src/utils.h')
-rw-r--r-- | src/utils.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..f7366dc --- /dev/null +++ b/src/utils.h @@ -0,0 +1,54 @@ +#pragma once +#include "json.hpp" +#include <string> +#include <vector> +#include <map> +#include <sstream> + + +namespace utils { + nlohmann::json loadJson(const std::string& path); + + void saveJson(const nlohmann::json& data, const std::string& path); + + template<typename Container> std::string join(Container parts, std::string joiner) { + std::stringstream out; + bool isFirst = true; + for(std::string p : parts) { + if(! isFirst) { + out << joiner; + } + isFirst = false; + out << p; + } + return out.str(); + } + + const std::map<std::string, int> cpValue { + {"cp", 1}, + {"sp", 10}, + {"ep", 50}, + {"gp", 100}, + {"pp", 1000} + }; + + // Accepts coins formatted "X Yp" where X is an integer and Y is any of c, s, e, g, p. + int coins2copper(const std::string& coins); + + // Greedily selects highest coin values to minimize total number of coins + // Returns a vector of pairs mapping coin type to coint + std::vector<std::pair<std::string, int>> copper2coins(int coppers); + + std::string getCostString(int coppers); + + // Recursively loads all .json files under directory + /*std::vector<Item> loadAll(std::string directory) { + std::vector<Item> items; + for(auto path : std::filesystem::recursive_directory_iterator(directory)) { + if(path.path().extension() == ".json") { + items.push_back(Item::instance(loadJson(path.path()))); + } + } + return items; + }*/ +} |