forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
68 lines (54 loc) · 1.67 KB
/
Copy pathfilesystem.cpp
File metadata and controls
68 lines (54 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdlib>
#include <climits>
#include <unistd.h>
#include <boost/filesystem.hpp>
#include <util/filesystem.h>
namespace fs = boost::filesystem;
namespace cc
{
namespace util
{
std::string binaryPathToInstallDir(const char* path)
{
// Expand the path to a full path as given by the shell. This is then stripped
// of any symbolic links and relative references.
// The path is then parented twice (from binary to "bin" folder, and then
// the root).
// If the path does not exist, the given binary is searched for in the PATH
// environment variable.
// TODO: replace else branch with fs::search_path if Boost 1.64 or above is used.
if (fs::exists(fs::system_complete(fs::path(path))))
{
return fs::canonical(fs::system_complete(fs::path(path)))
.parent_path().parent_path().string();
}
else
{
fs::path pPath;
std::string fullPath = std::getenv("PATH");
std::string delimiter = ":";
std::size_t pos = 0;
while ((pos = fullPath.find(delimiter)) != std::string::npos)
{
pPath = fs::path(fullPath.substr(0, pos)) / fs::path(path);
if (fs::exists(fs::path(pPath)))
{
return fs::canonical(pPath)
.parent_path().parent_path().string();
}
fullPath.erase(0, pos + delimiter.length());
}
}
throw std::runtime_error(std::string("Could not find ") + path + std::string("."));
}
std::string findCurrentExecutableDir()
{
char exePath[PATH_MAX];
ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath));
if (len == -1 || len == sizeof(exePath))
len = 0;
exePath[len] = '\0';
return fs::path(exePath).parent_path().string();
}
} // namespace util
} // namespace cc