From dfd168f5a0a7c09b7c0a6aa94daa299170ffd755 Mon Sep 17 00:00:00 2001 From: Kornel Date: Tue, 8 Aug 2017 15:53:55 +0100 Subject: [PATCH] CFURL from Path --- core-foundation/src/url.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/core-foundation/src/url.rs b/core-foundation/src/url.rs index c3aad09..34c53b7 100644 --- a/core-foundation/src/url.rs +++ b/core-foundation/src/url.rs @@ -11,11 +11,13 @@ pub use core_foundation_sys::url::*; -use base::{TCFType}; +use base::{TCFType, CFIndex}; use string::{CFString}; use core_foundation_sys::base::{kCFAllocatorDefault, CFRelease}; use std::fmt; +use std::ptr; +use std::path::Path; pub struct CFURL(CFURLRef); @@ -40,6 +42,21 @@ impl fmt::Debug for CFURL { } impl CFURL { + pub fn from_path>(path: P, isDirectory: bool) -> Option { + let path = match path.as_ref().to_str() { + Some(path) => path, + None => return None, + }; + + unsafe { + let url_ref = CFURLCreateFromFileSystemRepresentation(ptr::null_mut(), path.as_ptr(), path.len() as CFIndex, isDirectory as u8); + if url_ref.is_null() { + return None; + } + Some(TCFType::wrap_under_create_rule(url_ref)) + } + } + 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);