some fixes for hedu #64

Merged
PlexSheep merged 15 commits from devel into master 2024-01-18 13:45:27 +01:00
2 changed files with 64 additions and 18 deletions
Showing only changes of commit 7e9d337005 - Show all commits

View File

@ -27,7 +27,7 @@ pub struct HeduConfig {
rd_counter: usize, rd_counter: usize,
buf: [[u8; BYTES_PER_LINE]; 2], buf: [[u8; BYTES_PER_LINE]; 2],
alt_buf: usize, alt_buf: usize,
display_buf: String, pub display_buf: String,
} }
impl HeduConfig { impl HeduConfig {
@ -38,7 +38,7 @@ impl HeduConfig {
show_identical, show_identical,
limit, limit,
stop: false, stop: false,
len: usize::MIN, len: BYTES_PER_LINE,
data_idx: usize::MIN, data_idx: usize::MIN,
rd_counter: usize::MIN, rd_counter: usize::MIN,
buf: [[0; BYTES_PER_LINE]; 2], buf: [[0; BYTES_PER_LINE]; 2],
@ -47,10 +47,24 @@ impl HeduConfig {
} }
} }
#[inline] #[inline]
fn display(&mut self) { pub fn display(&mut self) {
println!("{}", self.display_buf); println!("{}", self.display_buf);
self.display_buf = String::new(); self.display_buf = String::new();
} }
#[inline]
pub fn sep(&mut self) {
if self.chars {
self.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(80);
} else {
self.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(59);
}
self.display();
}
#[inline]
pub fn newline(&mut self) {
self.display_buf += "\n";
self.display();
}
} }
pub trait DataSource: Read { pub trait DataSource: Read {
@ -82,20 +96,15 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> {
// print the head // print the head
config.display_buf += &format!("DATA IDX {LINE_SEP_VERT} DATA AS HEX"); config.display_buf += &format!("DATA IDX {LINE_SEP_VERT} DATA AS HEX");
if config.chars { if config.chars {
config.display_buf += &format!("{:width$} {LINE_SEP_VERT}", "", width = 37); config.display_buf += &format!("{:width$} {LINE_SEP_VERT} DATA AS CHAR", "", width = 37);
}
config.display();
if config.chars {
config.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(80);
} else {
config.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(59);
} }
config.display(); config.display();
config.sep();
// data dump loop // data dump loop
rd_data(data, config)?;
while config.len > 0 { while config.len > 0 {
config.display_buf += &format!("{:08X} {LINE_SEP_VERT} ", config.data_idx); config.display_buf += &format!("{:08X} {LINE_SEP_VERT} ", config.data_idx);
rd_data(data, config)?;
for i in 0..config.len { for i in 0..config.len {
if i as usize % BYTES_PER_LINE == BYTES_PER_LINE / 2 { if i as usize % BYTES_PER_LINE == BYTES_PER_LINE / 2 {
config.display_buf += " "; config.display_buf += " ";
@ -127,7 +136,6 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> {
} }
// after line logic // after line logic
rd_data(data, config)?;
config.alt_buf ^= 1; // toggle the alt buf config.alt_buf ^= 1; // toggle the alt buf
if config.buf[0] == config.buf[1] && config.len == BYTES_PER_LINE && !config.show_identical if config.buf[0] == config.buf[1] && config.len == BYTES_PER_LINE && !config.show_identical
{ {
@ -139,11 +147,14 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> {
while config.buf[0] == config.buf[1] && config.len == BYTES_PER_LINE { while config.buf[0] == config.buf[1] && config.len == BYTES_PER_LINE {
rd_data(data, config)?; rd_data(data, config)?;
} }
config.alt_buf ^= 1; // toggle the alt buf (now that we have a not same line)
config.display_buf += &format!( config.display_buf += &format!(
"^^^^^^^^ {LINE_SEP_VERT} (repeats {} lines)", "^^^^^^^^ {LINE_SEP_VERT} (repeats {} lines){:32}",
(config.data_idx - start_line) / (BYTES_PER_LINE * 2) (config.data_idx - start_line) / (BYTES_PER_LINE * 2),
""
); );
if config.chars {
config.display_buf += &format!("{LINE_SEP_VERT}");
}
trace!( trace!(
buf = format!("{:X?}", config.buf), buf = format!("{:X?}", config.buf),
"dumping buf after line skip" "dumping buf after line skip"
@ -154,6 +165,31 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> {
// We detect duplicate lines with this // We detect duplicate lines with this
config.alt_buf ^= 1; // toggle the alt buf config.alt_buf ^= 1; // toggle the alt buf
} }
config.data_idx += config.len;
config.sep();
config.display_buf += &format!(
"{:08X} {LINE_SEP_VERT} dumped total:\t{:<8} {:<16}{:3}",
config.rd_counter,
humanbytes(config.rd_counter),
format!("({} B)",config.rd_counter),
""
);
if config.chars {
config.display_buf += &format!("{LINE_SEP_VERT}");
}
config.display();
config.display_buf += &format!(
"{:08X} {LINE_SEP_VERT} read total:\t\t{:<8} {:<16}{:3}",
config.data_idx,
humanbytes(config.data_idx),
format!("({} B)",config.data_idx),
""
);
if config.chars {
config.display_buf += &format!("{LINE_SEP_VERT}");
}
config.display();
Ok(()) Ok(())
} }
@ -173,7 +209,7 @@ fn mask_chars(c: char) -> char {
#[inline] #[inline]
fn adjust_data_idx(config: &mut HeduConfig) { fn adjust_data_idx(config: &mut HeduConfig) {
config.data_idx += BYTES_PER_LINE - config.data_idx % BYTES_PER_LINE; config.data_idx -= config.data_idx % BYTES_PER_LINE;
} }
fn rd_data(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> { fn rd_data(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> {

View File

@ -104,7 +104,7 @@ fn main() {
if !data_source.is_file() { if !data_source.is_file() {
debug!("Not a regular file'{:?}'", data_source); debug!("Not a regular file'{:?}'", data_source);
// std::process::exit(1); // std::process::exit(1);
continue continue;
} }
trace!("Trying to open '{:?}'", data_source); trace!("Trying to open '{:?}'", data_source);
match File::open(&data_source) { match File::open(&data_source) {
@ -128,8 +128,15 @@ fn main() {
sources.push(Box::new(stdin)); sources.push(Box::new(stdin));
} }
for (i, source) in sources.iter_mut().enumerate() { for (i, source) in sources.iter_mut().enumerate() {
println!("{:=^59}", format!(" {} ", cli.data_source[i]));
let mut config = HeduConfig::new(cli.chars, cli.skip, cli.show_identical, cli.limit); let mut config = HeduConfig::new(cli.chars, cli.skip, cli.show_identical, cli.limit);
match config.chars {
false => {
println!("{:─^59}", format!(" {} ", cli.data_source[i]));
}
true => {
println!("{:─^80}", format!(" {} ", cli.data_source[i]));
}
}
match dump(&mut **source, &mut config) { match dump(&mut **source, &mut config) {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
@ -137,6 +144,9 @@ fn main() {
std::process::exit(3); std::process::exit(3);
} }
} }
if i < cli.data_source.len() - 1 {
config.newline();
}
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////