-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathconfigfile.pas
More file actions
399 lines (340 loc) · 10.7 KB
/
Copy pathconfigfile.pas
File metadata and controls
399 lines (340 loc) · 10.7 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
unit configfile;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
/// <summary>
/// Lightweight wrapper around a TStringList for INI/key-value config files.
/// Provides section-aware Get/Set/Delete operations without the overhead
/// of TIniFile (which requires a section) and works with plain key=value
/// files like MangoHud.conf, vkBasalt.conf, OptiScaler.ini, etc.
/// </summary>
TConfigFile = class
private
FLines: TStringList;
FFilePath: string;
FModified: Boolean;
function FindLineIndex(const AKeyPrefix: string; AStartIndex: Integer = 0): Integer;
function FindLineIndexInSection(const AKeyPrefix, ASection: string): Integer;
function IsSectionHeader(const ALine: string; out ASectionName: string): Boolean;
public
constructor Create;
destructor Destroy; override;
/// <summary>Load config from disk. Returns False if file does not exist.</summary>
function Load(const AFilePath: string): Boolean;
/// <summary>Save config back to disk (only if modified). Creates dirs if needed.</summary>
function Save: Boolean;
/// <summary>Access the raw TStringList for advanced operations.</summary>
property Lines: TStringList read FLines;
/// <summary>Full path of the loaded file.</summary>
property FilePath: string read FFilePath;
/// <summary>True if any Set* or Delete* was called since last Load/Save.</summary>
property Modified: Boolean read FModified;
// -----------------------------------------------------------------------
// String values (key=value)
// -----------------------------------------------------------------------
/// <summary>
/// Get the value for a key. Returns Default if key not found.
/// KeyPrefix should include the trailing '=' (e.g. 'ShortcutKey=').
/// </summary>
function GetValue(const AKeyPrefix, ADefault: string; const ASection: string = ''): string;
/// <summary>
/// Set a key=value line. Creates or updates the line.
/// If ASection is provided, the key is searched/added within that section.
/// </summary>
procedure SetValue(const AKeyPrefix, AValue: string; const ASection: string = '');
/// <summary>
/// Delete the first line matching the key prefix. Returns True if deleted.
/// </summary>
function DeleteKey(const AKeyPrefix: string): Boolean;
// -----------------------------------------------------------------------
// Boolean values (key=true/false/1/0 or standalone flags)
// -----------------------------------------------------------------------
/// <summary>
/// Check if a boolean flag is present and enabled.
/// Supports 'true', '1', 'yes', 'on' as True; anything else or missing as False.
/// For standalone flags (no value), use HasKey.
/// </summary>
function GetBool(const AKeyPrefix: string; ADefault: Boolean = False): Boolean;
/// <summary>
/// Set a boolean key=value. Writes 'true' or 'false'.</summary>
procedure SetBool(const AKeyPrefix: string; AValue: Boolean; const ASection: string = '');
// -----------------------------------------------------------------------
// Integer values
// -----------------------------------------------------------------------
/// <summary>Get integer value. Returns Default if not found or not numeric.</summary>
function GetInt(const AKeyPrefix: string; ADefault: Integer): Integer;
/// <summary>Set integer value.</summary>
procedure SetInt(const AKeyPrefix: string; AValue: Integer; const ASection: string = '');
// -----------------------------------------------------------------------
// Presence checks
// -----------------------------------------------------------------------
/// <summary>True if any line starts with the given prefix.</summary>
function HasKey(const AKeyPrefix: string): Boolean;
/// <summary>True if the given section header exists.</summary>
function HasSection(const ASection: string): Boolean;
// -----------------------------------------------------------------------
// Batch / advanced
// -----------------------------------------------------------------------
/// <summary>Clear all lines and mark as modified.</summary>
procedure Clear;
/// <summary>Add a raw line at the end.</summary>
procedure AddRaw(const ALine: string);
/// <summary>Remove all lines whose content contains ASubstring.</summary>
function DeleteLinesContaining(const ASubstring: string): Integer;
end;
implementation
uses
StrUtils;
constructor TConfigFile.Create;
begin
inherited;
FLines := TStringList.Create;
end;
destructor TConfigFile.Destroy;
begin
FLines.Free;
inherited;
end;
function TConfigFile.Load(const AFilePath: string): Boolean;
begin
FFilePath := AFilePath;
FModified := False;
FLines.Clear;
Result := FileExists(AFilePath);
if Result then
FLines.LoadFromFile(AFilePath);
end;
function TConfigFile.Save: Boolean;
var
Dir: string;
begin
Result := False;
if not FModified then Exit;
if FFilePath = '' then Exit;
Dir := ExtractFilePath(FFilePath);
if (Dir <> '') and not DirectoryExists(Dir) then
ForceDirectories(Dir);
FLines.SaveToFile(FFilePath);
FModified := False;
Result := True;
end;
function TConfigFile.IsSectionHeader(const ALine: string; out ASectionName: string): Boolean;
var
Trimmed: string;
begin
Trimmed := Trim(ALine);
Result := (Length(Trimmed) > 2) and (Trimmed[1] = '[') and (Trimmed[Length(Trimmed)] = ']');
if Result then
ASectionName := Trimmed;
end;
function IsCommentLine(const ALine: string): Boolean;
var
Trimmed: string;
begin
Trimmed := TrimLeft(ALine);
Result := (Length(Trimmed) > 0) and ((Trimmed[1] = ';') or (Trimmed[1] = '#'));
end;
function CleanKeyLine(const AStr: string): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(AStr) do
if not (AStr[i] in [' ', #9]) then
Result := Result + AStr[i];
Result := LowerCase(Result);
end;
function TConfigFile.FindLineIndex(const AKeyPrefix: string; AStartIndex: Integer = 0): Integer;
var
i: Integer;
CleanPrefix: string;
CleanLine: string;
begin
Result := -1;
CleanPrefix := CleanKeyLine(AKeyPrefix);
for i := AStartIndex to FLines.Count - 1 do
if not IsCommentLine(FLines[i]) then
begin
CleanLine := CleanKeyLine(FLines[i]);
if LeftStr(CleanLine, Length(CleanPrefix)) = CleanPrefix then
begin
Result := i;
Exit;
end;
end;
end;
function TConfigFile.FindLineIndexInSection(const AKeyPrefix, ASection: string): Integer;
var
i: Integer;
InSection: Boolean;
SectionName: string;
Trimmed: string;
CleanPrefix: string;
CleanLine: string;
begin
Result := -1;
InSection := (ASection = '');
CleanPrefix := CleanKeyLine(AKeyPrefix);
for i := 0 to FLines.Count - 1 do
begin
Trimmed := Trim(FLines[i]);
if IsSectionHeader(Trimmed, SectionName) then
begin
if InSection and (SectionName <> ASection) then
Exit; // left the target section without finding the key
InSection := (SectionName = ASection);
Continue;
end;
if InSection and not IsCommentLine(FLines[i]) then
begin
CleanLine := CleanKeyLine(FLines[i]);
if LeftStr(CleanLine, Length(CleanPrefix)) = CleanPrefix then
begin
Result := i;
Exit;
end;
end;
end;
end;
function TConfigFile.GetValue(const AKeyPrefix, ADefault: string; const ASection: string = ''): string;
var
idx: Integer;
s: string;
begin
if ASection <> '' then
idx := FindLineIndexInSection(AKeyPrefix, ASection)
else
idx := FindLineIndex(AKeyPrefix);
if idx < 0 then
begin
Result := ADefault;
Exit;
end;
s := FLines[idx];
// strip everything before and including the first '=' after the key prefix
idx := Pos('=', s);
if idx > 0 then
Result := Trim(Copy(s, idx + 1, MaxInt))
else
Result := ADefault;
end;
procedure TConfigFile.SetValue(const AKeyPrefix, AValue: string; const ASection: string = '');
var
idx: Integer;
KeyName: string;
begin
if ASection <> '' then
idx := FindLineIndexInSection(AKeyPrefix, ASection)
else
idx := FindLineIndex(AKeyPrefix);
if idx >= 0 then
begin
FLines[idx] := AKeyPrefix + AValue;
end
else
begin
// Key not found — append at end (or after section if specified)
if ASection <> '' then
begin
idx := FindLineIndex(ASection);
if idx >= 0 then
begin
// Insert after section header (or at end of section)
FLines.Insert(idx + 1, AKeyPrefix + AValue);
FModified := True;
Exit;
end;
// Section not found — add section header + key
FLines.Add(ASection);
end;
FLines.Add(AKeyPrefix + AValue);
end;
FModified := True;
end;
function TConfigFile.DeleteKey(const AKeyPrefix: string): Boolean;
var
idx: Integer;
begin
idx := FindLineIndex(AKeyPrefix);
Result := idx >= 0;
if Result then
begin
FLines.Delete(idx);
FModified := True;
end;
end;
function TConfigFile.GetBool(const AKeyPrefix: string; ADefault: Boolean): Boolean;
var
v: string;
begin
v := LowerCase(Trim(GetValue(AKeyPrefix, '')));
if v = '' then
Result := ADefault
else
Result := (v = 'true') or (v = '1') or (v = 'yes') or (v = 'on');
end;
procedure TConfigFile.SetBool(const AKeyPrefix: string; AValue: Boolean; const ASection: string = '');
begin
if AValue then
SetValue(AKeyPrefix, 'true', ASection)
else
SetValue(AKeyPrefix, 'false', ASection);
end;
function TConfigFile.GetInt(const AKeyPrefix: string; ADefault: Integer): Integer;
var
v: string;
i: Integer;
begin
v := Trim(GetValue(AKeyPrefix, ''));
if TryStrToInt(v, i) then
Result := i
else
Result := ADefault;
end;
procedure TConfigFile.SetInt(const AKeyPrefix: string; AValue: Integer; const ASection: string = '');
begin
SetValue(AKeyPrefix, IntToStr(AValue), ASection);
end;
function TConfigFile.HasKey(const AKeyPrefix: string): Boolean;
begin
Result := FindLineIndex(AKeyPrefix) >= 0;
end;
function TConfigFile.HasSection(const ASection: string): Boolean;
var
i: Integer;
SectionName: string;
begin
Result := False;
for i := 0 to FLines.Count - 1 do
if IsSectionHeader(Trim(FLines[i]), SectionName) and (SectionName = ASection) then
begin
Result := True;
Exit;
end;
end;
procedure TConfigFile.Clear;
begin
FLines.Clear;
FModified := True;
end;
procedure TConfigFile.AddRaw(const ALine: string);
begin
FLines.Add(ALine);
FModified := True;
end;
function TConfigFile.DeleteLinesContaining(const ASubstring: string): Integer;
var
i: Integer;
begin
Result := 0;
for i := FLines.Count - 1 downto 0 do
if Pos(ASubstring, FLines[i]) > 0 then
begin
FLines.Delete(i);
Inc(Result);
FModified := True;
end;
end;
end.