ReadContourJaM.mesa
Written by: Maureen Stone May 15, 1984 5:13:50 pm PDT
DIRECTORY
Curve USING [StartSamples, defaultHandle, AddSample],
JaM USING [ExecuteRope, PopInt, PopRope, PushRope,PushBool, State, Register],
FitJaM USING [RegisterInit, InitProc],
IO USING [STREAM, SetIndex, GetTokenRope, GetReal, EndOfStream, Close, Error],
FS USING [Error, StreamOpen],
Rope USING [ROPE, Equal, Length];
ReadContourJaM: CEDAR PROGRAM
IMPORTS JaM, Rope, Curve, FS, IO, FitJaM
EXPORTS = BEGIN
stream: IO.STREAM;
Done: SIGNAL = CODE;
ROPE: TYPE = Rope.ROPE;
State: TYPE = JaM.State;
contourNumber: INTEGER ← 0;
OpenInputFile: PROC[state: State] = {
ENABLE FS.Error => IF error.group=user THEN {
JaM.PushRope[state,error.explanation];
JaM.ExecuteRope[state, ".print"]; CONTINUE};
rope: ROPE ← JaM.PopRope[state];
IF stream # NIL THEN IO.Close[stream];
stream ← NIL;
IF rope.Length[] > 0 THEN stream ← FS.StreamOpen[rope, $read];
FindNextContour[]; --reads past any junk at the front
contourNumber ← 0;
};
CloseInputFile: PROC[state: State] = {
IF stream # NIL THEN IO.Close[stream];
stream ← NIL;
};
JFindContour: PROCEDURE [state: State] = {
ENABLE IO.EndOfStream =>{JaM.ExecuteRope[state, "(not found \n) .print"]; CONTINUE};
cn: INTEGER ← JaM.PopInt[state];
FindContour[cn];
};
FindContour: PUBLIC PROCEDURE [cn: NAT] = {
count: NAT ← 0;
IO.SetIndex[stream,0];
FindNextContour[]; --reads past any junk at the front
contourNumber ← 0;
UNTIL count=cn DO
FindNextContour[];
count ← count+1;
ENDLOOP;
};
FindNextContour: PUBLIC PROC = {
rope: ROPE;
UNTIL Rope.Equal[rope,"beginoutline",FALSE] DO
[rope,] ← IO.GetTokenRope[stream];
contourNumber ← contourNumber+1;
ENDLOOP;
};
JInputSamples: PROC[state: State] = {
JaM.PushBool[state, InputSamples[]];
};
InputSamples: PUBLIC PROCEDURE RETURNS[endOfFile: BOOLEAN] = {
x,y: REAL;
nSamples: INTEGER;
endOfFile ← FALSE;
DO
[x,y] ← GetFirst[! Done, IO.EndOfStream => ERROR];
Curve.StartSamples[Curve.defaultHandle, x,y];
nSamples ← 1;
DO
[x,y] ← GetNext[! Done => EXIT; IO.EndOfStream => {endOfFile ← TRUE; EXIT}];
Curve.AddSample[Curve.defaultHandle, x, y];
nSamples ← nSamples+1;
ENDLOOP;
contourNumber ← contourNumber+1;
IF nSamples>6 OR endOfFile THEN EXIT;
ENDLOOP;
};
GetFirst: PROC RETURNS[x,y: REAL] = {
r: ROPE;
x ← IO.GetReal[stream ! IO.Error => IF ec=SyntaxError THEN SIGNAL Done];
y ← IO.GetReal[stream];
[r,] ← IO.GetTokenRope[stream];
IF ~Rope.Equal[r,"setcp",FALSE] THEN ERROR;
};
GetNext: PROC RETURNS[x,y: REAL] = {
r: ROPE;
x ← IO.GetReal[stream ! IO.Error => IF ec=SyntaxError THEN SIGNAL Done];
y ← IO.GetReal[stream];
[r,] ← IO.GetTokenRope[stream];
IF ~Rope.Equal[r,"drawto",FALSE] THEN ERROR;
};
ReadContourInit: FitJaM.InitProc = {OPEN JaM; --to make the comments line up
Register[state,".openInput",OpenInputFile]; --open the input file (will close current file)
Register[state,".closeInput",CloseInputFile]; --close the input file
Register[state,".findContour",JFindContour]; --contour number .findContour
Register[state,".inputSamples",JInputSamples]; --inputs current contour. returns endOfFile
};
FitJaM.RegisterInit[$ReadContourInit,ReadContourInit];
END.