1 | unit URegistry;
|
---|
2 |
|
---|
3 | {$MODE delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, Registry;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
45 | const
|
---|
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 |
|
---|
50 | implementation
|
---|
51 |
|
---|
52 |
|
---|
53 | { TRegistryContext }
|
---|
54 |
|
---|
55 | class operator TRegistryContext.Equal(A, B: TRegistryContext): Boolean;
|
---|
56 | begin
|
---|
57 | Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
|
---|
58 | end;
|
---|
59 |
|
---|
60 | class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
|
---|
61 | begin
|
---|
62 | Result.RootKey := RegistryRootHKEY[RootKey];
|
---|
63 | Result.Key := Key;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
|
---|
67 | begin
|
---|
68 | Result.RootKey := RootKey;
|
---|
69 | Result.Key := Key;
|
---|
70 | end;
|
---|
71 |
|
---|
72 | { TRegistryEx }
|
---|
73 |
|
---|
74 | function TRegistryEx.ReadIntegerWithDefault(const Name: string;
|
---|
75 | DefaultValue: Integer): Integer;
|
---|
76 | begin
|
---|
77 | if ValueExists(Name) then Result := ReadInteger(Name)
|
---|
78 | else begin
|
---|
79 | WriteInteger(Name, DefaultValue);
|
---|
80 | Result := DefaultValue;
|
---|
81 | end;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | function TRegistryEx.ReadStringWithDefault(const Name: string;
|
---|
85 | DefaultValue: string): string;
|
---|
86 | begin
|
---|
87 | if ValueExists(Name) then Result := ReadString(Name)
|
---|
88 | else begin
|
---|
89 | WriteString(Name, DefaultValue);
|
---|
90 | Result := DefaultValue;
|
---|
91 | end;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
|
---|
95 | ): Char;
|
---|
96 | begin
|
---|
97 | if ValueExists(Name) then Result := ReadChar(Name)
|
---|
98 | else begin
|
---|
99 | WriteChar(Name, DefaultValue);
|
---|
100 | Result := DefaultValue;
|
---|
101 | end;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | function TRegistryEx.ReadFloatWithDefault(const Name: string;
|
---|
105 | DefaultValue: Double): Double;
|
---|
106 | begin
|
---|
107 | if ValueExists(Name) then Result := ReadFloat(Name)
|
---|
108 | else begin
|
---|
109 | WriteFloat(Name, DefaultValue);
|
---|
110 | Result := DefaultValue;
|
---|
111 | end;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | function TRegistryEx.DeleteKeyRecursive(const Key: string): Boolean;
|
---|
115 | var
|
---|
116 | SubKeys: TStringList;
|
---|
117 | I: Integer;
|
---|
118 | begin
|
---|
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;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
---|
133 | begin
|
---|
134 | {$IFDEF UNIX}
|
---|
135 | //CloseKey;
|
---|
136 | {$ENDIF}
|
---|
137 | Result := inherited OpenKey(Key, CanCreate);
|
---|
138 | end;
|
---|
139 |
|
---|
140 | function TRegistryEx.GetCurrentContext: TRegistryContext;
|
---|
141 | begin
|
---|
142 | Result.Key := String(CurrentPath);
|
---|
143 | Result.RootKey := RootKey;
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TRegistryEx.SetCurrentContext(AValue: TRegistryContext);
|
---|
147 | begin
|
---|
148 | RootKey := AValue.RootKey;
|
---|
149 | OpenKey(AValue.Key, True);
|
---|
150 | end;
|
---|
151 |
|
---|
152 | function TRegistryEx.ReadChar(const Name: string): Char;
|
---|
153 | var
|
---|
154 | S: string;
|
---|
155 | begin
|
---|
156 | S := ReadString(Name);
|
---|
157 | if Length(S) > 0 then Result := S[1]
|
---|
158 | else Result := #0;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
|
---|
162 | begin
|
---|
163 | WriteString(Name, Value);
|
---|
164 | end;
|
---|
165 |
|
---|
166 | function TRegistryEx.ReadBoolWithDefault(const Name: string;
|
---|
167 | DefaultValue: Boolean): Boolean;
|
---|
168 | begin
|
---|
169 | if ValueExists(Name) then Result := ReadBool(Name)
|
---|
170 | else begin
|
---|
171 | WriteBool(Name, DefaultValue);
|
---|
172 | Result := DefaultValue;
|
---|
173 | end;
|
---|
174 | end;
|
---|
175 |
|
---|
176 | end.
|
---|