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 >
C/C++ Source or Header  |  1988-07-04  |  3KB  |  148 lines

  1. /*
  2.  * Snake_Save  Copyright (c) 1988  MKSoft
  3.  *
  4.  * These routines set up and save the program's data in either a data file
  5.  * or within the code itself...
  6.  */
  7.  
  8. #include    "Snake.h"
  9. #include    "Snake_Save.h"
  10.  
  11. #define    BUFF_SIZE    512L
  12.  
  13. static    struct    FileHandle    *fd=NULL;
  14.  
  15. static        long        Keys_Pos=NULL;
  16. static        long        Score_Pos=NULL;
  17. static        long        Screen_Pos=NULL;
  18.  
  19. static        char        FileID[]=FILE_ID_HEADER;
  20.  
  21. static        char        Buffer[BUFF_SIZE];
  22.  
  23. static    long    Find_Pos(data,size)    register    char    *data;
  24.                             long    size;
  25. {
  26. register    long    pos=NULL;
  27. register    long    rds;
  28. register    long    p;
  29. register    long    t;
  30.         long    tfound;
  31.         long    found=NULL;
  32.         short    flag=TRUE;
  33.         short    flag1;
  34.         short    flag2;
  35.  
  36.     while (flag)
  37.     {
  38.         Seek(fd,pos,OFFSET_BEGINNING);
  39.         rds=Read(fd,Buffer,BUFF_SIZE);
  40.         flag2=TRUE;
  41.         if (rds)
  42.         {
  43.             p=NULL;
  44.             while ((p<rds)&&flag2)
  45.             {
  46.                 t=NULL;
  47.                 if (Buffer[p]==data[t])
  48.                 {
  49.                     tfound=pos+p;
  50.                     flag1=TRUE;
  51.                     while (flag1)
  52.                     {
  53.                         t++;
  54.                         if (t<size)
  55.                         {
  56.                             p++;
  57.                             if (p>=rds)
  58.                             {
  59.                                 rds=Read(fd,Buffer,BUFF_SIZE);
  60.                                 p=NULL;
  61.                                 flag2=FALSE;
  62.                                 pos=tfound+1L;
  63.                             }
  64.                             if (p<rds)
  65.                             {
  66.                                 if (Buffer[p]!=data[t]) flag1=FALSE;
  67.                             }
  68.                             else flag1=FALSE;
  69.                         }
  70.                         else
  71.                         {
  72.                             flag2=flag1=flag=FALSE;
  73.                             found=tfound;
  74.                         }
  75.                     }
  76.                 }
  77.                 else p++;
  78.             }
  79.         }
  80.         else flag=FALSE;
  81.     }
  82.     return(found);
  83. }
  84.  
  85. VOID    SetUp_For_Save()
  86. {
  87. char    temp[sizeof(FileID)];
  88.  
  89.     if (fd=Open(Save_File,MODE_OLDFILE))
  90.     {
  91.         Seek(fd,0L,OFFSET_BEGINNING);
  92.         Read(fd,temp,(long)sizeof(FileID));
  93.         if (strcmp(temp,FileID))
  94.         {    /* The exec file... */
  95.             Keys_Pos=Find_Pos(KeyList,(long)sizeof(KeyList));
  96.             Score_Pos=Find_Pos(ScoreList,(long)sizeof(ScoreList));
  97.             Screen_Pos=Find_Pos(All_Pit_Screens[0],(long)sizeof(Pit_Screen));
  98.         }
  99.         else
  100.         {    /* A data file... */
  101.             Score_Pos=Seek(fd,0L,OFFSET_CURRENT);
  102.             Read(fd,ScoreList,(long)sizeof(ScoreList));
  103.             Keys_Pos=Seek(fd,0L,OFFSET_CURRENT);
  104.             Read(fd,KeyList,(long)sizeof(KeyList));
  105.             Screen_Pos=Seek(fd,0L,OFFSET_CURRENT);
  106.             Read(fd,All_Pit_Screens[0],(long)(((long)MAX_LEVEL)*((long)sizeof(Pit_Screen))));
  107.         }
  108.     }
  109.     else if (fd=Open(Save_File,MODE_NEWFILE))
  110.     {    /* A new data file... */
  111.         Seek(fd,0L,OFFSET_BEGINNING);
  112.         Write(fd,FileID,(long)sizeof(FileID));
  113.         Score_Pos=Seek(fd,0L,OFFSET_CURRENT);
  114.         Write(fd,ScoreList,(long)sizeof(ScoreList));
  115.         Keys_Pos=Seek(fd,0L,OFFSET_CURRENT);
  116.         Write(fd,KeyList,(long)sizeof(KeyList));
  117.         Screen_Pos=Seek(fd,0L,OFFSET_CURRENT);
  118.         Write(fd,All_Pit_Screens[0],(long)(((long)MAX_LEVEL)*((long)sizeof(Pit_Screen))));
  119.     }
  120.     if (fd) Close(fd);
  121. }
  122.  
  123. VOID    Save_It_Now(Screens,Scores,Keys)    short    Screens;
  124.                         short    Scores;
  125.                         short    Keys;
  126. {
  127.     if ((Score_Pos)&&(Keys_Pos)&&(Screen_Pos)&&((Screens)||(Scores)||(Keys)))
  128.         if (fd=Open(Save_File,MODE_OLDFILE))
  129.         {
  130.             if (Keys)
  131.             {
  132.                 Seek(fd,Keys_Pos,OFFSET_BEGINNING);
  133.                 Write(fd,KeyList,(long)sizeof(KeyList));
  134.             }
  135.             if (Scores)
  136.             {
  137.                 Seek(fd,Score_Pos,OFFSET_BEGINNING);
  138.                 Write(fd,ScoreList,(long)sizeof(ScoreList));
  139.             }
  140.             if (Screens)
  141.             {
  142.                 Seek(fd,Screen_Pos,OFFSET_BEGINNING);
  143.                 Write(fd,All_Pit_Screens[0],(long)(((long)MAX_LEVEL)*((long)sizeof(Pit_Screen))));
  144.             }
  145.             Close(fd);
  146.         }
  147. }
  148.