home *** CD-ROM | disk | FTP | other *** search
/ Speccy ClassiX 1998 / Speccy ClassiX 98.iso / amiga_system / the_aminet / dev / c / asyncio.lha / AsyncIO / src / ReadAsync.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-10  |  1.4 KB  |  64 lines

  1. #include "async.h"
  2.  
  3. LibCall LONG
  4. ReadAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buffer, _REG( d0 ) LONG numBytes )
  5. {
  6. #ifdef ASIO_NOEXTERNALS
  7.     struct ExecBase    *SysBase;
  8. #endif
  9.     LONG totalBytes;
  10.     LONG bytesArrived;
  11.  
  12. #ifdef ASIO_NOEXTERNALS
  13.     SysBase = file->af_SysBase;
  14. #endif
  15.     totalBytes = 0;
  16.  
  17.     /* if we need more bytes than there are in the current buffer, enter the
  18.      * read loop
  19.      */
  20.  
  21.     while( numBytes > file->af_BytesLeft )
  22.     {
  23.         /* drain buffer */
  24.         CopyMem( file->af_Offset, buffer, file->af_BytesLeft );
  25.  
  26.         numBytes        -= file->af_BytesLeft;
  27.         buffer            =  ( APTR ) ( ( ULONG ) buffer + file->af_BytesLeft );
  28.         totalBytes        += file->af_BytesLeft;
  29.         file->af_BytesLeft    =  0;
  30.  
  31.         bytesArrived = AS_WaitPacket( file );
  32.  
  33.         if( bytesArrived <= 0 )
  34.         {
  35.             if( bytesArrived == 0 )
  36.             {
  37.                 return( totalBytes );
  38.             }
  39.  
  40.             return( -1 );
  41.         }
  42.  
  43.         /* ask that the buffer be filled */
  44.         AS_SendPacket( file, file->af_Buffers[ 1 - file->af_CurrentBuf ] );
  45.  
  46.         /* in case we tried to seek past EOF */
  47.         if( file->af_SeekOffset > bytesArrived )
  48.         {
  49.             file->af_SeekOffset = bytesArrived;
  50.         }
  51.  
  52.         file->af_Offset        = file->af_Buffers[ file->af_CurrentBuf ] + file->af_SeekOffset;
  53.         file->af_CurrentBuf    = 1 - file->af_CurrentBuf;
  54.         file->af_BytesLeft    = bytesArrived - file->af_SeekOffset;
  55.         file->af_SeekOffset    = 0;
  56.     }
  57.  
  58.     CopyMem( file->af_Offset, buffer, numBytes );
  59.     file->af_BytesLeft    -= numBytes;
  60.     file->af_Offset        += numBytes;
  61.  
  62.     return( totalBytes + numBytes );
  63. }
  64.