Hi,

TAsyncProcess from the LCL has a property NumBytesAvailable. Attached patch adds it to TProcess in the FCL.

NumBytesAvailable checks how many bytes are available in TProcess.Output, so you can decide not to read from it if there are no bytes available.

Maybe you want a different name for the property or different implementation details. If so, let it know, so I can adapt the patch.

Vincent
Index: inc/process.pp
===================================================================
--- inc/process.pp      (revision 6181)
+++ inc/process.pp      (working copy)
@@ -41,6 +41,9 @@
 
 
 Type
+
+  { TProcess }
+
   TProcess = Class (TComponent)
   Private
     FProcessOptions : TProcessOptions;
@@ -69,6 +72,7 @@
     dwy : Cardinal;
     Procedure FreeStreams;
     Function  GetExitStatus : Integer;
+    function GetNumBytesAvailable: dword;
     Function  GetRunning : Boolean;
     Function  GetWindowRect : TRect;
     Procedure SetWindowRect (Value : TRect);
@@ -113,6 +117,7 @@
     Property Stderr : TinputPipeStream  Read FStderrStream;
     Property ExitStatus : Integer Read GetExitStatus;
     Property InheritHandles : Boolean Read FInheritHandles Write 
FInheritHandles;
+    Property NumBytesAvailable: dword read GetNumBytesAvailable;
   Published
     Property Active : Boolean Read GetRunning Write SetActive;
     Property ApplicationName : String Read FApplicationName Write 
SetApplicationName;
Index: unix/process.inc
===================================================================
--- unix/process.inc    (revision 6181)
+++ unix/process.inc    (working copy)
@@ -4,7 +4,9 @@
 
 uses
    Unix,
-   Baseunix;
+   Baseunix
+   {$IFDEF linux},TermIO{$ENDIF}
+   ;
 
 resourcestring
   SErrNoSuchProgram = 'Executable not found: "%s"';
@@ -240,6 +242,23 @@
   end;
 end;
 
+{$ifdef BSD}
+const
+  FIONREAD = $4004667F;
+{$endif}
+
+function TProcess.GetNumBytesAvailable: dword;
+begin
+  if not (poUsePipes in Options) then
+    Result := 0
+  else begin
+    // FIONREAD -> bytes available for reading without blocking
+    if fpioctl(Output.Handle, FIONREAD, @Result)<0 then
+      Result := 0;
+  end;
+end;
+
+
 Procedure TProcess.Execute;
 
 Var
Index: win/process.inc
===================================================================
--- win/process.inc     (revision 6181)
+++ win/process.inc     (working copy)
@@ -149,6 +149,17 @@
 end;
 
 
+Function TProcess.GetNumBytesAvailable: DWORD;
+
+begin
+  if not (poUsePipes in Options) then
+    Result := 0
+  else
+  if not PeekNamedPipe(Output.Handle, nil, 0, nil, @Result, nil) then
+    Result := 0;
+end;
+
+
 Procedure TProcess.Execute;
 
 
Index: wince/process.inc
===================================================================
--- wince/process.inc   (revision 6181)
+++ wince/process.inc   (working copy)
@@ -149,6 +149,14 @@
 end;
 
 
+Function TProcess.GetNumBytesAvailable: DWORD;
+
+begin
+  // Windows CE doesn´t have the API function PeekNamedPipe
+  Result := 0;
+end;
+
+
 Procedure TProcess.Execute;
 
 
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to