/***************************************************************************

   Program:    
   File:       MyIO.c
   
   Version:    V1.3
   Date:       06.02.91
   Function:   New versions of gets and puts to use AmigaDOS IO
   
   Copyright:  SciTech Software 1991
   Author:     Andrew C. R. Martin
   Address:    SciTech Software
               23, Stag Leys,
               Ashtead,
               Surrey,
               KT21 2TD.
   Phone:      +44 (0372) 275775
   EMail:      UUCP: cbmuk!cbmuka!scitec!amartin
               JANET: andrew@uk.ac.ox.biop
               
****************************************************************************

   This program is not in the public domain, but it may be freely copied
   and distributed for no charge providing this header is included.
   The code may be modified as required, but any modifications must be
   documented so that the person responsible can be identified. If someone
   else breaks this code, I don't want to be blamed for code that does not
   work! The code may not be sold commercially without prior permission from
   the author, although it may be given away free with commercial products,
   providing it is made clear that this program is free and that the source
   code is provided with the program.

****************************************************************************

   Description:
   ============
   These routines to replace the Lattice gets and puts so direct AmigaDOS i/o
   routines may be used. Note that the string returned by mygets() has
   a '\n' before the '\0' (as with gets()) so it is necessary to call
   terminate() before you can do anything useful.

****************************************************************************

   Usage:
   ======
   mygets(file,string,maxl)
   Input:   struct FileHandle    *file       An AmigaDOS file handle
            int                  maxl        Max length of string
   Output:  char                 *string     A pointer to a string
   Return:  int                              Length of string (including '\0')
   

   myputs(file,string)
   Input:   struct FileHandle    *file       An AmigaDOS file handle
            char                 *string     A pointer to a string
   Return:  int                              Number of characters written


   mygetsbuff(file,string,maxl,iobuff)
   Input:   struct FileHandle    *file       An AmigaDOS file handle
            int                  maxl        Max length of string
   Output:  char                 *string     A pointer to a string
   I/O:     IOBUFF               *iobuff     Buffer for i/o (defined in myio.h)
   Return:  int                              Length of string (including '\0')
   
   iobuff->firstcall should be set to TRUE before the first call.
   
****************************************************************************

   Revision History:
   =================
   V1.1  13.09.91
   Added mygetsbuff()

   V1.2  07.11.91
   Fixed mygetsbuff() to work if there's no return on the last line
   
   V1.3  28.05.92
   ANSIed

***************************************************************************/
#include <exec/types.h>
#include <stdlib.h>
#include <dos.h>
#include <libraries/dosextens.h>

#include "myio.h"

/*------------------------------------------------------------------------*/
mygets(struct FileHandle *file,
       char              *string,
       int               maxl)
{
   int j,
       nret,
       first;
       
   first = 1;
   
   for(j=0;j<maxl-1;j++)
   {
      nret=Read(file, &string[j], 1);
      if(nret<=0)
      {
         if(first)
         {
            j = -1;  /* So the routine returns 0 */
            break;
         }
         else
         {
            string[j] = '\0';
            j--;
            break;
         }
      }
      first = 0;
      if(string[j]=='\n')
      {
         string[j+1] = '\0';
         break;
      }
   }
   return(j+1);
}

/*------------------------------------------------------------------------*/
mygetsbuff(struct   FileHandle *file,
           char                *string,
           int                 maxl,
           IOBUFF              *iobuff)
{
   int j,
       nret;
               
   /* Initialise stuff if this is the first call */
   if(iobuff->firstcall)
   {
      iobuff->firstcall = 0;
      iobuff->ngot      = 0;
      iobuff->bufptr    = 0;
   }
   
   for(j=0;j<maxl-1;j++)
   {
      /* If we haven't got any characters, read characters into the buffer */
      if(!iobuff->ngot)
      {
         nret = Read(file, iobuff->buffer, MYIO_MAXBUFF);
/* V1.2 */
         if(nret == 0)           /* EOF */
         {
            string[j] = '\0';
            return(j);           /* This will be 0 if this is the string start*/
         }
         else if (nret < 0)      /* A real error                              */
            return(nret);        /* Return error                              */
         else
         {
            iobuff->ngot = nret; /* Set to number of chars in our buffer      */
            iobuff->bufptr = 0;  /* Reset pointer to start of buffer          */
         }
      }
      
      /* Copy the character out of the buffer */
      string[j] = iobuff->buffer[(iobuff->bufptr)++];
      iobuff->ngot--;
      
      /* Check for newline character */
      if(string[j] == '\n')
      {
         j++;
         break;
      }
   }
   
   string[j] = '\0';
   
   return(j);
}

/*------------------------------------------------------------------------*/
myputs(struct FileHandle *file,
       char              *string)
{
   int i;
   i=Write(file,string,strlen(string));
   return(i);
}




/***************************************************************************
*                                                                          *
*                 DEMO CODE --- Define DEMO to compile                     *
*                 Assumes a file called "test.dat" exists                  *
*                                                                          *
***************************************************************************/

#ifdef DEMO
#include <stdio.h>

main()
{
   char string[80];
   struct FileHandle *in,*out;
   
   out=(struct FileHandle *)Output();

   if((in=(struct FileHandle *)Open("test.dat",MODE_OLDFILE))==NULL)
   {
      printf("Unable to open test.dat\n");
      exit(1);
   }

   while(mygets(in,string,80))
      myputs(out,string);
      
   Close(in);
}
#endif
