-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cpp
61 lines (51 loc) · 968 Bytes
/
file.cpp
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
#include "file.hpp"
namespace dwmstatus {
namespace {
ifstream& unbuf(ifstream& f)
{
f.rdbuf()->pubsetbuf(nullptr, 0);
return f;
}
} // namespace
ifstream open_unbuf(const string& filename)
{
ifstream f { filename };
if (!f.is_open()) {
throw std::ios_base::failure("couldn't open file");
}
return move(unbuf(f));
}
istream& rewind(istream& f)
{
f.clear();
f.seekg(0);
return f;
}
//----------------------------------------------------------------------------
optional<string> read_line(istream& f)
{
string ans;
if (getline(f, ans)) {
return ans;
} else {
return nullopt;
}
}
template <>
optional<string> read_value(istream& f)
{
string ans;
if (f >> quoted(ans)) {
return ans;
} else {
return nullopt;
}
}
istream& skip_line(istream& f, unsigned n)
{
while (n-- && read_line(f)) {
// Skip
}
return f;
}
} // namespace dwmstatus