Changeset 122 for trunk/Packages/Common/UCommon.pas
- Timestamp:
- Jun 29, 2018, 11:44:07 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UCommon.pas
r114 r122 6 6 7 7 uses 8 {$IFDEF Windows}Windows,{$ENDIF} 9 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, LazFileUtils, 8 {$ifdef Windows}Windows,{$endif} 9 {$ifdef Linux}baseunix,{$endif} 10 Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf, 10 11 FileUtil; //, ShFolder, ShellAPI; 11 12 … … 27 28 unfDNSDomainName = 11); 28 29 30 TFilterMethodMethod = function (FileName: string): Boolean of object; 29 31 var 30 32 ExceptionHandler: TExceptionEvent; … … 48 50 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 49 51 function SplitString(var Text: string; Count: Word): string; 52 function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer; 50 53 function GetBit(Variable: QWord; Index: Byte): Boolean; 54 procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); overload; 51 55 procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload; 52 56 procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload; … … 60 64 procedure OpenWebPage(URL: string); 61 65 procedure OpenFileInShell(FileName: string); 62 procedure ExecuteProgram( CommandLine:string);66 procedure ExecuteProgram(Executable: string; Parameters: array of string); 63 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 procedure SearchFiles(AList: TStrings; Dir: string; 75 FilterMethod: TFilterMethodMethod = nil); 76 function GetStringPart(var Text: string; Separator: string): string; 64 77 65 78 … … 103 116 Path := IncludeTrailingPathDelimiter(APath); 104 117 105 Find := FindFirst( UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);118 Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec); 106 119 while Find = 0 do begin 107 DeleteFile UTF8(Path + UTF8Encode(SearchRec.Name));120 DeleteFile(Path + SearchRec.Name); 108 121 109 122 Find := SysUtils.FindNext(SearchRec); … … 284 297 L: LongWord; 285 298 begin 286 287 299 L := MAX_USERNAME_LENGTH + 2; 288 300 SetLength(Result, L); … … 299 311 end; 300 312 end; 301 313 {$endif} 314 315 function ComputerName: string; 316 {$ifdef mswindows} 317 const 318 INFO_BUFFER_SIZE = 32767; 319 var 320 Buffer : array[0..INFO_BUFFER_SIZE] of WideChar; 321 Ret : DWORD; 322 begin 323 Ret := INFO_BUFFER_SIZE; 324 If (GetComputerNameW(@Buffer[0],Ret)) then begin 325 Result := UTF8Encode(WideString(Buffer)); 326 end 327 else begin 328 Result := 'ERROR_NO_COMPUTERNAME_RETURNED'; 329 end; 330 end; 331 {$endif} 332 {$ifdef unix} 333 var 334 Name: UtsName; 335 begin 336 fpuname(Name); 337 Result := Name.Nodename; 338 end; 339 {$endif} 340 341 {$ifdef windows} 302 342 function LoggedOnUserNameEx(Format: TUserNameFormat): string; 303 343 const … … 336 376 end; 337 377 378 function GetBitCount(Variable: QWord; MaxIndex: Integer): Integer; 379 var 380 I: Integer; 381 begin 382 Result := 0; 383 for I := 0 to MaxIndex - 1 do 384 if ((Variable shr I) and 1) = 1 then Inc(Result); 385 end; 386 338 387 function GetBit(Variable:QWord;Index:Byte):Boolean; 339 388 begin … … 341 390 end; 342 391 392 procedure SetBit(var Variable: Int64; Index: Byte; State: Boolean); 393 begin 394 Variable := (Variable and ((1 shl Index) xor High(QWord))) or (Int64(State) shl Index); 395 end; 396 343 397 procedure SetBit(var Variable:QWord;Index:Byte;State:Boolean); overload; 344 398 begin 345 Variable := (Variable and ((1 shl Index) xor QWord($ffffffffffffffff))) or (QWord(State) shl Index);399 Variable := (Variable and ((1 shl Index) xor High(QWord))) or (QWord(State) shl Index); 346 400 end; 347 401 348 402 procedure SetBit(var Variable:Cardinal;Index:Byte;State:Boolean); overload; 349 403 begin 350 Variable := (Variable and ((1 shl Index) xor Cardinal($ffffffff))) or (Cardinal(State) shl Index);404 Variable := (Variable and ((1 shl Index) xor High(Cardinal))) or (Cardinal(State) shl Index); 351 405 end; 352 406 353 407 procedure SetBit(var Variable:Word;Index:Byte;State:Boolean); overload; 354 408 begin 355 Variable := (Variable and ((1 shl Index) xor Word($ffff))) or (Word(State) shl Index);409 Variable := (Variable and ((1 shl Index) xor High(Word))) or (Word(State) shl Index); 356 410 end; 357 411 … … 379 433 end; 380 434 381 procedure ExecuteProgram( CommandLine:string);435 procedure ExecuteProgram(Executable: string; Parameters: array of string); 382 436 var 383 437 Process: TProcess; 438 I: Integer; 384 439 begin 385 440 try 386 441 Process := TProcess.Create(nil); 387 Process.CommandLine := CommandLine; 442 Process.Executable := Executable; 443 for I := 0 to Length(Parameters) - 1 do 444 Process.Parameters.Add(Parameters[I]); 388 445 Process.Options := [poNoConsole]; 389 446 Process.Execute; … … 400 457 401 458 procedure OpenWebPage(URL: string); 402 var403 Process: TProcess;404 Browser, Params: string;405 459 begin 406 460 OpenURL(URL); 407 {try 408 Process := TProcess.Create(nil); 409 Browser := ''; 410 //FindDefaultBrowser(Browser, Params); 411 //Process.Executable := Browser; 412 //Process.Parameters.Add(Format(Params, [ApplicationInfo.HomePage]); 413 Process.CommandLine := 'cmd.exe /c start ' + URL; 414 Process.Options := [poNoConsole]; 415 Process.Execute; 461 end; 462 463 procedure OpenFileInShell(FileName: string); 464 begin 465 ExecuteProgram('cmd.exe', ['/c', 'start', FileName]); 466 end; 467 468 function RemoveQuotes(Text: string): string; 469 begin 470 Result := Text; 471 if (Pos('"', Text) = 1) and (Text[Length(Text)] = '"') then 472 Result := Copy(Text, 2, Length(Text) - 2); 473 end; 474 475 function OccurenceOfChar(What: Char; Where: string): Integer; 476 var 477 I: Integer; 478 begin 479 Result := 0; 480 for I := 1 to Length(Where) do 481 if Where[I] = What then Inc(Result); 482 end; 483 484 function GetDirCount(Dir: string): Integer; 485 begin 486 Result := OccurenceOfChar(DirectorySeparator, Dir); 487 if Copy(Dir, Length(Dir), 1) = DirectorySeparator then 488 Dec(Result); 489 end; 490 491 function MergeArray(A, B: array of string): TArrayOfString; 492 var 493 I: Integer; 494 begin 495 SetLength(Result, Length(A) + Length(B)); 496 for I := 0 to Length(A) - 1 do 497 Result[I] := A[I]; 498 for I := 0 to Length(B) - 1 do 499 Result[Length(A) + I] := B[I]; 500 end; 501 502 function LoadFileToStr(const FileName: TFileName): AnsiString; 503 var 504 FileStream: TFileStream; 505 Read: Integer; 506 begin 507 Result := ''; 508 FileStream := TFileStream.Create(FileName, fmOpenRead); 509 try 510 if FileStream.Size > 0 then begin 511 SetLength(Result, FileStream.Size); 512 Read := FileStream.Read(Pointer(Result)^, FileStream.Size); 513 SetLength(Result, Read); 514 end; 416 515 finally 417 Process.Free; 418 end;} 419 end; 420 421 procedure OpenFileInShell(FileName: string); 422 begin 423 ExecuteProgram('cmd.exe /c start "' + FileName + '"'); 424 end; 516 FileStream.Free; 517 end; 518 end; 519 520 function DefaultSearchFilter(const FileName: string): Boolean; 521 begin 522 Result := True; 523 end; 524 525 procedure SearchFiles(AList: TStrings; Dir: string; 526 FilterMethod: TFilterMethodMethod = nil); 527 var 528 SR: TSearchRec; 529 begin 530 Dir := IncludeTrailingPathDelimiter(Dir); 531 if FindFirst(Dir + '*', faAnyFile, SR) = 0 then 532 try 533 repeat 534 if (SR.Name = '.') or (SR.Name = '..') or (Assigned(FilterMethod) and (not FilterMethod(SR.Name) or 535 not FilterMethod(Copy(Dir, 3, Length(Dir)) + SR.Name))) then Continue; 536 AList.Add(Dir + SR.Name); 537 if (SR.Attr and faDirectory) <> 0 then 538 SearchFiles(AList, Dir + SR.Name, FilterMethod); 539 until FindNext(SR) <> 0; 540 finally 541 FindClose(SR); 542 end; 543 end; 544 545 function GetStringPart(var Text: string; Separator: string): string; 546 var 547 P: Integer; 548 begin 549 P := Pos(Separator, Text); 550 if P > 0 then begin 551 Result := Copy(Text, 1, P - 1); 552 Delete(Text, 1, P - 1 + Length(Separator)); 553 end else begin 554 Result := Text; 555 Text := ''; 556 end; 557 Result := Trim(Result); 558 Text := Trim(Text); 559 end; 560 561 425 562 426 563 initialization
Note:
See TracChangeset
for help on using the changeset viewer.