DoBreakup:
PROC [inputName:
ROPE, log:
IO.
STREAM, style: BreakupStyle, div:
INT] ~ {
logProc:
PROC [class:
INT, code:
ATOM, explanation:
ROPE] ~ {
IO.PutRope[log, "\n *** Interpress Error "];
IO.PutRope[log, explanation];
};
n: ExpandedName ~ ExpandName[FS.FileInfo[inputName].fullFName];
base: ROPE ~ Rope.Substr[n.fullFName, n.cp.base.start, n.cp.base.length];
ext: ROPE ~ Rope.Substr[n.fullFName, n.cp.ext.start, n.cp.ext.length];
logPages: BOOL ~ div#1 OR style#pageCount;
master: Interpress.Master;
nFiles, j: INT ← 0;
IO.PutRope[log, "Reading "];
IO.PutRope[log, n.fullFName];
IO.PutRope[log, " . . . "];
master ← Interpress.Open[n.fullFName, logProc];
IO.PutF[log, IF logPages THEN " %g pages\n" ELSE " %g pages; ", [integer[master.pages]]];
SELECT style
FROM
pageCount => nFiles ← (master.pages+div-1)/div;
numFiles => nFiles ← div;
ENDCASE => ERROR;
FOR i:
INT
IN [1..nFiles]
DO
lim:
INT ~
SELECT style
FROM
pageCount => MIN[j+div, master.pages],
numFiles => (i*master.pages + div/2)/div,
ENDCASE => ERROR;
name: ROPE ~ Rope.Cat[base, "-", Convert.RopeFromInt[i], ".", ext];
out: ImagerInterpress.Ref ~ ImagerInterpress.Create[name];
IO.PutRope[log, FS.ExpandName[name].fullFName];
WHILE j<lim
DO
onePage:
PROC [context: Imager.Context] ~ {
IF logPages THEN IO.PutF[log, " [%g", [integer[j]]];
Interpress.DoPage[master, j, context, logProc];
IF logPages THEN IO.PutRope[log, "]"];
};
j ← j+1;
ImagerInterpress.DoPage[out, onePage];
ENDLOOP;
IO.PutRope[log, IF logPages THEN "\n" ELSE " "];
ImagerInterpress.Close[out];
ENDLOOP;
IO.PutRope[log, "done.\n"];
};
InterpressBreakupCommand: Commander.CommandProc ~ {
stream: IO.STREAM ← IO.RIS[cmd.commandLine];
style: BreakupStyle ← pageCount;
div: INT ← 1;
inputName: ROPE ← GetCmdToken[stream];
IF inputName.Equal["-by"]
THEN {
div ← stream.GetInt[!IO.Error => GOTO SyntaxError];
IF div<=0 THEN RETURN [result: $Failure, msg: IO.PutFR["Must divide by a positive number of pages, not %g", [integer[div]] ]];
inputName ← GetCmdToken[stream !IO.Error => GOTO SyntaxError];
}
ELSE
IF inputName.Equal["-into"]
THEN {
style ← numFiles;
div ← stream.GetInt[!IO.Error => GOTO SyntaxError];
IF div<=1 THEN RETURN [result: $Failure, msg: IO.PutFR["Number of output files must be greater than 1, not %g", [integer[div]] ]];
inputName ← GetCmdToken[stream !IO.Error => GOTO SyntaxError];
};
IF inputName = NIL THEN GOTO SyntaxError;
DoBreakup[inputName, cmd.out, style, div ! FS.Error => IF error.group = user THEN {result ← $Failure; msg ← error.explanation; CONTINUE}];
EXITS SyntaxError => RETURN[result: $Failure, msg: cmd.procData.doc]};