/*	*********************************************
*	*************		ROT128		*************
*	*********************************************
*
*   Following program takes two arguments: the input file and the out file. It reads the input file and applies the ROT128 algorithm and saves this as the output file. That is, it rotates all the characters.
*	Made by Thomas Madsen, Oklahoma 2012. You are free to copy and distrubute the code and software. This code and program does not come with any warrenty what so ever.
*	Use at own risk!
*
*/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
	string FileNameIn = "";		// Filename of input file
	string FileNameOut = "";	// Filename of output file
	int filesize = 0;			// Size of the file.
	unsigned char r;			// The character used for reading
	unsigned char w;			// The character used for writing

if (argc==3)					// Checks that we have two arguments when starting the program
	{
	FileNameIn = argv[1];		// First argument is the input file
	FileNameOut = argv[2];		// Second argument is the name of the output file
	ifstream myFileIn (FileNameIn, ios::out | ios::binary);	
	ofstream myFileOut (FileNameOut, ios::in | ios::binary | ios::trunc);   // Note that the if the output file already exists, it will be overwritten.
	
		if(myFileIn.is_open())
		{
			myFileIn.seekg(0, ios::end ); 			// Move to end of file
			filesize = myFileIn.tellg(); 			// Compute file size
			cout << "Working... this might take some time depending on the size of the file... " << endl;
			for (int i =0; i <= filesize -1; i++) 	// Converts file
				{
				myFileIn.seekg (i, ios::beg);		// Move again to the beginning of the file
				myFileIn >> noskipws >> r;			// Reads one character
				w = (r + 128) % 256;				// Perfomes rotation 
				myFileOut << w;						// Writes to output file
				}	// End for loop
			myFileIn.close();
			myFileOut.close();
		} 	// End if open
		else {cout << "Error 1: Input file does not exist?"; return 1;}	
	} // End if argc == 3
else{	// If not two arguments given
		cout << "-------------------------------" << endl;
		cout << "Syntax: rot123 filein finleout" << endl;
		cout << "-------------------------------";
		return 0;
	}	// End else
cout << "Succces!" << endl;
return 0;
}  /* End main*/