-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPropSet.pas
More file actions
489 lines (439 loc) · 14.5 KB
/
PropSet.pas
File metadata and controls
489 lines (439 loc) · 14.5 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
unit PropSet;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils,Types,IOUtils,BaseDir,Parse;
Type
TPropertySet = record
private
Type
TProperty = record
Name,Value: string;
end;
Var
FReadOnly: Boolean;
FNameValueSeparator: Char;
FPropertiesSeparator: Char;
FProperties: array of TProperty;
Function IndexOf(const Name: String): Integer;
Procedure SetNameValueSeparator(Separator: Char);
Procedure SetPropertiesSeparator(Separator: Char);
Function GetNames(Index: Integer): String; inline;
Function GetValues(const Name: String): String; inline;
Procedure SetValues(const Name,Value: String); inline;
Function GetValueFromIndex(Index: Integer): String;
Procedure SetValueFromIndex(Index: Integer; const Value: String);
Function GetAsString: String;
Procedure SetAsString(AsString: String);
Function GetAsStrings: TStringDynArray;
Procedure SetAsStrings(AsStrings: TStringDynArray);
Procedure Append(const AsString: String); overload;
public
Class Var BaseDirectory: TBaseDirectory;
Class Constructor Create;
public
Class Operator Initialize(out PropertySet: TPropertySet);
Class Operator Assign(var Left: TPropertySet; const [ref] Right: TPropertySet);
Class Operator Implicit(Properties: String): TPropertySet;
Class Operator Implicit(Properties: TPropertySet): String;
Class Operator Implicit(Properties: TStringDynArray): TPropertySet;
Class Operator Implicit(Properties: TPropertySet): TStringDynArray;
public
Property ReadOnly: Boolean read FReadOnly;
// Separator properties
Property NameValueSeparator: Char read FNameValueSeparator write SetNameValueSeparator;
Property PropertiesSeparator: Char read FPropertiesSeparator write SetPropertiesSeparator;
// Type casts
Property AsString: String read GetAsString write SetAsString;
Property AsStrings: TStringDynArray read GetAsStrings write SetAsStrings;
// Query content
Function Count: Integer; inline;
Function Contains(const Name: String): Boolean; overload;
Function Contains(const Name: String; var Value: String): Boolean; overload;
Function ContainsValue(const Name: String): Boolean; overload;
Function ContainsValue(const Name: String; var Value: String): Boolean; overload;
Property Names[Index: Integer]: String read GetNames;
Property Values[const Name: String]: string read GetValues write SetValues; default;
Property ValueFromIndex[Index: Integer]: String read GetValueFromIndex write SetValueFromIndex;
// Convert property values
Function ToStr(const Name,Default: String): String;
Function ToInt(const Name: String): Integer; overload;
Function ToInt(const Name: String; Default: Integer): Integer; overload;
Function ToFloat(const Name: String): Float64; overload;
Function ToFloat(const Name: String; Default: Float64): Float64; overload;
Function ToBool(const Name,FalseStr,TrueStr: String): Boolean; overload;
Function ToBool(const Name,FalseStr,TrueStr: String; Default: Boolean): Boolean; overload;
Function ToPath(const Name: String): String;
Function ToFileName(const Name: String): String; overload;
Function ToFileName(const Name,Extension: String): String; overload;
Function Parse(const Name: String; Delimiter: TDelimiter = Comma): TStringParser;
// Manage content
Constructor Create(ReadOnly: Boolean); overload;
Constructor Create(NameValueSeparator,PropertiesSeparator: Char; ReadOnly: Boolean = false); overload;
Constructor Create(const [ref] Properties: TPropertySet; ReadOnly: Boolean = false); overload;
Procedure Clear;
Procedure RemoveUnassigned;
Procedure Append(const Name,Value: String); overload;
Procedure Append(const Properties: TPropertySet; SkipUnassigned,SkipDuplicates: Boolean); overload;
Procedure Lock;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Class Constructor TPropertySet.Create;
begin
BaseDirectory := ExtractFileDir(ParamStr(0));
end;
Class Operator TPropertySet.Initialize(out PropertySet: TPropertySet);
begin
PropertySet.FReadOnly := false;
PropertySet.FNameValueSeparator := '=';
PropertySet.FPropertiesSeparator := ';';
end;
Class Operator TPropertySet.Assign(var Left: TPropertySet; const [ref] Right: TPropertySet);
begin
Left.FReadOnly := Right.FReadOnly;
Left.FNameValueSeparator := Right.FNameValueSeparator;
Left.FPropertiesSeparator := Right.FPropertiesSeparator;
Left.FProperties := Copy(Right.FProperties);
end;
Class Operator TPropertySet.Implicit(Properties: String): TPropertySet;
begin
Result.AsString := Properties;
end;
Class Operator TPropertySet.Implicit(Properties: TPropertySet): String;
begin
Result := Properties.AsString;
end;
Class Operator TPropertySet.Implicit(Properties: TStringDynArray): TPropertySet;
begin
Result.AsStrings := Properties;
end;
Class Operator TPropertySet.Implicit(Properties: TPropertySet): TStringDynArray;
begin
Result := Properties.AsStrings;
end;
Constructor TPropertySet.Create(ReadOnly: Boolean);
begin
FReadOnly := ReadOnly;
Finalize(FProperties);
end;
Constructor TPropertySet.Create(NameValueSeparator,PropertiesSeparator: Char; ReadOnly: Boolean = false);
begin
FReadOnly := ReadOnly;
FNameValueSeparator := NameValueSeparator;
FPropertiesSeparator := PropertiesSeparator;
Finalize(FProperties);
end;
Constructor TPropertySet.Create(const [ref] Properties: TPropertySet; ReadOnly: Boolean = false);
begin
FReadOnly := ReadOnly;
FNameValueSeparator := Properties.FNameValueSeparator;
FPropertiesSeparator := Properties.FPropertiesSeparator;
FProperties := Copy(Properties.FProperties);
end;
Function TPropertySet.IndexOf(const Name: String): Integer;
begin
Result := -1;
for var Prop := low(FProperties) to high(FProperties) do
if SameText(FProperties[Prop].Name,Name) then
begin
Result := Prop;
Break;
end;
end;
Procedure TPropertySet.SetNameValueSeparator(Separator: Char);
begin
Clear;
FNameValueSeparator := Separator;
end;
Procedure TPropertySet.SetPropertiesSeparator(Separator: Char);
begin
Clear;
FPropertiesSeparator := Separator;
end;
Function TPropertySet.GetNames(Index: Integer): string;
begin
Result := FProperties[Index].Name;
end;
Function TPropertySet.GetValues(const Name: String): String;
begin
Contains(Name,Result);
end;
Procedure TPropertySet.SetValues(const Name,Value: String);
begin
if not FReadOnly then
begin
var Index := IndexOf(Name);
if Index >= 0 then
FProperties[Index].Value := Value
else
raise Exception.Create('Property does not exist (' + Name + ')');
end else
raise Exception.Create('Cannot modify a read only property set');
end;
Function TPropertySet.GetValueFromIndex(Index: Integer): String;
begin
Result := FProperties[Index].Value;
end;
Procedure TPropertySet.SetValueFromIndex(Index: Integer; const Value: String);
begin
if not FReadOnly then
FProperties[Index].Value := Value
else
raise Exception.Create('Cannot modify a read only property set');
end;
Function TPropertySet.GetAsString: String;
begin
if Length(FProperties) > 0 then
begin
Result := FProperties[0].Name + FNameValueSeparator + FProperties[0].Value;
for var Prop := 1 to Count-1 do
Result := Result + FPropertiesSeparator + ' ' + FProperties[Prop].Name + FNameValueSeparator + FProperties[Prop].Value;
end else Result := '';
end;
Procedure TPropertySet.SetAsString(AsString: string);
begin
FProperties := nil;
if AsString <> '' then
begin
var PropertySeparatorPos := Pos(FPropertiesSeparator,AsString);
while PropertySeparatorPos > 0 do
begin
Append(Copy(AsString,1,PropertySeparatorPos-1));
AsString := Trim(Copy(AsString,PropertySeparatorPos+1,MaxInt));
PropertySeparatorPos := Pos(FPropertiesSeparator,AsString);
end;
// Append last property
Append(AsString);
end;
end;
Function TPropertySet.GetAsStrings: TStringDynArray;
begin
SetLength(Result,Count);
for var Prop := 0 to Count-1 do
Result[Prop] := FProperties[Prop].Name + FNameValueSeparator + FProperties[Prop].Value;
end;
Procedure TPropertySet.SetAsStrings(AsStrings: TStringDynArray);
begin
FProperties := nil;
for var Prop := low(AsStrings) to high(AsStrings) do Append(AsStrings[Prop]);
end;
Function TPropertySet.Count: Integer;
begin
Result := Length(FProperties);
end;
Function TPropertySet.Contains(const Name: String): Boolean;
Var
Value: String;
begin
Result := Contains(Name,Value);
end;
Function TPropertySet.Contains(const Name: String; var Value: String): Boolean;
begin
Result := false;
Value := '';
for var Prop := low(FProperties) to high(FProperties) do
if SameText(FProperties[Prop].Name,Name) then
begin
Result := true;
Value := FProperties[Prop].Value;
Break;
end;
end;
Function TPropertySet.ContainsValue(const Name: String): Boolean;
Var
Value: String;
begin
Result := ContainsValue(Name,Value);
end;
Function TPropertySet.ContainsValue(const Name: String; var Value: String): Boolean;
begin
if Contains(Name,Value) then
Result := (Value <> '')
else
Result := false;
end;
Function TPropertySet.ToStr(const Name,Default: String): String;
begin
if not Contains(Name,Result) then Result := Default;
end;
Function TPropertySet.ToInt(const Name: String): Integer;
begin
try
Result := GetValues(Name).ToInteger;
except
raise Exception.Create('Invalid integer value (' + Name + ')');
end;
end;
Function TPropertySet.ToInt(const Name: String; Default: Integer): Integer;
Var
Value: String;
begin
if Contains(Name,Value) then
try
Result := GetValues(Name).ToInteger;
except
raise Exception.Create('Invalid integer value (' + Name + ')');
end
else
Result := Default;
end;
Function TPropertySet.ToFloat(const Name: String): Float64;
begin
try
Result := GetValues(Name).ToDouble;
except
raise Exception.Create('Invalid floating point value (' + Name + ')');
end;
end;
Function TPropertySet.ToFloat(const Name: String; Default: Float64): Float64;
Var
Value: String;
begin
if Contains(Name,Value) then
try
Result := GetValues(Name).ToDouble;
except
raise Exception.Create('Invalid floating point value (' + Name + ')');
end
else
Result := Default;
end;
Function TPropertySet.ToBool(const Name,FalseStr,TrueStr: String): Boolean;
begin
var Value := GetValues(Name);
if SameText(Value,FalseStr) then Result := false else
if SameText(Value,TrueStr) then Result := true else
raise Exception.Create('Invalid boolean value (' + Name + ')');
end;
Function TPropertySet.ToBool(const Name,FalseStr,TrueStr: String; Default: Boolean): Boolean;
Var
Value: String;
begin
if Contains(Name,Value) then
begin
if SameText(Value,FalseStr) then Result := false else
if SameText(Value,TrueStr) then Result := true else
raise Exception.Create('Invalid boolean value (' + Name + ')')
end else
Result := Default;
end;
Function TPropertySet.ToPath(const Name: String): String;
begin
if Contains(Name,Result) then
Result := BaseDirectory.AbsolutePath(Result)
else
Result := '';
end;
Function TPropertySet.ToFileName(const Name: String): String;
begin
// Set file name
if Contains(Name,Result) then
if Result <> '' then
Result := BaseDirectory.AbsolutePath(Result)
else
Result := ''
else
Result := '';
end;
Function TPropertySet.ToFileName(const Name,Extension: String): String;
begin
// Set file name
if Contains(Name,Result) then
if Result <> '' then
Result := ChangeFileExt(BaseDirectory.AbsolutePath(Result),Extension)
else
Result := ''
else
Result := '';
end;
Function TPropertySet.Parse(const Name: String; Delimiter: TDelimiter = Comma): TStringParser;
Var
Value: string;
begin
if Contains(Name,Value) then
Result := TStringParser.Create(Delimiter,Value)
else
Result := TStringParser.Create(Delimiter,'');
end;
Procedure TPropertySet.Clear;
begin
Finalize(FProperties);
end;
Procedure TPropertySet.RemoveUnassigned;
begin
var Index := 0;
for var Prop := low(FProperties) to high(FProperties) do
if FProperties[Prop].Value <> '' then
begin
if Index < Prop then FProperties[Index] := FProperties[Prop];
Inc(Index);
end;
SetLength(FProperties,Index);
end;
Procedure TPropertySet.Append(const AsString: String);
begin
if AsString <> '' then
begin
var NameValueSeparatorPos := Pos(FNameValueSeparator,AsString);
if NameValueSeparatorPos > 0 then
begin
var Name := Trim(Copy(AsString,1,NameValueSeparatorPos-1));
if Name <> '' then
if IndexOf(Name) < 0 then
begin
var Value := Trim(Copy(AsString,NameValueSeparatorPos+1,MaxInt));
Append(Name,Value);
end else
raise Exception.Create('Duplicate property (' + Name + ')')
else
raise Exception.Create('Missing Property Name');
end else
raise Exception.Create('Missing Name-Value separator (' + AsString + ')');
end;
end;
Procedure TPropertySet.Append(const Name,Value: String);
begin
if Name <> '' then
if IndexOf(Name) < 0 then
begin
var Index := Count;
SetLength(FProperties,Index+1);
FProperties[Index].Name := Name;
FProperties[Index].Value := Value;
end else
raise Exception.Create('Duplicate property (' + Name + ')')
else
raise Exception.Create('Missing Property Name');
end;
Procedure TPropertySet.Append(const Properties: TPropertySet; SkipUnassigned,SkipDuplicates: Boolean);
begin
var Index := Count;
SetLength(FProperties,Count+Properties.Count);
for var Prop := 0 to Properties.Count-1 do
begin
var PropertyIndex := IndexOf(Properties.Names[Prop]);
if ((not SkipUnassigned) or (Properties.FProperties[Prop].Value <> ''))
and ((not SkipDuplicates) or (PropertyIndex < 0)) then
if PropertyIndex < 0 then
begin
FProperties[Index] := Properties.FProperties[Prop];
Inc(Index);
end else
raise Exception.Create('Duplicate property (' + Properties.Names[Prop] + ')')
end;
SetLength(FProperties,Index);
end;
Procedure TPropertySet.Lock;
begin
FReadOnly := true;
end;
end.