pipes.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt
  4. Implementation of pipe stream.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. Unit Pipes;
  13. Interface
  14. Uses sysutils,Classes;
  15. Type
  16. EPipeError = Class(EStreamError);
  17. EPipeSeek = Class (EPipeError);
  18. EPipeCreation = Class (EPipeError);
  19. { TInputPipeStream }
  20. TInputPipeStream = Class(THandleStream)
  21. Private
  22. FPos : Int64;
  23. function GetNumBytesAvailable: DWord;
  24. public
  25. Function Write (Const Buffer; Count : Longint) :Longint; Override;
  26. Function Seek (Offset : Longint;Origin : Word) : longint;override;
  27. Function Read (Var Buffer; Count : Longint) : longint; Override;
  28. property NumBytesAvailable: DWord read GetNumBytesAvailable;
  29. end;
  30. TOutputPipeStream = Class(THandleStream)
  31. Public
  32. Function Seek (Offset : Longint;Origin : Word) : longint;override;
  33. Function Read (Var Buffer; Count : Longint) : longint; Override;
  34. end;
  35. Function CreatePipeHandles (Var Inhandle,OutHandle : THandle) : Boolean;
  36. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  37. Var OutPipe : TOutputPipeStream);
  38. Const EPipeMsg = 'Failed to create pipe.';
  39. ENoSeekMsg = 'Cannot seek on pipes';
  40. Implementation
  41. {$i pipes.inc}
  42. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  43. Var OutPipe : TOutputPipeStream);
  44. Var InHandle,OutHandle : THandle;
  45. begin
  46. if CreatePipeHandles (InHandle, OutHandle) then
  47. begin
  48. InPipe:=TInputPipeStream.Create (InHandle);
  49. OutPipe:=TOutputPipeStream.Create (OutHandle);
  50. end
  51. Else
  52. Raise EPipeCreation.Create (EPipeMsg)
  53. end;
  54. Function TInputPipeStream.Write (Const Buffer; Count : Longint) : longint;
  55. begin
  56. {$ifdef ver2_2_0}
  57. raise EStreamError.Create( 'Cannot write to InputPipeStream');
  58. {$else}
  59. WriteNotImplemented;
  60. {$endif}
  61. Result := 0;
  62. end;
  63. Function TInputPipeStream.Read (Var Buffer; Count : Longint) : longint;
  64. begin
  65. Result:=Inherited Read(Buffer,Count);
  66. Inc(FPos,Result);
  67. end;
  68. Function TInputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  69. Const BufSize = 100;
  70. Var Buf : array[1..BufSize] of Byte;
  71. begin
  72. If (Origin=soFromCurrent) and (Offset=0) then
  73. result:=FPos;
  74. { Try to fake seek by reading and discarding }
  75. if Not((Origin=soFromCurrent) and (Offset>=0) or
  76. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  77. Raise EPipeSeek.Create(ENoSeekMSg);
  78. if Origin=soFromBeginning then
  79. Dec(Offset,FPos);
  80. While ((Offset Div BufSize)>0)
  81. and (Read(Buf,SizeOf(Buf))=BufSize) do
  82. Dec(Offset,BufSize);
  83. If (Offset>0) then
  84. Read(Buf,BufSize);
  85. Result:=FPos;
  86. end;
  87. Function TOutputPipeStream.Read(Var Buffer; Count : Longint) : longint;
  88. begin
  89. {$ifdef ver2_2_0}
  90. raise EStreamError.Create( 'Cannot read from OutputPipeStream');
  91. {$else}
  92. ReadNotImplemented;
  93. {$endif}
  94. Result := 0;
  95. end;
  96. Function TOutputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  97. begin
  98. Raise EPipeSeek.Create (ENoSeekMsg);
  99. end;
  100. end.