home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
mbug
/
mbug117.arc
/
FOLD2.PZS
/
FOLD2.PAS
Wrap
Pascal/Delphi Source File
|
1979-12-31
|
8KB
|
296 lines
PROGRAM FOLDFILE;
{-----------------------------------------------------------------------------}
{Programme to page a file like this: }
{ }
{ line 1 \ / line 1 line pageLength+1 line 2*pageLength+1 }
{ line 2 | | line 2 ... ... }
{ line 3 | | line 3 ... ... }
{ line 4 | | ... }
{ ... | --> | ... }
{ | | }
{ | | }
{ ... | | ... ... }
{ line n / \ line pageLength line 2*pageLength }
{ }
{-----------------------------------------------------------------------------}
{$C+}
{$R+}
CONST
defPageWidth = 79; {default values}
defPageLength = 22;
defNumCols = 3;
defBorderChar = '-';
maxPageLength = 100; {as big as the memory limitations allow}
asciiFF = ^l;
TYPE
CPMname = string[14];
fcbname = string[11];
anyStr = string[255];
pageArray = array [1..maxPageLength] of anyStr;
VAR
pageWidth,pageLength : integer;
inFile,outFile : text;
inFname,outFname : CPMname;
lineNumber: integer;
lineNumbers : boolean;
currPage : pageArray;
bordered : boolean; {whether pages are separated by a line of chars}
borderChar : char; {appears between pages}
numCols : integer;
ffeeds : boolean;
{$I linemsgs.inc}
{$I cline.inc}
PROCEDURE showSummary;
begin
writeln ('*****************************************************************************');
writeln;
writeln ('command: FOLD - A programme that folds files into pages of columns.');
writeln;
writeln ('syntax: FOLD in.ext out.ext {-b{c}} {-n} {-cn} {-ln} {-wn} {-f}');
writeln (' | | | | | |');
writeln (' page border char -------------+ | | | | |');
writeln (' turn on line numbering -------------+ | | | |');
writeln (' n columns per page [3] ----------------+ | | |');
writeln (' n lines per page [22] ---------------------+ | |');
writeln (' n chars per line (excess truncated) [80] ------+ |');
writeln (' form feed character after each page ---------------+');
writeln;
writeln ('example: FOLD PROG.PAS LST: -L60 -C2 -W132 -D+ -N -F');
writeln;
writeln ('Sam Lander 1988');
writeln;
writeln ('*****************************************************************************');
end;
FUNCTION checkFile(filename : CPMname) : boolean;
var
checkfl :file;
begin
assign(checkfl,filename);
{$I-}
reset(checkfl);
close(checkfl);
{$I+}
checkFile:= (IOresult = 0);
end;
FUNCTION defaultDrive: char;
begin
DefaultDrive:= chr(Mem[4] + 65);
end;
PROCEDURE showSettings;
begin
writeln ('Folding file ',inFname,' to file ',outFname,' using the following settings:');
writeln;
writeln (' ',numcols,' columns across the page.');
writeln (' Page width: ',pageWidth,' characters.');
writeln (' Page length: ',pageLength,' lines.');
write(' Line numbering ');
if linenumbers then
writeln('on.') else writeln('off.');
if bordered then
writeln (' Border character "',borderChar,'".')
else
writeln (' No border between pages.');
if ffeeds then
writeln (' Pages separated by form feed characters.');
writeln;
end;
PROCEDURE handleCommandFlags;
{Looks at the command line and changes global variables}
var
borderString: string[1];
i : integer;
begin
pageWidth := flagValue('w');
if pageWidth = -1 then
pagewidth := defPageWidth;
pageLength := flagValue ('l');
if pageLength = -1 then
pageLength := defPageLength;
numCols := flagValue ('c');
if numCols = -1 then
numCols := defNumCols;
if flagset('b') then
begin
bordered := true;
borderString := flagString ('b');
if borderString = '' then
borderChar := defBorderChar
else
borderChar := borderString[1];
end
else
bordered := false;
if flagSet ('n') then
linenumbers := true
else
linenumbers := false;
if flagSet ('f') then
ffeeds := true
else
ffeeds := false;
showSettings;
end;
PROCEDURE init;
begin
lineNumber := 1;
end;
PROCEDURE padOut (var s: anyStr; c: char; n: byte);
{fills the string s out to n characters with c}
var
i,oldlen : byte;
begin
oldlen := length(s);
s[0] := chr(n);
for i := oldlen+1 to n do
s[i] := c;
end;
PROCEDURE getLine(len: integer; var s: anyStr);
var
errStr:string[40];
begin
s:='';
if not eof(infile) then
readln (inFile,s)
else
padout (s,' ',len);
if length(s) > len then
begin
errStr := s; {some is truncated off}
if length (errStr) = 40 then
warning('Truncated "'+errStr+'..."')
else
warning('Truncated "'+errStr+'"');
s[0] := chr(len);
end
else
padout (s,' ',len);
end;
PROCEDURE openFiles;
var
present : boolean;
begin
inFname := stringArg(1);
if inFname = '' then
begin
showSummary;
error ('no input file supplied');
end;
present := checkFile (inFname);
if not present then
begin
showsummary;
error('cannot find file '+inFname);
end;
assign (inFile,inFname);
reset (inFile);
outFname := stringArg(2);
if outFname = '' then
begin
showSummary;
error ('no output file supplied');
end;
present := checkFile (outFname);
if present then
waitForAck ('overwriting file '+outFname+'.');
assign (outFile,outFname);
rewrite (outFile);
end;
PROCEDURE closeFiles;
begin
close (inFile);
close(outFile);
end;
PROCEDURE writePage(var p:pageArray); {var parameter to save time and space}
var
pageLine : integer;
begin
pageLine := 1;
for pageLine := 1 to pageLength do
begin
writeln (outFile,p[pageLine]);
if (outFname = 'lst:')or(outFname ='LST:') then
delay (1200); {I have a dodgy printer that needs a rest}
end;
{now, we might want a form feed character}
if ffeeds then
write(outFile,asciiFF);
end;
PROCEDURE blankPage (var p: pagearray);
var i: integer;
begin
for i := 1 to maxPageLength do
p[i] := '';
end;
PROCEDURE getPage(var p: pageArray);
var
temp : anyStr;
extras: byte;
prefix : string[5];
pageLine,colWidth,colNum : integer;
i,start : integer;
begin
blankpage(p);
{first, fix up a page boundary}
if bordered then
begin
p[1][0] := chr(pageWidth);
for i := 1 to pageWidth do
p[1][i] := borderChar;
start := 2;
end
else
start := 1;
if lineNumbers then
extras := numCols*6 + numcols {digits and a space for a separator}
else
extras := numcols; {for a separator}
colwidth := trunc((pageWidth-extras) / numCols) - 1;
for colNum := 1 to numCols do
for pageLine := start to pageLength do
begin
getline (colwidth,temp);
if lineNumbers then
begin
str(lineNumber:5,prefix);
temp := prefix+' '+temp;
end;
temp := temp + ' ';
p[pageline] := p[pageline] + temp;
lineNumber := lineNumber+1;
end;
end;
{--- Main bit ------------------------------------------------------------}
begin
findargs;
init;
openFiles;
handleCommandFlags;
while not eof(infile) do
begin
getpage(currPage);
writePage(currPage);
end;
closeFiles;
end.