use std::ffi::c_char;
pub(crate) const ERROR_BUF_SIZE: usize = 256;
pub(crate) fn with_error_buf<T>(f: impl FnOnce(*mut c_char, u32) -> T) -> (T, String) {
let mut buf = [0 as c_char; ERROR_BUF_SIZE];
let ret = f(buf.as_mut_ptr(), buf.len() as u32);
(ret, cstr_buf_to_string(&buf))
}
pub(crate) fn cstr_buf_to_string(buf: &[c_char]) -> String {
let bytes: Vec<u8> = buf
.iter()
.take_while(|&&c| c != 0)
.map(|&c| c as u8)
.collect();
String::from_utf8_lossy(&bytes).into_owned()
}
pub(crate) unsafe fn cstr_ptr_to_string(ptr: *const c_char) -> String {
unsafe {
if ptr.is_null() {
return String::new();
}
std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned()
}
}