Source

asconfig / Src / ASConfig.pas

Full commit
  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
unit ASConfig;

interface

uses
  Windows, Classes, SysUtils, Rtti, TypInfo, IniFiles, ASConfig.IniFileEx, ASConfigInterface;

type
  TSavePropMethod = procedure(const Name: string; const Value: TValue) of object;
  TSavePropMethodMap = array[TTypeKind] of TSavePropMethod;

  TLoadPropMethod = function(const Name: string; const Default: TValue): TValue of object;
  TLoadPropMethodMap = array[TTypeKind] of TLoadPropMethod;

  TASCustomConfig = class(TComponent)
  private
    FIniFile: IASConfigFile;
    FParentConfig: TASCustomConfig;
    FSavePropMethodMap: TSavePropMethodMap;
    FLoadPropMethodMap: TLoadPropMethodMap;
    procedure SetParentConfig(const Value: TASCustomConfig);

  protected
    procedure BeforeSave(const Obj: TObject; const Path: string); virtual;
    procedure AfterSave(const Obj: TObject; const Path: string); virtual;
    procedure BeforeLoad(const Obj: TObject; const Path: string); virtual;
    procedure AfterLoad(const Obj: TObject; const Path: string); virtual;

    property SavePropMethodMap: TSavePropMethodMap read FSavePropMethodMap write FSavePropMethodMap;
    property LoadPropMethodMap: TLoadPropMethodMap read FLoadPropMethodMap write FLoadPropMethodMap;
  public
    procedure Save(const Obj: TObject; const Path: string = 'Default');
    procedure Load(const Obj: TObject; const Path: string = 'Default');
    procedure Clear(ClearParents: Boolean = False); virtual;
    property IniFile: IASConfigFile read FIniFile;
    property ParentConfig: TASCustomConfig read FParentConfig write SetParentConfig;
  end;

  TASCustomConfigIniFile = class(TASCustomConfig)
  private
    FFileName: string;
    FSection: string;

  protected
    procedure SaveInteger(const Name: string; const Value: TValue);
    procedure SaveString(const Name: string; const Value: TValue);
    procedure SaveFloat(const Name: string; const Value: TValue);
    function LoadInteger(const Name: string; const Default: TValue): TValue;
    function LoadString(const Name: string; const Default: TValue): TValue;
    function LoadFloat(const Name: string; const Default: TValue): TValue;

    function CreateIniFile: TCustomIniFile; virtual;
    procedure ClearIniFile; virtual;
    procedure OpenIniFile(const Section: string); virtual;
    procedure CloseIniFile; virtual;


    procedure BeforeSave(const Obj: TObject; const Path: string); override;
    procedure AfterSave(const Obj: TObject; const Path: string); override;
    procedure BeforeLoad(const Obj: TObject; const Path: string); override;
    procedure AfterLoad(const Obj: TObject; const Path: string); override;

    property Section: string read FSection write FSection;

  public
    constructor Create(AOwner: TComponent); override;
    procedure Clear(ClearParents: Boolean = False); override;
    property FileName: string read FFileName write FFileName;
  end;

  TASCustomConfigRegistryIniFile = class(TASCustomConfigIniFile)
  private
    FRootKey: HKEY;
    procedure SetRootKey(const Value: HKEY);
    function GetPath: string;
    procedure SetPath(const Value: string);

  protected
    function CreateIniFile: TCustomIniFile; override;
    procedure ClearIniFile; override;

    property FileName;

  public
    constructor Create(AOwner: TComponent); override;
    property RootKey: HKEY read FRootKey write SetRootKey default HKEY_CURRENT_USER;
    property Path: string read GetPath write SetPath;
  end;

  TASConfigIniFile = class(TASCustomConfigIniFile)
  published
    property FileName;
    property ParentConfig;
  end;

  TASConfigRegistryIniFile = class(TASCustomConfigRegistryIniFile)
  private
    function GetRootKeyName: string;
    procedure SetRootKeyName(const Value: string);
    function GetRootKeyNameStored: Boolean;
  published
    property ParentConfig;
    property Path;
    property RootKeyName: string read GetRootKeyName write SetRootKeyName stored GetRootKeyNameStored;
  end;

  TASConfigStore = class(TComponent)
  private
    FConfig: TASCustomConfig;
  public
    procedure Save(const Obj: TObject; const Path: string = 'Default');
    procedure Load(const Obj: TObject; const Path: string = 'Default');

  published
    property Config: TASCustomConfig read FConfig write FConfig;
  end;

procedure Register;

implementation

uses
  Registry, StrUtils, Math;

const
  KeyNames: array[HKEY_CLASSES_ROOT..HKEY_DYN_DATA] of string = (
    'HKEY_CLASSES_ROOT', 'HKEY_CURRENT_USER', 'HKEY_LOCAL_MACHINE',
    'HKEY_USERS', 'HKEY_PERFORMANCE_DATA', 'HKEY_CURRENT_CONFIG', 'HKEY_DYN_DATA');

  KeyValues: array[0 .. HKEY_DYN_DATA - HKEY_CLASSES_ROOT] of HKEY = (
    HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE,
    HKEY_USERS, HKEY_PERFORMANCE_DATA, HKEY_CURRENT_CONFIG, HKEY_DYN_DATA);

procedure Register;
begin
  RegisterComponents('ASTEKK', [TASConfigStore, TASConfigIniFile, TASConfigRegistryIniFile]);
end;

{ TASCustomConfig }

procedure TASCustomConfig.AfterLoad(const Obj: TObject; const Path: string);
begin
end;

procedure TASCustomConfig.AfterSave(const Obj: TObject; const Path: string);
begin
end;

procedure TASCustomConfig.BeforeLoad(const Obj: TObject; const Path: string);
begin
end;

procedure TASCustomConfig.BeforeSave(const Obj: TObject; const Path: string);
begin
end;

procedure TASCustomConfig.Clear(ClearParents: Boolean);
begin
  if ClearParents and Assigned(FParentConfig) then
    ParentConfig.Clear(True);
end;

procedure TASCustomConfig.Load(const Obj: TObject; const Path: string);
var
  Ctx: TRttiContext;
  T: TRttiType;
  P: TRttiProperty;
  L: TLoadPropMethod;
begin
  BeforeLoad(Obj, Path);

  Ctx := TRttiContext.Create;
  try
    T := Ctx.GetType(Obj.ClassType);
    for P in T.GetProperties do begin
      if not ((P.Visibility in [mvPublic, mvPublished]) and P.IsReadable) then
        Continue;

      L := LoadPropMethodMap[P.PropertyType.TypeKind];
      if Assigned(L) then
        P.SetValue(Obj, L(P.Name, P.GetValue(Obj)));
    end;
  finally
    Ctx.Free;
  end;

  AfterLoad(Obj, Path);
end;

procedure TASCustomConfig.Save(const Obj: TObject; const Path: string);
var
  Ctx: TRttiContext;
  T: TRttiType;
  P: TRttiProperty;
  S: TSavePropMethod;
begin
  BeforeSave(Obj, Path);

  Ctx := TRttiContext.Create;
  try
    T := Ctx.GetType(Obj.ClassType);
    for P in T.GetProperties do begin
      if not ((P.Visibility in [mvPublic, mvPublished]) and P.IsReadable) then
        Continue;

      S := SavePropMethodMap[P.PropertyType.TypeKind];
      if Assigned(S) then
        S(P.Name, P.GetValue(Obj));
    end;
  finally
    Ctx.Free;
  end;

  AfterSave(Obj, Path);
end;

procedure TASCustomConfig.SetParentConfig(const Value: TASCustomConfig);
var
  I: TASCustomConfig;
begin
  I := Value;
  while Assigned(I) do begin
    if I = Self then
      Exit;

    I := I.ParentConfig;
  end;

  if Value <> Self then
    FParentConfig := Value;

  if Assigned(FIniFile) then begin
    if Assigned(Value) then
      IniFile.ParentFile := Value.IniFile
    else
      IniFile.ParentFile := nil;
  end;
end;

{ TASCustomConfigIniFile }

procedure TASCustomConfigIniFile.AfterLoad(const Obj: TObject; const Path: string);
begin
  CloseIniFile;

  if Assigned(FParentConfig) then
    ParentConfig.AfterLoad(Obj, Path);
end;

procedure TASCustomConfigIniFile.AfterSave(const Obj: TObject; const Path: string);
begin
  CloseIniFile;

  if Assigned(FParentConfig) then
    ParentConfig.AfterSave(Obj, Path);
end;

procedure TASCustomConfigIniFile.BeforeLoad(const Obj: TObject; const Path: string);
begin
  if Assigned(FParentConfig) then
    ParentConfig.BeforeLoad(Obj, Path);

  OpenIniFile(Path);
end;

procedure TASCustomConfigIniFile.BeforeSave(const Obj: TObject; const Path: string);
begin
  if Assigned(FParentConfig) then
    ParentConfig.BeforeSave(Obj, Path);

  OpenIniFile(Path);
end;

procedure TASCustomConfigIniFile.Clear(ClearParents: Boolean);
begin
  ClearIniFile;
  inherited;
end;

procedure TASCustomConfigIniFile.ClearIniFile;
begin
  if FileExists(FileName) then
    DeleteFile(FileName);
end;

procedure TASCustomConfigIniFile.CloseIniFile;
begin
  FIniFile := nil;
end;

constructor TASCustomConfigIniFile.Create(AOwner: TComponent);
var
  SaveMap: TSavePropMethodMap;
  LoadMap: TLoadPropMethodMap;
begin
  inherited Create(AOwner);

  SaveMap[tkUnknown     ] := nil;
  SaveMap[tkInteger     ] := SaveInteger;
  SaveMap[tkChar        ] := nil;
  SaveMap[tkEnumeration ] := nil;
  SaveMap[tkFloat       ] := SaveFloat;
  SaveMap[tkString      ] := SaveString;
  SaveMap[tkSet         ] := nil;
  SaveMap[tkClass       ] := nil;
  SaveMap[tkMethod      ] := nil;
  SaveMap[tkWChar       ] := nil;
  SaveMap[tkLString     ] := SaveString;
  SaveMap[tkWString     ] := SaveString;
  SaveMap[tkVariant     ] := nil;
  SaveMap[tkArray       ] := nil;
  SaveMap[tkRecord      ] := nil;
  SaveMap[tkInterface   ] := nil;
  SaveMap[tkInt64       ] := nil;
  SaveMap[tkDynArray    ] := nil;
  SaveMap[tkUString     ] := SaveString;
  SaveMap[tkClassRef    ] := nil;
  SaveMap[tkPointer     ] := nil;
  SaveMap[tkProcedure   ] := nil;
  SavePropMethodMap := SaveMap;

  LoadMap[tkUnknown     ] := nil;
  LoadMap[tkInteger     ] := LoadInteger;
  LoadMap[tkChar        ] := nil;
  LoadMap[tkEnumeration ] := nil;
  LoadMap[tkFloat       ] := LoadFloat;
  LoadMap[tkString      ] := LoadString;
  LoadMap[tkSet         ] := nil;
  LoadMap[tkClass       ] := nil;
  LoadMap[tkMethod      ] := nil;
  LoadMap[tkWChar       ] := nil;
  LoadMap[tkLString     ] := LoadString;
  LoadMap[tkWString     ] := LoadString;
  LoadMap[tkVariant     ] := nil;
  LoadMap[tkArray       ] := nil;
  LoadMap[tkRecord      ] := nil;
  LoadMap[tkInterface   ] := nil;
  LoadMap[tkInt64       ] := nil;
  LoadMap[tkDynArray    ] := nil;
  LoadMap[tkUString     ] := LoadString;
  LoadMap[tkClassRef    ] := nil;
  LoadMap[tkPointer     ] := nil;
  LoadMap[tkProcedure   ] := nil;
  LoadPropMethodMap := LoadMap;
end;

function TASCustomConfigIniFile.CreateIniFile: TCustomIniFile;
begin
  Result := TIniFile.Create(FileName);
end;

function TASCustomConfigIniFile.LoadFloat(const Name: string; const Default: TValue): TValue;
begin
  Result := TValue.From(IniFile.ReadFloat(Section, Name, Default.AsExtended));
end;

function TASCustomConfigIniFile.LoadInteger(const Name: string; const Default: TValue): TValue;
begin
  Result := TValue.From(IniFile.ReadInteger(Section, Name, Default.AsInteger));
end;

function TASCustomConfigIniFile.LoadString(const Name: string; const Default: TValue): TValue;
begin
  Result := TValue.From(IniFile.ReadString(Section, Name, Default.AsString));
end;

procedure TASCustomConfigIniFile.OpenIniFile(const Section: string);
var
  Parent: IASConfigFile;
begin
  if Assigned(FIniFile) then
    CloseIniFile;

  if Assigned(FParentConfig) then
    Parent := ParentConfig.IniFile;

  Self.Section := Section;
  FIniFile := TASConfigIniFileEx.Create(Name, CreateIniFile, Parent);
end;

procedure TASCustomConfigIniFile.SaveFloat(const Name: string; const Value: TValue);
begin
  IniFile.WriteFloat(Section, Name, Value.AsExtended);
end;

procedure TASCustomConfigIniFile.SaveInteger(const Name: string; const Value: TValue);
begin
  IniFile.WriteInteger(Section, Name, Value.AsInteger);
end;

procedure TASCustomConfigIniFile.SaveString(const Name: string; const Value: TValue);
begin
  IniFile.WriteString(Section, Name, Value.AsString);
end;

{ TASConfigStore }

procedure TASConfigStore.Load(const Obj: TObject; const Path: string);
begin
  Config.Load(Obj, Path);
end;

procedure TASConfigStore.Save(const Obj: TObject; const Path: string);
begin
  Config.Save(Obj, Path);
end;

{ TASCustomConfigRegistry }

procedure TASCustomConfigRegistryIniFile.ClearIniFile;
var
  R: TRegistry;
begin
  R := TRegistry.Create;
  try
    R.RootKey := RootKey;
    R.DeleteKey(Path);
  finally
    R.Free;
  end;
end;

constructor TASCustomConfigRegistryIniFile.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FRootKey := HKEY_CURRENT_USER;
end;

function TASCustomConfigRegistryIniFile.CreateIniFile: TCustomIniFile;
begin
  Result := TRegistryIniFile.Create(FileName);
  (Result as TRegistryIniFile).RegIniFile.RootKey := RootKey;
end;

function TASCustomConfigRegistryIniFile.GetPath: string;
begin
  Result := FileName;
end;

procedure TASCustomConfigRegistryIniFile.SetPath(const Value: string);
begin
  FileName := Value;
end;

procedure TASCustomConfigRegistryIniFile.SetRootKey(const Value: HKEY);
begin
  FRootKey := Value;

  if Assigned(FIniFile) then
    (IniFile.IniFile as TRegistryIniFile).RegIniFile.RootKey := Value;
end;

{ TASConfigRegistryIniFile }

function TASConfigRegistryIniFile.GetRootKeyName: string;
begin
  if (RootKey < HKEY_CLASSES_ROOT) or (RootKey > HKEY_DYN_DATA) then
    RootKey := HKEY_CURRENT_USER;

  Result := KeyNames[RootKey];
end;

function TASConfigRegistryIniFile.GetRootKeyNameStored: Boolean;
begin
  Result := RootKey <> HKEY_CURRENT_USER;
end;

procedure TASConfigRegistryIniFile.SetRootKeyName(const Value: string);
var
  I: Integer;
begin
  if Value <> '' then begin
    I := IndexText(Value, KeyNames);
    if InRange(I, Low(KeyValues), High(KeyValues)) then
      RootKey := KeyValues[I];
  end else
    RootKey := HKEY_CURRENT_USER;
end;

end.