-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStreamReader.cpp
More file actions
133 lines (109 loc) · 3.35 KB
/
StreamReader.cpp
File metadata and controls
133 lines (109 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "StreamReader.hpp"
#include <algorithm>
#include <stdexcept>
#include <sstream>
StreamReader::StreamReader()
: StreamReader(nullptr, 0, 0)
{
}
StreamReader::StreamReader(char* buffer, uint32_t length, uint32_t offset)
: bytes_array_(buffer), stream_length_(length), current_offset_(offset)
{
}
void StreamReader::setBuffer(char* buffer, uint32_t length)
{
bytes_array_ = buffer;
stream_length_ = length;
}
size_t StreamReader::getLength() {
return stream_length_;
}
char* StreamReader::getBuffer() {
return bytes_array_;
}
void StreamReader::setOffset(uint32_t offset) {
current_offset_ = offset;
}
size_t StreamReader::getCurrOffset() {
return current_offset_;
}
int8_t StreamReader::ReadInt8() {
int8_t data = *reinterpret_cast<int8_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int8_t);
return data;
}
void StreamReader::ReadInt8(int8_t& data) {
data = *reinterpret_cast<int8_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int8_t);
}
int16_t StreamReader::ReadInt16() {
int16_t data = *reinterpret_cast<int16_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int16_t);
return data;
}
void StreamReader::ReadInt16(int16_t& data) {
data = *reinterpret_cast<int16_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int16_t);
}
int32_t StreamReader::ReadInt32() {
int32_t data = *reinterpret_cast<int32_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int32_t);
return data;
}
void StreamReader::ReadInt32(int32_t& data) {
data = *reinterpret_cast<int32_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int32_t);
}
int64_t StreamReader::ReadInt64() {
int64_t data = *reinterpret_cast<int64_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int64_t);
return data;
}
void StreamReader::ReadInt64(int64_t& data) {
data = *reinterpret_cast<int64_t*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(int64_t);
}
float StreamReader::ReadFloat32() {
float data = *reinterpret_cast<float*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(float);
return data;
}
void StreamReader::ReadFloat32(float& data) {
data = *reinterpret_cast<float*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(float);
}
double StreamReader::ReadFloat64() {
float data = *reinterpret_cast<double*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(double);
return data;
}
void StreamReader::ReadFloat64(double& data) {
data = *reinterpret_cast<double*>(bytes_array_ + current_offset_);
current_offset_ += sizeof(double);
}
std::string StreamReader::ReadString()
{
char* data = reinterpret_cast<char*>(bytes_array_ + current_offset_);
size_t length = strlen(data) + 1;
std::string str(data);
current_offset_ += length;
return str;
}
std::string StreamReader::ReadString(size_t size)
{
char data[512] = { 0, };
if (size > 512) {
std::stringstream ss;
ss << __FUNCSIG__ << " Buffer Overflow... size: " << size << '\n';
std::runtime_error(ss.str().c_str());
}
memcpy(data, bytes_array_ + current_offset_, size);
auto begin = std::begin(data);
std::string str(begin, begin + strlen(data));
current_offset_ += size;
return str;
}
void StreamReader::Skip(size_t length)
{
current_offset_ += length;
}