From b189449b28625cec7b6ce399f999072e50efae45 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 24 Nov 2025 23:02:29 -0500 Subject: feat: expanding FIX module. Signed-off-by: Amlal El Mahrouss --- include/ocl/core/config.hpp | 9 ++ include/ocl/fix/checksum.hpp | 10 ++ include/ocl/fix/fix.hpp | 212 ------------------------------------------- include/ocl/fix/parser.hpp | 205 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 212 deletions(-) create mode 100644 include/ocl/fix/checksum.hpp delete mode 100644 include/ocl/fix/fix.hpp create mode 100644 include/ocl/fix/parser.hpp (limited to 'include') diff --git a/include/ocl/core/config.hpp b/include/ocl/core/config.hpp index de1ce76..59f2491 100644 --- a/include/ocl/core/config.hpp +++ b/include/ocl/core/config.hpp @@ -14,6 +14,15 @@ #include #include +#include +#include +#include +#include +#include +#include +#include +#include + #ifdef __cplusplus /// DLL/Dylib/So specific macro. # define OCL_EXPORT_DECL extern "C" BOOST_SYMBOL_EXPORT diff --git a/include/ocl/fix/checksum.hpp b/include/ocl/fix/checksum.hpp new file mode 100644 index 0000000..9205664 --- /dev/null +++ b/include/ocl/fix/checksum.hpp @@ -0,0 +1,10 @@ +/* + * File: fix/checksum.hpp + * Purpose: Financial Information Exchange checksum in C++ + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License. + */ + +#pragma once + +#include diff --git a/include/ocl/fix/fix.hpp b/include/ocl/fix/fix.hpp deleted file mode 100644 index 85a8b70..0000000 --- a/include/ocl/fix/fix.hpp +++ /dev/null @@ -1,212 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include -#include -#include - -namespace ocl::fix -{ - template - class basic_visitor; - - template - struct basic_range; - - template - class basic_range_data; - - /// @brief Buffer+Length structure - template - using range_ptr_t = basic_range; - - namespace detail - { - template - const char_type* begin_fix() noexcept; - - template <> - inline const char* begin_fix() noexcept - { - return "FIX.4.2"; - } - - template <> - inline const char16_t* begin_fix() noexcept - { - return u"FIX.4.2"; - } - - template <> - inline const char8_t* begin_fix() noexcept - { - return u8"FIX.4.2"; - } - } // namespace detail - - template - 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 - inline std::basic_string to_string(basic_range& basic_range) noexcept - { - if (basic_range.length_ < 0) - return std::basic_string{}; - - return std::basic_string(basic_range.ascii_bytes_, basic_range.length_); - } - - /// @brief a basic_range object containing the FIX packet values. - template - class basic_range_data final - { - public: - std::size_t magic_len_{}; - std::basic_string magic_{}; - std::size_t body_len_{}; - std::vector, std::basic_string>> body_{}; - - static inline const char_type* 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::basic_string operator[](const std::basic_string& key) - { - if (key.empty()) - { - return std::basic_string{}; - } - - for (const auto& pair : this->body_) - { - if (pair.first == key) - { - return pair.second; - } - } - - return std::basic_string{}; - } - - bool is_valid() - { - return magic_.starts_with(basic_range_data::begin); - } - - explicit operator bool() - { - return this->is_valid(); - } - }; - - /// @brief basic_visitor object which returns a fix::basic_range_data instance. - template - class basic_visitor final - { - public: - /// AMLALE: Yeah... - static constexpr const char_type soh = 0x01; - 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 operator()(const std::basic_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::basic_string& in) - { - thread_local basic_range_data ret{}; - - if (in.empty()) - return ret; - - std::basic_string in_tmp{"", in.size()}; - - for (auto& ch : in) - { - if (ch != basic_visitor::soh) - { - in_tmp += ch; - continue; - } - - std::basic_string key = in_tmp.substr(0, in_tmp.find(basic_visitor::eq)); - std::basic_string 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(); - } - - in_tmp.clear(); - return ret; - } - }; - - template - inline void must_pass(basic_range_data& basic_range, error_handler& handler) - { - if (!basic_range.is_valid()) - { - handler.template error("Invalid FIX Message."); - } - } - - using fix_tag_type = std::uint32_t; - - using range_data = basic_range_data; - using visitor = basic_visitor; -} // namespace ocl::fix - -#endif // ifndef _OCL_FIX_PARSER_HPP \ No newline at end of file diff --git a/include/ocl/fix/parser.hpp b/include/ocl/fix/parser.hpp new file mode 100644 index 0000000..a513a1f --- /dev/null +++ b/include/ocl/fix/parser.hpp @@ -0,0 +1,205 @@ +/* + * 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 + +namespace ocl::fix +{ + template + class basic_visitor; + + template + struct basic_range; + + template + class basic_range_data; + + /// @brief Buffer+Length structure + template + using range_ptr_t = basic_range; + + namespace detail + { + template + const char_type* begin_fix() noexcept; + + template <> + inline const char* begin_fix() noexcept + { + return "FIX.4.2"; + } + + template <> + inline const char16_t* begin_fix() noexcept + { + return u"FIX.4.2"; + } + + template <> + inline const char8_t* begin_fix() noexcept + { + return u8"FIX.4.2"; + } + } // namespace detail + + template + 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 + inline std::basic_string to_string(basic_range& basic_range) noexcept + { + if (basic_range.length_ < 0) + return std::basic_string{}; + + return std::basic_string(basic_range.ascii_bytes_, basic_range.length_); + } + + /// @brief a basic_range object containing the FIX packet values. + template + class basic_range_data final + { + public: + std::size_t magic_len_{}; + std::basic_string magic_{}; + std::size_t body_len_{}; + std::vector, std::basic_string>> body_{}; + + static inline const char_type* 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::basic_string operator[](const std::basic_string& key) + { + if (key.empty()) + { + return std::basic_string{}; + } + + for (const auto& pair : this->body_) + { + if (pair.first == key) + { + return pair.second; + } + } + + return std::basic_string{}; + } + + bool is_valid() + { + return magic_.starts_with(basic_range_data::begin); + } + + explicit operator bool() + { + return this->is_valid(); + } + }; + + /// @brief basic_visitor object which returns a fix::basic_range_data instance. + template + class basic_visitor final + { + public: + /// AMLALE: Yeah... + static constexpr const char_type soh = 0x01; + 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 operator()(const std::basic_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::basic_string& in) + { + thread_local basic_range_data ret{}; + + if (in.empty()) + return ret; + + std::basic_string in_tmp{"", in.size()}; + + for (auto& ch : in) + { + if (ch != basic_visitor::soh) + { + in_tmp += ch; + continue; + } + + std::basic_string key = in_tmp.substr(0, in_tmp.find(basic_visitor::eq)); + std::basic_string 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(); + } + + in_tmp.clear(); + return ret; + } + }; + + template + inline void must_pass(basic_range_data& basic_range, error_handler& handler) + { + if (!basic_range.is_valid()) + { + handler.template error("Invalid FIX Message."); + } + } + + using fix_tag_type = std::uint32_t; + + using range_data = basic_range_data; + using visitor = basic_visitor; +} // namespace ocl::fix + +#endif // ifndef _OCL_FIX_PARSER_HPP \ No newline at end of file -- cgit v1.2.3