-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVReader.cpp
More file actions
72 lines (55 loc) · 1.95 KB
/
CSVReader.cpp
File metadata and controls
72 lines (55 loc) · 1.95 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
#include "CSVReader.h"
#include <fstream>
#include <algorithm>
#include <iostream>
#include "ErrorHandler.h"
const std::vector<char> CSVReader::C_SEPARATORS = {',',';'};
/******************************************************************************/
CSVReader::CSVReader(const std::filesystem::path & filepath)
{
ErrorHandler::assert(std::filesystem::exists(filepath), "Error: File '", filepath, "' not found");
// Create an input filestream
std::ifstream csv_file(filepath);
std::string line;
// Read data, line by line
while(std::getline(csv_file, line))
{
if (line.size() == 0)
continue;
// Getting and splitting the line
std::vector<std::string> split_line;
size_t left = 0;
for (size_t right = 0 ; right < line.size() ; ++right)
{
if (std::find(C_SEPARATORS.cbegin(), C_SEPARATORS.cend(), line[right]) != C_SEPARATORS.cend())
{
split_line.push_back(line.substr(left, right-left));
left = right+1;
}
}
split_line.push_back(line.substr(left, line.size()-left));
m_csv_data.push_back(split_line);
}
// Close file
csv_file.close();
}
/******************************************************************************/
std::vector<std::vector<std::string>>::iterator CSVReader::begin()
{
return m_csv_data.begin();
}
/******************************************************************************/
std::vector<std::vector<std::string>>::iterator CSVReader::end()
{
return m_csv_data.end();
}
/******************************************************************************/
std::vector<std::vector<std::string>>::const_iterator CSVReader::cbegin() const
{
return m_csv_data.cbegin();
}
/******************************************************************************/
std::vector<std::vector<std::string>>::const_iterator CSVReader::cend() const
{
return m_csv_data.cend();
}