#![allow(non_upper_case_globals)]
use base::{CFAllocatorRef, CFIndex, CFOptionFlags, CFRelease, CFRetain};
use base::{Boolean, CFTypeID, CFTypeRef, TCFType};
use base::{kCFAllocatorDefault};
use string::{CFString, CFStringRef};
use std::fmt;
use std::mem;
#[repr(C)]
struct __CFURL;
pub type CFURLRef = *const __CFURL;
pub struct CFURL {
obj: CFURLRef,
}
impl Drop for CFURL {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
impl TCFType<CFURLRef> for CFURL {
#[inline]
fn as_concrete_TypeRef(&self) -> CFURLRef {
self.obj
}
#[inline]
unsafe fn wrap_under_get_rule(reference: CFURLRef) -> CFURL {
let reference: CFURLRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}
#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}
unsafe fn wrap_under_create_rule(obj: CFURLRef) -> CFURL {
CFURL {
obj: obj,
}
}
#[inline]
fn type_id() -> CFTypeID {
unsafe {
CFURLGetTypeID()
}
}
}
impl fmt::Debug for CFURL {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
unsafe {
let string: CFString = TCFType::wrap_under_get_rule(CFURLGetString(self.obj));
write!(f, "{}", string.to_string())
}
}
}
impl CFURL {
pub fn from_file_system_path(filePath: CFString, pathStyle: CFURLPathStyle, isDirectory: bool) -> CFURL {
unsafe {
let url_ref = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePath.as_concrete_TypeRef(), pathStyle, isDirectory as u8);
TCFType::wrap_under_create_rule(url_ref)
}
}
pub fn get_string(&self) -> CFString {
unsafe {
TCFType::wrap_under_get_rule(CFURLGetString(self.obj))
}
}
}
type CFURLBookmarkCreationOptions = CFOptionFlags;
pub type CFURLPathStyle = CFIndex;
pub const kCFURLPOSIXPathStyle: CFURLPathStyle = 0;
pub const kCFURLHFSPathStyle: CFURLPathStyle = 1;
pub const kCFURLWindowsPathStyle: CFURLPathStyle = 2;
#[link(name = "CoreFoundation", kind = "framework")]
extern {
fn CFURLCreateWithFileSystemPath(allocator: CFAllocatorRef, filePath: CFStringRef, pathStyle: CFURLPathStyle, isDirectory: Boolean) -> CFURLRef;
fn CFURLGetString(anURL: CFURLRef) -> CFStringRef;
fn CFURLGetTypeID() -> CFTypeID;
}
#[test]
fn file_url_from_path() {
let path = "/usr/local/foo/";
let cfstr_path = CFString::from_static_string(path);
let cfurl = CFURL::from_file_system_path(cfstr_path, kCFURLPOSIXPathStyle, true);
assert!(cfurl.get_string().to_string() == "file:///usr/local/foo/");
}