-- file: PascalNoviceFiles.mesa
-- last modified by Ramshaw, December 25, 1982 10:21 am
-- written by McCreight, November 17, 1980 3:13 PM

-- Pascal files for the novice user: slow, but anything works

DIRECTORY
IO USING [STREAM],
PascalBasic;

PascalNoviceFiles: CEDAR DEFINITIONS =

BEGIN OPEN PascalBasic;

--    T Y P E S

PascalFile: TYPE = LONG POINTER TO MesaFile ← NIL;
PascalFilePtr: TYPE = LONG POINTER TO PascalFile; -- for passing files as VAR parameters

MesaFile: TYPE = RECORD [
name: ROPE,
in, out, inSource, inSource2, outDest, outDest2: IO.STREAMNIL,
eof: BOOLEANFALSE,
openFileLink: MesaFilePtr ← NIL];
MesaFilePtr: TYPE = REF MesaFile;

TextControl: TYPE = RECORD [
readLowerCase: PascalBoolean ← TRUE,
readUpperCase: PascalBoolean ← TRUE,
readAllControlChars: PascalBoolean ← FALSE,
writeLowerCase: PascalBoolean ← TRUE,
writeUpperCase: PascalBoolean ← TRUE,
writeAllControlChars: PascalBoolean ← TRUE];

PascalTextFile: TYPE = RECORD [
baseFile: PascalFile,
eoln: BOOLEANTRUE, -- TRUE if element is a blank representing CR
control: TextControl,
lineNo: PascalInteger ← 1, -- line number
validElement: BOOLEANFALSE,
element: PascalChar ← ' -- last character read
];
PascalTextFilePtr: TYPE = LONG POINTER TO PascalTextFile; -- for passing by VAR

Text: TYPE = PascalTextFile;

--    V A R I A B L E S

Input, Output, Tty: PascalTextFile;

--     P R O C E D U R E S Called by PasMesa

-- Text File Operations:

PascalTextBREAK: PROCEDURE [file: PascalTextFilePtr];

PascalTextGET: PROCEDURE [file: PascalTextFilePtr];

PascalTextElement: PROCEDURE [file: PascalTextFilePtr] RETURNS [PascalChar];

PascalTextFinishGet: PROCEDURE [file: PascalTextFilePtr];

PascalTextPUT: PROCEDURE [file: PascalTextFilePtr];

PascalTextRESET: PROCEDURE [file: PascalTextFilePtr];

PascalTextREWRITE: PROCEDURE [file: PascalTextFilePtr];

PascalTextPAGE: PROCEDURE [file: PascalTextFilePtr];

PascalTextEOF: PROCEDURE [file: PascalTextFilePtr] RETURNS [BOOLEAN];

PascalTextEOLN: PROCEDURE [file: PascalTextFilePtr] RETURNS [BOOLEAN];

-- Binary File Operations:
-- The "length" argument is in units of bytes rather than words, but the only odd
-- value that will ever arise is 1. The Pascal program generates these arguments
-- by utilizing a little-known feature of the SIZE operator in Mesa: if you say
-- SIZE[T, 2], you get back the size in words of a packed array of 2 T's.

PascalBREAK: PROCEDURE [
file: PascalFilePtr, length: CARDINAL,
element: LONG POINTER];

PascalGET: UNSAFE PROCEDURE [
file: PascalFilePtr, length: CARDINAL,
element: LONG POINTER -- TO ARRAY[0..length/2) OF UNSPECIFIED --];

PascalPUT: PROCEDURE [
file: PascalFilePtr, length: CARDINAL,
element: LONG POINTER -- TO ARRAY[0..length/2) OF UNSPECIFIED --];

PascalRESET: UNSAFE PROCEDURE [
file: PascalFilePtr, length: CARDINAL,
element: LONG POINTER -- TO ARRAY[0..length/2) OF UNSPECIFIED --];

PascalREWRITE: PROCEDURE [
file: PascalFilePtr, length: CARDINAL ← 0,
element: LONG POINTER -- TO ARRAY[0..length/2) OF UNSPECIFIED --NIL];
-- The latter two arguments don't mean anything, and are just included since PasMesa
-- prefers to generate them.

PascalRead, PascalReadLong, PascalWrite, PascalWriteLong: UNSAFE PROCEDURE [
file: PascalFilePtr, length: CARDINAL,
element: LONG POINTER -- TO ARRAY[0..length/2) OF UNSPECIFIED --,
item: LONG POINTER -- TO ARRAY[0..length/2) OF UNSPECIFIED --];

PascalEOF: PROCEDURE [file: PascalFilePtr] RETURNS [BOOLEAN];

-- formatted IO for text files

PascalReadLn, PascalWriteLn: PROCEDURE [file: PascalTextFilePtr];
-- reads up to or writes a CR

PascalReadInteger: PROCEDURE [file: PascalTextFilePtr] RETURNS [PascalInteger];

PascalWriteInteger: PROCEDURE [
file: PascalTextFilePtr, item: PascalInteger,
fieldMinLength: PascalInteger ← -1];

PascalReadReal: PROCEDURE [file: PascalTextFilePtr] RETURNS [PascalReal];

PascalWriteReal: PROCEDURE [
file: PascalTextFilePtr, item: PascalReal,
fieldMinLength, fracLength: PascalInteger ← -1];

PascalReadChar: PROCEDURE [file: PascalTextFilePtr] RETURNS [PascalChar];

PascalWriteChar: PROCEDURE [
file: PascalTextFilePtr, item: PascalChar, fieldMinLength: PascalInteger ← -1];

PascalReadBoolean: PROCEDURE [file: PascalTextFilePtr] RETURNS [PascalBoolean];

PascalWriteBoolean: PROCEDURE [
file: PascalTextFilePtr, item: PascalBoolean,
fieldMinLength: PascalInteger ← -1];

PascalReadArrayOfChar, PascalReadLongArrayOfChar: UNSAFE PROCEDURE [
file: PascalTextFilePtr,
item: CharArrayPtr,
arrayBound: PascalInteger];

PascalWriteArrayOfChar, PascalWriteLongArrayOfChar: PROCEDURE [
file: PascalTextFilePtr,
item: CharArrayPtr,
arrayBound: PascalInteger, fieldMinLength: PascalInteger ← -1];

-- At this point, McCreight included the following ReadString routines, but I don't see
-- how they could ever arise, nor do I see how to make them safe.
-- PascalReadString, PascalReadLongString:
-- PROCEDURE [file: PascalTextFilePtr, item: LONG POINTER TO ??ROPE??];

PascalWriteString, PascalWriteLongString: PROCEDURE [
file: PascalTextFilePtr, item: ROPE, fieldMinLength: PascalInteger ← -1];



--     P R O C E D U R E S NOT Called by PasMesa
-- (and hence, intended to be called as external procedures by the Pascal
-- program, or else from Mesa code that implements such external procedures).

PascalInventFileName: PROCEDURE [name: ROPENIL] RETURNS [PascalFile];
-- Allocates a MesaFile to implement a PascalFile with the recommended name
-- This proc is called by PasMesa if you ask PasMesa to INVENTFILENAMES

PascalAttachFile: PROCEDURE [
file: PascalFilePtr, dir: PascalFileDirection ← in,
type: PascalFileType ← text];
PascalFileType: TYPE = {text, binary};
PascalFileDirection: TYPE = {in, out, both};

PascalCleanupFiles: PROCEDURE;

END. -- PascalNoviceFiles