source: trunk/Packages/Common/RegistryEx.pas

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