-
Notifications
You must be signed in to change notification settings - Fork 1
/
unitconfigs.pas
279 lines (241 loc) · 6.18 KB
/
unitconfigs.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
unit UnitConfigs;
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
Classes, SysUtils, fpjson, jsonparser, jsonscanner
, UnitInit // initialization in this unit affects GetConfDirectory, so keep it here in uses.
;
type
TConfJSON = class(TObject)
strict private
type
TRemapping = record
RemapFrom: String;
RemapTo: String;
end;
TRemappings = Array of TRemapping;
strict private
class var
FConf: TJSONObject;
FFileName: String;
FConfDirectory: String;
FRemappings: TRemappings;
FPossible092Conf: Boolean;
private
class procedure Init;
class procedure Final;
strict private
class function GetFileName: String;
class function GetConf: TJSONObject; static;
class function GetConfDirectory: String; static;
class function LoadFromStream(Stream: TStream): Boolean;
class function SaveToStream(Stream: TStream): Boolean;
class function LoadFromFile(): Boolean;
class function SaveToFile(): Boolean;
class procedure Remap(const R: TRemapping);
class procedure AddRemapping(RemapFrom, RemapTo: String);
public
class function AddToConf(const Section: String; Data: TJSONData): Boolean;
class function GetJSONObject(const Section: String): TJSONObject;
class function GetJSONArray(const Section: String): TJSONArray;
class procedure RemoveSection(const Section: String);
class property Possible092Conf: Boolean read FPossible092Conf;
end;
implementation
{ TConfJSON }
class procedure TConfJSON.Init;
begin
FConf := nil;
FConfDirectory := '';
FFileName := '';
FPossible092Conf := False;
SetLength(FRemappings, 0);
AddRemapping('f1', 'general');
end;
class procedure TConfJSON.Final;
begin
SaveToFile();
FConf.Free;
end;
class function TConfJSON.GetFileName: String;
begin
if FFileName = '' then
FFileName := GetConfDirectory + ChangeFileExt(ExtractFileName(ApplicationName), ConfigExtension);
Result := FFileName;
end;
class function TConfJSON.GetConf: TJSONObject;
{$push}
{$J+}
const
FirstPass: Boolean = True;
{$pop}
begin
if FirstPass then begin
FirstPass := False;
if FConf = nil then begin
LoadFromFile();
end;
end;
Result := FConf;
end;
class function TConfJSON.GetConfDirectory: String;
//{$push}
//{$J+}
//const
// FConfDirectory: String = ''; // moved to outer scope -- reason: unfreed memory reported (a bug in compiler)
// // bug report: https://gitlab.com/freepascal.org/fpc/source/-/issues/33804
//{$pop}
begin
if FConfDirectory = '' then
FConfDirectory := IncludeTrailingPathDelimiter(GetAppConfigDir(False));
Result := FConfDirectory;
end;
class function TConfJSON.LoadFromStream(Stream: TStream): Boolean;
var
Parser: TJSONParser;
JSData: TJSONData;
I: Integer;
begin
Result := False;
FreeAndNil(FConf);
if Assigned(Stream) then begin
Parser := TJSONParser.Create(Stream, [joUTF8]);
try
JSData := Parser.Parse;
if JSData is TJSONObject then begin
FConf := TJSONObject(JSData);
for I := Low(FRemappings) to High(FRemappings) do begin
Remap(FRemappings[I]);
end;
Result := True;
end else
JSData.Free;
except
end;
Parser.Free;
end;
end;
class function TConfJSON.SaveToStream(Stream: TStream): Boolean;
var
S: RawByteString;
L: Integer;
begin
Result := False;
if Assigned(Stream) and Assigned(FConf) then begin
S := FConf.FormatJSON();
L := Length(S);
Stream.Size := L;
Stream.Position := 0;
Result := Stream.Write(PAnsiChar(S)^, L) = L;
end;
end;
class function TConfJSON.LoadFromFile: Boolean;
var
Stream: TStream;
begin
Result := False;
FreeAndNil(FConf);
try
Stream := TFileStream.Create(GetFileName, fmOpenRead or fmShareDenyNone);
try
Result := LoadFromStream(Stream);
finally
Stream.Free;
end;
except
end;
end;
class function TConfJSON.SaveToFile(): Boolean;
var
Stream: TStream;
Mode: Word;
begin
Result := False;
try
if Assigned(FConf) and ForceDirectories(GetConfDirectory) then begin
Mode := fmOpenReadWrite or fmCreate;
Stream := TFileStream.Create(GetFileName, Mode);
try
Result := SaveToStream(Stream);
finally
Stream.Free;
end;
end;
except
end;
end;
class procedure TConfJSON.Remap(const R: TRemapping);
var
JD: TJSONData;
begin
if Assigned(GetConf) then begin
JD := GetConf.Extract(R.RemapFrom);
if Assigned(JD) then begin
if GetConf.IndexOfName(R.RemapTo) <> -1 then
JD.Free
else begin
if R.RemapFrom = 'f1' then // Conf saved before 0.9.4 has this section.
FPossible092Conf := True; // We can use this to recognize the old
// conf version, as it was also 0.9.4 when
// Swan started to save its version.
GetConf.Add(R.RemapTo, JD);
end;
end;
end;
end;
class procedure TConfJSON.AddRemapping(RemapFrom, RemapTo: String);
var
L: Integer;
begin
L := Length(FRemappings);
SetLength(FRemappings, L + 1);
FRemappings[L].RemapFrom := RemapFrom;
FRemappings[L].RemapTo := RemapTo;
end;
class function TConfJSON.AddToConf(const Section: String; Data: TJSONData
): Boolean;
begin
if GetConf = nil then begin
FConf := TJSONObject.Create;
end;
Result := FConf.Add(Section, Data) >= 0;
end;
class function TConfJSON.GetJSONObject(const Section: String): TJSONObject;
var
D: TJSONData;
begin
Result := nil;
if Assigned(GetConf) then begin
D := FConf.Find(Section);
if D is TJSONObject then
Result := TJSONObject(D);
end;
end;
class function TConfJSON.GetJSONArray(const Section: String): TJSONArray;
var
D: TJSONData;
begin
Result := nil;
if Assigned(GetConf) then begin
D := FConf.Find(Section);
if D is TJSONArray then
Result := TJSONArray(D);
end;
end;
class procedure TConfJSON.RemoveSection(const Section: String);
var
D: TJSONData;
begin
if Assigned(GetConf) then begin
D := FConf.Extract(Section);
D.Free;
end;
end;
initialization
TConfJSON.Init;
finalization
TConfJSON.Final;
end.