1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-29 16:40:05 +08:00

add get_read_stdin_or_file

This commit is contained in:
2020-05-02 11:54:13 +08:00
parent d75c331fa0
commit dd0a754631

View File

@@ -1,5 +1,6 @@
use std::{ use std::{
fs::File,
io::{self, io::{self,
ErrorKind, ErrorKind,
prelude::*, prelude::*,
@@ -7,13 +8,24 @@ use std::{
time::{SystemTime, Duration}, time::{SystemTime, Duration},
}; };
use super::XResult; use super::{ XResult, new_box_ioerror, };
use super::util_size::get_display_size; use super::util_size::get_display_size;
use super::util_msg::print_lastline; use super::util_msg::print_lastline;
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024; pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
pub fn get_read_stdin_or_file(file: &str) -> XResult<Box<dyn Read>> {
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<String> { pub fn read_to_string(read: &mut dyn Read) -> XResult<String> {
let mut buffer = String::new(); let mut buffer = String::new();
read.read_to_string(&mut buffer)?; read.read_to_string(&mut buffer)?;