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 "../common.h"
5
6#include "../util/byte_array.h"
7#include "../util/endian.h"
8
9#include <string>
10#include <vector>
11
12namespace oe::io
13{
14 // Todo: Use deque instead of ByteArray (to allow left-trimming and appending data on the fly)
15 class Stream
16 {
17 public:
18 Stream(const ByteArray& data);
19
20 size_t read(ByteArray& buffer, const size_t& len);
21 size_t write(ByteArray& buffer, const size_t& len);
22
23 bool eof();
24 bool seek(const size_t& pos);
25 size_t tell();
26 size_t size();
27
31 void align(size_t alignment);
32
36 std::string readStringToNull(const size_t& maxLength = 32767);
37
41 std::string readString(const size_t& length);
42
46 template <typename T>
47 T readValue(bool little_endian = true)
48 {
49 ByteArray data;
50 data.reserve(sizeof(T));
51
52 read(data, sizeof(T));
53
54 T result = oe::util::convertFromByteArray<T>(data);
55
56 if (little_endian)
57 return oe::util::endian::convertLittleEndianToHost(result);
58 else
59 return oe::util::endian::convertBigEndianToHost(result);
60 }
61
62 //template <typename T> bool writeValue(const T value, bool little_endian = true);
63
67 void trim()
68 {
69 _data.erase(_data.begin(), _cursor);
70 }
71
77 void appendData(const ByteArray& data)
78 {
79 _data.insert(end(_data), begin(data), end(data));
80 }
81
82 const ByteArray getData() const
83 {
84 return _data;
85 }
86
87 /*template <typename T> static Stream buildFromVector(const std::vector<T>& input)
88 {
89 Stream result;
90
91 for (auto& it : input)
92 {
93 result.writeValue<T>(it);
94 }
95
96 return result;
97 }*/
98 private:
99 ByteArray _data;
100 ByteArray::iterator _cursor;
101 };
102
103 template<> bool Stream::readValue<>(bool little_endian);
104 template<> uint8_t Stream::readValue<>(bool little_endian);
105 template<> int8_t Stream::readValue<>(bool little_endian);
106
107 /*template<> bool Stream::writeValue<>(const bool value, bool little_endian);
108 template<> bool Stream::writeValue<>(const uint8_t value, bool little_endian);
109 template<> bool Stream::writeValue<>(const int8_t value, bool little_endian);*/
110}
111
112#endif
Definition stream.h:16
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:67
void appendData(const ByteArray &data)
Add additional data at the end of the stream.
Definition stream.h:77
T readValue(bool little_endian=true)
read a value from the stream and move the cursor
Definition stream.h:47
Input/Output abstractions (Filesystem, Network, ...)
Definition file.h:14