From 5df5c78a62c4e4c085e4e6efacc5d77d62679092 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 6 Mar 2023 10:18:13 +0800 Subject: [PATCH] fix: string list ffi demo --- include/utils/assets.h | 2 +- src/assets/src/ffi.rs | 35 +++++------------------------------ src/cleardns.c | 23 ++++++++++++----------- 3 files changed, 18 insertions(+), 42 deletions(-) diff --git a/include/utils/assets.h b/include/utils/assets.h index 2000b48..d91b126 100644 --- a/include/utils/assets.h +++ b/include/utils/assets.h @@ -15,7 +15,7 @@ void assets_extract(); // RUST DEMO START void rust_test_single(const char *ptr); -void rust_test_multi(const char *const *ptr); +void rust_test_multi(char *const *ptr); // RUST DEMO END diff --git a/src/assets/src/ffi.rs b/src/assets/src/ffi.rs index 746533b..30817ce 100644 --- a/src/assets/src/ffi.rs +++ b/src/assets/src/ffi.rs @@ -1,28 +1,11 @@ -use std::ffi::{CStr, CString}; use std::os::raw::c_char; +use std::ffi::{CStr, CString}; /// Load c-style string from `char *` pointer. unsafe fn load_c_string(ptr: *const c_char) -> String { - - println!("c-style ptr: {:?}", ptr); - - // let s = CStr::from_ptr(ptr).to_str().unwrap(); - // - // println!("rust cstr ptr: {:?}", s.as_ptr()); - // - // let string = format!("{}", s); - // - // println!("rust string ptr: {:?}", string.as_ptr()); - // - // println!(); - - - let a = CString::from(CStr::from_ptr(ptr)); - println!("rust string ptr: {:?}", a.as_ptr()); - - a.into_string().unwrap() - - // String::from(CStr::from_ptr(ptr).to_str().unwrap()) + CString::from(CStr::from_ptr(ptr)) + .into_string() + .unwrap() } /// Load c-style string list from `char **` pointer. @@ -30,15 +13,7 @@ unsafe fn load_c_string_list(ptr: *const *const c_char) -> Vec { let mut index = 0; let mut string_list: Vec = vec![]; while *ptr.offset(index) != std::ptr::null() { // traverse until `NULL` - let s = load_c_string(*ptr.offset(index)); - - // println!("string ptr: `{:?}`", s.as_ptr()); - println!("content: `{}`", s); - - println!(); - - string_list.push(s); - + string_list.push(load_c_string(*ptr.offset(index))); index += 1; } string_list diff --git a/src/cleardns.c b/src/cleardns.c index a2d940d..2e9fb62 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -120,17 +120,16 @@ void cleardns() { // cleardns service int main(int argc, char *argv[]) { char **demo = string_list_init(); - string_list_append(&demo, "item 1 afdcafaed"); - string_list_append(&demo, "item 2 fasdfcade"); - string_list_append(&demo, "item 3 fadfa"); + + string_list_append(&demo, "item 1"); + string_list_append(&demo, "item 2"); + string_list_append(&demo, "item 3"); string_list_append(&demo, "item 4"); string_list_append(&demo, "item 5"); char *tmp = string_list_dump(demo); log_warn("dump -> %s", tmp); - string_list_free(demo); - log_info("string list -> %p -> %p", &demo, demo); log_info("string list 1 -> %p -> %p -> `%s`", &demo[0], demo[0], demo[0]); log_info("string list 2 -> %p -> %p -> `%s`", &demo[1], demo[1], demo[1]); @@ -139,14 +138,16 @@ int main(int argc, char *argv[]) { log_info("string list 5 -> %p -> %p -> `%s`", &demo[4], demo[4], demo[4]); log_info("string list 6 -> %p -> %p -> `%s`", &demo[5], demo[5], demo[5]); -// rust_test_single(demo[0]); -// rust_test_single(demo[1]); -// rust_test_single(demo[2]); -// rust_test_single(demo[3]); -// rust_test_single(demo[4]); -// rust_test_multi(demo); + log_info("string list 1 -> %p -> %p -> `%s`", &demo[0], demo[0], demo[0]); + log_info("string list 2 -> %p -> %p -> `%s`", &demo[1], demo[1], demo[1]); + log_info("string list 3 -> %p -> %p -> `%s`", &demo[2], demo[2], demo[2]); + log_info("string list 4 -> %p -> %p -> `%s`", &demo[3], demo[3], demo[3]); + log_info("string list 5 -> %p -> %p -> `%s`", &demo[4], demo[4], demo[4]); + log_info("string list 6 -> %p -> %p -> `%s`", &demo[5], demo[5], demo[5]); + + string_list_free(demo); log_warn("test end"); exit(0);