#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 3_16.c 3_16.cmp 3_16.in 3_16a.c 3_16b.c 3_16c.c 3_16d.c makefile # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 22:58:39 EDT 1990 # echo x - 3_16.c sed 's/^X//' > 3_16.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include #include "3_16b.c" /* dolinecomment */ #include "3_16c.c" /* doblockcomment */ #include "3_16d.c" /* do_string_or_char */ #include "3_16a.c" /* main */ !EOF! ls -l 3_16.c echo x - 3_16.cmp sed 's/^X//' > 3_16.cmp << '!EOF!' #include extern void error(char *); #include "3_16b.c" #include "3_16c.c" #include "3_16d.c" #include "3_16a.c" #include int main(int, char**) { char ch; while (cin.get(ch)) { if (ch == '/') { cin.get(ch); if (!cin) break; if (ch == '/') dolinecomment(); else if (ch == '*') doblockcomment(); else { cout.put('/'); cin.putback(ch); } } else { cout.put(ch); if (ch == '"' || ch == '\'') do_string_or_char(ch); } } return 0; } void dolinecomment() { char ch; while (cin.get(ch)) if (ch == '\n') { cout.put(ch); return; } error("EOF found within line comment"); } void doblockcomment() { cout.put(' '); char ch; while (cin.get(ch)) if (ch == '*') { cin.get(ch); if (ch == '/') return; cin.putback(ch); } else if (ch == '\n') cout.put(ch); error("EOF found within full comment"); } void do_string_or_char(char startquote) { char ch; while (cin.get(ch)) { cout.put(ch); if (ch == startquote) return; if (ch == '\\') { cin.get(ch); if (!cin) break; cout.put(ch); } } error("EOF found within string or char constant"); } !EOF! ls -l 3_16.cmp echo x - 3_16.in sed 's/^X//' > 3_16.in << '!EOF!' #include extern void error(char *); #include "3_16b.c" /* dolinecomment */ #include "3_16c.c" /* doblockcomment */ #include "3_16d.c" /* do_string_or_char */ #include "3_16a.c" /* main */ // Strip all C++ comments // from a source file read // on the standard input. #include int main(int, char**) { char ch; while (cin.get(ch)) { // a / may be followed by another / or a * if (ch == '/') { cin.get(ch); if (!cin) break; if (ch == '/') dolinecomment(); else if (ch == '*') doblockcomment(); // if not one of the above, then it's // not a comment and the 2nd character // must be rescanned else { cout.put('/'); cin.putback(ch); } } else // a printable character { cout.put(ch); if (ch == '"' || ch == '\'') do_string_or_char(ch); } } return 0; } /* handle a line comment */ void dolinecomment() { char ch; // loop until end of line while (cin.get(ch)) if (ch == '\n') { cout.put(ch); return; } error("EOF found within line comment"); } /* handle a block comment */ void doblockcomment() { cout.put(' '); char ch; // loop until matching */ while (cin.get(ch)) if (ch == '*') { cin.get(ch); if (ch == '/') return; cin.putback(ch); } else if (ch == '\n') cout.put(ch); error("EOF found within full comment"); } /* Copy a string or character constant. */ /* Startquote is the character, either */ /* ' or ", which started this constant. */ void do_string_or_char(char startquote) { char ch; while (cin.get(ch)) { // copy chars within string cout.put(ch); // found 2nd quote? if (ch == startquote) return; // found \x, skip past 1 char if (ch == '\\') { cin.get(ch); if (!cin) break; cout.put(ch); } } error("EOF found within string or char constant"); } !EOF! ls -l 3_16.in echo x - 3_16a.c sed 's/^X//' > 3_16a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Strip all C++ comments // from a source file read // on the standard input. #include int main(int, char**) { char ch; while (cin.get(ch)) { // a / may be followed by another / or a * if (ch == '/') { cin.get(ch); if (!cin) break; if (ch == '/') dolinecomment(); else if (ch == '*') doblockcomment(); // if not one of the above, then it's // not a comment and the 2nd character // must be rescanned else { cout.put('/'); cin.putback(ch); } } else // a printable character { cout.put(ch); if (ch == '"' || ch == '\'') do_string_or_char(ch); } } return 0; } !EOF! ls -l 3_16a.c echo x - 3_16b.c sed 's/^X//' > 3_16b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* handle a line comment */ void dolinecomment() { char ch; // loop until end of line while (cin.get(ch)) if (ch == '\n') { cout.put(ch); return; } error("EOF found within line comment"); } !EOF! ls -l 3_16b.c echo x - 3_16c.c sed 's/^X//' > 3_16c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* handle a block comment */ void doblockcomment() { cout.put(' '); char ch; // loop until matching */ while (cin.get(ch)) if (ch == '*') { cin.get(ch); if (!cin) break; if (ch == '/') return; cin.putback(ch); } else if (ch == '\n') cout.put(ch); error("EOF found within full comment"); } !EOF! ls -l 3_16c.c echo x - 3_16d.c sed 's/^X//' > 3_16d.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* Copy a string or character constant. */ /* Startquote is the character, either */ /* ' or ", which started this constant. */ void do_string_or_char(char startquote) { char ch; while (cin.get(ch)) { // copy chars within string cout.put(ch); // found 2nd quote? if (ch == startquote) return; // found \x, skip past 1 char if (ch == '\\') { cin.get(ch); if (!cin) break; cout.put(ch); } } error("EOF found within string or char constant"); } !EOF! ls -l 3_16d.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC ERROR= ../../error.o all: 3_16 3_16: 3_16a.c 3_16b.c 3_16c.c 3_16d.c $(CC) 3_16.c -o 3_16 $(ERROR) test: all 3_16.cmp 3_16 < 3_16.in > 3_16.out cmp 3_16.out 3_16.cmp echo tests done !EOF! ls -l makefile # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0