Skip to content

Commit

Permalink
wip: allow arbitrary Read + Seek as file source
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Oct 9, 2024
1 parent 245b1c8 commit e6b7230
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
7 changes: 3 additions & 4 deletions wellen/src/fst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use std::io::{BufRead, Seek};

pub type Result<T> = std::result::Result<T, WellenError>;

pub fn read_header(
filename: &str,
pub fn read_header<R: BufRead + Seek>(
input: R,
_options: &LoadOptions,
) -> Result<(Hierarchy, ReadBodyContinuation)> {
let input = std::fs::File::open(filename)?;
let mut reader = FstReader::open_and_read_time_table(std::io::BufReader::new(input))?;
let mut reader = FstReader::open_and_read_time_table(input)?;
let hierarchy = read_hierarchy(&mut reader)?;
let cont = ReadBodyContinuation {
reader: Reader::File(reader),
Expand Down
53 changes: 31 additions & 22 deletions wellen/src/viewers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub fn read_header(filename: &str, options: &LoadOptions) -> Result<HeaderResult
})
}
FileFormat::Fst => {
let (hierarchy, body) = crate::fst::read_header(filename, options)?;
let input = std::io::BufReader::new(std::fs::File::open(filename)?);
let (hierarchy, body) = crate::fst::read_header(input, options)?;
let body = ReadBodyContinuation::new(ReadBodyData::Fst(body));
Ok(HeaderResult {
hierarchy,
Expand All @@ -71,34 +72,42 @@ pub fn read_header(filename: &str, options: &LoadOptions) -> Result<HeaderResult
}

pub fn read_header_from_bytes(bytes: Vec<u8>, options: &LoadOptions) -> Result<HeaderResult> {
let file_format = {
let mut cursor = &mut std::io::Cursor::new(&bytes);
detect_file_format(&mut cursor)
};
read_header_from_reader(std::io::Cursor::new(bytes), options)
}

pub fn read_header_from_reader<R>(mut input: R, options: &LoadOptions) -> Result<HeaderResult>
where
R: std::io::BufRead + std::io::Seek,
{
// remember where we are supposed to start reading
let start = input.stream_position()?;
let file_format = { detect_file_format(&mut input) };
match file_format {
FileFormat::Unknown => Err(WellenError::UnknownFileFormat),
FileFormat::Vcd => {
let (hierarchy, body, body_len) = crate::vcd::read_header_from_bytes(bytes, options)?;
let body = ReadBodyContinuation::new(ReadBodyData::Vcd(body));
Ok(HeaderResult {
hierarchy,
file_format,
body_len,
body,
})
todo!()
// let (hierarchy, body, body_len) = crate::vcd::read_header_from_bytes(bytes, options)?;
// let body = ReadBodyContinuation::new(ReadBodyData::Vcd(body));
// Ok(HeaderResult {
// hierarchy,
// file_format,
// body_len,
// body,
// })
}
FileFormat::Ghw => {
let (hierarchy, body, body_len) = crate::ghw::read_header_from_bytes(bytes, options)?;
let body = ReadBodyContinuation::new(ReadBodyData::Ghw(body));
Ok(HeaderResult {
hierarchy,
file_format,
body_len,
body,
})
todo!()
// let (hierarchy, body, body_len) = crate::ghw::read_header_from_bytes(bytes, options)?;
// let body = ReadBodyContinuation::new(ReadBodyData::Ghw(body));
// Ok(HeaderResult {
// hierarchy,
// file_format,
// body_len,
// body,
// })
}
FileFormat::Fst => {
let (hierarchy, body) = crate::fst::read_header_from_bytes(bytes, options)?;
let (hierarchy, body) = crate::fst::read_header(input, options)?;
let body = ReadBodyContinuation::new(ReadBodyData::Fst(body));
Ok(HeaderResult {
hierarchy,
Expand Down

0 comments on commit e6b7230

Please sign in to comment.