summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: da0e599e9b87cd3a6103bdd04e314214c44dcae4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::{env, fs::File, io::Read};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    const CHUNK_SIZE: usize = 8;

    let filename = env::args().nth(1).unwrap_or_default();
    if filename.is_empty() {
        eprintln!("error missing filename");
        return Ok(());
    }

    let mut file = File::open(&filename)?;
    let mut chunk = Vec::with_capacity(CHUNK_SIZE);
    let mut idx = 0;

    while {
        chunk.clear();
        file.by_ref()
            .take(CHUNK_SIZE as u64)
            .read_to_end(&mut chunk)?
            > 0
    } {
        // write index
        print!("{:010}: ", idx);

        // write hex
        for byte in &chunk {
            print!("{:02X} ", byte);
        }

        // write ascii
        println!("{}", String::from_utf8_lossy(&chunk).replace('\n', "⏎"));
        idx += CHUNK_SIZE;
    }

    Ok(())
}