|
1 | 1 | // Copyright (c) 2024 Czarek Tomczak, PHP Desktop. |
2 | 2 |
|
3 | 3 | #include "utils.h" |
| 4 | +#include <filesystem> |
4 | 5 | #include <limits.h> |
5 | 6 | #include <unistd.h> |
6 | 7 | #include <fstream> |
|
9 | 10 | #import <Foundation/Foundation.h> |
10 | 11 | #include <mach-o/dyld.h> |
11 | 12 |
|
12 | | -bool GetResourcesDir(std::string& dir) |
| 13 | +std::string RealPath(std::string path) |
13 | 14 | { |
14 | | - static bool am_i_bundled = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".app"]; |
15 | | - uint32_t path_size = 0; |
16 | | - _NSGetExecutablePath(nullptr, &path_size); |
17 | | - if (path_size > 0) { |
18 | | - dir.resize(path_size); |
19 | | - _NSGetExecutablePath(const_cast<char*>(dir.c_str()), &path_size); |
| 15 | + char buffer[PATH_MAX]; |
| 16 | + char* result = realpath(path.c_str(), buffer); |
| 17 | + if (result) { |
| 18 | + return buffer; |
| 19 | + } else { |
| 20 | + return path; |
20 | 21 | } |
| 22 | +} |
| 23 | + |
| 24 | +std::string GetResourcesDir() |
| 25 | +{ |
| 26 | + static bool am_i_bundled = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".app"]; |
| 27 | + std::string dir = GetExecutableDir(); |
21 | 28 | if (am_i_bundled) { |
22 | | - std::string::size_type last_sep = dir.find_last_of("/"); |
23 | | - dir.resize(last_sep); |
24 | 29 | dir.append("/../Resources"); |
25 | | - return true; |
| 30 | + return RealPath(dir); |
26 | 31 | } |
27 | 32 | dir.append("/Resources"); |
28 | | - return true; |
| 33 | + return dir; |
29 | 34 | } |
30 | 35 |
|
31 | 36 | std::string GetExecutableDir() |
32 | 37 | { |
33 | 38 | // Directory in which executable resides. |
34 | 39 | char app_path[PATH_MAX] = {}; |
35 | | - ssize_t pplen = readlink("/proc/self/exe", app_path, sizeof(app_path)-1); |
36 | | - if (pplen != -1) { |
37 | | - app_path[pplen] = '\0'; |
38 | | - } |
39 | | - do { |
40 | | - pplen -= 1; |
41 | | - app_path[pplen+1] = '\0'; |
42 | | - } while (app_path[pplen] != '/' && pplen > 0); |
43 | | - // No slash at the end. |
44 | | - if (pplen > 0 && app_path[pplen] == '/') { |
45 | | - app_path[pplen] = '\0'; |
| 40 | + uint32_t bufsize = PATH_MAX; |
| 41 | + if (_NSGetExecutablePath(app_path, &bufsize) != 0) { |
| 42 | + return ""; |
46 | 43 | } |
47 | | - return app_path; |
| 44 | + return RealPath(std::filesystem::path{app_path}.parent_path()); |
48 | 45 | } |
49 | 46 |
|
50 | 47 | std::string GetFileContents(std::string file) |
|
0 commit comments