From b560594a1ca9f66f40ed087fbda952e0383b911f Mon Sep 17 00:00:00 2001 From: Sergey Kacheev Date: Fri, 23 Mar 2018 06:25:11 +0700 Subject: [PATCH] use cross platform io --- src/args.rs | 2 +- src/dfa.rs | 8 ++++---- src/main.rs | 45 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/args.rs b/src/args.rs index 4c55317..0fd04ca 100644 --- a/src/args.rs +++ b/src/args.rs @@ -85,7 +85,7 @@ pub fn build_app<'a>(name: &str) -> ArgMatches<'a> { .arg( Arg::with_name("INPUT") .required(false) - .help("Input stream (default: /dev/stdin)") + .help("Input stream (default: stdin)") .index(2), ) .arg( diff --git a/src/dfa.rs b/src/dfa.rs index 52b22fc..fe182cf 100644 --- a/src/dfa.rs +++ b/src/dfa.rs @@ -1,6 +1,6 @@ use std::fmt; use std::fs::File; -use std::io::{self, BufRead, BufReader}; +use std::io::{self, BufRead, BufReader, Read}; use std::collections::{HashMap, HashSet}; use std::hash::{Hash, Hasher}; @@ -310,9 +310,9 @@ impl DFA { msg == "OK" } - pub fn check(&self, input: &str, show_path: bool) -> io::Result<()> { - let file = BufReader::new(File::open(input)?); - for line in file.lines() { + pub fn check(&self, input: Box, show_path: bool) -> io::Result<()> { + let buf = BufReader::new(input); + for line in buf.lines() { self.check_string(line?, show_path); } Ok(()) diff --git a/src/main.rs b/src/main.rs index 989cd1f..bdafd25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,9 @@ mod generator; mod dfa; use std::collections::HashSet; +use std::path::Path; use std::fs::File; -use std::io::Write; +use std::io::{self, Read, Write, BufWriter}; use generator::{GeneratedItem, GeneratedSet, Generator}; use cfg::{Symbol, CFG}; use dfa::DFA; @@ -30,22 +31,42 @@ fn main() { } let left = !matches.is_present("right"); let gen = Generator::new(cfg, min, max, left); + let mut output_stream = match matches.value_of("OUT") { + Some(x) => { + let path = Path::new(x); + BufWriter::new(Box::new(File::create(&path).unwrap()) as Box) + } + None => BufWriter::new(Box::new(io::stdout()) as Box), + }; if matches.is_present("all") { for seq in gen { - print!("{}\n", GeneratedItem(&seq)); + output_stream + .write_fmt(format_args!("{}\n", GeneratedItem(&seq))) + .unwrap(); } } else { - print!("{}", GeneratedSet(gen.collect::>>())); + output_stream + .write_fmt(format_args!( + "{}", + GeneratedSet(gen.collect::>>()) + )) + .unwrap(); } } else if let Some(matches) = app.subcommand_matches("simplify") { let grammar = matches.value_of("CFG").unwrap(); let mut cfg = CFG::parse(grammar).unwrap(); - let output = matches.value_of("OUT").unwrap_or_else(|| "/dev/stdout"); - let mut output_stream = File::create(output).unwrap(); - if matches.is_present("verbose") { - eprintln!("Write simplified grammar to {}", output); - } + let mut output_stream = match matches.value_of("OUT") { + Some(x) => { + if matches.is_present("verbose") { + eprintln!("Write simplified grammar to {:?}", x); + } + let path = Path::new(x); + Box::new(File::create(&path).unwrap()) as Box + } + None => Box::new(io::stdout()) as Box, + }; + output_stream .write_fmt(format_args!("{}", cfg.simplify())) .unwrap(); @@ -77,7 +98,13 @@ fn main() { let debug = matches.is_present("debug"); let show_path = matches.is_present("path"); let mut dfa = DFA::parse(dfa_table, debug).unwrap(); - let input = matches.value_of("INPUT").unwrap_or_else(|| "/dev/stdin"); + let mut input = match matches.value_of("INPUT") { + Some(x) => { + let path = Path::new(x); + Box::new(File::open(&path).unwrap()) as Box + } + None => Box::new(io::stdin()) as Box, + }; dfa.check(input, show_path).unwrap(); } }