-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSVParser.cpp
More file actions
89 lines (74 loc) · 1.71 KB
/
CSVParser.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 "CSVParser.h"
#include <boost/tokenizer.hpp>
#include <iostream>
#include <fstream>
using namespace boost;
using namespace quantserver;
CSVParser::CSVParser(const std::string& filename , int minimalNumberOfTokensToAccept, bool hasHeaders)
{
parseCsvFile(filename , minimalNumberOfTokensToAccept , hasHeaders);
}
CSVParser::~CSVParser(void)
{
data.clear();
}
void CSVParser::parseCsvFile(const std::string& filename , int minimalNumberOfTokensToAccept, bool hasHeaders)
{
std::string line;
std::ifstream fileToParse (filename);
data.clear();
if (fileToParse.is_open())
{
while ( fileToParse.good() )
{
std::getline(fileToParse,line);
PtrStringVector currVector(new std::vector<std::string>);
tokenizer<escaped_list_separator<char> > tk(line, escaped_list_separator<char>('\\', ',', '\"'));
int elementCount = 0 ;
for (tokenizer<escaped_list_separator<char> >::iterator i(tk.begin());i!=tk.end();++i)
{
currVector->push_back(*i);
elementCount++ ;
}
if(elementCount >= minimalNumberOfTokensToAccept)
{
if(!headers && hasHeaders)
{
headers = currVector;
}
else
{
data.push_back(currVector);
}
}
}
}
if(hasHeaders)
{
std::cout << "Headers parsed: " << headers << std::endl;
}
std::cout << "data rows read: " << data.size() << std::endl;
}
int CSVParser::getNumberOfColumns()
{
if(!headers)
{
return 0;
}
else
{
return headers->size();
}
}
const std::string& CSVParser::getHeaderOfIndex(int index)
{
return headers->at(index);
}
int CSVParser::getNumberOfLines()
{
return data.size();
}
PtrStringVector CSVParser::getDataAt(int index)
{
return data.at(index);
}