StructuredStreamsImpl.Mesa
last edited by Spreitzer February 10, 1986 0:27:39 am PST
DIRECTORY IO, IOUtils, Rope, StructuredStreams, UnparserBuffer;
StructuredStreamsImpl: CEDAR PROGRAM
IMPORTS IO, IOUtils, UB:UnparserBuffer
EXPORTS StructuredStreams
SHARES IO =
BEGIN
SSData: TYPE = REF SSDataRec;
SSDataRec: TYPE = RECORD [
ubh: UB.Handle];
used: CARDINAL ← 0;
SSProcs: REF IO.StreamProcs ← IO.CreateStreamProcs[
variety: output,
class: $StructuredStream,
putChar: PutChar,
flush: Flush,
close: Close];
pfProcs: IOUtils.PFProcs = IOUtils.CopyPFProcs[NIL];
Create: PUBLIC PROCEDURE [onTopOf: UB.Handle] RETURNS [ss: IO.STREAM] =
BEGIN
ssd: SSData ← NEW [SSDataRec ← [ubh: onTopOf]];
UB.Init[ssd.ubh];
ss ← IO.CreateStream[streamProcs: SSProcs,
streamData: ssd,
backingStream: WITH onTopOf.output SELECT FROM
so: UB.BufferOutput.stream => so.stream,
ao: UB.BufferOutput.access => NIL,
ENDCASE => ERROR
];
[] ← IOUtils.SetPFProcs[ss, pfProcs];
END;
IsAnSS: PUBLIC PROC [s: IO.STREAM] RETURNS [BOOLEAN] =
BEGIN
IF s.streamData = NIL THEN RETURN [FALSE];
RETURN [ISTYPE[s.streamData, SSData]];
END;
GetHandle: PUBLIC PROC [ss: IO.STREAM] RETURNS [UB.Handle] =
BEGIN
IF ss.streamData = NIL THEN RETURN [NIL];
WITH ss.streamData SELECT FROM
ssd: SSData => RETURN [ssd.ubh];
ENDCASE => RETURN [NIL];
END;
Strip: PUBLIC PROC [ss: IO.STREAM] RETURNS [IO.STREAM] =
BEGIN
IF ss.streamData = NIL THEN RETURN [ss];
WITH ss.streamData SELECT FROM
ssd: SSData => WITH ssd.ubh.output SELECT FROM
so: UB.BufferOutput.stream => RETURN [so.stream];
ao: UB.BufferOutput.access => RETURN [NIL];
ENDCASE => ERROR;
ENDCASE => RETURN [ss];
END;
CloseThrough: PUBLIC PROCEDURE [self: IO.STREAM] =
BEGIN
IF NOT IsAnSS[self] THEN self.Close[]
ELSE {
ssd: SSData = NARROW[self.streamData];
self.Close[];
WITH ssd.ubh.output SELECT FROM
so: UB.BufferOutput.stream => so.stream.Close[];
ao: UB.BufferOutput.access => NULL;
ENDCASE => ERROR;
};
END;
Begin: PUBLIC PROCEDURE [ss: IO.STREAM] = TRUSTED
BEGIN
IF IsAnSS[ss] THEN NARROW[ss.streamData, SSData].ubh.Setb[];
END;
End: PUBLIC PROCEDURE [ss: IO.STREAM] = TRUSTED
BEGIN
IF IsAnSS[ss] THEN NARROW[ss.streamData, SSData].ubh.Endb[];
END;
Bp: PUBLIC PROCEDURE [ss: IO.STREAM, united: BOOLEAN, offset: INTEGER] = TRUSTED
BEGIN
IF IsAnSS[ss] THEN
BEGIN
ssd: SSData ← NARROW[ss.streamData];
ssd.ubh.Bp[united, offset];
END;
END;
ChangeMargin: PUBLIC PROCEDURE [ss: IO.STREAM, newMargin: INTEGER ← 69] =
BEGIN
IF IsAnSS[ss] THEN
BEGIN
ssd: SSData ← NARROW[ss.streamData];
ssd.ubh.margin ← newMargin;
END;
END;
PutChar: PROCEDURE [self: IO.STREAM, char: CHAR] = TRUSTED
{ssd: SSData = NARROW[self.streamData];
IF char = IO.CR THEN ssd.ubh.Newlineb[0]
ELSE ssd.ubh.Charb[char];
};
PrintLFormat: PROC [stream: IO.STREAM, val: IO.Value, format: IOUtils.Format, char: CHAR] --IOUtils.PFCodeProc-- = {
ssd: SSData = NARROW[stream.streamData];
WITH val SELECT FROM
x: rope IO.Value => ssd.ubh.Looksb[x.value];
ENDCASE => ERROR IO.Error[PFTypeMismatch, stream];
};
Flush: PROCEDURE [self: IO.STREAM] =
{IF self.backingStream # NIL THEN IO.Flush[self.backingStream]};
Close: PROC [self: IO.STREAM, abort: BOOLFALSE] =
BEGIN
IOUtils.AmbushStream[self: self, streamProcs: IOUtils.closedStreamProcs, streamData: NIL];
END;
Start: PROC = {
[] ← IOUtils.SetPFCodeProc[pfProcs, 'l, PrintLFormat];
};
Start[];
END.