summaryrefslogtreecommitdiff
path: root/packages/fcl-base/src/inc/streamio.pp
blob: 71e8f563545aef93eaa29c33d4bc005956f807a4 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 1999-2000 by the Free Pascal development team

    This unit converts a stream to a regular text file.

    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.

 **********************************************************************}
unit StreamIO;

interface

uses Classes,SysUtils;

Procedure AssignStream(var F: Textfile; Stream: TStream);
Function GetStream(var F: TTextRec) : TStream;

implementation

ResourceString
  SErrNilStream = 'Can not assign file to Nil stream';

Type
  PStream = ^TStream;

{ ---------------------------------------------------------------------
    Text IO functions
  ---------------------------------------------------------------------}


Function StreamRead(var F: TTextRec) : longint;

begin
  Result:=0;
  With F do
    Try
      Bufend:=GetStream(F).Read(BufPtr^,BufSize);
      BufPos:=0;
    except
      Result:=100;
    end;
end;


Function StreamWrite(var F: TTextRec ): longint;
begin
  Result:=0;
  with F do
    if (BufPos>0) then
      try
        GetStream(F).WriteBuffer(BufPtr^,BufPos);
        BufPos:=0;
      except
        Result:=101;
      end;
end;


Function StreamFlush(var F: TTextRec): longint;

begin
  Result:=0;
end;


Function StreamClose(var F: TTextRec): longint;
begin
  Result:=0;
end;


Function StreamOpen(var F: TTextRec ): longint;

begin
  Result := 0;
  with F do
    begin
    BufPos:=0;
    Bufend:=0;
    case Mode of
      fmInput:
        begin
        InOutFunc:=@StreamRead;
        FlushFunc:=@StreamFlush;
        end;
      fmOutput,fmAppend:
        begin
        InOutFunc:=@StreamWrite;
        FlushFunc:=@StreamWrite;
        if mode=fmAppend then
          Try
            GetStream(F).Seek(0,soFromEnd);
          except
            Result:=156;
          end;
        end;
    end;
    end;
end;


{ ---------------------------------------------------------------------
    Public functions
  ---------------------------------------------------------------------}


Procedure AssignStream(var F: Textfile; Stream : TStream);

Var
  E : EInoutError;

begin
  if (Stream=Nil) then
    begin
    E:=EInOutError.Create(SErrNilStream);
    E.ErrorCode:=6;
    Raise E;
    end;
  with TTextRec(F) do
    begin
    OpenFunc:=@StreamOpen;
    CloseFunc:=@StreamClose;
 Case DefaultTextLineBreakStyle Of
    tlbsLF: TextRec(f).LineEnd := #10;
    tlbsCRLF: TextRec(f).LineEnd := #13#10;
    tlbsCR: TextRec(f).LineEnd := #13;
  End;
    PStream(@UserData)^:=Stream;
    Mode:=fmClosed;
    BufSize:=SizeOf(Buffer);
    BufPtr:=@Buffer;
    Name[0]:=#0;
    end;
end;


Function GetStream(var F: TTextRec) : TStream;

begin
  Result:=PStream(@F.Userdata)^;
end;

end.