From 4d994eff01af837388699c0e44a59e001192b00a Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 16 Jan 2024 19:34:43 +0100 Subject: [PATCH] hedu limit --- Cargo.toml | 2 +- members/libpt-bin/src/hedu/mod.rs | 13 +++----- members/libpt-hedu/src/lib.rs | 54 +++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f241b93..4f081b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ members = [ default-members = [".", "members/libpt-bin", "members/libpt-core"] [workspace.package] publish = true -version = "0.2.3" +version = "0.2.4" edition = "2021" authors = ["Christoph J. Scherr "] license = "MIT" diff --git a/members/libpt-bin/src/hedu/mod.rs b/members/libpt-bin/src/hedu/mod.rs index 0673216..91fe487 100644 --- a/members/libpt-bin/src/hedu/mod.rs +++ b/members/libpt-bin/src/hedu/mod.rs @@ -77,7 +77,7 @@ pub struct Cli { /// only interpret N bytes (end after N) #[arg(short, long, default_value_t = 0)] - pub len: usize, + pub limit: usize, /// show identical lines #[arg(short = 'i', long)] @@ -99,7 +99,7 @@ fn main() { let mut source: Box; if cli.data_source.is_some() && cli.data_source.clone().is_some_and(|val| val != "-") { let data_source = cli.data_source.unwrap(); - debug!("Trying to open '{}'", data_source); + trace!("Trying to open '{}'", data_source); source = match File::open(&data_source) { Ok(file) => Box::new(file), Err(err) => { @@ -108,7 +108,7 @@ fn main() { } }; } else { - debug!("Trying to open stdout"); + trace!("Trying to open stdout"); let stdin = std::io::stdin(); if stdin.is_terminal() { warn!("Refusing to dump from interactive terminal"); @@ -119,12 +119,7 @@ fn main() { match dump( &mut *source, - DumpConfig { - chars: cli.chars, - skip: cli.skip, - show_identical: cli.show_identical, - len: cli.len, - }, + HeduConfig::new(cli.chars, cli.skip, cli.show_identical, cli.limit), ) { Ok(_) => (), Err(err) => { diff --git a/members/libpt-hedu/src/lib.rs b/members/libpt-hedu/src/lib.rs index 44c8431..584800a 100644 --- a/members/libpt-hedu/src/lib.rs +++ b/members/libpt-hedu/src/lib.rs @@ -7,18 +7,31 @@ use anyhow::{bail, Result}; use libpt_bintols::display::{bytes_to_bin, humanbytes}; -use libpt_log::{error, info, trace, warn, debug}; +use libpt_log::{debug, error, info, trace, warn}; use std::io::{prelude::*, BufReader, Read, SeekFrom}; const BYTES_PER_LINE: usize = 16; const LINE_SEP_HORIZ: char = '─'; const LINE_SEP_VERT: char = '│'; -pub struct DumpConfig { +pub struct HeduConfig { pub chars: bool, pub skip: usize, pub show_identical: bool, - pub len: usize, + pub limit: usize, + stop: bool, +} + +impl HeduConfig { + pub fn new(chars: bool, skip: usize, show_identical: bool, limit: usize) -> Self { + HeduConfig { + chars, + skip, + show_identical, + limit: limit, + stop: false, + } + } } pub trait DataSource: Read { @@ -38,7 +51,7 @@ impl DataSource for std::fs::File { } } -pub fn dump(data: &mut dyn DataSource, config: DumpConfig) -> Result<()> { +pub fn dump(data: &mut dyn DataSource, mut config: HeduConfig) -> Result<()> { // prepare some variables let mut buf: [[u8; BYTES_PER_LINE]; 2] = [[0; BYTES_PER_LINE]; 2]; let mut alt_buf = 0usize; @@ -65,7 +78,7 @@ pub fn dump(data: &mut dyn DataSource, config: DumpConfig) -> Result<()> { } // data dump loop - len = rd_data(data, &mut buf, &mut alt_buf).unwrap(); + len = rd_data(data, &mut buf, &mut alt_buf, &mut byte_counter, &mut config).unwrap(); while len > 0 { print!("{:08X} {LINE_SEP_VERT} ", byte_counter); for i in 0..len { @@ -90,15 +103,22 @@ pub fn dump(data: &mut dyn DataSource, config: DumpConfig) -> Result<()> { } print!("|"); } - byte_counter += 1; println!(); - len = rd_data(data, &mut buf, &mut alt_buf).unwrap(); + + // loop breaker logic + if config.stop { + break; + } + + // after line logic + len = rd_data(data, &mut buf, &mut alt_buf, &mut byte_counter, &mut config).unwrap(); alt_buf ^= 1; // toggle the alt buf if buf[0] == buf[1] && len == BYTES_PER_LINE && !config.show_identical { trace!(buf = format!("{:?}", buf), "found a duplicating line"); let start_line = byte_counter; while buf[0] == buf[1] && len == BYTES_PER_LINE { - len = rd_data(data, &mut buf, &mut alt_buf).unwrap(); + len = + rd_data(data, &mut buf, &mut alt_buf, &mut byte_counter, &mut config).unwrap(); byte_counter += 1; } println!( @@ -131,9 +151,25 @@ fn rd_data( data: &mut dyn DataSource, buf: &mut [[u8; BYTES_PER_LINE]; 2], alt_buf: &mut usize, + byte_counter: &mut usize, + config: &mut HeduConfig, ) -> Result { match data.read(&mut buf[*alt_buf]) { - Ok(len) => { + Ok(mut len) => { + *byte_counter += len; + if *byte_counter >= config.limit { + trace!( + byte_counter, + limit = config.limit, + len, + nlen = (config.limit % BYTES_PER_LINE), + "byte counter is farther than limit" + ); + if config.limit != 0 { + len = config.limit % BYTES_PER_LINE; + } + config.stop = true; + } return Ok(len); } Err(err) => {