#include "settings.h" #include #include #include namespace settings { const std::map dummySettings { {"weapon", "/usr/share/dmtool/items/weapons/"}, {"armor", "/usr/share/dmtool/items/armor/"}, {"spellcasting", "/usr/share/dmtool/spells/"}, {"monsters", "/usr/share/dmtool/monsters/"}, {"savedir", "~/.dmtool/"} }; // Update the input string. // Obtained from https://stackoverflow.com/a/23442780 void autoExpandEnvironmentVariables(std::string& text) { std::size_t tilde; while((tilde = text.find("~")) != std::string::npos) { text.replace(tilde, tilde+1, "${HOME}"); } static std::regex env("\\$(?:\\{([^}]+)\\}|([^/]+))"); std::smatch match; while(std::regex_search(text, match, env)) { std::string matchStr = match[1].str().empty()? match[2].str() : match[1].str(); auto s = getenv(matchStr.c_str()); const std::string var(s == NULL? "" : s); text.replace(match[0].first, match[0].second, var); } } // Returns the setting, or an empty string in the case of an error std::string getString(const std::string& key) { if(! dummySettings.contains(key)) { return ""; } std::string ret = dummySettings.at(key); autoExpandEnvironmentVariables(ret); return ret; } }