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