// DarC KonQuest 
// COMS4163

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int question(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws;
	if(in_stream.peek() == '>')

		in_stream >> tmp_char;
	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;
          in_stream >> tmp_char;
          in_stream >> tmp_char;
          in_stream >> tmp_char;
          return 0;
        }
        else
        {
		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;
					in_stream >> tmp_char;
					in_stream >> tmp_char;
					in_stream >> tmp_char;
					return 0;
				}
			}
		}
        }
	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;
          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;
}


int apostrophes(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws;
	in_stream >> tmp_char;
	while(tmp_char != '\'')
        {
          if(tmp_char == '\\')
		  in_stream >> tmp_char;
	  in_stream >> tmp_char;
        }
	return 0;
}


int quotes(fstream& in_stream, char tmp_char)
{
	in_stream >> noskipws;
	in_stream >> tmp_char;
        while(tmp_char != '\"')
        {
          in_stream >> tmp_char;
          if(tmp_char == '\\')
          {
            in_stream >> tmp_char;
            in_stream >> tmp_char;
          }
        }
	return 0;
}


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 '<':
        //Remove <?php opener
 	brackets(in_stream, tmp_char);
	break;
      
      case '?':
        question(in_stream, tmp_char);
        break;

      default:
        cout << tmp_char;
        break;
      }
    }
    in_stream.close();
    // delete our temp file for our file stream
    remove(".tmpx");
}


