diff --git a/plib/src/io.rs b/plib/src/io.rs index 672dd2ec..17199a0b 100644 --- a/plib/src/io.rs +++ b/plib/src/io.rs @@ -26,6 +26,13 @@ pub fn input_stream(pathname: &PathBuf, dashed_stdin: bool) -> io::Result) -> io::Result> { + match pathname { + Some(path) => input_stream(&path, false), + None => input_stream(&PathBuf::new(), false), + } +} + pub fn input_reader( pathname: &PathBuf, dashed_stdin: bool, diff --git a/text/src/asa.rs b/text/src/asa.rs index d556cf00..23194ecd 100644 --- a/text/src/asa.rs +++ b/text/src/asa.rs @@ -17,15 +17,15 @@ extern crate plib; use clap::Parser; use gettextrs::{bind_textdomain_codeset, gettext, textdomain}; use plib::PROJECT_NAME; -use std::fs; -use std::io::{self, BufRead, Read}; +use std::io::{self, BufRead}; +use std::path::PathBuf; /// asa - interpret carriage-control characters #[derive(Parser, Debug)] #[command(author, version, about, long_about)] struct Args { /// Files to read as input. - files: Vec, + files: Vec, } struct AsaState { @@ -69,14 +69,8 @@ impl AsaState { } } -fn asa_file(filename: &str) -> io::Result<()> { - let file: Box; - if filename == "" { - file = Box::new(io::stdin().lock()); - } else { - file = Box::new(fs::File::open(filename)?); - } - let mut reader = io::BufReader::new(file); +fn asa_file(pathname: &PathBuf) -> io::Result<()> { + let mut reader = plib::io::input_reader(pathname, false)?; let mut line_no: usize = 0; let mut state = AsaState::new(); @@ -142,7 +136,7 @@ fn main() -> Result<(), Box> { // if no files, read from stdin if args.files.is_empty() { - args.files.push(String::new()); + args.files.push(PathBuf::new()); } let mut exit_code = 0; @@ -150,7 +144,7 @@ fn main() -> Result<(), Box> { for filename in &args.files { if let Err(e) = asa_file(filename) { exit_code = 1; - eprintln!("{}: {}", filename, e); + eprintln!("{}: {}", filename.display(), e); } } diff --git a/text/src/expand.rs b/text/src/expand.rs index 83b6ca5d..53d2618a 100644 --- a/text/src/expand.rs +++ b/text/src/expand.rs @@ -13,8 +13,8 @@ extern crate plib; use clap::Parser; use gettextrs::{bind_textdomain_codeset, textdomain}; use plib::PROJECT_NAME; -use std::fs; use std::io::{self, BufWriter, Read, Write}; +use std::path::PathBuf; /// expand - convert tabs to spaces #[derive(Parser, Debug)] @@ -25,7 +25,7 @@ struct Args { tablist: Option, /// Files to read as input. - files: Vec, + files: Vec, } enum TabList { @@ -67,14 +67,9 @@ fn space_out(column: &mut usize, writer: &mut BufWriter) -> io::Resul Ok(()) } -fn expand_file(tablist: &TabList, filename: &str) -> io::Result<()> { +fn expand_file(tablist: &TabList, pathname: &PathBuf) -> io::Result<()> { // open file, or stdin - let mut file: Box; - if filename == "" { - file = Box::new(io::stdin().lock()); - } else { - file = Box::new(fs::File::open(filename)?); - } + let mut file = plib::io::input_stream(pathname, false)?; let mut raw_buffer = [0; plib::BUFSZ]; let mut writer = BufWriter::new(io::stdout()); @@ -160,7 +155,7 @@ fn main() -> Result<(), Box> { // if no files, read from stdin if args.files.is_empty() { - args.files.push(String::new()); + args.files.push(PathBuf::new()); } let mut exit_code = 0; @@ -168,7 +163,7 @@ fn main() -> Result<(), Box> { for filename in &args.files { if let Err(e) = expand_file(&tablist, filename) { exit_code = 1; - eprintln!("{}: {}", filename, e); + eprintln!("{}: {}", filename.display(), e); } } diff --git a/text/src/fold.rs b/text/src/fold.rs index a77e5551..20da046a 100644 --- a/text/src/fold.rs +++ b/text/src/fold.rs @@ -17,8 +17,8 @@ extern crate plib; use clap::Parser; use gettextrs::{bind_textdomain_codeset, textdomain}; use plib::PROJECT_NAME; -use std::fs; use std::io::{self, Read, Write}; +use std::path::PathBuf; const TABSTOP: usize = 8; @@ -39,7 +39,7 @@ struct Args { width: u64, /// Files to read as input. - files: Vec, + files: Vec, } struct OutputState { @@ -105,14 +105,9 @@ fn find_last_blank(v: &Vec) -> Option { return None; } -fn fold_file(args: &Args, filename: &str) -> io::Result<()> { +fn fold_file(args: &Args, pathname: &PathBuf) -> io::Result<()> { // open file, or stdin - let mut file: Box; - if filename == "" { - file = Box::new(io::stdin().lock()); - } else { - file = Box::new(fs::File::open(filename)?); - } + let mut file = plib::io::input_stream(pathname, false)?; let mut raw_buffer = [0; plib::BUFSZ]; let mut state = OutputState::new(args); @@ -185,7 +180,7 @@ fn main() -> Result<(), Box> { // if no files, read from stdin if args.files.is_empty() { - args.files.push(String::new()); + args.files.push(PathBuf::new()); } let mut exit_code = 0; @@ -193,7 +188,7 @@ fn main() -> Result<(), Box> { for filename in &args.files { if let Err(e) = fold_file(&args, filename) { exit_code = 1; - eprintln!("{}: {}", filename, e); + eprintln!("{}: {}", filename.display(), e); } } diff --git a/text/src/tsort.rs b/text/src/tsort.rs index ddd00601..9b740e2a 100644 --- a/text/src/tsort.rs +++ b/text/src/tsort.rs @@ -13,8 +13,8 @@ extern crate plib; use clap::Parser; use gettextrs::{bind_textdomain_codeset, textdomain}; use plib::PROJECT_NAME; -use std::fs; -use std::io::{self, BufRead, Read}; +use std::io::{self, BufRead}; +use std::path::PathBuf; use topological_sort::TopologicalSort; /// tsort - topological sort @@ -22,18 +22,13 @@ use topological_sort::TopologicalSort; #[command(author, version, about, long_about)] struct Args { /// File to read as input. - file: Option, + file: Option, } -fn tsort_file(filename: &str) -> io::Result<()> { - let file: Box; - if filename == "" { - file = Box::new(io::stdin().lock()); - } else { - file = Box::new(fs::File::open(filename)?); - } - +fn tsort_file(pathname: &Option) -> io::Result<()> { + let file = plib::io::input_stream_opt(pathname)?; let mut reader = io::BufReader::new(file); + let mut ts = TopologicalSort::::new(); let mut sv: Vec = Vec::new(); @@ -65,6 +60,13 @@ fn tsort_file(filename: &str) -> io::Result<()> { Ok(()) } +fn pathname_display(path: &Option) -> String { + match path { + None => String::from("stdin"), + Some(p) => p.display().to_string(), + } +} + fn main() -> Result<(), Box> { // parse command line arguments let args = Args::parse(); @@ -74,14 +76,9 @@ fn main() -> Result<(), Box> { let mut exit_code = 0; - let filename = match &args.file { - None => String::new(), - Some(name) => String::from(name), - }; - - if let Err(e) = tsort_file(&filename) { + if let Err(e) = tsort_file(&args.file) { exit_code = 1; - eprintln!("{}: {}", filename, e); + eprintln!("{}: {}", pathname_display(&args.file), e); } std::process::exit(exit_code) diff --git a/xform/src/cksum.rs b/xform/src/cksum.rs index ec6eb268..69f2e612 100644 --- a/xform/src/cksum.rs +++ b/xform/src/cksum.rs @@ -23,24 +23,19 @@ mod crc32; use clap::Parser; use gettextrs::{bind_textdomain_codeset, textdomain}; use plib::PROJECT_NAME; -use std::fs; use std::io::{self, Read}; +use std::path::PathBuf; /// cksum - write file checksums and sizes #[derive(Parser, Debug)] #[command(author, version, about, long_about)] struct Args { /// Files to read as input. Use "-" or no-args for stdin. - files: Vec, + files: Vec, } -fn cksum_file(filename: &str) -> io::Result<()> { - let mut file: Box; - if filename == "" { - file = Box::new(io::stdin().lock()); - } else { - file = Box::new(fs::File::open(filename)?); - } +fn cksum_file(filename: &PathBuf) -> io::Result<()> { + let mut file = plib::io::input_stream(filename, false)?; let mut buffer = [0; plib::BUFSZ]; let mut n_bytes: u64 = 0; @@ -57,7 +52,7 @@ fn cksum_file(filename: &str) -> io::Result<()> { } let filename_prefix = { - if filename == "" { + if filename.as_os_str() == "" { "" } else { " " @@ -68,7 +63,7 @@ fn cksum_file(filename: &str) -> io::Result<()> { crc32::finalize(crc, n_bytes as usize), n_bytes, filename_prefix, - filename + filename.display() ); Ok(()) @@ -83,7 +78,7 @@ fn main() -> Result<(), Box> { // if no file args, read from stdin if args.files.is_empty() { - args.files.push(String::new()); + args.files.push(PathBuf::new()); } let mut exit_code = 0; @@ -91,7 +86,7 @@ fn main() -> Result<(), Box> { for filename in &args.files { if let Err(e) = cksum_file(filename) { exit_code = 1; - eprintln!("{}: {}", filename, e); + eprintln!("{}: {}", filename.display(), e); } } diff --git a/xform/src/uncompress.rs b/xform/src/uncompress.rs index 9b683ca3..59afdaac 100644 --- a/xform/src/uncompress.rs +++ b/xform/src/uncompress.rs @@ -21,8 +21,8 @@ use clap::Parser; use gettextrs::{bind_textdomain_codeset, textdomain}; use lzw::UnixLZWReader; use plib::PROJECT_NAME; -use std::fs; -use std::io::{self, Read, Write}; +use std::io::{self, Write}; +use std::path::PathBuf; /// uncompress - expand compressed data #[derive(Parser, Debug)] @@ -41,17 +41,11 @@ struct Args { verbose: bool, /// Files to read as input. Use "-" or no-args for stdin. - files: Vec, + files: Vec, } -fn uncompress_file(filename: &str) -> io::Result<()> { - let file: Box; - if filename == "" { - file = Box::new(io::stdin().lock()); - } else { - file = Box::new(fs::File::open(filename)?); - } - +fn uncompress_file(pathname: &PathBuf) -> io::Result<()> { + let file = plib::io::input_stream(pathname, false)?; let mut decoder = UnixLZWReader::new(file); loop { @@ -80,7 +74,7 @@ fn main() -> Result<(), Box> { // if no file args, read from stdin if args.files.is_empty() { - args.files.push(String::new()); + args.files.push(PathBuf::new()); } // zcat is a special case: always write to stdout @@ -93,7 +87,7 @@ fn main() -> Result<(), Box> { for filename in &args.files { if let Err(e) = uncompress_file(filename) { exit_code = 1; - eprintln!("{}: {}", filename, e); + eprintln!("{}: {}", filename.display(), e); } } diff --git a/xform/src/uuencode.rs b/xform/src/uuencode.rs index db461792..6816cc05 100644 --- a/xform/src/uuencode.rs +++ b/xform/src/uuencode.rs @@ -15,8 +15,8 @@ use base64::prelude::*; use clap::Parser; use gettextrs::{bind_textdomain_codeset, textdomain}; use plib::PROJECT_NAME; -use std::fs; use std::io::{self, Read, Write}; +use std::path::PathBuf; /// uuencode - encode a binary file #[derive(Parser, Debug)] @@ -27,39 +27,30 @@ struct Args { base64: bool, /// File to read as input. - file: Option, + file: Option, /// Decode pathname decode_path: Option, } fn encode_file(args: &Args) -> io::Result<()> { - let mut file: Box; - if let Some(filename) = &args.file { - file = Box::new(fs::File::open(filename)?); - } else { - file = Box::new(io::stdin().lock()); - } + let mut file = plib::io::input_stream_opt(&args.file)?; let mut buffer = Vec::new(); file.read_to_end(&mut buffer)?; - let decode_path; - match &args.decode_path { - None => { - decode_path = String::from("/dev/stdout"); - } - Some(path) => { - decode_path = String::from(path); - } - } + let decode_path = match &args.decode_path { + None => String::from("/dev/stdout"), + Some(path) => String::from(path), + }; - let output; - if args.base64 { - output = BASE64_STANDARD.encode(&buffer[..]); - } else { - output = uuencode::uuencode(&decode_path, &buffer[..]); - } + let output = { + if args.base64 { + BASE64_STANDARD.encode(&buffer[..]) + } else { + uuencode::uuencode(&decode_path, &buffer[..]) + } + }; io::stdout().write_all(output.as_bytes())?; io::stdout().write_all(b"\n")?; @@ -67,6 +58,13 @@ fn encode_file(args: &Args) -> io::Result<()> { Ok(()) } +fn pathname_display(path: &Option) -> String { + match path { + None => String::from("stdin"), + Some(p) => p.display().to_string(), + } +} + fn main() -> Result<(), Box> { // parse command line arguments let args = Args::parse(); @@ -78,7 +76,7 @@ fn main() -> Result<(), Box> { if let Err(e) = encode_file(&args) { exit_code = 1; - eprintln!("{:?}: {}", args.file, e); + eprintln!("{:?}: {}", pathname_display(&args.file), e); } std::process::exit(exit_code)