-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProjectSettings.pas
96 lines (78 loc) · 1.88 KB
/
ProjectSettings.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
unit ProjectSettings;
interface
uses
Classes;
type
TProjectSettings = class
private
fRootFileName: String;
fSearchDirs: TStrings;
public
constructor Create;
destructor Destroy; override;
procedure LoadFromFile(FileName: String);
procedure SaveToFile(FileName: String);
property RootFileName: String read fRootFileName write fRootFileName;
property SearchDirs: TStrings read fSearchDirs;
end;
implementation
uses
IoUtils,
System.JSON,
SysUtils;
{ TProjectSettings }
constructor TProjectSettings.Create;
begin
fRootFileName:='';
fSearchDirs:=TStringList.Create;
end;
destructor TProjectSettings.Destroy;
begin
FreeAndNil(fSearchDirs);
inherited;
end;
procedure TProjectSettings.LoadFromFile(FileName: String);
var
Text : String;
JSON : TJSONValue;
DirArray: TJSONArray;
Dir : TJSONValue;
begin
fRootFileName:='';
fSearchDirs.Clear;
Text := TFile.ReadAllText(FileName);
JSON := TJSonObject.ParseJSONValue(Text);
try
if JSON is TJSONObject then
begin
fRootFileName:=(JSON as TJSONObject).GetValue<String>('RootFileName');
DirArray:=(JSON as TJSONObject).GetValue('SearchDirs') as TJSONArray;
if DirArray <> nil then
begin
for Dir in DirArray do
fSearchDirs.Add(Dir.Value);
end;
end;
finally
FreeAndNil(JSON);
end;
end;
procedure TProjectSettings.SaveToFile(FileName: String);
var
JSON: TJSONObject;
JSONDirs: TJsonArray;
Dir : String;
begin
JSON:=TJSONObject.Create;
try
JSON.AddPair('RootFileName', fRootFileName);
JSONDirs:=TJsonArray.Create;
for Dir in fSearchDirs do
JSONDirs.Add(Dir);
JSON.AddPair('SearchDirs', JSONDirs);
TFile.WriteAllText(fileName, JSON.ToJSON());
finally
FreeAndNil(JSON);
end;
end;
end.