Changeset 161 for trunk/Packages/Common


Ignore:
Timestamp:
Aug 20, 2024, 12:20:49 AM (3 months ago)
Author:
chronos
Message:
  • Fixed: All targets compilation and run.
Location:
trunk/Packages/Common
Files:
2 added
1 edited

Legend:

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

    r153 r161  
    5555function EndsWith(Text, What: string): Boolean;
    5656function Explode(Separator: Char; Data: string): TStringArray;
    57 procedure ExecuteProgram(Executable: string; Parameters: array of string);
     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 = '');
    5862procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
    5963procedure FreeThenNil(var Obj);
     
    6367function GetBit(Variable: QWord; Index: Byte): Boolean;
    6468function GetStringPart(var Text: string; Separator: string): string;
     69function GetEnvironmentVariables: TStringArray;
    6570function GenerateNewName(OldName: string): string;
    6671function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    6772function IntToBin(Data: Int64; Count: Byte): string;
    68 function Implode(Separator: string; List: TList<string>): string;
    69 function Implode(Separator: string; List: TStringList; Around: string = ''): 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;
    7076function LastPos(const SubStr: String; const S: String): Integer;
    7177function LoadFileToStr(const FileName: TFileName): AnsiString;
     
    98104implementation
    99105
     106resourcestring
     107  SExecutionError = 'Excution error: %s (exit code: %d)';
     108
    100109function StartsWith(Text, What: string): Boolean;
    101110begin
     
    108117end;
    109118
    110 function BinToInt(BinStr : string) : Int64;
    111 var
    112   i : byte;
    113   RetVar : Int64;
     119function BinToInt(BinStr: string): Int64;
     120var
     121  I: Byte;
     122  RetVar: Int64;
    114123begin
    115124  BinStr := UpperCase(BinStr);
    116   if BinStr[length(BinStr)] = 'B' then Delete(BinStr,length(BinStr),1);
     125  if BinStr[length(BinStr)] = 'B' then Delete(BinStr, Length(BinStr), 1);
    117126  RetVar := 0;
    118   for i := 1 to length(BinStr) do begin
    119     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
    120129      RetVar := 0;
    121130      Break;
    122131    end;
    123     RetVar := (RetVar shl 1) + (byte(BinStr[i]) and 1) ;
     132    RetVar := (RetVar shl 1) + (Byte(BinStr[I]) and 1);
    124133  end;
    125134
     
    136145  end;
    137146end;
    138 
    139147
    140148procedure DeleteFiles(APath, AFileSpec: string);
     
    154162  FindClose(SearchRec);
    155163end;
    156 
    157164
    158165function GetFileFilterItemExt(Filter: string; Index: Integer): string;
     
    177184  if FileExt <> '.*' then
    178185    FileDialog.FileName := ChangeFileExt(FileDialog.FileName, FileExt)
     186end;
     187
     188function GetEnvironmentVariables: TStringArray;
     189var
     190  I: Integer;
     191begin
     192  SetLength(Result, GetEnvironmentVariableCount);
     193  for I := 0 to GetEnvironmentVariableCount - 1 do
     194    Result[I] := GetEnvironmentString(I);
    179195end;
    180196
     
    219235end;*)
    220236
     237function Implode(Separator: string; List: array of string): string;
     238var
     239  I: Integer;
     240begin
     241  Result := '';
     242  for I := 0 to Length(List) - 1 do begin
     243    Result := Result + List[I];
     244    if I < Length(List) - 1 then Result := Result + Separator;
     245  end;
     246end;
     247
    221248function Implode(Separator: string; List: TStringList; Around: string = ''): string;
    222249var
     
    494521end;
    495522
    496 procedure ExecuteProgram(Executable: string; Parameters: array of string);
     523procedure ExecuteProgram(Executable: string; Parameters: array of string;
     524  Environment: array of string; CurrentDirectory: string = '');
    497525var
    498526  Process: TProcess;
    499527  I: Integer;
    500528begin
     529  Process := TProcess.Create(nil);
    501530  try
    502     Process := TProcess.Create(nil);
    503531    Process.Executable := Executable;
    504532    for I := 0 to Length(Parameters) - 1 do
    505533      Process.Parameters.Add(Parameters[I]);
     534    for I := 0 to Length(Environment) - 1 do
     535      Process.Environment.Add(Environment[I]);
     536    Process.CurrentDirectory := CurrentDirectory;
     537    Process.ShowWindow := swoHIDE;
    506538    Process.Options := [poNoConsole];
    507539    Process.Execute;
     
    511543end;
    512544
     545procedure ExecuteProgramOutput(Executable: string; Parameters: array of string;
     546  Environment: array of string; out Output, Error: string; out ExitCode: Integer;
     547  CurrentDirectory: string);
     548var
     549  Process: TProcess;
     550  I: Integer;
     551  ReadCount: Integer;
     552  Buffer: string;
     553const
     554  BufferSize = 1000;
     555begin
     556  Process := TProcess.Create(nil);
     557  try
     558    Process.Executable := Executable;
     559    for I := 0 to Length(Parameters) - 1 do
     560      Process.Parameters.Add(Parameters[I]);
     561    for I := 0 to Length(Environment) - 1 do
     562      Process.Environment.Add(Environment[I]);
     563    Process.CurrentDirectory := CurrentDirectory;
     564    Process.ShowWindow := swoHIDE;
     565    Process.Options := [poNoConsole, poUsePipes];
     566    Process.Execute;
     567
     568    Output := '';
     569    Error := '';
     570    Buffer := '';
     571    SetLength(Buffer, BufferSize);
     572    while Process.Running do begin
     573      if Process.Output.NumBytesAvailable > 0 then begin
     574        ReadCount := Process.Output.Read(Buffer[1], Length(Buffer));
     575        Output := Output + Copy(Buffer, 1, ReadCount);
     576      end;
     577
     578      if Process.Stderr.NumBytesAvailable > 0 then begin
     579        ReadCount := Process.Stderr.Read(Buffer[1], Length(Buffer));
     580        Error := Error + Copy(Buffer, 1, ReadCount)
     581      end;
     582
     583      Sleep(10);
     584    end;
     585
     586    if Process.Output.NumBytesAvailable > 0 then begin
     587      ReadCount := Process.Output.Read(Buffer[1], Length(Buffer));
     588      Output := Output + Copy(Buffer, 1, ReadCount);
     589    end;
     590
     591    if Process.Stderr.NumBytesAvailable > 0 then begin
     592      ReadCount := Process.Stderr.Read(Buffer[1], Length(Buffer));
     593      Error := Error + Copy(Buffer, 1, ReadCount);
     594    end;
     595
     596    ExitCode := Process.ExitCode;
     597
     598    if (ExitCode <> 0) or (Error <> '') then
     599      raise Exception.Create(Format(SExecutionError, [Error, ExitCode]));
     600  finally
     601    Process.Free;
     602  end;
     603end;
     604
    513605procedure FreeThenNil(var Obj);
    514606begin
     
    529621procedure OpenFileInShell(FileName: string);
    530622begin
    531   ExecuteProgram('cmd.exe', ['/c', 'start', FileName]);
     623  ExecuteProgram('cmd.exe', ['/c', 'start', FileName], []);
    532624end;
    533625
Note: See TracChangeset for help on using the changeset viewer.