1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/darwin/darwin.v

58 lines
1.6 KiB
V
Raw Normal View History

module darwin
#include <Cocoa/Cocoa.h>
2019-09-30 23:29:14 +03:00
#include <CoreFoundation/CoreFoundation.h>
#flag -framework Cocoa
2019-10-31 13:08:01 +03:00
#flag -framework Carbon
struct C.NSString {}
#include "@VEXEROOT/vlib/darwin/darwin.m"
fn C.nsstring2(s string) voidptr
// macOS and iOS helpers
// pub fn nsstring(s string) *C.NSString {
2019-11-01 00:57:16 +03:00
pub fn nsstring(s string) voidptr {
return C.nsstring2(s)
// println('ns $s len=$s.len')
//# return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len
//# encoding:NSUTF8StringEncoding freeWhenDone: false];
// return 0
2019-12-01 21:08:39 +03:00
// ns := C.alloc_NSString()
// return ns.initWithBytesNoCopy(s.str, length: s.len,
// encoding: NSUTF8StringEncoding, freeWhenDone: false)
}
2019-09-30 23:29:14 +03:00
// returns absolute path to folder where your resources should / will reside
// for .app packages: .../my.app/Contents/Resources
// for cli: .../parent_folder/Resources
2019-12-01 21:08:39 +03:00
2022-04-15 18:25:45 +03:00
fn C.CFBundleCopyResourcesDirectoryURL(bundle voidptr) &u8
2019-12-01 21:08:39 +03:00
fn C.CFBundleGetMainBundle() voidptr
2022-04-15 18:25:45 +03:00
fn C.CFURLGetFileSystemRepresentation(url &u8, resolve_against_base bool, buffer &u8, buffer_size int) int
fn C.CFRelease(url &u8)
2019-12-01 21:08:39 +03:00
2019-09-30 23:29:14 +03:00
pub fn resource_path() string {
main_bundle := C.CFBundleGetMainBundle()
resource_dir_url := C.CFBundleCopyResourcesDirectoryURL(main_bundle)
if resource_dir_url == unsafe { nil } {
2019-10-23 23:40:07 +03:00
panic('CFBundleCopyResourcesDirectoryURL failed')
}
2019-09-30 23:29:14 +03:00
buffer_size := 4096
mut buffer := unsafe { malloc_noscan(buffer_size) }
unsafe {
buffer[0] = 0
}
conv_result := C.CFURLGetFileSystemRepresentation(resource_dir_url, true, buffer,
buffer_size)
2020-05-05 21:07:43 +03:00
if conv_result == 0 {
2019-10-23 23:40:07 +03:00
panic('CFURLGetFileSystemRepresentation failed')
}
result := unsafe { buffer.vstring() }
2019-09-30 23:29:14 +03:00
C.CFRelease(resource_dir_url)
return result
}