1 (* InOut.mod provides a compatible PIM [234] InOut module.
3 Copyright (C) 2004-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 InOut ;
29 IMPORT FIO, SFIO, Terminal ;
30 FROM FIO IMPORT File, StdIn, StdOut ;
32 FROM DynamicStrings IMPORT String, InitString, Mark, KillString, ConCat,
33 RemoveWhitePrefix, char, ConCatChar, Length ;
35 FROM StringConvert IMPORT CardinalToString, stoc, stoi, ctos, itos ;
36 FROM ASCII IMPORT nul ;
37 FROM SYSTEM IMPORT ADR ;
38 FROM libc IMPORT read, write ;
39 FROM Termbase IMPORT AssignRead, AssignWrite ;
48 CharSet = SET OF CHAR ;
57 OpenInput - reads a string from stdin as the filename for reading.
58 If the filename ends with `.' then it appends the defext
59 extension. The global variable Done is set if all
63 PROCEDURE OpenInput (defext: ARRAY OF CHAR) ;
70 s := ConCat(s, Mark(InitString(defext)))
74 in := SFIO.OpenToRead(s) ;
75 Done := FIO.IsNoError(in) ;
86 CloseInput - closes an opened input file and returns input back to
90 PROCEDURE CloseInput ;
102 OpenOutput - reads a string from stdin as the filename for writing.
103 If the filename ends with `.' then it appends the defext
104 extension. The global variable Done is set if all
108 PROCEDURE OpenOutput (defext: ARRAY OF CHAR) ;
115 s := ConCat(s, Mark(InitString(defext)))
119 out := SFIO.OpenToWrite(s) ;
120 Done := FIO.IsNoError(out) ;
131 CloseOutput - closes an opened output file and returns output back to
135 PROCEDURE CloseOutput ;
150 PROCEDURE LocalRead (VAR ch: CHAR) ;
152 ch := FIO.ReadChar(in) ;
153 Done := FIO.IsNoError(in) AND (NOT FIO.EOF(in))
158 LocalStatus - returns TRUE if more characters may be read.
161 PROCEDURE LocalStatus () : BOOLEAN ;
167 RETURN Keyboard.KeyPressed ()
173 ReadS - returns a string which has is a sequence of characters.
174 Leading white space is ignored and string is terminated
175 with a character <= ' '.
178 PROCEDURE ReadS () : String ;
183 s := InitString('') ;
188 s := ConCatChar(s, ch) ;
197 Read - reads a single character from the current input file.
198 Done is set to FALSE if end of file is reached or an
202 PROCEDURE Read (VAR ch: CHAR) ;
209 ReadString - reads a sequence of characters. Leading white space
210 is ignored and the string is terminated with a character
214 PROCEDURE ReadString (VAR s: ARRAY OF CHAR) ;
218 (* skip leading spaces *)
239 Done := FALSE (* out of space *)
244 WriteString - writes a string to the output file.
247 PROCEDURE WriteString (s: ARRAY OF CHAR) ;
249 FIO.WriteString(out, s) ;
250 Done := FIO.IsNoError(out)
258 PROCEDURE LocalWrite (ch: CHAR) ;
260 FIO.WriteChar(out, ch) ;
261 Done := FIO.IsNoError(out)
265 FIO.WriteChar(out, ch) ;
266 Done := FIO.IsNoError(out)
268 Done := (write(stdout, ADR(ch), 1) = 1)
275 Write - writes out a single character, ch, to the current output file.
278 PROCEDURE Write (ch: CHAR) ;
285 WriteS - writes a String to the output device.
286 It returns the string, s.
289 PROCEDURE WriteS (s: String) : String ;
304 WriteLn - writes a newline to the output file.
312 Done := FIO.IsNoError(out)
320 ReadInt - reads a string and converts it into an INTEGER, x.
321 Done is set if an INTEGER is read.
324 PROCEDURE ReadInt (VAR x: INTEGER) ;
328 s := RemoveWhitePrefix(ReadS()) ;
329 IF char(s, 0) IN CharSet{'-', '+', '0'..'9'}
341 ReadInt - reads a string and converts it into an INTEGER, x.
342 Done is set if an INTEGER is read.
345 PROCEDURE ReadCard (VAR x: CARDINAL) ;
349 s := RemoveWhitePrefix(ReadS()) ;
350 IF char(s, 0) IN CharSet{'+', '0'..'9'}
362 WriteCard - writes the CARDINAL, x, to the output file. It ensures
363 that the number occupies, n, characters. Leading spaces
364 are added if required.
367 PROCEDURE WriteCard (x, n: CARDINAL) ;
369 IF KillString(SFIO.WriteS(out, ctos(x, n, ' ')))=NIL
376 WriteInt - writes the INTEGER, x, to the output file. It ensures
377 that the number occupies, n, characters. Leading spaces
378 are added if required.
381 PROCEDURE WriteInt (x: INTEGER; n: CARDINAL) ;
383 IF KillString(SFIO.WriteS(out, itos(x, n, ' ', FALSE)))=NIL
390 WriteOct - writes the CARDINAL, x, to the output file in octal.
391 It ensures that the number occupies, n, characters.
392 Leading spaces are added if required.
395 PROCEDURE WriteOct (x, n: CARDINAL) ;
397 IF KillString(SFIO.WriteS(out, CardinalToString(x, n, ' ', 8, FALSE)))=NIL
404 WriteHex - writes the CARDINAL, x, to the output file in hexadecimal.
405 It ensures that the number occupies, n, characters.
406 Leading spaces are added if required.
409 PROCEDURE WriteHex (x, n: CARDINAL) ;
411 IF KillString(SFIO.WriteS(out, CardinalToString(x, n, ' ', 16, TRUE)))=NIL
427 AssignRead(LocalRead, LocalStatus, Done) ;
428 AssignWrite(LocalWrite, Done)