From 9d15ec1cae09a1786bc9b38155fec0accf3872f4 Mon Sep 17 00:00:00 2001 From: Nathan Eikermann Date: Sat, 12 Jun 2021 13:41:25 +0200 Subject: [PATCH] using comma separated strings instead of string arrays... --- Cargo.toml | 1 - Makefile | 10 +++++++++- ctest/bindings.h | 4 +++- ctest/test.c | 14 +++++++------- src/lib.rs | 46 +++++++++++++++++++++++++++++++++++----------- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5636ebb..29ddf92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ build = "build.rs" [dependencies] libc = "0.2" -ffi-convert = "0.5" [build-dependencies] cbindgen = "0.19.0" diff --git a/Makefile b/Makefile index 6898064..e04f97c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,14 @@ DEBUG=target/debug/libmd_util_rs.so FILE=ctest/test.c OUTPUT=ctest/test -test: + +build: + cargo build + +test: build gcc -o ${OUTPUT} ${FILE} -Isrc -L. -l:${DEBUG} ./ctest/test + +clean: + cargo clean + rm ./ctest/test diff --git a/ctest/bindings.h b/ctest/bindings.h index 153368c..7b0efc6 100644 --- a/ctest/bindings.h +++ b/ctest/bindings.h @@ -3,4 +3,6 @@ #include #include -void files_exist(const char *const *f, size_t len); +char *files_exist(const char *f); + +void free_str(char *s); diff --git a/ctest/test.c b/ctest/test.c index d4682a2..552644c 100644 --- a/ctest/test.c +++ b/ctest/test.c @@ -1,15 +1,15 @@ -#include -#include +#include #include #include -extern void files_exist(const char *const *f, size_t len); +extern char *files_exist(const char *f); +extern void free_str(char *s); int main(int argc, char const* argv[]) { - const char *a[2]; - a[0] = "Hello"; - a[1] = "World!"; - files_exist(a, 2); + char *world = "/home/nathan/Downloads/hackerman.jpg,/home/nathan/x.txt"; + char *chunk = files_exist(world); + printf("C: %s", chunk); + free_str(chunk); return 0; } diff --git a/src/lib.rs b/src/lib.rs index 27b0f27..a7eac59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,51 @@ -extern crate ffi_convert; extern crate libc; -use ffi_convert::{AsRust, CReprOf, CStringArray}; use libc::c_char; -use libc::size_t; -//use std::ffi::CStr; -//use std::ffi::CString; +use std::ffi::CStr; +use std::ffi::CString; use std::path::Path; -//use std::slice; fn file_exists(file_path: &str) -> bool { return Path::new(file_path).is_file(); } #[no_mangle] -pub extern "C" fn files_exist(f: *const *const c_char, len: size_t) -> () { - let files = unsafe { +pub extern "C" fn files_exist(f: *const c_char) -> *mut c_char { + let files_c_str = unsafe { assert!(!f.is_null()); - CStringArray { data: f, size: len } + CStr::from_ptr(f) }; - println!("{:?}", files.as_rust().unwrap()); - std::mem::forget(files); + println!("Rust C Str: {:?}", files_c_str); + + let files_r_str = files_c_str.to_str().unwrap(); + println!("Rust Str {:?}", files_r_str); + + let files: Vec<&str> = files_r_str.split(",").collect(); + println!("Rust Vec {:?}", files); + + let mut result = String::new(); + for file in files { + result.push_str(file); + if file_exists(file) { + result.push_str(" exists!,"); + } else { + result.push_str(" does not exist!,"); + } + } + + let result_c_str = CString::new(result).unwrap(); + return result_c_str.into_raw(); +} + +#[no_mangle] +pub extern "C" fn free_str(s: *mut c_char) { + unsafe { + if s.is_null() { + return; + } + CString::from_raw(s) + }; } #[cfg(test)]