Skip to content

Commit

Permalink
Merge pull request #27 from ac-freeman/set-input-stream
Browse files Browse the repository at this point in the history
Fix #26
  • Loading branch information
ac-freeman authored Sep 8, 2022
2 parents 81670ae + 6244d99 commit a453ccb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/bin/adderinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use adder_codec_rs::raw::raw_stream::RawStream;
use adder_codec_rs::{Codec, Intensity, D_SHIFT};
use clap::ArgAction::SetTrue;
use clap::Parser;
use std::io;
use std::io::Write;
use std::path::Path;
use std::{error, io};

/// Command line argument parser
#[derive(Parser, Debug, Default)]
Expand All @@ -20,7 +20,7 @@ pub struct MyArgs {
pub(crate) dynamic_range: bool,
}

fn main() -> Result<(), std::io::Error> {
fn main() -> Result<(), Box<dyn error::Error>> {
let args: MyArgs = MyArgs::parse();
let file_path = args.input.as_str();

Expand Down Expand Up @@ -63,7 +63,7 @@ fn main() -> Result<(), std::io::Error> {
// event, and what is the lowest intensity event?
if args.dynamic_range {
let divisor = num_events as u64 / 100;
stream.set_input_stream_position(first_event_position);
stream.set_input_stream_position(first_event_position)?;
let mut max_intensity: Intensity = 0.0;
let mut min_intensity: Intensity = f64::MAX;
let mut event_count: u64 = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ pub trait Codec {
fn set_output_stream(&mut self, stream: Option<BufWriter<File>>);
fn set_input_stream(&mut self, stream: Option<BufReader<File>>);

fn set_input_stream_position(&mut self, pos: u64);
/// Go to this position (as a byte address) in the input stream. Returns a [StreamError] if
/// not aligned to an [Event]
fn set_input_stream_position(&mut self, pos: u64) -> Result<(), StreamError>;
fn get_input_stream_position(&mut self) -> Result<u64, StreamError>;

fn get_eof_position(&mut self) -> Result<usize, StreamError>;
Expand Down
33 changes: 25 additions & 8 deletions src/raw/raw_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bincode::config::{BigEndian, FixintEncoding, WithOtherEndian, WithOtherIntEn
use bincode::{DefaultOptions, Options};
use std::fs::File;
use std::io::{BufReader, BufWriter, Seek, SeekFrom, Write};
use std::mem;
use std::{error, fmt, mem};

#[derive(Debug)]
pub enum StreamError {
Expand All @@ -22,12 +22,24 @@ pub enum StreamError {

/// File formatted incorrectly
BadFile,

/// Attempted to seek to a bad position in the stream
Seek,
}

impl fmt::Display for StreamError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Stream error")
}
}

impl error::Error for StreamError {}

pub struct RawStream {
output_stream: Option<BufWriter<File>>,
input_stream: Option<BufReader<File>>,
pub codec_version: u8,
header_size: usize,
pub width: u16,
pub height: u16,
pub tps: DeltaT,
Expand All @@ -45,6 +57,7 @@ impl Codec for RawStream {
output_stream: None,
input_stream: None,
codec_version: 1,
header_size: 0,
width: 0,
height: 0,
tps: 0,
Expand Down Expand Up @@ -129,17 +142,20 @@ impl Codec for RawStream {
self.input_stream = stream;
}

fn set_input_stream_position(&mut self, pos: u64) {
fn set_input_stream_position(&mut self, pos: u64) -> Result<(), StreamError> {
if (pos as usize - self.header_size) % self.event_size as usize != 0 {
return Err(StreamError::Seek);
}
match &mut self.input_stream {
None => {
panic!("Input stream not initialized");
}
Some(stream) => {
stream
.seek(SeekFrom::Start(pos))
.expect("Invalid seek position");
}
}
Some(stream) => match stream.seek(SeekFrom::Start(pos)) {
Ok(_) => {}
Err(_) => return Err(StreamError::Seek),
},
};
Ok(())
}

fn get_input_stream_position(&mut self) -> Result<u64, StreamError> {
Expand Down Expand Up @@ -300,6 +316,7 @@ impl Codec for RawStream {
0
}
};
self.header_size = header_size;

Ok(header_size)
}
Expand Down
20 changes: 20 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ use adder_codec_rs::SourceCamera::FramedU8;
use adder_codec_rs::{Codec, Coord, Event};
use rand::Rng;

#[test]
fn test_set_stream_position() {
let input_path = "./tests/samples/sample_1_raw_events.adder";
let mut stream: RawStream = Codec::new();
stream.open_reader(input_path).unwrap();
let header_size = stream.decode_header().unwrap();
for i in 1..stream.event_size as usize {
assert!(stream
.set_input_stream_position((header_size + i) as u64)
.is_err());
}

assert!(stream
.set_input_stream_position((header_size + stream.event_size as usize) as u64)
.is_ok());
assert!(stream
.set_input_stream_position((header_size + stream.event_size as usize * 2) as u64)
.is_ok());
}

#[test]
fn test_sample_perfect_dt() {
let input_path = "./tests/samples/sample_1_raw_events.adder";
Expand Down

0 comments on commit a453ccb

Please sign in to comment.