diff --git a/src/util_io.rs b/src/util_io.rs index 9cc55d0..6a9b93a 100644 --- a/src/util_io.rs +++ b/src/util_io.rs @@ -1,5 +1,6 @@ use std::{ + fs::File, io::{self, ErrorKind, prelude::*, @@ -7,13 +8,24 @@ use std::{ time::{SystemTime, Duration}, }; -use super::XResult; +use super::{ XResult, new_box_ioerror, }; use super::util_size::get_display_size; use super::util_msg::print_lastline; pub const DEFAULT_BUF_SIZE: usize = 8 * 1024; +pub fn get_read_stdin_or_file(file: &str) -> XResult> { + if file.is_empty() { + Ok(Box::new(io::stdin())) + } else { + match File::open(file) { + Ok(f) => Ok(Box::new(f)), + Err(err) => Err(new_box_ioerror(&format!("Open file {}, erorr: {}", file, err))), + } + } +} + pub fn read_to_string(read: &mut dyn Read) -> XResult { let mut buffer = String::new(); read.read_to_string(&mut buffer)?;