use std::ffi::{CStr, CString};
use super::api::ParamScope;
use super::api::ParamScopeOps;
#[no_mangle]
pub unsafe extern "C" fn param_scope_create() -> *mut ParamScope {
let ps = Box::<ParamScope>::default();
Box::leak(ps)
}
#[no_mangle]
pub unsafe extern "C" fn param_scope_destroy(this: *mut ParamScope) {
if this.is_null() {
return;
}
drop(Box::from_raw(this));
}
#[no_mangle]
pub unsafe extern "C" fn param_scope_enter(this: *mut ParamScope) {
if this.is_null() {
return;
}
(*this).enter()
}
#[no_mangle]
pub unsafe extern "C" fn param_scope_exit(this: *mut ParamScope) {
if this.is_null() {
return;
}
(*this).exit();
}
#[no_mangle]
pub unsafe extern "C" fn param_scope_hget_i64(this: *mut ParamScope, hkey: u64, def: i64) -> i64 {
if this.is_null() {
return def;
}
(*this).get_or_else(hkey, def)
}
#[no_mangle]
pub unsafe extern "C" fn param_scope_hget_or_f64(
this: *mut ParamScope,
hkey: u64,
def: f64,
) -> f64 {
if this.is_null() {
return def;
}
(*this).get_or_else(hkey, def)
}
#[no_mangle]
pub unsafe extern "C" fn param_scope_free_str(ptr: *mut i8) {
if ptr.is_null() {
return;
}
let _ = CString::from_raw(ptr);
}
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_hget_or_str(
this: *mut ParamScope,
hkey: u64,
def: *mut i8,
) -> *mut i8 {
let default_str = if def.is_null() {
String::new()
} else {
match CStr::from_ptr(def).to_str() {
Ok(s) => s.to_string(),
Err(_) => String::new(), }
};
let result = if this.is_null() {
default_str
} else {
(*this).get_or_else(hkey, default_str)
};
match CString::new(result) {
Ok(cstr) => cstr.into_raw(),
Err(_) => std::ptr::null_mut(), }
}
#[cfg(target_arch = "aarch64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_hget_or_str(
this: *mut ParamScope,
hkey: u64,
def: *mut i8,
) -> *mut i8 {
let default_str = if def.is_null() {
String::new()
} else {
match CStr::from_ptr(def as *const i8).to_str() {
Ok(s) => s.to_string(),
Err(_) => String::new(), }
};
let result = if this.is_null() {
default_str
} else {
(*this).get_or_else(hkey, default_str)
};
match CString::new(result) {
Ok(cstr) => cstr.into_raw(),
Err(_) => std::ptr::null_mut(), }
}
#[no_mangle]
pub unsafe extern "C" fn param_scope_hget_or_bool(
this: *mut ParamScope,
hkey: u64,
def: bool,
) -> bool {
if this.is_null() {
return def;
}
(*this).get_or_else(hkey, def)
}
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_i64(this: *mut ParamScope, key: *const i8, val: i64) {
if this.is_null() || key.is_null() {
return;
}
if let Ok(key_str) = CStr::from_ptr(key).to_str() {
(*this).put(key_str.to_string(), val);
}
}
#[cfg(target_arch = "aarch64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_i64(this: *mut ParamScope, key: *const i8, val: i64) {
if this.is_null() || key.is_null() {
return;
}
if let Ok(key_str) = CStr::from_ptr(key as *const i8).to_str() {
(*this).put(key_str.to_string(), val);
}
}
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_f64(this: *mut ParamScope, key: *const i8, val: f64) {
if this.is_null() || key.is_null() {
return;
}
if let Ok(key_str) = CStr::from_ptr(key).to_str() {
(*this).put(key_str.to_string(), val);
}
}
#[cfg(target_arch = "aarch64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_f64(this: *mut ParamScope, key: *const i8, val: f64) {
if this.is_null() || key.is_null() {
return;
}
if let Ok(key_str) = CStr::from_ptr(key as *const i8).to_str() {
(*this).put(key_str.to_string(), val);
}
}
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_bool(this: *mut ParamScope, key: *const i8, val: bool) {
if this.is_null() || key.is_null() {
return;
}
if let Ok(key_str) = CStr::from_ptr(key).to_str() {
(*this).put(key_str.to_string(), val);
}
}
#[cfg(target_arch = "aarch64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_bool(this: *mut ParamScope, key: *const i8, val: bool) {
if this.is_null() || key.is_null() {
return;
}
if let Ok(key_str) = CStr::from_ptr(key as *const i8).to_str() {
(*this).put(key_str.to_string(), val);
}
}
#[cfg(target_arch = "x86_64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_str(
this: *mut ParamScope,
key: *const i8,
val: *const i8,
) {
if this.is_null() || key.is_null() || val.is_null() {
return;
}
if let (Ok(key_str), Ok(val_str)) = (CStr::from_ptr(key).to_str(), CStr::from_ptr(val).to_str())
{
(*this).put(key_str.to_string(), val_str.to_string());
}
}
#[cfg(target_arch = "aarch64")]
#[no_mangle]
pub unsafe extern "C" fn param_scope_put_str(
this: *mut ParamScope,
key: *const i8,
val: *const i8,
) {
if this.is_null() || key.is_null() || val.is_null() {
return;
}
if let (Ok(key_str), Ok(val_str)) = (
CStr::from_ptr(key as *const i8).to_str(),
CStr::from_ptr(val as *const i8).to_str(),
) {
(*this).put(key_str.to_string(), val_str.to_string());
}
}