Friday, July 3, 2009

program to convert the C or C++ file to simple HTML file


/*******************************\
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
\*******************************/

/*program to convert the C or C++ file to simple HTML file*/

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 500
#define TRUE 1
#define FALSE 0
int valid = FALSE;

/*umang bhatt*/
FILE * source;
FILE * html;
char source_path[MAX];
char dst_path[MAX];
int main()
{
char c;
void getfile();
void getfname(char []);
char temp[MAX];
getfile();
getfname(temp);
if ( source != '\0' && html != '\0' )
{
fprintf( html, "%s", "<html>\n" );
fprintf( html, "%s", "<head>\n" );
fprintf( html, "%s", "<title>\n" );
fprintf( html, "%s", "Umang Bhatt - %s",temp);
fprintf( html, "%s", "</title>\n" );
fprintf( html, "%s", "</head>\n" );
fprintf( html, "%s", "<body>\n" );
fprintf( html, "%s", "<pre>\n" );
fprintf( html, "%s", "/*******************************\\ \n * Umang B Bhatt *\n * bhatt.umang7@gmail.com *\n\\*******************************/\n" );
do
{
c = getc( source );
if ( c == EOF )
{
break;
}
else if ( c == '<' )
{
fprintf( html, "%s", "&lt;" );
}
else if ( c == '>' )
{
fprintf( html, "%s", "&gt;" );
}
else if ( c == '&' )
{
fprintf( html, "%s", "&amp;" );
}
else
{
fprintf( html, "%c", c );
}
}
while ( c != EOF );
fprintf( html, "%s", "</pre>" );
fprintf( html, "%s", "</body>" );
fprintf( html, "%s", "</html>" );
fclose( html );
fclose( source );
printf("\nThe file is now created");
}
return 0;
}



void getfile()
{

char c;
void convert( char []);
printf("Press Q at any place to terminate the program");
valid = FALSE;
do
{
printf( "\nEnter path of source code: " );
gets( source_path );
convert( source_path );
source = fopen( source_path, "r" );
if ( strcmp( source_path, "q" ) == 0 || strcmp( source_path, "Q" ) == 0 )
{
exit( 0 );
}
if ( source != '\0' )
{
valid = TRUE;
}
else
{
printf( "\nYou have not entered a valid path\n%s\n", source_path);
}
}
while ( valid == FALSE );

valid = FALSE;
do
{
printf( "Enter path of source code: " );
gets( dst_path );
convert( dst_path );
html = fopen( dst_path, "r" );
if ( strcmp( dst_path, "q" ) == 0 || strcmp( dst_path, "Q" ) == 0 )
{
exit( 0 );
}
if ( html != '\0' )
{
fclose( html );
do
{
printf( "\nThe file already exist. Do you want to overwrite? y / n" );
c = getch();
if ( c == 'y' || c == 'Y' )
{
fopen( dst_path, "w" );
break;
}
else if ( c == 'N' || c == 'n' )
{
printf("\nExitting the program.");
exit(0);
}
}
while ( c != 'Y' && c != 'y' && c != 'n' && c != 'N' );

}
if ( html != '\0' )
{
valid = TRUE;
}
else
{

html=fopen( dst_path, "w" );
break;
}
}
while ( valid == FALSE );
}
void convert( char a[] )
{
int i, j;
if ( a[1] != ':' )
{
printf( "invalid address" );
}
for ( i = 0;a[i] != '\0';i++ )
{
if ( a[i] == '\\' )
{
for ( j = strlen( a );j > i;j-- )
{
a[j+1] = a[j];
}
a[++i] = '\\';
i++;
}
}
}



void getfname(char temp[MAX])
{
int i;
int j=0;
for(i=strlen(source_path);source_path[i]!='\\';i--)
{
temp[j]=source_path[i];
j++;
}
temp[j]='\0';
strrev(temp);
}