xref: /original-bsd/games/trek/dumpgame.c (revision b4971bb3)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)dumpgame.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 # include	"trek.h"
13 
14 /***  THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/
15 # define	VERSION		2
16 
17 struct dump
18 {
19 	char	*area;
20 	int	count;
21 };
22 
23 
24 struct dump	Dump_template[] =
25 {
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),
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 	{
124 		if (read(fd, &junk, sizeof junk) != (sizeof junk))
125 			return (1);
126 		if ((char *)junk != d->area)
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 }
136