modified to use Streams module

This commit is contained in:
ceriel 1988-02-03 14:34:40 +00:00
parent 60278f1c52
commit f2dc30c912
2 changed files with 61 additions and 135 deletions

View file

@ -19,6 +19,7 @@ DEFINITION MODULE InOut;
If the filename ends with a '.', append the "defext" extension. If the filename ends with a '.', append the "defext" extension.
Done := "file was successfully opened". Done := "file was successfully opened".
If open, subsequent output is written to this file. If open, subsequent output is written to this file.
Files left open at program termination are automatically closed.
*) *)
PROCEDURE OpenInputFile(filename: ARRAY OF CHAR); PROCEDURE OpenInputFile(filename: ARRAY OF CHAR);

View file

@ -1,44 +1,30 @@
(*$R-*) (*$R-*)
IMPLEMENTATION MODULE InOut ; IMPLEMENTATION MODULE InOut ;
IMPORT Unix; IMPORT Streams;
IMPORT Conversions; IMPORT Conversions;
IMPORT Traps; IMPORT Traps;
FROM TTY IMPORT isatty; FROM TTY IMPORT isatty;
FROM SYSTEM IMPORT ADR;
CONST BUFSIZ = 1024; (* Tunable *) CONST TAB = 11C;
TAB = 11C;
TYPE IOBuf = RECORD TYPE numbuf = ARRAY[0..255] OF CHAR;
fildes: INTEGER;
cnt: INTEGER;
maxcnt: INTEGER;
bufferedcount: INTEGER;
buf: ARRAY [1..BUFSIZ] OF CHAR;
END;
numbuf = ARRAY[0..255] OF CHAR;
VAR ibuf, obuf: IOBuf; VAR unread: BOOLEAN;
unread: BOOLEAN;
unreadch: CHAR; unreadch: CHAR;
CurrIn, CurrOut: Streams.Stream;
result: Streams.StreamResult;
PROCEDURE Read(VAR c : CHAR); PROCEDURE Read(VAR c : CHAR);
BEGIN BEGIN
IF unread THEN IF unread THEN
unread := FALSE; unread := FALSE;
c := unreadch; c := unreadch;
Done := TRUE; Done := TRUE;
ELSE ELSE
WITH ibuf DO Streams.Read(CurrIn, c, result);
IF cnt <= maxcnt THEN Done := result = Streams.succeeded;
c := buf[cnt];
INC(cnt);
Done := TRUE;
ELSE
c := FillBuf(ibuf);
END;
END;
END; END;
END Read; END Read;
@ -48,135 +34,87 @@ IMPLEMENTATION MODULE InOut ;
unreadch := ch; unreadch := ch;
END UnRead; END UnRead;
PROCEDURE FillBuf(VAR ib: IOBuf) : CHAR;
VAR c : CHAR;
BEGIN
WITH ib DO
maxcnt := Unix.read(fildes, ADR(buf), bufferedcount);
cnt := 2;
Done := maxcnt > 0;
IF NOT Done THEN
c := 0C;
ELSE
c := buf[1];
END;
END;
RETURN c;
END FillBuf;
PROCEDURE Flush(VAR ob: IOBuf);
VAR dummy: INTEGER;
BEGIN
WITH ob DO
dummy := Unix.write(fildes, ADR(buf), cnt);
cnt := 0;
END;
END Flush;
PROCEDURE Write(c: CHAR); PROCEDURE Write(c: CHAR);
BEGIN BEGIN
WITH obuf DO Streams.Write(CurrOut, c, result);
INC(cnt);
buf[cnt] := c;
IF cnt >= bufferedcount THEN
Flush(obuf);
END;
END;
END Write; END Write;
PROCEDURE OpenInput(defext: ARRAY OF CHAR); PROCEDURE OpenInput(defext: ARRAY OF CHAR);
VAR namebuf : ARRAY [1..256] OF CHAR; VAR namebuf : ARRAY [1..128] OF CHAR;
BEGIN BEGIN
IF ibuf.fildes # 0 THEN IF CurrIn # Streams.InputStream THEN
CloseInput; Streams.CloseStream(CurrIn, result);
END; END;
MakeFileName("Name of input file: ", defext, namebuf); MakeFileName("Name of input file: ", defext, namebuf);
IF NOT Done THEN RETURN; END; IF NOT Done THEN RETURN; END;
IF (namebuf[1] = '-') AND (namebuf[2] = 0C) THEN openinput(namebuf);
ELSE
WITH ibuf DO
fildes := Unix.open(ADR(namebuf), 0);
Done := fildes >= 0;
maxcnt := 0;
cnt := 1;
END;
END;
END OpenInput; END OpenInput;
PROCEDURE OpenInputFile(filename: ARRAY OF CHAR); PROCEDURE OpenInputFile(filename: ARRAY OF CHAR);
BEGIN BEGIN
IF ibuf.fildes # 0 THEN IF CurrIn # Streams.InputStream THEN
CloseInput; Streams.CloseStream(CurrIn, result);
END;
IF (filename[0] = '-') AND (filename[1] = 0C) THEN
ELSE
WITH ibuf DO
fildes := Unix.open(ADR(filename), 0);
Done := fildes >= 0;
maxcnt := 0;
cnt := 1;
END;
END; END;
openinput(filename);
END OpenInputFile; END OpenInputFile;
PROCEDURE openinput(VAR namebuf: ARRAY OF CHAR);
BEGIN
IF (namebuf[0] = '-') AND (namebuf[1] = 0C) THEN
CurrIn := Streams.InputStream;
Done := TRUE;
ELSE
Streams.OpenStream(CurrIn, namebuf, Streams.text,
Streams.reading, result);
Done := result = Streams.succeeded;
END;
END openinput;
PROCEDURE CloseInput; PROCEDURE CloseInput;
BEGIN BEGIN
WITH ibuf DO IF CurrIn # Streams.InputStream THEN
IF (fildes > 0) AND (Unix.close(fildes) < 0) THEN Streams.CloseStream(CurrIn, result);
;
END;
fildes := 0;
maxcnt := 0;
cnt := 1;
END; END;
CurrIn := Streams.InputStream;
END CloseInput; END CloseInput;
PROCEDURE OpenOutput(defext: ARRAY OF CHAR); PROCEDURE OpenOutput(defext: ARRAY OF CHAR);
VAR namebuf : ARRAY [1..256] OF CHAR; VAR namebuf : ARRAY [1..128] OF CHAR;
BEGIN BEGIN
IF obuf.fildes # 1 THEN IF CurrOut # Streams.OutputStream THEN
CloseOutput; Streams.CloseStream(CurrOut, result);
END; END;
MakeFileName("Name of output file: ", defext, namebuf); MakeFileName("Name of output file: ", defext, namebuf);
IF NOT Done THEN RETURN; END; IF NOT Done THEN RETURN; END;
IF (namebuf[1] = '-') AND (namebuf[2] = 0C) THEN openoutput(namebuf);
ELSE
WITH obuf DO
fildes := Unix.creat(ADR(namebuf), 666B);
Done := fildes >= 0;
bufferedcount := BUFSIZ;
cnt := 0;
END;
END;
END OpenOutput; END OpenOutput;
PROCEDURE OpenOutputFile(filename: ARRAY OF CHAR); PROCEDURE OpenOutputFile(filename: ARRAY OF CHAR);
BEGIN BEGIN
IF obuf.fildes # 1 THEN IF CurrOut # Streams.OutputStream THEN
CloseOutput; Streams.CloseStream(CurrOut, result);
END;
IF (filename[0] = '-') AND (filename[1] = 0C) THEN
ELSE
WITH obuf DO
fildes := Unix.creat(ADR(filename), 666B);
Done := fildes >= 0;
bufferedcount := BUFSIZ;
cnt := 0;
END;
END; END;
openoutput(filename);
END OpenOutputFile; END OpenOutputFile;
PROCEDURE openoutput(VAR namebuf: ARRAY OF CHAR);
BEGIN
IF (namebuf[1] = '-') AND (namebuf[2] = 0C) THEN
CurrOut := Streams.OutputStream;
Done := TRUE;
ELSE
Streams.OpenStream(CurrOut, namebuf, Streams.text,
Streams.writing, result);
Done := result = Streams.succeeded;
END;
END openoutput;
PROCEDURE CloseOutput; PROCEDURE CloseOutput;
BEGIN BEGIN
Flush(obuf); IF CurrOut # Streams.OutputStream THEN
WITH obuf DO Streams.CloseStream(CurrOut, result);
IF (fildes # 1) AND (Unix.close(fildes) < 0) THEN
;
END;
fildes := 1;
bufferedcount := 1;
cnt := 0;
END; END;
CurrOut := Streams.OutputStream;
END CloseOutput; END CloseOutput;
PROCEDURE MakeFileName(prompt, defext : ARRAY OF CHAR; PROCEDURE MakeFileName(prompt, defext : ARRAY OF CHAR;
@ -332,15 +270,14 @@ IMPLEMENTATION MODULE InOut ;
END ReadString; END ReadString;
PROCEDURE XReadString(VAR s : ARRAY OF CHAR); PROCEDURE XReadString(VAR s : ARRAY OF CHAR);
VAR i : INTEGER; VAR j : CARDINAL;
j : CARDINAL;
ch : CHAR; ch : CHAR;
BEGIN BEGIN
j := 0; j := 0;
LOOP LOOP
i := Unix.read(0, ADR(ch), 1); Streams.Read(Streams.InputStream, ch, result);
IF i < 0 THEN IF result # Streams.succeeded THEN
EXIT; EXIT;
END; END;
IF ch <= " " THEN IF ch <= " " THEN
@ -360,14 +297,11 @@ IMPLEMENTATION MODULE InOut ;
i := 0; i := 0;
LOOP LOOP
IF (i <= HIGH(s)) AND (s[i] # 0C) THEN IF (i <= HIGH(s)) AND (s[i] # 0C) THEN
INC(i); Streams.Write(Streams.OutputStream, s[i], result);
ELSE ELSE
EXIT; EXIT;
END; END;
END; END;
IF Unix.write(1, ADR(s), i) < 0 THEN
;
END;
END XWriteString; END XWriteString;
PROCEDURE WriteCard(card, width : CARDINAL); PROCEDURE WriteCard(card, width : CARDINAL);
@ -419,16 +353,7 @@ IMPLEMENTATION MODULE InOut ;
END WriteString; END WriteString;
BEGIN (* InOut initialization *) BEGIN (* InOut initialization *)
CurrIn := Streams.InputStream;
CurrOut := Streams.OutputStream;
unread := FALSE; unread := FALSE;
WITH ibuf DO
fildes := 0;
bufferedcount := BUFSIZ;
maxcnt := 0;
cnt := 1;
END;
WITH obuf DO
fildes := 1;
bufferedcount := 1;
cnt := 0;
END;
END InOut. END InOut.