-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibBinaryStream.h
More file actions
142 lines (117 loc) · 3.46 KB
/
LibBinaryStream.h
File metadata and controls
142 lines (117 loc) · 3.46 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
134
135
136
137
138
139
140
141
142
/*
LibBinaryStream, Copyright (c) 2015 - 2023 Realsphere Entertainment
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <assert.h>
#include <fstream>
class BinaryWriter {
public:
void WriteInt(int value) {
str->write(reinterpret_cast<const char*>(&value), sizeof(value));
}
void WriteFloat(float value) {
str->write(reinterpret_cast<const char*>(&value), sizeof(value));
}
void WriteLong(long value) {
str->write(reinterpret_cast<const char*>(&value), sizeof(value));
}
void WriteShort(short value) {
str->write(reinterpret_cast<const char*>(&value), sizeof(value));
}
void WriteChar(char value) {
str->write(reinterpret_cast<const char*>(&value), sizeof(value));
}
template<typename T>
void Write(const T& data) {
str->write(reinterpret_cast<const char*>(&data), sizeof(T));
}
void WriteString(const char* value) {
WriteInt(strlen(value));
for (size_t i = 0; i < strlen(value); i++)
WriteChar(value[i]);
}
BinaryWriter(const char* filename) {
str = new std::ofstream(filename, std::ios::binary);
}
void Close() {
str->close();
delete str;
}
std::ofstream* GetFileStream() {
return str;
}
private:
std::ofstream* str;
};
class BinaryReader {
public:
int ReadInt() {
int value;
char buffer[sizeof(value)];
str->read(buffer, sizeof(value));
std::memcpy(&value, buffer, sizeof(value));
return value;
}
short ReadShort() {
short value;
char buffer[sizeof(value)];
str->read(buffer, sizeof(value));
std::memcpy(&value, buffer, sizeof(value));
return value;
}
float ReadFloat() {
float value;
char buffer[sizeof(value)];
str->read(buffer, sizeof(value));
std::memcpy(&value, buffer, sizeof(value));
return value;
}
long ReadLong() {
long value;
char buffer[sizeof(value)];
str->read(buffer, sizeof(value));
std::memcpy(&value, buffer, sizeof(value));
return value;
}
template<typename T>
T Read() {
T value;
char buffer[sizeof(T)];
str->read(buffer, sizeof(T));
std::memcpy(&value, buffer, sizeof(T));
return value;
}
char ReadChar() {
char buffer[1];
str->read(buffer, 1);
return buffer[0];
}
const char* ReadString() {
int size = ReadInt();
char* buffer = new char[size];
for (size_t i = 0; i < size; i++)
buffer[i] = ReadChar();
return buffer;
}
BinaryReader(const char* filename) {
str = new std::ifstream(filename, std::ios::binary);
}
void Close() {
str->close();
delete str;
}
std::ifstream* GetFileStream() {
return str;
}
private:
std::ifstream* str;
};