source: trunk/Packages/Common/URegistry.pas

Last change on this file was 25, checked in by chronos, 21 months ago
  • Modified: CoolTranslator replaced by Common package.
  • Modified: Update common package.
File size: 6.9 KB
Line 
1unit URegistry;
2
3interface
4
5uses
6 Classes, Registry;
7
8type
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
53const
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
59implementation
60
61{ TRegistryContext }
62
63class operator TRegistryContext.Equal(A, B: TRegistryContext): Boolean;
64begin
65 Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
66end;
67
68class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
69begin
70 Result.RootKey := RegistryRootHKEY[RootKey];
71 Result.Key := Key;
72end;
73
74class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
75begin
76 Result.RootKey := RootKey;
77 Result.Key := Key;
78end;
79
80{ TRegistryEx }
81
82function TRegistryEx.ReadIntegerWithDefault(const Name: string;
83 DefaultValue: Integer): Integer;
84begin
85 if ValueExists(Name) then Result := ReadInteger(Name)
86 else begin
87 WriteInteger(Name, DefaultValue);
88 Result := DefaultValue;
89 end;
90end;
91
92function TRegistryEx.ReadStringWithDefault(const Name: string;
93 DefaultValue: string): string;
94begin
95 if ValueExists(Name) then Result := ReadString(Name)
96 else begin
97 WriteString(Name, DefaultValue);
98 Result := DefaultValue;
99 end;
100end;
101
102function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
103 ): Char;
104begin
105 if ValueExists(Name) then Result := ReadChar(Name)
106 else begin
107 WriteChar(Name, DefaultValue);
108 Result := DefaultValue;
109 end;
110end;
111
112function TRegistryEx.ReadFloatWithDefault(const Name: string;
113 DefaultValue: Double): Double;
114begin
115 if ValueExists(Name) then Result := ReadFloat(Name)
116 else begin
117 WriteFloat(Name, DefaultValue);
118 Result := DefaultValue;
119 end;
120end;
121
122function TRegistryEx.GetValue(const Name: string; const DefaultValue: Integer
123 ): Integer;
124begin
125 Result := ReadIntegerWithDefault(Name, DefaultValue);
126end;
127
128function TRegistryEx.GetValue(const Name: string; const DefaultValue: string
129 ): string;
130begin
131 Result := ReadStringWithDefault(Name, DefaultValue);
132end;
133
134function TRegistryEx.GetValue(const Name: string; const DefaultValue: Boolean
135 ): Boolean;
136begin
137 Result := ReadBoolWithDefault(Name, DefaultValue);
138end;
139
140function TRegistryEx.GetValue(const Name: string; const DefaultValue: Double
141 ): Double;
142begin
143 Result := ReadFloatWithDefault(Name, DefaultValue);
144end;
145
146function TRegistryEx.GetValue(const Name: string; const DefaultValue: Char
147 ): Char;
148begin
149 Result := ReadCharWithDefault(Name, DefaultValue);
150end;
151
152procedure TRegistryEx.SetValue(const Name: string; const Value: Integer);
153begin
154 WriteInteger(Name, Value);
155end;
156
157procedure TRegistryEx.SetValue(const Name: string; const Value: string);
158begin
159 WriteString(Name, Value);
160end;
161
162procedure TRegistryEx.SetValue(const Name: string; const Value: Boolean);
163begin
164 WriteBool(Name, Value);
165end;
166
167procedure TRegistryEx.SetValue(const Name: string; const Value: Double);
168begin
169 WriteFloat(Name, Value);
170end;
171
172procedure TRegistryEx.SetValue(const Name: string; const Value: Char);
173begin
174 WriteChar(Name, Value);
175end;
176
177function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
178var
179 SubKeys: TStringList;
180 I: Integer;
181begin
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;
193end;
194
195function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
196begin
197 {$IFDEF UNIX}
198 //CloseKey;
199 {$ENDIF}
200 Result := inherited OpenKey(Key, CanCreate);
201end;
202
203function TRegistryEx.GetCurrentContext: TRegistryContext;
204begin
205 Result.Key := String(CurrentPath);
206 Result.RootKey := RootKey;
207end;
208
209procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
210begin
211 RootKey := AValue.RootKey;
212 OpenKey(AValue.Key, True);
213end;
214
215function TRegistryEx.ReadChar(const Name: string): Char;
216var
217 S: string;
218begin
219 S := ReadString(Name);
220 if Length(S) > 0 then Result := S[1]
221 else Result := #0;
222end;
223
224procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
225begin
226 WriteString(Name, Value);
227end;
228
229function TRegistryEx.ReadBoolWithDefault(const Name: string;
230 DefaultValue: Boolean): Boolean;
231begin
232 if ValueExists(Name) then Result := ReadBool(Name)
233 else begin
234 WriteBool(Name, DefaultValue);
235 Result := DefaultValue;
236 end;
237end;
238
239end.
Note: See TracBrowser for help on using the repository browser.