miércoles, 7 de octubre de 2009

Codigo fuente de filtro de C a C++ para doxygen

#ifndef MAIN_H
#define MAIN_H

#include
#include

using namespace std;

#ifndef MAX_PATH
#define MAX_PATH 256
#endif

#define MAX_BUFFER 512

#define I_FILE 1
#define O_FILE 0

#define OP_SUCCESS 0
#define ERR_MEM_ALLOC -1
#define IFILE_CANN_OPEN -2
#define OFILE_CANN_OPEN -3


class CAnalizador
{
private:
fstream *IFile;
fstream *OFile;
char IFname[ MAX_PATH ];
char OFname[ MAX_PATH ];
char className[ 61 ];

short ex_code;

public:
CAnalizador();
CAnalizador( short ex_cod ) : ex_code( ex_cod ) {}
~CAnalizador();

char* SetFileName( const char* Fname, bool in_out_flag );
const char* GetFileName( bool in_out_flag );
int AnalizarConv();
short GetExCode();

const char* what() const throw();
};

#endif // fin de archivo de cabacera

#include "main.h"

// Funcion principal
int main( int argc, char** argv )
{
CAnalizador analiza;
char fname[ MAX_PATH ];

if( argc == 2 ) // si se recibieron 2 parametros, el segundo es el archivo a filtrar
strcpy( fname, argv[1] );

else // sino solicita el nombre de archivo
{
cout << "Nombre del archivo fuente de C:\n";
cin.sync();
cin.getline( fname, MAX_PATH );
}

try {
analiza.SetFileName( fname, I_FILE ); // nombre de archivo de entrada

strcat( fname, ".cpp" );

analiza.SetFileName( fname, O_FILE ); // nombre de archivo de salida

analiza.AnalizarConv();
}

catch( CAnalizador &exc )
{
cout << exc.what() << endl;
}

if( analiza.GetExCode() == 0 && argc != 2 )
cout << "Operacion exitosa!" << endl;

cin.sync();
cin.get();

return 0;
} // Fin de funcion principal

// constructo
CAnalizador::CAnalizador()
{
this-> IFile = new fstream;
this-> OFile = new fstream;
}
// destructor
CAnalizador::~CAnalizador()
{
delete this-> IFile;
delete this-> OFile;
}
// pone el nombre de archivos
char* CAnalizador::SetFileName( const char* Fname, bool in_out_flag )
{
if( in_out_flag )
return strcpy( this->IFname, Fname );
else
return strcpy( this->OFname, Fname );
}
// obtiene el nombre de archivo
const char* CAnalizador::GetFileName( bool in_out_flag )
{ if( in_out_flag ) return this->IFname; else return this->OFname; }

// devuelve la excepcion
const char* CAnalizador::what() const throw()
{
switch( this->ex_code )
{
case 0: return "Operacion exitosa!";
case IFILE_CANN_OPEN: return "No se puede abrir el archivo de entrada!";
case OFILE_CANN_OPEN: return "No se puede abrir el archivo de salida!";
case ERR_MEM_ALLOC: return "Error de asigancion de memoria!";
}
}
// funcion principal del filtro
int CAnalizador::AnalizarConv()
{
char buffer[ MAX_BUFFER ];
char car, pal[ 21 ];
int i, j, len;
bool comflag = false;

this-> IFile->open( this-> IFname, ios::in ); // abre rchivo de entrada

if( this-> IFile->fail() ) throw CAnalizador( IFILE_CANN_OPEN );

this-> OFile->open( this-> OFname, ios::out ); // abre archivo de salida

if( this-> OFile->fail() ) throw CAnalizador( OFILE_CANN_OPEN );

strcpy( this-> className, "class " ); // pone el nombre del archivo a la clase

i = strlen( this-> IFname )-1;

for(; (this-> IFname[ i ] != '\\' && this-> IFname[ i ] != '/') && i >= 0; i-- ) ;

i++;

for( j=0; this-> IFname[ i+j ]; j++ )
this-> className[ j+6 ] = this-> IFname[ i+j ];

this-> className[ j+6 ] = 0;

i = strlen( this-> className ) -1;

for( ; this-> className[ i ] != '.' && len >= 0; i-- ) ;

this-> className[ i ] = 0;

strcat( this-> className, " {\n\tpublic:\n" );
// escribe el nombre de la clase
this-> OFile->write( this-> className, strlen( this-> className ) );

cout << className;

while( !this-> IFile->eof() ) // analiza el archivo de entrada
{
this-> IFile->getline( buffer, MAX_BUFFER );
len = strlen( buffer );

if( comflag )
{
this-> OFile->write( "///", 3 );
cout << "///";
}

for( i=0; i < len; i++ )
{
if( buffer[ i ] == '/' && buffer[ i+1 ] == '*' )
{
this-> OFile-> put( buffer[ i ] );
cout << buffer[ i ];

i++;
buffer[ i ] = '/';

this-> OFile-> put( buffer[ i ] );
cout << buffer[ i ];

i++;
buffer[ i ] = '/';

comflag = true;
}

else if( buffer[ i ] == '*' && buffer[ i+1 ] == '/' )
{
i++;
comflag = false;
continue;
}

this-> OFile-> put( buffer[ i ] );
cout << buffer[ i ];
}

this-> OFile-> put( '\n' );
cout << endl;
}

this-> OFile->write( "};", 2 );

cout << "};";

this-> IFile->close();
this-> OFile->close();

this-> ex_code = OP_SUCCESS;

return 0;
} // fin de AnalizarConv()

inline short CAnalizador::GetExCode() { return this-> ex_code; }

// fin de codigo

No hay comentarios:

Publicar un comentario