Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!oddjob!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Unstylish #defines ?? Message-ID: <8779@mimsy.UUCP> Date: Sat, 26-Sep-87 01:18:03 EDT Article-I.D.: mimsy.8779 Posted: Sat Sep 26 01:18:03 1987 Date-Received: Sun, 27-Sep-87 08:01:34 EDT References: <325@petro.UUCP> <1237@haddock.ISC.COM> Distribution: na Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 89 Keywords: quotes, files In article <1237@haddock.ISC.COM> karl@haddock.ISC.COM (Karl Heuer) writes: >... In pre-ANSI [C], the best you can do is paste the two pieces >together at run-time (with strcat or sprintf), or #define each such >path separately ... but in ANSI C string constants can be pasted at >compile-time: > #define FOO "/usr/me" > stream = fopen(FOO "/afile", "r") Or use the following program, e.g., `cc -E foo.c | string > x.c && cc -c x.c && mv x.o foo.o'. I wrote the original version of string on 30 October 1982. This one has been cleaned up a bit. (This can be done as a tiny lex program, but until Van sends out his new lex, we get slow scanners.) static char rcsid[] = "$Header: string.c,v 1.2 87/09/26 01:10:41 chris Exp $"; /* string -- combine adjacent C strings */ #include #define SQUOTE '\'' #define DQUOTE '"' #define BACKSLASH '\\' /*ARGSUSED*/ main(argc, argv) int argc; char **argv; { register int c; register char *p, *q; char buf[BUFSIZ]; /* process charcters, handling 'x' and "string" "s" */ while ((c = getchar()) != EOF) { switch (c) { case SQUOTE: /* char const */ (void) putchar(c); while ((c = getchar()) != EOF) { (void) putchar(c); if (c == BACKSLASH) { if ((c = getchar()) != EOF) (void) putchar(c); } else if (c == SQUOTE) break; } break; case DQUOTE: /* "string" */ (void) putchar(DQUOTE); more: while ((c = getchar()) != EOF) { if (c == BACKSLASH) { (void) putchar(c); if ((c = getchar()) != EOF) (void) putchar(c); } else if (c == DQUOTE) break; else (void) putchar(c); } /* found the close quote, but might be another str */ p = buf; while ((c = getchar()) == ' ' || c == '\t' || c == '\n') if (p < &buf[sizeof buf]) *p++ = c; if (c == DQUOTE) /* another string */ goto more; if (c != EOF) { /* close previous string and rescan later */ (void) putchar(DQUOTE); ungetc(c, stdin); } /* write buffered whitespace */ for (q = buf; q < p;) (void) putchar(*q++); break; default: /* anything else copied literally */ (void) putchar(c); } } exit(0); } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris