Thread: Tab->Space converter and comment colorizer

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    Tab->Space converter and comment colorizer

    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;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    http://www.codeguru.com/Cpp/misc/sam...icle.php/c4693

    I think somebody already modified the forum's code to convert tabs to spaces...

    edit:
    Quote Originally Posted by webmaster
    I modified the board code to replace tabs with four spaces.
    Last edited by major_small; 01-30-2005 at 02:05 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Wow, what language is that written in?

    It can't be C89 standard C, because of all those // comments, and the position of FILE *i; is after statements.
    It can't be C99 standard C, because in C99 bool is a reserved name (as are true and false).
    It can't be C++ either, because then you would have #include <cstdio> etc.

    I'm sure
    gcc --shrug prog.c
    compiles it OK, but
    gcc -W -Wall -ansi -pedantic -O2 prog.c
    doesn't.

    Shall I stop now, or do you want some of the bugs pointing out as well?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Salem
    It can't be C99 standard C, because in C99 bool is a reserved name (as are true and false).
    I thought C99 just had _Bool, not bool.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    For writing something like this, you really ought to consider Perl. C/C++ just isn't as nice when it comes to dealing with strings (even if you bring in Reg Exp libraries, the syntax is just as cryptic, and less elegant than Perl).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    It can't be C89 standard C, because of all those // comments...
    so shall we only use "/* */" comments in our C programs?

    just wondering
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Only if you are programming C89 and not C99.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    So what is the current standard with how and where to comment?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    C99 is the current one (1999 as opposed to 1989 ) and to my knowledge, // and /* */ both work.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed