home *** CD-ROM | disk | FTP | other *** search
- /* DiscIndex 1.00
- * Code for saving and loading files
- * By Neil A Carson 1994 A.D.
- */
-
- #include "DiscIndex.h"
- #include "heap.h"
- #include "kernel.h"
- #include "os.h"
- #include "trace.h"
- #include "visdelay.h"
- #include "xferrecv.h"
- #include "werr.h"
- #include "wimp.h"
- #include "wimpt.h"
-
- #include <stdio.h>
- #include <string.h>
-
- extern char std_save_name[256];
-
- BOOL save_records(char *filename, void *handle)
- {
- _kernel_osfile_block block;
- int rcnt;
- disc_type *disc;
- FILE *fp;
-
- handle = handle;
- fp = fopen(filename, "wb");
- visdelay_begin();
- if (fp == 0)
- {
- visdelay_end();
- werr(0, "Unable to open file %s", filename);
- return FALSE;
- }
-
- strncpy(std_save_name, filename, 256);
-
- fputc(192, fp);
-
- fputc(100, fp);
- fputc(records.no, fp);
- fputc(records.no >> 8, fp);
-
- for (rcnt = 0; rcnt < records.no; rcnt ++)
- {
- disc = disc_locate(rcnt);
- if (disc == 0) werr(1, "Fatal error in LoadSave.c");
- /* Write out entire structure - do next and files for hell of it*/
- if (fwrite(disc, sizeof(disc_type), 1, fp) != 1)
- {
- visdelay_end();
- werr(0, "Error writing file %s", filename);
- fclose(fp);
- return FALSE;
- }
- /* Now write out files in structure, following record straight away */
- if (fwrite(disc->files, sizeof(file_type) * disc->no, 1, fp) != 1)
- {
- visdelay_end();
- werr(0, "Error writing file %s", filename);
- fclose(fp);
- return FALSE;
- }
- /* And that's it !!! */
- }
- visdelay_end();
- fclose(fp);
- block.load = 0xFFF34500;
- _kernel_osfile(2, filename, &block);
- return TRUE;
- }
-
- BOOL load_records(char *filename, void *handle)
- {
- FILE *fp;
- disc_type *disc;
- int rcnt, no, x, y;
-
- visdelay_begin();
- handle = handle;
- fp = fopen(filename, "rb");
- if (fp == 0)
- {
- visdelay_end();
- werr(0, "Can't open filename %s\n", filename);
- return FALSE;
- }
-
- if (fgetc(fp) != 192)
- {
- visdelay_end();
- werr(0, "Error reading file %s / Not an DiscIndex file", filename);
- fclose(fp);
- return FALSE;
- }
- if (fgetc(fp) != 100)
- {
- visdelay_end();
- werr(0, "Sorry, this file is from a different version of Disc Index!");
- fclose(fp);
- return FALSE;
- }
- no = fgetc(fp);
- no |= fgetc(fp) << 8;
-
- x = 0 ^ 0x80;
- y = 0;
- wimpt_noerr(os_byte(0x79, &x, &y));
-
- if (x != 0xFF) mem_free();
-
- for (rcnt = 0; rcnt < no; rcnt ++)
- {
- /* Make the next disc */
- disc = disc_create();
- if (disc == 0)
- {
- visdelay_end();
- werr(0, "No room for this file - partly loaded!");
- fclose(fp);
- return FALSE;
- }
- /* Get it in from the file */
- if (fread(disc, sizeof(disc_type), 1, fp) != 1)
- {
- visdelay_end();
- werr(0, "Error reading file %s", filename);
- fclose(fp);
- return FALSE;
- }
- /* Allocate RAM for files from this disc */
- if (disc->no != 0)
- {
- disc->files = heap_alloc(sizeof(file_type) * disc->no);
- if (disc->files == 0)
- {
- visdelay_end();
- werr(0, "No room for this file - partly loaded!");
- fclose(fp);
- return FALSE;
- }
- /* Read files into RAM just allocated */
- if (fread(disc->files, sizeof(file_type) * disc->no, 1, fp) != 1)
- {
- visdelay_end();
- werr(0, "Error reading file %s", filename);
- fclose(fp);
- return FALSE;
- }
- }
- }
- fclose(fp);
- xferrecv_insertfileok();
- set_current_disc(0);
- visdelay_end();
- return TRUE;
- }
-