Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
stream.h
1#ifndef OE_IO_STREAM_H
2#define OE_IO_STREAM_H
3
4#include "../util/byte_array.h"
5#include "../util/endian.h"
6
7#include <string>
8#include <vector>
9
10namespace oe::io
11{
12 // Todo: Use deque instead of ByteArray (to allow left-trimming and appending data on the fly)
13 class Stream
14 {
15 public:
16 Stream(const ByteArray& data);
17
18 size_t read(ByteArray& buffer, const size_t& len);
19 size_t write(ByteArray& buffer, const size_t& len);
20
21 bool eof();
22 bool seek(const size_t& pos);
23 size_t tell();
24 size_t size();
25
29 void align(size_t alignment);
30
34 std::string readStringToNull(const size_t& maxLength = 32767);
35
39 std::string readString(const size_t& length);
40
44 template <typename T>
45 T readValue(bool little_endian = true)
46 {
47 ByteArray data;
48 data.reserve(sizeof(T));
49
50 read(data, sizeof(T));
51
52 T result = oe::util::convertFromByteArray<T>(data);
53
54 if (little_endian)
55 return oe::util::endian::convertLittleEndianToHost(result);
56 else
57 return oe::util::endian::convertBigEndianToHost(result);
58 }
59
60 //template <typename T> bool writeValue(const T value, bool little_endian = true);
61
65 void trim()
66 {
67 _data.erase(_data.begin(), _cursor);
68 }
69
75 void appendData(const ByteArray& data)
76 {
77 _data.insert(end(_data), begin(data), end(data));
78 }
79
80 const ByteArray getData() const
81 {
82 return _data;
83 }
84
85 /*template <typename T> static Stream buildFromVector(const std::vector<T>& input)
86 {
87 Stream result;
88
89 for (auto& it : input)
90 {
91 result.writeValue<T>(it);
92 }
93
94 return result;
95 }*/
96 private:
97 ByteArray _data;
98 ByteArray::iterator _cursor;
99 };
100
101 template<> bool Stream::readValue<>(bool little_endian);
102 template<> uint8_t Stream::readValue<>(bool little_endian);
103 template<> int8_t Stream::readValue<>(bool little_endian);
104
105 /*template<> bool Stream::writeValue<>(const bool value, bool little_endian);
106 template<> bool Stream::writeValue<>(const uint8_t value, bool little_endian);
107 template<> bool Stream::writeValue<>(const int8_t value, bool little_endian);*/
108}
109
110#endif
Definition stream.h:14
std::string readStringToNull(const size_t &maxLength=32767)
Read a string until either the null character '\0' is found or up to the maximum length.
std::string readString(const size_t &length)
Read a string of specified length.
void align(size_t alignment)
Move stream position to be aligned.
void trim()
Remove all data that are already already read.
Definition stream.h:65
void appendData(const ByteArray &data)
Add additional data at the end of the stream.
Definition stream.h:75
T readValue(bool little_endian=true)
read a value from the stream and move the cursor
Definition stream.h:45
Input/Output abstractions (Filesystem, Network, ...)
Definition file.h:10