Changeset 219 for trunk/Packages/Common/Common.pas
- Timestamp:
- Jan 17, 2025, 9:05:54 PM (4 days ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Common.pas
r218 r219 1 unit UCommon; 2 3 {$mode delphi} 1 unit Common; 4 2 5 3 interface … … 8 6 {$IFDEF WINDOWS}Windows,{$ENDIF} 9 7 {$IFDEF UNIX}baseunix,{$ENDIF} 10 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, 11 FileUtil ; //, ShFolder, ShellAPI;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 … … 35 32 DLLHandle1: HModule; 36 33 37 {$IFDEF WINDOWS} 38 GetUserNameEx: procedure (NameFormat: DWORD; 39 lpNameBuffer: LPSTR; nSize: PULONG); stdcall; 40 {$ENDIF} 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); 41 43 42 44 function AddLeadingZeroes(const aNumber, Length : integer) : string; … … 51 53 function ComputerName: string; 52 54 procedure DeleteFiles(APath, AFileSpec: string); 53 procedure ExecuteProgram(Executable: string; Parameters: array of string); 55 function EndsWith(Text, What: string): Boolean; 56 function Explode(Separator: Char; Data: string): TStringArray; 57 procedure ExecuteProgram(Executable: string; Parameters: array of string; 58 Environment: array of string; CurrentDirectory: string = ''); 59 procedure ExecuteProgramOutput(Executable: string; Parameters: array of string; 60 Environment: array of string; out Output, Error: string; 61 out ExitCode: Integer; CurrentDirectory: string = ''); 54 62 procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog); 55 63 procedure FreeThenNil(var Obj); … … 59 67 function GetBit(Variable: QWord; Index: Byte): Boolean; 60 68 function GetStringPart(var Text: string; Separator: string): string; 69 function GetEnvironmentVariables: TStringArray; 61 70 function GenerateNewName(OldName: string): string; 62 71 function GetFileFilterItemExt(Filter: string; Index: Integer): string; 63 72 function IntToBin(Data: Int64; Count: Byte): string; 73 function Implode(Separator: string; List: TList<string>): string; overload; 74 function Implode(Separator: string; List: array of string): string; overload; 75 function Implode(Separator: string; List: TStringList; Around: string = ''): string; overload; 64 76 function LastPos(const SubStr: String; const S: String): Integer; 65 77 function LoadFileToStr(const FileName: TFileName): AnsiString; 66 78 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 67 function MergeArray(A, B: array of string): T ArrayOfString;79 function MergeArray(A, B: array of string): TStringArray; 68 80 function OccurenceOfChar(What: Char; Where: string): Integer; 69 81 procedure OpenWebPage(URL: string); 82 procedure OpenEmail(Email: string); 70 83 procedure OpenFileInShell(FileName: string); 71 84 function PosFromIndex(SubStr: string; Text: string; … … 81 94 procedure SearchFiles(AList: TStrings; Dir: string; 82 95 FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil); 96 procedure SortStrings(Strings: TStrings); 83 97 function SplitString(var Text: string; Count: Word): string; 84 98 function StripTags(const S: string): string; 85 function TryHexToInt(Data: string; var Value: Integer): Boolean;86 function Try BinToInt(Data: string; varValue: Integer): Boolean;87 procedure SortStrings(Strings: TStrings);99 function StartsWith(Text, What: string): Boolean; 100 function TryHexToInt(Data: string; out Value: Integer): Boolean; 101 function TryBinToInt(Data: string; out Value: Integer): Boolean; 88 102 89 103 90 104 implementation 91 105 92 function BinToInt(BinStr : string) : Int64; 93 var 94 i : byte; 95 RetVar : Int64; 106 resourcestring 107 SExecutionError = 'Excution error: %s (exit code: %d)'; 108 109 function StartsWith(Text, What: string): Boolean; 110 begin 111 Result := Copy(Text, 1, Length(Text)) = What; 112 end; 113 114 function EndsWith(Text, What: string): Boolean; 115 begin 116 Result := Copy(Text, Length(Text) - Length(What) + 1, MaxInt) = What; 117 end; 118 119 function BinToInt(BinStr: string): Int64; 120 var 121 I: Byte; 122 RetVar: Int64; 96 123 begin 97 124 BinStr := UpperCase(BinStr); 98 if BinStr[length(BinStr)] = 'B' then Delete(BinStr, length(BinStr),1);125 if BinStr[length(BinStr)] = 'B' then Delete(BinStr, Length(BinStr), 1); 99 126 RetVar := 0; 100 for i := 1 to length(BinStr) do begin101 if not (BinStr[ i] in ['0','1']) then begin127 for I := 1 to Length(BinStr) do begin 128 if not (BinStr[I] in ['0','1']) then begin 102 129 RetVar := 0; 103 130 Break; 104 131 end; 105 RetVar := (RetVar shl 1) + ( byte(BinStr[i]) and 1);132 RetVar := (RetVar shl 1) + (Byte(BinStr[I]) and 1); 106 133 end; 107 134 … … 118 145 end; 119 146 end; 120 121 147 122 148 procedure DeleteFiles(APath, AFileSpec: string); … … 136 162 FindClose(SearchRec); 137 163 end; 138 139 164 140 165 function GetFileFilterItemExt(Filter: string; Index: Integer): string; … … 159 184 if FileExt <> '.*' then 160 185 FileDialog.FileName := ChangeFileExt(FileDialog.FileName, FileExt) 186 end; 187 188 function GetEnvironmentVariables: TStringArray; 189 var 190 I: Integer; 191 begin 192 Result := Default(TStringArray); 193 SetLength(Result, GetEnvironmentVariableCount); 194 for I := 0 to GetEnvironmentVariableCount - 1 do 195 Result[I] := GetEnvironmentString(I); 161 196 end; 162 197 … … 201 236 end;*) 202 237 238 function Implode(Separator: string; List: array of string): string; 239 var 240 I: Integer; 241 begin 242 Result := ''; 243 for I := 0 to Length(List) - 1 do begin 244 Result := Result + List[I]; 245 if I < Length(List) - 1 then Result := Result + Separator; 246 end; 247 end; 248 249 function Implode(Separator: string; List: TStringList; Around: string = ''): string; 250 var 251 I: Integer; 252 begin 253 Result := ''; 254 for I := 0 to List.Count - 1 do begin 255 Result := Result + Around + List[I] + Around; 256 if I < List.Count - 1 then Result := Result + Separator; 257 end; 258 end; 259 203 260 function LastPos(const SubStr: String; const S: String): Integer; 204 261 begin … … 246 303 end; 247 304 248 function TryHexToInt(Data: string; varValue: Integer): Boolean;305 function TryHexToInt(Data: string; out Value: Integer): Boolean; 249 306 var 250 307 I: Integer; … … 262 319 end; 263 320 264 function TryBinToInt(Data: string; varValue: Integer): Boolean;321 function TryBinToInt(Data: string; out Value: Integer): Boolean; 265 322 var 266 323 I: Integer; … … 290 347 end; 291 348 292 function Explode(Separator: char; Data: string): TArrayOfString; 293 begin 294 Result := nil; 295 SetLength(Result, 0); 296 while Pos(Separator, Data) > 0 do begin 349 function Explode(Separator: Char; Data: string): TStringArray; 350 var 351 Index: Integer; 352 begin 353 Result := Default(TStringArray); 354 repeat 355 Index := Pos(Separator, Data); 356 if Index > 0 then begin 357 SetLength(Result, Length(Result) + 1); 358 Result[High(Result)] := Copy(Data, 1, Index - 1); 359 Delete(Data, 1, Index); 360 end else Break; 361 until False; 362 if Data <> '' then begin 297 363 SetLength(Result, Length(Result) + 1); 298 Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1); 299 Delete(Data, 1, Pos(Separator, Data)); 300 end; 301 SetLength(Result, Length(Result) + 1); 302 Result[High(Result)] := Data; 303 end; 304 305 {$IFDEF Windows} 364 Result[High(Result)] := Data; 365 end; 366 end; 367 368 function Implode(Separator: string; List: TList<string>): string; 369 var 370 I: Integer; 371 begin 372 Result := ''; 373 for I := 0 to List.Count - 1 do begin 374 Result := Result + List[I]; 375 if I < List.Count - 1 then Result := Result + Separator; 376 end; 377 end; 378 379 {$IFDEF WINDOWS} 306 380 function GetUserName: string; 307 381 const … … 311 385 begin 312 386 L := MAX_USERNAME_LENGTH + 2; 387 Result := Default(string); 313 388 SetLength(Result, L); 314 389 if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin … … 324 399 end; 325 400 end; 326 {$ endif}401 {$ENDIF} 327 402 328 403 function ComputerName: string; 329 {$ ifdef mswindows}404 {$IFDEF WINDOWS} 330 405 const 331 406 INFO_BUFFER_SIZE = 32767; … … 342 417 end; 343 418 end; 344 {$ endif}345 {$ ifdef unix}419 {$ENDIF} 420 {$IFDEF UNIX} 346 421 var 347 422 Name: UtsName; … … 351 426 Result := Name.Nodename; 352 427 end; 353 {$ endif}354 355 {$ ifdef windows}428 {$ENDIF} 429 430 {$IFDEF WINDOWS} 356 431 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 357 432 const … … 431 506 procedure LoadLibraries; 432 507 begin 433 {$IFDEF W indows}508 {$IFDEF WINDOWS} 434 509 DLLHandle1 := LoadLibrary('secur32.dll'); 435 510 if DLLHandle1 <> 0 then … … 442 517 procedure FreeLibraries; 443 518 begin 444 {$IFDEF W indows}519 {$IFDEF WINDOWS} 445 520 if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1); 446 521 {$ENDIF} 447 522 end; 448 523 449 procedure ExecuteProgram(Executable: string; Parameters: array of string); 524 procedure ExecuteProgram(Executable: string; Parameters: array of string; 525 Environment: array of string; CurrentDirectory: string = ''); 450 526 var 451 527 Process: TProcess; 452 528 I: Integer; 453 529 begin 530 Process := TProcess.Create(nil); 454 531 try 455 Process := TProcess.Create(nil);456 532 Process.Executable := Executable; 457 533 for I := 0 to Length(Parameters) - 1 do 458 534 Process.Parameters.Add(Parameters[I]); 535 for I := 0 to Length(Environment) - 1 do 536 Process.Environment.Add(Environment[I]); 537 Process.CurrentDirectory := CurrentDirectory; 538 Process.ShowWindow := swoHIDE; 459 539 Process.Options := [poNoConsole]; 460 540 Process.Execute; … … 464 544 end; 465 545 546 procedure ExecuteProgramOutput(Executable: string; Parameters: array of string; 547 Environment: array of string; out Output, Error: string; out ExitCode: Integer; 548 CurrentDirectory: string); 549 var 550 Process: TProcess; 551 I: Integer; 552 ReadCount: Integer; 553 Buffer: string; 554 const 555 BufferSize = 1000; 556 begin 557 Process := TProcess.Create(nil); 558 try 559 Process.Executable := Executable; 560 for I := 0 to Length(Parameters) - 1 do 561 Process.Parameters.Add(Parameters[I]); 562 for I := 0 to Length(Environment) - 1 do 563 Process.Environment.Add(Environment[I]); 564 Process.CurrentDirectory := CurrentDirectory; 565 Process.ShowWindow := swoHIDE; 566 Process.Options := [poNoConsole, poUsePipes]; 567 Process.Execute; 568 569 Output := ''; 570 Error := ''; 571 Buffer := ''; 572 SetLength(Buffer, BufferSize); 573 while Process.Running do begin 574 if Process.Output.NumBytesAvailable > 0 then begin 575 ReadCount := Process.Output.Read(Buffer[1], Length(Buffer)); 576 Output := Output + Copy(Buffer, 1, ReadCount); 577 end; 578 579 if Process.Stderr.NumBytesAvailable > 0 then begin 580 ReadCount := Process.Stderr.Read(Buffer[1], Length(Buffer)); 581 Error := Error + Copy(Buffer, 1, ReadCount) 582 end; 583 584 Sleep(10); 585 end; 586 587 if Process.Output.NumBytesAvailable > 0 then begin 588 ReadCount := Process.Output.Read(Buffer[1], Length(Buffer)); 589 Output := Output + Copy(Buffer, 1, ReadCount); 590 end; 591 592 if Process.Stderr.NumBytesAvailable > 0 then begin 593 ReadCount := Process.Stderr.Read(Buffer[1], Length(Buffer)); 594 Error := Error + Copy(Buffer, 1, ReadCount); 595 end; 596 597 ExitCode := Process.ExitCode; 598 599 if (ExitCode <> 0) or (Error <> '') then 600 raise Exception.Create(Format(SExecutionError, [Output + Error, ExitCode])); 601 finally 602 Process.Free; 603 end; 604 end; 605 466 606 procedure FreeThenNil(var Obj); 467 607 begin … … 475 615 end; 476 616 617 procedure OpenEmail(Email: string); 618 begin 619 OpenURL('mailto:' + Email); 620 end; 621 477 622 procedure OpenFileInShell(FileName: string); 478 623 begin 479 ExecuteProgram('cmd.exe', ['/c', 'start', FileName] );624 ExecuteProgram('cmd.exe', ['/c', 'start', FileName], []); 480 625 end; 481 626 … … 503 648 end; 504 649 505 function MergeArray(A, B: array of string): T ArrayOfString;506 var 507 I: Integer; 508 begin 509 Result := Default(T ArrayOfString);650 function MergeArray(A, B: array of string): TStringArray; 651 var 652 I: Integer; 653 begin 654 Result := Default(TStringArray); 510 655 SetLength(Result, Length(A) + Length(B)); 511 656 for I := 0 to Length(A) - 1 do
Note:
See TracChangeset
for help on using the changeset viewer.