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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
{$mode objfpc}
{$h+}
unit xmlreg;
Interface
uses
sysutils,classes,dom,xmlread,xmlwrite;
Type
TDataType = (dtUnknown,dtDWORD,dtString,dtBinary);
TDataInfo = record
DataType : TDataType;
DataSize : Integer;
end;
TKeyInfo = record
SubKeys,
SubKeyLen,
Values,
ValueLen,
DataLen : Integer;
FTime : TDateTime;
end;
{ TXmlRegistry }
TXmlRegistry = Class(TObject)
Private
FAutoFlush,
FDirty : Boolean;
FFileName : String;
FRootKey : String;
FDocument : TXMLDocument;
FCurrentElement : TDomElement;
FCurrentKey : String;
Procedure SetFileName(Value : String);
Protected
Procedure LoadFromStream(S : TStream);
Function NormalizeKey(KeyPath : String) : String;
Procedure CreateEmptyDoc;
Function FindKey (S : String) : TDomElement;
Function FindSubKey (S : String; N : TDomElement) : TDomElement;
Function CreateSubKey (S : String; N : TDomElement) : TDomElement;
Function FindValueKey (S : String) : TDomElement;
Function CreateValueKey (S : String) : TDomElement;
Function BufToHex(Const Buf; Len : Integer) : String;
Function hexToBuf(Const Str : String; Var Buf; Var Len : Integer ) : Integer;
Procedure MaybeFlush;
Property Document : TXMLDocument Read FDocument;
Property Dirty : Boolean Read FDirty write FDirty;
Public
Constructor Create(AFileName : String);
Destructor Destroy;override;
Function SetKey(KeyPath : String; AllowCreate : Boolean) : Boolean ;
Procedure SetRootKey(Value : String);
Function DeleteKey(KeyPath : String) : Boolean;
Function CreateKey(KeyPath : String) : Boolean;
Function GetValueSize(Name : String) : Integer;
Function GetValueType(Name : String) : TDataType;
Function GetValueInfo(Name : String; Var Info : TDataInfo) : Boolean;
Function GetKeyInfo(Var Info : TKeyInfo) : Boolean;
Function EnumSubKeys(List : TStrings) : Integer;
Function EnumValues(List : TStrings) : Integer;
Function KeyExists(KeyPath : String) : Boolean;
Function ValueExists(ValueName : String) : Boolean;
Function RenameValue(Const OldName,NewName : String) : Boolean;
Function DeleteValue(S : String) : Boolean;
Procedure Flush;
Procedure Load;
Function GetValueData(Name : String; Var DataType : TDataType; Var Data; Var DataSize : Integer) : Boolean;
Function SetValueData(Name : String; DataType : TDataType; Const Data; DataSize : Integer) : Boolean;
Property FileName : String Read FFileName Write SetFileName;
Property RootKey : String Read FRootKey Write SetRootkey;
Property AutoFlush : Boolean Read FAutoFlush Write FAutoFlush;
end;
// used Key types
Const
SXmlReg = 'XMLReg';
SKey = 'Key';
SValue = 'Value';
SName = 'Name';
SType = 'Type';
SData = 'Data';
Implementation
Constructor TXmlRegistry.Create(AFileName : String);
begin
FFileName:=AFileName;
FautoFlush:=True;
If (AFileName<>'') then
Load
else
CreateEmptyDoc;
end;
destructor TXmlRegistry.Destroy;
begin
if Assigned(FDocument) then FDocument.Free;
inherited Destroy;
end;
Procedure TXmlRegistry.SetFileName(Value : String);
begin
If Value<>FFileName then
begin
FFilename:=Value;
Flush;
end;
end;
Procedure TXmlRegistry.CreateEmptyDoc;
Const
template = '<?xml version="1.0" encoding="ISO8859-1"?>'+LineEnding+
'<'+SXMLReg+'>'+LineEnding+
'</'+SXMLReg+'>'+LineEnding;
Var
S : TStream;
begin
S:=TStringStream.Create(Template);
S.Seek(0,soFromBeginning);
Try
LoadFromStream(S);
Finally
S.Free;
end;
end;
Function TXmlRegistry.NormalizeKey(KeyPath : String) : String;
Var
L : Integer;
begin
Result:=StringReplace(KeyPath,'\','/',[rfReplaceAll]);
L:=Length(Result);
If (L>0) and (Result[L]<>'/') then
Result:=Result+'/';
If (L>0) and (Result[1]<>'/') then
Result:='/' + Result;
end;
Function TXmlRegistry.SetKey(KeyPath : String; AllowCreate : Boolean) : boolean;
Var
SubKey,ResultKey : String;
P : Integer;
Node,Node2 : TDomElement;
begin
Result:=(Length(KeyPath)>0);
If Not Result then
Exit;
KeyPath:=NormalizeKey(KeyPath);
If (FCurrentElement<>nil) then
begin
Delete(Keypath,1,1);
Node:=FCurrentElement;
Resultkey:=FCurrentKey;
end
else
begin
Delete(Keypath,1,1);
Node:=FDocument.DocumentElement;
If (FRootKey<>'') then
KeyPath:=FRootKey+KeyPath;
ResultKey:='';
end;
Result:=True;
repeat
P:=Pos('/',KeyPath);
If (P<>0) then
begin
SubKey:=Copy(KeyPath,1,P-1);
Delete(KeyPath,1,P);
Node2:=FindSubKey(SubKey,Node);
Result:=(Node2<>Nil);
If Result then
Node:=Node2
else
begin
If AllowCreate then
Begin
Node2:=CreateSubKey(SubKey,Node);
Result:=Node2<>Nil;
If Result Then
Node:=Node2;
end;
end;
If Result then
ResultKey:=ResultKey+SubKey+'/';
end;
Until (Not Result) or (Length(KeyPath)=0);
If Result then
begin
FCurrentkey:=ResultKey;
FCurrentElement:=Node;
end;
MaybeFlush;
end;
Procedure TXmlRegistry.SetRootKey(Value : String);
begin
FRootKey:=NormalizeKey(Value);
If (Length(FRootKey)>1) and (FRootKey[1]='/') then
Delete(FRootKey,1,1);
FCurrentKey:='';
FCurrentElement:=Nil;
end;
Function TXmlRegistry.DeleteKey(KeyPath : String) : Boolean;
Var
N : TDomElement;
begin
N:=FindKey(KeyPath);
Result:=(N<>Nil);
If Result then
begin
(N.ParentNode as TDomElement).RemoveChild(N);
FDirty:=True;
MaybeFlush;
end;
end;
Function TXmlRegistry.CreateKey(KeyPath : String) : Boolean;
Var
SubKey : String;
P : Integer;
Node,Node2 : TDomElement;
begin
Result:=(Length(KeyPath)>0);
If Not Result then
Exit;
KeyPath:=NormalizeKey(KeyPath);
If (FCurrentElement<>nil) then
begin
Delete(Keypath,1,1);
Node:=FCurrentElement;
end
else
begin
Delete(Keypath,1,1);
Node:=FDocument.DocumentElement;
If (FRootKey<>'') then
KeyPath:=FRootKey+KeyPath;
end;
Result:=True;
repeat
P:=Pos('/',KeyPath);
If (P<>0) then
begin
SubKey:=Copy(KeyPath,1,P-1);
Delete(KeyPath,1,P);
Node2:=FindSubKey(SubKey,Node);
Result:=(Node2<>Nil);
If Result then
Node:=Node2
else
begin
Node2:=CreateSubKey(SubKey,Node);
Result:=Node2<>Nil;
Node:=Node2
end;
end;
Until (Not Result) or (Length(KeyPath)=0);
MaybeFlush;
end;
Function TXmlRegistry.GetValueData(Name : String; Var DataType : TDataType; Var Data; Var DataSize : Integer) : Boolean;
Type
PCardinal = ^Cardinal;
Var
Node : TDomElement;
DataNode : TDomNode;
ND : Integer;
S : AnsiString;
HasData: Boolean;
IntValue: Integer;
begin
Node:=FindValueKey(Name);
Result:=Node<>Nil;
If Result then
begin
DataNode:=Node.FirstChild;
HasData:=Assigned(DataNode) and (DataNode.NodeType=TEXT_NODE);
ND:=StrToIntDef(Node[Stype],0);
Result:=ND<=Ord(High(TDataType));
If Result then
begin
DataType:=TDataType(ND);
Case DataType of
dtDWORD : begin // DataNode is required
if HasData and TryStrToInt(DataNode.NodeValue,IntValue) then
begin
PCardinal(@Data)^:=IntValue;
DataSize:=SizeOf(Cardinal);
end
else
Result:=False;
end;
dtString : begin // DataNode is optional
if HasData then
begin
S:=DataNode.NodeValue; // Convert to ansistring
DataSize:=Length(S);
if (DataSize>0) then
Move(S[1],Data,DataSize);
end
else
DataSize:=0;
end;
dtBinary : begin // DataNode is optional
if HasData then
begin
DataSize:=Length(DataNode.NodeValue);
If (DataSize>0) then
HexToBuf(DataNode.NodeValue,Data,DataSize);
end
else
DataSize:=0;
end;
end;
end;
end;
end;
Function TXmlRegistry.SetValueData(Name : String; DataType : TDataType; Const Data; DataSize : Integer) : Boolean;
Type
PCardinal = ^Cardinal;
Var
Node : TDomElement;
DataNode : TDomNode;
S : String;
begin
Node:=FindValueKey(Name);
If Node=Nil then
Node:=CreateValueKey(Name);
Result:=(Node<>Nil);
If Result then
begin
Node[SType]:=IntToStr(Ord(DataType));
DataNode:=Node.FirstChild;
Case DataType of
dtDWORD : S:=IntToStr(PCardinal(@Data)^);
dtString : SetString(S, PAnsiChar(@Data), DataSize);
dtBinary : S:=BufToHex(Data,DataSize);
else
s:='';
end;
if s <> '' then
begin
if DataNode=nil then
begin
// may happen if previous value was empty;
// XML does not handle empty textnodes.
DataNode:=FDocument.CreateTextNode(s);
Node.AppendChild(DataNode);
end
else
DataNode.NodeValue:=s;
end
else
DataNode.Free;
FDirty:=True;
MaybeFlush;
end;
end;
Function TXmlRegistry.FindSubKey (S : String; N : TDomElement) : TDomElement;
Var
Node : TDOMNode;
begin
Result:=Nil;
If N<>Nil then
begin
Node:=N.FirstChild;
While (Result=Nil) and (Assigned(Node)) do
begin
If (Node.NodeType=ELEMENT_NODE) and (Node.NodeName=SKey) then
If CompareText(TDomElement(Node)[SName],S)=0 then
Result:=TDomElement(Node);
Node:=Node.NextSibling;
end;
end;
end;
Function TXmlRegistry.CreateSubKey (S : String; N : TDomElement) : TDomElement;
begin
Result:=FDocument.CreateElement(SKey);
Result[SName]:=S;
if N<>nil then
N.AppendChild(Result);
FDirty:=True;
end;
Function TXmlRegistry.FindValueKey (S : String) : TDomElement;
Var
Node : TDOMNode;
begin
If FCurrentElement<>Nil then
begin
Node:=FCurrentElement.FirstChild;
Result:=Nil;
While (Result=Nil) and (Assigned(Node)) do
begin
If (Node.NodeType=ELEMENT_NODE) and (Node.NodeName=SValue) then
If CompareText(TDomElement(Node)[SName],S)=0 then
Result:=TDomElement(Node);
Node:=Node.NextSibling;
end;
end;
end;
Function TXmlRegistry.CreateValueKey (S : String) : TDomElement;
begin
If Assigned(FCurrentElement) then
begin
Result:=FDocument.CreateElement(SValue);
Result[SName]:=S;
// textnode to hold the value;
Result.AppendChild(FDocument.CreateTextNode(''));
FCurrentElement.AppendChild(Result);
FDirty:=True;
end
else
Result:=Nil;
end;
Procedure TXMLregistry.MaybeFlush;
begin
If FAutoFlush then
Flush;
end;
Procedure TXmlRegistry.Flush;
Var
S : TStream;
begin
If FDirty then
begin
S:=TFileStream.Create(FFileName,fmCreate);
Try
WriteXMLFile(FDocument,S);
FDirty:=False;
finally
S.Free;
end;
end;
end;
Procedure TXmlRegistry.Load;
Var
S : TStream;
begin
If Not FileExists(FFileName) then
CreateEmptyDoc
else
begin
S:=TFileStream.Create(FFileName,fmOpenReadWrite);
try
LoadFromStream(S);
finally
S.Free;
end;
end;
end;
Procedure TXmlRegistry.LoadFromStream(S : TStream);
begin
If Assigned(FDocument) then
begin
FDocument.Free;
FDocument:=Nil;
end;
ReadXMLFile(FDocument,S);
if (FDocument=Nil) then
CreateEmptyDoc;
SetRootKey('HKEY_CURRENT_USER');
FDirty:=False;
end;
Function TXmlRegistry.BufToHex(Const Buf; Len : Integer) : String;
Var
P : PByte;
S : String;
I : Integer;
begin
SetLength(Result,Len*2);
P:=@Buf;
For I:=0 to Len-1 do
begin
S:=HexStr(P[I],2);
Move(S[1],Result[I*2+1],2);
end;
end;
Function TXMLRegistry.hexToBuf(Const Str : String; Var Buf; Var Len : Integer ) : Integer;
Var
I : Integer;
P : PByte;
S : String;
B : Byte;
Code : Integer;
begin
P:=@Buf;
Len:= Length(Str) div 2;
Result:=0;
For I:=0 to Len-1 do
begin
S:='$'+Copy(Str,(I*2)+1,2);
Val(S,B,Code);
If Code<>0 then
begin
Inc(Result);
B:=0;
end;
P[I]:=B;
end;
end;
Function TXMLRegistry.DeleteValue(S : String) : Boolean;
Var
N : TDomElement;
begin
N:=FindValueKey(S);
Result:=(N<>Nil);
If Result then
begin
FCurrentElement.RemoveChild(N);
FDirty:=True;
MaybeFlush;
end;
end;
Function TXMLRegistry.GetValueSize(Name : String) : Integer;
Var
Info : TDataInfo;
begin
If GetValueInfo(Name,Info) then
Result:=Info.DataSize
else
Result:=-1;
end;
Function TXMLRegistry.GetValueType(Name : String) : TDataType;
Var
Info : TDataInfo;
begin
If GetValueInfo(Name,Info) then
Result:=Info.DataType
else
Result:=dtUnknown;
end;
Function TXMLRegistry.GetValueInfo(Name : String; Var Info : TDataInfo) : Boolean;
Var
N : TDomElement;
DN : TDomNode;
L : Integer;
begin
N:=FindValueKey(Name);
Result:=(N<>Nil);
If Result then
begin
DN:=N.FirstChild;
if Assigned(DN) and (DN.NodeType=TEXT_NODE) then
L:=TDOMText(DN).Length
else
L:=0;
With Info do
begin
DataType:=TDataType(StrToIntDef(N[SType],0));
Case DataType of
dtUnknown : DataSize:=0;
dtDword : Datasize:=SizeOf(Cardinal);
dtString : DataSize:=L;
dtBinary : DataSize:=L div 2;
end;
end;
end;
end;
Function TXMLRegistry.GetKeyInfo(Var Info : TKeyInfo) : Boolean;
Var
Node,DataNode : TDOMNode;
L : Integer;
begin
FillChar(Info,SizeOf(Info),0);
Result:=FCurrentElement<>Nil;
If Result then
With Info do
begin
If (FFileName<>'') Then
FTime:=FileAge(FFileName);
Node:=FCurrentElement.FirstChild;
While Assigned(Node) do
begin
If (Node.NodeType=ELEMENT_NODE) then
If (Node.NodeName=SKey) then
begin
Inc(SubKeys);
L:=Length(TDomElement(Node)[SName]);
If (L>SubKeyLen) then
SubKeyLen:=L;
end
else if (Node.NodeName=SValue) then
begin
Inc(Values);
L:=Length(TDomElement(Node)[SName]);
If (L>ValueLen) then
ValueLen:=L;
DataNode:=TDomElement(Node).FirstChild;
If (DataNode<>Nil) and (DataNode is TDomText) then
Case TDataType(StrToIntDef(TDomElement(Node)[SType],0)) of
dtUnknown : L:=0;
dtDWord : L:=4;
DtString : L:=Length(DataNode.NodeValue);
dtBinary : L:=Length(DataNode.NodeValue) div 2;
end
else
L:=0;
If (L>DataLen) Then
DataLen:=L;
end;
Node:=Node.NextSibling;
end;
end;
end;
Function TXMLRegistry.EnumSubKeys(List : TStrings) : Integer;
Var
Node : TDOMNode;
begin
List.Clear;
Result:=0;
If FCurrentElement<>Nil then
begin
Node:=FCurrentElement.FirstChild;
While Assigned(Node) do
begin
If (Node.NodeType=ELEMENT_NODE) and (Node.NodeName=SKey) then
List.Add(TDomElement(Node)[SName]);
Node:=Node.NextSibling;
end;
Result:=List.Count;
end;
end;
Function TXMLRegistry.EnumValues(List : TStrings) : Integer;
Var
Node : TDOMNode;
begin
List.Clear;
Result:=0;
If FCurrentElement<>Nil then
begin
Node:=FCurrentElement.FirstChild;
While Assigned(Node) do
begin
If (Node.NodeType=ELEMENT_NODE) and (Node.NodeName=SValue) then
List.Add(TDomElement(Node)[SName]);
Node:=Node.NextSibling;
end;
Result:=List.Count;
end;
end;
Function TXMLRegistry.KeyExists(KeyPath : String) : Boolean;
begin
Result:=FindKey(KeyPath)<>Nil;
end;
Function TXMLRegistry.RenameValue(Const OldName,NewName : String) : Boolean;
Var
N : TDomElement;
begin
N:=FindValueKey(OldName);
result:=n<>nil;
If (Result) then
begin
N[SName]:=NewName;
FDirty:=True;
MaybeFlush;
end;
end;
Function TXMLRegistry.FindKey (S : String) : TDomElement;
Var
SubKey : String;
P : Integer;
Node : TDomElement;
begin
Result:=Nil;
If (Length(S)=0) then
Exit;
S:=NormalizeKey(S);
If (FCurrentElement<>nil) then
begin
Delete(S,1,1);
Node:=FCurrentElement;
end
else
begin
Delete(S,1,1);
Node:=FDocument.DocumentElement;
If (FRootKey<>'') then
S:=FRootKey+S;
end;
repeat
P:=Pos('/',S);
If (P<>0) then
begin
SubKey:=Copy(S,1,P-1);
Delete(S,1,P);
Result:=FindSubKey(SubKey,Node);
Node:=Result;
end;
Until (Result=Nil) or (Length(S)=0);
end;
Function TXmlRegistry.ValueExists(ValueName : String) : Boolean;
begin
Result:=FindValueKey(ValueName)<>Nil;
end;
end.
|