Program PRINTFILE;


{$I "pcq:Include/DOS.i"}
{$I "pcq:Include/Ports.i"}
{$I "pcq:Include/Parameters.i"}
{$I "pcq:Include/StringLib.i"}
const
	nbretotale = 69;
TYPE

var


	IMPTR 		: string;
	InputFileName  	: String;
	PAGE		: String;
	npage		: String;
	para		: String;
	I,lpage		: Integer;
	lsource,ldest	: Integer;
	p,long,long1	: integer;
	nbligne		: integer;
	nbpage		: integer;
	Infile		: FileHandle;
	Inptr		: FileHandle;
	data		: byte;
	source	 	: ^array [0..100000] of byte;
	numerote	: boolean;


procedure MET_PAGE;
	begin
	strcpy(PAGE,"\n\n\t\t\t\t\c1m-- ");
	lpage:=IntToStr(NPAGE,nbpage);
	Strncat(page,NPAGE,lpage);
	strncat(page," --\c0m\n",7);
	ldest:= DOSwrite(inptr,PAGE,strlen(page));
	nbligne:=nbligne+3;
	end;


procedure VIDE;
	begin
	for p:=long downto nbligne-1 do 
			ldest:=DOSwrite(Inptr,adr(data),1);	
	nbligne:=1;
	inc(nbpage);
	end;


procedure funct;
	begin;
	if not(numerote) then
		begin
		met_page;
		vide;
		end
		else vide;
	end;
			


procedure VIDE1;
	begin
	for p:= long1 downto nbligne+1 do ldest:= DOSwrite(Inptr,adr(data),1);
	nbligne:=long1;
	end;

begin
	long:=nbretotale;
	InputFileName := AllocString(60);
	PAGE := AllocString(60);
	IMPTR:=allocstring(8);
	npage:=allocstring(4);
	para:=allocstring(4);
	IMPTR:="PRT:";
	nbpage:=1;	
	
    GetParam(1, InputFileName);
    GETPARAM(2, para);
    numerote := strieq(para,"-p");
    Infile := DOSOpen(InputFilename, ModeOldFile);
    if not(numerote) then
		long1:=long-8
	else
		long1:=long-5;

    if Infile<>nil then 

	begin
	Inptr  := DOSOpen(IMPTR,ModeOldFile);
	if inptr<>nil then
		begin

		new(source);
		lsource := dosRead(Infile,source,100000);
		DOSClose(Infile);
		i:=0;nbligne:=1;
		while i<=lsource DO
			begin
			Data:=source^[i];
			if data = 10 then inc(nbligne);
			if data <> 12 then ldest:=DOSwrite(Inptr,adr(data),1)
				else
				begin
				 data:=10;
				 VIDE1;
				 funct;
				End;
			if nbligne=long1 then funct;
				
			inc(i);
			end;

		if data <> 12 then
			begin
			data:=10;
			vide1;
			funct;
			end;


		DOSclose(Inptr);
		dispose(source);
		end
		else
			begin
			DOSclose(Infile);
			writeln('error to open prt:');
			end;

	end
	else writeln('error file not found');
  
end.
