source: branches/ByteArray/Devices/Storage.pas

Last change on this file was 9, checked in by chronos, 2 months ago
  • Modified: Optimized DeviceMapper read/write handlers calls without case statement.
  • Modified: Use TInt as base data type which can be redefined to different higher integer type. For start it is Int64.
File size: 2.7 KB
Line 
1unit Storage;
2
3interface
4
5uses
6 Classes, SysUtils, Device, Channel, Int;
7
8type
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
36implementation
37
38{ TStorage }
39
40procedure TStorage.SetFileName(AValue: string);
41begin
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;
49end;
50
51function TStorage.GetSize: Integer;
52begin
53 Result := FFile.Size;
54end;
55
56procedure TStorage.SetSize(AValue: Integer);
57begin
58 FFile.Size := AValue;
59end;
60
61function TStorage.ReadByte(Address: TInt): Byte;
62begin
63 Result := 0;
64 FFile.Position := Address;
65 FFile.Read(Result, 1);
66end;
67
68function TStorage.ReadData(Size: TIntSize): TInt;
69begin
70 FFile.Position := Position;
71 FFile.Read(Result, Size);
72 Inc(Position, Size);
73end;
74
75function TStorage.ReadPosition(Size: TIntSize): TInt;
76begin
77 Result := Position;
78end;
79
80function TStorage.ReadSize(Size: TIntSize): TInt;
81begin
82 Result := FFile.Size;
83end;
84
85procedure TStorage.WriteData(Size: TIntSize; Value: TInt);
86begin
87 FFile.Position := Position;
88 FFile.Write(Value, Size);
89 Inc(Position, Size);
90end;
91
92procedure TStorage.WritePosition(Size: TIntSize; Value: TInt);
93begin
94 Position := Value;
95end;
96
97procedure TStorage.WriteSize(Size: TIntSize; Value: TInt);
98begin
99 FFile.Size := Value;
100end;
101function TStorage.GetHandlers: THandlers;
102begin
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);
110end;
111
112constructor TStorage.Create;
113begin
114 FileName := 'Storage.dat';
115 Position := 0;
116end;
117
118destructor TStorage.Destroy;
119begin
120 FreeAndNil(FFile);
121 inherited;
122end;
123
124end.
125
Note: See TracBrowser for help on using the repository browser.