File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -71,11 +71,17 @@ pub extern "C" fn raw_config_dir() -> *mut c_char {
7171 . and_then ( |path| path. into_os_string ( ) . into_string ( ) . ok ( ) )
7272 . and_then ( |string| CString :: new ( string) . ok ( ) ) ;
7373
74- result. map_or ( null_mut ( ) , |c_str| {
75- CString :: new ( c_str)
76- . map ( CString :: into_raw)
77- . unwrap_or ( null_mut ( ) )
78- } )
74+ result. map_or ( null_mut ( ) , CString :: into_raw)
75+ }
76+
77+ #[ must_use]
78+ #[ unsafe( no_mangle) ]
79+ pub extern "C" fn raw_cache_dir ( ) -> * mut c_char {
80+ let result = dirs:: cache_dir ( )
81+ . and_then ( |path| path. into_os_string ( ) . into_string ( ) . ok ( ) )
82+ . and_then ( |string| CString :: new ( string) . ok ( ) ) ;
83+
84+ result. map_or ( null_mut ( ) , CString :: into_raw)
7985}
8086
8187#[ must_use]
Original file line number Diff line number Diff line change 1+ #[ test]
2+ fn test_raw_cache_dir ( ) {
3+ use back_end:: utils:: fs_utils:: raw_cache_dir;
4+ use std:: ffi:: CStr ;
5+
6+ unsafe {
7+ let result = raw_cache_dir ( ) ;
8+ let result_str = CStr :: from_ptr ( result) . to_string_lossy ( ) ;
9+
10+ assert ! ( !result. is_null( ) ) ;
11+ assert ! ( !result_str. is_empty( ) ) ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1-
21#include < cassert>
2+ #include < cstddef>
3+ #include < cstdlib>
4+ #include < span>
5+ #include < string>
6+
7+ using std::span;
8+ using std::string;
9+
310extern " C" bool raw_is_path_exists (const char *path);
11+ extern " C" char *raw_cache_dir ();
12+ extern " C" void raw_free_c_str (char *c_str);
413
5- int main () {
6- const char *valid_path = " ../tests-ffi" ;
14+ int main (int argc, char *argv[]) {
15+ const auto this_file_span = span (argv, size_t (argc));
16+
17+ if (this_file_span.empty ()) {
18+ abort ();
19+ }
20+
21+ const char *file_name = this_file_span[0 ];
722 const char *wrong_path = " ../abcxyzw1133" ;
823
9- assert (raw_is_path_exists (valid_path));
24+ char *raw_cache_dir_value = raw_cache_dir ();
25+ const string cache_dir = string (raw_cache_dir_value);
26+ raw_free_c_str (raw_cache_dir_value);
27+
28+ assert (raw_is_path_exists (file_name));
1029 assert (!raw_is_path_exists (wrong_path));
30+ assert (!cache_dir.empty ());
1131
1232 return 0 ;
1333}
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ function run_ffi_test() {
4747 build_output=$( basename " $file " " .cxx" )
4848 file_name=$( basename " $file " )
4949
50- $clang_cxx " $file " -l" $sqlite_lib " " $static_lib_path " -o " $build_dir /$build_output "
50+ $clang_cxx " $file " -l" $sqlite_lib " " $static_lib_path " -o " $build_dir /$build_output " -std=c++20
5151 echo
5252 echo -e -n " \e[0;32m[+] Test for $file_name :\e[0;37m"
5353
You can’t perform that action at this time.
0 commit comments