home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fred Fish Collection 1.5
/
ffcollection-1-5-1992-11.iso
/
ff_disks
/
300-399
/
ff343.lzh
/
SnakePit
/
SnakePitSrc.lzh
/
Snake_Save.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-07-04
|
3KB
|
148 lines
/*
* Snake_Save Copyright (c) 1988 MKSoft
*
* These routines set up and save the program's data in either a data file
* or within the code itself...
*/
#include "Snake.h"
#include "Snake_Save.h"
#define BUFF_SIZE 512L
static struct FileHandle *fd=NULL;
static long Keys_Pos=NULL;
static long Score_Pos=NULL;
static long Screen_Pos=NULL;
static char FileID[]=FILE_ID_HEADER;
static char Buffer[BUFF_SIZE];
static long Find_Pos(data,size) register char *data;
long size;
{
register long pos=NULL;
register long rds;
register long p;
register long t;
long tfound;
long found=NULL;
short flag=TRUE;
short flag1;
short flag2;
while (flag)
{
Seek(fd,pos,OFFSET_BEGINNING);
rds=Read(fd,Buffer,BUFF_SIZE);
flag2=TRUE;
if (rds)
{
p=NULL;
while ((p<rds)&&flag2)
{
t=NULL;
if (Buffer[p]==data[t])
{
tfound=pos+p;
flag1=TRUE;
while (flag1)
{
t++;
if (t<size)
{
p++;
if (p>=rds)
{
rds=Read(fd,Buffer,BUFF_SIZE);
p=NULL;
flag2=FALSE;
pos=tfound+1L;
}
if (p<rds)
{
if (Buffer[p]!=data[t]) flag1=FALSE;
}
else flag1=FALSE;
}
else
{
flag2=flag1=flag=FALSE;
found=tfound;
}
}
}
else p++;
}
}
else flag=FALSE;
}
return(found);
}
VOID SetUp_For_Save()
{
char temp[sizeof(FileID)];
if (fd=Open(Save_File,MODE_OLDFILE))
{
Seek(fd,0L,OFFSET_BEGINNING);
Read(fd,temp,(long)sizeof(FileID));
if (strcmp(temp,FileID))
{ /* The exec file... */
Keys_Pos=Find_Pos(KeyList,(long)sizeof(KeyList));
Score_Pos=Find_Pos(ScoreList,(long)sizeof(ScoreList));
Screen_Pos=Find_Pos(All_Pit_Screens[0],(long)sizeof(Pit_Screen));
}
else
{ /* A data file... */
Score_Pos=Seek(fd,0L,OFFSET_CURRENT);
Read(fd,ScoreList,(long)sizeof(ScoreList));
Keys_Pos=Seek(fd,0L,OFFSET_CURRENT);
Read(fd,KeyList,(long)sizeof(KeyList));
Screen_Pos=Seek(fd,0L,OFFSET_CURRENT);
Read(fd,All_Pit_Screens[0],(long)(((long)MAX_LEVEL)*((long)sizeof(Pit_Screen))));
}
}
else if (fd=Open(Save_File,MODE_NEWFILE))
{ /* A new data file... */
Seek(fd,0L,OFFSET_BEGINNING);
Write(fd,FileID,(long)sizeof(FileID));
Score_Pos=Seek(fd,0L,OFFSET_CURRENT);
Write(fd,ScoreList,(long)sizeof(ScoreList));
Keys_Pos=Seek(fd,0L,OFFSET_CURRENT);
Write(fd,KeyList,(long)sizeof(KeyList));
Screen_Pos=Seek(fd,0L,OFFSET_CURRENT);
Write(fd,All_Pit_Screens[0],(long)(((long)MAX_LEVEL)*((long)sizeof(Pit_Screen))));
}
if (fd) Close(fd);
}
VOID Save_It_Now(Screens,Scores,Keys) short Screens;
short Scores;
short Keys;
{
if ((Score_Pos)&&(Keys_Pos)&&(Screen_Pos)&&((Screens)||(Scores)||(Keys)))
if (fd=Open(Save_File,MODE_OLDFILE))
{
if (Keys)
{
Seek(fd,Keys_Pos,OFFSET_BEGINNING);
Write(fd,KeyList,(long)sizeof(KeyList));
}
if (Scores)
{
Seek(fd,Score_Pos,OFFSET_BEGINNING);
Write(fd,ScoreList,(long)sizeof(ScoreList));
}
if (Screens)
{
Seek(fd,Screen_Pos,OFFSET_BEGINNING);
Write(fd,All_Pit_Screens[0],(long)(((long)MAX_LEVEL)*((long)sizeof(Pit_Screen))));
}
Close(fd);
}
}