Start of port of AsyncIO library.
[AROS-Contrib.git] / workbench / libs / asyncio / src / WriteAsync.c
blob93b3d0135b720c6941b0de34628c025f62417892
1 #include "async.h"
4 _LIBCALL LONG
5 WriteAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buffer, _REG( d0 ) LONG numBytes )
7 #ifdef ASIO_NOEXTERNALS
8 struct ExecBase *SysBase = file->af_SysBase;
9 #endif
10 LONG totalBytes = 0;
12 /* this takes care of NIL: */
13 if( !file->af_Handler )
15 file->af_Offset = file->af_Buffers[ 0 ];
16 file->af_BytesLeft = file->af_BufferSize;
17 return( numBytes );
20 while( numBytes > file->af_BytesLeft )
22 if( file->af_BytesLeft )
24 CopyMem( buffer, file->af_Offset, file->af_BytesLeft );
26 numBytes -= file->af_BytesLeft;
27 buffer = ( APTR ) ( ( ULONG ) buffer + file->af_BytesLeft );
28 totalBytes += file->af_BytesLeft;
31 if( AS_WaitPacket( file ) < 0 )
33 return( -1 );
36 /* send the current buffer out to disk */
37 AS_SendPacket( file, file->af_Buffers[ file->af_CurrentBuf ] );
39 file->af_CurrentBuf = 1 - file->af_CurrentBuf;
40 file->af_Offset = file->af_Buffers[ file->af_CurrentBuf ];
41 file->af_BytesLeft = file->af_BufferSize;
44 CopyMem( buffer, file->af_Offset, numBytes );
45 file->af_BytesLeft -= numBytes;
46 file->af_Offset += numBytes;
48 return ( totalBytes + numBytes );