1 | unit DataAccess;
|
---|
2 | interface
|
---|
3 |
|
---|
4 | uses TypeDefs, Classes, StrUtils, SysUtils;
|
---|
5 |
|
---|
6 | type
|
---|
7 | TDataAccess = class
|
---|
8 | private
|
---|
9 | protected
|
---|
10 | FConnectionID: Integer;
|
---|
11 | FFileName: String;
|
---|
12 | FBackend: TDataBackend;
|
---|
13 | FDataOS: TDataOS;
|
---|
14 | FLevelNumber: Integer;
|
---|
15 | FChangeRights: TChangeRights;
|
---|
16 | procedure SetDataOS(DataOS: TDataOS);
|
---|
17 | public
|
---|
18 | property ConnectionID: Integer read FConnectionID;
|
---|
19 | property FileName: String read FFileName;
|
---|
20 | property Backend: TDataBackend read FBackend;
|
---|
21 | property DataOS: TDataOS read FDataOS write SetDataOS;
|
---|
22 | property LevelNumber: Integer read FLevelNumber;
|
---|
23 | property ChangeRights: TChangeRights read FChangeRights;
|
---|
24 |
|
---|
25 | constructor Create(FileName: String; ConnectionID: Integer; var Msg: TStatusMessages); virtual; abstract;
|
---|
26 | procedure Close; virtual; abstract;
|
---|
27 |
|
---|
28 | function ExtractFileIDOfName(Name: String): Integer; virtual;
|
---|
29 | function GetFileInfo(FileID: Integer): TFileInfo; virtual;
|
---|
30 | function GetFileInfoByName(Name: String): TFileInfo; virtual;
|
---|
31 | function GetFilesList(Ext: String; Pattern: String;
|
---|
32 | NoEmptyFiles: Boolean; SortType: TSortType): TStrings; virtual; abstract;
|
---|
33 | function GetFileCount: Integer; virtual; abstract;
|
---|
34 | function GetExtensionsList(ExtListFormat: TExtensionFormat): TStrings; virtual; abstract;
|
---|
35 |
|
---|
36 | procedure LoadDatFile(FileID: Integer; var Target: TStream); overload; virtual; abstract;
|
---|
37 | procedure LoadDatFile(FileID: Integer; var Target: TByteData); overload;
|
---|
38 | procedure UpdateDatFile(FileID: Integer; Src: TStream); overload; virtual; abstract;
|
---|
39 | procedure UpdateDatFile(FileID: Integer; Src: TByteData); overload;
|
---|
40 | procedure LoadDatFilePart(FileID, Offset, Size: Integer; var Target: TStream); overload; virtual; abstract;
|
---|
41 | procedure LoadDatFilePart(FileID, Offset, Size: Integer; var Target: TByteData); overload;
|
---|
42 | procedure LoadDatFilePart(FileID, Offset, Size: Integer; Target: Pointer); overload;
|
---|
43 | procedure UpdateDatFilePart(FileID, Offset, Size: Integer; Src: TStream); overload; virtual; abstract;
|
---|
44 | procedure UpdateDatFilePart(FileID, Offset, Size: Integer; Src: Pointer); overload;
|
---|
45 |
|
---|
46 | function GetDatLinks(FileID: Integer): TDatLinkList; virtual; abstract;
|
---|
47 | function GetRawList(FileID: Integer): TRawDataList; virtual; abstract;
|
---|
48 | function GetRawInfo(FileID, DatOffset: Integer): TRawDataInfo; virtual; abstract;
|
---|
49 |
|
---|
50 | procedure LoadRawFile(FileID, DatOffset: Integer; var Target: TStream); overload; virtual; abstract;
|
---|
51 | procedure LoadRawFile(FileID, DatOffset: Integer; var Target: TByteData); overload;
|
---|
52 | procedure UpdateRawFile(FileID, DatOffset: Integer; Src: TStream); overload; virtual; abstract;
|
---|
53 | procedure UpdateRawFile(FileID, DatOffset: Integer; Src: TByteData); overload;
|
---|
54 | procedure LoadRawFilePart(FileID, DatOffset, Offset, Size: Integer; var Target: TStream); overload; virtual; abstract;
|
---|
55 | procedure LoadRawFilePart(FileID, DatOffset, Offset, Size: Integer; var Target: TByteData); overload;
|
---|
56 | procedure LoadRawFilePart(FileID, DatOffset, Offset, Size: Integer; Target: Pointer); overload;
|
---|
57 | procedure UpdateRawFilePart(FileID, DatOffset, Offset, Size: Integer; Src: TStream); overload; virtual; abstract;
|
---|
58 | procedure UpdateRawFilePart(FileID, DatOffset, Offset, Size: Integer; Src: Pointer); overload;
|
---|
59 |
|
---|
60 | function AppendRawFile(LocSep: Boolean; Src: TStream): Integer; overload; virtual;
|
---|
61 | function AppendRawFile(LocSep: Boolean; Src: TByteData): Integer; overload;
|
---|
62 | function AppendRawFile(LocSep: Boolean; Size: Integer; Src: Pointer): Integer; overload;
|
---|
63 | published
|
---|
64 | end;
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | implementation
|
---|
69 |
|
---|
70 |
|
---|
71 | (*
|
---|
72 | Implementation of TDataAccess
|
---|
73 | *)
|
---|
74 |
|
---|
75 | function TDataAccess.ExtractFileIDOfName(Name: String): Integer;
|
---|
76 | begin
|
---|
77 | if Name[5] = '-' then
|
---|
78 | Result := StrToInt(MidStr(Name, 1, 4))
|
---|
79 | else
|
---|
80 | Result := StrToInt(MidStr(Name, 1, 5));
|
---|
81 | end;
|
---|
82 |
|
---|
83 |
|
---|
84 | function TDataAccess.GetFileInfo(FileID: Integer): TFileInfo;
|
---|
85 | begin
|
---|
86 | Result.ID := -1;
|
---|
87 | Result.Name := '';
|
---|
88 | Result.Extension := '';
|
---|
89 | Result.Size := -1;
|
---|
90 | Result.FileType := 0;
|
---|
91 | Result.DatAddr := -1;
|
---|
92 | end;
|
---|
93 |
|
---|
94 |
|
---|
95 | function TDataAccess.GetFileInfoByName(Name: String): TFileInfo;
|
---|
96 | var
|
---|
97 | i: Integer;
|
---|
98 | files: TStrings;
|
---|
99 | begin
|
---|
100 | Result := GetFileInfo(-1);
|
---|
101 | files := GetFilesList('', Name, False, ST_IDAsc);
|
---|
102 | if files.Count = 0 then
|
---|
103 | else
|
---|
104 | begin
|
---|
105 | for i := 0 to files.Count - 1 do
|
---|
106 | begin
|
---|
107 | if MidStr(files.Strings[i], Pos('-', files.Strings[i]) + 1,
|
---|
108 | Length(files.Strings[i]) - Pos('-', files.Strings[i]) - 5) = name then
|
---|
109 | begin
|
---|
110 | Result := GetFileInfo(ExtractFileIDOfName(files.Strings[i]));
|
---|
111 | Break;
|
---|
112 | end;
|
---|
113 | end;
|
---|
114 | end;
|
---|
115 | files.Free;
|
---|
116 | end;
|
---|
117 |
|
---|
118 |
|
---|
119 | procedure TDataAccess.LoadDatFile(FileID: Integer; var Target: TByteData);
|
---|
120 | var
|
---|
121 | data: TStream;
|
---|
122 | begin
|
---|
123 | if fileid < GetFileCount then
|
---|
124 | begin
|
---|
125 | data := nil;
|
---|
126 | LoadDatFile(FileID, data);
|
---|
127 | SetLength(Target, data.Size);
|
---|
128 | data.Read(Target[0], data.Size);
|
---|
129 | data.Free;
|
---|
130 | end;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure TDataAccess.UpdateDatFile(FileID: Integer; Src: TByteData);
|
---|
134 | var
|
---|
135 | data: TStream;
|
---|
136 | begin
|
---|
137 | if fileid < GetFileCount then
|
---|
138 | begin
|
---|
139 | data := TMemoryStream.Create;
|
---|
140 | data.Write(Src[0], Length(Src));
|
---|
141 | data.Seek(0, soFromBeginning);
|
---|
142 | UpdateDatFile(FileID, data);
|
---|
143 | data.Free;
|
---|
144 | end;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | procedure TDataAccess.LoadDatFilePart(FileID, Offset, Size: Integer; var Target: TByteData);
|
---|
148 | var
|
---|
149 | data: TStream;
|
---|
150 | begin
|
---|
151 | if fileid < GetFileCount then
|
---|
152 | begin
|
---|
153 | data := nil;
|
---|
154 | LoadDatFilePart(FileID, offset, size, data);
|
---|
155 | SetLength(Target, data.Size);
|
---|
156 | data.Read(Target[0], data.Size);
|
---|
157 | data.Free;
|
---|
158 | end;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TDataAccess.LoadDatFilePart(FileID, Offset, Size: Integer; Target: Pointer);
|
---|
162 | var
|
---|
163 | data: TStream;
|
---|
164 | begin
|
---|
165 | if fileid < GetFileCount then
|
---|
166 | begin
|
---|
167 | data := nil;
|
---|
168 | LoadDatFilePart(FileID, offset, size, data);
|
---|
169 | data.Read(Target^, data.Size);
|
---|
170 | data.Free;
|
---|
171 | end;
|
---|
172 | end;
|
---|
173 |
|
---|
174 | procedure TDataAccess.UpdateDatFilePart(FileID, Offset, Size: Integer; Src: Pointer);
|
---|
175 | var
|
---|
176 | data: TStream;
|
---|
177 | begin
|
---|
178 | if fileid < GetFileCount then
|
---|
179 | begin
|
---|
180 | data := TMemoryStream.Create;
|
---|
181 | data.Write(Src^, Size);
|
---|
182 | data.Seek(0, soFromBeginning);
|
---|
183 | UpdateDatFilePart(FileID, offset, size, data);
|
---|
184 | data.Free;
|
---|
185 | end;
|
---|
186 | end;
|
---|
187 |
|
---|
188 |
|
---|
189 |
|
---|
190 | procedure TDataAccess.LoadRawFile(FileID, DatOffset: Integer; var Target: TByteData);
|
---|
191 | var
|
---|
192 | data: TStream;
|
---|
193 | begin
|
---|
194 | if fileid < GetFileCount then
|
---|
195 | begin
|
---|
196 | data := nil;
|
---|
197 | LoadRawFile(FileID, DatOffset, data);
|
---|
198 | SetLength(Target, data.Size);
|
---|
199 | data.Read(Target[0], data.Size);
|
---|
200 | data.Free;
|
---|
201 | end;
|
---|
202 | end;
|
---|
203 |
|
---|
204 | procedure TDataAccess.UpdateRawFile(FileID, DatOffset: Integer; Src: TByteData);
|
---|
205 | var
|
---|
206 | data: TStream;
|
---|
207 | begin
|
---|
208 | if fileid < GetFileCount then
|
---|
209 | begin
|
---|
210 | data := TMemoryStream.Create;
|
---|
211 | data.Write(Src[0], Length(Src));
|
---|
212 | data.Seek(0, soFromBeginning);
|
---|
213 | UpdateRawFile(FileID, DatOffset, data);
|
---|
214 | data.Free;
|
---|
215 | end;
|
---|
216 | end;
|
---|
217 |
|
---|
218 | procedure TDataAccess.LoadRawFilePart(FileID, DatOffset, Offset, Size: Integer; var Target: TByteData);
|
---|
219 | var
|
---|
220 | data: TStream;
|
---|
221 | begin
|
---|
222 | if fileid < GetFileCount then
|
---|
223 | begin
|
---|
224 | data := nil;
|
---|
225 | SetLength(Target, Size);
|
---|
226 | LoadRawFile(FileID, DatOffset, Data);
|
---|
227 | Data.Seek(Offset, soFromBeginning);
|
---|
228 | Data.Read(Target[0], Size);
|
---|
229 | Data.Free;
|
---|
230 | end;
|
---|
231 | end;
|
---|
232 |
|
---|
233 | procedure TDataAccess.LoadRawFilePart(FileID, DatOffset, Offset, Size: Integer; Target: Pointer);
|
---|
234 | var
|
---|
235 | data: TStream;
|
---|
236 | begin
|
---|
237 | if fileid < GetFileCount then
|
---|
238 | begin
|
---|
239 | data := nil;
|
---|
240 | LoadRawFile(FileID, DatOffset, Data);
|
---|
241 | Data.Seek(Offset, soFromBeginning);
|
---|
242 | Data.Read(Target^, Size);
|
---|
243 | Data.Free;
|
---|
244 | end;
|
---|
245 | end;
|
---|
246 |
|
---|
247 | procedure TDataAccess.UpdateRawFilePart(FileID, DatOffset, Offset, Size: Integer; Src: Pointer);
|
---|
248 | var
|
---|
249 | data: TStream;
|
---|
250 | begin
|
---|
251 | if fileid < GetFileCount then
|
---|
252 | begin
|
---|
253 | data := TMemoryStream.Create;
|
---|
254 | data.Write(Src^, Size);
|
---|
255 | data.Seek(0, soFromBeginning);
|
---|
256 | UpdateRawFilePart(FileID, DatOffset, Offset, Size, data);
|
---|
257 | data.Free;
|
---|
258 | end;
|
---|
259 | end;
|
---|
260 |
|
---|
261 |
|
---|
262 | function TDataAccess.AppendRawFile(LocSep: Boolean; Src: TStream): Integer;
|
---|
263 | begin
|
---|
264 | raise ENotImplemented.Create('ERROR: AppendRawFile not implemented here!!!');
|
---|
265 | end;
|
---|
266 |
|
---|
267 |
|
---|
268 | function TDataAccess.AppendRawFile(LocSep: Boolean; Src: TByteData): Integer;
|
---|
269 | var
|
---|
270 | data: TStream;
|
---|
271 | begin
|
---|
272 | data := TMemoryStream.Create;
|
---|
273 | data.Write(Src[0], Length(Src));
|
---|
274 | data.Seek(0, soFromBeginning);
|
---|
275 | Result := AppendRawFile(LocSep, data);
|
---|
276 | data.Free;
|
---|
277 | end;
|
---|
278 |
|
---|
279 | function TDataAccess.AppendRawFile(LocSep: Boolean; Size: Integer; Src: Pointer): Integer;
|
---|
280 | var
|
---|
281 | data: TStream;
|
---|
282 | begin
|
---|
283 | data := TMemoryStream.Create;
|
---|
284 | data.Write(Src^, Size);
|
---|
285 | data.Seek(0, soFromBeginning);
|
---|
286 | Result := AppendRawFile(LocSep, data);
|
---|
287 | data.Free;
|
---|
288 | end;
|
---|
289 |
|
---|
290 |
|
---|
291 |
|
---|
292 | procedure TDataAccess.SetDataOS(DataOS: TDataOS);
|
---|
293 | begin
|
---|
294 | raise ENotImplemented.Create('TDataAccess.SetDataOS: TBD!');
|
---|
295 | end;
|
---|
296 |
|
---|
297 | end.
|
---|