pipes.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. {$mode objfpc}
  13. Unit Pipes;
  14. Interface
  15. Uses sysutils,Classes;
  16. Type
  17. EPipeError = Class(EStreamError);
  18. ENoReadPipe = Class(EPipeError);
  19. ENoWritePipe = Class (EPipeError);
  20. EPipeSeek = Class (EPipeError);
  21. EPipeCreation = Class (EPipeError);
  22. TInputPipeStream = Class(THandleStream)
  23. Private
  24. FPos : longint;
  25. public
  26. Function Write (Const Buffer; Count : Longint) :Longint; Override;
  27. Function Seek (Offset : Longint;Origin : Word) : longint;override;
  28. Function Read (Var Buffer; Count : Longint) : longint; Override;
  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 : Longint) : Boolean;
  36. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  37. Var OutPipe : TOutputPipeStream);
  38. Const EPipeMsg = 'Failed to create pipe.';
  39. ENoReadMSg = 'Cannot read from OuputPipeStream.';
  40. ENoWriteMsg = 'Cannot write to InputPipeStream.';
  41. ENoSeekMsg = 'Cannot seek on pipes';
  42. Implementation
  43. {$i pipes.inc}
  44. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  45. Var OutPipe : TOutputPipeStream);
  46. Var InHandle,OutHandle : Longint;
  47. begin
  48. if CreatePipeHandles (InHandle, OutHandle) then
  49. begin
  50. Inpipe:=TinputPipeStream.Create (InHandle);
  51. OutPipe:=ToutputPipeStream.Create (OutHandle);
  52. end
  53. Else
  54. Raise EPipeCreation.Create (EPipeMsg)
  55. end;
  56. Function TInputPipeStream.Write (Const Buffer; Count : Longint) : longint;
  57. begin
  58. Raise ENoWritePipe.Create (ENoWriteMsg);
  59. end;
  60. Function TInputPipeStream.Read (Var Buffer; Count : Longint) : longint;
  61. begin
  62. Result:=Inherited Read(Buffer,Count);
  63. Inc(FPos,Result);
  64. end;
  65. Function TInputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  66. Const BufSize = 100;
  67. Var Buf : array[1..BufSize] of Byte;
  68. begin
  69. If (Origin=soFromCurrent) and (Offset=0) then
  70. result:=FPos;
  71. { Try to fake seek by reading and discarding }
  72. if Not((Origin=soFromCurrent) and (Offset>=0) or
  73. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  74. Raise EPipeSeek.Create(ENoSeekMSg);
  75. if Origin=soFromBeginning then
  76. Dec(Offset,FPos);
  77. While ((Offset Div BufSize)>0)
  78. and (Read(Buf,SizeOf(Buf))=BufSize) do
  79. Dec(Offset,BufSize);
  80. If (Offset>0) then
  81. Read(Buf,BufSize);
  82. Result:=FPos;
  83. end;
  84. Function TOutputPipeStream.Read(Var Buffer; Count : Longint) : longint;
  85. begin
  86. Raise ENoReadPipe.Create (ENoReadMsg);
  87. end;
  88. Function TOutputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  89. begin
  90. Raise EPipeSeek.Create (ENoSeekMsg);
  91. end;
  92. end.
  93. {
  94. $Log$
  95. Revision 1.4 2004-08-11 21:42:47 michael
  96. + Added CreatePipeHandles call to interface
  97. Revision 1.3 2002/09/07 15:15:25 peter
  98. * old logs removed and tabs fixed
  99. }