use coreaudio_sys::{
CFStringGetCString, CFStringGetLength, CFStringGetMaximumSizeForEncoding, CFStringRef,
kCFStringEncodingUTF8,
};
#[link(name = "CoreFoundation", kind = "framework")]
unsafe extern "C" {}
pub(crate) unsafe fn cfstring_to_string(cf_str: CFStringRef) -> String {
if cf_str.is_null() {
return String::new();
}
unsafe {
let length = CFStringGetLength(cf_str);
let max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
let mut buf = vec![0u8; max_size.max(1) as usize];
if CFStringGetCString(
cf_str,
buf.as_mut_ptr() as *mut i8,
max_size,
kCFStringEncodingUTF8,
) != 0
{
let cstr = std::ffi::CStr::from_ptr(buf.as_ptr() as *const i8);
cstr.to_string_lossy().into_owned()
} else {
String::new()
}
}
}