Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code page support to JvCreateProcess for stdout and stderr #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions jvcl/run/JvCreateProcess.pas
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ TJvCreateProcess = class(TJvComponent)
FHandle: THandle;
FExitCode: Cardinal;
FRunningThreadCount: Integer;
FCodePage: UINT;
function GetConsoleOutput: TStrings;
function GetEnvironment: TStrings;
procedure SetWaitForTerminate(const Value: Boolean);
Expand All @@ -184,6 +185,7 @@ TJvCreateProcess = class(TJvComponent)
procedure GotoReadyState;
procedure GotoWaitState(const AThreadCount: Integer);
procedure SetCommandLine(const Value: string);
procedure SetCodePage(const Value: UINT);
protected
procedure CheckReady;
procedure CheckRunning;
Expand Down Expand Up @@ -211,6 +213,7 @@ TJvCreateProcess = class(TJvComponent)
property ConsoleOutput: TStrings read GetConsoleOutput;
property InputReader: TJvBaseReader read FInputReader;
property ErrorReader: TJvBaseReader read FErrorReader;
property CodePage: UINT read FCodePage write SetCodePage;
published
property ApplicationName: string read FApplicationName write FApplicationName;
property CommandLine: string read FCommandLine write SetCommandLine;
Expand Down Expand Up @@ -324,6 +327,7 @@ TJvReader = class(TJvBaseReader)
FCursorPosition: Integer; // Position of the cursor on FCurrentLine
FStartsOnNewLine: Boolean;
FParseBuffer: TJvCPSBuffer;
FCodePage: UINT;
procedure ThreadTerminated(Sender: TObject);
protected
procedure DoReadEvent(const EndsWithNewLine: Boolean);
Expand All @@ -334,6 +338,7 @@ TJvReader = class(TJvBaseReader)
procedure CreateThread(const AReadHandle: THandle);
procedure CloseRead;
procedure Terminate;
property CodePage: UINT read FCodePage write FCodePage;
end;

//=== Local procedures =======================================================
Expand Down Expand Up @@ -1115,6 +1120,7 @@ constructor TJvCreateProcess.Create(AOwner: TComponent);
FConsoleOptions := [coOwnerData];
FErrorReader := TJvReader.Create(Self);
FInputReader := TJvReader.Create(Self);
SetCodePage(GetACP);
end;

destructor TJvCreateProcess.Destroy;
Expand Down Expand Up @@ -1384,6 +1390,13 @@ procedure TJvCreateProcess.SetOnRead(const Value: TJvCPSReadEvent);
FInputReader.OnRead := Value;
end;

procedure TJvCreateProcess.SetCodePage(const Value: UINT);
begin
FCodePage := Value;
TJvReader(FInputReader).CodePage := Value;
TJvReader(FErrorReader).CodePage := Value;
end;

procedure TJvCreateProcess.SetStartupInfo(Value: TJvCPSStartupInfo);
begin
FStartupInfo.Assign(Value);
Expand Down Expand Up @@ -1518,17 +1531,32 @@ procedure TJvReader.DoRawReadEvent(Data: PAnsiChar; const ASize: Cardinal);
end;

procedure TJvReader.DoReadEvent(const EndsWithNewLine: Boolean);
var
CurrentLine: String;
begin
{$IFDEF UNICODE}
if FCurrentLine <> '' then
begin
SetCodePage(RawByteString(FCurrentLine),FCodePage,False);
CurrentLine := String(FCurrentLine);
end
else
begin
CurrentLine := '';
end;
{$ELSE}
CurrentLine := FCurrentLine;
{$ENDIF UNICODE}
// Notify user and update current line & cursor
if not (coOwnerData in FCreateProcess.ConsoleOptions) then
begin
if FStartsOnNewLine or (ConsoleOutput.Count = 0) then
ConsoleOutput.Add(string(FCurrentLine))
ConsoleOutput.Add(CurrentLine)
else
ConsoleOutput[ConsoleOutput.Count - 1] := string(FCurrentLine);
ConsoleOutput[ConsoleOutput.Count - 1] := CurrentLine;
end;
if Assigned(FOnRead) then
FOnRead(FCreateProcess, string(FCurrentLine), FStartsOnNewLine);
FOnRead(FCreateProcess, CurrentLine, FStartsOnNewLine);
if EndsWithNewLine then
begin
FCurrentLine := '';
Expand Down