home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / COMMCONV.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  178 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4.  * COMMCONV.C
  5.  * Change C++ comments to C comments
  6.  *
  7.  * ver 2.2, 14 Jun 1995
  8.  *   - changed multiline C++ comment handling.
  9.  *
  10.  * ver 2.3, 8 Nov 1995
  11.  *   - bug: if file was ended with // comment with no CR/LF,
  12.  *          program jammed in an endless loop.
  13.  *
  14.  * ver 2.4, 21 Nov 1995
  15.  *   - bug: did not handle escape character \" inside strings.
  16.  *
  17.  * Public domain by:
  18.  *   Jari Laaksonen
  19.  *   Arkkitehdinkatu 30 A 2
  20.  *   FIN-33720 Tampere
  21.  *   FINLAND
  22.  *
  23.  *   Fidonet : 2:221/360.20
  24.  *   Internet: jla@to.icl.fi
  25.  */
  26.  
  27. #include <stdio.h>
  28.  
  29. int main (int argc, char **argv)
  30. {
  31.   int  Char,
  32.        Char2,
  33.        got_endl      = 0,
  34.        cpp_comment   = 0,
  35.        c_comment     = 0,
  36.        in_string     = 0,
  37.        cpp_multiline = 0;
  38.   char CannotOpen[] = "Cannot open %s\n\n";
  39.   FILE *InFile, *OutFile = stdout;
  40.  
  41.   if (argc < 2)
  42.   {
  43.     fprintf (stderr, "USAGE: COMMCONV InFile [OutFile]\n");
  44.     return 1;
  45.   }
  46.   if ((InFile = fopen (argv[1], "r")) == NULL)
  47.   {
  48.     fprintf (stderr, CannotOpen, argv[1]);
  49.     return 2;
  50.   }
  51.  
  52.   if (argc == 3)
  53.   {
  54.     if ((OutFile = fopen (argv[2], "w")) == NULL)
  55.     {
  56.       fprintf (stderr, CannotOpen, argv[2]);
  57.       OutFile = stdout;  /* if can't open, output goes to stdout instead */
  58.     }
  59.   }
  60.  
  61.   while ((Char = fgetc (InFile)) != EOF)
  62.   {
  63.     fputc (Char, OutFile);
  64.  
  65.     if (in_string && Char == '\\')
  66.     {
  67.       Char2 = fgetc (InFile);
  68.       fputc (Char2, OutFile);
  69.  
  70.       /* do we have an excape character \" inside string ? */
  71.       if (Char2 == '\"')
  72.         continue;
  73.     }
  74.  
  75.     if (Char == '\"')
  76.     {
  77.       Char2 = fgetc (InFile);            /* check next char */
  78.  
  79.       /* do we have a character constant '\"' or an empty string "" ? */
  80.       if (Char2 != '\'' && Char2 != '\"')
  81.         in_string = ! in_string;         /* if not, we are in a string */
  82.       fputc (Char2, OutFile);
  83.     }
  84.  
  85.     if (in_string)                       /* we are in a string now */
  86.       continue;
  87.  
  88.     if (Char == '/')
  89.     {
  90.       Char = fgetc (InFile);             /* check next char */
  91.       if (Char == '/')                   /* is it start of C++ comment? */
  92.       {
  93.         Char = '*';                      /* change it to C comment */
  94.         cpp_comment = 1;
  95.       }
  96.       else if (Char == '*')              /* is it start of C comment? */
  97.         c_comment = 1;
  98.  
  99.       fputc (Char, OutFile);
  100.  
  101.     }
  102.     else if (Char == '*' && c_comment)
  103.     {
  104.       Char = fgetc (InFile);
  105.       if (Char == '/')                   /* is it end of C comment? */
  106.         c_comment = 0;
  107.       fputc (Char, OutFile);
  108.     }
  109.  
  110.     if (c_comment || cpp_comment)        /* are we inside C or C++ comment? */
  111.     {
  112.       got_endl = 1;
  113.       while ((Char = fgetc (InFile)) != '\n') /* check the rest of the line */
  114.       {
  115.         if (Char == EOF)
  116.           break;
  117.  
  118.         if (cpp_multiline)
  119.         {
  120.           if (Char != ' ' && Char != '\t') /* if not white space => */
  121.             cpp_multiline = 0;             /* ...not C++ multiline comment */
  122.         }
  123.  
  124.         if (Char == '\\' && cpp_comment)
  125.           cpp_multiline = 1;
  126.  
  127.         if (Char == '*')
  128.         {
  129.           Char2 = fgetc (InFile);        /* check next char */
  130.           ungetc (Char2, InFile);        /* put it back to stream */
  131.  
  132.           if (Char2 == '/')              /* is it end of C comment */
  133.           {
  134.             c_comment = 0;
  135.             /* is it end of C comment inside C++ comment */
  136.             if (cpp_comment)
  137.             {
  138.               fputs ("* ", OutFile);
  139.               Char = fgetc (InFile);
  140.             }
  141.           }
  142.         }
  143.         fputc (Char, OutFile);
  144.         if (c_comment == 0 && cpp_comment == 0)
  145.         {
  146.           got_endl = 0;
  147.           break;          /* break if end of C comment found */
  148.         }
  149.       } /* while */
  150.  
  151.       if (cpp_comment && cpp_multiline == 0)
  152.       {
  153.         fputs (" */", OutFile);          /* put ending C comment mark */
  154.         cpp_comment = 0;
  155.       }
  156.  
  157.       /* print endl if we processed whole line */
  158.       if (got_endl)
  159.         fputc ('\n', OutFile);
  160.  
  161.       /* clear flag for the next round. if it is still clear after
  162.          next C++ comment line is processed, multiline C++ comment
  163.          is ended.
  164.       */
  165.       cpp_multiline = 0;
  166.     }
  167.   } /* while end */
  168.  
  169.   if (argc == 3)
  170.     fclose (OutFile);
  171.   fclose (InFile);
  172.  
  173.   fflush (stdout);
  174.   fprintf (stderr, "\nOK!\n");
  175.  
  176.   return 0;
  177. }
  178.