#include "define.h"
#include <stdio.h>
#include <errno.h>
#include <osbind.h>


/****************************************************************************
SUBROUTINE  :  init_path               VERSION  : 1.0
AUTHOR      :  ROBERT ALLEY            DATE     : 3/3/90
DESCRIPTION :  Initialise directory for disk ops.
LOGIC USED  :  STEP 1.  Get current drive, path.  Build directory string.
RETURNS     :  none.
PARAMETERS  :  mask        *C        Search mask (eg. *.* etc.)
VARIABLES   :  drv         I         Drive number
               lastpath    *C        Last path used
CONSTANTS   :  None.
REQD. FILES :  None.
REQD. SUBPG.:  None.
NOTES       :  Modified form the subrt. init_path by Jim Charlton part of the
               XXED program.
USAGE       :  init_path(mask);
****************************************************************************/
init_path(directory,mask)
char *directory;
char *mask;
{
   int drv;
   char lastpath[128];

   drv=Dgetdrv();
   Dgetpath(lastpath,0);
   sprintf(directory,"%c:%s\\%s",'A'+drv,lastpath,mask);
}

/****************************************************************************
SUBROUTINE  : save_graphic           VERSION  : 1.01
AUTHOR      : ROBERT ALLEY           DATE     : 9/2/90
DESCRIPTION : Save a graphic to disk
LOGIC USED  : STEP 1.   Initialise abort flag to false.  While the user has
                        not elected to quit : Call subroutine to
                        get filename.  If they didnt select cancel, open the
                        file, if the file couldnt be opened forma alert 
                        saying so.  Clear error flag.  Clear abort flag if 
                        they elected to continue.  Otherwise set abort flag
                        to an imposssible value.
              STEP 2.   If abort flag isnt 1 (ie. file has been succesfully
                        opened) then loop through each row and each column
                        writting each byte of data to the file.  Close the 
                        file.
PARAMETERS  : None.
VARIABLES   : write       *FIL Pointer to file to write
              loop,outloop I   Loop counters
              abort        I   Return from alert box
              file         *C  Filename to save to.
CONSTANTS   : None.
REQD. FILES : None.
REQD. SUBPG.: None.
NOTES       : None.
USAGE       : save_graphic();
****************************************************************************/
save_graphic(directory)
char *directory;
{
   extern char data[3][40];
   static char file[128]="";
   FILE *write;
   int loop,outloop;
   int abort;

/* STEP 1 */

   abort=0;
   while (abort==0) {
      abort = select(file,directory);
      if (abort ==0) {
         if ( (write=fopen(file,"wb"))==(FILE *)NULL) {
            abort=form_alert(1,"[1][Error opening file][Abort|Continue]");
            if (abort==2) abort=0;
         }
         else
            abort=2;
      }
   }
   
/* STEP 2 */

   if (abort != 1) {
      for(outloop=0; outloop<3;outloop++)
         for(loop=0;loop<40;loop++) {
            putc(data[outloop][loop],write);
         }
      fclose(write);
   }
}

/****************************************************************************
SUBROUTINE  : load_graphic           VERSION  : 1.1
AUTHOR      : ROBERT ALLEY           DATE     : 9/2/90
DESCRIPTION : Load a graphic from disk
LOGIC USED  : STEP 1.   Initialise abort flag to false.  While the user has
                        not elected to quit : Call subroutine to
                        get filename.  If they didnt select cancel, open the
                        file, if the file couldnt be opened forma alert 
                        saying so.  Clear error flag.  Clear abort flag if 
                        they elected to continue.  Otherwise set abort flag
                        to an imposssible value.
              STEP 2.   If abort flag isnt 1 (ie. file has been succesfully
                        opened) then loop through each row and each column
                        reading each byte of data from the file.  Close the 
                        file.
PARAMETERS  : None.
VARIABLES   : write       *FIL Pointer to file to read
              loop,outloop I   Loop counters
              abort        I   Return from alert box
              file         *C  Filename to save to.
CONSTANTS   : None.
REQD. FILES : None.
REQD. SUBPG.: None.
NOTES       : None.
USAGE       : save_graphic();
****************************************************************************/
load_graphic(directory)
char *directory;
{
   extern char data [3][40];
   static char file[128]="";
   FILE *read;
   int loop,outloop;
   int abort;

   abort=0;
   while (abort==0) {
      abort = select(file,directory);
      if (abort ==0) {
         if ( (read=fopen(file,"rb"))==(FILE *)NULL) {
            abort=form_alert(1,"[1][Error opening file][Abort|Continue]");
            if (abort==2) abort=0;
         }
         else
            abort=2;
      }
   }
   if (abort != 1) {
   for(outloop=0; outloop<3;outloop++)
      for(loop=0;loop<40;loop++) {
         data[outloop][loop]=getc(read);
      }  
   fclose(read);
   }
}

/****************************************************************************
SUBROUTINE  :                        VERSION  : 1.0
AUTHOR      : ROBERT ALLEY           DATE     : ??/??/89
DESCRIPTION : 
LOGIC USED  : STEP 1.   Form file selecetion box, if they didnt select CANCEL,
                        clear abort flag, set drive variable based on first
                        character in directory.  Set the current drive.  
                        Clear path variable, copy all but first two characters
                        of directory (eg. A:) to path.  Remove search mask
                        from path.  Set current path.  Return abort state.
                        If they selected CANCEL return 1.
PARAMETERS  : file         *C       filename selected
VARIABLES   : i            I        Loop counter
              abort        I        Abort status
              drive        I        Current drive
              button       I        Button selected in file selection box.
              path         *C       Path to build.
CONSTANTS   : None.
REQD. FILES : None.
REQD. SUBPG.: None.
NOTES       : None.
USAGE       : select(filename);
****************************************************************************/
select(file,directory)
char *file;
char *directory;
{

   int i,abort,drive,button;
   char path[128];

   fsel_input(directory,file,&button);
   if (button != 0) {
      abort=0;
      drive=directory[0];
      drive=toupper(drive)-65;
      Dsetdrv(drive);
      for (i=0;i<64;i++)
         path[i]=0;
      for (i=0;i<64;i++)
         path[i]=directory[i+2];
      i=strlen(path);
      while (path[i] != '\\') {
         path[i] = '\0';
         i--;
      }
      Dsetpath(path);
      return abort;
   }
   else return 1;
 }

