use std::ffi::CString;
use nappgui_sys::{stm_from_block, stm_from_file, stm_memory, stm_to_file};
use crate::error::NappguiError;
pub struct Stream {
pub(crate) inner: *mut nappgui_sys::Stream,
}
impl Stream {
pub(crate) fn new(ptr: *mut nappgui_sys::Stream) -> Self {
if ptr.is_null() {
panic!("ptr is null");
}
Self { inner: ptr }
}
pub fn from_block(data: &[u8], size: u32) -> Self {
let ptr = unsafe { stm_from_block(data.as_ptr(), size) };
Self::new(ptr)
}
pub fn memory(size: u32) -> Self {
let ptr = unsafe { stm_memory(size) };
Self::new(ptr)
}
pub fn from_file(pathname: &str) -> Result<Self, NappguiError> {
let error = std::ptr::null_mut();
let pathname = CString::new(pathname).unwrap();
let ptr = unsafe { stm_from_file(pathname.as_ptr(), error) };
let error = unsafe { *error };
if !ptr.is_null() {
Ok(Self::new(ptr))
} else {
Err(NappguiError::from_ferror_t(error))
}
}
pub fn to_file(pathname: &str) -> Result<Self, NappguiError> {
let error = std::ptr::null_mut();
let pathname = CString::new(pathname).unwrap();
let ptr = unsafe { stm_to_file(pathname.as_ptr(), error) };
let error = unsafe { *error };
if !ptr.is_null() {
Ok(Self::new(ptr))
} else {
Err(NappguiError::from_ferror_t(error))
}
}
pub fn append_file(pathname: &str) -> Result<Self, NappguiError> {
let error = std::ptr::null_mut();
let pathname = CString::new(pathname).unwrap();
let ptr = unsafe { stm_to_file(pathname.as_ptr(), error) };
let error = unsafe { *error };
if !ptr.is_null() {
Ok(Self::new(ptr))
} else {
Err(NappguiError::from_ferror_t(error))
}
}
}