1 #ifndef lint 2 static char sccsid[] = "@(#)dumpgame.c 4.2 (Berkeley) 05/27/83"; 3 #endif not lint 4 5 # include "trek.h" 6 7 /*** THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/ 8 # define VERSION 2 9 10 struct dump 11 { 12 char *area; 13 int count; 14 }; 15 16 17 struct dump Dump_template[] = 18 { 19 (char *)&Ship, sizeof (Ship), 20 (char *)&Now, sizeof (Now), 21 (char *)&Param, sizeof (Param), 22 (char *)&Etc, sizeof (Etc), 23 (char *)&Game, sizeof (Game), 24 (char *)Sect, sizeof (Sect), 25 (char *)Quad, sizeof (Quad), 26 (char *)&Move, sizeof (Move), 27 (char *)Event, sizeof (Event), 28 0 29 }; 30 31 /* 32 ** DUMP GAME 33 ** 34 ** This routine dumps the game onto the file "trek.dump". The 35 ** first two bytes of the file are a version number, which 36 ** reflects whether this image may be used. Obviously, it must 37 ** change as the size, content, or order of the data structures 38 ** output change. 39 */ 40 41 dumpgame() 42 { 43 int version; 44 register int fd; 45 register struct dump *d; 46 register int i; 47 48 if ((fd = creat("trek.dump", 0644)) < 0) 49 return (printf("cannot dump\n")); 50 version = VERSION; 51 write(fd, &version, sizeof version); 52 53 /* output the main data areas */ 54 for (d = Dump_template; d->area; d++) 55 { 56 write(fd, &d->area, sizeof d->area); 57 i = d->count; 58 write(fd, d->area, i); 59 } 60 61 close(fd); 62 } 63 64 65 /* 66 ** RESTORE GAME 67 ** 68 ** The game is restored from the file "trek.dump". In order for 69 ** this to succeed, the file must exist and be readable, must 70 ** have the correct version number, and must have all the appro- 71 ** priate data areas. 72 ** 73 ** Return value is zero for success, one for failure. 74 */ 75 76 restartgame() 77 { 78 register int fd; 79 int version; 80 81 if ((fd = open("trek.dump", 0)) < 0 || 82 read(fd, &version, sizeof version) != sizeof version || 83 version != VERSION || 84 readdump(fd)) 85 { 86 printf("cannot restart\n"); 87 close(fd); 88 return (1); 89 } 90 91 close(fd); 92 return (0); 93 } 94 95 96 /* 97 ** READ DUMP 98 ** 99 ** This is the business end of restartgame(). It reads in the 100 ** areas. 101 ** 102 ** Returns zero for success, one for failure. 103 */ 104 105 readdump(fd1) 106 int fd1; 107 { 108 register int fd; 109 register struct dump *d; 110 register int i; 111 int junk; 112 113 fd = fd1; 114 115 for (d = Dump_template; d->area; d++) 116 { 117 if (read(fd, &junk, sizeof junk) != (sizeof junk)) 118 return (1); 119 if ((char *)junk != d->area) 120 return (1); 121 i = d->count; 122 if (read(fd, d->area, i) != i) 123 return (1); 124 } 125 126 /* make quite certain we are at EOF */ 127 return (read(fd, &junk, 1)); 128 } 129