david@ukma.UUCP (David Herron, NPR Lover) (11/23/85)
Everybody I've shown this program to has *groaned* and complained about what a nasssty program it was... This program was written to help decode a bitnet routing table that I had been netcopy'd to me and didn't get translated into ascii. So after running dd over it, the line markers had disappeared into never never land. But from looking real closely at the file I could see that each line was supposed to start with ROUTE..... thus this program: I think it's *cute*! #include <stdio.h> main() { register int r, o, u, t, e; while ((r = getchar()) != EOF) { if (r == 'R') if ((o = getchar()) == 'O') if ((u = getchar()) == 'U') if ((t = getchar()) == 'T') if ((e = getchar()) == 'E') printf("\nROUTE"); else printf("ROUT%c", e); else printf("ROU%c", t); else printf("RO%c", u); else printf("R%c", o); else putchar(r); } putchar('\n'); } -- David Herron, cbosgd!ukma!david, david@UKMA.BITNET. Experience is something you don't get until just after you need it.
dimitrov@csd2.UUCP (Isaac Dimitrovsky) (11/24/85)
/* csd2:net.lang.c / david@ukma.UUCP (David Herron, NPR Lover) / 5:42 pm Nov 22, 1985 */ Everybody I've shown this program to has *groaned* and complained about what a nasssty program it was... This program was written to help decode a bitnet routing table that I had been netcopy'd to me and didn't get translated into ascii. So after running dd over it, the line markers had disappeared into never never land. But from looking real closely at the file I could see that each line was supposed to start with ROUTE..... thus this program: I think it's *cute*! #include <stdio.h> main() { register int r, o, u, t, e; while ((r = getchar()) != EOF) { if (r == 'R') if ((o = getchar()) == 'O') if ((u = getchar()) == 'U') if ((t = getchar()) == 'T') if ((e = getchar()) == 'E') printf("\nROUTE"); else printf("ROUT%c", e); else printf("ROU%c", t); else printf("RO%c", u); else printf("R%c", o); else putchar(r); } putchar('\n'); } -- David Herron, cbosgd!ukma!david, david@UKMA.BITNET. Experience is something you don't get until just after you need it. /* ---------- */
mikel@codas.UUCP (Mikel Manitius) (12/03/85)
> david@ukma.UUCP (David Herron, NPR Lover) / 5:42 pm Nov 22, 1985 */ > Everybody I've shown this program to has *groaned* and complained > about what a nasssty program it was... > > I think it's *cute*! > > [ ... some nasty C code ... ] > G R O A N !! -- Mikel Manitius @ AT&T-IS Altamonte Springs, FL ...{ihnp4|akguc|attmail|indra!koura}!codas!mikel
david@ukma.UUCP (David Herron, NPR Lover) (12/04/85)
In article <363@codas.UUCP> mikel@codas.UUCP (Mikel Manitius) writes: >> david@ukma.UUCP (David Herron, NPR Lover) / 5:42 pm Nov 22, 1985 */ >> Everybody I've shown this program to has *groaned* and complained >> about what a nasssty program it was... >> >> I think it's *cute*! >> >> [ ... some nasty C code ... ] >> > > G R O A N !! Aw cmon!!! I wanted to know *WHY* people were groaning!!! So far the only reasonable complaint I've seen is that it won't work in the general case (though it did work in the specific). And the best solution I've seen is this Lex program: >From cbosgd!seismo!ut-sally!shell!graffiti!peter Tue Nov 26 04:55:18 1985 >%% >ROUTE { printf("\nROUTE"); } >. { printf("%s", yytext); } Which eventually led me to think that the my program itself could be fixed using ungetc() at appropriate places. Or is the problem just religious differences? -- David Herron, cbosgd!ukma!david, david@UKMA.BITNET. Experience is something you don't get until just after you need it.
minow@decvax.UUCP (Martin Minow) (12/06/85)
I posted a "better" version a week ago, but got no flames, so it might have disappeared. I don't like the program because it (1) doesn't work, as noted already: fooRROUTEbar. (2) doesn't separate form from function: I can't see how to adapt the program for some other keyword. (Do I change the variable names, for example?) (3) doesn't present the algorithm -- such as it is -- clearly. (4) (3) makes fixing (1) harder. My (somewhat more efficient) version is appended. Martin Minow decvax!minow /* * This program reads an arbitrary file, inserting a newline * before every occurance of the string "ROUTE". It is * written so as to easily generalize to other strings. * Properly resetting state where bytes in string reoccur * is left as an exercise for the reader. */ #include <stdio.h> char *string = "ROUTE"; /* May be varied */ main() { register int c; register char *state; state = string; do { if ((c = getchar()) == *state) { if (*++state == '\0') { /* Whole string? */ printf("\n%s", string); /* Found */ state = string; } } else { if (state > string) { /* Partial match? */ printf("%.*s", state - string, string); state = string; /* Reset state pointer */ if (c == *state) { /* handle fooRROUTEbar */ ++state; /* Aha! saw first byte */ continue; /* Continue scanning */ } } if (c != EOF) /* Output non-matcher */ putchar(c); } } while (c != EOF); }
ado@elsie.UUCP (Arthur David Olson) (12/08/85)
: Some things were not meant to be programmed in C, and this is one of them. : Followups to this article are being directed to net.unix. : N.B. no provision has been made here for occurrences of a slash in word. case $# in 0|1) echo "`basename $0`: usage is `basename $0` word file ..." 1>&2 exit 1 ;; esac word="$1" shift case $#$1 in 1-) exec sed "/$word/s//\\ &/g" ;; *) exec sed "/$word/s//\\ &/g" "$@" ;; esac -- UNIX is an AT&T Bell Laboratories trademark. Word is a Microsoft trademark. -- UUCP: ..decvax!seismo!elsie!ado ARPA: elsie!ado@seismo.ARPA DEC, VAX and Elsie are Digital Equipment and Borden trademarks
darin@ut-dillo.UUCP (Darin Adler) (12/08/85)
< My apologies for following this up in net.lang.c > The shell/sed solution, is obviously an appropriate way to solve this problem, but let's not hide the simplicity of the solution while showing our shell script cleverness. The core of the sed solution is this: sed '/ROUTE/s//\\ &/g' or even: sed '/ROUTE/s//\\ ROUTE/g' Obviously, C programming is not the way to go here (unless sed is too slow?). -- Darin Adler {gatech,harvard,ihnp4,seismo}!ut-sally!ut-dillo!darin