-
Notifications
You must be signed in to change notification settings - Fork 1
/
UBinaryFile.cpp
181 lines (181 loc) · 4.12 KB
/
UBinaryFile.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
//---------------------------------------------------------------------------
#include "UBinaryFile.h"
//---------------------------------------------------------------------------
__fastcall TBinaryFile::TBinaryFile(UnicodeString File) : TObject()
{
this->fs = new TMemoryStream();
this->LoadFromFile(File);
}
__fastcall TBinaryFile::TBinaryFile(TStream* S) : TObject()
{
this->fs = new TMemoryStream();
this->LoadFromStream(S);
}
__fastcall TBinaryFile::TBinaryFile() : TObject()
{
this->fs = new TMemoryStream();
}
__fastcall TBinaryFile::TBinaryFile(const void* Buffer, const unsigned __int64 Size) : TObject()
{
this->fs = new TMemoryStream();
fs->Write(Buffer, Size);
}
__fastcall TBinaryFile::~TBinaryFile()
{
VCL_FREE(fs);
}
BYTE TBinaryFile::ReadByte()
{
BYTE Result = 0;
fs->Read(&Result, 1);
return Result;
}
short TBinaryFile::ReadShort()
{
short Result = 0;
fs->Read(&Result, 2);
return Result;
}
float TBinaryFile::ReadFloat()
{
float Result = 0;
fs->Read(&Result, 4);
return Result;
}
int TBinaryFile::ReadInt()
{
int Result = 0;
fs->Read(&Result, 4);
return Result;
}
double TBinaryFile::ReadDouble()
{
double Result = 0;
fs->Read(&Result, 8);
return Result;
}
__int64 TBinaryFile::ReadInt64()
{
__int64 Result = 0;
fs->Read(&Result, 8);
return Result;
}
UnicodeString TBinaryFile::ReadString()
{
unsigned __int64 size = 0;
fs->Read(&size, 8);
wchar_t* buffer = new wchar_t[size];
fs->Read(buffer, size);
const UnicodeString Result = UnicodeString(buffer).SubString(1, size);
DEL_ARRAY(buffer);
return Result;
}
AnsiString TBinaryFile::ReadAnsiString()
{
unsigned int size = 0;
fs->Read(&size, 4);
char* buffer = new char[size];
fs->Read(buffer, size);
const AnsiString Result = AnsiString(buffer).SubString(1, size);
DEL_ARRAY(buffer);
return Result;
}
void TBinaryFile::ReadStream(TStream* Dest)
{
unsigned __int64 size = 0;
fs->Read(&size, 8);
BYTE* buffer = new BYTE[size];
fs->Read(buffer, size);
Dest->Size = 0;
Dest->Write(buffer, size);
DEL_ARRAY(buffer);
}
void TBinaryFile::ReadDiskFile(const UnicodeString File)
{
if(FileExists(File)) DeleteFile(File);
TFileStream* f = new TFileStream(File, fmCreate);
this->ReadStream(f);
VCL_FREE(f);
}
void TBinaryFile::ReadNullTerminatedString(char* szDest, int nMaxRead)
{
char c = 1;
for(int i = 0; i != nMaxRead && c != 0; i++)
{
fs->Read(&c, 1);
if(szDest != NULL) szDest[i] = c;
}
}
void TBinaryFile::ReadBuffer(const unsigned nSize, PVOID pBuffer)
{
fs->Read(pBuffer, nSize);
}
void TBinaryFile::WriteByte(const BYTE Value)
{
fs->Write(&Value, 1);
}
void TBinaryFile::WriteShort(const short Value)
{
fs->Write(&Value, 2);
}
void TBinaryFile::WriteFloat(const float Value)
{
fs->Write(&Value, 4);
}
void TBinaryFile::WriteInt(const int Value)
{
fs->Write(&Value, 4);
}
void TBinaryFile::WriteDouble(const double Value)
{
fs->Write(&Value, 8);
}
void TBinaryFile::WriteInt64(const __int64 Value)
{
fs->Write(&Value, 8);
}
void TBinaryFile::WriteString(const UnicodeString Value)
{
const unsigned __int64 size = Value.Length() * sizeof(wchar_t);
wchar_t* buffer = WBUF(Value);
fs->Write(&size, 8);
fs->Write(buffer, size);
}
void TBinaryFile::WriteAnsiString(const AnsiString Value)
{
const unsigned int size = Value.Length();
char* buffer = BUF(Value);
fs->Write(&size, 4);
fs->Write(buffer, size);
}
void TBinaryFile::WriteStream(TStream* Value)
{
const unsigned __int64 size = Value->Size;
BYTE* buffer = new BYTE[size];
Value->Position = 0;
Value->Read(buffer, size);
fs->Write(&size, 8);
fs->Write(buffer, size);
}
void TBinaryFile::WriteDiskFile(const UnicodeString File)
{
short mode = fmOpenRead;
if(!FileExists(File)) mode = fmCreate;
TFileStream* fstream = new TFileStream(File, mode);
this->WriteStream(fstream);
VCL_FREE(fstream);
}
void TBinaryFile::WriteBuffer(const unsigned nSize, const PVOID pBuffer)
{
fs->Write(pBuffer, nSize);
}
void __fastcall TBinaryFile::SetPos(const unsigned __int64 __pos)
{
fs->Position = __pos;
}
unsigned __int64 __fastcall TBinaryFile::GetPos()
{
return fs->Position;
}
//---------------------------------------------------------------------------
#pragma package(smart_init)