1
0
mirror of https://github.com/anope/anope.git synced 2026-06-26 03:26:38 +02:00

Allow using absolute paths in more places.

This commit is contained in:
Sadie Powell
2024-03-19 15:10:25 +00:00
parent 1575dea5b9
commit fde3438ef2
13 changed files with 69 additions and 27 deletions
+24
View File
@@ -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());
}