source: trunk/Packages/Common/RegistryEx.pas

Last change on this file was 2, checked in by chronos, 16 months ago
  • Added: Initial development version.
File size: 4.7 KB
Line 
1unit RegistryEx;
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 property CurrentContext: TRegistryContext read GetCurrentContext write SetCurrentContext;
41 end;
42
43const
44 RegistryRootHKEY: array[TRegistryRoot] of HKEY = (HKEY_CLASSES_ROOT,
45 HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA,
46 HKEY_CURRENT_CONFIG, HKEY_DYN_DATA);
47
48
49implementation
50
51{ TRegistryContext }
52
53class operator TRegistryContext.Equal(A, B: TRegistryContext): Boolean;
54begin
55 Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
56end;
57
58class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
59begin
60 Result.RootKey := RegistryRootHKEY[RootKey];
61 Result.Key := Key;
62end;
63
64class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
65begin
66 Result.RootKey := RootKey;
67 Result.Key := Key;
68end;
69
70{ TRegistryEx }
71
72function TRegistryEx.ReadIntegerWithDefault(const Name: string;
73 DefaultValue: Integer): Integer;
74begin
75 if ValueExists(Name) then Result := ReadInteger(Name)
76 else begin
77 WriteInteger(Name, DefaultValue);
78 Result := DefaultValue;
79 end;
80end;
81
82function TRegistryEx.ReadStringWithDefault(const Name: string;
83 DefaultValue: string): string;
84begin
85 if ValueExists(Name) then Result := ReadString(Name)
86 else begin
87 WriteString(Name, DefaultValue);
88 Result := DefaultValue;
89 end;
90end;
91
92function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
93 ): Char;
94begin
95 if ValueExists(Name) then Result := ReadChar(Name)
96 else begin
97 WriteChar(Name, DefaultValue);
98 Result := DefaultValue;
99 end;
100end;
101
102function TRegistryEx.ReadFloatWithDefault(const Name: string;
103 DefaultValue: Double): Double;
104begin
105 if ValueExists(Name) then Result := ReadFloat(Name)
106 else begin
107 WriteFloat(Name, DefaultValue);
108 Result := DefaultValue;
109 end;
110end;
111
112function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
113var
114 SubKeys: TStringList;
115 I: Integer;
116begin
117 try
118 SubKeys := TStringList.Create;
119 if OpenKey(Key, False) and HasSubKeys then begin
120 GetKeyNames(SubKeys);
121 for I := 0 to SubKeys.Count - 1 do
122 DeleteKeyRecursive(Key + '\' + SubKeys[I]);
123 end;
124 Result := DeleteKey(Key);
125 finally
126 SubKeys.Free;
127 end;
128end;
129
130function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
131begin
132 {$IFDEF UNIX}
133 //CloseKey;
134 {$ENDIF}
135 Result := inherited OpenKey(Key, CanCreate);
136end;
137
138function TRegistryEx.GetCurrentContext: TRegistryContext;
139begin
140 Result.Key := String(CurrentPath);
141 Result.RootKey := RootKey;
142end;
143
144procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
145begin
146 RootKey := AValue.RootKey;
147 OpenKey(AValue.Key, True);
148end;
149
150function TRegistryEx.ReadChar(const Name: string): Char;
151var
152 S: string;
153begin
154 S := ReadString(Name);
155 if Length(S) > 0 then Result := S[1]
156 else Result := #0;
157end;
158
159procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
160begin
161 WriteString(Name, Value);
162end;
163
164function TRegistryEx.ReadBoolWithDefault(const Name: string;
165 DefaultValue: Boolean): Boolean;
166begin
167 if ValueExists(Name) then Result := ReadBool(Name)
168 else begin
169 WriteBool(Name, DefaultValue);
170 Result := DefaultValue;
171 end;
172end;
173
174end.
Note: See TracBrowser for help on using the repository browser.