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

  1. #include "async.h"
  2.  
  3. _ASM LibCall _ARGS LONG
  4. WriteAsync( _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.  
  11.     /* this takes care of NIL: */
  12.     if( !file->af_Handler )
  13.     {
  14.         file->af_Offset        = file->af_Buffers[ 0 ];
  15.         file->af_BytesLeft    = file->af_BufferSize;
  16.         return( numBytes );
  17.     }
  18.  
  19.     while( numBytes > file->af_BytesLeft )
  20.     {
  21.         if( file->af_BytesLeft )
  22.         {
  23.             CopyMem( buffer, file->af_Offset, file->af_BytesLeft );
  24.  
  25.             numBytes    -= file->af_BytesLeft;
  26.             buffer        =  ( APTR ) ( ( ULONG ) buffer + file->af_BytesLeft );
  27.             totalBytes    += file->af_BytesLeft;
  28.         }
  29.  
  30.         if( AS_WaitPacket( file ) < 0 )
  31.         {
  32.             return( -1 );
  33.         }
  34.  
  35.         /* send the current buffer out to disk */
  36.         AS_SendPacket( file, file->af_Buffers[ file->af_CurrentBuf ] );
  37.  
  38.         file->af_CurrentBuf    = 1 - file->af_CurrentBuf;
  39.         file->af_Offset        = file->af_Buffers[ file->af_CurrentBuf ];
  40.         file->af_BytesLeft    = file->af_BufferSize;
  41.     }
  42.  
  43.     CopyMem( buffer, file->af_Offset, numBytes );
  44.     file->af_BytesLeft    -= numBytes;
  45.     file->af_Offset        += numBytes;
  46.  
  47.     return ( totalBytes + numBytes );
  48. }
  49.