mirror of
https://github.com/uutils/coreutils
synced 2024-07-23 19:04:18 +00:00
od: refactor: convert bytes using byteorder crate
This commit is contained in:
parent
3e143217a9
commit
5c495359c1
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -176,6 +176,11 @@ name = "bitflags"
|
|||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cat"
|
||||
version = "0.0.1"
|
||||
|
@ -658,6 +663,7 @@ dependencies = [
|
|||
name = "od"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unindent 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -11,6 +11,7 @@ path = "od.rs"
|
|||
getopts = "*"
|
||||
libc = "*"
|
||||
unindent = "*"
|
||||
byteorder = "*"
|
||||
|
||||
[[bin]]
|
||||
name = "od"
|
||||
|
|
39
src/od/od.rs
39
src/od/od.rs
|
@ -11,14 +11,15 @@
|
|||
|
||||
extern crate getopts;
|
||||
extern crate unindent;
|
||||
extern crate byteorder;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::mem;
|
||||
use std::io::BufReader;
|
||||
use std::io::Write;
|
||||
use std::io;
|
||||
use unindent::*;
|
||||
use byteorder::*;
|
||||
|
||||
//This is available in some versions of std, but not all that we target.
|
||||
macro_rules! hashmap {
|
||||
|
@ -233,21 +234,35 @@ fn odfunc(input_offset_base: &Radix, fnames: &[InputSource], formats: &[OdFormat
|
|||
first = false;
|
||||
print!("{:>width$}", "", width = f.offmarg);// 4 spaces after offset - we print 2 more before each word
|
||||
|
||||
for b in 0..n / f.itembytes {
|
||||
let mut p: u64 = 0;
|
||||
for i in 0..f.itembytes {
|
||||
p |= (bytes[(f.itembytes * b) + i] as u64) << (8 * i);
|
||||
}
|
||||
(f.writer)(p, f.itembytes);
|
||||
}
|
||||
// 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;
|
||||
let mut p2: u64 = 0;
|
||||
for i in 0..(n % f.itembytes) {
|
||||
p2 |= (bytes[(f.itembytes * b) + i] as u64) << (8 * i);
|
||||
// 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;
|
||||
}
|
||||
(f.writer)(p2, f.itembytes);
|
||||
}
|
||||
|
||||
let mut b = 0;
|
||||
while b < n {
|
||||
let nextb = b + f.itembytes;
|
||||
let p: u64 = match f.itembytes {
|
||||
1 => {
|
||||
bytes[b] as u64
|
||||
}
|
||||
2 => {
|
||||
LittleEndian::read_u16(&bytes[b..nextb]) as u64
|
||||
}
|
||||
4 => {
|
||||
LittleEndian::read_u32(&bytes[b..nextb]) as u64
|
||||
}
|
||||
8 => {
|
||||
LittleEndian::read_u64(&bytes[b..nextb])
|
||||
}
|
||||
_ => { panic!("Invalid itembytes: {}", f.itembytes); }
|
||||
};
|
||||
(f.writer)(p, f.itembytes);
|
||||
b = nextb;
|
||||
}
|
||||
// Add extra spaces to pad out the short, presumably last, line.
|
||||
if n < LINEBYTES {
|
||||
|
|
Loading…
Reference in a new issue