From f49395b1e8df37dcfe690f1066b91a3670c0d12f Mon Sep 17 00:00:00 2001 From: Alex Lyon Date: Thu, 21 Feb 2019 05:10:12 -0800 Subject: [PATCH] base32, base64, uucore: merge base32/base64 code --- src/base32/base32.rs | 60 ++------------------------ src/base64/base64.rs | 59 ++----------------------- src/base64/base_common.rs | 91 +++++++++++++++++++++++++++++++++++++++ src/uucore/encoding.rs | 4 +- src/uucore/macros.rs | 1 + 5 files changed, 101 insertions(+), 114 deletions(-) create mode 100644 src/base64/base_common.rs diff --git a/src/base32/base32.rs b/src/base32/base32.rs index e87d982c5..548784298 100644 --- a/src/base32/base32.rs +++ b/src/base32/base32.rs @@ -10,11 +10,10 @@ #[macro_use] extern crate uucore; -use uucore::encoding::{wrap_print, Data, Format}; +use uucore::encoding::Format; -use std::fs::File; -use std::io::{stdin, BufReader, Read}; -use std::path::Path; +#[path = "../base64/base_common.rs"] +mod base_common; static SYNTAX: &str = "[OPTION]... [FILE]"; static SUMMARY: &str = @@ -30,56 +29,5 @@ static LONG_HELP: &str = " "; pub fn uumain(args: Vec) -> i32 { - let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP) - .optflag("d", "decode", "decode data") - .optflag( - "i", - "ignore-garbage", - "when decoding, ignore non-alphabetic characters", - ) - .optopt( - "w", - "wrap", - "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", - "COLS", - ) - .parse(args); - - let line_wrap = match matches.opt_str("wrap") { - Some(s) => match s.parse() { - Ok(n) => n, - Err(e) => { - crash!(1, "invalid wrap size: ‘{}’: {}", s, e); - } - }, - None => 76, - }; - - if matches.free.len() > 1 { - disp_err!("extra operand ‘{}’", matches.free[0]); - return 1; - } - - let input = if matches.free.is_empty() || &matches.free[0][..] == "-" { - BufReader::new(Box::new(stdin()) as Box) - } else { - let path = Path::new(matches.free[0].as_str()); - let file_buf = safe_unwrap!(File::open(&path)); - BufReader::new(Box::new(file_buf) as Box) - }; - - let mut data = Data::new(input, Format::Base32) - .line_wrap(line_wrap) - .ignore_garbage(matches.opt_present("ignore-garbage")); - - if !matches.opt_present("decode") { - wrap_print(line_wrap, data.encode()); - } else { - match data.decode() { - Ok(s) => print!("{}", String::from_utf8(s).unwrap()), - Err(_) => crash!(1, "invalid input"), - } - } - - 0 + base_common::execute(args, SYNTAX, SUMMARY, LONG_HELP, Format::Base32) } diff --git a/src/base64/base64.rs b/src/base64/base64.rs index 68c517e22..9072fecb0 100644 --- a/src/base64/base64.rs +++ b/src/base64/base64.rs @@ -11,11 +11,9 @@ #[macro_use] extern crate uucore; -use uucore::encoding::{wrap_print, Data, Format}; +use uucore::encoding::Format; -use std::fs::File; -use std::io::{stdin, BufReader, Read}; -use std::path::Path; +mod base_common; static SYNTAX: &str = "[OPTION]... [FILE]"; static SUMMARY: &str = @@ -31,56 +29,5 @@ static LONG_HELP: &str = " "; pub fn uumain(args: Vec) -> i32 { - let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP) - .optflag("d", "decode", "decode data") - .optflag( - "i", - "ignore-garbage", - "when decoding, ignore non-alphabetic characters", - ) - .optopt( - "w", - "wrap", - "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", - "COLS", - ) - .parse(args); - - let line_wrap = match matches.opt_str("wrap") { - Some(s) => match s.parse() { - Ok(n) => n, - Err(e) => { - crash!(1, "invalid wrap size: ‘{}’: {}", s, e); - } - }, - None => 76, - }; - - if matches.free.len() > 1 { - disp_err!("extra operand ‘{}’", matches.free[0]); - return 1; - } - - let input = if matches.free.is_empty() || &matches.free[0][..] == "-" { - BufReader::new(Box::new(stdin()) as Box) - } else { - let path = Path::new(matches.free[0].as_str()); - let file_buf = safe_unwrap!(File::open(&path)); - BufReader::new(Box::new(file_buf) as Box) - }; - - let mut data = Data::new(input, Format::Base64) - .line_wrap(line_wrap) - .ignore_garbage(matches.opt_present("ignore-garbage")); - - if !matches.opt_present("decode") { - wrap_print(line_wrap, data.encode()); - } else { - match data.decode() { - Ok(s) => print!("{}", String::from_utf8(s).unwrap()), - Err(_) => crash!(1, "invalid input"), - } - } - - 0 + base_common::execute(args, SYNTAX, SUMMARY, LONG_HELP, Format::Base64) } diff --git a/src/base64/base_common.rs b/src/base64/base_common.rs new file mode 100644 index 000000000..040b91608 --- /dev/null +++ b/src/base64/base_common.rs @@ -0,0 +1,91 @@ +// This file is part of the uutils coreutils package. +// +// (c) Jordy Dickinson +// (c) Jian Zeng +// (c) Alex Lyon +// +// For the full copyright and license information, please view the LICENSE file +// that was distributed with this source code. +// + +use uucore; +use uucore::encoding::{wrap_print, Data, Format}; + +use std::fs::File; +use std::io::{stdin, BufReader, Read}; +use std::path::Path; + +pub fn execute( + args: Vec, + syntax: &str, + summary: &str, + long_help: &str, + format: Format, +) -> i32 { + let matches = new_coreopts!(syntax, summary, long_help) + .optflag("d", "decode", "decode data") + .optflag( + "i", + "ignore-garbage", + "when decoding, ignore non-alphabetic characters", + ) + .optopt( + "w", + "wrap", + "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", + "COLS", + ) + .parse(args); + + let line_wrap = matches.opt_str("wrap").map(|s| { + match s.parse() { + Ok(n) => n, + Err(e) => { + crash!(1, "invalid wrap size: ‘{}’: {}", s, e); + } + } + }); + let ignore_garbage = matches.opt_present("ignore-garbage"); + let decode = matches.opt_present("decode"); + + if matches.free.len() > 1 { + disp_err!("extra operand ‘{}’", matches.free[0]); + return 1; + } + + if matches.free.is_empty() || &matches.free[0][..] == "-" { + let stdin_raw = stdin(); + handle_input(&mut stdin_raw.lock(), format, line_wrap, ignore_garbage, decode); + } else { + let path = Path::new(matches.free[0].as_str()); + let file_buf = safe_unwrap!(File::open(&path)); + let mut input = BufReader::new(file_buf); + handle_input(&mut input, format, line_wrap, ignore_garbage, decode); + }; + + 0 +} + +fn handle_input( + input: &mut R, + format: Format, + line_wrap: Option, + ignore_garbage: bool, + decode: bool, +) { + let mut data = Data::new(input, format) + .ignore_garbage(ignore_garbage); + if let Some(wrap) = line_wrap { + data = data.line_wrap(wrap); + } + + if !decode { + let encoded = data.encode(); + wrap_print(&data, encoded); + } else { + match data.decode() { + Ok(s) => print!("{}", String::from_utf8(s).unwrap()), + Err(_) => crash!(1, "invalid input"), + } + } +} \ No newline at end of file diff --git a/src/uucore/encoding.rs b/src/uucore/encoding.rs index ca0e2eeb7..b5b233e4d 100644 --- a/src/uucore/encoding.rs +++ b/src/uucore/encoding.rs @@ -104,9 +104,9 @@ impl Data { } // NOTE: this will likely be phased out at some point -pub fn wrap_print(line_wrap: usize, res: String) { +pub fn wrap_print(data: &Data, res: String) { let stdout = io::stdout(); - wrap_write(stdout.lock(), line_wrap, res).unwrap(); + wrap_write(stdout.lock(), data.line_wrap, res).unwrap(); } pub fn wrap_write(mut writer: W, line_wrap: usize, res: String) -> io::Result<()> { diff --git a/src/uucore/macros.rs b/src/uucore/macros.rs index 03decc92e..28046f2c8 100644 --- a/src/uucore/macros.rs +++ b/src/uucore/macros.rs @@ -11,6 +11,7 @@ macro_rules! executable( () => ({ let module = module_path!(); + let module = module.split("::").next().unwrap_or(module); if &module[0..3] == "uu_" { &module[3..] } else {