1use std::ffi::CStr;
2use std::fs::File;
3use std::io::prelude::*;
4use std::os::raw::c_char;
5use std::path::Path;
6
7use errors::Result;
8
9extern "C" {
10 fn string_delete(string: *mut c_char);
11}
12
13pub fn ptr_to_string(ptr: *mut c_char) -> String {
16 if ptr.is_null() {
17 return String::new();
18 }
19
20 let string = unsafe { CStr::from_ptr(ptr) }
21 .to_string_lossy()
22 .into_owned();
23
24 unsafe { string_delete(ptr) };
25 string
26}
27
28pub fn read_buffer<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> {
30 let mut buffer = Vec::new();
31 let mut file = File::open(path)?;
32 file.read_to_end(&mut buffer)?;
33 Ok(buffer)
34}