mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +00:00
Upgrade base64 to use recent Rust nightly build.
This commit is contained in:
parent
424923da94
commit
92bbd7b2ce
1 changed files with 27 additions and 28 deletions
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "base64"]
|
#![crate_name = "base64"]
|
||||||
#![feature(collections, core, old_io, old_path, rustc_private)]
|
#![feature(box_syntax, collections, rustc_private)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -17,8 +17,9 @@ extern crate libc;
|
||||||
|
|
||||||
use std::ascii::AsciiExt;
|
use std::ascii::AsciiExt;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::old_io::{println, File, stdout};
|
use std::fs::File;
|
||||||
use std::old_io::stdio::stdin_raw;
|
use std::io::{BufReader, Read, stdin, stdout, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use getopts::{
|
use getopts::{
|
||||||
getopts,
|
getopts,
|
||||||
|
@ -35,6 +36,8 @@ mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "base64";
|
static NAME: &'static str = "base64";
|
||||||
|
|
||||||
|
pub type FileOrStdReader = BufReader<Box<Read+'static>>;
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let opts = [
|
let opts = [
|
||||||
optflag("d", "decode", "decode data"),
|
optflag("d", "decode", "decode data"),
|
||||||
|
@ -75,30 +78,28 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
};
|
};
|
||||||
let mut stdin_buf;
|
let mut stdin_buf;
|
||||||
let mut file_buf;
|
let mut file_buf;
|
||||||
let input = if matches.free.is_empty() || matches.free[0].as_slice() == "-" {
|
let mut input = if matches.free.is_empty() || &matches.free[0][..] == "-" {
|
||||||
stdin_buf = stdin_raw();
|
stdin_buf = stdin();
|
||||||
&mut stdin_buf as &mut Reader
|
BufReader::new(box stdin_buf as Box<Read+'static>)
|
||||||
} else {
|
} else {
|
||||||
let path = Path::new(matches.free[0].as_slice());
|
let path = Path::new(&matches.free[0][..]);
|
||||||
file_buf = File::open(&path);
|
file_buf = safe_unwrap!(File::open(&path));
|
||||||
&mut file_buf as &mut Reader
|
BufReader::new(box file_buf as Box<Read+'static>)
|
||||||
};
|
};
|
||||||
|
|
||||||
match mode {
|
match mode {
|
||||||
Mode::Decode => decode(input, ignore_garbage),
|
Mode::Decode => decode(&mut input, ignore_garbage),
|
||||||
Mode::Encode => encode(input, line_wrap),
|
Mode::Encode => encode(&mut input, line_wrap),
|
||||||
Mode::Help => help(progname.as_slice(), usage.as_slice()),
|
Mode::Help => help(&progname[..], &usage[..]),
|
||||||
Mode::Version => version()
|
Mode::Version => version()
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode(input: &mut Reader, ignore_garbage: bool) {
|
fn decode(input: &mut FileOrStdReader, ignore_garbage: bool) {
|
||||||
let mut to_decode = match input.read_to_string() {
|
let mut to_decode = String::new();
|
||||||
Ok(m) => m,
|
input.read_to_string(&mut to_decode).unwrap();
|
||||||
Err(f) => panic!(f)
|
|
||||||
};
|
|
||||||
|
|
||||||
if ignore_garbage {
|
if ignore_garbage {
|
||||||
let mut clean = String::new();
|
let mut clean = String::new();
|
||||||
|
@ -115,11 +116,11 @@ fn decode(input: &mut Reader, ignore_garbage: bool) {
|
||||||
to_decode = clean;
|
to_decode = clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
match to_decode.as_slice().from_base64() {
|
match to_decode[..].from_base64() {
|
||||||
Ok(bytes) => {
|
Ok(bytes) => {
|
||||||
let mut out = stdout();
|
let mut out = stdout();
|
||||||
|
|
||||||
match out.write_all(bytes.as_slice()) {
|
match out.write_all(&bytes[..]) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(f) => { crash!(1, "{}", f); }
|
Err(f) => { crash!(1, "{}", f); }
|
||||||
}
|
}
|
||||||
|
@ -134,7 +135,7 @@ fn decode(input: &mut Reader, ignore_garbage: bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(input: &mut Reader, line_wrap: usize) {
|
fn encode(input: &mut FileOrStdReader, line_wrap: usize) {
|
||||||
let b64_conf = base64::Config {
|
let b64_conf = base64::Config {
|
||||||
char_set: base64::Standard,
|
char_set: base64::Standard,
|
||||||
newline: base64::Newline::LF,
|
newline: base64::Newline::LF,
|
||||||
|
@ -144,19 +145,17 @@ fn encode(input: &mut Reader, line_wrap: usize) {
|
||||||
_ => Some(line_wrap)
|
_ => Some(line_wrap)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let to_encode = match input.read_to_end() {
|
let mut to_encode: Vec<u8> = vec!();
|
||||||
Ok(m) => m,
|
input.read_to_end(&mut to_encode).unwrap();
|
||||||
Err(err) => crash!(1, "{}", err)
|
let encoded = to_encode.to_base64(b64_conf);
|
||||||
};
|
|
||||||
let encoded = to_encode.as_slice().to_base64(b64_conf);
|
|
||||||
|
|
||||||
println(encoded.as_slice());
|
println!("{}", &encoded[..]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn help(progname: &str, usage: &str) {
|
fn help(progname: &str, usage: &str) {
|
||||||
println!("Usage: {} [OPTION]... [FILE]", progname);
|
println!("Usage: {} [OPTION]... [FILE]", progname);
|
||||||
println!("");
|
println!("");
|
||||||
println(usage);
|
println!("{}", usage);
|
||||||
|
|
||||||
let msg = "With no FILE, or when FILE is -, read standard input.\n\n\
|
let msg = "With no FILE, or when FILE is -, read standard input.\n\n\
|
||||||
The data are encoded as described for the base64 alphabet in RFC \
|
The data are encoded as described for the base64 alphabet in RFC \
|
||||||
|
@ -165,7 +164,7 @@ fn help(progname: &str, usage: &str) {
|
||||||
to attempt to recover from any other\nnon-alphabet bytes in the \
|
to attempt to recover from any other\nnon-alphabet bytes in the \
|
||||||
encoded stream.";
|
encoded stream.";
|
||||||
|
|
||||||
println(msg);
|
println!("{}", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn version() {
|
fn version() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue