-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVReader.cpp
More file actions
143 lines (109 loc) · 3.61 KB
/
CSVReader.cpp
File metadata and controls
143 lines (109 loc) · 3.61 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
133
134
135
136
137
138
139
140
141
142
143
#include "CSVReader.h"
#include <fstream>
#include <iostream>
/** Constructor*/
CSVReader::CSVReader()
{
}
/** Read CSV given a filename*/
std::vector<OrderBookEntry> CSVReader::readCSV(std::string csvFilename)
{
std::vector<OrderBookEntry> entries;
std::ifstream csvFile{csvFilename};
std::string line;
if (csvFile.is_open())
{
while(std::getline(csvFile, line))
{
try
{
// create an order book entry from the tokenised input string
OrderBookEntry obe = stringsToOBE(tokenise(line, ','));
entries.push_back(obe);
}
catch(const std::exception& e){
std::cout << "CSVReader::readCSV Bad Data" <<std::endl;
}
}
}
std::cout << "CSVReader:: readCSV read "<< entries.size() << " entries" <<std::endl;
return entries;
}
/** Split a string based on a given separator.
* Used quite frequently throughout the code.
*/
std::vector<std::string> CSVReader::tokenise(std::string csvLine, char separator)
{
// stores the tokens
std::vector<std::string> tokens;
// delineate position
signed int start, end;
std::string token;
// first char
start = csvLine.find_first_not_of(separator, 0);
do {
// goes to first separator
end = csvLine.find_first_of(separator, start);
// checks that start and end can actually make a word/string
if (start == csvLine.length() || start == end) break;
// get the substring between separator/start/end for a token
if (end >= 0) token = csvLine.substr(start, end - start);
// if end has circled into negative
else token = csvLine.substr(start, csvLine.length() - start);
tokens.push_back(token);
start = end+1;
}
while(end != std::string::npos);
return tokens;
}
/**Converts a bunch of strings into an order book entry*/
OrderBookEntry CSVReader::stringsToOBE(std::string priceString,
std::string amountString,
std::string timeStamp,
std::string product,
OrderBookType type)
{
double price, amount;
// numbers into doubles conversion
try {
price = std::stod(priceString);
amount = std::stod(amountString);
std::cout << price << ":" << amount << std::endl;
}catch(const std::exception& e){
std::cout << "CSVReader::stringsToOBE Bad float " << priceString<< ":"<<amountString << std::endl;
throw;
}
// create the order book entry
OrderBookEntry obe{price,
amount,
timeStamp,
product,
type};
return obe;
}
/** Overloading the function.
* Takes a vector of string tokens.
*/
OrderBookEntry CSVReader::stringsToOBE(std::vector<std::string> tokens)
{
double price, amount;
if (tokens.size() != 5) // bad
{
throw std::exception{};
}
// we have 5 tokens
try {
price = std::stod(tokens[3]);
amount = std::stod(tokens[4]);
std::cout << price << ":" << amount << std::endl;
}catch(const std::exception& e){
std::cout << "CSVReader::stringsToOBE Bad float " << tokens[3]<< ":"<<tokens[4] << std::endl;
throw;
}
OrderBookEntry obe{price,
amount,
tokens[0],
tokens[1],
OrderBookEntry::strToOBT(tokens[2])};
return obe;
}