summaryrefslogtreecommitdiffstats
path: root/src/common/apple/version.m
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/apple/version.m')
-rw-r--r--src/common/apple/version.m43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/common/apple/version.m b/src/common/apple/version.m
new file mode 100644
index 0000000..133ec5b
--- /dev/null
+++ b/src/common/apple/version.m
@@ -0,0 +1,43 @@
+#include "common/apple/version.h"
+#include "common/strutil.h"
+
+#import <Foundation/Foundation.h>
+
+#define APP_CONTENTS_MACOS ".app/Contents/MacOS"
+
+bool ffGetAppNameAndVersion(const char* exePath, FFstrbuf* retName, FFstrbuf* retVersion) {
+ char* lastSlash = strrchr(exePath, '/');
+ if (!lastSlash) {
+ return false;
+ }
+ if ((size_t) (lastSlash - exePath) > strlen("X" APP_CONTENTS_MACOS) && memcmp(lastSlash - strlen(APP_CONTENTS_MACOS), APP_CONTENTS_MACOS, strlen(APP_CONTENTS_MACOS)) != 0) {
+ return false;
+ }
+
+ lastSlash -= strlen("MacOS");
+ char infoPlistPath[PATH_MAX];
+ memcpy(infoPlistPath, exePath, lastSlash - exePath);
+ memcpy(infoPlistPath + (lastSlash - exePath), "Info.plist", sizeof("Info.plist")); // X.app/Contents/Info.plist
+ NSError* error;
+ NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:@(infoPlistPath)]
+ error:&error];
+ if (!dict) {
+ return false;
+ }
+
+ if (retName) {
+ NSString* bundleName = dict[@"CFBundleDisplayName"] ?: dict[@"CFBundleName"];
+ if (bundleName) {
+ ffStrbufSetS(retName, bundleName.UTF8String);
+ }
+ }
+
+ if (retVersion) {
+ NSString* bundleVersion = dict[@"CFBundleShortVersionString"];
+ if (bundleVersion) {
+ ffStrbufSetS(retVersion, bundleVersion.UTF8String);
+ }
+ }
+
+ return true;
+}