#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 8_8_cons.c 8_8_des.c 8_8_op.c 8_8_stat.c 8_8a1.h 8_8a2.h main.c makefile tst.cmp tst.data tst.in # # 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 23:13:47 EDT 1990 # echo x - 8_8_cons.c sed 's/^X//' > 8_8_cons.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ randomchars::randomchars(char *file) { fp = fopen(file, "r"); curoffset = 0; } !EOF! ls -l 8_8_cons.c echo x - 8_8_des.c sed 's/^X//' > 8_8_des.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ randomchars::~randomchars() { if (fp) (void) fclose(fp); } !EOF! ls -l 8_8_des.c echo x - 8_8_op.c sed 's/^X//' > 8_8_op.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ int randomchars::operator[](long offset) { if (offset != curoffset) if (fseek(fp, offset, 0)) return -1; int c = getc(fp); if (c == EOF) { curoffset = offset; return -1; } curoffset = offset + 1; return c; } !EOF! ls -l 8_8_op.c echo x - 8_8_stat.c sed 's/^X//' > 8_8_stat.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ int randomchars::openedokay() { return fp != 0; } !EOF! ls -l 8_8_stat.c echo x - 8_8a1.h sed 's/^X//' > 8_8a1.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Exercise 8.8 // Random Access reading from a file #ifndef RC_H # define RC_H #include // DELETE class randomchars { #include "8_8a2.h" // DELETE public: randomchars(char *filename); ~randomchars(); int openedokay(); int operator[](long offset); }; #endif /* RC_H */ !EOF! ls -l 8_8a1.h echo x - 8_8a2.h sed 's/^X//' > 8_8a2.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ FILE *fp; long curoffset; !EOF! ls -l 8_8a2.h echo x - main.c sed 's/^X//' > main.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "8_8a1.h" #include "8_8_cons.c" #include "8_8_stat.c" #include "8_8_des.c" #include "8_8_op.c" #include #include main() { randomchars r("tst.data"); while (cin) { cout << "\nIndex: "; long i = 0; cin >> i; int c = r[i]; cout << "i=" << i << "\n"; cout << "c=" << c << ", " << oct(c); if (isprint(c)) cout << ", " << chr(c); cout << "\n"; } return 0; } !EOF! ls -l main.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC CFLAGS= all: tst tst: main.o $(CC) main.o -o tst main.o: 8_8a1.h 8_8a2.h 8_8_cons.c 8_8_des.c 8_8_op.c 8_8_stat.c main.c $(CC) $(CFLAGS) -c main.c CMP= tst.cmp OUT= tst.out tst.out: tst tst.in tst.data ; tst < tst.in > tst.out test: $(CMP) $(OUT) cmp tst.out tst.cmp @echo test done !EOF! ls -l makefile echo x - tst.cmp sed 's/^X//' > tst.cmp << '!EOF!' Index: i=1 c=111, 157, o Index: i=0 c=114, 162, r Index: i=20 c=49, 61, 1 Index: i=-1 c=-1, 37777777777 Index: i=1500 c=-1, 37777777777 Index: i=2 c=111, 157, o Index: i=0 c=114, 162, r !EOF! ls -l tst.cmp echo x - tst.data sed 's/^X//' > tst.data << '!EOF!' root::0:root other::1: bin::2:root,bin,daemon sys::3:root,bin,sys,adm adm::4:root,adm,daemon uucp::5:root,uucp mail::6:root daemon::12:root,daemon docx::21:pcor,russak,mds,dsl,jeremy,bwp,hansen,coleman,chu,jack guest::22: cmts::23:cmts,hansen,mzal mesa::24:mesa,hansen,mzal mrstat::26:cmts,hansen,mzal,bwp,danb,guidi,linda,dsl,eby,cbp,hlb,pcor,russak,clt,mds,aku,wpw,jack,cjs,bcc,paul,alf,sld,drr,jim,larry,jmt intest::27: arch::29:efh,maryrose,danb,russak,linda ots::31:butterly,fwl,daved,so,dwr,shorty,mjk,mjm,aj dbx::35: erm::37:efs,caf,jaa,mjb,lth,rlm games::50:hansen,autilio,avi news::51:netnews slan::57:slan,root sie::80: sable::117:sable dptg::88:emm,kbs,jak,ecc,jhc,jrw,rkh DOS-ash::92:msnet DOS--sh::93:msnet DOS-a-h::94:msnet DOS-as-::95:msnet DOS---h::96:msnet DOS--s-::97:msnet DOS-a--::98:msnet DOS----::99:msnet exptools::90:exptools vm::100:vmsys pcor::102:pcor,russak,eth,jim,hansen,jack dx::880: OLD::1000:scy olympus::1100:olympus,aku tsys::1101:root,tadm !EOF! ls -l tst.data echo x - tst.in sed 's/^X//' > tst.in << '!EOF!' 1 0 20 -1 1500 2 !EOF! ls -l tst.in # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0