/* * :set tabstop=4 * (c) SPDsoft 2006 spd _at_ daphne.cps.unizar.es * Wed May 24 13:54:34 CEST 2006 * Convert C++ comments to C */ #include #include #include #include #define PATH_SEP '/' #define IDENT "@(#) ccomment 0.0 (c) SPDsoft, 2006" #define Output_v if (G_prefs.verbose) fprintf #define Output if (!G_prefs.quiet) fprintf typedef struct { int quiet; int verbose; } pref; pref G_prefs; static char __ident[] = IDENT; #define VERS_STR ((char*)&__ident[4]) #define ZEROS 1024 int check( FILE *f ); void get_options(int argc,char **argv); void usage(char *name); int main(int argc, char **argv); void get_options(int argc,char **argv) { int option; G_prefs.verbose= 0; G_prefs.quiet= 1; while ( (option=getopt(argc,argv,"qvVh")) != EOF ) { switch(option) { case 'q': G_prefs.quiet = 0; break; case 'v': G_prefs.verbose = 1; break; case 'V': printf("%s\n",VERS_STR); break; default: usage(argv[0]); } } } void usage(char *name) { fprintf(stderr, "usage: %s [-z max][-q|-v][-V][-h] files...\n%s\n%s\n%s\n%s\n", name, "-q: not quiet", "-v: verbose", "-V: version", "-h: This text" ); exit(-1); } int main(int argc, char **argv) { char *app; int res=0; FILE *f; if ((app = strrchr(argv[0], PATH_SEP)) != NULL) app = app+1; else app = argv[0]; get_options(argc,argv); if (!argv[optind]) { Output(stderr,"(stdin):\t"); if ( 0 == check(stdin)) res++; } else while(argv[optind]) { Output(stderr,"%s:\t", argv[optind]); if (( f = fopen(argv[optind], "rb")) == NULL) { Output(stderr,"%s\n", sys_errlist[errno]); } else { if ( 0 == check(f)) res++; } fclose(f); optind ++; } return(!res); } int check( FILE *f ) { char c=0; int i; int slash=0; int ccomment=0; int ccommentstar=0; int cppcomment=0; int instring=0; while (EOF!=(i=fgetc(f))) { c=(char) (i & 0x00ff); switch(c) { case '"': if ( !ccomment && !cppcomment ) instring = !instring; putc(c,stdout); break; case '*': if (slash && !ccomment && !instring) { if ( cppcomment ) { /* can't create nested comment */ putc(' ',stdout); } else { putc(c,stdout); ccomment=1; } } else if ( ccomment && !instring) { ccommentstar=1; putc(c,stdout); } else putc(c,stdout); slash=0; break; case '/': if (slash && !instring) { slash=0; /* // */ if ((!ccomment)&&(!cppcomment)) { cppcomment=1; putc('*',stdout); } else { putc(c,stdout); } } else if ( ccommentstar && !instring) { ccommentstar=0; ccomment=0; slash=0; putc(c,stdout); } else { slash=1; putc(c,stdout); } break; case '\r': case '\n': if (cppcomment) { cppcomment=0; putc('*',stdout); putc('/',stdout); } default: if (slash) slash=0; if (ccommentstar) ccommentstar=0; putc(c,stdout); break; } } return (!ferror(f)); }