Browse Source

fix: string list ffi demo

dev
Dnomd343 2 years ago
parent
commit
5df5c78a62
  1. 2
      include/utils/assets.h
  2. 35
      src/assets/src/ffi.rs
  3. 23
      src/cleardns.c

2
include/utils/assets.h

@ -15,7 +15,7 @@ void assets_extract();
// RUST DEMO START // RUST DEMO START
void rust_test_single(const char *ptr); 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 // RUST DEMO END

35
src/assets/src/ffi.rs

@ -1,28 +1,11 @@
use std::ffi::{CStr, CString};
use std::os::raw::c_char; use std::os::raw::c_char;
use std::ffi::{CStr, CString};
/// Load c-style string from `char *` pointer. /// Load c-style string from `char *` pointer.
unsafe fn load_c_string(ptr: *const c_char) -> String { unsafe fn load_c_string(ptr: *const c_char) -> String {
CString::from(CStr::from_ptr(ptr))
println!("c-style ptr: {:?}", ptr); .into_string()
.unwrap()
// 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())
} }
/// Load c-style string list from `char **` pointer. /// Load c-style string list from `char **` pointer.
@ -30,15 +13,7 @@ unsafe fn load_c_string_list(ptr: *const *const c_char) -> Vec<String> {
let mut index = 0; let mut index = 0;
let mut string_list: Vec<String> = vec![]; let mut string_list: Vec<String> = vec![];
while *ptr.offset(index) != std::ptr::null() { // traverse until `NULL` while *ptr.offset(index) != std::ptr::null() { // traverse until `NULL`
let s = load_c_string(*ptr.offset(index)); string_list.push(load_c_string(*ptr.offset(index)));
// println!("string ptr: `{:?}`", s.as_ptr());
println!("content: `{}`", s);
println!();
string_list.push(s);
index += 1; index += 1;
} }
string_list string_list

23
src/cleardns.c

@ -120,17 +120,16 @@ void cleardns() { // cleardns service
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char **demo = string_list_init(); char **demo = string_list_init();
string_list_append(&demo, "item 1 afdcafaed");
string_list_append(&demo, "item 2 fasdfcade"); string_list_append(&demo, "item 1");
string_list_append(&demo, "item 3 fadfa"); string_list_append(&demo, "item 2");
string_list_append(&demo, "item 3");
string_list_append(&demo, "item 4"); string_list_append(&demo, "item 4");
string_list_append(&demo, "item 5"); string_list_append(&demo, "item 5");
char *tmp = string_list_dump(demo); char *tmp = string_list_dump(demo);
log_warn("dump -> %s", tmp); log_warn("dump -> %s", tmp);
string_list_free(demo);
log_info("string list -> %p -> %p", &demo, 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 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 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 5 -> %p -> %p -> `%s`", &demo[4], demo[4], demo[4]);
log_info("string list 6 -> %p -> %p -> `%s`", &demo[5], demo[5], demo[5]); 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); 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"); log_warn("test end");
exit(0); exit(0);

Loading…
Cancel
Save