/*
 * Daniel Arvesen
 * CS127
 * Project 3: Cryptogram
 */

#include <iostream>
#include <fstream>
#include <string>
#include "cs127.h"

using namespace std;

int main(int argc, char* argv[])
{
  string file_i, file_o;
  char walk;
  int check;
  int counter = 0;
  const int SIZE = 1000;
  string original[SIZE];
  string replacement[SIZE];
  int place = 0;
  const int x = 1000;
  ofstream output3;

  if (argc != 3)
    {
      cout << "Usage: " << argv[0] << " [Encrypted file] [Results file]\n";
      return 1;
    }
  else
    {
      file_i = argv[1];
      file_o = argv[2];
    }

  /*
    cout << "What file will you be using for the cryptogram? ";
    cin >> file_i;
  
    cout << "What file will I place your results in? ";
    cin >> file_o;
  */

  cout << "TO EXIT PROGRAM ENTER \"0\" OR PRES CTRL+C\n";
  while(1)
    {
      ifstream input, input2;   // i could never get this to work right
      ofstream output, output2; // unless it was redeclared every time.
      string block[SIZE];       // some problem with scope
      char a, b;
      input.open(file_i.c_str());
      if (input.fail())
	{
	  cout << "Unable to read " << file_i << "\n";
	  return 1;
	}
      
      output.open(file_o.c_str());
      if (output.fail())
	{
	  cout << "Unable to open " << file_o << " as a results file.\n";
	  return 1;
	}
           
      // ********************************************
      
      cout << "\nWhat character would you like to replace? ";
      cin >> a;   

      if (a == '0')
	break;

      cout << "What would you like to replace " << a << " with? ";
      cin >> b;

      if (a == '0')
	break;
      a = toupper(a);
      b = toupper(b);
      original[place] = a;
      replacement[place] = b;
      
      while (input.get(walk)) 
	{ 
	  if (walk == a) 
	    output.put(b); 
	  else 
	    output.put(toupper(walk));
	}

      counter++;
      place++;
      input.close();
      output.close();
      
      // **********************************************

      input2.open(file_o.c_str());
      if (input2.fail())
	cout << "Failed open temp input file\n" << endl;
      
      output2.open(file_i.c_str());
      if (output2.fail())
	cout << "Failed opening temp output file\n";
      
      while (input2.get(walk))
	output2.put(walk);
      
      input2.close();
      output2.close();
    }  
  
  // ***********************************************

  output3.open(file_o.c_str());    

  cout << "\n\n***************\n";
  if (counter == 1)
    {
      cout << "I did " << counter << " replacement.\n"; 
      output3 << "I did " << counter << " replacement.\n";
    }

  else
    {
      cout << "I did " << counter << " replacements.\n";
      output3 << "I did " << counter << " replacements.\n";
    }

  cout << "Here are the results:\n\n";
  output3 << "Results:\n\n";

  cout << "Original \t";
  output3 << "Original \t";
  for (int i = 0; i < counter; ++i)
    {
      cout << original[i] << " | ";
      output3 << original[i] << " | ";
    }
  cout << "\nReplacement\t";
  output3 << "\nReplacement\t";
  for (int j = 0; j < counter; ++j)
    {
      cout << replacement[j] << " | ";
      output3 << replacement[j] << " | ";
    }
  cout << "\nResults printed to " << file_o << ".\n";
  output3 << endl;

  return 0;
}
