-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (142 loc) · 4.87 KB
/
Copy pathmain.cpp
File metadata and controls
175 lines (142 loc) · 4.87 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "src/matchEngine.h"
#include <charconv>
#include <cstdint>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
namespace {
bool parseUint64(std::string_view token, uint64_t& value) {
const char* begin = token.data();
const char* end = begin + token.size();
auto [ptr, ec] = std::from_chars(begin, end, value);
return ec == std::errc{} && ptr == end;
}
bool parseQty(std::string_view token, Qty& qty) {
uint64_t value = 0;
if (!parseUint64(token, value) || value > UINT16_MAX) {
return false;
}
qty = static_cast<Qty>(value);
return true;
}
bool parseSide(const std::string& token, Side& side) {
if (token == "BUY") {
side = Side::Bid;
return true;
}
if (token == "SELL") {
side = Side::Ask;
return true;
}
return false;
}
bool parsePriceType(const std::string& token, PriceType& priceType) {
if (token == "LIMIT") {
priceType = PriceType::Limit;
return true;
}
if (token == "MARKET") {
priceType = PriceType::Market;
return true;
}
return false;
}
bool parseOrderType(const std::string& token, OrderType& orderType) {
if (token == "GTC") {
orderType = OrderType::GTC;
return true;
}
if (token == "IOC") {
orderType = OrderType::IOC;
return true;
}
return false;
}
void printUsage(std::ostream& out) {
out << "Commands:\n"
<< " BUY <LIMIT|MARKET> <GTC|IOC> <price> <qty> <order_id>\n"
<< " SELL <LIMIT|MARKET> <GTC|IOC> <price> <qty> <order_id>\n"
<< " CANCEL <order_id>\n"
<< " MODIFY <order_id> <new_price> <new_qty>\n";
}
} // namespace
int main() {
MatchEngine engine;
std::string line;
uint64_t lineNumber = 0;
while (std::getline(std::cin, line)) {
++lineNumber;
if (line.empty() || line.front() == '#') {
continue;
}
std::istringstream input(line);
std::string command;
input >> command;
if (command == "BUY" || command == "SELL") {
std::string priceTypeToken;
std::string orderTypeToken;
std::string priceToken;
std::string qtyToken;
std::string orderIdToken;
if (!(input >> priceTypeToken >> orderTypeToken >> priceToken >> qtyToken >> orderIdToken)) {
std::cout << "ERROR line " << lineNumber << ": invalid order command\n";
continue;
}
Side side = Side::Bid;
PriceType priceType = PriceType::Limit;
OrderType orderType = OrderType::GTC;
Price price = 0;
Qty qty = 0;
OrderId orderId = 0;
if (!parseSide(command, side) ||
!parsePriceType(priceTypeToken, priceType) ||
!parseOrderType(orderTypeToken, orderType) ||
!parseUint64(priceToken, price) ||
!parseQty(qtyToken, qty) ||
!parseUint64(orderIdToken, orderId)) {
std::cout << "ERROR line " << lineNumber << ": invalid order values\n";
continue;
}
engine.addOrder(Order{orderId, side, priceType, orderType, price, qty});
std::cout << "SUBMITTED " << command << " " << orderId << "\n";
} else if (command == "CANCEL") {
std::string orderIdToken;
if (!(input >> orderIdToken)) {
std::cout << "ERROR line " << lineNumber << ": invalid cancel command\n";
continue;
}
OrderId orderId = 0;
if (!parseUint64(orderIdToken, orderId)) {
std::cout << "ERROR line " << lineNumber << ": invalid order id\n";
continue;
}
engine.cancelOrder(orderId);
std::cout << "CANCELLED " << orderId << "\n";
} else if (command == "MODIFY") {
std::string orderIdToken;
std::string priceToken;
std::string qtyToken;
if (!(input >> orderIdToken >> priceToken >> qtyToken)) {
std::cout << "ERROR line " << lineNumber << ": invalid modify command\n";
continue;
}
OrderId orderId = 0;
Price price = 0;
Qty qty = 0;
if (!parseUint64(orderIdToken, orderId) ||
!parseUint64(priceToken, price) ||
!parseQty(qtyToken, qty)) {
std::cout << "ERROR line " << lineNumber << ": invalid modify values\n";
continue;
}
engine.modifyOrder(orderId, price, qty);
std::cout << "MODIFIED " << orderId << "\n";
} else if (command == "HELP") {
printUsage(std::cout);
} else {
std::cout << "ERROR line " << lineNumber << ": unknown command\n";
}
}
return 0;
}