Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
exec_path.hpp
Go to the documentation of this file.
1/*
2 _____ _ _
3 | ____|_ _____ ___ _ __ __ _| |_| |__
4 | _| \ \/ / _ \/ __| | '_ \ / _` | __| '_ \
5 | |___ > < __/ (__ | |_) | (_| | |_| | | |
6 |_____/_/\_\___|\___| | .__/ \__,_|\__|_| |_|
7 |_|
8* Calculate the path of an executable file.
9*/
10#include <string>
11#include <filesystem>
12
13#ifndef EXEC_PATH_HPP
14#define EXEC_PATH_HPP
15
16#if defined(_WIN32)
17#include <Shlwapi.h>
18#include <io.h>
19#include <windows.h>
20#define PATH_MAX MAX_PATH
21#define access _access_s
22#endif
23
24#ifdef __APPLE__
25#include <libgen.h>
26#include <limits.h>
27#include <mach-o/dyld.h>
28#include <unistd.h>
29#endif
30
31#ifdef __linux__
32#include <libgen.h>
33#include <limits.h>
34#include <unistd.h>
35
36#if defined(__sun)
37#define PROC_SELF_EXE "/proc/self/path/a.out"
38#else
39#define PROC_SELF_EXE "/proc/self/exe"
40#endif
41
42#endif
43
44namespace Mads {
45
46inline std::filesystem::path exec_path() {
47 char raw_path[PATH_MAX];
48#if defined(_WIN32)
49 GetModuleFileNameA(NULL, raw_path, MAX_PATH);
50 return std::string(raw_path);
51#else
52#if defined(__linux__)
53 realpath(PROC_SELF_EXE, raw_path);
54#elif defined(__APPLE__)
55 uint32_t rawPathSize = (uint32_t)sizeof(raw_path);
56 _NSGetExecutablePath(raw_path, &rawPathSize);
57#endif
58 return std::filesystem::weakly_canonical(raw_path);
59#endif
60}
61
62inline std::string exec_dir(std::string relative = "") {
63 std::string path = exec_path().parent_path().string();
64 if (relative != "") {
65 return std::filesystem::weakly_canonical(path + "/" + relative).string();
66 } else {
67 return std::filesystem::weakly_canonical(path).string();
68 }
69}
70
71inline std::string prefix() {
72 return exec_dir("../");
73}
74
75} // namespace Mads
76
77
78#endif // EXEC_PATH_HPP
Definition agent.hpp:67
std::string exec_dir(std::string relative="")
Definition exec_path.hpp:62
std::filesystem::path exec_path()
Definition exec_path.hpp:46
std::string prefix()
Definition exec_path.hpp:71