blob: 6f02fa1402e94b755ad80865f786bdee019742fb (
plain)
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
|
/*
* File: fix/fix.hpp
* Purpose: Financial Information Exchange parser in C++
* Author: Amlal El Mahrouss (amlal@nekernel.org)
* Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License.
*/
#ifndef _OCL_FIX_PARSER_HPP
#define _OCL_FIX_PARSER_HPP
#include <cstddef>
#include <cassert>
#include <utility>
#include <string>
#include <vector>
#include <cstdint>
#include <sys/types.h>
#include <unistd.h>
namespace ocl::fix
{
template <typename char_type>
class basic_visitor;
template <typename char_type>
struct basic_range;
template <typename char_type>
class basic_range_data;
/// @brief Buffer+Length structure
template <typename char_type = char>
using range_ptr_t = basic_range<char_type>;
namespace detail
{
template <typename char_type = char>
const char_type* begin_fix();
template <>
inline const char* begin_fix<char>()
{
return "FIX.4.2";
}
template <>
inline const char16_t* begin_fix<char16_t>()
{
return u"FIX.4.2";
}
template <>
inline const char8_t* begin_fix<char8_t>()
{
return u8"FIX.4.2";
}
} // namespace detail
template <typename char_type = char>
struct basic_range final
{
char_type* bytes_{nullptr};
size_t length_{};
bool is_valid() noexcept
{
return this->bytes_ && this->length_ > 0;
}
explicit operator bool()
{
return this->is_valid();
}
};
/// @brief Convert basic_range to usable string.
/// @note This function assumes that the basic_range is valid and contains ASCII bytes.
template <typename char_type = char>
inline std::basic_string<char_type> to_string(basic_range<char_type>& basic_range) noexcept
{
if (basic_range.length_ < 0)
return std::basic_string<char_type>{};
return std::basic_string<char_type>(basic_range.ascii_bytes_, basic_range.length_);
}
/// @brief a basic_range object containing the FIX packet values.
template <typename char_type = char>
class basic_range_data final
{
public:
std::size_t magic_len_{};
std::basic_string<char_type> magic_{};
std::size_t body_len_{};
std::vector<std::pair<std::basic_string<char_type>, std::basic_string<char_type>>> body_{};
static inline const char_type* begin = detail::begin_fix<char_type>();
explicit basic_range_data() = default;
~basic_range_data() = default;
basic_range_data& operator=(const basic_range_data&) = default;
basic_range_data(const basic_range_data&) = default;
std::basic_string<char_type> operator[](const std::basic_string<char_type>& key)
{
if (key.empty())
{
return std::basic_string<char_type>{};
}
for (const auto& pair : this->body_)
{
if (pair.first == key)
{
return pair.second;
}
}
return std::basic_string<char_type>{};
}
bool is_valid()
{
return magic_.starts_with(basic_range_data<char_type>::begin);
}
explicit operator bool()
{
return this->is_valid();
}
};
/// @brief basic_visitor object which returns a fix::basic_range_data instance.
template <typename char_type = char>
class basic_visitor final
{
public:
static constexpr const char_type soh = '|';
static constexpr const char_type eq = '=';
static constexpr uint32_t base = 10U;
explicit basic_visitor() = default;
~basic_visitor() = default;
basic_visitor& operator=(const basic_visitor&) = default;
basic_visitor(const basic_visitor&) = default;
basic_range<char_type> operator()(const std::basic_string<char_type>& in)
{
return this->visit(in);
}
basic_range_data<char_type> visit(const std::basic_string<char_type>& in)
{
thread_local basic_range_data<char_type> ret{};
if (in.empty())
return ret;
static thread_local std::basic_string<char_type> in_tmp{};
in_tmp.reserve(in.size());
try
{
for (auto& ch : in)
{
if (ch != basic_visitor::soh)
{
in_tmp += ch;
continue;
}
std::basic_string<char_type> key = in_tmp.substr(0, in_tmp.find(basic_visitor::eq));
std::basic_string<char_type> val = in_tmp.substr(in_tmp.find(basic_visitor::eq) + 1);
if (ret.magic_.empty())
{
ret.magic_ = val;
ret.magic_len_ = ret.magic_.size();
}
else
{
ret.body_.emplace_back(std::make_pair(key, val));
ret.body_len_ += in_tmp.size();
}
in_tmp.clear();
}
}
catch (...)
{
in_tmp.clear();
return ret;
}
in_tmp.clear();
return ret;
}
};
template <typename char_type = char, typename error_handler>
inline void must_pass(basic_range_data<char_type>& basic_range, error_handler& handler)
{
if (!basic_range.is_valid())
{
handler.template error<true>("Invalid FIX packet.");
}
}
using fix_tag_type = std::uint32_t;
using range_data = basic_range_data<char>;
using visitor = basic_visitor<char>;
} // namespace ocl::fix
#endif // ifndef _OCL_FIX_PARSER_HPP
|