2
0

pipes.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  36. Var OutPipe : TOutputPipeStream);
  37. Const EPipeMsg = 'Failed to create pipe.';
  38. ENoReadMSg = 'Cannot read from OuputPipeStream.';
  39. ENoWriteMsg = 'Cannot write to InputPipeStream.';
  40. ENoSeekMsg = 'Cannot seek on pipes';
  41. Implementation
  42. {$i pipes.inc}
  43. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  44. Var OutPipe : TOutputPipeStream);
  45. Var InHandle,OutHandle : Longint;
  46. begin
  47. if CreatePipeHandles (InHandle, OutHandle) then
  48. begin
  49. Inpipe:=TinputPipeStream.Create (InHandle);
  50. OutPipe:=ToutputPipeStream.Create (OutHandle);
  51. end
  52. Else
  53. Raise EPipeCreation.Create (EPipeMsg)
  54. end;
  55. Function TInputPipeStream.Write (Const Buffer; Count : Longint) : longint;
  56. begin
  57. Raise ENoWritePipe.Create (ENoWriteMsg);
  58. end;
  59. Function TInputPipeStream.Read (Var Buffer; Count : Longint) : longint;
  60. begin
  61. Result:=Inherited Read(Buffer,Count);
  62. Inc(FPos,Result);
  63. end;
  64. Function TInputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  65. Const BufSize = 100;
  66. Var Buf : array[1..BufSize] of Byte;
  67. begin
  68. If (Origin=soFromCurrent) and (Offset=0) then
  69. result:=FPos;
  70. { Try to fake seek by reading and discarding }
  71. if Not((Origin=soFromCurrent) and (Offset>=0) or
  72. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  73. Raise EPipeSeek.Create(ENoSeekMSg);
  74. if Origin=soFromBeginning then
  75. Dec(Offset,FPos);
  76. While ((Offset Div BufSize)>0)
  77. and (Read(Buf,SizeOf(Buf))=BufSize) do
  78. Dec(Offset,BufSize);
  79. If (Offset>0) then
  80. Read(Buf,BufSize);
  81. Result:=FPos;
  82. end;
  83. Function TOutputPipeStream.Read(Var Buffer; Count : Longint) : longint;
  84. begin
  85. Raise ENoReadPipe.Create (ENoReadMsg);
  86. end;
  87. Function TOutputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
  88. begin
  89. Raise EPipeSeek.Create (ENoSeekMsg);
  90. end;
  91. end.
  92. {
  93. $Log$
  94. Revision 1.2 2000-07-13 11:32:59 michael
  95. + removed logs
  96. }