blob: e218138b3d942938d0837d3398b26191988a83d6 (
plain)
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
38
39
|
use std::{io::Write, path::PathBuf};
use clap::Parser;
#[derive(Debug, Parser)]
struct Args {
file: Option<String>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let stdout = std::io::stdout();
let handle = stdout.lock();
let mut writer = std::io::BufWriter::new(handle);
let args = Args::parse();
let mut reader = match args.file {
Some(file) => {
let path = PathBuf::from(file);
let file = std::fs::File::open(path)?;
Box::new(std::io::BufReader::new(file)) as Box<dyn std::io::Read>
}
None => Box::new(std::io::BufReader::new(std::io::stdin())) as Box<dyn std::io::Read>,
};
let mut buf = [0u8; 1024];
write!(writer, "[")?;
loop {
let n = reader.read(&mut buf)?;
if n == 0 {
break;
}
for i in 0..n {
write!(writer, "{:#02X},", buf[i])?;
}
}
write!(writer, "];")?;
writer.flush()?;
Ok(())
}
|