From a9cf78ee0d2a3f6afe87da2aebdce4a3da5e12a7 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Tue, 16 Jan 2024 12:46:38 +0100 Subject: [PATCH] i think it works --- Cargo.toml | 2 +- members/libpt-bin/src/hedu/mod.rs | 6 ++- members/libpt-hedu/src/lib.rs | 82 ++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c0917ef..b113a99 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.1.7" +version = "0.1.8" 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 8fc4983..a5565fa 100644 --- a/members/libpt-bin/src/hedu/mod.rs +++ b/members/libpt-bin/src/hedu/mod.rs @@ -63,6 +63,10 @@ pub struct Cli { #[arg(short, long, global = true)] pub log_meta: bool, + /// show character representation + #[arg(short, long, global = true)] + pub chars: bool, + /// a data source, probably a file pub data_source: String, } @@ -82,7 +86,7 @@ fn main() { std::process::exit(1); } }; - match dump(BufReader::new(file)) { + match dump(BufReader::new(file), cli.chars) { Ok(_) => (), Err(err) => { error!("Could not dump data of file: {err}"); diff --git a/members/libpt-hedu/src/lib.rs b/members/libpt-hedu/src/lib.rs index ee6dc29..f84b95c 100644 --- a/members/libpt-hedu/src/lib.rs +++ b/members/libpt-hedu/src/lib.rs @@ -5,24 +5,82 @@ //! //! This crate is currently empty. -use anyhow::Result; -use std::{ - fmt::Debug, - io::{prelude::*, BufRead, BufReader, Bytes}, -}; +use anyhow::{bail, Result}; +use std::io::{prelude::*, BufReader}; -pub fn dump(mut data: BufReader) -> Result<()> +const BYTES_PER_LINE: usize = 16; +const LINE_SEP_HORIZ: char = '─'; +const LINE_SEP_VERT: char = '│'; + +pub fn dump(mut data: BufReader, chars: bool) -> Result<()> where T: Read, { - for (i, b) in data.bytes().enumerate() { - if i % 8 == 0 { - print!("{:08X}\t", i); + let mut buf: [u8; BYTES_PER_LINE] = [0; BYTES_PER_LINE]; + let mut line_counter: usize = 0; + let mut len: usize; + // print the head + print!("LINE IDX {LINE_SEP_VERT} DATA AS HEX"); + if chars { + print!("{:width$} {LINE_SEP_VERT} FOO", "", width = 37); + } + println!(); + if chars { + println!("{}", format!("{LINE_SEP_HORIZ}").repeat(78)); + } else { + println!("{}", format!("{LINE_SEP_HORIZ}").repeat(59)); + } + // data dump loop + len = rd_data(&mut data, &mut buf).unwrap(); + while len > 0 { + print!("{:08X} {LINE_SEP_VERT} ", line_counter); + for i in 0..len { + if i as usize % BYTES_PER_LINE == BYTES_PER_LINE / 2 { + print!(" "); + } + print!("{:02X} ", buf[i]); } - print!("{:02X?} ", b.unwrap()); - if i % 8 == 7 { - println!(); + for i in 0..(BYTES_PER_LINE - len) { + if i as usize % BYTES_PER_LINE == BYTES_PER_LINE / 2 { + print!(" "); + } + print!(" "); } + if chars { + print!("{LINE_SEP_VERT} "); + for i in 0..len { + print!("{}", mask_chars(buf[i] as char)); + } + } + line_counter += 1; + println!(); + len = rd_data(&mut data, &mut buf).unwrap(); } Ok(()) } + +fn mask_chars(c: char) -> char { + if c.is_ascii_graphic() { + return c; + } else if c == '\n' { + return '↩'; + } else if c == ' ' { + return '␣'; + } else if c == '\t' { + return '⭾'; + } else { + return '�'; + } +} + +fn rd_data(data: &mut BufReader, mut buf: &mut [u8]) -> Result +where + T: Read, +{ + match data.read(&mut buf) { + Ok(len) => Ok(len), + Err(err) => { + bail!(err) + } + } +}