IPWriterImpl.mesa
Copyright © 1984 Xerox Corporation. All rights reserved.
Doug Wyatt, August 23, 1984 11:22:37 am PDT
DIRECTORY
FS USING [StreamOpen],
IO USING [atom, Close, int, PutF, PutFR, STREAM],
IPBasic USING [currentVersion, Op, Rational, Version, XeroxPixelVectorType],
IPWriter USING [Class, ClassRep, ErrorCode, Writer, WriterRep],
Rope USING [ROPE];
IPWriterImpl: CEDAR MONITOR
IMPORTS FS, IO
EXPORTS IPWriter
~ BEGIN
ROPE: TYPE ~ Rope.ROPE;
STREAM: TYPE ~ IO.STREAM;
Op: TYPE ~ IPBasic.Op;
Rational: TYPE ~ IPBasic.Rational;
Version: TYPE ~ IPBasic.Version;
Writer: TYPE ~ IPWriter.Writer;
Class: TYPE ~ IPWriter.Class;
Error:
PUBLIC
ERROR[code: IPWriter.ErrorCode, explanation:
ROPE ←
NIL] ~
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[IPWriter.WriterRep ← [class: class, stream: stream]]];
};
PutHeader:
PUBLIC
PROC[writer: Writer] ~ {
stream: STREAM ~ writer.stream;
encoding: ATOM ~ writer.class.encoding;
version: Version ~ IPBasic.currentVersion;
stream.PutF["Interpress/%g/%g.%g ",
IO.atom[encoding], IO.int[version.major], IO.int[version.minor]];
};
PutOp:
PUBLIC
PROC[writer: Writer, op: Op] ~ {
writer.class.putOp[writer, op] };
PutInt:
PUBLIC
PROC[writer: Writer, value:
INT] ~ {
writer.class.putInt[writer, value] };
PutReal:
PUBLIC
PROC[writer: Writer, value:
REAL] ~ {
writer.class.putReal[writer, value] };
PutRational:
PUBLIC
PROC[writer: Writer, value: Rational] ~ {
writer.class.putRational[writer, value] };
PutIdentifier:
PUBLIC
PROC[writer: Writer, rope:
ROPE] ~ {
writer.class.putIdentifier[writer, rope] };
PutString:
PUBLIC
PROC[writer: Writer, rope:
ROPE] ~ {
writer.class.putString[writer, rope] };
PutComment:
PUBLIC
PROC[writer: Writer, rope:
ROPE] ~ {
writer.class.putComment[writer, rope] };
PutAnnotation:
PUBLIC
PROC[writer: Writer, rope:
ROPE] ~ {
writer.class.putAnnotation[writer, rope] };
PutInsertfile:
PUBLIC
PROC[writer: Writer, rope:
ROPE] ~ {
writer.class.putInsertfile[writer, rope] };
PutLargeVector:
PUBLIC
PROC[writer: Writer, putBytes:
PROC[
STREAM],
bytesPerElement: [0..256), type: IPBasic.XeroxPixelVectorType ← $nil] ~ {
writer.class.putLargeVector[writer, putBytes, bytesPerElement, type] };
closedClass: Class ~ NEW[IPWriter.ClassRep ← [encoding: $Closed, putOp: ClosedPutOp, putInt: ClosedPutInt, putReal: ClosedPutReal, putRational: ClosedPutRational, putIdentifier: ClosedPutIdentifier, putString: ClosedPutString, putInsertfile: ClosedPutInsertfile, putComment: ClosedPutComment, putAnnotation: ClosedPutAnnotation, putLargeVector: ClosedPutLargeVector]];
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];
};
ClosedPutInsertfile:
PROC[writer: Writer, rope:
ROPE] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutAnnotation:
PROC[writer: Writer, rope:
ROPE] ~ {
ERROR Error[$closed, closedExplanation];
};
ClosedPutLargeVector:
PROC[writer: Writer, putBytes:
PROC[
STREAM],
bytesPerElement: [0..256), type: IPBasic.XeroxPixelVectorType] ~ {
ERROR Error[$closed, closedExplanation];
};
Close:
PUBLIC
PROC[writer: Writer] ~ {
stream: STREAM ~ writer.stream;
writer.class ← closedClass;
writer.stream ← NIL;
stream.Close[];
};
END.