-
Notifications
You must be signed in to change notification settings - Fork 21
/
streamhelper.hpp
57 lines (52 loc) · 1.34 KB
/
streamhelper.hpp
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
// LGPL 3 or higher Robert Burner Schadek rburners@gmail.com
#pragma once
#include <fstream>
namespace sweet {
inline int checkErrorBits(std::ifstream* f) {
int stop = 0;
if (f->eof()) {
perror("stream eofbit. error state");
// EOF after std::getline() is not the criterion to stop processing
// data: In case there is data between the last delimiter and EOF,
// getline() extracts it and sets the eofbit.
stop = 0;
}
if (f->fail()) {
perror("stream failbit (or badbit). error state");
stop = 1;
}
if (f->bad()) {
perror("stream badbit. error state");
stop = 1;
}
return stop;
}
inline int checkErrorBits(std::ofstream* f) {
int stop = 0;
if (f->eof()) {
perror("stream eofbit. error state");
// EOF after std::getline() is not the criterion to stop processing
// data: In case there is data between the last delimiter and EOF,
// getline() extracts it and sets the eofbit.
stop = 0;
}
if (f->fail()) {
perror("stream failbit (or badbit). error state");
stop = 1;
}
if (f->bad()) {
perror("stream badbit. error state");
stop = 1;
}
return stop;
}
inline std::string istreamToString(std::istream& in) {
std::string ret;
char buffer[4096];
while(in.read(buffer, sizeof(buffer))) {
ret.append(buffer, sizeof(buffer));
}
ret.append(buffer, static_cast<size_t>(in.gcount()));
return ret;
}
} // ~namespace sweet