From dbd1d34ba3e45af0ba741e2c7514400bffcc842a Mon Sep 17 00:00:00 2001 From: Michal Piekarz Date: Sun, 4 Jan 2015 16:36:09 +0100 Subject: [PATCH] Added SIZE multiplier suffixes. --- src/split/split.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/split/split.rs b/src/split/split.rs index 9755f6797..7c3cbd12e 100644 --- a/src/split/split.rs +++ b/src/split/split.rs @@ -48,6 +48,8 @@ pub fn uumain(args: Vec) -> int { println!(" {0} [OPTION]... [INPUT [PREFIX]]", NAME); println!(""); io::print(getopts::usage("Output fixed-size pieces of INPUT to PREFIXaa, PREFIX ab, ...; default size is 1000, and default PREFIX is 'x'. With no INPUT, or when INPUT is -, read standard input." , &opts).as_slice()); + println!(""); + println!("SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg."); return 0; } @@ -164,13 +166,29 @@ struct ByteSplitter { impl Splitter for ByteSplitter { fn new(_: Option, settings: &Settings) -> Box { - let n = match from_str(settings.strategy_param.as_slice()) { - Some(a) => a, - _ => crash!(1, "invalid number of lines") + let mut strategy_param : Vec = settings.strategy_param.chars().collect(); + let suffix = strategy_param.pop().unwrap(); + let multiplier = match suffix { + '0'...'9' => 1u, + 'b' => 512u, + 'k' => 1024u, + 'm' => 1024u * 1024u, + _ => crash!(1, "invalid number of bytes") + }; + let n = if suffix.is_alphabetic() { + match String::from_chars(strategy_param.as_slice()).as_slice().parse::() { + Some(a) => a, + _ => crash!(1, "invalid number of bytes") + } + } else { + match settings.strategy_param.as_slice().parse::() { + Some(a) => a, + _ => crash!(1, "invalid number of bytes") + } }; box ByteSplitter { - saved_bytes_to_write: n, - bytes_to_write: n, + saved_bytes_to_write: n * multiplier, + bytes_to_write: n * multiplier, } as Box }