home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / biblioteki / asyncio / src / readasync.c < prev    next >
C/C++ Source or Header  |  1997-06-04  |  1KB  |  60 lines

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