source: trunk/Packages/Common/URegistry.pas

Last change on this file was 215, checked in by chronos, 3 years ago
  • Modified: Build under Lazarus 2.2.0.
  • Modified: Updated Common package.
File size: 4.7 KB
Line 
1unit URegistry;
2
3{$MODE delphi}
4
5interface
6
7uses
8 Classes, Registry;
9
10type
11 TRegistryRoot = (rrKeyClassesRoot, rrKeyCurrentUser, rrKeyLocalMachine,
12 rrKeyUsers, rrKeyPerformanceData, rrKeyCurrentConfig, rrKeyDynData);
13
14 { TRegistryContext }
15
16 TRegistryContext = record
17 RootKey: HKEY;
18 Key: string;
19 class function Create(RootKey: TRegistryRoot; Key: string): TRegistryContext; static; overload;
20 class function Create(RootKey: HKEY; Key: string): TRegistryContext; static; overload;
21 class operator Equal(A, B: TRegistryContext): Boolean;
22 end;
23
24 { TRegistryEx }
25
26 TRegistryEx = class(TRegistry)
27 private
28 function GetCurrentContext: TRegistryContext;
29 procedure SetCurrentContext(AValue: TRegistryContext);
30 public
31 function ReadChar(const Name: string): Char;
32 procedure WriteChar(const Name: string; Value: Char);
33 function ReadBoolWithDefault(const Name: string;
34 DefaultValue: Boolean): Boolean;
35 function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
36 function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
37 function ReadCharWithDefault(const Name: string; DefaultValue: Char): Char;
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 CurrentContext: TRegistryContext read GetCurrentContext write SetCurrentContext;
43 end;
44
45const
46 RegistryRootHKEY: array[TRegistryRoot] of HKEY = (HKEY_CLASSES_ROOT,
47 HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA,
48 HKEY_CURRENT_CONFIG, HKEY_DYN_DATA);
49
50implementation
51
52
53{ TRegistryContext }
54
55class operator TRegistryContext.Equal(A, B: TRegistryContext): Boolean;
56begin
57 Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
58end;
59
60class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
61begin
62 Result.RootKey := RegistryRootHKEY[RootKey];
63 Result.Key := Key;
64end;
65
66class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
67begin
68 Result.RootKey := RootKey;
69 Result.Key := Key;
70end;
71
72{ TRegistryEx }
73
74function TRegistryEx.ReadIntegerWithDefault(const Name: string;
75 DefaultValue: Integer): Integer;
76begin
77 if ValueExists(Name) then Result := ReadInteger(Name)
78 else begin
79 WriteInteger(Name, DefaultValue);
80 Result := DefaultValue;
81 end;
82end;
83
84function TRegistryEx.ReadStringWithDefault(const Name: string;
85 DefaultValue: string): string;
86begin
87 if ValueExists(Name) then Result := ReadString(Name)
88 else begin
89 WriteString(Name, DefaultValue);
90 Result := DefaultValue;
91 end;
92end;
93
94function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
95 ): Char;
96begin
97 if ValueExists(Name) then Result := ReadChar(Name)
98 else begin
99 WriteChar(Name, DefaultValue);
100 Result := DefaultValue;
101 end;
102end;
103
104function TRegistryEx.ReadFloatWithDefault(const Name: string;
105 DefaultValue: Double): Double;
106begin
107 if ValueExists(Name) then Result := ReadFloat(Name)
108 else begin
109 WriteFloat(Name, DefaultValue);
110 Result := DefaultValue;
111 end;
112end;
113
114function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
115var
116 SubKeys: TStringList;
117 I: Integer;
118begin
119 try
120 SubKeys := TStringList.Create;
121 if OpenKey(Key, False) and HasSubKeys then begin
122 GetKeyNames(SubKeys);
123 for I := 0 to SubKeys.Count - 1 do
124 DeleteKeyRecursive(Key + '\' + SubKeys[I]);
125 end;
126 Result := DeleteKey(Key);
127 finally
128 SubKeys.Free;
129 end;
130end;
131
132function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
133begin
134 {$IFDEF UNIX}
135 //CloseKey;
136 {$ENDIF}
137 Result := inherited OpenKey(Key, CanCreate);
138end;
139
140function TRegistryEx.GetCurrentContext: TRegistryContext;
141begin
142 Result.Key := String(CurrentPath);
143 Result.RootKey := RootKey;
144end;
145
146procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
147begin
148 RootKey := AValue.RootKey;
149 OpenKey(AValue.Key, True);
150end;
151
152function TRegistryEx.ReadChar(const Name: string): Char;
153var
154 S: string;
155begin
156 S := ReadString(Name);
157 if Length(S) > 0 then Result := S[1]
158 else Result := #0;
159end;
160
161procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
162begin
163 WriteString(Name, Value);
164end;
165
166function TRegistryEx.ReadBoolWithDefault(const Name: string;
167 DefaultValue: Boolean): Boolean;
168begin
169 if ValueExists(Name) then Result := ReadBool(Name)
170 else begin
171 WriteBool(Name, DefaultValue);
172 Result := DefaultValue;
173 end;
174end;
175
176end.
Note: See TracBrowser for help on using the repository browser.