-
Notifications
You must be signed in to change notification settings - Fork 49
/
OmniXMLConf.pas
400 lines (340 loc) · 14.3 KB
/
OmniXMLConf.pas
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
(*******************************************************************************
* 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/MPL/ *
* *
* 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. *
* *
* The Original Code is OmniXMLConf.pas *
* *
* The Initial Developer of the Original Code is Miha Vrhovnik *
* http://simail.sourceforge.net/, http://xcollect.sourceforge.net *
* *
* Last changed: 2004-04-10 *
* *
* History: *
* 1.0.7: 2007-02-08 *
* - check if root node of xml indeed is a config file *
* if it's not then recreate it with conf as default root element *
* 1.0.6: 2006-12-10 *
* - config file now opens xml via stream and holds file open this *
* puts a lot less stress on computers with slower processors and it *
* also gets rid of EFCreateError... file already used by another *
* process if those computers are using antivirus software *
* 1.0.5: 2005-05-12 *
* - rewritten class constructor and thus fixed possiblity *
* of getting exception because FxmlRoot = nil *
* 1.0.4: 2004-05-28 *
* - added function Read / Write WideString *
* 1.0.3: 2004-04-10 *
* - added property SaveAfterChange if this property is True, *
* then document is saved after each change *
* - procedure SaveConfig is now public so you can force document change *
* 1.0.2: 2003-12-14 *
* - document can be marked as read only (no changes are saved) *
* 1.0.1: 2003-11-01 *
* - fixed bug in WriteIdentNode *
* 1.0.0: 2003-10-25 *
* - initial version *
* *
* Contributor(s): *
* op: Ondrej Pokorny *
*******************************************************************************)
unit OmniXMLConf;
interface
{$I OmniXML.inc}
{$IFDEF OmniXML_HasZeroBasedStrings}
{$ZEROBASEDSTRINGS OFF}
{$ENDIF}
uses SysUtils, Classes, OmniXML, OmniXMLUtils, Controls, Forms
{$IFDEF TNT_UNICODE}, TntSysUtils, TntClasses{$ENDIF};
//you may use this class as replacement for TIniFile
type TxmlConf=class
private
FFileName: WideString;
{$IFDEF TNT_UNICODE}
FFileStream: TTntFileStream;
{$ELSE}
FFileStream: TFileStream;
{$ENDIF}
FxmlDoc: IXMLDocument;
FxmlRoot: IXMLElement;
FSaveAfterChange: Boolean;
FReadOnly: Boolean;
dirty, shutdown: Boolean;
procedure WriteIdentNode(Section: WideString; Ident: WideString; Value: WideString);
public
constructor Create(FileName: WideString);
destructor Destroy; override;
function ReadInteger(Section: WideString; Ident: WideString; Default: Int64): Int64;
function ReadString(Section: WideString; Ident: WideString; Default: String): String;
function ReadWideString(Section: WideString; Ident: WideString; Default: WideString): WideString;
function ReadBool(Section: WideString; Ident: WideString; Default: Boolean): Boolean;
function ReadFloat(Section: WideString; Ident: WideString; Default: Extended): Extended;
function ReadDate(Section: WideString; Ident: WideString; Default: TDateTime): TDateTime;
function ReadTime(Section: WideString; Ident: WideString; Default: TDateTime): TDateTime;
function ReadDateTime(Section: WideString; Ident: WideString; Default: TDateTime): TDateTime;
procedure ReadControlSettings(Control: TControl; ctlName: WideString = '');
procedure WriteInteger(Section: WideString; Ident: WideString; Value: Int64);
procedure WriteString(Section: WideString; Ident: WideString; Value: WideString);
procedure WriteWideString(Section: WideString; Ident: WideString; Value: WideString);
procedure WriteBool(Section: WideString; Ident: WideString; Value: Boolean);
procedure WriteFloat(Section: WideString; Ident: WideString; Value: Extended);
procedure WriteDate(Section: WideString; Ident: WideString; Value: TDateTime);
procedure WriteTime(Section: WideString; Ident: WideString; Value: TDateTime);
procedure WriteDateTime(Section: WideString; Ident: WideString; Value: TDateTime);
procedure WriteControlSettings(Control: TControl; ctlName: WideString = '');
procedure SaveConfig;
public
property DocReadOnly: Boolean read FReadOnly write FReadOnly;
property SaveAfterChange: Boolean read FSaveAfterChange write FSaveAfterChange;
end;
implementation
{ TxmlConf }
constructor TxmlConf.Create(FileName: WideString);
begin
FSaveAfterChange := True;
FReadOnly := False;
FFileName := FileName;
FxmlDoc := CreateXMLDoc;
FxmlDoc.PreserveWhiteSpace := False;
FxmlRoot := nil;
//read file if exists
{$IFDEF TNT_UNICODE}
if WideFileExists(FFileName) then begin
FFileStream := TTntFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyWrite);
{$ELSE}
if FileExists(FFileName) then begin
FFileStream := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyWrite);
{$ENDIF}
FxmlDoc.LoadFromStream(FFileStream);
FxmlRoot := FxmlDoc.DocumentElement;
end
else begin
{$IFDEF TNT_UNICODE}
FFileStream := TTntFileStream.Create(FileName, fmOpenReadWrite or fmCreate or fmShareDenyWrite);
{$ELSE}
FFileStream := TFileStream.Create(FileName, fmOpenReadWrite or fmCreate or fmShareDenyWrite);
{$ENDIF}
end;
if (FxmlDoc <> nil) and (FxmlRoot <> nil) then begin
if FxmlDoc.DocumentElement.TagName <> 'conf' then begin
FxmlDoc := CreateXMLDoc;
FxmlDoc.PreserveWhiteSpace := False;
FxmlRoot := nil;
end;
end;
if FxmlRoot = nil then begin
FxmlDoc.AppendChild(FxmlDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="utf-8"'));
FxmlRoot := FxmlDoc.CreateElement('conf');
FxmlDoc.DocumentElement := FxmlRoot;
SaveConfig;
end;
end;
destructor TxmlConf.Destroy;
begin
shutdown := True;
SaveConfig; //Save settings before exit
shutdown := False;
FreeAndNil(FFileStream);
FxmlRoot := nil;
FxmlDoc := nil;
end;
function TxmlConf.ReadBool(Section, Ident: WideString; Default: Boolean): Boolean;
var dataNode: IXMLNode;
begin
//set return value to default one
Result := Default;
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
if Length(dataNode.Text) = 1 then
XMLStrToBool(dataNode.Text, Result)
else //this is solely for backward compatibility purposes
Result := (dataNode.Text[1] = 'T')
end;
end;
procedure TxmlConf.ReadControlSettings(Control: TControl; ctlName: WideString = '');
var t,l: Integer;
begin
if ctlName = '' then
ctlName := Control.Name;
if Control is TForm then begin
//damn this throws an exception
//(Control as TForm).Position := poDesigned;
//set form width & height only if form is sizeable
if (Control as TForm).BorderStyle = bsSizeable then begin
Control.Width := ReadInteger(ctlName, 'width', Control.Width);
Control.Height := ReadInteger(ctlName, 'height', Control.Height);
end;
t := (Screen.Height div 2) - (Control.Height div 2);
l := (Screen.Width div 2) - (Control.Width div 2);
Control.Top := ReadInteger(ctlName, 'top', t);
Control.Left := ReadInteger(ctlName, 'left', l);
end
else begin
Control.Width := ReadInteger(ctlName, 'width', Control.Width);
Control.Height := ReadInteger(ctlName, 'height', Control.Height);
Control.Top := ReadInteger(ctlName, 'top', Control.Top);
Control.Left := ReadInteger(ctlName, 'left', Control.Left);
end;
end;
function TxmlConf.ReadDate(Section, Ident: WideString; Default: TDateTime): TDateTime;
var dataNode: IXMLNode;
begin
//set return value to default one
Result := Default;
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
XMLStrToDate(dataNode.Text, Result)
end;
end;
function TxmlConf.ReadDateTime(Section, Ident: WideString; Default: TDateTime): TDateTime;
var dataNode: IXMLNode;
begin
//set return value to default one
Result := Default;
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
XMLStrToDateTime(dataNode.Text, Result)
end;
end;
function TxmlConf.ReadFloat(Section, Ident: WideString; Default: Extended): Extended;
var dataNode: IXMLNode;
begin
//set return value to default one
Result := Default;
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
XMLStrToExtended(dataNode.Text, Result)
end;
end;
function TxmlConf.ReadInteger(Section, Ident: WideString; Default: Int64): Int64;
var dataNode: IXMLNode;
begin
//set return value to default one
Result := Default;
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
XMLStrToInt64(dataNode.Text, Result)
end;
end;
function TxmlConf.ReadString(Section, Ident: WideString; Default: String): String;
var dataNode: IXMLNode;
begin
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
Result := dataNode.Text
end
//return default value
else Result := Default;
end;
function TxmlConf.ReadWideString(Section, Ident:WideString; Default: WideString): WideString;
var dataNode: IXMLNode;
begin
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
Result := dataNode.Text
end
//return default value
else Result := Default;
end;
function TxmlConf.ReadTime(Section, Ident: WideString; Default: TDateTime): TDateTime;
var dataNode: IXMLNode;
begin
//1st find section node
dataNode := FxmlRoot.SelectSingleNode(Section + '/' + Ident);
if dataNode <> nil then begin
XMLStrToTime(dataNode.Text, Result)
end
//return default value
else Result := Default;
end;
procedure TxmlConf.SaveConfig;
begin
if FFileName = '' then Exit;
if FReadOnly then Exit;
if FFileStream = nil then Exit;
if dirty or shutdown then
begin
FFileStream.Size := 0;//op: clear file stream before saving!!!
FxmlDoc.SaveToStream(FFileStream, ofIndent);
end;
end;
procedure TxmlConf.WriteBool(Section, Ident: WideString; Value: Boolean);
begin
WriteIdentNode(Section, Ident, XMLBoolToStr(Value));
end;
procedure TxmlConf.WriteControlSettings(Control: TControl; ctlName: WideString = '');
begin
if ctlName = '' then
ctlName := Control.Name;
WriteInteger(ctlName, 'width', Control.Width);
WriteInteger(ctlName, 'height', Control.Height);
WriteInteger(ctlName, 'top', Control.Top);
WriteInteger(ctlName, 'left', Control.Left);
if FSaveAfterChange then
SaveConfig;
end;
procedure TxmlConf.WriteDate(Section, Ident: WideString; Value: TDateTime);
begin
WriteIdentNode(Section, Ident, XMLDateToStr(Value));
end;
procedure TxmlConf.WriteDateTime(Section, Ident: WideString; Value: TDateTime);
begin
WriteIdentNode(Section, Ident, XMLDateTimeToStr(Value));
end;
procedure TxmlConf.WriteFloat(Section, Ident: WideString; Value: Extended);
begin
WriteIdentNode(Section, Ident, XMLExtendedToStr(Value));
end;
procedure TxmlConf.WriteIdentNode(Section, Ident: WideString; Value: WideString);
var sectNode, identNode: IXMLNode;
begin
dirty := True;
sectNode := nil;
identNode := nil;
//1st find section node
sectNode := FindNode(FxmlRoot, Section, '');
if sectNode <> nil then begin
//section node exists
//now find ident node
identNode := FindNode(sectNode, Ident, '');
//if does not exists then create it
if identNode = nil then
identNode := EnsureNode(sectNode, Ident);
end
//create both nodes
else begin
sectNode := EnsureNode(FxmlRoot, Section);
identNode := EnsureNode(sectNode, Ident);
end;
identNode.Text := Value;
if FSaveAfterChange then
SaveConfig;
end;
procedure TxmlConf.WriteInteger(Section, Ident: WideString; Value: Int64);
begin
WriteIdentNode(Section, Ident, XMLInt64ToStr(Value));
end;
procedure TxmlConf.WriteString(Section, Ident, Value: WideString);
begin
WriteIdentNode(Section, Ident, Value);
end;
procedure TxmlConf.WriteWideString(Section, Ident: WideString; Value: WideString);
begin
WriteIdentNode(Section, Ident, Value);
end;
procedure TxmlConf.WriteTime(Section, Ident: WideString; Value: TDateTime);
begin
WriteIdentNode(Section, Ident, XMLTimeToStr(Value));
end;
end.