@@ 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)
@@ 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;
+}