IOInterpImpl:
CEDAR
PROGRAM
IMPORTS BasicTime,
FS,
IO
EXPORTS IOInterp =
BEGIN
OPEN IOInterp;
GetChar: PUBLIC PROC[self: STREAM] RETURNS [CHAR] = {RETURN[IO.GetChar[self]]};
GetBlock:
PUBLIC
PROC [self:
STREAM, block:
REF
TEXT,
startIndex: NAT ← 0, count: NAT ← NAT.LAST]
RETURNS [nBytesRead: NAT] = {RETURN[IO.GetBlock[self, block, startIndex, count]]};
UnsafeGetBlock:
PUBLIC
UNSAFE
PROC [self:
STREAM, block: UnsafeBlock]
RETURNS [nBytesRead: INT] = UNCHECKED {RETURN[IO.UnsafeGetBlock[self, block]]};
! RuntimeError.BoundsFault (block.startIndex < 0 OR block.count < 0)
Analogous to GetBlock. Never raises EndOfStream.
EndOf:
PUBLIC
PROC [self:
STREAM]
RETURNS [
BOOL]
= {RETURN[IO.EndOf[self]]};
CharsAvail:
PUBLIC
PROC [self:
STREAM, wait:
BOOL ←
FALSE]
RETURNS [
INT]
= {RETURN[IO.CharsAvail[self, wait]]};
Returns the number of characters that can be obtained from the stream quickly (e.g. without waiting for the user to type something). If wait, does not return until it can return a nonzero value.
Backup:
PUBLIC
PROC [self:
STREAM, char:
CHAR]
= {IO.Backup[self, char]};
! Error[self, IllegalBackup] (char is not the last byte read).
Undoes the effect of the most recent GetChar, which returned the value char.
PutChar:
PUBLIC
PROC [self:
STREAM, char:
CHAR]
= {IO.PutChar[self, char]};
PutBlock:
PUBLIC
PROC [self:
STREAM, block:
REF
READONLY
TEXT, startIndex:
NAT ← 0, count:
NAT ←
NAT.
LAST]
= {IO.PutBlock[self, block, startIndex, count]};
Effect is like: If startIndex+count > block.maxLength, then set count ← block.length-startIndex. Then perform PutChar for each byte in block[startIndex .. startIndex+count).
UnsafePutBlock:
PUBLIC
UNSAFE
PROC [self:
STREAM, block: UnsafeBlock]
= UNCHECKED {IO.UnsafePutBlock[self, block]};
! RuntimeError.BoundsFault (block.startIndex < 0 OR block.count < 0)
Analogous to PutBlock.
Flush:
PUBLIC
PROC [self:
STREAM]
= {IO.Flush[self]};
Causes characters that have been output to stream, but not yet sent (because of buffering) to be sent
Reset:
PUBLIC
PROC [self:
STREAM]
= {IO.Reset[self]};
For input streams, has the effect of flushing all pending input, so that self.CharsAvail[] = 0. For output and input/output streams, has some class-specific effect.
Close:
PUBLIC
PROC [self:
STREAM, abort:
BOOL ←
FALSE]
= {IO.Close[self, abort]};
Makes stream unusable for further operations; any stream operation except Flush, Reset, and Close will raise ERROR Error[self, StreamClosed].
GetIndex:
PUBLIC
PROC[self:
STREAM]
RETURNS [index:
INT]
= {RETURN[IO.GetIndex[self]]};
SetIndex:
PUBLIC
PROC[self:
STREAM, index:
INT]
= {IO.SetIndex[self, index]};
TextFromTOS:
PUBLIC
PROC [self:
STREAM]
RETURNS [
REF
TEXT]
Applies only to the result of a TOS call. Returns the entire output sequence as a ref text. Does not close the stream, so may be called repeatedly, but same ref text may be returned several times (and is mutable).
= { RETURN[IO.TextFromTOS[self]] };
PutText:
PUBLIC
PROC [self:
STREAM, t:
REF
READONLY
TEXT]
= { IO.PutText[self, t] };
atom: PUBLIC PROC [v: ATOM] RETURNS [Value] = {RETURN[[atom[v]]]};
bool: PUBLIC PROC [v: BOOL] RETURNS [Value] = {RETURN[[boolean[v]]]};
card: PUBLIC PROC [v: LONG CARDINAL] RETURNS [Value] = {RETURN[[cardinal[v]]]};
char: PUBLIC PROC [v: CHAR] RETURNS [Value] = {RETURN[[character[v]]]};
int: PUBLIC PROC [v: INT] RETURNS [Value] = {RETURN[[integer[v]]]};
real: PUBLIC PROC [v: REAL] RETURNS [Value] = {RETURN[[real[v]]]};
refAny: PUBLIC PROC [v: REF READONLY ANY] RETURNS [Value] = {RETURN[[refAny[v]]]};
rope: PUBLIC PROC [v: ROPE] RETURNS [Value] = {RETURN[[rope[v]]]};
text: PUBLIC PROC [v: REF READONLY TEXT] RETURNS [Value] = {RETURN[[text[v]]]};
time:
PUBLIC
PROC [v: BasicTime.
GMT ← BasicTime.nullGMT]
RETURNS [Value]
= {RETURN[[time[(IF v#BasicTime.nullGMT THEN v ELSE BasicTime.Now[])]]]}; -- so IO.time[] gives the current time.
FSStreamOpen:
PUBLIC
PROC [fileName:
ROPE, accessOptions:
FS.AccessOptions ← $read, streamOptions:
REF
FS.StreamOptions ←
NIL, keep:
CARDINAL ← 1, createByteCount:
FS.ByteCount ← 2560, streamBufferParms:
REF
FS.StreamBufferParms ←
NIL]
RETURNS [
STREAM] =
{so: FS.StreamOptions = (IF streamOptions=NIL THEN FS.defaultStreamOptions ELSE streamOptions^);
sbp: FS.StreamBufferParms = (IF streamBufferParms=NIL THEN FS.defaultStreamBufferParms ELSE streamBufferParms^);
RETURN[FS.StreamOpen[fileName: fileName, accessOptions: accessOptions, streamOptions: so, keep: keep, createByteCount: createByteCount, streamBufferParms: sbp, extendFileProc: NIL]]};
END.