Feel free to copy/edit/send to your friends/send to the president of the united states/send to bill gates:
Code:
#include <stdio.h>
#include <stdlib.h>
// Create bool datatype
typedef enum
{
false,
true
} bool;
int main( int argc, char **argv )
{
// Make sure the input has 3 arguments
if( argc != 3 )
{
puts("tabtospace [input] [output]");
return 1;
}
// i = The input file that we'll be reading from
// o = The output file that we'll be writing to
// c = temp character for reading/writing to files
FILE *i;
FILE *o;
int c;
// ccm = C Comment under production!
// cpcm = C++ Comment under production!
// sl = Within string literal
bool ccm = false;
bool cpcm = false;
bool sl = false;
// Open the input file for reading, make sure it's open
if( !( i=fopen( argv[1], "r" ) ) )
{
puts("Could not open input file");
return 1;
}
// Create/open the output file for writing
if( !( o=fopen( argv[2], "w+" ) ) )
{
puts( "Could not open output file" );
return 1;
}
// Continue to move chars from input->output
while( ( c=fgetc( i ) ) != EOF )
{
// If you see a \t, output " " instead
if( c == '\t' )
{
fputs( " ", o );
continue;
}
// Are we entering/leaving a string literal?
if( c == '\"' && sl == false )
sl = true;
else if( c == '\"' && sl == true )
sl = false;
// A comment in production perhaps?
if( c == '/' && ccm == false && cpcm == false && sl == false )
{
// Get the next character
c = fgetc( i );
// What type of comment?
// 1. C++ Comment
// 2. C Comment
// 3. No Comment
if( c == '/' )
{
ccm = true;
fputs( "[color=blue ]//", o ); CHANGE THIS STRING
continue;
}
else if( c == '*' )
{
cpcm = true;
fputs( "[color=blue ]/*", o ); CHANGE THIS STRING
continue;
}
else
{
fputc( '/', o );
fputc( c, o );
continue;
}
}
// C Comment ending?
if( c == '\n' && ccm == true )
{
ccm = false;
fputs( "[/color]", o );
fputc( c, o );
continue;
}
// C++ Comment ending?
if( c == '*' && cpcm == true )
{
fputc( c, o );
c = fgetc( i );
fputc( c, o );
if( c == '/' )
{
cpcm = false;
fputs( "[/color]", o );
}
continue;
}
fputc( c, o );
}
// Close file pointers
fclose( i );
fclose( o );
// Return successfully
return 0;
}