Skip to content

Commit

Permalink
use cross platform io
Browse files Browse the repository at this point in the history
  • Loading branch information
sakateka committed Mar 22, 2018
1 parent 6bb0ec5 commit b560594
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions src/dfa.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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<Read>, show_path: bool) -> io::Result<()> {
let buf = BufReader::new(input);
for line in buf.lines() {
self.check_string(line?, show_path);
}
Ok(())
Expand Down
45 changes: 36 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Write>)
}
None => BufWriter::new(Box::new(io::stdout()) as Box<Write>),
};
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::<HashSet<Vec<Symbol>>>()));
output_stream
.write_fmt(format_args!(
"{}",
GeneratedSet(gen.collect::<HashSet<Vec<Symbol>>>())
))
.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<Write>
}
None => Box::new(io::stdout()) as Box<Write>,
};

output_stream
.write_fmt(format_args!("{}", cfg.simplify()))
.unwrap();
Expand Down Expand Up @@ -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<Read>
}
None => Box::new(io::stdin()) as Box<Read>,
};
dfa.check(input, show_path).unwrap();
}
}

0 comments on commit b560594

Please sign in to comment.