-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLightCore.INIFile.pas
More file actions
287 lines (209 loc) · 9.59 KB
/
Copy pathLightCore.INIFile.pas
File metadata and controls
287 lines (209 loc) · 9.59 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
UNIT LightCore.INIFile;
{=============================================================================================================
2026.01.29
www.GabrielMoraru.com
--------------------------------------------------------------------------------------------------------------
Features:
* Extends the capabilities of TIniFile.
* We don't need to provide a section name (except for the constructor). All values are written to that single section.
* Provides a lots of overloads where we don't need to provide a section name, in preparation for TIniFileApp
Reminder: TIniFile limitations:
- Cannot put spaces at the beginning of a 'value'. The spaces will be trimmed. This behavior does not happen if you use a TMemInifile. https://stackoverflow.com/questions/3702647/reading-inifile-to-stringlist-problem-spaces-problem
- Cannot handle quotes and enters
- Issues with Unicode strings
Some of these limitations are not in TMemIniFile.
=============================================================================================================}
INTERFACE
USES
System.SysUtils, System.UITypes, System.IniFiles, System.IOUtils; //, LightCore, LightCore.Time;
TYPE
FontStruct = record
Name : string;
Size : Integer;
Style : TFontStyles; { Set of TFontStyle - can hold multiple styles like [fsBold, fsItalic] }
Color : TColor;
end;
TYPE
{
BUG: stackoverflow.com/questions/20419347/delphi-inifiles-readdatetime
The date format will not depend anymore on user's regional settings
This method cannot be simply named Write because it will conflict with Write(Real).
I had a case where it wrote Write(2.0) to the ini file as a date (1900-01-01)
The Default MUST be in the 0.0 format otherwise the wrong function will be called (the one for integer) }
{ Extends TIniFile to handle dates using float format (independent of regional settings).
See: stackoverflow.com/questions/20419347/delphi-inifiles-readdatetime }
TIniFileDt = class(TIniFile)
public
function ReadDate (CONST Section, Ident: string; Default: TDateTime): TDateTime; reintroduce;
procedure WriteDate(CONST Section, Ident: string; Value : TDateTime); reintroduce;
end;
TIniFileEx = class(TIniFile) // Old name: TCubicIniFile
protected
FSection: string;
public
constructor Create (CONST SectionName, FileName: string); virtual;
{$IFDEF MSWINDOWS}
function ReadString (CONST Section, Ident, Default: string): string; override; // Repairs the RTL's silent 2047-char cut. See implementation.
{$ENDIF}
function ValueExists(CONST Ident: string): Boolean; reintroduce; overload;
{ Data/Time }
function ReadDate (CONST Ident: string; Default: TDateTime): TDateTime; reintroduce; overload;
procedure WriteDate (CONST Ident: string; Value: TDateTime); reintroduce; overload;
{ String }
function Read (CONST Ident: string; Default: string): string; overload;
procedure Write (const Ident, Value: String); overload;
{ Integer }
function Read (const Ident: string; Default: Integer= 0): Integer; overload;
procedure Write (const Ident: string; Value: Integer); overload;
{ Bool }
function Read (CONST Ident: string; Default: Boolean= TRUE): Boolean; overload;
procedure Write (const Ident: string; Value: Boolean); overload;
function ReadFont (CONST Ident: string): FontStruct;
procedure WriteFont (CONST Ident: string; Font: FontStruct);
{ Float }
function Read (const Ident: string; Default: Double): Double; overload;
procedure Write (const Ident: string; Value: Double); overload;
end;
IMPLEMENTATION
USES
{$IFDEF MSWINDOWS} Winapi.Windows, {$ENDIF}
LightCore.IO;
{-------------------------------------------------------------------------------------------------------------
MAIN
-------------------------------------------------------------------------------------------------------------}
constructor TIniFileEx.Create(CONST SectionName, FileName: string);
begin
VAR Dir:= ExtractFilePath(FileName);
if Dir <> ''
then TDirectory.CreateDirectory(Dir); // Make sure the path exists otherwise we get an error. Empty Dir = relative filename (current dir) — nothing to create; CreateDirectory('') would raise.
inherited Create(FileName);
FSection:= SectionName;
end;
{$IFDEF MSWINDOWS}
{ Repairs a silent data-loss bug in the RTL.
TIniFile.ReadString reads into a fixed 'Buffer: array[0..2047] of Char' (C:\Delphi\Delphi 13\source\rtl\common\System.IniFiles.pas, TIniFile.ReadString),
so any value longer than 2047 characters comes back CUT - and the next save writes the cut value back over the good one, so the loss is permanent.
The API reports the truncation: it returns nSize-1 when the buffer was too small
(learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestringw), so we simply grow the buffer and read again.
A value under 2047 chars still costs exactly one API call, as before.
Only TIniFile on Windows has the cut; on the other platforms TIniFile descends from TMemIniFile, which reads whole lines. }
function TIniFileEx.ReadString(CONST Section, Ident, Default: string): string;
CONST
InitialSize = 2048; { Same size the RTL uses, so the common (short) value is read in a single call }
MaxSize = 1024*1024; { Safety stop. A single INI value of one million chars is a bug, not a setting }
VAR
Buffer : string;
Size : Integer;
Returned: Integer;
begin
Size:= InitialSize;
REPEAT
SetLength(Buffer, Size);
Returned:= Integer(GetPrivateProfileString(PChar(Section), PChar(Ident), PChar(Default), PChar(Buffer), Size, PChar(FileName)));
{ A returned length that fills the buffer means it was probably truncated -> read again into a bigger one.
Size-2 (not just Size-1) because with an empty Ident the API returns nSize-2 when it truncates the key-name list. }
if (Returned < Size-2)
or (Size >= MaxSize)
then BREAK;
Size:= Size * 4;
UNTIL FALSE;
SetLength(Buffer, Returned);
Result:= Buffer;
end;
{$ENDIF}
{---------------
FONT
---------------}
{ Reads font settings from INI file. Returns defaults if font entry not found. }
function TIniFileEx.ReadFont(CONST Ident: string): FontStruct;
begin
if ValueExists(FSection, Ident+ 'Name')
then
begin
Result.Name := ReadString (FSection, Ident+ 'Name', 'Arial');
Result.Color := TColor (ReadInteger(FSection, Ident+ 'Color', 0));
Result.Size := ReadInteger(FSection, Ident+ 'Size', 8);
Result.Style := TFontStyles(Byte(ReadInteger(FSection, Ident+ 'Style', 0)));
end
else
begin
{ Return sensible defaults }
Result.Name := 'Arial';
Result.Color := TColor(0); { clBlack }
Result.Size := 8;
Result.Style := [];
end;
end;
{ Writes font settings to INI file.
Note: Writes empty string for Ident so TIniFileVCL.ReadFont can find the font by identifier. }
procedure TIniFileEx.WriteFont(CONST Ident: string; Font: FontStruct);
begin
WriteString (FSection, Ident, '');
WriteString (FSection, Ident + 'Name', Font.Name);
WriteInteger(FSection, Ident + 'Color', Font.Color);
WriteInteger(FSection, Ident + 'Size', Font.Size);
WriteInteger(FSection, Ident + 'Style', Byte(Font.Style));
end;
{---------------
DATE
---------------}
function TIniFileEx.ReadDate(const Ident: string; Default: TDateTime): TDateTime;
begin
Result:= inherited ReadDate(FSection, Ident, Default);
end;
procedure TIniFileEx.WriteDate(const Ident: string; Value: TDateTime);
begin
inherited writedate(FSection, Ident, Value);
end;
{----------------------
DEFAULT OVERLOADS
----------------------}
function TIniFileEx.ValueExists(const Ident: string): Boolean;
begin
Result:= inherited ValueExists(FSection, Ident);
end;
function TIniFileEx.Read(const Ident: string; Default: Boolean= TRUE): Boolean;
begin
Result:= inherited ReadBool(FSection, Ident, Default);
end;
procedure TIniFileEx.Write(const Ident: string; Value: Boolean);
begin
inherited WriteBool(FSection, Ident, Value);
end;
function TIniFileEx.Read(CONST Ident: string; Default: Integer= 0): Integer;
begin
Result:= inherited ReadInteger(FSection, Ident, Default);
end;
procedure TIniFileEx.Write(CONST Ident: string; Value: Integer);
begin
inherited WriteInteger(FSection, Ident, Value);
end;
function TIniFileEx.Read(CONST Ident: string; Default: string): string;
begin
Result:= inherited ReadString(FSection, Ident, Default);
end;
procedure TIniFileEx.Write(const Ident, Value: String);
begin
inherited WriteString(FSection, Ident, Value);
end;
function TIniFileEx.Read(const Ident: string; Default: Double): Double;
begin
Result:= inherited ReadFloat(FSection, Ident, Default);
end;
procedure TIniFileEx.Write(const Ident: string; Value: Double);
begin
inherited WriteFloat(FSection, Ident, Value);
end;
{-------------------------------------------------------------------------------------------------------------
TIniFileDt
Overrides date read/write to use float format (independent of regional settings).
-------------------------------------------------------------------------------------------------------------}
function TIniFileDt.ReadDate(const Section, Ident: string; Default: TDateTime): TDateTime;
begin
Result:= TDateTime(ReadFloat(Section, Ident, Default));
end;
procedure TIniFileDt.WriteDate(const Section, Ident: string; Value: TDateTime);
begin
WriteFloat(Section, Ident, Value);
end;
end.