Ignore:
Timestamp:
Jan 17, 2025, 9:05:54 PM (4 days ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Remove U prefix from unit names.
  • Modified: Use Gneeric.Collections instead of fgl.
  • Modified: Do not use global form variables.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Common.pas

    r218 r219  
    1 unit UCommon;
    2 
    3 {$mode delphi}
     1unit Common;
    42
    53interface
     
    86  {$IFDEF WINDOWS}Windows,{$ENDIF}
    97  {$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;
    1210
    1311type
    1412  TArrayOfByte = array of Byte;
    15   TArrayOfString = array of string;
    1613  TExceptionEvent = procedure(Sender: TObject; E: Exception) of object;
    1714
     
    3532  DLLHandle1: HModule;
    3633
    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
     39const
     40  clLightBlue = TColor($FF8080);
     41  clLightGreen = TColor($80FF80);
     42  clLightRed = TColor($8080FF);
    4143
    4244function AddLeadingZeroes(const aNumber, Length : integer) : string;
     
    5153function ComputerName: string;
    5254procedure DeleteFiles(APath, AFileSpec: string);
    53 procedure ExecuteProgram(Executable: string; Parameters: array of string);
     55function EndsWith(Text, What: string): Boolean;
     56function Explode(Separator: Char; Data: string): TStringArray;
     57procedure ExecuteProgram(Executable: string; Parameters: array of string;
     58  Environment: array of string; CurrentDirectory: string = '');
     59procedure ExecuteProgramOutput(Executable: string; Parameters: array of string;
     60  Environment: array of string; out Output, Error: string;
     61  out ExitCode: Integer; CurrentDirectory: string = '');
    5462procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
    5563procedure FreeThenNil(var Obj);
     
    5967function GetBit(Variable: QWord; Index: Byte): Boolean;
    6068function GetStringPart(var Text: string; Separator: string): string;
     69function GetEnvironmentVariables: TStringArray;
    6170function GenerateNewName(OldName: string): string;
    6271function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    6372function IntToBin(Data: Int64; Count: Byte): string;
     73function Implode(Separator: string; List: TList<string>): string; overload;
     74function Implode(Separator: string; List: array of string): string; overload;
     75function Implode(Separator: string; List: TStringList; Around: string = ''): string; overload;
    6476function LastPos(const SubStr: String; const S: String): Integer;
    6577function LoadFileToStr(const FileName: TFileName): AnsiString;
    6678function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    67 function MergeArray(A, B: array of string): TArrayOfString;
     79function MergeArray(A, B: array of string): TStringArray;
    6880function OccurenceOfChar(What: Char; Where: string): Integer;
    6981procedure OpenWebPage(URL: string);
     82procedure OpenEmail(Email: string);
    7083procedure OpenFileInShell(FileName: string);
    7184function PosFromIndex(SubStr: string; Text: string;
     
    8194procedure SearchFiles(AList: TStrings; Dir: string;
    8295  FilterMethod: TFilterMethod = nil; FileNameMethod: TFileNameMethod = nil);
     96procedure SortStrings(Strings: TStrings);
    8397function SplitString(var Text: string; Count: Word): string;
    8498function StripTags(const S: string): string;
    85 function TryHexToInt(Data: string; var Value: Integer): Boolean;
    86 function TryBinToInt(Data: string; var Value: Integer): Boolean;
    87 procedure SortStrings(Strings: TStrings);
     99function StartsWith(Text, What: string): Boolean;
     100function TryHexToInt(Data: string; out Value: Integer): Boolean;
     101function TryBinToInt(Data: string; out Value: Integer): Boolean;
    88102
    89103
    90104implementation
    91105
    92 function BinToInt(BinStr : string) : Int64;
    93 var
    94   i : byte;
    95   RetVar : Int64;
     106resourcestring
     107  SExecutionError = 'Excution error: %s (exit code: %d)';
     108
     109function StartsWith(Text, What: string): Boolean;
     110begin
     111  Result := Copy(Text, 1, Length(Text)) = What;
     112end;
     113
     114function EndsWith(Text, What: string): Boolean;
     115begin
     116  Result := Copy(Text, Length(Text) - Length(What) + 1, MaxInt) = What;
     117end;
     118
     119function BinToInt(BinStr: string): Int64;
     120var
     121  I: Byte;
     122  RetVar: Int64;
    96123begin
    97124  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);
    99126  RetVar := 0;
    100   for i := 1 to length(BinStr) do begin
    101     if not (BinStr[i] in ['0','1']) then begin
     127  for I := 1 to Length(BinStr) do begin
     128    if not (BinStr[I] in ['0','1']) then begin
    102129      RetVar := 0;
    103130      Break;
    104131    end;
    105     RetVar := (RetVar shl 1) + (byte(BinStr[i]) and 1) ;
     132    RetVar := (RetVar shl 1) + (Byte(BinStr[I]) and 1);
    106133  end;
    107134
     
    118145  end;
    119146end;
    120 
    121147
    122148procedure DeleteFiles(APath, AFileSpec: string);
     
    136162  FindClose(SearchRec);
    137163end;
    138 
    139164
    140165function GetFileFilterItemExt(Filter: string; Index: Integer): string;
     
    159184  if FileExt <> '.*' then
    160185    FileDialog.FileName := ChangeFileExt(FileDialog.FileName, FileExt)
     186end;
     187
     188function GetEnvironmentVariables: TStringArray;
     189var
     190  I: Integer;
     191begin
     192  Result := Default(TStringArray);
     193  SetLength(Result, GetEnvironmentVariableCount);
     194  for I := 0 to GetEnvironmentVariableCount - 1 do
     195    Result[I] := GetEnvironmentString(I);
    161196end;
    162197
     
    201236end;*)
    202237
     238function Implode(Separator: string; List: array of string): string;
     239var
     240  I: Integer;
     241begin
     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;
     247end;
     248
     249function Implode(Separator: string; List: TStringList; Around: string = ''): string;
     250var
     251  I: Integer;
     252begin
     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;
     258end;
     259
    203260function LastPos(const SubStr: String; const S: String): Integer;
    204261begin
     
    246303end;
    247304
    248 function TryHexToInt(Data: string; var Value: Integer): Boolean;
     305function TryHexToInt(Data: string; out Value: Integer): Boolean;
    249306var
    250307  I: Integer;
     
    262319end;
    263320
    264 function TryBinToInt(Data: string; var Value: Integer): Boolean;
     321function TryBinToInt(Data: string; out Value: Integer): Boolean;
    265322var
    266323  I: Integer;
     
    290347end;
    291348
    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
     349function Explode(Separator: Char; Data: string): TStringArray;
     350var
     351  Index: Integer;
     352begin
     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
    297363    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;
     366end;
     367
     368function Implode(Separator: string; List: TList<string>): string;
     369var
     370  I: Integer;
     371begin
     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;
     377end;
     378
     379{$IFDEF WINDOWS}
    306380function GetUserName: string;
    307381const
     
    311385begin
    312386  L := MAX_USERNAME_LENGTH + 2;
     387  Result := Default(string);
    313388  SetLength(Result, L);
    314389  if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
     
    324399  end;
    325400end;
    326 {$endif}
     401{$ENDIF}
    327402
    328403function ComputerName: string;
    329 {$ifdef mswindows}
     404{$IFDEF WINDOWS}
    330405const
    331406 INFO_BUFFER_SIZE = 32767;
     
    342417  end;
    343418end;
    344 {$endif}
    345 {$ifdef unix}
     419{$ENDIF}
     420{$IFDEF UNIX}
    346421var
    347422  Name: UtsName;
     
    351426  Result := Name.Nodename;
    352427end;
    353 {$endif}
    354 
    355 {$ifdef windows}
     428{$ENDIF}
     429
     430{$IFDEF WINDOWS}
    356431function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    357432const
     
    431506procedure LoadLibraries;
    432507begin
    433   {$IFDEF Windows}
     508  {$IFDEF WINDOWS}
    434509  DLLHandle1 := LoadLibrary('secur32.dll');
    435510  if DLLHandle1 <> 0 then
     
    442517procedure FreeLibraries;
    443518begin
    444   {$IFDEF Windows}
     519  {$IFDEF WINDOWS}
    445520  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
    446521  {$ENDIF}
    447522end;
    448523
    449 procedure ExecuteProgram(Executable: string; Parameters: array of string);
     524procedure ExecuteProgram(Executable: string; Parameters: array of string;
     525  Environment: array of string; CurrentDirectory: string = '');
    450526var
    451527  Process: TProcess;
    452528  I: Integer;
    453529begin
     530  Process := TProcess.Create(nil);
    454531  try
    455     Process := TProcess.Create(nil);
    456532    Process.Executable := Executable;
    457533    for I := 0 to Length(Parameters) - 1 do
    458534      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;
    459539    Process.Options := [poNoConsole];
    460540    Process.Execute;
     
    464544end;
    465545
     546procedure ExecuteProgramOutput(Executable: string; Parameters: array of string;
     547  Environment: array of string; out Output, Error: string; out ExitCode: Integer;
     548  CurrentDirectory: string);
     549var
     550  Process: TProcess;
     551  I: Integer;
     552  ReadCount: Integer;
     553  Buffer: string;
     554const
     555  BufferSize = 1000;
     556begin
     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;
     604end;
     605
    466606procedure FreeThenNil(var Obj);
    467607begin
     
    475615end;
    476616
     617procedure OpenEmail(Email: string);
     618begin
     619  OpenURL('mailto:' + Email);
     620end;
     621
    477622procedure OpenFileInShell(FileName: string);
    478623begin
    479   ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
     624  ExecuteProgram('cmd.exe', ['/c', 'start', FileName], []);
    480625end;
    481626
     
    503648end;
    504649
    505 function MergeArray(A, B: array of string): TArrayOfString;
    506 var
    507   I: Integer;
    508 begin
    509   Result := Default(TArrayOfString);
     650function MergeArray(A, B: array of string): TStringArray;
     651var
     652  I: Integer;
     653begin
     654  Result := Default(TStringArray);
    510655  SetLength(Result, Length(A) + Length(B));
    511656  for I := 0 to Length(A) - 1 do
Note: See TracChangeset for help on using the changeset viewer.