// darc
// COMS4163
// Time to completion:  7hrs
// Still hangs on a certain circumstance but I have to study for my database concepts test.
//
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

bool in_php_block = false;

int question(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws;
 
  if(in_stream.peek() == '>')
  {
		in_stream >> tmp_char;
    in_php_block = false;
    return 0;
  }
  else if(in_php_block == false)
   return 0; 
	else
		cout << tmp_char;
	

	return 0;
}

int brackets(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws;
	//if this is a PHP opener
	if(in_stream.peek() == '?')
        {
            in_stream >> tmp_char; 
            if(in_stream.peek() != 'p')
            {
              in_php_block = true;
              return 0;
            }
            else if(in_stream.peek() == 'p')
            {
              in_stream >> tmp_char;
              in_stream >> tmp_char;
              in_stream >> tmp_char;
              in_php_block = true;
            }
          return 0;
        }
        else if(in_php_block == false);
        {
		while(tmp_char != '>' && in_stream >> tmp_char)
		{
			//check for PHP closing tags
			if(tmp_char == '?')
				question(in_stream, tmp_char);
			else if(tmp_char == '<')
			{
				if(in_stream.peek() == '?')
				{
  					in_stream >> tmp_char;
            if(in_stream.peek() != '>')
            {
              in_php_block = true;
              break;
            }
            else
            {
              in_stream >> tmp_char;
              in_stream >> tmp_char;
              in_stream >> tmp_char;
              in_php_block = true;
            }
					return 0;
				}
      }
      else if(tmp_char == '\n')
          cout << '\n';
			
    }
        } 
	return 0;
}

int comments(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws;
        if(in_stream.peek() == '/')
          while(in_stream.peek() != '\n')
            in_stream >> tmp_char;
//Check if it's a /* comment
        else if(in_stream.peek() == '*')
        {
          in_stream >> tmp_char;
          in_stream >> tmp_char;
          while((tmp_char != '*') || (in_stream.peek() != '/'))
          {
            in_stream >> tmp_char;
            if(tmp_char == '\n')
              cout << '\n';
          }
          in_stream >> tmp_char; //discard end of comment '/'
        }
//Else, it wasn't a comment, so cout the '/' in tmp_char 
        else
          cout << tmp_char;
	return 0;
}
void quotes(fstream&, char);

int apostrophes(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws;
	in_stream >> tmp_char;
  in_stream >> tmp_char;
	while(tmp_char != '\'')
        {
      	  in_stream >> tmp_char;
          if(in_php_block == true)
          {
            if(tmp_char == '\n')
              cout << '\n';
            else if(tmp_char == '\\')
      	  	  in_stream >> tmp_char;
            else if(tmp_char == '\"')
              quotes(in_stream, tmp_char);
            else if(tmp_char == '<' && in_stream.peek() == '?')
              brackets(in_stream, tmp_char);
            else if(tmp_char == '<') //&& in_stream.peek() == '?')
              in_stream >> tmp_char;
            else if(tmp_char == '?') //&& in_stream.peek() == '>')
              question(in_stream, tmp_char);
            else if(tmp_char == '\'')
              return 0;
          }
          else if(in_php_block == false)
          {
            if(tmp_char == '\n')
              cout << '\n';
            else if(tmp_char == '\\')
              in_stream >> tmp_char;
            else if(tmp_char == '\"')
              quotes(in_stream, tmp_char);
            else if(tmp_char == '<') //&& in_stream.peek() == '?')
              brackets(in_stream, tmp_char);
            else if(tmp_char == '?') //&& in_stream.peek() == '>')
              in_stream >> tmp_char;
            else if(tmp_char == '\'')
              return 0;
          }
        }
	return 0;
}


void quotes(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws; 
	in_stream >> tmp_char; 
        while(tmp_char != '\"')
        {
      	  in_stream >> tmp_char;
          if(in_php_block == true)
          {
            if(tmp_char == '\n')
              cout << '\n';
            else if(tmp_char == '\\')
              in_stream >> tmp_char;
            else if(tmp_char == '\'')
              apostrophes(in_stream, tmp_char);
            else if(tmp_char == '<' && in_stream.peek() == '?')
              brackets(in_stream, tmp_char);
            else if(tmp_char == '<') //&& in_stream.peek() == '?')
              in_stream >> tmp_char;
            else if(tmp_char == '?') //&& in_stream.peek() == '>')
              question(in_stream, tmp_char);
            else if(tmp_char == '\"')
              return ;

          }
          else if(in_php_block == false)
          {
            if(tmp_char == '\n')
              cout << '\n';
            else if(tmp_char == '\\')
              in_stream >> tmp_char;
            else if(tmp_char == '\'')
              apostrophes(in_stream, tmp_char);
            else if(tmp_char == '<' && in_stream.peek() == '?')
              brackets(in_stream, tmp_char);
            else if(tmp_char == '<') //&& in_stream.peek() == '?')
              brackets(in_stream, tmp_char);
            else if(tmp_char == '?') //&& in_stream.peek() == '>')
              question(in_stream, tmp_char);
            else if(tmp_char == '\"')
              return ;

          }
       }
	return ;
}


int main(int argc, char* argv[])
{
    string in_file;
    string out_file;
    char tmp_char, in_char;
    int i = 1;
    fstream out_stream;
    fstream in_stream;
/* Because I originally wrote this to manipulate a file stream, not work with cin's (I used peek() a lot) I now have to cin all input into a file, then read that filestream in to use the code I'd already developed and tested.  More overhead, but reused everything I'd already written so it is definitely worth it. */

    out_file = ".tmpx";
    out_stream.open(out_file.c_str(), ios::out);
    
    cin >> noskipws;
    while(cin >> in_char)
      out_stream << in_char;
    out_stream.close();
        
    in_file = ".tmpx";
    in_stream.open(in_file.c_str(), ios::in);
   
    //Don't skip white space
    in_stream >> noskipws;
    //While we have input
    while(in_stream >> tmp_char)
    {
      switch(tmp_char)
      {
        // Check for "//" comments
      case '/':
      	comments(in_stream, tmp_char);
        break;

//Check for APOSTROPHE
      case '\'':
        apostrophes(in_stream, tmp_char);
        break;

//Check for QUOTATION        
      case '\"':
        quotes(in_stream, tmp_char);
        break;
       
      case '<':
        if(in_stream.peek() == '?')
          brackets(in_stream, tmp_char);
        break;

      case '?': 
        question(in_stream, tmp_char);
        break;

      case EOF:
        return 0;
        break;

      case '\n':
        cout << '\n';
        break;

      default:
        if(in_php_block == false)
          break;
        else if(in_php_block == true)
          cout << tmp_char;
          break;
      }
    }
    in_stream.close();
    // delete our temp file for our file stream
    remove(".tmpx");
}


