mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
od: refactor uumain into smaller functions
This commit is contained in:
parent
2c24911d9c
commit
92fc286b0e
2 changed files with 154 additions and 116 deletions
|
@ -31,28 +31,9 @@ impl InputOffset {
|
|||
}
|
||||
}
|
||||
|
||||
/// set `self.radix` to the value provided by the --address-radix commandline option
|
||||
pub fn parse_radix_from_commandline(&mut self, radix_str: Option<String>) -> Result<(), &'static str> {
|
||||
match radix_str {
|
||||
None => self.radix = Radix::Octal,
|
||||
Some(s) => {
|
||||
let st = s.into_bytes();
|
||||
if st.len() != 1 {
|
||||
return Err("Radix must be one of [d, o, n, x]\n")
|
||||
} else {
|
||||
let radix: char = *(st.get(0)
|
||||
.expect("byte string of length 1 lacks a 0th elem")) as char;
|
||||
match radix {
|
||||
'd' => self.radix = Radix::Decimal,
|
||||
'x' => self.radix = Radix::Hexadecimal,
|
||||
'o' => self.radix = Radix::Octal,
|
||||
'n' => self.radix = Radix::NoPrefix,
|
||||
_ => return Err("Radix must be one of [d, o, n, x]\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
#[cfg(test)]
|
||||
fn set_radix(&mut self, radix: Radix) {
|
||||
self.radix = radix;
|
||||
}
|
||||
|
||||
/// returns a string with the current byte offset
|
||||
|
@ -86,20 +67,20 @@ fn test_input_offset() {
|
|||
assert_eq!("000014", &sut.format_byte_offset());
|
||||
|
||||
// note normally the radix will not change after initialisation
|
||||
sut.parse_radix_from_commandline(Some("d".to_string())).unwrap();
|
||||
sut.set_radix(Radix::Decimal);
|
||||
assert_eq!("0000020", &sut.format_byte_offset());
|
||||
|
||||
sut.parse_radix_from_commandline(Some("x".to_string())).unwrap();
|
||||
sut.set_radix(Radix::Hexadecimal);
|
||||
assert_eq!("000014", &sut.format_byte_offset());
|
||||
|
||||
sut.parse_radix_from_commandline(Some("o".to_string())).unwrap();
|
||||
sut.set_radix(Radix::Octal);
|
||||
assert_eq!("0000024", &sut.format_byte_offset());
|
||||
|
||||
sut.parse_radix_from_commandline(Some("n".to_string())).unwrap();
|
||||
sut.set_radix(Radix::NoPrefix);
|
||||
assert_eq!("", &sut.format_byte_offset());
|
||||
|
||||
sut.increase_position(10);
|
||||
sut.parse_radix_from_commandline(None).unwrap();
|
||||
sut.set_radix(Radix::Octal);
|
||||
assert_eq!("0000036", &sut.format_byte_offset());
|
||||
}
|
||||
|
||||
|
@ -111,19 +92,19 @@ fn test_input_offset_with_label() {
|
|||
assert_eq!("000014 (00001E)", &sut.format_byte_offset());
|
||||
|
||||
// note normally the radix will not change after initialisation
|
||||
sut.parse_radix_from_commandline(Some("d".to_string())).unwrap();
|
||||
sut.set_radix(Radix::Decimal);
|
||||
assert_eq!("0000020 (0000030)", &sut.format_byte_offset());
|
||||
|
||||
sut.parse_radix_from_commandline(Some("x".to_string())).unwrap();
|
||||
sut.set_radix(Radix::Hexadecimal);
|
||||
assert_eq!("000014 (00001E)", &sut.format_byte_offset());
|
||||
|
||||
sut.parse_radix_from_commandline(Some("o".to_string())).unwrap();
|
||||
sut.set_radix(Radix::Octal);
|
||||
assert_eq!("0000024 (0000036)", &sut.format_byte_offset());
|
||||
|
||||
sut.parse_radix_from_commandline(Some("n".to_string())).unwrap();
|
||||
sut.set_radix(Radix::NoPrefix);
|
||||
assert_eq!("(0000036)", &sut.format_byte_offset());
|
||||
|
||||
sut.increase_position(10);
|
||||
sut.parse_radix_from_commandline(None).unwrap();
|
||||
sut.set_radix(Radix::Octal);
|
||||
assert_eq!("0000036 (0000050)", &sut.format_byte_offset());
|
||||
}
|
||||
|
|
129
src/od/od.rs
129
src/od/od.rs
|
@ -40,7 +40,7 @@ use partialreader::*;
|
|||
use peekreader::*;
|
||||
use formatteriteminfo::*;
|
||||
use parse_nrofbytes::parse_number_of_bytes;
|
||||
use parse_formats::parse_format_flags;
|
||||
use parse_formats::{parse_format_flags, ParsedFormatterItemInfo};
|
||||
use prn_char::format_ascii_dump;
|
||||
use parse_inputs::{parse_inputs, CommandLineInputs};
|
||||
use inputoffset::{InputOffset, Radix};
|
||||
|
@ -94,9 +94,7 @@ Any type specification can have a "z" suffic, which will add a ASCII dump at
|
|||
If an error occurred, a diagnostic message will be printed to stderr, and the
|
||||
exitcode will be non-zero."#;
|
||||
|
||||
/// parses and validates commandline parameters, prepares data structures,
|
||||
/// opens the input and calls `odfunc` to process the input.
|
||||
pub fn uumain(args: Vec<String>) -> i32 {
|
||||
fn create_getopts_options() -> getopts::Options {
|
||||
let mut opts = getopts::Options::new();
|
||||
|
||||
opts.optopt("A", "address-radix",
|
||||
|
@ -143,30 +141,29 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
opts.optflag("", "version", "output version information and exit.");
|
||||
opts.optflag("", "traditional", "compatibility mode with one input, offset and label.");
|
||||
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => m,
|
||||
Err(f) => {
|
||||
disp_err!("{}", f);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
if matches.opt_present("h") {
|
||||
println!("{}", opts.usage(&USAGE));
|
||||
return 0;
|
||||
}
|
||||
if matches.opt_present("version") {
|
||||
println!("{} {}", executable!(), VERSION);
|
||||
return 0;
|
||||
opts
|
||||
}
|
||||
|
||||
struct OdOptions {
|
||||
byte_order: ByteOrder,
|
||||
skip_bytes : usize,
|
||||
read_bytes : Option<usize>,
|
||||
label: Option<usize>,
|
||||
input_strings: Vec<String>,
|
||||
formats: Vec<ParsedFormatterItemInfo>,
|
||||
line_bytes: usize,
|
||||
output_duplicates: bool,
|
||||
radix: Radix,
|
||||
}
|
||||
|
||||
impl OdOptions {
|
||||
fn new(matches: getopts::Matches, args: Vec<String>) -> Result<OdOptions, String> {
|
||||
let byte_order = match matches.opt_str("endian").as_ref().map(String::as_ref) {
|
||||
None => { ByteOrder::Native },
|
||||
Some("little") => { ByteOrder::Little },
|
||||
Some("big") => { ByteOrder::Big },
|
||||
Some(s) => {
|
||||
disp_err!("Invalid argument --endian={}", s);
|
||||
return 1;
|
||||
return Err(format!("Invalid argument --endian={}", s));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -176,8 +173,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
match parse_number_of_bytes(&s) {
|
||||
Ok(i) => { i }
|
||||
Err(_) => {
|
||||
disp_err!("Invalid argument --skip-bytes={}", s);
|
||||
return 1;
|
||||
return Err(format!("Invalid argument --skip-bytes={}", s));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -193,16 +189,14 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
vec!{f}
|
||||
},
|
||||
Err(e) => {
|
||||
disp_err!("Invalid inputs: {}", e);
|
||||
return 1;
|
||||
return Err(format!("Invalid inputs: {}", e));
|
||||
}
|
||||
};
|
||||
|
||||
let formats = match parse_format_flags(&args) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
disp_err!("{}", e);
|
||||
return 1;
|
||||
return Err(format!("{}", e));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -229,23 +223,86 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
match parse_number_of_bytes(&s) {
|
||||
Ok(i) => { Some(i) }
|
||||
Err(_) => {
|
||||
disp_err!("Invalid argument --read-bytes={}", s);
|
||||
return 1;
|
||||
return Err(format!("Invalid argument --read-bytes={}", s));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut input_offset = InputOffset::new(Radix::Octal, skip_bytes, label);
|
||||
if let Err(e) = input_offset.parse_radix_from_commandline(matches.opt_str("A")) {
|
||||
disp_err!("Invalid -A/--address-radix\n{}", e);
|
||||
return 1;
|
||||
let radix = match matches.opt_str("A") {
|
||||
None => Radix::Octal,
|
||||
Some(s) => {
|
||||
let st = s.into_bytes();
|
||||
if st.len() != 1 {
|
||||
return Err(format!("Radix must be one of [d, o, n, x]"))
|
||||
} else {
|
||||
let radix: char = *(st.get(0)
|
||||
.expect("byte string of length 1 lacks a 0th elem")) as char;
|
||||
match radix {
|
||||
'd' => Radix::Decimal,
|
||||
'x' => Radix::Hexadecimal,
|
||||
'o' => Radix::Octal,
|
||||
'n' => Radix::NoPrefix,
|
||||
_ => return Err(format!("Radix must be one of [d, o, n, x]"))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(OdOptions {
|
||||
byte_order: byte_order,
|
||||
skip_bytes: skip_bytes,
|
||||
read_bytes: read_bytes,
|
||||
label: label,
|
||||
input_strings: input_strings,
|
||||
formats: formats,
|
||||
line_bytes: line_bytes,
|
||||
output_duplicates: output_duplicates,
|
||||
radix: radix,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let mut input = open_input_peek_reader(&input_strings, skip_bytes, read_bytes);
|
||||
let mut input_decoder = InputDecoder::new(&mut input, line_bytes, PEEK_BUFFER_SIZE, byte_order);
|
||||
/// parses and validates commandline parameters, prepares data structures,
|
||||
/// opens the input and calls `odfunc` to process the input.
|
||||
pub fn uumain(args: Vec<String>) -> i32 {
|
||||
let opts = create_getopts_options();
|
||||
|
||||
let output_info = OutputInfo::new(line_bytes, &formats[..], output_duplicates);
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => m,
|
||||
Err(f) => {
|
||||
disp_err!("{}", f);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
if matches.opt_present("h") {
|
||||
println!("{}", opts.usage(&USAGE));
|
||||
return 0;
|
||||
}
|
||||
if matches.opt_present("version") {
|
||||
println!("{} {}", executable!(), VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
let od_options = match OdOptions::new(matches, args) {
|
||||
Err(s) => {
|
||||
disp_err!("{}", s);
|
||||
return 1;
|
||||
},
|
||||
Ok(o) => o,
|
||||
};
|
||||
|
||||
let mut input_offset = InputOffset::new(od_options.radix, od_options.skip_bytes,
|
||||
od_options.label);
|
||||
|
||||
let mut input = open_input_peek_reader(&od_options.input_strings,
|
||||
od_options.skip_bytes, od_options.read_bytes);
|
||||
let mut input_decoder = InputDecoder::new(&mut input, od_options.line_bytes,
|
||||
PEEK_BUFFER_SIZE, od_options.byte_order);
|
||||
|
||||
let output_info = OutputInfo::new(od_options.line_bytes, &od_options.formats[..],
|
||||
od_options.output_duplicates);
|
||||
|
||||
odfunc(&mut input_offset, &mut input_decoder, &output_info)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue