home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / snippets / remcmmnt.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  5KB  |  146 lines

  1. /*
  2.  * REMCMMNT.C
  3.  * Remove comments from C or C++ source
  4.  *
  5.  * ver 1.0, 05 Mar 1995
  6.  *
  7.  * Public domain by:
  8.  *   Jari Laaksonen
  9.  *   Arkkitehdinkatu 30 A 2
  10.  *   FIN-33720 Tampere
  11.  *   FINLAND
  12.  *
  13.  *   Fidonet : 2:221/360.20
  14.  *   Internet: jla@to.icl.fi
  15.  */
  16.  
  17. #include <stdio.h>
  18.  
  19. int main (int argc, char **argv)
  20. {
  21.       int   Char,
  22.             Char2,
  23.             cpp_comment   = 0,
  24.             c_comment     = 0,
  25.             in_string     = 0,
  26.             cpp_multiline = 0;
  27.       char CannotOpen[] = "Cannot open %s\n\n";
  28.       FILE *InFile, *OutFile = stdout;
  29.  
  30.       if (argc < 2)
  31.       {
  32.             fprintf (stderr, "USAGE: REMCMMNT InFile [OutFile]\n");
  33.             return 1;
  34.       }
  35.       if ((InFile = fopen (argv[1], "r")) == NULL)
  36.       {
  37.             fprintf (stderr, CannotOpen, argv[1]);
  38.             return 2;
  39.       }
  40.  
  41.       if (argc == 3)
  42.       {
  43.             if ((OutFile = fopen (argv[2], "w")) == NULL)
  44.             {
  45.                   fprintf (stderr, CannotOpen, argv[2]);
  46.                   /* if can't open, output goes to stdout instead */
  47.                   OutFile = stdout;
  48.             }
  49.       }
  50.  
  51.       while ((Char = fgetc (InFile)) != EOF)
  52.       {
  53.             if (Char == '\"')
  54.             {
  55.                   Char2 = fgetc (InFile);       /* check next char      */
  56.                   if (Char2 != '\'')            /* character constant?  */
  57.                         in_string = ! in_string;/* no, toggle flag      */
  58.  
  59.                   if (c_comment == 0 && cpp_comment == 0)
  60.                   {
  61.                         fputc (Char,  OutFile);
  62.                         fputc (Char2, OutFile);
  63.                         continue;
  64.                   }
  65.             }
  66.  
  67.             if (! in_string)
  68.             {
  69.                   if (Char == '/')
  70.                   {
  71.                         Char2 = fgetc (InFile); /* check next char      */
  72.                         if (Char2 == '/')       /* start of C++ comment?*/
  73.                               cpp_comment = 1;
  74.                         else if (Char2 == '*')  /* start of C comment?  */
  75.                               c_comment = 1;
  76.  
  77.                         if (c_comment == 0 && cpp_comment == 0)
  78.                         {
  79.                               fputc (Char,  OutFile);
  80.                               fputc (Char2, OutFile);
  81.                               continue;
  82.                         }
  83.                   }
  84.                   else if (Char == '*' && c_comment)
  85.                   {
  86.                         Char2 = fgetc (InFile);
  87.                         if (Char2 == '/')       /* end of C comment?    */
  88.                         {
  89.                               c_comment = 0;
  90.                               Char = fgetc (InFile);
  91.                         }
  92.                   }
  93.  
  94.                   if (c_comment || cpp_comment) /* inside C or C++ comment? */
  95.                   {
  96.                         /* print newline after comment line is processed    */
  97.                         Char = '\n';
  98.                         /* rest of the line */
  99.                         while ((Char2 = fgetc (InFile)) != '\n')
  100.                         {
  101.                               if (Char2 == '\\' && cpp_comment)
  102.                                     cpp_multiline = 1;
  103.  
  104.                               if (Char2 == '*' && c_comment)
  105.                               {
  106.                                     Char2 = fgetc (InFile); /* check next   */
  107.                                     if (Char2 == '/')     /* end C comment? */
  108.                                     {
  109.                                           c_comment = 0;
  110.                                           Char = fgetc (InFile);
  111.                                           break;
  112.                                     }
  113.                                     /* single splat inside C comment */
  114.                                     if (Char2 == '\n')
  115.                                           break;
  116.                               }
  117.                         }
  118.                         if (cpp_comment && cpp_multiline == 0)
  119.                               cpp_comment = 0;
  120.                         if (c_comment || cpp_comment)
  121.                               fputc (Char,  OutFile);
  122.  
  123.                         /*
  124.                         ** clear flag for the next round. if it is still
  125.                         ** clear after next C++ comment line is processed,
  126.                         ** multiline C++ comment is ended.
  127.                         */
  128.  
  129.                         cpp_multiline = 0;
  130.                   }
  131.             }
  132.             if (c_comment == 0 && cpp_comment == 0)
  133.                   fputc (Char,  OutFile);
  134.  
  135.       } /* while end */
  136.  
  137.       if (argc == 3)
  138.             fclose (OutFile);
  139.       fclose (InFile);
  140.  
  141.       fflush (stdout);
  142.       fprintf (stderr, "\nOK!\n");
  143.  
  144.       return 0;
  145. }
  146.