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
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use crate::errors::{check, Error};
use chemfiles_sys::chfl_status;
pub fn from_c(buffer: *const c_char) -> String {
unsafe {
let rust_str = CStr::from_ptr(buffer).to_str().expect("Invalid Rust string from C");
return String::from(rust_str);
}
}
pub fn to_c<S>(string: S) -> CString
where
S: Into<Vec<u8>>,
{
CString::new(string).expect("Invalid C string from Rust")
}
fn buffer_was_big_enough(buffer: &[c_char]) -> bool {
let len = buffer.len();
if len < 2 {
false
} else {
debug_assert_eq!(buffer[len - 1], 0);
buffer[len - 2] == 0
}
}
pub fn call_autogrow_buffer<F>(initial: usize, callback: F) -> Result<Vec<c_char>, Error>
where
F: Fn(*mut c_char, u64) -> chfl_status,
{
let mut size = initial;
let mut buffer = vec![0; size];
check(callback(buffer.as_mut_ptr(), buffer.len() as u64))?;
while !buffer_was_big_enough(&buffer) {
size *= 2;
buffer.resize(size, 0);
check(callback(buffer.as_mut_ptr(), buffer.len() as u64))?;
}
Ok(buffer)
}