| 1 |
/* |
| 2 |
* Copyright (C) 2018-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include <deque> |
| 29 |
#include <vector> |
| 30 |
#include <cstdint> |
| 31 |
#include <string> |
| 32 |
|
| 33 |
namespace io |
| 34 |
{ |
| 35 |
class BinaryContainer |
| 36 |
{ |
| 37 |
public: |
| 38 |
using container_type = std::deque<uint8_t>; |
| 39 |
using value_type = container_type::value_type; |
| 40 |
using size_type = container_type::size_type; |
| 41 |
using iterator = container_type::iterator; |
| 42 |
using const_iterator = container_type::const_iterator; |
| 43 |
using reverse_iterator = container_type::reverse_iterator; |
| 44 |
using const_reverse_iterator = container_type::const_reverse_iterator; |
| 45 |
|
| 46 |
explicit BinaryContainer(); |
| 47 |
explicit BinaryContainer(const std::vector<uint8_t>& buf); |
| 48 |
explicit BinaryContainer(std::vector<uint8_t>&& buf); |
| 49 |
|
| 50 |
inline iterator begin() noexcept { return buf_.begin(); } |
| 51 |
inline const_iterator begin() const noexcept { return buf_.begin(); } |
| 52 |
|
| 53 |
inline iterator end() noexcept { return buf_.end(); } |
| 54 |
inline const_iterator end() const noexcept { return buf_.end(); } |
| 55 |
|
| 56 |
inline const_iterator cbegin() const noexcept { return buf_.cbegin(); } |
| 57 |
inline const_iterator cend() const noexcept { return buf_.cend(); } |
| 58 |
|
| 59 |
inline reverse_iterator rbegin() noexcept { return buf_.rbegin(); } |
| 60 |
inline const_reverse_iterator rbegin() const noexcept { return buf_.rbegin(); } |
| 61 |
|
| 62 |
inline reverse_iterator rend() noexcept { return buf_.rend(); } |
| 63 |
inline const_reverse_iterator rend() const noexcept { return buf_.rend(); } |
| 64 |
|
| 65 |
inline const_reverse_iterator crbegin() const noexcept { return buf_.crbegin(); } |
| 66 |
inline const_reverse_iterator crend() const noexcept { return buf_.crend(); } |
| 67 |
|
| 68 |
inline void push_back(uint8_t v) { appendUint8(v); } |
| 69 |
|
| 70 |
inline size_type size() const noexcept { return buf_.size(); } |
| 71 |
void clear(); |
| 72 |
inline void resize(size_type size) { buf_.resize(size); } |
| 73 |
|
| 74 |
inline void setEndian(bool isLittleEndian) noexcept { isLE_ = isLittleEndian; } |
| 75 |
inline bool isLittleEndian() const noexcept { return isLE_; } |
| 76 |
|
| 77 |
void appendInt8(int8_t v); |
| 78 |
void appendUint8(uint8_t v); |
| 79 |
void appendInt16(int16_t v); |
| 80 |
void appendUint16(uint16_t v); |
| 81 |
void appendInt32(int32_t v); |
| 82 |
void appendUint32(uint32_t v); |
| 83 |
void appendChar(char c); |
| 84 |
void appendString(const std::string& str); |
| 85 |
void appendArray(const uint8_t* array, size_type size); |
| 86 |
void appendVector(const std::vector<uint8_t>& vec); |
| 87 |
void appendVector(std::vector<uint8_t>&& vec); |
| 88 |
void appendBinaryContainer(const BinaryContainer& bc); |
| 89 |
void appendBinaryContainer(BinaryContainer&& bc); |
| 90 |
|
| 91 |
void writeInt8(size_type offset, int8_t v); |
| 92 |
void writeUint8(size_type offset, uint8_t v); |
| 93 |
void writeInt16(size_type offset, int16_t v); |
| 94 |
void writeUint16(size_type offset, uint16_t v); |
| 95 |
void writeInt32(size_type offset, int32_t v); |
| 96 |
void writeUint32(size_type offset, uint32_t v); |
| 97 |
void writeChar(size_type offset, char c); |
| 98 |
void writeString(size_type offset, const std::string& str); |
| 99 |
|
| 100 |
int8_t readInt8(size_type offset) const; |
| 101 |
uint8_t readUint8(size_type offset) const; |
| 102 |
int16_t readInt16(size_type offset) const; |
| 103 |
uint16_t readUint16(size_type offset) const; |
| 104 |
int32_t readInt32(size_type offset) const; |
| 105 |
uint32_t readUint32(size_type offset) const; |
| 106 |
char readChar(size_type offset) const; |
| 107 |
std::string readString(size_type offset, size_type length) const; |
| 108 |
|
| 109 |
BinaryContainer getSubcontainer(size_type offset, size_type length) const; |
| 110 |
|
| 111 |
std::vector<uint8_t> toVector() const; |
| 112 |
|
| 113 |
private: |
| 114 |
container_type buf_; |
| 115 |
bool isLE_; |
| 116 |
|
| 117 |
void append(const std::vector<uint8_t>&& a); |
| 118 |
void write(size_t offset, const std::vector<uint8_t>&& a); |
| 119 |
std::vector<uint8_t> read(size_type offset, size_type size) const; |
| 120 |
}; |
| 121 |
} |