-
Notifications
You must be signed in to change notification settings - Fork 2
/
matio.formats.pas
373 lines (335 loc) · 13.3 KB
/
matio.formats.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
unit matio.formats;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/matio
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils, Classes, Types, PropSet, matio;
Type
TMatrixFormat = Class
strict protected
Procedure AppendFormatProperties(const [ref] Properties: TPropertySet); virtual;
strict protected
Function ExtendProperties(const [ref] Properties: TPropertySet): TPropertySet;
public
Const
FileProperty = 'file';
FormatProperty = 'format';
Class Function FileName(const [ref] Properties: TPropertySet; Expand: Boolean = true): String;
public
Function Format: String; virtual; abstract;
Function Available: Boolean; virtual;
Function FormatProperties(ReadOnly: Boolean = true): TPropertySet;
Function PropertyPickList(const PropertyName: string; out PickList: TStringDynArray): Boolean; virtual;
Function TidyProperties(const [ref] Properties: TPropertySet; ReadOnly: Boolean = true): TPropertySet;
end;
TMatrixReaderFormat = Class(TMatrixFormat)
// When creating a matrix reader for a selection (by index or name) of matrices,
// the index to use to access a specific matrix is its index in the selection array.
public
Class Function FileExists(const [ref] Properties: TPropertySet): Boolean;
public
Function HasFormat(const FileExtension: String): Boolean; overload; virtual;
Function HasFormat(const Header: TBytes): Boolean; overload; virtual;
Function CreateReader(const [ref] Properties: TPropertySet): TMatrixReader; overload; virtual; abstract;
Function CreateReader(const [ref] Properties: TPropertySet; const Selection: array of Integer): TMatrixReader; overload; virtual;
Function CreateReader(const [ref] Properties: TPropertySet; const Selection: array of String): TMatrixReader; overload; virtual;
end;
TMatrixWriterFormat = Class(TMatrixFormat)
public
Function CreateWriter(const [ref] Properties: TPropertySet;
const FileLabel: string;
const MatrixLabels: array of String;
const Size: Integer): TMatrixWriter; virtual; abstract;
end;
TMatrixFormats = record
// The record takes ownership of the registered matrix formats.
private
Const
HeaderSize = 75;
Var
ReaderFormats: TArray<TMatrixReaderFormat>;
WriterFormats: TArray<TMatrixWriterFormat>;
public
Class Operator Finalize (var Formats: TMatrixFormats);
public
// Register format
Procedure RegisterFormat(const Format: TMatrixReaderFormat); overload;
Procedure RegisterFormat(const Format: TMatrixWriterFormat); overload;
// Query registered reader formats
Function RegisteredReaderFormats: TStringDynArray;
Function ReaderFormat(const Format: string): TMatrixFormat; overload;
Function ReaderFormat(const FileName: TFileName): TMatrixFormat; overload;
Function ReaderFormat(const Header: TBytes): TMatrixFormat; overload;
// Query registered writer formats
Function RegisteredWriterFormats: TStringDynArray;
Function WriterFormat(const Format: string): TMatrixFormat;
// Create matrix reader
Function CreateReader(const [ref] Properties: TPropertySet; Ordered: Boolean = true): TMatrixReader; overload;
Function CreateReader(const [ref] Properties: TPropertySet;
const Selection: array of Integer): TMatrixReader; overload;
Function CreateReader(const [ref] Properties: TPropertySet;
const Selection: array of String): TMatrixReader; overload;
// Create matrix writer
Function CreateWriter(const [ref] Properties: TPropertySet;
const FileLabel: string;
const MatrixLabels: array of String;
const Size: Integer): TMatrixWriter;
end;
Var
MatrixFormats: TMatrixFormats;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Uses
matio.formats.text, matio.formats.gen4, matio.formats.minutp, matio.formats.visum,
matio.formats.omx, matio.formats.cube;
Class Function TMatrixFormat.FileName(const [ref] Properties: TPropertySet; Expand: Boolean = true): String;
begin
if Expand then
Result := Properties.ToPath(FileProperty)
else
Result := Properties[FileProperty];
end;
Procedure TMatrixFormat.AppendFormatProperties(const [ref] Properties: TPropertySet);
begin
end;
Function TMatrixFormat.ExtendProperties(const [ref] Properties: TPropertySet): TPropertySet;
Var
Value: String;
begin
Result := FormatProperties(false);
for var Prop := 0 to Result.Count-1 do
begin
if Properties.Contains(Result.Names[Prop],Value) then
Result.ValueFromIndex[Prop] := Value;
end;
end;
Function TMatrixFormat.Available: Boolean;
begin
Result := true;
end;
Function TMatrixFormat.FormatProperties(ReadOnly: Boolean = true): TPropertySet;
begin
Result := TPropertySet.Create(ReadOnly);
Result.Append(FileProperty,'');
Result.Append(FormatProperty,Format);
AppendFormatProperties(Result);
end;
Function TMatrixFormat.PropertyPickList(const PropertyName: string; out PickList: TStringDynArray): Boolean;
begin
if PropertyName = FormatProperty then
begin
Result := true;
PickList := [Format];
end else
Result := false;
end;
Function TMatrixFormat.TidyProperties(const [ref] Properties: TPropertySet; ReadOnly: Boolean = true): TPropertySet;
Var
Value: String;
begin
if SameText(Properties[FormatProperty],Format) then
begin
var Defaults := FormatProperties;
Result := TPropertySet.Create(ReadOnly);
for var Prop := 0 to Defaults.Count-1 do
begin
var Name := Defaults.Names[Prop];
if SameText(Name,FileProperty) or SameText(Name,FormatProperty) then
Result.Append(Name,Properties[Name])
else
if Properties.Contains(Name,Value) then
if not SameText(Defaults.ValueFromIndex[Prop],Value) then
Result.Append(Name,Value)
end;
end else
raise Exception.Create('Invalid format-property');
end;
////////////////////////////////////////////////////////////////////////////////
Class Function TMatrixReaderFormat.FileExists(const [ref] Properties: TPropertySet): Boolean;
begin
Result := SysUtils.FileExists(FileName(Properties));
end;
Function TMatrixReaderFormat.HasFormat(const FileExtension: String): Boolean;
begin
Result := false;
end;
Function TMatrixReaderFormat.HasFormat(const Header: TBytes): Boolean;
begin
Result := false;
end;
Function TMatrixReaderFormat.CreateReader(const [ref] Properties: TPropertySet; const Selection: array of Integer): TMatrixReader;
begin
var Reader := CreateReader(Properties);
if Reader <> nil then
if Reader.Ordered then
Result := TMaskedMatrixReader.Create(Reader,Selection)
else
begin
Result := nil;
Reader.Free;
raise Exception.Create('Matrix indices within file undefined')
end
else
Result := nil;
end;
Function TMatrixReaderFormat.CreateReader(const [ref] Properties: TPropertySet; const Selection: array of String): TMatrixReader;
begin
var Reader := CreateReader(Properties);
if Reader <> nil then
Result := TMaskedMatrixReader.Create(Reader,Selection)
else
Result := nil;
end;
////////////////////////////////////////////////////////////////////////////////
Class Operator TMatrixFormats.Finalize(var Formats: TMatrixFormats);
begin
// Destroy reader formats
for var Format := low(Formats.ReaderFormats) to high(Formats.ReaderFormats) do
Formats.ReaderFormats[Format].Free;
// Destroy writer formats
for var Format := low(Formats.WriterFormats) to high(Formats.WriterFormats) do
Formats.WriterFormats[Format].Free;
end;
Procedure TMatrixFormats.RegisterFormat(const Format: TMatrixReaderFormat);
begin
var Count := Length(ReaderFormats);
SetLength(ReaderFormats,Count+1);
ReaderFormats[Count] := Format;
end;
Procedure TMatrixFormats.RegisterFormat(const Format: TMatrixWriterFormat);
begin
var Count := Length(WriterFormats);
SetLength(WriterFormats,Count+1);
WriterFormats[Count] := Format;
end;
Function TMatrixFormats.RegisteredReaderFormats: TStringDynArray;
begin
Result := [];
for var Format := low(ReaderFormats) to high(ReaderFormats) do
Result := Result + [ReaderFormats[Format].Format];
end;
Function TMatrixFormats.ReaderFormat(const Format: string): TMatrixFormat;
begin
Result := nil;
for var ReaderFormat := low(ReaderFormats) to high(ReaderFormats) do
if SameText(ReaderFormats[ReaderFormat].Format,Format) then
Exit(ReaderFormats[ReaderFormat]);
end;
Function TMatrixFormats.ReaderFormat(const FileName: TFileName): TMatrixFormat;
Var
Header: TBytes;
begin
Result := nil;
// Determine file format based on file extension
var FileExtension := ExtractFileExt(FileName);
for var ReaderFormat := low(ReaderFormats) to high(ReaderFormats) do
if ReaderFormats[ReaderFormat].HasFormat(FileExtension) then
Exit(ReaderFormats[ReaderFormat]);
// Determine file format based on file header
if FileExists(FileName) then
begin
var FileStream := TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
try
SetLength(Header,HeaderSize);
FileStream.Read(Header,HeaderSize)
finally
FileStream.Free;
end;
Result := ReaderFormat(Header);
end;
end;
Function TMatrixFormats.ReaderFormat(const Header: TBytes): TMatrixFormat;
begin
Result := nil;
for var ReaderFormat := low(ReaderFormats) to high(ReaderFormats) do
if ReaderFormats[ReaderFormat].HasFormat(Header) then
Exit(ReaderFormats[ReaderFormat]);
end;
Function TMatrixFormats.RegisteredWriterFormats: TStringDynArray;
begin
Result := [];
for var Format := low(WriterFormats) to high(WriterFormats) do
Result := Result + [WriterFormats[Format].Format];
end;
Function TMatrixFormats.WriterFormat(const Format: string): TMatrixFormat;
begin
Result := nil;
for var WriterFormat := low(WriterFormats) to high(WriterFormats) do
if SameText(WriterFormats[WriterFormat].Format,Format) then
Exit(WriterFormats[WriterFormat]);
end;
Function TMatrixFormats.CreateReader(const [ref] Properties: TPropertySet; Ordered: Boolean = true): TMatrixReader;
begin
Result := nil;
var Format := Properties[TMatrixFormat.FormatProperty];
for var ReaderFormat := low(ReaderFormats) to high(ReaderFormats) do
if SameText(ReaderFormats[ReaderFormat].Format,Format) then
if ReaderFormats[ReaderFormat].Available then
begin
Result := ReaderFormats[ReaderFormat].CreateReader(Properties);
if Ordered and (not Result.Ordered) then
begin
FreeAndNil(Result);
raise Exception.Create('Matrix indices within file undefined');
end;
end else
Break;
end;
Function TMatrixFormats.CreateReader(const [ref] Properties: TPropertySet;
const Selection: array of Integer): TMatrixReader;
begin
Result := nil;
var Format := Properties[TMatrixFormat.FormatProperty];
for var ReaderFormat := low(ReaderFormats) to high(ReaderFormats) do
if SameText(ReaderFormats[ReaderFormat].Format,Format) then
if ReaderFormats[ReaderFormat].Available then
Exit(ReaderFormats[ReaderFormat].CreateReader(Properties,Selection))
else
Break;
end;
Function TMatrixFormats.CreateReader(const [ref] Properties: TPropertySet;
const Selection: array of String): TMatrixReader;
begin
Result := nil;
var Format := Properties[TMatrixFormat.FormatProperty];
for var ReaderFormat := low(ReaderFormats) to high(ReaderFormats) do
if SameText(ReaderFormats[ReaderFormat].Format,Format) then
if ReaderFormats[ReaderFormat].Available then
Exit(ReaderFormats[ReaderFormat].CreateReader(Properties,Selection))
else
Break;
end;
Function TMatrixFormats.CreateWriter(const [ref] Properties: TPropertySet;
const FileLabel: string;
const MatrixLabels: array of String;
const Size: Integer): TMatrixWriter;
begin
Result := nil;
var Format := Properties[TMatrixFormat.FormatProperty];
for var WriterFormat := low(WriterFormats) to high(WriterFormats) do
if SameText(WriterFormats[WriterFormat].Format,Format) then
if WriterFormats[WriterFormat].Available then
Exit(WriterFormats[WriterFormat].CreateWriter(Properties,FileLabel,MatrixLabels,Size))
else
Break;
end;
Initialization
MatrixFormats.RegisterFormat(TTextMatrixReaderFormat.Create);
MatrixFormats.RegisterFormat(TTextMatrixWriterFormat.Create);
MatrixFormats.RegisterFormat(TMinutpMatrixReaderFormat.Create);
MatrixFormats.RegisterFormat(TMinutpMatrixWriterFormat.Create);
MatrixFormats.RegisterFormat(T4GMatrixReaderFormat.Create);
MatrixFormats.RegisterFormat(T4GMatrixWriterFormat.Create);
MatrixFormats.RegisterFormat(TOMXMatrixReaderFormat.Create);
MatrixFormats.RegisterFormat(TOMXMatrixWriterFormat.Create);
MatrixFormats.RegisterFormat(TCubeMatrixReaderFormat.Create);
MatrixFormats.RegisterFormat(TCubeMatrixWriterFormat.Create);
MatrixFormats.RegisterFormat(TVisumMatrixReaderFormat.Create);
end.