-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiFileReader.cpp
More file actions
89 lines (74 loc) · 1.71 KB
/
Copy pathMultiFileReader.cpp
File metadata and controls
89 lines (74 loc) · 1.71 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "MultiFileReader.h"
MultiFileReader::MultiFileReader(std::string dir, int pageSize)
: filenames(this->listDirectory(dir))
, PAGE_SIZE(pageSize)
, DIRECTORY(dir)
{
}
bool MultiFileReader::nextPage(Page& page, float minPageFullness)
{
page.clear();
for (int i = 0; i < this->PAGE_SIZE; i++) {
bool keepBlanks = false;
std::string line;
if (!this->nextLine(line, keepBlanks)) {
break;
}
page.emplace_back(line);
}
return page.size() >= this->PAGE_SIZE * minPageFullness;
}
bool MultiFileReader::nextLine(std::string& line, bool keepBlanks)
{
if (keepBlanks) {
return this->nextLine(line);
}
bool ok;
while ((ok = nextLine(line)) && line == "");
return ok;
}
bool MultiFileReader::nextLine(std::string& line)
{
bool ok;
if (ok = std::getline(this->infile, line)) {
return ok;
}
this->nextFile();
return std::getline(this->infile, line);
}
bool MultiFileReader::nextFile()
{
if (this->filenames.size() == 0) {
return false;
}
this->infile.close();
this->infile.open(
this->DIRECTORY
+ Util::separator()
+ this->filenames.back());
this->filenames.pop_back();
return this->infile.good();
}
std::vector<std::string> MultiFileReader::listDirectory(std::string dirName)
{
auto filenames = std::vector<std::string>();
DIR *dir;
struct dirent *ent;
if ((dir = opendir(dirName.c_str())) == NULL) {
throw "Could not open directory";
}
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] == '.') {
continue;
}
filenames.emplace_back(ent->d_name);
}
closedir (dir);
std::sort(filenames.rbegin(), filenames.rend()); // yes, reverse order
return filenames;
}
void MultiFileReader::rewind()
{
filenames = this->listDirectory(this->DIRECTORY);
this->nextFile();
}