-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVReader.cpp
More file actions
132 lines (114 loc) · 3.04 KB
/
CSVReader.cpp
File metadata and controls
132 lines (114 loc) · 3.04 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <fstream>
#include "CSVReader.h"
#include "OrderBookEntry.h"
using namespace std;
CSVReader::CSVReader()
{
}
vector<OrderBookEntry> CSVReader::readCSV(string csvFilename)
{
vector<OrderBookEntry> entries;
ifstream csvFile{csvFilename};
string line;
if (csvFile.is_open())
{
while (getline(csvFile, line))
{
try
{
OrderBookEntry order = stringsToOBE(tokenise(line, ','));
entries.push_back(order);
}
catch (const exception &e)
{
cout << "CSVReader::readCSV error: Could not read line from file." << endl;
cout << "Line: " << line << endl;
}
}
csvFile.close();
}
else
{
cout << "CSVReader::readCSV error: Could not open file " << csvFilename << endl;
throw exception();
}
cout << "CSVReader::readCSV read " << entries.size() << " entries from " << csvFilename << endl;
return entries;
}
vector<string> CSVReader::tokenise(string csvLine, char seperator)
{
vector<string> tokens;
long unsigned int start, end;
string token;
start = csvLine.find_first_not_of(seperator, 0);
do
{
end = csvLine.find_first_of(seperator, start);
if (start == csvLine.length() || start == end)
break;
if (end >= 0)
token = csvLine.substr(start, end - start);
else
token = csvLine.substr(start, csvLine.length() - start);
tokens.push_back(token);
start = end + 1;
} while (end != string::npos);
return tokens;
}
OrderBookEntry CSVReader::stringsToOBE(vector<string> tokens)
{
if (tokens.size() != 5)
{
cout << "CSVReader::stringsToOBE error: Expected 5 tokens per line - found " << tokens.size() << endl;
throw exception();
}
double price, amount;
try
{
price = stod(tokens[3]);
amount = stod(tokens[4]);
}
catch (const exception &e)
{
cout << "CSVReader::stringsToOBE error: Could not convert price or amount to double." << endl;
cout << "Price: " << tokens[3] << endl;
cout << "Amount: " << tokens[4] << endl;
throw;
}
OrderBookEntry order{
price,
amount,
tokens[0],
tokens[1],
OrderBookEntry::stringToOrderBookType(tokens[2])};
return order;
}
OrderBookEntry CSVReader::stringsToOBE(
string price,
string amount,
string timestamp,
string product,
OrderBookType orderType)
{
double _price, _amount;
try
{
_price = stod(price);
_amount = stod(amount);
}
catch (const exception &e)
{
cout << "CSVReader::stringsToOBE error: Could not convert price or amount to double." << endl;
cout << "Price: " << price << endl;
cout << "Amount: " << amount << endl;
throw;
}
OrderBookEntry order{
_price,
_amount,
timestamp,
product,
orderType};
return order;
}