/* This file uses a file name requestor, slightly modified, by "cheath", 
   which was distributed by AZTEC on their examples disk. */

#include "getfile.h"
#include "life.h"
#include "stdio.h"
#include "functions.h"
#include "libraries/dosextens.h"

FILE *population, *fopen();
char filename[FNAME_SIZE+1] = "life.start";
char dirname[FNAME_SIZE+2]  = ":";
char fullname[2 * FNAME_SIZE + 4];
static struct Process	*OurTask;
static struct Window	*old_pr_WindowPtr;


dofile(eS, eW, lifearray) 
struct Screen *eS;
struct Window *eW;
short *lifearray;
{
char *mode;                    /* open mode, "r" or "w" */
int  typepick;                 /* return from get_fname */
int  io_count;                 /* no. of items read or written */
  /* CAUTION!!! KNOW ABOUT pr_WindowPtr before you use it!!! */

  WBenchToFront();
  OurTask = (struct Process *)FindTask(0L);
  old_pr_WindowPtr = OurTask->pr_WindowPtr;
  OurTask->pr_WindowPtr = eW;

  typepick = get_fname(eW, NULL, "Select file", filename, dirname);
  /* ALWAYS RESTORE pr_WindowPtr BEFORE CLOSING THE WINDOW!!! */
  OurTask->pr_WindowPtr = old_pr_WindowPtr;
  WBenchToBack();

  switch(typepick)
  { case CANPICK:
	  return(FALSE);
      break;         /* do nothing */
	case READPICK:
	  mode = "r";
	  break;
	case WRTPICK:
	  mode = "w";
	  break;
  } /* switch */
  strcpy(fullname, dirname);
  strcat(fullname, filename);
  if ((population = fopen(fullname, mode)) == NULL )
    { printf("Can't open file %s\n", fullname);
	  DisplayBeep(eS);
	  return(FALSE);
	}
  if (typepick == READPICK)
    io_count = fread(lifearray, HWORDS*2, VSIZE, population);
  if (typepick == WRTPICK)
    io_count = fwrite(lifearray, HWORDS*2, VSIZE, population);
  if (io_count != VSIZE)
    printf("File size error; io_count=%d, %d, %d\n",
	                         io_count, HWORDS*2, VSIZE);
  fclose(population);
  return(typepick==READPICK); /* tell caller we read something */
} /* dofile */
