<> <> <> 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.STREAM _ IO.noWhereStream] RETURNS[pla: PLA] = { ENABLE FS.Error => IF error.group = user THEN { log.PutRope[error.explanation]; CONTINUE }; checkedOnce: BOOL _ FALSE; iForm, oForm: Format; iFormat, oFormat: Format; length: CARDINAL _ 0; rope, inName, outName: IO.ROPE _ NIL; time: BasicTime.GMT; inStm: IO.STREAM _ FS.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=NIL THEN {log.PutRope["\nI couldn't find 'OutType'\n"]; RETURN[NIL]}; IF outName=NIL THEN {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.STREAM _ FS.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.ROPE _ NIL] RETURNS[header: IO.ROPE] = { out: IO.STREAM _ IO.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_0; 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.