summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author53hornet <atc@53hor.net>2021-08-05 11:41:41 -0400
committer53hornet <atc@53hor.net>2021-08-05 11:41:41 -0400
commit5befe69b3f4ae57d4dbd366268acedb48b63678f (patch)
tree439e0003ba8aafb299c596bb109a7bcf853916cf
parenta2c00337bc9bac1942ad835181ae09f8a66d524b (diff)
downloadrexdump-5befe69b3f4ae57d4dbd366268acedb48b63678f.tar.xz
rexdump-5befe69b3f4ae57d4dbd366268acedb48b63678f.zip
read from stdin if filename is missing
-rw-r--r--src/main.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index da0e599..fb030d0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,20 +1,20 @@
+use std::io::BufReader;
use std::{env, fs::File, io::Read};
fn main() -> Result<(), Box<dyn std::error::Error>> {
const CHUNK_SIZE: usize = 8;
+ let mut file: Box<dyn Read>;
- let filename = env::args().nth(1).unwrap_or_default();
- if filename.is_empty() {
- eprintln!("error missing filename");
- return Ok(());
- }
+ file = if let Some(filename) = env::args().nth(1) {
+ Box::new(BufReader::new(File::open(&filename)?))
+ } else {
+ Box::new(BufReader::new(std::io::stdin()))
+ };
- 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)?
@@ -31,6 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// write ascii
println!("{}", String::from_utf8_lossy(&chunk).replace('\n', "⏎"));
idx += CHUNK_SIZE;
+ chunk.clear();
}
Ok(())