1 (* WholeIO.mod implement the ISO WholeIO specification.
3 Copyright (C) 2008-2023 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 WholeIO ;
29 FROM ConvTypes IMPORT ScanClass, ScanState ;
30 FROM TextIO IMPORT WriteChar, ReadChar ;
31 FROM DynamicStrings IMPORT String, char, KillString, Length ;
32 FROM StringConvert IMPORT IntegerToString, CardinalToString ;
33 FROM WholeConv IMPORT ScanInt, ScanCard ;
34 FROM StringChan IMPORT writeString ;
35 FROM IOConsts IMPORT ReadResults ;
38 (* Input and output of whole numbers in decimal text form
39 over specified channels. The read result is of the
40 type IOConsts.ReadResults.
45 (* The text form of a signed whole number is
46 ["+" | "-"], decimal digit, {decimal digit}
48 The text form of an unsigned whole number is
49 decimal digit, {decimal digit}
52 PROCEDURE ReadInt (cid: IOChan.ChanId; VAR int: INTEGER) ;
53 (* Skips leading spaces, and removes any remaining characters
54 from cid that form part of a signed whole number. The
55 value of this number is assigned to int. The read result
56 is set to the value allRight, outOfRange, wrongFormat,
57 endOfLine, or endOfInput.
61 nextState: ScanState ;
69 nextState := ScanInt ;
71 nextState(ch, chClass, nextState) ;
79 negative := NOT negative
81 c := c*10+(ORD(ch)-ORD('0'))
88 UNTIL (chClass=invalid) OR (chClass=terminator) ;
93 IF c=VAL(CARDINAL, MAX(INTEGER))+1
96 ELSIF c<=VAL(CARDINAL, MAX(INTEGER))
98 int := -VAL(INTEGER, c)
101 IOChan.SetReadResult(cid, outOfRange)
110 PROCEDURE WriteInt (cid: IOChan.ChanId; int: INTEGER;
112 (* Writes the value of int to cid in text form, in a field of
113 the given minimum width. *)
117 s := IntegerToString(int, width, ' ', TRUE, 10, FALSE) ;
118 writeString(cid, s) ;
123 PROCEDURE ReadCard (cid: IOChan.ChanId; VAR card: CARDINAL) ;
124 (* Skips leading spaces, and removes any remaining characters
125 from cid that form part of an unsigned whole number. The
126 value of this number is assigned to card. The read result
127 is set to the value allRight, outOfRange, wrongFormat,
128 endOfLine, or endOfInput.
131 chClass : ScanClass ;
132 nextState: ScanState ;
138 nextState := ScanCard ;
140 nextState(ch, chClass, nextState) ;
147 c := c*10+(ORD(ch)-ORD('0'))
150 ELSIF chClass=padding
154 UNTIL (chClass=invalid) OR (chClass=terminator) ;
155 IF chClass=terminator
162 PROCEDURE WriteCard (cid: IOChan.ChanId; card: CARDINAL;
164 (* Writes the value of card to cid in text form, in a field
165 of the given minimum width. *)
169 s := CardinalToString(card, width, ' ', 10, FALSE) ;
170 writeString(cid, s) ;