i think it works

This commit is contained in:
Christoph J. Scherr 2024-01-16 12:46:38 +01:00
parent 66932f70a3
commit a9cf78ee0d
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
3 changed files with 76 additions and 14 deletions

View File

@ -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 <software@cscherr.de>"]
license = "MIT"

View File

@ -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}");

View File

@ -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<T>(mut data: BufReader<T>) -> Result<()>
const BYTES_PER_LINE: usize = 16;
const LINE_SEP_HORIZ: char = '─';
const LINE_SEP_VERT: char = '│';
pub fn dump<T>(mut data: BufReader<T>, 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);
}
print!("{:02X?} ", b.unwrap());
if i % 8 == 7 {
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]);
}
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 '<27>';
}
}
fn rd_data<T>(data: &mut BufReader<T>, mut buf: &mut [u8]) -> Result<usize>
where
T: Read,
{
match data.read(&mut buf) {
Ok(len) => Ok(len),
Err(err) => {
bail!(err)
}
}
}