blob: 9cc3d6af874f96e5480e12b59fa350eacaf29736 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
use anyhow::Context as _;
use std::io::{BufRead as _, Read as _};
macro_rules! data {
() => {{
let file = crate::util::src_file_to_data_file(&std::file!());
std::fs::File::open(file.clone())
.with_context(|| format!("couldn't find data file {}", file))
}};
}
macro_rules! data_ints {
() => {
crate::util::read_ints(&crate::util::src_file_to_data_file(
&std::file!(),
))
};
}
macro_rules! data_bytes {
() => {
crate::util::read_file(&crate::util::src_file_to_data_file(
&std::file!(),
))
};
}
macro_rules! data_str {
() => {
crate::util::read_file_str(&crate::util::src_file_to_data_file(
&std::file!(),
))
};
}
pub fn src_file_to_data_file(file: &str) -> String {
let parts: Vec<_> = file.split('/').collect();
format!(
"data/{}/{}.txt",
parts[parts.len() - 3],
parts[parts.len() - 2]
)
}
pub fn read_ints(filename: &str) -> anyhow::Result<Vec<i64>> {
let f = std::fs::File::open(filename)
.with_context(|| format!("couldn't find data file {}", filename))?;
let f = std::io::BufReader::new(f);
let ints: anyhow::Result<Vec<_>> = f
.lines()
.map(|l| {
l.context("failed to read a line")?
.parse()
.context("failed to parse line into an integer")
})
.collect();
ints
}
pub fn read_file(filename: &str) -> anyhow::Result<Vec<u8>> {
let mut f = std::fs::File::open(filename)
.with_context(|| format!("couldn't find data file {}", filename))?;
let mut s = vec![];
f.read_to_end(&mut s)
.context("failed to read map contents")?;
Ok(s)
}
pub fn read_file_str(filename: &str) -> anyhow::Result<String> {
let mut f = std::fs::File::open(filename)
.with_context(|| format!("couldn't find data file {}", filename))?;
let mut s = String::new();
f.read_to_string(&mut s)
.context("failed to read map contents")?;
Ok(s)
}
|