Changeset 75 for trunk/Packages/Common/Common.pas
- Timestamp:
- Jun 4, 2024, 12:22:49 AM (5 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Common.pas
r74 r75 1 unit UCommon; 2 3 {$mode delphi} 1 unit Common; 4 2 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: string; List: TList<string>): string; 68 function Implode(Separator: string; List: TStringList; Around: string = ''): string; 69 function LastPos(const SubStr: String; const S: String): Integer; 70 function LoadFileToStr(const FileName: TFileName): AnsiString; 71 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 72 function MergeArray(A, B: array of string): TStringArray; 73 function OccurenceOfChar(What: Char; Where: string): Integer; 74 procedure OpenWebPage(URL: string); 75 procedure OpenEmail(Email: string); 76 procedure OpenFileInShell(FileName: string); 77 function PosFromIndex(SubStr: string; Text: string; 78 StartIndex: Integer): Integer; 79 function PosFromIndexReverse(SubStr: string; Text: string; 80 StartIndex: Integer): Integer; 81 function RemoveQuotes(Text: string): string; 82 procedure SaveStringToFile(S, FileName: string); 53 83 procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload; 54 84 procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload; 55 85 procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload; 56 86 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(CommandLine: 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; 87 procedure SearchFiles(AList: TStrings; Dir: string; 88 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 89 function SplitString(var Text: string; Count: Word): string; 90 function StripTags(const S: string): string; 91 function TryHexToInt(Data: string; out Value: Integer): Boolean; 92 function TryBinToInt(Data: string; out Value: Integer): Boolean; 93 procedure SortStrings(Strings: TStrings); 73 94 74 95 … … 98 119 I: Integer; 99 120 begin 121 Result := ''; 100 122 for I := 1 to Length(Source) do begin 101 123 Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2)); … … 112 134 Path := IncludeTrailingPathDelimiter(APath); 113 135 114 Find := FindFirst( UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);136 Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec); 115 137 while Find = 0 do begin 116 DeleteFile(Path + UTF8Encode(SearchRec.Name));138 DeleteFile(Path + SearchRec.Name); 117 139 118 140 Find := SysUtils.FindNext(SearchRec); … … 185 207 end;*) 186 208 209 function Implode(Separator: string; List: TStringList; Around: string = ''): string; 210 var 211 I: Integer; 212 begin 213 Result := ''; 214 for I := 0 to List.Count - 1 do begin 215 Result := Result + Around + List[I] + Around; 216 if I < List.Count - 1 then Result := Result + Separator; 217 end; 218 end; 219 187 220 function LastPos(const SubStr: String; const S: String): Integer; 188 221 begin … … 230 263 end; 231 264 232 function TryHexToInt(Data: string; varValue: Integer): Boolean;265 function TryHexToInt(Data: string; out Value: Integer): Boolean; 233 266 var 234 267 I: Integer; … … 246 279 end; 247 280 248 function TryBinToInt(Data: string; varValue: Integer): Boolean;281 function TryBinToInt(Data: string; out Value: Integer): Boolean; 249 282 var 250 283 I: Integer; … … 274 307 end; 275 308 276 function Explode(Separator: char; Data: string): TArrayOfString; 277 begin 278 SetLength(Result, 0); 279 while Pos(Separator, Data) > 0 do begin 309 function Explode(Separator: Char; Data: string): TStringArray; 310 var 311 Index: Integer; 312 begin 313 Result := Default(TStringArray); 314 repeat 315 Index := Pos(Separator, Data); 316 if Index > 0 then begin 317 SetLength(Result, Length(Result) + 1); 318 Result[High(Result)] := Copy(Data, 1, Index - 1); 319 Delete(Data, 1, Index); 320 end else Break; 321 until False; 322 if Data <> '' then begin 280 323 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} 324 Result[High(Result)] := Data; 325 end; 326 end; 327 328 function Implode(Separator: string; List: TList<string>): string; 329 var 330 I: Integer; 331 begin 332 Result := ''; 333 for I := 0 to List.Count - 1 do begin 334 Result := Result + List[I]; 335 if I < List.Count - 1 then Result := Result + Separator; 336 end; 337 end; 338 339 {$IFDEF WINDOWS} 289 340 function GetUserName: string; 290 341 const … … 294 345 begin 295 346 L := MAX_USERNAME_LENGTH + 2; 347 Result := Default(string); 296 348 SetLength(Result, L); 297 349 if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin … … 307 359 end; 308 360 end; 309 {$ endif}361 {$ENDIF} 310 362 311 363 function ComputerName: string; 312 {$ ifdef mswindows}364 {$IFDEF WINDOWS} 313 365 const 314 366 INFO_BUFFER_SIZE = 32767; … … 325 377 end; 326 378 end; 327 {$ endif}328 {$ ifdef unix}379 {$ENDIF} 380 {$IFDEF UNIX} 329 381 var 330 382 Name: UtsName; 331 383 begin 384 Name := Default(UtsName); 332 385 fpuname(Name); 333 386 Result := Name.Nodename; 334 387 end; 335 {$ endif}336 337 {$ ifdef windows}388 {$ENDIF} 389 390 {$IFDEF WINDOWS} 338 391 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 339 392 const … … 413 466 procedure LoadLibraries; 414 467 begin 415 {$IFDEF W indows}468 {$IFDEF WINDOWS} 416 469 DLLHandle1 := LoadLibrary('secur32.dll'); 417 470 if DLLHandle1 <> 0 then … … 424 477 procedure FreeLibraries; 425 478 begin 426 {$IFDEF W indows}479 {$IFDEF WINDOWS} 427 480 if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1); 428 481 {$ENDIF} 429 482 end; 430 483 431 procedure ExecuteProgram( CommandLine:string);484 procedure ExecuteProgram(Executable: string; Parameters: array of string); 432 485 var 433 486 Process: TProcess; 487 I: Integer; 434 488 begin 435 489 try 436 490 Process := TProcess.Create(nil); 437 Process.CommandLine := CommandLine; 491 Process.Executable := Executable; 492 for I := 0 to Length(Parameters) - 1 do 493 Process.Parameters.Add(Parameters[I]); 438 494 Process.Options := [poNoConsole]; 439 495 Process.Execute; … … 454 510 end; 455 511 512 procedure OpenEmail(Email: string); 513 begin 514 OpenURL('mailto:' + Email); 515 end; 516 456 517 procedure OpenFileInShell(FileName: string); 457 518 begin 458 ExecuteProgram('cmd.exe /c start "' + FileName + '"');519 ExecuteProgram('cmd.exe', ['/c', 'start', FileName]); 459 520 end; 460 521 … … 482 543 end; 483 544 484 function MergeArray(A, B: array of string): TArrayOfString; 485 var 486 I: Integer; 487 begin 545 function MergeArray(A, B: array of string): TStringArray; 546 var 547 I: Integer; 548 begin 549 Result := Default(TStringArray); 488 550 SetLength(Result, Length(A) + Length(B)); 489 551 for I := 0 to Length(A) - 1 do … … 511 573 end; 512 574 575 function DefaultSearchFilter(const FileName: string): Boolean; 576 begin 577 Result := True; 578 end; 579 580 procedure SaveStringToFile(S, FileName: string); 581 var 582 F: TextFile; 583 begin 584 AssignFile(F, FileName); 585 try 586 ReWrite(F); 587 Write(F, S); 588 finally 589 CloseFile(F); 590 end; 591 end; 592 593 procedure SearchFiles(AList: TStrings; Dir: string; 594 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 595 var 596 SR: TSearchRec; 597 begin 598 Dir := IncludeTrailingPathDelimiter(Dir); 599 if FindFirst(Dir + '*', faAnyFile, SR) = 0 then 600 try 601 repeat 602 if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or 603 not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue; 604 if Assigned(FileNameMethod) then 605 FileNameMethod(Dir + SR.Name); 606 AList.Add(Dir + SR.Name); 607 if (SR.Attr and faDirectory) <> 0 then 608 SearchFiles(AList, Dir + SR.Name, FilterMethod); 609 until FindNext(SR) <> 0; 610 finally 611 FindClose(SR); 612 end; 613 end; 614 615 function GetStringPart(var Text: string; Separator: string): string; 616 var 617 P: Integer; 618 begin 619 P := Pos(Separator, Text); 620 if P > 0 then begin 621 Result := Copy(Text, 1, P - 1); 622 Delete(Text, 1, P - 1 + Length(Separator)); 623 end else begin 624 Result := Text; 625 Text := ''; 626 end; 627 Result := Trim(Result); 628 Text := Trim(Text); 629 end; 630 631 function StripTags(const S: string): string; 632 var 633 Len: Integer; 634 635 function ReadUntil(const ReadFrom: Integer; const C: Char): Integer; 636 var 637 J: Integer; 638 begin 639 for J := ReadFrom to Len do 640 if (S[j] = C) then 641 begin 642 Result := J; 643 Exit; 644 end; 645 Result := Len + 1; 646 end; 647 648 var 649 I, APos: Integer; 650 begin 651 Len := Length(S); 652 I := 0; 653 Result := ''; 654 while (I <= Len) do begin 655 Inc(I); 656 APos := ReadUntil(I, '<'); 657 Result := Result + Copy(S, I, APos - i); 658 I := ReadUntil(APos + 1, '>'); 659 end; 660 end; 661 662 function PosFromIndex(SubStr: string; Text: string; 663 StartIndex: Integer): Integer; 664 var 665 I, MaxLen: SizeInt; 666 Ptr: PAnsiChar; 667 begin 668 Result := 0; 669 if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit; 670 if Length(SubStr) > 0 then begin 671 MaxLen := Length(Text) - Length(SubStr) + 1; 672 I := StartIndex; 673 Ptr := @Text[StartIndex]; 674 while (I <= MaxLen) do begin 675 if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin 676 Result := I; 677 Exit; 678 end; 679 Inc(I); 680 Inc(Ptr); 681 end; 682 end; 683 end; 684 685 function PosFromIndexReverse(SubStr: string; Text: string; 686 StartIndex: Integer): Integer; 687 var 688 I: SizeInt; 689 Ptr: PAnsiChar; 690 begin 691 Result := 0; 692 if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit; 693 if Length(SubStr) > 0 then begin 694 I := StartIndex; 695 Ptr := @Text[StartIndex]; 696 while (I > 0) do begin 697 if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin 698 Result := I; 699 Exit; 700 end; 701 Dec(I); 702 Dec(Ptr); 703 end; 704 end; 705 end; 706 707 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 708 var 709 I: Integer; 710 begin 711 SetLength(Dest, Length(Source)); 712 for I := 0 to Length(Dest) - 1 do 713 Dest[I] := Source[I]; 714 end; 715 716 function CombinePaths(Path1, Path2: string): string; 717 begin 718 Result := Path1; 719 if Result <> '' then Result := Result + DirectorySeparator + Path2 720 else Result := Path2; 721 end; 722 723 procedure SortStrings(Strings: TStrings); 724 var 725 Tmp: TStringList; 726 begin 727 Strings.BeginUpdate; 728 try 729 if Strings is TStringList then begin 730 TStringList(Strings).Sort; 731 end else begin 732 Tmp := TStringList.Create; 733 try 734 Tmp.Assign(Strings); 735 Tmp.Sort; 736 Strings.Assign(Tmp); 737 finally 738 Tmp.Free; 739 end; 740 end; 741 finally 742 Strings.EndUpdate; 743 end; 744 end; 513 745 514 746
Note:
See TracChangeset
for help on using the changeset viewer.