From dd65981470ce23bb23000b9c16a4dbcd80bfc99e Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Fri, 7 Nov 2014 14:06:44 +0100 Subject: [PATCH] added TProcess utils to ce_common --- src/ce_common.pas | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/ce_common.pas b/src/ce_common.pas index 7825c03e..b029584e 100644 --- a/src/ce_common.pas +++ b/src/ce_common.pas @@ -9,7 +9,7 @@ uses {$IFDEF WINDOWS} Windows, {$ENDIF} - ActnList, dialogs, forms, process; + ActnList, dialogs, forms, process, asyncprocess; const DdiagFilter = 'D source|*.d|D interface|*.di|All files|*.*'; @@ -179,6 +179,16 @@ type *) function exeInSysPath(anExeName: string): boolean; + (** + * Clears then fills aList with aProcess output stream. + *) + procedure processOutputToStrings(const aProcess: TProcess; var aList: TStringList); + + (** + * Terminates and frees aProcess; + *) + procedure killProcess(var aProcess: TAsyncProcess); + implementation // https://stackoverflow.com/questions/25438091/objectbinarytotext-error-with-a-treader-twriter-helper-class @@ -666,6 +676,42 @@ begin exit(ExeSearch(anExeName, '') <> ''); end; +procedure processOutputToStrings(const aProcess: TProcess; var aList: TStringList); +var + str: TMemoryStream; + sum: Integer; + cnt: Integer; +const + buffSz = 1024; +begin + if not (poUsePipes in aProcess.Options) then + exit; + // + sum := 0; + str := TMemoryStream.Create; + try + while aProcess.Output.NumBytesAvailable <> 0 do begin + str.Size := str.Size + buffSz; + cnt := aProcess.Output.Read((str.Memory + sum)^, buffSz); + sum += cnt; + end; + str.Size := sum; + aList.LoadFromStream(str); + finally + str.Free; + end; +end; + +procedure killProcess(var aProcess: TAsyncProcess); +begin + if aProcess = nil then + exit; + if aProcess.Running then + aProcess.Terminate(0); + aProcess.Free; + aProcess := nil; +end; + initialization RegisterClasses([TMRUList, TMRUFileList]); DExtList := TStringList.Create;