PLAOpsImplD.mesa
Copyright c 1984 by Xerox Corporation. All rights reserved.
Last edited by Curry, September 16, 1985 2:50:40 pm PDT
DIRECTORY
BasicTime,
FS,
IO,
PLAOps,
REFBit,
Rope;
PLAOpsImplD: CEDAR PROGRAM
IMPORTS FS, IO, PLAOps, Rope, REFBit EXPORTS PLAOps =
BEGIN
OPEN PLAOps;
ReadPLAFile: PUBLIC PROC [fileName: IO.ROPE, log: IO.STREAMIO.noWhereStream]
RETURNS[pla: PLA] = {
ENABLE FS.Error => IF error.group = user THEN {
log.PutRope[error.explanation];
CONTINUE };
checkedOnce: BOOLFALSE;
iForm, oForm: Format;
iFormat, oFormat: Format;
length: CARDINAL ← 0;
rope, inName, outName: IO.ROPENIL;
time: BasicTime.GMT;
inStm: IO.STREAMFS.StreamOpen[fileName: fileName];
IF inStm=NIL THEN RETURN[NIL];
DO
ENABLE IO.EndOfStream => EXIT;
rope ← IO.GetTokenRope[inStm].token;
SELECT TRUE FROM
Rope.Equal[rope, "File"]  => {[ ]   ← IO.GetTokenRope[inStm]};
Rope.Equal[rope, "Date"]  =>
{[ ] ← IO.GetChar[inStm]; time    ← IO.GetTime[inStm]};
Rope.Equal[rope, "InType"] => {inName  ← IO.GetTokenRope[inStm].token};
Rope.Equal[rope, "OutType"] => {outName ← IO.GetTokenRope[inStm].token};
Rope.Equal[rope, "Terms"]  =>
{[ ] ← IO.GetChar[inStm];  length  ← IO.GetCard[inStm]; EXIT};
Rope.Equal[rope, "BitFormat"] => {
WHILE NOT Rope.Equal[IO.GetTokenRope[inStm].token,"BitTotal"] DO ENDLOOP;
[ ]  ← IO.GetChar[inStm]; [ ]   ← IO.GetTokenRope[inStm]};
Rope.Equal[rope, "TermList"] => {[ ] ← IO.GetChar[inStm]; EXIT};
ENDCASE => {LOOP};
ENDLOOP;
IF outName=NILTHEN {log.PutRope["\nI couldn't find 'OutType'\n"]; RETURN[NIL]};
IF outName=NILTHEN {log.PutRope["\nI couldn't find 'OutType'\n"]; RETURN[NIL]};
IF length=0  THEN {log.PutRope["\nI couldn't find 'Terms'\n"];  RETURN[NIL]};
pla ← NewPLA[inName, outName];
IF pla=NIL THEN {log.PutRope["\nProblem with 'InType' or 'OutType'\n"]; RETURN[NIL]};
pla.time ← time;
DO
ENABLE IO.EndOfStream => EXIT;
OK: PROC RETURNS[BOOL] = {
IF rope=NIL OR rope.Length=0 THEN RETURN[FALSE];
RETURN[SELECT rope.Fetch[0] FROM '0,'1,'-,'+,'x,'X,'. => TRUE, ENDCASE => FALSE] };
rope ← IO.GetLineRope[inStm];
IF ~OK[] THEN LOOP;
IF NOT checkedOnce THEN DO
checkedOnce ← TRUE;
[iFormat, oFormat] ← RopeToFormat[rope];
iForm ← REFBit.Desc[pla.data].fieldForm;
oForm ← REFBit.Desc[pla.out].fieldForm;
IF iFormat.size # iForm.size THEN {SIGNAL InvalidTermRope[rope]; EXIT};
IF oFormat.size # oForm.size THEN {SIGNAL InvalidTermRope[rope]; EXIT};
FOR i: NAT IN [0..iFormat.size) DO
IF iFormat[i].bitSize#iForm[i].bitSize THEN
{SIGNAL InvalidTermRope[rope]; GOTO Exit};
REPEAT Exit => EXIT ENDLOOP;
FOR i: NAT IN [0..oFormat.size) DO
IF oFormat[i].bitSize#oForm[i].bitSize THEN
{SIGNAL InvalidTermRope[rope]; GOTO Exit};
REPEAT Exit => EXIT ENDLOOP;
EXIT ENDLOOP;
AppendTerm[RopeToTerm[rope, pla.termList, iForm, oForm], pla.termList];
IF (pla.termList.length MOD 100)=0 THEN log.PutRope["!"] ELSE
IF (pla.termList.length MOD 10)=0 THEN log.PutRope["."];
ENDLOOP;
IF pla.termList.length # length THEN ERROR; -- length disagreement
log.PutRope[PLAHeader[pla, fileName]]};
WritePLAFile: PUBLIC PROC
[fileName: IO.ROPE, log: IO.STREAM IO.noWhereStream, pla: PLA] = {
out:   IO.STREAMFS.StreamOpen[fileName, $create];
log.PutRope[PLAHeader[pla, fileName]];
out.PutRope[PLAHeader[pla]];
out.PutRope[REFBit.FormatListing[pla.data]];
out.PutRope[REFBit.FormatListing[pla.out]];
out.PutRope["\nTermList:\n"];
ListTermList[
pla.termList,
FALSE,
FALSE,
out,
REFBit.Desc[pla.data].fieldForm,
REFBit.Desc[pla.out].fieldForm];
out.Close[]};
PLAHeader: PUBLIC PROC [pla: PLA, fileName: IO.ROPENIL] RETURNS[header: IO.ROPE] = {
out: IO.STREAMIO.ROS[];
out.PutRope["\n"];
IF fileName#NIL THEN-- notice this is just for next line
out.PutF["File: %g\n", IO.rope[fileName]];
out.PutF["Date: %g\n", IO.time[pla.time]];
out.PutF["InType: %g\n", IO.rope[REFBit.Desc[pla.data].typeName]];
out.PutF["OutType: %g\n", IO.rope[REFBit.Desc[pla.out].typeName]];
out.PutF["Terms: %g\n", IO.card[pla.termList.length]];
header ← IO.RopeFromROS[out]};
StripDirFromName: PROC[ file: IO.ROPE] RETURNS[IO.ROPE] = {
index: INT𡤀
WHILE index>=0 DO
index ← file.Find[s2: ">"];
file ← file.Substr[start: index+1] ENDLOOP;
RETURN[file]};
DefaultCMDLine: PUBLIC PROC[ cmdLine, default: IO.ROPE] RETURNS[IO.ROPE] = {
DO
ENABLE IO.EndOfStream => EXIT;
default ← IO.GetTokenRope[IO.RIS[cmdLine]].token; EXIT; ENDLOOP;
RETURN[default]};
END.