-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DefRegistry.pas
299 lines (244 loc) · 8.69 KB
/
DefRegistry.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
DefRegistry
Standard TRegistry class read functions, when called for non-existing or
otherwise inaccessible value, will raise an exception.
TDefRegistry class available here is a direct descendant of TRegistry that
provides wrapper functions which catch these errors and instead indicate
failure in result value or by returning a default value instead of a read
one, similarly to functions for text to number conversion TryStrToInt and
StrToIntDef.
Version 1.1 (2015-02-20)
Last change 2020-08-02
©2015-2020 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.DefRegistry
Dependencies:
none
===============================================================================}
unit DefRegistry;
{$IFDEF FPC}
{$MODE ObjFPC}
{$ENDIF}
{$H+}
interface
uses
Registry, DateUtils;
type
TDefRegistry = class(TRegistry)
public
Function TryReadCurrency(const Name: String; out Value: Currency): Boolean; virtual;
Function TryReadBinaryData(const Name: String; var Buffer; BuffSize: Integer; out BytesRead: Integer): Boolean; virtual;
Function TryReadBool(const Name: String; out Value: Boolean): Boolean; virtual;
Function TryReadDate(const Name: String; out Value: TDateTime): Boolean; virtual;
Function TryReadDateTime(const Name: String; out Value: TDateTime): Boolean; virtual;
Function TryReadFloat(const Name: String; out Value: Double): Boolean; virtual;
Function TryReadInteger(const Name: String; out Value: Integer): Boolean; virtual;
Function TryReadString(const Name: String; out Value: String): Boolean; virtual;
Function TryReadTime(const Name: String; out Value: TDateTime): Boolean; virtual;
//---------- ----------//
Function ReadCurrencyDef(const Name: String; const DefaultValue: Currency): Currency; virtual;
Function ReadBinaryDataDef(const Name: String; var Buffer; BuffSize: Integer; const DefaultBuffer): Integer; virtual;
Function ReadBoolDef(const Name: String; const DefaultValue: Boolean): Boolean; virtual;
Function ReadDateDef(const Name: String; const DefaultValue: TDateTime): TDateTime; virtual;
Function ReadDateTimeDef(const Name: String; const DefaultValue: TDateTime): TDateTime; virtual;
Function ReadFloatDef(const Name: String; const DefaultValue: Double): Double; virtual;
Function ReadIntegerDef(const Name: String; const DefaultValue: Integer): Integer; virtual;
Function ReadStringDef(const Name: String; const DefaultValue: String): String; virtual;
Function ReadTimeDef(const Name: String; const DefaultValue: TDateTime): TDateTime; virtual;
end;
implementation
Function TDefRegistry.TryReadCurrency(const Name: String; out Value: Currency): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadCurrency(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadBinaryData(const Name: String; var Buffer; BuffSize: Integer; out BytesRead: Integer): Boolean;
begin
try
If ValueExists(Name) then
begin
BytesRead := ReadBinaryData(Name,Buffer,BuffSize);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadBool(const Name: String; out Value: Boolean): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadBool(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadDate(const Name: String; out Value: TDateTime): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadDate(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadDateTime(const Name: String; out Value: TDateTime): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadDateTime(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadFloat(const Name: String; out Value: Double): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadFloat(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadInteger(const Name: String; out Value: Integer): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadInteger(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadString(const Name: String; out Value: String): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadString(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.TryReadTime(const Name: String; out Value: TDateTime): Boolean;
begin
try
If ValueExists(Name) then
begin
Value := ReadTime(Name);
Result := True;
end
else Result := False;
except
Result := False;
end;
end;
//==============================================================================
Function TDefRegistry.ReadCurrencyDef(const Name: String; const DefaultValue: Currency): Currency;
begin
If not TryReadCurrency(Name,Result) then
Result := DefaultValue;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadBinaryDataDef(const Name: String; var Buffer; BuffSize: Integer; const DefaultBuffer): Integer;
begin
If not TryReadBinaryData(Name,Buffer,BuffSize,Result) then
begin
Move(DefaultBuffer,Buffer,BuffSize);
Result := BuffSize
end;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadBoolDef(const Name: String; const DefaultValue: Boolean): Boolean;
begin
If not TryReadBool(Name,Result) then
Result := DefaultValue;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadDateDef(const Name: String; const DefaultValue: TDateTime): TDateTime;
begin
If not TryReadDate(Name,Result) then
Result := DefaultValue;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadDateTimeDef(const Name: String; const DefaultValue: TDateTime): TDateTime;
begin
If not TryReadDateTime(Name,Result) then
Result := DefaultValue;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadFloatDef(const Name: String; const DefaultValue: Double): Double;
begin
If not TryReadFloat(Name,Result) then
Result := DefaultValue;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadIntegerDef(const Name: String; const DefaultValue: Integer): Integer;
begin
If not TryReadInteger(Name,Result) then
Result := DefaultValue;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadStringDef(const Name: String; const DefaultValue: String): String;
begin
If not TryReadString(Name,Result) then
Result := DefaultValue;
end;
//------------------------------------------------------------------------------
Function TDefRegistry.ReadTimeDef(const Name: String; const DefaultValue: TDateTime): TDateTime;
begin
If not TryReadTime(Name,Result) then
Result := DefaultValue;
end;
end.