// Console TextViewer 1.0
// By Ben a.k.a DreamVB
// 23:46 28/09/2016

#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <algorithm>

using namespace std;
using std::cout;
using std::endl;

string UCase(string src)
{
	string Temp = src;
	std::transform(Temp.begin(), Temp.end(), Temp.begin(), ::toupper);
	return Temp;
}

string LCase(string src)
{
	string Temp = src;
	std::transform(Temp.begin(), Temp.end(), Temp.begin(), ::tolower);
	return Temp;
}

int main(int argc, char *argv[]){
	
	string src_file = "";
	string StrLine = "";
	string StrTemp = "";
	string parm = "";
	fstream fs;

	int pos = string::npos;
	int i = 2;
	int Rows = 10;
	int CurRow = 1;

	//Flag vars
	bool DispUpper = false;
	bool DispLower = false;
	bool ClearSrc = false;

	if (argc < 2){
		cout << "Usage: " << argv[0] << " <filename>" << endl;
		exit(1);
	}

	//Get any other parms
	while (i < argc){
		if (argv[i][0] == '/'){
			//Extract parm
			parm = LCase(argv[i]);
			pos = parm.find_first_of(':');
			if (pos != string::npos){
				//Extract value.
				StrTemp = parm.substr(pos + 1);
				//Fix the line
				parm.erase(pos);
			}
			//Set ammount of rows to show before pause.
			if (parm == "/rows"){
				Rows = atoi(StrTemp.c_str());
			}
			//Check if displaying in uppercase
			if (parm == "/u"){
				DispUpper = true;
			}
			//Check if displaying lowercase
			if (parm == "/l"){
				DispLower = true;
			}
			//Check if clearing screen on first load
			if (parm == "/c"){
				ClearSrc = true;
			}
		}
		i++;
	}

	//Get filename
	fs.open(argv[1], ios::in);
	
	//Check if file was opened.
	if (!fs.good()){
		cout << "Cannot read file: " << endl;
		cout << argv[1] << endl;
		exit(1);
	}
	
	//Check if clear screen is enabled
	if (ClearSrc){
		system("cls");
	}

	//Read in the file.
	while (!fs.eof()){
		//Read in line
		std::getline(fs, StrLine);
		//Check if current line is greator than total of display lines.
		if (CurRow > Rows){
			//Wait for a key press
			getch();
			CurRow = 1;
		}

		//Convert to uppercase.
		if (DispUpper){
			StrLine = UCase(StrLine);
		}

		//Convert to lowercase.
		if (DispLower){
			StrLine = LCase(StrLine);
		}

		//Output the line
		cout << StrLine.c_str() << endl;

		//INC counters
		CurRow++;
	}

	//Close file.
	fs.close();
	return 0;
}