Skip to content

Commit

Permalink
Add -echo4 CRLF {readyxx} CRLF to args file. so we know when to stop…
Browse files Browse the repository at this point in the history
… reading StdErr. #163

Fix #163
  • Loading branch information
FrankBijnen committed Nov 15, 2023
1 parent 49d0037 commit 8beafc4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
37 changes: 23 additions & 14 deletions Source/ExifTool.pas
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ function ET_OptionsRec.GetOptions(Charset: boolean = true): string;
// +further options...
end;

procedure UpdateExecNum;
// Add ExecNum to FinalCmd
// '-echo4 CRLF {readyxx} CRLF' Ensures that something is written to StdErr. TSOPipeStream relies on it.
// '-executexx CRLF' The same for STDout.
procedure AddExecNum(var FinalCmd: string);
begin
inc(ExecNum);
// Update Execnum. From 10 to 99.
Inc(ExecNum);
if (ExecNum > 99) then
ExecNum := 10;

FinalCmd := FinalCmd + Format('-echo4%s{ready%u}%s-execute%u%s', [CRLF, ExecNum, CRLF, ExecNum, CRLF]);
end;

function ETWorkDir: string;
Expand Down Expand Up @@ -189,6 +195,8 @@ function ET_StayOpen(WorkDir: string): boolean;

function ET_OpenExec(ETcmd: string; FNames: string; var ETouts, ETErrs: string; PopupOnError: boolean = true): boolean;
var
ReadOut: TSOReadPipeThread;
ReadErr: TSOReadPipeThread;
FinalCmd: string;
TempFile: string;
StatusLine: string;
Expand All @@ -198,13 +206,9 @@ function ET_OpenExec(ETcmd: string; FNames: string; var ETouts, ETErrs: string;
CanUseUtf8: boolean;
Wr: TWaitResult;
CrWait, CrNormal: HCURSOR;
SOReadPipeThread: TSOReadPipeThread;
begin
result := false;

// Update Execnum
UpdateExecNum;

if (FETWorkDir <> '') and
(Length(ETcmd) > 1) then
begin
Expand All @@ -229,7 +233,8 @@ function ET_OpenExec(ETcmd: string; FNames: string; var ETouts, ETErrs: string;
FinalCmd := EndsWithCRLF(ET_Options.GetOptions(CanUseUtf8) + ETcmd);
if FNames <> '' then
FinalCmd := EndsWithCRLF(FinalCmd + FNames);
FinalCmd := EndsWithCRLF(FinalCmd + '-execute' + IntToStr(ExecNum));

AddExecNum(FinalCmd);

// Create tempfile
TempFile := GetExifToolTmp;
Expand All @@ -242,17 +247,20 @@ function ET_OpenExec(ETcmd: string; FNames: string; var ETouts, ETErrs: string;

// ========= Read StdOut and stdErr =======================
EtOutPipe.SetCounter(GetCounter);
SOReadPipeThread := TSOReadPipeThread.Create(EtOutPipe, EtErrPipe, ExecNum);
ReadOut := TSOReadPipeThread.Create(EtOutPipe, ExecNum);
ReadErr := TSOReadPipeThread.Create(EtErrPipe, ExecNum);
try
SOReadPipeThread.WaitFor;
ReadOut.WaitFor;
ReadErr.WaitFor;
finally
SetCounter(nil, 0);
SOReadPipeThread.Free;
ReadOut.Free;
ReadErr.Free;
end;
ETouts := ETOutPipe.AnalyseResult(StatusLine, LengthReady);
ETOutPipe.Clear;

ETErrs := ETErrPipe.AsString;
ETErrs := ETErrPipe.AnalyseError;
ETErrPipe.Clear;

// Callback for Logging
Expand Down Expand Up @@ -332,8 +340,6 @@ function ExecET(ETcmd, FNames, WorkDir: string; var ETouts, ETErrs: string): boo
CrWait := LoadCursor(0, IDC_WAIT);
CrNormal := SetCursor(CrWait);

UpdateExecNum;

try
FillChar(ProcessInfo, SizeOf(TProcessInformation), #0);
FillChar(SecurityAttr, SizeOf(TSecurityAttributes), #0);
Expand Down Expand Up @@ -361,6 +367,9 @@ function ExecET(ETcmd, FNames, WorkDir: string; var ETouts, ETErrs: string): boo

FinalCmd := EndsWithCRLF(ET_Options.GetOptions(CanUseUtf8) + ArgsFromDirectCmd(ETcmd));
FinalCmd := EndsWithCRLF(FinalCmd + FNames);

AddExecNum(FinalCmd);

TempFile := GetExifToolTmp;
WriteArgsFile(FinalCmd, TempFile);
Call_ET := GUIsettings.ETOverrideDir + 'exiftool -@ "' + TempFile + '"';
Expand All @@ -381,7 +390,7 @@ function ExecET(ETcmd, FNames, WorkDir: string; var ETouts, ETErrs: string): boo
ETouts := ReadOut.PipeStream.AnalyseResult(StatusLine, LengthReady);

ReadErr.WaitFor;
ETErrs := ReadErr.PipeStream.AsString;
ETErrs := ReadErr.PipeStream.AnalyseError;
finally
ReadOut.Free;
ReadErr.Free;
Expand Down
Binary file modified Source/ExifToolGUI.res
Binary file not shown.
31 changes: 18 additions & 13 deletions Source/ExifTool_PipeStream.pas
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ TPipeStream = class(TBytesStream)
function ReadPipe: DWORD;
function AsString: string;
function AnalyseResult(var StatusLine: string; var LengthReady: integer): string;
function AnalyseError: string;
end;

// Reads the PipeStream in a separate thread
Expand All @@ -55,11 +56,10 @@ TReadPipeThread = class(TThread)
TSOReadPipeThread = class(TThread)
protected
FExecNum: word;
FOutPipeStream: TPipeStream;
FErrPipeStream: TPipeStream;
FPipeStream: TPipeStream;
procedure Execute; override;
public
constructor Create(AOutPipeStream, AErrPipeStream: TPipeStream; AExecNum: word);
constructor Create(APipeStream: TPipeStream; AExecNum: word);
end;

procedure SetCounter(ACounterEvent: TCounterETEvent; ACounter: integer);
Expand Down Expand Up @@ -246,6 +246,15 @@ function TPipeStream.AnalyseResult(var StatusLine: string; var LengthReady: inte
end;
end;

// Dont need the statusline, just the data.
function TPipeStream.AnalyseError: string;
var StatusLine: string;
LengthReady: integer;
begin
result := AnalyseResult(StatusLine, LengthReady);
SetLength(result, Length(result) - LengthReady);
end;

{ TReadPipeThread }
// Read Pipe in separate thread

Expand All @@ -268,23 +277,19 @@ procedure TReadPipeThread.Execute;

{ TSOReadPipeThread }
// Read Pipe in separate thread for Stay Open

constructor TSOReadPipeThread.Create(AOutPipeStream, AErrPipeStream: TPipeStream; AExecNum: word);
constructor TSOReadPipeThread.Create(APipeStream: TPipeStream; AExecNum: word);
begin
FOutPipeStream := AOutPipeStream;
FErrPipeStream := AErrPipeStream;
FPipeStream := APipeStream;
FExecNum := AExecNum;
inherited Create(False); // start running;
end;

procedure TSOReadPipeThread.Execute;
begin
while (not FOutPipeStream.PipeHasReady(FExecNum)) do // Continue until we see our execnum
begin
FOutPipeStream.ReadPipe;
while (FErrPipeStream.PipeHasData) do
FErrPipeStream.ReadPipe;
end;
// Continue until we see our execnum
// -executexx and -echo4 CRLF xx need to be set for this to work
while (not FPipeStream.PipeHasReady(FExecNum)) do
FPipeStream.ReadPipe;
end;

initialization
Expand Down

0 comments on commit 8beafc4

Please sign in to comment.