From 39cfb1d6a6595947f6bd190cb2e158c4528bd26d Mon Sep 17 00:00:00 2001 From: Tony Kostanjsek <708330+lobotony@users.noreply.github.com> Date: Mon, 30 Sep 2019 22:29:14 +0200 Subject: [PATCH] darwin: resource_path() --- vlib/darwin/darwin.v | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/vlib/darwin/darwin.v b/vlib/darwin/darwin.v index 849023e7f1..ac015a3067 100644 --- a/vlib/darwin/darwin.v +++ b/vlib/darwin/darwin.v @@ -1,6 +1,8 @@ module darwin #include +#include + #flag -framework Cocoa struct C.NSString { } @@ -17,3 +19,22 @@ pub fn nsstring(s string) *C.NSString { //encoding: NSUTF8StringEncoding, freeWhenDone: false) } +// returns absolute path to folder where your resources should / will reside +// for .app packages: .../my.app/Contents/Resources +// for cli: .../parent_folder/Resources +pub fn resource_path() string { + + main_bundle := C.CFBundleGetMainBundle() + resource_dir_url := C.CFBundleCopyResourcesDirectoryURL(main_bundle) + assert !isnil(resource_dir_url) + buffer_size := 4096 + mut buffer := malloc(buffer_size) + buffer[0] = 0 + conv_result := C.CFURLGetFileSystemRepresentation(resource_dir_url, true, buffer, buffer_size) + assert conv_result + result := string(buffer) + C.CFRelease(resource_dir_url) + free(buffer) + return result +} +