blob: 17fb63cf6724c930299c6fa51c69d4736d2baaa3 (
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
|
/*
* File: fix/parser.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 <core/config.hpp>
#include <hashing/crc_hash.hpp>
#include <io/print.hpp>
#include <string>
namespace ocl::fix
{
class basic_visitor;
struct basic_range;
class basic_range_data;
/// @brief Buffer+Length structure
using range_ptr_t = basic_range*;
namespace detail
{
inline const char* begin_fix() noexcept
{
return "FIX.4.2";
}
} // namespace detail
struct basic_range final
{
char* 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.
inline std::string to_string(basic_range& basic_range) noexcept
{
if (basic_range.length_ < 1)
return std::string{};
return std::string(basic_range.bytes_, basic_range.length_);
}
/// @brief a basic_range object containing the FIX packet values.
class basic_range_data final
{
public:
std::size_t magic_len_{};
std::string magic_{};
string_hash_map<std::string> message_{};
static inline const char* begin = detail::begin_fix();
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::string operator[](const std::string& key)
{
if (key.empty())
return std::string{};
auto it = message_.find(key);
if (it != message_.end())
return it->second;
return std::string{};
}
bool is_valid()
{
return this->operator[]("8").empty() == false;
}
explicit operator bool()
{
return this->is_valid();
}
};
/// @brief basic_visitor object which returns a fix::basic_range_data instance.
class basic_visitor final
{
public:
/// AMLALE: Yeah...
static constexpr const int soh = '\x01';
static constexpr const char 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_data operator()(const std::string& in)
{
return this->visit(in);
}
/// @brief Visit a FIX message and parse it into a basic_range_data object.
/// @param in The input FIX message as a string.
/// @warning This function may throw exceptions.
basic_range_data visit(const std::string& in)
{
basic_range_data ret{};
if (in.empty())
return ret;
std::string key;
std::size_t off = 0UL;
while (off < in.size())
{
// Find the '=' separator
std::size_t eq_pos = in.find(eq, off);
if (eq_pos == std::string::npos)
break;
// Extract tag (everything from current position to '=')
std::string tag = in.substr(off, eq_pos - off);
// Find the SOH delimiter after the value (or end of string)
std::size_t soh_pos = in.find(soh, eq_pos + 1);
if (soh_pos == std::string::npos)
soh_pos = in.size();
// Extract value (everything from after '=' to SOH)
std::string value = in.substr(eq_pos + 1, soh_pos - eq_pos - 1);
// Store first value as magic (should be BeginString)
if (ret.magic_.empty())
{
ret.magic_ = value;
ret.magic_len_ = value.size();
}
// Store tag-value pair
ret.message_[tag] = value;
// Move to position after the SOH
off = soh_pos + 1;
}
return ret;
}
};
using fix_tag_type = std::string;
using range_data = basic_range_data;
using visitor = basic_visitor;
} // namespace ocl::fix
#endif // ifndef _OCL_FIX_PARSER_HPP
|