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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::convert::AsRef;
use std::iter::IntoIterator;
use std::path::Path;
use std::io::{Result as IoResult, Error as IoError};
use std::io::{Write, Read, BufRead, Lines, BufReader, BufWriter};
use std::fs::{OpenOptions, File, remove_file};
use std::fs::copy as fs_copy;
#[cfg(windows)]
const LINE_SEP: &'static [u8] = b"\r\n";
#[cfg(not(windows))]
const LINE_SEP: &'static [u8] = b"\n";
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> IoResult<()> {
let file = try!(OpenOptions::new().write(true).create(true).truncate(true).open(path));
let mut file = BufWriter::new(file);
file.write_all(contents.as_ref())
}
pub fn write_lines<P: AsRef<Path>, I: IntoIterator<Item=B, IntoIter=A>, A: Iterator<Item=B>, B: AsRef<[u8]>>(path: P, lines: I) -> IoResult<()> {
let file = try!(OpenOptions::new().write(true).create(true).truncate(true).open(path));
let mut file = BufWriter::new(file);
for line in lines.into_iter() {
try!(file.write_all(line.as_ref()));
try!(file.write_all(LINE_SEP));
}
Ok(())
}
pub fn append<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> IoResult<()> {
let mut file = try!(OpenOptions::new().write(true).create(true).append(true).open(path));
file.write_all(contents.as_ref())
}
pub fn append_line<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> IoResult<()> {
let mut file = try!(OpenOptions::new().write(true).create(true).append(true).open(path));
try!(file.write_all(contents.as_ref()));
file.write_all(LINE_SEP)
}
pub fn read<P: AsRef<Path>>(path: P) -> IoResult<Vec<u8>> {
let file = try!(OpenOptions::new().read(true).open(path));
let mut file = BufReader::new(file);
let mut out = vec![];
try!(file.read_to_end(&mut out));
Ok(out)
}
pub fn read_string_utf8<P: AsRef<Path>>(path: P) -> IoResult<String> {
match read(path) {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(s) => Ok(s),
Err(e) => Err(IoError::new(::std::io::ErrorKind::Other, e)),
},
Err(e) => Err(e)
}
}
pub fn read_string_utf8_lossy<P: AsRef<Path>>(path: P) -> IoResult<String> {
match read(path) {
Ok(bytes) => Ok(String::from_utf8_lossy(&bytes[..]).into_owned()),
Err(e) => Err(e)
}
}
pub fn read_lines<P: AsRef<Path>>(path: P) -> IoResult<Lines<BufReader<File>>> {
let file = try!(OpenOptions::new().read(true).open(path));
let file = BufReader::new(file);
Ok(file.lines())
}
pub fn exists<P: AsRef<Path>>(path: P) -> bool {
let path = path.as_ref();
path.is_file() && path.exists()
}
pub fn copy<Fp: AsRef<Path>, Tp: AsRef<Path>>(from: Fp, to: Tp) -> IoResult<()> {
fs_copy(from, to).map(|_| ())
}
pub fn remove<P: AsRef<Path>>(path: P) -> IoResult<()> {
remove_file(path)
}
pub fn has_extension<P: AsRef<Path>, S: AsRef<str>>(path: P, ext: S) -> bool {
path.as_ref().extension()
.and_then(|ext| ext.to_str())
.map(|provided_ext| provided_ext == ext.as_ref())
.unwrap_or(false)
}