1 | unit URegistry;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, Registry;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TRegistryRoot = (rrKeyClassesRoot, rrKeyCurrentUser, rrKeyLocalMachine,
|
---|
10 | rrKeyUsers, rrKeyPerformanceData, rrKeyCurrentConfig, rrKeyDynData);
|
---|
11 |
|
---|
12 | { TRegistryContext }
|
---|
13 |
|
---|
14 | TRegistryContext = record
|
---|
15 | RootKey: HKEY;
|
---|
16 | Key: string;
|
---|
17 | class function Create(RootKey: TRegistryRoot; Key: string): TRegistryContext; static; overload;
|
---|
18 | class function Create(RootKey: HKEY; Key: string): TRegistryContext; static; overload;
|
---|
19 | class operator Equal(A, B: TRegistryContext): Boolean;
|
---|
20 | end;
|
---|
21 |
|
---|
22 | { TRegistryEx }
|
---|
23 |
|
---|
24 | TRegistryEx = class(TRegistry)
|
---|
25 | private
|
---|
26 | function GetCurrentContext: TRegistryContext;
|
---|
27 | procedure SetCurrentContext(AValue: TRegistryContext);
|
---|
28 | public
|
---|
29 | function ReadChar(const Name: string): Char;
|
---|
30 | procedure WriteChar(const Name: string; Value: Char);
|
---|
31 | function ReadBoolWithDefault(const Name: string;
|
---|
32 | DefaultValue: Boolean): Boolean;
|
---|
33 | function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
|
---|
34 | function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
|
---|
35 | function ReadCharWithDefault(const Name: string; DefaultValue: Char): Char;
|
---|
36 | function ReadFloatWithDefault(const Name: string;
|
---|
37 | DefaultValue: Double): Double;
|
---|
38 | function DeleteKeyRecursive(const Key: string): Boolean;
|
---|
39 | function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
---|
40 | function GetValue(const Name: string; const DefaultValue: Integer): Integer; overload;
|
---|
41 | function GetValue(const Name: string; const DefaultValue: string): string; overload;
|
---|
42 | function GetValue(const Name: string; const DefaultValue: Boolean): Boolean; overload;
|
---|
43 | function GetValue(const Name: string; const DefaultValue: Double): Double; overload;
|
---|
44 | function GetValue(const Name: string; const DefaultValue: Char): Char; overload;
|
---|
45 | procedure SetValue(const Name: string; const Value: Integer); overload;
|
---|
46 | procedure SetValue(const Name: string; const Value: string); overload;
|
---|
47 | procedure SetValue(const Name: string; const Value: Boolean); overload;
|
---|
48 | procedure SetValue(const Name: string; const Value: Double); overload;
|
---|
49 | procedure SetValue(const Name: string; const Value: Char); overload;
|
---|
50 | property CurrentContext: TRegistryContext read GetCurrentContext write SetCurrentContext;
|
---|
51 | end;
|
---|
52 |
|
---|
53 | const
|
---|
54 | RegistryRootHKEY: array[TRegistryRoot] of HKEY = (HKEY_CLASSES_ROOT,
|
---|
55 | HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA,
|
---|
56 | HKEY_CURRENT_CONFIG, HKEY_DYN_DATA);
|
---|
57 |
|
---|
58 |
|
---|
59 | implementation
|
---|
60 |
|
---|
61 | { TRegistryContext }
|
---|
62 |
|
---|
63 | class operator TRegistryContext.Equal(A, B: TRegistryContext): Boolean;
|
---|
64 | begin
|
---|
65 | Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
|
---|
66 | end;
|
---|
67 |
|
---|
68 | class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
|
---|
69 | begin
|
---|
70 | Result.RootKey := RegistryRootHKEY[RootKey];
|
---|
71 | Result.Key := Key;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
|
---|
75 | begin
|
---|
76 | Result.RootKey := RootKey;
|
---|
77 | Result.Key := Key;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | { TRegistryEx }
|
---|
81 |
|
---|
82 | function TRegistryEx.ReadIntegerWithDefault(const Name: string;
|
---|
83 | DefaultValue: Integer): Integer;
|
---|
84 | begin
|
---|
85 | if ValueExists(Name) then Result := ReadInteger(Name)
|
---|
86 | else begin
|
---|
87 | WriteInteger(Name, DefaultValue);
|
---|
88 | Result := DefaultValue;
|
---|
89 | end;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | function TRegistryEx.ReadStringWithDefault(const Name: string;
|
---|
93 | DefaultValue: string): string;
|
---|
94 | begin
|
---|
95 | if ValueExists(Name) then Result := ReadString(Name)
|
---|
96 | else begin
|
---|
97 | WriteString(Name, DefaultValue);
|
---|
98 | Result := DefaultValue;
|
---|
99 | end;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
|
---|
103 | ): Char;
|
---|
104 | begin
|
---|
105 | if ValueExists(Name) then Result := ReadChar(Name)
|
---|
106 | else begin
|
---|
107 | WriteChar(Name, DefaultValue);
|
---|
108 | Result := DefaultValue;
|
---|
109 | end;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | function TRegistryEx.ReadFloatWithDefault(const Name: string;
|
---|
113 | DefaultValue: Double): Double;
|
---|
114 | begin
|
---|
115 | if ValueExists(Name) then Result := ReadFloat(Name)
|
---|
116 | else begin
|
---|
117 | WriteFloat(Name, DefaultValue);
|
---|
118 | Result := DefaultValue;
|
---|
119 | end;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | function TRegistryEx.GetValue(const Name: string; const DefaultValue: Integer
|
---|
123 | ): Integer;
|
---|
124 | begin
|
---|
125 | Result := ReadIntegerWithDefault(Name, DefaultValue);
|
---|
126 | end;
|
---|
127 |
|
---|
128 | function TRegistryEx.GetValue(const Name: string; const DefaultValue: string
|
---|
129 | ): string;
|
---|
130 | begin
|
---|
131 | Result := ReadStringWithDefault(Name, DefaultValue);
|
---|
132 | end;
|
---|
133 |
|
---|
134 | function TRegistryEx.GetValue(const Name: string; const DefaultValue: Boolean
|
---|
135 | ): Boolean;
|
---|
136 | begin
|
---|
137 | Result := ReadBoolWithDefault(Name, DefaultValue);
|
---|
138 | end;
|
---|
139 |
|
---|
140 | function TRegistryEx.GetValue(const Name: string; const DefaultValue: Double
|
---|
141 | ): Double;
|
---|
142 | begin
|
---|
143 | Result := ReadFloatWithDefault(Name, DefaultValue);
|
---|
144 | end;
|
---|
145 |
|
---|
146 | function TRegistryEx.GetValue(const Name: string; const DefaultValue: Char
|
---|
147 | ): Char;
|
---|
148 | begin
|
---|
149 | Result := ReadCharWithDefault(Name, DefaultValue);
|
---|
150 | end;
|
---|
151 |
|
---|
152 | procedure TRegistryEx.SetValue(const Name: string; const Value: Integer);
|
---|
153 | begin
|
---|
154 | WriteInteger(Name, Value);
|
---|
155 | end;
|
---|
156 |
|
---|
157 | procedure TRegistryEx.SetValue(const Name: string; const Value: string);
|
---|
158 | begin
|
---|
159 | WriteString(Name, Value);
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TRegistryEx.SetValue(const Name: string; const Value: Boolean);
|
---|
163 | begin
|
---|
164 | WriteBool(Name, Value);
|
---|
165 | end;
|
---|
166 |
|
---|
167 | procedure TRegistryEx.SetValue(const Name: string; const Value: Double);
|
---|
168 | begin
|
---|
169 | WriteFloat(Name, Value);
|
---|
170 | end;
|
---|
171 |
|
---|
172 | procedure TRegistryEx.SetValue(const Name: string; const Value: Char);
|
---|
173 | begin
|
---|
174 | WriteChar(Name, Value);
|
---|
175 | end;
|
---|
176 |
|
---|
177 | function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
|
---|
178 | var
|
---|
179 | SubKeys: TStringList;
|
---|
180 | I: Integer;
|
---|
181 | begin
|
---|
182 | try
|
---|
183 | SubKeys := TStringList.Create;
|
---|
184 | if OpenKey(Key, False) and HasSubKeys then begin
|
---|
185 | GetKeyNames(SubKeys);
|
---|
186 | for I := 0 to SubKeys.Count - 1 do
|
---|
187 | DeleteKeyRecursive(Key + '\' + SubKeys[I]);
|
---|
188 | end;
|
---|
189 | Result := DeleteKey(Key);
|
---|
190 | finally
|
---|
191 | SubKeys.Free;
|
---|
192 | end;
|
---|
193 | end;
|
---|
194 |
|
---|
195 | function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
---|
196 | begin
|
---|
197 | {$IFDEF UNIX}
|
---|
198 | //CloseKey;
|
---|
199 | {$ENDIF}
|
---|
200 | Result := inherited OpenKey(Key, CanCreate);
|
---|
201 | end;
|
---|
202 |
|
---|
203 | function TRegistryEx.GetCurrentContext: TRegistryContext;
|
---|
204 | begin
|
---|
205 | Result.Key := String(CurrentPath);
|
---|
206 | Result.RootKey := RootKey;
|
---|
207 | end;
|
---|
208 |
|
---|
209 | procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
|
---|
210 | begin
|
---|
211 | RootKey := AValue.RootKey;
|
---|
212 | OpenKey(AValue.Key, True);
|
---|
213 | end;
|
---|
214 |
|
---|
215 | function TRegistryEx.ReadChar(const Name: string): Char;
|
---|
216 | var
|
---|
217 | S: string;
|
---|
218 | begin
|
---|
219 | S := ReadString(Name);
|
---|
220 | if Length(S) > 0 then Result := S[1]
|
---|
221 | else Result := #0;
|
---|
222 | end;
|
---|
223 |
|
---|
224 | procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
|
---|
225 | begin
|
---|
226 | WriteString(Name, Value);
|
---|
227 | end;
|
---|
228 |
|
---|
229 | function TRegistryEx.ReadBoolWithDefault(const Name: string;
|
---|
230 | DefaultValue: Boolean): Boolean;
|
---|
231 | begin
|
---|
232 | if ValueExists(Name) then Result := ReadBool(Name)
|
---|
233 | else begin
|
---|
234 | WriteBool(Name, DefaultValue);
|
---|
235 | Result := DefaultValue;
|
---|
236 | end;
|
---|
237 | end;
|
---|
238 |
|
---|
239 | end.
|
---|