1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

od: refactor: do all printing in odfunc

This commit is contained in:
Wim Hueskes 2016-07-24 22:28:22 +02:00
parent 36b88f268d
commit e006a84136
4 changed files with 55 additions and 49 deletions

View file

@ -150,28 +150,28 @@ pub fn uumain(args: Vec<String>) -> i32 {
offmarg: usize, offmarg: usize,
}; };
let oct = OdFormater { let oct = OdFormater {
writer: FormatWriter::IntWriter(print_item_oct), offmarg: 2 writer: FormatWriter::IntWriter(format_item_oct), offmarg: 2
}; };
let hex = OdFormater { let hex = OdFormater {
writer: FormatWriter::IntWriter(print_item_hex), offmarg: 2 writer: FormatWriter::IntWriter(format_item_hex), offmarg: 2
}; };
let dec_u = OdFormater { let dec_u = OdFormater {
writer: FormatWriter::IntWriter(print_item_dec_u), offmarg: 2 writer: FormatWriter::IntWriter(format_item_dec_u), offmarg: 2
}; };
let dec_s = OdFormater { let dec_s = OdFormater {
writer: FormatWriter::IntWriter(print_item_dec_s), offmarg: 2 writer: FormatWriter::IntWriter(format_item_dec_s), offmarg: 2
}; };
let a_char = OdFormater { let a_char = OdFormater {
writer: FormatWriter::IntWriter(print_item_a), offmarg: 1 writer: FormatWriter::IntWriter(format_item_a), offmarg: 1
}; };
let c_char = OdFormater { let c_char = OdFormater {
writer: FormatWriter::IntWriter(print_item_c), offmarg: 1 writer: FormatWriter::IntWriter(format_item_c), offmarg: 1
}; };
let flo32 = OdFormater { let flo32 = OdFormater {
writer: FormatWriter::FloatWriter(print_item_flo32), offmarg: 0 writer: FormatWriter::FloatWriter(format_item_flo32), offmarg: 0
}; };
let flo64 = OdFormater { let flo64 = OdFormater {
writer: FormatWriter::FloatWriter(print_item_flo64), offmarg: 0 writer: FormatWriter::FloatWriter(format_item_flo64), offmarg: 0
}; };
fn mkfmt(itembytes: usize, fmtspec: &OdFormater) -> OdFormat { fn mkfmt(itembytes: usize, fmtspec: &OdFormater) -> OdFormat {
@ -248,24 +248,17 @@ fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource],
loop { loop {
// print each line data (or multi-format raster of several lines describing the same data). // print each line data (or multi-format raster of several lines describing the same data).
print_with_radix(input_offset_base, addr); // print offset
// if printing in multiple formats offset is printed only once
match mf.f_read(bytes.as_mut_slice()) { match mf.f_read(bytes.as_mut_slice()) {
Ok(0) => { Ok(0) => {
print!("\n"); print!("{}\n", print_with_radix(input_offset_base, addr)); // print final offset
break; break;
} }
Ok(n) => { Ok(n) => {
let mut first = true; // First line of a multi-format raster. let mut first = true; // First line of a multi-format raster.
for f in formats { for f in formats {
if !first { let mut output_text = String::new();
// this takes the space of the file offset on subsequent
// lines of multi-format rasters. output_text.push_str(&format!("{:>width$}", "", width = f.offmarg));// 4 spaces after offset - we print 2 more before each word
print!(" ");
}
first = false;
print!("{:>width$}", "", width = f.offmarg);// 4 spaces after offset - we print 2 more before each word
// not enough byte for a whole element, this should only happen on the last line. // not enough byte for a whole element, this should only happen on the last line.
if n % f.itembytes != 0 { if n % f.itembytes != 0 {
@ -296,7 +289,7 @@ fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource],
} }
_ => { panic!("Invalid itembytes: {}", f.itembytes); } _ => { panic!("Invalid itembytes: {}", f.itembytes); }
}; };
func(p, f.itembytes); output_text.push_str(&func(p, f.itembytes));
} }
FormatWriter::FloatWriter(func) => { FormatWriter::FloatWriter(func) => {
let p: f64 = match f.itembytes { let p: f64 = match f.itembytes {
@ -308,12 +301,23 @@ fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource],
} }
_ => { panic!("Invalid itembytes: {}", f.itembytes); } _ => { panic!("Invalid itembytes: {}", f.itembytes); }
}; };
func(p); output_text.push_str(&func(p));
} }
} }
b = nextb; b = nextb;
} }
print!("\n");
if first {
print!("{}", print_with_radix(input_offset_base, addr)); // print offset
// if printing in multiple formats offset is printed only once
first = false;
}
else {
// this takes the space of the file offset on subsequent
// lines of multi-format rasters.
print!(" ");
}
print!("{}\n", output_text);
} }
addr += n; addr += n;
} }
@ -322,6 +326,7 @@ fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource],
} }
}; };
} }
if mf.any_err { if mf.any_err {
1 1
} else { } else {
@ -352,21 +357,21 @@ fn parse_radix(radix_str: Option<String>) -> Result<Radix, &'static str> {
} }
} }
fn print_with_radix(r: &Radix, x: usize) { fn print_with_radix(r: &Radix, x: usize) -> String{
// TODO(keunwoo): field widths should be based on sizeof(x), or chosen dynamically based on the // TODO(keunwoo): field widths should be based on sizeof(x), or chosen dynamically based on the
// expected range of address values. Binary in particular is not great here. // expected range of address values. Binary in particular is not great here.
match *r { match *r {
Radix::Decimal => print!("{:07}", x), Radix::Decimal => format!("{:07}", x),
Radix::Hexadecimal => print!("{:07X}", x), Radix::Hexadecimal => format!("{:07X}", x),
Radix::Octal => print!("{:07o}", x), Radix::Octal => format!("{:07o}", x),
Radix::Binary => print!("{:07b}", x) Radix::Binary => format!("{:07b}", x)
} }
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
enum FormatWriter { enum FormatWriter {
IntWriter(fn(u64, usize)), IntWriter(fn(u64, usize) -> String),
FloatWriter(fn(f64)), FloatWriter(fn(f64) -> String),
} }
struct OdFormat { struct OdFormat {

View file

@ -24,11 +24,11 @@ static A_CHRS : [&'static str; 160] =
"90", "91", "92", "93", "94", "95", "96", "97", "90", "91", "92", "93", "94", "95", "96", "97",
"98", "99", "9a", "9b", "9c", "9d", "9e", "9f"]; "98", "99", "9a", "9b", "9c", "9d", "9e", "9f"];
pub fn print_item_a(p: u64, _: usize) { pub fn format_item_a(p: u64, _: usize) -> String {
// itembytes == 1 // itembytes == 1
let b = (p & 0xff) as u8; let b = (p & 0xff) as u8;
print!("{:>4}", A_CHRS.get(b as usize).unwrap_or(&"?") // XXX od dose not actually do this, it just prints the byte format!("{:>4}", A_CHRS.get(b as usize).unwrap_or(&"?") // XXX od dose not actually do this, it just prints the byte
); )
} }
@ -51,14 +51,15 @@ static C_CHRS : [&'static str; 127] = [
"x", "y", "z", "{", "|", "}", "~" ]; "x", "y", "z", "{", "|", "}", "~" ];
pub fn print_item_c(p: u64, _: usize) { pub fn format_item_c(p: u64, _: usize) -> String {
// itembytes == 1 // itembytes == 1
let b = (p & 0xff) as usize; let b = (p & 0xff) as usize;
if b < C_CHRS.len() { if b < C_CHRS.len() {
match C_CHRS.get(b as usize) { match C_CHRS.get(b as usize) {
Some(s) => print!("{:>4}", s), Some(s) => format!("{:>4}", s),
None => print!("{:>4}", b), None => format!("{:>4}", b),
} }
} }
else { String::new() }
} }

View file

@ -2,12 +2,12 @@ use std::num::FpCategory;
use std::f32; use std::f32;
use std::f64; use std::f64;
pub fn print_item_flo32(f: f64) { pub fn format_item_flo32(f: f64) -> String {
print!(" {}", format_flo32(f as f32)) format!(" {}", format_flo32(f as f32))
} }
pub fn print_item_flo64(f: f64) { pub fn format_item_flo64(f: f64) -> String {
print!(" {}", format_flo64(f)) format!(" {}", format_flo64(f))
} }
// formats float with 8 significant digits, eg 12345678 or -1.2345678e+12 // formats float with 8 significant digits, eg 12345678 or -1.2345678e+12

View file

@ -1,24 +1,24 @@
// TODO: use some sort of byte iterator, instead of passing bytes in u64 // TODO: use some sort of byte iterator, instead of passing bytes in u64
pub fn print_item_oct(p: u64, itembytes: usize) { pub fn format_item_oct(p: u64, itembytes: usize) -> String {
let itemwidth = 3 * itembytes; let itemwidth = 3 * itembytes;
let itemspace = 4 * itembytes - itemwidth; let itemspace = 4 * itembytes - itemwidth;
print!("{:>itemspace$}{:0width$o}", format!("{:>itemspace$}{:0width$o}",
"", "",
p, p,
width = itemwidth, width = itemwidth,
itemspace = itemspace); itemspace = itemspace)
} }
pub fn print_item_hex(p: u64, itembytes: usize) { pub fn format_item_hex(p: u64, itembytes: usize) -> String {
let itemwidth = 2 * itembytes; let itemwidth = 2 * itembytes;
let itemspace = 4 * itembytes - itemwidth; let itemspace = 4 * itembytes - itemwidth;
print!("{:>itemspace$}{:0width$x}", format!("{:>itemspace$}{:0width$x}",
"", "",
p, p,
width = itemwidth, width = itemwidth,
itemspace = itemspace); itemspace = itemspace)
} }
@ -28,12 +28,12 @@ fn sign_extend(item: u64, itembytes: usize) -> i64{
} }
pub fn print_item_dec_s(p: u64, itembytes: usize) { pub fn format_item_dec_s(p: u64, itembytes: usize) -> String {
// sign extend // sign extend
let s = sign_extend(p,itembytes); let s = sign_extend(p,itembytes);
print!("{:totalwidth$}", s, totalwidth = 4 * itembytes); format!("{:totalwidth$}", s, totalwidth = 4 * itembytes)
} }
pub fn print_item_dec_u(p: u64, itembytes: usize) { pub fn format_item_dec_u(p: u64, itembytes: usize) -> String {
print!("{:totalwidth$}", p, totalwidth = 4 * itembytes); format!("{:totalwidth$}", p, totalwidth = 4 * itembytes)
} }