This commit is contained in:
Basile Burg 2015-07-14 17:53:29 +02:00
parent 4dbfe58326
commit 8aa027b79f
1 changed files with 42 additions and 22 deletions

View File

@ -166,7 +166,7 @@ type
function listAsteriskPath(const aPath: string; aList: TStrings; someExts: TStrings = nil): boolean; function listAsteriskPath(const aPath: string; aList: TStrings; someExts: TStrings = nil): boolean;
(** (**
* Lets the shell open a file. * Lets the shell open a file
*) *)
function shellOpen(const aFilename: string): boolean; function shellOpen(const aFilename: string): boolean;
@ -176,19 +176,19 @@ type
function exeInSysPath(anExeName: string): boolean; function exeInSysPath(anExeName: string): boolean;
(** (**
* Returns the full path to anExeName. Works if exeInSysPath() returns true. * Returns the full to anExeName. Works if exeInSysPath().
*) *)
function exeFullName(anExeName: string): string; function exeFullName(anExeName: string): string;
(** (**
* Clears then fills aList with aProcess output stream. * Clears then fills aList with aProcess output stream.
*) *)
procedure processOutputToStrings(proc: TProcess; strings: TStrings); procedure processOutputToStrings(aProcess: TProcess; var aList: TStringList);
(** (**
* Copies process output to stream. Existing stream content is not cleared. * Copy available process output to a stream.
*) *)
procedure processOutputToStream(proc: TProcess; stream: TMemoryStream); procedure processOutputToStream(aProcess: TProcess; output: TMemoryStream);
(** (**
* Terminates and frees aProcess. * Terminates and frees aProcess.
@ -710,41 +710,61 @@ begin
end; end;
end; end;
procedure processOutputToStrings(proc: TProcess; strings: TStrings); procedure processOutputToStrings(aProcess: TProcess; var aList: TStringList);
var var
str: TMemoryStream; str: TMemoryStream;
sum: Integer;
cnt: Integer;
buffSz: Integer;
begin begin
if not (poUsePipes in proc.Options) then if not (poUsePipes in aProcess.Options) then
exit; exit;
// //
// note: aList.LoadFromStream() does not work, lines can be split, which breaks message parsing (e.g filename detector).
//
sum := 0;
str := TMemoryStream.Create; str := TMemoryStream.Create;
try try
processOutputToStream(proc, str); buffSz := aProcess.PipeBufferSize;
str.Position := 0; // temp fix: messages are cut if the TAsyncProcess version is used on simple TProcess.
strings.LoadFromStream(str); if aProcess is TAsyncProcess then begin
while aProcess.Output.NumBytesAvailable <> 0 do begin
str.SetSize(sum + buffSz);
cnt := aProcess.Output.Read((str.Memory + sum)^, buffSz);
sum += cnt;
end;
end else begin
repeat
str.SetSize(sum + buffSz);
cnt := aProcess.Output.Read((str.Memory + sum)^, buffSz);
sum += cnt;
until
cnt = 0;
end;
str.Size := sum;
aList.LoadFromStream(str);
finally finally
str.Free; str.Free;
end; end;
end; end;
procedure processOutputToStream(proc: TProcess; stream: TMemoryStream); procedure processOutputToStream(aProcess: TProcess; output: TMemoryStream);
var var
sum, cnt: Integer; sum, cnt: Integer;
buffSz: Integer; const
buffSz = 2048;
begin begin
if not (poUsePipes in proc.Options) then if not (poUsePipes in aProcess.Options) then
exit; exit;
// //
buffSz := proc.PipeBufferSize; sum := output.Size;
sum := stream.Size; while aProcess.Output.NumBytesAvailable <> 0 do begin
stream.Position := sum; output.SetSize(sum + buffSz);
repeat cnt := aProcess.Output.Read((output.Memory + sum)^, buffSz);
stream.SetSize(sum + buffSz);
cnt := proc.Output.Read((stream.Memory + sum)^, buffSz);
sum += cnt; sum += cnt;
until (proc.Output.NumBytesAvailable = 0) or (cnt = 0); end;
stream.size := sum; output.SetSize(sum);
stream.Position := sum; output.Position := sum;
end; end;
procedure killProcess(var aProcess: TAsyncProcess); procedure killProcess(var aProcess: TAsyncProcess);