home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / xsokoban-31 / save.c < prev    next >
C/C++ Source or Header  |  1998-01-11  |  4KB  |  167 lines

  1. #include "config_local.h"
  2.  
  3. #include <stdio.h>
  4. #ifndef VMS
  5. #include <sys/types.h>
  6. #else
  7. #include "unix_types.h"
  8. #endif
  9. #include <sys/stat.h>
  10. #include <signal.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14.  
  15. #include "externs.h"
  16. #include "globals.h"
  17.  
  18. static long savedbn;
  19. static char *sfname;
  20. static FILE *savefile;
  21. static struct stat sfstat;
  22. extern void unlink();
  23.  
  24. /* save out a game in a standard format.  Uses the ntoh functions and hton
  25.  * functions so that the same save file will work on an MSB or LSB system
  26.  * other than that, it is a standard sokoban score file.
  27.  */
  28. short SaveGame(void)
  29. {
  30.   short ret = 0;
  31.  
  32.   signal(SIGINT, SIG_IGN);
  33.   sfname = malloc(strlen(SAVEPATH) + strlen(username) + 6);
  34.  
  35. #ifdef VMS
  36.   sprintf(sfname, "%s:%s.sav", SAVEPATH, username);
  37. #else
  38.   sprintf(sfname, "%s/%s.sav", SAVEPATH, username);
  39. #endif
  40.  
  41. #ifdef DEBUG
  42.   printf("DEBUG :: Save file %s\n",sfname);
  43. #endif
  44.  
  45.   packets = htons(packets);
  46.   pushes = htons(pushes);
  47.   moves = htons(moves);
  48.   level = htons(level);
  49.   cols = htons(cols);
  50.   savepack = htons(savepack);
  51.   rows = htons(rows);
  52.   ppos.x = htons(ppos.x);
  53.   ppos.y = htons(ppos.y);
  54.  
  55.   if ((savefile = fopen(sfname, "w")) == NULL)
  56.     ret = E_FOPENSAVE;
  57.   else {
  58.     savedbn = fileno(savefile);
  59.     if (write(savedbn, &(map[0][0]), MAXROW * MAXCOL) != MAXROW * MAXCOL)
  60.       ret = E_WRITESAVE;
  61.     else if (write(savedbn, &ppos, sizeof(POS)) != sizeof(POS))
  62.       ret = E_WRITESAVE;
  63.     else if (write(savedbn, &level, 2) != 2)
  64.       ret = E_WRITESAVE;
  65.     else if (write(savedbn, &moves, 2) != 2)
  66.       ret = E_WRITESAVE;
  67.     else if (write(savedbn, &pushes, 2) != 2)
  68.       ret = E_WRITESAVE;
  69.     else if (write(savedbn, &packets, 2) != 2)
  70.       ret = E_WRITESAVE;
  71.     else if (write(savedbn, &savepack, 2) != 2)
  72.       ret = E_WRITESAVE;
  73.     else if (write(savedbn, &rows, 2) != 2)
  74.       ret = E_WRITESAVE;
  75.     else if (write(savedbn, &cols, 2) != 2)
  76.       ret = E_WRITESAVE;
  77.     else
  78.       fclose(savefile);
  79.     if (stat(sfname, &sfstat) != 0)
  80.       ret = E_STATSAVE;
  81.     else if ((savefile = fopen(sfname, "a")) == NULL)
  82.       ret = E_FOPENSAVE;
  83.     else if (write(savedbn, &sfstat, sizeof(sfstat)) != sizeof(sfstat))
  84.       ret = E_WRITESAVE;
  85.     fclose(savefile);
  86.   }
  87.  
  88.   ppos.x = ntohs(ppos.x);
  89.   ppos.y = ntohs(ppos.y);
  90.   pushes = ntohs(pushes);
  91.   moves = ntohs(moves);
  92.   level = ntohs(level);
  93.   packets = ntohs(packets);
  94.   cols = ntohs(cols);
  95.   rows = ntohs(rows);
  96.   savepack = ntohs(savepack);
  97.  
  98.   if ((ret == E_WRITESAVE) || (ret == E_STATSAVE))
  99.     unlink(sfname);
  100.   signal(SIGINT, SIG_DFL);
  101.  
  102.   free(sfname);
  103.   return ret;
  104. }
  105.  
  106. /* loads in a previously saved game */
  107. short RestoreGame(void)
  108. {
  109.   short ret = 0;
  110.   struct stat oldsfstat;
  111.  
  112.   signal(SIGINT, SIG_IGN);
  113.  
  114.   sfname = malloc(strlen(SAVEPATH) + strlen(username) + 6);
  115.  
  116. #ifdef VMS
  117.   sprintf(sfname, "%s:%s.sav", SAVEPATH, username);
  118. #else
  119.   sprintf(sfname, "%s/%s.sav", SAVEPATH, username);
  120. #endif
  121.  
  122.   if (stat(sfname, &oldsfstat) < -1)
  123.     ret = E_NOSAVEFILE;
  124.   else {
  125.     if ((savefile = fopen(sfname, "r")) == NULL)
  126.       ret = E_FOPENSAVE;
  127.     else {
  128.       savedbn = fileno(savefile);
  129.       if (read(savedbn, &(map[0][0]), MAXROW * MAXCOL) != MAXROW * MAXCOL)
  130.     ret = E_READSAVE;
  131.       else if (read(savedbn, &ppos, sizeof(POS)) != sizeof(POS))
  132.     ret = E_READSAVE;
  133.       else if (read(savedbn, &level, 2) != 2)
  134.     ret = E_READSAVE;
  135.       else if (read(savedbn, &moves, 2) != 2)
  136.     ret = E_READSAVE;
  137.       else if (read(savedbn, &pushes, 2) != 2)
  138.     ret = E_READSAVE;
  139.       else if (read(savedbn, &packets, 2) != 2)
  140.     ret = E_READSAVE;
  141.       else if (read(savedbn, &savepack, 2) != 2)
  142.     ret = E_READSAVE;
  143.       else if (read(savedbn, &rows, 2) != 2)
  144.     ret = E_READSAVE;
  145.       else if (read(savedbn, &cols, 2) != 2)
  146.     ret = E_READSAVE;
  147.       else if (read(savedbn, &sfstat, sizeof(sfstat)) != sizeof(sfstat))
  148.     ret = E_READSAVE;
  149.     }
  150.  
  151.     ppos.x = ntohs(ppos.x);
  152.     ppos.y = ntohs(ppos.y);
  153.     level = ntohs(level);
  154.     moves = ntohs(moves);
  155.     pushes = ntohs(pushes);
  156.     packets = ntohs(packets);
  157.     savepack = ntohs(savepack);
  158.     rows = ntohs(rows);
  159.     cols = ntohs(cols);
  160.  
  161.     unlink(sfname);
  162.   }
  163.   signal(SIGINT, SIG_DFL);
  164.   free(sfname);
  165.   return ret;
  166. }
  167.