mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +00:00
od: clean up parse_radix code and use site
+ Make parse_radix terser and clearer. + Make purpose of radix clearer at use site (note that the code currently completely misuses the --address-radix flag; this is inherited from the previous code) + Don't panic! inside parse_radix; instead return Result<> and let the caller handle errors (currently we panic, but probably we'll want to use some less alarming error routine); this will be more testable later as well.
This commit is contained in:
parent
2f0d8c89c9
commit
b756a57345
1 changed files with 22 additions and 31 deletions
51
src/od/od.rs
51
src/od/od.rs
|
@ -46,13 +46,10 @@ pub fn uumain(args: Vec<String>) -> isize {
|
||||||
Err(f) => panic!("Invalid options\n{}", f)
|
Err(f) => panic!("Invalid options\n{}", f)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let input_offset_base = match parse_radix(matches.opt_str("A")) {
|
||||||
let mut rad = Radix::Octal;
|
Ok(r) => r,
|
||||||
if matches.opt_present("A") {
|
Err(f) => { panic!("Invalid -A/--address-radix\n{}", f) }
|
||||||
rad = parse_radix(matches.opt_str("A"));
|
};
|
||||||
} else {
|
|
||||||
println!("{}", getopts::usage("od", &opts));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut fname;
|
let mut fname;
|
||||||
match args.last() {
|
match args.last() {
|
||||||
|
@ -60,12 +57,12 @@ pub fn uumain(args: Vec<String>) -> isize {
|
||||||
None => { panic!("Need fname for now") ; }
|
None => { panic!("Need fname for now") ; }
|
||||||
};
|
};
|
||||||
|
|
||||||
main(rad, fname.clone());
|
main(input_offset_base, fname.clone());
|
||||||
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main(radix: Radix, fname: String) {
|
fn main(input_offset_base: Radix, fname: String) {
|
||||||
let mut f = match File::open(&Path::new(fname)) {
|
let mut f = match File::open(&Path::new(fname)) {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(e) => panic!("file error: {}", e)
|
Err(e) => panic!("file error: {}", e)
|
||||||
|
@ -77,7 +74,7 @@ fn main(radix: Radix, fname: String) {
|
||||||
match f.read(bytes) {
|
match f.read(bytes) {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
print!("{:07o}", addr);
|
print!("{:07o}", addr);
|
||||||
match radix {
|
match input_offset_base {
|
||||||
Radix::Decimal => {},
|
Radix::Decimal => {},
|
||||||
Radix::Octal => {
|
Radix::Octal => {
|
||||||
for b in range(0, n / std::u16::BYTES) {
|
for b in range(0, n / std::u16::BYTES) {
|
||||||
|
@ -99,30 +96,24 @@ fn main(radix: Radix, fname: String) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_radix(radix_str: Option<String>) -> Radix {
|
fn parse_radix(radix_str: Option<String>) -> Result<Radix, &'static str> {
|
||||||
let rad = match radix_str {
|
match radix_str {
|
||||||
|
None => Ok(Radix::Octal),
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
let st = s.into_bytes();
|
let st = s.into_bytes();
|
||||||
if st.len() != 1 {
|
if st.len() != 1 {
|
||||||
panic!("Radix must be one of [d, o, b, x]\n");
|
Err("Radix must be one of [d, o, b, x]\n")
|
||||||
}
|
} else {
|
||||||
|
|
||||||
let radix: char = *(st.get(0)
|
let radix: char = *(st.get(0)
|
||||||
.expect("byte string of length 1 lacks a 0th elem")) as char;
|
.expect("byte string of length 1 lacks a 0th elem")) as char;
|
||||||
if radix == 'd' {
|
match radix {
|
||||||
Radix::Decimal
|
'd' => Ok(Radix::Decimal),
|
||||||
} else if radix == 'x' {
|
'x' => Ok(Radix::Hexadecimal),
|
||||||
Radix::Hexadecimal
|
'o' => Ok(Radix::Octal),
|
||||||
} else if radix == 'o' {
|
'b' => Ok(Radix::Binary),
|
||||||
Radix::Octal
|
_ => Err("Radix must be one of [d, o, b, x]\n")
|
||||||
} else if radix == 'b' {
|
}
|
||||||
Radix::Binary
|
}
|
||||||
} else {
|
}
|
||||||
panic!("Radix must be one of [d, o, b, x]\n");
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
None => Radix::Octal
|
|
||||||
};
|
|
||||||
|
|
||||||
rad
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue