Changeset 131 for trunk/Packages/Common/UCommon.pas
- Timestamp:
- Mar 18, 2022, 1:37:03 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UCommon.pas
r122 r131 6 6 7 7 uses 8 {$ ifdef Windows}Windows,{$endif}9 {$ ifdef Linux}baseunix,{$endif}10 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, 8 {$IFDEF WINDOWS}Windows,{$ENDIF} 9 {$IFDEF UNIX}baseunix,{$ENDIF} 10 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, Graphics, 11 11 FileUtil; //, ShFolder, ShellAPI; 12 12 13 13 type 14 14 TArrayOfByte = array of Byte; 15 TArrayOfString = array of string;16 15 TExceptionEvent = procedure(Sender: TObject; E: Exception) of object; 17 16 … … 28 27 unfDNSDomainName = 11); 29 28 30 TFilterMethodMethod = function (FileName: string): Boolean of object; 29 TFilterMethod = function (FileName: string): Boolean of object; 30 TFileNameMethod = procedure (FileName: string) of object; 31 31 32 var 32 33 ExceptionHandler: TExceptionEvent; 33 34 DLLHandle1: HModule; 34 35 35 {$IFDEF Windows} 36 const 37 clLightBlue = TColor($FF8080); 38 clLightGreen = TColor($80FF80); 39 clLightRed = TColor($8080FF); 40 41 {$IFDEF WINDOWS} 36 42 GetUserNameEx: procedure (NameFormat: DWORD; 37 43 lpNameBuffer: LPSTR; nSize: PULONG); stdcall; 38 44 {$ENDIF} 39 45 40 function IntToBin(Data: Int64; Count: Byte): string;46 function AddLeadingZeroes(const aNumber, Length : integer) : string; 41 47 function BinToInt(BinStr: string): Int64; 42 function TryHexToInt(Data: string; var Value: Integer): Boolean;43 function TryBinToInt(Data: string; var Value: Integer): Boolean;44 48 function BinToHexString(Source: AnsiString): string; 45 49 //function DelTree(DirName : string): Boolean; … … 47 51 function BCDToInt(Value: Byte): Byte; 48 52 function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean; 53 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 54 function CombinePaths(Path1, Path2: string): string; 55 function ComputerName: string; 56 procedure DeleteFiles(APath, AFileSpec: string); 57 function Explode(Separator: Char; Data: string): TStringArray; 58 procedure ExecuteProgram(Executable: string; Parameters: array of string); 59 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog); 60 procedure FreeThenNil(var Obj); 61 function GetDirCount(Dir: string): Integer; 49 62 function GetUserName: string; 50 function LoggedOnUserNameEx(Format: TUserNameFormat): string;51 function SplitString(var Text: string; Count: Word): string;52 63 function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer; 53 64 function GetBit(Variable: QWord; Index: Byte): Boolean; 65 function GetStringPart(var Text: string; Separator: string): string; 66 function GenerateNewName(OldName: string): string; 67 function GetFileFilterItemExt(Filter: string; Index: Integer): string; 68 function IntToBin(Data: Int64; Count: Byte): 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); 54 83 procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload; 55 84 procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload; 56 85 procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload; 57 86 procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload; 58 function AddLeadingZeroes(const aNumber, Length : integer) : string;59 function LastPos(const SubStr: String; const S: String): Integer;60 function GenerateNewName(OldName: string): string;61 function GetFileFilterItemExt(Filter: string; Index: Integer): string;62 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);63 procedure DeleteFiles(APath, AFileSpec: string);64 procedure OpenWebPage(URL: string);65 procedure OpenFileInShell(FileName: string);66 procedure ExecuteProgram(Executable: string; Parameters: array of string);67 procedure FreeThenNil(var Obj);68 function RemoveQuotes(Text: string): string;69 function ComputerName: string;70 function OccurenceOfChar(What: Char; Where: string): Integer;71 function GetDirCount(Dir: string): Integer;72 function MergeArray(A, B: array of string): TArrayOfString;73 function LoadFileToStr(const FileName: TFileName): AnsiString;74 87 procedure SearchFiles(AList: TStrings; Dir: string; 75 FilterMethod: TFilterMethodMethod = nil); 76 function GetStringPart(var Text: string; Separator: string): 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); 77 94 78 95 … … 102 119 I: Integer; 103 120 begin 121 Result := ''; 104 122 for I := 1 to Length(Source) do begin 105 123 Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2)); … … 234 252 end; 235 253 236 function TryHexToInt(Data: string; varValue: Integer): Boolean;254 function TryHexToInt(Data: string; out Value: Integer): Boolean; 237 255 var 238 256 I: Integer; … … 250 268 end; 251 269 252 function TryBinToInt(Data: string; varValue: Integer): Boolean;270 function TryBinToInt(Data: string; out Value: Integer): Boolean; 253 271 var 254 272 I: Integer; … … 278 296 end; 279 297 280 function Explode(Separator: char; Data: string): TArrayOfString; 281 begin 282 SetLength(Result, 0); 283 while Pos(Separator, Data) > 0 do begin 298 function Explode(Separator: Char; Data: string): TStringArray; 299 var 300 Index: Integer; 301 begin 302 Result := Default(TStringArray); 303 repeat 304 Index := Pos(Separator, Data); 305 if Index > 0 then begin 306 SetLength(Result, Length(Result) + 1); 307 Result[High(Result)] := Copy(Data, 1, Index - 1); 308 Delete(Data, 1, Index); 309 end else Break; 310 until False; 311 if Data <> '' then begin 284 312 SetLength(Result, Length(Result) + 1); 285 Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1); 286 Delete(Data, 1, Pos(Separator, Data)); 287 end; 288 SetLength(Result, Length(Result) + 1); 289 Result[High(Result)] := Data; 290 end; 291 292 {$IFDEF Windows} 313 Result[High(Result)] := Data; 314 end; 315 end; 316 317 {$IFDEF WINDOWS} 293 318 function GetUserName: string; 294 319 const … … 298 323 begin 299 324 L := MAX_USERNAME_LENGTH + 2; 325 Result := Default(string); 300 326 SetLength(Result, L); 301 327 if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin … … 311 337 end; 312 338 end; 313 {$ endif}339 {$ENDIF} 314 340 315 341 function ComputerName: string; 316 {$ ifdef mswindows}342 {$IFDEF WINDOWS} 317 343 const 318 344 INFO_BUFFER_SIZE = 32767; … … 329 355 end; 330 356 end; 331 {$ endif}332 {$ ifdef unix}357 {$ENDIF} 358 {$IFDEF UNIX} 333 359 var 334 360 Name: UtsName; 335 361 begin 362 Name := Default(UtsName); 336 363 fpuname(Name); 337 364 Result := Name.Nodename; 338 365 end; 339 {$ endif}340 341 {$ ifdef windows}366 {$ENDIF} 367 368 {$IFDEF WINDOWS} 342 369 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 343 370 const … … 417 444 procedure LoadLibraries; 418 445 begin 419 {$IFDEF W indows}446 {$IFDEF WINDOWS} 420 447 DLLHandle1 := LoadLibrary('secur32.dll'); 421 448 if DLLHandle1 <> 0 then … … 428 455 procedure FreeLibraries; 429 456 begin 430 {$IFDEF W indows}457 {$IFDEF WINDOWS} 431 458 if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1); 432 459 {$ENDIF} … … 461 488 end; 462 489 490 procedure OpenEmail(Email: string); 491 begin 492 OpenURL('mailto:' + Email); 493 end; 494 463 495 procedure OpenFileInShell(FileName: string); 464 496 begin … … 489 521 end; 490 522 491 function MergeArray(A, B: array of string): TArrayOfString; 492 var 493 I: Integer; 494 begin 523 function MergeArray(A, B: array of string): TStringArray; 524 var 525 I: Integer; 526 begin 527 Result := Default(TStringArray); 495 528 SetLength(Result, Length(A) + Length(B)); 496 529 for I := 0 to Length(A) - 1 do … … 523 556 end; 524 557 558 procedure SaveStringToFile(S, FileName: string); 559 var 560 F: TextFile; 561 begin 562 AssignFile(F, FileName); 563 try 564 ReWrite(F); 565 Write(F, S); 566 finally 567 CloseFile(F); 568 end; 569 end; 570 525 571 procedure SearchFiles(AList: TStrings; Dir: string; 526 FilterMethod: TFilterMethod Method = nil);572 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 527 573 var 528 574 SR: TSearchRec; … … 534 580 if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or 535 581 not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue; 582 if Assigned(FileNameMethod) then 583 FileNameMethod(Dir + SR.Name); 536 584 AList.Add(Dir + SR.Name); 537 585 if (SR.Attr and faDirectory) <> 0 then … … 559 607 end; 560 608 609 function StripTags(const S: string): string; 610 var 611 Len: Integer; 612 613 function ReadUntil(const ReadFrom: Integer; const C: Char): Integer; 614 var 615 J: Integer; 616 begin 617 for J := ReadFrom to Len do 618 if (S[j] = C) then 619 begin 620 Result := J; 621 Exit; 622 end; 623 Result := Len + 1; 624 end; 625 626 var 627 I, APos: Integer; 628 begin 629 Len := Length(S); 630 I := 0; 631 Result := ''; 632 while (I <= Len) do begin 633 Inc(I); 634 APos := ReadUntil(I, '<'); 635 Result := Result + Copy(S, I, APos - i); 636 I := ReadUntil(APos + 1, '>'); 637 end; 638 end; 639 640 function PosFromIndex(SubStr: string; Text: string; 641 StartIndex: Integer): Integer; 642 var 643 I, MaxLen: SizeInt; 644 Ptr: PAnsiChar; 645 begin 646 Result := 0; 647 if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit; 648 if Length(SubStr) > 0 then begin 649 MaxLen := Length(Text) - Length(SubStr) + 1; 650 I := StartIndex; 651 Ptr := @Text[StartIndex]; 652 while (I <= MaxLen) do begin 653 if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin 654 Result := I; 655 Exit; 656 end; 657 Inc(I); 658 Inc(Ptr); 659 end; 660 end; 661 end; 662 663 function PosFromIndexReverse(SubStr: string; Text: string; 664 StartIndex: Integer): Integer; 665 var 666 I: SizeInt; 667 Ptr: PAnsiChar; 668 begin 669 Result := 0; 670 if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit; 671 if Length(SubStr) > 0 then begin 672 I := StartIndex; 673 Ptr := @Text[StartIndex]; 674 while (I > 0) 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 Dec(I); 680 Dec(Ptr); 681 end; 682 end; 683 end; 684 685 procedure CopyStringArray(Dest: TStringArray; Source: array of string); 686 var 687 I: Integer; 688 begin 689 SetLength(Dest, Length(Source)); 690 for I := 0 to Length(Dest) - 1 do 691 Dest[I] := Source[I]; 692 end; 693 694 function CombinePaths(Path1, Path2: string): string; 695 begin 696 Result := Path1; 697 if Result <> '' then Result := Result + DirectorySeparator + Path2 698 else Result := Path2; 699 end; 700 701 procedure SortStrings(Strings: TStrings); 702 var 703 Tmp: TStringList; 704 begin 705 Strings.BeginUpdate; 706 try 707 if Strings is TStringList then begin 708 TStringList(Strings).Sort; 709 end else begin 710 Tmp := TStringList.Create; 711 try 712 Tmp.Assign(Strings); 713 Tmp.Sort; 714 Strings.Assign(Tmp); 715 finally 716 Tmp.Free; 717 end; 718 end; 719 finally 720 Strings.EndUpdate; 721 end; 722 end; 561 723 562 724
Note:
See TracChangeset
for help on using the changeset viewer.