CHP_modelling
Loading...
Searching...
No Matches
pathfinder.h
1#if defined(_WIN32)
2#include <Shlwapi.h>
3#include <io.h>
4#include <windows.h>
5#define access _access_s
6#endif
7
8#ifdef __APPLE__
9#include <libgen.h>
10#include <limits.h>
11#include <mach-o/dyld.h>
12#include <unistd.h>
13#endif
14
15#ifdef __linux__
16#include <libgen.h>
17#include <limits.h>
18#include <unistd.h>
19
20#if defined(__sun)
21#define PROC_SELF_EXE "/proc/self/path/a.out"
22#else
23#define PROC_SELF_EXE "/proc/self/exe"
24#endif
25
26#endif
27
28namespace MyPaths {
29
30#if defined(_WIN32)
31
32std::string getExecutablePath() {
33 char rawPathName[MAX_PATH];
34 GetModuleFileName(NULL, rawPathName, MAX_PATH);
35 return std::string(rawPathName);
36}
37
38std::string getExecutableDir() {
39 std::string executablePath = getExecutablePath();
40 char* exePath = new char[executablePath.length()];
41 strcpy(exePath, executablePath.c_str());
42 PathRemoveFileSpecA(exePath);
43 std::string directory = std::string(exePath);
44 delete[] exePath;
45 return directory;
46}
47
48std::string mergePaths(std::string pathA, std::string pathB) {
49 char combined[MAX_PATH];
50 PathCombineA(combined, pathA.c_str(), pathB.c_str());
51 std::string mergedPath(combined);
52 return mergedPath;
53}
54
55#endif
56
57#ifdef __linux__
58
59std::string getExecutablePath() {
60 /*char rawPathName[PATH_MAX];
61 realpath(PROC_SELF_EXE, rawPathName);
62 return std::string(rawPathName);*/
63
64 char path[PATH_MAX];
65 ssize_t count = readlink("/proc/self/exe", path, PATH_MAX);
66 if (count != -1) {
67 path[count] = '\0'; // Null-terminate the string
68 return std::string(path);
69 } else {
70 throw std::runtime_error("Failed to get executable path.");
71 }
72}
73
74std::string getExecutableDir() {
75 std::string executablePath = getExecutablePath();
76 vector<string> dir = divide_string(getExecutablePath(), '/');
77 dir.pop_back();
78 string Dir = "";
79 for (int n = 0; n < dir.size(); n++) {
80 Dir = Dir + dir[n] + "/";
81 }
82 return Dir;
83 /*char *executablePathStr = new char[executablePath.length() + 1];
84 strcpy(executablePathStr, executablePath.c_str());
85 char* executableDir = dirname(executablePathStr);
86 delete [] executablePathStr;
87 return std::string(executableDir);*/
88}
89
90std::string mergePaths(std::string pathA, std::string pathB) { return pathA + "/" + pathB; }
91
92#endif
93
94#ifdef __APPLE__
95std::string getExecutablePath() {
96 char rawPathName[PATH_MAX];
97 char realPathName[PATH_MAX];
98 uint32_t rawPathSize = (uint32_t)sizeof(rawPathName);
99
100 if (!_NSGetExecutablePath(rawPathName, &rawPathSize)) {
101 realpath(rawPathName, realPathName);
102 }
103 return std::string(realPathName);
104}
105
106std::string getExecutableDir() {
107 std::string executablePath = getExecutablePath();
108 char* executablePathStr = new char[executablePath.length() + 1];
109 strcpy(executablePathStr, executablePath.c_str());
110 char* executableDir = dirname(executablePathStr);
111 delete[] executablePathStr;
112 return std::string(executableDir);
113}
114
115std::string mergePaths(std::string pathA, std::string pathB) { return pathA + "/" + pathB; }
116
117#endif
118
119/*bool checkIfFileExists(const std::string& filePath) {
120 return access(filePath.c_str(), 0) == 0;
121}*/
122
123} // namespace MyPaths
124
125std::string getFileDirectory() {
126 std::string filePath = __FILE__;
127 size_t pos = filePath.find_last_of("/\\");
128 return (pos == std::string::npos) ? "" : filePath.substr(0, pos);
129}
130
131std::string getCurrentDirectory() {
132 char cwd[PATH_MAX];
133 if (getcwd(cwd, sizeof(cwd)) != NULL) {
134 return std::string(cwd);
135 } else {
136 perror("getcwd() error");
137 return std::string();
138 }
139}
140
141std::string getActualDir(string file_path) {
142 vector<string> dir = divide_string(file_path, '/');
143 dir.pop_back();
144 string Dir = "";
145 for (size_t n = 0; n < dir.size(); n++) {
146 Dir = Dir + dir[n] + "/";
147 }
148 return Dir;
149}
150
151std::string project_name() {
152 std::string executablePath = MyPaths::getExecutablePath();
153 vector<string> dir = divide_string(MyPaths::getExecutablePath(), '/');
154 vector<string> name = divide_string(dir[dir.size() - 1], '.');
155 return name[0];
156}