xref: /csrg-svn/games/trek/dumpgame.c (revision 60857)
125990Smckusick /*
2*60857Sbostic  * Copyright (c) 1980, 1993
3*60857Sbostic  *	The Regents of the University of California.  All rights reserved.
434205Sbostic  *
542605Sbostic  * %sccs.include.redist.c%
625990Smckusick  */
725990Smckusick 
811669Smckusick #ifndef lint
9*60857Sbostic static char sccsid[] = "@(#)dumpgame.c	8.1 (Berkeley) 05/31/93";
1034205Sbostic #endif /* not lint */
1111669Smckusick 
1211669Smckusick # include	"trek.h"
1311669Smckusick 
1411669Smckusick /***  THIS CONSTANT MUST CHANGE AS THE DATA SPACES CHANGE ***/
1512798Slayer # define	VERSION		2
1611669Smckusick 
1711669Smckusick struct dump
1811669Smckusick {
1911669Smckusick 	char	*area;
2011669Smckusick 	int	count;
2111669Smckusick };
2211669Smckusick 
2312798Slayer 
2412798Slayer struct dump	Dump_template[] =
2511669Smckusick {
2612798Slayer 	(char *)&Ship,		sizeof (Ship),
2712798Slayer 	(char *)&Now,		sizeof (Now),
2812798Slayer 	(char *)&Param,		sizeof (Param),
2912798Slayer 	(char *)&Etc,		sizeof (Etc),
3012798Slayer 	(char *)&Game,		sizeof (Game),
3112798Slayer 	(char *)Sect,		sizeof (Sect),
3212798Slayer 	(char *)Quad,		sizeof (Quad),
3312798Slayer 	(char *)&Move,		sizeof (Move),
3412798Slayer 	(char *)Event,		sizeof (Event),
3511669Smckusick 	0
3611669Smckusick };
3711669Smckusick 
3811669Smckusick /*
3911669Smckusick **  DUMP GAME
4011669Smckusick **
4111669Smckusick **	This routine dumps the game onto the file "trek.dump".  The
4211669Smckusick **	first two bytes of the file are a version number, which
4311669Smckusick **	reflects whether this image may be used.  Obviously, it must
4411669Smckusick **	change as the size, content, or order of the data structures
4511669Smckusick **	output change.
4611669Smckusick */
4711669Smckusick 
dumpgame()4811669Smckusick dumpgame()
4911669Smckusick {
5011669Smckusick 	int			version;
5111669Smckusick 	register int		fd;
5211669Smckusick 	register struct dump	*d;
5311669Smckusick 	register int		i;
5411669Smckusick 
5511669Smckusick 	if ((fd = creat("trek.dump", 0644)) < 0)
5611669Smckusick 		return (printf("cannot dump\n"));
5711669Smckusick 	version = VERSION;
5811669Smckusick 	write(fd, &version, sizeof version);
5911669Smckusick 
6011669Smckusick 	/* output the main data areas */
6111669Smckusick 	for (d = Dump_template; d->area; d++)
6211669Smckusick 	{
6311669Smckusick 		write(fd, &d->area, sizeof d->area);
6411669Smckusick 		i = d->count;
6511669Smckusick 		write(fd, d->area, i);
6611669Smckusick 	}
6711669Smckusick 
6811669Smckusick 	close(fd);
6911669Smckusick }
7011669Smckusick 
7111669Smckusick 
7211669Smckusick /*
7311669Smckusick **  RESTORE GAME
7411669Smckusick **
7511669Smckusick **	The game is restored from the file "trek.dump".  In order for
7611669Smckusick **	this to succeed, the file must exist and be readable, must
7711669Smckusick **	have the correct version number, and must have all the appro-
7811669Smckusick **	priate data areas.
7911669Smckusick **
8011669Smckusick **	Return value is zero for success, one for failure.
8111669Smckusick */
8211669Smckusick 
restartgame()8311669Smckusick restartgame()
8411669Smckusick {
8511669Smckusick 	register int	fd;
8611669Smckusick 	int		version;
8711669Smckusick 
8811669Smckusick 	if ((fd = open("trek.dump", 0)) < 0 ||
8911669Smckusick 	    read(fd, &version, sizeof version) != sizeof version ||
9011669Smckusick 	    version != VERSION ||
9111669Smckusick 	    readdump(fd))
9211669Smckusick 	{
9311669Smckusick 		printf("cannot restart\n");
9411669Smckusick 		close(fd);
9511669Smckusick 		return (1);
9611669Smckusick 	}
9711669Smckusick 
9811669Smckusick 	close(fd);
9911669Smckusick 	return (0);
10011669Smckusick }
10111669Smckusick 
10211669Smckusick 
10311669Smckusick /*
10411669Smckusick **  READ DUMP
10511669Smckusick **
10611669Smckusick **	This is the business end of restartgame().  It reads in the
10711669Smckusick **	areas.
10811669Smckusick **
10911669Smckusick **	Returns zero for success, one for failure.
11011669Smckusick */
11111669Smckusick 
readdump(fd1)11211669Smckusick readdump(fd1)
11311669Smckusick int	fd1;
11411669Smckusick {
11511669Smckusick 	register int		fd;
11611669Smckusick 	register struct dump	*d;
11711669Smckusick 	register int		i;
11811669Smckusick 	int			junk;
11911669Smckusick 
12011669Smckusick 	fd = fd1;
12111669Smckusick 
12211669Smckusick 	for (d = Dump_template; d->area; d++)
12311669Smckusick 	{
12412798Slayer 		if (read(fd, &junk, sizeof junk) != (sizeof junk))
12511669Smckusick 			return (1);
12612798Slayer 		if ((char *)junk != d->area)
12711669Smckusick 			return (1);
12811669Smckusick 		i = d->count;
12911669Smckusick 		if (read(fd, d->area, i) != i)
13011669Smckusick 			return (1);
13111669Smckusick 	}
13211669Smckusick 
13311669Smckusick 	/* make quite certain we are at EOF */
13411669Smckusick 	return (read(fd, &junk, 1));
13511669Smckusick }
136