/
Binary.cpp
192 lines (161 loc) · 3.75 KB
/
Binary.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// MIT Licensed (see LICENSE.md).
#include "Precompiled.hpp"
namespace Zero
{
BinaryBufferSaver::BinaryBufferSaver()
{
}
BinaryBufferSaver::~BinaryBufferSaver()
{
}
void BinaryBufferSaver::Open()
{
}
void BinaryBufferSaver::Data(::byte* data, uint sizeInBytes)
{
mBuffer.Append(data, sizeInBytes);
}
uint BinaryBufferSaver::GetSize()
{
return mBuffer.GetSize();
}
void BinaryBufferSaver::ExtractInto(DataBlock& block)
{
mBuffer.ExtractInto(block.Data, block.Size);
}
void BinaryBufferSaver::ExtractInto(::byte* data, uint size)
{
mBuffer.ExtractInto(data, size);
}
DataBlock BinaryBufferSaver::ExtractAsDataBlock()
{
uint size = mBuffer.GetSize();
DataBlock dataBlock;
dataBlock.Data = (::byte*)zAllocate(size);
dataBlock.Size = size;
ExtractInto(dataBlock);
return dataBlock;
}
void BinaryBufferSaver::Deallocate()
{
mBuffer.Deallocate();
}
void BinaryBufferLoader::SetBuffer(::byte* data, uint size)
{
mBufferSize = size;
mCurrentPosition = data;
mBuffer = data;
}
void BinaryBufferLoader::SetBlock(DataBlock block)
{
mBufferSize = block.Size;
mCurrentPosition = block.Data;
mBuffer = block.Data;
}
bool BinaryBufferLoader::TestForObjectEnd(BoundType** data)
{
*data = nullptr;
size_t bytesToRead = sizeof(u32);
::byte* endOfBuffer = mBuffer + mBufferSize;
::byte* ifMoved = mCurrentPosition + bytesToRead;
if (ifMoved < endOfBuffer)
{
//*data = *(BoundType**)mCurrentPosition;
if (*(u32*)mCurrentPosition == BinaryEndSignature)
{
return false;
}
else
{
mCurrentPosition += bytesToRead;
return true;
}
}
else
{
// end of buffer
return false;
}
}
void BinaryBufferLoader::Data(::byte* data, uint sizeInBytes)
{
const bool bufferOverrun = mCurrentPosition + sizeInBytes > mBuffer + mBufferSize;
ErrorIf(bufferOverrun, "Access buffer out of range.");
if (!bufferOverrun)
{
memcpy(data, mCurrentPosition, sizeInBytes);
mCurrentPosition += sizeInBytes;
}
}
bool BinaryBufferLoader::StringField(cstr typeName, cstr fieldName, StringRange& stringRange)
{
u32 size = 0;
Data((::byte*)&size, sizeof(size));
::byte* start = mCurrentPosition;
mCurrentPosition += size;
::byte* end = mCurrentPosition;
stringRange = StringRange((char*)start, (char*)end);
return true;
}
bool BinaryFileLoader::OpenFile(Status& status, cstr filename)
{
return mFile.Open(filename, FileMode::Read, FileAccessPattern::Sequential);
}
void BinaryFileLoader::Close()
{
mFile.Close();
}
bool BinaryFileLoader::TestForObjectEnd(BoundType** data)
{
*data = nullptr;
size_t bytesToRead = sizeof(u32);
FilePosition fp = mFile.Tell();
if (fp + bytesToRead < mFile.Size())
{
u32 end = BinaryEndSignature;
Data((::byte*)&end, bytesToRead);
if (end == BinaryEndSignature)
{
// End of object
// Move back to prevent reading the end tag
mFile.Seek(-(int)bytesToRead, SeekOrigin::Current);
return false;
}
return true;
}
else
{
// End of file
return false;
}
}
void BinaryFileLoader::Data(::byte* data, uint sizeInBytes)
{
Status status;
mFile.Read(status, data, sizeInBytes);
}
bool BinaryFileLoader::StringField(cstr typeName, cstr fieldName, StringRange& stringRange)
{
Status status;
u32 size = 0;
mFile.Read(status, (::byte*)&size, sizeof(size));
if (size < 512)
{
mFile.Read(status, (::byte*)mTempSpace, size);
stringRange = StringRange(mTempSpace, mTempSpace, mTempSpace + size);
}
return true;
}
bool BinaryFileSaver::Open(Status& status, cstr filename)
{
return mFile.Open(filename, FileMode::Write, FileAccessPattern::Sequential);
}
void BinaryFileSaver::Close()
{
mFile.Close();
}
void BinaryFileSaver::Data(::byte* data, uint sizeInBytes)
{
mFile.Write(data, sizeInBytes);
}
} // namespace Zero