| 1 |
(* InOut.mod provides a compatible PIM [234] InOut module. |
| 2 |
|
| 3 |
Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 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 InOut ; |
| 28 |
|
| 29 |
IMPORT FIO, SFIO, Terminal ; |
| 30 |
FROM FIO IMPORT File, StdIn, StdOut ; |
| 31 |
|
| 32 |
FROM DynamicStrings IMPORT InitString, Mark, KillString, ConCat, |
| 33 |
RemoveWhitePrefix, char, ConCatChar, Length ; |
| 34 |
|
| 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 ; |
| 40 |
IMPORT Keyboard ; |
| 41 |
|
| 42 |
|
| 43 |
CONST |
| 44 |
stdin = 0 ; |
| 45 |
stdout = 1 ; |
| 46 |
|
| 47 |
TYPE |
| 48 |
CharSet = SET OF CHAR ; |
| 49 |
|
| 50 |
VAR |
| 51 |
inFile, outFile: File ; |
| 52 |
inUsed, outUsed: BOOLEAN ; |
| 53 |
|
| 54 |
|
| 55 |
(* |
| 56 |
OpenInput - reads a string from stdin as the filename for reading. |
| 57 |
If the filename ends with `.' then it appends the defext |
| 58 |
extension. The global variable Done is set if all |
| 59 |
was successful. |
| 60 |
*) |
| 61 |
|
| 62 |
PROCEDURE OpenInput (defext: ARRAY OF CHAR) ; |
| 63 |
VAR |
| 64 |
s: String ; |
| 65 |
BEGIN |
| 66 |
s := ReadS() ; |
| 67 |
IF char(s, -1)='.' |
| 68 |
THEN |
| 69 |
s := ConCat(s, Mark(InitString(defext))) |
| 70 |
END ; |
| 71 |
IF SFIO.Exists(s) |
| 72 |
THEN |
| 73 |
inFile := SFIO.OpenToRead(s) ; |
| 74 |
Done := FIO.IsNoError(inFile) ; |
| 75 |
inUsed := TRUE |
| 76 |
ELSE |
| 77 |
Done := FALSE ; |
| 78 |
inUsed := FALSE |
| 79 |
END ; |
| 80 |
s := KillString(s) |
| 81 |
END OpenInput ; |
| 82 |
|
| 83 |
|
| 84 |
(* |
| 85 |
CloseInput - closes an opened input file and returns input back to |
| 86 |
StdIn. |
| 87 |
*) |
| 88 |
|
| 89 |
PROCEDURE CloseInput ; |
| 90 |
BEGIN |
| 91 |
IF inUsed |
| 92 |
THEN |
| 93 |
FIO.Close(inFile) ; |
| 94 |
inFile := StdIn ; |
| 95 |
inUsed := FALSE |
| 96 |
END |
| 97 |
END CloseInput ; |
| 98 |
|
| 99 |
|
| 100 |
(* |
| 101 |
OpenOutput - reads a string from stdin as the filename for writing. |
| 102 |
If the filename ends with `.' then it appends the defext |
| 103 |
extension. The global variable Done is set if all |
| 104 |
was successful. |
| 105 |
*) |
| 106 |
|
| 107 |
PROCEDURE OpenOutput (defext: ARRAY OF CHAR) ; |
| 108 |
VAR |
| 109 |
s: String ; |
| 110 |
BEGIN |
| 111 |
s := ReadS() ; |
| 112 |
IF char(s, -1)='.' |
| 113 |
THEN |
| 114 |
s := ConCat(s, Mark(InitString(defext))) |
| 115 |
END ; |
| 116 |
IF SFIO.Exists(s) |
| 117 |
THEN |
| 118 |
outFile := SFIO.OpenToWrite(s) ; |
| 119 |
Done := FIO.IsNoError(outFile) ; |
| 120 |
outUsed := TRUE |
| 121 |
ELSE |
| 122 |
Done := FALSE ; |
| 123 |
outUsed := FALSE |
| 124 |
END ; |
| 125 |
s := KillString(s) |
| 126 |
END OpenOutput ; |
| 127 |
|
| 128 |
|
| 129 |
(* |
| 130 |
CloseOutput - closes an opened output file and returns output back to |
| 131 |
StdOut. |
| 132 |
*) |
| 133 |
|
| 134 |
PROCEDURE CloseOutput ; |
| 135 |
BEGIN |
| 136 |
IF outUsed |
| 137 |
THEN |
| 138 |
FIO.Close(outFile) ; |
| 139 |
outFile := StdOut ; |
| 140 |
outUsed := FALSE |
| 141 |
END |
| 142 |
END CloseOutput ; |
| 143 |
|
| 144 |
|
| 145 |
(* |
| 146 |
LocalRead - |
| 147 |
*) |
| 148 |
|
| 149 |
PROCEDURE LocalRead (VAR ch: CHAR) ; |
| 150 |
BEGIN |
| 151 |
ch := FIO.ReadChar(inFile) ; |
| 152 |
Done := FIO.IsNoError(inFile) AND (NOT FIO.EOF(inFile)) |
| 153 |
END LocalRead ; |
| 154 |
|
| 155 |
|
| 156 |
(* |
| 157 |
LocalStatus - returns TRUE if more characters may be read. |
| 158 |
*) |
| 159 |
|
| 160 |
PROCEDURE LocalStatus () : BOOLEAN ; |
| 161 |
BEGIN |
| 162 |
IF inUsed |
| 163 |
THEN |
| 164 |
RETURN Done |
| 165 |
ELSE |
| 166 |
RETURN Keyboard.KeyPressed () |
| 167 |
END |
| 168 |
END LocalStatus ; |
| 169 |
|
| 170 |
|
| 171 |
(* |
| 172 |
ReadS - returns a string which has is a sequence of characters. |
| 173 |
Leading white space is ignored and string is terminated |
| 174 |
with a character <= ' '. |
| 175 |
*) |
| 176 |
|
| 177 |
PROCEDURE ReadS () : String ; |
| 178 |
VAR |
| 179 |
s : String ; |
| 180 |
ch: CHAR ; |
| 181 |
BEGIN |
| 182 |
s := InitString('') ; |
| 183 |
REPEAT |
| 184 |
Read(ch) |
| 185 |
UNTIL ch>' ' ; |
| 186 |
WHILE ch>' ' DO |
| 187 |
s := ConCatChar(s, ch) ; |
| 188 |
Read(ch) |
| 189 |
END ; |
| 190 |
(* successful *) |
| 191 |
RETURN( s ) |
| 192 |
END ReadS ; |
| 193 |
|
| 194 |
|
| 195 |
(* |
| 196 |
Read - reads a single character from the current input file. |
| 197 |
Done is set to FALSE if end of file is reached or an |
| 198 |
error occurs. |
| 199 |
*) |
| 200 |
|
| 201 |
PROCEDURE Read (VAR ch: CHAR) ; |
| 202 |
BEGIN |
| 203 |
Terminal.Read(ch) |
| 204 |
END Read ; |
| 205 |
|
| 206 |
|
| 207 |
(* |
| 208 |
ReadString - reads a sequence of characters. Leading white space |
| 209 |
is ignored and the string is terminated with a character |
| 210 |
<= ' ' |
| 211 |
*) |
| 212 |
|
| 213 |
PROCEDURE ReadString (VAR s: ARRAY OF CHAR) ; |
| 214 |
VAR |
| 215 |
h, i: CARDINAL ; |
| 216 |
BEGIN |
| 217 |
(* skip leading spaces *) |
| 218 |
REPEAT |
| 219 |
Read(termCH) |
| 220 |
UNTIL termCH>' ' ; |
| 221 |
s[0] := termCH ; |
| 222 |
i := 1 ; |
| 223 |
h := HIGH(s) ; |
| 224 |
IF i<=h |
| 225 |
THEN |
| 226 |
REPEAT |
| 227 |
Read(termCH) ; |
| 228 |
IF termCH<=' ' |
| 229 |
THEN |
| 230 |
s[i] := nul ; |
| 231 |
(* successful *) |
| 232 |
RETURN |
| 233 |
END ; |
| 234 |
s[i] := termCH ; |
| 235 |
INC(i) |
| 236 |
UNTIL i>h ; |
| 237 |
END ; |
| 238 |
Done := FALSE (* out of space *) |
| 239 |
END ReadString ; |
| 240 |
|
| 241 |
|
| 242 |
(* |
| 243 |
WriteString - writes a string to the output file. |
| 244 |
*) |
| 245 |
|
| 246 |
PROCEDURE WriteString (s: ARRAY OF CHAR) ; |
| 247 |
BEGIN |
| 248 |
FIO.WriteString(outFile, s) ; |
| 249 |
Done := FIO.IsNoError(outFile) |
| 250 |
END WriteString ; |
| 251 |
|
| 252 |
|
| 253 |
(* |
| 254 |
LocalWrite - |
| 255 |
*) |
| 256 |
|
| 257 |
PROCEDURE LocalWrite (ch: CHAR) ; |
| 258 |
BEGIN |
| 259 |
FIO.WriteChar(outFile, ch) ; |
| 260 |
Done := FIO.IsNoError(outFile) ; |
| 261 |
FIO.FlushBuffer (outFile) |
| 262 |
END LocalWrite ; |
| 263 |
|
| 264 |
|
| 265 |
(* |
| 266 |
Write - writes out a single character, ch, to the current output file. |
| 267 |
*) |
| 268 |
|
| 269 |
PROCEDURE Write (ch: CHAR) ; |
| 270 |
BEGIN |
| 271 |
Terminal.Write(ch) |
| 272 |
END Write ; |
| 273 |
|
| 274 |
|
| 275 |
(* |
| 276 |
WriteS - writes a String to the output device. |
| 277 |
It returns the string, s. |
| 278 |
*) |
| 279 |
|
| 280 |
PROCEDURE WriteS (s: String) : String ; |
| 281 |
VAR |
| 282 |
i, h: CARDINAL ; |
| 283 |
BEGIN |
| 284 |
i := 0 ; |
| 285 |
h := Length(s) ; |
| 286 |
WHILE i<h DO |
| 287 |
Write(char(s, i)) ; |
| 288 |
INC(i) |
| 289 |
END ; |
| 290 |
RETURN( s ) |
| 291 |
END WriteS ; |
| 292 |
|
| 293 |
|
| 294 |
(* |
| 295 |
WriteLn - writes a newline to the output file. |
| 296 |
*) |
| 297 |
|
| 298 |
PROCEDURE WriteLn ; |
| 299 |
BEGIN |
| 300 |
IF outUsed |
| 301 |
THEN |
| 302 |
FIO.WriteLine(outFile) ; |
| 303 |
Done := FIO.IsNoError(outFile) |
| 304 |
ELSE |
| 305 |
Terminal.WriteLn |
| 306 |
END |
| 307 |
END WriteLn ; |
| 308 |
|
| 309 |
|
| 310 |
(* |
| 311 |
ReadInt - reads a string and converts it into an INTEGER, x. |
| 312 |
Done is set if an INTEGER is read. |
| 313 |
*) |
| 314 |
|
| 315 |
PROCEDURE ReadInt (VAR x: INTEGER) ; |
| 316 |
VAR |
| 317 |
s: String ; |
| 318 |
BEGIN |
| 319 |
s := RemoveWhitePrefix(ReadS()) ; |
| 320 |
IF char(s, 0) IN CharSet{'-', '+', '0'..'9'} |
| 321 |
THEN |
| 322 |
x := stoi(s) ; |
| 323 |
Done := TRUE |
| 324 |
ELSE |
| 325 |
Done := FALSE |
| 326 |
END ; |
| 327 |
s := KillString(s) |
| 328 |
END ReadInt ; |
| 329 |
|
| 330 |
|
| 331 |
(* |
| 332 |
ReadInt - reads a string and converts it into an INTEGER, x. |
| 333 |
Done is set if an INTEGER is read. |
| 334 |
*) |
| 335 |
|
| 336 |
PROCEDURE ReadCard (VAR x: CARDINAL) ; |
| 337 |
VAR |
| 338 |
s: String ; |
| 339 |
BEGIN |
| 340 |
s := RemoveWhitePrefix(ReadS()) ; |
| 341 |
IF char(s, 0) IN CharSet{'+', '0'..'9'} |
| 342 |
THEN |
| 343 |
x := stoc(s) ; |
| 344 |
Done := TRUE |
| 345 |
ELSE |
| 346 |
Done := FALSE |
| 347 |
END ; |
| 348 |
s := KillString(s) |
| 349 |
END ReadCard ; |
| 350 |
|
| 351 |
|
| 352 |
(* |
| 353 |
WriteCard - writes the CARDINAL, x, to the output file. It ensures |
| 354 |
that the number occupies, n, characters. Leading spaces |
| 355 |
are added if required. |
| 356 |
*) |
| 357 |
|
| 358 |
PROCEDURE WriteCard (x, n: CARDINAL) ; |
| 359 |
BEGIN |
| 360 |
IF KillString(SFIO.WriteS(outFile, ctos(x, n, ' ')))=NIL |
| 361 |
THEN |
| 362 |
END |
| 363 |
END WriteCard ; |
| 364 |
|
| 365 |
|
| 366 |
(* |
| 367 |
WriteInt - writes the INTEGER, x, to the output file. It ensures |
| 368 |
that the number occupies, n, characters. Leading spaces |
| 369 |
are added if required. |
| 370 |
*) |
| 371 |
|
| 372 |
PROCEDURE WriteInt (x: INTEGER; n: CARDINAL) ; |
| 373 |
BEGIN |
| 374 |
IF KillString(SFIO.WriteS(outFile, itos(x, n, ' ', FALSE)))=NIL |
| 375 |
THEN |
| 376 |
END |
| 377 |
END WriteInt ; |
| 378 |
|
| 379 |
|
| 380 |
(* |
| 381 |
WriteOct - writes the CARDINAL, x, to the output file in octal. |
| 382 |
It ensures that the number occupies, n, characters. |
| 383 |
Leading spaces are added if required. |
| 384 |
*) |
| 385 |
|
| 386 |
PROCEDURE WriteOct (x, n: CARDINAL) ; |
| 387 |
BEGIN |
| 388 |
IF KillString(SFIO.WriteS(outFile, CardinalToString(x, n, ' ', 8, FALSE)))=NIL |
| 389 |
THEN |
| 390 |
END |
| 391 |
END WriteOct ; |
| 392 |
|
| 393 |
|
| 394 |
(* |
| 395 |
WriteHex - writes the CARDINAL, x, to the output file in hexadecimal. |
| 396 |
It ensures that the number occupies, n, characters. |
| 397 |
Leading spaces are added if required. |
| 398 |
*) |
| 399 |
|
| 400 |
PROCEDURE WriteHex (x, n: CARDINAL) ; |
| 401 |
BEGIN |
| 402 |
IF KillString(SFIO.WriteS(outFile, CardinalToString(x, n, ' ', 16, TRUE)))=NIL |
| 403 |
THEN |
| 404 |
END |
| 405 |
END WriteHex ; |
| 406 |
|
| 407 |
|
| 408 |
(* |
| 409 |
Init - |
| 410 |
*) |
| 411 |
|
| 412 |
PROCEDURE Init ; |
| 413 |
BEGIN |
| 414 |
inFile := FIO.StdIn ; |
| 415 |
outFile := FIO.StdOut ; |
| 416 |
inUsed := FALSE ; |
| 417 |
outUsed := FALSE ; |
| 418 |
AssignRead(LocalRead, LocalStatus, Done) ; |
| 419 |
AssignWrite(LocalWrite, Done) |
| 420 |
END Init ; |
| 421 |
|
| 422 |
|
| 423 |
BEGIN |
| 424 |
Init |
| 425 |
END InOut. |