pipes.pp 3.7 KB

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