DIRECTORY
Ascii USING [SP],
Commander USING [CommandProc, Register],
Convert USING [RopeFromInt],
FileNames USING [GetShortName],
FS USING [Error, StreamOpen],
Imager USING [Context, ScaleT, SetFont, SetXY, metersPerInch, ShowRope, ShowText, TranslateT],
ImagerFont USING [Find, Font, Scale],
ImagerInterpress USING [Close, Create, DoPage, Ref],
IO USING [Close, EndOfStream, GetLine, GetLineRope, PutF, RIS, SkipWhitespace, STREAM],
RefText USING [Length, ObtainScratch],
Rope USING [Cat, Find, ROPE, Substr];
FortranToIp:
CEDAR
PROGRAM
IMPORTS
Commander, Convert, FileNames, FS, Imager, ImagerFont, ImagerInterpress, IO, RefText, Rope = {
height: NAT ← 6;
maxLinesPerPage: REAL ← 10.75*72/(height+leading);
gacha: ImagerFont.Font ← ImagerFont.Find["Xerox/Pressfonts/Gacha-brr"];
gacha6: ImagerFont.Font ← ImagerFont.Scale[gacha, height];
MakeOutFileName:
PROC [in: Rope.
ROPE]
RETURNS [out: Rope.
ROPE] = {
lastDot: INT ← Rope.Find[in, "."];
UNTIL lastDot = -1
DO
in ← Rope.Substr[in, 0, lastDot];
lastDot ← Rope.Find[in, "."];
ENDLOOP;
out ← FileNames.GetShortName[in, TRUE];
out ← Rope.Cat[out, ".ip"];
};
FortranToIp: Commander.CommandProc = {
commandLineStream: IO.STREAM = IO.RIS[cmd.commandLine];
buffer: REF TEXT ← RefText.ObtainScratch[100];
DO
skipWhitespace: INT = IO.SkipWhitespace[commandLineStream];
inFileName: Rope.ROPE ← IO.GetLineRope[commandLineStream ! IO.EndOfStream => EXIT];
fortran: IO.STREAM = FS.StreamOpen[inFileName ! FS.Error => EXIT];
outFileName: Rope.ROPE ← MakeOutFileName[inFileName];
ip: ImagerInterpress.Ref ← ImagerInterpress.Create[outFileName];
done: BOOL ← FALSE;
pages: INT ← 0;
linesThisPage: INT ← 0;
Page:
PROC [context: Imager.Context] = {
Imager.ScaleT[context, Imager.metersPerInch/72];
Imager.TranslateT[context, [0.5*72, 10.75*72]]; -- Indent slightly, Top of page
Imager.SetFont[context, gacha6];
Imager.SetXY[context, [7.50*72, 0]];
Imager.ShowRope[context, Convert.RopeFromInt[pages+1]];
IF RefText.Length[buffer] # 0
THEN {
-- Left over
Imager.SetXY[context, [0, 0]];
Imager.ShowText[context, buffer, 1];
Imager.TranslateT[context, [0, -(height+leading)]];
linesThisPage ← linesThisPage + 1; };
WHILE linesThisPage < maxLinesPerPage
DO
offset: NAT ← 1;
buffer ← IO.GetLine[fortran, buffer ! IO.EndOfStream => GOTO EndOfInput];
IF RefText.Length[buffer] # 0
THEN {
SELECT buffer[0]
FROM
'1 => IF linesThisPage # 0 THEN RETURN; -- New Page
'0 =>
{
-- Double space
Imager.TranslateT[context, [0, -(height+leading)]];
linesThisPage ← linesThisPage + 1; };
Ascii.SP => NULL; -- Single space
'X, 'Y => NULL; -- Plot start/stop flags
'( => offset ← 0; -- Bug in Spice ?? (only used on Plot headings)
ENDCASE =>
IO.PutF[cmd.out, "\n ** Unknown character in Col 1: %G ", [character[buffer[0]]]];
Imager.SetXY[context, [0, 0]];
Imager.ShowText[context, buffer, offset];
Imager.TranslateT[context, [0, -(height+leading)]];
linesThisPage ← linesThisPage + 1; };
buffer.length ← 0;
REPEAT EndOfInput => done ← TRUE;
ENDLOOP;
};
IO.PutF[cmd.out, "%G <= %G ", [rope[outFileName]], [rope[inFileName]]];
UNTIL done
DO
linesThisPage ← 0;
ImagerInterpress.DoPage[self: ip, action: Page];
pages ← pages + 1;
IO.PutF[cmd.out, "!"];
ENDLOOP;
ImagerInterpress.Close[ip];
IO.Close[fortran];
IO.PutF[cmd.out, "\n%G has %G pages.\n", [rope[outFileName]], [integer[pages]]];
ENDLOOP;
IO.Close[commandLineStream];
};
Commander.Register["FortranToIp", FortranToIp, "Convert Fortran LPT printout files with carrage controls in col 1 to IP files"];
}.