| 1 |
{$mode delphi} |
| 2 |
{$longstrings on} |
| 3 |
|
| 4 |
unit un_process; |
| 5 |
|
| 6 |
interface |
| 7 |
|
| 8 |
uses |
| 9 |
process, Math, SysUtils; |
| 10 |
|
| 11 |
type |
| 12 |
|
| 13 |
TOnReadLn = procedure(str: String) of object; |
| 14 |
|
| 15 |
{ TExProcess } |
| 16 |
|
| 17 |
TExProcess = class |
| 18 |
protected |
| 19 |
p: TProcess; |
| 20 |
s: String; |
| 21 |
FStop: Boolean; |
| 22 |
function _GetExitStatus(): Integer; |
| 23 |
public |
| 24 |
OnReadLn: TOnReadLn; |
| 25 |
constructor Create(commandline: String = ''); |
| 26 |
procedure Execute; |
| 27 |
procedure Stop; |
| 28 |
procedure SetCmdLine(commandline: String); |
| 29 |
destructor Destroy; override; |
| 30 |
|
| 31 |
property ExitStatus: Integer read _GetExitStatus; |
| 32 |
end; |
| 33 |
|
| 34 |
implementation |
| 35 |
|
| 36 |
const |
| 37 |
buf_len = 3000; |
| 38 |
|
| 39 |
|
| 40 |
{ TExProcess } |
| 41 |
|
| 42 |
function TExProcess._GetExitStatus(): Integer; |
| 43 |
begin |
| 44 |
Result := p.ExitStatus; |
| 45 |
end; |
| 46 |
|
| 47 |
constructor TExProcess.Create(commandline: String = ''); |
| 48 |
begin |
| 49 |
s := ''; |
| 50 |
p := TProcess.Create(nil); |
| 51 |
p.CommandLine := commandline; |
| 52 |
p.Options := [poUsePipes, poNoConsole, poWaitOnExit]; |
| 53 |
end; |
| 54 |
|
| 55 |
procedure TExProcess.Execute; |
| 56 |
var |
| 57 |
buf: String; |
| 58 |
i, j: Integer; |
| 59 |
begin |
| 60 |
try |
| 61 |
p.Execute; |
| 62 |
repeat |
| 63 |
if FStop then |
| 64 |
exit; |
| 65 |
SetLength(buf, buf_len); |
| 66 |
SetLength(buf, p.output.Read(buf[1], length(buf))); //waits for the process output |
| 67 |
// cut the incoming stream to lines: |
| 68 |
s := s + buf; //add to the accumulator |
| 69 |
|
| 70 |
repeat //detect the line breaks and cut. |
| 71 |
i := Pos(#13, s); |
| 72 |
j := Pos(#10, s); |
| 73 |
if i = 0 then |
| 74 |
i := j; |
| 75 |
if j = 0 then |
| 76 |
j := i; |
| 77 |
if j = 0 then |
| 78 |
Break; //there are no complete lines yet. |
| 79 |
if Assigned(OnReadLn) then |
| 80 |
OnReadLn(Copy(s, 1, min(i, j) - 1)); |
| 81 |
//return the line without the CR/LF characters |
| 82 |
s := Copy(s, max(i, j) + 1, length(s) - max(i, j)); |
| 83 |
//remove the line from accumulator |
| 84 |
until False; |
| 85 |
until buf = ''; |
| 86 |
if s <> '' then |
| 87 |
if Assigned(OnReadLn) then |
| 88 |
OnReadLn(s); |
| 89 |
buf := ''; |
| 90 |
except |
| 91 |
{$IFDEF UNIX} |
| 92 |
on e: Exception do |
| 93 |
Writeln('DSXLocate error: ', e.Message); |
| 94 |
{$ENDIF} |
| 95 |
end; |
| 96 |
|
| 97 |
if Assigned(OnReadLn) then |
| 98 |
OnReadLn(buf); //Empty line to notify DC about search process finish |
| 99 |
end; |
| 100 |
|
| 101 |
procedure TExProcess.Stop; |
| 102 |
begin |
| 103 |
FStop := True; |
| 104 |
end; |
| 105 |
|
| 106 |
procedure TExProcess.SetCmdLine(commandline: String); |
| 107 |
begin |
| 108 |
p.CommandLine := commandline; |
| 109 |
end; |
| 110 |
|
| 111 |
destructor TExProcess.Destroy; |
| 112 |
begin |
| 113 |
FreeAndNil(p); |
| 114 |
inherited Destroy; |
| 115 |
end; |
| 116 |
|
| 117 |
end. |
| 118 |
|