mod ascii;
mod binary;
mod changes;
mod normalize;
mod recalc;
pub use changes::StatusChange;
pub use recalc::recalculate_segments;
use std::path::Path;
use cbase::encoding;
use cbase::error::{Error, Result};
use cfg::{Config, DataType};
#[derive(Debug, Clone, Default)]
pub struct DatFile {
pub sample_index: Vec<i32>,
pub timestamp_us: Vec<f64>,
pub analogs: Vec<Vec<f64>>,
pub statuses: Vec<Vec<u8>>,
}
impl DatFile {
pub fn len(&self) -> usize {
self.sample_index.len()
}
pub fn is_empty(&self) -> bool {
self.sample_index.is_empty()
}
pub fn analog_count(&self) -> usize {
self.analogs.len()
}
pub fn status_count(&self) -> usize {
self.statuses.len()
}
pub fn from_file(path: &Path, cfg: &Config) -> Result<DatFile> {
let bytes = std::fs::read(path).map_err(|e| Error::Io {
path: path.to_path_buf(),
source: e,
})?;
DatFile::from_bytes(&bytes, cfg)
}
pub fn from_bytes(bytes: &[u8], cfg: &Config) -> Result<DatFile> {
match cfg.data_type {
DataType::Ascii => {
let text = encoding::decode(bytes);
DatFile::from_ascii(&text, cfg)
},
_ => binary::parse_binary(bytes, cfg),
}
}
pub fn from_ascii(text: &str, cfg: &Config) -> Result<DatFile> {
let mut dat = ascii::parse_ascii(text, cfg)?;
dat = normalize::fit_to_config(dat, cfg)?;
normalize::apply_timemult(&mut dat, cfg);
Ok(dat)
}
pub fn to_ascii(&self, cfg: &Config) -> String {
ascii::write_ascii(self, cfg)
}
pub fn to_bytes(&self, cfg: &Config, dt: DataType) -> Vec<u8> {
binary::write_binary(self, cfg, dt)
}
pub fn write_file(&self, path: &Path, cfg: &Config, dt: DataType) -> Result<()> {
let bytes = match dt {
DataType::Ascii => self.to_ascii(cfg).into_bytes(),
_ => self.to_bytes(cfg, dt),
};
std::fs::write(path, bytes).map_err(|e| Error::Io {
path: path.to_path_buf(),
source: e,
})
}
pub fn analog_samples(&self, channel_index: usize) -> Option<&[f64]> {
self.analogs.get(channel_index).map(|v| v.as_slice())
}
pub fn status_samples(&self, channel_index: usize) -> Option<&[u8]> {
self.statuses.get(channel_index).map(|v| v.as_slice())
}
}