pipes.pp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt
  5. Implementation of pipe stream.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  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 = Class(THandleStream)
  22. Private
  23. FPos : longint;
  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. end;
  29. TOutputPipeStream = Class(THandleStream)
  30. Public
  31. Function Seek (Offset : Longint;Origin : Word) : longint;override;
  32. Function Read (Var Buffer; Count : Longint) : longint; Override;
  33. end;
  34. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  35. Var OutPipe : TOutputPipeStream);
  36. Const EPipeMsg = 'Failed to create pipe.';
  37. ENoReadMSg = 'Cannot read from OuputPipeStream.';
  38. ENoWriteMsg = 'Cannot write to InputPipeStream.';
  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 : Longint;
  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. Raise ENoWritePipe.Create (ENoWriteMsg);
  57. end;
  58. Function TInputPipeStream.Read (Var Buffer; Count : Longint) : longint;
  59. begin
  60. Result:=Inherited Read(Buffer,Count);
  61. Inc(FPos,Result);
  62. end;
  63. Function TInputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  64. Const BufSize = 100;
  65. Var Buf : array[1..BufSize] of Byte;
  66. begin
  67. If (Origin=soFromCurrent) and (Offset=0) then
  68. result:=FPos;
  69. { Try to fake seek by reading and discarding }
  70. if Not((Origin=soFromCurrent) and (Offset>=0) or
  71. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  72. Raise EPipeSeek.Create(ENoSeekMSg);
  73. if Origin=soFromBeginning then
  74. Dec(Offset,FPos);
  75. While ((Offset Div BufSize)>0)
  76. and (Read(Buf,SizeOf(Buf))=BufSize) do
  77. Dec(Offset,BufSize);
  78. If (Offset>0) then
  79. Read(Buf,BufSize);
  80. Result:=FPos;
  81. end;
  82. Function TOutputPipeStream.Read(Var Buffer; Count : Longint) : longint;
  83. begin
  84. Raise ENoReadPipe.Create (ENoReadMsg);
  85. end;
  86. Function TOutputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  87. begin
  88. Raise EPipeSeek.Create (ENoSeekMsg);
  89. end;
  90. end.