<> <> <> DIRECTORY Commander USING [CommandProc, Register], CommandTool USING [ParseToList], Convert USING [CardFromRope, Error], FileNames USING [CurrentWorkingDirectory], FS USING [ComponentPositions, EnumerateForNames, ExpandName, NameProc, Error, StreamOpen], IO USING [Close, EndOfStream, Error, GetInt, GetReal, GetRopeLiteral, int, PutF, PutFR, STREAM], List USING [DReverse, Kill], Graph USING [ValueList], GraphOps USING [AddCurve, AddText, CreateGraph, SetXValues], GraphPrivate USING [GraphHandle], Real USING [LargestNumber], Rope USING [Cat, Equal, Fetch, Find, IsEmpty, Length, ROPE, Substr]; SimpleGraph: CEDAR PROGRAM IMPORTS Commander, CommandTool, Convert, FileNames, FS, GraphOps, IO, List, Rope = { OPEN IO, Graph, GraphPrivate; RopeList: TYPE = LIST OF Rope.ROPE; DataError: SIGNAL[reason: Rope.ROPE _ NIL] = CODE; SGraph: Commander.CommandProc = { argList: RopeList; length: NAT; [argList, length] _ CommandTool.ParseToList[cmd]; IF length <= 0 THEN msg _ "To plot data in files, type: SGraph " ELSE { wDir: Rope.ROPE _ FileNames.CurrentWorkingDirectory[]; nGraphs: NAT _ 0; maxGraphs: NAT _ 12; allVersions: BOOL _ FALSE; FOR arg: RopeList _ argList, arg.rest UNTIL arg = NIL OR msg # NIL DO IF arg.first.Fetch[0] = '- THEN { IF arg.first.Length[] >= 2 THEN SELECT arg.first.Fetch[1] FROM 'A, 'a => allVersions _ TRUE; 'H, 'h => allVersions _ FALSE; IN ['0..'9] => { max: NAT _ Convert.CardFromRope[arg.first.Substr[1] ! Convert.Error => max _ maxGraphs]; -- don't change it maxGraphs _ max; }; ENDCASE; -- simply ignors illegal switches } ELSE { fileList: RopeList _ FileListFrom[arg.first, wDir, allVersions]; FOR file: RopeList _ fileList, file.rest UNTIL file = NIL OR msg # NIL DO msg _ IF nGraphs >= maxGraphs THEN IO.PutFR["Note: max. %g plots for each command.", IO.int[maxGraphs]] ELSE SimpleGraph[file.first]; nGraphs _ nGraphs + 1; ENDLOOP; }; ENDLOOP; }; }; -- SGraph FileListFrom: PROC [pattern, wDir: Rope.ROPE _ NIL, allVersions: BOOL _ FALSE] RETURNS [fileList: RopeList _ NIL] = { root, lastRoot: Rope.ROPE _ NIL; LinkIt: FS.NameProc -- PROC [fullFName] RETURNS [continue: BOOL] -- = { excl: INT _ fullFName.Find["!"]; continue _ TRUE; IF fullFName.Substr[excl-6, 6].Equal[".press", FALSE] THEN RETURN; IF ~allVersions THEN { root _ fullFName.Substr[0, excl]; IF root.Equal[lastRoot, FALSE] THEN { fileList.first _ fullFName; RETURN; }; lastRoot _ root; }; fileList _ CONS[fullFName, fileList]; }; -- LinkIt FS.EnumerateForNames[pattern, LinkIt, wDir]; TRUSTED {fileList _ LOOPHOLE[List.DReverse[LOOPHOLE[fileList]]]}; }; -- FileListFrom SimpleGraph: PROC[file: Rope.ROPE _ NIL] RETURNS[msg: Rope.ROPE _ NIL] = { s: IO.STREAM _ NIL; buffer: REF TEXT _ RefText.Scratch[512]; tokenKind: IO.TokenKind _ tokenROPE; token: REF TEXT; charsSkipped: INT; error: TokenError; ok: BOOL _ TRUE; fullName, status: Rope.ROPE _ NIL; nNames: INT _ 0; names, lastName: LIST OF Rope.ROPE _ NIL; xList: ValueList _ NIL; lvl, tlvl: LIST OF ValueList _ NIL; r: REAL; handle: GraphHandle _ NIL; int: INT _ 0; xmin, xmax, ymin, ymax: REAL; IF file.IsEmpty[] THEN RETURN["No file name."]; s _ FS. StreamOpen[file ! FS.Error => {status _ error.explanation; ok _ FALSE; CONTINUE} ]; IF ok THEN { ENABLE { DataError => { msg _ reason; ok _ FALSE; CONTINUE; }; IO.EndOfStream => { msg _ Rope.Cat["End of file reached", status]; ok _ FALSE; CONTINUE; }; IO.Error => { msg _ Rope.Cat[ SELECT ec FROM SyntaxError => "Syntax error", Overflow => "Overflow in input conversion" ENDCASE => IO.PutFR["IO Error # %g", IO.int[LOOPHOLE[ec, CARDINAL]]], status]; ok _ FALSE; CONTINUE; }; ABORTED => { msg _ "SGraph aborted. ... "; ok _ FALSE; CONTINUE; }; }; DO [tokenKind, token, charsSkipped, error] _ s.GetCedarToken[buffer]; IF error = none THEN { IF tokenKind = tokenROPE OR tokenKind = tokenID THEN { nl: LIST OF ROPE _ CONS[Rope.FromRefText[token], NIL]; IF lastName = NIL THEN lastName _ names _ nl ELSE lastName.rest _ nl; nNames _ nNames + 1; } ELSE IF tokenKind = tokenCHAR OR tokenKind = tokenCOMMENT THEN LOOP ELSE EXIT; } ELSE SIGNAL DataError[IO.PutFR["Error after the %g-th name.", IO.int[nNames]]]; ENDLOOP; IF nNames = 0 THEN DataError["Can't find any names of curves."]; xmin _ ymin _ Real.LargestNumber; xmax _ ymax _ -xmin; FOR j: INT IN [0..nNames) DO lvl _ CONS[NIL, lvl]; ENDLOOP; UNTIL s.EndOf[] DO first: BOOL _ TRUE; tlvl _ lvl; FOR j: INT IN [0..nCurves] DO ENABLE { IO.Error => { msg _ Rope.Cat[ SELECT ec FROM SyntaxError => "Syntax error", Overflow => "Overflow" ENDCASE => IO.PutFR["IO Error # %g", IO.int[LOOPHOLE[ec, CARDINAL]]], IO.PutFR[" in getting the %g-th number on the %g-th row in the table.", IO.int[j+1], IO.int[i+1]]]; GOTO mistake; }; IO.EndOfStream => { IF j = 0 THEN GOTO finished ELSE { msg _ Rope.Cat[ "End of file reached", IO.PutFR[" in getting the %g-th number on the %g-th row in the table.", IO.int[j+1], IO.int[i+1]]]; GOTO mistake; }; }; }; r _ s.GetReal[]; IF j = 0 THEN { xList _ CONS[r, xList]; IF first THEN {xmin _ r; first _ FALSE} ELSE xmax _ r; } ELSE { tlvl.first _ CONS[r, tlvl.first]; ymax _ MAX[ymax, r]; ymin _ MIN[ymin, r]; tlvl _ tlvl.rest; }; ENDLOOP; out.PutF["."]; REPEAT mistake => ok _ FALSE; finished => NULL; ENDLOOP; }; IF ok THEN { IF ymax = ymin THEN { IF ymax = 0.0 THEN { ymax _ 1.0; ymin _ -1.0 } ELSE { ymax _ ymax + ABS[ymax]; ymin _ ymin - ABS[ymin]; }; }; IF xmax = xmin THEN { IF xmax = 0.0 THEN { xmax _ 1.0; xmin _ -1.0 } ELSE { xmax _ xmax + ABS[xmax]; xmin _ xmin - ABS[xmin]; }; }; out.PutF[" creating graph ... "]; handle _ GraphOps.CreateGraph[ bounds: [xmin, ymin, xmax, ymax], fileName: fullName ]; <<[] _ GraphOps.AddText[handle: handle,>> <> [] _ GraphOps.SetXValues[handle, xList]; int _ nCurves; TRUSTED { lvl _ LOOPHOLE[List.DReverse[LOOPHOLE[lvl]]]; names _ LOOPHOLE[List.DReverse[LOOPHOLE[names]]]; }; tlvl _ lvl; FOR lastName _ names, lastName.rest UNTIL lastName = NIL DO [] _ GraphOps.AddCurve[handle, int, tlvl.first, lastName.first, int]; int _ int - 1; tlvl _ tlvl.rest; ENDLOOP; TRUSTED { List.Kill[LOOPHOLE[lvl]]; List.Kill[LOOPHOLE[names]]; }; out.PutF["done.\n"]; }; IF s # NIL THEN s.Close[]; }; -- SimpleGraph Commander.Register[ "SGraph", SGraph, "SGraph , use a simple minded plot routine to plot data in the data files that have the following special format: -- comment nodes are ignored. -- carriage returns are treated the same as spaces. -- a text string enclosed by double quotes. <xmin> <ymin> <xmax> <ymax> -- four real numbers. <number of curves> -- Curves are functions of x. <names of the curves> -- Each name is a text string enclosed by double quotes. <number of rows> -- Each row in the table below has a leading x value followed by the corresponding y values. -- If there are n curves then the table for m different x values will be an m x n+1 matrix as follows. x1 y1(x1) y2(x1) y3(x1) ... yn(x1) x2 y1(x2) y2(x2) y3(x2) ... yn(x2) ... ... ... ... ... ... ... ... ... ... ... ... ... ... xm y1(xm) y2(xm) y3(xm) ... yn(xm) "]; }.