home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities3 / discindex / DiscIndex / Source / C / LoadSave < prev    next >
Text File  |  1994-08-03  |  4KB  |  161 lines

  1. /* DiscIndex 1.00
  2.  * Code for saving and loading files
  3.  * By Neil A Carson 1994 A.D.
  4.  */
  5.  
  6. #include "DiscIndex.h"
  7. #include "heap.h"
  8. #include "kernel.h"
  9. #include "os.h"
  10. #include "trace.h"
  11. #include "visdelay.h"
  12. #include "xferrecv.h"
  13. #include "werr.h"
  14. #include "wimp.h"
  15. #include "wimpt.h"
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. extern char std_save_name[256];
  21.  
  22. BOOL save_records(char *filename, void *handle)
  23. {
  24.     _kernel_osfile_block block;
  25.     int rcnt;
  26.     disc_type *disc;
  27.     FILE *fp;
  28.  
  29.     handle = handle;
  30.     fp = fopen(filename, "wb");
  31.     visdelay_begin();
  32.     if (fp == 0)
  33.     {
  34.         visdelay_end();
  35.         werr(0, "Unable to open file %s", filename);
  36.         return FALSE;
  37.     }
  38.  
  39.     strncpy(std_save_name, filename, 256);
  40.  
  41.     fputc(192, fp);
  42.  
  43.     fputc(100, fp);
  44.     fputc(records.no, fp);
  45.     fputc(records.no >> 8, fp);
  46.  
  47.     for (rcnt = 0; rcnt < records.no; rcnt ++)
  48.     {
  49.         disc = disc_locate(rcnt);
  50.         if (disc == 0) werr(1, "Fatal error in LoadSave.c");
  51.         /* Write out entire structure  - do next and files for hell of it*/
  52.         if (fwrite(disc, sizeof(disc_type), 1, fp) != 1)
  53.         {
  54.             visdelay_end();
  55.             werr(0, "Error writing file %s", filename);
  56.             fclose(fp);
  57.             return FALSE;
  58.         }
  59.         /* Now write out files in structure, following record straight away */
  60.         if (fwrite(disc->files, sizeof(file_type) * disc->no, 1, fp) != 1)
  61.         {
  62.             visdelay_end();
  63.             werr(0, "Error writing file %s", filename);
  64.             fclose(fp);
  65.             return FALSE;
  66.         }
  67.         /* And that's it !!! */
  68.     }
  69.     visdelay_end();
  70.     fclose(fp);
  71.     block.load = 0xFFF34500;
  72.     _kernel_osfile(2, filename, &block);
  73.     return TRUE;
  74. }
  75.  
  76. BOOL load_records(char *filename, void *handle)
  77. {
  78.     FILE *fp;
  79.     disc_type *disc;
  80.     int rcnt, no, x, y;
  81.  
  82.     visdelay_begin();
  83.     handle = handle;
  84.     fp = fopen(filename, "rb");
  85.     if (fp == 0)
  86.     {
  87.         visdelay_end();
  88.         werr(0, "Can't open filename %s\n", filename);
  89.         return FALSE;
  90.     }
  91.  
  92.     if (fgetc(fp) != 192)
  93.     {
  94.         visdelay_end();
  95.         werr(0, "Error reading file %s / Not an DiscIndex file", filename);
  96.         fclose(fp);
  97.         return FALSE;
  98.     }
  99.     if (fgetc(fp) != 100)
  100.     {
  101.         visdelay_end();
  102.         werr(0, "Sorry, this file is from a different version of Disc Index!");
  103.         fclose(fp);
  104.         return FALSE;
  105.     }
  106.     no = fgetc(fp);
  107.     no |= fgetc(fp) << 8;
  108.  
  109.     x = 0 ^ 0x80;
  110.     y = 0;
  111.     wimpt_noerr(os_byte(0x79, &x, &y));
  112.  
  113.     if (x != 0xFF) mem_free();
  114.  
  115.     for (rcnt = 0; rcnt < no; rcnt ++)
  116.     {
  117.         /* Make the next disc */
  118.         disc = disc_create();
  119.         if (disc == 0)
  120.         {
  121.             visdelay_end();
  122.             werr(0, "No room for this file - partly loaded!");
  123.             fclose(fp);
  124.             return FALSE;
  125.         }
  126.         /* Get it in from the file */
  127.         if (fread(disc, sizeof(disc_type), 1, fp) != 1)
  128.         {
  129.             visdelay_end();
  130.             werr(0, "Error reading file %s", filename);
  131.             fclose(fp);
  132.             return FALSE;
  133.         }
  134.         /* Allocate RAM for files from this disc */
  135.         if (disc->no != 0)
  136.         {
  137.             disc->files = heap_alloc(sizeof(file_type) * disc->no);
  138.             if (disc->files == 0)
  139.             {
  140.                 visdelay_end();
  141.                 werr(0, "No room for this file - partly loaded!");
  142.                 fclose(fp);
  143.                 return FALSE;
  144.             }
  145.             /* Read files into RAM just allocated */
  146.             if (fread(disc->files, sizeof(file_type) * disc->no, 1, fp) != 1)
  147.             {
  148.                 visdelay_end();
  149.                 werr(0, "Error reading file %s", filename);
  150.                 fclose(fp);
  151.                 return FALSE;
  152.             }
  153.         }
  154.     }
  155.     fclose(fp);
  156.     xferrecv_insertfileok();
  157.     set_current_disc(0);
  158.     visdelay_end();
  159.     return TRUE;
  160. }
  161.