pipes.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. protected
  25. function GetPosition: Int64; override;
  26. procedure InvalidSeek; override;
  27. public
  28. destructor Destroy; override;
  29. Function Write (Const Buffer; Count : Longint) :Longint; Override;
  30. function Seek(const Offset: int64; Origin: TSeekOrigin): int64; override;
  31. Function Read (Var Buffer; Count : Longint) : longint; Override;
  32. property NumBytesAvailable: DWord read GetNumBytesAvailable;
  33. end;
  34. TOutputPipeStream = Class(THandleStream)
  35. Public
  36. destructor Destroy; override;
  37. function Seek(const Offset: int64; Origin: TSeekOrigin): int64; override;
  38. Function Read (Var Buffer; Count : Longint) : longint; Override;
  39. end;
  40. Function CreatePipeHandles (Var Inhandle,OutHandle : THandle; APipeBufferSize : Cardinal = 1024) : Boolean;
  41. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  42. Var OutPipe : TOutputPipeStream);
  43. Const EPipeMsg = 'Failed to create pipe.';
  44. ENoSeekMsg = 'Cannot seek on pipes';
  45. Implementation
  46. {$i pipes.inc}
  47. Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
  48. Var OutPipe : TOutputPipeStream);
  49. Var InHandle,OutHandle : THandle;
  50. begin
  51. if CreatePipeHandles (InHandle, OutHandle) then
  52. begin
  53. InPipe:=TInputPipeStream.Create (InHandle);
  54. OutPipe:=TOutputPipeStream.Create (OutHandle);
  55. end
  56. Else
  57. Raise EPipeCreation.Create (EPipeMsg)
  58. end;
  59. destructor TInputPipeStream.Destroy;
  60. begin
  61. PipeClose (Handle);
  62. inherited;
  63. end;
  64. Function TInputPipeStream.Write (Const Buffer; Count : Longint) : longint;
  65. begin
  66. WriteNotImplemented;
  67. Result := 0;
  68. end;
  69. Function TInputPipeStream.Read (Var Buffer; Count : Longint) : longint;
  70. {$ifdef MorphOS}
  71. var
  72. i: Integer;
  73. Runner: PByte;
  74. {$endif}
  75. begin
  76. {$ifdef MorphOS}
  77. FillChar(Buffer, Count, 0);
  78. if FGetS(Handle, @Buffer, Count) = nil then
  79. Result := 0
  80. else
  81. begin
  82. Result := 0;
  83. Runner := @Buffer;
  84. repeat
  85. if Runner^ = 0 then
  86. Break;
  87. Inc(Result);
  88. until Result >= Count;
  89. end;
  90. {$else}
  91. Result:=Inherited Read(Buffer,Count);
  92. Inc(FPos,Result);
  93. {$endif}
  94. end;
  95. function TInputPipeStream.Seek(const Offset: int64; Origin: TSeekOrigin): int64;
  96. begin
  97. FakeSeekForward(Offset,Origin,FPos);
  98. Result:=FPos;
  99. end;
  100. destructor TOutputPipeStream.Destroy;
  101. begin
  102. PipeClose (Handle);
  103. inherited;
  104. end;
  105. Function TOutputPipeStream.Read(Var Buffer; Count : Longint) : longint;
  106. begin
  107. ReadNotImplemented;
  108. Result := 0;
  109. end;
  110. function TOutputPipeStream.Seek(const Offset: int64; Origin: TSeekOrigin): int64;
  111. begin
  112. Result:=0; { to silence warning mostly }
  113. InvalidSeek;
  114. end;
  115. end.