aboutsummaryrefslogtreecommitdiff
path: root/src/settings.cc
diff options
context:
space:
mode:
authorYour Name <you@example.com>2021-04-19 13:46:14 -0400
committerYour Name <you@example.com>2021-04-19 13:46:14 -0400
commit2cae1aa33f80ce0844fb54a84ce103146a7fe7ad (patch)
tree26945223a2672c5a80975a7b1998622da6edcc28 /src/settings.cc
parent4618763c0e3a723bf4bb43c7b9edbce87240e0af (diff)
downloaddmtool-2cae1aa33f80ce0844fb54a84ce103146a7fe7ad.tar.gz
dmtool-2cae1aa33f80ce0844fb54a84ce103146a7fe7ad.tar.bz2
dmtool-2cae1aa33f80ce0844fb54a84ce103146a7fe7ad.zip
Started earnest work on cli
Diffstat (limited to 'src/settings.cc')
-rw-r--r--src/settings.cc41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/settings.cc b/src/settings.cc
new file mode 100644
index 0000000..e9a6165
--- /dev/null
+++ b/src/settings.cc
@@ -0,0 +1,41 @@
+#include "settings.h"
+#include <map>
+#include <cstdlib>
+#include <regex>
+
+namespace settings {
+ const std::map<std::string, std::string> dummySettings {
+ {"weapon", "parser/items/weapons/"},
+ {"armor", "parser/items/armor/"},
+ {"spellcasting", "parser/spells/"},
+ {"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;
+ }
+}