Changeset 308 for trunk/Packages/Common/UCommon.pas
- Timestamp:
- Aug 18, 2021, 11:50:13 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UCommon.pas
r192 r308 28 28 unfDNSDomainName = 11); 29 29 30 TFilterMethodMethod = function (FileName: string): Boolean of object; 30 TFilterMethod = function (FileName: string): Boolean of object; 31 TFileNameMethod = procedure (FileName: string) of object; 32 31 33 var 32 34 ExceptionHandler: TExceptionEvent; … … 72 74 function MergeArray(A, B: array of string): TArrayOfString; 73 75 function LoadFileToStr(const FileName: TFileName): AnsiString; 76 procedure SaveStringToFile(S, FileName: string); 74 77 procedure SearchFiles(AList: TStrings; Dir: string; 75 FilterMethod: TFilterMethod Method);78 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 76 79 function GetStringPart(var Text: string; Separator: string): string; 80 function StripTags(const S: string): string; 81 function PosFromIndex(SubStr: string; Text: string; 82 StartIndex: Integer): Integer; 83 function PosFromIndexReverse(SubStr: string; Text: string; 84 StartIndex: Integer): Integer; 85 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 77 86 78 87 … … 102 111 I: Integer; 103 112 begin 113 Result := ''; 104 114 for I := 1 to Length(Source) do begin 105 115 Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2)); … … 523 533 end; 524 534 535 procedure SaveStringToFile(S, FileName: string); 536 var 537 F: TextFile; 538 begin 539 AssignFile(F, FileName); 540 try 541 ReWrite(F); 542 Write(F, S); 543 finally 544 CloseFile(F); 545 end; 546 end; 547 525 548 procedure SearchFiles(AList: TStrings; Dir: string; 526 FilterMethod: TFilterMethod Method);549 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 527 550 var 528 551 SR: TSearchRec; … … 532 555 try 533 556 repeat 534 if (SR.Name = '.') or (SR.Name = '..') or not FilterMethod(SR.Name) or 535 not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name) then Continue; 557 if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or 558 not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue; 559 if Assigned(FileNameMethod) then 560 FileNameMethod(Dir + SR.Name); 536 561 AList.Add(Dir + SR.Name); 537 562 if (SR.Attr and faDirectory) <> 0 then … … 559 584 end; 560 585 586 function StripTags(const S: string): string; 587 var 588 Len: Integer; 589 590 function ReadUntil(const ReadFrom: Integer; const C: Char): Integer; 591 var 592 J: Integer; 593 begin 594 for J := ReadFrom to Len do 595 if (S[j] = C) then 596 begin 597 Result := J; 598 Exit; 599 end; 600 Result := Len + 1; 601 end; 602 603 var 604 I, APos: Integer; 605 begin 606 Len := Length(S); 607 I := 0; 608 Result := ''; 609 while (I <= Len) do begin 610 Inc(I); 611 APos := ReadUntil(I, '<'); 612 Result := Result + Copy(S, I, APos - i); 613 I := ReadUntil(APos + 1, '>'); 614 end; 615 end; 616 617 function PosFromIndex(SubStr: string; Text: string; 618 StartIndex: Integer): Integer; 619 var 620 I, MaxLen: SizeInt; 621 Ptr: PAnsiChar; 622 begin 623 Result := 0; 624 if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit; 625 if Length(SubStr) > 0 then begin 626 MaxLen := Length(Text) - Length(SubStr) + 1; 627 I := StartIndex; 628 Ptr := @Text[StartIndex]; 629 while (I <= MaxLen) do begin 630 if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin 631 Result := I; 632 Exit; 633 end; 634 Inc(I); 635 Inc(Ptr); 636 end; 637 end; 638 end; 639 640 function PosFromIndexReverse(SubStr: string; Text: string; 641 StartIndex: Integer): Integer; 642 var 643 I: SizeInt; 644 Ptr: PAnsiChar; 645 begin 646 Result := 0; 647 if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit; 648 if Length(SubStr) > 0 then begin 649 I := StartIndex; 650 Ptr := @Text[StartIndex]; 651 while (I > 0) do begin 652 if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin 653 Result := I; 654 Exit; 655 end; 656 Dec(I); 657 Dec(Ptr); 658 end; 659 end; 660 end; 661 662 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 663 var 664 I: Integer; 665 begin 666 SetLength(Dest, Length(Source)); 667 for I := 0 to Length(Dest) - 1 do 668 Dest[I] := Source[I]; 669 end; 561 670 562 671
Note:
See TracChangeset
for help on using the changeset viewer.