IPWriterImpl.mesa
Last edited by:
Doug Wyatt, March 12, 1984 1:14:19 pm PST
DIRECTORY
FS USING [StreamOpen],
IO USING [atom, Close, int, PutF, PutFR, STREAM],
IPBasic USING [Op, Rational, Version, version],
IPWriter USING [Class, ClassRep, ErrorCode, Writer, WriterRep],
Rope USING [ROPE];
IPWriterImpl: CEDAR MONITOR
IMPORTS FS, IO
EXPORTS IPWriter
~ BEGIN OPEN IPWriter, IPBasic;
ROPE: TYPE ~ Rope.ROPE;
STREAM: TYPE ~ IO.STREAM;
Error: PUBLIC ERROR[code: ErrorCode, explanation: ROPENIL] ~ CODE;
classList: LIST OF Class ← NIL; -- monitored data
Register: PUBLIC ENTRY PROC[class: Class] ~ {
classList ← CONS[class, classList];
};
ClassList: ENTRY PROC RETURNS[LIST OF Class] ~ {
RETURN[classList];
};
GetClass: PROC[encoding: ATOM] RETURNS[Class] = {
FOR list: LIST OF Class ← ClassList[], list.rest UNTIL list=NIL DO
class: Class ~ list.first;
IF class.encoding=encoding THEN RETURN[class];
ENDLOOP;
ERROR Error[$unknownEncoding,
IO.PutFR["\"%g\" is an unrecognized encoding type.", IO.atom[encoding]]];
};
Open: PUBLIC PROC[name: ROPE, encoding: ATOM] RETURNS[Writer] ~ {
stream: STREAM ~ FS.StreamOpen[name, $create];
writer: Writer ~ Create[stream, encoding];
PutHeader[writer];
RETURN[writer];
};
Create: PUBLIC PROC[stream: STREAM, encoding: ATOM] RETURNS[Writer] ~ {
class: Class ~ GetClass[encoding];
RETURN[NEW[WriterRep ← [class: class, stream: stream]]];
};
PutHeader: PUBLIC PROC[writer: Writer] ~ {
stream: STREAM ~ writer.stream;
encoding: ATOM ~ writer.class.encoding;
version: Version ~ IPBasic.version;
stream.PutF["Interpress/%g/%g.%g ",
IO.atom[encoding], IO.int[version.major], IO.int[version.minor]];
};
closedClass: Class ~ NEW[ClassRep ← [encoding: $Closed, putOp: ClosedPutOp, putInt: ClosedPutInt, putReal: ClosedPutReal, putRational: ClosedPutRational, putIdentifier: ClosedPutIdentifier, putString: ClosedPutString, putInsertfile: ClosedPutInsertfile, putComment: ClosedPutComment, putAnnotation: ClosedPutAnnotation]];
closedExplanation: ROPE ~ "Tried to use a Writer that has been closed.";
ClosedPutOp: PROC[writer: Writer, op: Op] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutInt: PROC[writer: Writer, value: INT] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutReal: PROC[writer: Writer, value: REAL] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutRational: PROC[writer: Writer, value: Rational] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutIdentifier: PROC[writer: Writer, rope: ROPE] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutString: PROC[writer: Writer, rope: ROPE] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutComment: PROC[writer: Writer, rope: ROPE] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutAnnotation: PROC[writer: Writer, rope: ROPE] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutInsertfile: PROC[writer: Writer, rope: ROPE] ~ {
ERROR Error[$closed, closedExplanation];
};
Close: PUBLIC PROC[writer: Writer] ~ {
stream: STREAM ~ writer.stream;
writer.class ← closedClass;
writer.stream ← NIL;
stream.Close[];
};
END.