// PagePrinter v1.0

// N.B : This source uses the StringX class from The Hanged Man. Mail me if
//       you are interrested (steffen.mars@stenloese.mail.telia.com).

const char*		ver="$VER:[1;0;1mPagePrinter v1.1[;;m ŠThe Hanged Man 1998\n";
const char*		usage="[1;0;1mUSAGE: [;;m PagePrinter INFILE <file> OUTFILE <file> [LINES n] [COLUMNS n] [JUSTIFY l|r|c] [TAB n]\n";
const char*		version=&ver[5];

#include <CXX:Protos/StringX.cxx>
#include <fstream.h>

void main(int argc,char** argv)
{
	if (argc<3 || argv[1][0]=='?')
	{
		cout <<version<<usage;
		return;
	}
	
	ifstream	infile;
	ofstream	outfile;
	int			lines=63;
	int			columns=96;
	char		justify='c';
	int			indent=0;
	int			tabsize=4;
	int			col=1,row=1;
	int			argnr=1;
	int			loop=1;
	char		chr;
	int			count;
	int			page = 1;
	int			paragraph=0;
	StringX		streng;
	StringX		page_streng;

	// Check om antal af argumenter er iorden
	while (argnr<argc)
	{
		streng = argv[argnr++];
		
		if (streng == "INFILE")
		{
			infile.open(argv[argnr++]);
			if (!infile)
			{
				cout <<"Unable to open input file"<<endl;
				return;
			}
			continue;
		}
		
		if (streng == "OUTFILE")
		{
			outfile.open(argv[argnr++]);
			if (!outfile)
			{
				cout <<"Unable to open output file"<<endl;
				return;
			}
			continue;
		}
		
		if (streng == "LINES")
		{
			lines = atoi(argv[argnr++]);
			continue;
		}
		
		if (streng == "COLUMNS")
		{
			columns = atoi(argv[argnr++]);
			continue;
		}
		
		if (streng == "JUSTIFY")
		{
			justify = argv[argnr++][0];
			continue;
		}
		
		if (streng == "TAB")
		{
			tabsize = atoi(argv[argnr++]);
			continue;
		}
		
		if (streng == "INDENT")
		{
			indent = atoi(argv[argnr++]);
			continue;
		}
		
		cout <<"Unknown code. Goodbye !"<<endl;
		return;
	}
	if (!(lines && columns))
	{
		cout <<"Bad values for columns and lines : " <<columns<<"x"<<lines<<endl;
		return;
	}
	
	while (loop)
	{
		infile.get(chr);
		
		switch (chr)
		{
			case 0:
			loop = 0;
			break;
			
			case '\t':
			do
			{
				outfile.put(' ');
				col++;
			}while ((col-1)%tabsize);
			break;
			
			case '\n':
			outfile<<endl;
			col=1;
			row++;
			paragraph=0;
			break;
			
			case ' ':
			if (col>1 || paragraph==0)
			{
				outfile.put(' ');
				col++;
			}
			break;
			
			default:
			col++;
			outfile.put(chr);
			paragraph=1;
			break;
		}
		if (col>= columns) { col=1;row++;outfile<<endl;}
		if (row==lines)
		{
			page_streng = page++;
			streng = "- ";
			streng += page_streng;
			streng += " -";
			
			switch (justify)
			{
				case 'L':
				case 'l':
				count=0;
				break;
				
				case 'R':
				case 'r':
				count = columns - streng.Len();
				break;
				
				default:
				count = (columns - streng.Len())/2;
				break;
			}
			outfile<<endl;
			for (;count;count--) outfile.put(' ');
			outfile <<streng <<endl;
			row=col=1;
		}
		if (infile.peek() == EOF) loop = 0;
	}

	// End of by finishing the page and printing the page number
	while (row<lines)
	{
		outfile<<endl;
		row++;
	}

	page_streng = page++;
	streng = "- ";
	streng += page_streng;
	streng += " -";

	switch (justify)
	{
		case 'L':
		case 'l':
		count=0;
		break;
		
		case 'R':
		case 'r':
		count = columns - streng.Len();
		break;
		
		default:
		count = (columns - streng.Len())/2;
		break;
	}
	outfile<<endl;
	for (;count;count--) outfile.put(' ');
	outfile <<streng <<endl;
	
	infile.close();
	outfile.close();
}	