mirror of https://github.com/dnomd343/ClearDNS
Dnomd343
2 years ago
2 changed files with 23 additions and 11 deletions
@ -1,14 +1,27 @@ |
|||
use crate::convert::to_json; |
|||
use std::ffi::{c_char, CStr, CString}; |
|||
use crate::json::to_json; |
|||
|
|||
fn to_c_string(string: String) -> *const c_char { // fetch c-style ptr of string
|
|||
CString::new(string).unwrap().into_raw() |
|||
} |
|||
|
|||
unsafe fn load_c_string(ptr: *const c_char) -> String { // load string from c-style ptr
|
|||
String::from( |
|||
CStr::from_ptr(ptr).to_str().unwrap() |
|||
) |
|||
} |
|||
|
|||
#[no_mangle] |
|||
pub unsafe extern "C" fn free_rust_string(string: *const c_char) { |
|||
let _ = CString::from_raw(string as *mut _); |
|||
pub unsafe extern "C" fn free_rust_string(ptr: *const c_char) { |
|||
let _ = CString::from_raw(ptr as *mut _); |
|||
} |
|||
|
|||
#[no_mangle] |
|||
pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { |
|||
let content: &str = CStr::from_ptr(content).to_str().unwrap(); |
|||
let content: String = to_json(content); // may return empty string
|
|||
CString::new(content).unwrap().into_raw() |
|||
pub unsafe extern "C" fn to_json_ffi(content: *const c_char) -> *const c_char { |
|||
let content = load_c_string(content); |
|||
let result = match to_json(&content) { // convert to JSON format
|
|||
Some(data) => data, |
|||
None => String::new(), // convert failed -> empty string
|
|||
}; |
|||
to_c_string(result) // return c-style ptr
|
|||
} |
|||
|
Loading…
Reference in new issue