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