/* cs-opt2.c - Don Yang (uguu.org) Compress whitespaces and remove comments. Uses alternative DFA from cs-opt1*, more states but simpler code. 04/29/05 */ #include static void Filter(FILE *infile, FILE *outfile) { /* 0 123 456 70 1 */ char *input = "\n \"/'\\*\r\t"; char *dfa = "+++3@K+ 11111111IIIIIIII////h//8 pXx)1\0I))111)11\t1" "8888888PX@@@@@@@IIIII)\21I8888X88PXX-5\30M--\253\253\253\253@" "\253\253 Xhhhhhhh x pXx\251\261`\311\251\251"; char *state; int i, j, k; for(state = dfa + 40; (i = fgetc(infile)) != EOF; state = dfa + (k & 0x78)) { for(j = 0; input[j] != '\0' && (int)input[j] != i; j++); k = (int)*(state + (j & 7)); if( (k & 0x80) != 0 ) (void)fputc(' ', outfile); if( (k & 4) != 0 ) (void)fputc('\n', outfile); if( (k & 2) != 0 ) (void)fputc('/', outfile); if( (k & 1) != 0 ) (void)fputc(i, outfile); } (void)fputc('\n', outfile); } int main(int argc, char **argv) { FILE *infile, *outfile; infile = stdin; outfile = stdout; if( argc > 1 ) { if( (infile = fopen(argv[1], "rb")) == NULL ) return printf("Can not open %s\n", argv[1]); if( argc > 2 ) { if( (outfile = fopen(argv[2], "wb+")) == NULL ) { (void)fclose(infile); return printf("Can not write to %s\n", argv[2]); } } } Filter(infile, outfile); (void)fclose(infile); (void)fclose(outfile); return 0; }