1 (* Library module defined by the International Standard
2 Information technology - programming languages
3 BS ISO/IEC 10514-1:1996E Part 1: Modula-2, Base Language.
5 Copyright ISO/IEC (International Organization for Standardization
6 and International Electrotechnical Commission) 1996-2021.
8 It may be freely copied for the purpose of implementation (see page
9 707 of the Information technology - Programming languages Part 1:
10 Modula-2, Base Language. BS ISO/IEC 10514-1:1996). *)
12 IMPLEMENTATION MODULE LongIO
;
14 (* Input and output of real numbers in decimal text form
15 over specified channels. The read result is of the
16 type IOConsts.ReadResults.
19 FROM TextIO
IMPORT WriteChar
, ReadChar
;
20 FROM StringChan
IMPORT writeString
, writeFieldWidth
;
21 FROM IOChan
IMPORT SetReadResult
;
22 FROM IOConsts
IMPORT ReadResults
;
24 FROM ConvStringLong
IMPORT RealToFixedString
, RealToFloatString
,
27 FROM ConvTypes
IMPORT ScanClass
, ScanState
;
28 FROM DynamicStrings
IMPORT String
, char
, KillString
, Length
, InitString
, ConCatChar
, string
;
29 FROM LongConv
IMPORT ScanReal
;
30 FROM ldtoa
IMPORT strtold
;
31 FROM TextUtil
IMPORT SkipSpaces
;
34 (* The text form of a signed fixed-point real number is
35 ["+" | "-"], decimal digit, {decimal digit},
36 [".", {decimal digit}]
38 The text form of a signed floating-point real number is
39 signed fixed-point real number,
40 "E", ["+" | "-"], decimal digit, {decimal digit}
43 PROCEDURE ReadReal (cid
: IOChan.ChanId
; VAR real
: LONGREAL);
44 (* Skips leading spaces, and removes any remaining characters
45 from cid that form part of a signed fixed or floating
46 point number. The value of this number is assigned to real.
47 The read result is set to the value allRight, outOfRange,
48 wrongFormat, endOfLine, or endOfInput.
52 nextState
: ScanState
;
59 nextState
:= ScanReal
;
61 nextState(ch
, chClass
, nextState
) ;
66 UNTIL chClass#padding
;
70 WHILE chClass
=valid
DO
71 s
:= ConCatChar(s
, ch
) ;
73 nextState(ch
, chClass
, nextState
)
75 real
:= strtold(string(s
), error
) ;
79 SetReadResult(cid
, outOfRange
)
81 SetReadResult(cid
, allRight
)
84 SetReadResult(cid
, wrongFormat
)
89 PROCEDURE WriteFloat (cid
: IOChan.ChanId
; real
: LONGREAL;
90 sigFigs
: CARDINAL; width
: CARDINAL);
91 (* Writes the value of real to cid in floating-point text form,
92 with sigFigs significant figures, in a field of the given
98 s
:= RealToFloatString(real
, sigFigs
) ;
99 writeFieldWidth(cid
, s
, width
) ;
104 PROCEDURE WriteEng (cid
: IOChan.ChanId
; real
: LONGREAL;
105 sigFigs
: CARDINAL; width
: CARDINAL);
106 (* As for WriteFloat, except that the number is scaled with
107 one to three digits in the whole number part, and with an
108 exponent that is a multiple of three.
113 s
:= RealToEngString(real
, sigFigs
) ;
114 writeFieldWidth(cid
, s
, width
) ;
119 PROCEDURE WriteFixed (cid
: IOChan.ChanId
; real
: LONGREAL;
120 place
: INTEGER; width
: CARDINAL);
121 (* Writes the value of real to cid in fixed-point text form,
122 rounded to the given place relative to the decimal point,
123 in a field of the given minimum width.
128 s
:= RealToFixedString(real
, place
) ;
129 writeFieldWidth(cid
, s
, width
) ;
134 PROCEDURE WriteReal (cid
: IOChan.ChanId
;
135 real
: LONGREAL; width
: CARDINAL);
136 (* Writes the value of real to cid, as WriteFixed if the sign
137 and magnitude can be shown in the given width, or otherwise
138 as WriteFloat. The number of places or significant digits
139 depends on the given width.
147 s
:= RealToFixedString(real
, sigFigs
) ;
150 writeFieldWidth(cid
, s
, width
) ;
159 s
:= RealToFloatString(real
, sigFigs
) ;
162 writeFieldWidth(cid
, s
, width
) ;