forked from fanvanzh/3dtiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun_c.rs
62 lines (58 loc) · 1.58 KB
/
fun_c.rs
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#[no_mangle]
pub extern "C" fn write_file(file_name: *const i8, buf: *const u8, buf_len: u32) -> bool {
use std::ffi;
use std::slice;
use std::fs::File;
use std::error::Error;
use std::io::prelude::*;
unsafe {
if let Ok(file_name) = ffi::CStr::from_ptr(file_name).to_str() {
if let Ok(mut f) = File::create(file_name) {
let arr = slice::from_raw_parts(buf, buf_len as usize);
match f.write_all(arr) {
Ok(_) => true,
Err(e) => {
error!("{}", e.description());
false
}
}
} else {
false
}
} else {
error!("convert file_name fail");
false
}
}
}
#[no_mangle]
pub extern "C" fn mkdirs(path: *const i8) -> bool {
use std::fs;
use std::ffi;
use std::error::Error;
unsafe {
match ffi::CStr::from_ptr(path).to_str() {
Ok(buf) => {
match fs::create_dir_all(buf) {
Ok(_) => true,
Err(e) => {
error!("{}", e.description());
false
}
}
}
Err(e) => {
error!("{}", e.description());
false
}
}
}
}
#[no_mangle]
pub extern "C" fn log_error(msg: *const i8) {
use std::ffi::CStr;
unsafe {
let input = CStr::from_ptr(msg);
error!("{}", input.to_string_lossy());
}
}