9 EBinSizeError
= Class (Exception
);
11 TBinMemoryWriter
= Class (TObject
)
17 Procedure WriteVar(Var x
; varSize
: Cardinal);
18 Procedure ExtendMemory(addLen
: Cardinal);
21 Constructor Create(aSize
: Cardinal);
22 Destructor Destroy(); Override;
23 Procedure WriteByte(Var x
: Byte);
24 Procedure WriteWord(Var x
: Word);
25 Procedure WriteDWORD(Var x
: DWORD
);
26 Procedure WriteShortInt(Var x
: ShortInt);
27 Procedure WriteSmallInt(Var x
: SmallInt);
28 Procedure WriteInt(Var x
: Integer);
29 Procedure WriteSingle(Var x
: Single);
30 Procedure WriteBoolean(Var x
: Boolean);
31 Procedure WriteString(Var x
: String; aMaxLen
: Byte = 255);
32 Procedure WriteMemory(Var x
: Pointer; memSize
: Cardinal);
33 Procedure Fill(aLen
: Cardinal; aFillSym
: Byte);
34 Procedure SaveToFile(Var aFile
: File);
35 Procedure SaveToMemory(Var aMem
: TBinMemoryWriter
);
38 TBinMemoryReader
= Class (TObject
)
44 Procedure ReadVar(Var x
; varSize
: Cardinal);
48 Destructor Destroy(); Override;
49 Procedure ReadByte(Var x
: Byte);
50 Procedure ReadWord(Var x
: Word);
51 Procedure ReadDWORD(Var x
: DWORD
);
52 Procedure ReadShortInt(Var x
: ShortInt);
53 Procedure ReadSmallInt(Var x
: SmallInt);
54 Procedure ReadInt(Var x
: Integer);
55 Procedure ReadSingle(Var x
: Single);
56 Procedure ReadBoolean(Var x
: Boolean);
57 Procedure ReadString(Var x
: String);
58 Procedure ReadMemory(Var x
: Pointer; Var memSize
: Cardinal);
59 Procedure Skip(aLen
: Cardinal);
60 Procedure LoadFromFile(Var aFile
: File);
61 Procedure LoadFromMemory(Var aMem
: TBinMemoryReader
);
64 TBinFileWriter
= Class (TObject
)
71 Destructor Destroy(); Override;
72 Procedure OpenFile(Const aFileName
: String;
75 aOverWrite
: Boolean = True);
77 Procedure WriteMemory(Var aMemory
: TBinMemoryWriter
);
80 TBinFileReader
= Class (TObject
)
87 Destructor Destroy(); Override;
88 Function OpenFile(Const aFileName
: String;
90 aFileVer
: Byte): Boolean;
92 Procedure ReadMemory(Var aMemory
: TBinMemoryReader
);
95 procedure FillMemory(Dest
: Pointer; Len
: Cardinal; Ch
: Byte);
96 procedure CopyMemory(Dest
: Pointer; Src
: Pointer; Len
: Cardinal);
97 procedure ZeroMemory(Dest
: Pointer; Len
: Cardinal);
105 MAX_BIN_SIZE
= 4 * 1024 * 1024; // 4 MB
107 procedure CopyMemory(Dest
: Pointer; Src
: Pointer; Len
: Cardinal);
109 Move(Src
^, Dest
^, Len
);
112 procedure FillMemory(Dest
: Pointer; Len
: Cardinal; Ch
: Byte);
114 FillChar(Dest
^, Len
, Ch
);
117 procedure ZeroMemory(Dest
: Pointer; Len
: Cardinal);
119 FillChar(Dest
^, Len
, 0);
122 { T B i n M e m o r y W r i t e r : }
124 Constructor TBinMemoryWriter
.Create(aSize
: Cardinal);
130 if FSize
> MAX_BIN_SIZE
then
131 FSize
:= MAX_BIN_SIZE
;
133 GetMem(FData
, FSize
);
137 Destructor TBinMemoryWriter
.Destroy();
148 Procedure TBinMemoryWriter
.WriteVar(Var x
; varSize
: Cardinal);
150 if (FPosition
+ varSize
) > FSize
then
151 ExtendMemory(varSize
);
153 CopyMemory(Pointer(PtrUInt(FData
) + FPosition
),
155 FPosition
:= FPosition
+ varSize
;
158 Procedure TBinMemoryWriter
.ExtendMemory(addLen
: Cardinal);
163 while ((FPosition
+ addLen
) > FSize
) and
164 (FSize
<= MAX_BIN_SIZE
) do
167 if FSize
> MAX_BIN_SIZE
then
169 raise EBinSizeError
.Create('TBinMemoryWriter.ExtendMemory: Tried to allocete more than 4 MB');
175 if FPosition
> 0 then
176 CopyMemory(tmp
, FData
, FPosition
);
181 e_WriteLog('Save Memory Extended: '+IntToStr(FSize
), MSG_NOTIFY
);
184 Procedure TBinMemoryWriter
.WriteByte(Var x
: Byte);
186 WriteVar(x
, SizeOf(Byte));
189 Procedure TBinMemoryWriter
.WriteWord(Var x
: Word);
191 WriteVar(x
, SizeOf(Word));
194 Procedure TBinMemoryWriter
.WriteDWORD(Var x
: DWORD
);
196 WriteVar(x
, SizeOf(DWORD
));
199 Procedure TBinMemoryWriter
.WriteShortInt(Var x
: ShortInt);
201 WriteVar(x
, SizeOf(ShortInt));
204 Procedure TBinMemoryWriter
.WriteSmallInt(Var x
: SmallInt);
206 WriteVar(x
, SizeOf(SmallInt));
209 Procedure TBinMemoryWriter
.WriteInt(Var x
: Integer);
211 WriteVar(x
, SizeOf(Integer));
214 Procedure TBinMemoryWriter
.WriteSingle(Var x
: Single);
216 WriteVar(x
, SizeOf(Single));
219 Procedure TBinMemoryWriter
.WriteBoolean(Var x
: Boolean);
229 WriteVar(y
, SizeOf(Byte));
232 Procedure TBinMemoryWriter
.WriteString(Var x
: String; aMaxLen
: Byte = 255);
237 len
:= Min(Length(x
), aMaxLen
);
239 if (FPosition
+ SizeOf(Byte) + len
) > FSize
then
240 ExtendMemory(SizeOf(Byte) + len
);
243 CopyMemory(Pointer(PtrUInt(FData
) + FPosition
),
245 FPosition
:= FPosition
+ SizeOf(Byte);
249 CopyMemory(Pointer(PtrUInt(FData
) + FPosition
),
251 FPosition
:= FPosition
+ len
;
255 Procedure TBinMemoryWriter
.WriteMemory(Var x
: Pointer; memSize
: Cardinal);
257 if (FPosition
+ SizeOf(Cardinal) + memSize
) > FSize
then
258 ExtendMemory(SizeOf(Cardinal) + memSize
);
260 // Äëèíà áëîêà ïàìÿòè:
261 CopyMemory(Pointer(PtrUInt(FData
) + FPosition
),
262 @memSize
, SizeOf(Cardinal));
263 FPosition
:= FPosition
+ SizeOf(Cardinal);
267 CopyMemory(Pointer(PtrUInt(FData
) + FPosition
),
269 FPosition
:= FPosition
+ memSize
;
273 Procedure TBinMemoryWriter
.Fill(aLen
: Cardinal; aFillSym
: Byte);
275 if (FPosition
+ aLen
) > FSize
then
280 FillMemory(Pointer(PtrUInt(FData
) + FPosition
),
282 FPosition
:= FPosition
+ aLen
;
286 Procedure TBinMemoryWriter
.SaveToFile(Var aFile
: File);
292 BlockWrite(aFile
, FPosition
, SizeOf(Cardinal), nw
);
293 if nw
<> SizeOf(Cardinal) then
295 raise EInOutError
.Create('TBinMemoryWriter.SaveToFile: Writing Length');
300 BlockWrite(aFile
, FData
^, FPosition
, nw
);
301 if nw
<> FPosition
then
303 raise EInOutError
.Create('TBinMemoryWriter.SaveToFile: Writing Data');
308 Procedure TBinMemoryWriter
.SaveToMemory(Var aMem
: TBinMemoryWriter
);
312 aMem
.WriteMemory(FData
, FPosition
);
316 { T B i n M e m o r y R e a d e r : }
318 Constructor TBinMemoryReader
.Create();
325 Destructor TBinMemoryReader
.Destroy();
336 Procedure TBinMemoryReader
.ReadVar(Var x
; varSize
: Cardinal);
338 if (FPosition
+ varSize
) <= FSize
then
341 Pointer(PtrUInt(FData
) + FPosition
),
343 FPosition
:= FPosition
+ varSize
;
346 raise EBinSizeError
.Create('TBinMemoryReader.ReadVar: End of Memory');
349 Procedure TBinMemoryReader
.ReadByte(Var x
: Byte);
351 ReadVar(x
, SizeOf(Byte));
354 Procedure TBinMemoryReader
.ReadWord(Var x
: Word);
356 ReadVar(x
, SizeOf(Word));
359 Procedure TBinMemoryReader
.ReadDWORD(Var x
: DWORD
);
361 ReadVar(x
, SizeOf(DWORD
));
364 Procedure TBinMemoryReader
.ReadShortInt(Var x
: ShortInt);
366 ReadVar(x
, SizeOf(ShortInt));
369 Procedure TBinMemoryReader
.ReadSmallInt(Var x
: SmallInt);
371 ReadVar(x
, SizeOf(SmallInt));
374 Procedure TBinMemoryReader
.ReadInt(Var x
: Integer);
376 ReadVar(x
, SizeOf(Integer));
379 Procedure TBinMemoryReader
.ReadSingle(Var x
: Single);
381 ReadVar(x
, SizeOf(Single));
384 Procedure TBinMemoryReader
.ReadBoolean(Var x
: Boolean);
389 ReadVar(y
, SizeOf(Byte));
397 Procedure TBinMemoryReader
.ReadString(Var x
: String);
402 if (FPosition
+ SizeOf(Byte)) <= FSize
then
406 Pointer(PtrUInt(FData
) + FPosition
),
409 if (FPosition
+ SizeOf(Byte) + len
) <= FSize
then
411 FPosition
:= FPosition
+ SizeOf(Byte);
417 Pointer(PtrUInt(FData
) + FPosition
),
419 FPosition
:= FPosition
+ len
;
425 raise EBinSizeError
.Create('TBinMemoryReader.ReadString: Too Long String');
428 raise EBinSizeError
.Create('TBinMemoryReader.ReadString: End of Memory');
431 Procedure TBinMemoryReader
.ReadMemory(Var x
: Pointer; Var memSize
: Cardinal);
433 if (FPosition
+ SizeOf(Cardinal)) <= FSize
then
435 // Äëèíà áëîêà ïàìÿòè:
437 Pointer(PtrUInt(FData
) + FPosition
),
440 if (FPosition
+ SizeOf(Cardinal) + memSize
) <= FSize
then
442 FPosition
:= FPosition
+ SizeOf(Cardinal);
448 Pointer(PtrUInt(FData
) + FPosition
),
450 FPosition
:= FPosition
+ memSize
;
456 raise EBinSizeError
.Create('TBinMemoryReader.ReadMemory: Too Long Memory');
459 raise EBinSizeError
.Create('TBinMemoryReader.ReadMemory: End of Memory');
462 Procedure TBinMemoryReader
.Skip(aLen
: Cardinal);
464 if (FPosition
+ aLen
) <= FSize
then
466 FPosition
:= FPosition
+ aLen
;
469 raise EBinSizeError
.Create('TBinMemoryReader.Skip: End of Memory');
472 Procedure TBinMemoryReader
.LoadFromFile(Var aFile
: File);
482 BlockRead(aFile
, aSize
, SizeOf(Cardinal), nr
);
483 if nr
<> SizeOf(Cardinal) then
485 raise EInOutError
.Create('TBinMemoryReader.LoadFromFile: Reading Length');
490 GetMem(FData
, FSize
);
493 BlockRead(aFile
, FData
^, FSize
, nr
);
496 raise EInOutError
.Create('TBinMemoryReader.LoadFromFile: Reading Data');
501 Procedure TBinMemoryReader
.LoadFromMemory(Var aMem
: TBinMemoryReader
);
508 aMem
.ReadMemory(FData
, FSize
);
513 { T B i n F i l e W r i t e r : }
515 Constructor TBinFileWriter
.Create();
520 Destructor TBinFileWriter
.Destroy();
527 Procedure TBinFileWriter
.OpenFile(Const aFileName
: String;
530 aOverWrite
: Boolean = True);
537 if (not FileExists(aFileName
)) or (aOverWrite
) then
539 AssignFile(FHandle
, findFileCIStr(aFileName
));
543 BlockWrite(FHandle
, aFileSig
, SizeOf(Cardinal), nw
);
544 if nw
<> SizeOf(Cardinal) then
546 raise EInOutError
.Create('TBinFileWriter.OpenFile: Writing File Signature');
551 BlockWrite(FHandle
, aFileVer
, SizeOf(Byte), nw
);
552 if nw
<> SizeOf(Byte) then
554 raise EInOutError
.Create('TBinFileWriter.OpenFile: Writing File Version');
564 Procedure TBinFileWriter
.Close();
568 System
.Close(FHandle
);
573 Procedure TBinFileWriter
.WriteMemory(Var aMemory
: TBinMemoryWriter
);
575 if (FOpened
) and (aMemory
<> nil) then
577 aMemory
.SaveToFile(FHandle
);
581 { T B i n F i l e R e a d e r : }
583 Constructor TBinFileReader
.Create();
588 Destructor TBinFileReader
.Destroy();
595 Function TBinFileReader
.OpenFile(Const aFileName
: String;
597 aFileVer
: Byte): Boolean;
608 if FileExists(aFileName
) then
610 AssignFile(FHandle
, findFileCIStr(aFileName
));
614 BlockRead(FHandle
, sig
, SizeOf(Cardinal), nr
);
615 if (nr
<> SizeOf(Cardinal)) then
617 raise EInOutError
.Create('TBinFileReader.OpenFile: Reading File Signature');
620 if (sig
<> aFileSig
) then
622 raise EInOutError
.Create('TBinFileReader.OpenFile: Wrong File Signature');
627 BlockRead(FHandle
, ver
, SizeOf(Byte), nr
);
628 if (nr
<> SizeOf(Byte)) then
630 raise EInOutError
.Create('TBinFileReader.OpenFile: Reading File Version');
633 if (ver
<> aFileVer
) then
635 raise EInOutError
.Create('TBinFileReader.OpenFile: Wrong File Version');
646 Procedure TBinFileReader
.Close();
650 System
.Close(FHandle
);
655 Procedure TBinFileReader
.ReadMemory(Var aMemory
: TBinMemoryReader
);
657 if (FOpened
) and (aMemory
<> nil) then
659 aMemory
.LoadFromFile(FHandle
);