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) {
91 return pathA + "/" + pathB;
92}
93
94#endif
95
96#ifdef __APPLE__
97std::string getExecutablePath() {
98 char rawPathName[PATH_MAX];
99 char realPathName[PATH_MAX];
100 uint32_t rawPathSize = (uint32_t)sizeof(rawPathName);
101
102 if (!_NSGetExecutablePath(rawPathName, &rawPathSize)) {
103 realpath(rawPathName, realPathName);
104 }
105 return std::string(realPathName);
106}
107
108std::string getExecutableDir() {
109 std::string executablePath = getExecutablePath();
110 char *executablePathStr = new char[executablePath.length() + 1];
111 strcpy(executablePathStr, executablePath.c_str());
112 char *executableDir = dirname(executablePathStr);
113 delete[] executablePathStr;
114 return std::string(executableDir);
115}
116
117std::string mergePaths(std::string pathA, std::string pathB) {
118 return pathA + "/" + pathB;
119}
120
121#endif
122
123/*bool checkIfFileExists(const std::string& filePath) {
124 return access(filePath.c_str(), 0) == 0;
125}*/
126
127} // namespace MyPaths
128
129std::string getFileDirectory() {
130 std::string filePath = __FILE__;
131 size_t pos = filePath.find_last_of("/\\");
132 return (pos == std::string::npos) ? "" : filePath.substr(0, pos);
133}
134
135std::string getCurrentDirectory() {
136 char cwd[PATH_MAX];
137 if (getcwd(cwd, sizeof(cwd)) != NULL) {
138 return std::string(cwd);
139 } else {
140 perror("getcwd() error");
141 return std::string();
142 }
143}
144
145std::string getActualDir(string file_path) {
146 vector<string> dir = divide_string(file_path, '/');
147 dir.pop_back();
148 string Dir = "";
149 for (size_t n = 0; n < dir.size(); n++) {
150 Dir = Dir + dir[n] + "/";
151 }
152 return Dir;
153}
154
155std::string project_name() {
156 std::string executablePath = MyPaths::getExecutablePath();
157 vector<string> dir = divide_string(MyPaths::getExecutablePath(), '/');
158 vector<string> name = divide_string(dir[dir.size() - 1], '.');
159 return name[0];
160}