mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
base32, base64, uucore: merge base32/base64 code
This commit is contained in:
parent
39b5760f8e
commit
f49395b1e8
5 changed files with 101 additions and 114 deletions
|
@ -10,11 +10,10 @@
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
use uucore::encoding::{wrap_print, Data, Format};
|
use uucore::encoding::Format;
|
||||||
|
|
||||||
use std::fs::File;
|
#[path = "../base64/base_common.rs"]
|
||||||
use std::io::{stdin, BufReader, Read};
|
mod base_common;
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
static SYNTAX: &str = "[OPTION]... [FILE]";
|
static SYNTAX: &str = "[OPTION]... [FILE]";
|
||||||
static SUMMARY: &str =
|
static SUMMARY: &str =
|
||||||
|
@ -30,56 +29,5 @@ static LONG_HELP: &str = "
|
||||||
";
|
";
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP)
|
base_common::execute(args, SYNTAX, SUMMARY, LONG_HELP, Format::Base32)
|
||||||
.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<Read>)
|
|
||||||
} 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<Read>)
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,9 @@
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
use uucore::encoding::{wrap_print, Data, Format};
|
use uucore::encoding::Format;
|
||||||
|
|
||||||
use std::fs::File;
|
mod base_common;
|
||||||
use std::io::{stdin, BufReader, Read};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
static SYNTAX: &str = "[OPTION]... [FILE]";
|
static SYNTAX: &str = "[OPTION]... [FILE]";
|
||||||
static SUMMARY: &str =
|
static SUMMARY: &str =
|
||||||
|
@ -31,56 +29,5 @@ static LONG_HELP: &str = "
|
||||||
";
|
";
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP)
|
base_common::execute(args, SYNTAX, SUMMARY, LONG_HELP, Format::Base64)
|
||||||
.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<Read>)
|
|
||||||
} 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<Read>)
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
91
src/base64/base_common.rs
Normal file
91
src/base64/base_common.rs
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
// This file is part of the uutils coreutils package.
|
||||||
|
//
|
||||||
|
// (c) Jordy Dickinson <jordy.dickinson@gmail.com>
|
||||||
|
// (c) Jian Zeng <anonymousknight96@gmail.com>
|
||||||
|
// (c) Alex Lyon <arcterus@mail.com>
|
||||||
|
//
|
||||||
|
// 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<String>,
|
||||||
|
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<R: Read>(
|
||||||
|
input: &mut R,
|
||||||
|
format: Format,
|
||||||
|
line_wrap: Option<usize>,
|
||||||
|
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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -104,9 +104,9 @@ impl<R: Read> Data<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: this will likely be phased out at some point
|
// NOTE: this will likely be phased out at some point
|
||||||
pub fn wrap_print(line_wrap: usize, res: String) {
|
pub fn wrap_print<R: Read>(data: &Data<R>, res: String) {
|
||||||
let stdout = io::stdout();
|
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<W: Write>(mut writer: W, line_wrap: usize, res: String) -> io::Result<()> {
|
pub fn wrap_write<W: Write>(mut writer: W, line_wrap: usize, res: String) -> io::Result<()> {
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
macro_rules! executable(
|
macro_rules! executable(
|
||||||
() => ({
|
() => ({
|
||||||
let module = module_path!();
|
let module = module_path!();
|
||||||
|
let module = module.split("::").next().unwrap_or(module);
|
||||||
if &module[0..3] == "uu_" {
|
if &module[0..3] == "uu_" {
|
||||||
&module[3..]
|
&module[3..]
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue