ReadContourImpl.mesa
Written by: Maureen Stone November 9, 1983 4:56 pm
DIRECTORY
Curve USING [StartSamples, defaultHandle, AddSample],
ConvertUnsafe USING [ToRope],
JaMFnsDefs,
IO,
FileIO USING [OpenFailed, Open],
Rope USING [ROPE, Equal, Length];
ReadContourImpl: PROGRAM
IMPORTS JaMFnsDefs, Rope, Curve, FileIO, IO, ConvertUnsafe
EXPORTS = BEGIN
stream: IO.Handle;
Done: SIGNAL = CODE;
ROPE: TYPE = Rope.ROPE;
contourNumber: INTEGER ← 0;
OpenInputFile: PROC = {
ENABLE FileIO.OpenFailed => TRUSTED{
JaMFnsDefs.JaMExec["(open failed\n) .print"]; CONTINUE};
ls: LONG STRING ← [80];
rope: ROPE;
JaMFnsDefs.PopString[ls];
rope ← ConvertUnsafe.ToRope[ls];
IF stream # NIL THEN stream.Close[];
stream ← NIL;
IF rope.Length[] > 0 THEN stream ← FileIO.Open[rope, read];
FindNextContour[]; --reads past any junk at the front
contourNumber ← 0;
};
CloseInputFile: PROC = {
IF stream # NIL THEN stream.Close[];
stream ← NIL;
};
JFindContour: PROCEDURE = {
ENABLE IO.EndOfStream =>{JaMFnsDefs.JaMExec["(not found \n) .print"]; CONTINUE};
cn: INTEGER ← JaMFnsDefs.PopInteger[];
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.GetToken[stream];
contourNumber ← contourNumber+1;
ENDLOOP;
};
JInputSamples: PROC = {
JaMFnsDefs.PushBoolean[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.SyntaxError => SIGNAL Done];
y ← IO.GetReal[stream];
r ← IO.GetToken[stream];
IF ~Rope.Equal[r,"setcp",FALSE] THEN ERROR;
};
GetNext: PROC RETURNS[x,y: REAL] = {
r: ROPE;
x ← IO.GetReal[stream ! IO.SyntaxError => SIGNAL Done];
y ← IO.GetReal[stream];
r ← IO.GetToken[stream];
IF ~Rope.Equal[r,"drawto",FALSE] THEN ERROR;
};
JaMFnsDefs.Register[".openInput"L,OpenInputFile]; --open the input file (will close current file)
JaMFnsDefs.Register[".closeInput"L,CloseInputFile]; --close the input file
JaMFnsDefs.Register[".findContour"L,JFindContour]; --contour number .findContour
JaMFnsDefs.Register[".inputSamples"L,JInputSamples]; --inputs current contour. returns endOfFile
END.