Start of port of AsyncIO library.
[AROS-Contrib.git] / workbench / libs / asyncio / src / ReadAsync.c
blob0896ebda4d1c745f2333dd954b1dbbec94838452
1 #include "async.h"
4 _LIBCALL LONG
5 ReadAsync( _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;
11 LONG bytesArrived;
13 /* if we need more bytes than there are in the current buffer, enter the
14 * read loop
17 while( numBytes > file->af_BytesLeft )
19 /* drain buffer */
20 CopyMem( file->af_Offset, buffer, file->af_BytesLeft );
22 numBytes -= file->af_BytesLeft;
23 buffer = ( APTR ) ( ( ULONG ) buffer + file->af_BytesLeft );
24 totalBytes += file->af_BytesLeft;
25 file->af_BytesLeft = 0;
27 bytesArrived = AS_WaitPacket( file );
29 if( bytesArrived <= 0 )
31 if( bytesArrived == 0 )
33 return( totalBytes );
36 return( -1 );
39 /* ask that the buffer be filled */
40 AS_SendPacket( file, file->af_Buffers[ 1 - file->af_CurrentBuf ] );
42 /* in case we tried to seek past EOF */
43 if( file->af_SeekOffset > bytesArrived )
45 file->af_SeekOffset = bytesArrived;
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;
54 CopyMem( file->af_Offset, buffer, numBytes );
55 file->af_BytesLeft -= numBytes;
56 file->af_Offset += numBytes;
58 return( totalBytes + numBytes );