-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseConfig.pas
More file actions
648 lines (555 loc) · 15.4 KB
/
BaseConfig.pas
File metadata and controls
648 lines (555 loc) · 15.4 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
unit BaseConfig;
interface
uses
System.SysUtils, System.Classes, System.JSON, JSONHelpers, System.IniFiles;
type
// 配置存储基类
TBaseConfig = class abstract
protected
FFileName: string;
FModified: Boolean;
public
constructor Create(const AFileName: string); virtual;
destructor Destroy; override;
// 文件操作
function Exists: Boolean; virtual;
function CreateFile: Boolean; virtual; abstract;
function Reload: Boolean; virtual; abstract;
function Save: Boolean; virtual; abstract;
function SaveToFile(const FileName: string): Boolean; virtual; abstract;
// 属性
property FileName: string read FFileName write FFileName;
property Modified: Boolean read FModified write FModified;
end;
// JSON配置类
TJSONConfig = class(TBaseConfig)
private
FRoot: TJSONObject;
public
constructor Create(const AFileName: string); override;
destructor Destroy; override;
// 文件操作
function CreateFile: Boolean; override;
function Reload: Boolean; override;
function Save: Boolean; override;
function SaveToFile(const FileName: string): Boolean; override;
// 读取操作
function ReadString(const Path: string; const DefaultValue: string = ''): string;
function ReadInteger(const Path: string; DefaultValue: Integer = 0): Integer;
function ReadFloat(const Path: string; DefaultValue: Double = 0): Double;
function ReadBoolean(const Path: string; DefaultValue: Boolean = False): Boolean;
function ReadDateTime(const Path: string; DefaultValue: TDateTime = 0): TDateTime;
// 写入操作
procedure WriteString(const Path, Value: string);
procedure WriteInteger(const Path: string; Value: Integer);
procedure WriteFloat(const Path: string; Value: Double);
procedure WriteBoolean(const Path: string; Value: Boolean);
procedure WriteDateTime(const Path: string; Value: TDateTime);
procedure WriteObject(const Path: string; Value: TJSONObject);
// JSON根节点
property Root: TJSONObject read FRoot;
end;
// INI配置类
TINIConfig = class(TBaseConfig)
private
FIniFile: TMemIniFile;
public
constructor Create(const AFileName: string); override;
destructor Destroy; override;
// 文件操作
function CreateFile: Boolean; override;
function Reload: Boolean; override;
function Save: Boolean; override;
function SaveToFile(const FileName: string): Boolean; override;
// 读取操作
function ReadString(const Section, Name, DefaultValue: string): string;
function ReadInteger(const Section, Name: string; DefaultValue: Integer): Integer;
function ReadFloat(const Section, Name: string; DefaultValue: Double): Double;
function ReadBool(const Section, Name: string; DefaultValue: Boolean): Boolean;
function ReadDateTime(const Section, Name: string; DefaultValue: TDateTime): TDateTime;
// 写入操作
procedure WriteString(const Section, Name, Value: string);
procedure WriteInteger(const Section, Name: string; Value: Integer);
procedure WriteFloat(const Section, Name: string; Value: Double);
procedure WriteBool(const Section, Name: string; Value: Boolean);
procedure WriteDateTime(const Section, Name: string; Value: TDateTime);
// INI文件访问
property IniFile: TMemIniFile read FIniFile;
end;
implementation
uses
System.IOUtils, System.DateUtils, System.Variants;
{ TBaseConfig }
constructor TBaseConfig.Create(const AFileName: string);
begin
inherited Create;
FFileName := AFileName;
FModified := False;
end;
destructor TBaseConfig.Destroy;
begin
if FModified then
try
Save;
except
// 忽略保存错误
end;
inherited;
end;
function TBaseConfig.Exists: Boolean;
begin
Result := TFile.Exists(FFileName);
end;
{ TJSONConfig }
constructor TJSONConfig.Create(const AFileName: string);
begin
inherited Create(AFileName);
FRoot := TJSONObject.Create;
// 如果文件存在,则加载
if Exists then
Reload
else
CreateFile;
end;
destructor TJSONConfig.Destroy;
begin
FRoot.Free;
inherited;
end;
function TJSONConfig.CreateFile: Boolean;
begin
try
// 确保目录存在
TDirectory.CreateDirectory(ExtractFilePath(FFileName));
// 创建一个空的JSON文件
Save;
Result := True;
except
Result := False;
end;
end;
function TJSONConfig.Reload: Boolean;
var
FileStream: TFileStream;
StringStream: TStringStream;
JSONValue: TJSONValue;
begin
Result := False;
if not Exists then
Exit;
try
// 读取JSON文件内容
FileStream := TFileStream.Create(FFileName, fmOpenRead or fmShareDenyWrite);
try
StringStream := TStringStream.Create('', TEncoding.UTF8);
try
StringStream.CopyFrom(FileStream, 0);
// 解析JSON内容
JSONValue := TJSONObject.ParseJSONValue(StringStream.DataString);
if Assigned(JSONValue) and (JSONValue is TJSONObject) then
begin
// 释放旧的根节点
FRoot.Free;
FRoot := JSONValue as TJSONObject;
Result := True;
end
else
begin
// 解析失败,创建空的JSON对象
if Assigned(JSONValue) then
JSONValue.Free;
FRoot.Free;
FRoot := TJSONObject.Create;
end;
finally
StringStream.Free;
end;
finally
FileStream.Free;
end;
except
// 读取或解析出错,保持空的JSON对象
FRoot.Free;
FRoot := TJSONObject.Create;
end;
end;
function TJSONConfig.Save: Boolean;
var
FileStream: TFileStream;
StringStream: TStringStream;
JSONString: string;
begin
Result := False;
try
// 创建目录(如果不存在)
TDirectory.CreateDirectory(ExtractFilePath(FFileName));
// 将JSON对象转换为字符串
JSONString := FRoot.ToString;
// 创建字符串流和文件流
StringStream := TStringStream.Create(JSONString, TEncoding.UTF8);
try
FileStream := TFileStream.Create(FFileName, fmCreate);
try
// 写入文件
FileStream.CopyFrom(StringStream, 0);
Result := True;
FModified := False;
finally
FileStream.Free;
end;
finally
StringStream.Free;
end;
except
// 写入出错
Result := False;
end;
end;
function TJSONConfig.SaveToFile(const FileName: string): Boolean;
var
OldFileName: string;
begin
OldFileName := FFileName;
FFileName := FileName;
try
Result := Save;
finally
FFileName := OldFileName;
end;
end;
function TJSONConfig.ReadString(const Path: string; const DefaultValue: string): string;
var
PathParts: TArray<string>;
CurrentObj: TJSONObject;
CurrentValue: TJSONValue;
I: Integer;
begin
Result := DefaultValue;
if Path = '' then
Exit;
// 分割路径
PathParts := Path.Split(['/']);
// 从根节点开始查找
CurrentObj := FRoot;
// 遍历路径
for I := 0 to Length(PathParts) - 2 do
begin
CurrentValue := CurrentObj.GetValue(PathParts[I]);
if not Assigned(CurrentValue) or not (CurrentValue is TJSONObject) then
Exit;
CurrentObj := CurrentValue as TJSONObject;
end;
// 获取最终节点的值
if Length(PathParts) > 0 then
begin
CurrentValue := CurrentObj.GetValue(PathParts[Length(PathParts) - 1]);
if Assigned(CurrentValue) then
begin
if CurrentValue is TJSONString then
Result := (CurrentValue as TJSONString).Value
else
Result := CurrentValue.ToString;
end;
end;
end;
function TJSONConfig.ReadInteger(const Path: string; DefaultValue: Integer): Integer;
var
StrValue: string;
begin
StrValue := ReadString(Path);
if StrValue = '' then
Result := DefaultValue
else
Result := StrToIntDef(StrValue, DefaultValue);
end;
function TJSONConfig.ReadFloat(const Path: string; DefaultValue: Double): Double;
var
StrValue: string;
begin
StrValue := ReadString(Path);
if StrValue = '' then
Result := DefaultValue
else
Result := StrToFloatDef(StrValue, DefaultValue);
end;
function TJSONConfig.ReadBoolean(const Path: string; DefaultValue: Boolean): Boolean;
var
StrValue: string;
begin
StrValue := ReadString(Path);
if StrValue = '' then
Result := DefaultValue
else
Result := StrToBoolDef(StrValue, DefaultValue);
end;
function TJSONConfig.ReadDateTime(const Path: string; DefaultValue: TDateTime): TDateTime;
var
StrValue: string;
begin
StrValue := ReadString(Path);
if StrValue = '' then
Result := DefaultValue
else
Result := StrToDateTimeDef(StrValue, DefaultValue);
end;
procedure TJSONConfig.WriteString(const Path, Value: string);
var
PathParts: TArray<string>;
CurrentObj: TJSONObject;
CurrentValue: TJSONValue;
I: Integer;
begin
if Path = '' then
Exit;
// 分割路径
PathParts := Path.Split(['/']);
// 从根节点开始查找
CurrentObj := FRoot;
// 遍历路径,创建中间节点(如果不存在)
for I := 0 to Length(PathParts) - 2 do
begin
CurrentValue := CurrentObj.GetValue(PathParts[I]);
if not Assigned(CurrentValue) then
begin
// 创建新节点
CurrentValue := TJSONObject.Create;
CurrentObj.AddPair(PathParts[I], CurrentValue);
end
else if not (CurrentValue is TJSONObject) then
begin
// 如果存在但不是对象,则替换为对象
CurrentObj.RemovePair(PathParts[I]);
CurrentValue := TJSONObject.Create;
CurrentObj.AddPair(PathParts[I], CurrentValue);
end;
CurrentObj := CurrentValue as TJSONObject;
end;
// 设置最终节点的值
if Length(PathParts) > 0 then
begin
CurrentObj.RemovePair(PathParts[Length(PathParts) - 1]);
CurrentObj.AddPair(PathParts[Length(PathParts) - 1], Value);
FModified := True;
end;
end;
procedure TJSONConfig.WriteInteger(const Path: string; Value: Integer);
begin
WriteString(Path, IntToStr(Value));
end;
procedure TJSONConfig.WriteFloat(const Path: string; Value: Double);
begin
WriteString(Path, FloatToStr(Value));
end;
procedure TJSONConfig.WriteBoolean(const Path: string; Value: Boolean);
begin
WriteString(Path, BoolToStr(Value, True));
end;
procedure TJSONConfig.WriteDateTime(const Path: string; Value: TDateTime);
begin
WriteString(Path, DateTimeToStr(Value));
end;
procedure TJSONConfig.WriteObject(const Path: string; Value: TJSONObject);
var
PathParts: TArray<string>;
CurrentObj: TJSONObject;
CurrentValue: TJSONValue;
I: Integer;
begin
if (Path = '') or (Value = nil) then
Exit;
// 分割路径
PathParts := Path.Split(['/']);
// 从根节点开始查找
CurrentObj := FRoot;
// 遍历路径,创建中间节点(如果不存在)
for I := 0 to Length(PathParts) - 2 do
begin
CurrentValue := CurrentObj.GetValue(PathParts[I]);
if not Assigned(CurrentValue) then
begin
// 创建新节点
CurrentValue := TJSONObject.Create;
CurrentObj.AddPair(PathParts[I], CurrentValue);
end
else if not (CurrentValue is TJSONObject) then
begin
// 如果存在但不是对象,则替换为对象
CurrentObj.RemovePair(PathParts[I]);
CurrentValue := TJSONObject.Create;
CurrentObj.AddPair(PathParts[I], CurrentValue);
end;
CurrentObj := CurrentValue as TJSONObject;
end;
// 设置最终节点的值
if Length(PathParts) > 0 then
begin
CurrentObj.RemovePair(PathParts[Length(PathParts) - 1]);
CurrentObj.AddPair(PathParts[Length(PathParts) - 1], Value.Clone as TJSONValue);
FModified := True;
end;
end;
{ TINIConfig }
constructor TINIConfig.Create(const AFileName: string);
begin
inherited Create(AFileName);
// 如果文件不存在,则创建
if not Exists then
CreateFile;
FIniFile := TMemIniFile.Create(FFileName);
end;
destructor TINIConfig.Destroy;
begin
if FModified then
Save;
FIniFile.Free;
inherited;
end;
function TINIConfig.CreateFile: Boolean;
var
StringList: TStringList;
begin
try
// 确保目录存在
TDirectory.CreateDirectory(ExtractFilePath(FFileName));
// 创建一个空的INI文件
StringList := TStringList.Create;
try
StringList.SaveToFile(FFileName);
finally
StringList.Free;
end;
Result := True;
except
Result := False;
end;
end;
function TINIConfig.Reload: Boolean;
begin
try
if Assigned(FIniFile) then
FIniFile.Free;
FIniFile := TMemIniFile.Create(FFileName);
Result := True;
except
Result := False;
end;
end;
function TINIConfig.Save: Boolean;
begin
try
if Assigned(FIniFile) then
begin
FIniFile.UpdateFile;
FModified := False;
Result := True;
end
else
Result := False;
except
Result := False;
end;
end;
function TINIConfig.SaveToFile(const FileName: string): Boolean;
var
OldFileName: string;
begin
Result := False; // 设置默认返回值
if not Assigned(FIniFile) then
Exit;
OldFileName := FFileName;
try
FFileName := FileName;
FIniFile.Rename(FileName, False);
Result := Save;
finally
if not Result then
begin
FFileName := OldFileName;
FIniFile.Rename(OldFileName, False);
end;
end;
end;
function TINIConfig.ReadString(const Section, Name, DefaultValue: string): string;
begin
if Assigned(FIniFile) then
Result := FIniFile.ReadString(Section, Name, DefaultValue)
else
Result := DefaultValue;
end;
function TINIConfig.ReadInteger(const Section, Name: string; DefaultValue: Integer): Integer;
begin
if Assigned(FIniFile) then
Result := FIniFile.ReadInteger(Section, Name, DefaultValue)
else
Result := DefaultValue;
end;
function TINIConfig.ReadFloat(const Section, Name: string; DefaultValue: Double): Double;
begin
if Assigned(FIniFile) then
Result := FIniFile.ReadFloat(Section, Name, DefaultValue)
else
Result := DefaultValue;
end;
function TINIConfig.ReadBool(const Section, Name: string; DefaultValue: Boolean): Boolean;
begin
if Assigned(FIniFile) then
Result := FIniFile.ReadBool(Section, Name, DefaultValue)
else
Result := DefaultValue;
end;
function TINIConfig.ReadDateTime(const Section, Name: string; DefaultValue: TDateTime): TDateTime;
var
DateStr: string;
begin
if Assigned(FIniFile) then
begin
DateStr := FIniFile.ReadString(Section, Name, '');
if DateStr <> '' then
Result := StrToDateTimeDef(DateStr, DefaultValue)
else
Result := DefaultValue;
end
else
Result := DefaultValue;
end;
procedure TINIConfig.WriteString(const Section, Name, Value: string);
begin
if Assigned(FIniFile) then
begin
FIniFile.WriteString(Section, Name, Value);
FModified := True;
end;
end;
procedure TINIConfig.WriteInteger(const Section, Name: string; Value: Integer);
begin
if Assigned(FIniFile) then
begin
FIniFile.WriteInteger(Section, Name, Value);
FModified := True;
end;
end;
procedure TINIConfig.WriteFloat(const Section, Name: string; Value: Double);
begin
if Assigned(FIniFile) then
begin
FIniFile.WriteFloat(Section, Name, Value);
FModified := True;
end;
end;
procedure TINIConfig.WriteBool(const Section, Name: string; Value: Boolean);
begin
if Assigned(FIniFile) then
begin
FIniFile.WriteBool(Section, Name, Value);
FModified := True;
end;
end;
procedure TINIConfig.WriteDateTime(const Section, Name: string; Value: TDateTime);
begin
if Assigned(FIniFile) then
begin
FIniFile.WriteString(Section, Name, DateTimeToStr(Value));
FModified := True;
end;
end;
end.