Skip to content

Commit

Permalink
lrlex: Allow stdin as input file
Browse files Browse the repository at this point in the history
  • Loading branch information
ratmice committed Aug 22, 2024
1 parent 67e1adc commit 06ea4d6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lrlex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use getopts::Options;
use std::{
env,
fs::File,
io::{stderr, Read, Write},
io::{stdin, stderr, Read, Write},
path::Path,
process,
str::FromStr,
Expand All @@ -26,14 +26,18 @@ fn usage(prog: &str, msg: &str) {
}

fn read_file(path: &str) -> String {
let mut s = String::new();
if path == "-" {
stdin().read_to_string(&mut s).unwrap();
return s;
}
let mut f = match File::open(path) {
Ok(r) => r,
Err(e) => {
writeln!(stderr(), "Can't open file {}: {}", path, e).ok();
process::exit(1);
}
};
let mut s = String::new();
f.read_to_string(&mut s).unwrap();
s
}
Expand Down

0 comments on commit 06ea4d6

Please sign in to comment.