Skip to content

Commit 46579b9

Browse files
committed
Fix paths and utils.
Add settings and www to resources.
1 parent 79f95ea commit 46579b9

File tree

7 files changed

+49
-33
lines changed

7 files changed

+49
-33
lines changed

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ set(HELPER_SOURCES
5050
)
5151
set(RESOURCES
5252
icon.icns
53+
settings.json
54+
www/_pdo.php
55+
www/cookies.php
56+
www/dialogs.php
57+
www/download.php
58+
www/env-variables.php
59+
www/forms.php
60+
www/google.php
61+
www/html5-video.php
62+
www/index.php
63+
www/loading-error.php
64+
www/php-error.php
65+
www/phpinfo.php
66+
www/popup.php
67+
www/pretty-urls.php
68+
www/request-quota.php
69+
www/session.php
70+
www/sqlite.php
71+
www/style.css
72+
www/upload.php
73+
www/window-close.php
5374
)
5475
set(PHPDESKTOP_OUT_DIR "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}")
5576
set(PHPDESKTOP_APP "${PHPDESKTOP_OUT_DIR}/PHP Desktop.app")

buildandrun.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ if [ $retval -ne 0 ]; then
1010
exit $retval
1111
fi
1212
open -W --stdout $(tty) --stderr $(tty) "./Debug/PHP Desktop.app"
13-
#./Debug/PHP\ Desktop.app/Contents/MacOS/PHP\ Desktop

main.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ int main(int argc, char **argv) {
117117
}
118118

119119
LOG(INFO) << "Executable directory: " << GetExecutableDir();
120+
LOG(INFO) << "Resources directory: " << GetResourcesDir();
120121

121122
// If reading settings.json fails exit app immediately
122123
json_value* app_settings = Settings();

mongoose_server.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ bool mongoose_start() {
8080

8181
// www_directory
8282
std::string www_directory((*app_settings)["web_server"]["www_directory"]);
83-
www_directory = GetFullPath(www_directory);
83+
www_directory = GetResourcesDir().append("/").append(www_directory);
8484
LOG(INFO) << "www_directory=" << www_directory;
8585

8686
// index_files
@@ -102,9 +102,8 @@ bool mongoose_start() {
102102
LOG(INFO) << "index_files=" << index_files;
103103

104104
// cgi_interpreter
105-
std::string cgi_interpreter(
106-
(*app_settings)["web_server"]["cgi_interpreter"]);
107-
cgi_interpreter = GetFullPath(cgi_interpreter);
105+
std::string cgi_interpreter((*app_settings)["web_server"]["cgi_interpreter"]);
106+
cgi_interpreter = GetExecutableDir().append("/").append(cgi_interpreter);
108107
LOG(INFO) << "cgi_interpreter=" << cgi_interpreter;
109108

110109
// cgi_pattern (from cgi_extensions)

settings.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ json_value* Settings()
1414
return ret;
1515
}
1616
settings_fetched = true;
17-
LOG(INFO) << "Fetching settings from settings.json file";
1817

19-
std::string settings_file;
20-
GetResourcesDir(settings_file);
21-
settings_file.append("/settings.json");
18+
std::string settings_file = GetResourcesDir().append("/settings.json");
19+
LOG(INFO) << "Fetching settings from file: " << settings_file;
20+
2221
std::string contents = GetFileContents(settings_file);
2322
if (contents.empty()) {
2423
g_SettingsError = "Error while reading settings.json file";

utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <string>
66

7-
bool GetResourcesDir(std::string& dir);
7+
std::string GetResourcesDir();
88
std::string GetExecutableDir();
99
std::string GetFileContents(std::string file);
1010
std::string GetFullPath(std::string path);

utils.mm

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2024 Czarek Tomczak, PHP Desktop.
22

33
#include "utils.h"
4+
#include <filesystem>
45
#include <limits.h>
56
#include <unistd.h>
67
#include <fstream>
@@ -9,42 +10,38 @@
910
#import <Foundation/Foundation.h>
1011
#include <mach-o/dyld.h>
1112

12-
bool GetResourcesDir(std::string& dir)
13+
std::string RealPath(std::string path)
1314
{
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;
2021
}
22+
}
23+
24+
std::string GetResourcesDir()
25+
{
26+
static bool am_i_bundled = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".app"];
27+
std::string dir = GetExecutableDir();
2128
if (am_i_bundled) {
22-
std::string::size_type last_sep = dir.find_last_of("/");
23-
dir.resize(last_sep);
2429
dir.append("/../Resources");
25-
return true;
30+
return RealPath(dir);
2631
}
2732
dir.append("/Resources");
28-
return true;
33+
return dir;
2934
}
3035

3136
std::string GetExecutableDir()
3237
{
3338
// Directory in which executable resides.
3439
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 "";
4643
}
47-
return app_path;
44+
return RealPath(std::filesystem::path{app_path}.parent_path());
4845
}
4946

5047
std::string GetFileContents(std::string file)

0 commit comments

Comments
 (0)