/*
UFSC. CTC, EEL

PUCRS, FACIN, GSE

Conversor HTML - abre arquivo de entrada (texto), e gera arquivo de saida
(texto), ambos HTML, substituindo todas as ocorrencias de <B> por <i>, <b>
por <i>, </B> por </I> e </b> por </i>.

Autor: Eduardo Augusto Bezerra
Data: 24/05/2006

*/
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {

   const int MAX = 100000;
   string str1;

   std::ifstream fin;
   std::ofstream fout;

   char * infile = "arq_in.html";
   char * outfile = "arq_out.html";

   char ach[MAX];

   cout << "Abrindo arquivo de entrada ..." << endl;
   fin.open( infile , ios::in );
   if ( !fin.is_open( ) )
   	return (0);
   memset( ach , 0 , MAX );
   fin.read( ach , MAX );
   str1 = ach;

   cout << "Abrindo arquivo de saida ..." << endl;
   fout.open( outfile , ios::out ); // append
   if ( !fout.is_open( ) ) {
   	fin.close( );
	return (0);
   }
   cout << "Leitura do HTML de entrada, substituicao dos negritos por" <<
           " italico, e gravacao do novo arquivo." << endl << endl;

   cout << "Ocorrencias dos tokens: 1 = <B>, 2 = <b>, 3 = </B>, 4 = </b>" << endl;

		while (1)	{
			int pos = 0;
			pos = str1.find( "<b>" , pos + 1 );
			if ( pos == -1 ) break;			
			str1.replace( pos , 3 , "<i>" );
            cout << "1";
       }
        cout << endl;
		while (1)	{
			int pos = 0;
			pos = str1.find( "<B>" , pos + 1 );
			if ( pos == -1 ) break;			
			str1.replace( pos , 3 , "<i>" );
            cout << "2";
       }
        cout << endl;
		while (1)	{
			int pos = 0;
			pos = str1.find( "</b>" , pos + 1 );
			if ( pos == -1 ) break;			
			str1.replace( pos , 4 , "</i>" );
            cout << "3";
       }
        cout << endl;
		while (1)	{
			int pos = 0;
			pos = str1.find( "</B>" , pos + 1 );
			if ( pos == -1 ) break;			
			str1.replace( pos , 4 , "</i>" );
            cout << "4";
       }
        cout << endl << "Escrita no arquivo de saida ..." << endl;
		fout << str1 << endl;
		
   cout << "Fechando o arquivo de entrada ..." << endl;
   fin.close( );
   cout << "Fechando o arquivo de saida ..." << endl;
   fout.close( );

   return 0;
}

