|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <cstring> |
| 5 | +#include <iostream> |
| 6 | +#include <streambuf> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +/// @brief Custom traits for uint8_t for usage in std template classes that use char_traits (e.g. std::basic_streambuf) |
| 10 | +template <> struct std::char_traits<uint8_t> { |
| 11 | + using char_type = uint8_t; |
| 12 | + using int_type = int; |
| 13 | + using off_type = std::streamoff; |
| 14 | + using pos_type = std::streampos; |
| 15 | + using state_type = std::mbstate_t; |
| 16 | + |
| 17 | + static void assign(char_type & c1, const char_type & c2) noexcept { c1 = c2; } |
| 18 | + |
| 19 | + static constexpr bool eq(char_type a, char_type b) noexcept { return a == b; } |
| 20 | + |
| 21 | + static constexpr bool lt(char_type a, char_type b) noexcept { return a < b; } |
| 22 | + |
| 23 | + static int compare(const char_type * s1, const char_type * s2, std::size_t n) { |
| 24 | + for (std::size_t i = 0; i < n; ++i) { |
| 25 | + if (lt(s1[i], s2[i])) { |
| 26 | + return -1; |
| 27 | + } |
| 28 | + if (lt(s2[i], s1[i])) { |
| 29 | + return 1; |
| 30 | + } |
| 31 | + } |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + static std::size_t length(const char_type * s) { |
| 36 | + std::size_t i = 0; |
| 37 | + while (!eq(s[i], char_type())) { |
| 38 | + ++i; |
| 39 | + } |
| 40 | + return i; |
| 41 | + } |
| 42 | + |
| 43 | + static const char_type * find(const char_type * s, std::size_t n, const char_type & c) { |
| 44 | + for (std::size_t i = 0; i < n; ++i) { |
| 45 | + if (eq(s[i], c)) { |
| 46 | + return s + i; |
| 47 | + } |
| 48 | + } |
| 49 | + return nullptr; |
| 50 | + } |
| 51 | + |
| 52 | + static char_type * move(char_type * s1, const char_type * s2, std::size_t n) { |
| 53 | + return static_cast<char_type *>(std::memmove(s1, s2, n)); |
| 54 | + } |
| 55 | + |
| 56 | + static char_type * copy(char_type * s1, const char_type * s2, std::size_t n) { |
| 57 | + return static_cast<char_type *>(std::memcpy(s1, s2, n)); |
| 58 | + } |
| 59 | + |
| 60 | + static char_type * assign(char_type * s, std::size_t n, char_type c) { |
| 61 | + for (std::size_t i = 0; i < n; ++i) { |
| 62 | + s[i] = c; |
| 63 | + } |
| 64 | + return s; |
| 65 | + } |
| 66 | + |
| 67 | + static constexpr int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? 0 : c; } |
| 68 | + |
| 69 | + static constexpr char_type to_char_type(int_type c) noexcept { |
| 70 | + return c >= 0 && c <= 255 ? static_cast<char_type>(c) : char_type(); |
| 71 | + } |
| 72 | + |
| 73 | + static constexpr int_type to_int_type(char_type c) noexcept { return static_cast<int_type>(c); } |
| 74 | + |
| 75 | + static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; } |
| 76 | + |
| 77 | + static constexpr int_type eof() noexcept { return static_cast<int_type>(-1); } |
| 78 | +}; |
| 79 | + |
| 80 | +/// @brief Custom streambuf for uint8_t |
| 81 | +class Uint8BufferStreamBuf : public std::basic_streambuf<uint8_t> { |
| 82 | + public: |
| 83 | + Uint8BufferStreamBuf(std::vector<uint8_t> && _data); |
| 84 | + |
| 85 | + protected: |
| 86 | + int_type underflow() override; |
| 87 | + |
| 88 | + /// @brief Efficient bulk reading. The standard implementation specifies that this function can be overridden |
| 89 | + /// to provide a more efficient implementation: sgetn will call this function if it is overridden. |
| 90 | + std::streamsize xsgetn(char_type * s, std::streamsize n) override; |
| 91 | + |
| 92 | + pos_type seekoff(off_type off, std::ios_base::seekdir dir, |
| 93 | + std::ios_base::openmode which = std::ios_base::in) override; |
| 94 | + |
| 95 | + pos_type seekpos(pos_type pos, std::ios_base::openmode which = std::ios_base::in) override; |
| 96 | + |
| 97 | + private: |
| 98 | + std::vector<uint8_t> data; |
| 99 | +}; |
0 commit comments