File: RegistrySettings.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (301 lines) | stat: -rw-r--r-- 7,887 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
unit RegistrySettings;

{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code 

The Original Code is RegistrySettings.pas, released October 2001.
The Initial Developer of the Original Code is Anthony Steele. 
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved.
Contributor(s): Anthony Steele. 

The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations 
under the License.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

{$I JcfGlobal.inc}

interface

{ this unit is conrete subclasses of the input & output streams
 that wrap the registry
 it is not portable as it uses the regirst via a TRegistry object }

  { object to read settings from the registry
    Due to design above (ie the ExtractSection function),
    this object serves as both the root and the section
  }
uses
  { delphi }
  Registry, Classes,
  { local }
  SettingsStream;

type

  TSettingsRegistryOutput = class(TSettingsOutput)
  private
    fReg: TRegIniFile;
    fsSection: string;

  public

    constructor Create(const psRootKey: string);

    destructor Destroy; override;

    procedure OpenSection(const psName: string); override;
    procedure CloseSection(const psName: string); override;

    procedure Write(const psTagName, psValue: string); override;
    procedure Write(const psTagName: string; const piValue: integer); override;
    procedure Write(const psTagName: string; const pbValue: boolean); override;
    procedure Write(const psTagName: string; const pdValue: double); override;
    procedure Write(const psTagName: string; const pcValue: TStrings); override;
  end;

  { TSettingsInputRegistry }

  TSettingsInputRegistry = class(TSettingsInput)
  private
    fReg: TRegIniFile;
    fsSection: string;
    fbOwnReg: boolean;

  public
    constructor Create(const psRootKey: string); overload;
    constructor Create(const pcReg: TRegIniFile; const psSection: string); overload;

    destructor Destroy; override;

    function ExtractSection(const psSection: string): TSettingsInput; override;

    function HasTag(const psTag: string): boolean; override;

    function Read(const psTag: string): string; override;
    function Read(const psTag, psDefault: string): string; override;
    function Read(const psTag: string; const piDefault: integer): integer; override;
    function Read(const psTag: string; const pfDefault: double): double; override;
    function Read(const psTag: string; const pbDefault: boolean): boolean; override;
    function Read(const psTag: string; const pcStrings: TStrings): boolean; override;
  end;

implementation

uses
  { delphi }
  SysUtils,
  { local }
  JcfMiscFunctions,
  JcfStringUtils;

{-------------------------------------------------------------------------------
  Settings Output Registry }

constructor TSettingsRegistryOutput.Create(const psRootKey: string);
begin
  Assert(psRootKey <> '');
  inherited Create;

  fReg := TRegIniFile.Create;
  fReg.OpenKey(psRootKey, True);
  fsSection := '';
end;

destructor TSettingsRegistryOutput.Destroy;
begin
  FreeAndNil(fReg);
  inherited;
end;

procedure TSettingsRegistryOutput.OpenSection(const psName: string);
begin
  fsSection := psName;
end;

procedure TSettingsRegistryOutput.CloseSection(const psName: string);
begin
  fsSection := '';
end;

procedure TSettingsRegistryOutput.Write(const psTagName, psValue: string);
begin
  Assert(fReg <> nil);
  fReg.WriteString(fsSection, psTagName, psValue);
end;

procedure TSettingsRegistryOutput.Write(const psTagName: string; const piValue: integer);
begin
  Assert(fReg <> nil);
  fReg.WriteInteger(fsSection, psTagName, piValue);
end;

procedure TSettingsRegistryOutput.Write(const psTagName: string; const pbValue: boolean);
begin
  Assert(fReg <> nil);
  fReg.WriteBool(fsSection, psTagName, pbValue);
end;

procedure TSettingsRegistryOutput.Write(const psTagName: string; const pdValue: double);
begin
  Assert(fReg <> nil);
  // WTF? WriteFloat is broken in D6?
  fReg.WriteString(fsSection, psTagName, Float2Str(pdValue));
end;

// method from JcfSetBase
procedure TSettingsRegistryOutput.Write(const psTagName: string;
  const pcValue: TStrings);
var
  liLoop: integer;
  lsItem: string;
begin
  Assert(pcValue <> nil);

  Write(psTagName, 'String list');
  Write(psTagName + '_Count', pcValue.Count);

  for liLoop := 1 to pcValue.Count do
  begin
    lsItem := psTagName + '_' + string(PadNumber(liLoop));
    Write(lsItem, pcValue.Strings[liLoop - 1]);
  end;
end;

{-------------------------------------------------------------------------------
  SettingsInputRegistry }

constructor TSettingsInputRegistry.Create(const psRootKey: string);
begin
  Assert(psRootKey <> '');
  inherited Create;

  fReg := TRegIniFile.Create;
  fReg.OpenKey(psRootKey, True);

  fsSection := '';
  fbOwnReg  := True;
end;

constructor TSettingsInputRegistry.Create(const pcReg: TRegIniFile;
  const psSection: string);
begin
  Assert(psSection <> '');
  Assert(pcReg <> nil);

  inherited Create;

  fReg      := pcReg;
  fsSection := psSection;
  fbOwnReg  := False;
end;



destructor TSettingsInputRegistry.Destroy;
begin
  if fbOwnReg then
    FreeAndNil(fReg)
  else
    fReg := nil;

  inherited Destroy;
end;

function TSettingsInputRegistry.ExtractSection(const psSection: string): TSettingsInput;
begin
  Assert(fReg <> nil);
  Result := TSettingsInputRegistry.Create(fReg, psSection);
end;

function TSettingsInputRegistry.HasTag(const psTag: string): boolean;
const
  NON_EXISISTENCE_MARKER = '~';
var
  lsValue: string;
begin
  Assert(fReg <> nil);
  lsValue := fReg.ReadString(fsSection, psTag, NON_EXISISTENCE_MARKER);
  Result  := (lsValue <> NON_EXISISTENCE_MARKER);
end;

function TSettingsInputRegistry.Read(const psTag: string): string;
begin
  Assert(fReg <> nil);
  Result := fReg.ReadString(fsSection, psTag, '')
end;

function TSettingsInputRegistry.Read(const psTag, psDefault: string): string;
begin
  Assert(fReg <> nil);
  Result := fReg.ReadString(fsSection, psTag, psDefault)
end;

function TSettingsInputRegistry.Read(const psTag: string;
  const piDefault: integer): integer;
begin
  Assert(fReg <> nil);
  Result := fReg.ReadInteger(fsSection, psTag, piDefault)
end;

function TSettingsInputRegistry.Read(const psTag: string;
  const pfDefault: double): double;
begin
  Assert(fReg <> nil);
  Result := Str2Float(fReg.ReadString(fsSection, psTag, Float2Str(pfDefault)));
end;

function TSettingsInputRegistry.Read(const psTag: string;
  const pbDefault: boolean): boolean;
begin
  Assert(fReg <> nil);
  Result := fReg.ReadBool(fsSection, psTag, pbDefault)
end;


// method from JcfSetBase
function TSettingsInputRegistry.Read(const psTag: string;
  const pcStrings: TStrings): boolean;
var
  lsItemName, lsItem: string;
  liCount, liLoop:    integer;
begin
  Assert(pcStrings <> nil);

  lsItem := Read(psTag, '');
  if lsItem = '' then
  begin
    Result := False;
  end
  else
  begin
    pcStrings.Clear;

    liCount := Read(psTag + '_Count', 0);

    for liLoop := 1 to liCount do
    begin
      lsItemName := psTag + '_' + string(PadNumber(liLoop));
      lsItem     := Read(lsItemName, '');
      if lsItem <> '' then
        pcStrings.Add(lsItem);
    end;

    Result := True;
  end;
end;


end.