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 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
|
{
*****************************************************************************
This file is part of LazUtils.
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Author: Mattias Gaertner
Abstract:
This unit defines various base classes for loading and saving of configs.
}
unit LazConfigStorage;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, typinfo, Laz_AVL_Tree,
// LazUtils
LazLoggerBase, AvgLvlTree;
type
{ TConfigStorage }
TConfigStorage = class
private
FPathStack: TStrings;
FCurrentBasePath: string;
protected
function GetFullPathValue(const APath, ADefault: String): String; virtual; abstract;
function GetFullPathValue(const APath: String; ADefault: Integer): Integer; virtual; abstract;
function GetFullPathValue(const APath: String; ADefault: Boolean): Boolean; virtual; abstract;
procedure SetFullPathValue(const APath, AValue: String); virtual; abstract;
procedure SetDeleteFullPathValue(const APath, AValue, DefValue: String); virtual; abstract;
procedure SetFullPathValue(const APath: String; AValue: Integer); virtual; abstract;
procedure SetDeleteFullPathValue(const APath: String; AValue, DefValue: Integer); virtual; abstract;
procedure SetFullPathValue(const APath: String; AValue: Boolean); virtual; abstract;
procedure SetDeleteFullPathValue(const APath: String; AValue, DefValue: Boolean); virtual; abstract;
procedure DeleteFullPath(const APath: string); virtual; abstract;
procedure DeleteFullPathValue(const APath: string); virtual; abstract;
protected
procedure WriteProperty(Path: String; Instance: TPersistent;
PropInfo: Pointer; DefInstance: TPersistent = nil;
OnlyProperty: String= '');
procedure ReadProperty(Path: String; Instance: TPersistent;
PropInfo: Pointer; DefInstance: TPersistent = nil;
OnlyProperty: String= '');
public
constructor Create(const {%H-}Filename: string; {%H-}LoadFromDisk: Boolean); virtual;
destructor Destroy; override;
procedure Clear; virtual; abstract;
function GetValue(const APath, ADefault: String): String;
function GetValue(const APath: String; ADefault: Integer): Integer;
function GetValue(const APath: String; ADefault: Boolean): Boolean;
procedure GetValue(const APath: String; out ARect: TRect;
const ADefault: TRect);
procedure GetValue(const APath: String; out APoint: TPoint;
const ADefault: TPoint);
procedure GetValue(const APath: String; const List: TStrings);
procedure SetValue(const APath, AValue: String);
procedure SetDeleteValue(const APath, AValue, DefValue: String);
procedure SetValue(const APath: String; AValue: Integer);
procedure SetDeleteValue(const APath: String; AValue, DefValue: Integer);
procedure SetValue(const APath: String; AValue: Boolean);
procedure SetDeleteValue(const APath: String; AValue, DefValue: Boolean);
procedure SetValue(const APath: String; const AValue: TRect);
procedure SetDeleteValue(const APath: String; const AValue, DefValue: TRect);
procedure SetValue(const APath: String; const AValue: TPoint);
procedure SetDeleteValue(const APath: String; const AValue, DefValue: TPoint);
procedure SetValue(const APath: String; const AValue: TStrings);
procedure DeletePath(const APath: string);
procedure DeleteValue(const APath: string);
property CurrentBasePath: string read FCurrentBasePath;
function ExtendPath(const APath: string): string;
procedure AppendBasePath(const Path: string);
procedure UndoAppendBasePath;
procedure WriteToDisk; virtual; abstract;
function GetFilename: string; virtual; abstract;
procedure WriteObject(Path: String; Obj: TPersistent;
DefObject: TPersistent= nil; OnlyProperty: String= '');
procedure ReadObject(Path: String; Obj: TPersistent;
DefObject: TPersistent= nil; OnlyProperty: String= '');
end;
TConfigStorageClass = class of TConfigStorage;
{ TConfigMemStorageNode }
TConfigMemStorageNode = class
public
Name: string;
Value: string;
Parent: TConfigMemStorageNode;
Children: TAvlTree; // tree of TConfigMemStorageNode
procedure ClearChilds;
constructor Create(AParent: TConfigMemStorageNode; const AName: string);
destructor Destroy; override;
end;
TConfigMemStorageModification = (cmsmSet, cmsmGet, cmsmDelete, cmsmDeleteValue);
const
ConfigMemStorageFormatVersion = 2; // change this when format changes
type
{ TConfigMemStorage }
TConfigMemStorage = class(TConfigStorage)
private
procedure CreateRoot;
procedure CreateChilds(Node: TConfigMemStorageNode);
procedure Modify(const APath: string; Mode: TConfigMemStorageModification;
var AValue: string);
protected
procedure DeleteFullPath(const APath: string); override;
procedure DeleteFullPathValue(const APath: string); override;
function GetFullPathValue(const APath, ADefault: String): String; override;
function GetFullPathValue(const APath: String; ADefault: Boolean): Boolean;
override;
function GetFullPathValue(const APath: String; ADefault: Integer): Integer;
override;
procedure SetDeleteFullPathValue(const APath, AValue, DefValue: String);
override;
procedure SetDeleteFullPathValue(const APath: String; AValue, DefValue:
Boolean); override;
procedure SetDeleteFullPathValue(const APath: String; AValue, DefValue:
Integer); override;
procedure SetFullPathValue(const APath, AValue: String); override;
procedure SetFullPathValue(const APath: String; AValue: Boolean); override;
procedure SetFullPathValue(const APath: String; AValue: Integer); override;
public
Root: TConfigMemStorageNode;
function GetFilename: string; override;
procedure WriteToDisk; override;
destructor Destroy; override;
procedure Clear; override;
procedure SaveToConfig(Config: TConfigStorage; const APath: string);
procedure LoadFromConfig(Config: TConfigStorage; const APath: string);
procedure WriteDebugReport;
end;
procedure LoadStringToStringTree(Config: TConfigStorage; const Path: string;
Tree: TStringToStringTree);
procedure SaveStringToStringTree(Config: TConfigStorage; const Path: string;
Tree: TStringToStringTree);
function CompareConfigMemStorageNames(p1, p2: PChar): integer;
function CompareConfigMemStorageNodes(Node1, Node2: Pointer): integer;
function ComparePCharWithConfigMemStorageNode(aPChar, ANode: Pointer): integer;
implementation
procedure LoadStringToStringTree(Config: TConfigStorage; const Path: string;
Tree: TStringToStringTree);
var
Cnt: LongInt;
SubPath: String;
CurName: String;
CurValue: String;
i: Integer;
begin
Tree.Clear;
Cnt:=Config.GetValue(Path+'Count',0);
for i:=0 to Cnt-1 do begin
SubPath:=Path+'Item'+IntToStr(i)+'/';
CurName:=Config.GetValue(SubPath+'Name','');
CurValue:=Config.GetValue(SubPath+'Value','');
Tree.Values[CurName]:=CurValue;
end;
end;
procedure SaveStringToStringTree(Config: TConfigStorage; const Path: string;
Tree: TStringToStringTree);
var
Node: TAvlTreeNode;
Item: PStringToStringItem;
i: Integer;
SubPath: String;
begin
Config.SetDeleteValue(Path+'Count',Tree.Tree.Count,0);
Node:=Tree.Tree.FindLowest;
i:=0;
while Node<>nil do begin
Item:=PStringToStringItem(Node.Data);
SubPath:=Path+'Item'+IntToStr(i)+'/';
Config.SetDeleteValue(SubPath+'Name',Item^.Name,'');
Config.SetDeleteValue(SubPath+'Value',Item^.Value,'');
Node:=Tree.Tree.FindSuccessor(Node);
inc(i);
end;
end;
function CompareConfigMemStorageNames(p1, p2: PChar): integer;
// compare strings till / or #0
begin
if (p1=nil) then begin
if (p2=nil) or (p2^ in ['/',#0]) then begin
// both empty
Result:=0;
end else begin
// p1 shorter
Result:=-1;
end;
end else begin
if p2=nil then begin
// p2 shorter
Result:=1;
end else begin
Result:=0;
repeat
if p1^ in ['/',#0] then begin
if p2^ in ['/',#0] then begin
// same
exit(0);
end else begin
// p1 shorter
exit(-1);
end;
end else begin
if p2^ in ['/',#0] then begin
// p2 shorter
exit(1);
end else if p1^=p2^ then begin
// continue
end else begin
// differ
exit(ord(p1^)-ord(p2^));
end;
end;
inc(p1);
inc(p2);
until false;
end;
end;
end;
function CompareConfigMemStorageNodes(Node1, Node2: Pointer): integer;
var
CfgNode1: TConfigMemStorageNode absolute Node1;
CfgNode2: TConfigMemStorageNode absolute Node2;
begin
Result:=CompareConfigMemStorageNames(PChar(CfgNode1.Name),PChar(CfgNode2.Name));
end;
function ComparePCharWithConfigMemStorageNode(aPChar, ANode: Pointer): integer;
begin
Result:=CompareConfigMemStorageNames(PChar(aPChar),
PChar(TConfigMemStorageNode(ANode).Name));
end;
{ TConfigStorage }
procedure TConfigStorage.WriteProperty(Path: String; Instance: TPersistent; PropInfo: Pointer;
DefInstance: TPersistent; OnlyProperty: String);
// based on FPC TWriter
// path is already extende
type
tset = set of 0..31;
var
i: Integer;
PropType: PTypeInfo;
Value, DefValue: LongInt;
Ident: String;
IntToIdentFn: TIntToIdent;
SetType: Pointer;
FloatValue, DefFloatValue: Extended;
//WStrValue, WDefStrValue: WideString;
StrValue, DefStrValue: String;
//Int64Value, DefInt64Value: Int64;
BoolValue, DefBoolValue: boolean;
begin
// do not stream properties without getter and setter
if not (Assigned(PPropInfo(PropInfo)^.GetProc) and
Assigned(PPropInfo(PropInfo)^.SetProc)) then
exit;
PropType := PPropInfo(PropInfo)^.PropType;
Path := Path + PPropInfo(PropInfo)^.Name;
if (OnlyProperty <> '') and (OnlyProperty <> PPropInfo(PropInfo)^.Name) then
exit;
case PropType^.Kind of
tkInteger, tkChar, tkEnumeration, tkSet, tkWChar:
begin
Value := GetOrdProp(Instance, PropInfo);
if (DefInstance <> nil) then
DefValue := GetOrdProp(DefInstance, PropInfo);
if (DefInstance <> nil) and (Value = DefValue) then
DeleteValue(Path)
else begin
case PropType^.Kind of
tkInteger:
begin // Check if this integer has a string identifier
IntToIdentFn := FindIntToIdent(PPropInfo(PropInfo)^.PropType);
if Assigned(IntToIdentFn) and IntToIdentFn(Value, Ident{%H-}) then
SetValue(Path, Ident) // Integer can be written a human-readable identifier
else
SetValue(Path, Value); // Integer has to be written just as number
end;
tkChar:
SetValue(Path, Chr(Value));
tkWChar:
SetValue(Path, Value);
tkSet:
begin
SetType := GetTypeData(PropType)^.CompType;
Ident := '';
for i := 0 to 31 do
if (i in tset(Value)) then begin
if Ident <> '' then Ident := Ident + ',';
Ident := Ident + GetEnumName(PTypeInfo(SetType), i);
end;
SetValue(Path, Ident);
end;
tkEnumeration:
SetValue(Path, GetEnumName(PropType, Value));
end;
end;
end;
tkFloat:
begin
FloatValue := GetFloatProp(Instance, PropInfo);
if (DefInstance <> nil) then
DefFloatValue := GetFloatProp(DefInstance, PropInfo);
if (DefInstance <> nil) and (DefFloatValue = FloatValue) then
DeleteValue(Path)
else
SetValue(Path, FloatToStr(FloatValue));
end;
tkSString, tkLString, tkAString:
begin
StrValue := GetStrProp(Instance, PropInfo);
if (DefInstance <> nil) then
DefStrValue := GetStrProp(DefInstance, PropInfo);
if (DefInstance <> nil) and (DefStrValue = StrValue) then
DeleteValue(Path)
else
SetValue(Path, StrValue);
end;
(* tkWString:
begin
WStrValue := GetWideStrProp(Instance, PropInfo);
if (DefInstance <> nil) then
WDefStrValue := GetWideStrProp(DefInstance, PropInfo);
if (DefInstance <> nil) and (WDefStrValue = WStrValue) then
DeleteValue(Path)
else
SetValue(Path, WStrValue);
end;*)
(* tkInt64, tkQWord:
begin
Int64Value := GetInt64Prop(Instance, PropInfo);
if (DefInstance <> nil) then
DefInt64Value := GetInt64Prop(DefInstance, PropInfo)
if (DefInstance <> nil) and (Int64Value = DefInt64Value) then
DeleteValue(Path, Path)
else
SetValue(StrValue);
end;*)
tkBool:
begin
BoolValue := GetOrdProp(Instance, PropInfo)<>0;
if (DefInstance <> nil) then
DefBoolValue := GetOrdProp(DefInstance, PropInfo)<>0;
if (DefInstance <> nil) and (BoolValue = DefBoolValue) then
DeleteValue(Path)
else
SetValue(Path, BoolValue);
end;
end;
end;
procedure TConfigStorage.ReadProperty(Path: String; Instance: TPersistent; PropInfo: Pointer;
DefInstance: TPersistent; OnlyProperty: String);
type
tset = set of 0..31;
var
i, j: Integer;
PropType: PTypeInfo;
Value, DefValue: LongInt;
Ident, s: String;
IdentToIntFn: TIdentToInt;
SetType: Pointer;
FloatValue, DefFloatValue: Extended;
//WStrValue, WDefStrValue: WideString;
StrValue, DefStrValue: String;
//Int64Value, DefInt64Value: Int64;
BoolValue, DefBoolValue: boolean;
begin
// do not stream properties without getter and setter
if not (Assigned(PPropInfo(PropInfo)^.GetProc) and
Assigned(PPropInfo(PropInfo)^.SetProc)) then
exit;
PropType := PPropInfo(PropInfo)^.PropType;
Path := Path + PPropInfo(PropInfo)^.Name;
if (OnlyProperty <> '') and (OnlyProperty <> PPropInfo(PropInfo)^.Name) then
exit;
if DefInstance = nil then
DefInstance := Instance;
case PropType^.Kind of
tkInteger, tkChar, tkEnumeration, tkSet, tkWChar:
begin
DefValue := GetOrdProp(DefInstance, PropInfo);
case PropType^.Kind of
tkInteger:
begin // Check if this integer has a string identifier
Ident := GetValue(Path, IntToStr(DefValue));
IdentToIntFn := FindIdentToInt(PPropInfo(PropInfo)^.PropType);
if TryStrToInt(Ident, Value) then
SetOrdProp(Instance, PropInfo, Value)
else if Assigned(IdentToIntFn) and IdentToIntFn(Ident, Value) then
SetOrdProp(Instance, PropInfo, Value)
else
SetOrdProp(Instance, PropInfo, DefValue)
end;
tkChar:
begin
Ident := GetValue(Path, chr(DefValue));
if Length(Ident) > 0 then
SetOrdProp(Instance, PropInfo, ord(Ident[1]))
else
SetOrdProp(Instance, PropInfo, DefValue);
end;
tkWChar:
SetOrdProp(Instance, PropInfo, GetValue(Path, DefValue));
tkSet:
begin
SetType := GetTypeData(PropType)^.CompType;
Ident := GetValue(Path, '-');
If Ident = '-' then
Value := DefValue
else begin
Value := 0;
while length(Ident) > 0 do begin
i := Pos(',', Ident);
if i < 1 then
i := length(Ident) + 1;
s := copy(Ident, 1, i-1);
Ident := copy(Ident, i+1, length(Ident));
j := GetEnumValue(PTypeInfo(SetType), s);
if j <> -1 then
include(tset(Value), j)
else Begin
Value := DefValue;
break;
end;
end;
end;
SetOrdProp(Instance, PropInfo, Value);
end;
tkEnumeration:
begin
Ident := GetValue(Path, '-');
If Ident = '-' then
Value := DefValue
else
Value := GetEnumValue(PropType, Ident);
if Value <> -1 then
SetOrdProp(Instance, PropInfo, Value)
else
SetOrdProp(Instance, PropInfo, DefValue);
end;
end;
end;
tkFloat:
begin
DefFloatValue := GetFloatProp(DefInstance, PropInfo);
Ident := GetValue(Path, FloatToStr(DefFloatValue));
if TryStrToFloat(Ident, FloatValue) then
SetFloatProp(Instance, PropInfo, FloatValue)
else
SetFloatProp(Instance, PropInfo, DefFloatValue)
end;
tkSString, tkLString, tkAString:
begin
DefStrValue := GetStrProp(DefInstance, PropInfo);
StrValue := GetValue(Path, DefStrValue);
SetStrProp(Instance, PropInfo, StrValue)
end;
(* tkWString:
begin
end;*)
(* tkInt64, tkQWord:
begin
end;*)
tkBool:
begin
DefBoolValue := GetOrdProp(DefInstance, PropInfo) <> 0;
BoolValue := GetValue(Path, DefBoolValue);
SetOrdProp(Instance, PropInfo, ord(BoolValue));
end;
end;
end;
constructor TConfigStorage.Create(const Filename: string; LoadFromDisk: Boolean
);
begin
end;
destructor TConfigStorage.Destroy;
begin
FPathStack.Free;
inherited Destroy;
end;
function TConfigStorage.GetValue(const APath, ADefault: String): String;
begin
Result:=GetFullPathValue(ExtendPath(APath),ADefault);
end;
function TConfigStorage.GetValue(const APath: String; ADefault: Integer
): Integer;
begin
Result:=GetFullPathValue(ExtendPath(APath),ADefault);
end;
function TConfigStorage.GetValue(const APath: String; ADefault: Boolean
): Boolean;
begin
Result:=GetFullPathValue(ExtendPath(APath),ADefault);
end;
procedure TConfigStorage.GetValue(const APath: String; out ARect: TRect;
const ADefault: TRect);
begin
ARect.Left:=GetValue(APath+'Left',ADefault.Left);
ARect.Top:=GetValue(APath+'Top',ADefault.Top);
ARect.Right:=GetValue(APath+'Right',ADefault.Right);
ARect.Bottom:=GetValue(APath+'Bottom',ADefault.Bottom);
end;
procedure TConfigStorage.GetValue(const APath: String; out APoint: TPoint;
const ADefault: TPoint);
begin
APoint.X:=GetValue(APath+'X',ADefault.X);
APoint.Y:=GetValue(APath+'Y',ADefault.Y);
end;
procedure TConfigStorage.GetValue(const APath: String; const List: TStrings);
var
NewCount: LongInt;
i: Integer;
NewLine: String;
begin
NewCount:=GetValue(APath+'Count',0);
for i:=0 to NewCount-1 do begin
NewLine:=GetValue(APath+'Item'+IntToStr(i+1)+'/Value','');
if List.Count>i then
List[i]:=NewLine
else
List.Add(NewLine);
end;
while List.Count>NewCount do List.Delete(List.Count-1);
end;
procedure TConfigStorage.SetValue(const APath, AValue: String);
begin
SetFullPathValue(ExtendPath(APath),AValue);
end;
procedure TConfigStorage.SetDeleteValue(const APath, AValue, DefValue: String);
begin
SetDeleteFullPathValue(ExtendPath(APath),AValue,DefValue);
end;
procedure TConfigStorage.SetValue(const APath: String; AValue: Integer);
begin
SetFullPathValue(ExtendPath(APath),AValue);
end;
procedure TConfigStorage.SetDeleteValue(const APath: String; AValue,
DefValue: Integer);
begin
SetDeleteFullPathValue(ExtendPath(APath),AValue,DefValue);
end;
procedure TConfigStorage.SetValue(const APath: String; AValue: Boolean);
begin
SetFullPathValue(ExtendPath(APath),AValue);
end;
procedure TConfigStorage.SetDeleteValue(const APath: String; AValue,
DefValue: Boolean);
begin
SetDeleteFullPathValue(ExtendPath(APath),AValue,DefValue);
end;
procedure TConfigStorage.SetValue(const APath: String; const AValue: TRect);
begin
SetValue(APath+'Left',AValue.Left);
SetValue(APath+'Top',AValue.Top);
SetValue(APath+'Right',AValue.Right);
SetValue(APath+'Bottom',AValue.Bottom);
end;
procedure TConfigStorage.SetDeleteValue(const APath: String; const AValue,
DefValue: TRect);
begin
SetDeleteValue(APath+'Left',AValue.Left,DefValue.Left);
SetDeleteValue(APath+'Top',AValue.Top,DefValue.Top);
SetDeleteValue(APath+'Right',AValue.Right,DefValue.Right);
SetDeleteValue(APath+'Bottom',AValue.Bottom,DefValue.Bottom);
end;
procedure TConfigStorage.SetValue(const APath: String; const AValue: TPoint);
begin
SetValue(APath+'X',AValue.X);
SetValue(APath+'Y',AValue.Y);
end;
procedure TConfigStorage.SetDeleteValue(const APath: String; const AValue,
DefValue: TPoint);
begin
SetDeleteValue(APath+'X',AValue.X,DefValue.X);
SetDeleteValue(APath+'Y',AValue.Y,DefValue.Y);
end;
procedure TConfigStorage.SetValue(const APath: String; const AValue: TStrings);
var
i: Integer;
begin
SetDeleteValue(APath+'Count',AValue.Count,0);
for i:=0 to AValue.Count-1 do
SetDeleteValue(APath+'Item'+IntToStr(i+1)+'/Value',AValue[i],'');
end;
procedure TConfigStorage.DeletePath(const APath: string);
begin
DeleteFullPath(ExtendPath(APath));
end;
procedure TConfigStorage.DeleteValue(const APath: string);
begin
DeleteFullPathValue(ExtendPath(APath));
end;
function TConfigStorage.ExtendPath(const APath: string): string;
begin
Result:=FCurrentBasePath+APath;
end;
procedure TConfigStorage.AppendBasePath(const Path: string);
begin
if FPathStack=nil then FPathStack:=TStringList.Create;
FPathStack.Add(FCurrentBasePath);
FCurrentBasePath:=FCurrentBasePath+Path;
if (FCurrentBasePath<>'')
and (FCurrentBasePath[length(FCurrentBasePath)]<>'/') then
FCurrentBasePath:=FCurrentBasePath+'/';
end;
procedure TConfigStorage.UndoAppendBasePath;
begin
if (FPathStack=nil) or (FPathStack.Count=0) then
raise Exception.Create('TConfigStorage.UndoAppendBasePath');
FCurrentBasePath:=FPathStack[FPathStack.Count-1];
FPathStack.Delete(FPathStack.Count-1);
end;
procedure TConfigStorage.WriteObject(Path: String; Obj: TPersistent; DefObject: TPersistent;
OnlyProperty: String);
var
PropCount,i : integer;
PropList : PPropList;
begin
// Do Not extebd the path, individual SetValue will be called, and Extend it
//Path := ExtendPath(Path);
PropCount:=GetPropList(Obj,PropList);
if PropCount>0 then begin
try
for i := 0 to PropCount-1 do
WriteProperty(Path, Obj, PropList^[i], DefObject, OnlyProperty);
finally
Freemem(PropList);
end;
end;
end;
procedure TConfigStorage.ReadObject(Path: String; Obj: TPersistent; DefObject: TPersistent;
OnlyProperty: String);
var
PropCount,i : integer;
PropList : PPropList;
begin
// Do Not extebd the path, individual SetValue will be called, and Extend it
//Path := ExtendPath(Path);
PropCount:=GetPropList(Obj,PropList);
if PropCount>0 then begin
try
for i := 0 to PropCount-1 do
ReadProperty(Path, Obj, PropList^[i], DefObject, OnlyProperty);
finally
Freemem(PropList);
end;
end;
end;
{ TConfigMemStorage }
procedure TConfigMemStorage.CreateRoot;
begin
Root:=TConfigMemStorageNode.Create(nil,'');
end;
procedure TConfigMemStorage.CreateChilds(Node: TConfigMemStorageNode);
begin
Node.Children:=TAvlTree.Create(@CompareConfigMemStorageNodes);
end;
procedure TConfigMemStorage.Modify(const APath: string;
Mode: TConfigMemStorageModification; var AValue: string);
var
Node: TConfigMemStorageNode;
p: PChar;
StartPos: PChar;
ChildNode: TAvlTreeNode;
Child: TConfigMemStorageNode;
NewName: string;
begin
//DebugLn(['TConfigMemStorage.Modify APath="',APath,'" Mode=',ord(Mode),' AValue="',AValue,'"']);
p:=PChar(APath);
if p=nil then begin
if Root<>nil then begin
if Mode in [cmsmDelete,cmsmDeleteValue] then
Root.Value:='';
if Mode=cmsmDelete then
Root.ClearChilds;
end;
end else begin
if Root=nil then begin
if Mode in [cmsmDelete,cmsmDeleteValue] then exit;
CreateRoot;
end;
Node:=Root;
repeat
StartPos:=p;
while (not (p^ in ['/',#0])) do inc(p);
//DebugLn(['TConfigMemStorage.Modify Node="',Node.Name,'" StartPos="',StartPos,'"']);
// child node
if Node.Children=nil then begin
if Mode in [cmsmDelete,cmsmDeleteValue] then exit;
CreateChilds(Node);
end;
ChildNode:=Node.Children.FindKey(StartPos,@ComparePCharWithConfigMemStorageNode);
if ChildNode=nil then begin
if Mode in [cmsmDelete,cmsmDeleteValue] then exit;
NewName:='';
SetLength(NewName,p-StartPos);
if NewName<>'' then
System.Move(StartPos^,NewName[1],p-StartPos);
//DebugLn(['TConfigMemStorage.Modify Adding "',NewName,'"']);
Child:=TConfigMemStorageNode.Create(Node,NewName);
Node.Children.Add(Child);
end else
Child:=TConfigMemStorageNode(ChildNode.Data);
Node:=Child;
if p^='/' then begin
// next level
while (p^='/') do inc(p);
end else begin
// end of path
case Mode of
cmsmSet: Node.Value:=AValue;
cmsmGet: AValue:=Node.Value;
cmsmDelete,cmsmDeleteValue:
begin
Node.Value:='';
if Mode=cmsmDelete then
Node.ClearChilds;
while (Node<>nil) and ((Node.Children=nil) or (Node.Children.Count=0))
do begin
Child:=Node;
Node:=Node.Parent;
if Root=Child then Root:=nil;
Child.Free;
end;
end;
end;
exit;
end;
until false;
end;
end;
procedure TConfigMemStorage.DeleteFullPath(const APath: string);
var
V: string;
begin
V:='';
Modify(APath,cmsmDelete,V);
end;
procedure TConfigMemStorage.DeleteFullPathValue(const APath: string);
var
V: string;
begin
V:='';
Modify(APath,cmsmDeleteValue,V);
end;
function TConfigMemStorage.GetFullPathValue(const APath, ADefault: String
): String;
begin
Result:=ADefault;
Modify(APath,cmsmGet,Result);
end;
function TConfigMemStorage.GetFullPathValue(const APath: String; ADefault:
Boolean): Boolean;
var
s: string;
begin
if ADefault then
s := 'True'
else
s := 'False';
s := GetFullPathValue(APath, s);
if CompareText(s,'TRUE')=0 then
Result := True
else if CompareText(s,'FALSE')=0 then
Result := False
else
Result := ADefault;
end;
function TConfigMemStorage.GetFullPathValue(const APath: String; ADefault:
Integer): Integer;
begin
Result := StrToIntDef(GetFullPathValue(APath, IntToStr(ADefault)),ADefault);
end;
procedure TConfigMemStorage.SetDeleteFullPathValue(const APath, AValue, DefValue
: String);
begin
if AValue=DefValue then
DeleteFullPathValue(APath)
else
SetFullPathValue(APath,AValue);
end;
procedure TConfigMemStorage.SetDeleteFullPathValue(const APath: String; AValue,
DefValue: Boolean);
begin
if AValue=DefValue then
DeleteFullPath(APath)
else
SetFullPathValue(APath,AValue);
end;
procedure TConfigMemStorage.SetDeleteFullPathValue(const APath: String; AValue,
DefValue: Integer);
begin
if AValue=DefValue then
DeleteFullPath(APath)
else
SetFullPathValue(APath,AValue);
end;
procedure TConfigMemStorage.SetFullPathValue(const APath, AValue: String);
var
V: String;
begin
V:=AValue;
Modify(APath,cmsmSet,V);
end;
procedure TConfigMemStorage.SetFullPathValue(const APath: String; AValue:
Boolean);
begin
if AValue then
SetFullPathValue(APath, 'True')
else
SetFullPathValue(APath, 'False');
end;
procedure TConfigMemStorage.SetFullPathValue(const APath: String; AValue:
Integer);
begin
SetFullPathValue(APath,IntToStr(AValue));
end;
function TConfigMemStorage.GetFilename: string;
begin
Result:='';
end;
procedure TConfigMemStorage.WriteToDisk;
begin
raise Exception.Create('TConfigMemStorage.WriteToDisk invalid operation');
end;
destructor TConfigMemStorage.Destroy;
begin
FreeAndNil(Root);
inherited Destroy;
end;
procedure TConfigMemStorage.Clear;
begin
FreeAndNil(Root);
end;
procedure TConfigMemStorage.SaveToConfig(Config: TConfigStorage;
const APath: string);
procedure Save(Node: TConfigMemStorageNode; SubPath: string);
var
ChildNode: TAvlTreeNode;
Child: TConfigMemStorageNode;
Names: String;
begin
if Node=nil then exit;
if (Node<>Root) then
SubPath:=SubPath+'_'+Node.Name+'/';
Config.SetDeleteValue(SubPath+'Value',Node.Value,'');
Names:='';
if Node.Children<>nil then begin
ChildNode:=Node.Children.FindLowest;
while ChildNode<>nil do begin
Child:=TConfigMemStorageNode(ChildNode.Data);
if Names<>'' then Names:=Names+'/';
Names:=Names+Child.Name;
Save(Child,SubPath);
ChildNode:=Node.Children.FindSuccessor(ChildNode);
end;
end;
Config.SetDeleteValue(SubPath+'Items',Names,'');
end;
begin
Save(Root,APath);
if (Root<>nil) and ((Root.Value<>'') or (Root.Children<>nil)) then
Config.SetValue(APath+'Version',ConfigMemStorageFormatVersion);
end;
procedure TConfigMemStorage.LoadFromConfig(Config: TConfigStorage;
const APath: string);
var
StorageVersion: LongInt;
procedure Load(Node: TConfigMemStorageNode; SubPath: string);
var
ChildNames: string;
ChildName: string;
p: PChar;
StartPos: PChar;
Child: TConfigMemStorageNode;
begin
if Node=nil then exit;
if (Node<>Root) then
SubPath:=SubPath+'_'+Node.Name+'/';
Node.Value:=Config.GetValue(SubPath+'Value','');
if StorageVersion<2 then
ChildNames:=Config.GetValue(SubPath+'Childs','')
else
ChildNames:=Config.GetValue(SubPath+'Items','');
//DebugLn(['Load SubPath="',SubPath,'" Value="',Node.Value,'" ChildNames="',ChildNames,'"']);
if ChildNames<>'' then begin
p:=PChar(ChildNames);
repeat
StartPos:=p;
while not (p^ in ['/',#0]) do inc(p);
ChildName:='';
SetLength(ChildName,p-StartPos);
if ChildName<>'' then
System.Move(StartPos^,ChildName[1],p-StartPos);
Child:=TConfigMemStorageNode.Create(Node,ChildName);
if Node.Children=nil then
CreateChilds(Node);
Node.Children.Add(Child);
Load(Child,SubPath);
if p^=#0 then break;
inc(p);
until false;
end;
end;
begin
//DebugLn(['TConfigMemStorage.LoadFromConfig ']);
Clear;
if Root=nil then
CreateRoot;
StorageVersion:=Config.GetValue(APath+'Version',0);
//debugln(['TConfigMemStorage.LoadFromConfig ',APath,' Version=',StorageVersion]);
Load(Root,APath);
end;
procedure TConfigMemStorage.WriteDebugReport;
procedure w(Node: TConfigMemStorageNode; Prefix: string);
var
AVLNode: TAvlTreeNode;
begin
if Node=nil then exit;
DebugLn(['TConfigMemStorage.WriteDebugReport ',Prefix,'Name="',Node.Name,'" Value="',Node.Value,'"']);
if Node.Children<>nil then begin
AVLNode:=Node.Children.FindLowest;
while AVLNode<>nil do begin
w(TConfigMemStorageNode(AVLNode.Data),Prefix+' ');
AVLNode:=Node.Children.FindSuccessor(AVLNode);
end;
end;
end;
begin
DebugLn(['TConfigMemStorage.WriteDebugReport ']);
w(Root,'');
end;
{ TConfigMemStorageNode }
procedure TConfigMemStorageNode.ClearChilds;
var
OldChilds: TAvlTree;
begin
if Children<>nil then begin
OldChilds:=Children;
Children:=nil;
OldChilds.FreeAndClear;
OldChilds.Free;
end;
end;
constructor TConfigMemStorageNode.Create(AParent: TConfigMemStorageNode;
const AName: string);
begin
Parent:=AParent;
Name:=AName;
end;
destructor TConfigMemStorageNode.Destroy;
begin
ClearChilds;
if (Parent<>nil) and (Parent.Children<>nil) then
Parent.Children.Remove(Self);
inherited Destroy;
end;
end.
|