From 480054a094d0b71fb0984e7c87f97d39c55d46e7 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Wed, 14 Sep 2016 13:14:43 -0700 Subject: [PATCH] Add a utility read_to_string() function. --- src/tools/cat.rs | 11 +++-------- src/tools/main.rs | 1 + src/tools/print_cfg.rs | 9 +++------ src/tools/rsfilecheck.rs | 8 ++------ src/tools/utils.rs | 13 +++++++++++++ 5 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 src/tools/utils.rs diff --git a/src/tools/cat.rs b/src/tools/cat.rs index a41b5b5f27..566997c403 100644 --- a/src/tools/cat.rs +++ b/src/tools/cat.rs @@ -3,11 +3,9 @@ //! Read a sequence of Cretonne IL files and print them again to stdout. This has the effect of //! normalizing formatting and removing comments. -use std::fs::File; -use std::io::{self, Read}; - +use std::io; use CommandResult; - +use utils::read_to_string; use cton_reader::parse_functions; use cretonne::write::write_function; @@ -22,10 +20,7 @@ pub fn run(files: Vec) -> CommandResult { } fn cat_one(filename: String) -> CommandResult { - let mut file = try!(File::open(&filename).map_err(|e| format!("{}: {}", filename, e))); - let mut buffer = String::new(); - try!(file.read_to_string(&mut buffer) - .map_err(|e| format!("Couldn't read {}: {}", filename, e))); + let buffer = try!(read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e))); let items = try!(parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e))); for (idx, func) in items.into_iter().enumerate() { diff --git a/src/tools/main.rs b/src/tools/main.rs index 5abfa89462..af7a092394 100644 --- a/src/tools/main.rs +++ b/src/tools/main.rs @@ -10,6 +10,7 @@ use docopt::Docopt; use std::io::{self, Write}; use std::process; +mod utils; mod filetest; mod cat; mod print_cfg; diff --git a/src/tools/print_cfg.rs b/src/tools/print_cfg.rs index 649d87fb9f..1c57710b29 100644 --- a/src/tools/print_cfg.rs +++ b/src/tools/print_cfg.rs @@ -2,10 +2,10 @@ //! //! Read a series of Cretonne IL files and print their control flow graphs //! in graphviz format. -use std::fs::File; -use std::io::{Read, Write, stdout}; +use std::io::{Write, stdout}; use CommandResult; +use utils::read_to_string; use cretonne::ir::Function; use cretonne::cfg::ControlFlowGraph; use cretonne::ir::instructions::InstructionData; @@ -156,10 +156,7 @@ impl CFGPrinter { } fn print_cfg(filename: String) -> CommandResult { - let mut file = try!(File::open(&filename).map_err(|e| format!("{}: {}", filename, e))); - let mut buffer = String::new(); - try!(file.read_to_string(&mut buffer) - .map_err(|e| format!("Couldn't read {}: {}", filename, e))); + let buffer = try!(read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e))); let items = try!(parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e))); let mut cfg_printer = CFGPrinter::new(stdout()); diff --git a/src/tools/rsfilecheck.rs b/src/tools/rsfilecheck.rs index b64a86283e..5ff6c596d1 100644 --- a/src/tools/rsfilecheck.rs +++ b/src/tools/rsfilecheck.rs @@ -1,6 +1,6 @@ use CommandResult; +use utils::read_to_string; use filecheck::{CheckerBuilder, Checker, NO_VARIABLES}; -use std::fs::File; use std::io::{self, Read}; pub fn run(files: Vec, verbose: bool) -> CommandResult { @@ -40,11 +40,7 @@ pub fn run(files: Vec, verbose: bool) -> CommandResult { } fn read_checkfile(filename: &str) -> Result { - let mut file = try!(File::open(&filename).map_err(|e| format!("{}: {}", filename, e))); - let mut buffer = String::new(); - try!(file.read_to_string(&mut buffer) - .map_err(|e| format!("Couldn't read {}: {}", filename, e))); - + let buffer = try!(read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e))); let mut builder = CheckerBuilder::new(); try!(builder.text(&buffer).map_err(|e| format!("{}: {}", filename, e))); Ok(builder.finish()) diff --git a/src/tools/utils.rs b/src/tools/utils.rs new file mode 100644 index 0000000000..5973f25814 --- /dev/null +++ b/src/tools/utils.rs @@ -0,0 +1,13 @@ +//! Utility functions. + +use std::path::Path; +use std::fs::File; +use std::io::{Result, Read}; + +/// Read an entire file into a string. +pub fn read_to_string>(path: P) -> Result { + let mut file = try!(File::open(path)); + let mut buffer = String::new(); + try!(file.read_to_string(&mut buffer)); + Ok(buffer) +}