1module darwin 2 3#include <Cocoa/Cocoa.h> 4#include <CoreFoundation/CoreFoundation.h> 5 6#flag -framework Cocoa 7#flag -framework Carbon 8 9struct C.NSString { } 10 11// macOS and iOS helpers 12//pub fn nsstring(s string) *C.NSString { 13pub fn nsstring(s string) voidptr { 14 // println('ns $s len=$s.len') 15 # return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len 16 # encoding:NSUTF8StringEncoding freeWhenDone: false]; 17 return 0 18 19 //ns := C.alloc_NSString() 20 //return ns.initWithBytesNoCopy(s.str, length: s.len, 21 //encoding: NSUTF8StringEncoding, freeWhenDone: false) 22} 23 24// returns absolute path to folder where your resources should / will reside 25// for .app packages: .../my.app/Contents/Resources 26// for cli: .../parent_folder/Resources 27 28fn C.CFBundleCopyResourcesDirectoryURL() byteptr 29fn C.CFBundleGetMainBundle() voidptr 30fn C.CFURLGetFileSystemRepresentation() int 31fn C.CFRelease() 32 33pub fn resource_path() string { 34 35 main_bundle := C.CFBundleGetMainBundle() 36 resource_dir_url := C.CFBundleCopyResourcesDirectoryURL(main_bundle) 37 if isnil(resource_dir_url) { 38 panic('CFBundleCopyResourcesDirectoryURL failed') 39 } 40 buffer_size := 4096 41 mut buffer := malloc(buffer_size) 42 buffer[0] = 0 43 conv_result := C.CFURLGetFileSystemRepresentation(resource_dir_url, true, buffer, buffer_size) 44 if conv_result == 0 { 45 panic('CFURLGetFileSystemRepresentation failed') 46 } 47 result := string(buffer) 48 C.CFRelease(resource_dir_url) 49 return result 50} 51