home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / GNUFIX.ZIP / UNCOMM.C < prev    next >
C/C++ Source or Header  |  1992-08-03  |  4KB  |  178 lines

  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include "getopt.h"
  7. #include "xstring.h"
  8.  
  9. static int  statement_count = 0;
  10. static int  block_count = 0;
  11.  
  12. static char *infilename;
  13. static char *outfilename;
  14.  
  15. static FILE *infile;
  16. static FILE *outfile;
  17.  
  18. void
  19. waste_cplus_comment( FILE *input, FILE *output )
  20. {
  21.    char c;
  22.  
  23.    while ( !feof(input) ) {
  24.       c = fgetc(input);
  25.       if ( c == '\n' ) {
  26.          fputc(c,output);
  27.          return;
  28.          }
  29.       }
  30. }
  31.  
  32. void
  33. waste_c_comment( FILE *input, FILE *output )
  34. {
  35.    int   exit_flag = FALSE;
  36.    char  c;
  37.  
  38.    while ( !feof(input) ) {
  39.       c = fgetc(input);
  40.       switch ( c ) {
  41.          case '*' :
  42.             exit_flag = TRUE;
  43.             break;
  44.  
  45.          case '/' :
  46.             if ( exit_flag ) {
  47.                return;
  48.                }
  49.             break;
  50.          default:
  51.             exit_flag = FALSE;
  52.          } 
  53.       }
  54.  
  55. }
  56.  
  57. void
  58. filter( FILE *input, FILE *output )
  59. {
  60.    int  comment_entry_flag = FALSE;
  61.    char c;
  62.    char holder;
  63.    char lastone = 0;
  64.    int  compress = FALSE;
  65.  
  66.    while ( !feof(input) ) {
  67.       c = fgetc(input);
  68.       switch ( c ) {
  69.          case '*' :
  70.                
  71.             if ( comment_entry_flag ) {
  72.                waste_c_comment(input,output);
  73.                comment_entry_flag = FALSE;
  74.                continue;
  75.                }
  76.             comment_entry_flag = FALSE;
  77.             break;
  78.  
  79.          case '/' :
  80.                
  81.             if ( comment_entry_flag ) {
  82.                waste_cplus_comment(input,output);
  83.                comment_entry_flag = FALSE;
  84.                continue;
  85.                break;
  86.                }
  87.  
  88.             comment_entry_flag = TRUE;
  89.             holder = c;
  90.             continue;
  91.             break;
  92.  
  93.          default :
  94.             if ( comment_entry_flag ) {
  95.                fputc(holder,output);
  96.                comment_entry_flag = FALSE;
  97.                }
  98.             if ( c == '}' )
  99.                block_count++;
  100.             if ( c == ';' ) {
  101.                statement_count++;
  102.                }
  103.          } 
  104.  
  105.       if ( lastone == '\n' && c == '\n' && compress )
  106.          continue;
  107.  
  108.       fputc(c,output);
  109.  
  110.       if ( lastone == '\n' && c == '\n' )
  111.          compress = TRUE;
  112.       else
  113.          compress = FALSE;
  114.  
  115.       lastone = c;
  116.  
  117.       } 
  118.  
  119. }
  120.  
  121. main(int argc, char *argv[], char *envp[])
  122. {
  123.    char c;
  124.    char *ptroption;
  125.  
  126.    infilename = NULL;
  127.    outfilename = NULL;
  128.  
  129.    while ( (c = getopt(argc,argv,"i:o:",&ptroption)) != EOF) {
  130.       switch (c) {
  131.          case 'i' :
  132.         infilename = ptroption;
  133.             break;
  134.      case 'o' :
  135.         outfilename = ptroption;
  136.         break;
  137.          default :
  138.             return 2;
  139.       }
  140.    }
  141.  
  142.    if ( infilename == NULL ) {
  143.       printf("specify input file\n");
  144.       printf("uncomm -i filename -o filename\n");
  145.       exit(1);
  146.       }
  147.  
  148.    if ( outfilename == NULL ) {
  149.       printf("specify output file\n");
  150.       printf("uncomm -i filename -o filename\n");
  151.       exit(2);
  152.       }
  153.  
  154.    infile = fopen(infilename,"r");
  155.    if ( infile == NULL ) {
  156.       printf("can't find input file %s\n",infilename);
  157.       exit(3);
  158.       }
  159.  
  160.    outfile = fopen(outfilename, "w");
  161.    if ( outfile == NULL ) {
  162.       printf("can't create output file %s\n",outfilename);
  163.       exit(4);
  164.       }
  165.  
  166.    printf("uncommenting: %s\n",infilename);
  167.  
  168.    filter(infile,outfile);
  169.  
  170.    printf("%5d blocks\n",block_count);
  171.    printf("%5d statements\n",statement_count);
  172.  
  173.    fflush(outfile);
  174.    fclose(infile);
  175.    fclose(outfile);
  176. }
  177.  
  178.