1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
use std::ffi::{CStr, CString}; use std::ptr; use lazy_static::lazy_static; use libc::{c_char, size_t}; use super::two_touch_input::Converter; #[repr(C)] pub struct TwoTouchStringResult { len: size_t, data: *const *const c_char, } #[no_mangle] pub extern "C" fn convert_to_two_touch_string(val: *const c_char) -> TwoTouchStringResult { let c_str = unsafe { CStr::from_ptr(val) }; let s = match c_str.to_str() { Ok(s) => s, Err(_) => { return TwoTouchStringResult { len: 0, data: ptr::null(), } } }; let results = CONVERTER.convert_to_two_touch_string(s.to_string()); let results = match results { Ok(r) => r, Err(_) => { return TwoTouchStringResult { len: 0, data: ptr::null(), } } }; let mut data: Vec<*const c_char> = Vec::with_capacity(results.len()); for r in results { let s = match CString::new(r.as_str()) { Ok(s) => s, Err(_) => { return TwoTouchStringResult { len: 0, data: ptr::null(), } } }; data.push(s.into_raw()); } let two_touch_string_result = TwoTouchStringResult { len: data.len(), data: data.as_ptr() as *const *const c_char, }; std::mem::forget(data); two_touch_string_result } #[no_mangle] pub extern "C" fn convert_from_two_touch_string(val: *const c_char) -> *const c_char { let c_str = unsafe { CStr::from_ptr(val) }; let s = match c_str.to_str() { Ok(s) => s, Err(_) => return ptr::null(), }; let result = CONVERTER.convert_from_two_touch_string(s.to_string()); let result = match result { Ok(r) => r, Err(_) => return ptr::null(), }; let result = match CString::new(result.as_str()) { Ok(r) => r, Err(_) => return ptr::null(), }; result.into_raw() } lazy_static! { static ref CONVERTER: Converter = Converter::new(); }