summaryrefslogtreecommitdiff
path: root/src/misc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/misc.cpp')
-rw-r--r--src/misc.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/misc.cpp b/src/misc.cpp
index 1984477f7..861e6ee20 100644
--- a/src/misc.cpp
+++ b/src/misc.cpp
@@ -23,6 +23,7 @@
#include <climits>
#include <numeric>
#include <random>
+#include <filesystem>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef _WIN32
@@ -808,3 +809,26 @@ void Anope::UpdateTime()
CurTimeNs = tv.tv_usec * 1000;
#endif
}
+
+Anope::string Anope::Expand(const Anope::string& base, const Anope::string& fragment)
+{
+ // The fragment is an absolute path, don't modify it.
+ if (std::filesystem::path(fragment.str()).is_absolute())
+ return fragment;
+
+#ifdef _WIN32
+ static constexpr const char separator = '\\';
+#else
+ static constexpr const char separator = '/';
+#endif
+
+ // The fragment is relative to a home directory, expand that.
+ if (!fragment.compare(0, 2, "~/", 2))
+ {
+ const auto *homedir = getenv("HOME");
+ if (homedir && *homedir)
+ return Anope::printf("%s%c%s", homedir, separator, fragment.c_str() + 2);
+ }
+
+ return Anope::printf("%s%c%s", base.c_str(), separator, fragment.c_str());
+}