Read from file or stdin
This commit is contained in:
parent
c4353a4d03
commit
cc2f67dda9
24
src/main.rs
24
src/main.rs
|
@ -2,21 +2,12 @@ mod colormap;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::{self, Read};
|
||||||
|
|
||||||
use colormap::{map_char_to_color, map_u8_to_color};
|
use colormap::{map_char_to_color, map_u8_to_color};
|
||||||
|
|
||||||
const GLOBAL_BUFFER_LENGTH: usize = 16;
|
const GLOBAL_BUFFER_LENGTH: usize = 16;
|
||||||
|
|
||||||
fn get_file(filename: String) -> File {
|
|
||||||
match File::open(filename) {
|
|
||||||
Ok(f) => f,
|
|
||||||
Err(e) => {
|
|
||||||
panic!("{:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dump_to_hex(bytes: &mut [u8]) -> String {
|
fn dump_to_hex(bytes: &mut [u8]) -> String {
|
||||||
let out_vec: Vec<String> = bytes
|
let out_vec: Vec<String> = bytes
|
||||||
.chunks(2)
|
.chunks(2)
|
||||||
|
@ -38,18 +29,19 @@ fn dump_to_chr(bytes: &mut [u8]) -> String {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
if args.len() < 2 {
|
|
||||||
panic!("Not enough arguments!");
|
|
||||||
}
|
|
||||||
|
|
||||||
let filename = &args[1];
|
let mut reader: Box<dyn io::Read> = if args.len() == 1 {
|
||||||
let mut open_file = get_file(filename.to_string());
|
Box::new(io::stdin())
|
||||||
|
} else {
|
||||||
|
let filename = &args[1];
|
||||||
|
Box::new(File::open(filename).expect("Could not open file."))
|
||||||
|
};
|
||||||
|
|
||||||
let mut buf = [0; GLOBAL_BUFFER_LENGTH];
|
let mut buf = [0; GLOBAL_BUFFER_LENGTH];
|
||||||
let mut offset: usize = 0;
|
let mut offset: usize = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let bytes_read = open_file.read(&mut buf);
|
let bytes_read = reader.read(&mut buf);
|
||||||
match bytes_read {
|
match bytes_read {
|
||||||
Ok(num) => {
|
Ok(num) => {
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
|
|
Loading…
Reference in New Issue