
#include <iostream.h>
#include <time.h>
#include <fstream.h>
#include <stdlib.h>


char randhex();
char hex(int num);

int main()
{
  char one, two, three;
  char four, five, six; 
  int i = 0;
  char inan[30];
  char outan[30];
  char ch = ' ';
  
  srand(time(NULL));

  cout << "Please enter a file name to anaylise" << endl;
  cin.getline(inan, 30);
  ifstream infile(inan);
  if(infile.fail())
    cout << "Opening of " << inan << " Failed" << endl;
  
  while(ch != '.')
    {
      ch = inan[i];
      outan[i] = ch;
      i++;
    }
  outan[i] = 'h';
  i++;
  outan[i] = 't';
  i++;
  outan[i] = 'm';
  i++ ;
  outan[i] = '\0';
  
  //cout << "opening " << outan << endl;
  
  ofstream outfile(outan);
  
  if(outfile.fail())
    {
    cout << "Opening of " << outan << " Failed" << endl;
    return -1;
    }

  // setup html file
  outfile << "<html> \n";
  outfile << "<head> \n";
  outfile << "<title>Scope Analysis of " << inan << "</title>" << endl;
  outfile << "</head> \n <body>" << endl;


  while(!infile.eof())
    {
      ch = infile.get();
      
      if(!infile.eof())
	{
	  
	  if(ch == '{')
	    {
	      outfile << "{";
	      one = randhex();
	      two = randhex();
	      three = randhex();
	      four = randhex();
	      five = randhex();
	      six = randhex();

	      outfile << "<table bgcolor='#" << one << two << three << four << five << six << "' cellspacing='1' cellpadding='1' border='0'> \n<tr>\n<td>\n" << endl;
	  
	    }
	  else if(ch == '}')
	    {
	      outfile << "</font>" << endl;
	      outfile << "</td>\n</tr>\n</table>" << endl;
	      outfile << "}";
	    }
	  else if(ch == '\n')
	    {
	      outfile << "<br>" << endl;
	    }
	  
	  else if(ch == '<')
	    {
	      outfile << "&lt;";
	    }
	
	  else if(ch == '>')
	    {
	      outfile << "&gt;";
	    }
	  
	  else
	    outfile << ch;
	  
	}
      
    }
  

  // finish off html file

  outfile << "<hr>" << endl;
  outfile << "Scope Analysis of " << inan << " produced by jambe <FONT color=#cc0000>Sco</FONT><FONT color=#00cc00>pe<FONT 
color=#0033ff>Ana</FONT>ly</FONT><FONT color=#cc0000>zer 
</FONT>" << endl;
  outfile << "<a href = \"http://www.jambe.cjb.net\">www.jambe.cjb.net</a>" << endl;
  outfile << "\n </body>" << endl;
  outfile << "</html>";

  cout << "Scope analysed" << endl;
  cout << "Please view " << outan << " To see the results " << endl;
  
  cin >> i;
  return 0;
}

char randhex(void)
{
 int rn;
 
 rn = rand();
 //cout << " Actual random number: " << rn;
 rn = (rn * 1063001) % 16;
 // cout << "Random number is " << rn << endl;
    
 if(rn < 0)
   rn = -rn;
 
 
 
 return (hex(rn));
} 




char hex (int num)
{
  char rn;
  
  if(num == 0)
    rn = '0';
  
  else if (num == 1)
    rn = '1';
  
  else if (num == 2)
    rn = '2';
  
  else if (num == 3)
    rn = '3';
  
  else if (num == 4)
    rn = '4';
  
  else if (num == 5)
    rn = '5';
  
  else if (num == 6)
    rn = '6';

  else if (num == 7)
    rn = '7';
  
  else if (num == 8)
    rn = '8';
  
  else if (num == 9)
    rn = '9';
  
  else if (num == 10)
    {
      rn = 'a';
    } 
  
  
  else if (num == 11)
    {
      rn = 'b';
    } 
  
  
  
  else if (num == 12)
    {
      rn = 'c';
    } 
  
  
  
  else if (num == 13)
    {
      rn = 'd';
    } 
  
  
  
  else if (num == 14)
    {
      rn = 'e';
    } 
  
  
  
  else if (num == 15)
    {
      rn = 'f';
    } 
  

else
  {
    cout << "Error " << num << " is too large to be hexidecmial digit" << endl;
      rn = 'f';    
  }
  

  return rn;
}
