~whereswaldon/gio-x

c0c92248522e155403437e20284ce9610aa700a2 — Peter Sanford 1 year, 11 months ago e94c4e0
explorer: expose Name() for darwin files

This exposes a Name() method for files on darwin and iOS.
Useful if you want the filename of the selected file.

Signed-off-by: Peter Sanford <psanford@sanford.io>
2 files changed, 32 insertions(+), 2 deletions(-)

M explorer/file_darwin.go
M explorer/file_darwin.m
M explorer/file_darwin.go => explorer/file_darwin.go +22 -1
@@ 16,17 16,20 @@ extern uint64_t fileRead(CFTypeRef file, uint8_t *b, uint64_t len);
extern bool fileWrite(CFTypeRef file, uint8_t *b, uint64_t len);
extern bool fileClose(CFTypeRef file);
extern char* getError(CFTypeRef file);
extern const char* getURL(CFTypeRef url_ref);

*/
import "C"
import (
	"errors"
	"io"
	"net/url"
	"unsafe"
)

type File struct {
	file   C.CFTypeRef
	url    string
	closed bool
}



@@ 35,7 38,16 @@ func newFile(url C.CFTypeRef) (*File, error) {
	if err := getError(file); err != nil {
		return nil, err
	}
	return &File{file: file}, nil

	cstr := C.getURL(url)
	urlStr := C.GoString(cstr)
	C.free(unsafe.Pointer(cstr))

	ret := &File{
		file: file,
		url:  urlStr,
	}
	return ret, nil
}

func (f *File) Read(b []byte) (n int, err error) {


@@ 72,6 84,15 @@ func (f *File) Write(b []byte) (n int, err error) {
	return len(b), nil
}

func (f *File) Name() string {
	parsed, err := url.Parse(f.url)
	if err != nil {
		return ""
	}

	return parsed.Path
}

func (f *File) Close() error {
	if ok := bool(C.fileClose(f.file)); !ok {
		return getError(f.file)

M explorer/file_darwin.m => explorer/file_darwin.m +10 -1
@@ 70,4 70,13 @@ char* getError(CFTypeRef file) {
        return 0;
    }
    return (char*)([[f.err localizedDescription] UTF8String]);
}
\ No newline at end of file
}

const char* getURL(CFTypeRef url_ref) {
    NSURL *url = (__bridge NSURL *)url_ref;
    NSString *str = [url absoluteString];

    const char *unsafe_cstr = [str UTF8String];
    char *safe_cstr = strdup(unsafe_cstr);
    return safe_cstr;
}