ack/lang/m2/libm2/PascalIO.mod

479 lines
9.3 KiB
Modula-2
Raw Normal View History

1987-08-19 18:07:01 +00:00
(*$R-*)
1987-07-03 16:07:18 +00:00
IMPLEMENTATION MODULE PascalIO;
1987-06-26 15:59:52 +00:00
IMPORT Unix;
IMPORT Conversions;
IMPORT Traps;
IMPORT RealConversions;
FROM TTY IMPORT isatty;
FROM Storage IMPORT ALLOCATE;
FROM SYSTEM IMPORT ADR;
TYPE charset = SET OF CHAR;
btype = (reading, writing, free);
CONST BUFSIZ = 1024; (* Tunable *)
spaces = charset{11C, 12C, 13C, 14C, 15C, ' '};
TYPE IOBuf = RECORD
type: btype;
eof: BOOLEAN;
next: Text;
fildes: INTEGER;
cnt: INTEGER;
maxcnt: INTEGER;
bufferedcount: INTEGER;
buf: ARRAY [1..BUFSIZ] OF CHAR;
END;
Text = POINTER TO IOBuf;
numbuf = ARRAY[0..255] OF CHAR;
VAR ibuf, obuf: IOBuf;
head: Text;
PROCEDURE Reset(VAR InputText: Text; Filename: ARRAY OF CHAR);
VAR i: CARDINAL;
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
doclose(InputText);
getstruct(InputText);
WITH InputText^ DO
1987-06-26 15:59:52 +00:00
eof := FALSE;
FOR i := 0 TO HIGH(Filename) DO
buf[i+1] := Filename[i];
END;
buf[HIGH(Filename)+2] := 0C;
fildes := Unix.open(ADR(buf), 0);
1987-06-26 15:59:52 +00:00
IF fildes < 0 THEN
Traps.Message("could not open input file");
HALT;
END;
type := reading;
cnt := 1;
maxcnt := 0;
bufferedcount := BUFSIZ;
END;
END Reset;
PROCEDURE Rewrite(VAR OutputText: Text; Filename: ARRAY OF CHAR);
VAR i: CARDINAL;
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
doclose(OutputText);
getstruct(OutputText);
WITH OutputText^ DO
1987-06-26 15:59:52 +00:00
eof := FALSE;
FOR i := 0 TO HIGH(Filename) DO
buf[i+1] := Filename[i];
END;
buf[HIGH(Filename)+2] := 0C;
fildes := Unix.creat(ADR(buf), 666B);
1987-06-26 15:59:52 +00:00
IF fildes < 0 THEN
Traps.Message("could not open output file");
HALT;
END;
type := writing;
cnt := 0;
maxcnt := 0;
bufferedcount := BUFSIZ;
END;
END Rewrite;
1987-07-03 16:07:18 +00:00
PROCEDURE CloseOutput();
1987-06-26 15:59:52 +00:00
VAR text: Text;
BEGIN
text := head;
WHILE text # NIL DO
doclose(text);
text := text^.next;
END;
1987-07-03 16:07:18 +00:00
END CloseOutput;
1987-06-26 15:59:52 +00:00
PROCEDURE doclose(text: Text);
VAR dummy: INTEGER;
BEGIN
1987-07-03 16:07:18 +00:00
IF text # Notext THEN
1987-06-26 15:59:52 +00:00
WITH text^ DO
IF type = writing THEN
Flush(text);
END;
IF type # free THEN
type := free;
dummy := Unix.close(fildes);
END;
END;
END;
END doclose;
PROCEDURE getstruct(VAR text: Text);
BEGIN
text := head;
WHILE (text # NIL) AND (text^.type # free) DO
text := text^.next;
END;
IF text = NIL THEN
NEW(text);
text^.next := head;
head := text;
END;
END getstruct;
PROCEDURE chk(text: Text; tp: btype);
BEGIN
IF text^.type # tp THEN
IF tp = reading THEN
Traps.Message("input text expected");
ELSE
Traps.Message("output text expected");
END;
HALT;
END;
END chk;
1987-07-03 16:07:18 +00:00
PROCEDURE ReadChar(InputText: Text; VAR ch : CHAR);
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
ch := NextChar(InputText);
Get(InputText);
END ReadChar;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE NextChar(InputText: Text): CHAR;
1987-06-26 15:59:52 +00:00
VAR c: CHAR;
BEGIN
1987-07-03 16:07:18 +00:00
chk(InputText, reading);
WITH InputText^ DO
1987-06-26 15:59:52 +00:00
IF cnt <= maxcnt THEN
c := buf[cnt];
ELSE
1987-07-03 16:07:18 +00:00
c := FillBuf(InputText);
1987-06-26 15:59:52 +00:00
END;
END;
RETURN c;
1987-07-03 16:07:18 +00:00
END NextChar;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE Get(InputText: Text);
1987-06-26 15:59:52 +00:00
VAR dummy: CHAR;
BEGIN
1987-07-03 16:07:18 +00:00
chk(InputText, reading);
WITH InputText^ DO
1987-06-26 15:59:52 +00:00
IF eof THEN
1987-06-29 12:27:50 +00:00
Traps.Message("unexpected EOF");
HALT;
1987-06-26 15:59:52 +00:00
END;
IF cnt > maxcnt THEN
1987-07-03 16:07:18 +00:00
dummy := FillBuf(InputText);
1987-06-26 15:59:52 +00:00
END;
INC(cnt);
END;
END Get;
PROCEDURE FillBuf(ib: Text) : CHAR;
VAR c : CHAR;
BEGIN
WITH ib^ DO
IF eof THEN RETURN 0C; END;
maxcnt := Unix.read(fildes, ADR(buf), bufferedcount);
cnt := 1;
IF maxcnt <= 0 THEN
c := 0C;
eof := TRUE;
ELSE
c := buf[1];
END;
END;
RETURN c;
END FillBuf;
1987-07-03 16:07:18 +00:00
PROCEDURE Eoln(InputText: Text): BOOLEAN;
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
RETURN NextChar(InputText) = 12C;
1987-06-26 15:59:52 +00:00
END Eoln;
1987-07-03 16:07:18 +00:00
PROCEDURE Eof(InputText: Text): BOOLEAN;
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
RETURN (NextChar(InputText) = 0C) AND InputText^.eof;
1987-06-26 15:59:52 +00:00
END Eof;
1987-07-03 16:07:18 +00:00
PROCEDURE ReadLn(InputText: Text);
1987-06-26 15:59:52 +00:00
VAR ch: CHAR;
BEGIN
REPEAT
1987-07-03 16:07:18 +00:00
ReadChar(InputText, ch)
1987-06-26 15:59:52 +00:00
UNTIL ch = 12C;
END ReadLn;
PROCEDURE Flush(ob: Text);
VAR dummy: INTEGER;
BEGIN
WITH ob^ DO
dummy := Unix.write(fildes, ADR(buf), cnt);
cnt := 0;
END;
END Flush;
1987-07-03 16:07:18 +00:00
PROCEDURE WriteChar(OutputText: Text; ch: CHAR);
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
chk(OutputText, writing);
WITH OutputText^ DO
1987-06-26 15:59:52 +00:00
INC(cnt);
buf[cnt] := ch;
IF cnt >= bufferedcount THEN
1987-07-03 16:07:18 +00:00
Flush(OutputText);
1987-06-26 15:59:52 +00:00
END;
END;
1987-07-03 16:07:18 +00:00
END WriteChar;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE WriteLn(OutputText: Text);
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
WriteChar(OutputText, 12C);
1987-06-26 15:59:52 +00:00
END WriteLn;
1987-07-03 16:07:18 +00:00
PROCEDURE Page(OutputText: Text);
1987-06-26 15:59:52 +00:00
BEGIN
1987-07-03 16:07:18 +00:00
WriteChar(OutputText, 14C);
1987-06-26 15:59:52 +00:00
END Page;
1987-07-03 16:07:18 +00:00
PROCEDURE ReadInteger(InputText: Text; VAR int : INTEGER);
1987-06-26 15:59:52 +00:00
CONST
SAFELIMITDIV10 = MAX(INTEGER) DIV 10;
SAFELIMITREM10 = MAX(INTEGER) MOD 10;
VAR
neg : BOOLEAN;
safedigit: CARDINAL;
ch: CHAR;
chvalue: CARDINAL;
BEGIN
1987-07-03 16:07:18 +00:00
WHILE NextChar(InputText) IN spaces DO
Get(InputText);
1987-06-26 15:59:52 +00:00
END;
1987-07-03 16:07:18 +00:00
ch := NextChar(InputText);
1987-06-26 15:59:52 +00:00
IF ch = '-' THEN
1987-07-03 16:07:18 +00:00
Get(InputText);
ch := NextChar(InputText);
1987-06-26 15:59:52 +00:00
neg := TRUE;
ELSIF ch = '+' THEN
1987-07-03 16:07:18 +00:00
Get(InputText);
ch := NextChar(InputText);
1987-06-26 15:59:52 +00:00
neg := FALSE;
ELSE
neg := FALSE
END;
safedigit := SAFELIMITREM10;
IF neg THEN safedigit := safedigit + 1 END;
int := 0;
IF (ch >= '0') AND (ch <= '9') THEN
WHILE (ch >= '0') & (ch <= '9') DO
chvalue := ORD(ch) - ORD('0');
IF (int < -SAFELIMITDIV10) OR
( (int = -SAFELIMITDIV10) AND
(chvalue > safedigit)) THEN
Traps.Message("integer too large");
HALT;
ELSE
int := 10*int - VAL(INTEGER, chvalue);
1987-07-03 16:07:18 +00:00
Get(InputText);
ch := NextChar(InputText);
1987-06-26 15:59:52 +00:00
END;
END;
IF NOT neg THEN
int := -int
END;
ELSE
Traps.Message("integer expected");
HALT;
END;
1987-07-03 16:07:18 +00:00
END ReadInteger;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE ReadCardinal(InputText: Text; VAR card : CARDINAL);
1987-06-26 15:59:52 +00:00
CONST
SAFELIMITDIV10 = MAX(CARDINAL) DIV 10;
SAFELIMITREM10 = MAX(CARDINAL) MOD 10;
VAR
ch : CHAR;
safedigit: CARDINAL;
chvalue: CARDINAL;
BEGIN
1987-07-03 16:07:18 +00:00
WHILE NextChar(InputText) IN spaces DO
Get(InputText);
1987-06-26 15:59:52 +00:00
END;
1987-07-03 16:07:18 +00:00
ch := NextChar(InputText);
1987-06-26 15:59:52 +00:00
safedigit := SAFELIMITREM10;
card := 0;
IF (ch >= '0') AND (ch <= '9') THEN
WHILE (ch >= '0') & (ch <= '9') DO
chvalue := ORD(ch) - ORD('0');
IF (card > SAFELIMITDIV10) OR
( (card = SAFELIMITDIV10) AND
(chvalue > safedigit)) THEN
Traps.Message("cardinal too large");
HALT;
ELSE
card := 10*card + chvalue;
1987-07-03 16:07:18 +00:00
Get(InputText);
ch := NextChar(InputText);
1987-06-26 15:59:52 +00:00
END;
END;
ELSE
Traps.Message("cardinal expected");
HALT;
END;
1987-07-03 16:07:18 +00:00
END ReadCardinal;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE ReadReal(InputText: Text; VAR real: REAL);
1987-06-26 15:59:52 +00:00
VAR
buf: numbuf;
ch: CHAR;
ok: BOOLEAN;
index: INTEGER;
1987-06-29 12:27:50 +00:00
PROCEDURE inch(): CHAR;
BEGIN
buf[index] := ch;
INC(index);
1987-07-03 16:07:18 +00:00
Get(InputText);
RETURN NextChar(InputText);
1987-06-29 12:27:50 +00:00
END inch;
1987-06-26 15:59:52 +00:00
BEGIN
index := 0;
ok := TRUE;
1987-07-03 16:07:18 +00:00
WHILE NextChar(InputText) IN spaces DO
Get(InputText);
1987-06-26 15:59:52 +00:00
END;
1987-07-03 16:07:18 +00:00
ch := NextChar(InputText);
1987-06-26 15:59:52 +00:00
IF (ch ='+') OR (ch = '-') THEN
1987-06-29 12:27:50 +00:00
ch := inch();
1987-06-26 15:59:52 +00:00
END;
IF (ch >= '0') AND (ch <= '9') THEN
WHILE (ch >= '0') AND (ch <= '9') DO
1987-06-29 12:27:50 +00:00
ch := inch();
1987-06-26 15:59:52 +00:00
END;
IF (ch = '.') THEN
1987-06-29 12:27:50 +00:00
ch := inch();
1987-06-26 15:59:52 +00:00
IF (ch >= '0') AND (ch <= '9') THEN
WHILE (ch >= '0') AND (ch <= '9') DO
1987-06-29 12:27:50 +00:00
ch := inch();
1987-06-26 15:59:52 +00:00
END;
ELSE
ok := FALSE;
END;
END;
IF ok AND (ch = 'E') THEN
1987-06-29 12:27:50 +00:00
ch := inch();
1987-06-26 15:59:52 +00:00
IF (ch ='+') OR (ch = '-') THEN
1987-06-29 12:27:50 +00:00
ch := inch();
1987-06-26 15:59:52 +00:00
END;
IF (ch >= '0') AND (ch <= '9') THEN
WHILE (ch >= '0') AND (ch <= '9') DO
1987-06-29 12:27:50 +00:00
ch := inch();
1987-06-26 15:59:52 +00:00
END;
ELSE
ok := FALSE;
END;
END;
ELSE
ok := FALSE;
END;
IF ok THEN
buf[index] := 0C;
RealConversions.StringToReal(buf, real, ok);
END;
IF NOT ok THEN
Traps.Message("Illegal real");
HALT;
END;
1987-07-03 16:07:18 +00:00
END ReadReal;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE WriteCardinal(OutputText: Text; card: CARDINAL; width: CARDINAL);
1987-06-26 15:59:52 +00:00
VAR
buf : numbuf;
BEGIN
Conversions.ConvertCardinal(card, 1, buf);
1987-07-03 16:07:18 +00:00
WriteString(OutputText, buf, width);
END WriteCardinal;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE WriteInteger(OutputText: Text; int: INTEGER; width: CARDINAL);
1987-06-26 15:59:52 +00:00
VAR
buf : numbuf;
BEGIN
Conversions.ConvertInteger(int, 1, buf);
1987-07-03 16:07:18 +00:00
WriteString(OutputText, buf, width);
END WriteInteger;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE WriteBoolean(OutputText: Text; bool: BOOLEAN; width: CARDINAL);
1987-06-26 15:59:52 +00:00
BEGIN
IF bool THEN
1987-07-03 16:07:18 +00:00
WriteString(OutputText, " TRUE", width);
1987-06-26 15:59:52 +00:00
ELSE
1987-07-03 16:07:18 +00:00
WriteString(OutputText, "FALSE", width);
1987-06-26 15:59:52 +00:00
END;
1987-07-03 16:07:18 +00:00
END WriteBoolean;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE WriteReal(OutputText: Text; real: REAL; width, nfrac: CARDINAL);
1987-06-26 15:59:52 +00:00
VAR
buf: numbuf;
ok: BOOLEAN;
digits: INTEGER;
BEGIN
IF width > SIZE(buf) THEN
width := SIZE(buf);
END;
IF nfrac > 0 THEN
1987-06-29 12:27:50 +00:00
RealConversions.RealToString(real, width, nfrac, buf, ok);
1987-06-26 15:59:52 +00:00
ELSE
IF width < 9 THEN width := 9; END;
IF real < 0.0 THEN
digits := 7 - INTEGER(width);
ELSE
digits := 6 - INTEGER(width);
END;
1987-06-29 12:27:50 +00:00
RealConversions.RealToString(real, width, digits, buf, ok);
1987-06-26 15:59:52 +00:00
END;
1987-07-03 16:07:18 +00:00
WriteString(OutputText, buf, 0);
END WriteReal;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
PROCEDURE WriteString(OutputText: Text; str: ARRAY OF CHAR; width: CARDINAL);
1987-06-26 15:59:52 +00:00
VAR index: CARDINAL;
BEGIN
index := 0;
1987-07-03 16:07:18 +00:00
WHILE (index <= HIGH(str)) AND (str[index] # Eos) DO
1987-06-26 15:59:52 +00:00
INC(index);
END;
WHILE index < width DO
1987-07-03 16:07:18 +00:00
WriteChar(OutputText, " ");
1987-06-26 15:59:52 +00:00
INC(index);
END;
index := 0;
1987-07-03 16:07:18 +00:00
WHILE (index <= HIGH(str)) AND (str[index] # Eos) DO
WriteChar(OutputText, str[index]);
1987-06-26 15:59:52 +00:00
INC(index);
END;
1987-07-03 16:07:18 +00:00
END WriteString;
1987-06-26 15:59:52 +00:00
1987-07-03 16:07:18 +00:00
BEGIN (* PascalIO initialization *)
1987-06-26 15:59:52 +00:00
WITH ibuf DO
eof := FALSE;
type := reading;
fildes := 0;
bufferedcount := BUFSIZ;
maxcnt := 0;
cnt := 1;
END;
WITH obuf DO
eof := FALSE;
type := writing;
fildes := 1;
IF isatty(1) THEN
bufferedcount := 1;
ELSE
bufferedcount := BUFSIZ;
END;
cnt := 0;
END;
1987-07-03 16:07:18 +00:00
Notext := NIL;
Input := ADR(ibuf);
Output := ADR(obuf);
Input^.next := Output;
Output^.next := NIL;
head := Input;
END PascalIO.