Changeset 25 for trunk/Packages/Common/UCommon.pas
- Timestamp:
- Sep 10, 2022, 6:54:43 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UCommon.pas
r15 r25 1 1 unit UCommon; 2 2 3 {$mode delphi}4 5 3 interface 6 4 7 5 uses 8 {$ ifdef Windows}Windows,{$endif}9 {$ ifdef Linux}baseunix,{$endif}10 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, 11 FileUtil ; //, ShFolder, ShellAPI;6 {$IFDEF WINDOWS}Windows,{$ENDIF} 7 {$IFDEF UNIX}baseunix,{$ENDIF} 8 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, Graphics, 9 FileUtil, Generics.Collections; //, ShFolder, ShellAPI; 12 10 13 11 type 14 12 TArrayOfByte = array of Byte; 15 TArrayOfString = array of string;16 13 TExceptionEvent = procedure(Sender: TObject; E: Exception) of object; 17 14 … … 28 25 unfDNSDomainName = 11); 29 26 27 TFilterMethod = function (FileName: string): Boolean of object; 28 TFileNameMethod = procedure (FileName: string) of object; 29 30 30 var 31 31 ExceptionHandler: TExceptionEvent; 32 32 DLLHandle1: HModule; 33 33 34 {$IFDEF Windows} 35 GetUserNameEx: procedure (NameFormat: DWORD; 36 lpNameBuffer: LPSTR; nSize: PULONG); stdcall; 37 {$ENDIF} 38 39 function IntToBin(Data: Int64; Count: Byte): string; 34 {$IFDEF WINDOWS} 35 GetUserNameEx: procedure (NameFormat: DWORD; 36 lpNameBuffer: LPSTR; nSize: PULONG); stdcall; 37 {$ENDIF} 38 39 const 40 clLightBlue = TColor($FF8080); 41 clLightGreen = TColor($80FF80); 42 clLightRed = TColor($8080FF); 43 44 function AddLeadingZeroes(const aNumber, Length : integer) : string; 40 45 function BinToInt(BinStr: string): Int64; 41 function TryHexToInt(Data: string; var Value: Integer): Boolean;42 function TryBinToInt(Data: string; var Value: Integer): Boolean;43 46 function BinToHexString(Source: AnsiString): string; 44 47 //function DelTree(DirName : string): Boolean; … … 46 49 function BCDToInt(Value: Byte): Byte; 47 50 function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean; 51 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 52 function CombinePaths(Path1, Path2: string): string; 53 function ComputerName: string; 54 procedure DeleteFiles(APath, AFileSpec: string); 55 function Explode(Separator: Char; Data: string): TStringArray; 56 procedure ExecuteProgram(Executable: string; Parameters: array of string); 57 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog); 58 procedure FreeThenNil(var Obj); 59 function GetDirCount(Dir: string): Integer; 48 60 function GetUserName: string; 49 function LoggedOnUserNameEx(Format: TUserNameFormat): string;50 function SplitString(var Text: string; Count: Word): string;51 61 function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer; 52 62 function GetBit(Variable: QWord; Index: Byte): Boolean; 63 function GetStringPart(var Text: string; Separator: string): string; 64 function GenerateNewName(OldName: string): string; 65 function GetFileFilterItemExt(Filter: string; Index: Integer): string; 66 function IntToBin(Data: Int64; Count: Byte): string; 67 function Implode(Separator: Char; List: TList<string>): string; 68 function LastPos(const SubStr: String; const S: String): Integer; 69 function LoadFileToStr(const FileName: TFileName): AnsiString; 70 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 71 function MergeArray(A, B: array of string): TStringArray; 72 function OccurenceOfChar(What: Char; Where: string): Integer; 73 procedure OpenWebPage(URL: string); 74 procedure OpenEmail(Email: string); 75 procedure OpenFileInShell(FileName: string); 76 function PosFromIndex(SubStr: string; Text: string; 77 StartIndex: Integer): Integer; 78 function PosFromIndexReverse(SubStr: string; Text: string; 79 StartIndex: Integer): Integer; 80 function RemoveQuotes(Text: string): string; 81 procedure SaveStringToFile(S, FileName: string); 53 82 procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload; 54 83 procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload; 55 84 procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload; 56 85 procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload; 57 function AddLeadingZeroes(const aNumber, Length : integer) : string; 58 function LastPos(const SubStr: String; const S: String): Integer; 59 function GenerateNewName(OldName: string): string; 60 function GetFileFilterItemExt(Filter: string; Index: Integer): string; 61 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog); 62 procedure DeleteFiles(APath, AFileSpec: string); 63 procedure OpenWebPage(URL: string); 64 procedure OpenFileInShell(FileName: string); 65 procedure ExecuteProgram(Executable: string; Parameters: array of string); 66 procedure FreeThenNil(var Obj); 67 function RemoveQuotes(Text: string): string; 68 function ComputerName: string; 69 function OccurenceOfChar(What: Char; Where: string): Integer; 70 function GetDirCount(Dir: string): Integer; 71 function MergeArray(A, B: array of string): TArrayOfString; 72 function LoadFileToStr(const FileName: TFileName): AnsiString; 86 procedure SearchFiles(AList: TStrings; Dir: string; 87 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 88 function SplitString(var Text: string; Count: Word): string; 89 function StripTags(const S: string): string; 90 function TryHexToInt(Data: string; out Value: Integer): Boolean; 91 function TryBinToInt(Data: string; out Value: Integer): Boolean; 92 procedure SortStrings(Strings: TStrings); 73 93 74 94 … … 98 118 I: Integer; 99 119 begin 120 Result := ''; 100 121 for I := 1 to Length(Source) do begin 101 122 Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2)); … … 230 251 end; 231 252 232 function TryHexToInt(Data: string; varValue: Integer): Boolean;253 function TryHexToInt(Data: string; out Value: Integer): Boolean; 233 254 var 234 255 I: Integer; … … 246 267 end; 247 268 248 function TryBinToInt(Data: string; varValue: Integer): Boolean;269 function TryBinToInt(Data: string; out Value: Integer): Boolean; 249 270 var 250 271 I: Integer; … … 274 295 end; 275 296 276 function Explode(Separator: char; Data: string): TArrayOfString; 277 begin 278 SetLength(Result, 0); 279 while Pos(Separator, Data) > 0 do begin 297 function Explode(Separator: Char; Data: string): TStringArray; 298 var 299 Index: Integer; 300 begin 301 Result := Default(TStringArray); 302 repeat 303 Index := Pos(Separator, Data); 304 if Index > 0 then begin 305 SetLength(Result, Length(Result) + 1); 306 Result[High(Result)] := Copy(Data, 1, Index - 1); 307 Delete(Data, 1, Index); 308 end else Break; 309 until False; 310 if Data <> '' then begin 280 311 SetLength(Result, Length(Result) + 1); 281 Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1); 282 Delete(Data, 1, Pos(Separator, Data)); 283 end; 284 SetLength(Result, Length(Result) + 1); 285 Result[High(Result)] := Data; 286 end; 287 288 {$IFDEF Windows} 312 Result[High(Result)] := Data; 313 end; 314 end; 315 316 function Implode(Separator: Char; List: TList<string>): string; 317 var 318 I: Integer; 319 begin 320 Result := ''; 321 for I := 0 to List.Count - 1 do begin 322 Result := Result + List[I]; 323 if I < List.Count - 1 then Result := Result + Separator; 324 end; 325 end; 326 327 {$IFDEF WINDOWS} 289 328 function GetUserName: string; 290 329 const … … 294 333 begin 295 334 L := MAX_USERNAME_LENGTH + 2; 335 Result := Default(string); 296 336 SetLength(Result, L); 297 337 if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin … … 307 347 end; 308 348 end; 309 {$ endif}349 {$ENDIF} 310 350 311 351 function ComputerName: string; 312 {$ ifdef mswindows}352 {$IFDEF WINDOWS} 313 353 const 314 354 INFO_BUFFER_SIZE = 32767; … … 325 365 end; 326 366 end; 327 {$ endif}328 {$ ifdef unix}367 {$ENDIF} 368 {$IFDEF UNIX} 329 369 var 330 370 Name: UtsName; 331 371 begin 372 Name := Default(UtsName); 332 373 fpuname(Name); 333 374 Result := Name.Nodename; 334 375 end; 335 {$ endif}336 337 {$ ifdef windows}376 {$ENDIF} 377 378 {$IFDEF WINDOWS} 338 379 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 339 380 const … … 413 454 procedure LoadLibraries; 414 455 begin 415 {$IFDEF W indows}456 {$IFDEF WINDOWS} 416 457 DLLHandle1 := LoadLibrary('secur32.dll'); 417 458 if DLLHandle1 <> 0 then … … 424 465 procedure FreeLibraries; 425 466 begin 426 {$IFDEF W indows}467 {$IFDEF WINDOWS} 427 468 if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1); 428 469 {$ENDIF} … … 457 498 end; 458 499 500 procedure OpenEmail(Email: string); 501 begin 502 OpenURL('mailto:' + Email); 503 end; 504 459 505 procedure OpenFileInShell(FileName: string); 460 506 begin … … 485 531 end; 486 532 487 function MergeArray(A, B: array of string): TArrayOfString; 488 var 489 I: Integer; 490 begin 533 function MergeArray(A, B: array of string): TStringArray; 534 var 535 I: Integer; 536 begin 537 Result := Default(TStringArray); 491 538 SetLength(Result, Length(A) + Length(B)); 492 539 for I := 0 to Length(A) - 1 do … … 514 561 end; 515 562 563 function DefaultSearchFilter(const FileName: string): Boolean; 564 begin 565 Result := True; 566 end; 567 568 procedure SaveStringToFile(S, FileName: string); 569 var 570 F: TextFile; 571 begin 572 AssignFile(F, FileName); 573 try 574 ReWrite(F); 575 Write(F, S); 576 finally 577 CloseFile(F); 578 end; 579 end; 580 581 procedure SearchFiles(AList: TStrings; Dir: string; 582 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 583 var 584 SR: TSearchRec; 585 begin 586 Dir := IncludeTrailingPathDelimiter(Dir); 587 if FindFirst(Dir + '*', faAnyFile, SR) = 0 then 588 try 589 repeat 590 if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or 591 not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue; 592 if Assigned(FileNameMethod) then 593 FileNameMethod(Dir + SR.Name); 594 AList.Add(Dir + SR.Name); 595 if (SR.Attr and faDirectory) <> 0 then 596 SearchFiles(AList, Dir + SR.Name, FilterMethod); 597 until FindNext(SR) <> 0; 598 finally 599 FindClose(SR); 600 end; 601 end; 602 603 function GetStringPart(var Text: string; Separator: string): string; 604 var 605 P: Integer; 606 begin 607 P := Pos(Separator, Text); 608 if P > 0 then begin 609 Result := Copy(Text, 1, P - 1); 610 Delete(Text, 1, P - 1 + Length(Separator)); 611 end else begin 612 Result := Text; 613 Text := ''; 614 end; 615 Result := Trim(Result); 616 Text := Trim(Text); 617 end; 618 619 function StripTags(const S: string): string; 620 var 621 Len: Integer; 622 623 function ReadUntil(const ReadFrom: Integer; const C: Char): Integer; 624 var 625 J: Integer; 626 begin 627 for J := ReadFrom to Len do 628 if (S[j] = C) then 629 begin 630 Result := J; 631 Exit; 632 end; 633 Result := Len + 1; 634 end; 635 636 var 637 I, APos: Integer; 638 begin 639 Len := Length(S); 640 I := 0; 641 Result := ''; 642 while (I <= Len) do begin 643 Inc(I); 644 APos := ReadUntil(I, '<'); 645 Result := Result + Copy(S, I, APos - i); 646 I := ReadUntil(APos + 1, '>'); 647 end; 648 end; 649 650 function PosFromIndex(SubStr: string; Text: string; 651 StartIndex: Integer): Integer; 652 var 653 I, MaxLen: SizeInt; 654 Ptr: PAnsiChar; 655 begin 656 Result := 0; 657 if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit; 658 if Length(SubStr) > 0 then begin 659 MaxLen := Length(Text) - Length(SubStr) + 1; 660 I := StartIndex; 661 Ptr := @Text[StartIndex]; 662 while (I <= MaxLen) do begin 663 if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin 664 Result := I; 665 Exit; 666 end; 667 Inc(I); 668 Inc(Ptr); 669 end; 670 end; 671 end; 672 673 function PosFromIndexReverse(SubStr: string; Text: string; 674 StartIndex: Integer): Integer; 675 var 676 I: SizeInt; 677 Ptr: PAnsiChar; 678 begin 679 Result := 0; 680 if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit; 681 if Length(SubStr) > 0 then begin 682 I := StartIndex; 683 Ptr := @Text[StartIndex]; 684 while (I > 0) do begin 685 if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin 686 Result := I; 687 Exit; 688 end; 689 Dec(I); 690 Dec(Ptr); 691 end; 692 end; 693 end; 694 695 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 696 var 697 I: Integer; 698 begin 699 SetLength(Dest, Length(Source)); 700 for I := 0 to Length(Dest) - 1 do 701 Dest[I] := Source[I]; 702 end; 703 704 function CombinePaths(Path1, Path2: string): string; 705 begin 706 Result := Path1; 707 if Result <> '' then Result := Result + DirectorySeparator + Path2 708 else Result := Path2; 709 end; 710 711 procedure SortStrings(Strings: TStrings); 712 var 713 Tmp: TStringList; 714 begin 715 Strings.BeginUpdate; 716 try 717 if Strings is TStringList then begin 718 TStringList(Strings).Sort; 719 end else begin 720 Tmp := TStringList.Create; 721 try 722 Tmp.Assign(Strings); 723 Tmp.Sort; 724 Strings.Assign(Tmp); 725 finally 726 Tmp.Free; 727 end; 728 end; 729 finally 730 Strings.EndUpdate; 731 end; 732 end; 516 733 517 734
Note:
See TracChangeset
for help on using the changeset viewer.