summaryrefslogtreecommitdiff
path: root/rtl/inc/iso7185.pp
blob: 539339382d0449493ffa99474f1eee5be43abad8 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{
    This file is part of the Free Pascal Run time library.
    Copyright (c) 2010 by Florian Klaempfl

    This unit contain procedures specific for iso pascal mode.
    It should be platform independant.

    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 iso7185;

  interface

    const
       MaxInt  = MaxLongint;

    type
      Integer = Longint;

    Procedure Rewrite(var t : Text);
    Procedure Reset(var t : Text);
    Procedure Reset(var f : TypedFile);   [INTERNPROC: fpc_in_Reset_TypedFile];
    Procedure Rewrite(var f : TypedFile); [INTERNPROC: fpc_in_Rewrite_TypedFile];

    Procedure Rewrite(var t : Text;const filename : string);
    Procedure Reset(var t : Text;const filename : string);
    Procedure Reset(var f : TypedFile;const filename : string);   [INTERNPROC: fpc_in_Reset_TypedFile_Name];
    Procedure Rewrite(var f : TypedFile;const filename : string); [INTERNPROC: fpc_in_Rewrite_TypedFile_Name];

    Function Eof(Var t: Text): Boolean;
    Function Eof:Boolean;
    Function Eoln(Var t: Text): Boolean;
    Function Eoln:Boolean;

    Procedure Page;
    Procedure Page(Var t: Text);

    Procedure Get(Var t: Text);
    Procedure Put(Var t: Text);
    procedure Get;
    Procedure Put;

    Procedure Get(Var f: TypedFile);
    Procedure Put(Var f: TypedFile);
    Procedure Seek(var f:TypedFile;Pos:Int64);
    Function FilePos(var f:TypedFile):Int64;

    Function Eof(var f:TypedFile): Boolean;

{$ifndef FPUNONE}
{$ifdef FPC_CURRENCY_IS_INT64}
    function round(c : currency) : int64;
{$ifndef cpujvm}
    function round(c : comp) : int64;
{$else not cpujvm}
    function round_comp(c : comp) : int64;
{$endif not cpujvm}
{$endif FPC_CURRENCY_IS_INT64}
    function Round(d : ValReal) : int64;
{$endif FPUNONE}

  implementation


{$i isotmp.inc}
 
{$i-}
    procedure DoAssign(var t : Text);
{$ifndef FPC_HAS_FEATURE_RANDOM}
      const
        NextIndex : Word = 1;
{$endif FPC_HAS_FEATURE_RANDOM}
      begin
{$ifdef FPC_HAS_FEATURE_RANDOM}
        Assign(t,getTempDir+'fpc_'+HexStr(random(1000000000),8)+'.tmp');
{$else FPC_HAS_FEATURE_RANDOM}
        Assign(t,getTempDir+'fpc_'+HexStr(NextIndex,4)+'.tmp');
        Inc(NextIndex);
{$endif FPC_HAS_FEATURE_RANDOM}
      end;


    Procedure Rewrite(var t : Text);[IOCheck];
      Begin
        { create file name? }
        if Textrec(t).mode=0 then
          DoAssign(t);

        System.Rewrite(t);
      End;


    Procedure Reset(var t : Text);[IOCheck];
      Begin
        case Textrec(t).mode of
          { create file name? }
          0:
            DoAssign(t);
          fmOutput:
            Write(t,#26);
        end;

        System.Reset(t);
      End;


    Procedure Rewrite(var t : Text;const filename : string);[IOCheck];
      Begin
        { create file name? }
        if Textrec(t).mode=0 then
          Assign(t,filename);

        System.Rewrite(t);
      End;


    Procedure Reset(var t : Text;const filename : string);[IOCheck];
      Begin
        case Textrec(t).mode of
          { create file name? }
          0:
            Assign(t,filename);
          fmOutput:
            Write(t,#26);
        end;

        System.Reset(t);
      End;


    Function Eof(Var t: Text): Boolean;[IOCheck];
      var
        OldCtrlZMarksEof : Boolean;
      Begin
        { not sure if this is correct, but we are always at eof when
          writing to a file }
        if TextRec(t).mode=fmOutput then
          Eof:=true
        else
          begin
            OldCtrlZMarksEof:=CtrlZMarksEOF;
            CtrlZMarksEof:=true;
            Eof:=System.Eof(t);
            CtrlZMarksEof:=OldCtrlZMarksEOF;
          end;
      end;


    Function Eof:Boolean;
      Begin
        Eof:=Eof(Input);
      End;


    Function Eoln(Var t: Text): Boolean;[IOCheck];
      var
        OldCtrlZMarksEof : Boolean;
      Begin
        OldCtrlZMarksEof:=CtrlZMarksEOF;
        CtrlZMarksEof:=true;
        Eoln:=System.Eoln(t);
        CtrlZMarksEof:=OldCtrlZMarksEOF;
      end;


    Function Eoln:Boolean;
      Begin
        Eoln:=Eoln(Input);
      End;


    Procedure Page;[IOCheck];
      begin
        Page(Output);
      end;


    Procedure Page(var t : Text);[IOCheck];
      Begin
        write(#12);
      End;


    procedure Get(var t : Text);[IOCheck];
      var
        c : char;
      Begin
        Read(t,c);
      End;


    Procedure Put(var t : Text);[IOCheck];
      type
        FileFunc = Procedure(var t : TextRec);
      begin
        inc(TextRec(t).BufPos);
        If TextRec(t).BufPos>=TextRec(t).BufSize Then
          FileFunc(TextRec(t).InOutFunc)(TextRec(t));
      end;


    procedure Get;[IOCheck];
      var
        c : char;
      Begin
        Read(input,c);
      End;


    Procedure Put;[IOCheck];
      type
        FileFunc = Procedure(var t : TextRec);
      begin
        inc(TextRec(Output).BufPos);
        If TextRec(Output).BufPos>=TextRec(Output).BufSize Then
          FileFunc(TextRec(Output).InOutFunc)(TextRec(Output));
      end;


    procedure Get(var f:TypedFile);[IOCheck];
      Begin
        if not(system.eof(f)) then
          BlockRead(f,(pbyte(@f)+sizeof(FileRec))^,1)
        else
          FileRec(f)._private[1]:=1;
      End;


    Procedure Put(var f:TypedFile);[IOCheck];
      begin
        BlockWrite(f,(pbyte(@f)+sizeof(FileRec))^,1);
      end;


    Function Eof(var f:TypedFile): Boolean;[IOCheck];
      Begin
        Eof:=FileRec(f)._private[1]=1;
      End;


    Procedure Seek(var f:TypedFile;Pos:Int64);[IOCheck];
      Begin
        System.Seek(f,Pos);
        if (FileRec(f).mode=fmInOut) or
          (FileRec(f).mode=fmInput) then
          begin
            if FilePos(f)<FileSize(f) then
              begin
                FileRec(f)._private[1]:=0;
                Get(f);
              end
            else
              FileRec(f)._private[1]:=1;
          end;
      End;


    Function FilePos(var f:TypedFile):Int64;[IOCheck];
      Begin
        FilePos:=System.FilePos(f);
        { in case of reading a file, the buffer is always filled, so the result of Do_FilePos is off by one }
        if (FileRec(f).mode=fmInOut) or
          (FileRec(f).mode=fmInput) then
          dec(FilePos);
      End;


{$ifndef FPUNONE}
{$ifdef FPC_CURRENCY_IS_INT64}
    function round(c : currency) : int64;
      begin
        if c>=0.0 then
          Round:=Trunc(c+0.5)
        else
          Round:=Trunc(c-0.5);
      end;


{$ifndef cpujvm}
    function round(c : comp) : int64;
      begin
        if c>=0.0 then
          round:=Trunc(c+0.5)
        else
          round:=Trunc(c-0.5);
      end;
{$else not cpujvm}
    function round_comp(c : comp) : int64;
      begin
        if c>=0.0 then
          round_comp:=Trunc(c+0.5)
        else
          round_comp:=Trunc(c-0.5);
      end;
{$endif cpujvm}
{$endif FPC_CURRENCY_IS_INT64}


    function Round(d : ValReal) : int64;
      begin
        if d>=0.0 then
          Round:=Trunc(d+0.5)
        else
          Round:=Trunc(d-0.5);
      end;
{$endif FPUNONE}

begin
  { we shouldn't do this because it might confuse user programs, but for now it
    is good enough to get pretty unique tmp file names }
{$ifdef FPC_HAS_FEATURE_RANDOM}
  Randomize;
{$endif FPC_HAS_FEATURE_RANDOM}
  { reset opens with read-only }
  Filemode:=0;
end.