Caching bin path
This commit is contained in:
parent
0ab009e17a
commit
cf13499158
|
@ -11,29 +11,32 @@ using boost::filesystem::path;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
path getBinPath() {
|
path getBinPath() {
|
||||||
try {
|
static const path binPath = [] {
|
||||||
// Determine path length
|
try {
|
||||||
int pathLength = wai_getExecutablePath(nullptr, 0, nullptr);
|
// Determine path length
|
||||||
if (pathLength == -1) {
|
int pathLength = wai_getExecutablePath(nullptr, 0, nullptr);
|
||||||
throw std::runtime_error("Error determining path length.");
|
if (pathLength == -1) {
|
||||||
}
|
throw std::runtime_error("Error determining path length.");
|
||||||
|
}
|
||||||
|
|
||||||
// Get path
|
// Get path
|
||||||
// Note: According to documentation, pathLength does *not* include the trailing zero. Actually, it does.
|
// Note: According to documentation, pathLength does *not* include the trailing zero. Actually, it does.
|
||||||
// In case there are situations where it doesn't, we allocate one character more.
|
// In case there are situations where it doesn't, we allocate one character more.
|
||||||
std::vector<char> buffer(pathLength + 1);
|
std::vector<char> buffer(pathLength + 1);
|
||||||
if (wai_getExecutablePath(buffer.data(), buffer.size(), nullptr) == -1) {
|
if (wai_getExecutablePath(buffer.data(), buffer.size(), nullptr) == -1) {
|
||||||
throw std::runtime_error("Error reading path.");
|
throw std::runtime_error("Error reading path.");
|
||||||
}
|
}
|
||||||
buffer[pathLength] = 0;
|
buffer[pathLength] = 0;
|
||||||
|
|
||||||
// Convert to boost::filesystem::path
|
// Convert to boost::filesystem::path
|
||||||
string pathString(buffer.data());
|
string pathString(buffer.data());
|
||||||
static path binPath(boost::filesystem::canonical(pathString).make_preferred());
|
static path binPath(boost::filesystem::canonical(pathString).make_preferred());
|
||||||
return binPath;
|
return binPath;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
std::throw_with_nested(std::runtime_error("Could not determine path of bin directory.") );
|
std::throw_with_nested(std::runtime_error("Could not determine path of bin directory."));
|
||||||
}
|
}
|
||||||
|
}();
|
||||||
|
return binPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
path getBinDirectory() {
|
path getBinDirectory() {
|
||||||
|
|
Loading…
Reference in New Issue