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
|
{******************************************************************************
TRegIniFile
******************************************************************************}
constructor TRegIniFile.Create(const FN: String);
begin
Create(FN, KEY_ALL_ACCESS);
end;
constructor TRegIniFile.Create(const FN: String;aaccess:longword);
begin
inherited Create(aaccess);
fFileName := FN;
if fFileName<>'' then begin
fPath := fFileName + '\';
if fPath[1]='\' then
System.Delete(fPath,1,1);
OpenKey(fFileName, aaccess <> KEY_READ);
end
else
fPath := '';
fPreferStringValues:=True; // Delphi compatibility
end;
procedure TRegIniFile.DeleteKey(const Section, Ident: String);
begin
if OpenSection(Section) then
try
DeleteValue(Ident);
finally
CloseSection;
end;
end;
procedure TRegIniFile.EraseSection(const Section: string);
begin
inherited DeleteKey(Section);
end;
procedure TRegIniFile.ReadSection(const Section: string; Strings: TStrings);
begin
if OpenSection(Section) then
try
GetValueNames(Strings);
finally
CloseSection;
end;
end;
procedure TRegIniFile.ReadSections(Strings: TStrings);
begin
GetKeyNames(Strings);
end;
procedure TRegIniFile.ReadSectionValues(const Section: string; Strings: TStrings);
var
ValList : TStringList;
V : String;
i : Integer;
begin
if OpenSection(Section) then
try
ValList := TStringList.Create;
try
GetValueNames(ValList);
for i:=0 to ValList.Count-1 do
begin
V := inherited ReadString(ValList.Strings[i]);
Strings.Add(ValList.Strings[i] + '=' + V);
end;
finally
ValList.Free;
end;
finally
CloseSection;
end;
end;
procedure TRegIniFile.WriteBool(const Section, Ident: string; Value: Boolean);
begin
if OpenSection(Section,True) then
try
if not fPreferStringValues then
inherited WriteBool(Ident,Value)
else begin
if ValueExists(Ident) and (GetDataType(Ident)=rdInteger) then
inherited WriteBool(Ident,Value)
else
inherited WriteString(Ident,BoolToStr(Value));
end;
finally
CloseSection;
end;
end;
procedure TRegIniFile.WriteInteger(const Section, Ident: string; Value: LongInt);
begin
if OpenSection(Section,True) then
try
if not fPreferStringValues then
inherited WriteInteger(Ident,Value)
else begin
if ValueExists(Ident) and (GetDataType(Ident)=rdInteger) then
inherited WriteInteger(Ident,Value)
else
inherited WriteString(Ident,IntToStr(Value));
end;
finally
CloseSection;
end;
end;
procedure TRegIniFile.WriteString(const Section, Ident, Value: String);
begin
if OpenSection(Section,True) then
try
inherited WriteString(Ident,Value);
finally
CloseSection;
end;
end;
procedure TRegIniFile.WriteDate(const Section, Ident: string; Value: TDateTime);
begin
if OpenSection(Section,true) then
try
if not fPreferStringValues then
inherited WriteDate(Ident,Value)
else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
inherited WriteDate(Ident,Value)
else
inherited WriteString(Ident,DateToStr(Value));
finally
CloseKey;
end;
end;
procedure TRegIniFile.WriteDateTime(const Section, Ident: string; Value: TDateTime);
begin
if OpenSection(Section,true) then
try
if not fPreferStringValues then
inherited WriteDateTime(Ident,Value)
else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
inherited WriteDateTime(Ident,Value)
else
inherited WriteString(Ident,DateTimeToStr(Value));
finally
CloseKey;
end;
end;
procedure TRegIniFile.WriteTime(const Section, Ident: string; Value: TDateTime);
begin
if OpenSection(Section,true) then
try
if not fPreferStringValues then
inherited WriteTime(Ident,Value)
else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
inherited WriteTime(Ident,Value)
else
inherited WriteString(Ident,TimeToStr(Value));
finally
CloseKey;
end;
end;
procedure TRegIniFile.WriteFloat(const Section, Ident: string; Value: Double);
begin
if OpenSection(Section,true) then
try
if not fPreferStringValues then
inherited WriteFloat(Ident,Value)
else if ValueExists(Ident) and (GetDataType(Ident)<>rdString) then
inherited WriteFloat(Ident,Value)
else
inherited WriteString(Ident,FloatToStr(Value));
finally
CloseKey;
end;
end;
function TRegIniFile.ReadBool(const Section, Ident: string; Default: Boolean): Boolean;
begin
Result := Default;
if OpenSection(Section) then
try
if ValueExists(Ident) then
if (not fPreferStringValues) or (GetDataType(Ident)=rdInteger) then
Result := inherited ReadBool(Ident)
else
Result := StrToBool(inherited ReadString(Ident));
finally
CloseSection;
end;
end;
function TRegIniFile.ReadInteger(const Section, Ident: string; Default: LongInt): LongInt;
begin
Result := Default;
if OpenSection(Section) then
try
if ValueExists(Ident) then
if (not fPreferStringValues) or (GetDataType(Ident)=rdInteger) then
Result := inherited ReadInteger(Ident)
else
Result := StrToInt(inherited ReadString(Ident));
finally
CloseSection;
end;
end;
function TRegIniFile.ReadString(const Section, Ident, Default: String): String;
begin
Result := Default;
if OpenSection(Section) then
try
if ValueExists(Ident) then
Result := inherited ReadString(Ident);
finally
CloseSection;
end;
end;
function TRegIniFile.ReadDate(const Section, Ident: string; Default: TDateTime):TDateTime;
begin
Result := Default;
if OpenSection(Section) then
try
if ValueExists(Ident) then
if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
Result := inherited ReadDate(Ident)
else
Result := StrToDateDef(inherited ReadString(Ident),Result);
finally
CloseSection;
end;
end;
function TRegIniFile.ReadDateTime(const Section, Ident: string; Default: TDateTime):TDateTime;
begin
Result := Default;
if OpenSection(Section) then
try
if ValueExists(Ident) then
if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
Result := inherited ReadDateTime(Ident)
else
Result := StrToDateTimeDef(inherited ReadString(Ident),Result);
finally
CloseSection;
end;
end;
function TRegIniFile.ReadTime(const Section, Ident: string; Default: TDateTime):TDateTime;
begin
Result := Default;
if OpenSection(Section) then
try
if ValueExists(Ident) then
if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
Result := inherited ReadTime(Ident)
else
Result := StrToTimeDef(inherited ReadString(Ident),Result);
finally
CloseSection;
end;
end;
function TRegIniFile.ReadFloat(const Section, Ident: string; Default: Double): Double;
begin
Result := Default;
if OpenSection(Section) then
try
if ValueExists(Ident) then
if (not fPreferStringValues) or (GetDataType(Ident)<>rdString) then
Result := inherited ReadFloat(Ident)
else
Result := StrToFloatDef(inherited ReadString(Ident),Result);
finally
CloseSection;
end;
end;
function TRegIniFile.OpenSection(const Section: string; CreateSection : Boolean = false): boolean;
var
k: HKEY;
S : String;
begin
S:=Section;
If (S<>'') and (S[1] = '\') then
Delete(S,1,1);
if CreateSection and (S<>'') then
CreateKey('\'+CurrentPath+'\'+S);
if S <> '' then
k:=GetKey('\'+CurrentPath+'\'+S)
else
k:=GetKey('\'+CurrentPath);
if k = 0 then
begin
Result:=False;
exit;
end;
SetCurrentKey(k);
Result:=True;
end;
procedure TRegIniFile.CloseSection;
begin
CloseKey(CurrentKey);
fCurrentKey:=0;
end;
|