-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stream.cpp
167 lines (151 loc) · 3.7 KB
/
Stream.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Stream.h"
#define READ_TYPE_MACRO(type) \
type data; \
ReadBytes(&data, sizeof(data));
void Stream::Close() {
delete this;
}
void Stream::Seek(int64_t offset) {
}
void Stream::SeekEnd(int64_t offset) {
}
void Stream::Skip(int64_t offset) {
}
size_t Stream::Position() {
return 0;
}
size_t Stream::Length() {
return 0;
}
size_t Stream::ReadBytes(void* data, int n) {
return 0;
}
uint8_t Stream::ReadByte() {
READ_TYPE_MACRO(uint8_t);
return data;
}
uint16_t Stream::ReadUInt16() {
READ_TYPE_MACRO(uint16_t);
return data;
}
uint16_t Stream::ReadUInt16BE() {
return (uint16_t)(ReadByte() << 8 | ReadByte());
}
uint32_t Stream::ReadUInt32() {
READ_TYPE_MACRO(uint32_t);
return data;
}
uint32_t Stream::ReadUInt32BE() {
return (uint32_t)(ReadByte() << 24 | ReadByte() << 16 | ReadByte() << 8 | ReadByte());
}
uint64_t Stream::ReadUInt64() {
READ_TYPE_MACRO(uint64_t);
return data;
}
int16_t Stream::ReadInt16() {
READ_TYPE_MACRO(int16_t);
return data;
}
int16_t Stream::ReadInt16BE() {
return (int16_t)(ReadByte() << 8 | ReadByte());
}
int32_t Stream::ReadInt32() {
READ_TYPE_MACRO(int32_t);
return data;
}
int32_t Stream::ReadInt32BE() {
return (int32_t)(ReadByte() << 24 | ReadByte() << 16 | ReadByte() << 8 | ReadByte());
}
int64_t Stream::ReadInt64() {
READ_TYPE_MACRO(int64_t);
return data;
}
float Stream::ReadFloat() {
READ_TYPE_MACRO(float);
return data;
}
char* Stream::ReadLine() {
uint8_t byte;
size_t start = Position();
while ((byte = ReadByte()) != '\n' && byte);
size_t size = Position() - start;
char* data = (char*)malloc(size + 1);
Skip(-size);
ReadBytes(data, size);
data[size] = 0;
return data;
}
char* Stream::ReadString() {
size_t start = Position();
while (ReadByte());
size_t size = Position() - start;
char* data = (char*)malloc(size + 1);
Skip(-size);
ReadBytes(data, size);
data[size] = 0;
return data;
}
char* Stream::ReadHeaderedString() {
uint8_t size = ReadByte();
char* data = (char*)malloc(size + 1);
ReadBytes(data, size);
data[size] = 0;
return data;
}
size_t Stream::WriteBytes(void* data, int n) {
return 0;
}
void Stream::WriteByte(uint8_t data) {
WriteBytes(&data, sizeof(data));
}
void Stream::WriteUInt16(uint16_t data) {
WriteBytes(&data, sizeof(data));
}
void Stream::WriteUInt16BE(uint16_t data) {
WriteByte(data >> 8 & 0xFF);
WriteByte(data & 0xFF);
}
void Stream::WriteUInt32(uint32_t data) {
WriteBytes(&data, sizeof(data));
}
void Stream::WriteUInt32BE(uint32_t data) {
WriteByte(data >> 24 & 0xFF);
WriteByte(data >> 16 & 0xFF);
WriteByte(data >> 8 & 0xFF);
WriteByte(data & 0xFF);
}
void Stream::WriteInt16(int16_t data) {
WriteBytes(&data, sizeof(data));
}
void Stream::WriteInt16BE(int16_t data) {
WriteUInt16BE((uint16_t)data);
}
void Stream::WriteInt32(int32_t data) {
WriteBytes(&data, sizeof(data));
}
void Stream::WriteInt32BE(int32_t data) {
WriteUInt32BE((int32_t)data);
}
void Stream::WriteFloat(float data) {
WriteBytes(&data, sizeof(data));
}
void Stream::WriteString(char* string) {
size_t size = strlen(string) + 1;
WriteBytes(string, size);
}
void Stream::WriteHeaderedString(char* string) {
size_t size = strlen(string) + 1;
WriteByte((uint8_t)size);
WriteBytes(string, size);
}
void Stream::CopyTo(Stream* dest) {
size_t length = Length();
void* memory = Memory::Malloc(length);
Seek(0);
ReadBytes(memory, length);
dest->WriteBytes(memory, length);
dest->Seek(0);
free(memory);
}
Stream::~Stream() {
}