Use buffered read and write for better performance

This commit is contained in:
jaby 2022-09-28 21:21:15 +02:00
parent 6d86f4e99f
commit 6237132fe2
2 changed files with 16 additions and 16 deletions

View File

@ -32,21 +32,21 @@ const WINDOWS_LINEFEED: &'static str = "\r\n";
const C_DECLARATIONS: FileDeclarations = FileDeclarations{include: "", var_type: "char"};
const CPP_DECLARATIONS: FileDeclarations = FileDeclarations{include: "#include <stdint.h>", var_type: "uint8_t"};
fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<(), std::io::Error> {
let mut byte_line_count = 1;
let mut bytes = input.bytes();
fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<(), std::io::Error> {
let mut byte_line_count = 0;
if let Some(byte) = bytes.next() {
write!(output, "\t{:#04X}", byte?)?;
write!(output, "\t")?;
for byte in input.bytes() {
if byte_line_count > 0 {
write!(output, ", ")?;
}
for byte in bytes {
write!(output, ", {:#04X}", byte?)?;
byte_line_count += 1;
write!(output, "{:#04X}", byte?)?;
byte_line_count += 1;
if byte_line_count >= 16 {
write!(output, "{}\t", line_feed)?;
byte_line_count = 0;
}
if byte_line_count >= 16 {
write!(output, ",{}\t", line_feed)?;
byte_line_count = 0;
}
}

View File

@ -76,15 +76,15 @@ impl std::convert::From<std::io::Error> for Error {
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output, Error> {
match output_file {
Some(output_path) => Ok(Box::new(std::fs::File::create(output_path)?)),
None => Ok(Box::new(std::io::stdout())),
Some(output_path) => Ok(Box::new(std::io::BufWriter::new(std::fs::File::create(output_path)?))),
None => Ok(Box::new(std::io::BufWriter::new(std::io::stdout()))),
}
}
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> {
match input_file {
Some(input_path) => Ok(Box::new(std::fs::File::open(input_path)?)),
None => Ok(Box::new(std::io::stdin())),
Some(input_path) => Ok(Box::new(std::io::BufReader::new(std::fs::File::open(input_path)?))),
None => Ok(Box::new(std::io::BufReader::new(std::io::stdin()))),
}
}