From 40946f2651f32969e7188289fda07bcf94c6cca7 Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Tue, 12 Sep 2017 08:39:04 +0300 Subject: [PATCH] bineditor: switched from old-style `File` i/o to new-style `TStream` i/o; should fix some windows problems (or not) --- src/shared/BinEditor.pas | 688 +++++++++++++++------------------------ 1 file changed, 258 insertions(+), 430 deletions(-) diff --git a/src/shared/BinEditor.pas b/src/shared/BinEditor.pas index e1e9022..62fe9bf 100644 --- a/src/shared/BinEditor.pas +++ b/src/shared/BinEditor.pas @@ -14,180 +14,166 @@ * along with this program. If not, see . *) {$INCLUDE a_modes.inc} -Unit BinEditor; +unit BinEditor; -Interface +interface Uses - SysUtils; + SysUtils, Classes; -Type - EBinSizeError = Class (Exception); +type + EBinSizeError = class(Exception); - TBinMemoryWriter = Class (TObject) - Private - FSize: Cardinal; + TBinMemoryWriter = class + private + FSize: LongWord; FData: Pointer; - FPosition: Cardinal; - - Procedure WriteVar (var x; varSize: Cardinal); - Procedure ExtendMemory(addLen: Cardinal); - - Public - Constructor Create (aSize: Cardinal); - Destructor Destroy (); Override; - - Procedure WriteByte (x: Byte); - Procedure WriteWord (x: Word); - Procedure WriteDWORD (x: DWORD); - Procedure WriteShortInt (x: ShortInt); - Procedure WriteSmallInt (x: SmallInt); - Procedure WriteInt (x: Integer); - Procedure WriteSingle (x: Single); - Procedure WriteBoolean (x: Boolean); - Procedure WriteString (const x: AnsiString; aMaxLen: Word=65535); - Procedure WriteMemory (x: Pointer; memSize: Cardinal); - Procedure Fill (aLen: Cardinal; aFillSym: Byte); - Procedure SaveToFile (Var aFile: File); - Procedure SaveToMemory (Var aMem: TBinMemoryWriter); - End; - - TBinMemoryReader = Class (TObject) - Private - FSize: Cardinal; + FPosition: LongWord; + + procedure WriteVar (var x; varSize: LongWord); + procedure ExtendMemory (addLen: LongWord); + + public + constructor Create (aSize: LongWord); + destructor Destroy (); override; + + procedure WriteByte (x: Byte); + procedure WriteWord (x: Word); + procedure WriteDWORD (x: LongWord); + procedure WriteShortInt (x: ShortInt); + procedure WriteSmallInt (x: SmallInt); + procedure WriteInt (x: LongInt); + procedure WriteSingle (x: Single); + procedure WriteBoolean (x: Boolean); + procedure WriteString (const x: AnsiString; aMaxLen: Word=65535); + procedure WriteMemory (x: Pointer; memSize: LongWord); + procedure Fill (aLen: LongWord; aFillSym: Byte); + procedure SaveToFile (st: TStream); + procedure SaveToMemory (aMem: TBinMemoryWriter); + end; + + TBinMemoryReader = class + private + FSize: LongWord; FData: Pointer; - FPosition: Cardinal; - - Procedure ReadVar(Var x; varSize: Cardinal); - - Public - Constructor Create(); - Destructor Destroy(); Override; - Procedure ReadByte(Var x: Byte); - Procedure ReadWord(Var x: Word); - Procedure ReadDWORD(Var x: DWORD); - Procedure ReadShortInt(Var x: ShortInt); - Procedure ReadSmallInt(Var x: SmallInt); - Procedure ReadInt(Var x: Integer); - Procedure ReadSingle(Var x: Single); - Procedure ReadBoolean(Var x: Boolean); - Procedure ReadString(Var x: AnsiString); - Procedure ReadMemory(Var x: Pointer; Var memSize: Cardinal); - Procedure Skip(aLen: Cardinal); - Procedure LoadFromFile(Var aFile: File); - Procedure LoadFromMemory(Var aMem: TBinMemoryReader); - End; - - TBinFileWriter = Class (TObject) - Private - FHandle: File; - FOpened: Boolean; - - Public - Constructor Create(); - Destructor Destroy(); Override; - Procedure OpenFile(Const aFileName: String; - aFileSig: Cardinal; - aFileVer: Byte; - aOverWrite: Boolean = True); - Procedure Close(); - Procedure WriteMemory(Var aMemory: TBinMemoryWriter); - End; - - TBinFileReader = Class (TObject) - Private - FHandle: File; - FOpened: Boolean; - - Public - Constructor Create(); - Destructor Destroy(); Override; - Function OpenFile(Const aFileName: String; - aFileSig: Cardinal; - aFileVer: Byte): Boolean; - Procedure Close(); - Procedure ReadMemory(Var aMemory: TBinMemoryReader); - End; - -procedure FillMemory(Dest: Pointer; Len: Cardinal; Ch: Byte); -procedure CopyMemory(Dest: Pointer; Src: Pointer; Len: Cardinal); -procedure ZeroMemory(Dest: Pointer; Len: Cardinal); - -Implementation + FPosition: LongWord; + + procedure ReadVar (var x; varSize: LongWord); + + public + constructor Create (); + destructor Destroy (); override; + procedure ReadByte (var x: Byte); + procedure ReadWord (var x: Word); + procedure ReadDWORD (var x: LongWord); + procedure ReadShortInt (var x: ShortInt); + procedure ReadSmallInt (var x: SmallInt); + procedure ReadInt (var x: LongInt); + procedure ReadSingle (var x: Single); + procedure ReadBoolean (var x: Boolean); + procedure ReadString (var x: AnsiString); + procedure ReadMemory (var x: Pointer; var memSize: LongWord); + procedure Skip (aLen: LongWord); + procedure LoadFromFile (st: TStream); + procedure LoadFromMemory (aMem: TBinMemoryReader); + end; -Uses - Math, e_log; + TBinFileWriter = class + private + FHandle: TStream; + + public + constructor Create (); + destructor Destroy (); override; + procedure OpenFile (const aFileName: AnsiString; aFileSig: LongWord; + aFileVer: Byte; aOverWrite: Boolean=true); + procedure Close (); + procedure WriteMemory (aMemory: TBinMemoryWriter); + end; + + TBinFileReader = class + private + FHandle: TStream; + + public + constructor Create (); + destructor Destroy (); override; + function OpenFile (const aFileName: AnsiString; aFileSig: LongWord; aFileVer: Byte): Boolean; + procedure Close (); + procedure ReadMemory (aMemory: TBinMemoryReader); + end; + +procedure FillMemory (Dest: Pointer; Len: LongWord; Ch: Byte); inline; +procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline; +procedure ZeroMemory (Dest: Pointer; Len: LongWord); inline; -Const - MAX_BIN_SIZE = 4 * 1024 * 1024; // 4 MB -procedure CopyMemory(Dest: Pointer; Src: Pointer; Len: Cardinal); +implementation + +uses + Math, e_log, utils; + +const + MAX_BIN_SIZE = 42*1024*1024; // 42 MB + + +procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline; begin Move(Src^, Dest^, Len); end; -procedure FillMemory(Dest: Pointer; Len: Cardinal; Ch: Byte); +procedure FillMemory (Dest: Pointer; Len: LongWord; Ch: Byte); inline; begin FillChar(Dest^, Len, Ch); end; -procedure ZeroMemory(Dest: Pointer; Len: Cardinal); +procedure ZeroMemory (Dest: Pointer; Len: LongWord); inline; begin FillChar(Dest^, Len, 0); end; + { T B i n M e m o r y W r i t e r : } -Constructor TBinMemoryWriter.Create(aSize: Cardinal); +constructor TBinMemoryWriter.Create (aSize: LongWord); begin - if aSize <= 0 then - FSize := 1 - else - FSize := aSize; - if FSize > MAX_BIN_SIZE then - FSize := MAX_BIN_SIZE; - + if (aSize <= 0) then FSize := 1 else FSize := aSize; + if (FSize > MAX_BIN_SIZE) then FSize := MAX_BIN_SIZE; GetMem(FData, FSize); FPosition := 0; end; -Destructor TBinMemoryWriter.Destroy(); +destructor TBinMemoryWriter.Destroy (); begin - if FData <> nil then + if (FData <> nil) then begin FreeMem(FData); FData := nil; end; - - Inherited; + inherited; end; -Procedure TBinMemoryWriter.WriteVar (var x; varSize: Cardinal); +procedure TBinMemoryWriter.WriteVar (var x; varSize: LongWord); begin - if (FPosition+varSize > FSize) then ExtendMemory(varSize); - CopyMemory(Pointer(PtrUInt(FData)+FPosition), @x, varSize); - FPosition := FPosition+varSize; + if (varSize > 0) then + begin + if (FPosition+varSize > FSize) then ExtendMemory(varSize); + CopyMemory(Pointer(PtrUInt(FData)+FPosition), @x, varSize); + FPosition := FPosition+varSize; + end; end; -Procedure TBinMemoryWriter.ExtendMemory(addLen: Cardinal); +procedure TBinMemoryWriter.ExtendMemory (addLen: LongWord); var tmp: Pointer; - begin - while ((FPosition + addLen) > FSize) and - (FSize <= MAX_BIN_SIZE) do - FSize := FSize * 2; + while (FPosition+addLen > FSize) and (FSize <= MAX_BIN_SIZE) do FSize := FSize*2; - if FSize > MAX_BIN_SIZE then - begin - raise EBinSizeError.Create('TBinMemoryWriter.ExtendMemory: Tried to allocete more than 4 MB'); - Exit; - end; + if (FSize > MAX_BIN_SIZE) then raise EBinSizeError.Create('TBinMemoryWriter.ExtendMemory: Tried to allocete more than 42 MB'); GetMem(tmp, FSize); - if FPosition > 0 then - CopyMemory(tmp, FData, FPosition); + if (FPosition > 0) then CopyMemory(tmp, FData, FPosition); FreeMem(FData); FData := tmp; @@ -195,74 +181,47 @@ begin e_WriteLog('Save Memory Extended: '+IntToStr(FSize), MSG_NOTIFY); end; -Procedure TBinMemoryWriter.WriteByte (x: Byte); -begin - WriteVar(x, SizeOf(Byte)); -end; - -Procedure TBinMemoryWriter.WriteWord (x: Word); -begin - WriteVar(x, SizeOf(Word)); -end; - -Procedure TBinMemoryWriter.WriteDWORD (x: DWORD); -begin - WriteVar(x, SizeOf(DWORD)); -end; - -Procedure TBinMemoryWriter.WriteShortInt (x: ShortInt); -begin - WriteVar(x, SizeOf(ShortInt)); -end; - -Procedure TBinMemoryWriter.WriteSmallInt (x: SmallInt); -begin - WriteVar(x, SizeOf(SmallInt)); -end; - -Procedure TBinMemoryWriter.WriteInt (x: Integer); -begin - WriteVar(x, SizeOf(Integer)); -end; - -Procedure TBinMemoryWriter.WriteSingle (x: Single); -begin - WriteVar(x, SizeOf(Single)); -end; +procedure TBinMemoryWriter.WriteByte (x: Byte); begin WriteVar(x, sizeof(Byte)); end; +procedure TBinMemoryWriter.WriteWord (x: Word); begin WriteVar(x, sizeof(Word)); end; +procedure TBinMemoryWriter.WriteDWORD (x: LongWord); begin WriteVar(x, sizeof(LongWord)); end; +procedure TBinMemoryWriter.WriteShortInt (x: ShortInt); begin WriteVar(x, sizeof(ShortInt)); end; +procedure TBinMemoryWriter.WriteSmallInt (x: SmallInt); begin WriteVar(x, sizeof(SmallInt)); end; +procedure TBinMemoryWriter.WriteInt (x: LongInt); begin WriteVar(x, sizeof(LongInt)); end; +procedure TBinMemoryWriter.WriteSingle (x: Single); begin WriteVar(x, sizeof(Single)); end; -Procedure TBinMemoryWriter.WriteBoolean (x: Boolean); +procedure TBinMemoryWriter.WriteBoolean (x: Boolean); var y: Byte; begin if x then y := 1 else y := 0; - WriteVar(y, SizeOf(Byte)); + WriteVar(y, sizeof(Byte)); end; -Procedure TBinMemoryWriter.WriteString (const x: AnsiString; aMaxLen: Word=65535); +procedure TBinMemoryWriter.WriteString (const x: AnsiString; aMaxLen: Word=65535); var len: Word; begin if (Length(x) > aMaxLen) then len := aMaxLen else len := Word(Length(x)); - if (FPosition+SizeOf(Byte)+len) > FSize then ExtendMemory(SizeOf(Byte)+len); + if (FPosition+sizeof(Byte)+len) > FSize then ExtendMemory(sizeof(Byte)+len); - // Äëèíà ñòðîêè: - CopyMemory(Pointer(NativeUInt(FData)+FPosition), @len, SizeOf(len)); - FPosition := FPosition+SizeOf(len); - // Ñòðîêà: + // Äëèíà ñòðîêè + CopyMemory(Pointer(PtrUInt(FData)+FPosition), @len, sizeof(len)); + FPosition := FPosition+sizeof(len); + // Ñòðîêà if (len > 0) then begin - CopyMemory(Pointer(NativeUInt(FData) + FPosition), @x[1], len); + CopyMemory(Pointer(PtrUInt(FData) + FPosition), @x[1], len); FPosition := FPosition+len; end; end; -Procedure TBinMemoryWriter.WriteMemory (x: Pointer; memSize: Cardinal); +procedure TBinMemoryWriter.WriteMemory (x: Pointer; memSize: LongWord); begin - if (FPosition+SizeOf(Cardinal)+memSize) > FSize then ExtendMemory(SizeOf(Cardinal)+memSize); + if (FPosition+sizeof(LongWord)+memSize) > FSize then ExtendMemory(sizeof(LongWord)+memSize); // Äëèíà áëîêà ïàìÿòè - CopyMemory(Pointer(PtrUInt(FData)+FPosition), @memSize, SizeOf(Cardinal)); - FPosition := FPosition+SizeOf(Cardinal); + CopyMemory(Pointer(PtrUInt(FData)+FPosition), @memSize, sizeof(LongWord)); + FPosition := FPosition+sizeof(LongWord); // Áëîê ïàìÿòè if (memSize > 0) then begin @@ -271,7 +230,7 @@ begin end; end; -Procedure TBinMemoryWriter.Fill (aLen: Cardinal; aFillSym: Byte); +procedure TBinMemoryWriter.Fill (aLen: LongWord; aFillSym: Byte); begin if (FPosition+aLen > FSize) then ExtendMemory(aLen); if (aLen > 0) then @@ -281,134 +240,80 @@ begin end; end; -Procedure TBinMemoryWriter.SaveToFile(Var aFile: File); -var - nw: Cardinal; - +procedure TBinMemoryWriter.SaveToFile (st: TStream); begin -// Ðàçìåð áëîêà: - BlockWrite(aFile, FPosition, SizeOf(Cardinal), nw); - if nw <> SizeOf(Cardinal) then - begin - raise EInOutError.Create('TBinMemoryWriter.SaveToFile: Writing Length'); - end - else - begin - // Äàííûå áëîêà: - BlockWrite(aFile, FData^, FPosition, nw); - if nw <> FPosition then - begin - raise EInOutError.Create('TBinMemoryWriter.SaveToFile: Writing Data'); - end - end; + // Ðàçìåð áëîêà + utils.writeInt(st, LongWord(FPosition)); + // Äàííûå áëîêà + if (FPosition > 0) then st.WriteBuffer(FData^, FPosition); end; -Procedure TBinMemoryWriter.SaveToMemory(Var aMem: TBinMemoryWriter); +procedure TBinMemoryWriter.SaveToMemory (aMem: TBinMemoryWriter); begin - if aMem <> nil then - begin - aMem.WriteMemory(FData, FPosition); - end; + if (aMem <> nil) then aMem.WriteMemory(FData, FPosition); end; + { T B i n M e m o r y R e a d e r : } -Constructor TBinMemoryReader.Create(); +constructor TBinMemoryReader.Create (); begin FSize := 0; FData := nil; FPosition := 1; end; -Destructor TBinMemoryReader.Destroy(); +destructor TBinMemoryReader.Destroy (); begin - if FData <> nil then + if (FData <> nil) then begin FreeMem(FData); FData := nil; end; - - Inherited; -end; - -Procedure TBinMemoryReader.ReadVar(Var x; varSize: Cardinal); -begin - if (FPosition + varSize) <= FSize then - begin - CopyMemory(@x, - Pointer(NativeUInt(FData) + FPosition), - varSize); - FPosition := FPosition + varSize; - end - else - raise EBinSizeError.Create('TBinMemoryReader.ReadVar: End of Memory'); -end; - -Procedure TBinMemoryReader.ReadByte(Var x: Byte); -begin - ReadVar(x, SizeOf(Byte)); -end; - -Procedure TBinMemoryReader.ReadWord(Var x: Word); -begin - ReadVar(x, SizeOf(Word)); + inherited; end; -Procedure TBinMemoryReader.ReadDWORD(Var x: DWORD); +procedure TBinMemoryReader.ReadVar (var x; varSize: LongWord); begin - ReadVar(x, SizeOf(DWORD)); -end; - -Procedure TBinMemoryReader.ReadShortInt(Var x: ShortInt); -begin - ReadVar(x, SizeOf(ShortInt)); -end; - -Procedure TBinMemoryReader.ReadSmallInt(Var x: SmallInt); -begin - ReadVar(x, SizeOf(SmallInt)); -end; - -Procedure TBinMemoryReader.ReadInt(Var x: Integer); -begin - ReadVar(x, SizeOf(Integer)); + if (varSize = 0) then exit; + if (FPosition+varSize > FSize) then raise EBinSizeError.Create('TBinMemoryReader.ReadVar: End of Memory'); + CopyMemory(@x, Pointer(PtrUInt(FData) + FPosition), varSize); + FPosition := FPosition+varSize; end; -Procedure TBinMemoryReader.ReadSingle(Var x: Single); -begin - ReadVar(x, SizeOf(Single)); -end; +procedure TBinMemoryReader.ReadByte (var x: Byte); begin ReadVar(x, sizeof(Byte)); end; +procedure TBinMemoryReader.ReadWord (var x: Word); begin ReadVar(x, sizeof(Word)); end; +procedure TBinMemoryReader.ReadDWORD (var x: LongWord); begin ReadVar(x, sizeof(LongWord)); end; +procedure TBinMemoryReader.ReadShortInt (var x: ShortInt); begin ReadVar(x, sizeof(ShortInt)); end; +procedure TBinMemoryReader.ReadSmallInt (var x: SmallInt); begin ReadVar(x, sizeof(SmallInt)); end; +procedure TBinMemoryReader.ReadInt (var x: LongInt); begin ReadVar(x, sizeof(LongInt)); end; +procedure TBinMemoryReader.ReadSingle (var x: Single); begin ReadVar(x, sizeof(Single)); end; -Procedure TBinMemoryReader.ReadBoolean(Var x: Boolean); +procedure TBinMemoryReader.ReadBoolean (var x: Boolean); var y: Byte; - begin - ReadVar(y, SizeOf(Byte)); - - if y > 0 then - x := True - else - x := False; + ReadVar(y, sizeof(Byte)); + x := (y > 0); end; -Procedure TBinMemoryReader.ReadString (Var x: AnsiString); +procedure TBinMemoryReader.ReadString (var x: AnsiString); var len: Word; begin - if (FPosition+SizeOf(len)) <= FSize then + if (FPosition+sizeof(len)) <= FSize then begin - // Äëèíà ñòðîêè: - CopyMemory(@len, Pointer(NativeUInt(FData)+FPosition), SizeOf(len)); - if (FPosition+SizeOf(len)+len <= FSize) then + // Äëèíà ñòðîêè + CopyMemory(@len, Pointer(PtrUInt(FData)+FPosition), sizeof(len)); + if (FPosition+sizeof(len)+len <= FSize) then begin - FPosition := FPosition+SizeOf(len); - // Ñòðîêà: + FPosition := FPosition+sizeof(len); + // Ñòðîêà UniqueString(x); SetLength(x, len); if (len > 0) then begin - CopyMemory(@x[1], Pointer(NativeUInt(FData) + FPosition), len); + CopyMemory(@x[1], Pointer(PtrUInt(FData) + FPosition), len); FPosition := FPosition+len; end else @@ -418,7 +323,7 @@ begin end else begin - raise EBinSizeError.Create('TBinMemoryReader.ReadString: Too Long String'); + raise EBinSizeError.Create('TBinMemoryReader.ReadString: Too Long AnsiString'); end; end else @@ -427,237 +332,160 @@ begin end; end; -Procedure TBinMemoryReader.ReadMemory(Var x: Pointer; Var memSize: Cardinal); +procedure TBinMemoryReader.ReadMemory (var x: Pointer; var memSize: LongWord); begin - if (FPosition + SizeOf(Cardinal)) <= FSize then - begin - // Äëèíà áëîêà ïàìÿòè: - CopyMemory(@memSize, - Pointer(NativeUInt(FData) + FPosition), - SizeOf(Cardinal)); - - if (FPosition + SizeOf(Cardinal) + memSize) <= FSize then - begin - FPosition := FPosition + SizeOf(Cardinal); - // Áëîê ïàìÿòè: - if memSize > 0 then - begin - GetMem(x, memSize); - CopyMemory(x, - Pointer(NativeUInt(FData) + FPosition), - memSize); - FPosition := FPosition + memSize; - end - else - x := nil; - end - else - raise EBinSizeError.Create('TBinMemoryReader.ReadMemory: Too Long Memory'); - end + if (FPosition+sizeof(LongWord) > FSize) then raise EBinSizeError.Create('TBinMemoryReader.ReadMemory: End of Memory'); + // Äëèíà áëîêà ïàìÿòè + CopyMemory(@memSize, Pointer(PtrUInt(FData)+FPosition), sizeof(LongWord)); + if (FPosition+sizeof(LongWord)+memSize > FSize) then raise EBinSizeError.Create('TBinMemoryReader.ReadMemory: Too Long Memory'); + FPosition := FPosition+sizeof(LongWord); + // Áëîê ïàìÿòè + if (memSize > 0) then + begin + GetMem(x, memSize); + CopyMemory(x, Pointer(PtrUInt(FData)+FPosition), memSize); + FPosition += memSize; + end else - raise EBinSizeError.Create('TBinMemoryReader.ReadMemory: End of Memory'); + begin + x := nil; + end; end; -Procedure TBinMemoryReader.Skip(aLen: Cardinal); +procedure TBinMemoryReader.Skip(aLen: LongWord); begin - if (FPosition + aLen) <= FSize then - begin - FPosition := FPosition + aLen; - end - else - raise EBinSizeError.Create('TBinMemoryReader.Skip: End of Memory'); + if (FPosition+aLen > FSize) then raise EBinSizeError.Create('TBinMemoryReader.Skip: End of Memory'); + FPosition += aLen; end; -Procedure TBinMemoryReader.LoadFromFile(Var aFile: File); +procedure TBinMemoryReader.LoadFromFile (st: TStream); var - nr: Cardinal; - aSize: Cardinal; - + aSize: LongWord; begin - if FData <> nil then - FreeMem(FData); - -// Ðàçìåð áëîêà: - BlockRead(aFile, aSize, SizeOf(Cardinal), nr); - if nr <> SizeOf(Cardinal) then - begin - raise EInOutError.Create('TBinMemoryReader.LoadFromFile: Reading Length'); - end - else - begin - FSize := aSize; - GetMem(FData, FSize); - FPosition := 0; - // Äàííûå áëîêà: - BlockRead(aFile, FData^, FSize, nr); - if nr <> FSize then - begin - raise EInOutError.Create('TBinMemoryReader.LoadFromFile: Reading Data'); - end - end; + if (FData <> nil) then begin FreeMem(FData); FData := nil; end; + // Ðàçìåð áëîêà + aSize := utils.readLongWord(st); + FSize := aSize; + GetMem(FData, FSize); + FPosition := 0; + // Äàííûå áëîêà + if (aSize <> 0) then st.ReadBuffer(FData^, FSize); end; -Procedure TBinMemoryReader.LoadFromMemory(Var aMem: TBinMemoryReader); -begin - if FData <> nil then - FreeMem(FData); - if aMem <> nil then +procedure TBinMemoryReader.LoadFromMemory (aMem: TBinMemoryReader); +begin + if (FData <> nil) then begin FreeMem(FData); FData := nil; end; + if (aMem <> nil) then begin aMem.ReadMemory(FData, FSize); FPosition := 0; end; end; + { T B i n F i l e W r i t e r : } -Constructor TBinFileWriter.Create(); +constructor TBinFileWriter.Create (); begin - FOpened := False; + FHandle := nil; end; -Destructor TBinFileWriter.Destroy(); +destructor TBinFileWriter.Destroy (); begin Close(); - - Inherited; + inherited; end; -Procedure TBinFileWriter.OpenFile(Const aFileName: String; - aFileSig: Cardinal; - aFileVer: Byte; - aOverWrite: Boolean = True); -var - nw: Integer; - +procedure TBinFileWriter.OpenFile (const aFileName: AnsiString; aFileSig: LongWord; + aFileVer: Byte; aOverWrite: Boolean=true); begin Close(); - - if (not FileExists(aFileName)) or (aOverWrite) then + if (not FileExists(aFileName)) or aOverWrite then begin - AssignFile(FHandle, aFileName); - ReWrite(FHandle, 1); - - // Ñèãíàòóðà: - BlockWrite(FHandle, aFileSig, SizeOf(Cardinal), nw); - if nw <> SizeOf(Cardinal) then - begin - raise EInOutError.Create('TBinFileWriter.OpenFile: Writing File Signature'); - end - else - begin - // Âåðñèÿ: - BlockWrite(FHandle, aFileVer, SizeOf(Byte), nw); - if nw <> SizeOf(Byte) then - begin - raise EInOutError.Create('TBinFileWriter.OpenFile: Writing File Version'); - end - else - begin - FOpened := True; - end; - end; + try + FHandle := createDiskFile(aFileName); + // Ñèãíàòóðà + utils.writeInt(FHandle, LongWord(aFileSig)); + // Âåðñèÿ + utils.writeInt(FHandle, Byte(aFileVer)); + except + FHandle.Free(); + FHandle := nil; + raise; + end; end; end; -Procedure TBinFileWriter.Close(); +procedure TBinFileWriter.Close(); begin - if FOpened then + if (FHandle <> nil) then begin - System.Close(FHandle); - FOpened := False; + FHandle.Free(); + FHandle := nil; end; end; -Procedure TBinFileWriter.WriteMemory(Var aMemory: TBinMemoryWriter); +procedure TBinFileWriter.WriteMemory (aMemory: TBinMemoryWriter); begin - if (FOpened) and (aMemory <> nil) then - begin - aMemory.SaveToFile(FHandle); - end; + if (FHandle <> nil) and (aMemory <> nil) then aMemory.SaveToFile(FHandle); end; + { T B i n F i l e R e a d e r : } -Constructor TBinFileReader.Create(); +constructor TBinFileReader.Create (); begin - FOpened := False; + FHandle := nil; end; -Destructor TBinFileReader.Destroy(); +destructor TBinFileReader.Destroy (); begin Close(); - - Inherited; + inherited; end; -Function TBinFileReader.OpenFile(Const aFileName: String; - aFileSig: Cardinal; - aFileVer: Byte): Boolean; +function TBinFileReader.OpenFile (const aFileName: AnsiString; aFileSig: LongWord; aFileVer: Byte): Boolean; var - nr: Integer; - sig: Cardinal; + sig: LongWord; ver: Byte; - begin - Result := False; + result := false; Close(); if FileExists(aFileName) then begin - AssignFile(FHandle, aFileName); - ReSet(FHandle, 1); - - // Ñèãíàòóðà: - BlockRead(FHandle, sig, SizeOf(Cardinal), nr); - if (nr <> SizeOf(Cardinal)) then - begin - raise EInOutError.Create('TBinFileReader.OpenFile: Reading File Signature'); - end - else - if (sig <> aFileSig) then - begin - raise EInOutError.Create('TBinFileReader.OpenFile: Wrong File Signature'); - end - else - begin - // Âåðñèÿ: - BlockRead(FHandle, ver, SizeOf(Byte), nr); - if (nr <> SizeOf(Byte)) then - begin - raise EInOutError.Create('TBinFileReader.OpenFile: Reading File Version'); - end - else - if (ver <> aFileVer) then - begin - raise EInOutError.Create('TBinFileReader.OpenFile: Wrong File Version'); - end - else - begin - FOpened := True; - Result := True; - end; - end; + FHandle := openDiskFileRO(aFileName); + try + // Ñèãíàòóðà + sig := utils.readLongWord(FHandle); + if (sig <> aFileSig) then raise EInOutError.Create('TBinFileReader.OpenFile: Wrong File Signature'); + // Âåðñèÿ + ver := utils.readByte(FHandle); + if (ver <> aFileVer) then raise EInOutError.Create('TBinFileReader.OpenFile: Wrong File Version'); + result := true; + except + FHandle.Free(); + FHandle := nil; + raise; + end; end; end; -Procedure TBinFileReader.Close(); +procedure TBinFileReader.Close (); begin - if FOpened then + if (FHandle <> nil) then begin - System.Close(FHandle); - FOpened := False; + FHandle.Free(); + FHandle := nil; end; end; -Procedure TBinFileReader.ReadMemory(Var aMemory: TBinMemoryReader); +procedure TBinFileReader.ReadMemory (aMemory: TBinMemoryReader); begin - if (FOpened) and (aMemory <> nil) then - begin - aMemory.LoadFromFile(FHandle); - end; + if (FHandle <> nil) and (aMemory <> nil) then aMemory.LoadFromFile(FHandle); end; -End. +end. -- 2.29.2