// author Stuart Errol Anderson;
//Mar 7, 2014
// stuart.errol.anderson@gmail.com
//  pp2html
//  projective plane to html and save

#include <iomanip>
#include <cmath>
#include <vector>
#include <iterator>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <exception>
#include <sys/time.h>
double get_time(){
	    struct timeval t;
	    gettimeofday(&t, NULL);
	    double d = t.tv_sec + (double) t.tv_usec/1000000;
	    return d;
	}
int main (int argc, char* argv[]){
	//create an ifstream infile
	double time_start = get_time();
	std::ifstream infile;
	//open ifstream infile
	infile.open(argv[1]);
	if(infile.fail())
 	{
		if (argc == 1) {
			std::cout<<"pp2html (projective plane to html) is an application"<<std::endl;
			std::cout<<"designed to convert a projective plane code file to html form."<<std::endl;
			std::cout<<"usage: pp2html filename, where filename is a projective plane code file."<<std::endl;
			std::cout<<"author; Stuart Errol Anderson; Mar 7, 2014 "<<std::endl;
			std::cout<<"stuart.errol.anderson@gmail.com "<<std::endl;
			exit(0);
		}
		else {
            std::cout<<"error; file "<<argv[1]<<" failed to load"<<std::endl;
	       	exit(0);
		}
	}
	std::cout<<"Processing squared surface code ..."<<std::endl;
	//create a stringstream, strings for lines and values
	std::istringstream stream;
	std::istringstream ss;
	std::ostringstream ous;
	std::string line;
	std::string value;
	std::ofstream html_file;	// create ofstream bouwkampcode
	int counter = 1;
	int Order = 0;
	double RectWidth = 0;
	double RectHeight = 0;
	int IsomerCount = 0;
		while (( std::getline(infile, line, '\n' ) ) &&(line.length()>1)){ // extract lines
  		 size_t found;
  		 std::string linetitle;
  		 std::string lineend;
  		 std::string schar;
  		 found=line.find("r");
  		 if (found != std::string::npos) {
              linetitle = line.substr(0,found-1); // separate the bouwkampcode from the extended fields if they exist
              lineend = line.substr(found+1);
         } else {
        	  linetitle = line;
        }
  		//if bouwkampcode convert to tablecode by deleting parentheses and commas and replacing with whitespace

		std::string newline;
  		newline += linetitle;
		stream.clear();
		stream.str(newline); 
		int s = 0;
		while ( stream >> value  ) 	{ // get values in columns
				if (s == 0) { 
					std::istringstream ss(value);
					ss >> Order ;//1st value is order
					ss.str("");
				}
	  			if ((s)== 1) {
		  			std::istringstream	ss(value);
		  			ss >> RectWidth ;//2nd value is width
		  			ss.str("");
	  			}
				if ((s)== 2) {
					std::istringstream	ss(value);
					ss >> RectHeight ;//3rd value is height
					ss.str("");
				} 
				if ((s)== 3) {
					std::istringstream	ss(value);
					ss >> IsomerCount ;//3rd value is height
					ss.str("");
					if (IsomerCount == 1) {schar = "A";}
					if (IsomerCount == 2) {schar = "B";}
					if (IsomerCount == 3) {schar = "C";}
					if (IsomerCount == 4) {schar = "D";}
					if (IsomerCount == 5) {schar = "E";}
				} 
			s++;
		} 

		ous <<argv[1]<<".html";
		html_file.open (ous.str().c_str(), std::ios::out|std::ios::app);
		if (!html_file) // check it opened ok
		{
				std::cerr << "Cannot open file " << ous.str() << " for output.\n"; 
		}
		
			
		if (RectHeight!=1) {
			//std::cout <<Order<<": "<<RectWidth<<"&radic;"<<" x "<<RectHeight<<"&radic;"<<" "<<schar<< std::endl;
		html_file <<"<!--[if !IE]>-->"<<'\n'
		 <<"<object data=\"FPSPPs2-7_"<<counter<<".svg \""<<" type=\"image/svg+xml\""<<'\n'
			 <<"        width=\""<<510<<"\" height=\""<<std::fixed<<int(500*RectHeight/RectWidth+100)<<"\" id=\"mySVGObject\"> <!--<![endif]-->"<<'\n'
		<<"<!--[if lt IE 9]>"<<'\n'
		  <<"<object src=\"FPSPPs2-7_"<<counter<<".svg \""<<" classid=\"image/svg+xml\""<<'\n'
			 <<"         width=\""<<510<<"\" height=\""<<std::fixed<<int(500*RectHeight/RectWidth+100)<<"\" id=\"mySVGObject\"> <![endif]-->"<<'\n'
		<<"<!--[if gte IE 9]>"<<'\n'
		  <<"<object data=\"FPSPPs2-7_"<<counter<<".svg \""<<" type=\"image/svg+xml\""<<'\n'
			 <<"         width=\""<<510<<"\" height=\""<<std::fixed<<int(500*RectHeight/RectWidth+100)<<"\" id=\"mySVGObject\"> <![endif]-->"<<'\n'
		 <<" </object>"<<'\n'
			<<"<h2> " <<Order<<": "<<std::setprecision(1)<<std::fixed<<int(RectWidth)<<"&radic;"<<"2 x "<<int(RectHeight)<<" &radic;"<<"2 "<<schar<<"</h2>"<< std::endl<<std::setprecision(3)<<std::fixed;

		} else {
		html_file <<"<!--[if !IE]>-->"<<'\n'
		  <<"<object data=\"FPSPPs2-7_"<<counter<<".svg \""<<" type=\"image/svg+xml\""<<'\n'
			 <<"         width=\""<<510<<"\" height=\""<<std::fixed<<int(500*RectHeight/RectWidth+100)<<"\" id=\"mySVGObject\"> <!--<![endif]-->"<<'\n'
		<<"<!--[if lt IE 9]>"<<'\n'
		  <<"<object src=\"FPSPPs2-7_"<<counter<<".svg \""<<" classid=\"image/svg+xml\""<<'\n'
			 <<"         width=\""<<510<<"\" height=\""<<std::fixed<<int(500*RectHeight/RectWidth+100)<<"\" id=\"mySVGObject\"> <![endif]-->"<<'\n'
		<<"<!--[if gte IE 9]>"<<'\n'
		  <<"<object data=\"FPSPPs2-7_"<<counter<<".svg \""<<" type=\"image/svg+xml\""<<'\n'
			 <<"         width=\""<<510<<"\" height=\""<<std::fixed<<int(500*RectHeight/RectWidth+100)<<"\" id=\"mySVGObject\"> <![endif]-->"<<'\n'
		 <<" </object>"<<'\n'
			<<"<h2> " <<Order<<": "<<std::setprecision(1)<<std::fixed<<RectWidth<<"&radic;"<<"2 x "<<RectHeight<<"&radic;"<<"2 "<<schar<<"</h2>"<< std::endl<<std::setprecision(3)<<std::fixed;
		}
		counter++;
		html_file.close();
		ous.str("");
		ous.clear();
	}
	 infile.close();  
	double time_end = get_time();
	std::cout<<counter-1<<" squared surface codes saved in html form "<<std::endl;
	std::cout<<"This program has run for: "<< time_end - time_start<< " seconds."<<std::endl;
  return 0;
}// end function
