Skip to content
This repository was archived by the owner on Jun 22, 2023. It is now read-only.

Commit d87fb4d

Browse files
committed
Playing around with reading bytes
1 parent c120633 commit d87fb4d

File tree

8 files changed

+381
-2
lines changed

8 files changed

+381
-2
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
21
/target/
32
**/*.rs.bk
3+
master.dat

.idea/fo2dat.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+287
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.rs

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,59 @@
1+
use std::path::Path;
2+
use std::fs::File;
3+
use std::io;
4+
use std::error::Error;
5+
use std::io::ErrorKind;
6+
use std::io::BufReader;
7+
use std::io::Read;
8+
19
fn main() {
2-
println!("Hello, world!");
10+
let usage = "usage: fo2dat DAT_FILE";
11+
12+
let args: Vec<String> = std::env::args().collect();
13+
14+
match args.len() {
15+
2 => {
16+
if let Err(e) = run(&args[1]) {
17+
print_and_die(e.description(), 1);
18+
}
19+
}
20+
_ => {
21+
print_and_die(usage, 1);
22+
}
23+
}
324
}
25+
26+
fn print_and_die(s: &str, exit_code: i32) {
27+
println!("{}", s);
28+
std::process::exit(1);
29+
}
30+
31+
fn run(dat_file_path: &String) -> io::Result<()> {
32+
let dat_file_path = Path::new(dat_file_path);
33+
let dat_file = File::open(dat_file_path)?;
34+
35+
let reader = BufReader::new(dat_file);
36+
37+
let bytes = reader.bytes()
38+
.filter(|result| result.is_ok())
39+
.map(|result| result.unwrap());
40+
41+
print_hexdump(bytes);
42+
43+
Ok(())
44+
}
45+
46+
fn print_hexdump<I>(iter: I)
47+
where I: Iterator<Item = u8>
48+
{
49+
iter.map(|byte| format!("{:02x}", byte))
50+
.enumerate()
51+
.take(50)
52+
.for_each(|(i, hex)| {
53+
if i % 5 == 0 {
54+
println!("{}", hex);
55+
} else {
56+
print!("{}", hex);
57+
}
58+
});
59+
}

0 commit comments

Comments
 (0)