1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 21:17:46 +00:00

od: whitespace fix (4-space indents, 99-column)

This commit is contained in:
Keunwoo Lee 2015-01-25 00:03:14 -08:00
parent 2d8e7f6dec
commit a6e5deaa16

View file

@ -19,100 +19,110 @@ use std::io::File;
enum Radix { Decimal, Hexadecimal, Octal, Binary } enum Radix { Decimal, Hexadecimal, Octal, Binary }
pub fn uumain(args: Vec<String>) -> isize { pub fn uumain(args: Vec<String>) -> isize {
let opts = [ let opts = [
getopts::optopt("A", "address-radix", "Select the base in which file offsets are printed.", "RADIX"), getopts::optopt("A", "address-radix",
getopts::optopt("j", "skip-bytes", "Skip bytes input bytes before formatting and writing.", "BYTES"), "Select the base in which file offsets are printed.", "RADIX"),
getopts::optopt("N", "read-bytes", "limit dump to BYTES input bytes", "BYTES"), getopts::optopt("j", "skip-bytes",
getopts::optopt("S", "strings", "output strings of at least BYTES graphic chars. 3 is assumed when BYTES is not specified.", "BYTES"), "Skip bytes input bytes before formatting and writing.", "BYTES"),
getopts::optopt("t", "format", "select output format or formats", "TYPE"), getopts::optopt("N", "read-bytes",
getopts::optflag("v", "output-duplicates", "do not use * to mark line suppression"), "limit dump to BYTES input bytes", "BYTES"),
getopts::optopt("w", "width", "output BYTES bytes per output line. 32 is implied when BYTES is not specified.", "BYTES"), getopts::optopt("S", "strings",
getopts::optflag("h", "help", "display this help and exit."), ("output strings of at least BYTES graphic chars. 3 is assumed when \
getopts::optflag("v", "version", "output version information and exit."), BYTES is not specified."),
]; "BYTES"),
getopts::optopt("t", "format", "select output format or formats", "TYPE"),
getopts::optflag("v", "output-duplicates", "do not use * to mark line suppression"),
getopts::optopt("w", "width",
("output BYTES bytes per output line. 32 is implied when BYTES is not \
specified."),
"BYTES"),
getopts::optflag("h", "help", "display this help and exit."),
getopts::optflag("v", "version", "output version information and exit."),
];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(args.tail(), &opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => panic!("Invalid options\n{}", f) Err(f) => panic!("Invalid options\n{}", f)
}; };
let mut rad = Radix::Octal; let mut rad = Radix::Octal;
if matches.opt_present("A") { if matches.opt_present("A") {
rad = parse_radix(matches.opt_str("A")); rad = parse_radix(matches.opt_str("A"));
} else { } else {
println!("{}", getopts::usage("od", &opts)); println!("{}", getopts::usage("od", &opts));
} }
let mut fname; let mut fname;
match args.last() { match args.last() {
Some(n) => fname = n, Some(n) => fname = n,
None => { panic!("Need fname for now") ; } None => { panic!("Need fname for now") ; }
}; };
main(rad, fname.clone()); main(rad, fname.clone());
0 0
} }
fn main(radix: Radix, fname: String) { fn main(radix: 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)
}; };
let mut addr = 0; let mut addr = 0;
let bytes = &mut [b'\x00'; 16]; let bytes = &mut [b'\x00'; 16];
loop { loop {
match f.read(bytes) { match f.read(bytes) {
Ok(n) => { Ok(n) => {
print!("{:07o}", addr); print!("{:07o}", addr);
match radix { match radix {
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) {
let bs = bytes.slice(2*b, 2*b+2); let bs = bytes.slice(2*b, 2*b+2);
let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16; let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16;
print!(" {:06o}", p); print!(" {:06o}", p);
} }
if n % std::u16::BYTES == 1 { if n % std::u16::BYTES == 1 {
print!(" {:06o}", bytes[n-1]); print!(" {:06o}", bytes[n-1]);
} }
} }
_ => { } _ => { }
}; };
print!("\n"); print!("\n");
addr += n; addr += n;
}, },
Err(_) => { println!("{:07o}", addr); break; } Err(_) => { println!("{:07o}", addr); break; }
}; };
}; };
} }
fn parse_radix(radix_str: Option<String>) -> Radix { fn parse_radix(radix_str: Option<String>) -> Radix {
let rad = match radix_str { let rad = match radix_str {
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"); panic!("Radix must be one of [d, o, b, x]\n");
} }
let radix: char = *(st.get(0).expect("byte string of length 1 lacks a 0th elem")) as char; let radix: char = *(st.get(0)
if radix == 'd' { .expect("byte string of length 1 lacks a 0th elem")) as char;
Radix::Decimal if radix == 'd' {
} else if radix == 'x' { Radix::Decimal
Radix::Hexadecimal } else if radix == 'x' {
} else if radix == 'o' { Radix::Hexadecimal
Radix::Octal } else if radix == 'o' {
} else if radix == 'b' { Radix::Octal
Radix::Binary } else if radix == 'b' {
} else { Radix::Binary
panic!("Radix must be one of [d, o, b, x]\n"); } else {
} panic!("Radix must be one of [d, o, b, x]\n");
}, }
None => Radix::Octal },
}; None => Radix::Octal
};
rad rad
} }