| 1 | unit Storage;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Device, Channel, Int;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 |
|
|---|
| 10 | { TStorage }
|
|---|
| 11 |
|
|---|
| 12 | TStorage = class(TDevice)
|
|---|
| 13 | private
|
|---|
| 14 | FFileName: string;
|
|---|
| 15 | function GetSize: Integer;
|
|---|
| 16 | procedure SetFileName(AValue: string);
|
|---|
| 17 | procedure SetSize(AValue: Integer);
|
|---|
| 18 | public
|
|---|
| 19 | FFile: TFileStream;
|
|---|
| 20 | Position: Integer;
|
|---|
| 21 | function ReadByte(Address: TInt): Byte;
|
|---|
| 22 | function ReadData(Size: TIntSize): TInt;
|
|---|
| 23 | function ReadPosition(Size: TIntSize): TInt;
|
|---|
| 24 | function ReadSize(Size: TIntSize): TInt;
|
|---|
| 25 | procedure WriteData(Size: TIntSize; Value: TInt);
|
|---|
| 26 | procedure WritePosition(Size: TIntSize; Value: TInt);
|
|---|
| 27 | procedure WriteSize(Size: TIntSize; Value: TInt);
|
|---|
| 28 | function GetHandlers: THandlers; override;
|
|---|
| 29 | constructor Create;
|
|---|
| 30 | destructor Destroy; override;
|
|---|
| 31 | property FileName: string read FFileName write SetFileName;
|
|---|
| 32 | property Size: Integer read GetSize write SetSize;
|
|---|
| 33 | end;
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | implementation
|
|---|
| 37 |
|
|---|
| 38 | { TStorage }
|
|---|
| 39 |
|
|---|
| 40 | procedure TStorage.SetFileName(AValue: string);
|
|---|
| 41 | begin
|
|---|
| 42 | if FFileName = AValue then Exit;
|
|---|
| 43 | if AValue = '' then FreeAndNil(FFile);
|
|---|
| 44 | FFileName := AValue;
|
|---|
| 45 | if FFileName <> '' then begin
|
|---|
| 46 | if FileExists(FFileName) then FFile := TFileStream.Create(FFileName, fmOpenReadWrite)
|
|---|
| 47 | else FFile := TFileStream.Create(FFileName, fmCreate);
|
|---|
| 48 | end;
|
|---|
| 49 | end;
|
|---|
| 50 |
|
|---|
| 51 | function TStorage.GetSize: Integer;
|
|---|
| 52 | begin
|
|---|
| 53 | Result := FFile.Size;
|
|---|
| 54 | end;
|
|---|
| 55 |
|
|---|
| 56 | procedure TStorage.SetSize(AValue: Integer);
|
|---|
| 57 | begin
|
|---|
| 58 | FFile.Size := AValue;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | function TStorage.ReadByte(Address: TInt): Byte;
|
|---|
| 62 | begin
|
|---|
| 63 | Result := 0;
|
|---|
| 64 | FFile.Position := Address;
|
|---|
| 65 | FFile.Read(Result, 1);
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | function TStorage.ReadData(Size: TIntSize): TInt;
|
|---|
| 69 | begin
|
|---|
| 70 | FFile.Position := Position;
|
|---|
| 71 | FFile.Read(Result, Size);
|
|---|
| 72 | Inc(Position, Size);
|
|---|
| 73 | end;
|
|---|
| 74 |
|
|---|
| 75 | function TStorage.ReadPosition(Size: TIntSize): TInt;
|
|---|
| 76 | begin
|
|---|
| 77 | Result := Position;
|
|---|
| 78 | end;
|
|---|
| 79 |
|
|---|
| 80 | function TStorage.ReadSize(Size: TIntSize): TInt;
|
|---|
| 81 | begin
|
|---|
| 82 | Result := FFile.Size;
|
|---|
| 83 | end;
|
|---|
| 84 |
|
|---|
| 85 | procedure TStorage.WriteData(Size: TIntSize; Value: TInt);
|
|---|
| 86 | begin
|
|---|
| 87 | FFile.Position := Position;
|
|---|
| 88 | FFile.Write(Value, Size);
|
|---|
| 89 | Inc(Position, Size);
|
|---|
| 90 | end;
|
|---|
| 91 |
|
|---|
| 92 | procedure TStorage.WritePosition(Size: TIntSize; Value: TInt);
|
|---|
| 93 | begin
|
|---|
| 94 | Position := Value;
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | procedure TStorage.WriteSize(Size: TIntSize; Value: TInt);
|
|---|
| 98 | begin
|
|---|
| 99 | FFile.Size := Value;
|
|---|
| 100 | end;
|
|---|
| 101 | function TStorage.GetHandlers: THandlers;
|
|---|
| 102 | begin
|
|---|
| 103 | Result := THandlers.Create;
|
|---|
| 104 | Result.ReadHandlers.Add(ReadData);
|
|---|
| 105 | Result.ReadHandlers.Add(ReadPosition);
|
|---|
| 106 | Result.ReadHandlers.Add(ReadSize);
|
|---|
| 107 | Result.WriteHandlers.Add(WriteData);
|
|---|
| 108 | Result.WriteHandlers.Add(WritePosition);
|
|---|
| 109 | Result.WriteHandlers.Add(WriteSize);
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | constructor TStorage.Create;
|
|---|
| 113 | begin
|
|---|
| 114 | FileName := 'Storage.dat';
|
|---|
| 115 | Position := 0;
|
|---|
| 116 | end;
|
|---|
| 117 |
|
|---|
| 118 | destructor TStorage.Destroy;
|
|---|
| 119 | begin
|
|---|
| 120 | FreeAndNil(FFile);
|
|---|
| 121 | inherited;
|
|---|
| 122 | end;
|
|---|
| 123 |
|
|---|
| 124 | end.
|
|---|
| 125 |
|
|---|