1 (* LongWholeIO.mod provides a WholeIO interface for gm2 LONGINT/LONGCARD.
3 Copyright (C) 2008-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 LongWholeIO ;
29 FROM ConvTypes IMPORT ScanClass, ScanState ;
30 FROM TextIO IMPORT WriteChar, ReadChar ;
31 FROM DynamicStrings IMPORT String, char, KillString, Length ;
32 FROM StringConvert IMPORT LongIntegerToString, LongCardinalToString ;
33 FROM WholeConv IMPORT ScanInt, ScanCard ;
34 FROM StringChan IMPORT writeString ;
35 FROM IOConsts IMPORT ReadResults ;
36 FROM TextUtil IMPORT SkipSpaces ;
39 (* Input and output of whole numbers in decimal text form
40 over specified channels. The read result is of the
41 type IOConsts.ReadResults.
44 (* The text form of a signed whole number is
45 ["+" | "-"], decimal digit, {decimal digit}
47 The text form of an unsigned whole number is
48 decimal digit, {decimal digit}
51 PROCEDURE ReadInt (cid: IOChan.ChanId; VAR int: LONGINT) ;
52 (* Skips leading spaces, and removes any remaining characters
53 from cid that form part of a signed whole number. The
54 value of this number is assigned to int. The read result
55 is set to the value allRight, outOfRange, wrongFormat,
56 endOfLine, or endOfInput.
60 nextState: ScanState ;
69 nextState := ScanInt ;
71 nextState(ch, chClass, nextState) ;
79 negative := NOT negative
81 c := c*10+VAL(LONGCARD, ORD(ch)-ORD('0'))
88 UNTIL (chClass=invalid) OR (chClass=terminator) ;
93 IF c=VAL(LONGCARD, MAX(LONGINT))+1
96 ELSIF c<=VAL(LONGCARD, MAX(LONGINT))
98 int := -VAL(LONGINT, c)
101 IOChan.SetReadResult(cid, outOfRange)
110 PROCEDURE WriteInt (cid: IOChan.ChanId; int: LONGINT;
112 (* Writes the value of int to cid in text form, in a field of
113 the given minimum width. *)
117 s := LongIntegerToString (int, width, ' ', int < 0, 10, FALSE) ;
118 writeString (cid, s) ;
123 PROCEDURE ReadCard (cid: IOChan.ChanId; VAR card: LONGCARD) ;
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 ;
139 nextState := ScanCard ;
141 nextState(ch, chClass, nextState) ;
148 c := c*10+VAL(LONGCARD, ORD(ch)-ORD('0'))
151 ELSIF chClass=padding
155 UNTIL (chClass=invalid) OR (chClass=terminator) ;
156 IF chClass=terminator
163 PROCEDURE WriteCard (cid: IOChan.ChanId; card: LONGCARD;
165 (* Writes the value of card to cid in text form, in a field
166 of the given minimum width. *)
170 s := LongCardinalToString(card, width, ' ', 10, FALSE) ;
171 writeString(cid, s) ;