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
|
{*******************************************************}
{ }
{ Delphi VCL Extensions (RX) }
{ }
{ Copyright (c) 1996 AO ROSNO }
{ Copyright (c) 1997, 1998 Master-Bank }
{ }
{*******************************************************}
{$mode objfpc}
{$H+}
unit StrHolder;
interface
uses SysUtils, Classes, LResources;
type
{$ifdef usevariant}
TMacroData = Variant;
{$else}
TMacroData = AnsiString;
{$endif}
{ TMacro }
TMacros = class;
TMacroTextEvent = procedure(Sender: TObject; Data: TMacroData;
var Text: string) of object;
TMacro = class(TCollectionItem)
private
FName: string;
FData: TMacroData;
FOnGetText: TMacroTextEvent;
function IsMacroStored: Boolean;
function GetText: string;
function GetMacros: TMacros;
protected
function GetDisplayName: string; override;
procedure SetDisplayName(const Value: string); override;
procedure GetMacroText(var AText: string);
function GetAsTMacroData: TMacroData;
procedure SetAsTMacroData(Value: TMacroData);
public
constructor Create(ACollection: TCollection); override;
procedure Assign(Source: TPersistent); override;
procedure Clear;
function IsEqual(Value: TMacro): Boolean;
property Macros: TMacros read GetMacros;
property Text: string read GetText;
published
property Name: string read FName write SetDisplayName;
property Value: TMacroData read GetAsTMacroData write SetAsTMacroData stored IsMacroStored;
property OnGetText: TMacroTextEvent read FOnGetText write FOnGetText;
end;
{ TMacros }
TMacros = class(TOwnedCollection)
private
function GetMacroValue(const MacroName: string): TMacroData;
procedure SetMacroValue(const MacroName: string;
const Value: TMacroData);
function GetItem(Index: Integer): TMacro;
procedure SetItem(Index: Integer; Value: TMacro);
public
constructor Create(AOwner: TPersistent);
procedure AssignValues(Value: TMacros);
procedure AddMacro(Value: TMacro);
procedure RemoveMacro(Value: TMacro);
function CreateMacro(const MacroName: string): TMacro;
procedure GetMacroList(List: TList; const MacroNames: string);
function IndexOf(const AName: string): Integer;
function IsEqual(Value: TMacros): Boolean;
function ParseString(const Value: string; DoCreate: Boolean;
SpecialChar: Char): string;
function MacroByName(const Value: string): TMacro;
function FindMacro(const Value: string): TMacro;
property Items[Index: Integer]: TMacro read GetItem write SetItem; default;
property MacroValues[const MacroName: string]: TMacroData read GetMacroValue write SetMacroValue;
end;
{ TStrHolder }
TStrHolder = class(TComponent)
private
FStrings: TStrings;
FXorKey: string;
FMacros: TMacros;
FMacroChar: Char;
FOnExpandMacros: TNotifyEvent;
FOnChange: TNotifyEvent;
FOnChanging: TNotifyEvent;
function GetDuplicates: TDuplicates;
procedure SetDuplicates(Value: TDuplicates);
function GetSorted: Boolean;
procedure SetSorted(Value: Boolean);
procedure SetStrings(Value: TStrings);
procedure StringsChanged(Sender: TObject);
procedure StringsChanging(Sender: TObject);
procedure ReadStrings(Reader: TReader);
procedure WriteStrings(Writer: TWriter);
function GetCommaText: string;
procedure SetCommaText(const Value: string);
function GetCapacity: Integer;
procedure SetCapacity(NewCapacity: Integer);
procedure SetMacros(Value: TMacros);
procedure RecreateMacros;
procedure SetMacroChar(Value: Char);
protected
procedure AssignTo(Dest: TPersistent); override;
procedure DefineProperties(Filer: TFiler); override;
procedure Changed; dynamic;
procedure Changing; dynamic;
procedure BeforeExpandMacros; dynamic;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Clear;
function MacroCount: Integer;
function MacroByName(const MacroName: string): TMacro;
function ExpandMacros: string;
property CommaText: string read GetCommaText write SetCommaText;
published
property Capacity: Integer read GetCapacity write SetCapacity default 0;
property MacroChar: Char read FMacroChar write SetMacroChar default '%';
property Macros: TMacros read FMacros write SetMacros;
property OnExpandMacros: TNotifyEvent read FOnExpandMacros write FOnExpandMacros;
property Duplicates: TDuplicates read GetDuplicates write SetDuplicates
default dupIgnore;
property KeyString: string read FXorKey write FXorKey stored False;
property Sorted: Boolean read GetSorted write SetSorted default False;
property Strings: TStrings read FStrings write SetStrings stored False;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
end;
Procedure Register;
implementation
uses
RTLConsts;
Procedure Register;
begin
RegisterComponents('Misc',[TStrHolder])
end;
function XorEncode(const Key, Source: string): string;
var
I: Integer;
C: Byte;
begin
Result := '';
for I := 1 to Length(Source) do begin
if Length(Key) > 0 then
C := Byte(Key[1 + ((I - 1) mod Length(Key))]) xor Byte(Source[I])
else
C := Byte(Source[I]);
Result := Result + AnsiLowerCase(IntToHex(C, 2));
end;
end;
function XorDecode(const Key, Source: string): string;
var
I: Integer;
C: Char;
begin
Result := '';
for I := 0 to Length(Source) div 2 - 1 do begin
C := Chr(StrToIntDef('$' + Copy(Source, (I * 2) + 1, 2), Ord(' ')));
if Length(Key) > 0 then
C := Chr(Byte(Key[1 + (I mod Length(Key))]) xor Byte(C));
Result := Result + C;
end;
end;
function ExtractName(const Items: string; var Pos: Integer): string;
var
I: Integer;
begin
I := Pos;
while (I <= Length(Items)) and (Items[I] <> ';') do Inc(I);
Result := Trim(Copy(Items, Pos, I - Pos));
if (I <= Length(Items)) and (Items[I] = ';') then Inc(I);
Pos := I;
end;
Type
TCharSet = Set of char;
function NameDelimiter(C: Char; Delims: TCharSet): Boolean;
begin
Result := (C in [' ', ',', ';', ')', #13, #10]) or (C in Delims);
end;
function IsLiteral(C: Char): Boolean;
begin
Result := C in ['''', '"'];
end;
procedure CreateMacros(List: TMacros; const Value: PChar; SpecialChar: Char; Delims: TCharSet);
var
CurPos, StartPos: PChar;
CurChar: Char;
Literal: Boolean;
EmbeddedLiteral: Boolean;
Name: string;
function StripLiterals(Buffer: PChar): string;
var
Len: Word;
TempBuf: PChar;
procedure StripChar(Value: Char);
begin
if TempBuf^ = Value then
StrMove(TempBuf, TempBuf + 1, Len - 1);
if TempBuf[StrLen(TempBuf) - 1] = Value then
TempBuf[StrLen(TempBuf) - 1] := #0;
end;
begin
Len := StrLen(Buffer) + 1;
TempBuf := AllocMem(Len);
Result := '';
try
StrCopy(TempBuf, Buffer);
StripChar('''');
StripChar('"');
Result := StrPas(TempBuf);
finally
FreeMem(TempBuf, Len);
end;
end;
begin
if SpecialChar = #0 then Exit;
CurPos := Value;
Literal := False;
EmbeddedLiteral := False;
repeat
CurChar := CurPos^;
if (CurChar = SpecialChar) and not Literal and ((CurPos + 1)^ <> SpecialChar) then
begin
StartPos := CurPos;
while (CurChar <> #0) and (Literal or not NameDelimiter(CurChar, Delims)) do begin
Inc(CurPos);
CurChar := CurPos^;
if IsLiteral(CurChar) then begin
Literal := Literal xor True;
if CurPos = StartPos + 1 then EmbeddedLiteral := True;
end;
end;
CurPos^ := #0;
if EmbeddedLiteral then begin
Name := StripLiterals(StartPos + 1);
EmbeddedLiteral := False;
end
else Name := StrPas(StartPos + 1);
if Assigned(List) then begin
if List.FindMacro(Name) = nil then
List.CreateMacro(Name);
end;
CurPos^ := CurChar;
StartPos^ := '?';
Inc(StartPos);
StrMove(StartPos, CurPos, StrLen(CurPos) + 1);
CurPos := StartPos;
end
else if (CurChar = SpecialChar) and not Literal and ((CurPos + 1)^ = SpecialChar) then
StrMove(CurPos, CurPos + 1, StrLen(CurPos) + 1)
else if IsLiteral(CurChar) then Literal := Literal xor True;
Inc(CurPos);
until CurChar = #0;
end;
{ TMacro }
constructor TMacro.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
{$ifdef usevariant}
FData := Unassigned;
{$else}
FData:='';
{$endif}
end;
procedure TMacro.Assign(Source: TPersistent);
begin
if (Source is TMacro) and (Source <> nil) then
begin
{$ifdef usevariant}
if VarIsEmpty(TMacro(Source).FData) then
Clear
else
{$endif}
Value := TMacro(Source).FData;
Name := TMacro(Source).Name;
end;
end;
function TMacro.GetDisplayName: string;
begin
if FName = '' then
Result := inherited GetDisplayName
else
Result := FName;
end;
procedure TMacro.SetDisplayName(const Value: string);
begin
if (Value <> '') and (AnsiCompareText(Value, FName) <> 0) and
(Collection is TMacros) and (TMacros(Collection).IndexOf(Value) >= 0) then
raise Exception.Create(SDuplicateString);
FName := Value;
inherited;
end;
procedure TMacro.GetMacroText(var AText: string);
begin
if Assigned(FOnGetText) then FOnGetText(Self, FData, AText);
end;
function TMacro.GetText: string;
begin
Result := FData;
GetMacroText(Result);
end;
function TMacro.GetMacros: TMacros;
begin
if Collection is TMacros then
Result := TMacros(Collection)
else
Result := nil;
end;
procedure TMacro.Clear;
begin
{$ifdef usevariant}
FData := Unassigned;
{$else}
FData := '';
{$endif}
end;
function TMacro.IsMacroStored: Boolean;
begin
{$ifdef usevariant}
Result := not VarIsEmpty(FData);
{$else}
Result := (FData<>'');
{$endif}
end;
function TMacro.GetAsTMacroData: TMacroData;
begin
Result := FData;
end;
procedure TMacro.SetAsTMacroData(Value: TMacroData);
begin
FData := Value;
end;
function TMacro.IsEqual(Value: TMacro): Boolean;
begin
{$ifdef usevariant}
Result := (VarType(FData) = VarType(Value.FData)) and
(VarIsEmpty(FData) or (FData = Value.FData)) and
(Name = Value.Name);
{$else}
Result := (FData=Value.FData) and
(Name = Value.Name);
{$endif}
end;
{ TMacros }
constructor TMacros.Create(AOwner: TPersistent);
begin
inherited Create(AOwner, TMacro);
end;
function TMacros.IndexOf(const AName: string): Integer;
begin
for Result := 0 to Count - 1 do
if AnsiCompareText(TMacro(Items[Result]).Name, AName) = 0 then Exit;
Result := -1;
end;
function TMacros.GetItem(Index: Integer): TMacro;
begin
Result := TMacro(inherited Items[Index]);
end;
procedure TMacros.SetItem(Index: Integer; Value: TMacro);
begin
inherited SetItem(Index, TCollectionItem(Value));
end;
procedure TMacros.AddMacro(Value: TMacro);
begin
Value.Collection := Self;
end;
procedure TMacros.RemoveMacro(Value: TMacro);
begin
if Value.Collection = Self then
Value.Collection := nil;
end;
function TMacros.CreateMacro(const MacroName: string): TMacro;
begin
Result := Add as TMacro;
Result.Name := MacroName;
end;
function TMacros.IsEqual(Value: TMacros): Boolean;
var
I: Integer;
begin
Result := Count = Value.Count;
if Result then
for I := 0 to Count - 1 do begin
Result := Items[I].IsEqual(Value.Items[I]);
if not Result then Break;
end;
end;
function TMacros.MacroByName(const Value: string): TMacro;
begin
Result := FindMacro(Value);
if Result = nil then
raise Exception.Create(SInvalidPropertyValue);
end;
function TMacros.FindMacro(const Value: string): TMacro;
var
I: Integer;
begin
for I := 0 to Count - 1 do begin
Result := TMacro(inherited Items[I]);
if AnsiCompareText(Result.Name, Value) = 0 then Exit;
end;
Result := nil;
end;
procedure TMacros.AssignValues(Value: TMacros);
var
I: Integer;
P: TMacro;
begin
BeginUpdate;
try
for I := 0 to Value.Count - 1 do begin
P := FindMacro(Value[I].Name);
if P <> nil then P.Assign(Value[I]);
end;
finally
EndUpdate;
end;
end;
function TMacros.ParseString(const Value: string; DoCreate: Boolean;
SpecialChar: Char): string;
var
Macros: TMacros;
begin
Result := Value;
Macros := TMacros.Create(Self.GetOwner);
try
CreateMacros(Macros, PChar(Result), SpecialChar, ['.']);
if DoCreate then begin
Macros.AssignValues(Self);
Self.Assign(Macros);
end;
finally
Macros.Free;
end;
end;
function TMacros.GetMacroValue(const MacroName: string): TMacroData;
{$ifdef usevariant}
var
I: Integer;
Macros: TList;
{$ENDIF}
begin
{$ifdef usevariant}
if Pos(';', MacroName) <> 0 then
begin
Macros := TList.Create;
try
GetMacroList(Macros, MacroName);
Result := VarArrayCreate([0, Macros.Count - 1], varVariant);
for I := 0 to Macros.Count - 1 do
Result[I] := TMacro(Macros[I]).Value;
finally
Macros.Free;
end;
end
else
{$else}
Result := MacroByName(MacroName).Value;
{$endif}
end;
procedure TMacros.SetMacroValue(const MacroName: string;
const Value: TMacroData);
var
I: Integer;
Macros: TList;
begin
if Pos(';', MacroName) <> 0 then begin
Macros := TList.Create;
try
GetMacroList(Macros, MacroName);
for I := 0 to Macros.Count - 1 do
TMacro(Macros[I]).Value := Value[I];
finally
Macros.Free;
end;
end
else MacroByName(MacroName).Value := Value;
end;
procedure TMacros.GetMacroList(List: TList; const MacroNames: string);
var
Pos: Integer;
begin
Pos := 1;
while Pos <= Length(MacroNames) do
List.Add(MacroByName(ExtractName(MacroNames, Pos)));
end;
{ TStrHolder }
constructor TStrHolder.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FStrings := TStringList.Create;
FMacros := TMacros.Create(Self);
FMacroChar := '%';
TStringList(FStrings).OnChange := @StringsChanged;
TStringList(FStrings).OnChanging := @StringsChanging;
end;
destructor TStrHolder.Destroy;
begin
FOnChange := nil;
FOnChanging := nil;
FMacros.Free;
FStrings.Free;
inherited Destroy;
end;
procedure TStrHolder.Assign(Source: TPersistent);
begin
if Source is TStrings then
FStrings.Assign(Source)
else if Source is TStrHolder then
FStrings.Assign(TStrHolder(Source).Strings)
else
inherited Assign(Source);
end;
procedure TStrHolder.AssignTo(Dest: TPersistent);
begin
if Dest is TStrings then
Dest.Assign(Strings)
else
inherited AssignTo(Dest);
end;
procedure TStrHolder.Changed;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure TStrHolder.Changing;
begin
if Assigned(FOnChanging) then FOnChanging(Self);
end;
procedure TStrHolder.Clear;
begin
FStrings.Clear;
end;
function TStrHolder.GetCommaText: string;
begin
Result := FStrings.CommaText;
end;
procedure TStrHolder.SetCommaText(const Value: string);
begin
FStrings.CommaText := Value;
end;
function TStrHolder.GetCapacity: Integer;
begin
Result := FStrings.Capacity;
end;
procedure TStrHolder.SetCapacity(NewCapacity: Integer);
begin
FStrings.Capacity := NewCapacity;
end;
procedure TStrHolder.BeforeExpandMacros;
begin
if Assigned(FOnExpandMacros) then FOnExpandMacros(Self);
end;
procedure TStrHolder.SetMacros(Value: TMacros);
begin
FMacros.AssignValues(Value);
end;
procedure TStrHolder.RecreateMacros;
begin
if not (csReading in ComponentState) then
Macros.ParseString(FStrings.Text, True, MacroChar);
end;
procedure TStrHolder.SetMacroChar(Value: Char);
begin
if Value <> FMacroChar then begin
FMacroChar := Value;
RecreateMacros;
end;
end;
function TStrHolder.MacroCount: Integer;
begin
Result := Macros.Count;
end;
function TStrHolder.MacroByName(const MacroName: string): TMacro;
begin
Result := Macros.MacroByName(MacroName);
end;
function TStrHolder.ExpandMacros: string;
var
I, J, P, LiteralChars: Integer;
Macro: TMacro;
Found: Boolean;
begin
BeforeExpandMacros;
Result := FStrings.Text;
for I := Macros.Count - 1 downto 0 do
begin
Macro := Macros[I];
{$ifdef usevariant}
if VarIsEmpty(Macro.FData) then
Continue;
{$endif}
repeat
P := Pos(MacroChar + Macro.Name, Result);
Found := (P > 0) and ((Length(Result) = P + Length(Macro.Name)) or
NameDelimiter(Result[P + Length(Macro.Name) + 1], ['.']));
if Found then begin
LiteralChars := 0;
for J := 1 to P - 1 do
if IsLiteral(Result[J]) then Inc(LiteralChars);
Found := LiteralChars mod 2 = 0;
if Found then begin
Result := Copy(Result, 1, P - 1) + Macro.Text + Copy(Result,
P + Length(Macro.Name) + 1, MaxInt);
end;
end;
until not Found;
end;
end;
procedure TStrHolder.DefineProperties(Filer: TFiler);
function DoWrite: Boolean;
var
I: Integer;
Ancestor: TStrHolder;
begin
Ancestor := TStrHolder(Filer.Ancestor);
Result := False;
if (Ancestor <> nil) and (Ancestor.FStrings.Count = FStrings.Count) and
(KeyString = Ancestor.KeyString) and (FStrings.Count > 0) then
for I := 0 to FStrings.Count - 1 do begin
Result := CompareText(FStrings[I], Ancestor.FStrings[I]) <> 0;
if Result then Break;
end
else Result := (FStrings.Count > 0) or (Length(KeyString) > 0);
end;
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('StrData', @ReadStrings, @WriteStrings, DoWrite);
end;
function TStrHolder.GetSorted: Boolean;
begin
Result := TStringList(FStrings).Sorted;
end;
function TStrHolder.GetDuplicates: TDuplicates;
begin
Result := TStringList(FStrings).Duplicates;
end;
procedure TStrHolder.ReadStrings(Reader: TReader);
begin
Reader.ReadListBegin;
if not Reader.EndOfList then KeyString := Reader.ReadString;
FStrings.Clear;
while not Reader.EndOfList do
FStrings.Add(XorDecode(KeyString, Reader.ReadString));
Reader.ReadListEnd;
end;
procedure TStrHolder.SetDuplicates(Value: TDuplicates);
begin
TStringList(FStrings).Duplicates := Value;
end;
procedure TStrHolder.SetSorted(Value: Boolean);
begin
TStringList(FStrings).Sorted := Value;
end;
procedure TStrHolder.SetStrings(Value: TStrings);
begin
FStrings.Assign(Value);
end;
procedure TStrHolder.StringsChanged(Sender: TObject);
begin
if Sender=nil then ;
RecreateMacros;
if not (csReading in ComponentState) then Changed;
end;
procedure TStrHolder.StringsChanging(Sender: TObject);
begin
if Sender=nil then ;
if not (csReading in ComponentState) then Changing;
end;
procedure TStrHolder.WriteStrings(Writer: TWriter);
var
I: Integer;
begin
Writer.WriteListBegin;
Writer.WriteString(KeyString);
for I := 0 to FStrings.Count - 1 do
Writer.WriteString(XorEncode(KeyString, FStrings[I]));
Writer.WriteListEnd;
end;
initialization
{$I strholder.lrs}
end.
|