source: trunk/Packages/Common/URegistry.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 3.6 KB
Line 
1unit URegistry;
2
3{$MODE Delphi}
4
5interface
6
7uses
8 Classes, Registry;
9
10type
11 TRegistryRoot = (rrKeyClassesRoot = HKEY($80000000),
12 rrKeyCurrentUser = HKEY($80000001),
13 rrKeyLocalMachine = HKEY($80000002),
14 rrKeyUsers = HKEY($80000003),
15 rrKeyPerformanceData = HKEY($80000004),
16 rrKeyCurrentConfig = HKEY($80000005),
17 rrKeyDynData = HKEY($80000006));
18
19 { TRegistryContext }
20
21 TRegistryContext = record
22 RootKey: HKEY;
23 Key: string;
24 class operator Equal(A: TRegistryContext; B: TRegistryContext): Boolean;
25 end;
26
27 { TRegistryEx }
28
29 TRegistryEx = class(TRegistry)
30 private
31 function GetContext: TRegistryContext;
32 procedure SetContext(AValue: TRegistryContext);
33 public
34 function ReadBoolWithDefault(const Name: string;
35 DefaultValue: Boolean): Boolean;
36 function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
37 function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
38 function ReadFloatWithDefault(const Name: string;
39 DefaultValue: Double): Double;
40 function DeleteKeyRecursive(const Key: string): Boolean;
41 function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
42 property Context: TRegistryContext read GetContext write SetContext;
43 end;
44
45function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
46
47
48implementation
49
50function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
51begin
52 Result.RootKey := RootKey;
53 Result.Key := Key;
54end;
55
56{ TRegistryContext }
57
58class operator TRegistryContext.Equal(A: TRegistryContext; B: TRegistryContext
59 ): Boolean;
60begin
61 Result := (A.RootKey = B.RootKey) and (A.Key = B.Key);
62end;
63
64{ TRegistryEx }
65
66function TRegistryEx.ReadIntegerWithDefault(const Name: string;
67 DefaultValue: Integer): Integer;
68begin
69 if ValueExists(Name) then Result := ReadInteger(Name)
70 else begin
71 WriteInteger(Name, DefaultValue);
72 Result := DefaultValue;
73 end;
74end;
75
76function TRegistryEx.ReadStringWithDefault(const Name: string;
77 DefaultValue: string): string;
78begin
79 if ValueExists(Name) then Result := ReadString(Name)
80 else begin
81 WriteString(Name, DefaultValue);
82 Result := DefaultValue;
83 end;
84end;
85
86function TRegistryEx.ReadFloatWithDefault(const Name: string;
87 DefaultValue: Double): Double;
88begin
89 if ValueExists(Name) then Result := ReadFloat(Name)
90 else begin
91 WriteFloat(Name, DefaultValue);
92 Result := DefaultValue;
93 end;
94end;
95
96function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
97var
98 SubKeys: TStringList;
99 I: Integer;
100begin
101 try
102 SubKeys := TStringList.Create;
103 if OpenKey(Key, False) and HasSubKeys then begin
104 GetKeyNames(SubKeys);
105 for I := 0 to SubKeys.Count - 1 do
106 DeleteKeyRecursive(Key + '\' + SubKeys[I]);
107 end;
108 Result := DeleteKey(Key);
109 finally
110 SubKeys.Free;
111 end;
112end;
113
114function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
115begin
116 {$IFDEF Linux}
117 CloseKey;
118 {$ENDIF}
119 Result := inherited OpenKey(Key, CanCreate);
120end;
121
122function TRegistryEx.GetContext: TRegistryContext;
123begin
124 Result.Key := CurrentPath;
125 Result.RootKey := RootKey;
126end;
127
128procedure TRegistryEx.SetContext(AValue: TRegistryContext);
129begin
130 RootKey := AValue.RootKey;
131 OpenKey(AValue.Key, True);
132end;
133
134function TRegistryEx.ReadBoolWithDefault(const Name: string;
135 DefaultValue: Boolean): Boolean;
136begin
137 if ValueExists(Name) then Result := ReadBool(Name)
138 else begin
139 WriteBool(Name, DefaultValue);
140 Result := DefaultValue;
141 end;
142end;
143
144end.
Note: See TracBrowser for help on using the repository browser.