]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/gm2-libs-iso/LongWholeIO.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs-iso / LongWholeIO.mod
1 (* LongWholeIO.mod provides a WholeIO interface for gm2 LONGINT/LONGCARD.
2
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6 This file is part of GNU Modula-2.
7
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)
11 any later version.
12
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.
17
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.
21
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/>. *)
26
27 IMPLEMENTATION MODULE LongWholeIO ;
28
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 ;
37
38
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.
42 *)
43
44 (* The text form of a signed whole number is
45 ["+" | "-"], decimal digit, {decimal digit}
46
47 The text form of an unsigned whole number is
48 decimal digit, {decimal digit}
49 *)
50
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.
57 *)
58 VAR
59 chClass : ScanClass ;
60 nextState: ScanState ;
61 c : LONGCARD ;
62 ch : CHAR ;
63 negative : BOOLEAN ;
64 BEGIN
65 SkipSpaces (cid) ;
66 ReadChar(cid, ch) ;
67 negative := FALSE ;
68 c := 0 ;
69 nextState := ScanInt ;
70 REPEAT
71 nextState(ch, chClass, nextState) ;
72 IF chClass=valid
73 THEN
74 IF ch='+'
75 THEN
76 (* ignore *)
77 ELSIF ch='-'
78 THEN
79 negative := NOT negative
80 ELSE
81 c := c*10+VAL(LONGCARD, ORD(ch)-ORD('0'))
82 END ;
83 ReadChar(cid, ch)
84 ELSIF chClass=padding
85 THEN
86 ReadChar(cid, ch)
87 END
88 UNTIL (chClass=invalid) OR (chClass=terminator) ;
89 IF chClass=terminator
90 THEN
91 IF negative
92 THEN
93 IF c=VAL(LONGCARD, MAX(LONGINT))+1
94 THEN
95 int := MIN(LONGINT)
96 ELSIF c<=VAL(LONGCARD, MAX(LONGINT))
97 THEN
98 int := -VAL(LONGINT, c)
99 ELSE
100 (* overflow *)
101 IOChan.SetReadResult(cid, outOfRange)
102 END
103 ELSE
104 int := c
105 END
106 END
107 END ReadInt ;
108
109
110 PROCEDURE WriteInt (cid: IOChan.ChanId; int: LONGINT;
111 width: CARDINAL) ;
112 (* Writes the value of int to cid in text form, in a field of
113 the given minimum width. *)
114 VAR
115 s: String ;
116 BEGIN
117 s := LongIntegerToString (int, width, ' ', int < 0, 10, FALSE) ;
118 writeString (cid, s) ;
119 s := KillString (s)
120 END WriteInt ;
121
122
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.
129 *)
130 VAR
131 chClass : ScanClass ;
132 nextState: ScanState ;
133 ch : CHAR ;
134 c : LONGCARD ;
135 BEGIN
136 SkipSpaces (cid) ;
137 ReadChar(cid, ch) ;
138 c := 0 ;
139 nextState := ScanCard ;
140 REPEAT
141 nextState(ch, chClass, nextState) ;
142 IF chClass=valid
143 THEN
144 IF ch='+'
145 THEN
146 (* ignore *)
147 ELSE
148 c := c*10+VAL(LONGCARD, ORD(ch)-ORD('0'))
149 END ;
150 ReadChar(cid, ch)
151 ELSIF chClass=padding
152 THEN
153 ReadChar(cid, ch)
154 END
155 UNTIL (chClass=invalid) OR (chClass=terminator) ;
156 IF chClass=terminator
157 THEN
158 card := c
159 END
160 END ReadCard ;
161
162
163 PROCEDURE WriteCard (cid: IOChan.ChanId; card: LONGCARD;
164 width: CARDINAL);
165 (* Writes the value of card to cid in text form, in a field
166 of the given minimum width. *)
167 VAR
168 s: String ;
169 BEGIN
170 s := LongCardinalToString(card, width, ' ', 10, FALSE) ;
171 writeString(cid, s) ;
172 s := KillString(s)
173 END WriteCard ;
174
175
176 END LongWholeIO.