File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ use std:: { ffi:: { CStr } , os:: raw:: c_char, path:: Path } ;
2+
3+ #[ unsafe( no_mangle) ]
4+ pub unsafe extern "C" fn is_valid_path ( path : * mut c_char ) -> bool { unsafe {
5+ if path. is_null ( ) {
6+ return false ;
7+ }
8+
9+ let c_str = match CStr :: from_ptr ( path) . to_str ( ) {
10+ Ok ( string) => string,
11+ Err ( _) => return false ,
12+ } ;
13+
14+ if c_str. is_empty ( ) {
15+ return false ;
16+ }
17+
18+ let path = Path :: new ( c_str) ;
19+
20+ path
21+ . as_os_str ( )
22+ . as_encoded_bytes ( )
23+ . iter ( )
24+ . all ( |& char| char != 0 )
25+ } }
26+
27+ #[ test]
28+ fn test_is_valid_path ( ) {
29+ use std:: ffi:: { CString } ;
30+ use std:: ptr:: null_mut;
31+ use crate :: alloc:: memory:: free_c_str;
32+
33+ unsafe {
34+ let c_string = CString
35+ :: new ( "../../src/utils/path.rs" ) /* Path to some exits file ...*/
36+ . map ( |raw| raw. into_raw ( ) )
37+ . unwrap_or ( null_mut ( ) ) ;
38+
39+ assert_eq ! ( true , is_valid_path( c_string) ) ;
40+ free_c_str ( c_string) ;
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments