<> <> <> 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.