1 (* FpuIO.mod implements a fixed format input/output for REAL/LONGREAL.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE FpuIO
;
29 FROM StrIO
IMPORT ReadString
, WriteString
, WriteLn
;
30 FROM StrLib
IMPORT StrLen
, StrRemoveWhitePrefix
;
31 FROM ASCII
IMPORT nul
;
32 FROM DynamicStrings
IMPORT String
, InitString
, KillString
, CopyOut
,
33 InitStringDB
, InitStringCharStarDB
,
34 InitStringCharDB
, MultDB
, DupDB
, SliceDB
;
36 FROM StringConvert
IMPORT StringToLongreal
, LongrealToString
,
37 LongIntegerToString
, StringToLongInteger
;
40 #undef GM2_DEBUG_FPUIO
41 if defined(GM2_DEBUG_FPUIO)
42 # define InitString(X) InitStringDB(X, __FILE__, __LINE__)
43 # define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__)
44 # define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__)
45 # define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__)
46 # define Dup(X) DupDB(X, __FILE__, __LINE__)
47 # define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__)
56 PROCEDURE ReadReal (VAR x
: REAL) ;
58 a
: ARRAY [0..MaxLineLength
] OF CHAR ;
66 WriteReal - converts a REAL number, x, which has a, TotalWidth, and
67 FractionWidth into, string, a.
70 PROCEDURE WriteReal (x
: REAL; TotalWidth
, FractionWidth
: CARDINAL) ;
72 a
: ARRAY [0..MaxLineLength
] OF CHAR ;
74 RealToStr(x
, TotalWidth
, FractionWidth
, a
) ;
79 PROCEDURE StrToReal (a
: ARRAY OF CHAR ; VAR x
: REAL) ;
83 StrToLongReal(a
, lr
) ; (* let StrToLongReal do the work and we convert the result back to REAL *)
88 PROCEDURE ReadLongReal (VAR x
: LONGREAL) ;
90 a
: ARRAY [0..MaxLineLength
] OF CHAR ;
98 WriteLongReal - converts a LONGREAL number, x, which has a, TotalWidth, and
99 FractionWidth into a string.
102 PROCEDURE WriteLongReal (x
: LONGREAL; TotalWidth
, FractionWidth
: CARDINAL) ;
104 a
: ARRAY [0..MaxLineLength
] OF CHAR ;
106 LongRealToStr(x
, TotalWidth
, FractionWidth
, a
) ;
111 PROCEDURE StrToLongReal (a
: ARRAY OF CHAR ; VAR x
: LONGREAL) ;
117 x
:= StringToLongreal(s
, found
) ;
123 RealToStr - converts a LONGREAL number, Real, which has, TotalWidth, and
124 FractionWidth into a string.
127 PROCEDURE RealToStr (x
: REAL; TotalWidth
, FractionWidth
: CARDINAL; VAR a
: ARRAY OF CHAR) ;
131 lr
:= VAL(LONGREAL, x
) ;
132 LongRealToStr(lr
, TotalWidth
, FractionWidth
, a
)
137 LongRealToStr - converts a LONGREAL number, Real, which has, TotalWidth, and
138 FractionWidth into a string.
141 PROCEDURE LongRealToStr (x
: LONGREAL; TotalWidth
, FractionWidth
: CARDINAL; VAR a
: ARRAY OF CHAR) ;
145 s
:= LongrealToString(x
, TotalWidth
, FractionWidth
) ;
151 PROCEDURE ReadLongInt (VAR x
: LONGINT) ;
153 a
: ARRAY [0..MaxLineLength
] OF CHAR ;
160 PROCEDURE WriteLongInt (x
: LONGINT; n
: CARDINAL) ;
162 a
: ARRAY [0..MaxLineLength
] OF CHAR ;
164 LongIntToStr(x
, n
, a
) ;
169 PROCEDURE LongIntToStr (x
: LONGINT; n
: CARDINAL ; VAR a
: ARRAY OF CHAR) ;
173 s
:= LongIntegerToString(x
, n
, ' ', FALSE, 10, TRUE) ;
179 PROCEDURE StrToLongInt (a
: ARRAY OF CHAR ; VAR x
: LONGINT) ;
185 x
:= StringToLongInteger(s
, 10, found
) ;