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:
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user