home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / biblioteki / c_library / asyncio / source / readasync.c < prev    next >
C/C++ Source or Header  |  1977-12-31  |  1KB  |  59 lines

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