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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
extern crate bincode;
extern crate lz4;
extern crate serde;
use bincode::{deserialize_from, serialize_into};
use serde::{de::DeserializeOwned, Serialize};
use std::ffi::OsStr;
use std::fs;
use std::{fmt::Debug, fs::File, io::prelude::*, path::Path};
pub fn dir_list(d: &str) -> Vec<String> {
let x = fs::read_dir(&d).unwrap_or_else(|_| panic!("failed to read directory {}", d));
let mut y = Vec::<String>::new();
for f in x {
let s: String = f.unwrap().file_name().into_string().unwrap();
y.push(s);
}
y.sort();
y
}
pub fn path_exists(p: &str) -> bool {
Path::new(p).exists()
}
#[macro_export]
macro_rules! fwriteln {
($f:expr, $u:expr) => {
writeln!( $f, $u ).expect( &format!( "writeln! failed" ) );
};
($f:expr, $u:expr, $($x:tt)*) => {
writeln!( $f, $u, $($x)* )
.expect( &format!( "writeln! failed while writing \"{}\"", $u ) );
};
}
#[macro_export]
macro_rules! fwrite {
($f:expr, $u:expr) => {
write!( $f, $u ).expect( &format!( "write! failed" ) );
};
($f:expr, $u:expr, $($x:tt)*) => {
write!( $f, $u, $($x)* )
.expect( &format!( "write! failed while writing \"{}\"", $u ) );
};
}
#[macro_export]
macro_rules! open_for_read {
($filename:expr) => {
BufReader::new(
File::open(&$filename).expect(&format!("Could not open file \"{}\"", &$filename)),
);
};
}
#[macro_export]
macro_rules! open_for_write_new {
($filename:expr) => {
BufWriter::new(
File::create(&$filename).expect(&format!("Could not create file \"{}\"", &$filename)),
);
};
}
pub fn open_lz4<P: AsRef<Path>>(filename: P) -> lz4::Decoder<File> {
let f = File::open(filename).expect("Failed to open file for reading");
lz4::Decoder::new(f).expect("Failed to create lz4 decoder")
}
pub fn open_maybe_compressed<P: AsRef<Path>>(filename: P) -> Box<dyn Read> {
match filename.as_ref().extension().and_then(OsStr::to_str) {
Some("lz4") => Box::new(open_lz4(filename)) as Box<dyn Read>,
_ => Box::new(File::open(filename).expect("Failed to open file for reading"))
as Box<dyn Read>,
}
}
pub fn write_obj<T: Serialize, P: AsRef<Path> + Debug>(g: &T, filename: P) {
let f = match std::fs::File::create(&filename) {
Err(err) => panic!("couldn't create file {:?}: {}", filename, err),
Ok(f) => f,
};
let mut writer = std::io::BufWriter::new(f);
serialize_into(&mut writer, &g)
.unwrap_or_else(|_| panic!("write_obj of file {:?} failed", filename))
}
pub fn read_obj<T: DeserializeOwned, P: AsRef<Path> + Debug>(filename: P) -> T {
let f = match std::fs::File::open(&filename) {
Err(err) => panic!("couldn't open file {:?}: {}", filename, err),
Ok(f) => f,
};
let mut reader = std::io::BufReader::new(f);
deserialize_from(&mut reader)
.unwrap_or_else(|_| panic!("read_obj of file {:?} failed", filename))
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! printme {
( $( $x:expr ),* ) => {
println!(concat!( $( stringify!($x), " = {}, ", )* ), $($x,)*);
}
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! eprintme {
( $( $x:expr ),* ) => {
eprintln!(concat!( $( stringify!($x), " = {}, ", )* ), $($x,)*);
}
}