diff src/sys/osx/filesystem.cc @ 194:8548dc425697

sys/osx: remove all objective-c++ stuff mmmm :)
author Paper <mrpapersonic@gmail.com>
date Thu, 07 Dec 2023 03:17:05 -0500
parents
children 975a3f0965e2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sys/osx/filesystem.cc	Thu Dec 07 03:17:05 2023 -0500
@@ -0,0 +1,43 @@
+#include "sys/osx/filesystem.h"
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include <string>
+
+static constexpr unsigned long NSApplicationSupportDirectory = 14;
+static constexpr unsigned long NSUserDomainMask = 1;
+
+extern "C" {
+	CFArrayRef NSSearchPathForDirectoriesInDomains(unsigned long directory, unsigned long domainMask, int expandTilde);
+}
+
+namespace osx {
+
+bool GetApplicationSupportDirectory(std::string& result) {
+	const CFArrayRef strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
+	if (!strings)
+		return false;
+
+	const CFIndex count = CFArrayGetCount(strings);
+	if (count < 1) {
+		CFRelease(strings);
+		return false;
+	}
+
+	const CFStringRef string = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(strings, 0));
+	if (!string) {
+		CFRelease(strings);
+		return false;
+	}
+
+	result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1);
+	if (!CFStringGetCString(string, &result.front(), result.length(), kCFStringEncodingUTF8)) {
+		CFRelease(strings);
+		return false;
+	}
+	result.resize(result.find('\0'));
+
+	return true;
+}
+
+} // namespace osx