macro_rules! strc_noctx {
($e:expr) => {
std::ffi::CString::new($e).expect("CString::new failed").into_raw()
};
}
macro_rules! str_fromraw {
($e:expr) => {
std::ffi::CString::from_raw($e as *mut std::os::raw::c_char)
};
}
pub struct StrcCtx{
pub ptr: *mut c_char,
}
impl StrcCtx {
pub fn new(s: &str) -> StrcCtx{
StrcCtx{
ptr: strc_noctx!(s),
}
}
}
impl Drop for StrcCtx{
fn drop(&mut self) {
unsafe{
let _ = str_fromraw!(self.ptr);
}
}
}
macro_rules! strc {
($e:expr) => {
crate::macros::StrcCtx::new($e).ptr
};
}
macro_rules! cstr {
($e:expr) => {
if $e == std::ptr::null_mut() {
""
}else{
std::ffi::CStr::from_ptr($e).to_str().unwrap_or("")
}
};
}
macro_rules! to_bool {
($e:expr) => {
if $e == -1 { false } else { true }
};
}
use std::os::raw::c_char;
use std::ffi::CString;
pub unsafe fn strc_context1(s: &str, context: fn(raw_str: *mut std::os::raw::c_char)){
let raw_str = CString::new(s).expect("CString::new failed").into_raw();
context(raw_str);
let _ = CString::from_raw(raw_str);
}
pub unsafe fn strc_context2(s1: &str, s2: &str, context: fn(raw_str1: *mut c_char, raw_str2: *mut c_char)){
let raw_str1 = CString::new(s1).expect("CString::new failed").into_raw();
let raw_str2 = CString::new(s2).expect("CString::new failed").into_raw();
context(raw_str1, raw_str2);
let _ = CString::from_raw(raw_str1);
let _ = CString::from_raw(raw_str2);
}