blob: d5984d424b83f9108110d50aa6fc0598631c9f13 (
plain)
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
|
#include "coding/csv_file_reader.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include <fstream>
#include <sstream>
namespace coding
{
using namespace std;
void CSVReader::ReadLineByLine(string const & filePath, LineByLineCallback const & fn,
Params const & params) const
{
ifstream file(filePath);
if (!file)
{
LOG(LERROR, ("File not found at path: ", filePath));
return;
}
string line;
bool readFirstLine = params.m_shouldReadHeader;
while (getline(file, line))
{
vector<string> splitLine;
strings::ParseCSVRow(line, params.m_delimiter, splitLine);
if (!readFirstLine)
{
readFirstLine = true;
continue;
}
fn(splitLine);
}
}
void CSVReader::ReadFullFile(string const & filePath, FullFileCallback const & fn,
Params const & params) const
{
vector<vector<string>> file;
ReadLineByLine(filePath, [&file](vector<string> const & row) { file.emplace_back(row); }, params);
fn(file);
}
} // namespace coding
|