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