home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 22 gnu
/
22-gnu.zip
/
GNUFIX.ZIP
/
UNCOMM.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-03
|
4KB
|
178 lines
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
#include "xstring.h"
static int statement_count = 0;
static int block_count = 0;
static char *infilename;
static char *outfilename;
static FILE *infile;
static FILE *outfile;
void
waste_cplus_comment( FILE *input, FILE *output )
{
char c;
while ( !feof(input) ) {
c = fgetc(input);
if ( c == '\n' ) {
fputc(c,output);
return;
}
}
}
void
waste_c_comment( FILE *input, FILE *output )
{
int exit_flag = FALSE;
char c;
while ( !feof(input) ) {
c = fgetc(input);
switch ( c ) {
case '*' :
exit_flag = TRUE;
break;
case '/' :
if ( exit_flag ) {
return;
}
break;
default:
exit_flag = FALSE;
}
}
}
void
filter( FILE *input, FILE *output )
{
int comment_entry_flag = FALSE;
char c;
char holder;
char lastone = 0;
int compress = FALSE;
while ( !feof(input) ) {
c = fgetc(input);
switch ( c ) {
case '*' :
if ( comment_entry_flag ) {
waste_c_comment(input,output);
comment_entry_flag = FALSE;
continue;
}
comment_entry_flag = FALSE;
break;
case '/' :
if ( comment_entry_flag ) {
waste_cplus_comment(input,output);
comment_entry_flag = FALSE;
continue;
break;
}
comment_entry_flag = TRUE;
holder = c;
continue;
break;
default :
if ( comment_entry_flag ) {
fputc(holder,output);
comment_entry_flag = FALSE;
}
if ( c == '}' )
block_count++;
if ( c == ';' ) {
statement_count++;
}
}
if ( lastone == '\n' && c == '\n' && compress )
continue;
fputc(c,output);
if ( lastone == '\n' && c == '\n' )
compress = TRUE;
else
compress = FALSE;
lastone = c;
}
}
main(int argc, char *argv[], char *envp[])
{
char c;
char *ptroption;
infilename = NULL;
outfilename = NULL;
while ( (c = getopt(argc,argv,"i:o:",&ptroption)) != EOF) {
switch (c) {
case 'i' :
infilename = ptroption;
break;
case 'o' :
outfilename = ptroption;
break;
default :
return 2;
}
}
if ( infilename == NULL ) {
printf("specify input file\n");
printf("uncomm -i filename -o filename\n");
exit(1);
}
if ( outfilename == NULL ) {
printf("specify output file\n");
printf("uncomm -i filename -o filename\n");
exit(2);
}
infile = fopen(infilename,"r");
if ( infile == NULL ) {
printf("can't find input file %s\n",infilename);
exit(3);
}
outfile = fopen(outfilename, "w");
if ( outfile == NULL ) {
printf("can't create output file %s\n",outfilename);
exit(4);
}
printf("uncommenting: %s\n",infilename);
filter(infile,outfile);
printf("%5d blocks\n",block_count);
printf("%5d statements\n",statement_count);
fflush(outfile);
fclose(infile);
fclose(outfile);
}