diff --git a/src/media.rs b/src/media.rs index 4cdfb90..362120e 100644 --- a/src/media.rs +++ b/src/media.rs @@ -1,9 +1,9 @@ -use std::ffi::{CString}; +use std::ffi::CString; use std::mem; use std::path::Path; - +use libc::c_ulong; use std::os::unix::prelude::*; - +use std::slice; /// A trait for types that can store media. pub trait Media { @@ -63,6 +63,28 @@ impl FileMedia { } } } + + pub fn create_mem() -> ::Result { + let mut ptr = unsafe { mem::uninitialized() }; + + match unsafe { ::gphoto2::gp_file_new(&mut ptr) } { + ::gphoto2::GP_OK => Ok(FileMedia { file: ptr }), + err => Err(::error::from_libgphoto2(err)), + } + } + + pub fn get_data(&mut self) -> Vec { + let mut ptr = unsafe { mem::uninitialized() }; + let mut len: c_ulong = 0; + + unsafe { + ::gphoto2::gp_file_get_data_and_size(self.file, &mut ptr, &mut len) + }; + + unsafe { + slice::from_raw_parts(ptr as *const u8, len as usize).to_vec() + } + } } impl Media for FileMedia {