-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.h
More file actions
78 lines (58 loc) · 1.43 KB
/
FileReader.h
File metadata and controls
78 lines (58 loc) · 1.43 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
#ifndef FILEREADER_H
#define FILEREADER_H
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
#include "Record.h"
#include "Operator.h"
// This function takes in a string with items delimited by commas
// and returns a vector of those items.
// Example - input: "str1,str2,str3,str4"
// output: {"str1", "str2", "str3", "str4"}
vector<string> split(string line) {
vector<string> v;
string s;
istringstream is(line);
while (getline(is, s, ','))
v.push_back(s);
return v;
}
class FileReader : public Operator {
private:
fstream fin;
string fileName;
vector<string> attrNames;
vector<string> attrTypes;
public:
FileReader() { }
FileReader(string fileName) : fileName(fileName) {}
void setFilename(string s){
fileName = s;
}
void open() {
fin.open(fileName);
string line;
// Read in names
getline(fin, line);
attrNames = split(line);
// Read in types
getline(fin, line);
attrTypes = split(line);
// Read in junk line delimiter
getline(fin, line);
}
vector<Record> next() {
vector<Record> outPage;
string line;
for(size_t i = 0; i < pageSize && getline(fin, line); i++) {
vector<string> values = split(line);
outPage.push_back( Record(attrNames, attrTypes, values) );
}
return outPage;
}
void close() {
fin.close();
}
};
#endif