-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileInput.cpp
More file actions
70 lines (60 loc) · 1.49 KB
/
FileInput.cpp
File metadata and controls
70 lines (60 loc) · 1.49 KB
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
/*******************************************************************
* FileInput.cpp
* KPS
*
* Author: Kareem Omar
* kareem.omar@uah.edu
* https://github.com/komrad36
*
* Last updated Feb 27, 2016
* This application is entirely my own work.
*******************************************************************/
//
// Platform-indepdendent file reading (i.e. independent of newline character)
//
#include "FileInput.h"
// hand-rolled getLine compatible with
// Windows and Linux newlines, as well
// as files without a final trailing newline
std::istream& safeGetline(std::istream& is, std::string& t) {
t.clear();
// streambuf for speed
// requires stream sentry to guard buffer
std::istream::sentry se(is, DO_NOT_SKIP_WHITESPACE);
std::streambuf* sb = is.rdbuf();
short c;
for (;;) {
c = sb->sbumpc();
switch (c) {
case '\n':
return is;
case '\r':
if (sb->sgetc() == '\n')
sb->sbumpc();
return is;
case EOF:
// handle the case when the last line has no line ending
if (t.empty())
is.setstate(std::ios::eofbit);
return is;
default:
t += static_cast<char>(c);
}
}
}
// wrapper to simply retrive clean line from ifstream
// returns false when no more valid lines
bool getNextLine(std::ifstream& in_f, std::string& line) {
bool cont = true;
while (cont) {
safeGetline(in_f, line);
if (!in_f.eof()) {
cont = false;
}
else {
line.clear();
return false;
}
}
return true;
}