hedu limit
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 2m1s Details

This commit is contained in:
Christoph J. Scherr 2024-01-16 19:34:43 +01:00
parent 521ae9a193
commit 0155465332
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
3 changed files with 49 additions and 20 deletions

View File

@ -13,7 +13,7 @@ members = [
default-members = [".", "members/libpt-bin", "members/libpt-core"] default-members = [".", "members/libpt-bin", "members/libpt-core"]
[workspace.package] [workspace.package]
publish = true publish = true
version = "0.2.3" version = "0.2.4"
edition = "2021" edition = "2021"
authors = ["Christoph J. Scherr <software@cscherr.de>"] authors = ["Christoph J. Scherr <software@cscherr.de>"]
license = "MIT" license = "MIT"

View File

@ -77,7 +77,7 @@ pub struct Cli {
/// only interpret N bytes (end after N) /// only interpret N bytes (end after N)
#[arg(short, long, default_value_t = 0)] #[arg(short, long, default_value_t = 0)]
pub len: usize, pub limit: usize,
/// show identical lines /// show identical lines
#[arg(short = 'i', long)] #[arg(short = 'i', long)]
@ -99,7 +99,7 @@ fn main() {
let mut source: Box<dyn DataSource>; let mut source: Box<dyn DataSource>;
if cli.data_source.is_some() && cli.data_source.clone().is_some_and(|val| val != "-") { if cli.data_source.is_some() && cli.data_source.clone().is_some_and(|val| val != "-") {
let data_source = cli.data_source.unwrap(); 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) { source = match File::open(&data_source) {
Ok(file) => Box::new(file), Ok(file) => Box::new(file),
Err(err) => { Err(err) => {
@ -108,7 +108,7 @@ fn main() {
} }
}; };
} else { } else {
debug!("Trying to open stdout"); trace!("Trying to open stdout");
let stdin = std::io::stdin(); let stdin = std::io::stdin();
if stdin.is_terminal() { if stdin.is_terminal() {
warn!("Refusing to dump from interactive terminal"); warn!("Refusing to dump from interactive terminal");
@ -119,12 +119,7 @@ fn main() {
match dump( match dump(
&mut *source, &mut *source,
DumpConfig { HeduConfig::new(cli.chars, cli.skip, cli.show_identical, cli.limit),
chars: cli.chars,
skip: cli.skip,
show_identical: cli.show_identical,
len: cli.len,
},
) { ) {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {

View File

@ -7,18 +7,31 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use libpt_bintols::display::{bytes_to_bin, humanbytes}; 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}; use std::io::{prelude::*, BufReader, Read, SeekFrom};
const BYTES_PER_LINE: usize = 16; const BYTES_PER_LINE: usize = 16;
const LINE_SEP_HORIZ: char = '─'; const LINE_SEP_HORIZ: char = '─';
const LINE_SEP_VERT: char = '│'; const LINE_SEP_VERT: char = '│';
pub struct DumpConfig { pub struct HeduConfig {
pub chars: bool, pub chars: bool,
pub skip: usize, pub skip: usize,
pub show_identical: bool, 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 { 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 // prepare some variables
let mut buf: [[u8; BYTES_PER_LINE]; 2] = [[0; BYTES_PER_LINE]; 2]; let mut buf: [[u8; BYTES_PER_LINE]; 2] = [[0; BYTES_PER_LINE]; 2];
let mut alt_buf = 0usize; let mut alt_buf = 0usize;
@ -65,7 +78,7 @@ pub fn dump(data: &mut dyn DataSource, config: DumpConfig) -> Result<()> {
} }
// data dump loop // 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 { while len > 0 {
print!("{:08X} {LINE_SEP_VERT} ", byte_counter); print!("{:08X} {LINE_SEP_VERT} ", byte_counter);
for i in 0..len { for i in 0..len {
@ -90,16 +103,23 @@ pub fn dump(data: &mut dyn DataSource, config: DumpConfig) -> Result<()> {
} }
print!("|"); print!("|");
} }
byte_counter += 1;
println!(); 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 alt_buf ^= 1; // toggle the alt buf
if buf[0] == buf[1] && len == BYTES_PER_LINE && !config.show_identical { if buf[0] == buf[1] && len == BYTES_PER_LINE && !config.show_identical {
trace!(buf = format!("{:?}", buf), "found a duplicating line"); trace!(buf = format!("{:?}", buf), "found a duplicating line");
let start_line = byte_counter; let start_line = byte_counter;
while buf[0] == buf[1] && len == BYTES_PER_LINE { while buf[0] == buf[1] && len == BYTES_PER_LINE {
len = rd_data(data, &mut buf, &mut alt_buf).unwrap(); len =
byte_counter += 1; rd_data(data, &mut buf, &mut alt_buf, &mut byte_counter, &mut config).unwrap();
byte_counter += BYTES_PER_LINE;
} }
println!( println!(
"^^^^^^^^ {LINE_SEP_VERT} (repeats {} lines)", "^^^^^^^^ {LINE_SEP_VERT} (repeats {} lines)",
@ -131,9 +151,23 @@ fn rd_data(
data: &mut dyn DataSource, data: &mut dyn DataSource,
buf: &mut [[u8; BYTES_PER_LINE]; 2], buf: &mut [[u8; BYTES_PER_LINE]; 2],
alt_buf: &mut usize, alt_buf: &mut usize,
byte_counter: &mut usize,
config: &mut HeduConfig,
) -> Result<usize> { ) -> Result<usize> {
match data.read(&mut buf[*alt_buf]) { match data.read(&mut buf[*alt_buf]) {
Ok(len) => { Ok(mut len) => {
*byte_counter += len; // FIXME: incremented too early! dump always starts at 0x10
if config.limit != 0 && *byte_counter >= config.limit {
trace!(
byte_counter,
limit = config.limit,
len,
nlen = (config.limit % BYTES_PER_LINE),
"byte counter is farther than limit"
);
len = config.limit % BYTES_PER_LINE;
config.stop = true;
}
return Ok(len); return Ok(len);
} }
Err(err) => { Err(err) => {