forked from jackokring/rub
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipes.pas
133 lines (100 loc) · 3.34 KB
/
pipes.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2000 by Michael Van Canneyt
Implementation of pipe stream.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$mode objfpc}
Unit Pipes;
Interface
Uses sysutils,Classes;
Type
EPipeError = Class(EStreamError);
EPipeSeek = Class (EPipeError);
EPipeCreation = Class (EPipeError);
{ TInputPipeStream }
TInputPipeStream = Class(THandleStream)
Private
FPos : Int64;
function GetNumBytesAvailable: DWord;
public
Function Write (Const Buffer; Count : Longint) :Longint; Override;
Function Seek (Offset : Longint;Origin : Word) : longint;override;
Function Read (Var Buffer; Count : Longint) : longint; Override;
property NumBytesAvailable: DWord read GetNumBytesAvailable;
end;
TOutputPipeStream = Class(THandleStream)
Public
Function Seek (Offset : Longint;Origin : Word) : longint;override;
Function Read (Var Buffer; Count : Longint) : longint; Override;
end;
Function CreatePipeHandles (Var Inhandle,OutHandle : THandle) : Boolean;
Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
Var OutPipe : TOutputPipeStream);
Const EPipeMsg = 'Failed to create pipe.';
ENoSeekMsg = 'Cannot seek on pipes';
Implementation
{$i pipes.inc}
Procedure CreatePipeStreams (Var InPipe : TInputPipeStream;
Var OutPipe : TOutputPipeStream);
Var InHandle,OutHandle : THandle;
begin
if CreatePipeHandles (InHandle, OutHandle) then
begin
InPipe:=TInputPipeStream.Create (InHandle);
OutPipe:=TOutputPipeStream.Create (OutHandle);
end
Else
Raise EPipeCreation.Create (EPipeMsg)
end;
Function TInputPipeStream.Write (Const Buffer; Count : Longint) : longint;
begin
{$ifdef ver2_2_0}
raise EStreamError.Create( 'Cannot write to InputPipeStream');
{$else}
WriteNotImplemented;
{$endif}
Result := 0;
end;
Function TInputPipeStream.Read (Var Buffer; Count : Longint) : longint;
begin
Result:=Inherited Read(Buffer,Count);
Inc(FPos,Result);
end;
Function TInputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
Const BufSize = 100;
Var Buf : array[1..BufSize] of Byte;
begin
If (Origin=soFromCurrent) and (Offset=0) then
result:=FPos;
{ Try to fake seek by reading and discarding }
if Not((Origin=soFromCurrent) and (Offset>=0) or
((Origin=soFrombeginning) and (OffSet>=FPos))) then
Raise EPipeSeek.Create(ENoSeekMSg);
if Origin=soFromBeginning then
Dec(Offset,FPos);
While ((Offset Div BufSize)>0)
and (Read(Buf,SizeOf(Buf))=BufSize) do
Dec(Offset,BufSize);
If (Offset>0) then
Read(Buf,BufSize);
Result:=FPos;
end;
Function TOutputPipeStream.Read(Var Buffer; Count : Longint) : longint;
begin
{$ifdef ver2_2_0}
raise EStreamError.Create( 'Cannot read from OutputPipeStream');
{$else}
ReadNotImplemented;
{$endif}
Result := 0;
end;
Function TOutputPipeStream.Seek (Offset : Longint;Origin : Word) : longint;
begin
Raise EPipeSeek.Create (ENoSeekMsg);
end;
end.