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