| 1 | unit UFile1;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, Contnrs, UStreamHelper;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 | TFile1 = class;
|
|---|
| 12 | TArrayOfByte = array of Byte;
|
|---|
| 13 |
|
|---|
| 14 | { TFile1Block }
|
|---|
| 15 |
|
|---|
| 16 | TFile1Block = class
|
|---|
| 17 | Parent: TFile1;
|
|---|
| 18 | Name: string;
|
|---|
| 19 | Start: Integer;
|
|---|
| 20 | Size: Integer;
|
|---|
| 21 | function Read: TArrayOfByte;
|
|---|
| 22 | end;
|
|---|
| 23 |
|
|---|
| 24 | { TFile1 }
|
|---|
| 25 |
|
|---|
| 26 | TFile1 = class
|
|---|
| 27 | private
|
|---|
| 28 | function GetOpenned: Boolean;
|
|---|
| 29 | public
|
|---|
| 30 | FileData: TFileStream;
|
|---|
| 31 | Blocks: TObjectList; // TObjectList<TFile1Block>
|
|---|
| 32 | InternalSize: Cardinal;
|
|---|
| 33 | InternalName: string;
|
|---|
| 34 | procedure Open(FileName: string);
|
|---|
| 35 | procedure Close;
|
|---|
| 36 | constructor Create;
|
|---|
| 37 | destructor Destroy; override;
|
|---|
| 38 | property Openned: Boolean read GetOpenned;
|
|---|
| 39 | end;
|
|---|
| 40 |
|
|---|
| 41 | implementation
|
|---|
| 42 |
|
|---|
| 43 | { TFile1Block }
|
|---|
| 44 |
|
|---|
| 45 | function TFile1Block.Read: TArrayOfByte;
|
|---|
| 46 | begin
|
|---|
| 47 | SetLength(Result, Size);
|
|---|
| 48 | Parent.FileData.Position := Start;
|
|---|
| 49 | Parent.FileData.Read(Result[0], Size);
|
|---|
| 50 | end;
|
|---|
| 51 |
|
|---|
| 52 | { TFile1 }
|
|---|
| 53 |
|
|---|
| 54 | function TFile1.GetOpenned: Boolean;
|
|---|
| 55 | begin
|
|---|
| 56 | Result := Assigned(FileData);
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | procedure TFile1.Open(FileName: string);
|
|---|
| 60 | var
|
|---|
| 61 | Helper: TStreamHelper;
|
|---|
| 62 | MagicString: string;
|
|---|
| 63 | NewBlock: TFile1Block;
|
|---|
| 64 | begin
|
|---|
| 65 | Close;
|
|---|
| 66 | FileData := TFileStream.Create(FileName, fmOpenRead);
|
|---|
| 67 | Helper := TStreamHelper.Create(FileData);
|
|---|
| 68 | Helper.Endianness := enBig;
|
|---|
| 69 | MagicString := Helper.ReadString(4);
|
|---|
| 70 | if MagicString = 'FORM' then begin
|
|---|
| 71 | InternalSize := Helper.ReadCardinal;
|
|---|
| 72 | InternalName := Helper.ReadString(4);
|
|---|
| 73 | while Helper.Position < Helper.Size do begin
|
|---|
| 74 | NewBlock := TFile1Block.Create;
|
|---|
| 75 | NewBlock.Name := Helper.ReadString(4);
|
|---|
| 76 | NewBlock.Size := Helper.ReadCardinal;
|
|---|
| 77 | NewBlock.Start := Helper.Position;
|
|---|
| 78 | NewBlock.Parent := Self;
|
|---|
| 79 | Helper.Position := NewBlock.Start + NewBlock.Size;
|
|---|
| 80 | Blocks.Add(NewBlock);
|
|---|
| 81 | end;
|
|---|
| 82 | end;
|
|---|
| 83 | Helper.Free;
|
|---|
| 84 | end;
|
|---|
| 85 |
|
|---|
| 86 | procedure TFile1.Close;
|
|---|
| 87 | begin
|
|---|
| 88 | if Openned then begin
|
|---|
| 89 | Blocks.Count := 0;
|
|---|
| 90 | FreeAndNil(FileData);
|
|---|
| 91 | end;
|
|---|
| 92 | end;
|
|---|
| 93 |
|
|---|
| 94 | constructor TFile1.Create;
|
|---|
| 95 | begin
|
|---|
| 96 | FileData := nil;
|
|---|
| 97 | Blocks := TObjectList.Create;
|
|---|
| 98 | end;
|
|---|
| 99 |
|
|---|
| 100 | destructor TFile1.Destroy;
|
|---|
| 101 | begin
|
|---|
| 102 | Close;
|
|---|
| 103 | FreeAndNil(Blocks);
|
|---|
| 104 | inherited Destroy;
|
|---|
| 105 | end;
|
|---|
| 106 |
|
|---|
| 107 | end.
|
|---|
| 108 |
|
|---|