-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.h
More file actions
231 lines (203 loc) · 5.14 KB
/
Copy pathutils.h
File metadata and controls
231 lines (203 loc) · 5.14 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* utils.h
*
* Created on: Jun 18, 2014
* Author: busygin
*/
#ifndef UTILS_H_
#define UTILS_H_
#include <cstdint>
#include <cctype>
#include <exception>
#include <string>
#include <boost/lexical_cast.hpp>
namespace FIX {
template<typename Handler, typename Msg>
class OnFirstMDEntryFunctor {
Handler& handler_;
const Msg& msg_;
public:
OnFirstMDEntryFunctor(Handler& handler, const Msg& msg) : handler_(handler), msg_(msg) { }
inline bool operator()() { return handler_.on_first_MDEntry(msg_); }
};
class UnexpectedHeader: public std::exception {
public:
UnexpectedHeader(size_t tag) : s("Header starts with unexpected tag ") {
s += boost::lexical_cast<std::string>(tag);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class InvalidFieldTagDelimiter: public std::exception {
public:
InvalidFieldTagDelimiter(size_t tag) : s("Invalid delimiter after tag ") {
s += boost::lexical_cast<std::string>(tag);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class UnexpectedTag: public std::exception {
public:
UnexpectedTag(size_t tag) : s("Unexpected tag encountered: ") {
s += boost::lexical_cast<std::string>(tag);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class DuplicateTag: public std::exception {
public:
DuplicateTag(size_t tag) : s("Duplicate tag ") {
s += boost::lexical_cast<std::string>(tag);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class InvalidChksum: public std::exception {
public:
InvalidChksum(uint16_t s_expected, uint16_t s_received) : s("Invalid checksum: expected ") {
s += boost::lexical_cast<std::string>(s_expected) + " received " + boost::lexical_cast<std::string>(s_received);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class UnexpectedEndOfMessageBuffer: public std::exception {
private:
virtual const char* what() const noexcept {
return "Unexpected end of message buffer";
}
};
class UnknownMessageType: public std::exception {
private:
virtual const char* what() const noexcept {
return "Unknown message type";
}
};
class UnknownLength: public std::exception {
public:
UnknownLength(const char* field) : s("Unknown length for field ") {
s += field;
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class MissingFastTemplateID: public std::exception {
private:
virtual const char* what() const noexcept {
return "Missing FAST template ID";
}
};
class MandatoryFastFieldAbsent: public std::exception {
public:
MandatoryFastFieldAbsent(size_t tag) : s("Mandatory field is absent in FAST message: ") {
s += boost::lexical_cast<std::string>(tag);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class MandatoryFastFieldNull: public std::exception {
public:
MandatoryFastFieldNull(size_t tag) : s("Mandatory field is null in FAST message: ") {
s += boost::lexical_cast<std::string>(tag);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
class MissingPreviousValueInFastDeltaField: public std::exception {
public:
MissingPreviousValueInFastDeltaField(size_t tag) : s("Missing previous value for FAST delta field: ") {
s += boost::lexical_cast<std::string>(tag);
}
private:
std::string s;
virtual const char* what() const noexcept {
return s.c_str();
}
};
inline void strreverse(char* begin, char* end) {
char tmp;
while(--end>begin) {
tmp = *end;
*end = *begin;
*begin++ = tmp;
}
}
inline void signed_itoa(char* &buf, int64_t value) {
char* p(buf);
int64_t tmp_value;
do {
tmp_value = value;
value /= 10;
*p++ = "9876543210123456789"[9 + (tmp_value - value * 10)];
} while(value);
if (tmp_value < 0)
*p++ = '-';
char* p1(buf);
buf = p;
strreverse(p1, p);
}
inline void unsigned_itoa(char* &buf, uint64_t value) {
char* p(buf);
uint64_t tmp_value;
do {
tmp_value = value;
value /= 10;
*p++ = '0' + static_cast<char>(tmp_value - value * 10);
} while(value);
char* p1(buf);
buf = p;
strreverse(p1, p);
}
inline uint8_t msg_chksum(const char* buf, const char* end) {
uint8_t result = 0;
for(; buf<end; ++buf) result += static_cast<uint8_t>(*buf);
return result;
}
inline size_t scan_tag(const char* &buf, const char* end) {
// chksum = '='; to speed up by one op, as we know '=' will be after tag
size_t result = 0;
while (buf<end && isdigit(*buf)) {
// chksum += static_cast<unsigned char>(*buf);
result = result*10 + static_cast<size_t>(*(buf++) - '0');
}
if ( *buf != '=' )
throw InvalidFieldTagDelimiter(result);
++buf;
#ifdef BUFFER_RANGE_CHECK
if (buf>=end)
throw UnexpectedEndOfMessageBuffer();
#endif
return result;
}
template<typename RecordType>
inline void scan(RecordType& r, size_t& tag, const char* &buf, const char* end) {
while (r.scan_field(tag, buf, end)) {
if (buf==end) break;
tag = scan_tag(buf, end);
}
}
}
#endif /* UTILS_H_ */