use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use audio_cpp_sys::*;
use crate::error::Error;
pub(crate) unsafe fn take_string(ptr: *mut c_char) -> Result<String, Error> {
if ptr.is_null() {
return Ok(String::new());
}
let s = unsafe { CStr::from_ptr(ptr) }.to_string_lossy().into_owned();
unsafe { audiocpp_free_string(ptr) };
Ok(s)
}
pub(crate) fn last_error() -> String {
unsafe {
let p = audiocpp_last_error();
if p.is_null() {
String::from("(无错误信息)")
} else {
CStr::from_ptr(p).to_string_lossy().into_owned()
}
}
}
pub(crate) fn check_rc(rc: i32) -> Result<(), Error> {
if rc == 0 {
Ok(())
} else {
Err(Error::Ffi(last_error()))
}
}
pub(crate) fn cstring(s: &str) -> Result<CString, Error> {
CString::new(s).map_err(Error::from)
}