1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

od: split odfunc()

This commit is contained in:
Wim Hueskes 2016-07-25 13:59:21 +02:00
parent e006a84136
commit 80386ef04f

View file

@ -254,23 +254,40 @@ fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource],
break;
}
Ok(n) => {
// not enough byte for a whole element, this should only happen on the last line.
if n != line_bytes {
// set zero bytes in the part of the buffer that will be used, but is not filled.
for i in n..line_bytes {
bytes[i] = 0;
}
}
print_bytes(&bytes, n, &print_with_radix(input_offset_base, addr), formats);
addr += n;
}
Err(_) => {
break;
}
};
}
if mf.any_err {
1
} else {
0
}
}
fn print_bytes(bytes: &[u8], length: usize, prefix: &str, formats: &[OdFormat]) {
let mut first = true; // First line of a multi-format raster.
for f in formats {
let mut output_text = String::new();
output_text.push_str(&format!("{:>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.
if n % f.itembytes != 0 {
let b = n / f.itembytes;
// set zero bytes in the part of the buffer that will be used, but is not filled.
for i in n..(b + 1) * f.itembytes {
bytes[i] = 0;
}
}
let mut b = 0;
while b < n {
while b < length {
let nextb = b + f.itembytes;
match f.writer {
FormatWriter::IntWriter(func) => {
@ -308,30 +325,17 @@ fn odfunc(line_bytes: usize, input_offset_base: &Radix, fnames: &[InputSource],
}
if first {
print!("{}", print_with_radix(input_offset_base, addr)); // print offset
print!("{}", prefix); // 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!("{:>width$}", "", width=prefix.chars().count());
}
print!("{}\n", output_text);
}
addr += n;
}
Err(_) => {
break;
}
};
}
if mf.any_err {
1
} else {
0
}
}
// For file byte offset printed at left margin.