G2dGraphCmdsImpl.mesa
Copyright Ó 1985, 1992 by Xerox Corporation. All rights reserved.
Bloomenthal, July 20, 1992 1:10 pm PDT
Glassner, September 14, 1989 12:50:34 pm PDT
DIRECTORY Commander, CommanderOps, Convert, FS, G2dBasic, G2dGraph, G2dTool, IO, Real, Rope;
G2dGraphCmdsImpl: CEDAR PROGRAM
IMPORTS CommanderOps, Convert, FS, G2dBasic, G2dGraph, G2dTool, IO, Real, Rope
~ BEGIN
Graph Command
GraphFunctionCmd: Commander.CommandProc ~ {
xMin: REAL ¬ 0.0;
scale, a, xMax: REAL ¬ 1.0;
fileName: Rope.ROPE ¬ NIL;
function: G2dGraph.Function;
reals: G2dBasic.RealSequence ¬ NIL;
args: CommanderOps.ArgumentVector ¬ CommanderOps.Parse[cmd];
IF args.argc < 2 OR Rope.Equal[args[1], "?"]
THEN RETURN[msg: Rope.Concat["usage: 2dGraphFunction ", GraphFunctionUsage[]]];
FOR n: NAT IN [0..args.argc-1) DO
IF Rope.Equal["-file", args[n], FALSE] THEN {fileName ¬ args[n+1]; EXIT};
ENDLOOP;
IF fileName = NIL
THEN {
function ¬ G2dGraph.GetFunction[args[1]];
IF Rope.IsEmpty[function.name] THEN RETURN[$Failure, "no such function"];
}
ELSE {
ENABLE IO.Error => GOTO BadData;
s: IO.STREAM ¬ FS.StreamOpen[fileName ! FS.Error => GOTO FSError];
DO
reals ¬ G2dBasic.AddToRealSequence[reals, IO.GetReal[s ! IO.EndOfStream => EXIT]];
ENDLOOP;
IF reals = NIL OR reals.length = 0 THEN RETURN[$Failure, "no data in file"];
function ¬ [fileName, Reals,, reals];
EXITS
FSError => RETURN[$Failure, Rope.Concat["can't open ", fileName]];
BadData => RETURN[$Failure, "bad data in file"];
};
FOR n: NAT IN [0..args.argc-1) DO
ENABLE Convert.Error => GOTO ConvertError;
Test: PROC [a: Rope.ROPE, r: REAL] RETURNS [s: REAL] ~ {
s ¬ IF Rope.Equal[a, args[n], FALSE] THEN Convert.RealFromRope[args[n+1]] ELSE r;
};
scale ¬ Test["-scale", scale];
xMin ¬ Test["-xMin", xMin];
xMax ¬ Test["-xMax", xMax];
a ¬ Test["-a", a];
ENDLOOP;
msg ¬ G2dGraph.GraphFunction[function, xMin, xMax, scale, a, reals];
EXITS ConvertError => RETURN[$Failure, "argument conversion error"];
};
Reals: G2dGraph.GraphProc ~ {
reals: G2dBasic.RealSequence ¬ NARROW[g.clientData];
xx: REAL ¬ x*REAL[reals.length-1];
n0: CARD ¬ Real.Floor[xx];
y ¬ reals[n0];
IF n0 < reals.length-1 THEN y ¬ y+(xx-REAL[n0])*(reals[n0+1]-y);
};
GraphFunctionUsage: PROC RETURNS [usage: Rope.ROPE] ~ {
usage ¬ " [function] [-option]\n\tfunctions are:\n";
FOR l: LIST OF G2dGraph.Function ¬ G2dGraph.GetFunctions[], l.rest WHILE l # NIL DO
usage ¬ IO.PutFR["%g\t\t%g:\t%g\n",
IO.rope[usage], IO.rope[l.first.name], IO.rope[l.first.use]];
ENDLOOP;
usage ¬ Rope.Concat[usage, "\toptions are:
  -file <filename> (file must contain list of reals or integers)
  -scale <REAL> (default = 1)
  -xMin <REAL> (default = 0)
  -xMax <REAL> (default = 1)
  -a  <REAL> (default = 1)"];
};
Start Code
G2dTool.Register["GraphFunction", GraphFunctionCmd, GraphFunctionUsage[]];
END.