pipes.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. ENoReadPipe = Class(EPipeError);
  18. ENoWritePipe = Class (EPipeError);
  19. EPipeSeek = Class (EPipeError);
  20. EPipeCreation = Class (EPipeError);
  21. { TInputPipeStream }
  22. TInputPipeStream = Class(THandleStream)
  23. Private
  24. FPos : Int64;
  25. function GetNumBytesAvailable: DWord;
  26. public
  27. Function Write (Const Buffer; Count : Longint) :Longint; Override;
  28. Function Seek (Offset : Longint;Origin : Word) : longint;override;
  29. Function Read (Var Buffer; Count : Longint) : longint; Override;
  30. property NumBytesAvailable: DWord read GetNumBytesAvailable;
  31. end;
  32. TOutputPipeStream = Class(THandleStream)
  33. Public
  34. Function Seek (Offset : Longint;Origin : Word) : longint;override;
  35. Function Read (Var Buffer; Count : Longint) : longint; Override;
  36. end;
  37. Function CreatePipeHandles (Var Inhandle,OutHandle : THandle) : Boolean;
  38. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  39. Var OutPipe : TOutputPipeStream);
  40. Const EPipeMsg = 'Failed to create pipe.';
  41. ENoReadMSg = 'Cannot read from OuputPipeStream.';
  42. ENoWriteMsg = 'Cannot write to InputPipeStream.';
  43. ENoSeekMsg = 'Cannot seek on pipes';
  44. Implementation
  45. {$i pipes.inc}
  46. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  47. Var OutPipe : TOutputPipeStream);
  48. Var InHandle,OutHandle : THandle;
  49. begin
  50. if CreatePipeHandles (InHandle, OutHandle) then
  51. begin
  52. InPipe:=TInputPipeStream.Create (InHandle);
  53. OutPipe:=TOutputPipeStream.Create (OutHandle);
  54. end
  55. Else
  56. Raise EPipeCreation.Create (EPipeMsg)
  57. end;
  58. Function TInputPipeStream.Write (Const Buffer; Count : Longint) : longint;
  59. begin
  60. Raise ENoWritePipe.Create (ENoWriteMsg);
  61. end;
  62. Function TInputPipeStream.Read (Var Buffer; Count : Longint) : longint;
  63. begin
  64. Result:=Inherited Read(Buffer,Count);
  65. Inc(FPos,Result);
  66. end;
  67. Function TInputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  68. Const BufSize = 100;
  69. Var Buf : array[1..BufSize] of Byte;
  70. begin
  71. If (Origin=soFromCurrent) and (Offset=0) then
  72. result:=FPos;
  73. { Try to fake seek by reading and discarding }
  74. if Not((Origin=soFromCurrent) and (Offset>=0) or
  75. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  76. Raise EPipeSeek.Create(ENoSeekMSg);
  77. if Origin=soFromBeginning then
  78. Dec(Offset,FPos);
  79. While ((Offset Div BufSize)>0)
  80. and (Read(Buf,SizeOf(Buf))=BufSize) do
  81. Dec(Offset,BufSize);
  82. If (Offset>0) then
  83. Read(Buf,BufSize);
  84. Result:=FPos;
  85. end;
  86. Function TOutputPipeStream.Read(Var Buffer; Count : Longint) : longint;
  87. begin
  88. Raise ENoReadPipe.Create (ENoReadMsg);
  89. end;
  90. Function TOutputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  91. begin
  92. Raise EPipeSeek.Create (ENoSeekMsg);
  93. end;
  94. end.