1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::path::{Path, MAIN_SEPARATOR};
use libc::c_char;
use std::ffi::{CStr, CString};
use std::str;

#[no_mangle]
pub extern fn add_trailing_separator(string: *const c_char) -> *const c_char {
  let c_str = unsafe {
    if string.is_null() {
      return string;
    }
    CStr::from_ptr(string)
  };
  let r_str = str::from_utf8(c_str.to_bytes()).unwrap_or("");

  if r_str.is_empty() {
    return string;
  }

  let path = Path::new(r_str);
  let out_str = if !(path.to_str().unwrap().chars().last().unwrap() == '/') {
    format!("{}{}", path.to_str().unwrap(), MAIN_SEPARATOR)
  } else {
    path.to_str().unwrap().to_string()
  };

  let output = CString::new(out_str).unwrap();
  output.into_raw()
}